blob: b4c66e39b033fe68bce3725ace3da12f065b2dd2 [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,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000579 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000580 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100581 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000582 int argidx)
583{
584 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000585
586 RETURN_OK_IF_SKIP(cctx);
587 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
588 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000589 type_T *tt;
590 if (expected->tt_type == VAR_FLOAT && number_ok)
591 {
592 // always allocate, also for static types
593 tt = ALLOC_ONE(type_T);
594 if (tt != NULL)
595 {
596 *tt = *expected;
597 tt->tt_flags |= TTFLAG_NUMBER_OK;
598 }
599 }
600 else
601 tt = alloc_type(expected);
602
603 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000604 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100605 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000606 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
607
608 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000609 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000610
611 return OK;
612}
613
614 int
615generate_SETTYPE(
616 cctx_T *cctx,
617 type_T *expected)
618{
619 isn_T *isn;
620
621 RETURN_OK_IF_SKIP(cctx);
622 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
623 return FAIL;
624 isn->isn_arg.type.ct_type = alloc_type(expected);
625 return OK;
626}
627
628/*
629 * Generate a PUSH instruction for "tv".
630 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000631 */
632 int
633generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
634{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100635 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100637 case VAR_BOOL:
638 generate_PUSHBOOL(cctx, tv->vval.v_number);
639 break;
640 case VAR_SPECIAL:
641 generate_PUSHSPEC(cctx, tv->vval.v_number);
642 break;
643 case VAR_NUMBER:
644 generate_PUSHNR(cctx, tv->vval.v_number);
645 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100646 case VAR_FLOAT:
647 generate_PUSHF(cctx, tv->vval.v_float);
648 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100649 case VAR_BLOB:
650 generate_PUSHBLOB(cctx, tv->vval.v_blob);
651 tv->vval.v_blob = NULL;
652 break;
653 case VAR_LIST:
654 if (tv->vval.v_list != NULL)
655 iemsg("non-empty list constant not supported");
656 generate_NEWLIST(cctx, 0, TRUE);
657 break;
658 case VAR_DICT:
659 if (tv->vval.v_dict != NULL)
660 iemsg("non-empty dict constant not supported");
661 generate_NEWDICT(cctx, 0, TRUE);
662 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000663#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100664 case VAR_JOB:
665 if (tv->vval.v_job != NULL)
666 iemsg("non-null job constant not supported");
667 generate_PUSHJOB(cctx);
668 break;
669 case VAR_CHANNEL:
670 if (tv->vval.v_channel != NULL)
671 iemsg("non-null channel constant not supported");
672 generate_PUSHCHANNEL(cctx);
673 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000674#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100675 case VAR_FUNC:
676 if (tv->vval.v_string != NULL)
677 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100678 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100679 break;
680 case VAR_PARTIAL:
681 if (tv->vval.v_partial != NULL)
682 iemsg("non-null partial constant not supported");
683 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
684 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000685 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100686 break;
687 case VAR_STRING:
688 generate_PUSHS(cctx, &tv->vval.v_string);
689 tv->vval.v_string = NULL;
690 break;
691 default:
692 siemsg("constant type %d not supported", tv->v_type);
693 clear_tv(tv);
694 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000695 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100696 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000697 return OK;
698}
699
700/*
701 * Generate an ISN_PUSHNR instruction.
702 */
703 int
704generate_PUSHNR(cctx_T *cctx, varnumber_T number)
705{
706 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000707
708 RETURN_OK_IF_SKIP(cctx);
709 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
710 return FAIL;
711 isn->isn_arg.number = number;
712
713 if (number == 0 || number == 1)
714 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000715 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000716 return OK;
717}
718
719/*
720 * Generate an ISN_PUSHBOOL instruction.
721 */
722 int
723generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
724{
725 isn_T *isn;
726
727 RETURN_OK_IF_SKIP(cctx);
728 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
729 return FAIL;
730 isn->isn_arg.number = number;
731
732 return OK;
733}
734
735/*
736 * Generate an ISN_PUSHSPEC instruction.
737 */
738 int
739generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
740{
741 isn_T *isn;
742
743 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000744 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
745 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746 return FAIL;
747 isn->isn_arg.number = number;
748
749 return OK;
750}
751
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752/*
753 * Generate an ISN_PUSHF instruction.
754 */
755 int
756generate_PUSHF(cctx_T *cctx, float_T fnumber)
757{
758 isn_T *isn;
759
760 RETURN_OK_IF_SKIP(cctx);
761 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
762 return FAIL;
763 isn->isn_arg.fnumber = fnumber;
764
765 return OK;
766}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000767
768/*
769 * Generate an ISN_PUSHS instruction.
770 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100771 * Note that if "str" is used in the instruction OK is returned and "*str" is
772 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000773 */
774 int
775generate_PUSHS(cctx_T *cctx, char_u **str)
776{
777 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100778 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000779
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100780 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000781 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100782 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
783 ret = FAIL;
784 else
785 {
786 isn->isn_arg.string = str == NULL ? NULL : *str;
787 return OK;
788 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100790 if (str != NULL)
791 VIM_CLEAR(*str);
792 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793}
794
795/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000796 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797 */
798 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000799generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000800{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000801 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100802#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100803 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000804 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000805 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100806#else
807 emsg(_(e_channel_job_feature_not_available));
808 return FAIL;
809#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000810}
811
812/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000813 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000814 */
815 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000816generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000818 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100819#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100820 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000822 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100823#else
824 emsg(_(e_channel_job_feature_not_available));
825 return FAIL;
826#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827}
828
829/*
830 * Generate an ISN_PUSHBLOB instruction.
831 * Consumes "blob".
832 */
833 int
834generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
835{
836 isn_T *isn;
837
838 RETURN_OK_IF_SKIP(cctx);
839 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
840 return FAIL;
841 isn->isn_arg.blob = blob;
842
843 return OK;
844}
845
846/*
847 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100848 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
849 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000850 */
851 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100852generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853{
854 isn_T *isn;
855 char_u *funcname;
856
857 RETURN_OK_IF_SKIP(cctx);
858 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
859 return FAIL;
860 if (name == NULL)
861 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100862 else if (!may_prefix
863 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000864 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000865 funcname = vim_strsave(name);
866 else
867 {
868 funcname = alloc(STRLEN(name) + 3);
869 if (funcname != NULL)
870 {
871 STRCPY(funcname, "g:");
872 STRCPY(funcname + 2, name);
873 }
874 }
875
876 isn->isn_arg.string = funcname;
877 return OK;
878}
879
880/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000881 * Generate an ISN_AUTOLOAD instruction.
882 */
883 int
884generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
885{
886 isn_T *isn;
887
888 RETURN_OK_IF_SKIP(cctx);
889 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
890 return FAIL;
891 isn->isn_arg.string = vim_strsave(name);
892 if (isn->isn_arg.string == NULL)
893 return FAIL;
894 return OK;
895}
896
897/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000898 * Generate an ISN_GETITEM instruction with "index".
899 * "with_op" is TRUE for "+=" and other operators, the stack has the current
900 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100901 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000902 */
903 int
904generate_GETITEM(cctx_T *cctx, int index, int with_op)
905{
906 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000907 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000908 type_T *item_type = &t_any;
909
910 RETURN_OK_IF_SKIP(cctx);
911
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000912 item_type = type->tt_member;
913 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
914 return FAIL;
915 isn->isn_arg.getitem.gi_index = index;
916 isn->isn_arg.getitem.gi_with_op = with_op;
917
918 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000919 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920}
921
922/*
923 * Generate an ISN_SLICE instruction with "count".
924 */
925 int
926generate_SLICE(cctx_T *cctx, int count)
927{
928 isn_T *isn;
929
930 RETURN_OK_IF_SKIP(cctx);
931 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
932 return FAIL;
933 isn->isn_arg.number = count;
934 return OK;
935}
936
937/*
938 * Generate an ISN_CHECKLEN instruction with "min_len".
939 */
940 int
941generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
942{
943 isn_T *isn;
944
945 RETURN_OK_IF_SKIP(cctx);
946
947 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
948 return FAIL;
949 isn->isn_arg.checklen.cl_min_len = min_len;
950 isn->isn_arg.checklen.cl_more_OK = more_OK;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_STORE instruction.
957 */
958 int
959generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
960{
961 isn_T *isn;
962
963 RETURN_OK_IF_SKIP(cctx);
964 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
965 return FAIL;
966 if (name != NULL)
967 isn->isn_arg.string = vim_strsave(name);
968 else
969 isn->isn_arg.number = idx;
970
971 return OK;
972}
973
974/*
Bram Moolenaard505d172022-12-18 21:42:55 +0000975 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
976 * ("load" == FALSE) instruction.
977 */
978 int
979generate_CLASSMEMBER(
980 cctx_T *cctx,
981 int load,
982 class_T *cl,
983 int idx)
984{
985 isn_T *isn;
986
987 RETURN_OK_IF_SKIP(cctx);
988 if (load)
989 {
990 ocmember_T *m = &cl->class_class_members[idx];
991 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
992 }
993 else
994 {
995 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
996 }
997 if (isn == NULL)
998 return FAIL;
999 isn->isn_arg.classmember.cm_class = cl;
1000 ++cl->class_refcount;
1001 isn->isn_arg.classmember.cm_idx = idx;
1002
1003 return OK;
1004}
1005
1006/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007 * Generate an ISN_STOREOUTER instruction.
1008 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001009 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001010generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001011{
1012 isn_T *isn;
1013
1014 RETURN_OK_IF_SKIP(cctx);
1015 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1016 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001017 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1018 {
1019 // Store a variable defined in a loop. A copy will be made at the end
1020 // of the loop. TODO: how about deeper nesting?
1021 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1022 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1023 }
1024 else
1025 {
1026 isn->isn_arg.outer.outer_idx = idx;
1027 isn->isn_arg.outer.outer_depth = level;
1028 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001029
1030 return OK;
1031}
1032
1033/*
1034 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1035 */
1036 int
1037generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1038{
1039 isn_T *isn;
1040
1041 RETURN_OK_IF_SKIP(cctx);
1042 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1043 return FAIL;
1044 isn->isn_arg.storenr.stnr_idx = idx;
1045 isn->isn_arg.storenr.stnr_val = value;
1046
1047 return OK;
1048}
1049
1050/*
1051 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1052 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001053 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001054generate_STOREOPT(
1055 cctx_T *cctx,
1056 isntype_T isn_type,
1057 char_u *name,
1058 int opt_flags)
1059{
1060 isn_T *isn;
1061
1062 RETURN_OK_IF_SKIP(cctx);
1063 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1064 return FAIL;
1065 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1066 isn->isn_arg.storeopt.so_flags = opt_flags;
1067
1068 return OK;
1069}
1070
1071/*
1072 * Generate an ISN_LOAD or similar instruction.
1073 */
1074 int
1075generate_LOAD(
1076 cctx_T *cctx,
1077 isntype_T isn_type,
1078 int idx,
1079 char_u *name,
1080 type_T *type)
1081{
1082 isn_T *isn;
1083
1084 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001085 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001086 return FAIL;
1087 if (name != NULL)
1088 isn->isn_arg.string = vim_strsave(name);
1089 else
1090 isn->isn_arg.number = idx;
1091
1092 return OK;
1093}
1094
1095/*
1096 * Generate an ISN_LOADOUTER instruction
1097 */
1098 int
1099generate_LOADOUTER(
1100 cctx_T *cctx,
1101 int idx,
1102 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001103 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001104 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001105 type_T *type)
1106{
1107 isn_T *isn;
1108
1109 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001110 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001111 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001112 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1113 {
1114 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001115 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001116 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001117 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001118 }
1119 else
1120 {
1121 isn->isn_arg.outer.outer_idx = idx;
1122 isn->isn_arg.outer.outer_depth = nesting;
1123 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001124
1125 return OK;
1126}
1127
1128/*
1129 * Generate an ISN_LOADV instruction for v:var.
1130 */
1131 int
1132generate_LOADV(
1133 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001134 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001135{
1136 int di_flags;
1137 int vidx = find_vim_var(name, &di_flags);
1138 type_T *type;
1139
1140 RETURN_OK_IF_SKIP(cctx);
1141 if (vidx < 0)
1142 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001143 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144 return FAIL;
1145 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001146 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001147 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1148}
1149
1150/*
1151 * Generate an ISN_UNLET instruction.
1152 */
1153 int
1154generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1155{
1156 isn_T *isn;
1157
1158 RETURN_OK_IF_SKIP(cctx);
1159 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1160 return FAIL;
1161 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1162 isn->isn_arg.unlet.ul_forceit = forceit;
1163
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_LOCKCONST instruction.
1169 */
1170 int
1171generate_LOCKCONST(cctx_T *cctx)
1172{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001173 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001174 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001175 return FAIL;
1176 return OK;
1177}
1178
1179/*
1180 * Generate an ISN_LOADS instruction.
1181 */
1182 int
1183generate_OLDSCRIPT(
1184 cctx_T *cctx,
1185 isntype_T isn_type,
1186 char_u *name,
1187 int sid,
1188 type_T *type)
1189{
1190 isn_T *isn;
1191
1192 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001193 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001194 isn = generate_instr_type(cctx, isn_type, type);
1195 else
1196 isn = generate_instr_drop(cctx, isn_type, 1);
1197 if (isn == NULL)
1198 return FAIL;
1199 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1200 isn->isn_arg.loadstore.ls_sid = sid;
1201
1202 return OK;
1203}
1204
1205/*
1206 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1207 */
1208 int
1209generate_VIM9SCRIPT(
1210 cctx_T *cctx,
1211 isntype_T isn_type,
1212 int sid,
1213 int idx,
1214 type_T *type)
1215{
1216 isn_T *isn;
1217 scriptref_T *sref;
1218 scriptitem_T *si = SCRIPT_ITEM(sid);
1219
1220 RETURN_OK_IF_SKIP(cctx);
1221 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001222 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001223 else
1224 isn = generate_instr_drop(cctx, isn_type, 1);
1225 if (isn == NULL)
1226 return FAIL;
1227
1228 // This requires three arguments, which doesn't fit in an instruction, thus
1229 // we need to allocate a struct for this.
1230 sref = ALLOC_ONE(scriptref_T);
1231 if (sref == NULL)
1232 return FAIL;
1233 isn->isn_arg.script.scriptref = sref;
1234 sref->sref_sid = sid;
1235 sref->sref_idx = idx;
1236 sref->sref_seq = si->sn_script_seq;
1237 sref->sref_type = type;
1238 return OK;
1239}
1240
1241/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001242 * Generate an ISN_NEWLIST instruction for "count" items.
1243 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001244 */
1245 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001246generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001247{
1248 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001249 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001250 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001251 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001252
1253 RETURN_OK_IF_SKIP(cctx);
1254 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1255 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001256 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001257
Bram Moolenaar078a4612022-01-04 15:17:03 +00001258 // Get the member type and the declared member type from all the items on
1259 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001260 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001261 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001262 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001263
1264 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001265 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001266
1267 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001268 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001269}
1270
1271/*
1272 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001273 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001274 */
1275 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001276generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001277{
1278 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001279 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001280 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001281 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282
1283 RETURN_OK_IF_SKIP(cctx);
1284 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1285 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001286 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001287
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001288 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001289 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001290 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001291
1292 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001293 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294
1295 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001296 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001297}
1298
1299/*
1300 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001301 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001302 * If variables were declared inside a loop "loop_var_idx" is the index of the
1303 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304 */
1305 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001306generate_FUNCREF(
1307 cctx_T *cctx,
1308 ufunc_T *ufunc,
1309 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001310{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001311 isn_T *isn;
1312 type_T *type;
1313 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001314 loopvarinfo_T loopinfo;
1315 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001316
1317 RETURN_OK_IF_SKIP(cctx);
1318 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1319 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001320 if (isnp != NULL)
1321 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001322
Bram Moolenaarcc341812022-09-19 15:54:34 +01001323 has_vars = get_loop_var_info(cctx, &loopinfo);
1324 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001325 {
1326 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1327 if (extra == NULL)
1328 return FAIL;
1329 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001330 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001331 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001333 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001334 else
1335 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001336
1337 // Reserve an extra variable to keep track of the number of closures
1338 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001339 cctx->ctx_has_closure = 1;
1340
1341 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001342 // the nested context, including this one. But not a function defined at
1343 // the script level.
1344 if ((ufunc->uf_flags & FC_CLOSURE)
1345 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001346 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1347
Bram Moolenaar078a4612022-01-04 15:17:03 +00001348 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1349 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001350}
1351
1352/*
1353 * Generate an ISN_NEWFUNC instruction.
1354 * "lambda_name" and "func_name" must be in allocated memory and will be
1355 * consumed.
1356 */
1357 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001358generate_NEWFUNC(
1359 cctx_T *cctx,
1360 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001361 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001362{
1363 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001364 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001365
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001366 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001367 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001368 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1369 ret = FAIL;
1370 else
1371 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001372 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1373
1374 if (arg == NULL)
1375 ret = FAIL;
1376 else
1377 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001378 // Reserve an extra variable to keep track of the number of
1379 // closures created.
1380 cctx->ctx_has_closure = 1;
1381
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001382 isn->isn_arg.newfunc.nf_arg = arg;
1383 arg->nfa_lambda = lambda_name;
1384 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001385 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001386 return OK;
1387 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001388 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001389 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001390 vim_free(lambda_name);
1391 vim_free(func_name);
1392 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001393}
1394
1395/*
1396 * Generate an ISN_DEF instruction: list functions
1397 */
1398 int
1399generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1400{
1401 isn_T *isn;
1402
1403 RETURN_OK_IF_SKIP(cctx);
1404 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1405 return FAIL;
1406 if (len > 0)
1407 {
1408 isn->isn_arg.string = vim_strnsave(name, len);
1409 if (isn->isn_arg.string == NULL)
1410 return FAIL;
1411 }
1412 return OK;
1413}
1414
1415/*
1416 * Generate an ISN_JUMP instruction.
1417 */
1418 int
1419generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1420{
1421 isn_T *isn;
1422 garray_T *stack = &cctx->ctx_type_stack;
1423
1424 RETURN_OK_IF_SKIP(cctx);
1425 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1426 return FAIL;
1427 isn->isn_arg.jump.jump_when = when;
1428 isn->isn_arg.jump.jump_where = where;
1429
1430 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1431 --stack->ga_len;
1432
1433 return OK;
1434}
1435
1436/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001437 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1438 */
1439 int
1440generate_WHILE(cctx_T *cctx, int funcref_idx)
1441{
1442 isn_T *isn;
1443 garray_T *stack = &cctx->ctx_type_stack;
1444
1445 RETURN_OK_IF_SKIP(cctx);
1446 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1447 return FAIL;
1448 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1449 isn->isn_arg.whileloop.while_end = 0; // filled in later
1450
1451 if (stack->ga_len > 0)
1452 --stack->ga_len;
1453
1454 return OK;
1455}
1456
1457/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001458 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001459 */
1460 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001461generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001462{
1463 isn_T *isn;
1464
1465 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001466 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001467 return FAIL;
1468 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1469 // jump_where is set later
1470 return OK;
1471}
1472
1473 int
1474generate_FOR(cctx_T *cctx, int loop_idx)
1475{
1476 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001477
1478 RETURN_OK_IF_SKIP(cctx);
1479 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1480 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001481 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001482
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001484 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001485}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001486
1487 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001488generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001489{
1490 isn_T *isn;
1491
1492 RETURN_OK_IF_SKIP(cctx);
1493 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1494 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001495 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1496 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1497 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001498 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001499 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001500 return OK;
1501}
1502
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001503/*
1504 * Generate an ISN_TRYCONT instruction.
1505 */
1506 int
1507generate_TRYCONT(cctx_T *cctx, int levels, int where)
1508{
1509 isn_T *isn;
1510
1511 RETURN_OK_IF_SKIP(cctx);
1512 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1513 return FAIL;
1514 isn->isn_arg.trycont.tct_levels = levels;
1515 isn->isn_arg.trycont.tct_where = where;
1516
1517 return OK;
1518}
1519
Bram Moolenaar16900322022-09-08 19:51:45 +01001520/*
1521 * Check "argount" arguments and their types on the type stack.
1522 * Give an error and return FAIL if something is wrong.
1523 * When "method_call" is NULL no code is generated.
1524 */
1525 int
1526check_internal_func_args(
1527 cctx_T *cctx,
1528 int func_idx,
1529 int argcount,
1530 int method_call,
1531 type2_T **argtypes,
1532 type2_T *shuffled_argtypes)
1533{
1534 garray_T *stack = &cctx->ctx_type_stack;
1535 int argoff = check_internal_func(func_idx, argcount);
1536
1537 if (argoff < 0)
1538 return FAIL;
1539
1540 if (method_call && argoff > 1)
1541 {
1542 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1543
1544 if (isn == NULL)
1545 return FAIL;
1546 isn->isn_arg.shuffle.shfl_item = argcount;
1547 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1548 }
1549
1550 if (argcount > 0)
1551 {
1552 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1553
1554 // Check the types of the arguments.
1555 if (method_call && argoff > 1)
1556 {
1557 int i;
1558
1559 for (i = 0; i < argcount; ++i)
1560 shuffled_argtypes[i] = (i < argoff - 1)
1561 ? typep[i + 1]
1562 : (i == argoff - 1) ? typep[0] : typep[i];
1563 *argtypes = shuffled_argtypes;
1564 }
1565 else
1566 {
1567 int i;
1568
1569 for (i = 0; i < argcount; ++i)
1570 shuffled_argtypes[i] = typep[i];
1571 *argtypes = shuffled_argtypes;
1572 }
1573 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1574 cctx) == FAIL)
1575 return FAIL;
1576 }
1577 return OK;
1578}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001579
1580/*
1581 * Generate an ISN_BCALL instruction.
1582 * "method_call" is TRUE for "value->method()"
1583 * Return FAIL if the number of arguments is wrong.
1584 */
1585 int
1586generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1587{
1588 isn_T *isn;
1589 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001590 type2_T *argtypes = NULL;
1591 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1592 type2_T *maptype = NULL;
1593 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001594 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001595
1596 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001597
1598 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1599 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001600 return FAIL;
1601
Bram Moolenaar16900322022-09-08 19:51:45 +01001602 if (internal_func_is_map(func_idx))
1603 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001604
1605 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1606 return FAIL;
1607 isn->isn_arg.bfunc.cbf_idx = func_idx;
1608 isn->isn_arg.bfunc.cbf_argcount = argcount;
1609
1610 // Drop the argument types and push the return type.
1611 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001612 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1613 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001614 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001615
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001616 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1617 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001618 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001619 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001620
1621 return OK;
1622}
1623
1624/*
1625 * Generate an ISN_LISTAPPEND instruction. Works like add().
1626 * Argument count is already checked.
1627 */
1628 int
1629generate_LISTAPPEND(cctx_T *cctx)
1630{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001631 type_T *list_type;
1632 type_T *item_type;
1633 type_T *expected;
1634
1635 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001636 // For checking the item type we use the declared type of the list and the
1637 // current type of the added item, adding a string to [1, 2] is OK.
1638 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001639 if (arg_type_modifiable(list_type, 1) == FAIL)
1640 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001641 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001642 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001643 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001644 return FAIL;
1645
1646 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1647 return FAIL;
1648
Bram Moolenaar078a4612022-01-04 15:17:03 +00001649 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001650 return OK;
1651}
1652
1653/*
1654 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1655 * Argument count is already checked.
1656 */
1657 int
1658generate_BLOBAPPEND(cctx_T *cctx)
1659{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001660 type_T *item_type;
1661
Bram Moolenaarfa103972022-09-29 19:14:42 +01001662 // Caller already checked that blob_type is a blob, check it is modifiable.
1663 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1664 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001665 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001666 if (need_type(item_type, &t_number, FALSE,
1667 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001668 return FAIL;
1669
1670 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1671 return FAIL;
1672
Bram Moolenaar078a4612022-01-04 15:17:03 +00001673 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001674 return OK;
1675}
1676
1677/*
1678 * Generate an ISN_DCALL or ISN_UCALL instruction.
1679 * Return FAIL if the number of arguments is wrong.
1680 */
1681 int
1682generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1683{
1684 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001685 int regular_args = ufunc->uf_args.ga_len;
1686 int argcount = pushed_argcount;
1687
1688 RETURN_OK_IF_SKIP(cctx);
1689 if (argcount > regular_args && !has_varargs(ufunc))
1690 {
1691 semsg(_(e_too_many_arguments_for_function_str),
1692 printable_func_name(ufunc));
1693 return FAIL;
1694 }
1695 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1696 {
1697 semsg(_(e_not_enough_arguments_for_function_str),
1698 printable_func_name(ufunc));
1699 return FAIL;
1700 }
1701
1702 if (ufunc->uf_def_status != UF_NOT_COMPILED
1703 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1704 {
1705 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001706 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001707
1708 for (i = 0; i < argcount; ++i)
1709 {
1710 type_T *expected;
1711 type_T *actual;
1712
Bram Moolenaar078a4612022-01-04 15:17:03 +00001713 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001714 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001715 && i >= regular_args - ufunc->uf_def_args.ga_len)
1716 {
1717 // assume v:none used for default argument value
1718 continue;
1719 }
1720 if (i < regular_args)
1721 {
1722 if (ufunc->uf_arg_types == NULL)
1723 continue;
1724 expected = ufunc->uf_arg_types[i];
1725 }
1726 else if (ufunc->uf_va_type == NULL
1727 || ufunc->uf_va_type == &t_list_any)
1728 // possibly a lambda or "...: any"
1729 expected = &t_any;
1730 else
1731 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001732 if (need_type(actual, expected, FALSE,
1733 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001734 {
1735 arg_type_mismatch(expected, actual, i + 1);
1736 return FAIL;
1737 }
1738 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001739 compile_type = get_compile_type(ufunc);
1740 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001741 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001742 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001743 return FAIL;
1744 }
1745 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1746 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001747 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001748 ufunc->uf_name);
1749 return FAIL;
1750 }
1751
1752 if ((isn = generate_instr(cctx,
1753 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1754 : ISN_UCALL)) == NULL)
1755 return FAIL;
1756 if (isn->isn_type == ISN_DCALL)
1757 {
1758 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1759 isn->isn_arg.dfunc.cdf_argcount = argcount;
1760 }
1761 else
1762 {
1763 // A user function may be deleted and redefined later, can't use the
1764 // ufunc pointer, need to look it up again at runtime.
1765 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1766 isn->isn_arg.ufunc.cuf_argcount = argcount;
1767 }
1768
Bram Moolenaar078a4612022-01-04 15:17:03 +00001769 // drop the argument types
1770 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001771
Bram Moolenaar078a4612022-01-04 15:17:03 +00001772 // add return type
1773 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001774}
1775
1776/*
1777 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1778 */
1779 int
1780generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1781{
1782 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001783
1784 RETURN_OK_IF_SKIP(cctx);
1785 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1786 return FAIL;
1787 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1788 isn->isn_arg.ufunc.cuf_argcount = argcount;
1789
Bram Moolenaar078a4612022-01-04 15:17:03 +00001790 // drop the argument types
1791 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001792
Bram Moolenaar078a4612022-01-04 15:17:03 +00001793 // add return value
1794 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001795}
1796
1797/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001798 * Check the arguments of function "type" against the types on the stack.
1799 * Returns OK or FAIL;
1800 */
1801 int
1802check_func_args_from_type(
1803 cctx_T *cctx,
1804 type_T *type,
1805 int argcount,
1806 int at_top,
1807 char_u *name)
1808{
1809 if (type->tt_argcount != -1)
1810 {
1811 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1812
1813 if (argcount < type->tt_min_argcount - varargs)
1814 {
1815 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1816 return FAIL;
1817 }
1818 if (!varargs && argcount > type->tt_argcount)
1819 {
1820 emsg_funcname(e_too_many_arguments_for_function_str, name);
1821 return FAIL;
1822 }
1823 if (type->tt_args != NULL)
1824 {
1825 int i;
1826
1827 for (i = 0; i < argcount; ++i)
1828 {
1829 int offset = -argcount + i - (at_top ? 0 : 1);
1830 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1831 type_T *expected;
1832
1833 if (varargs && i >= type->tt_argcount - 1)
1834 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1835 else if (i >= type->tt_min_argcount
1836 && actual->tt_type == VAR_SPECIAL)
1837 expected = &t_any;
1838 else
1839 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001840 if (need_type(actual, expected, FALSE,
1841 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001842 {
1843 arg_type_mismatch(expected, actual, i + 1);
1844 return FAIL;
1845 }
1846 }
1847 }
1848 }
1849
1850 return OK;
1851}
1852/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001853 * Generate an ISN_PCALL instruction.
1854 * "type" is the type of the FuncRef.
1855 */
1856 int
1857generate_PCALL(
1858 cctx_T *cctx,
1859 int argcount,
1860 char_u *name,
1861 type_T *type,
1862 int at_top)
1863{
1864 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001865 type_T *ret_type;
1866
1867 RETURN_OK_IF_SKIP(cctx);
1868
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001869 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001870 ret_type = &t_any;
1871 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1872 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001873 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1874 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001876 ret_type = type->tt_member;
1877 if (ret_type == &t_unknown)
1878 // return type not known yet, use a runtime check
1879 ret_type = &t_any;
1880 }
1881 else
1882 {
1883 semsg(_(e_not_callable_type_str), name);
1884 return FAIL;
1885 }
1886
1887 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1888 return FAIL;
1889 isn->isn_arg.pfunc.cpf_top = at_top;
1890 isn->isn_arg.pfunc.cpf_argcount = argcount;
1891
Bram Moolenaar078a4612022-01-04 15:17:03 +00001892 // drop the arguments and the funcref/partial
1893 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001894
Bram Moolenaar078a4612022-01-04 15:17:03 +00001895 // push the return value
1896 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001897
1898 // If partial is above the arguments it must be cleared and replaced with
1899 // the return value.
1900 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1901 return FAIL;
1902
1903 return OK;
1904}
1905
1906/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001907 * Generate an ISN_DEFER instruction.
1908 */
1909 int
1910generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1911{
1912 isn_T *isn;
1913
1914 RETURN_OK_IF_SKIP(cctx);
1915 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1916 return FAIL;
1917 isn->isn_arg.defer.defer_var_idx = var_idx;
1918 isn->isn_arg.defer.defer_argcount = argcount;
1919 return OK;
1920}
1921
1922/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923 * Generate an ISN_STRINGMEMBER instruction.
1924 */
1925 int
1926generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1927{
1928 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001929 type_T *type;
1930
1931 RETURN_OK_IF_SKIP(cctx);
1932 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1933 return FAIL;
1934 isn->isn_arg.string = vim_strnsave(name, len);
1935
1936 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001937 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001938 if (type->tt_type != VAR_DICT
1939 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001940 {
1941 char *tofree;
1942
1943 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1944 name, type_name(type, &tofree));
1945 vim_free(tofree);
1946 return FAIL;
1947 }
1948 // change dict type to dict member type
1949 if (type->tt_type == VAR_DICT)
1950 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001951 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00001952 ? &t_any : type->tt_member;
1953 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001954 }
1955
1956 return OK;
1957}
1958
1959/*
1960 * Generate an ISN_ECHO instruction.
1961 */
1962 int
1963generate_ECHO(cctx_T *cctx, int with_white, int count)
1964{
1965 isn_T *isn;
1966
1967 RETURN_OK_IF_SKIP(cctx);
1968 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1969 return FAIL;
1970 isn->isn_arg.echo.echo_with_white = with_white;
1971 isn->isn_arg.echo.echo_count = count;
1972
1973 return OK;
1974}
1975
1976/*
1977 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1978 */
1979 int
1980generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1981{
1982 isn_T *isn;
1983
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01001984 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001985 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1986 return FAIL;
1987 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001988 return OK;
1989}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001990
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001991/*
1992 * Generate an ISN_ECHOWINDOW instruction
1993 */
1994 int
1995generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
1996{
1997 isn_T *isn;
1998
1999 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2000 return FAIL;
2001 isn->isn_arg.echowin.ewin_count = count;
2002 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002003 return OK;
2004}
2005
2006/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002007 * Generate an ISN_SOURCE instruction.
2008 */
2009 int
2010generate_SOURCE(cctx_T *cctx, int sid)
2011{
2012 isn_T *isn;
2013
2014 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2015 return FAIL;
2016 isn->isn_arg.number = sid;
2017
2018 return OK;
2019}
2020
2021/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002022 * Generate an ISN_PUT instruction.
2023 */
2024 int
2025generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2026{
2027 isn_T *isn;
2028
2029 RETURN_OK_IF_SKIP(cctx);
2030 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2031 return FAIL;
2032 isn->isn_arg.put.put_regname = regname;
2033 isn->isn_arg.put.put_lnum = lnum;
2034 return OK;
2035}
2036
2037/*
2038 * Generate an EXEC instruction that takes a string argument.
2039 * A copy is made of "line".
2040 */
2041 int
2042generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2043{
2044 isn_T *isn;
2045
2046 RETURN_OK_IF_SKIP(cctx);
2047 if ((isn = generate_instr(cctx, isntype)) == NULL)
2048 return FAIL;
2049 isn->isn_arg.string = vim_strsave(line);
2050 return OK;
2051}
2052
2053/*
2054 * Generate an EXEC instruction that takes a string argument.
2055 * "str" must be allocated, it is consumed.
2056 */
2057 int
2058generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2059{
2060 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002061 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002062
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002063 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002064 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002065 if ((isn = generate_instr(cctx, isntype)) == NULL)
2066 ret = FAIL;
2067 else
2068 {
2069 isn->isn_arg.string = str;
2070 return OK;
2071 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002072 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002073 vim_free(str);
2074 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002075}
2076
2077 int
2078generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2079{
2080 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002081
2082 RETURN_OK_IF_SKIP(cctx);
2083 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2084 return FAIL;
2085 isn->isn_arg.string = vim_strsave(line);
2086
Bram Moolenaar078a4612022-01-04 15:17:03 +00002087 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002088}
2089
2090 int
2091generate_EXECCONCAT(cctx_T *cctx, int count)
2092{
2093 isn_T *isn;
2094
2095 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2096 return FAIL;
2097 isn->isn_arg.number = count;
2098 return OK;
2099}
2100
2101/*
2102 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2103 */
2104 int
2105generate_RANGE(cctx_T *cctx, char_u *range)
2106{
2107 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002108
2109 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2110 return FAIL;
2111 isn->isn_arg.string = range;
2112
Bram Moolenaar078a4612022-01-04 15:17:03 +00002113 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002114}
2115
2116 int
2117generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2118{
2119 isn_T *isn;
2120
2121 RETURN_OK_IF_SKIP(cctx);
2122 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2123 return FAIL;
2124 isn->isn_arg.unpack.unp_count = var_count;
2125 isn->isn_arg.unpack.unp_semicolon = semicolon;
2126 return OK;
2127}
2128
2129/*
2130 * Generate an instruction for any command modifiers.
2131 */
2132 int
2133generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2134{
2135 isn_T *isn;
2136
2137 if (has_cmdmod(cmod, FALSE))
2138 {
2139 cctx->ctx_has_cmdmod = TRUE;
2140
2141 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2142 return FAIL;
2143 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2144 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2145 return FAIL;
2146 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2147 // filter program now belongs to the instruction
2148 cmod->cmod_filter_regmatch.regprog = NULL;
2149 }
2150
2151 return OK;
2152}
2153
2154 int
2155generate_undo_cmdmods(cctx_T *cctx)
2156{
2157 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2158 return FAIL;
2159 cctx->ctx_has_cmdmod = FALSE;
2160 return OK;
2161}
2162
2163/*
2164 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002165 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002166 * Return FAIL when out of memory.
2167 */
2168 int
2169generate_store_var(
2170 cctx_T *cctx,
2171 assign_dest_T dest,
2172 int opt_flags,
2173 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002174 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002175 char_u *name,
2176 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002177{
2178 switch (dest)
2179 {
2180 case dest_option:
2181 return generate_STOREOPT(cctx, ISN_STOREOPT,
2182 skip_option_env_lead(name), opt_flags);
2183 case dest_func_option:
2184 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2185 skip_option_env_lead(name), opt_flags);
2186 case dest_global:
2187 // include g: with the name, easier to execute that way
2188 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2189 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2190 case dest_buffer:
2191 // include b: with the name, easier to execute that way
2192 return generate_STORE(cctx, ISN_STOREB, 0, name);
2193 case dest_window:
2194 // include w: with the name, easier to execute that way
2195 return generate_STORE(cctx, ISN_STOREW, 0, name);
2196 case dest_tab:
2197 // include t: with the name, easier to execute that way
2198 return generate_STORE(cctx, ISN_STORET, 0, name);
2199 case dest_env:
2200 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2201 case dest_reg:
2202 return generate_STORE(cctx, ISN_STOREREG,
2203 name[1] == '@' ? '"' : name[1], NULL);
2204 case dest_vimvar:
2205 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2206 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002207 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002208 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2209 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2210 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002211 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002212 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002213
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002214 if (SCRIPT_ID_VALID(scriptvar_sid)
2215 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2216 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2217 == NULL)
2218 {
2219 // "import autoload './dir/script.vim'" - load script
2220 // first
2221 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2222 return FAIL;
2223 isn_type = ISN_STOREEXPORT;
2224 }
2225
2226 // "s:" may be included in the name.
2227 return generate_OLDSCRIPT(cctx, isn_type, name,
2228 scriptvar_sid, type);
2229 }
2230 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002231 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002232 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002233 case dest_class_member:
2234 return generate_CLASSMEMBER(cctx, FALSE,
2235 lhs->lhs_class, lhs->lhs_classmember_idx);
2236
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237 case dest_local:
2238 case dest_expr:
2239 // cannot happen
2240 break;
2241 }
2242 return FAIL;
2243}
2244
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002245/*
2246 * Return TRUE when inside a "for" or "while" loop.
2247 */
2248 int
2249inside_loop_scope(cctx_T *cctx)
2250{
2251 scope_T *scope = cctx->ctx_scope;
2252
2253 for (;;)
2254 {
2255 if (scope == NULL)
2256 break;
2257 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2258 return TRUE;
2259 scope = scope->se_outer;
2260 }
2261 return FALSE;
2262}
2263
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002264 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002265generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002266{
2267 if (lhs->lhs_dest != dest_local)
2268 return generate_store_var(cctx, lhs->lhs_dest,
2269 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002270 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002271
2272 if (lhs->lhs_lvar != NULL)
2273 {
2274 garray_T *instr = &cctx->ctx_instr;
2275 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2276
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002277 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2278 // ISN_STORENR.
2279 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002280 if (lhs->lhs_lvar->lv_from_outer == 0
2281 && instr->ga_len == instr_count + 1
2282 && isn->isn_type == ISN_PUSHNR)
2283 {
2284 varnumber_T val = isn->isn_arg.number;
2285 garray_T *stack = &cctx->ctx_type_stack;
2286
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002287 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002288 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002289 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002290 --instr->ga_len;
2291 }
2292 else
2293 {
2294 isn->isn_type = ISN_STORENR;
2295 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2296 isn->isn_arg.storenr.stnr_val = val;
2297 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002298 if (stack->ga_len > 0)
2299 --stack->ga_len;
2300 }
2301 else if (lhs->lhs_lvar->lv_from_outer > 0)
2302 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002303 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304 else
2305 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2306 }
2307 return OK;
2308}
2309
2310#if defined(FEAT_PROFILE) || defined(PROTO)
2311 void
2312may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2313{
2314 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2315 generate_instr(cctx, ISN_PROF_END);
2316}
2317#endif
2318
2319
2320/*
2321 * Delete an instruction, free what it contains.
2322 */
2323 void
2324delete_instr(isn_T *isn)
2325{
2326 switch (isn->isn_type)
2327 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002328 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002329 case ISN_DEF:
2330 case ISN_EXEC:
2331 case ISN_EXECRANGE:
2332 case ISN_EXEC_SPLIT:
2333 case ISN_LEGACY_EVAL:
2334 case ISN_LOADAUTO:
2335 case ISN_LOADB:
2336 case ISN_LOADENV:
2337 case ISN_LOADG:
2338 case ISN_LOADOPT:
2339 case ISN_LOADT:
2340 case ISN_LOADW:
2341 case ISN_LOCKUNLOCK:
2342 case ISN_PUSHEXC:
2343 case ISN_PUSHFUNC:
2344 case ISN_PUSHS:
2345 case ISN_RANGE:
2346 case ISN_STOREAUTO:
2347 case ISN_STOREB:
2348 case ISN_STOREENV:
2349 case ISN_STOREG:
2350 case ISN_STORET:
2351 case ISN_STOREW:
2352 case ISN_STRINGMEMBER:
2353 vim_free(isn->isn_arg.string);
2354 break;
2355
2356 case ISN_SUBSTITUTE:
2357 {
2358 int idx;
2359 isn_T *list = isn->isn_arg.subs.subs_instr;
2360
2361 vim_free(isn->isn_arg.subs.subs_cmd);
2362 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2363 delete_instr(list + idx);
2364 vim_free(list);
2365 }
2366 break;
2367
2368 case ISN_INSTR:
2369 {
2370 int idx;
2371 isn_T *list = isn->isn_arg.instr;
2372
2373 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2374 delete_instr(list + idx);
2375 vim_free(list);
2376 }
2377 break;
2378
2379 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002380 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002381 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002382 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 vim_free(isn->isn_arg.loadstore.ls_name);
2384 break;
2385
2386 case ISN_UNLET:
2387 case ISN_UNLETENV:
2388 vim_free(isn->isn_arg.unlet.ul_name);
2389 break;
2390
2391 case ISN_STOREOPT:
2392 case ISN_STOREFUNCOPT:
2393 vim_free(isn->isn_arg.storeopt.so_name);
2394 break;
2395
2396 case ISN_PUSHBLOB: // push blob isn_arg.blob
2397 blob_unref(isn->isn_arg.blob);
2398 break;
2399
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002400 case ISN_UCALL:
2401 vim_free(isn->isn_arg.ufunc.cuf_name);
2402 break;
2403
2404 case ISN_FUNCREF:
2405 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002406 funcref_T *funcref = &isn->isn_arg.funcref;
2407 funcref_extra_T *extra = funcref->fr_extra;
2408
2409 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002410 {
2411 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002412 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002413 ufunc_T *ufunc = dfunc->df_ufunc;
2414
2415 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2416 func_ptr_unref(ufunc);
2417 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002418 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002419 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002420 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002421
2422 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002423 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002424 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002425 vim_free(name);
2426 }
2427 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002428 }
2429 }
2430 break;
2431
2432 case ISN_DCALL:
2433 {
2434 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002435 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002436
2437 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002438 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002439 func_ptr_unref(dfunc->df_ufunc);
2440 }
2441 break;
2442
2443 case ISN_NEWFUNC:
2444 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002445 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002446
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002447 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002448 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002449 ufunc_T *ufunc = find_func_even_dead(
2450 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002451
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002452 if (ufunc != NULL)
2453 {
2454 unlink_def_function(ufunc);
2455 func_ptr_unref(ufunc);
2456 }
2457
2458 vim_free(arg->nfa_lambda);
2459 vim_free(arg->nfa_global);
2460 vim_free(arg);
2461 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462 }
2463 break;
2464
2465 case ISN_CHECKTYPE:
2466 case ISN_SETTYPE:
2467 free_type(isn->isn_arg.type.ct_type);
2468 break;
2469
2470 case ISN_CMDMOD:
2471 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002472 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002473 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2474 break;
2475
2476 case ISN_LOADSCRIPT:
2477 case ISN_STORESCRIPT:
2478 vim_free(isn->isn_arg.script.scriptref);
2479 break;
2480
Bram Moolenaard505d172022-12-18 21:42:55 +00002481 case ISN_LOAD_CLASSMEMBER:
2482 case ISN_STORE_CLASSMEMBER:
2483 class_unref(isn->isn_arg.classmember.cm_class);
2484 break;
2485
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002486 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002487 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002488 break;
2489
2490 case ISN_CEXPR_CORE:
2491 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2492 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2493 break;
2494
2495 case ISN_2BOOL:
2496 case ISN_2STRING:
2497 case ISN_2STRING_ANY:
2498 case ISN_ADDBLOB:
2499 case ISN_ADDLIST:
2500 case ISN_ANYINDEX:
2501 case ISN_ANYSLICE:
2502 case ISN_BCALL:
2503 case ISN_BLOBAPPEND:
2504 case ISN_BLOBINDEX:
2505 case ISN_BLOBSLICE:
2506 case ISN_CATCH:
2507 case ISN_CEXPR_AUCMD:
2508 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002509 case ISN_CLEARDICT:
2510 case ISN_CMDMOD_REV:
2511 case ISN_COMPAREANY:
2512 case ISN_COMPAREBLOB:
2513 case ISN_COMPAREBOOL:
2514 case ISN_COMPAREDICT:
2515 case ISN_COMPAREFLOAT:
2516 case ISN_COMPAREFUNC:
2517 case ISN_COMPARELIST:
2518 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002519 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002520 case ISN_COMPARESPECIAL:
2521 case ISN_COMPARESTRING:
2522 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002523 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002524 case ISN_COND2BOOL:
2525 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002526 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002527 case ISN_DROP:
2528 case ISN_ECHO:
2529 case ISN_ECHOCONSOLE:
2530 case ISN_ECHOERR:
2531 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002532 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002533 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002534 case ISN_ENDTRY:
2535 case ISN_EXECCONCAT:
2536 case ISN_EXECUTE:
2537 case ISN_FINALLY:
2538 case ISN_FINISH:
2539 case ISN_FOR:
2540 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002541 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002542 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002543 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002544 case ISN_JUMP_IF_ARG_SET:
2545 case ISN_LISTAPPEND:
2546 case ISN_LISTINDEX:
2547 case ISN_LISTSLICE:
2548 case ISN_LOAD:
2549 case ISN_LOADBDICT:
2550 case ISN_LOADGDICT:
2551 case ISN_LOADOUTER:
2552 case ISN_LOADREG:
2553 case ISN_LOADTDICT:
2554 case ISN_LOADV:
2555 case ISN_LOADWDICT:
2556 case ISN_LOCKCONST:
2557 case ISN_MEMBER:
2558 case ISN_NEGATENR:
2559 case ISN_NEWDICT:
2560 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002561 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002562 case ISN_OPANY:
2563 case ISN_OPFLOAT:
2564 case ISN_OPNR:
2565 case ISN_PCALL:
2566 case ISN_PCALL_END:
2567 case ISN_PROF_END:
2568 case ISN_PROF_START:
2569 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002570 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002571 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002572 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002573 case ISN_PUSHNR:
2574 case ISN_PUSHSPEC:
2575 case ISN_PUT:
2576 case ISN_REDIREND:
2577 case ISN_REDIRSTART:
2578 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002579 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002580 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002581 case ISN_SHUFFLE:
2582 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002583 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002584 case ISN_STORE:
2585 case ISN_STOREINDEX:
2586 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002587 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002588 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002589 case ISN_STORERANGE:
2590 case ISN_STOREREG:
2591 case ISN_STOREV:
2592 case ISN_STRINDEX:
2593 case ISN_STRSLICE:
2594 case ISN_THROW:
2595 case ISN_TRYCONT:
2596 case ISN_UNLETINDEX:
2597 case ISN_UNLETRANGE:
2598 case ISN_UNPACK:
2599 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002600 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002601 // nothing allocated
2602 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002603 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002604}
2605
2606 void
2607clear_instr_ga(garray_T *gap)
2608{
2609 int idx;
2610
2611 for (idx = 0; idx < gap->ga_len; ++idx)
2612 delete_instr(((isn_T *)gap->ga_data) + idx);
2613 ga_clear(gap);
2614}
2615
2616
2617#endif // defined(FEAT_EVAL)