blob: 7fbf529a6477e9e9acc8850a69e86cdf561f6613 [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/*
117 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
118 * But only for simple types.
119 * When "tolerant" is TRUE convert most types to string, e.g. a List.
120 */
121 int
122may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
123{
124 isn_T *isn;
125 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000127
128 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000129 type = get_type_on_stack(cctx, -1 - offset);
130 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000131 {
132 // nothing to be done
133 case VAR_STRING: return OK;
134
135 // conversion possible
136 case VAR_SPECIAL:
137 case VAR_BOOL:
138 case VAR_NUMBER:
139 case VAR_FLOAT:
140 break;
141
142 // conversion possible (with runtime check)
143 case VAR_ANY:
144 case VAR_UNKNOWN:
145 isntype = ISN_2STRING_ANY;
146 break;
147
148 // conversion possible when tolerant
149 case VAR_LIST:
150 if (tolerant)
151 {
152 isntype = ISN_2STRING_ANY;
153 break;
154 }
155 // FALLTHROUGH
156
157 // conversion not possible
158 case VAR_VOID:
159 case VAR_BLOB:
160 case VAR_FUNC:
161 case VAR_PARTIAL:
162 case VAR_DICT:
163 case VAR_JOB:
164 case VAR_CHANNEL:
165 case VAR_INSTR:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000166 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000167 return FAIL;
168 }
169
Bram Moolenaar078a4612022-01-04 15:17:03 +0000170 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000171 if ((isn = generate_instr(cctx, isntype)) == NULL)
172 return FAIL;
173 isn->isn_arg.tostring.offset = offset;
174 isn->isn_arg.tostring.tolerant = tolerant;
175
176 return OK;
177}
178
179 static int
180check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
181{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000182 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
183 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000184 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000185 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000186 {
187 if (*op == '+')
188 emsg(_(e_wrong_argument_type_for_plus));
189 else
190 semsg(_(e_char_requires_number_or_float_arguments), *op);
191 return FAIL;
192 }
193 return OK;
194}
195
196/*
197 * Generate instruction for "+". For a list this creates a new list.
198 */
199 int
200generate_add_instr(
201 cctx_T *cctx,
202 vartype_T vartype,
203 type_T *type1,
204 type_T *type2,
205 exprtype_T expr_type)
206{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000207 isn_T *isn = generate_instr_drop(cctx,
208 vartype == VAR_NUMBER ? ISN_OPNR
209 : vartype == VAR_LIST ? ISN_ADDLIST
210 : vartype == VAR_BLOB ? ISN_ADDBLOB
211#ifdef FEAT_FLOAT
212 : vartype == VAR_FLOAT ? ISN_OPFLOAT
213#endif
214 : ISN_OPANY, 1);
215
216 if (vartype != VAR_LIST && vartype != VAR_BLOB
217 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000218 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000220 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000221 && check_number_or_float(
222 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
223 return FAIL;
224
225 if (isn != NULL)
226 {
227 if (isn->isn_type == ISN_ADDLIST)
228 isn->isn_arg.op.op_type = expr_type;
229 else
230 isn->isn_arg.op.op_type = EXPR_ADD;
231 }
232
233 // When concatenating two lists with different member types the member type
234 // becomes "any".
235 if (vartype == VAR_LIST
236 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
237 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000238 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000239
240 return isn == NULL ? FAIL : OK;
241}
242
243/*
244 * Get the type to use for an instruction for an operation on "type1" and
245 * "type2". If they are matching use a type-specific instruction. Otherwise
246 * fall back to runtime type checking.
247 */
248 vartype_T
249operator_type(type_T *type1, type_T *type2)
250{
251 if (type1->tt_type == type2->tt_type
252 && (type1->tt_type == VAR_NUMBER
253 || type1->tt_type == VAR_LIST
254#ifdef FEAT_FLOAT
255 || type1->tt_type == VAR_FLOAT
256#endif
257 || type1->tt_type == VAR_BLOB))
258 return type1->tt_type;
259 return VAR_ANY;
260}
261
262/*
263 * Generate an instruction with two arguments. The instruction depends on the
264 * type of the arguments.
265 */
266 int
267generate_two_op(cctx_T *cctx, char_u *op)
268{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000269 type_T *type1;
270 type_T *type2;
271 vartype_T vartype;
272 isn_T *isn;
273
274 RETURN_OK_IF_SKIP(cctx);
275
276 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000277 type1 = get_type_on_stack(cctx, 1);
278 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000279 vartype = operator_type(type1, type2);
280
281 switch (*op)
282 {
283 case '+':
284 if (generate_add_instr(cctx, vartype, type1, type2,
285 EXPR_COPY) == FAIL)
286 return FAIL;
287 break;
288
289 case '-':
290 case '*':
291 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
292 op) == FAIL)
293 return FAIL;
294 if (vartype == VAR_NUMBER)
295 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
296#ifdef FEAT_FLOAT
297 else if (vartype == VAR_FLOAT)
298 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
299#endif
300 else
301 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
302 if (isn != NULL)
303 isn->isn_arg.op.op_type = *op == '*'
304 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
305 break;
306
307 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000308 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000309 && type1->tt_type != VAR_NUMBER)
310 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000311 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000312 && type2->tt_type != VAR_NUMBER))
313 {
314 emsg(_(e_percent_requires_number_arguments));
315 return FAIL;
316 }
317 isn = generate_instr_drop(cctx,
318 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
319 if (isn != NULL)
320 isn->isn_arg.op.op_type = EXPR_REM;
321 break;
322 }
323
324 // correct type of result
325 if (vartype == VAR_ANY)
326 {
327 type_T *type = &t_any;
328
329#ifdef FEAT_FLOAT
330 // float+number and number+float results in float
331 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
332 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
333 type = &t_float;
334#endif
Bram Moolenaar078a4612022-01-04 15:17:03 +0000335 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000336 }
337
338 return OK;
339}
340
341/*
342 * Get the instruction to use for comparing "type1" with "type2"
343 * Return ISN_DROP when failed.
344 */
345 static isntype_T
346get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2)
347{
348 isntype_T isntype = ISN_DROP;
349
350 if (type1 == VAR_UNKNOWN)
351 type1 = VAR_ANY;
352 if (type2 == VAR_UNKNOWN)
353 type2 = VAR_ANY;
354
355 if (type1 == type2)
356 {
357 switch (type1)
358 {
359 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
360 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
361 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
362 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
363 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
364 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
365 case VAR_LIST: isntype = ISN_COMPARELIST; break;
366 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
367 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
368 default: isntype = ISN_COMPAREANY; break;
369 }
370 }
371 else if (type1 == VAR_ANY || type2 == VAR_ANY
372 || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT)
373 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT)))
374 isntype = ISN_COMPAREANY;
Bram Moolenaar7a222242022-03-01 19:23:24 +0000375 else if (type1 == VAR_SPECIAL || type2 == VAR_SPECIAL)
376 {
377 switch (type1 == VAR_SPECIAL ? type2 : type1)
378 {
379 case VAR_BLOB: break;
380 case VAR_CHANNEL: break;
381 case VAR_DICT: break;
382 case VAR_FUNC: break;
383 case VAR_JOB: break;
384 case VAR_LIST: break;
385 case VAR_PARTIAL: break;
386 case VAR_STRING: break;
387 default: semsg(_(e_cannot_compare_str_with_str),
388 vartype_name(type1), vartype_name(type2));
389 return ISN_DROP;
390 }
391 isntype = ISN_COMPARENULL;
392 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000393
394 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
395 && (isntype == ISN_COMPAREBOOL
396 || isntype == ISN_COMPARESPECIAL
397 || isntype == ISN_COMPARENR
398 || isntype == ISN_COMPAREFLOAT))
399 {
400 semsg(_(e_cannot_use_str_with_str),
401 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
402 return ISN_DROP;
403 }
404 if (isntype == ISN_DROP
405 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
406 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
407 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
408 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000409 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000410 && (type1 == VAR_BLOB || type2 == VAR_BLOB
411 || type1 == VAR_LIST || type2 == VAR_LIST))))
412 {
413 semsg(_(e_cannot_compare_str_with_str),
414 vartype_name(type1), vartype_name(type2));
415 return ISN_DROP;
416 }
417 return isntype;
418}
419
420 int
421check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
422{
423 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
424 return FAIL;
425 return OK;
426}
427
428/*
429 * Generate an ISN_COMPARE* instruction with a boolean result.
430 */
431 int
432generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
433{
434 isntype_T isntype;
435 isn_T *isn;
436 garray_T *stack = &cctx->ctx_type_stack;
437 vartype_T type1;
438 vartype_T type2;
439
440 RETURN_OK_IF_SKIP(cctx);
441
442 // Get the known type of the two items on the stack. If they are matching
443 // use a type-specific instruction. Otherwise fall back to runtime type
444 // checking.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000445 type1 = get_type_on_stack(cctx, 1)->tt_type;
446 type2 = get_type_on_stack(cctx, 0)->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000447 isntype = get_compare_isn(exprtype, type1, type2);
448 if (isntype == ISN_DROP)
449 return FAIL;
450
451 if ((isn = generate_instr(cctx, isntype)) == NULL)
452 return FAIL;
453 isn->isn_arg.op.op_type = exprtype;
454 isn->isn_arg.op.op_ic = ic;
455
456 // takes two arguments, puts one bool back
457 if (stack->ga_len >= 2)
458 {
459 --stack->ga_len;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000460 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000461 }
462
463 return OK;
464}
465
466/*
467 * Generate an ISN_2BOOL instruction.
468 * "offset" is the offset in the type stack.
469 */
470 int
471generate_2BOOL(cctx_T *cctx, int invert, int offset)
472{
473 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000474
475 RETURN_OK_IF_SKIP(cctx);
476 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
477 return FAIL;
478 isn->isn_arg.tobool.invert = invert;
479 isn->isn_arg.tobool.offset = offset;
480
481 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000482 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000483
484 return OK;
485}
486
487/*
488 * Generate an ISN_COND2BOOL instruction.
489 */
490 int
491generate_COND2BOOL(cctx_T *cctx)
492{
493 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000494
495 RETURN_OK_IF_SKIP(cctx);
496 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
497 return FAIL;
498
499 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000500 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000501
502 return OK;
503}
504
505 int
506generate_TYPECHECK(
507 cctx_T *cctx,
508 type_T *expected,
509 int offset,
510 int argidx)
511{
512 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000513
514 RETURN_OK_IF_SKIP(cctx);
515 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
516 return FAIL;
517 isn->isn_arg.type.ct_type = alloc_type(expected);
518 isn->isn_arg.type.ct_off = (int8_T)offset;
519 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
520
521 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000522 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000523
524 return OK;
525}
526
527 int
528generate_SETTYPE(
529 cctx_T *cctx,
530 type_T *expected)
531{
532 isn_T *isn;
533
534 RETURN_OK_IF_SKIP(cctx);
535 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
536 return FAIL;
537 isn->isn_arg.type.ct_type = alloc_type(expected);
538 return OK;
539}
540
541/*
542 * Generate a PUSH instruction for "tv".
543 * "tv" will be consumed or cleared.
544 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
545 */
546 int
547generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
548{
549 if (tv != NULL)
550 {
551 switch (tv->v_type)
552 {
553 case VAR_UNKNOWN:
554 break;
555 case VAR_BOOL:
556 generate_PUSHBOOL(cctx, tv->vval.v_number);
557 break;
558 case VAR_SPECIAL:
559 generate_PUSHSPEC(cctx, tv->vval.v_number);
560 break;
561 case VAR_NUMBER:
562 generate_PUSHNR(cctx, tv->vval.v_number);
563 break;
564#ifdef FEAT_FLOAT
565 case VAR_FLOAT:
566 generate_PUSHF(cctx, tv->vval.v_float);
567 break;
568#endif
569 case VAR_BLOB:
570 generate_PUSHBLOB(cctx, tv->vval.v_blob);
571 tv->vval.v_blob = NULL;
572 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000573 case VAR_LIST:
574 if (tv->vval.v_list != NULL)
575 iemsg("non-empty list constant not supported");
576 generate_NEWLIST(cctx, 0);
577 break;
578 case VAR_DICT:
579 if (tv->vval.v_dict != NULL)
580 iemsg("non-empty dict constant not supported");
581 generate_NEWDICT(cctx, 0);
582 break;
583#ifdef FEAT_JOB_CHANNEL
584 case VAR_JOB:
585 if (tv->vval.v_job != NULL)
586 iemsg("non-null job constant not supported");
587 generate_PUSHJOB(cctx, NULL);
588 break;
589 case VAR_CHANNEL:
590 if (tv->vval.v_channel != NULL)
591 iemsg("non-null channel constant not supported");
592 generate_PUSHCHANNEL(cctx, NULL);
593 break;
594#endif
595 case VAR_FUNC:
596 if (tv->vval.v_string != NULL)
597 iemsg("non-null function constant not supported");
598 generate_PUSHFUNC(cctx, NULL, &t_func_unknown);
599 break;
600 case VAR_PARTIAL:
601 if (tv->vval.v_partial != NULL)
602 iemsg("non-null partial constant not supported");
603 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
604 == NULL)
605 return FAIL;
606 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000607 case VAR_STRING:
608 generate_PUSHS(cctx, &tv->vval.v_string);
609 tv->vval.v_string = NULL;
610 break;
611 default:
612 iemsg("constant type not supported");
613 clear_tv(tv);
614 return FAIL;
615 }
616 tv->v_type = VAR_UNKNOWN;
617 }
618 return OK;
619}
620
621/*
622 * Generate an ISN_PUSHNR instruction.
623 */
624 int
625generate_PUSHNR(cctx_T *cctx, varnumber_T number)
626{
627 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000628
629 RETURN_OK_IF_SKIP(cctx);
630 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
631 return FAIL;
632 isn->isn_arg.number = number;
633
634 if (number == 0 || number == 1)
635 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000636 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000637 return OK;
638}
639
640/*
641 * Generate an ISN_PUSHBOOL instruction.
642 */
643 int
644generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
645{
646 isn_T *isn;
647
648 RETURN_OK_IF_SKIP(cctx);
649 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
650 return FAIL;
651 isn->isn_arg.number = number;
652
653 return OK;
654}
655
656/*
657 * Generate an ISN_PUSHSPEC instruction.
658 */
659 int
660generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
661{
662 isn_T *isn;
663
664 RETURN_OK_IF_SKIP(cctx);
665 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
666 return FAIL;
667 isn->isn_arg.number = number;
668
669 return OK;
670}
671
672#if defined(FEAT_FLOAT) || defined(PROTO)
673/*
674 * Generate an ISN_PUSHF instruction.
675 */
676 int
677generate_PUSHF(cctx_T *cctx, float_T fnumber)
678{
679 isn_T *isn;
680
681 RETURN_OK_IF_SKIP(cctx);
682 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
683 return FAIL;
684 isn->isn_arg.fnumber = fnumber;
685
686 return OK;
687}
688#endif
689
690/*
691 * Generate an ISN_PUSHS instruction.
692 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
693 */
694 int
695generate_PUSHS(cctx_T *cctx, char_u **str)
696{
697 isn_T *isn;
698
699 if (cctx->ctx_skip == SKIP_YES)
700 {
701 if (str != NULL)
702 VIM_CLEAR(*str);
703 return OK;
704 }
705 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
706 {
707 if (str != NULL)
708 VIM_CLEAR(*str);
709 return FAIL;
710 }
711 isn->isn_arg.string = str == NULL ? NULL : *str;
712
713 return OK;
714}
715
716/*
717 * Generate an ISN_PUSHCHANNEL instruction.
718 * Consumes "channel".
719 */
720 int
721generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
722{
723 isn_T *isn;
724
725 RETURN_OK_IF_SKIP(cctx);
726 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
727 return FAIL;
728 isn->isn_arg.channel = channel;
729
730 return OK;
731}
732
733/*
734 * Generate an ISN_PUSHJOB instruction.
735 * Consumes "job".
736 */
737 int
738generate_PUSHJOB(cctx_T *cctx, job_T *job)
739{
740 isn_T *isn;
741
742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000743 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_job)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000744 return FAIL;
745 isn->isn_arg.job = job;
746
747 return OK;
748}
749
750/*
751 * Generate an ISN_PUSHBLOB instruction.
752 * Consumes "blob".
753 */
754 int
755generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
756{
757 isn_T *isn;
758
759 RETURN_OK_IF_SKIP(cctx);
760 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
761 return FAIL;
762 isn->isn_arg.blob = blob;
763
764 return OK;
765}
766
767/*
768 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769 */
770 int
771generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
772{
773 isn_T *isn;
774 char_u *funcname;
775
776 RETURN_OK_IF_SKIP(cctx);
777 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
778 return FAIL;
779 if (name == NULL)
780 funcname = NULL;
Bram Moolenaard041f422022-01-12 19:54:00 +0000781 else if (*name == K_SPECIAL // script-local
782 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000783 funcname = vim_strsave(name);
784 else
785 {
786 funcname = alloc(STRLEN(name) + 3);
787 if (funcname != NULL)
788 {
789 STRCPY(funcname, "g:");
790 STRCPY(funcname + 2, name);
791 }
792 }
793
794 isn->isn_arg.string = funcname;
795 return OK;
796}
797
798/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000799 * Generate an ISN_AUTOLOAD instruction.
800 */
801 int
802generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
803{
804 isn_T *isn;
805
806 RETURN_OK_IF_SKIP(cctx);
807 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
808 return FAIL;
809 isn->isn_arg.string = vim_strsave(name);
810 if (isn->isn_arg.string == NULL)
811 return FAIL;
812 return OK;
813}
814
815/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000816 * Generate an ISN_GETITEM instruction with "index".
817 * "with_op" is TRUE for "+=" and other operators, the stack has the current
818 * value below the list with values.
819 */
820 int
821generate_GETITEM(cctx_T *cctx, int index, int with_op)
822{
823 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000824 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000825 type_T *item_type = &t_any;
826
827 RETURN_OK_IF_SKIP(cctx);
828
829 if (type->tt_type != VAR_LIST)
830 {
831 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000832 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000833 return FAIL;
834 }
835 item_type = type->tt_member;
836 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
837 return FAIL;
838 isn->isn_arg.getitem.gi_index = index;
839 isn->isn_arg.getitem.gi_with_op = with_op;
840
841 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000842 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000843}
844
845/*
846 * Generate an ISN_SLICE instruction with "count".
847 */
848 int
849generate_SLICE(cctx_T *cctx, int count)
850{
851 isn_T *isn;
852
853 RETURN_OK_IF_SKIP(cctx);
854 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
855 return FAIL;
856 isn->isn_arg.number = count;
857 return OK;
858}
859
860/*
861 * Generate an ISN_CHECKLEN instruction with "min_len".
862 */
863 int
864generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
865{
866 isn_T *isn;
867
868 RETURN_OK_IF_SKIP(cctx);
869
870 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
871 return FAIL;
872 isn->isn_arg.checklen.cl_min_len = min_len;
873 isn->isn_arg.checklen.cl_more_OK = more_OK;
874
875 return OK;
876}
877
878/*
879 * Generate an ISN_STORE instruction.
880 */
881 int
882generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
883{
884 isn_T *isn;
885
886 RETURN_OK_IF_SKIP(cctx);
887 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
888 return FAIL;
889 if (name != NULL)
890 isn->isn_arg.string = vim_strsave(name);
891 else
892 isn->isn_arg.number = idx;
893
894 return OK;
895}
896
897/*
898 * Generate an ISN_STOREOUTER instruction.
899 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000900 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000901generate_STOREOUTER(cctx_T *cctx, int idx, int level)
902{
903 isn_T *isn;
904
905 RETURN_OK_IF_SKIP(cctx);
906 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
907 return FAIL;
908 isn->isn_arg.outer.outer_idx = idx;
909 isn->isn_arg.outer.outer_depth = level;
910
911 return OK;
912}
913
914/*
915 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
916 */
917 int
918generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
919{
920 isn_T *isn;
921
922 RETURN_OK_IF_SKIP(cctx);
923 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
924 return FAIL;
925 isn->isn_arg.storenr.stnr_idx = idx;
926 isn->isn_arg.storenr.stnr_val = value;
927
928 return OK;
929}
930
931/*
932 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
933 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000934 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000935generate_STOREOPT(
936 cctx_T *cctx,
937 isntype_T isn_type,
938 char_u *name,
939 int opt_flags)
940{
941 isn_T *isn;
942
943 RETURN_OK_IF_SKIP(cctx);
944 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
945 return FAIL;
946 isn->isn_arg.storeopt.so_name = vim_strsave(name);
947 isn->isn_arg.storeopt.so_flags = opt_flags;
948
949 return OK;
950}
951
952/*
953 * Generate an ISN_LOAD or similar instruction.
954 */
955 int
956generate_LOAD(
957 cctx_T *cctx,
958 isntype_T isn_type,
959 int idx,
960 char_u *name,
961 type_T *type)
962{
963 isn_T *isn;
964
965 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000966 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000967 return FAIL;
968 if (name != NULL)
969 isn->isn_arg.string = vim_strsave(name);
970 else
971 isn->isn_arg.number = idx;
972
973 return OK;
974}
975
976/*
977 * Generate an ISN_LOADOUTER instruction
978 */
979 int
980generate_LOADOUTER(
981 cctx_T *cctx,
982 int idx,
983 int nesting,
984 type_T *type)
985{
986 isn_T *isn;
987
988 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000989 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000990 return FAIL;
991 isn->isn_arg.outer.outer_idx = idx;
992 isn->isn_arg.outer.outer_depth = nesting;
993
994 return OK;
995}
996
997/*
998 * Generate an ISN_LOADV instruction for v:var.
999 */
1000 int
1001generate_LOADV(
1002 cctx_T *cctx,
1003 char_u *name,
1004 int error)
1005{
1006 int di_flags;
1007 int vidx = find_vim_var(name, &di_flags);
1008 type_T *type;
1009
1010 RETURN_OK_IF_SKIP(cctx);
1011 if (vidx < 0)
1012 {
1013 if (error)
1014 semsg(_(e_variable_not_found_str), name);
1015 return FAIL;
1016 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001017 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001018 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1019}
1020
1021/*
1022 * Generate an ISN_UNLET instruction.
1023 */
1024 int
1025generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1026{
1027 isn_T *isn;
1028
1029 RETURN_OK_IF_SKIP(cctx);
1030 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1031 return FAIL;
1032 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1033 isn->isn_arg.unlet.ul_forceit = forceit;
1034
1035 return OK;
1036}
1037
1038/*
1039 * Generate an ISN_LOCKCONST instruction.
1040 */
1041 int
1042generate_LOCKCONST(cctx_T *cctx)
1043{
1044 isn_T *isn;
1045
1046 RETURN_OK_IF_SKIP(cctx);
1047 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1048 return FAIL;
1049 return OK;
1050}
1051
1052/*
1053 * Generate an ISN_LOADS instruction.
1054 */
1055 int
1056generate_OLDSCRIPT(
1057 cctx_T *cctx,
1058 isntype_T isn_type,
1059 char_u *name,
1060 int sid,
1061 type_T *type)
1062{
1063 isn_T *isn;
1064
1065 RETURN_OK_IF_SKIP(cctx);
1066 if (isn_type == ISN_LOADS)
1067 isn = generate_instr_type(cctx, isn_type, type);
1068 else
1069 isn = generate_instr_drop(cctx, isn_type, 1);
1070 if (isn == NULL)
1071 return FAIL;
1072 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1073 isn->isn_arg.loadstore.ls_sid = sid;
1074
1075 return OK;
1076}
1077
1078/*
1079 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1080 */
1081 int
1082generate_VIM9SCRIPT(
1083 cctx_T *cctx,
1084 isntype_T isn_type,
1085 int sid,
1086 int idx,
1087 type_T *type)
1088{
1089 isn_T *isn;
1090 scriptref_T *sref;
1091 scriptitem_T *si = SCRIPT_ITEM(sid);
1092
1093 RETURN_OK_IF_SKIP(cctx);
1094 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001095 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001096 else
1097 isn = generate_instr_drop(cctx, isn_type, 1);
1098 if (isn == NULL)
1099 return FAIL;
1100
1101 // This requires three arguments, which doesn't fit in an instruction, thus
1102 // we need to allocate a struct for this.
1103 sref = ALLOC_ONE(scriptref_T);
1104 if (sref == NULL)
1105 return FAIL;
1106 isn->isn_arg.script.scriptref = sref;
1107 sref->sref_sid = sid;
1108 sref->sref_idx = idx;
1109 sref->sref_seq = si->sn_script_seq;
1110 sref->sref_type = type;
1111 return OK;
1112}
1113
1114/*
1115 * Generate an ISN_NEWLIST instruction.
1116 */
1117 int
1118generate_NEWLIST(cctx_T *cctx, int count)
1119{
1120 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001121 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001122 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001123 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001124
1125 RETURN_OK_IF_SKIP(cctx);
1126 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1127 return FAIL;
1128 isn->isn_arg.number = count;
1129
Bram Moolenaar078a4612022-01-04 15:17:03 +00001130 // Get the member type and the declared member type from all the items on
1131 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001132 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001133 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001134 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001135
1136 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001137 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001138
1139 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001140 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141}
1142
1143/*
1144 * Generate an ISN_NEWDICT instruction.
1145 */
1146 int
1147generate_NEWDICT(cctx_T *cctx, int count)
1148{
1149 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001150 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001151 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001152 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001153
1154 RETURN_OK_IF_SKIP(cctx);
1155 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1156 return FAIL;
1157 isn->isn_arg.number = count;
1158
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001159 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001160 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001161 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162
1163 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001164 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165
1166 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001167 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001168}
1169
1170/*
1171 * Generate an ISN_FUNCREF instruction.
1172 */
1173 int
1174generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1175{
1176 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001177 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178
1179 RETURN_OK_IF_SKIP(cctx);
1180 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1181 return FAIL;
1182 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1183 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1184 else
1185 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1186 cctx->ctx_has_closure = 1;
1187
1188 // If the referenced function is a closure, it may use items further up in
1189 // the nested context, including this one.
1190 if (ufunc->uf_flags & FC_CLOSURE)
1191 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1192
Bram Moolenaar078a4612022-01-04 15:17:03 +00001193 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1194 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001195}
1196
1197/*
1198 * Generate an ISN_NEWFUNC instruction.
1199 * "lambda_name" and "func_name" must be in allocated memory and will be
1200 * consumed.
1201 */
1202 int
1203generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1204{
1205 isn_T *isn;
1206
1207 if (cctx->ctx_skip == SKIP_YES)
1208 {
1209 vim_free(lambda_name);
1210 vim_free(func_name);
1211 return OK;
1212 }
1213 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1214 {
1215 vim_free(lambda_name);
1216 vim_free(func_name);
1217 return FAIL;
1218 }
1219 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1220 isn->isn_arg.newfunc.nf_global = func_name;
1221
1222 return OK;
1223}
1224
1225/*
1226 * Generate an ISN_DEF instruction: list functions
1227 */
1228 int
1229generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1230{
1231 isn_T *isn;
1232
1233 RETURN_OK_IF_SKIP(cctx);
1234 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1235 return FAIL;
1236 if (len > 0)
1237 {
1238 isn->isn_arg.string = vim_strnsave(name, len);
1239 if (isn->isn_arg.string == NULL)
1240 return FAIL;
1241 }
1242 return OK;
1243}
1244
1245/*
1246 * Generate an ISN_JUMP instruction.
1247 */
1248 int
1249generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1250{
1251 isn_T *isn;
1252 garray_T *stack = &cctx->ctx_type_stack;
1253
1254 RETURN_OK_IF_SKIP(cctx);
1255 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1256 return FAIL;
1257 isn->isn_arg.jump.jump_when = when;
1258 isn->isn_arg.jump.jump_where = where;
1259
1260 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1261 --stack->ga_len;
1262
1263 return OK;
1264}
1265
1266/*
1267 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1268 */
1269 int
1270generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1271{
1272 isn_T *isn;
1273
1274 RETURN_OK_IF_SKIP(cctx);
1275 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1276 return FAIL;
1277 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1278 // jump_where is set later
1279 return OK;
1280}
1281
1282 int
1283generate_FOR(cctx_T *cctx, int loop_idx)
1284{
1285 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001286
1287 RETURN_OK_IF_SKIP(cctx);
1288 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1289 return FAIL;
1290 isn->isn_arg.forloop.for_idx = loop_idx;
1291
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001292 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001293 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294}
1295/*
1296 * Generate an ISN_TRYCONT instruction.
1297 */
1298 int
1299generate_TRYCONT(cctx_T *cctx, int levels, int where)
1300{
1301 isn_T *isn;
1302
1303 RETURN_OK_IF_SKIP(cctx);
1304 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1305 return FAIL;
1306 isn->isn_arg.trycont.tct_levels = levels;
1307 isn->isn_arg.trycont.tct_where = where;
1308
1309 return OK;
1310}
1311
1312
1313/*
1314 * Generate an ISN_BCALL instruction.
1315 * "method_call" is TRUE for "value->method()"
1316 * Return FAIL if the number of arguments is wrong.
1317 */
1318 int
1319generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1320{
1321 isn_T *isn;
1322 garray_T *stack = &cctx->ctx_type_stack;
1323 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001324 type2_T *typep;
1325 type2_T *argtypes = NULL;
1326 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1327 type2_T *maptype = NULL;
1328 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001329 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001330
1331 RETURN_OK_IF_SKIP(cctx);
1332 argoff = check_internal_func(func_idx, argcount);
1333 if (argoff < 0)
1334 return FAIL;
1335
1336 if (method_call && argoff > 1)
1337 {
1338 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1339 return FAIL;
1340 isn->isn_arg.shuffle.shfl_item = argcount;
1341 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1342 }
1343
1344 if (argcount > 0)
1345 {
1346 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001347 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001348 if (method_call && argoff > 1)
1349 {
1350 int i;
1351
1352 for (i = 0; i < argcount; ++i)
1353 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001354 ? typep[i + 1]
1355 : (i == argoff - 1) ? typep[0] : typep[i];
1356 argtypes = shuffled_argtypes;
1357 }
1358 else
1359 {
1360 int i;
1361
1362 for (i = 0; i < argcount; ++i)
1363 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001364 argtypes = shuffled_argtypes;
1365 }
1366 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1367 cctx) == FAIL)
1368 return FAIL;
1369 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001370 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001371 }
1372
1373 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1374 return FAIL;
1375 isn->isn_arg.bfunc.cbf_idx = func_idx;
1376 isn->isn_arg.bfunc.cbf_argcount = argcount;
1377
1378 // Drop the argument types and push the return type.
1379 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001380 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1381 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001383
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001384 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1385 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001386 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001387 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001388
1389 return OK;
1390}
1391
1392/*
1393 * Generate an ISN_LISTAPPEND instruction. Works like add().
1394 * Argument count is already checked.
1395 */
1396 int
1397generate_LISTAPPEND(cctx_T *cctx)
1398{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001399 type_T *list_type;
1400 type_T *item_type;
1401 type_T *expected;
1402
1403 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001404 // For checking the item type we use the declared type of the list and the
1405 // current type of the added item, adding a string to [1, 2] is OK.
1406 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001407 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001408 expected = list_type->tt_member;
1409 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1410 return FAIL;
1411
1412 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1413 return FAIL;
1414
Bram Moolenaar078a4612022-01-04 15:17:03 +00001415 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001416 return OK;
1417}
1418
1419/*
1420 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1421 * Argument count is already checked.
1422 */
1423 int
1424generate_BLOBAPPEND(cctx_T *cctx)
1425{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001426 type_T *item_type;
1427
1428 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001429 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001430 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1431 return FAIL;
1432
1433 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1434 return FAIL;
1435
Bram Moolenaar078a4612022-01-04 15:17:03 +00001436 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437 return OK;
1438}
1439
1440/*
1441 * Generate an ISN_DCALL or ISN_UCALL instruction.
1442 * Return FAIL if the number of arguments is wrong.
1443 */
1444 int
1445generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1446{
1447 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001448 int regular_args = ufunc->uf_args.ga_len;
1449 int argcount = pushed_argcount;
1450
1451 RETURN_OK_IF_SKIP(cctx);
1452 if (argcount > regular_args && !has_varargs(ufunc))
1453 {
1454 semsg(_(e_too_many_arguments_for_function_str),
1455 printable_func_name(ufunc));
1456 return FAIL;
1457 }
1458 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1459 {
1460 semsg(_(e_not_enough_arguments_for_function_str),
1461 printable_func_name(ufunc));
1462 return FAIL;
1463 }
1464
1465 if (ufunc->uf_def_status != UF_NOT_COMPILED
1466 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1467 {
1468 int i;
1469
1470 for (i = 0; i < argcount; ++i)
1471 {
1472 type_T *expected;
1473 type_T *actual;
1474
Bram Moolenaar078a4612022-01-04 15:17:03 +00001475 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001476 if (actual == &t_special
1477 && i >= regular_args - ufunc->uf_def_args.ga_len)
1478 {
1479 // assume v:none used for default argument value
1480 continue;
1481 }
1482 if (i < regular_args)
1483 {
1484 if (ufunc->uf_arg_types == NULL)
1485 continue;
1486 expected = ufunc->uf_arg_types[i];
1487 }
1488 else if (ufunc->uf_va_type == NULL
1489 || ufunc->uf_va_type == &t_list_any)
1490 // possibly a lambda or "...: any"
1491 expected = &t_any;
1492 else
1493 expected = ufunc->uf_va_type->tt_member;
1494 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1495 TRUE, FALSE) == FAIL)
1496 {
1497 arg_type_mismatch(expected, actual, i + 1);
1498 return FAIL;
1499 }
1500 }
1501 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
1502 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1503 COMPILE_TYPE(ufunc), NULL) == FAIL)
1504 return FAIL;
1505 }
1506 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1507 {
1508 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1509 ufunc->uf_name);
1510 return FAIL;
1511 }
1512
1513 if ((isn = generate_instr(cctx,
1514 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1515 : ISN_UCALL)) == NULL)
1516 return FAIL;
1517 if (isn->isn_type == ISN_DCALL)
1518 {
1519 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1520 isn->isn_arg.dfunc.cdf_argcount = argcount;
1521 }
1522 else
1523 {
1524 // A user function may be deleted and redefined later, can't use the
1525 // ufunc pointer, need to look it up again at runtime.
1526 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1527 isn->isn_arg.ufunc.cuf_argcount = argcount;
1528 }
1529
Bram Moolenaar078a4612022-01-04 15:17:03 +00001530 // drop the argument types
1531 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001532
Bram Moolenaar078a4612022-01-04 15:17:03 +00001533 // add return type
1534 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001535}
1536
1537/*
1538 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1539 */
1540 int
1541generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1542{
1543 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001544
1545 RETURN_OK_IF_SKIP(cctx);
1546 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1547 return FAIL;
1548 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1549 isn->isn_arg.ufunc.cuf_argcount = argcount;
1550
Bram Moolenaar078a4612022-01-04 15:17:03 +00001551 // drop the argument types
1552 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001553
Bram Moolenaar078a4612022-01-04 15:17:03 +00001554 // add return value
1555 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001556}
1557
1558/*
1559 * Generate an ISN_PCALL instruction.
1560 * "type" is the type of the FuncRef.
1561 */
1562 int
1563generate_PCALL(
1564 cctx_T *cctx,
1565 int argcount,
1566 char_u *name,
1567 type_T *type,
1568 int at_top)
1569{
1570 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001571 type_T *ret_type;
1572
1573 RETURN_OK_IF_SKIP(cctx);
1574
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001575 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576 ret_type = &t_any;
1577 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1578 {
1579 if (type->tt_argcount != -1)
1580 {
1581 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1582
1583 if (argcount < type->tt_min_argcount - varargs)
1584 {
1585 semsg(_(e_not_enough_arguments_for_function_str), name);
1586 return FAIL;
1587 }
1588 if (!varargs && argcount > type->tt_argcount)
1589 {
1590 semsg(_(e_too_many_arguments_for_function_str), name);
1591 return FAIL;
1592 }
1593 if (type->tt_args != NULL)
1594 {
1595 int i;
1596
1597 for (i = 0; i < argcount; ++i)
1598 {
1599 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001600 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001601 type_T *expected;
1602
1603 if (varargs && i >= type->tt_argcount - 1)
1604 expected = type->tt_args[
1605 type->tt_argcount - 1]->tt_member;
1606 else if (i >= type->tt_min_argcount
1607 && actual == &t_special)
1608 expected = &t_any;
1609 else
1610 expected = type->tt_args[i];
1611 if (need_type(actual, expected, offset, i + 1,
1612 cctx, TRUE, FALSE) == FAIL)
1613 {
1614 arg_type_mismatch(expected, actual, i + 1);
1615 return FAIL;
1616 }
1617 }
1618 }
1619 }
1620 ret_type = type->tt_member;
1621 if (ret_type == &t_unknown)
1622 // return type not known yet, use a runtime check
1623 ret_type = &t_any;
1624 }
1625 else
1626 {
1627 semsg(_(e_not_callable_type_str), name);
1628 return FAIL;
1629 }
1630
1631 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1632 return FAIL;
1633 isn->isn_arg.pfunc.cpf_top = at_top;
1634 isn->isn_arg.pfunc.cpf_argcount = argcount;
1635
Bram Moolenaar078a4612022-01-04 15:17:03 +00001636 // drop the arguments and the funcref/partial
1637 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001638
Bram Moolenaar078a4612022-01-04 15:17:03 +00001639 // push the return value
1640 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001641
1642 // If partial is above the arguments it must be cleared and replaced with
1643 // the return value.
1644 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1645 return FAIL;
1646
1647 return OK;
1648}
1649
1650/*
1651 * Generate an ISN_STRINGMEMBER instruction.
1652 */
1653 int
1654generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1655{
1656 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001657 type_T *type;
1658
1659 RETURN_OK_IF_SKIP(cctx);
1660 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1661 return FAIL;
1662 isn->isn_arg.string = vim_strnsave(name, len);
1663
1664 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001665 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001666 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001667 {
1668 char *tofree;
1669
1670 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1671 name, type_name(type, &tofree));
1672 vim_free(tofree);
1673 return FAIL;
1674 }
1675 // change dict type to dict member type
1676 if (type->tt_type == VAR_DICT)
1677 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001678 type_T *ntype = type->tt_member == &t_unknown
1679 ? &t_any : type->tt_member;
1680 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001681 }
1682
1683 return OK;
1684}
1685
1686/*
1687 * Generate an ISN_ECHO instruction.
1688 */
1689 int
1690generate_ECHO(cctx_T *cctx, int with_white, int count)
1691{
1692 isn_T *isn;
1693
1694 RETURN_OK_IF_SKIP(cctx);
1695 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1696 return FAIL;
1697 isn->isn_arg.echo.echo_with_white = with_white;
1698 isn->isn_arg.echo.echo_count = count;
1699
1700 return OK;
1701}
1702
1703/*
1704 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1705 */
1706 int
1707generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1708{
1709 isn_T *isn;
1710
1711 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1712 return FAIL;
1713 isn->isn_arg.number = count;
1714
1715 return OK;
1716}
1717
1718/*
1719 * Generate an ISN_PUT instruction.
1720 */
1721 int
1722generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1723{
1724 isn_T *isn;
1725
1726 RETURN_OK_IF_SKIP(cctx);
1727 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1728 return FAIL;
1729 isn->isn_arg.put.put_regname = regname;
1730 isn->isn_arg.put.put_lnum = lnum;
1731 return OK;
1732}
1733
1734/*
1735 * Generate an EXEC instruction that takes a string argument.
1736 * A copy is made of "line".
1737 */
1738 int
1739generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1740{
1741 isn_T *isn;
1742
1743 RETURN_OK_IF_SKIP(cctx);
1744 if ((isn = generate_instr(cctx, isntype)) == NULL)
1745 return FAIL;
1746 isn->isn_arg.string = vim_strsave(line);
1747 return OK;
1748}
1749
1750/*
1751 * Generate an EXEC instruction that takes a string argument.
1752 * "str" must be allocated, it is consumed.
1753 */
1754 int
1755generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1756{
1757 isn_T *isn;
1758
1759 if (cctx->ctx_skip == SKIP_YES)
1760 {
1761 vim_free(str);
1762 return OK;
1763 }
1764 if ((isn = generate_instr(cctx, isntype)) == NULL)
1765 {
1766 vim_free(str);
1767 return FAIL;
1768 }
1769 isn->isn_arg.string = str;
1770 return OK;
1771}
1772
1773 int
1774generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1775{
1776 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001777
1778 RETURN_OK_IF_SKIP(cctx);
1779 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1780 return FAIL;
1781 isn->isn_arg.string = vim_strsave(line);
1782
Bram Moolenaar078a4612022-01-04 15:17:03 +00001783 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001784}
1785
1786 int
1787generate_EXECCONCAT(cctx_T *cctx, int count)
1788{
1789 isn_T *isn;
1790
1791 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1792 return FAIL;
1793 isn->isn_arg.number = count;
1794 return OK;
1795}
1796
1797/*
1798 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1799 */
1800 int
1801generate_RANGE(cctx_T *cctx, char_u *range)
1802{
1803 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001804
1805 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1806 return FAIL;
1807 isn->isn_arg.string = range;
1808
Bram Moolenaar078a4612022-01-04 15:17:03 +00001809 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001810}
1811
1812 int
1813generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1814{
1815 isn_T *isn;
1816
1817 RETURN_OK_IF_SKIP(cctx);
1818 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1819 return FAIL;
1820 isn->isn_arg.unpack.unp_count = var_count;
1821 isn->isn_arg.unpack.unp_semicolon = semicolon;
1822 return OK;
1823}
1824
1825/*
1826 * Generate an instruction for any command modifiers.
1827 */
1828 int
1829generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1830{
1831 isn_T *isn;
1832
1833 if (has_cmdmod(cmod, FALSE))
1834 {
1835 cctx->ctx_has_cmdmod = TRUE;
1836
1837 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1838 return FAIL;
1839 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1840 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1841 return FAIL;
1842 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1843 // filter program now belongs to the instruction
1844 cmod->cmod_filter_regmatch.regprog = NULL;
1845 }
1846
1847 return OK;
1848}
1849
1850 int
1851generate_undo_cmdmods(cctx_T *cctx)
1852{
1853 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1854 return FAIL;
1855 cctx->ctx_has_cmdmod = FALSE;
1856 return OK;
1857}
1858
1859/*
1860 * Generate a STORE instruction for "dest", not being "dest_local".
1861 * Return FAIL when out of memory.
1862 */
1863 int
1864generate_store_var(
1865 cctx_T *cctx,
1866 assign_dest_T dest,
1867 int opt_flags,
1868 int vimvaridx,
1869 int scriptvar_idx,
1870 int scriptvar_sid,
1871 type_T *type,
1872 char_u *name)
1873{
1874 switch (dest)
1875 {
1876 case dest_option:
1877 return generate_STOREOPT(cctx, ISN_STOREOPT,
1878 skip_option_env_lead(name), opt_flags);
1879 case dest_func_option:
1880 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1881 skip_option_env_lead(name), opt_flags);
1882 case dest_global:
1883 // include g: with the name, easier to execute that way
1884 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1885 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1886 case dest_buffer:
1887 // include b: with the name, easier to execute that way
1888 return generate_STORE(cctx, ISN_STOREB, 0, name);
1889 case dest_window:
1890 // include w: with the name, easier to execute that way
1891 return generate_STORE(cctx, ISN_STOREW, 0, name);
1892 case dest_tab:
1893 // include t: with the name, easier to execute that way
1894 return generate_STORE(cctx, ISN_STORET, 0, name);
1895 case dest_env:
1896 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1897 case dest_reg:
1898 return generate_STORE(cctx, ISN_STOREREG,
1899 name[1] == '@' ? '"' : name[1], NULL);
1900 case dest_vimvar:
1901 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1902 case dest_script:
1903 if (scriptvar_idx < 0)
1904 // "s:" may be included in the name.
1905 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1906 scriptvar_sid, type);
1907 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1908 scriptvar_sid, scriptvar_idx, type);
1909 case dest_local:
1910 case dest_expr:
1911 // cannot happen
1912 break;
1913 }
1914 return FAIL;
1915}
1916
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001917/*
1918 * Return TRUE when inside a "for" or "while" loop.
1919 */
1920 int
1921inside_loop_scope(cctx_T *cctx)
1922{
1923 scope_T *scope = cctx->ctx_scope;
1924
1925 for (;;)
1926 {
1927 if (scope == NULL)
1928 break;
1929 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1930 return TRUE;
1931 scope = scope->se_outer;
1932 }
1933 return FALSE;
1934}
1935
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001936 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001937generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938{
1939 if (lhs->lhs_dest != dest_local)
1940 return generate_store_var(cctx, lhs->lhs_dest,
1941 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1942 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1943 lhs->lhs_type, lhs->lhs_name);
1944
1945 if (lhs->lhs_lvar != NULL)
1946 {
1947 garray_T *instr = &cctx->ctx_instr;
1948 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1949
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001950 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1951 // ISN_STORENR.
1952 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001953 if (lhs->lhs_lvar->lv_from_outer == 0
1954 && instr->ga_len == instr_count + 1
1955 && isn->isn_type == ISN_PUSHNR)
1956 {
1957 varnumber_T val = isn->isn_arg.number;
1958 garray_T *stack = &cctx->ctx_type_stack;
1959
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001960 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001961 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001962 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001963 --instr->ga_len;
1964 }
1965 else
1966 {
1967 isn->isn_type = ISN_STORENR;
1968 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1969 isn->isn_arg.storenr.stnr_val = val;
1970 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001971 if (stack->ga_len > 0)
1972 --stack->ga_len;
1973 }
1974 else if (lhs->lhs_lvar->lv_from_outer > 0)
1975 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1976 lhs->lhs_lvar->lv_from_outer);
1977 else
1978 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
1979 }
1980 return OK;
1981}
1982
1983#if defined(FEAT_PROFILE) || defined(PROTO)
1984 void
1985may_generate_prof_end(cctx_T *cctx, int prof_lnum)
1986{
1987 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
1988 generate_instr(cctx, ISN_PROF_END);
1989}
1990#endif
1991
1992
1993/*
1994 * Delete an instruction, free what it contains.
1995 */
1996 void
1997delete_instr(isn_T *isn)
1998{
1999 switch (isn->isn_type)
2000 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002001 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002002 case ISN_DEF:
2003 case ISN_EXEC:
2004 case ISN_EXECRANGE:
2005 case ISN_EXEC_SPLIT:
2006 case ISN_LEGACY_EVAL:
2007 case ISN_LOADAUTO:
2008 case ISN_LOADB:
2009 case ISN_LOADENV:
2010 case ISN_LOADG:
2011 case ISN_LOADOPT:
2012 case ISN_LOADT:
2013 case ISN_LOADW:
2014 case ISN_LOCKUNLOCK:
2015 case ISN_PUSHEXC:
2016 case ISN_PUSHFUNC:
2017 case ISN_PUSHS:
2018 case ISN_RANGE:
2019 case ISN_STOREAUTO:
2020 case ISN_STOREB:
2021 case ISN_STOREENV:
2022 case ISN_STOREG:
2023 case ISN_STORET:
2024 case ISN_STOREW:
2025 case ISN_STRINGMEMBER:
2026 vim_free(isn->isn_arg.string);
2027 break;
2028
2029 case ISN_SUBSTITUTE:
2030 {
2031 int idx;
2032 isn_T *list = isn->isn_arg.subs.subs_instr;
2033
2034 vim_free(isn->isn_arg.subs.subs_cmd);
2035 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2036 delete_instr(list + idx);
2037 vim_free(list);
2038 }
2039 break;
2040
2041 case ISN_INSTR:
2042 {
2043 int idx;
2044 isn_T *list = isn->isn_arg.instr;
2045
2046 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2047 delete_instr(list + idx);
2048 vim_free(list);
2049 }
2050 break;
2051
2052 case ISN_LOADS:
2053 case ISN_STORES:
2054 vim_free(isn->isn_arg.loadstore.ls_name);
2055 break;
2056
2057 case ISN_UNLET:
2058 case ISN_UNLETENV:
2059 vim_free(isn->isn_arg.unlet.ul_name);
2060 break;
2061
2062 case ISN_STOREOPT:
2063 case ISN_STOREFUNCOPT:
2064 vim_free(isn->isn_arg.storeopt.so_name);
2065 break;
2066
2067 case ISN_PUSHBLOB: // push blob isn_arg.blob
2068 blob_unref(isn->isn_arg.blob);
2069 break;
2070
2071 case ISN_PUSHJOB:
2072#ifdef FEAT_JOB_CHANNEL
2073 job_unref(isn->isn_arg.job);
2074#endif
2075 break;
2076
2077 case ISN_PUSHCHANNEL:
2078#ifdef FEAT_JOB_CHANNEL
2079 channel_unref(isn->isn_arg.channel);
2080#endif
2081 break;
2082
2083 case ISN_UCALL:
2084 vim_free(isn->isn_arg.ufunc.cuf_name);
2085 break;
2086
2087 case ISN_FUNCREF:
2088 {
2089 if (isn->isn_arg.funcref.fr_func_name == NULL)
2090 {
2091 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2092 + isn->isn_arg.funcref.fr_dfunc_idx;
2093 ufunc_T *ufunc = dfunc->df_ufunc;
2094
2095 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2096 func_ptr_unref(ufunc);
2097 }
2098 else
2099 {
2100 char_u *name = isn->isn_arg.funcref.fr_func_name;
2101
2102 if (name != NULL)
2103 func_unref(name);
2104 vim_free(isn->isn_arg.funcref.fr_func_name);
2105 }
2106 }
2107 break;
2108
2109 case ISN_DCALL:
2110 {
2111 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2112 + isn->isn_arg.dfunc.cdf_idx;
2113
2114 if (dfunc->df_ufunc != NULL
2115 && func_name_refcount(dfunc->df_ufunc->uf_name))
2116 func_ptr_unref(dfunc->df_ufunc);
2117 }
2118 break;
2119
2120 case ISN_NEWFUNC:
2121 {
2122 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002123 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002124
2125 if (ufunc != NULL)
2126 {
2127 unlink_def_function(ufunc);
2128 func_ptr_unref(ufunc);
2129 }
2130
2131 vim_free(lambda);
2132 vim_free(isn->isn_arg.newfunc.nf_global);
2133 }
2134 break;
2135
2136 case ISN_CHECKTYPE:
2137 case ISN_SETTYPE:
2138 free_type(isn->isn_arg.type.ct_type);
2139 break;
2140
2141 case ISN_CMDMOD:
2142 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2143 ->cmod_filter_regmatch.regprog);
2144 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2145 break;
2146
2147 case ISN_LOADSCRIPT:
2148 case ISN_STORESCRIPT:
2149 vim_free(isn->isn_arg.script.scriptref);
2150 break;
2151
2152 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002153 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002154 break;
2155
2156 case ISN_CEXPR_CORE:
2157 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2158 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2159 break;
2160
2161 case ISN_2BOOL:
2162 case ISN_2STRING:
2163 case ISN_2STRING_ANY:
2164 case ISN_ADDBLOB:
2165 case ISN_ADDLIST:
2166 case ISN_ANYINDEX:
2167 case ISN_ANYSLICE:
2168 case ISN_BCALL:
2169 case ISN_BLOBAPPEND:
2170 case ISN_BLOBINDEX:
2171 case ISN_BLOBSLICE:
2172 case ISN_CATCH:
2173 case ISN_CEXPR_AUCMD:
2174 case ISN_CHECKLEN:
2175 case ISN_CHECKNR:
2176 case ISN_CLEARDICT:
2177 case ISN_CMDMOD_REV:
2178 case ISN_COMPAREANY:
2179 case ISN_COMPAREBLOB:
2180 case ISN_COMPAREBOOL:
2181 case ISN_COMPAREDICT:
2182 case ISN_COMPAREFLOAT:
2183 case ISN_COMPAREFUNC:
2184 case ISN_COMPARELIST:
2185 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002186 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002187 case ISN_COMPARESPECIAL:
2188 case ISN_COMPARESTRING:
2189 case ISN_CONCAT:
2190 case ISN_COND2BOOL:
2191 case ISN_DEBUG:
2192 case ISN_DROP:
2193 case ISN_ECHO:
2194 case ISN_ECHOCONSOLE:
2195 case ISN_ECHOERR:
2196 case ISN_ECHOMSG:
2197 case ISN_ENDTRY:
2198 case ISN_EXECCONCAT:
2199 case ISN_EXECUTE:
2200 case ISN_FINALLY:
2201 case ISN_FINISH:
2202 case ISN_FOR:
2203 case ISN_GETITEM:
2204 case ISN_JUMP:
2205 case ISN_JUMP_IF_ARG_SET:
2206 case ISN_LISTAPPEND:
2207 case ISN_LISTINDEX:
2208 case ISN_LISTSLICE:
2209 case ISN_LOAD:
2210 case ISN_LOADBDICT:
2211 case ISN_LOADGDICT:
2212 case ISN_LOADOUTER:
2213 case ISN_LOADREG:
2214 case ISN_LOADTDICT:
2215 case ISN_LOADV:
2216 case ISN_LOADWDICT:
2217 case ISN_LOCKCONST:
2218 case ISN_MEMBER:
2219 case ISN_NEGATENR:
2220 case ISN_NEWDICT:
2221 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002222 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002223 case ISN_OPANY:
2224 case ISN_OPFLOAT:
2225 case ISN_OPNR:
2226 case ISN_PCALL:
2227 case ISN_PCALL_END:
2228 case ISN_PROF_END:
2229 case ISN_PROF_START:
2230 case ISN_PUSHBOOL:
2231 case ISN_PUSHF:
2232 case ISN_PUSHNR:
2233 case ISN_PUSHSPEC:
2234 case ISN_PUT:
2235 case ISN_REDIREND:
2236 case ISN_REDIRSTART:
2237 case ISN_RETURN:
2238 case ISN_RETURN_VOID:
2239 case ISN_SHUFFLE:
2240 case ISN_SLICE:
2241 case ISN_STORE:
2242 case ISN_STOREINDEX:
2243 case ISN_STORENR:
2244 case ISN_STOREOUTER:
2245 case ISN_STORERANGE:
2246 case ISN_STOREREG:
2247 case ISN_STOREV:
2248 case ISN_STRINDEX:
2249 case ISN_STRSLICE:
2250 case ISN_THROW:
2251 case ISN_TRYCONT:
2252 case ISN_UNLETINDEX:
2253 case ISN_UNLETRANGE:
2254 case ISN_UNPACK:
2255 case ISN_USEDICT:
2256 // nothing allocated
2257 break;
2258 }
2259}
2260
2261 void
2262clear_instr_ga(garray_T *gap)
2263{
2264 int idx;
2265
2266 for (idx = 0; idx < gap->ga_len; ++idx)
2267 delete_instr(((isn_T *)gap->ga_data) + idx);
2268 ga_clear(gap);
2269}
2270
2271
2272#endif // defined(FEAT_EVAL)