blob: 8bd2485f6c6240f07191b5b87fe7f7027405a6a7 [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(
257 cctx_T *cctx,
258 vartype_T vartype,
259 type_T *type1,
260 type_T *type2,
261 exprtype_T expr_type)
262{
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;
419 default: isntype = ISN_COMPAREANY; break;
420 }
421 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000422 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
423 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
424 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
425 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
426 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000427 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000428 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000429 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000430 if ((vartype1 == VAR_SPECIAL
431 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
432 : type1 == &t_none)
433 && vartype2 != VAR_STRING)
434 || (vartype2 == VAR_SPECIAL
435 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
436 : type2 == &t_none)
437 && vartype1 != VAR_STRING))
438 {
439 semsg(_(e_cannot_compare_str_with_str),
440 vartype_name(vartype1), vartype_name(vartype2));
441 return ISN_DROP;
442 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000443 // although comparing null with number, float or bool is not useful, we
444 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000445 isntype = ISN_COMPARENULL;
446 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000447
448 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
449 && (isntype == ISN_COMPAREBOOL
450 || isntype == ISN_COMPARESPECIAL
451 || isntype == ISN_COMPARENR
452 || isntype == ISN_COMPAREFLOAT))
453 {
454 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000455 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456 return ISN_DROP;
457 }
458 if (isntype == ISN_DROP
459 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000460 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
461 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000462 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000463 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000464 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
465 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000466 {
467 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000468 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469 return ISN_DROP;
470 }
471 return isntype;
472}
473
474 int
475check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
476{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000477 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000478 return FAIL;
479 return OK;
480}
481
482/*
483 * Generate an ISN_COMPARE* instruction with a boolean result.
484 */
485 int
486generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
487{
488 isntype_T isntype;
489 isn_T *isn;
490 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491
492 RETURN_OK_IF_SKIP(cctx);
493
494 // Get the known type of the two items on the stack. If they are matching
495 // use a type-specific instruction. Otherwise fall back to runtime type
496 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000497 isntype = get_compare_isn(exprtype, NULL, NULL,
498 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000499 if (isntype == ISN_DROP)
500 return FAIL;
501
502 if ((isn = generate_instr(cctx, isntype)) == NULL)
503 return FAIL;
504 isn->isn_arg.op.op_type = exprtype;
505 isn->isn_arg.op.op_ic = ic;
506
507 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100508 --stack->ga_len;
509 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000510
511 return OK;
512}
513
514/*
LemonBoy372bcce2022-04-25 12:43:20 +0100515 * Generate an ISN_CONCAT instruction.
516 * "count" is the number of stack elements to join together and it must be
517 * greater or equal to one.
518 * The caller ensures all the "count" elements on the stack have the right type.
519 */
520 int
521generate_CONCAT(cctx_T *cctx, int count)
522{
523 isn_T *isn;
524 garray_T *stack = &cctx->ctx_type_stack;
525
526 RETURN_OK_IF_SKIP(cctx);
527
LemonBoy372bcce2022-04-25 12:43:20 +0100528 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
529 return FAIL;
530 isn->isn_arg.number = count;
531
532 // drop the argument types
533 stack->ga_len -= count - 1;
534
535 return OK;
536}
537
538/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539 * Generate an ISN_2BOOL instruction.
540 * "offset" is the offset in the type stack.
541 */
542 int
543generate_2BOOL(cctx_T *cctx, int invert, int offset)
544{
545 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000546
547 RETURN_OK_IF_SKIP(cctx);
548 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
549 return FAIL;
550 isn->isn_arg.tobool.invert = invert;
551 isn->isn_arg.tobool.offset = offset;
552
553 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000554 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000555
556 return OK;
557}
558
559/*
560 * Generate an ISN_COND2BOOL instruction.
561 */
562 int
563generate_COND2BOOL(cctx_T *cctx)
564{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000565 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100566 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000567 return FAIL;
568
569 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000570 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000571
572 return OK;
573}
574
575 int
576generate_TYPECHECK(
577 cctx_T *cctx,
578 type_T *expected,
579 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100580 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000581 int argidx)
582{
583 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000584
585 RETURN_OK_IF_SKIP(cctx);
586 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
587 return FAIL;
588 isn->isn_arg.type.ct_type = alloc_type(expected);
589 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100590 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000591 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
592
593 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000594 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000595
596 return OK;
597}
598
599 int
600generate_SETTYPE(
601 cctx_T *cctx,
602 type_T *expected)
603{
604 isn_T *isn;
605
606 RETURN_OK_IF_SKIP(cctx);
607 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
608 return FAIL;
609 isn->isn_arg.type.ct_type = alloc_type(expected);
610 return OK;
611}
612
613/*
614 * Generate a PUSH instruction for "tv".
615 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000616 */
617 int
618generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
619{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100620 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000621 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100622 case VAR_BOOL:
623 generate_PUSHBOOL(cctx, tv->vval.v_number);
624 break;
625 case VAR_SPECIAL:
626 generate_PUSHSPEC(cctx, tv->vval.v_number);
627 break;
628 case VAR_NUMBER:
629 generate_PUSHNR(cctx, tv->vval.v_number);
630 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100631 case VAR_FLOAT:
632 generate_PUSHF(cctx, tv->vval.v_float);
633 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100634 case VAR_BLOB:
635 generate_PUSHBLOB(cctx, tv->vval.v_blob);
636 tv->vval.v_blob = NULL;
637 break;
638 case VAR_LIST:
639 if (tv->vval.v_list != NULL)
640 iemsg("non-empty list constant not supported");
641 generate_NEWLIST(cctx, 0, TRUE);
642 break;
643 case VAR_DICT:
644 if (tv->vval.v_dict != NULL)
645 iemsg("non-empty dict constant not supported");
646 generate_NEWDICT(cctx, 0, TRUE);
647 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000648#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100649 case VAR_JOB:
650 if (tv->vval.v_job != NULL)
651 iemsg("non-null job constant not supported");
652 generate_PUSHJOB(cctx);
653 break;
654 case VAR_CHANNEL:
655 if (tv->vval.v_channel != NULL)
656 iemsg("non-null channel constant not supported");
657 generate_PUSHCHANNEL(cctx);
658 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000659#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100660 case VAR_FUNC:
661 if (tv->vval.v_string != NULL)
662 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100663 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100664 break;
665 case VAR_PARTIAL:
666 if (tv->vval.v_partial != NULL)
667 iemsg("non-null partial constant not supported");
668 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
669 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000670 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100671 break;
672 case VAR_STRING:
673 generate_PUSHS(cctx, &tv->vval.v_string);
674 tv->vval.v_string = NULL;
675 break;
676 default:
677 siemsg("constant type %d not supported", tv->v_type);
678 clear_tv(tv);
679 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000680 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100681 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000682 return OK;
683}
684
685/*
686 * Generate an ISN_PUSHNR instruction.
687 */
688 int
689generate_PUSHNR(cctx_T *cctx, varnumber_T number)
690{
691 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000692
693 RETURN_OK_IF_SKIP(cctx);
694 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
695 return FAIL;
696 isn->isn_arg.number = number;
697
698 if (number == 0 || number == 1)
699 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000700 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000701 return OK;
702}
703
704/*
705 * Generate an ISN_PUSHBOOL instruction.
706 */
707 int
708generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
709{
710 isn_T *isn;
711
712 RETURN_OK_IF_SKIP(cctx);
713 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
714 return FAIL;
715 isn->isn_arg.number = number;
716
717 return OK;
718}
719
720/*
721 * Generate an ISN_PUSHSPEC instruction.
722 */
723 int
724generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
725{
726 isn_T *isn;
727
728 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000729 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
730 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000731 return FAIL;
732 isn->isn_arg.number = number;
733
734 return OK;
735}
736
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737/*
738 * Generate an ISN_PUSHF instruction.
739 */
740 int
741generate_PUSHF(cctx_T *cctx, float_T fnumber)
742{
743 isn_T *isn;
744
745 RETURN_OK_IF_SKIP(cctx);
746 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
747 return FAIL;
748 isn->isn_arg.fnumber = fnumber;
749
750 return OK;
751}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752
753/*
754 * Generate an ISN_PUSHS instruction.
755 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100756 * Note that if "str" is used in the instruction OK is returned and "*str" is
757 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000758 */
759 int
760generate_PUSHS(cctx_T *cctx, char_u **str)
761{
762 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100763 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000764
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100765 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000766 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100767 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
768 ret = FAIL;
769 else
770 {
771 isn->isn_arg.string = str == NULL ? NULL : *str;
772 return OK;
773 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000774 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100775 if (str != NULL)
776 VIM_CLEAR(*str);
777 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000778}
779
780/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000781 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782 */
783 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000784generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000785{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100787#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100788 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100791#else
792 emsg(_(e_channel_job_feature_not_available));
793 return FAIL;
794#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000795}
796
797/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000798 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000799 */
800 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000801generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000802{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000803 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100804#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100805 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000807 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100808#else
809 emsg(_(e_channel_job_feature_not_available));
810 return FAIL;
811#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000812}
813
814/*
815 * Generate an ISN_PUSHBLOB instruction.
816 * Consumes "blob".
817 */
818 int
819generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
820{
821 isn_T *isn;
822
823 RETURN_OK_IF_SKIP(cctx);
824 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
825 return FAIL;
826 isn->isn_arg.blob = blob;
827
828 return OK;
829}
830
831/*
832 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100833 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
834 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000835 */
836 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100837generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000838{
839 isn_T *isn;
840 char_u *funcname;
841
842 RETURN_OK_IF_SKIP(cctx);
843 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
844 return FAIL;
845 if (name == NULL)
846 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100847 else if (!may_prefix
848 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000849 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000850 funcname = vim_strsave(name);
851 else
852 {
853 funcname = alloc(STRLEN(name) + 3);
854 if (funcname != NULL)
855 {
856 STRCPY(funcname, "g:");
857 STRCPY(funcname + 2, name);
858 }
859 }
860
861 isn->isn_arg.string = funcname;
862 return OK;
863}
864
865/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000866 * Generate an ISN_AUTOLOAD instruction.
867 */
868 int
869generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
870{
871 isn_T *isn;
872
873 RETURN_OK_IF_SKIP(cctx);
874 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
875 return FAIL;
876 isn->isn_arg.string = vim_strsave(name);
877 if (isn->isn_arg.string == NULL)
878 return FAIL;
879 return OK;
880}
881
882/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883 * Generate an ISN_GETITEM instruction with "index".
884 * "with_op" is TRUE for "+=" and other operators, the stack has the current
885 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100886 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000887 */
888 int
889generate_GETITEM(cctx_T *cctx, int index, int with_op)
890{
891 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000892 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893 type_T *item_type = &t_any;
894
895 RETURN_OK_IF_SKIP(cctx);
896
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 item_type = type->tt_member;
898 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
899 return FAIL;
900 isn->isn_arg.getitem.gi_index = index;
901 isn->isn_arg.getitem.gi_with_op = with_op;
902
903 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000904 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000905}
906
907/*
908 * Generate an ISN_SLICE instruction with "count".
909 */
910 int
911generate_SLICE(cctx_T *cctx, int count)
912{
913 isn_T *isn;
914
915 RETURN_OK_IF_SKIP(cctx);
916 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
917 return FAIL;
918 isn->isn_arg.number = count;
919 return OK;
920}
921
922/*
923 * Generate an ISN_CHECKLEN instruction with "min_len".
924 */
925 int
926generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
927{
928 isn_T *isn;
929
930 RETURN_OK_IF_SKIP(cctx);
931
932 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
933 return FAIL;
934 isn->isn_arg.checklen.cl_min_len = min_len;
935 isn->isn_arg.checklen.cl_more_OK = more_OK;
936
937 return OK;
938}
939
940/*
941 * Generate an ISN_STORE instruction.
942 */
943 int
944generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
945{
946 isn_T *isn;
947
948 RETURN_OK_IF_SKIP(cctx);
949 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
950 return FAIL;
951 if (name != NULL)
952 isn->isn_arg.string = vim_strsave(name);
953 else
954 isn->isn_arg.number = idx;
955
956 return OK;
957}
958
959/*
Bram Moolenaard505d172022-12-18 21:42:55 +0000960 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
961 * ("load" == FALSE) instruction.
962 */
963 int
964generate_CLASSMEMBER(
965 cctx_T *cctx,
966 int load,
967 class_T *cl,
968 int idx)
969{
970 isn_T *isn;
971
972 RETURN_OK_IF_SKIP(cctx);
973 if (load)
974 {
975 ocmember_T *m = &cl->class_class_members[idx];
976 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
977 }
978 else
979 {
980 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
981 }
982 if (isn == NULL)
983 return FAIL;
984 isn->isn_arg.classmember.cm_class = cl;
985 ++cl->class_refcount;
986 isn->isn_arg.classmember.cm_idx = idx;
987
988 return OK;
989}
990
991/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000992 * Generate an ISN_STOREOUTER instruction.
993 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000994 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100995generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000996{
997 isn_T *isn;
998
999 RETURN_OK_IF_SKIP(cctx);
1000 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1001 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001002 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1003 {
1004 // Store a variable defined in a loop. A copy will be made at the end
1005 // of the loop. TODO: how about deeper nesting?
1006 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1007 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1008 }
1009 else
1010 {
1011 isn->isn_arg.outer.outer_idx = idx;
1012 isn->isn_arg.outer.outer_depth = level;
1013 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001014
1015 return OK;
1016}
1017
1018/*
1019 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1020 */
1021 int
1022generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1023{
1024 isn_T *isn;
1025
1026 RETURN_OK_IF_SKIP(cctx);
1027 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1028 return FAIL;
1029 isn->isn_arg.storenr.stnr_idx = idx;
1030 isn->isn_arg.storenr.stnr_val = value;
1031
1032 return OK;
1033}
1034
1035/*
1036 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1037 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001038 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001039generate_STOREOPT(
1040 cctx_T *cctx,
1041 isntype_T isn_type,
1042 char_u *name,
1043 int opt_flags)
1044{
1045 isn_T *isn;
1046
1047 RETURN_OK_IF_SKIP(cctx);
1048 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1049 return FAIL;
1050 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1051 isn->isn_arg.storeopt.so_flags = opt_flags;
1052
1053 return OK;
1054}
1055
1056/*
1057 * Generate an ISN_LOAD or similar instruction.
1058 */
1059 int
1060generate_LOAD(
1061 cctx_T *cctx,
1062 isntype_T isn_type,
1063 int idx,
1064 char_u *name,
1065 type_T *type)
1066{
1067 isn_T *isn;
1068
1069 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001070 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 return FAIL;
1072 if (name != NULL)
1073 isn->isn_arg.string = vim_strsave(name);
1074 else
1075 isn->isn_arg.number = idx;
1076
1077 return OK;
1078}
1079
1080/*
1081 * Generate an ISN_LOADOUTER instruction
1082 */
1083 int
1084generate_LOADOUTER(
1085 cctx_T *cctx,
1086 int idx,
1087 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001088 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001089 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001090 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_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001096 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001097 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1098 {
1099 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001100 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001101 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001102 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001103 }
1104 else
1105 {
1106 isn->isn_arg.outer.outer_idx = idx;
1107 isn->isn_arg.outer.outer_depth = nesting;
1108 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001109
1110 return OK;
1111}
1112
1113/*
1114 * Generate an ISN_LOADV instruction for v:var.
1115 */
1116 int
1117generate_LOADV(
1118 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001119 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001120{
1121 int di_flags;
1122 int vidx = find_vim_var(name, &di_flags);
1123 type_T *type;
1124
1125 RETURN_OK_IF_SKIP(cctx);
1126 if (vidx < 0)
1127 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001128 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001129 return FAIL;
1130 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001131 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001132 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1133}
1134
1135/*
1136 * Generate an ISN_UNLET instruction.
1137 */
1138 int
1139generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1140{
1141 isn_T *isn;
1142
1143 RETURN_OK_IF_SKIP(cctx);
1144 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1145 return FAIL;
1146 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1147 isn->isn_arg.unlet.ul_forceit = forceit;
1148
1149 return OK;
1150}
1151
1152/*
1153 * Generate an ISN_LOCKCONST instruction.
1154 */
1155 int
1156generate_LOCKCONST(cctx_T *cctx)
1157{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001158 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001159 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001160 return FAIL;
1161 return OK;
1162}
1163
1164/*
1165 * Generate an ISN_LOADS instruction.
1166 */
1167 int
1168generate_OLDSCRIPT(
1169 cctx_T *cctx,
1170 isntype_T isn_type,
1171 char_u *name,
1172 int sid,
1173 type_T *type)
1174{
1175 isn_T *isn;
1176
1177 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001178 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179 isn = generate_instr_type(cctx, isn_type, type);
1180 else
1181 isn = generate_instr_drop(cctx, isn_type, 1);
1182 if (isn == NULL)
1183 return FAIL;
1184 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1185 isn->isn_arg.loadstore.ls_sid = sid;
1186
1187 return OK;
1188}
1189
1190/*
1191 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1192 */
1193 int
1194generate_VIM9SCRIPT(
1195 cctx_T *cctx,
1196 isntype_T isn_type,
1197 int sid,
1198 int idx,
1199 type_T *type)
1200{
1201 isn_T *isn;
1202 scriptref_T *sref;
1203 scriptitem_T *si = SCRIPT_ITEM(sid);
1204
1205 RETURN_OK_IF_SKIP(cctx);
1206 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001207 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001208 else
1209 isn = generate_instr_drop(cctx, isn_type, 1);
1210 if (isn == NULL)
1211 return FAIL;
1212
1213 // This requires three arguments, which doesn't fit in an instruction, thus
1214 // we need to allocate a struct for this.
1215 sref = ALLOC_ONE(scriptref_T);
1216 if (sref == NULL)
1217 return FAIL;
1218 isn->isn_arg.script.scriptref = sref;
1219 sref->sref_sid = sid;
1220 sref->sref_idx = idx;
1221 sref->sref_seq = si->sn_script_seq;
1222 sref->sref_type = type;
1223 return OK;
1224}
1225
1226/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001227 * Generate an ISN_NEWLIST instruction for "count" items.
1228 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001229 */
1230 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001231generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001232{
1233 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001234 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001235 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001236 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001237
1238 RETURN_OK_IF_SKIP(cctx);
1239 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1240 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001241 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001242
Bram Moolenaar078a4612022-01-04 15:17:03 +00001243 // Get the member type and the declared member type from all the items on
1244 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001245 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001246 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001247 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001248
1249 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001250 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251
1252 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001253 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001254}
1255
1256/*
1257 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001258 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001259 */
1260 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001261generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001262{
1263 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001264 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001265 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001266 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001267
1268 RETURN_OK_IF_SKIP(cctx);
1269 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1270 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001271 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001272
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001273 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001274 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001275 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001276
1277 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001278 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001279
1280 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001281 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282}
1283
1284/*
1285 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001286 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001287 * If variables were declared inside a loop "loop_var_idx" is the index of the
1288 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001289 */
1290 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001291generate_FUNCREF(
1292 cctx_T *cctx,
1293 ufunc_T *ufunc,
1294 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001295{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001296 isn_T *isn;
1297 type_T *type;
1298 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001299 loopvarinfo_T loopinfo;
1300 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001301
1302 RETURN_OK_IF_SKIP(cctx);
1303 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1304 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001305 if (isnp != NULL)
1306 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001307
Bram Moolenaarcc341812022-09-19 15:54:34 +01001308 has_vars = get_loop_var_info(cctx, &loopinfo);
1309 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001310 {
1311 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1312 if (extra == NULL)
1313 return FAIL;
1314 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001315 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001316 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001317 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001318 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001319 else
1320 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001321
1322 // Reserve an extra variable to keep track of the number of closures
1323 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001324 cctx->ctx_has_closure = 1;
1325
1326 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001327 // the nested context, including this one. But not a function defined at
1328 // the script level.
1329 if ((ufunc->uf_flags & FC_CLOSURE)
1330 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001331 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1332
Bram Moolenaar078a4612022-01-04 15:17:03 +00001333 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1334 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001335}
1336
1337/*
1338 * Generate an ISN_NEWFUNC instruction.
1339 * "lambda_name" and "func_name" must be in allocated memory and will be
1340 * consumed.
1341 */
1342 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001343generate_NEWFUNC(
1344 cctx_T *cctx,
1345 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001346 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001347{
1348 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001349 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001350
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001351 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001352 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001353 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1354 ret = FAIL;
1355 else
1356 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001357 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1358
1359 if (arg == NULL)
1360 ret = FAIL;
1361 else
1362 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001363 // Reserve an extra variable to keep track of the number of
1364 // closures created.
1365 cctx->ctx_has_closure = 1;
1366
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001367 isn->isn_arg.newfunc.nf_arg = arg;
1368 arg->nfa_lambda = lambda_name;
1369 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001370 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001371 return OK;
1372 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001373 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001374 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001375 vim_free(lambda_name);
1376 vim_free(func_name);
1377 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001378}
1379
1380/*
1381 * Generate an ISN_DEF instruction: list functions
1382 */
1383 int
1384generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1385{
1386 isn_T *isn;
1387
1388 RETURN_OK_IF_SKIP(cctx);
1389 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1390 return FAIL;
1391 if (len > 0)
1392 {
1393 isn->isn_arg.string = vim_strnsave(name, len);
1394 if (isn->isn_arg.string == NULL)
1395 return FAIL;
1396 }
1397 return OK;
1398}
1399
1400/*
1401 * Generate an ISN_JUMP instruction.
1402 */
1403 int
1404generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1405{
1406 isn_T *isn;
1407 garray_T *stack = &cctx->ctx_type_stack;
1408
1409 RETURN_OK_IF_SKIP(cctx);
1410 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1411 return FAIL;
1412 isn->isn_arg.jump.jump_when = when;
1413 isn->isn_arg.jump.jump_where = where;
1414
1415 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1416 --stack->ga_len;
1417
1418 return OK;
1419}
1420
1421/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001422 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1423 */
1424 int
1425generate_WHILE(cctx_T *cctx, int funcref_idx)
1426{
1427 isn_T *isn;
1428 garray_T *stack = &cctx->ctx_type_stack;
1429
1430 RETURN_OK_IF_SKIP(cctx);
1431 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1432 return FAIL;
1433 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1434 isn->isn_arg.whileloop.while_end = 0; // filled in later
1435
1436 if (stack->ga_len > 0)
1437 --stack->ga_len;
1438
1439 return OK;
1440}
1441
1442/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001443 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001444 */
1445 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001446generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001447{
1448 isn_T *isn;
1449
1450 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001451 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001452 return FAIL;
1453 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1454 // jump_where is set later
1455 return OK;
1456}
1457
1458 int
1459generate_FOR(cctx_T *cctx, int loop_idx)
1460{
1461 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001462
1463 RETURN_OK_IF_SKIP(cctx);
1464 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1465 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001466 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001467
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001468 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001469 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001470}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001471
1472 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001473generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001474{
1475 isn_T *isn;
1476
1477 RETURN_OK_IF_SKIP(cctx);
1478 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1479 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001480 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1481 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1482 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001483 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001484 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001485 return OK;
1486}
1487
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001488/*
1489 * Generate an ISN_TRYCONT instruction.
1490 */
1491 int
1492generate_TRYCONT(cctx_T *cctx, int levels, int where)
1493{
1494 isn_T *isn;
1495
1496 RETURN_OK_IF_SKIP(cctx);
1497 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1498 return FAIL;
1499 isn->isn_arg.trycont.tct_levels = levels;
1500 isn->isn_arg.trycont.tct_where = where;
1501
1502 return OK;
1503}
1504
Bram Moolenaar16900322022-09-08 19:51:45 +01001505/*
1506 * Check "argount" arguments and their types on the type stack.
1507 * Give an error and return FAIL if something is wrong.
1508 * When "method_call" is NULL no code is generated.
1509 */
1510 int
1511check_internal_func_args(
1512 cctx_T *cctx,
1513 int func_idx,
1514 int argcount,
1515 int method_call,
1516 type2_T **argtypes,
1517 type2_T *shuffled_argtypes)
1518{
1519 garray_T *stack = &cctx->ctx_type_stack;
1520 int argoff = check_internal_func(func_idx, argcount);
1521
1522 if (argoff < 0)
1523 return FAIL;
1524
1525 if (method_call && argoff > 1)
1526 {
1527 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1528
1529 if (isn == NULL)
1530 return FAIL;
1531 isn->isn_arg.shuffle.shfl_item = argcount;
1532 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1533 }
1534
1535 if (argcount > 0)
1536 {
1537 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1538
1539 // Check the types of the arguments.
1540 if (method_call && argoff > 1)
1541 {
1542 int i;
1543
1544 for (i = 0; i < argcount; ++i)
1545 shuffled_argtypes[i] = (i < argoff - 1)
1546 ? typep[i + 1]
1547 : (i == argoff - 1) ? typep[0] : typep[i];
1548 *argtypes = shuffled_argtypes;
1549 }
1550 else
1551 {
1552 int i;
1553
1554 for (i = 0; i < argcount; ++i)
1555 shuffled_argtypes[i] = typep[i];
1556 *argtypes = shuffled_argtypes;
1557 }
1558 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1559 cctx) == FAIL)
1560 return FAIL;
1561 }
1562 return OK;
1563}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001564
1565/*
1566 * Generate an ISN_BCALL instruction.
1567 * "method_call" is TRUE for "value->method()"
1568 * Return FAIL if the number of arguments is wrong.
1569 */
1570 int
1571generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1572{
1573 isn_T *isn;
1574 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001575 type2_T *argtypes = NULL;
1576 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1577 type2_T *maptype = NULL;
1578 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001579 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001580
1581 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001582
1583 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1584 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001585 return FAIL;
1586
Bram Moolenaar16900322022-09-08 19:51:45 +01001587 if (internal_func_is_map(func_idx))
1588 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001589
1590 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1591 return FAIL;
1592 isn->isn_arg.bfunc.cbf_idx = func_idx;
1593 isn->isn_arg.bfunc.cbf_argcount = argcount;
1594
1595 // Drop the argument types and push the return type.
1596 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001597 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1598 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001599 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001600
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001601 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1602 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001603 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001604 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001605
1606 return OK;
1607}
1608
1609/*
1610 * Generate an ISN_LISTAPPEND instruction. Works like add().
1611 * Argument count is already checked.
1612 */
1613 int
1614generate_LISTAPPEND(cctx_T *cctx)
1615{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001616 type_T *list_type;
1617 type_T *item_type;
1618 type_T *expected;
1619
1620 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001621 // For checking the item type we use the declared type of the list and the
1622 // current type of the added item, adding a string to [1, 2] is OK.
1623 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001624 if (arg_type_modifiable(list_type, 1) == FAIL)
1625 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001626 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001627 expected = list_type->tt_member;
1628 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1629 return FAIL;
1630
1631 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1632 return FAIL;
1633
Bram Moolenaar078a4612022-01-04 15:17:03 +00001634 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001635 return OK;
1636}
1637
1638/*
1639 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1640 * Argument count is already checked.
1641 */
1642 int
1643generate_BLOBAPPEND(cctx_T *cctx)
1644{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001645 type_T *item_type;
1646
Bram Moolenaarfa103972022-09-29 19:14:42 +01001647 // Caller already checked that blob_type is a blob, check it is modifiable.
1648 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1649 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001650 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001651 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1652 return FAIL;
1653
1654 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1655 return FAIL;
1656
Bram Moolenaar078a4612022-01-04 15:17:03 +00001657 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001658 return OK;
1659}
1660
1661/*
1662 * Generate an ISN_DCALL or ISN_UCALL instruction.
1663 * Return FAIL if the number of arguments is wrong.
1664 */
1665 int
1666generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1667{
1668 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001669 int regular_args = ufunc->uf_args.ga_len;
1670 int argcount = pushed_argcount;
1671
1672 RETURN_OK_IF_SKIP(cctx);
1673 if (argcount > regular_args && !has_varargs(ufunc))
1674 {
1675 semsg(_(e_too_many_arguments_for_function_str),
1676 printable_func_name(ufunc));
1677 return FAIL;
1678 }
1679 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1680 {
1681 semsg(_(e_not_enough_arguments_for_function_str),
1682 printable_func_name(ufunc));
1683 return FAIL;
1684 }
1685
1686 if (ufunc->uf_def_status != UF_NOT_COMPILED
1687 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1688 {
1689 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001690 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001691
1692 for (i = 0; i < argcount; ++i)
1693 {
1694 type_T *expected;
1695 type_T *actual;
1696
Bram Moolenaar078a4612022-01-04 15:17:03 +00001697 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001698 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001699 && i >= regular_args - ufunc->uf_def_args.ga_len)
1700 {
1701 // assume v:none used for default argument value
1702 continue;
1703 }
1704 if (i < regular_args)
1705 {
1706 if (ufunc->uf_arg_types == NULL)
1707 continue;
1708 expected = ufunc->uf_arg_types[i];
1709 }
1710 else if (ufunc->uf_va_type == NULL
1711 || ufunc->uf_va_type == &t_list_any)
1712 // possibly a lambda or "...: any"
1713 expected = &t_any;
1714 else
1715 expected = ufunc->uf_va_type->tt_member;
1716 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1717 TRUE, FALSE) == FAIL)
1718 {
1719 arg_type_mismatch(expected, actual, i + 1);
1720 return FAIL;
1721 }
1722 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001723 compile_type = get_compile_type(ufunc);
1724 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001725 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001726 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001727 return FAIL;
1728 }
1729 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1730 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001731 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001732 ufunc->uf_name);
1733 return FAIL;
1734 }
1735
1736 if ((isn = generate_instr(cctx,
1737 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1738 : ISN_UCALL)) == NULL)
1739 return FAIL;
1740 if (isn->isn_type == ISN_DCALL)
1741 {
1742 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1743 isn->isn_arg.dfunc.cdf_argcount = argcount;
1744 }
1745 else
1746 {
1747 // A user function may be deleted and redefined later, can't use the
1748 // ufunc pointer, need to look it up again at runtime.
1749 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1750 isn->isn_arg.ufunc.cuf_argcount = argcount;
1751 }
1752
Bram Moolenaar078a4612022-01-04 15:17:03 +00001753 // drop the argument types
1754 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001755
Bram Moolenaar078a4612022-01-04 15:17:03 +00001756 // add return type
1757 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001758}
1759
1760/*
1761 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1762 */
1763 int
1764generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1765{
1766 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001767
1768 RETURN_OK_IF_SKIP(cctx);
1769 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1770 return FAIL;
1771 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1772 isn->isn_arg.ufunc.cuf_argcount = argcount;
1773
Bram Moolenaar078a4612022-01-04 15:17:03 +00001774 // drop the argument types
1775 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001776
Bram Moolenaar078a4612022-01-04 15:17:03 +00001777 // add return value
1778 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001779}
1780
1781/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001782 * Check the arguments of function "type" against the types on the stack.
1783 * Returns OK or FAIL;
1784 */
1785 int
1786check_func_args_from_type(
1787 cctx_T *cctx,
1788 type_T *type,
1789 int argcount,
1790 int at_top,
1791 char_u *name)
1792{
1793 if (type->tt_argcount != -1)
1794 {
1795 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1796
1797 if (argcount < type->tt_min_argcount - varargs)
1798 {
1799 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1800 return FAIL;
1801 }
1802 if (!varargs && argcount > type->tt_argcount)
1803 {
1804 emsg_funcname(e_too_many_arguments_for_function_str, name);
1805 return FAIL;
1806 }
1807 if (type->tt_args != NULL)
1808 {
1809 int i;
1810
1811 for (i = 0; i < argcount; ++i)
1812 {
1813 int offset = -argcount + i - (at_top ? 0 : 1);
1814 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1815 type_T *expected;
1816
1817 if (varargs && i >= type->tt_argcount - 1)
1818 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1819 else if (i >= type->tt_min_argcount
1820 && actual->tt_type == VAR_SPECIAL)
1821 expected = &t_any;
1822 else
1823 expected = type->tt_args[i];
1824 if (need_type(actual, expected, offset, i + 1,
1825 cctx, TRUE, FALSE) == FAIL)
1826 {
1827 arg_type_mismatch(expected, actual, i + 1);
1828 return FAIL;
1829 }
1830 }
1831 }
1832 }
1833
1834 return OK;
1835}
1836/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001837 * Generate an ISN_PCALL instruction.
1838 * "type" is the type of the FuncRef.
1839 */
1840 int
1841generate_PCALL(
1842 cctx_T *cctx,
1843 int argcount,
1844 char_u *name,
1845 type_T *type,
1846 int at_top)
1847{
1848 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849 type_T *ret_type;
1850
1851 RETURN_OK_IF_SKIP(cctx);
1852
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001853 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001854 ret_type = &t_any;
1855 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1856 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001857 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1858 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001859
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001860 ret_type = type->tt_member;
1861 if (ret_type == &t_unknown)
1862 // return type not known yet, use a runtime check
1863 ret_type = &t_any;
1864 }
1865 else
1866 {
1867 semsg(_(e_not_callable_type_str), name);
1868 return FAIL;
1869 }
1870
1871 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1872 return FAIL;
1873 isn->isn_arg.pfunc.cpf_top = at_top;
1874 isn->isn_arg.pfunc.cpf_argcount = argcount;
1875
Bram Moolenaar078a4612022-01-04 15:17:03 +00001876 // drop the arguments and the funcref/partial
1877 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001878
Bram Moolenaar078a4612022-01-04 15:17:03 +00001879 // push the return value
1880 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001881
1882 // If partial is above the arguments it must be cleared and replaced with
1883 // the return value.
1884 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1885 return FAIL;
1886
1887 return OK;
1888}
1889
1890/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001891 * Generate an ISN_DEFER instruction.
1892 */
1893 int
1894generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1895{
1896 isn_T *isn;
1897
1898 RETURN_OK_IF_SKIP(cctx);
1899 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1900 return FAIL;
1901 isn->isn_arg.defer.defer_var_idx = var_idx;
1902 isn->isn_arg.defer.defer_argcount = argcount;
1903 return OK;
1904}
1905
1906/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001907 * Generate an ISN_STRINGMEMBER instruction.
1908 */
1909 int
1910generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1911{
1912 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001913 type_T *type;
1914
1915 RETURN_OK_IF_SKIP(cctx);
1916 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1917 return FAIL;
1918 isn->isn_arg.string = vim_strnsave(name, len);
1919
1920 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001921 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001922 if (type->tt_type != VAR_DICT
1923 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001924 {
1925 char *tofree;
1926
1927 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1928 name, type_name(type, &tofree));
1929 vim_free(tofree);
1930 return FAIL;
1931 }
1932 // change dict type to dict member type
1933 if (type->tt_type == VAR_DICT)
1934 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001935 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00001936 ? &t_any : type->tt_member;
1937 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938 }
1939
1940 return OK;
1941}
1942
1943/*
1944 * Generate an ISN_ECHO instruction.
1945 */
1946 int
1947generate_ECHO(cctx_T *cctx, int with_white, int count)
1948{
1949 isn_T *isn;
1950
1951 RETURN_OK_IF_SKIP(cctx);
1952 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1953 return FAIL;
1954 isn->isn_arg.echo.echo_with_white = with_white;
1955 isn->isn_arg.echo.echo_count = count;
1956
1957 return OK;
1958}
1959
1960/*
1961 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1962 */
1963 int
1964generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1965{
1966 isn_T *isn;
1967
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01001968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001969 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1970 return FAIL;
1971 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001972 return OK;
1973}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001974
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001975/*
1976 * Generate an ISN_ECHOWINDOW instruction
1977 */
1978 int
1979generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
1980{
1981 isn_T *isn;
1982
1983 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
1984 return FAIL;
1985 isn->isn_arg.echowin.ewin_count = count;
1986 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001987 return OK;
1988}
1989
1990/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001991 * Generate an ISN_SOURCE instruction.
1992 */
1993 int
1994generate_SOURCE(cctx_T *cctx, int sid)
1995{
1996 isn_T *isn;
1997
1998 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1999 return FAIL;
2000 isn->isn_arg.number = sid;
2001
2002 return OK;
2003}
2004
2005/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002006 * Generate an ISN_PUT instruction.
2007 */
2008 int
2009generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2010{
2011 isn_T *isn;
2012
2013 RETURN_OK_IF_SKIP(cctx);
2014 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2015 return FAIL;
2016 isn->isn_arg.put.put_regname = regname;
2017 isn->isn_arg.put.put_lnum = lnum;
2018 return OK;
2019}
2020
2021/*
2022 * Generate an EXEC instruction that takes a string argument.
2023 * A copy is made of "line".
2024 */
2025 int
2026generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2027{
2028 isn_T *isn;
2029
2030 RETURN_OK_IF_SKIP(cctx);
2031 if ((isn = generate_instr(cctx, isntype)) == NULL)
2032 return FAIL;
2033 isn->isn_arg.string = vim_strsave(line);
2034 return OK;
2035}
2036
2037/*
2038 * Generate an EXEC instruction that takes a string argument.
2039 * "str" must be allocated, it is consumed.
2040 */
2041 int
2042generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2043{
2044 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002045 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002046
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002047 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002048 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002049 if ((isn = generate_instr(cctx, isntype)) == NULL)
2050 ret = FAIL;
2051 else
2052 {
2053 isn->isn_arg.string = str;
2054 return OK;
2055 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002056 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002057 vim_free(str);
2058 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002059}
2060
2061 int
2062generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2063{
2064 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002065
2066 RETURN_OK_IF_SKIP(cctx);
2067 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2068 return FAIL;
2069 isn->isn_arg.string = vim_strsave(line);
2070
Bram Moolenaar078a4612022-01-04 15:17:03 +00002071 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002072}
2073
2074 int
2075generate_EXECCONCAT(cctx_T *cctx, int count)
2076{
2077 isn_T *isn;
2078
2079 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2080 return FAIL;
2081 isn->isn_arg.number = count;
2082 return OK;
2083}
2084
2085/*
2086 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2087 */
2088 int
2089generate_RANGE(cctx_T *cctx, char_u *range)
2090{
2091 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002092
2093 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2094 return FAIL;
2095 isn->isn_arg.string = range;
2096
Bram Moolenaar078a4612022-01-04 15:17:03 +00002097 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002098}
2099
2100 int
2101generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2102{
2103 isn_T *isn;
2104
2105 RETURN_OK_IF_SKIP(cctx);
2106 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2107 return FAIL;
2108 isn->isn_arg.unpack.unp_count = var_count;
2109 isn->isn_arg.unpack.unp_semicolon = semicolon;
2110 return OK;
2111}
2112
2113/*
2114 * Generate an instruction for any command modifiers.
2115 */
2116 int
2117generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2118{
2119 isn_T *isn;
2120
2121 if (has_cmdmod(cmod, FALSE))
2122 {
2123 cctx->ctx_has_cmdmod = TRUE;
2124
2125 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2126 return FAIL;
2127 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2128 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2129 return FAIL;
2130 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2131 // filter program now belongs to the instruction
2132 cmod->cmod_filter_regmatch.regprog = NULL;
2133 }
2134
2135 return OK;
2136}
2137
2138 int
2139generate_undo_cmdmods(cctx_T *cctx)
2140{
2141 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2142 return FAIL;
2143 cctx->ctx_has_cmdmod = FALSE;
2144 return OK;
2145}
2146
2147/*
2148 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002149 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002150 * Return FAIL when out of memory.
2151 */
2152 int
2153generate_store_var(
2154 cctx_T *cctx,
2155 assign_dest_T dest,
2156 int opt_flags,
2157 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002158 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002159 char_u *name,
2160 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002161{
2162 switch (dest)
2163 {
2164 case dest_option:
2165 return generate_STOREOPT(cctx, ISN_STOREOPT,
2166 skip_option_env_lead(name), opt_flags);
2167 case dest_func_option:
2168 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2169 skip_option_env_lead(name), opt_flags);
2170 case dest_global:
2171 // include g: with the name, easier to execute that way
2172 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2173 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2174 case dest_buffer:
2175 // include b: with the name, easier to execute that way
2176 return generate_STORE(cctx, ISN_STOREB, 0, name);
2177 case dest_window:
2178 // include w: with the name, easier to execute that way
2179 return generate_STORE(cctx, ISN_STOREW, 0, name);
2180 case dest_tab:
2181 // include t: with the name, easier to execute that way
2182 return generate_STORE(cctx, ISN_STORET, 0, name);
2183 case dest_env:
2184 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2185 case dest_reg:
2186 return generate_STORE(cctx, ISN_STOREREG,
2187 name[1] == '@' ? '"' : name[1], NULL);
2188 case dest_vimvar:
2189 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2190 case dest_script:
Bram Moolenaard505d172022-12-18 21:42:55 +00002191 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2192 int scriptvar_sid = lhs->lhs_scriptvar_sid;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002193 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002194 {
Bram Moolenaard505d172022-12-18 21:42:55 +00002195 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002196
2197 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01002198 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2199 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2200 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002201 {
2202 // "import autoload './dir/script.vim'" - load script first
2203 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2204 return FAIL;
2205 isn_type = ISN_STOREEXPORT;
2206 }
2207
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002208 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002209 return generate_OLDSCRIPT(cctx, isn_type, name,
2210 scriptvar_sid, type);
2211 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002212 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
2213 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaard505d172022-12-18 21:42:55 +00002214 case dest_class_member:
2215 return generate_CLASSMEMBER(cctx, FALSE,
2216 lhs->lhs_class, lhs->lhs_classmember_idx);
2217
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002218 case dest_local:
2219 case dest_expr:
2220 // cannot happen
2221 break;
2222 }
2223 return FAIL;
2224}
2225
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002226/*
2227 * Return TRUE when inside a "for" or "while" loop.
2228 */
2229 int
2230inside_loop_scope(cctx_T *cctx)
2231{
2232 scope_T *scope = cctx->ctx_scope;
2233
2234 for (;;)
2235 {
2236 if (scope == NULL)
2237 break;
2238 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2239 return TRUE;
2240 scope = scope->se_outer;
2241 }
2242 return FALSE;
2243}
2244
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002245 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002246generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002247{
2248 if (lhs->lhs_dest != dest_local)
2249 return generate_store_var(cctx, lhs->lhs_dest,
2250 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002251 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002252
2253 if (lhs->lhs_lvar != NULL)
2254 {
2255 garray_T *instr = &cctx->ctx_instr;
2256 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2257
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002258 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2259 // ISN_STORENR.
2260 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002261 if (lhs->lhs_lvar->lv_from_outer == 0
2262 && instr->ga_len == instr_count + 1
2263 && isn->isn_type == ISN_PUSHNR)
2264 {
2265 varnumber_T val = isn->isn_arg.number;
2266 garray_T *stack = &cctx->ctx_type_stack;
2267
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002268 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002269 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002270 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002271 --instr->ga_len;
2272 }
2273 else
2274 {
2275 isn->isn_type = ISN_STORENR;
2276 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2277 isn->isn_arg.storenr.stnr_val = val;
2278 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002279 if (stack->ga_len > 0)
2280 --stack->ga_len;
2281 }
2282 else if (lhs->lhs_lvar->lv_from_outer > 0)
2283 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002284 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002285 else
2286 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2287 }
2288 return OK;
2289}
2290
2291#if defined(FEAT_PROFILE) || defined(PROTO)
2292 void
2293may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2294{
2295 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2296 generate_instr(cctx, ISN_PROF_END);
2297}
2298#endif
2299
2300
2301/*
2302 * Delete an instruction, free what it contains.
2303 */
2304 void
2305delete_instr(isn_T *isn)
2306{
2307 switch (isn->isn_type)
2308 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002309 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002310 case ISN_DEF:
2311 case ISN_EXEC:
2312 case ISN_EXECRANGE:
2313 case ISN_EXEC_SPLIT:
2314 case ISN_LEGACY_EVAL:
2315 case ISN_LOADAUTO:
2316 case ISN_LOADB:
2317 case ISN_LOADENV:
2318 case ISN_LOADG:
2319 case ISN_LOADOPT:
2320 case ISN_LOADT:
2321 case ISN_LOADW:
2322 case ISN_LOCKUNLOCK:
2323 case ISN_PUSHEXC:
2324 case ISN_PUSHFUNC:
2325 case ISN_PUSHS:
2326 case ISN_RANGE:
2327 case ISN_STOREAUTO:
2328 case ISN_STOREB:
2329 case ISN_STOREENV:
2330 case ISN_STOREG:
2331 case ISN_STORET:
2332 case ISN_STOREW:
2333 case ISN_STRINGMEMBER:
2334 vim_free(isn->isn_arg.string);
2335 break;
2336
2337 case ISN_SUBSTITUTE:
2338 {
2339 int idx;
2340 isn_T *list = isn->isn_arg.subs.subs_instr;
2341
2342 vim_free(isn->isn_arg.subs.subs_cmd);
2343 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2344 delete_instr(list + idx);
2345 vim_free(list);
2346 }
2347 break;
2348
2349 case ISN_INSTR:
2350 {
2351 int idx;
2352 isn_T *list = isn->isn_arg.instr;
2353
2354 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2355 delete_instr(list + idx);
2356 vim_free(list);
2357 }
2358 break;
2359
2360 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002361 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002362 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002363 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002364 vim_free(isn->isn_arg.loadstore.ls_name);
2365 break;
2366
2367 case ISN_UNLET:
2368 case ISN_UNLETENV:
2369 vim_free(isn->isn_arg.unlet.ul_name);
2370 break;
2371
2372 case ISN_STOREOPT:
2373 case ISN_STOREFUNCOPT:
2374 vim_free(isn->isn_arg.storeopt.so_name);
2375 break;
2376
2377 case ISN_PUSHBLOB: // push blob isn_arg.blob
2378 blob_unref(isn->isn_arg.blob);
2379 break;
2380
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002381 case ISN_UCALL:
2382 vim_free(isn->isn_arg.ufunc.cuf_name);
2383 break;
2384
2385 case ISN_FUNCREF:
2386 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002387 funcref_T *funcref = &isn->isn_arg.funcref;
2388 funcref_extra_T *extra = funcref->fr_extra;
2389
2390 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002391 {
2392 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002393 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002394 ufunc_T *ufunc = dfunc->df_ufunc;
2395
2396 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2397 func_ptr_unref(ufunc);
2398 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002399 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002400 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002401 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002402
2403 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002404 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002405 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002406 vim_free(name);
2407 }
2408 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002409 }
2410 }
2411 break;
2412
2413 case ISN_DCALL:
2414 {
2415 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002416 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002417
2418 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002419 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002420 func_ptr_unref(dfunc->df_ufunc);
2421 }
2422 break;
2423
2424 case ISN_NEWFUNC:
2425 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002426 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002427
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002428 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002429 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002430 ufunc_T *ufunc = find_func_even_dead(
2431 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002432
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002433 if (ufunc != NULL)
2434 {
2435 unlink_def_function(ufunc);
2436 func_ptr_unref(ufunc);
2437 }
2438
2439 vim_free(arg->nfa_lambda);
2440 vim_free(arg->nfa_global);
2441 vim_free(arg);
2442 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002443 }
2444 break;
2445
2446 case ISN_CHECKTYPE:
2447 case ISN_SETTYPE:
2448 free_type(isn->isn_arg.type.ct_type);
2449 break;
2450
2451 case ISN_CMDMOD:
2452 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002453 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002454 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2455 break;
2456
2457 case ISN_LOADSCRIPT:
2458 case ISN_STORESCRIPT:
2459 vim_free(isn->isn_arg.script.scriptref);
2460 break;
2461
Bram Moolenaard505d172022-12-18 21:42:55 +00002462 case ISN_LOAD_CLASSMEMBER:
2463 case ISN_STORE_CLASSMEMBER:
2464 class_unref(isn->isn_arg.classmember.cm_class);
2465 break;
2466
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002468 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002469 break;
2470
2471 case ISN_CEXPR_CORE:
2472 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2473 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2474 break;
2475
2476 case ISN_2BOOL:
2477 case ISN_2STRING:
2478 case ISN_2STRING_ANY:
2479 case ISN_ADDBLOB:
2480 case ISN_ADDLIST:
2481 case ISN_ANYINDEX:
2482 case ISN_ANYSLICE:
2483 case ISN_BCALL:
2484 case ISN_BLOBAPPEND:
2485 case ISN_BLOBINDEX:
2486 case ISN_BLOBSLICE:
2487 case ISN_CATCH:
2488 case ISN_CEXPR_AUCMD:
2489 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002490 case ISN_CLEARDICT:
2491 case ISN_CMDMOD_REV:
2492 case ISN_COMPAREANY:
2493 case ISN_COMPAREBLOB:
2494 case ISN_COMPAREBOOL:
2495 case ISN_COMPAREDICT:
2496 case ISN_COMPAREFLOAT:
2497 case ISN_COMPAREFUNC:
2498 case ISN_COMPARELIST:
2499 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002500 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002501 case ISN_COMPARESPECIAL:
2502 case ISN_COMPARESTRING:
2503 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002504 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002505 case ISN_COND2BOOL:
2506 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002507 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002508 case ISN_DROP:
2509 case ISN_ECHO:
2510 case ISN_ECHOCONSOLE:
2511 case ISN_ECHOERR:
2512 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002513 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002514 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002515 case ISN_ENDTRY:
2516 case ISN_EXECCONCAT:
2517 case ISN_EXECUTE:
2518 case ISN_FINALLY:
2519 case ISN_FINISH:
2520 case ISN_FOR:
2521 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002522 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002523 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002524 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002525 case ISN_JUMP_IF_ARG_SET:
2526 case ISN_LISTAPPEND:
2527 case ISN_LISTINDEX:
2528 case ISN_LISTSLICE:
2529 case ISN_LOAD:
2530 case ISN_LOADBDICT:
2531 case ISN_LOADGDICT:
2532 case ISN_LOADOUTER:
2533 case ISN_LOADREG:
2534 case ISN_LOADTDICT:
2535 case ISN_LOADV:
2536 case ISN_LOADWDICT:
2537 case ISN_LOCKCONST:
2538 case ISN_MEMBER:
2539 case ISN_NEGATENR:
2540 case ISN_NEWDICT:
2541 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002542 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002543 case ISN_OPANY:
2544 case ISN_OPFLOAT:
2545 case ISN_OPNR:
2546 case ISN_PCALL:
2547 case ISN_PCALL_END:
2548 case ISN_PROF_END:
2549 case ISN_PROF_START:
2550 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002551 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002552 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002553 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002554 case ISN_PUSHNR:
2555 case ISN_PUSHSPEC:
2556 case ISN_PUT:
2557 case ISN_REDIREND:
2558 case ISN_REDIRSTART:
2559 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002560 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002561 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002562 case ISN_SHUFFLE:
2563 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002564 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002565 case ISN_STORE:
2566 case ISN_STOREINDEX:
2567 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002568 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002569 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002570 case ISN_STORERANGE:
2571 case ISN_STOREREG:
2572 case ISN_STOREV:
2573 case ISN_STRINDEX:
2574 case ISN_STRSLICE:
2575 case ISN_THROW:
2576 case ISN_TRYCONT:
2577 case ISN_UNLETINDEX:
2578 case ISN_UNLETRANGE:
2579 case ISN_UNPACK:
2580 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002581 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002582 // nothing allocated
2583 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002584 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002585}
2586
2587 void
2588clear_instr_ga(garray_T *gap)
2589{
2590 int idx;
2591
2592 for (idx = 0; idx < gap->ga_len; ++idx)
2593 delete_instr(((isn_T *)gap->ga_data) + idx);
2594 ga_clear(gap);
2595}
2596
2597
2598#endif // defined(FEAT_EVAL)