blob: 60963a3c9b2baa6222e3656546f2c6fb77f27aaa [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/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000747 * Generate an ISN_AUTOLOAD instruction.
748 */
749 int
750generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
751{
752 isn_T *isn;
753
754 RETURN_OK_IF_SKIP(cctx);
755 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
756 return FAIL;
757 isn->isn_arg.string = vim_strsave(name);
758 if (isn->isn_arg.string == NULL)
759 return FAIL;
760 return OK;
761}
762
763/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000764 * Generate an ISN_GETITEM instruction with "index".
765 * "with_op" is TRUE for "+=" and other operators, the stack has the current
766 * value below the list with values.
767 */
768 int
769generate_GETITEM(cctx_T *cctx, int index, int with_op)
770{
771 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000772 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000773 type_T *item_type = &t_any;
774
775 RETURN_OK_IF_SKIP(cctx);
776
777 if (type->tt_type != VAR_LIST)
778 {
779 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000780 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000781 return FAIL;
782 }
783 item_type = type->tt_member;
784 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
785 return FAIL;
786 isn->isn_arg.getitem.gi_index = index;
787 isn->isn_arg.getitem.gi_with_op = with_op;
788
789 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000790 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791}
792
793/*
794 * Generate an ISN_SLICE instruction with "count".
795 */
796 int
797generate_SLICE(cctx_T *cctx, int count)
798{
799 isn_T *isn;
800
801 RETURN_OK_IF_SKIP(cctx);
802 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
803 return FAIL;
804 isn->isn_arg.number = count;
805 return OK;
806}
807
808/*
809 * Generate an ISN_CHECKLEN instruction with "min_len".
810 */
811 int
812generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
813{
814 isn_T *isn;
815
816 RETURN_OK_IF_SKIP(cctx);
817
818 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
819 return FAIL;
820 isn->isn_arg.checklen.cl_min_len = min_len;
821 isn->isn_arg.checklen.cl_more_OK = more_OK;
822
823 return OK;
824}
825
826/*
827 * Generate an ISN_STORE instruction.
828 */
829 int
830generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
831{
832 isn_T *isn;
833
834 RETURN_OK_IF_SKIP(cctx);
835 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
836 return FAIL;
837 if (name != NULL)
838 isn->isn_arg.string = vim_strsave(name);
839 else
840 isn->isn_arg.number = idx;
841
842 return OK;
843}
844
845/*
846 * Generate an ISN_STOREOUTER instruction.
847 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000848 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000849generate_STOREOUTER(cctx_T *cctx, int idx, int level)
850{
851 isn_T *isn;
852
853 RETURN_OK_IF_SKIP(cctx);
854 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
855 return FAIL;
856 isn->isn_arg.outer.outer_idx = idx;
857 isn->isn_arg.outer.outer_depth = level;
858
859 return OK;
860}
861
862/*
863 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
864 */
865 int
866generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
867{
868 isn_T *isn;
869
870 RETURN_OK_IF_SKIP(cctx);
871 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
872 return FAIL;
873 isn->isn_arg.storenr.stnr_idx = idx;
874 isn->isn_arg.storenr.stnr_val = value;
875
876 return OK;
877}
878
879/*
880 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
881 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000882 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883generate_STOREOPT(
884 cctx_T *cctx,
885 isntype_T isn_type,
886 char_u *name,
887 int opt_flags)
888{
889 isn_T *isn;
890
891 RETURN_OK_IF_SKIP(cctx);
892 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
893 return FAIL;
894 isn->isn_arg.storeopt.so_name = vim_strsave(name);
895 isn->isn_arg.storeopt.so_flags = opt_flags;
896
897 return OK;
898}
899
900/*
901 * Generate an ISN_LOAD or similar instruction.
902 */
903 int
904generate_LOAD(
905 cctx_T *cctx,
906 isntype_T isn_type,
907 int idx,
908 char_u *name,
909 type_T *type)
910{
911 isn_T *isn;
912
913 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000914 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000915 return FAIL;
916 if (name != NULL)
917 isn->isn_arg.string = vim_strsave(name);
918 else
919 isn->isn_arg.number = idx;
920
921 return OK;
922}
923
924/*
925 * Generate an ISN_LOADOUTER instruction
926 */
927 int
928generate_LOADOUTER(
929 cctx_T *cctx,
930 int idx,
931 int nesting,
932 type_T *type)
933{
934 isn_T *isn;
935
936 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000937 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938 return FAIL;
939 isn->isn_arg.outer.outer_idx = idx;
940 isn->isn_arg.outer.outer_depth = nesting;
941
942 return OK;
943}
944
945/*
946 * Generate an ISN_LOADV instruction for v:var.
947 */
948 int
949generate_LOADV(
950 cctx_T *cctx,
951 char_u *name,
952 int error)
953{
954 int di_flags;
955 int vidx = find_vim_var(name, &di_flags);
956 type_T *type;
957
958 RETURN_OK_IF_SKIP(cctx);
959 if (vidx < 0)
960 {
961 if (error)
962 semsg(_(e_variable_not_found_str), name);
963 return FAIL;
964 }
Bram Moolenaard787e402021-12-24 21:36:12 +0000965 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000966 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
967}
968
969/*
970 * Generate an ISN_UNLET instruction.
971 */
972 int
973generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
974{
975 isn_T *isn;
976
977 RETURN_OK_IF_SKIP(cctx);
978 if ((isn = generate_instr(cctx, isn_type)) == NULL)
979 return FAIL;
980 isn->isn_arg.unlet.ul_name = vim_strsave(name);
981 isn->isn_arg.unlet.ul_forceit = forceit;
982
983 return OK;
984}
985
986/*
987 * Generate an ISN_LOCKCONST instruction.
988 */
989 int
990generate_LOCKCONST(cctx_T *cctx)
991{
992 isn_T *isn;
993
994 RETURN_OK_IF_SKIP(cctx);
995 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
996 return FAIL;
997 return OK;
998}
999
1000/*
1001 * Generate an ISN_LOADS instruction.
1002 */
1003 int
1004generate_OLDSCRIPT(
1005 cctx_T *cctx,
1006 isntype_T isn_type,
1007 char_u *name,
1008 int sid,
1009 type_T *type)
1010{
1011 isn_T *isn;
1012
1013 RETURN_OK_IF_SKIP(cctx);
1014 if (isn_type == ISN_LOADS)
1015 isn = generate_instr_type(cctx, isn_type, type);
1016 else
1017 isn = generate_instr_drop(cctx, isn_type, 1);
1018 if (isn == NULL)
1019 return FAIL;
1020 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1021 isn->isn_arg.loadstore.ls_sid = sid;
1022
1023 return OK;
1024}
1025
1026/*
1027 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1028 */
1029 int
1030generate_VIM9SCRIPT(
1031 cctx_T *cctx,
1032 isntype_T isn_type,
1033 int sid,
1034 int idx,
1035 type_T *type)
1036{
1037 isn_T *isn;
1038 scriptref_T *sref;
1039 scriptitem_T *si = SCRIPT_ITEM(sid);
1040
1041 RETURN_OK_IF_SKIP(cctx);
1042 if (isn_type == ISN_LOADSCRIPT)
1043 isn = generate_instr_type(cctx, isn_type, type);
1044 else
1045 isn = generate_instr_drop(cctx, isn_type, 1);
1046 if (isn == NULL)
1047 return FAIL;
1048
1049 // This requires three arguments, which doesn't fit in an instruction, thus
1050 // we need to allocate a struct for this.
1051 sref = ALLOC_ONE(scriptref_T);
1052 if (sref == NULL)
1053 return FAIL;
1054 isn->isn_arg.script.scriptref = sref;
1055 sref->sref_sid = sid;
1056 sref->sref_idx = idx;
1057 sref->sref_seq = si->sn_script_seq;
1058 sref->sref_type = type;
1059 return OK;
1060}
1061
1062/*
1063 * Generate an ISN_NEWLIST instruction.
1064 */
1065 int
1066generate_NEWLIST(cctx_T *cctx, int count)
1067{
1068 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001069 type_T *member_type;
1070 type_T *decl_member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001072 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001073
1074 RETURN_OK_IF_SKIP(cctx);
1075 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1076 return FAIL;
1077 isn->isn_arg.number = count;
1078
Bram Moolenaar078a4612022-01-04 15:17:03 +00001079 // Get the member type and the declared member type from all the items on
1080 // the stack.
Bram Moolenaar81330182022-02-01 12:11:58 +00001081 member_type = get_member_type_from_stack(count, 1,
1082 &decl_member_type, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001083 type = get_list_type(member_type, cctx->ctx_type_list);
1084 decl_type = get_list_type(decl_member_type, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001085
1086 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001087 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001088
1089 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001090 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001091}
1092
1093/*
1094 * Generate an ISN_NEWDICT instruction.
1095 */
1096 int
1097generate_NEWDICT(cctx_T *cctx, int count)
1098{
1099 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001100 type_T *member_type;
1101 type_T *decl_member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001102 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001103 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001104
1105 RETURN_OK_IF_SKIP(cctx);
1106 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1107 return FAIL;
1108 isn->isn_arg.number = count;
1109
Bram Moolenaar078a4612022-01-04 15:17:03 +00001110 member_type = get_member_type_from_stack(count, 2,
1111 &decl_member_type, cctx);
1112 type = get_dict_type(member_type, cctx->ctx_type_list);
1113 decl_type = get_dict_type(decl_member_type, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001114
1115 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001116 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001117
1118 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001119 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001120}
1121
1122/*
1123 * Generate an ISN_FUNCREF instruction.
1124 */
1125 int
1126generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1127{
1128 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001129 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001130
1131 RETURN_OK_IF_SKIP(cctx);
1132 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1133 return FAIL;
1134 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1135 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1136 else
1137 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1138 cctx->ctx_has_closure = 1;
1139
1140 // If the referenced function is a closure, it may use items further up in
1141 // the nested context, including this one.
1142 if (ufunc->uf_flags & FC_CLOSURE)
1143 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1144
Bram Moolenaar078a4612022-01-04 15:17:03 +00001145 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1146 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001147}
1148
1149/*
1150 * Generate an ISN_NEWFUNC instruction.
1151 * "lambda_name" and "func_name" must be in allocated memory and will be
1152 * consumed.
1153 */
1154 int
1155generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1156{
1157 isn_T *isn;
1158
1159 if (cctx->ctx_skip == SKIP_YES)
1160 {
1161 vim_free(lambda_name);
1162 vim_free(func_name);
1163 return OK;
1164 }
1165 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1166 {
1167 vim_free(lambda_name);
1168 vim_free(func_name);
1169 return FAIL;
1170 }
1171 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1172 isn->isn_arg.newfunc.nf_global = func_name;
1173
1174 return OK;
1175}
1176
1177/*
1178 * Generate an ISN_DEF instruction: list functions
1179 */
1180 int
1181generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1182{
1183 isn_T *isn;
1184
1185 RETURN_OK_IF_SKIP(cctx);
1186 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1187 return FAIL;
1188 if (len > 0)
1189 {
1190 isn->isn_arg.string = vim_strnsave(name, len);
1191 if (isn->isn_arg.string == NULL)
1192 return FAIL;
1193 }
1194 return OK;
1195}
1196
1197/*
1198 * Generate an ISN_JUMP instruction.
1199 */
1200 int
1201generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1202{
1203 isn_T *isn;
1204 garray_T *stack = &cctx->ctx_type_stack;
1205
1206 RETURN_OK_IF_SKIP(cctx);
1207 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1208 return FAIL;
1209 isn->isn_arg.jump.jump_when = when;
1210 isn->isn_arg.jump.jump_where = where;
1211
1212 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1213 --stack->ga_len;
1214
1215 return OK;
1216}
1217
1218/*
1219 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1220 */
1221 int
1222generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1223{
1224 isn_T *isn;
1225
1226 RETURN_OK_IF_SKIP(cctx);
1227 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1228 return FAIL;
1229 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1230 // jump_where is set later
1231 return OK;
1232}
1233
1234 int
1235generate_FOR(cctx_T *cctx, int loop_idx)
1236{
1237 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001238
1239 RETURN_OK_IF_SKIP(cctx);
1240 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1241 return FAIL;
1242 isn->isn_arg.forloop.for_idx = loop_idx;
1243
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001244 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001245 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001246}
1247/*
1248 * Generate an ISN_TRYCONT instruction.
1249 */
1250 int
1251generate_TRYCONT(cctx_T *cctx, int levels, int where)
1252{
1253 isn_T *isn;
1254
1255 RETURN_OK_IF_SKIP(cctx);
1256 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1257 return FAIL;
1258 isn->isn_arg.trycont.tct_levels = levels;
1259 isn->isn_arg.trycont.tct_where = where;
1260
1261 return OK;
1262}
1263
1264
1265/*
1266 * Generate an ISN_BCALL instruction.
1267 * "method_call" is TRUE for "value->method()"
1268 * Return FAIL if the number of arguments is wrong.
1269 */
1270 int
1271generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1272{
1273 isn_T *isn;
1274 garray_T *stack = &cctx->ctx_type_stack;
1275 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001276 type2_T *typep;
1277 type2_T *argtypes = NULL;
1278 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1279 type2_T *maptype = NULL;
1280 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001281 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282
1283 RETURN_OK_IF_SKIP(cctx);
1284 argoff = check_internal_func(func_idx, argcount);
1285 if (argoff < 0)
1286 return FAIL;
1287
1288 if (method_call && argoff > 1)
1289 {
1290 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1291 return FAIL;
1292 isn->isn_arg.shuffle.shfl_item = argcount;
1293 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1294 }
1295
1296 if (argcount > 0)
1297 {
1298 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001299 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001300 if (method_call && argoff > 1)
1301 {
1302 int i;
1303
1304 for (i = 0; i < argcount; ++i)
1305 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001306 ? typep[i + 1]
1307 : (i == argoff - 1) ? typep[0] : typep[i];
1308 argtypes = shuffled_argtypes;
1309 }
1310 else
1311 {
1312 int i;
1313
1314 for (i = 0; i < argcount; ++i)
1315 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001316 argtypes = shuffled_argtypes;
1317 }
1318 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1319 cctx) == FAIL)
1320 return FAIL;
1321 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001322 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001323 }
1324
1325 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1326 return FAIL;
1327 isn->isn_arg.bfunc.cbf_idx = func_idx;
1328 isn->isn_arg.bfunc.cbf_argcount = argcount;
1329
1330 // Drop the argument types and push the return type.
1331 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001332 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1333 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001334 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001335
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001336 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1337 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001339 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001340
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{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001351 type_T *list_type;
1352 type_T *item_type;
1353 type_T *expected;
1354
1355 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001356 // For checking the item type we use the declared type of the list and the
1357 // current type of the added item, adding a string to [1, 2] is OK.
1358 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001359 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360 expected = list_type->tt_member;
1361 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1362 return FAIL;
1363
1364 if (generate_instr(cctx, ISN_LISTAPPEND) == 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_BLOBAPPEND instruction. Works like add().
1373 * Argument count is already checked.
1374 */
1375 int
1376generate_BLOBAPPEND(cctx_T *cctx)
1377{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001378 type_T *item_type;
1379
1380 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001381 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382 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
Bram Moolenaar078a4612022-01-04 15:17:03 +00001388 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001389 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;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001400 int regular_args = ufunc->uf_args.ga_len;
1401 int argcount = pushed_argcount;
1402
1403 RETURN_OK_IF_SKIP(cctx);
1404 if (argcount > regular_args && !has_varargs(ufunc))
1405 {
1406 semsg(_(e_too_many_arguments_for_function_str),
1407 printable_func_name(ufunc));
1408 return FAIL;
1409 }
1410 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1411 {
1412 semsg(_(e_not_enough_arguments_for_function_str),
1413 printable_func_name(ufunc));
1414 return FAIL;
1415 }
1416
1417 if (ufunc->uf_def_status != UF_NOT_COMPILED
1418 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1419 {
1420 int i;
1421
1422 for (i = 0; i < argcount; ++i)
1423 {
1424 type_T *expected;
1425 type_T *actual;
1426
Bram Moolenaar078a4612022-01-04 15:17:03 +00001427 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001428 if (actual == &t_special
1429 && i >= regular_args - ufunc->uf_def_args.ga_len)
1430 {
1431 // assume v:none used for default argument value
1432 continue;
1433 }
1434 if (i < regular_args)
1435 {
1436 if (ufunc->uf_arg_types == NULL)
1437 continue;
1438 expected = ufunc->uf_arg_types[i];
1439 }
1440 else if (ufunc->uf_va_type == NULL
1441 || ufunc->uf_va_type == &t_list_any)
1442 // possibly a lambda or "...: any"
1443 expected = &t_any;
1444 else
1445 expected = ufunc->uf_va_type->tt_member;
1446 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1447 TRUE, FALSE) == FAIL)
1448 {
1449 arg_type_mismatch(expected, actual, i + 1);
1450 return FAIL;
1451 }
1452 }
1453 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
1454 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1455 COMPILE_TYPE(ufunc), NULL) == FAIL)
1456 return FAIL;
1457 }
1458 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1459 {
1460 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1461 ufunc->uf_name);
1462 return FAIL;
1463 }
1464
1465 if ((isn = generate_instr(cctx,
1466 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1467 : ISN_UCALL)) == NULL)
1468 return FAIL;
1469 if (isn->isn_type == ISN_DCALL)
1470 {
1471 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1472 isn->isn_arg.dfunc.cdf_argcount = argcount;
1473 }
1474 else
1475 {
1476 // A user function may be deleted and redefined later, can't use the
1477 // ufunc pointer, need to look it up again at runtime.
1478 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1479 isn->isn_arg.ufunc.cuf_argcount = argcount;
1480 }
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 type
1486 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487}
1488
1489/*
1490 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1491 */
1492 int
1493generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1494{
1495 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001496
1497 RETURN_OK_IF_SKIP(cctx);
1498 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1499 return FAIL;
1500 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1501 isn->isn_arg.ufunc.cuf_argcount = argcount;
1502
Bram Moolenaar078a4612022-01-04 15:17:03 +00001503 // drop the argument types
1504 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001505
Bram Moolenaar078a4612022-01-04 15:17:03 +00001506 // add return value
1507 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001508}
1509
1510/*
1511 * Generate an ISN_PCALL instruction.
1512 * "type" is the type of the FuncRef.
1513 */
1514 int
1515generate_PCALL(
1516 cctx_T *cctx,
1517 int argcount,
1518 char_u *name,
1519 type_T *type,
1520 int at_top)
1521{
1522 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001523 type_T *ret_type;
1524
1525 RETURN_OK_IF_SKIP(cctx);
1526
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001527 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001528 ret_type = &t_any;
1529 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1530 {
1531 if (type->tt_argcount != -1)
1532 {
1533 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1534
1535 if (argcount < type->tt_min_argcount - varargs)
1536 {
1537 semsg(_(e_not_enough_arguments_for_function_str), name);
1538 return FAIL;
1539 }
1540 if (!varargs && argcount > type->tt_argcount)
1541 {
1542 semsg(_(e_too_many_arguments_for_function_str), name);
1543 return FAIL;
1544 }
1545 if (type->tt_args != NULL)
1546 {
1547 int i;
1548
1549 for (i = 0; i < argcount; ++i)
1550 {
1551 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001552 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001553 type_T *expected;
1554
1555 if (varargs && i >= type->tt_argcount - 1)
1556 expected = type->tt_args[
1557 type->tt_argcount - 1]->tt_member;
1558 else if (i >= type->tt_min_argcount
1559 && actual == &t_special)
1560 expected = &t_any;
1561 else
1562 expected = type->tt_args[i];
1563 if (need_type(actual, expected, offset, i + 1,
1564 cctx, TRUE, FALSE) == FAIL)
1565 {
1566 arg_type_mismatch(expected, actual, i + 1);
1567 return FAIL;
1568 }
1569 }
1570 }
1571 }
1572 ret_type = type->tt_member;
1573 if (ret_type == &t_unknown)
1574 // return type not known yet, use a runtime check
1575 ret_type = &t_any;
1576 }
1577 else
1578 {
1579 semsg(_(e_not_callable_type_str), name);
1580 return FAIL;
1581 }
1582
1583 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1584 return FAIL;
1585 isn->isn_arg.pfunc.cpf_top = at_top;
1586 isn->isn_arg.pfunc.cpf_argcount = argcount;
1587
Bram Moolenaar078a4612022-01-04 15:17:03 +00001588 // drop the arguments and the funcref/partial
1589 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001590
Bram Moolenaar078a4612022-01-04 15:17:03 +00001591 // push the return value
1592 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001593
1594 // If partial is above the arguments it must be cleared and replaced with
1595 // the return value.
1596 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1597 return FAIL;
1598
1599 return OK;
1600}
1601
1602/*
1603 * Generate an ISN_STRINGMEMBER instruction.
1604 */
1605 int
1606generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1607{
1608 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001609 type_T *type;
1610
1611 RETURN_OK_IF_SKIP(cctx);
1612 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1613 return FAIL;
1614 isn->isn_arg.string = vim_strnsave(name, len);
1615
1616 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001617 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001618 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001619 {
1620 char *tofree;
1621
1622 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1623 name, type_name(type, &tofree));
1624 vim_free(tofree);
1625 return FAIL;
1626 }
1627 // change dict type to dict member type
1628 if (type->tt_type == VAR_DICT)
1629 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001630 type_T *ntype = type->tt_member == &t_unknown
1631 ? &t_any : type->tt_member;
1632 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 }
1634
1635 return OK;
1636}
1637
1638/*
1639 * Generate an ISN_ECHO instruction.
1640 */
1641 int
1642generate_ECHO(cctx_T *cctx, int with_white, int count)
1643{
1644 isn_T *isn;
1645
1646 RETURN_OK_IF_SKIP(cctx);
1647 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1648 return FAIL;
1649 isn->isn_arg.echo.echo_with_white = with_white;
1650 isn->isn_arg.echo.echo_count = count;
1651
1652 return OK;
1653}
1654
1655/*
1656 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1657 */
1658 int
1659generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1660{
1661 isn_T *isn;
1662
1663 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1664 return FAIL;
1665 isn->isn_arg.number = count;
1666
1667 return OK;
1668}
1669
1670/*
1671 * Generate an ISN_PUT instruction.
1672 */
1673 int
1674generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1675{
1676 isn_T *isn;
1677
1678 RETURN_OK_IF_SKIP(cctx);
1679 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1680 return FAIL;
1681 isn->isn_arg.put.put_regname = regname;
1682 isn->isn_arg.put.put_lnum = lnum;
1683 return OK;
1684}
1685
1686/*
1687 * Generate an EXEC instruction that takes a string argument.
1688 * A copy is made of "line".
1689 */
1690 int
1691generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1692{
1693 isn_T *isn;
1694
1695 RETURN_OK_IF_SKIP(cctx);
1696 if ((isn = generate_instr(cctx, isntype)) == NULL)
1697 return FAIL;
1698 isn->isn_arg.string = vim_strsave(line);
1699 return OK;
1700}
1701
1702/*
1703 * Generate an EXEC instruction that takes a string argument.
1704 * "str" must be allocated, it is consumed.
1705 */
1706 int
1707generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1708{
1709 isn_T *isn;
1710
1711 if (cctx->ctx_skip == SKIP_YES)
1712 {
1713 vim_free(str);
1714 return OK;
1715 }
1716 if ((isn = generate_instr(cctx, isntype)) == NULL)
1717 {
1718 vim_free(str);
1719 return FAIL;
1720 }
1721 isn->isn_arg.string = str;
1722 return OK;
1723}
1724
1725 int
1726generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1727{
1728 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001729
1730 RETURN_OK_IF_SKIP(cctx);
1731 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1732 return FAIL;
1733 isn->isn_arg.string = vim_strsave(line);
1734
Bram Moolenaar078a4612022-01-04 15:17:03 +00001735 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001736}
1737
1738 int
1739generate_EXECCONCAT(cctx_T *cctx, int count)
1740{
1741 isn_T *isn;
1742
1743 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1744 return FAIL;
1745 isn->isn_arg.number = count;
1746 return OK;
1747}
1748
1749/*
1750 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1751 */
1752 int
1753generate_RANGE(cctx_T *cctx, char_u *range)
1754{
1755 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001756
1757 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1758 return FAIL;
1759 isn->isn_arg.string = range;
1760
Bram Moolenaar078a4612022-01-04 15:17:03 +00001761 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001762}
1763
1764 int
1765generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1766{
1767 isn_T *isn;
1768
1769 RETURN_OK_IF_SKIP(cctx);
1770 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1771 return FAIL;
1772 isn->isn_arg.unpack.unp_count = var_count;
1773 isn->isn_arg.unpack.unp_semicolon = semicolon;
1774 return OK;
1775}
1776
1777/*
1778 * Generate an instruction for any command modifiers.
1779 */
1780 int
1781generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1782{
1783 isn_T *isn;
1784
1785 if (has_cmdmod(cmod, FALSE))
1786 {
1787 cctx->ctx_has_cmdmod = TRUE;
1788
1789 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1790 return FAIL;
1791 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1792 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1793 return FAIL;
1794 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1795 // filter program now belongs to the instruction
1796 cmod->cmod_filter_regmatch.regprog = NULL;
1797 }
1798
1799 return OK;
1800}
1801
1802 int
1803generate_undo_cmdmods(cctx_T *cctx)
1804{
1805 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1806 return FAIL;
1807 cctx->ctx_has_cmdmod = FALSE;
1808 return OK;
1809}
1810
1811/*
1812 * Generate a STORE instruction for "dest", not being "dest_local".
1813 * Return FAIL when out of memory.
1814 */
1815 int
1816generate_store_var(
1817 cctx_T *cctx,
1818 assign_dest_T dest,
1819 int opt_flags,
1820 int vimvaridx,
1821 int scriptvar_idx,
1822 int scriptvar_sid,
1823 type_T *type,
1824 char_u *name)
1825{
1826 switch (dest)
1827 {
1828 case dest_option:
1829 return generate_STOREOPT(cctx, ISN_STOREOPT,
1830 skip_option_env_lead(name), opt_flags);
1831 case dest_func_option:
1832 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1833 skip_option_env_lead(name), opt_flags);
1834 case dest_global:
1835 // include g: with the name, easier to execute that way
1836 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1837 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1838 case dest_buffer:
1839 // include b: with the name, easier to execute that way
1840 return generate_STORE(cctx, ISN_STOREB, 0, name);
1841 case dest_window:
1842 // include w: with the name, easier to execute that way
1843 return generate_STORE(cctx, ISN_STOREW, 0, name);
1844 case dest_tab:
1845 // include t: with the name, easier to execute that way
1846 return generate_STORE(cctx, ISN_STORET, 0, name);
1847 case dest_env:
1848 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1849 case dest_reg:
1850 return generate_STORE(cctx, ISN_STOREREG,
1851 name[1] == '@' ? '"' : name[1], NULL);
1852 case dest_vimvar:
1853 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1854 case dest_script:
1855 if (scriptvar_idx < 0)
1856 // "s:" may be included in the name.
1857 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1858 scriptvar_sid, type);
1859 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1860 scriptvar_sid, scriptvar_idx, type);
1861 case dest_local:
1862 case dest_expr:
1863 // cannot happen
1864 break;
1865 }
1866 return FAIL;
1867}
1868
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001869/*
1870 * Return TRUE when inside a "for" or "while" loop.
1871 */
1872 int
1873inside_loop_scope(cctx_T *cctx)
1874{
1875 scope_T *scope = cctx->ctx_scope;
1876
1877 for (;;)
1878 {
1879 if (scope == NULL)
1880 break;
1881 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1882 return TRUE;
1883 scope = scope->se_outer;
1884 }
1885 return FALSE;
1886}
1887
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001888 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001889generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001890{
1891 if (lhs->lhs_dest != dest_local)
1892 return generate_store_var(cctx, lhs->lhs_dest,
1893 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1894 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1895 lhs->lhs_type, lhs->lhs_name);
1896
1897 if (lhs->lhs_lvar != NULL)
1898 {
1899 garray_T *instr = &cctx->ctx_instr;
1900 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1901
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001902 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1903 // ISN_STORENR.
1904 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001905 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
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001912 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001913 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001914 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001915 --instr->ga_len;
1916 }
1917 else
1918 {
1919 isn->isn_type = ISN_STORENR;
1920 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1921 isn->isn_arg.storenr.stnr_val = val;
1922 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923 if (stack->ga_len > 0)
1924 --stack->ga_len;
1925 }
1926 else if (lhs->lhs_lvar->lv_from_outer > 0)
1927 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1928 lhs->lhs_lvar->lv_from_outer);
1929 else
1930 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
1931 }
1932 return OK;
1933}
1934
1935#if defined(FEAT_PROFILE) || defined(PROTO)
1936 void
1937may_generate_prof_end(cctx_T *cctx, int prof_lnum)
1938{
1939 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
1940 generate_instr(cctx, ISN_PROF_END);
1941}
1942#endif
1943
1944
1945/*
1946 * Delete an instruction, free what it contains.
1947 */
1948 void
1949delete_instr(isn_T *isn)
1950{
1951 switch (isn->isn_type)
1952 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00001953 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001954 case ISN_DEF:
1955 case ISN_EXEC:
1956 case ISN_EXECRANGE:
1957 case ISN_EXEC_SPLIT:
1958 case ISN_LEGACY_EVAL:
1959 case ISN_LOADAUTO:
1960 case ISN_LOADB:
1961 case ISN_LOADENV:
1962 case ISN_LOADG:
1963 case ISN_LOADOPT:
1964 case ISN_LOADT:
1965 case ISN_LOADW:
1966 case ISN_LOCKUNLOCK:
1967 case ISN_PUSHEXC:
1968 case ISN_PUSHFUNC:
1969 case ISN_PUSHS:
1970 case ISN_RANGE:
1971 case ISN_STOREAUTO:
1972 case ISN_STOREB:
1973 case ISN_STOREENV:
1974 case ISN_STOREG:
1975 case ISN_STORET:
1976 case ISN_STOREW:
1977 case ISN_STRINGMEMBER:
1978 vim_free(isn->isn_arg.string);
1979 break;
1980
1981 case ISN_SUBSTITUTE:
1982 {
1983 int idx;
1984 isn_T *list = isn->isn_arg.subs.subs_instr;
1985
1986 vim_free(isn->isn_arg.subs.subs_cmd);
1987 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
1988 delete_instr(list + idx);
1989 vim_free(list);
1990 }
1991 break;
1992
1993 case ISN_INSTR:
1994 {
1995 int idx;
1996 isn_T *list = isn->isn_arg.instr;
1997
1998 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
1999 delete_instr(list + idx);
2000 vim_free(list);
2001 }
2002 break;
2003
2004 case ISN_LOADS:
2005 case ISN_STORES:
2006 vim_free(isn->isn_arg.loadstore.ls_name);
2007 break;
2008
2009 case ISN_UNLET:
2010 case ISN_UNLETENV:
2011 vim_free(isn->isn_arg.unlet.ul_name);
2012 break;
2013
2014 case ISN_STOREOPT:
2015 case ISN_STOREFUNCOPT:
2016 vim_free(isn->isn_arg.storeopt.so_name);
2017 break;
2018
2019 case ISN_PUSHBLOB: // push blob isn_arg.blob
2020 blob_unref(isn->isn_arg.blob);
2021 break;
2022
2023 case ISN_PUSHJOB:
2024#ifdef FEAT_JOB_CHANNEL
2025 job_unref(isn->isn_arg.job);
2026#endif
2027 break;
2028
2029 case ISN_PUSHCHANNEL:
2030#ifdef FEAT_JOB_CHANNEL
2031 channel_unref(isn->isn_arg.channel);
2032#endif
2033 break;
2034
2035 case ISN_UCALL:
2036 vim_free(isn->isn_arg.ufunc.cuf_name);
2037 break;
2038
2039 case ISN_FUNCREF:
2040 {
2041 if (isn->isn_arg.funcref.fr_func_name == NULL)
2042 {
2043 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2044 + isn->isn_arg.funcref.fr_dfunc_idx;
2045 ufunc_T *ufunc = dfunc->df_ufunc;
2046
2047 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2048 func_ptr_unref(ufunc);
2049 }
2050 else
2051 {
2052 char_u *name = isn->isn_arg.funcref.fr_func_name;
2053
2054 if (name != NULL)
2055 func_unref(name);
2056 vim_free(isn->isn_arg.funcref.fr_func_name);
2057 }
2058 }
2059 break;
2060
2061 case ISN_DCALL:
2062 {
2063 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2064 + isn->isn_arg.dfunc.cdf_idx;
2065
2066 if (dfunc->df_ufunc != NULL
2067 && func_name_refcount(dfunc->df_ufunc->uf_name))
2068 func_ptr_unref(dfunc->df_ufunc);
2069 }
2070 break;
2071
2072 case ISN_NEWFUNC:
2073 {
2074 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002075 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002076
2077 if (ufunc != NULL)
2078 {
2079 unlink_def_function(ufunc);
2080 func_ptr_unref(ufunc);
2081 }
2082
2083 vim_free(lambda);
2084 vim_free(isn->isn_arg.newfunc.nf_global);
2085 }
2086 break;
2087
2088 case ISN_CHECKTYPE:
2089 case ISN_SETTYPE:
2090 free_type(isn->isn_arg.type.ct_type);
2091 break;
2092
2093 case ISN_CMDMOD:
2094 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2095 ->cmod_filter_regmatch.regprog);
2096 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2097 break;
2098
2099 case ISN_LOADSCRIPT:
2100 case ISN_STORESCRIPT:
2101 vim_free(isn->isn_arg.script.scriptref);
2102 break;
2103
2104 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002105 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002106 break;
2107
2108 case ISN_CEXPR_CORE:
2109 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2110 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2111 break;
2112
2113 case ISN_2BOOL:
2114 case ISN_2STRING:
2115 case ISN_2STRING_ANY:
2116 case ISN_ADDBLOB:
2117 case ISN_ADDLIST:
2118 case ISN_ANYINDEX:
2119 case ISN_ANYSLICE:
2120 case ISN_BCALL:
2121 case ISN_BLOBAPPEND:
2122 case ISN_BLOBINDEX:
2123 case ISN_BLOBSLICE:
2124 case ISN_CATCH:
2125 case ISN_CEXPR_AUCMD:
2126 case ISN_CHECKLEN:
2127 case ISN_CHECKNR:
2128 case ISN_CLEARDICT:
2129 case ISN_CMDMOD_REV:
2130 case ISN_COMPAREANY:
2131 case ISN_COMPAREBLOB:
2132 case ISN_COMPAREBOOL:
2133 case ISN_COMPAREDICT:
2134 case ISN_COMPAREFLOAT:
2135 case ISN_COMPAREFUNC:
2136 case ISN_COMPARELIST:
2137 case ISN_COMPARENR:
2138 case ISN_COMPARESPECIAL:
2139 case ISN_COMPARESTRING:
2140 case ISN_CONCAT:
2141 case ISN_COND2BOOL:
2142 case ISN_DEBUG:
2143 case ISN_DROP:
2144 case ISN_ECHO:
2145 case ISN_ECHOCONSOLE:
2146 case ISN_ECHOERR:
2147 case ISN_ECHOMSG:
2148 case ISN_ENDTRY:
2149 case ISN_EXECCONCAT:
2150 case ISN_EXECUTE:
2151 case ISN_FINALLY:
2152 case ISN_FINISH:
2153 case ISN_FOR:
2154 case ISN_GETITEM:
2155 case ISN_JUMP:
2156 case ISN_JUMP_IF_ARG_SET:
2157 case ISN_LISTAPPEND:
2158 case ISN_LISTINDEX:
2159 case ISN_LISTSLICE:
2160 case ISN_LOAD:
2161 case ISN_LOADBDICT:
2162 case ISN_LOADGDICT:
2163 case ISN_LOADOUTER:
2164 case ISN_LOADREG:
2165 case ISN_LOADTDICT:
2166 case ISN_LOADV:
2167 case ISN_LOADWDICT:
2168 case ISN_LOCKCONST:
2169 case ISN_MEMBER:
2170 case ISN_NEGATENR:
2171 case ISN_NEWDICT:
2172 case ISN_NEWLIST:
2173 case ISN_OPANY:
2174 case ISN_OPFLOAT:
2175 case ISN_OPNR:
2176 case ISN_PCALL:
2177 case ISN_PCALL_END:
2178 case ISN_PROF_END:
2179 case ISN_PROF_START:
2180 case ISN_PUSHBOOL:
2181 case ISN_PUSHF:
2182 case ISN_PUSHNR:
2183 case ISN_PUSHSPEC:
2184 case ISN_PUT:
2185 case ISN_REDIREND:
2186 case ISN_REDIRSTART:
2187 case ISN_RETURN:
2188 case ISN_RETURN_VOID:
2189 case ISN_SHUFFLE:
2190 case ISN_SLICE:
2191 case ISN_STORE:
2192 case ISN_STOREINDEX:
2193 case ISN_STORENR:
2194 case ISN_STOREOUTER:
2195 case ISN_STORERANGE:
2196 case ISN_STOREREG:
2197 case ISN_STOREV:
2198 case ISN_STRINDEX:
2199 case ISN_STRSLICE:
2200 case ISN_THROW:
2201 case ISN_TRYCONT:
2202 case ISN_UNLETINDEX:
2203 case ISN_UNLETRANGE:
2204 case ISN_UNPACK:
2205 case ISN_USEDICT:
2206 // nothing allocated
2207 break;
2208 }
2209}
2210
2211 void
2212clear_instr_ga(garray_T *gap)
2213{
2214 int idx;
2215
2216 for (idx = 0; idx < gap->ga_len; ++idx)
2217 delete_instr(((isn_T *)gap->ga_data) + idx);
2218 ga_clear(gap);
2219}
2220
2221
2222#endif // defined(FEAT_EVAL)