blob: bd2c1b41470d1f6c20fb67adcdc21140b08c23e0 [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 Moolenaar7ce7daf2022-12-10 18:42:12 +0000153 * Generate ISN_STORE_THIS - store value in member of "this" object with member
154 * index "idx".
155 */
156 int
157generate_STORE_THIS(cctx_T *cctx, int idx)
158{
159 RETURN_OK_IF_SKIP(cctx);
160
161 // drop the value type
162 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
163 if (isn == NULL)
164 return FAIL;
165
166 isn->isn_arg.number = idx;
167 return OK;
168}
169
170/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000171 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
172 * But only for simple types.
173 * When "tolerant" is TRUE convert most types to string, e.g. a List.
174 */
175 int
176may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
177{
178 isn_T *isn;
179 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000180 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000181
182 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000183 type = get_type_on_stack(cctx, -1 - offset);
184 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000185 {
186 // nothing to be done
187 case VAR_STRING: return OK;
188
189 // conversion possible
190 case VAR_SPECIAL:
191 case VAR_BOOL:
192 case VAR_NUMBER:
193 case VAR_FLOAT:
194 break;
195
196 // conversion possible (with runtime check)
197 case VAR_ANY:
198 case VAR_UNKNOWN:
199 isntype = ISN_2STRING_ANY;
200 break;
201
202 // conversion possible when tolerant
203 case VAR_LIST:
204 if (tolerant)
205 {
206 isntype = ISN_2STRING_ANY;
207 break;
208 }
209 // FALLTHROUGH
210
211 // conversion not possible
212 case VAR_VOID:
213 case VAR_BLOB:
214 case VAR_FUNC:
215 case VAR_PARTIAL:
216 case VAR_DICT:
217 case VAR_JOB:
218 case VAR_CHANNEL:
219 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000220 case VAR_CLASS:
221 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000222 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000223 return FAIL;
224 }
225
Bram Moolenaar078a4612022-01-04 15:17:03 +0000226 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000227 if ((isn = generate_instr(cctx, isntype)) == NULL)
228 return FAIL;
229 isn->isn_arg.tostring.offset = offset;
230 isn->isn_arg.tostring.tolerant = tolerant;
231
232 return OK;
233}
234
235 static int
236check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
237{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000238 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
239 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000240 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000241 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000242 {
243 if (*op == '+')
244 emsg(_(e_wrong_argument_type_for_plus));
245 else
246 semsg(_(e_char_requires_number_or_float_arguments), *op);
247 return FAIL;
248 }
249 return OK;
250}
251
252/*
253 * Generate instruction for "+". For a list this creates a new list.
254 */
255 int
256generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000257 cctx_T *cctx,
258 vartype_T vartype,
259 type_T *type1,
260 type_T *type2,
261 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000262{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000263 isn_T *isn = generate_instr_drop(cctx,
264 vartype == VAR_NUMBER ? ISN_OPNR
265 : vartype == VAR_LIST ? ISN_ADDLIST
266 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000267 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000268 : ISN_OPANY, 1);
269
270 if (vartype != VAR_LIST && vartype != VAR_BLOB
271 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000272 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000273 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000274 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000275 && check_number_or_float(
276 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
277 return FAIL;
278
279 if (isn != NULL)
280 {
281 if (isn->isn_type == ISN_ADDLIST)
282 isn->isn_arg.op.op_type = expr_type;
283 else
284 isn->isn_arg.op.op_type = EXPR_ADD;
285 }
286
287 // When concatenating two lists with different member types the member type
288 // becomes "any".
289 if (vartype == VAR_LIST
290 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
291 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000292 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000293
294 return isn == NULL ? FAIL : OK;
295}
296
297/*
298 * Get the type to use for an instruction for an operation on "type1" and
299 * "type2". If they are matching use a type-specific instruction. Otherwise
300 * fall back to runtime type checking.
301 */
302 vartype_T
303operator_type(type_T *type1, type_T *type2)
304{
305 if (type1->tt_type == type2->tt_type
306 && (type1->tt_type == VAR_NUMBER
307 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000308 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000309 || type1->tt_type == VAR_BLOB))
310 return type1->tt_type;
311 return VAR_ANY;
312}
313
314/*
315 * Generate an instruction with two arguments. The instruction depends on the
316 * type of the arguments.
317 */
318 int
319generate_two_op(cctx_T *cctx, char_u *op)
320{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000321 type_T *type1;
322 type_T *type2;
323 vartype_T vartype;
324 isn_T *isn;
325
326 RETURN_OK_IF_SKIP(cctx);
327
328 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000329 type1 = get_type_on_stack(cctx, 1);
330 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000331 vartype = operator_type(type1, type2);
332
333 switch (*op)
334 {
335 case '+':
336 if (generate_add_instr(cctx, vartype, type1, type2,
337 EXPR_COPY) == FAIL)
338 return FAIL;
339 break;
340
341 case '-':
342 case '*':
343 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
344 op) == FAIL)
345 return FAIL;
346 if (vartype == VAR_NUMBER)
347 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000348 else if (vartype == VAR_FLOAT)
349 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000350 else
351 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
352 if (isn != NULL)
353 isn->isn_arg.op.op_type = *op == '*'
354 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
355 break;
356
357 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000358 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000359 && type1->tt_type != VAR_NUMBER)
360 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000361 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000362 && type2->tt_type != VAR_NUMBER))
363 {
364 emsg(_(e_percent_requires_number_arguments));
365 return FAIL;
366 }
367 isn = generate_instr_drop(cctx,
368 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
369 if (isn != NULL)
370 isn->isn_arg.op.op_type = EXPR_REM;
371 break;
372 }
373
374 // correct type of result
375 if (vartype == VAR_ANY)
376 {
377 type_T *type = &t_any;
378
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 // float+number and number+float results in float
380 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100381 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000382 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000383 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000384 }
385
386 return OK;
387}
388
389/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000390 * Get the instruction to use for comparing two values with specified types.
391 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000392 * Return ISN_DROP when failed.
393 */
394 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000395get_compare_isn(
396 exprtype_T exprtype,
397 typval_T *tv1,
398 typval_T *tv2,
399 type_T *type1,
400 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000401{
402 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000403 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
404 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000405
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000406 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000407 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000408 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000409 {
410 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
411 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
412 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
413 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
414 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
415 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
416 case VAR_LIST: isntype = ISN_COMPARELIST; break;
417 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
418 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000419 case VAR_CLASS: isntype = ISN_COMPARECLASS; break;
420 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000421 default: isntype = ISN_COMPAREANY; break;
422 }
423 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000424 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
425 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
426 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
427 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
428 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000429 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000430 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000431 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000432 if ((vartype1 == VAR_SPECIAL
433 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
434 : type1 == &t_none)
435 && vartype2 != VAR_STRING)
436 || (vartype2 == VAR_SPECIAL
437 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
438 : type2 == &t_none)
439 && vartype1 != VAR_STRING))
440 {
441 semsg(_(e_cannot_compare_str_with_str),
442 vartype_name(vartype1), vartype_name(vartype2));
443 return ISN_DROP;
444 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000445 // although comparing null with number, float or bool is not useful, we
446 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000447 isntype = ISN_COMPARENULL;
448 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000449
450 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
451 && (isntype == ISN_COMPAREBOOL
452 || isntype == ISN_COMPARESPECIAL
453 || isntype == ISN_COMPARENR
454 || isntype == ISN_COMPAREFLOAT))
455 {
456 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000457 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000458 return ISN_DROP;
459 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000460 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
461 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
462 && (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
463 {
464 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
465 return ISN_DROP;
466 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000467 if (isntype == ISN_DROP
468 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000469 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
470 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000471 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000472 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000473 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
474 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000475 {
476 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000477 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000478 return ISN_DROP;
479 }
480 return isntype;
481}
482
483 int
484check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
485{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000486 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000487 return FAIL;
488 return OK;
489}
490
491/*
492 * Generate an ISN_COMPARE* instruction with a boolean result.
493 */
494 int
495generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
496{
497 isntype_T isntype;
498 isn_T *isn;
499 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000500
501 RETURN_OK_IF_SKIP(cctx);
502
503 // Get the known type of the two items on the stack. If they are matching
504 // use a type-specific instruction. Otherwise fall back to runtime type
505 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000506 isntype = get_compare_isn(exprtype, NULL, NULL,
507 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000508 if (isntype == ISN_DROP)
509 return FAIL;
510
511 if ((isn = generate_instr(cctx, isntype)) == NULL)
512 return FAIL;
513 isn->isn_arg.op.op_type = exprtype;
514 isn->isn_arg.op.op_ic = ic;
515
516 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100517 --stack->ga_len;
518 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000519
520 return OK;
521}
522
523/*
LemonBoy372bcce2022-04-25 12:43:20 +0100524 * Generate an ISN_CONCAT instruction.
525 * "count" is the number of stack elements to join together and it must be
526 * greater or equal to one.
527 * The caller ensures all the "count" elements on the stack have the right type.
528 */
529 int
530generate_CONCAT(cctx_T *cctx, int count)
531{
532 isn_T *isn;
533 garray_T *stack = &cctx->ctx_type_stack;
534
535 RETURN_OK_IF_SKIP(cctx);
536
LemonBoy372bcce2022-04-25 12:43:20 +0100537 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
538 return FAIL;
539 isn->isn_arg.number = count;
540
541 // drop the argument types
542 stack->ga_len -= count - 1;
543
544 return OK;
545}
546
547/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000548 * Generate an ISN_2BOOL instruction.
549 * "offset" is the offset in the type stack.
550 */
551 int
552generate_2BOOL(cctx_T *cctx, int invert, int offset)
553{
554 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000555
556 RETURN_OK_IF_SKIP(cctx);
557 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
558 return FAIL;
559 isn->isn_arg.tobool.invert = invert;
560 isn->isn_arg.tobool.offset = offset;
561
562 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000563 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000564
565 return OK;
566}
567
568/*
569 * Generate an ISN_COND2BOOL instruction.
570 */
571 int
572generate_COND2BOOL(cctx_T *cctx)
573{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000574 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100575 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000576 return FAIL;
577
578 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000579 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000580
581 return OK;
582}
583
584 int
585generate_TYPECHECK(
586 cctx_T *cctx,
587 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000588 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000589 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100590 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000591 int argidx)
592{
593 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000594
595 RETURN_OK_IF_SKIP(cctx);
596 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
597 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000598 type_T *tt;
599 if (expected->tt_type == VAR_FLOAT && number_ok)
600 {
601 // always allocate, also for static types
602 tt = ALLOC_ONE(type_T);
603 if (tt != NULL)
604 {
605 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000606 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000607 tt->tt_flags |= TTFLAG_NUMBER_OK;
608 }
609 }
610 else
611 tt = alloc_type(expected);
612
613 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100615 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000616 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
617
618 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000619 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620
621 return OK;
622}
623
624 int
625generate_SETTYPE(
626 cctx_T *cctx,
627 type_T *expected)
628{
629 isn_T *isn;
630
631 RETURN_OK_IF_SKIP(cctx);
632 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
633 return FAIL;
634 isn->isn_arg.type.ct_type = alloc_type(expected);
635 return OK;
636}
637
638/*
639 * Generate a PUSH instruction for "tv".
640 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000641 */
642 int
643generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
644{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100645 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000646 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100647 case VAR_BOOL:
648 generate_PUSHBOOL(cctx, tv->vval.v_number);
649 break;
650 case VAR_SPECIAL:
651 generate_PUSHSPEC(cctx, tv->vval.v_number);
652 break;
653 case VAR_NUMBER:
654 generate_PUSHNR(cctx, tv->vval.v_number);
655 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100656 case VAR_FLOAT:
657 generate_PUSHF(cctx, tv->vval.v_float);
658 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100659 case VAR_BLOB:
660 generate_PUSHBLOB(cctx, tv->vval.v_blob);
661 tv->vval.v_blob = NULL;
662 break;
663 case VAR_LIST:
664 if (tv->vval.v_list != NULL)
665 iemsg("non-empty list constant not supported");
666 generate_NEWLIST(cctx, 0, TRUE);
667 break;
668 case VAR_DICT:
669 if (tv->vval.v_dict != NULL)
670 iemsg("non-empty dict constant not supported");
671 generate_NEWDICT(cctx, 0, TRUE);
672 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000673#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100674 case VAR_JOB:
675 if (tv->vval.v_job != NULL)
676 iemsg("non-null job constant not supported");
677 generate_PUSHJOB(cctx);
678 break;
679 case VAR_CHANNEL:
680 if (tv->vval.v_channel != NULL)
681 iemsg("non-null channel constant not supported");
682 generate_PUSHCHANNEL(cctx);
683 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000684#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100685 case VAR_FUNC:
686 if (tv->vval.v_string != NULL)
687 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100688 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100689 break;
690 case VAR_PARTIAL:
691 if (tv->vval.v_partial != NULL)
692 iemsg("non-null partial constant not supported");
693 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
694 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000695 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100696 break;
697 case VAR_STRING:
698 generate_PUSHS(cctx, &tv->vval.v_string);
699 tv->vval.v_string = NULL;
700 break;
701 default:
702 siemsg("constant type %d not supported", tv->v_type);
703 clear_tv(tv);
704 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000705 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100706 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000707 return OK;
708}
709
710/*
711 * Generate an ISN_PUSHNR instruction.
712 */
713 int
714generate_PUSHNR(cctx_T *cctx, varnumber_T number)
715{
716 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000717
718 RETURN_OK_IF_SKIP(cctx);
719 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
720 return FAIL;
721 isn->isn_arg.number = number;
722
723 if (number == 0 || number == 1)
724 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000725 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000726 return OK;
727}
728
729/*
730 * Generate an ISN_PUSHBOOL instruction.
731 */
732 int
733generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
734{
735 isn_T *isn;
736
737 RETURN_OK_IF_SKIP(cctx);
738 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
739 return FAIL;
740 isn->isn_arg.number = number;
741
742 return OK;
743}
744
745/*
746 * Generate an ISN_PUSHSPEC instruction.
747 */
748 int
749generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
750{
751 isn_T *isn;
752
753 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000754 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
755 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000756 return FAIL;
757 isn->isn_arg.number = number;
758
759 return OK;
760}
761
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762/*
763 * Generate an ISN_PUSHF instruction.
764 */
765 int
766generate_PUSHF(cctx_T *cctx, float_T fnumber)
767{
768 isn_T *isn;
769
770 RETURN_OK_IF_SKIP(cctx);
771 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
772 return FAIL;
773 isn->isn_arg.fnumber = fnumber;
774
775 return OK;
776}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000777
778/*
779 * Generate an ISN_PUSHS instruction.
780 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100781 * Note that if "str" is used in the instruction OK is returned and "*str" is
782 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000783 */
784 int
785generate_PUSHS(cctx_T *cctx, char_u **str)
786{
787 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100788 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100790 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100792 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
793 ret = FAIL;
794 else
795 {
796 isn->isn_arg.string = str == NULL ? NULL : *str;
797 return OK;
798 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000799 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100800 if (str != NULL)
801 VIM_CLEAR(*str);
802 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000803}
804
805/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000806 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000807 */
808 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000809generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000810{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000811 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100812#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100813 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000814 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000815 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100816#else
817 emsg(_(e_channel_job_feature_not_available));
818 return FAIL;
819#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000820}
821
822/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000823 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000824 */
825 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000826generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000828 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100829#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100830 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000831 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000832 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100833#else
834 emsg(_(e_channel_job_feature_not_available));
835 return FAIL;
836#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000837}
838
839/*
840 * Generate an ISN_PUSHBLOB instruction.
841 * Consumes "blob".
842 */
843 int
844generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
845{
846 isn_T *isn;
847
848 RETURN_OK_IF_SKIP(cctx);
849 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
850 return FAIL;
851 isn->isn_arg.blob = blob;
852
853 return OK;
854}
855
856/*
857 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100858 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
859 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000860 */
861 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100862generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000863{
864 isn_T *isn;
865 char_u *funcname;
866
867 RETURN_OK_IF_SKIP(cctx);
868 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
869 return FAIL;
870 if (name == NULL)
871 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100872 else if (!may_prefix
873 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000874 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875 funcname = vim_strsave(name);
876 else
877 {
878 funcname = alloc(STRLEN(name) + 3);
879 if (funcname != NULL)
880 {
881 STRCPY(funcname, "g:");
882 STRCPY(funcname + 2, name);
883 }
884 }
885
886 isn->isn_arg.string = funcname;
887 return OK;
888}
889
890/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000891 * Generate an ISN_AUTOLOAD instruction.
892 */
893 int
894generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
895{
896 isn_T *isn;
897
898 RETURN_OK_IF_SKIP(cctx);
899 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
900 return FAIL;
901 isn->isn_arg.string = vim_strsave(name);
902 if (isn->isn_arg.string == NULL)
903 return FAIL;
904 return OK;
905}
906
907/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000908 * Generate an ISN_GETITEM instruction with "index".
909 * "with_op" is TRUE for "+=" and other operators, the stack has the current
910 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100911 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000912 */
913 int
914generate_GETITEM(cctx_T *cctx, int index, int with_op)
915{
916 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000917 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000918 type_T *item_type = &t_any;
919
920 RETURN_OK_IF_SKIP(cctx);
921
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922 item_type = type->tt_member;
923 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
924 return FAIL;
925 isn->isn_arg.getitem.gi_index = index;
926 isn->isn_arg.getitem.gi_with_op = with_op;
927
928 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000929 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000930}
931
932/*
933 * Generate an ISN_SLICE instruction with "count".
934 */
935 int
936generate_SLICE(cctx_T *cctx, int count)
937{
938 isn_T *isn;
939
940 RETURN_OK_IF_SKIP(cctx);
941 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
942 return FAIL;
943 isn->isn_arg.number = count;
944 return OK;
945}
946
947/*
948 * Generate an ISN_CHECKLEN instruction with "min_len".
949 */
950 int
951generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
952{
953 isn_T *isn;
954
955 RETURN_OK_IF_SKIP(cctx);
956
957 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
958 return FAIL;
959 isn->isn_arg.checklen.cl_min_len = min_len;
960 isn->isn_arg.checklen.cl_more_OK = more_OK;
961
962 return OK;
963}
964
965/*
966 * Generate an ISN_STORE instruction.
967 */
968 int
969generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
970{
971 isn_T *isn;
972
973 RETURN_OK_IF_SKIP(cctx);
974 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
975 return FAIL;
976 if (name != NULL)
977 isn->isn_arg.string = vim_strsave(name);
978 else
979 isn->isn_arg.number = idx;
980
981 return OK;
982}
983
984/*
Bram Moolenaard505d172022-12-18 21:42:55 +0000985 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
986 * ("load" == FALSE) instruction.
987 */
988 int
989generate_CLASSMEMBER(
990 cctx_T *cctx,
991 int load,
992 class_T *cl,
993 int idx)
994{
995 isn_T *isn;
996
997 RETURN_OK_IF_SKIP(cctx);
998 if (load)
999 {
1000 ocmember_T *m = &cl->class_class_members[idx];
1001 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1002 }
1003 else
1004 {
1005 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1006 }
1007 if (isn == NULL)
1008 return FAIL;
1009 isn->isn_arg.classmember.cm_class = cl;
1010 ++cl->class_refcount;
1011 isn->isn_arg.classmember.cm_idx = idx;
1012
1013 return OK;
1014}
1015
1016/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001017 * Generate an ISN_STOREOUTER instruction.
1018 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001019 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001020generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001021{
1022 isn_T *isn;
1023
1024 RETURN_OK_IF_SKIP(cctx);
1025 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1026 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001027 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1028 {
1029 // Store a variable defined in a loop. A copy will be made at the end
1030 // of the loop. TODO: how about deeper nesting?
1031 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1032 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1033 }
1034 else
1035 {
1036 isn->isn_arg.outer.outer_idx = idx;
1037 isn->isn_arg.outer.outer_depth = level;
1038 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001039
1040 return OK;
1041}
1042
1043/*
1044 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1045 */
1046 int
1047generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1048{
1049 isn_T *isn;
1050
1051 RETURN_OK_IF_SKIP(cctx);
1052 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1053 return FAIL;
1054 isn->isn_arg.storenr.stnr_idx = idx;
1055 isn->isn_arg.storenr.stnr_val = value;
1056
1057 return OK;
1058}
1059
1060/*
1061 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1062 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001063 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001064generate_STOREOPT(
1065 cctx_T *cctx,
1066 isntype_T isn_type,
1067 char_u *name,
1068 int opt_flags)
1069{
1070 isn_T *isn;
1071
1072 RETURN_OK_IF_SKIP(cctx);
1073 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1074 return FAIL;
1075 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1076 isn->isn_arg.storeopt.so_flags = opt_flags;
1077
1078 return OK;
1079}
1080
1081/*
1082 * Generate an ISN_LOAD or similar instruction.
1083 */
1084 int
1085generate_LOAD(
1086 cctx_T *cctx,
1087 isntype_T isn_type,
1088 int idx,
1089 char_u *name,
1090 type_T *type)
1091{
1092 isn_T *isn;
1093
1094 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001095 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001096 return FAIL;
1097 if (name != NULL)
1098 isn->isn_arg.string = vim_strsave(name);
1099 else
1100 isn->isn_arg.number = idx;
1101
1102 return OK;
1103}
1104
1105/*
1106 * Generate an ISN_LOADOUTER instruction
1107 */
1108 int
1109generate_LOADOUTER(
1110 cctx_T *cctx,
1111 int idx,
1112 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001113 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001114 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001115 type_T *type)
1116{
1117 isn_T *isn;
1118
1119 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001120 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001121 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001122 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1123 {
1124 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001125 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001126 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001127 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001128 }
1129 else
1130 {
1131 isn->isn_arg.outer.outer_idx = idx;
1132 isn->isn_arg.outer.outer_depth = nesting;
1133 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001134
1135 return OK;
1136}
1137
1138/*
1139 * Generate an ISN_LOADV instruction for v:var.
1140 */
1141 int
1142generate_LOADV(
1143 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001144 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001145{
1146 int di_flags;
1147 int vidx = find_vim_var(name, &di_flags);
1148 type_T *type;
1149
1150 RETURN_OK_IF_SKIP(cctx);
1151 if (vidx < 0)
1152 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001153 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154 return FAIL;
1155 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001156 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001157 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1158}
1159
1160/*
1161 * Generate an ISN_UNLET instruction.
1162 */
1163 int
1164generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1165{
1166 isn_T *isn;
1167
1168 RETURN_OK_IF_SKIP(cctx);
1169 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1170 return FAIL;
1171 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1172 isn->isn_arg.unlet.ul_forceit = forceit;
1173
1174 return OK;
1175}
1176
1177/*
1178 * Generate an ISN_LOCKCONST instruction.
1179 */
1180 int
1181generate_LOCKCONST(cctx_T *cctx)
1182{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001184 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001185 return FAIL;
1186 return OK;
1187}
1188
1189/*
1190 * Generate an ISN_LOADS instruction.
1191 */
1192 int
1193generate_OLDSCRIPT(
1194 cctx_T *cctx,
1195 isntype_T isn_type,
1196 char_u *name,
1197 int sid,
1198 type_T *type)
1199{
1200 isn_T *isn;
1201
1202 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001203 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001204 isn = generate_instr_type(cctx, isn_type, type);
1205 else
1206 isn = generate_instr_drop(cctx, isn_type, 1);
1207 if (isn == NULL)
1208 return FAIL;
1209 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1210 isn->isn_arg.loadstore.ls_sid = sid;
1211
1212 return OK;
1213}
1214
1215/*
1216 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1217 */
1218 int
1219generate_VIM9SCRIPT(
1220 cctx_T *cctx,
1221 isntype_T isn_type,
1222 int sid,
1223 int idx,
1224 type_T *type)
1225{
1226 isn_T *isn;
1227 scriptref_T *sref;
1228 scriptitem_T *si = SCRIPT_ITEM(sid);
1229
1230 RETURN_OK_IF_SKIP(cctx);
1231 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001232 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001233 else
1234 isn = generate_instr_drop(cctx, isn_type, 1);
1235 if (isn == NULL)
1236 return FAIL;
1237
1238 // This requires three arguments, which doesn't fit in an instruction, thus
1239 // we need to allocate a struct for this.
1240 sref = ALLOC_ONE(scriptref_T);
1241 if (sref == NULL)
1242 return FAIL;
1243 isn->isn_arg.script.scriptref = sref;
1244 sref->sref_sid = sid;
1245 sref->sref_idx = idx;
1246 sref->sref_seq = si->sn_script_seq;
1247 sref->sref_type = type;
1248 return OK;
1249}
1250
1251/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001252 * Generate an ISN_NEWLIST instruction for "count" items.
1253 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001254 */
1255 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001256generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001257{
1258 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001259 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001260 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001261 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001262
1263 RETURN_OK_IF_SKIP(cctx);
1264 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1265 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001266 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001267
Bram Moolenaar078a4612022-01-04 15:17:03 +00001268 // Get the member type and the declared member type from all the items on
1269 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001270 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001271 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001272 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001273
1274 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001275 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001276
1277 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001278 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001279}
1280
1281/*
1282 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001283 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001284 */
1285 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001286generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001287{
1288 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001289 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001290 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001291 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001292
1293 RETURN_OK_IF_SKIP(cctx);
1294 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1295 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001296 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001297
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001298 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001299 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001300 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001301
1302 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001303 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304
1305 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001306 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001307}
1308
1309/*
1310 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001311 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001312 * If variables were declared inside a loop "loop_var_idx" is the index of the
1313 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001314 */
1315 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001316generate_FUNCREF(
1317 cctx_T *cctx,
1318 ufunc_T *ufunc,
1319 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001320{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001321 isn_T *isn;
1322 type_T *type;
1323 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001324 loopvarinfo_T loopinfo;
1325 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001326
1327 RETURN_OK_IF_SKIP(cctx);
1328 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1329 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001330 if (isnp != NULL)
1331 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001332
Bram Moolenaarcc341812022-09-19 15:54:34 +01001333 has_vars = get_loop_var_info(cctx, &loopinfo);
1334 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001335 {
1336 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1337 if (extra == NULL)
1338 return FAIL;
1339 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001340 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001341 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001342 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001343 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344 else
1345 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001346
1347 // Reserve an extra variable to keep track of the number of closures
1348 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001349 cctx->ctx_has_closure = 1;
1350
1351 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001352 // the nested context, including this one. But not a function defined at
1353 // the script level.
1354 if ((ufunc->uf_flags & FC_CLOSURE)
1355 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001356 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1357
Bram Moolenaar078a4612022-01-04 15:17:03 +00001358 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1359 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360}
1361
1362/*
1363 * Generate an ISN_NEWFUNC instruction.
1364 * "lambda_name" and "func_name" must be in allocated memory and will be
1365 * consumed.
1366 */
1367 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001368generate_NEWFUNC(
1369 cctx_T *cctx,
1370 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001371 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372{
1373 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001374 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001375
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001376 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001377 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001378 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1379 ret = FAIL;
1380 else
1381 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001382 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1383
1384 if (arg == NULL)
1385 ret = FAIL;
1386 else
1387 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001388 // Reserve an extra variable to keep track of the number of
1389 // closures created.
1390 cctx->ctx_has_closure = 1;
1391
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001392 isn->isn_arg.newfunc.nf_arg = arg;
1393 arg->nfa_lambda = lambda_name;
1394 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001395 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001396 return OK;
1397 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001398 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001399 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001400 vim_free(lambda_name);
1401 vim_free(func_name);
1402 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001403}
1404
1405/*
1406 * Generate an ISN_DEF instruction: list functions
1407 */
1408 int
1409generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1410{
1411 isn_T *isn;
1412
1413 RETURN_OK_IF_SKIP(cctx);
1414 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1415 return FAIL;
1416 if (len > 0)
1417 {
1418 isn->isn_arg.string = vim_strnsave(name, len);
1419 if (isn->isn_arg.string == NULL)
1420 return FAIL;
1421 }
1422 return OK;
1423}
1424
1425/*
1426 * Generate an ISN_JUMP instruction.
1427 */
1428 int
1429generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1430{
1431 isn_T *isn;
1432 garray_T *stack = &cctx->ctx_type_stack;
1433
1434 RETURN_OK_IF_SKIP(cctx);
1435 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1436 return FAIL;
1437 isn->isn_arg.jump.jump_when = when;
1438 isn->isn_arg.jump.jump_where = where;
1439
1440 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1441 --stack->ga_len;
1442
1443 return OK;
1444}
1445
1446/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001447 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1448 */
1449 int
1450generate_WHILE(cctx_T *cctx, int funcref_idx)
1451{
1452 isn_T *isn;
1453 garray_T *stack = &cctx->ctx_type_stack;
1454
1455 RETURN_OK_IF_SKIP(cctx);
1456 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1457 return FAIL;
1458 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1459 isn->isn_arg.whileloop.while_end = 0; // filled in later
1460
1461 if (stack->ga_len > 0)
1462 --stack->ga_len;
1463
1464 return OK;
1465}
1466
1467/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001468 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001469 */
1470 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001471generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001472{
1473 isn_T *isn;
1474
1475 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001476 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001477 return FAIL;
1478 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1479 // jump_where is set later
1480 return OK;
1481}
1482
1483 int
1484generate_FOR(cctx_T *cctx, int loop_idx)
1485{
1486 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487
1488 RETURN_OK_IF_SKIP(cctx);
1489 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1490 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001491 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001492
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001493 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001494 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001495}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001496
1497 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001498generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001499{
1500 isn_T *isn;
1501
1502 RETURN_OK_IF_SKIP(cctx);
1503 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1504 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001505 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1506 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1507 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001508 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001509 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001510 return OK;
1511}
1512
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001513/*
1514 * Generate an ISN_TRYCONT instruction.
1515 */
1516 int
1517generate_TRYCONT(cctx_T *cctx, int levels, int where)
1518{
1519 isn_T *isn;
1520
1521 RETURN_OK_IF_SKIP(cctx);
1522 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1523 return FAIL;
1524 isn->isn_arg.trycont.tct_levels = levels;
1525 isn->isn_arg.trycont.tct_where = where;
1526
1527 return OK;
1528}
1529
Bram Moolenaar16900322022-09-08 19:51:45 +01001530/*
1531 * Check "argount" arguments and their types on the type stack.
1532 * Give an error and return FAIL if something is wrong.
1533 * When "method_call" is NULL no code is generated.
1534 */
1535 int
1536check_internal_func_args(
1537 cctx_T *cctx,
1538 int func_idx,
1539 int argcount,
1540 int method_call,
1541 type2_T **argtypes,
1542 type2_T *shuffled_argtypes)
1543{
1544 garray_T *stack = &cctx->ctx_type_stack;
1545 int argoff = check_internal_func(func_idx, argcount);
1546
1547 if (argoff < 0)
1548 return FAIL;
1549
1550 if (method_call && argoff > 1)
1551 {
1552 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1553
1554 if (isn == NULL)
1555 return FAIL;
1556 isn->isn_arg.shuffle.shfl_item = argcount;
1557 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1558 }
1559
1560 if (argcount > 0)
1561 {
1562 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1563
1564 // Check the types of the arguments.
1565 if (method_call && argoff > 1)
1566 {
1567 int i;
1568
1569 for (i = 0; i < argcount; ++i)
1570 shuffled_argtypes[i] = (i < argoff - 1)
1571 ? typep[i + 1]
1572 : (i == argoff - 1) ? typep[0] : typep[i];
1573 *argtypes = shuffled_argtypes;
1574 }
1575 else
1576 {
1577 int i;
1578
1579 for (i = 0; i < argcount; ++i)
1580 shuffled_argtypes[i] = typep[i];
1581 *argtypes = shuffled_argtypes;
1582 }
1583 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1584 cctx) == FAIL)
1585 return FAIL;
1586 }
1587 return OK;
1588}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001589
1590/*
1591 * Generate an ISN_BCALL instruction.
1592 * "method_call" is TRUE for "value->method()"
1593 * Return FAIL if the number of arguments is wrong.
1594 */
1595 int
1596generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1597{
1598 isn_T *isn;
1599 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001600 type2_T *argtypes = NULL;
1601 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1602 type2_T *maptype = NULL;
1603 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001604 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001605
1606 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001607
1608 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1609 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001610 return FAIL;
1611
Bram Moolenaar16900322022-09-08 19:51:45 +01001612 if (internal_func_is_map(func_idx))
1613 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001614
1615 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1616 return FAIL;
1617 isn->isn_arg.bfunc.cbf_idx = func_idx;
1618 isn->isn_arg.bfunc.cbf_argcount = argcount;
1619
1620 // Drop the argument types and push the return type.
1621 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001622 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1623 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001624 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001625
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001626 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1627 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001628 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001629 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001630
1631 return OK;
1632}
1633
1634/*
1635 * Generate an ISN_LISTAPPEND instruction. Works like add().
1636 * Argument count is already checked.
1637 */
1638 int
1639generate_LISTAPPEND(cctx_T *cctx)
1640{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001641 type_T *list_type;
1642 type_T *item_type;
1643 type_T *expected;
1644
1645 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001646 // For checking the item type we use the declared type of the list and the
1647 // current type of the added item, adding a string to [1, 2] is OK.
1648 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001649 if (arg_type_modifiable(list_type, 1) == FAIL)
1650 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001651 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001652 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001653 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001654 return FAIL;
1655
1656 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1657 return FAIL;
1658
Bram Moolenaar078a4612022-01-04 15:17:03 +00001659 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001660 return OK;
1661}
1662
1663/*
1664 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1665 * Argument count is already checked.
1666 */
1667 int
1668generate_BLOBAPPEND(cctx_T *cctx)
1669{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001670 type_T *item_type;
1671
Bram Moolenaarfa103972022-09-29 19:14:42 +01001672 // Caller already checked that blob_type is a blob, check it is modifiable.
1673 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1674 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001675 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001676 if (need_type(item_type, &t_number, FALSE,
1677 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001678 return FAIL;
1679
1680 if (generate_instr(cctx, ISN_BLOBAPPEND) == 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_DCALL or ISN_UCALL instruction.
1689 * Return FAIL if the number of arguments is wrong.
1690 */
1691 int
1692generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1693{
1694 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695 int regular_args = ufunc->uf_args.ga_len;
1696 int argcount = pushed_argcount;
1697
1698 RETURN_OK_IF_SKIP(cctx);
1699 if (argcount > regular_args && !has_varargs(ufunc))
1700 {
1701 semsg(_(e_too_many_arguments_for_function_str),
1702 printable_func_name(ufunc));
1703 return FAIL;
1704 }
1705 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1706 {
1707 semsg(_(e_not_enough_arguments_for_function_str),
1708 printable_func_name(ufunc));
1709 return FAIL;
1710 }
1711
1712 if (ufunc->uf_def_status != UF_NOT_COMPILED
1713 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1714 {
1715 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001716 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001717
1718 for (i = 0; i < argcount; ++i)
1719 {
1720 type_T *expected;
1721 type_T *actual;
1722
Bram Moolenaar078a4612022-01-04 15:17:03 +00001723 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001724 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001725 && i >= regular_args - ufunc->uf_def_args.ga_len)
1726 {
1727 // assume v:none used for default argument value
1728 continue;
1729 }
1730 if (i < regular_args)
1731 {
1732 if (ufunc->uf_arg_types == NULL)
1733 continue;
1734 expected = ufunc->uf_arg_types[i];
1735 }
1736 else if (ufunc->uf_va_type == NULL
1737 || ufunc->uf_va_type == &t_list_any)
1738 // possibly a lambda or "...: any"
1739 expected = &t_any;
1740 else
1741 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001742 if (need_type(actual, expected, FALSE,
1743 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001744 {
1745 arg_type_mismatch(expected, actual, i + 1);
1746 return FAIL;
1747 }
1748 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001749 compile_type = get_compile_type(ufunc);
1750 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001751 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001752 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001753 return FAIL;
1754 }
1755 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1756 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001757 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001758 ufunc->uf_name);
1759 return FAIL;
1760 }
1761
1762 if ((isn = generate_instr(cctx,
1763 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1764 : ISN_UCALL)) == NULL)
1765 return FAIL;
1766 if (isn->isn_type == ISN_DCALL)
1767 {
1768 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1769 isn->isn_arg.dfunc.cdf_argcount = argcount;
1770 }
1771 else
1772 {
1773 // A user function may be deleted and redefined later, can't use the
1774 // ufunc pointer, need to look it up again at runtime.
1775 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1776 isn->isn_arg.ufunc.cuf_argcount = argcount;
1777 }
1778
Bram Moolenaar078a4612022-01-04 15:17:03 +00001779 // drop the argument types
1780 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001781
Bram Moolenaar078a4612022-01-04 15:17:03 +00001782 // add return type
1783 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001784}
1785
1786/*
1787 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1788 */
1789 int
1790generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1791{
1792 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001793
1794 RETURN_OK_IF_SKIP(cctx);
1795 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1796 return FAIL;
1797 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1798 isn->isn_arg.ufunc.cuf_argcount = argcount;
1799
Bram Moolenaar078a4612022-01-04 15:17:03 +00001800 // drop the argument types
1801 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001802
Bram Moolenaar078a4612022-01-04 15:17:03 +00001803 // add return value
1804 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001805}
1806
1807/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001808 * Check the arguments of function "type" against the types on the stack.
1809 * Returns OK or FAIL;
1810 */
1811 int
1812check_func_args_from_type(
1813 cctx_T *cctx,
1814 type_T *type,
1815 int argcount,
1816 int at_top,
1817 char_u *name)
1818{
1819 if (type->tt_argcount != -1)
1820 {
1821 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1822
1823 if (argcount < type->tt_min_argcount - varargs)
1824 {
1825 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1826 return FAIL;
1827 }
1828 if (!varargs && argcount > type->tt_argcount)
1829 {
1830 emsg_funcname(e_too_many_arguments_for_function_str, name);
1831 return FAIL;
1832 }
1833 if (type->tt_args != NULL)
1834 {
1835 int i;
1836
1837 for (i = 0; i < argcount; ++i)
1838 {
1839 int offset = -argcount + i - (at_top ? 0 : 1);
1840 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1841 type_T *expected;
1842
1843 if (varargs && i >= type->tt_argcount - 1)
1844 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1845 else if (i >= type->tt_min_argcount
1846 && actual->tt_type == VAR_SPECIAL)
1847 expected = &t_any;
1848 else
1849 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001850 if (need_type(actual, expected, FALSE,
1851 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001852 {
1853 arg_type_mismatch(expected, actual, i + 1);
1854 return FAIL;
1855 }
1856 }
1857 }
1858 }
1859
1860 return OK;
1861}
1862/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001863 * Generate an ISN_PCALL instruction.
1864 * "type" is the type of the FuncRef.
1865 */
1866 int
1867generate_PCALL(
1868 cctx_T *cctx,
1869 int argcount,
1870 char_u *name,
1871 type_T *type,
1872 int at_top)
1873{
1874 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875 type_T *ret_type;
1876
1877 RETURN_OK_IF_SKIP(cctx);
1878
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001879 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001880 ret_type = &t_any;
1881 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1882 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001883 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1884 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001885
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001886 ret_type = type->tt_member;
1887 if (ret_type == &t_unknown)
1888 // return type not known yet, use a runtime check
1889 ret_type = &t_any;
1890 }
1891 else
1892 {
1893 semsg(_(e_not_callable_type_str), name);
1894 return FAIL;
1895 }
1896
1897 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1898 return FAIL;
1899 isn->isn_arg.pfunc.cpf_top = at_top;
1900 isn->isn_arg.pfunc.cpf_argcount = argcount;
1901
Bram Moolenaar078a4612022-01-04 15:17:03 +00001902 // drop the arguments and the funcref/partial
1903 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001904
Bram Moolenaar078a4612022-01-04 15:17:03 +00001905 // push the return value
1906 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001907
1908 // If partial is above the arguments it must be cleared and replaced with
1909 // the return value.
1910 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1911 return FAIL;
1912
1913 return OK;
1914}
1915
1916/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001917 * Generate an ISN_DEFER instruction.
1918 */
1919 int
1920generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1921{
1922 isn_T *isn;
1923
1924 RETURN_OK_IF_SKIP(cctx);
1925 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1926 return FAIL;
1927 isn->isn_arg.defer.defer_var_idx = var_idx;
1928 isn->isn_arg.defer.defer_argcount = argcount;
1929 return OK;
1930}
1931
1932/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001933 * Generate an ISN_STRINGMEMBER instruction.
1934 */
1935 int
1936generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1937{
1938 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001939 type_T *type;
1940
1941 RETURN_OK_IF_SKIP(cctx);
1942 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1943 return FAIL;
1944 isn->isn_arg.string = vim_strnsave(name, len);
1945
1946 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001947 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001948 if (type->tt_type != VAR_DICT
1949 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001950 {
1951 char *tofree;
1952
1953 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1954 name, type_name(type, &tofree));
1955 vim_free(tofree);
1956 return FAIL;
1957 }
1958 // change dict type to dict member type
1959 if (type->tt_type == VAR_DICT)
1960 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001961 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00001962 ? &t_any : type->tt_member;
1963 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964 }
1965
1966 return OK;
1967}
1968
1969/*
1970 * Generate an ISN_ECHO instruction.
1971 */
1972 int
1973generate_ECHO(cctx_T *cctx, int with_white, int count)
1974{
1975 isn_T *isn;
1976
1977 RETURN_OK_IF_SKIP(cctx);
1978 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1979 return FAIL;
1980 isn->isn_arg.echo.echo_with_white = with_white;
1981 isn->isn_arg.echo.echo_count = count;
1982
1983 return OK;
1984}
1985
1986/*
1987 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1988 */
1989 int
1990generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1991{
1992 isn_T *isn;
1993
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01001994 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001995 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1996 return FAIL;
1997 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001998 return OK;
1999}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002000
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002001/*
2002 * Generate an ISN_ECHOWINDOW instruction
2003 */
2004 int
2005generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2006{
2007 isn_T *isn;
2008
2009 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2010 return FAIL;
2011 isn->isn_arg.echowin.ewin_count = count;
2012 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002013 return OK;
2014}
2015
2016/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002017 * Generate an ISN_SOURCE instruction.
2018 */
2019 int
2020generate_SOURCE(cctx_T *cctx, int sid)
2021{
2022 isn_T *isn;
2023
2024 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2025 return FAIL;
2026 isn->isn_arg.number = sid;
2027
2028 return OK;
2029}
2030
2031/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002032 * Generate an ISN_PUT instruction.
2033 */
2034 int
2035generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2036{
2037 isn_T *isn;
2038
2039 RETURN_OK_IF_SKIP(cctx);
2040 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2041 return FAIL;
2042 isn->isn_arg.put.put_regname = regname;
2043 isn->isn_arg.put.put_lnum = lnum;
2044 return OK;
2045}
2046
2047/*
2048 * Generate an EXEC instruction that takes a string argument.
2049 * A copy is made of "line".
2050 */
2051 int
2052generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2053{
2054 isn_T *isn;
2055
2056 RETURN_OK_IF_SKIP(cctx);
2057 if ((isn = generate_instr(cctx, isntype)) == NULL)
2058 return FAIL;
2059 isn->isn_arg.string = vim_strsave(line);
2060 return OK;
2061}
2062
2063/*
2064 * Generate an EXEC instruction that takes a string argument.
2065 * "str" must be allocated, it is consumed.
2066 */
2067 int
2068generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2069{
2070 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002071 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002072
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002073 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002074 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002075 if ((isn = generate_instr(cctx, isntype)) == NULL)
2076 ret = FAIL;
2077 else
2078 {
2079 isn->isn_arg.string = str;
2080 return OK;
2081 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002082 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002083 vim_free(str);
2084 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002085}
2086
2087 int
2088generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2089{
2090 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091
2092 RETURN_OK_IF_SKIP(cctx);
2093 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2094 return FAIL;
2095 isn->isn_arg.string = vim_strsave(line);
2096
Bram Moolenaar078a4612022-01-04 15:17:03 +00002097 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002098}
2099
2100 int
2101generate_EXECCONCAT(cctx_T *cctx, int count)
2102{
2103 isn_T *isn;
2104
2105 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2106 return FAIL;
2107 isn->isn_arg.number = count;
2108 return OK;
2109}
2110
2111/*
2112 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2113 */
2114 int
2115generate_RANGE(cctx_T *cctx, char_u *range)
2116{
2117 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002118
2119 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2120 return FAIL;
2121 isn->isn_arg.string = range;
2122
Bram Moolenaar078a4612022-01-04 15:17:03 +00002123 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002124}
2125
2126 int
2127generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2128{
2129 isn_T *isn;
2130
2131 RETURN_OK_IF_SKIP(cctx);
2132 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2133 return FAIL;
2134 isn->isn_arg.unpack.unp_count = var_count;
2135 isn->isn_arg.unpack.unp_semicolon = semicolon;
2136 return OK;
2137}
2138
2139/*
2140 * Generate an instruction for any command modifiers.
2141 */
2142 int
2143generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2144{
2145 isn_T *isn;
2146
2147 if (has_cmdmod(cmod, FALSE))
2148 {
2149 cctx->ctx_has_cmdmod = TRUE;
2150
2151 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2152 return FAIL;
2153 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2154 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2155 return FAIL;
2156 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2157 // filter program now belongs to the instruction
2158 cmod->cmod_filter_regmatch.regprog = NULL;
2159 }
2160
2161 return OK;
2162}
2163
2164 int
2165generate_undo_cmdmods(cctx_T *cctx)
2166{
2167 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2168 return FAIL;
2169 cctx->ctx_has_cmdmod = FALSE;
2170 return OK;
2171}
2172
2173/*
2174 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002175 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002176 * Return FAIL when out of memory.
2177 */
2178 int
2179generate_store_var(
2180 cctx_T *cctx,
2181 assign_dest_T dest,
2182 int opt_flags,
2183 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002184 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002185 char_u *name,
2186 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002187{
2188 switch (dest)
2189 {
2190 case dest_option:
2191 return generate_STOREOPT(cctx, ISN_STOREOPT,
2192 skip_option_env_lead(name), opt_flags);
2193 case dest_func_option:
2194 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2195 skip_option_env_lead(name), opt_flags);
2196 case dest_global:
2197 // include g: with the name, easier to execute that way
2198 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2199 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2200 case dest_buffer:
2201 // include b: with the name, easier to execute that way
2202 return generate_STORE(cctx, ISN_STOREB, 0, name);
2203 case dest_window:
2204 // include w: with the name, easier to execute that way
2205 return generate_STORE(cctx, ISN_STOREW, 0, name);
2206 case dest_tab:
2207 // include t: with the name, easier to execute that way
2208 return generate_STORE(cctx, ISN_STORET, 0, name);
2209 case dest_env:
2210 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2211 case dest_reg:
2212 return generate_STORE(cctx, ISN_STOREREG,
2213 name[1] == '@' ? '"' : name[1], NULL);
2214 case dest_vimvar:
2215 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2216 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002217 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002218 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2219 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2220 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002221 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002222 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002223
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002224 if (SCRIPT_ID_VALID(scriptvar_sid)
2225 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2226 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2227 == NULL)
2228 {
2229 // "import autoload './dir/script.vim'" - load script
2230 // first
2231 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2232 return FAIL;
2233 isn_type = ISN_STOREEXPORT;
2234 }
2235
2236 // "s:" may be included in the name.
2237 return generate_OLDSCRIPT(cctx, isn_type, name,
2238 scriptvar_sid, type);
2239 }
2240 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002241 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002242 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002243 case dest_class_member:
2244 return generate_CLASSMEMBER(cctx, FALSE,
2245 lhs->lhs_class, lhs->lhs_classmember_idx);
2246
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002247 case dest_local:
2248 case dest_expr:
2249 // cannot happen
2250 break;
2251 }
2252 return FAIL;
2253}
2254
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002255/*
2256 * Return TRUE when inside a "for" or "while" loop.
2257 */
2258 int
2259inside_loop_scope(cctx_T *cctx)
2260{
2261 scope_T *scope = cctx->ctx_scope;
2262
2263 for (;;)
2264 {
2265 if (scope == NULL)
2266 break;
2267 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2268 return TRUE;
2269 scope = scope->se_outer;
2270 }
2271 return FALSE;
2272}
2273
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002274 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002275generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002276{
2277 if (lhs->lhs_dest != dest_local)
2278 return generate_store_var(cctx, lhs->lhs_dest,
2279 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002280 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002281
2282 if (lhs->lhs_lvar != NULL)
2283 {
2284 garray_T *instr = &cctx->ctx_instr;
2285 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2286
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002287 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2288 // ISN_STORENR.
2289 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002290 if (lhs->lhs_lvar->lv_from_outer == 0
2291 && instr->ga_len == instr_count + 1
2292 && isn->isn_type == ISN_PUSHNR)
2293 {
2294 varnumber_T val = isn->isn_arg.number;
2295 garray_T *stack = &cctx->ctx_type_stack;
2296
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002297 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002298 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002299 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002300 --instr->ga_len;
2301 }
2302 else
2303 {
2304 isn->isn_type = ISN_STORENR;
2305 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2306 isn->isn_arg.storenr.stnr_val = val;
2307 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002308 if (stack->ga_len > 0)
2309 --stack->ga_len;
2310 }
2311 else if (lhs->lhs_lvar->lv_from_outer > 0)
2312 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002313 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002314 else
2315 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2316 }
2317 return OK;
2318}
2319
2320#if defined(FEAT_PROFILE) || defined(PROTO)
2321 void
2322may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2323{
2324 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2325 generate_instr(cctx, ISN_PROF_END);
2326}
2327#endif
2328
2329
2330/*
2331 * Delete an instruction, free what it contains.
2332 */
2333 void
2334delete_instr(isn_T *isn)
2335{
2336 switch (isn->isn_type)
2337 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002338 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002339 case ISN_DEF:
2340 case ISN_EXEC:
2341 case ISN_EXECRANGE:
2342 case ISN_EXEC_SPLIT:
2343 case ISN_LEGACY_EVAL:
2344 case ISN_LOADAUTO:
2345 case ISN_LOADB:
2346 case ISN_LOADENV:
2347 case ISN_LOADG:
2348 case ISN_LOADOPT:
2349 case ISN_LOADT:
2350 case ISN_LOADW:
2351 case ISN_LOCKUNLOCK:
2352 case ISN_PUSHEXC:
2353 case ISN_PUSHFUNC:
2354 case ISN_PUSHS:
2355 case ISN_RANGE:
2356 case ISN_STOREAUTO:
2357 case ISN_STOREB:
2358 case ISN_STOREENV:
2359 case ISN_STOREG:
2360 case ISN_STORET:
2361 case ISN_STOREW:
2362 case ISN_STRINGMEMBER:
2363 vim_free(isn->isn_arg.string);
2364 break;
2365
2366 case ISN_SUBSTITUTE:
2367 {
2368 int idx;
2369 isn_T *list = isn->isn_arg.subs.subs_instr;
2370
2371 vim_free(isn->isn_arg.subs.subs_cmd);
2372 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2373 delete_instr(list + idx);
2374 vim_free(list);
2375 }
2376 break;
2377
2378 case ISN_INSTR:
2379 {
2380 int idx;
2381 isn_T *list = isn->isn_arg.instr;
2382
2383 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2384 delete_instr(list + idx);
2385 vim_free(list);
2386 }
2387 break;
2388
2389 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002390 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002391 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002392 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 vim_free(isn->isn_arg.loadstore.ls_name);
2394 break;
2395
2396 case ISN_UNLET:
2397 case ISN_UNLETENV:
2398 vim_free(isn->isn_arg.unlet.ul_name);
2399 break;
2400
2401 case ISN_STOREOPT:
2402 case ISN_STOREFUNCOPT:
2403 vim_free(isn->isn_arg.storeopt.so_name);
2404 break;
2405
2406 case ISN_PUSHBLOB: // push blob isn_arg.blob
2407 blob_unref(isn->isn_arg.blob);
2408 break;
2409
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002410 case ISN_UCALL:
2411 vim_free(isn->isn_arg.ufunc.cuf_name);
2412 break;
2413
2414 case ISN_FUNCREF:
2415 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002416 funcref_T *funcref = &isn->isn_arg.funcref;
2417 funcref_extra_T *extra = funcref->fr_extra;
2418
2419 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002420 {
2421 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002422 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002423 ufunc_T *ufunc = dfunc->df_ufunc;
2424
2425 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2426 func_ptr_unref(ufunc);
2427 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002428 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002429 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002430 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431
2432 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002433 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002434 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002435 vim_free(name);
2436 }
2437 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002438 }
2439 }
2440 break;
2441
2442 case ISN_DCALL:
2443 {
2444 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002445 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002446
2447 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002448 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002449 func_ptr_unref(dfunc->df_ufunc);
2450 }
2451 break;
2452
2453 case ISN_NEWFUNC:
2454 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002455 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002456
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002457 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002458 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002459 ufunc_T *ufunc = find_func_even_dead(
2460 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002461
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002462 if (ufunc != NULL)
2463 {
2464 unlink_def_function(ufunc);
2465 func_ptr_unref(ufunc);
2466 }
2467
2468 vim_free(arg->nfa_lambda);
2469 vim_free(arg->nfa_global);
2470 vim_free(arg);
2471 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002472 }
2473 break;
2474
2475 case ISN_CHECKTYPE:
2476 case ISN_SETTYPE:
2477 free_type(isn->isn_arg.type.ct_type);
2478 break;
2479
2480 case ISN_CMDMOD:
2481 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002482 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002483 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2484 break;
2485
2486 case ISN_LOADSCRIPT:
2487 case ISN_STORESCRIPT:
2488 vim_free(isn->isn_arg.script.scriptref);
2489 break;
2490
Bram Moolenaard505d172022-12-18 21:42:55 +00002491 case ISN_LOAD_CLASSMEMBER:
2492 case ISN_STORE_CLASSMEMBER:
2493 class_unref(isn->isn_arg.classmember.cm_class);
2494 break;
2495
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002496 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002497 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002498 break;
2499
2500 case ISN_CEXPR_CORE:
2501 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2502 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2503 break;
2504
2505 case ISN_2BOOL:
2506 case ISN_2STRING:
2507 case ISN_2STRING_ANY:
2508 case ISN_ADDBLOB:
2509 case ISN_ADDLIST:
2510 case ISN_ANYINDEX:
2511 case ISN_ANYSLICE:
2512 case ISN_BCALL:
2513 case ISN_BLOBAPPEND:
2514 case ISN_BLOBINDEX:
2515 case ISN_BLOBSLICE:
2516 case ISN_CATCH:
2517 case ISN_CEXPR_AUCMD:
2518 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002519 case ISN_CLEARDICT:
2520 case ISN_CMDMOD_REV:
2521 case ISN_COMPAREANY:
2522 case ISN_COMPAREBLOB:
2523 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002524 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002525 case ISN_COMPAREDICT:
2526 case ISN_COMPAREFLOAT:
2527 case ISN_COMPAREFUNC:
2528 case ISN_COMPARELIST:
2529 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002530 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002531 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002532 case ISN_COMPARESPECIAL:
2533 case ISN_COMPARESTRING:
2534 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002535 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002536 case ISN_COND2BOOL:
2537 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002538 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002539 case ISN_DROP:
2540 case ISN_ECHO:
2541 case ISN_ECHOCONSOLE:
2542 case ISN_ECHOERR:
2543 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002544 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002545 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002546 case ISN_ENDTRY:
2547 case ISN_EXECCONCAT:
2548 case ISN_EXECUTE:
2549 case ISN_FINALLY:
2550 case ISN_FINISH:
2551 case ISN_FOR:
2552 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002553 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002554 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002555 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002556 case ISN_JUMP_IF_ARG_SET:
2557 case ISN_LISTAPPEND:
2558 case ISN_LISTINDEX:
2559 case ISN_LISTSLICE:
2560 case ISN_LOAD:
2561 case ISN_LOADBDICT:
2562 case ISN_LOADGDICT:
2563 case ISN_LOADOUTER:
2564 case ISN_LOADREG:
2565 case ISN_LOADTDICT:
2566 case ISN_LOADV:
2567 case ISN_LOADWDICT:
2568 case ISN_LOCKCONST:
2569 case ISN_MEMBER:
2570 case ISN_NEGATENR:
2571 case ISN_NEWDICT:
2572 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002573 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002574 case ISN_OPANY:
2575 case ISN_OPFLOAT:
2576 case ISN_OPNR:
2577 case ISN_PCALL:
2578 case ISN_PCALL_END:
2579 case ISN_PROF_END:
2580 case ISN_PROF_START:
2581 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002582 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002583 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002584 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002585 case ISN_PUSHNR:
2586 case ISN_PUSHSPEC:
2587 case ISN_PUT:
2588 case ISN_REDIREND:
2589 case ISN_REDIRSTART:
2590 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002591 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002592 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002593 case ISN_SHUFFLE:
2594 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002595 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002596 case ISN_STORE:
2597 case ISN_STOREINDEX:
2598 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002599 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002600 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002601 case ISN_STORERANGE:
2602 case ISN_STOREREG:
2603 case ISN_STOREV:
2604 case ISN_STRINDEX:
2605 case ISN_STRSLICE:
2606 case ISN_THROW:
2607 case ISN_TRYCONT:
2608 case ISN_UNLETINDEX:
2609 case ISN_UNLETRANGE:
2610 case ISN_UNPACK:
2611 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002612 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002613 // nothing allocated
2614 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002615 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002616}
2617
2618 void
2619clear_instr_ga(garray_T *gap)
2620{
2621 int idx;
2622
2623 for (idx = 0; idx < gap->ga_len; ++idx)
2624 delete_instr(((isn_T *)gap->ga_data) + idx);
2625 ga_clear(gap);
2626}
2627
2628
2629#endif // defined(FEAT_EVAL)