blob: cf26650d17b914770fd90691aaf2de00c0ac2d1e [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;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001070 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001071 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001072
1073 RETURN_OK_IF_SKIP(cctx);
1074 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1075 return FAIL;
1076 isn->isn_arg.number = count;
1077
Bram Moolenaar078a4612022-01-04 15:17:03 +00001078 // Get the member type and the declared member type from all the items on
1079 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001080 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001081 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001082 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001083
1084 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001085 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001086
1087 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001088 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001089}
1090
1091/*
1092 * Generate an ISN_NEWDICT instruction.
1093 */
1094 int
1095generate_NEWDICT(cctx_T *cctx, int count)
1096{
1097 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001098 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001100 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001101
1102 RETURN_OK_IF_SKIP(cctx);
1103 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1104 return FAIL;
1105 isn->isn_arg.number = count;
1106
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001107 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001108 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001109 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001110
1111 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001112 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001113
1114 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001115 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001116}
1117
1118/*
1119 * Generate an ISN_FUNCREF instruction.
1120 */
1121 int
1122generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1123{
1124 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001125 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001126
1127 RETURN_OK_IF_SKIP(cctx);
1128 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1129 return FAIL;
1130 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1131 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1132 else
1133 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1134 cctx->ctx_has_closure = 1;
1135
1136 // If the referenced function is a closure, it may use items further up in
1137 // the nested context, including this one.
1138 if (ufunc->uf_flags & FC_CLOSURE)
1139 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1140
Bram Moolenaar078a4612022-01-04 15:17:03 +00001141 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1142 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001143}
1144
1145/*
1146 * Generate an ISN_NEWFUNC instruction.
1147 * "lambda_name" and "func_name" must be in allocated memory and will be
1148 * consumed.
1149 */
1150 int
1151generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1152{
1153 isn_T *isn;
1154
1155 if (cctx->ctx_skip == SKIP_YES)
1156 {
1157 vim_free(lambda_name);
1158 vim_free(func_name);
1159 return OK;
1160 }
1161 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1162 {
1163 vim_free(lambda_name);
1164 vim_free(func_name);
1165 return FAIL;
1166 }
1167 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1168 isn->isn_arg.newfunc.nf_global = func_name;
1169
1170 return OK;
1171}
1172
1173/*
1174 * Generate an ISN_DEF instruction: list functions
1175 */
1176 int
1177generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1178{
1179 isn_T *isn;
1180
1181 RETURN_OK_IF_SKIP(cctx);
1182 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1183 return FAIL;
1184 if (len > 0)
1185 {
1186 isn->isn_arg.string = vim_strnsave(name, len);
1187 if (isn->isn_arg.string == NULL)
1188 return FAIL;
1189 }
1190 return OK;
1191}
1192
1193/*
1194 * Generate an ISN_JUMP instruction.
1195 */
1196 int
1197generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1198{
1199 isn_T *isn;
1200 garray_T *stack = &cctx->ctx_type_stack;
1201
1202 RETURN_OK_IF_SKIP(cctx);
1203 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1204 return FAIL;
1205 isn->isn_arg.jump.jump_when = when;
1206 isn->isn_arg.jump.jump_where = where;
1207
1208 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1209 --stack->ga_len;
1210
1211 return OK;
1212}
1213
1214/*
1215 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1216 */
1217 int
1218generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1219{
1220 isn_T *isn;
1221
1222 RETURN_OK_IF_SKIP(cctx);
1223 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1224 return FAIL;
1225 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1226 // jump_where is set later
1227 return OK;
1228}
1229
1230 int
1231generate_FOR(cctx_T *cctx, int loop_idx)
1232{
1233 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001234
1235 RETURN_OK_IF_SKIP(cctx);
1236 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1237 return FAIL;
1238 isn->isn_arg.forloop.for_idx = loop_idx;
1239
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001240 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001241 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001242}
1243/*
1244 * Generate an ISN_TRYCONT instruction.
1245 */
1246 int
1247generate_TRYCONT(cctx_T *cctx, int levels, int where)
1248{
1249 isn_T *isn;
1250
1251 RETURN_OK_IF_SKIP(cctx);
1252 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1253 return FAIL;
1254 isn->isn_arg.trycont.tct_levels = levels;
1255 isn->isn_arg.trycont.tct_where = where;
1256
1257 return OK;
1258}
1259
1260
1261/*
1262 * Generate an ISN_BCALL instruction.
1263 * "method_call" is TRUE for "value->method()"
1264 * Return FAIL if the number of arguments is wrong.
1265 */
1266 int
1267generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1268{
1269 isn_T *isn;
1270 garray_T *stack = &cctx->ctx_type_stack;
1271 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001272 type2_T *typep;
1273 type2_T *argtypes = NULL;
1274 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1275 type2_T *maptype = NULL;
1276 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001277 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001278
1279 RETURN_OK_IF_SKIP(cctx);
1280 argoff = check_internal_func(func_idx, argcount);
1281 if (argoff < 0)
1282 return FAIL;
1283
1284 if (method_call && argoff > 1)
1285 {
1286 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1287 return FAIL;
1288 isn->isn_arg.shuffle.shfl_item = argcount;
1289 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1290 }
1291
1292 if (argcount > 0)
1293 {
1294 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001295 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001296 if (method_call && argoff > 1)
1297 {
1298 int i;
1299
1300 for (i = 0; i < argcount; ++i)
1301 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001302 ? typep[i + 1]
1303 : (i == argoff - 1) ? typep[0] : typep[i];
1304 argtypes = shuffled_argtypes;
1305 }
1306 else
1307 {
1308 int i;
1309
1310 for (i = 0; i < argcount; ++i)
1311 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001312 argtypes = shuffled_argtypes;
1313 }
1314 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1315 cctx) == FAIL)
1316 return FAIL;
1317 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001318 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001319 }
1320
1321 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1322 return FAIL;
1323 isn->isn_arg.bfunc.cbf_idx = func_idx;
1324 isn->isn_arg.bfunc.cbf_argcount = argcount;
1325
1326 // Drop the argument types and push the return type.
1327 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001328 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1329 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001330 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001331
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001332 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1333 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001334 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001335 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001336
1337 return OK;
1338}
1339
1340/*
1341 * Generate an ISN_LISTAPPEND instruction. Works like add().
1342 * Argument count is already checked.
1343 */
1344 int
1345generate_LISTAPPEND(cctx_T *cctx)
1346{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001347 type_T *list_type;
1348 type_T *item_type;
1349 type_T *expected;
1350
1351 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001352 // For checking the item type we use the declared type of the list and the
1353 // current type of the added item, adding a string to [1, 2] is OK.
1354 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001355 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001356 expected = list_type->tt_member;
1357 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1358 return FAIL;
1359
1360 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1361 return FAIL;
1362
Bram Moolenaar078a4612022-01-04 15:17:03 +00001363 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001364 return OK;
1365}
1366
1367/*
1368 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1369 * Argument count is already checked.
1370 */
1371 int
1372generate_BLOBAPPEND(cctx_T *cctx)
1373{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001374 type_T *item_type;
1375
1376 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001377 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001378 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1379 return FAIL;
1380
1381 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1382 return FAIL;
1383
Bram Moolenaar078a4612022-01-04 15:17:03 +00001384 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001385 return OK;
1386}
1387
1388/*
1389 * Generate an ISN_DCALL or ISN_UCALL instruction.
1390 * Return FAIL if the number of arguments is wrong.
1391 */
1392 int
1393generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1394{
1395 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001396 int regular_args = ufunc->uf_args.ga_len;
1397 int argcount = pushed_argcount;
1398
1399 RETURN_OK_IF_SKIP(cctx);
1400 if (argcount > regular_args && !has_varargs(ufunc))
1401 {
1402 semsg(_(e_too_many_arguments_for_function_str),
1403 printable_func_name(ufunc));
1404 return FAIL;
1405 }
1406 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1407 {
1408 semsg(_(e_not_enough_arguments_for_function_str),
1409 printable_func_name(ufunc));
1410 return FAIL;
1411 }
1412
1413 if (ufunc->uf_def_status != UF_NOT_COMPILED
1414 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1415 {
1416 int i;
1417
1418 for (i = 0; i < argcount; ++i)
1419 {
1420 type_T *expected;
1421 type_T *actual;
1422
Bram Moolenaar078a4612022-01-04 15:17:03 +00001423 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001424 if (actual == &t_special
1425 && i >= regular_args - ufunc->uf_def_args.ga_len)
1426 {
1427 // assume v:none used for default argument value
1428 continue;
1429 }
1430 if (i < regular_args)
1431 {
1432 if (ufunc->uf_arg_types == NULL)
1433 continue;
1434 expected = ufunc->uf_arg_types[i];
1435 }
1436 else if (ufunc->uf_va_type == NULL
1437 || ufunc->uf_va_type == &t_list_any)
1438 // possibly a lambda or "...: any"
1439 expected = &t_any;
1440 else
1441 expected = ufunc->uf_va_type->tt_member;
1442 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1443 TRUE, FALSE) == FAIL)
1444 {
1445 arg_type_mismatch(expected, actual, i + 1);
1446 return FAIL;
1447 }
1448 }
1449 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
1450 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1451 COMPILE_TYPE(ufunc), NULL) == FAIL)
1452 return FAIL;
1453 }
1454 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1455 {
1456 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1457 ufunc->uf_name);
1458 return FAIL;
1459 }
1460
1461 if ((isn = generate_instr(cctx,
1462 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1463 : ISN_UCALL)) == NULL)
1464 return FAIL;
1465 if (isn->isn_type == ISN_DCALL)
1466 {
1467 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1468 isn->isn_arg.dfunc.cdf_argcount = argcount;
1469 }
1470 else
1471 {
1472 // A user function may be deleted and redefined later, can't use the
1473 // ufunc pointer, need to look it up again at runtime.
1474 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1475 isn->isn_arg.ufunc.cuf_argcount = argcount;
1476 }
1477
Bram Moolenaar078a4612022-01-04 15:17:03 +00001478 // drop the argument types
1479 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001480
Bram Moolenaar078a4612022-01-04 15:17:03 +00001481 // add return type
1482 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483}
1484
1485/*
1486 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1487 */
1488 int
1489generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1490{
1491 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001492
1493 RETURN_OK_IF_SKIP(cctx);
1494 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1495 return FAIL;
1496 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1497 isn->isn_arg.ufunc.cuf_argcount = argcount;
1498
Bram Moolenaar078a4612022-01-04 15:17:03 +00001499 // drop the argument types
1500 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001501
Bram Moolenaar078a4612022-01-04 15:17:03 +00001502 // add return value
1503 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001504}
1505
1506/*
1507 * Generate an ISN_PCALL instruction.
1508 * "type" is the type of the FuncRef.
1509 */
1510 int
1511generate_PCALL(
1512 cctx_T *cctx,
1513 int argcount,
1514 char_u *name,
1515 type_T *type,
1516 int at_top)
1517{
1518 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001519 type_T *ret_type;
1520
1521 RETURN_OK_IF_SKIP(cctx);
1522
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001523 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001524 ret_type = &t_any;
1525 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1526 {
1527 if (type->tt_argcount != -1)
1528 {
1529 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1530
1531 if (argcount < type->tt_min_argcount - varargs)
1532 {
1533 semsg(_(e_not_enough_arguments_for_function_str), name);
1534 return FAIL;
1535 }
1536 if (!varargs && argcount > type->tt_argcount)
1537 {
1538 semsg(_(e_too_many_arguments_for_function_str), name);
1539 return FAIL;
1540 }
1541 if (type->tt_args != NULL)
1542 {
1543 int i;
1544
1545 for (i = 0; i < argcount; ++i)
1546 {
1547 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001548 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001549 type_T *expected;
1550
1551 if (varargs && i >= type->tt_argcount - 1)
1552 expected = type->tt_args[
1553 type->tt_argcount - 1]->tt_member;
1554 else if (i >= type->tt_min_argcount
1555 && actual == &t_special)
1556 expected = &t_any;
1557 else
1558 expected = type->tt_args[i];
1559 if (need_type(actual, expected, offset, i + 1,
1560 cctx, TRUE, FALSE) == FAIL)
1561 {
1562 arg_type_mismatch(expected, actual, i + 1);
1563 return FAIL;
1564 }
1565 }
1566 }
1567 }
1568 ret_type = type->tt_member;
1569 if (ret_type == &t_unknown)
1570 // return type not known yet, use a runtime check
1571 ret_type = &t_any;
1572 }
1573 else
1574 {
1575 semsg(_(e_not_callable_type_str), name);
1576 return FAIL;
1577 }
1578
1579 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1580 return FAIL;
1581 isn->isn_arg.pfunc.cpf_top = at_top;
1582 isn->isn_arg.pfunc.cpf_argcount = argcount;
1583
Bram Moolenaar078a4612022-01-04 15:17:03 +00001584 // drop the arguments and the funcref/partial
1585 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001586
Bram Moolenaar078a4612022-01-04 15:17:03 +00001587 // push the return value
1588 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001589
1590 // If partial is above the arguments it must be cleared and replaced with
1591 // the return value.
1592 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1593 return FAIL;
1594
1595 return OK;
1596}
1597
1598/*
1599 * Generate an ISN_STRINGMEMBER instruction.
1600 */
1601 int
1602generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1603{
1604 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001605 type_T *type;
1606
1607 RETURN_OK_IF_SKIP(cctx);
1608 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1609 return FAIL;
1610 isn->isn_arg.string = vim_strnsave(name, len);
1611
1612 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001613 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001614 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001615 {
1616 char *tofree;
1617
1618 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1619 name, type_name(type, &tofree));
1620 vim_free(tofree);
1621 return FAIL;
1622 }
1623 // change dict type to dict member type
1624 if (type->tt_type == VAR_DICT)
1625 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001626 type_T *ntype = type->tt_member == &t_unknown
1627 ? &t_any : type->tt_member;
1628 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001629 }
1630
1631 return OK;
1632}
1633
1634/*
1635 * Generate an ISN_ECHO instruction.
1636 */
1637 int
1638generate_ECHO(cctx_T *cctx, int with_white, int count)
1639{
1640 isn_T *isn;
1641
1642 RETURN_OK_IF_SKIP(cctx);
1643 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1644 return FAIL;
1645 isn->isn_arg.echo.echo_with_white = with_white;
1646 isn->isn_arg.echo.echo_count = count;
1647
1648 return OK;
1649}
1650
1651/*
1652 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1653 */
1654 int
1655generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1656{
1657 isn_T *isn;
1658
1659 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1660 return FAIL;
1661 isn->isn_arg.number = count;
1662
1663 return OK;
1664}
1665
1666/*
1667 * Generate an ISN_PUT instruction.
1668 */
1669 int
1670generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1671{
1672 isn_T *isn;
1673
1674 RETURN_OK_IF_SKIP(cctx);
1675 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1676 return FAIL;
1677 isn->isn_arg.put.put_regname = regname;
1678 isn->isn_arg.put.put_lnum = lnum;
1679 return OK;
1680}
1681
1682/*
1683 * Generate an EXEC instruction that takes a string argument.
1684 * A copy is made of "line".
1685 */
1686 int
1687generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1688{
1689 isn_T *isn;
1690
1691 RETURN_OK_IF_SKIP(cctx);
1692 if ((isn = generate_instr(cctx, isntype)) == NULL)
1693 return FAIL;
1694 isn->isn_arg.string = vim_strsave(line);
1695 return OK;
1696}
1697
1698/*
1699 * Generate an EXEC instruction that takes a string argument.
1700 * "str" must be allocated, it is consumed.
1701 */
1702 int
1703generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1704{
1705 isn_T *isn;
1706
1707 if (cctx->ctx_skip == SKIP_YES)
1708 {
1709 vim_free(str);
1710 return OK;
1711 }
1712 if ((isn = generate_instr(cctx, isntype)) == NULL)
1713 {
1714 vim_free(str);
1715 return FAIL;
1716 }
1717 isn->isn_arg.string = str;
1718 return OK;
1719}
1720
1721 int
1722generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1723{
1724 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001725
1726 RETURN_OK_IF_SKIP(cctx);
1727 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1728 return FAIL;
1729 isn->isn_arg.string = vim_strsave(line);
1730
Bram Moolenaar078a4612022-01-04 15:17:03 +00001731 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001732}
1733
1734 int
1735generate_EXECCONCAT(cctx_T *cctx, int count)
1736{
1737 isn_T *isn;
1738
1739 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1740 return FAIL;
1741 isn->isn_arg.number = count;
1742 return OK;
1743}
1744
1745/*
1746 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1747 */
1748 int
1749generate_RANGE(cctx_T *cctx, char_u *range)
1750{
1751 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001752
1753 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1754 return FAIL;
1755 isn->isn_arg.string = range;
1756
Bram Moolenaar078a4612022-01-04 15:17:03 +00001757 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001758}
1759
1760 int
1761generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1762{
1763 isn_T *isn;
1764
1765 RETURN_OK_IF_SKIP(cctx);
1766 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1767 return FAIL;
1768 isn->isn_arg.unpack.unp_count = var_count;
1769 isn->isn_arg.unpack.unp_semicolon = semicolon;
1770 return OK;
1771}
1772
1773/*
1774 * Generate an instruction for any command modifiers.
1775 */
1776 int
1777generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1778{
1779 isn_T *isn;
1780
1781 if (has_cmdmod(cmod, FALSE))
1782 {
1783 cctx->ctx_has_cmdmod = TRUE;
1784
1785 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1786 return FAIL;
1787 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1788 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1789 return FAIL;
1790 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1791 // filter program now belongs to the instruction
1792 cmod->cmod_filter_regmatch.regprog = NULL;
1793 }
1794
1795 return OK;
1796}
1797
1798 int
1799generate_undo_cmdmods(cctx_T *cctx)
1800{
1801 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1802 return FAIL;
1803 cctx->ctx_has_cmdmod = FALSE;
1804 return OK;
1805}
1806
1807/*
1808 * Generate a STORE instruction for "dest", not being "dest_local".
1809 * Return FAIL when out of memory.
1810 */
1811 int
1812generate_store_var(
1813 cctx_T *cctx,
1814 assign_dest_T dest,
1815 int opt_flags,
1816 int vimvaridx,
1817 int scriptvar_idx,
1818 int scriptvar_sid,
1819 type_T *type,
1820 char_u *name)
1821{
1822 switch (dest)
1823 {
1824 case dest_option:
1825 return generate_STOREOPT(cctx, ISN_STOREOPT,
1826 skip_option_env_lead(name), opt_flags);
1827 case dest_func_option:
1828 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1829 skip_option_env_lead(name), opt_flags);
1830 case dest_global:
1831 // include g: with the name, easier to execute that way
1832 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1833 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1834 case dest_buffer:
1835 // include b: with the name, easier to execute that way
1836 return generate_STORE(cctx, ISN_STOREB, 0, name);
1837 case dest_window:
1838 // include w: with the name, easier to execute that way
1839 return generate_STORE(cctx, ISN_STOREW, 0, name);
1840 case dest_tab:
1841 // include t: with the name, easier to execute that way
1842 return generate_STORE(cctx, ISN_STORET, 0, name);
1843 case dest_env:
1844 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1845 case dest_reg:
1846 return generate_STORE(cctx, ISN_STOREREG,
1847 name[1] == '@' ? '"' : name[1], NULL);
1848 case dest_vimvar:
1849 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1850 case dest_script:
1851 if (scriptvar_idx < 0)
1852 // "s:" may be included in the name.
1853 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1854 scriptvar_sid, type);
1855 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1856 scriptvar_sid, scriptvar_idx, type);
1857 case dest_local:
1858 case dest_expr:
1859 // cannot happen
1860 break;
1861 }
1862 return FAIL;
1863}
1864
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001865/*
1866 * Return TRUE when inside a "for" or "while" loop.
1867 */
1868 int
1869inside_loop_scope(cctx_T *cctx)
1870{
1871 scope_T *scope = cctx->ctx_scope;
1872
1873 for (;;)
1874 {
1875 if (scope == NULL)
1876 break;
1877 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1878 return TRUE;
1879 scope = scope->se_outer;
1880 }
1881 return FALSE;
1882}
1883
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001884 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001885generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001886{
1887 if (lhs->lhs_dest != dest_local)
1888 return generate_store_var(cctx, lhs->lhs_dest,
1889 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1890 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1891 lhs->lhs_type, lhs->lhs_name);
1892
1893 if (lhs->lhs_lvar != NULL)
1894 {
1895 garray_T *instr = &cctx->ctx_instr;
1896 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1897
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001898 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1899 // ISN_STORENR.
1900 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001901 if (lhs->lhs_lvar->lv_from_outer == 0
1902 && instr->ga_len == instr_count + 1
1903 && isn->isn_type == ISN_PUSHNR)
1904 {
1905 varnumber_T val = isn->isn_arg.number;
1906 garray_T *stack = &cctx->ctx_type_stack;
1907
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001908 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001909 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001910 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001911 --instr->ga_len;
1912 }
1913 else
1914 {
1915 isn->isn_type = ISN_STORENR;
1916 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1917 isn->isn_arg.storenr.stnr_val = val;
1918 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001919 if (stack->ga_len > 0)
1920 --stack->ga_len;
1921 }
1922 else if (lhs->lhs_lvar->lv_from_outer > 0)
1923 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1924 lhs->lhs_lvar->lv_from_outer);
1925 else
1926 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
1927 }
1928 return OK;
1929}
1930
1931#if defined(FEAT_PROFILE) || defined(PROTO)
1932 void
1933may_generate_prof_end(cctx_T *cctx, int prof_lnum)
1934{
1935 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
1936 generate_instr(cctx, ISN_PROF_END);
1937}
1938#endif
1939
1940
1941/*
1942 * Delete an instruction, free what it contains.
1943 */
1944 void
1945delete_instr(isn_T *isn)
1946{
1947 switch (isn->isn_type)
1948 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00001949 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001950 case ISN_DEF:
1951 case ISN_EXEC:
1952 case ISN_EXECRANGE:
1953 case ISN_EXEC_SPLIT:
1954 case ISN_LEGACY_EVAL:
1955 case ISN_LOADAUTO:
1956 case ISN_LOADB:
1957 case ISN_LOADENV:
1958 case ISN_LOADG:
1959 case ISN_LOADOPT:
1960 case ISN_LOADT:
1961 case ISN_LOADW:
1962 case ISN_LOCKUNLOCK:
1963 case ISN_PUSHEXC:
1964 case ISN_PUSHFUNC:
1965 case ISN_PUSHS:
1966 case ISN_RANGE:
1967 case ISN_STOREAUTO:
1968 case ISN_STOREB:
1969 case ISN_STOREENV:
1970 case ISN_STOREG:
1971 case ISN_STORET:
1972 case ISN_STOREW:
1973 case ISN_STRINGMEMBER:
1974 vim_free(isn->isn_arg.string);
1975 break;
1976
1977 case ISN_SUBSTITUTE:
1978 {
1979 int idx;
1980 isn_T *list = isn->isn_arg.subs.subs_instr;
1981
1982 vim_free(isn->isn_arg.subs.subs_cmd);
1983 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
1984 delete_instr(list + idx);
1985 vim_free(list);
1986 }
1987 break;
1988
1989 case ISN_INSTR:
1990 {
1991 int idx;
1992 isn_T *list = isn->isn_arg.instr;
1993
1994 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
1995 delete_instr(list + idx);
1996 vim_free(list);
1997 }
1998 break;
1999
2000 case ISN_LOADS:
2001 case ISN_STORES:
2002 vim_free(isn->isn_arg.loadstore.ls_name);
2003 break;
2004
2005 case ISN_UNLET:
2006 case ISN_UNLETENV:
2007 vim_free(isn->isn_arg.unlet.ul_name);
2008 break;
2009
2010 case ISN_STOREOPT:
2011 case ISN_STOREFUNCOPT:
2012 vim_free(isn->isn_arg.storeopt.so_name);
2013 break;
2014
2015 case ISN_PUSHBLOB: // push blob isn_arg.blob
2016 blob_unref(isn->isn_arg.blob);
2017 break;
2018
2019 case ISN_PUSHJOB:
2020#ifdef FEAT_JOB_CHANNEL
2021 job_unref(isn->isn_arg.job);
2022#endif
2023 break;
2024
2025 case ISN_PUSHCHANNEL:
2026#ifdef FEAT_JOB_CHANNEL
2027 channel_unref(isn->isn_arg.channel);
2028#endif
2029 break;
2030
2031 case ISN_UCALL:
2032 vim_free(isn->isn_arg.ufunc.cuf_name);
2033 break;
2034
2035 case ISN_FUNCREF:
2036 {
2037 if (isn->isn_arg.funcref.fr_func_name == NULL)
2038 {
2039 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2040 + isn->isn_arg.funcref.fr_dfunc_idx;
2041 ufunc_T *ufunc = dfunc->df_ufunc;
2042
2043 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2044 func_ptr_unref(ufunc);
2045 }
2046 else
2047 {
2048 char_u *name = isn->isn_arg.funcref.fr_func_name;
2049
2050 if (name != NULL)
2051 func_unref(name);
2052 vim_free(isn->isn_arg.funcref.fr_func_name);
2053 }
2054 }
2055 break;
2056
2057 case ISN_DCALL:
2058 {
2059 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2060 + isn->isn_arg.dfunc.cdf_idx;
2061
2062 if (dfunc->df_ufunc != NULL
2063 && func_name_refcount(dfunc->df_ufunc->uf_name))
2064 func_ptr_unref(dfunc->df_ufunc);
2065 }
2066 break;
2067
2068 case ISN_NEWFUNC:
2069 {
2070 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002071 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002072
2073 if (ufunc != NULL)
2074 {
2075 unlink_def_function(ufunc);
2076 func_ptr_unref(ufunc);
2077 }
2078
2079 vim_free(lambda);
2080 vim_free(isn->isn_arg.newfunc.nf_global);
2081 }
2082 break;
2083
2084 case ISN_CHECKTYPE:
2085 case ISN_SETTYPE:
2086 free_type(isn->isn_arg.type.ct_type);
2087 break;
2088
2089 case ISN_CMDMOD:
2090 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2091 ->cmod_filter_regmatch.regprog);
2092 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2093 break;
2094
2095 case ISN_LOADSCRIPT:
2096 case ISN_STORESCRIPT:
2097 vim_free(isn->isn_arg.script.scriptref);
2098 break;
2099
2100 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002101 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002102 break;
2103
2104 case ISN_CEXPR_CORE:
2105 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2106 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2107 break;
2108
2109 case ISN_2BOOL:
2110 case ISN_2STRING:
2111 case ISN_2STRING_ANY:
2112 case ISN_ADDBLOB:
2113 case ISN_ADDLIST:
2114 case ISN_ANYINDEX:
2115 case ISN_ANYSLICE:
2116 case ISN_BCALL:
2117 case ISN_BLOBAPPEND:
2118 case ISN_BLOBINDEX:
2119 case ISN_BLOBSLICE:
2120 case ISN_CATCH:
2121 case ISN_CEXPR_AUCMD:
2122 case ISN_CHECKLEN:
2123 case ISN_CHECKNR:
2124 case ISN_CLEARDICT:
2125 case ISN_CMDMOD_REV:
2126 case ISN_COMPAREANY:
2127 case ISN_COMPAREBLOB:
2128 case ISN_COMPAREBOOL:
2129 case ISN_COMPAREDICT:
2130 case ISN_COMPAREFLOAT:
2131 case ISN_COMPAREFUNC:
2132 case ISN_COMPARELIST:
2133 case ISN_COMPARENR:
2134 case ISN_COMPARESPECIAL:
2135 case ISN_COMPARESTRING:
2136 case ISN_CONCAT:
2137 case ISN_COND2BOOL:
2138 case ISN_DEBUG:
2139 case ISN_DROP:
2140 case ISN_ECHO:
2141 case ISN_ECHOCONSOLE:
2142 case ISN_ECHOERR:
2143 case ISN_ECHOMSG:
2144 case ISN_ENDTRY:
2145 case ISN_EXECCONCAT:
2146 case ISN_EXECUTE:
2147 case ISN_FINALLY:
2148 case ISN_FINISH:
2149 case ISN_FOR:
2150 case ISN_GETITEM:
2151 case ISN_JUMP:
2152 case ISN_JUMP_IF_ARG_SET:
2153 case ISN_LISTAPPEND:
2154 case ISN_LISTINDEX:
2155 case ISN_LISTSLICE:
2156 case ISN_LOAD:
2157 case ISN_LOADBDICT:
2158 case ISN_LOADGDICT:
2159 case ISN_LOADOUTER:
2160 case ISN_LOADREG:
2161 case ISN_LOADTDICT:
2162 case ISN_LOADV:
2163 case ISN_LOADWDICT:
2164 case ISN_LOCKCONST:
2165 case ISN_MEMBER:
2166 case ISN_NEGATENR:
2167 case ISN_NEWDICT:
2168 case ISN_NEWLIST:
2169 case ISN_OPANY:
2170 case ISN_OPFLOAT:
2171 case ISN_OPNR:
2172 case ISN_PCALL:
2173 case ISN_PCALL_END:
2174 case ISN_PROF_END:
2175 case ISN_PROF_START:
2176 case ISN_PUSHBOOL:
2177 case ISN_PUSHF:
2178 case ISN_PUSHNR:
2179 case ISN_PUSHSPEC:
2180 case ISN_PUT:
2181 case ISN_REDIREND:
2182 case ISN_REDIRSTART:
2183 case ISN_RETURN:
2184 case ISN_RETURN_VOID:
2185 case ISN_SHUFFLE:
2186 case ISN_SLICE:
2187 case ISN_STORE:
2188 case ISN_STOREINDEX:
2189 case ISN_STORENR:
2190 case ISN_STOREOUTER:
2191 case ISN_STORERANGE:
2192 case ISN_STOREREG:
2193 case ISN_STOREV:
2194 case ISN_STRINDEX:
2195 case ISN_STRSLICE:
2196 case ISN_THROW:
2197 case ISN_TRYCONT:
2198 case ISN_UNLETINDEX:
2199 case ISN_UNLETRANGE:
2200 case ISN_UNPACK:
2201 case ISN_USEDICT:
2202 // nothing allocated
2203 break;
2204 }
2205}
2206
2207 void
2208clear_instr_ga(garray_T *gap)
2209{
2210 int idx;
2211
2212 for (idx = 0; idx < gap->ga_len; ++idx)
2213 delete_instr(((isn_T *)gap->ga_data) + idx);
2214 ga_clear(gap);
2215}
2216
2217
2218#endif // defined(FEAT_EVAL)