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