blob: ae030443569df0f3108b8460024dc71ee6f985b4 [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9instr.c: Dealing with instructions of a compiled function
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24
25/////////////////////////////////////////////////////////////////////
26// Following generate_ functions expect the caller to call ga_grow().
27
28#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
29#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
30
31/*
32 * Generate an instruction without arguments.
33 * Returns a pointer to the new instruction, NULL if failed.
34 */
35 isn_T *
36generate_instr(cctx_T *cctx, isntype_T isn_type)
37{
38 garray_T *instr = &cctx->ctx_instr;
39 isn_T *isn;
40
41 RETURN_NULL_IF_SKIP(cctx);
42 if (GA_GROW_FAILS(instr, 1))
43 return NULL;
44 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
45 isn->isn_type = isn_type;
46 isn->isn_lnum = cctx->ctx_lnum + 1;
47 ++instr->ga_len;
48
49 return isn;
50}
51
52/*
53 * Generate an instruction without arguments.
54 * "drop" will be removed from the stack.
55 * Returns a pointer to the new instruction, NULL if failed.
56 */
57 isn_T *
58generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
59{
Bram Moolenaardc7c3662021-12-20 15:04:29 +000060 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +000061 cctx->ctx_type_stack.ga_len -= drop;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000062 return generate_instr(cctx, isn_type);
63}
64
65/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000066 * Generate instruction "isn_type" and put "type" on the type stack,
67 * use "decl_type" for the declared type.
Bram Moolenaardc7c3662021-12-20 15:04:29 +000068 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +000069 static isn_T *
Bram Moolenaar078a4612022-01-04 15:17:03 +000070generate_instr_type2(
71 cctx_T *cctx,
72 isntype_T isn_type,
73 type_T *type,
74 type_T *decl_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000075{
76 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000077
78 if ((isn = generate_instr(cctx, isn_type)) == NULL)
79 return NULL;
80
Bram Moolenaar078a4612022-01-04 15:17:03 +000081 if (push_type_stack2(cctx, type == NULL ? &t_any : type,
82 decl_type == NULL ? &t_any : decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000083 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084
85 return isn;
86}
87
88/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000089 * Generate instruction "isn_type" and put "type" on the type stack.
90 * Uses "any" for the declared type, which works for constants. For declared
91 * variables use generate_instr_type2().
92 */
93 isn_T *
94generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
95{
96 return generate_instr_type2(cctx, isn_type, type, &t_any);
97}
98
99/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000100 * Generate an ISN_DEBUG instruction.
101 */
102 isn_T *
103generate_instr_debug(cctx_T *cctx)
104{
105 isn_T *isn;
106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
107 + cctx->ctx_ufunc->uf_dfunc_idx;
108
109 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
110 return NULL;
111 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
112 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
113 return isn;
114}
115
116/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000117 * Generate an ISN_CONSTRUCT instruction.
118 * The object will have "size" members.
119 */
120 int
121generate_CONSTRUCT(cctx_T *cctx, class_T *cl)
122{
123 isn_T *isn;
124
125 RETURN_OK_IF_SKIP(cctx);
126 if ((isn = generate_instr(cctx, ISN_CONSTRUCT)) == NULL)
127 return FAIL;
128 isn->isn_arg.construct.construct_size = sizeof(object_T)
129 + cl->class_obj_member_count * sizeof(typval_T);
130 isn->isn_arg.construct.construct_class = cl;
131 return OK;
132}
133
134/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000135 * Generate ISN_GET_OBJ_MEMBER - access member of object at bottom of stack by
136 * index.
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000137 */
138 int
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000139generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000140{
141 RETURN_OK_IF_SKIP(cctx);
142
143 // drop the object type
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000144 isn_T *isn = generate_instr_drop(cctx, ISN_GET_OBJ_MEMBER, 1);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000145 if (isn == NULL)
146 return FAIL;
147
148 isn->isn_arg.number = idx;
149 return push_type_stack2(cctx, type, &t_any);
150}
151
152/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000153 * Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack
154 * by index.
155 */
156 int
157generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type)
158{
159 RETURN_OK_IF_SKIP(cctx);
160
161 // drop the object type
162 isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1);
163 if (isn == NULL)
164 return FAIL;
165
166 isn->isn_arg.classmember.cm_class = itf;
167 ++itf->class_refcount;
168 isn->isn_arg.classmember.cm_idx = idx;
169 return push_type_stack2(cctx, type, &t_any);
170}
171
172/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000173 * Generate ISN_STORE_THIS - store value in member of "this" object with member
174 * index "idx".
175 */
176 int
177generate_STORE_THIS(cctx_T *cctx, int idx)
178{
179 RETURN_OK_IF_SKIP(cctx);
180
181 // drop the value type
182 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
183 if (isn == NULL)
184 return FAIL;
185
186 isn->isn_arg.number = idx;
187 return OK;
188}
189
190/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000191 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
192 * But only for simple types.
193 * When "tolerant" is TRUE convert most types to string, e.g. a List.
194 */
195 int
196may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
197{
198 isn_T *isn;
199 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000200 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201
202 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000203 type = get_type_on_stack(cctx, -1 - offset);
204 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 {
206 // nothing to be done
207 case VAR_STRING: return OK;
208
209 // conversion possible
210 case VAR_SPECIAL:
211 case VAR_BOOL:
212 case VAR_NUMBER:
213 case VAR_FLOAT:
214 break;
215
216 // conversion possible (with runtime check)
217 case VAR_ANY:
218 case VAR_UNKNOWN:
219 isntype = ISN_2STRING_ANY;
220 break;
221
222 // conversion possible when tolerant
223 case VAR_LIST:
224 if (tolerant)
225 {
226 isntype = ISN_2STRING_ANY;
227 break;
228 }
229 // FALLTHROUGH
230
231 // conversion not possible
232 case VAR_VOID:
233 case VAR_BLOB:
234 case VAR_FUNC:
235 case VAR_PARTIAL:
236 case VAR_DICT:
237 case VAR_JOB:
238 case VAR_CHANNEL:
239 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000240 case VAR_CLASS:
241 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000242 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000243 return FAIL;
244 }
245
Bram Moolenaar078a4612022-01-04 15:17:03 +0000246 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000247 if ((isn = generate_instr(cctx, isntype)) == NULL)
248 return FAIL;
249 isn->isn_arg.tostring.offset = offset;
250 isn->isn_arg.tostring.tolerant = tolerant;
251
252 return OK;
253}
254
255 static int
256check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
257{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000258 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
259 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000260 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000261 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000262 {
263 if (*op == '+')
264 emsg(_(e_wrong_argument_type_for_plus));
265 else
266 semsg(_(e_char_requires_number_or_float_arguments), *op);
267 return FAIL;
268 }
269 return OK;
270}
271
272/*
273 * Generate instruction for "+". For a list this creates a new list.
274 */
275 int
276generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000277 cctx_T *cctx,
278 vartype_T vartype,
279 type_T *type1,
280 type_T *type2,
281 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000282{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283 isn_T *isn = generate_instr_drop(cctx,
284 vartype == VAR_NUMBER ? ISN_OPNR
285 : vartype == VAR_LIST ? ISN_ADDLIST
286 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000287 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000288 : ISN_OPANY, 1);
289
290 if (vartype != VAR_LIST && vartype != VAR_BLOB
291 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000292 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000293 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000294 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000295 && check_number_or_float(
296 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
297 return FAIL;
298
299 if (isn != NULL)
300 {
301 if (isn->isn_type == ISN_ADDLIST)
302 isn->isn_arg.op.op_type = expr_type;
303 else
304 isn->isn_arg.op.op_type = EXPR_ADD;
305 }
306
307 // When concatenating two lists with different member types the member type
308 // becomes "any".
309 if (vartype == VAR_LIST
310 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
311 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000312 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000313
314 return isn == NULL ? FAIL : OK;
315}
316
317/*
318 * Get the type to use for an instruction for an operation on "type1" and
319 * "type2". If they are matching use a type-specific instruction. Otherwise
320 * fall back to runtime type checking.
321 */
322 vartype_T
323operator_type(type_T *type1, type_T *type2)
324{
325 if (type1->tt_type == type2->tt_type
326 && (type1->tt_type == VAR_NUMBER
327 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000328 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000329 || type1->tt_type == VAR_BLOB))
330 return type1->tt_type;
331 return VAR_ANY;
332}
333
334/*
335 * Generate an instruction with two arguments. The instruction depends on the
336 * type of the arguments.
337 */
338 int
339generate_two_op(cctx_T *cctx, char_u *op)
340{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000341 type_T *type1;
342 type_T *type2;
343 vartype_T vartype;
344 isn_T *isn;
345
346 RETURN_OK_IF_SKIP(cctx);
347
348 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000349 type1 = get_type_on_stack(cctx, 1);
350 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000351 vartype = operator_type(type1, type2);
352
353 switch (*op)
354 {
355 case '+':
356 if (generate_add_instr(cctx, vartype, type1, type2,
357 EXPR_COPY) == FAIL)
358 return FAIL;
359 break;
360
361 case '-':
362 case '*':
363 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
364 op) == FAIL)
365 return FAIL;
366 if (vartype == VAR_NUMBER)
367 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000368 else if (vartype == VAR_FLOAT)
369 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000370 else
371 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
372 if (isn != NULL)
373 isn->isn_arg.op.op_type = *op == '*'
374 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
375 break;
376
377 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000378 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 && type1->tt_type != VAR_NUMBER)
380 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000381 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000382 && type2->tt_type != VAR_NUMBER))
383 {
384 emsg(_(e_percent_requires_number_arguments));
385 return FAIL;
386 }
387 isn = generate_instr_drop(cctx,
388 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
389 if (isn != NULL)
390 isn->isn_arg.op.op_type = EXPR_REM;
391 break;
392 }
393
394 // correct type of result
395 if (vartype == VAR_ANY)
396 {
397 type_T *type = &t_any;
398
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000399 // float+number and number+float results in float
400 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100401 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000402 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000403 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404 }
405
406 return OK;
407}
408
409/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000410 * Get the instruction to use for comparing two values with specified types.
411 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000412 * Return ISN_DROP when failed.
413 */
414 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000415get_compare_isn(
416 exprtype_T exprtype,
417 typval_T *tv1,
418 typval_T *tv2,
419 type_T *type1,
420 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000421{
422 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000423 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
424 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000425
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000426 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000427 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000428 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000429 {
430 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
431 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
432 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
433 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
434 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
435 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
436 case VAR_LIST: isntype = ISN_COMPARELIST; break;
437 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
438 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000439 case VAR_CLASS: isntype = ISN_COMPARECLASS; break;
440 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000441 default: isntype = ISN_COMPAREANY; break;
442 }
443 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000444 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
445 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
446 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
447 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
448 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000449 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000450 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000451 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000452 if ((vartype1 == VAR_SPECIAL
453 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
454 : type1 == &t_none)
455 && vartype2 != VAR_STRING)
456 || (vartype2 == VAR_SPECIAL
457 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
458 : type2 == &t_none)
459 && vartype1 != VAR_STRING))
460 {
461 semsg(_(e_cannot_compare_str_with_str),
462 vartype_name(vartype1), vartype_name(vartype2));
463 return ISN_DROP;
464 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000465 // although comparing null with number, float or bool is not useful, we
466 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000467 isntype = ISN_COMPARENULL;
468 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469
470 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
471 && (isntype == ISN_COMPAREBOOL
472 || isntype == ISN_COMPARESPECIAL
473 || isntype == ISN_COMPARENR
474 || isntype == ISN_COMPAREFLOAT))
475 {
476 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000477 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000478 return ISN_DROP;
479 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000480 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
481 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
482 && (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
483 {
484 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
485 return ISN_DROP;
486 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000487 if (isntype == ISN_DROP
488 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000489 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
490 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000492 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000493 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
494 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000495 {
496 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000497 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000498 return ISN_DROP;
499 }
500 return isntype;
501}
502
503 int
504check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
505{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000506 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000507 return FAIL;
508 return OK;
509}
510
511/*
512 * Generate an ISN_COMPARE* instruction with a boolean result.
513 */
514 int
515generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
516{
517 isntype_T isntype;
518 isn_T *isn;
519 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000520
521 RETURN_OK_IF_SKIP(cctx);
522
523 // Get the known type of the two items on the stack. If they are matching
524 // use a type-specific instruction. Otherwise fall back to runtime type
525 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000526 isntype = get_compare_isn(exprtype, NULL, NULL,
527 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528 if (isntype == ISN_DROP)
529 return FAIL;
530
531 if ((isn = generate_instr(cctx, isntype)) == NULL)
532 return FAIL;
533 isn->isn_arg.op.op_type = exprtype;
534 isn->isn_arg.op.op_ic = ic;
535
536 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100537 --stack->ga_len;
538 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539
540 return OK;
541}
542
543/*
LemonBoy372bcce2022-04-25 12:43:20 +0100544 * Generate an ISN_CONCAT instruction.
545 * "count" is the number of stack elements to join together and it must be
546 * greater or equal to one.
547 * The caller ensures all the "count" elements on the stack have the right type.
548 */
549 int
550generate_CONCAT(cctx_T *cctx, int count)
551{
552 isn_T *isn;
553 garray_T *stack = &cctx->ctx_type_stack;
554
555 RETURN_OK_IF_SKIP(cctx);
556
LemonBoy372bcce2022-04-25 12:43:20 +0100557 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
558 return FAIL;
559 isn->isn_arg.number = count;
560
561 // drop the argument types
562 stack->ga_len -= count - 1;
563
564 return OK;
565}
566
567/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568 * Generate an ISN_2BOOL instruction.
569 * "offset" is the offset in the type stack.
570 */
571 int
572generate_2BOOL(cctx_T *cctx, int invert, int offset)
573{
574 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000575
576 RETURN_OK_IF_SKIP(cctx);
577 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
578 return FAIL;
579 isn->isn_arg.tobool.invert = invert;
580 isn->isn_arg.tobool.offset = offset;
581
582 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000583 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000584
585 return OK;
586}
587
588/*
589 * Generate an ISN_COND2BOOL instruction.
590 */
591 int
592generate_COND2BOOL(cctx_T *cctx)
593{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000594 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100595 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000596 return FAIL;
597
598 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000599 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600
601 return OK;
602}
603
604 int
605generate_TYPECHECK(
606 cctx_T *cctx,
607 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000608 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000609 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100610 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000611 int argidx)
612{
613 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614
615 RETURN_OK_IF_SKIP(cctx);
616 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
617 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000618 type_T *tt;
619 if (expected->tt_type == VAR_FLOAT && number_ok)
620 {
621 // always allocate, also for static types
622 tt = ALLOC_ONE(type_T);
623 if (tt != NULL)
624 {
625 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000626 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000627 tt->tt_flags |= TTFLAG_NUMBER_OK;
628 }
629 }
630 else
631 tt = alloc_type(expected);
632
633 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000634 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100635 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
637
638 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000639 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000640
641 return OK;
642}
643
644 int
645generate_SETTYPE(
646 cctx_T *cctx,
647 type_T *expected)
648{
649 isn_T *isn;
650
651 RETURN_OK_IF_SKIP(cctx);
652 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
653 return FAIL;
654 isn->isn_arg.type.ct_type = alloc_type(expected);
655 return OK;
656}
657
658/*
659 * Generate a PUSH instruction for "tv".
660 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000661 */
662 int
663generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
664{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100665 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000666 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100667 case VAR_BOOL:
668 generate_PUSHBOOL(cctx, tv->vval.v_number);
669 break;
670 case VAR_SPECIAL:
671 generate_PUSHSPEC(cctx, tv->vval.v_number);
672 break;
673 case VAR_NUMBER:
674 generate_PUSHNR(cctx, tv->vval.v_number);
675 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100676 case VAR_FLOAT:
677 generate_PUSHF(cctx, tv->vval.v_float);
678 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100679 case VAR_BLOB:
680 generate_PUSHBLOB(cctx, tv->vval.v_blob);
681 tv->vval.v_blob = NULL;
682 break;
683 case VAR_LIST:
684 if (tv->vval.v_list != NULL)
685 iemsg("non-empty list constant not supported");
686 generate_NEWLIST(cctx, 0, TRUE);
687 break;
688 case VAR_DICT:
689 if (tv->vval.v_dict != NULL)
690 iemsg("non-empty dict constant not supported");
691 generate_NEWDICT(cctx, 0, TRUE);
692 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000693#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100694 case VAR_JOB:
695 if (tv->vval.v_job != NULL)
696 iemsg("non-null job constant not supported");
697 generate_PUSHJOB(cctx);
698 break;
699 case VAR_CHANNEL:
700 if (tv->vval.v_channel != NULL)
701 iemsg("non-null channel constant not supported");
702 generate_PUSHCHANNEL(cctx);
703 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000704#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100705 case VAR_FUNC:
706 if (tv->vval.v_string != NULL)
707 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100708 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100709 break;
710 case VAR_PARTIAL:
711 if (tv->vval.v_partial != NULL)
712 iemsg("non-null partial constant not supported");
713 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
714 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000715 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100716 break;
717 case VAR_STRING:
718 generate_PUSHS(cctx, &tv->vval.v_string);
719 tv->vval.v_string = NULL;
720 break;
721 default:
722 siemsg("constant type %d not supported", tv->v_type);
723 clear_tv(tv);
724 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000725 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100726 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000727 return OK;
728}
729
730/*
731 * Generate an ISN_PUSHNR instruction.
732 */
733 int
734generate_PUSHNR(cctx_T *cctx, varnumber_T number)
735{
736 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737
738 RETURN_OK_IF_SKIP(cctx);
739 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
740 return FAIL;
741 isn->isn_arg.number = number;
742
743 if (number == 0 || number == 1)
744 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000745 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746 return OK;
747}
748
749/*
750 * Generate an ISN_PUSHBOOL instruction.
751 */
752 int
753generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
754{
755 isn_T *isn;
756
757 RETURN_OK_IF_SKIP(cctx);
758 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
759 return FAIL;
760 isn->isn_arg.number = number;
761
762 return OK;
763}
764
765/*
766 * Generate an ISN_PUSHSPEC instruction.
767 */
768 int
769generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
770{
771 isn_T *isn;
772
773 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000774 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
775 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000776 return FAIL;
777 isn->isn_arg.number = number;
778
779 return OK;
780}
781
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782/*
783 * Generate an ISN_PUSHF instruction.
784 */
785 int
786generate_PUSHF(cctx_T *cctx, float_T fnumber)
787{
788 isn_T *isn;
789
790 RETURN_OK_IF_SKIP(cctx);
791 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
792 return FAIL;
793 isn->isn_arg.fnumber = fnumber;
794
795 return OK;
796}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797
798/*
799 * Generate an ISN_PUSHS instruction.
800 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100801 * Note that if "str" is used in the instruction OK is returned and "*str" is
802 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000803 */
804 int
805generate_PUSHS(cctx_T *cctx, char_u **str)
806{
807 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100808 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000809
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100810 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000811 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100812 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
813 ret = FAIL;
814 else
815 {
816 isn->isn_arg.string = str == NULL ? NULL : *str;
817 return OK;
818 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100820 if (str != NULL)
821 VIM_CLEAR(*str);
822 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000823}
824
825/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000826 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827 */
828 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000829generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000830{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000831 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100832#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100833 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000834 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000835 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100836#else
837 emsg(_(e_channel_job_feature_not_available));
838 return FAIL;
839#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000840}
841
842/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000843 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000844 */
845 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000846generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000847{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000848 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100849#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100850 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000852 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100853#else
854 emsg(_(e_channel_job_feature_not_available));
855 return FAIL;
856#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857}
858
859/*
860 * Generate an ISN_PUSHBLOB instruction.
861 * Consumes "blob".
862 */
863 int
864generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
865{
866 isn_T *isn;
867
868 RETURN_OK_IF_SKIP(cctx);
869 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
870 return FAIL;
871 isn->isn_arg.blob = blob;
872
873 return OK;
874}
875
876/*
877 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100878 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
879 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000880 */
881 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100882generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883{
884 isn_T *isn;
885 char_u *funcname;
886
887 RETURN_OK_IF_SKIP(cctx);
888 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
889 return FAIL;
890 if (name == NULL)
891 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100892 else if (!may_prefix
893 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000894 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895 funcname = vim_strsave(name);
896 else
897 {
898 funcname = alloc(STRLEN(name) + 3);
899 if (funcname != NULL)
900 {
901 STRCPY(funcname, "g:");
902 STRCPY(funcname + 2, name);
903 }
904 }
905
906 isn->isn_arg.string = funcname;
907 return OK;
908}
909
910/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000911 * Generate an ISN_AUTOLOAD instruction.
912 */
913 int
914generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
915{
916 isn_T *isn;
917
918 RETURN_OK_IF_SKIP(cctx);
919 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
920 return FAIL;
921 isn->isn_arg.string = vim_strsave(name);
922 if (isn->isn_arg.string == NULL)
923 return FAIL;
924 return OK;
925}
926
927/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928 * Generate an ISN_GETITEM instruction with "index".
929 * "with_op" is TRUE for "+=" and other operators, the stack has the current
930 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100931 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000932 */
933 int
934generate_GETITEM(cctx_T *cctx, int index, int with_op)
935{
936 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000937 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938 type_T *item_type = &t_any;
939
940 RETURN_OK_IF_SKIP(cctx);
941
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000942 item_type = type->tt_member;
943 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
944 return FAIL;
945 isn->isn_arg.getitem.gi_index = index;
946 isn->isn_arg.getitem.gi_with_op = with_op;
947
948 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000949 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950}
951
952/*
953 * Generate an ISN_SLICE instruction with "count".
954 */
955 int
956generate_SLICE(cctx_T *cctx, int count)
957{
958 isn_T *isn;
959
960 RETURN_OK_IF_SKIP(cctx);
961 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
962 return FAIL;
963 isn->isn_arg.number = count;
964 return OK;
965}
966
967/*
968 * Generate an ISN_CHECKLEN instruction with "min_len".
969 */
970 int
971generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
972{
973 isn_T *isn;
974
975 RETURN_OK_IF_SKIP(cctx);
976
977 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
978 return FAIL;
979 isn->isn_arg.checklen.cl_min_len = min_len;
980 isn->isn_arg.checklen.cl_more_OK = more_OK;
981
982 return OK;
983}
984
985/*
986 * Generate an ISN_STORE instruction.
987 */
988 int
989generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
990{
991 isn_T *isn;
992
993 RETURN_OK_IF_SKIP(cctx);
994 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
995 return FAIL;
996 if (name != NULL)
997 isn->isn_arg.string = vim_strsave(name);
998 else
999 isn->isn_arg.number = idx;
1000
1001 return OK;
1002}
1003
1004/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001005 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1006 * ("load" == FALSE) instruction.
1007 */
1008 int
1009generate_CLASSMEMBER(
1010 cctx_T *cctx,
1011 int load,
1012 class_T *cl,
1013 int idx)
1014{
1015 isn_T *isn;
1016
1017 RETURN_OK_IF_SKIP(cctx);
1018 if (load)
1019 {
1020 ocmember_T *m = &cl->class_class_members[idx];
1021 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1022 }
1023 else
1024 {
1025 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1026 }
1027 if (isn == NULL)
1028 return FAIL;
1029 isn->isn_arg.classmember.cm_class = cl;
1030 ++cl->class_refcount;
1031 isn->isn_arg.classmember.cm_idx = idx;
1032
1033 return OK;
1034}
1035
1036/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001037 * Generate an ISN_STOREOUTER instruction.
1038 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001039 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001040generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001041{
1042 isn_T *isn;
1043
1044 RETURN_OK_IF_SKIP(cctx);
1045 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1046 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001047 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1048 {
1049 // Store a variable defined in a loop. A copy will be made at the end
1050 // of the loop. TODO: how about deeper nesting?
1051 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1052 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1053 }
1054 else
1055 {
1056 isn->isn_arg.outer.outer_idx = idx;
1057 isn->isn_arg.outer.outer_depth = level;
1058 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001059
1060 return OK;
1061}
1062
1063/*
1064 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1065 */
1066 int
1067generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1068{
1069 isn_T *isn;
1070
1071 RETURN_OK_IF_SKIP(cctx);
1072 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1073 return FAIL;
1074 isn->isn_arg.storenr.stnr_idx = idx;
1075 isn->isn_arg.storenr.stnr_val = value;
1076
1077 return OK;
1078}
1079
1080/*
1081 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1082 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001083 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001084generate_STOREOPT(
1085 cctx_T *cctx,
1086 isntype_T isn_type,
1087 char_u *name,
1088 int opt_flags)
1089{
1090 isn_T *isn;
1091
1092 RETURN_OK_IF_SKIP(cctx);
1093 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1094 return FAIL;
1095 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1096 isn->isn_arg.storeopt.so_flags = opt_flags;
1097
1098 return OK;
1099}
1100
1101/*
1102 * Generate an ISN_LOAD or similar instruction.
1103 */
1104 int
1105generate_LOAD(
1106 cctx_T *cctx,
1107 isntype_T isn_type,
1108 int idx,
1109 char_u *name,
1110 type_T *type)
1111{
1112 isn_T *isn;
1113
1114 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001115 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001116 return FAIL;
1117 if (name != NULL)
1118 isn->isn_arg.string = vim_strsave(name);
1119 else
1120 isn->isn_arg.number = idx;
1121
1122 return OK;
1123}
1124
1125/*
1126 * Generate an ISN_LOADOUTER instruction
1127 */
1128 int
1129generate_LOADOUTER(
1130 cctx_T *cctx,
1131 int idx,
1132 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001133 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001134 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001135 type_T *type)
1136{
1137 isn_T *isn;
1138
1139 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001140 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001142 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1143 {
1144 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001145 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001146 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001147 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001148 }
1149 else
1150 {
1151 isn->isn_arg.outer.outer_idx = idx;
1152 isn->isn_arg.outer.outer_depth = nesting;
1153 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154
1155 return OK;
1156}
1157
1158/*
1159 * Generate an ISN_LOADV instruction for v:var.
1160 */
1161 int
1162generate_LOADV(
1163 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001164 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165{
1166 int di_flags;
1167 int vidx = find_vim_var(name, &di_flags);
1168 type_T *type;
1169
1170 RETURN_OK_IF_SKIP(cctx);
1171 if (vidx < 0)
1172 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001173 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001174 return FAIL;
1175 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001176 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001177 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1178}
1179
1180/*
1181 * Generate an ISN_UNLET instruction.
1182 */
1183 int
1184generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1185{
1186 isn_T *isn;
1187
1188 RETURN_OK_IF_SKIP(cctx);
1189 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1192 isn->isn_arg.unlet.ul_forceit = forceit;
1193
1194 return OK;
1195}
1196
1197/*
1198 * Generate an ISN_LOCKCONST instruction.
1199 */
1200 int
1201generate_LOCKCONST(cctx_T *cctx)
1202{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001203 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001204 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001205 return FAIL;
1206 return OK;
1207}
1208
1209/*
1210 * Generate an ISN_LOADS instruction.
1211 */
1212 int
1213generate_OLDSCRIPT(
1214 cctx_T *cctx,
1215 isntype_T isn_type,
1216 char_u *name,
1217 int sid,
1218 type_T *type)
1219{
1220 isn_T *isn;
1221
1222 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001223 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001224 isn = generate_instr_type(cctx, isn_type, type);
1225 else
1226 isn = generate_instr_drop(cctx, isn_type, 1);
1227 if (isn == NULL)
1228 return FAIL;
1229 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1230 isn->isn_arg.loadstore.ls_sid = sid;
1231
1232 return OK;
1233}
1234
1235/*
1236 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1237 */
1238 int
1239generate_VIM9SCRIPT(
1240 cctx_T *cctx,
1241 isntype_T isn_type,
1242 int sid,
1243 int idx,
1244 type_T *type)
1245{
1246 isn_T *isn;
1247 scriptref_T *sref;
1248 scriptitem_T *si = SCRIPT_ITEM(sid);
1249
1250 RETURN_OK_IF_SKIP(cctx);
1251 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001252 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001253 else
1254 isn = generate_instr_drop(cctx, isn_type, 1);
1255 if (isn == NULL)
1256 return FAIL;
1257
1258 // This requires three arguments, which doesn't fit in an instruction, thus
1259 // we need to allocate a struct for this.
1260 sref = ALLOC_ONE(scriptref_T);
1261 if (sref == NULL)
1262 return FAIL;
1263 isn->isn_arg.script.scriptref = sref;
1264 sref->sref_sid = sid;
1265 sref->sref_idx = idx;
1266 sref->sref_seq = si->sn_script_seq;
1267 sref->sref_type = type;
1268 return OK;
1269}
1270
1271/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001272 * Generate an ISN_NEWLIST instruction for "count" items.
1273 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001274 */
1275 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001276generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001277{
1278 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001279 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001280 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001281 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282
1283 RETURN_OK_IF_SKIP(cctx);
1284 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1285 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001286 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001287
Bram Moolenaar078a4612022-01-04 15:17:03 +00001288 // Get the member type and the declared member type from all the items on
1289 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001290 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001291 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001292 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001293
1294 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001295 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001296
1297 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001298 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299}
1300
1301/*
1302 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001303 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304 */
1305 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001306generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001307{
1308 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001309 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001310 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001311 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001312
1313 RETURN_OK_IF_SKIP(cctx);
1314 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1315 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001316 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001317
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001318 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001319 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001320 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001321
1322 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001323 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001324
1325 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001326 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001327}
1328
1329/*
1330 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001331 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332 */
1333 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001334generate_FUNCREF(
1335 cctx_T *cctx,
1336 ufunc_T *ufunc,
1337 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001339 isn_T *isn;
1340 type_T *type;
1341 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001342 loopvarinfo_T loopinfo;
1343 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344
1345 RETURN_OK_IF_SKIP(cctx);
1346 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1347 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001348 if (isnp != NULL)
1349 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001350
Bram Moolenaarcc341812022-09-19 15:54:34 +01001351 has_vars = get_loop_var_info(cctx, &loopinfo);
1352 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001353 {
1354 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1355 if (extra == NULL)
1356 return FAIL;
1357 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001358 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001359 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001361 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001362 else
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001363 {
1364 if (isnp == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
1365 // compile the function now, we need the uf_dfunc_idx value
1366 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001367 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001368 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001369
1370 // Reserve an extra variable to keep track of the number of closures
1371 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372 cctx->ctx_has_closure = 1;
1373
1374 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001375 // the nested context, including this one. But not a function defined at
1376 // the script level.
1377 if ((ufunc->uf_flags & FC_CLOSURE)
1378 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001379 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1380
Bram Moolenaar078a4612022-01-04 15:17:03 +00001381 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1382 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001383}
1384
1385/*
1386 * Generate an ISN_NEWFUNC instruction.
1387 * "lambda_name" and "func_name" must be in allocated memory and will be
1388 * consumed.
1389 */
1390 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001391generate_NEWFUNC(
1392 cctx_T *cctx,
1393 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001394 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001395{
1396 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001397 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001398
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001399 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001400 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001401 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1402 ret = FAIL;
1403 else
1404 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001405 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1406
1407 if (arg == NULL)
1408 ret = FAIL;
1409 else
1410 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001411 // Reserve an extra variable to keep track of the number of
1412 // closures created.
1413 cctx->ctx_has_closure = 1;
1414
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001415 isn->isn_arg.newfunc.nf_arg = arg;
1416 arg->nfa_lambda = lambda_name;
1417 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001418 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001419 return OK;
1420 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001421 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001422 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001423 vim_free(lambda_name);
1424 vim_free(func_name);
1425 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001426}
1427
1428/*
1429 * Generate an ISN_DEF instruction: list functions
1430 */
1431 int
1432generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1433{
1434 isn_T *isn;
1435
1436 RETURN_OK_IF_SKIP(cctx);
1437 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1438 return FAIL;
1439 if (len > 0)
1440 {
1441 isn->isn_arg.string = vim_strnsave(name, len);
1442 if (isn->isn_arg.string == NULL)
1443 return FAIL;
1444 }
1445 return OK;
1446}
1447
1448/*
1449 * Generate an ISN_JUMP instruction.
1450 */
1451 int
1452generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1453{
1454 isn_T *isn;
1455 garray_T *stack = &cctx->ctx_type_stack;
1456
1457 RETURN_OK_IF_SKIP(cctx);
1458 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1459 return FAIL;
1460 isn->isn_arg.jump.jump_when = when;
1461 isn->isn_arg.jump.jump_where = where;
1462
1463 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1464 --stack->ga_len;
1465
1466 return OK;
1467}
1468
1469/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001470 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1471 */
1472 int
1473generate_WHILE(cctx_T *cctx, int funcref_idx)
1474{
1475 isn_T *isn;
1476 garray_T *stack = &cctx->ctx_type_stack;
1477
1478 RETURN_OK_IF_SKIP(cctx);
1479 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1480 return FAIL;
1481 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1482 isn->isn_arg.whileloop.while_end = 0; // filled in later
1483
1484 if (stack->ga_len > 0)
1485 --stack->ga_len;
1486
1487 return OK;
1488}
1489
1490/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001491 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001492 */
1493 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001494generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001495{
1496 isn_T *isn;
1497
1498 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001499 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001500 return FAIL;
1501 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1502 // jump_where is set later
1503 return OK;
1504}
1505
1506 int
1507generate_FOR(cctx_T *cctx, int loop_idx)
1508{
1509 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001510
1511 RETURN_OK_IF_SKIP(cctx);
1512 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1513 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001514 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001515
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001516 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001517 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001518}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001519
1520 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001521generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001522{
1523 isn_T *isn;
1524
1525 RETURN_OK_IF_SKIP(cctx);
1526 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1527 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001528 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1529 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1530 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001531 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001532 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001533 return OK;
1534}
1535
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001536/*
1537 * Generate an ISN_TRYCONT instruction.
1538 */
1539 int
1540generate_TRYCONT(cctx_T *cctx, int levels, int where)
1541{
1542 isn_T *isn;
1543
1544 RETURN_OK_IF_SKIP(cctx);
1545 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1546 return FAIL;
1547 isn->isn_arg.trycont.tct_levels = levels;
1548 isn->isn_arg.trycont.tct_where = where;
1549
1550 return OK;
1551}
1552
Bram Moolenaar16900322022-09-08 19:51:45 +01001553/*
1554 * Check "argount" arguments and their types on the type stack.
1555 * Give an error and return FAIL if something is wrong.
1556 * When "method_call" is NULL no code is generated.
1557 */
1558 int
1559check_internal_func_args(
1560 cctx_T *cctx,
1561 int func_idx,
1562 int argcount,
1563 int method_call,
1564 type2_T **argtypes,
1565 type2_T *shuffled_argtypes)
1566{
1567 garray_T *stack = &cctx->ctx_type_stack;
1568 int argoff = check_internal_func(func_idx, argcount);
1569
1570 if (argoff < 0)
1571 return FAIL;
1572
1573 if (method_call && argoff > 1)
1574 {
1575 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1576
1577 if (isn == NULL)
1578 return FAIL;
1579 isn->isn_arg.shuffle.shfl_item = argcount;
1580 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1581 }
1582
1583 if (argcount > 0)
1584 {
1585 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1586
1587 // Check the types of the arguments.
1588 if (method_call && argoff > 1)
1589 {
1590 int i;
1591
1592 for (i = 0; i < argcount; ++i)
1593 shuffled_argtypes[i] = (i < argoff - 1)
1594 ? typep[i + 1]
1595 : (i == argoff - 1) ? typep[0] : typep[i];
1596 *argtypes = shuffled_argtypes;
1597 }
1598 else
1599 {
1600 int i;
1601
1602 for (i = 0; i < argcount; ++i)
1603 shuffled_argtypes[i] = typep[i];
1604 *argtypes = shuffled_argtypes;
1605 }
1606 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1607 cctx) == FAIL)
1608 return FAIL;
1609 }
1610 return OK;
1611}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001612
1613/*
1614 * Generate an ISN_BCALL instruction.
1615 * "method_call" is TRUE for "value->method()"
1616 * Return FAIL if the number of arguments is wrong.
1617 */
1618 int
1619generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1620{
1621 isn_T *isn;
1622 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001623 type2_T *argtypes = NULL;
1624 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1625 type2_T *maptype = NULL;
1626 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001627 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001628
1629 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001630
1631 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1632 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 return FAIL;
1634
Bram Moolenaar16900322022-09-08 19:51:45 +01001635 if (internal_func_is_map(func_idx))
1636 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001637
1638 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1639 return FAIL;
1640 isn->isn_arg.bfunc.cbf_idx = func_idx;
1641 isn->isn_arg.bfunc.cbf_argcount = argcount;
1642
1643 // Drop the argument types and push the return type.
1644 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001645 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1646 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001647 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001648 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001649
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001650 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1651 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001652 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001653 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001654
1655 return OK;
1656}
1657
1658/*
1659 * Generate an ISN_LISTAPPEND instruction. Works like add().
1660 * Argument count is already checked.
1661 */
1662 int
1663generate_LISTAPPEND(cctx_T *cctx)
1664{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001665 type_T *list_type;
1666 type_T *item_type;
1667 type_T *expected;
1668
1669 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001670 // For checking the item type we use the declared type of the list and the
1671 // current type of the added item, adding a string to [1, 2] is OK.
1672 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001673 if (arg_type_modifiable(list_type, 1) == FAIL)
1674 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001675 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001676 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001677 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001678 return FAIL;
1679
1680 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1681 return FAIL;
1682
Bram Moolenaar078a4612022-01-04 15:17:03 +00001683 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001684 return OK;
1685}
1686
1687/*
1688 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1689 * Argument count is already checked.
1690 */
1691 int
1692generate_BLOBAPPEND(cctx_T *cctx)
1693{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001694 type_T *item_type;
1695
Bram Moolenaarfa103972022-09-29 19:14:42 +01001696 // Caller already checked that blob_type is a blob, check it is modifiable.
1697 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1698 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001699 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001700 if (need_type(item_type, &t_number, FALSE,
1701 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001702 return FAIL;
1703
1704 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1705 return FAIL;
1706
Bram Moolenaar078a4612022-01-04 15:17:03 +00001707 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001708 return OK;
1709}
1710
1711/*
1712 * Generate an ISN_DCALL or ISN_UCALL instruction.
1713 * Return FAIL if the number of arguments is wrong.
1714 */
1715 int
1716generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1717{
1718 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001719 int regular_args = ufunc->uf_args.ga_len;
1720 int argcount = pushed_argcount;
1721
1722 RETURN_OK_IF_SKIP(cctx);
1723 if (argcount > regular_args && !has_varargs(ufunc))
1724 {
1725 semsg(_(e_too_many_arguments_for_function_str),
1726 printable_func_name(ufunc));
1727 return FAIL;
1728 }
1729 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1730 {
1731 semsg(_(e_not_enough_arguments_for_function_str),
1732 printable_func_name(ufunc));
1733 return FAIL;
1734 }
1735
1736 if (ufunc->uf_def_status != UF_NOT_COMPILED
1737 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1738 {
1739 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001740 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001741
1742 for (i = 0; i < argcount; ++i)
1743 {
1744 type_T *expected;
1745 type_T *actual;
1746
Bram Moolenaar078a4612022-01-04 15:17:03 +00001747 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001748 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001749 && i >= regular_args - ufunc->uf_def_args.ga_len)
1750 {
1751 // assume v:none used for default argument value
1752 continue;
1753 }
1754 if (i < regular_args)
1755 {
1756 if (ufunc->uf_arg_types == NULL)
1757 continue;
1758 expected = ufunc->uf_arg_types[i];
1759 }
1760 else if (ufunc->uf_va_type == NULL
1761 || ufunc->uf_va_type == &t_list_any)
1762 // possibly a lambda or "...: any"
1763 expected = &t_any;
1764 else
1765 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001766 if (need_type(actual, expected, FALSE,
1767 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001768 {
1769 arg_type_mismatch(expected, actual, i + 1);
1770 return FAIL;
1771 }
1772 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001773 compile_type = get_compile_type(ufunc);
1774 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001776 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001777 return FAIL;
1778 }
1779 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1780 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001781 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001782 ufunc->uf_name);
1783 return FAIL;
1784 }
1785
1786 if ((isn = generate_instr(cctx,
1787 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1788 : ISN_UCALL)) == NULL)
1789 return FAIL;
1790 if (isn->isn_type == ISN_DCALL)
1791 {
1792 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1793 isn->isn_arg.dfunc.cdf_argcount = argcount;
1794 }
1795 else
1796 {
1797 // A user function may be deleted and redefined later, can't use the
1798 // ufunc pointer, need to look it up again at runtime.
1799 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1800 isn->isn_arg.ufunc.cuf_argcount = argcount;
1801 }
1802
Bram Moolenaar078a4612022-01-04 15:17:03 +00001803 // drop the argument types
1804 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001805
Bram Moolenaar078a4612022-01-04 15:17:03 +00001806 // add return type
1807 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001808}
1809
1810/*
1811 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1812 */
1813 int
1814generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1815{
1816 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001817
1818 RETURN_OK_IF_SKIP(cctx);
1819 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1820 return FAIL;
1821 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1822 isn->isn_arg.ufunc.cuf_argcount = argcount;
1823
Bram Moolenaar078a4612022-01-04 15:17:03 +00001824 // drop the argument types
1825 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001826
Bram Moolenaar078a4612022-01-04 15:17:03 +00001827 // add return value
1828 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001829}
1830
1831/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001832 * Check the arguments of function "type" against the types on the stack.
1833 * Returns OK or FAIL;
1834 */
1835 int
1836check_func_args_from_type(
1837 cctx_T *cctx,
1838 type_T *type,
1839 int argcount,
1840 int at_top,
1841 char_u *name)
1842{
1843 if (type->tt_argcount != -1)
1844 {
1845 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1846
1847 if (argcount < type->tt_min_argcount - varargs)
1848 {
1849 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1850 return FAIL;
1851 }
1852 if (!varargs && argcount > type->tt_argcount)
1853 {
1854 emsg_funcname(e_too_many_arguments_for_function_str, name);
1855 return FAIL;
1856 }
1857 if (type->tt_args != NULL)
1858 {
1859 int i;
1860
1861 for (i = 0; i < argcount; ++i)
1862 {
1863 int offset = -argcount + i - (at_top ? 0 : 1);
1864 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1865 type_T *expected;
1866
1867 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001868 {
1869 expected = type->tt_args[type->tt_argcount - 1];
1870 if (expected != NULL && expected->tt_type == VAR_LIST)
1871 expected = expected->tt_member;
1872 if (expected == NULL)
1873 expected = &t_any;
1874 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001875 else if (i >= type->tt_min_argcount
1876 && actual->tt_type == VAR_SPECIAL)
1877 expected = &t_any;
1878 else
1879 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001880 if (need_type(actual, expected, FALSE,
1881 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001882 {
1883 arg_type_mismatch(expected, actual, i + 1);
1884 return FAIL;
1885 }
1886 }
1887 }
1888 }
1889
1890 return OK;
1891}
1892/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001893 * Generate an ISN_PCALL instruction.
1894 * "type" is the type of the FuncRef.
1895 */
1896 int
1897generate_PCALL(
1898 cctx_T *cctx,
1899 int argcount,
1900 char_u *name,
1901 type_T *type,
1902 int at_top)
1903{
1904 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001905 type_T *ret_type;
1906
1907 RETURN_OK_IF_SKIP(cctx);
1908
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001909 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910 ret_type = &t_any;
1911 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1912 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001913 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1914 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001915
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001916 ret_type = type->tt_member;
1917 if (ret_type == &t_unknown)
1918 // return type not known yet, use a runtime check
1919 ret_type = &t_any;
1920 }
1921 else
1922 {
1923 semsg(_(e_not_callable_type_str), name);
1924 return FAIL;
1925 }
1926
1927 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1928 return FAIL;
1929 isn->isn_arg.pfunc.cpf_top = at_top;
1930 isn->isn_arg.pfunc.cpf_argcount = argcount;
1931
Bram Moolenaar078a4612022-01-04 15:17:03 +00001932 // drop the arguments and the funcref/partial
1933 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001934
Bram Moolenaar078a4612022-01-04 15:17:03 +00001935 // push the return value
1936 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001937
1938 // If partial is above the arguments it must be cleared and replaced with
1939 // the return value.
1940 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1941 return FAIL;
1942
1943 return OK;
1944}
1945
1946/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001947 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001948 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001949 */
1950 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001951generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001952{
1953 isn_T *isn;
1954
1955 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001956 if ((isn = generate_instr_drop(cctx,
1957 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
1958 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001959 return FAIL;
1960 isn->isn_arg.defer.defer_var_idx = var_idx;
1961 isn->isn_arg.defer.defer_argcount = argcount;
1962 return OK;
1963}
1964
1965/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001966 * Generate an ISN_STRINGMEMBER instruction.
1967 */
1968 int
1969generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1970{
1971 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001972 type_T *type;
1973
1974 RETURN_OK_IF_SKIP(cctx);
1975 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1976 return FAIL;
1977 isn->isn_arg.string = vim_strnsave(name, len);
1978
1979 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001980 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001981 if (type->tt_type != VAR_DICT
1982 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001983 {
1984 char *tofree;
1985
1986 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1987 name, type_name(type, &tofree));
1988 vim_free(tofree);
1989 return FAIL;
1990 }
1991 // change dict type to dict member type
1992 if (type->tt_type == VAR_DICT)
1993 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001994 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00001995 ? &t_any : type->tt_member;
1996 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001997 }
1998
1999 return OK;
2000}
2001
2002/*
2003 * Generate an ISN_ECHO instruction.
2004 */
2005 int
2006generate_ECHO(cctx_T *cctx, int with_white, int count)
2007{
2008 isn_T *isn;
2009
2010 RETURN_OK_IF_SKIP(cctx);
2011 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2012 return FAIL;
2013 isn->isn_arg.echo.echo_with_white = with_white;
2014 isn->isn_arg.echo.echo_count = count;
2015
2016 return OK;
2017}
2018
2019/*
2020 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2021 */
2022 int
2023generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2024{
2025 isn_T *isn;
2026
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002027 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002028 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2029 return FAIL;
2030 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002031 return OK;
2032}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002033
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002034/*
2035 * Generate an ISN_ECHOWINDOW instruction
2036 */
2037 int
2038generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2039{
2040 isn_T *isn;
2041
2042 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2043 return FAIL;
2044 isn->isn_arg.echowin.ewin_count = count;
2045 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002046 return OK;
2047}
2048
2049/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002050 * Generate an ISN_SOURCE instruction.
2051 */
2052 int
2053generate_SOURCE(cctx_T *cctx, int sid)
2054{
2055 isn_T *isn;
2056
2057 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2058 return FAIL;
2059 isn->isn_arg.number = sid;
2060
2061 return OK;
2062}
2063
2064/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002065 * Generate an ISN_PUT instruction.
2066 */
2067 int
2068generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2069{
2070 isn_T *isn;
2071
2072 RETURN_OK_IF_SKIP(cctx);
2073 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2074 return FAIL;
2075 isn->isn_arg.put.put_regname = regname;
2076 isn->isn_arg.put.put_lnum = lnum;
2077 return OK;
2078}
2079
2080/*
2081 * Generate an EXEC instruction that takes a string argument.
2082 * A copy is made of "line".
2083 */
2084 int
2085generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2086{
2087 isn_T *isn;
2088
2089 RETURN_OK_IF_SKIP(cctx);
2090 if ((isn = generate_instr(cctx, isntype)) == NULL)
2091 return FAIL;
2092 isn->isn_arg.string = vim_strsave(line);
2093 return OK;
2094}
2095
2096/*
2097 * Generate an EXEC instruction that takes a string argument.
2098 * "str" must be allocated, it is consumed.
2099 */
2100 int
2101generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2102{
2103 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002104 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002105
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002106 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002107 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002108 if ((isn = generate_instr(cctx, isntype)) == NULL)
2109 ret = FAIL;
2110 else
2111 {
2112 isn->isn_arg.string = str;
2113 return OK;
2114 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002115 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002116 vim_free(str);
2117 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002118}
2119
2120 int
2121generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2122{
2123 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002124
2125 RETURN_OK_IF_SKIP(cctx);
2126 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2127 return FAIL;
2128 isn->isn_arg.string = vim_strsave(line);
2129
Bram Moolenaar078a4612022-01-04 15:17:03 +00002130 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002131}
2132
2133 int
2134generate_EXECCONCAT(cctx_T *cctx, int count)
2135{
2136 isn_T *isn;
2137
2138 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2139 return FAIL;
2140 isn->isn_arg.number = count;
2141 return OK;
2142}
2143
2144/*
2145 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2146 */
2147 int
2148generate_RANGE(cctx_T *cctx, char_u *range)
2149{
2150 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002151
2152 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2153 return FAIL;
2154 isn->isn_arg.string = range;
2155
Bram Moolenaar078a4612022-01-04 15:17:03 +00002156 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002157}
2158
2159 int
2160generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2161{
2162 isn_T *isn;
2163
2164 RETURN_OK_IF_SKIP(cctx);
2165 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2166 return FAIL;
2167 isn->isn_arg.unpack.unp_count = var_count;
2168 isn->isn_arg.unpack.unp_semicolon = semicolon;
2169 return OK;
2170}
2171
2172/*
2173 * Generate an instruction for any command modifiers.
2174 */
2175 int
2176generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2177{
2178 isn_T *isn;
2179
2180 if (has_cmdmod(cmod, FALSE))
2181 {
2182 cctx->ctx_has_cmdmod = TRUE;
2183
2184 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2185 return FAIL;
2186 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2187 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2188 return FAIL;
2189 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2190 // filter program now belongs to the instruction
2191 cmod->cmod_filter_regmatch.regprog = NULL;
2192 }
2193
2194 return OK;
2195}
2196
2197 int
2198generate_undo_cmdmods(cctx_T *cctx)
2199{
2200 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2201 return FAIL;
2202 cctx->ctx_has_cmdmod = FALSE;
2203 return OK;
2204}
2205
2206/*
2207 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002208 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002209 * Return FAIL when out of memory.
2210 */
2211 int
2212generate_store_var(
2213 cctx_T *cctx,
2214 assign_dest_T dest,
2215 int opt_flags,
2216 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002217 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002218 char_u *name,
2219 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002220{
2221 switch (dest)
2222 {
2223 case dest_option:
2224 return generate_STOREOPT(cctx, ISN_STOREOPT,
2225 skip_option_env_lead(name), opt_flags);
2226 case dest_func_option:
2227 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2228 skip_option_env_lead(name), opt_flags);
2229 case dest_global:
2230 // include g: with the name, easier to execute that way
2231 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2232 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2233 case dest_buffer:
2234 // include b: with the name, easier to execute that way
2235 return generate_STORE(cctx, ISN_STOREB, 0, name);
2236 case dest_window:
2237 // include w: with the name, easier to execute that way
2238 return generate_STORE(cctx, ISN_STOREW, 0, name);
2239 case dest_tab:
2240 // include t: with the name, easier to execute that way
2241 return generate_STORE(cctx, ISN_STORET, 0, name);
2242 case dest_env:
2243 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2244 case dest_reg:
2245 return generate_STORE(cctx, ISN_STOREREG,
2246 name[1] == '@' ? '"' : name[1], NULL);
2247 case dest_vimvar:
2248 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2249 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002250 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002251 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2252 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2253 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002254 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002255 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002256
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002257 if (SCRIPT_ID_VALID(scriptvar_sid)
2258 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2259 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2260 == NULL)
2261 {
2262 // "import autoload './dir/script.vim'" - load script
2263 // first
2264 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2265 return FAIL;
2266 isn_type = ISN_STOREEXPORT;
2267 }
2268
2269 // "s:" may be included in the name.
2270 return generate_OLDSCRIPT(cctx, isn_type, name,
2271 scriptvar_sid, type);
2272 }
2273 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002274 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002275 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002276 case dest_class_member:
2277 return generate_CLASSMEMBER(cctx, FALSE,
2278 lhs->lhs_class, lhs->lhs_classmember_idx);
2279
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002280 case dest_local:
2281 case dest_expr:
2282 // cannot happen
2283 break;
2284 }
2285 return FAIL;
2286}
2287
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002288/*
2289 * Return TRUE when inside a "for" or "while" loop.
2290 */
2291 int
2292inside_loop_scope(cctx_T *cctx)
2293{
2294 scope_T *scope = cctx->ctx_scope;
2295
2296 for (;;)
2297 {
2298 if (scope == NULL)
2299 break;
2300 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2301 return TRUE;
2302 scope = scope->se_outer;
2303 }
2304 return FALSE;
2305}
2306
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002307 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002308generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002309{
2310 if (lhs->lhs_dest != dest_local)
2311 return generate_store_var(cctx, lhs->lhs_dest,
2312 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002313 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002314
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002315 if (lhs->lhs_lvar == NULL)
2316 return OK;
2317
2318 garray_T *instr = &cctx->ctx_instr;
2319 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2320
2321 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2322 // ISN_STORENR.
2323 // And "var = 0" does not need any instruction.
2324 if (lhs->lhs_lvar->lv_from_outer == 0
2325 && instr->ga_len == instr_count + 1
2326 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002327 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002328 varnumber_T val = isn->isn_arg.number;
2329 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002330
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002331 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002332 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002333 // zero is the default value, no need to do anything
2334 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002335 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002336 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002337 {
2338 isn->isn_type = ISN_STORENR;
2339 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2340 isn->isn_arg.storenr.stnr_val = val;
2341 }
2342 if (stack->ga_len > 0)
2343 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002344 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002345 else if (lhs->lhs_lvar->lv_from_outer > 0)
2346 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2347 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2348 else
2349 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002350 return OK;
2351}
2352
2353#if defined(FEAT_PROFILE) || defined(PROTO)
2354 void
2355may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2356{
2357 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2358 generate_instr(cctx, ISN_PROF_END);
2359}
2360#endif
2361
2362
2363/*
2364 * Delete an instruction, free what it contains.
2365 */
2366 void
2367delete_instr(isn_T *isn)
2368{
2369 switch (isn->isn_type)
2370 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002371 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002372 case ISN_DEF:
2373 case ISN_EXEC:
2374 case ISN_EXECRANGE:
2375 case ISN_EXEC_SPLIT:
2376 case ISN_LEGACY_EVAL:
2377 case ISN_LOADAUTO:
2378 case ISN_LOADB:
2379 case ISN_LOADENV:
2380 case ISN_LOADG:
2381 case ISN_LOADOPT:
2382 case ISN_LOADT:
2383 case ISN_LOADW:
2384 case ISN_LOCKUNLOCK:
2385 case ISN_PUSHEXC:
2386 case ISN_PUSHFUNC:
2387 case ISN_PUSHS:
2388 case ISN_RANGE:
2389 case ISN_STOREAUTO:
2390 case ISN_STOREB:
2391 case ISN_STOREENV:
2392 case ISN_STOREG:
2393 case ISN_STORET:
2394 case ISN_STOREW:
2395 case ISN_STRINGMEMBER:
2396 vim_free(isn->isn_arg.string);
2397 break;
2398
2399 case ISN_SUBSTITUTE:
2400 {
2401 int idx;
2402 isn_T *list = isn->isn_arg.subs.subs_instr;
2403
2404 vim_free(isn->isn_arg.subs.subs_cmd);
2405 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2406 delete_instr(list + idx);
2407 vim_free(list);
2408 }
2409 break;
2410
2411 case ISN_INSTR:
2412 {
2413 int idx;
2414 isn_T *list = isn->isn_arg.instr;
2415
2416 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2417 delete_instr(list + idx);
2418 vim_free(list);
2419 }
2420 break;
2421
2422 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002423 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002424 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002425 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002426 vim_free(isn->isn_arg.loadstore.ls_name);
2427 break;
2428
2429 case ISN_UNLET:
2430 case ISN_UNLETENV:
2431 vim_free(isn->isn_arg.unlet.ul_name);
2432 break;
2433
2434 case ISN_STOREOPT:
2435 case ISN_STOREFUNCOPT:
2436 vim_free(isn->isn_arg.storeopt.so_name);
2437 break;
2438
2439 case ISN_PUSHBLOB: // push blob isn_arg.blob
2440 blob_unref(isn->isn_arg.blob);
2441 break;
2442
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002443 case ISN_UCALL:
2444 vim_free(isn->isn_arg.ufunc.cuf_name);
2445 break;
2446
2447 case ISN_FUNCREF:
2448 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002449 funcref_T *funcref = &isn->isn_arg.funcref;
2450 funcref_extra_T *extra = funcref->fr_extra;
2451
2452 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002453 {
2454 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002455 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002456 ufunc_T *ufunc = dfunc->df_ufunc;
2457
2458 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2459 func_ptr_unref(ufunc);
2460 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002461 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002463 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002464
2465 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002466 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002468 vim_free(name);
2469 }
2470 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002471 }
2472 }
2473 break;
2474
2475 case ISN_DCALL:
2476 {
2477 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002478 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002479
2480 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002481 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002482 func_ptr_unref(dfunc->df_ufunc);
2483 }
2484 break;
2485
2486 case ISN_NEWFUNC:
2487 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002488 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002489
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002490 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002491 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002492 ufunc_T *ufunc = find_func_even_dead(
2493 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002494
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002495 if (ufunc != NULL)
2496 {
2497 unlink_def_function(ufunc);
2498 func_ptr_unref(ufunc);
2499 }
2500
2501 vim_free(arg->nfa_lambda);
2502 vim_free(arg->nfa_global);
2503 vim_free(arg);
2504 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002505 }
2506 break;
2507
2508 case ISN_CHECKTYPE:
2509 case ISN_SETTYPE:
2510 free_type(isn->isn_arg.type.ct_type);
2511 break;
2512
2513 case ISN_CMDMOD:
2514 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002515 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002516 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2517 break;
2518
2519 case ISN_LOADSCRIPT:
2520 case ISN_STORESCRIPT:
2521 vim_free(isn->isn_arg.script.scriptref);
2522 break;
2523
Bram Moolenaard505d172022-12-18 21:42:55 +00002524 case ISN_LOAD_CLASSMEMBER:
2525 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002526 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002527 class_unref(isn->isn_arg.classmember.cm_class);
2528 break;
2529
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002530 case ISN_STOREINDEX:
2531 class_unref(isn->isn_arg.storeindex.si_class);
2532 break;
2533
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002534 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002535 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002536 break;
2537
2538 case ISN_CEXPR_CORE:
2539 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2540 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2541 break;
2542
2543 case ISN_2BOOL:
2544 case ISN_2STRING:
2545 case ISN_2STRING_ANY:
2546 case ISN_ADDBLOB:
2547 case ISN_ADDLIST:
2548 case ISN_ANYINDEX:
2549 case ISN_ANYSLICE:
2550 case ISN_BCALL:
2551 case ISN_BLOBAPPEND:
2552 case ISN_BLOBINDEX:
2553 case ISN_BLOBSLICE:
2554 case ISN_CATCH:
2555 case ISN_CEXPR_AUCMD:
2556 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002557 case ISN_CLEARDICT:
2558 case ISN_CMDMOD_REV:
2559 case ISN_COMPAREANY:
2560 case ISN_COMPAREBLOB:
2561 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002562 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002563 case ISN_COMPAREDICT:
2564 case ISN_COMPAREFLOAT:
2565 case ISN_COMPAREFUNC:
2566 case ISN_COMPARELIST:
2567 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002568 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002569 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002570 case ISN_COMPARESPECIAL:
2571 case ISN_COMPARESTRING:
2572 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002573 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002574 case ISN_COND2BOOL:
2575 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002576 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002577 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002578 case ISN_DROP:
2579 case ISN_ECHO:
2580 case ISN_ECHOCONSOLE:
2581 case ISN_ECHOERR:
2582 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002583 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002584 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002585 case ISN_ENDTRY:
2586 case ISN_EXECCONCAT:
2587 case ISN_EXECUTE:
2588 case ISN_FINALLY:
2589 case ISN_FINISH:
2590 case ISN_FOR:
2591 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002592 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002593 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002594 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002595 case ISN_JUMP_IF_ARG_SET:
2596 case ISN_LISTAPPEND:
2597 case ISN_LISTINDEX:
2598 case ISN_LISTSLICE:
2599 case ISN_LOAD:
2600 case ISN_LOADBDICT:
2601 case ISN_LOADGDICT:
2602 case ISN_LOADOUTER:
2603 case ISN_LOADREG:
2604 case ISN_LOADTDICT:
2605 case ISN_LOADV:
2606 case ISN_LOADWDICT:
2607 case ISN_LOCKCONST:
2608 case ISN_MEMBER:
2609 case ISN_NEGATENR:
2610 case ISN_NEWDICT:
2611 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002612 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002613 case ISN_OPANY:
2614 case ISN_OPFLOAT:
2615 case ISN_OPNR:
2616 case ISN_PCALL:
2617 case ISN_PCALL_END:
2618 case ISN_PROF_END:
2619 case ISN_PROF_START:
2620 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002621 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002623 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002624 case ISN_PUSHNR:
2625 case ISN_PUSHSPEC:
2626 case ISN_PUT:
2627 case ISN_REDIREND:
2628 case ISN_REDIRSTART:
2629 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002630 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002631 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002632 case ISN_SHUFFLE:
2633 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002634 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002635 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002636 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002637 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002638 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002639 case ISN_STORERANGE:
2640 case ISN_STOREREG:
2641 case ISN_STOREV:
2642 case ISN_STRINDEX:
2643 case ISN_STRSLICE:
2644 case ISN_THROW:
2645 case ISN_TRYCONT:
2646 case ISN_UNLETINDEX:
2647 case ISN_UNLETRANGE:
2648 case ISN_UNPACK:
2649 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002650 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002651 // nothing allocated
2652 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002653 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002654}
2655
2656 void
2657clear_instr_ga(garray_T *gap)
2658{
2659 int idx;
2660
2661 for (idx = 0; idx < gap->ga_len; ++idx)
2662 delete_instr(((isn_T *)gap->ga_data) + idx);
2663 ga_clear(gap);
2664}
2665
2666
2667#endif // defined(FEAT_EVAL)