blob: f5795cdf94b0c1edeec303b40a0283c3adc4131a [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;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000597 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000598 tt->tt_flags |= TTFLAG_NUMBER_OK;
599 }
600 }
601 else
602 tt = alloc_type(expected);
603
604 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000605 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100606 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000607 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
608
609 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000610 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000611
612 return OK;
613}
614
615 int
616generate_SETTYPE(
617 cctx_T *cctx,
618 type_T *expected)
619{
620 isn_T *isn;
621
622 RETURN_OK_IF_SKIP(cctx);
623 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
624 return FAIL;
625 isn->isn_arg.type.ct_type = alloc_type(expected);
626 return OK;
627}
628
629/*
630 * Generate a PUSH instruction for "tv".
631 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000632 */
633 int
634generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
635{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100636 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000637 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100638 case VAR_BOOL:
639 generate_PUSHBOOL(cctx, tv->vval.v_number);
640 break;
641 case VAR_SPECIAL:
642 generate_PUSHSPEC(cctx, tv->vval.v_number);
643 break;
644 case VAR_NUMBER:
645 generate_PUSHNR(cctx, tv->vval.v_number);
646 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100647 case VAR_FLOAT:
648 generate_PUSHF(cctx, tv->vval.v_float);
649 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100650 case VAR_BLOB:
651 generate_PUSHBLOB(cctx, tv->vval.v_blob);
652 tv->vval.v_blob = NULL;
653 break;
654 case VAR_LIST:
655 if (tv->vval.v_list != NULL)
656 iemsg("non-empty list constant not supported");
657 generate_NEWLIST(cctx, 0, TRUE);
658 break;
659 case VAR_DICT:
660 if (tv->vval.v_dict != NULL)
661 iemsg("non-empty dict constant not supported");
662 generate_NEWDICT(cctx, 0, TRUE);
663 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000664#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100665 case VAR_JOB:
666 if (tv->vval.v_job != NULL)
667 iemsg("non-null job constant not supported");
668 generate_PUSHJOB(cctx);
669 break;
670 case VAR_CHANNEL:
671 if (tv->vval.v_channel != NULL)
672 iemsg("non-null channel constant not supported");
673 generate_PUSHCHANNEL(cctx);
674 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000675#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100676 case VAR_FUNC:
677 if (tv->vval.v_string != NULL)
678 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100679 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100680 break;
681 case VAR_PARTIAL:
682 if (tv->vval.v_partial != NULL)
683 iemsg("non-null partial constant not supported");
684 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
685 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000686 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100687 break;
688 case VAR_STRING:
689 generate_PUSHS(cctx, &tv->vval.v_string);
690 tv->vval.v_string = NULL;
691 break;
692 default:
693 siemsg("constant type %d not supported", tv->v_type);
694 clear_tv(tv);
695 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000696 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100697 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000698 return OK;
699}
700
701/*
702 * Generate an ISN_PUSHNR instruction.
703 */
704 int
705generate_PUSHNR(cctx_T *cctx, varnumber_T number)
706{
707 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000708
709 RETURN_OK_IF_SKIP(cctx);
710 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
711 return FAIL;
712 isn->isn_arg.number = number;
713
714 if (number == 0 || number == 1)
715 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000716 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000717 return OK;
718}
719
720/*
721 * Generate an ISN_PUSHBOOL instruction.
722 */
723 int
724generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
725{
726 isn_T *isn;
727
728 RETURN_OK_IF_SKIP(cctx);
729 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
730 return FAIL;
731 isn->isn_arg.number = number;
732
733 return OK;
734}
735
736/*
737 * Generate an ISN_PUSHSPEC instruction.
738 */
739 int
740generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
741{
742 isn_T *isn;
743
744 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000745 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
746 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000747 return FAIL;
748 isn->isn_arg.number = number;
749
750 return OK;
751}
752
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000753/*
754 * Generate an ISN_PUSHF instruction.
755 */
756 int
757generate_PUSHF(cctx_T *cctx, float_T fnumber)
758{
759 isn_T *isn;
760
761 RETURN_OK_IF_SKIP(cctx);
762 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
763 return FAIL;
764 isn->isn_arg.fnumber = fnumber;
765
766 return OK;
767}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768
769/*
770 * Generate an ISN_PUSHS instruction.
771 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100772 * Note that if "str" is used in the instruction OK is returned and "*str" is
773 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000774 */
775 int
776generate_PUSHS(cctx_T *cctx, char_u **str)
777{
778 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100779 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000780
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100781 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100783 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
784 ret = FAIL;
785 else
786 {
787 isn->isn_arg.string = str == NULL ? NULL : *str;
788 return OK;
789 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100791 if (str != NULL)
792 VIM_CLEAR(*str);
793 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000794}
795
796/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000797 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000798 */
799 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000800generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000801{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000802 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100803#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100804 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000805 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100807#else
808 emsg(_(e_channel_job_feature_not_available));
809 return FAIL;
810#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000811}
812
813/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000814 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000815 */
816 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000817generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000818{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100820#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100821 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000822 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000823 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100824#else
825 emsg(_(e_channel_job_feature_not_available));
826 return FAIL;
827#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000828}
829
830/*
831 * Generate an ISN_PUSHBLOB instruction.
832 * Consumes "blob".
833 */
834 int
835generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
836{
837 isn_T *isn;
838
839 RETURN_OK_IF_SKIP(cctx);
840 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
841 return FAIL;
842 isn->isn_arg.blob = blob;
843
844 return OK;
845}
846
847/*
848 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100849 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
850 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851 */
852 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100853generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000854{
855 isn_T *isn;
856 char_u *funcname;
857
858 RETURN_OK_IF_SKIP(cctx);
859 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
860 return FAIL;
861 if (name == NULL)
862 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100863 else if (!may_prefix
864 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000865 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000866 funcname = vim_strsave(name);
867 else
868 {
869 funcname = alloc(STRLEN(name) + 3);
870 if (funcname != NULL)
871 {
872 STRCPY(funcname, "g:");
873 STRCPY(funcname + 2, name);
874 }
875 }
876
877 isn->isn_arg.string = funcname;
878 return OK;
879}
880
881/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000882 * Generate an ISN_AUTOLOAD instruction.
883 */
884 int
885generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
886{
887 isn_T *isn;
888
889 RETURN_OK_IF_SKIP(cctx);
890 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
891 return FAIL;
892 isn->isn_arg.string = vim_strsave(name);
893 if (isn->isn_arg.string == NULL)
894 return FAIL;
895 return OK;
896}
897
898/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000899 * Generate an ISN_GETITEM instruction with "index".
900 * "with_op" is TRUE for "+=" and other operators, the stack has the current
901 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100902 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903 */
904 int
905generate_GETITEM(cctx_T *cctx, int index, int with_op)
906{
907 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000908 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000909 type_T *item_type = &t_any;
910
911 RETURN_OK_IF_SKIP(cctx);
912
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000913 item_type = type->tt_member;
914 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
915 return FAIL;
916 isn->isn_arg.getitem.gi_index = index;
917 isn->isn_arg.getitem.gi_with_op = with_op;
918
919 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000920 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000921}
922
923/*
924 * Generate an ISN_SLICE instruction with "count".
925 */
926 int
927generate_SLICE(cctx_T *cctx, int count)
928{
929 isn_T *isn;
930
931 RETURN_OK_IF_SKIP(cctx);
932 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
933 return FAIL;
934 isn->isn_arg.number = count;
935 return OK;
936}
937
938/*
939 * Generate an ISN_CHECKLEN instruction with "min_len".
940 */
941 int
942generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
943{
944 isn_T *isn;
945
946 RETURN_OK_IF_SKIP(cctx);
947
948 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
949 return FAIL;
950 isn->isn_arg.checklen.cl_min_len = min_len;
951 isn->isn_arg.checklen.cl_more_OK = more_OK;
952
953 return OK;
954}
955
956/*
957 * Generate an ISN_STORE instruction.
958 */
959 int
960generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
961{
962 isn_T *isn;
963
964 RETURN_OK_IF_SKIP(cctx);
965 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
966 return FAIL;
967 if (name != NULL)
968 isn->isn_arg.string = vim_strsave(name);
969 else
970 isn->isn_arg.number = idx;
971
972 return OK;
973}
974
975/*
Bram Moolenaard505d172022-12-18 21:42:55 +0000976 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
977 * ("load" == FALSE) instruction.
978 */
979 int
980generate_CLASSMEMBER(
981 cctx_T *cctx,
982 int load,
983 class_T *cl,
984 int idx)
985{
986 isn_T *isn;
987
988 RETURN_OK_IF_SKIP(cctx);
989 if (load)
990 {
991 ocmember_T *m = &cl->class_class_members[idx];
992 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
993 }
994 else
995 {
996 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
997 }
998 if (isn == NULL)
999 return FAIL;
1000 isn->isn_arg.classmember.cm_class = cl;
1001 ++cl->class_refcount;
1002 isn->isn_arg.classmember.cm_idx = idx;
1003
1004 return OK;
1005}
1006
1007/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001008 * Generate an ISN_STOREOUTER instruction.
1009 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001010 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001011generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001012{
1013 isn_T *isn;
1014
1015 RETURN_OK_IF_SKIP(cctx);
1016 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1017 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001018 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1019 {
1020 // Store a variable defined in a loop. A copy will be made at the end
1021 // of the loop. TODO: how about deeper nesting?
1022 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1023 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1024 }
1025 else
1026 {
1027 isn->isn_arg.outer.outer_idx = idx;
1028 isn->isn_arg.outer.outer_depth = level;
1029 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001030
1031 return OK;
1032}
1033
1034/*
1035 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1036 */
1037 int
1038generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1039{
1040 isn_T *isn;
1041
1042 RETURN_OK_IF_SKIP(cctx);
1043 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1044 return FAIL;
1045 isn->isn_arg.storenr.stnr_idx = idx;
1046 isn->isn_arg.storenr.stnr_val = value;
1047
1048 return OK;
1049}
1050
1051/*
1052 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1053 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001054 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001055generate_STOREOPT(
1056 cctx_T *cctx,
1057 isntype_T isn_type,
1058 char_u *name,
1059 int opt_flags)
1060{
1061 isn_T *isn;
1062
1063 RETURN_OK_IF_SKIP(cctx);
1064 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1065 return FAIL;
1066 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1067 isn->isn_arg.storeopt.so_flags = opt_flags;
1068
1069 return OK;
1070}
1071
1072/*
1073 * Generate an ISN_LOAD or similar instruction.
1074 */
1075 int
1076generate_LOAD(
1077 cctx_T *cctx,
1078 isntype_T isn_type,
1079 int idx,
1080 char_u *name,
1081 type_T *type)
1082{
1083 isn_T *isn;
1084
1085 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001086 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001087 return FAIL;
1088 if (name != NULL)
1089 isn->isn_arg.string = vim_strsave(name);
1090 else
1091 isn->isn_arg.number = idx;
1092
1093 return OK;
1094}
1095
1096/*
1097 * Generate an ISN_LOADOUTER instruction
1098 */
1099 int
1100generate_LOADOUTER(
1101 cctx_T *cctx,
1102 int idx,
1103 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001104 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001105 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001106 type_T *type)
1107{
1108 isn_T *isn;
1109
1110 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001111 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001112 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001113 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1114 {
1115 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001116 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001117 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001118 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001119 }
1120 else
1121 {
1122 isn->isn_arg.outer.outer_idx = idx;
1123 isn->isn_arg.outer.outer_depth = nesting;
1124 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001125
1126 return OK;
1127}
1128
1129/*
1130 * Generate an ISN_LOADV instruction for v:var.
1131 */
1132 int
1133generate_LOADV(
1134 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001135 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001136{
1137 int di_flags;
1138 int vidx = find_vim_var(name, &di_flags);
1139 type_T *type;
1140
1141 RETURN_OK_IF_SKIP(cctx);
1142 if (vidx < 0)
1143 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001144 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001145 return FAIL;
1146 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001147 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001148 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1149}
1150
1151/*
1152 * Generate an ISN_UNLET instruction.
1153 */
1154 int
1155generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1156{
1157 isn_T *isn;
1158
1159 RETURN_OK_IF_SKIP(cctx);
1160 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1161 return FAIL;
1162 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1163 isn->isn_arg.unlet.ul_forceit = forceit;
1164
1165 return OK;
1166}
1167
1168/*
1169 * Generate an ISN_LOCKCONST instruction.
1170 */
1171 int
1172generate_LOCKCONST(cctx_T *cctx)
1173{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001174 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001175 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001176 return FAIL;
1177 return OK;
1178}
1179
1180/*
1181 * Generate an ISN_LOADS instruction.
1182 */
1183 int
1184generate_OLDSCRIPT(
1185 cctx_T *cctx,
1186 isntype_T isn_type,
1187 char_u *name,
1188 int sid,
1189 type_T *type)
1190{
1191 isn_T *isn;
1192
1193 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001194 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001195 isn = generate_instr_type(cctx, isn_type, type);
1196 else
1197 isn = generate_instr_drop(cctx, isn_type, 1);
1198 if (isn == NULL)
1199 return FAIL;
1200 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1201 isn->isn_arg.loadstore.ls_sid = sid;
1202
1203 return OK;
1204}
1205
1206/*
1207 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1208 */
1209 int
1210generate_VIM9SCRIPT(
1211 cctx_T *cctx,
1212 isntype_T isn_type,
1213 int sid,
1214 int idx,
1215 type_T *type)
1216{
1217 isn_T *isn;
1218 scriptref_T *sref;
1219 scriptitem_T *si = SCRIPT_ITEM(sid);
1220
1221 RETURN_OK_IF_SKIP(cctx);
1222 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001223 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001224 else
1225 isn = generate_instr_drop(cctx, isn_type, 1);
1226 if (isn == NULL)
1227 return FAIL;
1228
1229 // This requires three arguments, which doesn't fit in an instruction, thus
1230 // we need to allocate a struct for this.
1231 sref = ALLOC_ONE(scriptref_T);
1232 if (sref == NULL)
1233 return FAIL;
1234 isn->isn_arg.script.scriptref = sref;
1235 sref->sref_sid = sid;
1236 sref->sref_idx = idx;
1237 sref->sref_seq = si->sn_script_seq;
1238 sref->sref_type = type;
1239 return OK;
1240}
1241
1242/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001243 * Generate an ISN_NEWLIST instruction for "count" items.
1244 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001245 */
1246 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001247generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001248{
1249 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001250 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001252 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001253
1254 RETURN_OK_IF_SKIP(cctx);
1255 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1256 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001257 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001258
Bram Moolenaar078a4612022-01-04 15:17:03 +00001259 // Get the member type and the declared member type from all the items on
1260 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001261 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001262 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001263 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001264
1265 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001266 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001267
1268 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001269 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001270}
1271
1272/*
1273 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001274 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001275 */
1276 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001277generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001278{
1279 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001280 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001281 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001282 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001283
1284 RETURN_OK_IF_SKIP(cctx);
1285 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1286 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001287 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001288
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001289 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001290 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001291 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001292
1293 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001294 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001295
1296 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001297 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001298}
1299
1300/*
1301 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001302 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001303 * If variables were declared inside a loop "loop_var_idx" is the index of the
1304 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001305 */
1306 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001307generate_FUNCREF(
1308 cctx_T *cctx,
1309 ufunc_T *ufunc,
1310 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001311{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001312 isn_T *isn;
1313 type_T *type;
1314 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001315 loopvarinfo_T loopinfo;
1316 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001317
1318 RETURN_OK_IF_SKIP(cctx);
1319 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1320 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001321 if (isnp != NULL)
1322 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001323
Bram Moolenaarcc341812022-09-19 15:54:34 +01001324 has_vars = get_loop_var_info(cctx, &loopinfo);
1325 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001326 {
1327 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1328 if (extra == NULL)
1329 return FAIL;
1330 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001331 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001332 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001333 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001334 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001335 else
1336 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001337
1338 // Reserve an extra variable to keep track of the number of closures
1339 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001340 cctx->ctx_has_closure = 1;
1341
1342 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001343 // the nested context, including this one. But not a function defined at
1344 // the script level.
1345 if ((ufunc->uf_flags & FC_CLOSURE)
1346 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001347 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1348
Bram Moolenaar078a4612022-01-04 15:17:03 +00001349 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1350 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001351}
1352
1353/*
1354 * Generate an ISN_NEWFUNC instruction.
1355 * "lambda_name" and "func_name" must be in allocated memory and will be
1356 * consumed.
1357 */
1358 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001359generate_NEWFUNC(
1360 cctx_T *cctx,
1361 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001362 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001363{
1364 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001365 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001366
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001367 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001368 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001369 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1370 ret = FAIL;
1371 else
1372 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001373 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1374
1375 if (arg == NULL)
1376 ret = FAIL;
1377 else
1378 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001379 // Reserve an extra variable to keep track of the number of
1380 // closures created.
1381 cctx->ctx_has_closure = 1;
1382
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001383 isn->isn_arg.newfunc.nf_arg = arg;
1384 arg->nfa_lambda = lambda_name;
1385 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001386 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001387 return OK;
1388 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001389 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001390 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001391 vim_free(lambda_name);
1392 vim_free(func_name);
1393 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001394}
1395
1396/*
1397 * Generate an ISN_DEF instruction: list functions
1398 */
1399 int
1400generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1401{
1402 isn_T *isn;
1403
1404 RETURN_OK_IF_SKIP(cctx);
1405 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1406 return FAIL;
1407 if (len > 0)
1408 {
1409 isn->isn_arg.string = vim_strnsave(name, len);
1410 if (isn->isn_arg.string == NULL)
1411 return FAIL;
1412 }
1413 return OK;
1414}
1415
1416/*
1417 * Generate an ISN_JUMP instruction.
1418 */
1419 int
1420generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1421{
1422 isn_T *isn;
1423 garray_T *stack = &cctx->ctx_type_stack;
1424
1425 RETURN_OK_IF_SKIP(cctx);
1426 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1427 return FAIL;
1428 isn->isn_arg.jump.jump_when = when;
1429 isn->isn_arg.jump.jump_where = where;
1430
1431 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1432 --stack->ga_len;
1433
1434 return OK;
1435}
1436
1437/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001438 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1439 */
1440 int
1441generate_WHILE(cctx_T *cctx, int funcref_idx)
1442{
1443 isn_T *isn;
1444 garray_T *stack = &cctx->ctx_type_stack;
1445
1446 RETURN_OK_IF_SKIP(cctx);
1447 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1448 return FAIL;
1449 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1450 isn->isn_arg.whileloop.while_end = 0; // filled in later
1451
1452 if (stack->ga_len > 0)
1453 --stack->ga_len;
1454
1455 return OK;
1456}
1457
1458/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001459 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001460 */
1461 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001462generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001463{
1464 isn_T *isn;
1465
1466 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001467 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001468 return FAIL;
1469 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1470 // jump_where is set later
1471 return OK;
1472}
1473
1474 int
1475generate_FOR(cctx_T *cctx, int loop_idx)
1476{
1477 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001478
1479 RETURN_OK_IF_SKIP(cctx);
1480 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1481 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001482 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001484 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001485 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001486}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001487
1488 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001489generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001490{
1491 isn_T *isn;
1492
1493 RETURN_OK_IF_SKIP(cctx);
1494 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1495 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001496 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1497 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1498 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001499 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001500 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001501 return OK;
1502}
1503
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001504/*
1505 * Generate an ISN_TRYCONT instruction.
1506 */
1507 int
1508generate_TRYCONT(cctx_T *cctx, int levels, int where)
1509{
1510 isn_T *isn;
1511
1512 RETURN_OK_IF_SKIP(cctx);
1513 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1514 return FAIL;
1515 isn->isn_arg.trycont.tct_levels = levels;
1516 isn->isn_arg.trycont.tct_where = where;
1517
1518 return OK;
1519}
1520
Bram Moolenaar16900322022-09-08 19:51:45 +01001521/*
1522 * Check "argount" arguments and their types on the type stack.
1523 * Give an error and return FAIL if something is wrong.
1524 * When "method_call" is NULL no code is generated.
1525 */
1526 int
1527check_internal_func_args(
1528 cctx_T *cctx,
1529 int func_idx,
1530 int argcount,
1531 int method_call,
1532 type2_T **argtypes,
1533 type2_T *shuffled_argtypes)
1534{
1535 garray_T *stack = &cctx->ctx_type_stack;
1536 int argoff = check_internal_func(func_idx, argcount);
1537
1538 if (argoff < 0)
1539 return FAIL;
1540
1541 if (method_call && argoff > 1)
1542 {
1543 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1544
1545 if (isn == NULL)
1546 return FAIL;
1547 isn->isn_arg.shuffle.shfl_item = argcount;
1548 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1549 }
1550
1551 if (argcount > 0)
1552 {
1553 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1554
1555 // Check the types of the arguments.
1556 if (method_call && argoff > 1)
1557 {
1558 int i;
1559
1560 for (i = 0; i < argcount; ++i)
1561 shuffled_argtypes[i] = (i < argoff - 1)
1562 ? typep[i + 1]
1563 : (i == argoff - 1) ? typep[0] : typep[i];
1564 *argtypes = shuffled_argtypes;
1565 }
1566 else
1567 {
1568 int i;
1569
1570 for (i = 0; i < argcount; ++i)
1571 shuffled_argtypes[i] = typep[i];
1572 *argtypes = shuffled_argtypes;
1573 }
1574 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1575 cctx) == FAIL)
1576 return FAIL;
1577 }
1578 return OK;
1579}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001580
1581/*
1582 * Generate an ISN_BCALL instruction.
1583 * "method_call" is TRUE for "value->method()"
1584 * Return FAIL if the number of arguments is wrong.
1585 */
1586 int
1587generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1588{
1589 isn_T *isn;
1590 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001591 type2_T *argtypes = NULL;
1592 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1593 type2_T *maptype = NULL;
1594 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001595 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001596
1597 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001598
1599 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1600 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001601 return FAIL;
1602
Bram Moolenaar16900322022-09-08 19:51:45 +01001603 if (internal_func_is_map(func_idx))
1604 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001605
1606 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1607 return FAIL;
1608 isn->isn_arg.bfunc.cbf_idx = func_idx;
1609 isn->isn_arg.bfunc.cbf_argcount = argcount;
1610
1611 // Drop the argument types and push the return type.
1612 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001613 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1614 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001615 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001616
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001617 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1618 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001619 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001620 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001621
1622 return OK;
1623}
1624
1625/*
1626 * Generate an ISN_LISTAPPEND instruction. Works like add().
1627 * Argument count is already checked.
1628 */
1629 int
1630generate_LISTAPPEND(cctx_T *cctx)
1631{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001632 type_T *list_type;
1633 type_T *item_type;
1634 type_T *expected;
1635
1636 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001637 // For checking the item type we use the declared type of the list and the
1638 // current type of the added item, adding a string to [1, 2] is OK.
1639 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001640 if (arg_type_modifiable(list_type, 1) == FAIL)
1641 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001642 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001643 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001644 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001645 return FAIL;
1646
1647 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1648 return FAIL;
1649
Bram Moolenaar078a4612022-01-04 15:17:03 +00001650 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001651 return OK;
1652}
1653
1654/*
1655 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1656 * Argument count is already checked.
1657 */
1658 int
1659generate_BLOBAPPEND(cctx_T *cctx)
1660{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001661 type_T *item_type;
1662
Bram Moolenaarfa103972022-09-29 19:14:42 +01001663 // Caller already checked that blob_type is a blob, check it is modifiable.
1664 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1665 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001666 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001667 if (need_type(item_type, &t_number, FALSE,
1668 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001669 return FAIL;
1670
1671 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1672 return FAIL;
1673
Bram Moolenaar078a4612022-01-04 15:17:03 +00001674 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001675 return OK;
1676}
1677
1678/*
1679 * Generate an ISN_DCALL or ISN_UCALL instruction.
1680 * Return FAIL if the number of arguments is wrong.
1681 */
1682 int
1683generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1684{
1685 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001686 int regular_args = ufunc->uf_args.ga_len;
1687 int argcount = pushed_argcount;
1688
1689 RETURN_OK_IF_SKIP(cctx);
1690 if (argcount > regular_args && !has_varargs(ufunc))
1691 {
1692 semsg(_(e_too_many_arguments_for_function_str),
1693 printable_func_name(ufunc));
1694 return FAIL;
1695 }
1696 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1697 {
1698 semsg(_(e_not_enough_arguments_for_function_str),
1699 printable_func_name(ufunc));
1700 return FAIL;
1701 }
1702
1703 if (ufunc->uf_def_status != UF_NOT_COMPILED
1704 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1705 {
1706 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001707 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001708
1709 for (i = 0; i < argcount; ++i)
1710 {
1711 type_T *expected;
1712 type_T *actual;
1713
Bram Moolenaar078a4612022-01-04 15:17:03 +00001714 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001715 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001716 && i >= regular_args - ufunc->uf_def_args.ga_len)
1717 {
1718 // assume v:none used for default argument value
1719 continue;
1720 }
1721 if (i < regular_args)
1722 {
1723 if (ufunc->uf_arg_types == NULL)
1724 continue;
1725 expected = ufunc->uf_arg_types[i];
1726 }
1727 else if (ufunc->uf_va_type == NULL
1728 || ufunc->uf_va_type == &t_list_any)
1729 // possibly a lambda or "...: any"
1730 expected = &t_any;
1731 else
1732 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001733 if (need_type(actual, expected, FALSE,
1734 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001735 {
1736 arg_type_mismatch(expected, actual, i + 1);
1737 return FAIL;
1738 }
1739 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001740 compile_type = get_compile_type(ufunc);
1741 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001742 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001743 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001744 return FAIL;
1745 }
1746 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1747 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001748 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001749 ufunc->uf_name);
1750 return FAIL;
1751 }
1752
1753 if ((isn = generate_instr(cctx,
1754 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1755 : ISN_UCALL)) == NULL)
1756 return FAIL;
1757 if (isn->isn_type == ISN_DCALL)
1758 {
1759 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1760 isn->isn_arg.dfunc.cdf_argcount = argcount;
1761 }
1762 else
1763 {
1764 // A user function may be deleted and redefined later, can't use the
1765 // ufunc pointer, need to look it up again at runtime.
1766 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1767 isn->isn_arg.ufunc.cuf_argcount = argcount;
1768 }
1769
Bram Moolenaar078a4612022-01-04 15:17:03 +00001770 // drop the argument types
1771 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001772
Bram Moolenaar078a4612022-01-04 15:17:03 +00001773 // add return type
1774 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775}
1776
1777/*
1778 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1779 */
1780 int
1781generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1782{
1783 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001784
1785 RETURN_OK_IF_SKIP(cctx);
1786 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1787 return FAIL;
1788 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1789 isn->isn_arg.ufunc.cuf_argcount = argcount;
1790
Bram Moolenaar078a4612022-01-04 15:17:03 +00001791 // drop the argument types
1792 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001793
Bram Moolenaar078a4612022-01-04 15:17:03 +00001794 // add return value
1795 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001796}
1797
1798/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001799 * Check the arguments of function "type" against the types on the stack.
1800 * Returns OK or FAIL;
1801 */
1802 int
1803check_func_args_from_type(
1804 cctx_T *cctx,
1805 type_T *type,
1806 int argcount,
1807 int at_top,
1808 char_u *name)
1809{
1810 if (type->tt_argcount != -1)
1811 {
1812 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1813
1814 if (argcount < type->tt_min_argcount - varargs)
1815 {
1816 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1817 return FAIL;
1818 }
1819 if (!varargs && argcount > type->tt_argcount)
1820 {
1821 emsg_funcname(e_too_many_arguments_for_function_str, name);
1822 return FAIL;
1823 }
1824 if (type->tt_args != NULL)
1825 {
1826 int i;
1827
1828 for (i = 0; i < argcount; ++i)
1829 {
1830 int offset = -argcount + i - (at_top ? 0 : 1);
1831 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1832 type_T *expected;
1833
1834 if (varargs && i >= type->tt_argcount - 1)
1835 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1836 else if (i >= type->tt_min_argcount
1837 && actual->tt_type == VAR_SPECIAL)
1838 expected = &t_any;
1839 else
1840 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001841 if (need_type(actual, expected, FALSE,
1842 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001843 {
1844 arg_type_mismatch(expected, actual, i + 1);
1845 return FAIL;
1846 }
1847 }
1848 }
1849 }
1850
1851 return OK;
1852}
1853/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001854 * Generate an ISN_PCALL instruction.
1855 * "type" is the type of the FuncRef.
1856 */
1857 int
1858generate_PCALL(
1859 cctx_T *cctx,
1860 int argcount,
1861 char_u *name,
1862 type_T *type,
1863 int at_top)
1864{
1865 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001866 type_T *ret_type;
1867
1868 RETURN_OK_IF_SKIP(cctx);
1869
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001870 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001871 ret_type = &t_any;
1872 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1873 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001874 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1875 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001876
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001877 ret_type = type->tt_member;
1878 if (ret_type == &t_unknown)
1879 // return type not known yet, use a runtime check
1880 ret_type = &t_any;
1881 }
1882 else
1883 {
1884 semsg(_(e_not_callable_type_str), name);
1885 return FAIL;
1886 }
1887
1888 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1889 return FAIL;
1890 isn->isn_arg.pfunc.cpf_top = at_top;
1891 isn->isn_arg.pfunc.cpf_argcount = argcount;
1892
Bram Moolenaar078a4612022-01-04 15:17:03 +00001893 // drop the arguments and the funcref/partial
1894 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001895
Bram Moolenaar078a4612022-01-04 15:17:03 +00001896 // push the return value
1897 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001898
1899 // If partial is above the arguments it must be cleared and replaced with
1900 // the return value.
1901 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1902 return FAIL;
1903
1904 return OK;
1905}
1906
1907/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001908 * Generate an ISN_DEFER instruction.
1909 */
1910 int
1911generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1912{
1913 isn_T *isn;
1914
1915 RETURN_OK_IF_SKIP(cctx);
1916 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1917 return FAIL;
1918 isn->isn_arg.defer.defer_var_idx = var_idx;
1919 isn->isn_arg.defer.defer_argcount = argcount;
1920 return OK;
1921}
1922
1923/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001924 * Generate an ISN_STRINGMEMBER instruction.
1925 */
1926 int
1927generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1928{
1929 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001930 type_T *type;
1931
1932 RETURN_OK_IF_SKIP(cctx);
1933 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1934 return FAIL;
1935 isn->isn_arg.string = vim_strnsave(name, len);
1936
1937 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001938 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001939 if (type->tt_type != VAR_DICT
1940 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001941 {
1942 char *tofree;
1943
1944 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1945 name, type_name(type, &tofree));
1946 vim_free(tofree);
1947 return FAIL;
1948 }
1949 // change dict type to dict member type
1950 if (type->tt_type == VAR_DICT)
1951 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001952 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00001953 ? &t_any : type->tt_member;
1954 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955 }
1956
1957 return OK;
1958}
1959
1960/*
1961 * Generate an ISN_ECHO instruction.
1962 */
1963 int
1964generate_ECHO(cctx_T *cctx, int with_white, int count)
1965{
1966 isn_T *isn;
1967
1968 RETURN_OK_IF_SKIP(cctx);
1969 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1970 return FAIL;
1971 isn->isn_arg.echo.echo_with_white = with_white;
1972 isn->isn_arg.echo.echo_count = count;
1973
1974 return OK;
1975}
1976
1977/*
1978 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1979 */
1980 int
1981generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1982{
1983 isn_T *isn;
1984
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01001985 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001986 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1987 return FAIL;
1988 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001989 return OK;
1990}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001991
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001992/*
1993 * Generate an ISN_ECHOWINDOW instruction
1994 */
1995 int
1996generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
1997{
1998 isn_T *isn;
1999
2000 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2001 return FAIL;
2002 isn->isn_arg.echowin.ewin_count = count;
2003 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002004 return OK;
2005}
2006
2007/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002008 * Generate an ISN_SOURCE instruction.
2009 */
2010 int
2011generate_SOURCE(cctx_T *cctx, int sid)
2012{
2013 isn_T *isn;
2014
2015 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2016 return FAIL;
2017 isn->isn_arg.number = sid;
2018
2019 return OK;
2020}
2021
2022/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002023 * Generate an ISN_PUT instruction.
2024 */
2025 int
2026generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2027{
2028 isn_T *isn;
2029
2030 RETURN_OK_IF_SKIP(cctx);
2031 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2032 return FAIL;
2033 isn->isn_arg.put.put_regname = regname;
2034 isn->isn_arg.put.put_lnum = lnum;
2035 return OK;
2036}
2037
2038/*
2039 * Generate an EXEC instruction that takes a string argument.
2040 * A copy is made of "line".
2041 */
2042 int
2043generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2044{
2045 isn_T *isn;
2046
2047 RETURN_OK_IF_SKIP(cctx);
2048 if ((isn = generate_instr(cctx, isntype)) == NULL)
2049 return FAIL;
2050 isn->isn_arg.string = vim_strsave(line);
2051 return OK;
2052}
2053
2054/*
2055 * Generate an EXEC instruction that takes a string argument.
2056 * "str" must be allocated, it is consumed.
2057 */
2058 int
2059generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2060{
2061 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002062 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002063
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002064 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002065 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002066 if ((isn = generate_instr(cctx, isntype)) == NULL)
2067 ret = FAIL;
2068 else
2069 {
2070 isn->isn_arg.string = str;
2071 return OK;
2072 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002073 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002074 vim_free(str);
2075 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002076}
2077
2078 int
2079generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2080{
2081 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002082
2083 RETURN_OK_IF_SKIP(cctx);
2084 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2085 return FAIL;
2086 isn->isn_arg.string = vim_strsave(line);
2087
Bram Moolenaar078a4612022-01-04 15:17:03 +00002088 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002089}
2090
2091 int
2092generate_EXECCONCAT(cctx_T *cctx, int count)
2093{
2094 isn_T *isn;
2095
2096 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2097 return FAIL;
2098 isn->isn_arg.number = count;
2099 return OK;
2100}
2101
2102/*
2103 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2104 */
2105 int
2106generate_RANGE(cctx_T *cctx, char_u *range)
2107{
2108 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002109
2110 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2111 return FAIL;
2112 isn->isn_arg.string = range;
2113
Bram Moolenaar078a4612022-01-04 15:17:03 +00002114 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002115}
2116
2117 int
2118generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2119{
2120 isn_T *isn;
2121
2122 RETURN_OK_IF_SKIP(cctx);
2123 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2124 return FAIL;
2125 isn->isn_arg.unpack.unp_count = var_count;
2126 isn->isn_arg.unpack.unp_semicolon = semicolon;
2127 return OK;
2128}
2129
2130/*
2131 * Generate an instruction for any command modifiers.
2132 */
2133 int
2134generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2135{
2136 isn_T *isn;
2137
2138 if (has_cmdmod(cmod, FALSE))
2139 {
2140 cctx->ctx_has_cmdmod = TRUE;
2141
2142 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2143 return FAIL;
2144 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2145 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2146 return FAIL;
2147 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2148 // filter program now belongs to the instruction
2149 cmod->cmod_filter_regmatch.regprog = NULL;
2150 }
2151
2152 return OK;
2153}
2154
2155 int
2156generate_undo_cmdmods(cctx_T *cctx)
2157{
2158 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2159 return FAIL;
2160 cctx->ctx_has_cmdmod = FALSE;
2161 return OK;
2162}
2163
2164/*
2165 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002166 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002167 * Return FAIL when out of memory.
2168 */
2169 int
2170generate_store_var(
2171 cctx_T *cctx,
2172 assign_dest_T dest,
2173 int opt_flags,
2174 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002175 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002176 char_u *name,
2177 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178{
2179 switch (dest)
2180 {
2181 case dest_option:
2182 return generate_STOREOPT(cctx, ISN_STOREOPT,
2183 skip_option_env_lead(name), opt_flags);
2184 case dest_func_option:
2185 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2186 skip_option_env_lead(name), opt_flags);
2187 case dest_global:
2188 // include g: with the name, easier to execute that way
2189 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2190 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2191 case dest_buffer:
2192 // include b: with the name, easier to execute that way
2193 return generate_STORE(cctx, ISN_STOREB, 0, name);
2194 case dest_window:
2195 // include w: with the name, easier to execute that way
2196 return generate_STORE(cctx, ISN_STOREW, 0, name);
2197 case dest_tab:
2198 // include t: with the name, easier to execute that way
2199 return generate_STORE(cctx, ISN_STORET, 0, name);
2200 case dest_env:
2201 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2202 case dest_reg:
2203 return generate_STORE(cctx, ISN_STOREREG,
2204 name[1] == '@' ? '"' : name[1], NULL);
2205 case dest_vimvar:
2206 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2207 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002208 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002209 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2210 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2211 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002212 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002213 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002214
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002215 if (SCRIPT_ID_VALID(scriptvar_sid)
2216 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2217 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2218 == NULL)
2219 {
2220 // "import autoload './dir/script.vim'" - load script
2221 // first
2222 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2223 return FAIL;
2224 isn_type = ISN_STOREEXPORT;
2225 }
2226
2227 // "s:" may be included in the name.
2228 return generate_OLDSCRIPT(cctx, isn_type, name,
2229 scriptvar_sid, type);
2230 }
2231 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002232 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002233 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002234 case dest_class_member:
2235 return generate_CLASSMEMBER(cctx, FALSE,
2236 lhs->lhs_class, lhs->lhs_classmember_idx);
2237
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002238 case dest_local:
2239 case dest_expr:
2240 // cannot happen
2241 break;
2242 }
2243 return FAIL;
2244}
2245
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002246/*
2247 * Return TRUE when inside a "for" or "while" loop.
2248 */
2249 int
2250inside_loop_scope(cctx_T *cctx)
2251{
2252 scope_T *scope = cctx->ctx_scope;
2253
2254 for (;;)
2255 {
2256 if (scope == NULL)
2257 break;
2258 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2259 return TRUE;
2260 scope = scope->se_outer;
2261 }
2262 return FALSE;
2263}
2264
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002265 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002266generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002267{
2268 if (lhs->lhs_dest != dest_local)
2269 return generate_store_var(cctx, lhs->lhs_dest,
2270 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002271 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002272
2273 if (lhs->lhs_lvar != NULL)
2274 {
2275 garray_T *instr = &cctx->ctx_instr;
2276 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2277
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002278 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2279 // ISN_STORENR.
2280 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002281 if (lhs->lhs_lvar->lv_from_outer == 0
2282 && instr->ga_len == instr_count + 1
2283 && isn->isn_type == ISN_PUSHNR)
2284 {
2285 varnumber_T val = isn->isn_arg.number;
2286 garray_T *stack = &cctx->ctx_type_stack;
2287
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002288 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002289 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002290 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002291 --instr->ga_len;
2292 }
2293 else
2294 {
2295 isn->isn_type = ISN_STORENR;
2296 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2297 isn->isn_arg.storenr.stnr_val = val;
2298 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002299 if (stack->ga_len > 0)
2300 --stack->ga_len;
2301 }
2302 else if (lhs->lhs_lvar->lv_from_outer > 0)
2303 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002304 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002305 else
2306 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2307 }
2308 return OK;
2309}
2310
2311#if defined(FEAT_PROFILE) || defined(PROTO)
2312 void
2313may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2314{
2315 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2316 generate_instr(cctx, ISN_PROF_END);
2317}
2318#endif
2319
2320
2321/*
2322 * Delete an instruction, free what it contains.
2323 */
2324 void
2325delete_instr(isn_T *isn)
2326{
2327 switch (isn->isn_type)
2328 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002329 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002330 case ISN_DEF:
2331 case ISN_EXEC:
2332 case ISN_EXECRANGE:
2333 case ISN_EXEC_SPLIT:
2334 case ISN_LEGACY_EVAL:
2335 case ISN_LOADAUTO:
2336 case ISN_LOADB:
2337 case ISN_LOADENV:
2338 case ISN_LOADG:
2339 case ISN_LOADOPT:
2340 case ISN_LOADT:
2341 case ISN_LOADW:
2342 case ISN_LOCKUNLOCK:
2343 case ISN_PUSHEXC:
2344 case ISN_PUSHFUNC:
2345 case ISN_PUSHS:
2346 case ISN_RANGE:
2347 case ISN_STOREAUTO:
2348 case ISN_STOREB:
2349 case ISN_STOREENV:
2350 case ISN_STOREG:
2351 case ISN_STORET:
2352 case ISN_STOREW:
2353 case ISN_STRINGMEMBER:
2354 vim_free(isn->isn_arg.string);
2355 break;
2356
2357 case ISN_SUBSTITUTE:
2358 {
2359 int idx;
2360 isn_T *list = isn->isn_arg.subs.subs_instr;
2361
2362 vim_free(isn->isn_arg.subs.subs_cmd);
2363 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2364 delete_instr(list + idx);
2365 vim_free(list);
2366 }
2367 break;
2368
2369 case ISN_INSTR:
2370 {
2371 int idx;
2372 isn_T *list = isn->isn_arg.instr;
2373
2374 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2375 delete_instr(list + idx);
2376 vim_free(list);
2377 }
2378 break;
2379
2380 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002381 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002382 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002383 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002384 vim_free(isn->isn_arg.loadstore.ls_name);
2385 break;
2386
2387 case ISN_UNLET:
2388 case ISN_UNLETENV:
2389 vim_free(isn->isn_arg.unlet.ul_name);
2390 break;
2391
2392 case ISN_STOREOPT:
2393 case ISN_STOREFUNCOPT:
2394 vim_free(isn->isn_arg.storeopt.so_name);
2395 break;
2396
2397 case ISN_PUSHBLOB: // push blob isn_arg.blob
2398 blob_unref(isn->isn_arg.blob);
2399 break;
2400
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002401 case ISN_UCALL:
2402 vim_free(isn->isn_arg.ufunc.cuf_name);
2403 break;
2404
2405 case ISN_FUNCREF:
2406 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002407 funcref_T *funcref = &isn->isn_arg.funcref;
2408 funcref_extra_T *extra = funcref->fr_extra;
2409
2410 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002411 {
2412 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002413 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002414 ufunc_T *ufunc = dfunc->df_ufunc;
2415
2416 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2417 func_ptr_unref(ufunc);
2418 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002419 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002420 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002421 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002422
2423 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002424 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002425 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002426 vim_free(name);
2427 }
2428 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002429 }
2430 }
2431 break;
2432
2433 case ISN_DCALL:
2434 {
2435 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002436 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002437
2438 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002439 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002440 func_ptr_unref(dfunc->df_ufunc);
2441 }
2442 break;
2443
2444 case ISN_NEWFUNC:
2445 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002446 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002447
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002448 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002449 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002450 ufunc_T *ufunc = find_func_even_dead(
2451 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002452
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002453 if (ufunc != NULL)
2454 {
2455 unlink_def_function(ufunc);
2456 func_ptr_unref(ufunc);
2457 }
2458
2459 vim_free(arg->nfa_lambda);
2460 vim_free(arg->nfa_global);
2461 vim_free(arg);
2462 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002463 }
2464 break;
2465
2466 case ISN_CHECKTYPE:
2467 case ISN_SETTYPE:
2468 free_type(isn->isn_arg.type.ct_type);
2469 break;
2470
2471 case ISN_CMDMOD:
2472 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002473 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002474 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2475 break;
2476
2477 case ISN_LOADSCRIPT:
2478 case ISN_STORESCRIPT:
2479 vim_free(isn->isn_arg.script.scriptref);
2480 break;
2481
Bram Moolenaard505d172022-12-18 21:42:55 +00002482 case ISN_LOAD_CLASSMEMBER:
2483 case ISN_STORE_CLASSMEMBER:
2484 class_unref(isn->isn_arg.classmember.cm_class);
2485 break;
2486
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002487 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002488 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002489 break;
2490
2491 case ISN_CEXPR_CORE:
2492 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2493 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2494 break;
2495
2496 case ISN_2BOOL:
2497 case ISN_2STRING:
2498 case ISN_2STRING_ANY:
2499 case ISN_ADDBLOB:
2500 case ISN_ADDLIST:
2501 case ISN_ANYINDEX:
2502 case ISN_ANYSLICE:
2503 case ISN_BCALL:
2504 case ISN_BLOBAPPEND:
2505 case ISN_BLOBINDEX:
2506 case ISN_BLOBSLICE:
2507 case ISN_CATCH:
2508 case ISN_CEXPR_AUCMD:
2509 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002510 case ISN_CLEARDICT:
2511 case ISN_CMDMOD_REV:
2512 case ISN_COMPAREANY:
2513 case ISN_COMPAREBLOB:
2514 case ISN_COMPAREBOOL:
2515 case ISN_COMPAREDICT:
2516 case ISN_COMPAREFLOAT:
2517 case ISN_COMPAREFUNC:
2518 case ISN_COMPARELIST:
2519 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002520 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002521 case ISN_COMPARESPECIAL:
2522 case ISN_COMPARESTRING:
2523 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002524 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002525 case ISN_COND2BOOL:
2526 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002527 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002528 case ISN_DROP:
2529 case ISN_ECHO:
2530 case ISN_ECHOCONSOLE:
2531 case ISN_ECHOERR:
2532 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002533 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002534 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002535 case ISN_ENDTRY:
2536 case ISN_EXECCONCAT:
2537 case ISN_EXECUTE:
2538 case ISN_FINALLY:
2539 case ISN_FINISH:
2540 case ISN_FOR:
2541 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002542 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002543 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002544 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002545 case ISN_JUMP_IF_ARG_SET:
2546 case ISN_LISTAPPEND:
2547 case ISN_LISTINDEX:
2548 case ISN_LISTSLICE:
2549 case ISN_LOAD:
2550 case ISN_LOADBDICT:
2551 case ISN_LOADGDICT:
2552 case ISN_LOADOUTER:
2553 case ISN_LOADREG:
2554 case ISN_LOADTDICT:
2555 case ISN_LOADV:
2556 case ISN_LOADWDICT:
2557 case ISN_LOCKCONST:
2558 case ISN_MEMBER:
2559 case ISN_NEGATENR:
2560 case ISN_NEWDICT:
2561 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002562 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002563 case ISN_OPANY:
2564 case ISN_OPFLOAT:
2565 case ISN_OPNR:
2566 case ISN_PCALL:
2567 case ISN_PCALL_END:
2568 case ISN_PROF_END:
2569 case ISN_PROF_START:
2570 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002571 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002572 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002573 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002574 case ISN_PUSHNR:
2575 case ISN_PUSHSPEC:
2576 case ISN_PUT:
2577 case ISN_REDIREND:
2578 case ISN_REDIRSTART:
2579 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002580 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002581 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002582 case ISN_SHUFFLE:
2583 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002584 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002585 case ISN_STORE:
2586 case ISN_STOREINDEX:
2587 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002588 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002589 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002590 case ISN_STORERANGE:
2591 case ISN_STOREREG:
2592 case ISN_STOREV:
2593 case ISN_STRINDEX:
2594 case ISN_STRSLICE:
2595 case ISN_THROW:
2596 case ISN_TRYCONT:
2597 case ISN_UNLETINDEX:
2598 case ISN_UNLETRANGE:
2599 case ISN_UNPACK:
2600 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002601 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002602 // nothing allocated
2603 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002604 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002605}
2606
2607 void
2608clear_instr_ga(garray_T *gap)
2609{
2610 int idx;
2611
2612 for (idx = 0; idx < gap->ga_len; ++idx)
2613 delete_instr(((isn_T *)gap->ga_data) + idx);
2614 ga_clear(gap);
2615}
2616
2617
2618#endif // defined(FEAT_EVAL)