blob: 48b22a6d761b128fe5f228da9fb56fe8caca5b8f [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;
375
376 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
377 && (isntype == ISN_COMPAREBOOL
378 || isntype == ISN_COMPARESPECIAL
379 || isntype == ISN_COMPARENR
380 || isntype == ISN_COMPAREFLOAT))
381 {
382 semsg(_(e_cannot_use_str_with_str),
383 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
384 return ISN_DROP;
385 }
386 if (isntype == ISN_DROP
387 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
388 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
389 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
390 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
391 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
392 && (type1 == VAR_BLOB || type2 == VAR_BLOB
393 || type1 == VAR_LIST || type2 == VAR_LIST))))
394 {
395 semsg(_(e_cannot_compare_str_with_str),
396 vartype_name(type1), vartype_name(type2));
397 return ISN_DROP;
398 }
399 return isntype;
400}
401
402 int
403check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
404{
405 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
406 return FAIL;
407 return OK;
408}
409
410/*
411 * Generate an ISN_COMPARE* instruction with a boolean result.
412 */
413 int
414generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
415{
416 isntype_T isntype;
417 isn_T *isn;
418 garray_T *stack = &cctx->ctx_type_stack;
419 vartype_T type1;
420 vartype_T type2;
421
422 RETURN_OK_IF_SKIP(cctx);
423
424 // Get the known type of the two items on the stack. If they are matching
425 // use a type-specific instruction. Otherwise fall back to runtime type
426 // checking.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000427 type1 = get_type_on_stack(cctx, 1)->tt_type;
428 type2 = get_type_on_stack(cctx, 0)->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000429 isntype = get_compare_isn(exprtype, type1, type2);
430 if (isntype == ISN_DROP)
431 return FAIL;
432
433 if ((isn = generate_instr(cctx, isntype)) == NULL)
434 return FAIL;
435 isn->isn_arg.op.op_type = exprtype;
436 isn->isn_arg.op.op_ic = ic;
437
438 // takes two arguments, puts one bool back
439 if (stack->ga_len >= 2)
440 {
441 --stack->ga_len;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000442 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443 }
444
445 return OK;
446}
447
448/*
449 * Generate an ISN_2BOOL instruction.
450 * "offset" is the offset in the type stack.
451 */
452 int
453generate_2BOOL(cctx_T *cctx, int invert, int offset)
454{
455 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456
457 RETURN_OK_IF_SKIP(cctx);
458 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
459 return FAIL;
460 isn->isn_arg.tobool.invert = invert;
461 isn->isn_arg.tobool.offset = offset;
462
463 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000464 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000465
466 return OK;
467}
468
469/*
470 * Generate an ISN_COND2BOOL instruction.
471 */
472 int
473generate_COND2BOOL(cctx_T *cctx)
474{
475 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000476
477 RETURN_OK_IF_SKIP(cctx);
478 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
479 return FAIL;
480
481 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000482 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000483
484 return OK;
485}
486
487 int
488generate_TYPECHECK(
489 cctx_T *cctx,
490 type_T *expected,
491 int offset,
492 int argidx)
493{
494 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000495
496 RETURN_OK_IF_SKIP(cctx);
497 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
498 return FAIL;
499 isn->isn_arg.type.ct_type = alloc_type(expected);
500 isn->isn_arg.type.ct_off = (int8_T)offset;
501 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
502
503 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000504 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000505
506 return OK;
507}
508
509 int
510generate_SETTYPE(
511 cctx_T *cctx,
512 type_T *expected)
513{
514 isn_T *isn;
515
516 RETURN_OK_IF_SKIP(cctx);
517 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
518 return FAIL;
519 isn->isn_arg.type.ct_type = alloc_type(expected);
520 return OK;
521}
522
523/*
524 * Generate a PUSH instruction for "tv".
525 * "tv" will be consumed or cleared.
526 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
527 */
528 int
529generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
530{
531 if (tv != NULL)
532 {
533 switch (tv->v_type)
534 {
535 case VAR_UNKNOWN:
536 break;
537 case VAR_BOOL:
538 generate_PUSHBOOL(cctx, tv->vval.v_number);
539 break;
540 case VAR_SPECIAL:
541 generate_PUSHSPEC(cctx, tv->vval.v_number);
542 break;
543 case VAR_NUMBER:
544 generate_PUSHNR(cctx, tv->vval.v_number);
545 break;
546#ifdef FEAT_FLOAT
547 case VAR_FLOAT:
548 generate_PUSHF(cctx, tv->vval.v_float);
549 break;
550#endif
551 case VAR_BLOB:
552 generate_PUSHBLOB(cctx, tv->vval.v_blob);
553 tv->vval.v_blob = NULL;
554 break;
555 case VAR_STRING:
556 generate_PUSHS(cctx, &tv->vval.v_string);
557 tv->vval.v_string = NULL;
558 break;
559 default:
560 iemsg("constant type not supported");
561 clear_tv(tv);
562 return FAIL;
563 }
564 tv->v_type = VAR_UNKNOWN;
565 }
566 return OK;
567}
568
569/*
570 * Generate an ISN_PUSHNR instruction.
571 */
572 int
573generate_PUSHNR(cctx_T *cctx, varnumber_T number)
574{
575 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000576
577 RETURN_OK_IF_SKIP(cctx);
578 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
579 return FAIL;
580 isn->isn_arg.number = number;
581
582 if (number == 0 || number == 1)
583 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000584 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000585 return OK;
586}
587
588/*
589 * Generate an ISN_PUSHBOOL instruction.
590 */
591 int
592generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
593{
594 isn_T *isn;
595
596 RETURN_OK_IF_SKIP(cctx);
597 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
598 return FAIL;
599 isn->isn_arg.number = number;
600
601 return OK;
602}
603
604/*
605 * Generate an ISN_PUSHSPEC instruction.
606 */
607 int
608generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
609{
610 isn_T *isn;
611
612 RETURN_OK_IF_SKIP(cctx);
613 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
614 return FAIL;
615 isn->isn_arg.number = number;
616
617 return OK;
618}
619
620#if defined(FEAT_FLOAT) || defined(PROTO)
621/*
622 * Generate an ISN_PUSHF instruction.
623 */
624 int
625generate_PUSHF(cctx_T *cctx, float_T fnumber)
626{
627 isn_T *isn;
628
629 RETURN_OK_IF_SKIP(cctx);
630 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
631 return FAIL;
632 isn->isn_arg.fnumber = fnumber;
633
634 return OK;
635}
636#endif
637
638/*
639 * Generate an ISN_PUSHS instruction.
640 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
641 */
642 int
643generate_PUSHS(cctx_T *cctx, char_u **str)
644{
645 isn_T *isn;
646
647 if (cctx->ctx_skip == SKIP_YES)
648 {
649 if (str != NULL)
650 VIM_CLEAR(*str);
651 return OK;
652 }
653 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
654 {
655 if (str != NULL)
656 VIM_CLEAR(*str);
657 return FAIL;
658 }
659 isn->isn_arg.string = str == NULL ? NULL : *str;
660
661 return OK;
662}
663
664/*
665 * Generate an ISN_PUSHCHANNEL instruction.
666 * Consumes "channel".
667 */
668 int
669generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
670{
671 isn_T *isn;
672
673 RETURN_OK_IF_SKIP(cctx);
674 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
675 return FAIL;
676 isn->isn_arg.channel = channel;
677
678 return OK;
679}
680
681/*
682 * Generate an ISN_PUSHJOB instruction.
683 * Consumes "job".
684 */
685 int
686generate_PUSHJOB(cctx_T *cctx, job_T *job)
687{
688 isn_T *isn;
689
690 RETURN_OK_IF_SKIP(cctx);
691 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL)
692 return FAIL;
693 isn->isn_arg.job = job;
694
695 return OK;
696}
697
698/*
699 * Generate an ISN_PUSHBLOB instruction.
700 * Consumes "blob".
701 */
702 int
703generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
704{
705 isn_T *isn;
706
707 RETURN_OK_IF_SKIP(cctx);
708 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
709 return FAIL;
710 isn->isn_arg.blob = blob;
711
712 return OK;
713}
714
715/*
716 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000717 */
718 int
719generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
720{
721 isn_T *isn;
722 char_u *funcname;
723
724 RETURN_OK_IF_SKIP(cctx);
725 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
726 return FAIL;
727 if (name == NULL)
728 funcname = NULL;
Bram Moolenaard041f422022-01-12 19:54:00 +0000729 else if (*name == K_SPECIAL // script-local
730 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000731 funcname = vim_strsave(name);
732 else
733 {
734 funcname = alloc(STRLEN(name) + 3);
735 if (funcname != NULL)
736 {
737 STRCPY(funcname, "g:");
738 STRCPY(funcname + 2, name);
739 }
740 }
741
742 isn->isn_arg.string = funcname;
743 return OK;
744}
745
746/*
747 * Generate an ISN_GETITEM instruction with "index".
748 * "with_op" is TRUE for "+=" and other operators, the stack has the current
749 * value below the list with values.
750 */
751 int
752generate_GETITEM(cctx_T *cctx, int index, int with_op)
753{
754 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000755 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000756 type_T *item_type = &t_any;
757
758 RETURN_OK_IF_SKIP(cctx);
759
760 if (type->tt_type != VAR_LIST)
761 {
762 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000763 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000764 return FAIL;
765 }
766 item_type = type->tt_member;
767 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
768 return FAIL;
769 isn->isn_arg.getitem.gi_index = index;
770 isn->isn_arg.getitem.gi_with_op = with_op;
771
772 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000773 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000774}
775
776/*
777 * Generate an ISN_SLICE instruction with "count".
778 */
779 int
780generate_SLICE(cctx_T *cctx, int count)
781{
782 isn_T *isn;
783
784 RETURN_OK_IF_SKIP(cctx);
785 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
786 return FAIL;
787 isn->isn_arg.number = count;
788 return OK;
789}
790
791/*
792 * Generate an ISN_CHECKLEN instruction with "min_len".
793 */
794 int
795generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
796{
797 isn_T *isn;
798
799 RETURN_OK_IF_SKIP(cctx);
800
801 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
802 return FAIL;
803 isn->isn_arg.checklen.cl_min_len = min_len;
804 isn->isn_arg.checklen.cl_more_OK = more_OK;
805
806 return OK;
807}
808
809/*
810 * Generate an ISN_STORE instruction.
811 */
812 int
813generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
814{
815 isn_T *isn;
816
817 RETURN_OK_IF_SKIP(cctx);
818 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
819 return FAIL;
820 if (name != NULL)
821 isn->isn_arg.string = vim_strsave(name);
822 else
823 isn->isn_arg.number = idx;
824
825 return OK;
826}
827
828/*
829 * Generate an ISN_STOREOUTER instruction.
830 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000831 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000832generate_STOREOUTER(cctx_T *cctx, int idx, int level)
833{
834 isn_T *isn;
835
836 RETURN_OK_IF_SKIP(cctx);
837 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
838 return FAIL;
839 isn->isn_arg.outer.outer_idx = idx;
840 isn->isn_arg.outer.outer_depth = level;
841
842 return OK;
843}
844
845/*
846 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
847 */
848 int
849generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
850{
851 isn_T *isn;
852
853 RETURN_OK_IF_SKIP(cctx);
854 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
855 return FAIL;
856 isn->isn_arg.storenr.stnr_idx = idx;
857 isn->isn_arg.storenr.stnr_val = value;
858
859 return OK;
860}
861
862/*
863 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
864 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000865 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000866generate_STOREOPT(
867 cctx_T *cctx,
868 isntype_T isn_type,
869 char_u *name,
870 int opt_flags)
871{
872 isn_T *isn;
873
874 RETURN_OK_IF_SKIP(cctx);
875 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
876 return FAIL;
877 isn->isn_arg.storeopt.so_name = vim_strsave(name);
878 isn->isn_arg.storeopt.so_flags = opt_flags;
879
880 return OK;
881}
882
883/*
884 * Generate an ISN_LOAD or similar instruction.
885 */
886 int
887generate_LOAD(
888 cctx_T *cctx,
889 isntype_T isn_type,
890 int idx,
891 char_u *name,
892 type_T *type)
893{
894 isn_T *isn;
895
896 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000897 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000898 return FAIL;
899 if (name != NULL)
900 isn->isn_arg.string = vim_strsave(name);
901 else
902 isn->isn_arg.number = idx;
903
904 return OK;
905}
906
907/*
908 * Generate an ISN_LOADOUTER instruction
909 */
910 int
911generate_LOADOUTER(
912 cctx_T *cctx,
913 int idx,
914 int nesting,
915 type_T *type)
916{
917 isn_T *isn;
918
919 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000920 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000921 return FAIL;
922 isn->isn_arg.outer.outer_idx = idx;
923 isn->isn_arg.outer.outer_depth = nesting;
924
925 return OK;
926}
927
928/*
929 * Generate an ISN_LOADV instruction for v:var.
930 */
931 int
932generate_LOADV(
933 cctx_T *cctx,
934 char_u *name,
935 int error)
936{
937 int di_flags;
938 int vidx = find_vim_var(name, &di_flags);
939 type_T *type;
940
941 RETURN_OK_IF_SKIP(cctx);
942 if (vidx < 0)
943 {
944 if (error)
945 semsg(_(e_variable_not_found_str), name);
946 return FAIL;
947 }
Bram Moolenaard787e402021-12-24 21:36:12 +0000948 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000949 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
950}
951
952/*
953 * Generate an ISN_UNLET instruction.
954 */
955 int
956generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
957{
958 isn_T *isn;
959
960 RETURN_OK_IF_SKIP(cctx);
961 if ((isn = generate_instr(cctx, isn_type)) == NULL)
962 return FAIL;
963 isn->isn_arg.unlet.ul_name = vim_strsave(name);
964 isn->isn_arg.unlet.ul_forceit = forceit;
965
966 return OK;
967}
968
969/*
970 * Generate an ISN_LOCKCONST instruction.
971 */
972 int
973generate_LOCKCONST(cctx_T *cctx)
974{
975 isn_T *isn;
976
977 RETURN_OK_IF_SKIP(cctx);
978 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
979 return FAIL;
980 return OK;
981}
982
983/*
984 * Generate an ISN_LOADS instruction.
985 */
986 int
987generate_OLDSCRIPT(
988 cctx_T *cctx,
989 isntype_T isn_type,
990 char_u *name,
991 int sid,
992 type_T *type)
993{
994 isn_T *isn;
995
996 RETURN_OK_IF_SKIP(cctx);
997 if (isn_type == ISN_LOADS)
998 isn = generate_instr_type(cctx, isn_type, type);
999 else
1000 isn = generate_instr_drop(cctx, isn_type, 1);
1001 if (isn == NULL)
1002 return FAIL;
1003 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1004 isn->isn_arg.loadstore.ls_sid = sid;
1005
1006 return OK;
1007}
1008
1009/*
1010 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1011 */
1012 int
1013generate_VIM9SCRIPT(
1014 cctx_T *cctx,
1015 isntype_T isn_type,
1016 int sid,
1017 int idx,
1018 type_T *type)
1019{
1020 isn_T *isn;
1021 scriptref_T *sref;
1022 scriptitem_T *si = SCRIPT_ITEM(sid);
1023
1024 RETURN_OK_IF_SKIP(cctx);
1025 if (isn_type == ISN_LOADSCRIPT)
1026 isn = generate_instr_type(cctx, isn_type, type);
1027 else
1028 isn = generate_instr_drop(cctx, isn_type, 1);
1029 if (isn == NULL)
1030 return FAIL;
1031
1032 // This requires three arguments, which doesn't fit in an instruction, thus
1033 // we need to allocate a struct for this.
1034 sref = ALLOC_ONE(scriptref_T);
1035 if (sref == NULL)
1036 return FAIL;
1037 isn->isn_arg.script.scriptref = sref;
1038 sref->sref_sid = sid;
1039 sref->sref_idx = idx;
1040 sref->sref_seq = si->sn_script_seq;
1041 sref->sref_type = type;
1042 return OK;
1043}
1044
1045/*
1046 * Generate an ISN_NEWLIST instruction.
1047 */
1048 int
1049generate_NEWLIST(cctx_T *cctx, int count)
1050{
1051 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001052 type_T *member_type;
1053 type_T *decl_member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001054 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001055 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001056
1057 RETURN_OK_IF_SKIP(cctx);
1058 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1059 return FAIL;
1060 isn->isn_arg.number = count;
1061
Bram Moolenaar078a4612022-01-04 15:17:03 +00001062 // Get the member type and the declared member type from all the items on
1063 // the stack.
1064 member_type = get_member_type_from_stack(count, 1, &decl_member_type, cctx);
1065 type = get_list_type(member_type, cctx->ctx_type_list);
1066 decl_type = get_list_type(decl_member_type, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001067
1068 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001069 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001070
1071 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001072 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001073}
1074
1075/*
1076 * Generate an ISN_NEWDICT instruction.
1077 */
1078 int
1079generate_NEWDICT(cctx_T *cctx, int count)
1080{
1081 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001082 type_T *member_type;
1083 type_T *decl_member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001084 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001085 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001086
1087 RETURN_OK_IF_SKIP(cctx);
1088 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1089 return FAIL;
1090 isn->isn_arg.number = count;
1091
Bram Moolenaar078a4612022-01-04 15:17:03 +00001092 member_type = get_member_type_from_stack(count, 2,
1093 &decl_member_type, cctx);
1094 type = get_dict_type(member_type, cctx->ctx_type_list);
1095 decl_type = get_dict_type(decl_member_type, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001096
1097 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001098 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099
1100 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001101 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001102}
1103
1104/*
1105 * Generate an ISN_FUNCREF instruction.
1106 */
1107 int
1108generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1109{
1110 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001111 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001112
1113 RETURN_OK_IF_SKIP(cctx);
1114 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1115 return FAIL;
1116 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1117 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1118 else
1119 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1120 cctx->ctx_has_closure = 1;
1121
1122 // If the referenced function is a closure, it may use items further up in
1123 // the nested context, including this one.
1124 if (ufunc->uf_flags & FC_CLOSURE)
1125 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1126
Bram Moolenaar078a4612022-01-04 15:17:03 +00001127 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1128 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001129}
1130
1131/*
1132 * Generate an ISN_NEWFUNC instruction.
1133 * "lambda_name" and "func_name" must be in allocated memory and will be
1134 * consumed.
1135 */
1136 int
1137generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1138{
1139 isn_T *isn;
1140
1141 if (cctx->ctx_skip == SKIP_YES)
1142 {
1143 vim_free(lambda_name);
1144 vim_free(func_name);
1145 return OK;
1146 }
1147 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1148 {
1149 vim_free(lambda_name);
1150 vim_free(func_name);
1151 return FAIL;
1152 }
1153 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1154 isn->isn_arg.newfunc.nf_global = func_name;
1155
1156 return OK;
1157}
1158
1159/*
1160 * Generate an ISN_DEF instruction: list functions
1161 */
1162 int
1163generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1164{
1165 isn_T *isn;
1166
1167 RETURN_OK_IF_SKIP(cctx);
1168 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1169 return FAIL;
1170 if (len > 0)
1171 {
1172 isn->isn_arg.string = vim_strnsave(name, len);
1173 if (isn->isn_arg.string == NULL)
1174 return FAIL;
1175 }
1176 return OK;
1177}
1178
1179/*
1180 * Generate an ISN_JUMP instruction.
1181 */
1182 int
1183generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1184{
1185 isn_T *isn;
1186 garray_T *stack = &cctx->ctx_type_stack;
1187
1188 RETURN_OK_IF_SKIP(cctx);
1189 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.jump.jump_when = when;
1192 isn->isn_arg.jump.jump_where = where;
1193
1194 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1195 --stack->ga_len;
1196
1197 return OK;
1198}
1199
1200/*
1201 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1202 */
1203 int
1204generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1205{
1206 isn_T *isn;
1207
1208 RETURN_OK_IF_SKIP(cctx);
1209 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1210 return FAIL;
1211 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1212 // jump_where is set later
1213 return OK;
1214}
1215
1216 int
1217generate_FOR(cctx_T *cctx, int loop_idx)
1218{
1219 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001220
1221 RETURN_OK_IF_SKIP(cctx);
1222 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1223 return FAIL;
1224 isn->isn_arg.forloop.for_idx = loop_idx;
1225
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001226 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001227 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001228}
1229/*
1230 * Generate an ISN_TRYCONT instruction.
1231 */
1232 int
1233generate_TRYCONT(cctx_T *cctx, int levels, int where)
1234{
1235 isn_T *isn;
1236
1237 RETURN_OK_IF_SKIP(cctx);
1238 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1239 return FAIL;
1240 isn->isn_arg.trycont.tct_levels = levels;
1241 isn->isn_arg.trycont.tct_where = where;
1242
1243 return OK;
1244}
1245
1246
1247/*
1248 * Generate an ISN_BCALL instruction.
1249 * "method_call" is TRUE for "value->method()"
1250 * Return FAIL if the number of arguments is wrong.
1251 */
1252 int
1253generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1254{
1255 isn_T *isn;
1256 garray_T *stack = &cctx->ctx_type_stack;
1257 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001258 type2_T *typep;
1259 type2_T *argtypes = NULL;
1260 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1261 type2_T *maptype = NULL;
1262 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001263
1264 RETURN_OK_IF_SKIP(cctx);
1265 argoff = check_internal_func(func_idx, argcount);
1266 if (argoff < 0)
1267 return FAIL;
1268
1269 if (method_call && argoff > 1)
1270 {
1271 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1272 return FAIL;
1273 isn->isn_arg.shuffle.shfl_item = argcount;
1274 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1275 }
1276
1277 if (argcount > 0)
1278 {
1279 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001280 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001281 if (method_call && argoff > 1)
1282 {
1283 int i;
1284
1285 for (i = 0; i < argcount; ++i)
1286 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001287 ? typep[i + 1]
1288 : (i == argoff - 1) ? typep[0] : typep[i];
1289 argtypes = shuffled_argtypes;
1290 }
1291 else
1292 {
1293 int i;
1294
1295 for (i = 0; i < argcount; ++i)
1296 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001297 argtypes = shuffled_argtypes;
1298 }
1299 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1300 cctx) == FAIL)
1301 return FAIL;
1302 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001303 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304 }
1305
1306 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1307 return FAIL;
1308 isn->isn_arg.bfunc.cbf_idx = func_idx;
1309 isn->isn_arg.bfunc.cbf_argcount = argcount;
1310
1311 // Drop the argument types and push the return type.
1312 stack->ga_len -= argcount;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001313 type = internal_func_ret_type(func_idx, argcount, argtypes);
1314 if (push_type_stack(cctx, type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001315 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001316
Bram Moolenaar078a4612022-01-04 15:17:03 +00001317 if (maptype != NULL && maptype[0].type_curr->tt_member != NULL
1318 && maptype[0].type_curr->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001319 // Check that map() didn't change the item types.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001320 generate_TYPECHECK(cctx, maptype[0].type_curr, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001321
1322 return OK;
1323}
1324
1325/*
1326 * Generate an ISN_LISTAPPEND instruction. Works like add().
1327 * Argument count is already checked.
1328 */
1329 int
1330generate_LISTAPPEND(cctx_T *cctx)
1331{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332 type_T *list_type;
1333 type_T *item_type;
1334 type_T *expected;
1335
1336 // Caller already checked that list_type is a list.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001337 list_type = get_type_on_stack(cctx, 1);
1338 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001339 expected = list_type->tt_member;
1340 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1341 return FAIL;
1342
1343 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1344 return FAIL;
1345
Bram Moolenaar078a4612022-01-04 15:17:03 +00001346 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001347 return OK;
1348}
1349
1350/*
1351 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1352 * Argument count is already checked.
1353 */
1354 int
1355generate_BLOBAPPEND(cctx_T *cctx)
1356{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001357 type_T *item_type;
1358
1359 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001360 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001361 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1362 return FAIL;
1363
1364 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1365 return FAIL;
1366
Bram Moolenaar078a4612022-01-04 15:17:03 +00001367 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001368 return OK;
1369}
1370
1371/*
1372 * Generate an ISN_DCALL or ISN_UCALL instruction.
1373 * Return FAIL if the number of arguments is wrong.
1374 */
1375 int
1376generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1377{
1378 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001379 int regular_args = ufunc->uf_args.ga_len;
1380 int argcount = pushed_argcount;
1381
1382 RETURN_OK_IF_SKIP(cctx);
1383 if (argcount > regular_args && !has_varargs(ufunc))
1384 {
1385 semsg(_(e_too_many_arguments_for_function_str),
1386 printable_func_name(ufunc));
1387 return FAIL;
1388 }
1389 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1390 {
1391 semsg(_(e_not_enough_arguments_for_function_str),
1392 printable_func_name(ufunc));
1393 return FAIL;
1394 }
1395
1396 if (ufunc->uf_def_status != UF_NOT_COMPILED
1397 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1398 {
1399 int i;
1400
1401 for (i = 0; i < argcount; ++i)
1402 {
1403 type_T *expected;
1404 type_T *actual;
1405
Bram Moolenaar078a4612022-01-04 15:17:03 +00001406 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001407 if (actual == &t_special
1408 && i >= regular_args - ufunc->uf_def_args.ga_len)
1409 {
1410 // assume v:none used for default argument value
1411 continue;
1412 }
1413 if (i < regular_args)
1414 {
1415 if (ufunc->uf_arg_types == NULL)
1416 continue;
1417 expected = ufunc->uf_arg_types[i];
1418 }
1419 else if (ufunc->uf_va_type == NULL
1420 || ufunc->uf_va_type == &t_list_any)
1421 // possibly a lambda or "...: any"
1422 expected = &t_any;
1423 else
1424 expected = ufunc->uf_va_type->tt_member;
1425 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1426 TRUE, FALSE) == FAIL)
1427 {
1428 arg_type_mismatch(expected, actual, i + 1);
1429 return FAIL;
1430 }
1431 }
1432 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
1433 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1434 COMPILE_TYPE(ufunc), NULL) == FAIL)
1435 return FAIL;
1436 }
1437 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1438 {
1439 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1440 ufunc->uf_name);
1441 return FAIL;
1442 }
1443
1444 if ((isn = generate_instr(cctx,
1445 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1446 : ISN_UCALL)) == NULL)
1447 return FAIL;
1448 if (isn->isn_type == ISN_DCALL)
1449 {
1450 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1451 isn->isn_arg.dfunc.cdf_argcount = argcount;
1452 }
1453 else
1454 {
1455 // A user function may be deleted and redefined later, can't use the
1456 // ufunc pointer, need to look it up again at runtime.
1457 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1458 isn->isn_arg.ufunc.cuf_argcount = argcount;
1459 }
1460
Bram Moolenaar078a4612022-01-04 15:17:03 +00001461 // drop the argument types
1462 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001463
Bram Moolenaar078a4612022-01-04 15:17:03 +00001464 // add return type
1465 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001466}
1467
1468/*
1469 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1470 */
1471 int
1472generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1473{
1474 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001475
1476 RETURN_OK_IF_SKIP(cctx);
1477 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1478 return FAIL;
1479 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1480 isn->isn_arg.ufunc.cuf_argcount = argcount;
1481
Bram Moolenaar078a4612022-01-04 15:17:03 +00001482 // drop the argument types
1483 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001484
Bram Moolenaar078a4612022-01-04 15:17:03 +00001485 // add return value
1486 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487}
1488
1489/*
1490 * Generate an ISN_PCALL instruction.
1491 * "type" is the type of the FuncRef.
1492 */
1493 int
1494generate_PCALL(
1495 cctx_T *cctx,
1496 int argcount,
1497 char_u *name,
1498 type_T *type,
1499 int at_top)
1500{
1501 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001502 type_T *ret_type;
1503
1504 RETURN_OK_IF_SKIP(cctx);
1505
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001506 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001507 ret_type = &t_any;
1508 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1509 {
1510 if (type->tt_argcount != -1)
1511 {
1512 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1513
1514 if (argcount < type->tt_min_argcount - varargs)
1515 {
1516 semsg(_(e_not_enough_arguments_for_function_str), name);
1517 return FAIL;
1518 }
1519 if (!varargs && argcount > type->tt_argcount)
1520 {
1521 semsg(_(e_too_many_arguments_for_function_str), name);
1522 return FAIL;
1523 }
1524 if (type->tt_args != NULL)
1525 {
1526 int i;
1527
1528 for (i = 0; i < argcount; ++i)
1529 {
1530 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001531 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001532 type_T *expected;
1533
1534 if (varargs && i >= type->tt_argcount - 1)
1535 expected = type->tt_args[
1536 type->tt_argcount - 1]->tt_member;
1537 else if (i >= type->tt_min_argcount
1538 && actual == &t_special)
1539 expected = &t_any;
1540 else
1541 expected = type->tt_args[i];
1542 if (need_type(actual, expected, offset, i + 1,
1543 cctx, TRUE, FALSE) == FAIL)
1544 {
1545 arg_type_mismatch(expected, actual, i + 1);
1546 return FAIL;
1547 }
1548 }
1549 }
1550 }
1551 ret_type = type->tt_member;
1552 if (ret_type == &t_unknown)
1553 // return type not known yet, use a runtime check
1554 ret_type = &t_any;
1555 }
1556 else
1557 {
1558 semsg(_(e_not_callable_type_str), name);
1559 return FAIL;
1560 }
1561
1562 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1563 return FAIL;
1564 isn->isn_arg.pfunc.cpf_top = at_top;
1565 isn->isn_arg.pfunc.cpf_argcount = argcount;
1566
Bram Moolenaar078a4612022-01-04 15:17:03 +00001567 // drop the arguments and the funcref/partial
1568 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569
Bram Moolenaar078a4612022-01-04 15:17:03 +00001570 // push the return value
1571 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001572
1573 // If partial is above the arguments it must be cleared and replaced with
1574 // the return value.
1575 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1576 return FAIL;
1577
1578 return OK;
1579}
1580
1581/*
1582 * Generate an ISN_STRINGMEMBER instruction.
1583 */
1584 int
1585generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1586{
1587 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001588 type_T *type;
1589
1590 RETURN_OK_IF_SKIP(cctx);
1591 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1592 return FAIL;
1593 isn->isn_arg.string = vim_strnsave(name, len);
1594
1595 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001596 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001597 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001598 {
1599 char *tofree;
1600
1601 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1602 name, type_name(type, &tofree));
1603 vim_free(tofree);
1604 return FAIL;
1605 }
1606 // change dict type to dict member type
1607 if (type->tt_type == VAR_DICT)
1608 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001609 type_T *ntype = type->tt_member == &t_unknown
1610 ? &t_any : type->tt_member;
1611 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001612 }
1613
1614 return OK;
1615}
1616
1617/*
1618 * Generate an ISN_ECHO instruction.
1619 */
1620 int
1621generate_ECHO(cctx_T *cctx, int with_white, int count)
1622{
1623 isn_T *isn;
1624
1625 RETURN_OK_IF_SKIP(cctx);
1626 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1627 return FAIL;
1628 isn->isn_arg.echo.echo_with_white = with_white;
1629 isn->isn_arg.echo.echo_count = count;
1630
1631 return OK;
1632}
1633
1634/*
1635 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1636 */
1637 int
1638generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1639{
1640 isn_T *isn;
1641
1642 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1643 return FAIL;
1644 isn->isn_arg.number = count;
1645
1646 return OK;
1647}
1648
1649/*
1650 * Generate an ISN_PUT instruction.
1651 */
1652 int
1653generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1654{
1655 isn_T *isn;
1656
1657 RETURN_OK_IF_SKIP(cctx);
1658 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1659 return FAIL;
1660 isn->isn_arg.put.put_regname = regname;
1661 isn->isn_arg.put.put_lnum = lnum;
1662 return OK;
1663}
1664
1665/*
1666 * Generate an EXEC instruction that takes a string argument.
1667 * A copy is made of "line".
1668 */
1669 int
1670generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1671{
1672 isn_T *isn;
1673
1674 RETURN_OK_IF_SKIP(cctx);
1675 if ((isn = generate_instr(cctx, isntype)) == NULL)
1676 return FAIL;
1677 isn->isn_arg.string = vim_strsave(line);
1678 return OK;
1679}
1680
1681/*
1682 * Generate an EXEC instruction that takes a string argument.
1683 * "str" must be allocated, it is consumed.
1684 */
1685 int
1686generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1687{
1688 isn_T *isn;
1689
1690 if (cctx->ctx_skip == SKIP_YES)
1691 {
1692 vim_free(str);
1693 return OK;
1694 }
1695 if ((isn = generate_instr(cctx, isntype)) == NULL)
1696 {
1697 vim_free(str);
1698 return FAIL;
1699 }
1700 isn->isn_arg.string = str;
1701 return OK;
1702}
1703
1704 int
1705generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1706{
1707 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001708
1709 RETURN_OK_IF_SKIP(cctx);
1710 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1711 return FAIL;
1712 isn->isn_arg.string = vim_strsave(line);
1713
Bram Moolenaar078a4612022-01-04 15:17:03 +00001714 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001715}
1716
1717 int
1718generate_EXECCONCAT(cctx_T *cctx, int count)
1719{
1720 isn_T *isn;
1721
1722 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1723 return FAIL;
1724 isn->isn_arg.number = count;
1725 return OK;
1726}
1727
1728/*
1729 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1730 */
1731 int
1732generate_RANGE(cctx_T *cctx, char_u *range)
1733{
1734 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001735
1736 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1737 return FAIL;
1738 isn->isn_arg.string = range;
1739
Bram Moolenaar078a4612022-01-04 15:17:03 +00001740 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001741}
1742
1743 int
1744generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1745{
1746 isn_T *isn;
1747
1748 RETURN_OK_IF_SKIP(cctx);
1749 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1750 return FAIL;
1751 isn->isn_arg.unpack.unp_count = var_count;
1752 isn->isn_arg.unpack.unp_semicolon = semicolon;
1753 return OK;
1754}
1755
1756/*
1757 * Generate an instruction for any command modifiers.
1758 */
1759 int
1760generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1761{
1762 isn_T *isn;
1763
1764 if (has_cmdmod(cmod, FALSE))
1765 {
1766 cctx->ctx_has_cmdmod = TRUE;
1767
1768 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1769 return FAIL;
1770 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1771 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1772 return FAIL;
1773 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1774 // filter program now belongs to the instruction
1775 cmod->cmod_filter_regmatch.regprog = NULL;
1776 }
1777
1778 return OK;
1779}
1780
1781 int
1782generate_undo_cmdmods(cctx_T *cctx)
1783{
1784 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1785 return FAIL;
1786 cctx->ctx_has_cmdmod = FALSE;
1787 return OK;
1788}
1789
1790/*
1791 * Generate a STORE instruction for "dest", not being "dest_local".
1792 * Return FAIL when out of memory.
1793 */
1794 int
1795generate_store_var(
1796 cctx_T *cctx,
1797 assign_dest_T dest,
1798 int opt_flags,
1799 int vimvaridx,
1800 int scriptvar_idx,
1801 int scriptvar_sid,
1802 type_T *type,
1803 char_u *name)
1804{
1805 switch (dest)
1806 {
1807 case dest_option:
1808 return generate_STOREOPT(cctx, ISN_STOREOPT,
1809 skip_option_env_lead(name), opt_flags);
1810 case dest_func_option:
1811 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1812 skip_option_env_lead(name), opt_flags);
1813 case dest_global:
1814 // include g: with the name, easier to execute that way
1815 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1816 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1817 case dest_buffer:
1818 // include b: with the name, easier to execute that way
1819 return generate_STORE(cctx, ISN_STOREB, 0, name);
1820 case dest_window:
1821 // include w: with the name, easier to execute that way
1822 return generate_STORE(cctx, ISN_STOREW, 0, name);
1823 case dest_tab:
1824 // include t: with the name, easier to execute that way
1825 return generate_STORE(cctx, ISN_STORET, 0, name);
1826 case dest_env:
1827 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1828 case dest_reg:
1829 return generate_STORE(cctx, ISN_STOREREG,
1830 name[1] == '@' ? '"' : name[1], NULL);
1831 case dest_vimvar:
1832 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1833 case dest_script:
1834 if (scriptvar_idx < 0)
1835 // "s:" may be included in the name.
1836 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1837 scriptvar_sid, type);
1838 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1839 scriptvar_sid, scriptvar_idx, type);
1840 case dest_local:
1841 case dest_expr:
1842 // cannot happen
1843 break;
1844 }
1845 return FAIL;
1846}
1847
1848 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001849generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001850{
1851 if (lhs->lhs_dest != dest_local)
1852 return generate_store_var(cctx, lhs->lhs_dest,
1853 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1854 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1855 lhs->lhs_type, lhs->lhs_name);
1856
1857 if (lhs->lhs_lvar != NULL)
1858 {
1859 garray_T *instr = &cctx->ctx_instr;
1860 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1861
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001862 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1863 // ISN_STORENR.
1864 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001865 if (lhs->lhs_lvar->lv_from_outer == 0
1866 && instr->ga_len == instr_count + 1
1867 && isn->isn_type == ISN_PUSHNR)
1868 {
1869 varnumber_T val = isn->isn_arg.number;
1870 garray_T *stack = &cctx->ctx_type_stack;
1871
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001872 if (val == 0 && is_decl)
1873 {
1874 --instr->ga_len;
1875 }
1876 else
1877 {
1878 isn->isn_type = ISN_STORENR;
1879 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1880 isn->isn_arg.storenr.stnr_val = val;
1881 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001882 if (stack->ga_len > 0)
1883 --stack->ga_len;
1884 }
1885 else if (lhs->lhs_lvar->lv_from_outer > 0)
1886 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1887 lhs->lhs_lvar->lv_from_outer);
1888 else
1889 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
1890 }
1891 return OK;
1892}
1893
1894#if defined(FEAT_PROFILE) || defined(PROTO)
1895 void
1896may_generate_prof_end(cctx_T *cctx, int prof_lnum)
1897{
1898 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
1899 generate_instr(cctx, ISN_PROF_END);
1900}
1901#endif
1902
1903
1904/*
1905 * Delete an instruction, free what it contains.
1906 */
1907 void
1908delete_instr(isn_T *isn)
1909{
1910 switch (isn->isn_type)
1911 {
1912 case ISN_DEF:
1913 case ISN_EXEC:
1914 case ISN_EXECRANGE:
1915 case ISN_EXEC_SPLIT:
1916 case ISN_LEGACY_EVAL:
1917 case ISN_LOADAUTO:
1918 case ISN_LOADB:
1919 case ISN_LOADENV:
1920 case ISN_LOADG:
1921 case ISN_LOADOPT:
1922 case ISN_LOADT:
1923 case ISN_LOADW:
1924 case ISN_LOCKUNLOCK:
1925 case ISN_PUSHEXC:
1926 case ISN_PUSHFUNC:
1927 case ISN_PUSHS:
1928 case ISN_RANGE:
1929 case ISN_STOREAUTO:
1930 case ISN_STOREB:
1931 case ISN_STOREENV:
1932 case ISN_STOREG:
1933 case ISN_STORET:
1934 case ISN_STOREW:
1935 case ISN_STRINGMEMBER:
1936 vim_free(isn->isn_arg.string);
1937 break;
1938
1939 case ISN_SUBSTITUTE:
1940 {
1941 int idx;
1942 isn_T *list = isn->isn_arg.subs.subs_instr;
1943
1944 vim_free(isn->isn_arg.subs.subs_cmd);
1945 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
1946 delete_instr(list + idx);
1947 vim_free(list);
1948 }
1949 break;
1950
1951 case ISN_INSTR:
1952 {
1953 int idx;
1954 isn_T *list = isn->isn_arg.instr;
1955
1956 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
1957 delete_instr(list + idx);
1958 vim_free(list);
1959 }
1960 break;
1961
1962 case ISN_LOADS:
1963 case ISN_STORES:
1964 vim_free(isn->isn_arg.loadstore.ls_name);
1965 break;
1966
1967 case ISN_UNLET:
1968 case ISN_UNLETENV:
1969 vim_free(isn->isn_arg.unlet.ul_name);
1970 break;
1971
1972 case ISN_STOREOPT:
1973 case ISN_STOREFUNCOPT:
1974 vim_free(isn->isn_arg.storeopt.so_name);
1975 break;
1976
1977 case ISN_PUSHBLOB: // push blob isn_arg.blob
1978 blob_unref(isn->isn_arg.blob);
1979 break;
1980
1981 case ISN_PUSHJOB:
1982#ifdef FEAT_JOB_CHANNEL
1983 job_unref(isn->isn_arg.job);
1984#endif
1985 break;
1986
1987 case ISN_PUSHCHANNEL:
1988#ifdef FEAT_JOB_CHANNEL
1989 channel_unref(isn->isn_arg.channel);
1990#endif
1991 break;
1992
1993 case ISN_UCALL:
1994 vim_free(isn->isn_arg.ufunc.cuf_name);
1995 break;
1996
1997 case ISN_FUNCREF:
1998 {
1999 if (isn->isn_arg.funcref.fr_func_name == NULL)
2000 {
2001 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2002 + isn->isn_arg.funcref.fr_dfunc_idx;
2003 ufunc_T *ufunc = dfunc->df_ufunc;
2004
2005 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2006 func_ptr_unref(ufunc);
2007 }
2008 else
2009 {
2010 char_u *name = isn->isn_arg.funcref.fr_func_name;
2011
2012 if (name != NULL)
2013 func_unref(name);
2014 vim_free(isn->isn_arg.funcref.fr_func_name);
2015 }
2016 }
2017 break;
2018
2019 case ISN_DCALL:
2020 {
2021 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2022 + isn->isn_arg.dfunc.cdf_idx;
2023
2024 if (dfunc->df_ufunc != NULL
2025 && func_name_refcount(dfunc->df_ufunc->uf_name))
2026 func_ptr_unref(dfunc->df_ufunc);
2027 }
2028 break;
2029
2030 case ISN_NEWFUNC:
2031 {
2032 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
2033 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
2034
2035 if (ufunc != NULL)
2036 {
2037 unlink_def_function(ufunc);
2038 func_ptr_unref(ufunc);
2039 }
2040
2041 vim_free(lambda);
2042 vim_free(isn->isn_arg.newfunc.nf_global);
2043 }
2044 break;
2045
2046 case ISN_CHECKTYPE:
2047 case ISN_SETTYPE:
2048 free_type(isn->isn_arg.type.ct_type);
2049 break;
2050
2051 case ISN_CMDMOD:
2052 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2053 ->cmod_filter_regmatch.regprog);
2054 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2055 break;
2056
2057 case ISN_LOADSCRIPT:
2058 case ISN_STORESCRIPT:
2059 vim_free(isn->isn_arg.script.scriptref);
2060 break;
2061
2062 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002063 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002064 break;
2065
2066 case ISN_CEXPR_CORE:
2067 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2068 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2069 break;
2070
2071 case ISN_2BOOL:
2072 case ISN_2STRING:
2073 case ISN_2STRING_ANY:
2074 case ISN_ADDBLOB:
2075 case ISN_ADDLIST:
2076 case ISN_ANYINDEX:
2077 case ISN_ANYSLICE:
2078 case ISN_BCALL:
2079 case ISN_BLOBAPPEND:
2080 case ISN_BLOBINDEX:
2081 case ISN_BLOBSLICE:
2082 case ISN_CATCH:
2083 case ISN_CEXPR_AUCMD:
2084 case ISN_CHECKLEN:
2085 case ISN_CHECKNR:
2086 case ISN_CLEARDICT:
2087 case ISN_CMDMOD_REV:
2088 case ISN_COMPAREANY:
2089 case ISN_COMPAREBLOB:
2090 case ISN_COMPAREBOOL:
2091 case ISN_COMPAREDICT:
2092 case ISN_COMPAREFLOAT:
2093 case ISN_COMPAREFUNC:
2094 case ISN_COMPARELIST:
2095 case ISN_COMPARENR:
2096 case ISN_COMPARESPECIAL:
2097 case ISN_COMPARESTRING:
2098 case ISN_CONCAT:
2099 case ISN_COND2BOOL:
2100 case ISN_DEBUG:
2101 case ISN_DROP:
2102 case ISN_ECHO:
2103 case ISN_ECHOCONSOLE:
2104 case ISN_ECHOERR:
2105 case ISN_ECHOMSG:
2106 case ISN_ENDTRY:
2107 case ISN_EXECCONCAT:
2108 case ISN_EXECUTE:
2109 case ISN_FINALLY:
2110 case ISN_FINISH:
2111 case ISN_FOR:
2112 case ISN_GETITEM:
2113 case ISN_JUMP:
2114 case ISN_JUMP_IF_ARG_SET:
2115 case ISN_LISTAPPEND:
2116 case ISN_LISTINDEX:
2117 case ISN_LISTSLICE:
2118 case ISN_LOAD:
2119 case ISN_LOADBDICT:
2120 case ISN_LOADGDICT:
2121 case ISN_LOADOUTER:
2122 case ISN_LOADREG:
2123 case ISN_LOADTDICT:
2124 case ISN_LOADV:
2125 case ISN_LOADWDICT:
2126 case ISN_LOCKCONST:
2127 case ISN_MEMBER:
2128 case ISN_NEGATENR:
2129 case ISN_NEWDICT:
2130 case ISN_NEWLIST:
2131 case ISN_OPANY:
2132 case ISN_OPFLOAT:
2133 case ISN_OPNR:
2134 case ISN_PCALL:
2135 case ISN_PCALL_END:
2136 case ISN_PROF_END:
2137 case ISN_PROF_START:
2138 case ISN_PUSHBOOL:
2139 case ISN_PUSHF:
2140 case ISN_PUSHNR:
2141 case ISN_PUSHSPEC:
2142 case ISN_PUT:
2143 case ISN_REDIREND:
2144 case ISN_REDIRSTART:
2145 case ISN_RETURN:
2146 case ISN_RETURN_VOID:
2147 case ISN_SHUFFLE:
2148 case ISN_SLICE:
2149 case ISN_STORE:
2150 case ISN_STOREINDEX:
2151 case ISN_STORENR:
2152 case ISN_STOREOUTER:
2153 case ISN_STORERANGE:
2154 case ISN_STOREREG:
2155 case ISN_STOREV:
2156 case ISN_STRINDEX:
2157 case ISN_STRSLICE:
2158 case ISN_THROW:
2159 case ISN_TRYCONT:
2160 case ISN_UNLETINDEX:
2161 case ISN_UNLETRANGE:
2162 case ISN_UNPACK:
2163 case ISN_USEDICT:
2164 // nothing allocated
2165 break;
2166 }
2167}
2168
2169 void
2170clear_instr_ga(garray_T *gap)
2171{
2172 int idx;
2173
2174 for (idx = 0; idx < gap->ga_len; ++idx)
2175 delete_instr(((isn_T *)gap->ga_data) + idx);
2176 ga_clear(gap);
2177}
2178
2179
2180#endif // defined(FEAT_EVAL)