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