blob: e36ea16e2e9b36f6fd44449b211edda3b81dbcff [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)
Bram Moolenaared0c62e2022-03-08 19:43:55 +0000373 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT))
374 || (type1 == VAR_FUNC && type2 == VAR_PARTIAL)
375 || (type1 == VAR_PARTIAL && type2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000376 isntype = ISN_COMPAREANY;
Bram Moolenaar7a222242022-03-01 19:23:24 +0000377 else if (type1 == VAR_SPECIAL || type2 == VAR_SPECIAL)
378 {
379 switch (type1 == VAR_SPECIAL ? type2 : type1)
380 {
381 case VAR_BLOB: break;
382 case VAR_CHANNEL: break;
383 case VAR_DICT: break;
384 case VAR_FUNC: break;
385 case VAR_JOB: break;
386 case VAR_LIST: break;
387 case VAR_PARTIAL: break;
388 case VAR_STRING: break;
389 default: semsg(_(e_cannot_compare_str_with_str),
390 vartype_name(type1), vartype_name(type2));
391 return ISN_DROP;
392 }
393 isntype = ISN_COMPARENULL;
394 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000395
396 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
397 && (isntype == ISN_COMPAREBOOL
398 || isntype == ISN_COMPARESPECIAL
399 || isntype == ISN_COMPARENR
400 || isntype == ISN_COMPAREFLOAT))
401 {
402 semsg(_(e_cannot_use_str_with_str),
403 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1));
404 return ISN_DROP;
405 }
406 if (isntype == ISN_DROP
407 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
408 && (type1 == VAR_BOOL || type1 == VAR_SPECIAL
409 || type2 == VAR_BOOL || type2 == VAR_SPECIAL)))
410 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000411 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000412 && (type1 == VAR_BLOB || type2 == VAR_BLOB
413 || type1 == VAR_LIST || type2 == VAR_LIST))))
414 {
415 semsg(_(e_cannot_compare_str_with_str),
416 vartype_name(type1), vartype_name(type2));
417 return ISN_DROP;
418 }
419 return isntype;
420}
421
422 int
423check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
424{
425 if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP)
426 return FAIL;
427 return OK;
428}
429
430/*
431 * Generate an ISN_COMPARE* instruction with a boolean result.
432 */
433 int
434generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
435{
436 isntype_T isntype;
437 isn_T *isn;
438 garray_T *stack = &cctx->ctx_type_stack;
439 vartype_T type1;
440 vartype_T type2;
441
442 RETURN_OK_IF_SKIP(cctx);
443
444 // Get the known type of the two items on the stack. If they are matching
445 // use a type-specific instruction. Otherwise fall back to runtime type
446 // checking.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000447 type1 = get_type_on_stack(cctx, 1)->tt_type;
448 type2 = get_type_on_stack(cctx, 0)->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000449 isntype = get_compare_isn(exprtype, type1, type2);
450 if (isntype == ISN_DROP)
451 return FAIL;
452
453 if ((isn = generate_instr(cctx, isntype)) == NULL)
454 return FAIL;
455 isn->isn_arg.op.op_type = exprtype;
456 isn->isn_arg.op.op_ic = ic;
457
458 // takes two arguments, puts one bool back
459 if (stack->ga_len >= 2)
460 {
461 --stack->ga_len;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000462 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000463 }
464
465 return OK;
466}
467
468/*
469 * Generate an ISN_2BOOL instruction.
470 * "offset" is the offset in the type stack.
471 */
472 int
473generate_2BOOL(cctx_T *cctx, int invert, int offset)
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_2BOOL)) == NULL)
479 return FAIL;
480 isn->isn_arg.tobool.invert = invert;
481 isn->isn_arg.tobool.offset = offset;
482
483 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000484 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000485
486 return OK;
487}
488
489/*
490 * Generate an ISN_COND2BOOL instruction.
491 */
492 int
493generate_COND2BOOL(cctx_T *cctx)
494{
495 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000496
497 RETURN_OK_IF_SKIP(cctx);
498 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
499 return FAIL;
500
501 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000502 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000503
504 return OK;
505}
506
507 int
508generate_TYPECHECK(
509 cctx_T *cctx,
510 type_T *expected,
511 int offset,
512 int argidx)
513{
514 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000515
516 RETURN_OK_IF_SKIP(cctx);
517 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
518 return FAIL;
519 isn->isn_arg.type.ct_type = alloc_type(expected);
520 isn->isn_arg.type.ct_off = (int8_T)offset;
521 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
522
523 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000524 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000525
526 return OK;
527}
528
529 int
530generate_SETTYPE(
531 cctx_T *cctx,
532 type_T *expected)
533{
534 isn_T *isn;
535
536 RETURN_OK_IF_SKIP(cctx);
537 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
538 return FAIL;
539 isn->isn_arg.type.ct_type = alloc_type(expected);
540 return OK;
541}
542
543/*
544 * Generate a PUSH instruction for "tv".
545 * "tv" will be consumed or cleared.
546 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
547 */
548 int
549generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
550{
551 if (tv != NULL)
552 {
553 switch (tv->v_type)
554 {
555 case VAR_UNKNOWN:
556 break;
557 case VAR_BOOL:
558 generate_PUSHBOOL(cctx, tv->vval.v_number);
559 break;
560 case VAR_SPECIAL:
561 generate_PUSHSPEC(cctx, tv->vval.v_number);
562 break;
563 case VAR_NUMBER:
564 generate_PUSHNR(cctx, tv->vval.v_number);
565 break;
566#ifdef FEAT_FLOAT
567 case VAR_FLOAT:
568 generate_PUSHF(cctx, tv->vval.v_float);
569 break;
570#endif
571 case VAR_BLOB:
572 generate_PUSHBLOB(cctx, tv->vval.v_blob);
573 tv->vval.v_blob = NULL;
574 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000575 case VAR_LIST:
576 if (tv->vval.v_list != NULL)
577 iemsg("non-empty list constant not supported");
578 generate_NEWLIST(cctx, 0);
579 break;
580 case VAR_DICT:
581 if (tv->vval.v_dict != NULL)
582 iemsg("non-empty dict constant not supported");
583 generate_NEWDICT(cctx, 0);
584 break;
585#ifdef FEAT_JOB_CHANNEL
586 case VAR_JOB:
587 if (tv->vval.v_job != NULL)
588 iemsg("non-null job constant not supported");
589 generate_PUSHJOB(cctx, NULL);
590 break;
591 case VAR_CHANNEL:
592 if (tv->vval.v_channel != NULL)
593 iemsg("non-null channel constant not supported");
594 generate_PUSHCHANNEL(cctx, NULL);
595 break;
596#endif
597 case VAR_FUNC:
598 if (tv->vval.v_string != NULL)
599 iemsg("non-null function constant not supported");
600 generate_PUSHFUNC(cctx, NULL, &t_func_unknown);
601 break;
602 case VAR_PARTIAL:
603 if (tv->vval.v_partial != NULL)
604 iemsg("non-null partial constant not supported");
605 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
606 == NULL)
607 return FAIL;
608 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000609 case VAR_STRING:
610 generate_PUSHS(cctx, &tv->vval.v_string);
611 tv->vval.v_string = NULL;
612 break;
613 default:
614 iemsg("constant type not supported");
615 clear_tv(tv);
616 return FAIL;
617 }
618 tv->v_type = VAR_UNKNOWN;
619 }
620 return OK;
621}
622
623/*
624 * Generate an ISN_PUSHNR instruction.
625 */
626 int
627generate_PUSHNR(cctx_T *cctx, varnumber_T number)
628{
629 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000630
631 RETURN_OK_IF_SKIP(cctx);
632 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
633 return FAIL;
634 isn->isn_arg.number = number;
635
636 if (number == 0 || number == 1)
637 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000638 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000639 return OK;
640}
641
642/*
643 * Generate an ISN_PUSHBOOL instruction.
644 */
645 int
646generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
647{
648 isn_T *isn;
649
650 RETURN_OK_IF_SKIP(cctx);
651 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
652 return FAIL;
653 isn->isn_arg.number = number;
654
655 return OK;
656}
657
658/*
659 * Generate an ISN_PUSHSPEC instruction.
660 */
661 int
662generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
663{
664 isn_T *isn;
665
666 RETURN_OK_IF_SKIP(cctx);
667 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL)
668 return FAIL;
669 isn->isn_arg.number = number;
670
671 return OK;
672}
673
674#if defined(FEAT_FLOAT) || defined(PROTO)
675/*
676 * Generate an ISN_PUSHF instruction.
677 */
678 int
679generate_PUSHF(cctx_T *cctx, float_T fnumber)
680{
681 isn_T *isn;
682
683 RETURN_OK_IF_SKIP(cctx);
684 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
685 return FAIL;
686 isn->isn_arg.fnumber = fnumber;
687
688 return OK;
689}
690#endif
691
692/*
693 * Generate an ISN_PUSHS instruction.
694 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
695 */
696 int
697generate_PUSHS(cctx_T *cctx, char_u **str)
698{
699 isn_T *isn;
700
701 if (cctx->ctx_skip == SKIP_YES)
702 {
703 if (str != NULL)
704 VIM_CLEAR(*str);
705 return OK;
706 }
707 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
708 {
709 if (str != NULL)
710 VIM_CLEAR(*str);
711 return FAIL;
712 }
713 isn->isn_arg.string = str == NULL ? NULL : *str;
714
715 return OK;
716}
717
718/*
719 * Generate an ISN_PUSHCHANNEL instruction.
720 * Consumes "channel".
721 */
722 int
723generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
724{
725 isn_T *isn;
726
727 RETURN_OK_IF_SKIP(cctx);
728 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
729 return FAIL;
730 isn->isn_arg.channel = channel;
731
732 return OK;
733}
734
735/*
736 * Generate an ISN_PUSHJOB instruction.
737 * Consumes "job".
738 */
739 int
740generate_PUSHJOB(cctx_T *cctx, job_T *job)
741{
742 isn_T *isn;
743
744 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000745 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_job)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746 return FAIL;
747 isn->isn_arg.job = job;
748
749 return OK;
750}
751
752/*
753 * Generate an ISN_PUSHBLOB instruction.
754 * Consumes "blob".
755 */
756 int
757generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
758{
759 isn_T *isn;
760
761 RETURN_OK_IF_SKIP(cctx);
762 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
763 return FAIL;
764 isn->isn_arg.blob = blob;
765
766 return OK;
767}
768
769/*
770 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000771 */
772 int
773generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
774{
775 isn_T *isn;
776 char_u *funcname;
777
778 RETURN_OK_IF_SKIP(cctx);
779 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
780 return FAIL;
781 if (name == NULL)
782 funcname = NULL;
Bram Moolenaard041f422022-01-12 19:54:00 +0000783 else if (*name == K_SPECIAL // script-local
784 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000785 funcname = vim_strsave(name);
786 else
787 {
788 funcname = alloc(STRLEN(name) + 3);
789 if (funcname != NULL)
790 {
791 STRCPY(funcname, "g:");
792 STRCPY(funcname + 2, name);
793 }
794 }
795
796 isn->isn_arg.string = funcname;
797 return OK;
798}
799
800/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000801 * Generate an ISN_AUTOLOAD instruction.
802 */
803 int
804generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
805{
806 isn_T *isn;
807
808 RETURN_OK_IF_SKIP(cctx);
809 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
810 return FAIL;
811 isn->isn_arg.string = vim_strsave(name);
812 if (isn->isn_arg.string == NULL)
813 return FAIL;
814 return OK;
815}
816
817/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000818 * Generate an ISN_GETITEM instruction with "index".
819 * "with_op" is TRUE for "+=" and other operators, the stack has the current
820 * value below the list with values.
821 */
822 int
823generate_GETITEM(cctx_T *cctx, int index, int with_op)
824{
825 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000826 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827 type_T *item_type = &t_any;
828
829 RETURN_OK_IF_SKIP(cctx);
830
831 if (type->tt_type != VAR_LIST)
832 {
833 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000834 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000835 return FAIL;
836 }
837 item_type = type->tt_member;
838 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
839 return FAIL;
840 isn->isn_arg.getitem.gi_index = index;
841 isn->isn_arg.getitem.gi_with_op = with_op;
842
843 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000844 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000845}
846
847/*
848 * Generate an ISN_SLICE instruction with "count".
849 */
850 int
851generate_SLICE(cctx_T *cctx, int count)
852{
853 isn_T *isn;
854
855 RETURN_OK_IF_SKIP(cctx);
856 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
857 return FAIL;
858 isn->isn_arg.number = count;
859 return OK;
860}
861
862/*
863 * Generate an ISN_CHECKLEN instruction with "min_len".
864 */
865 int
866generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
867{
868 isn_T *isn;
869
870 RETURN_OK_IF_SKIP(cctx);
871
872 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
873 return FAIL;
874 isn->isn_arg.checklen.cl_min_len = min_len;
875 isn->isn_arg.checklen.cl_more_OK = more_OK;
876
877 return OK;
878}
879
880/*
881 * Generate an ISN_STORE instruction.
882 */
883 int
884generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
885{
886 isn_T *isn;
887
888 RETURN_OK_IF_SKIP(cctx);
889 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
890 return FAIL;
891 if (name != NULL)
892 isn->isn_arg.string = vim_strsave(name);
893 else
894 isn->isn_arg.number = idx;
895
896 return OK;
897}
898
899/*
900 * Generate an ISN_STOREOUTER instruction.
901 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000902 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000903generate_STOREOUTER(cctx_T *cctx, int idx, int level)
904{
905 isn_T *isn;
906
907 RETURN_OK_IF_SKIP(cctx);
908 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
909 return FAIL;
910 isn->isn_arg.outer.outer_idx = idx;
911 isn->isn_arg.outer.outer_depth = level;
912
913 return OK;
914}
915
916/*
917 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
918 */
919 int
920generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
921{
922 isn_T *isn;
923
924 RETURN_OK_IF_SKIP(cctx);
925 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
926 return FAIL;
927 isn->isn_arg.storenr.stnr_idx = idx;
928 isn->isn_arg.storenr.stnr_val = value;
929
930 return OK;
931}
932
933/*
934 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
935 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000936 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000937generate_STOREOPT(
938 cctx_T *cctx,
939 isntype_T isn_type,
940 char_u *name,
941 int opt_flags)
942{
943 isn_T *isn;
944
945 RETURN_OK_IF_SKIP(cctx);
946 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
947 return FAIL;
948 isn->isn_arg.storeopt.so_name = vim_strsave(name);
949 isn->isn_arg.storeopt.so_flags = opt_flags;
950
951 return OK;
952}
953
954/*
955 * Generate an ISN_LOAD or similar instruction.
956 */
957 int
958generate_LOAD(
959 cctx_T *cctx,
960 isntype_T isn_type,
961 int idx,
962 char_u *name,
963 type_T *type)
964{
965 isn_T *isn;
966
967 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000968 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000969 return FAIL;
970 if (name != NULL)
971 isn->isn_arg.string = vim_strsave(name);
972 else
973 isn->isn_arg.number = idx;
974
975 return OK;
976}
977
978/*
979 * Generate an ISN_LOADOUTER instruction
980 */
981 int
982generate_LOADOUTER(
983 cctx_T *cctx,
984 int idx,
985 int nesting,
986 type_T *type)
987{
988 isn_T *isn;
989
990 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000991 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000992 return FAIL;
993 isn->isn_arg.outer.outer_idx = idx;
994 isn->isn_arg.outer.outer_depth = nesting;
995
996 return OK;
997}
998
999/*
1000 * Generate an ISN_LOADV instruction for v:var.
1001 */
1002 int
1003generate_LOADV(
1004 cctx_T *cctx,
1005 char_u *name,
1006 int error)
1007{
1008 int di_flags;
1009 int vidx = find_vim_var(name, &di_flags);
1010 type_T *type;
1011
1012 RETURN_OK_IF_SKIP(cctx);
1013 if (vidx < 0)
1014 {
1015 if (error)
1016 semsg(_(e_variable_not_found_str), name);
1017 return FAIL;
1018 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001019 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001020 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1021}
1022
1023/*
1024 * Generate an ISN_UNLET instruction.
1025 */
1026 int
1027generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1028{
1029 isn_T *isn;
1030
1031 RETURN_OK_IF_SKIP(cctx);
1032 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1033 return FAIL;
1034 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1035 isn->isn_arg.unlet.ul_forceit = forceit;
1036
1037 return OK;
1038}
1039
1040/*
1041 * Generate an ISN_LOCKCONST instruction.
1042 */
1043 int
1044generate_LOCKCONST(cctx_T *cctx)
1045{
1046 isn_T *isn;
1047
1048 RETURN_OK_IF_SKIP(cctx);
1049 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1050 return FAIL;
1051 return OK;
1052}
1053
1054/*
1055 * Generate an ISN_LOADS instruction.
1056 */
1057 int
1058generate_OLDSCRIPT(
1059 cctx_T *cctx,
1060 isntype_T isn_type,
1061 char_u *name,
1062 int sid,
1063 type_T *type)
1064{
1065 isn_T *isn;
1066
1067 RETURN_OK_IF_SKIP(cctx);
1068 if (isn_type == ISN_LOADS)
1069 isn = generate_instr_type(cctx, isn_type, type);
1070 else
1071 isn = generate_instr_drop(cctx, isn_type, 1);
1072 if (isn == NULL)
1073 return FAIL;
1074 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1075 isn->isn_arg.loadstore.ls_sid = sid;
1076
1077 return OK;
1078}
1079
1080/*
1081 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1082 */
1083 int
1084generate_VIM9SCRIPT(
1085 cctx_T *cctx,
1086 isntype_T isn_type,
1087 int sid,
1088 int idx,
1089 type_T *type)
1090{
1091 isn_T *isn;
1092 scriptref_T *sref;
1093 scriptitem_T *si = SCRIPT_ITEM(sid);
1094
1095 RETURN_OK_IF_SKIP(cctx);
1096 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001097 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001098 else
1099 isn = generate_instr_drop(cctx, isn_type, 1);
1100 if (isn == NULL)
1101 return FAIL;
1102
1103 // This requires three arguments, which doesn't fit in an instruction, thus
1104 // we need to allocate a struct for this.
1105 sref = ALLOC_ONE(scriptref_T);
1106 if (sref == NULL)
1107 return FAIL;
1108 isn->isn_arg.script.scriptref = sref;
1109 sref->sref_sid = sid;
1110 sref->sref_idx = idx;
1111 sref->sref_seq = si->sn_script_seq;
1112 sref->sref_type = type;
1113 return OK;
1114}
1115
1116/*
1117 * Generate an ISN_NEWLIST instruction.
1118 */
1119 int
1120generate_NEWLIST(cctx_T *cctx, int count)
1121{
1122 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001123 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001124 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001125 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001126
1127 RETURN_OK_IF_SKIP(cctx);
1128 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1129 return FAIL;
1130 isn->isn_arg.number = count;
1131
Bram Moolenaar078a4612022-01-04 15:17:03 +00001132 // Get the member type and the declared member type from all the items on
1133 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001134 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001135 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001136 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001137
1138 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001139 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001140
1141 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001142 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001143}
1144
1145/*
1146 * Generate an ISN_NEWDICT instruction.
1147 */
1148 int
1149generate_NEWDICT(cctx_T *cctx, int count)
1150{
1151 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001152 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001153 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001154 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001155
1156 RETURN_OK_IF_SKIP(cctx);
1157 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1158 return FAIL;
1159 isn->isn_arg.number = count;
1160
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001161 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001162 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001163 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001164
1165 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001166 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167
1168 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001169 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001170}
1171
1172/*
1173 * Generate an ISN_FUNCREF instruction.
1174 */
1175 int
1176generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1177{
1178 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001179 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001180
1181 RETURN_OK_IF_SKIP(cctx);
1182 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1183 return FAIL;
1184 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1185 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1186 else
1187 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1188 cctx->ctx_has_closure = 1;
1189
1190 // If the referenced function is a closure, it may use items further up in
1191 // the nested context, including this one.
1192 if (ufunc->uf_flags & FC_CLOSURE)
1193 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1194
Bram Moolenaar078a4612022-01-04 15:17:03 +00001195 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1196 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001197}
1198
1199/*
1200 * Generate an ISN_NEWFUNC instruction.
1201 * "lambda_name" and "func_name" must be in allocated memory and will be
1202 * consumed.
1203 */
1204 int
1205generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1206{
1207 isn_T *isn;
1208
1209 if (cctx->ctx_skip == SKIP_YES)
1210 {
1211 vim_free(lambda_name);
1212 vim_free(func_name);
1213 return OK;
1214 }
1215 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1216 {
1217 vim_free(lambda_name);
1218 vim_free(func_name);
1219 return FAIL;
1220 }
1221 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1222 isn->isn_arg.newfunc.nf_global = func_name;
1223
1224 return OK;
1225}
1226
1227/*
1228 * Generate an ISN_DEF instruction: list functions
1229 */
1230 int
1231generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1232{
1233 isn_T *isn;
1234
1235 RETURN_OK_IF_SKIP(cctx);
1236 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1237 return FAIL;
1238 if (len > 0)
1239 {
1240 isn->isn_arg.string = vim_strnsave(name, len);
1241 if (isn->isn_arg.string == NULL)
1242 return FAIL;
1243 }
1244 return OK;
1245}
1246
1247/*
1248 * Generate an ISN_JUMP instruction.
1249 */
1250 int
1251generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1252{
1253 isn_T *isn;
1254 garray_T *stack = &cctx->ctx_type_stack;
1255
1256 RETURN_OK_IF_SKIP(cctx);
1257 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1258 return FAIL;
1259 isn->isn_arg.jump.jump_when = when;
1260 isn->isn_arg.jump.jump_where = where;
1261
1262 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1263 --stack->ga_len;
1264
1265 return OK;
1266}
1267
1268/*
1269 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1270 */
1271 int
1272generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1273{
1274 isn_T *isn;
1275
1276 RETURN_OK_IF_SKIP(cctx);
1277 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1278 return FAIL;
1279 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1280 // jump_where is set later
1281 return OK;
1282}
1283
1284 int
1285generate_FOR(cctx_T *cctx, int loop_idx)
1286{
1287 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001288
1289 RETURN_OK_IF_SKIP(cctx);
1290 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1291 return FAIL;
1292 isn->isn_arg.forloop.for_idx = loop_idx;
1293
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001295 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001296}
1297/*
1298 * Generate an ISN_TRYCONT instruction.
1299 */
1300 int
1301generate_TRYCONT(cctx_T *cctx, int levels, int where)
1302{
1303 isn_T *isn;
1304
1305 RETURN_OK_IF_SKIP(cctx);
1306 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1307 return FAIL;
1308 isn->isn_arg.trycont.tct_levels = levels;
1309 isn->isn_arg.trycont.tct_where = where;
1310
1311 return OK;
1312}
1313
1314
1315/*
1316 * Generate an ISN_BCALL instruction.
1317 * "method_call" is TRUE for "value->method()"
1318 * Return FAIL if the number of arguments is wrong.
1319 */
1320 int
1321generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1322{
1323 isn_T *isn;
1324 garray_T *stack = &cctx->ctx_type_stack;
1325 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001326 type2_T *typep;
1327 type2_T *argtypes = NULL;
1328 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1329 type2_T *maptype = NULL;
1330 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001331 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332
1333 RETURN_OK_IF_SKIP(cctx);
1334 argoff = check_internal_func(func_idx, argcount);
1335 if (argoff < 0)
1336 return FAIL;
1337
1338 if (method_call && argoff > 1)
1339 {
1340 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1341 return FAIL;
1342 isn->isn_arg.shuffle.shfl_item = argcount;
1343 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1344 }
1345
1346 if (argcount > 0)
1347 {
1348 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001349 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001350 if (method_call && argoff > 1)
1351 {
1352 int i;
1353
1354 for (i = 0; i < argcount; ++i)
1355 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001356 ? typep[i + 1]
1357 : (i == argoff - 1) ? typep[0] : typep[i];
1358 argtypes = shuffled_argtypes;
1359 }
1360 else
1361 {
1362 int i;
1363
1364 for (i = 0; i < argcount; ++i)
1365 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001366 argtypes = shuffled_argtypes;
1367 }
1368 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1369 cctx) == FAIL)
1370 return FAIL;
1371 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001372 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001373 }
1374
1375 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1376 return FAIL;
1377 isn->isn_arg.bfunc.cbf_idx = func_idx;
1378 isn->isn_arg.bfunc.cbf_argcount = argcount;
1379
1380 // Drop the argument types and push the return type.
1381 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001382 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1383 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001384 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001385
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001386 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1387 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001388 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001389 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001390
1391 return OK;
1392}
1393
1394/*
1395 * Generate an ISN_LISTAPPEND instruction. Works like add().
1396 * Argument count is already checked.
1397 */
1398 int
1399generate_LISTAPPEND(cctx_T *cctx)
1400{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001401 type_T *list_type;
1402 type_T *item_type;
1403 type_T *expected;
1404
1405 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001406 // For checking the item type we use the declared type of the list and the
1407 // current type of the added item, adding a string to [1, 2] is OK.
1408 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001409 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001410 expected = list_type->tt_member;
1411 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1412 return FAIL;
1413
1414 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1415 return FAIL;
1416
Bram Moolenaar078a4612022-01-04 15:17:03 +00001417 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001418 return OK;
1419}
1420
1421/*
1422 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1423 * Argument count is already checked.
1424 */
1425 int
1426generate_BLOBAPPEND(cctx_T *cctx)
1427{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001428 type_T *item_type;
1429
1430 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001431 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001432 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1433 return FAIL;
1434
1435 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1436 return FAIL;
1437
Bram Moolenaar078a4612022-01-04 15:17:03 +00001438 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001439 return OK;
1440}
1441
1442/*
1443 * Generate an ISN_DCALL or ISN_UCALL instruction.
1444 * Return FAIL if the number of arguments is wrong.
1445 */
1446 int
1447generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1448{
1449 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001450 int regular_args = ufunc->uf_args.ga_len;
1451 int argcount = pushed_argcount;
1452
1453 RETURN_OK_IF_SKIP(cctx);
1454 if (argcount > regular_args && !has_varargs(ufunc))
1455 {
1456 semsg(_(e_too_many_arguments_for_function_str),
1457 printable_func_name(ufunc));
1458 return FAIL;
1459 }
1460 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1461 {
1462 semsg(_(e_not_enough_arguments_for_function_str),
1463 printable_func_name(ufunc));
1464 return FAIL;
1465 }
1466
1467 if (ufunc->uf_def_status != UF_NOT_COMPILED
1468 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1469 {
1470 int i;
1471
1472 for (i = 0; i < argcount; ++i)
1473 {
1474 type_T *expected;
1475 type_T *actual;
1476
Bram Moolenaar078a4612022-01-04 15:17:03 +00001477 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001478 if (actual == &t_special
1479 && i >= regular_args - ufunc->uf_def_args.ga_len)
1480 {
1481 // assume v:none used for default argument value
1482 continue;
1483 }
1484 if (i < regular_args)
1485 {
1486 if (ufunc->uf_arg_types == NULL)
1487 continue;
1488 expected = ufunc->uf_arg_types[i];
1489 }
1490 else if (ufunc->uf_va_type == NULL
1491 || ufunc->uf_va_type == &t_list_any)
1492 // possibly a lambda or "...: any"
1493 expected = &t_any;
1494 else
1495 expected = ufunc->uf_va_type->tt_member;
1496 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1497 TRUE, FALSE) == FAIL)
1498 {
1499 arg_type_mismatch(expected, actual, i + 1);
1500 return FAIL;
1501 }
1502 }
1503 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
1504 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1505 COMPILE_TYPE(ufunc), NULL) == FAIL)
1506 return FAIL;
1507 }
1508 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1509 {
1510 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1511 ufunc->uf_name);
1512 return FAIL;
1513 }
1514
1515 if ((isn = generate_instr(cctx,
1516 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1517 : ISN_UCALL)) == NULL)
1518 return FAIL;
1519 if (isn->isn_type == ISN_DCALL)
1520 {
1521 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1522 isn->isn_arg.dfunc.cdf_argcount = argcount;
1523 }
1524 else
1525 {
1526 // A user function may be deleted and redefined later, can't use the
1527 // ufunc pointer, need to look it up again at runtime.
1528 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1529 isn->isn_arg.ufunc.cuf_argcount = argcount;
1530 }
1531
Bram Moolenaar078a4612022-01-04 15:17:03 +00001532 // drop the argument types
1533 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001534
Bram Moolenaar078a4612022-01-04 15:17:03 +00001535 // add return type
1536 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001537}
1538
1539/*
1540 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1541 */
1542 int
1543generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1544{
1545 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001546
1547 RETURN_OK_IF_SKIP(cctx);
1548 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1549 return FAIL;
1550 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1551 isn->isn_arg.ufunc.cuf_argcount = argcount;
1552
Bram Moolenaar078a4612022-01-04 15:17:03 +00001553 // drop the argument types
1554 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001555
Bram Moolenaar078a4612022-01-04 15:17:03 +00001556 // add return value
1557 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001558}
1559
1560/*
1561 * Generate an ISN_PCALL instruction.
1562 * "type" is the type of the FuncRef.
1563 */
1564 int
1565generate_PCALL(
1566 cctx_T *cctx,
1567 int argcount,
1568 char_u *name,
1569 type_T *type,
1570 int at_top)
1571{
1572 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001573 type_T *ret_type;
1574
1575 RETURN_OK_IF_SKIP(cctx);
1576
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001577 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578 ret_type = &t_any;
1579 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1580 {
1581 if (type->tt_argcount != -1)
1582 {
1583 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1584
1585 if (argcount < type->tt_min_argcount - varargs)
1586 {
1587 semsg(_(e_not_enough_arguments_for_function_str), name);
1588 return FAIL;
1589 }
1590 if (!varargs && argcount > type->tt_argcount)
1591 {
1592 semsg(_(e_too_many_arguments_for_function_str), name);
1593 return FAIL;
1594 }
1595 if (type->tt_args != NULL)
1596 {
1597 int i;
1598
1599 for (i = 0; i < argcount; ++i)
1600 {
1601 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001602 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001603 type_T *expected;
1604
1605 if (varargs && i >= type->tt_argcount - 1)
1606 expected = type->tt_args[
1607 type->tt_argcount - 1]->tt_member;
1608 else if (i >= type->tt_min_argcount
1609 && actual == &t_special)
1610 expected = &t_any;
1611 else
1612 expected = type->tt_args[i];
1613 if (need_type(actual, expected, offset, i + 1,
1614 cctx, TRUE, FALSE) == FAIL)
1615 {
1616 arg_type_mismatch(expected, actual, i + 1);
1617 return FAIL;
1618 }
1619 }
1620 }
1621 }
1622 ret_type = type->tt_member;
1623 if (ret_type == &t_unknown)
1624 // return type not known yet, use a runtime check
1625 ret_type = &t_any;
1626 }
1627 else
1628 {
1629 semsg(_(e_not_callable_type_str), name);
1630 return FAIL;
1631 }
1632
1633 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1634 return FAIL;
1635 isn->isn_arg.pfunc.cpf_top = at_top;
1636 isn->isn_arg.pfunc.cpf_argcount = argcount;
1637
Bram Moolenaar078a4612022-01-04 15:17:03 +00001638 // drop the arguments and the funcref/partial
1639 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001640
Bram Moolenaar078a4612022-01-04 15:17:03 +00001641 // push the return value
1642 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001643
1644 // If partial is above the arguments it must be cleared and replaced with
1645 // the return value.
1646 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1647 return FAIL;
1648
1649 return OK;
1650}
1651
1652/*
1653 * Generate an ISN_STRINGMEMBER instruction.
1654 */
1655 int
1656generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1657{
1658 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001659 type_T *type;
1660
1661 RETURN_OK_IF_SKIP(cctx);
1662 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1663 return FAIL;
1664 isn->isn_arg.string = vim_strnsave(name, len);
1665
1666 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001667 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001668 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001669 {
1670 char *tofree;
1671
1672 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1673 name, type_name(type, &tofree));
1674 vim_free(tofree);
1675 return FAIL;
1676 }
1677 // change dict type to dict member type
1678 if (type->tt_type == VAR_DICT)
1679 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001680 type_T *ntype = type->tt_member == &t_unknown
1681 ? &t_any : type->tt_member;
1682 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001683 }
1684
1685 return OK;
1686}
1687
1688/*
1689 * Generate an ISN_ECHO instruction.
1690 */
1691 int
1692generate_ECHO(cctx_T *cctx, int with_white, int count)
1693{
1694 isn_T *isn;
1695
1696 RETURN_OK_IF_SKIP(cctx);
1697 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1698 return FAIL;
1699 isn->isn_arg.echo.echo_with_white = with_white;
1700 isn->isn_arg.echo.echo_count = count;
1701
1702 return OK;
1703}
1704
1705/*
1706 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1707 */
1708 int
1709generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1710{
1711 isn_T *isn;
1712
1713 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1714 return FAIL;
1715 isn->isn_arg.number = count;
1716
1717 return OK;
1718}
1719
1720/*
1721 * Generate an ISN_PUT instruction.
1722 */
1723 int
1724generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1725{
1726 isn_T *isn;
1727
1728 RETURN_OK_IF_SKIP(cctx);
1729 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1730 return FAIL;
1731 isn->isn_arg.put.put_regname = regname;
1732 isn->isn_arg.put.put_lnum = lnum;
1733 return OK;
1734}
1735
1736/*
1737 * Generate an EXEC instruction that takes a string argument.
1738 * A copy is made of "line".
1739 */
1740 int
1741generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1742{
1743 isn_T *isn;
1744
1745 RETURN_OK_IF_SKIP(cctx);
1746 if ((isn = generate_instr(cctx, isntype)) == NULL)
1747 return FAIL;
1748 isn->isn_arg.string = vim_strsave(line);
1749 return OK;
1750}
1751
1752/*
1753 * Generate an EXEC instruction that takes a string argument.
1754 * "str" must be allocated, it is consumed.
1755 */
1756 int
1757generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1758{
1759 isn_T *isn;
1760
1761 if (cctx->ctx_skip == SKIP_YES)
1762 {
1763 vim_free(str);
1764 return OK;
1765 }
1766 if ((isn = generate_instr(cctx, isntype)) == NULL)
1767 {
1768 vim_free(str);
1769 return FAIL;
1770 }
1771 isn->isn_arg.string = str;
1772 return OK;
1773}
1774
1775 int
1776generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1777{
1778 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001779
1780 RETURN_OK_IF_SKIP(cctx);
1781 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1782 return FAIL;
1783 isn->isn_arg.string = vim_strsave(line);
1784
Bram Moolenaar078a4612022-01-04 15:17:03 +00001785 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001786}
1787
1788 int
1789generate_EXECCONCAT(cctx_T *cctx, int count)
1790{
1791 isn_T *isn;
1792
1793 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1794 return FAIL;
1795 isn->isn_arg.number = count;
1796 return OK;
1797}
1798
1799/*
1800 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1801 */
1802 int
1803generate_RANGE(cctx_T *cctx, char_u *range)
1804{
1805 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001806
1807 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1808 return FAIL;
1809 isn->isn_arg.string = range;
1810
Bram Moolenaar078a4612022-01-04 15:17:03 +00001811 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001812}
1813
1814 int
1815generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1816{
1817 isn_T *isn;
1818
1819 RETURN_OK_IF_SKIP(cctx);
1820 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1821 return FAIL;
1822 isn->isn_arg.unpack.unp_count = var_count;
1823 isn->isn_arg.unpack.unp_semicolon = semicolon;
1824 return OK;
1825}
1826
1827/*
1828 * Generate an instruction for any command modifiers.
1829 */
1830 int
1831generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1832{
1833 isn_T *isn;
1834
1835 if (has_cmdmod(cmod, FALSE))
1836 {
1837 cctx->ctx_has_cmdmod = TRUE;
1838
1839 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1840 return FAIL;
1841 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1842 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1843 return FAIL;
1844 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1845 // filter program now belongs to the instruction
1846 cmod->cmod_filter_regmatch.regprog = NULL;
1847 }
1848
1849 return OK;
1850}
1851
1852 int
1853generate_undo_cmdmods(cctx_T *cctx)
1854{
1855 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1856 return FAIL;
1857 cctx->ctx_has_cmdmod = FALSE;
1858 return OK;
1859}
1860
1861/*
1862 * Generate a STORE instruction for "dest", not being "dest_local".
1863 * Return FAIL when out of memory.
1864 */
1865 int
1866generate_store_var(
1867 cctx_T *cctx,
1868 assign_dest_T dest,
1869 int opt_flags,
1870 int vimvaridx,
1871 int scriptvar_idx,
1872 int scriptvar_sid,
1873 type_T *type,
1874 char_u *name)
1875{
1876 switch (dest)
1877 {
1878 case dest_option:
1879 return generate_STOREOPT(cctx, ISN_STOREOPT,
1880 skip_option_env_lead(name), opt_flags);
1881 case dest_func_option:
1882 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1883 skip_option_env_lead(name), opt_flags);
1884 case dest_global:
1885 // include g: with the name, easier to execute that way
1886 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1887 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1888 case dest_buffer:
1889 // include b: with the name, easier to execute that way
1890 return generate_STORE(cctx, ISN_STOREB, 0, name);
1891 case dest_window:
1892 // include w: with the name, easier to execute that way
1893 return generate_STORE(cctx, ISN_STOREW, 0, name);
1894 case dest_tab:
1895 // include t: with the name, easier to execute that way
1896 return generate_STORE(cctx, ISN_STORET, 0, name);
1897 case dest_env:
1898 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1899 case dest_reg:
1900 return generate_STORE(cctx, ISN_STOREREG,
1901 name[1] == '@' ? '"' : name[1], NULL);
1902 case dest_vimvar:
1903 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1904 case dest_script:
1905 if (scriptvar_idx < 0)
1906 // "s:" may be included in the name.
1907 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1908 scriptvar_sid, type);
1909 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1910 scriptvar_sid, scriptvar_idx, type);
1911 case dest_local:
1912 case dest_expr:
1913 // cannot happen
1914 break;
1915 }
1916 return FAIL;
1917}
1918
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001919/*
1920 * Return TRUE when inside a "for" or "while" loop.
1921 */
1922 int
1923inside_loop_scope(cctx_T *cctx)
1924{
1925 scope_T *scope = cctx->ctx_scope;
1926
1927 for (;;)
1928 {
1929 if (scope == NULL)
1930 break;
1931 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1932 return TRUE;
1933 scope = scope->se_outer;
1934 }
1935 return FALSE;
1936}
1937
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001939generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001940{
1941 if (lhs->lhs_dest != dest_local)
1942 return generate_store_var(cctx, lhs->lhs_dest,
1943 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1944 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1945 lhs->lhs_type, lhs->lhs_name);
1946
1947 if (lhs->lhs_lvar != NULL)
1948 {
1949 garray_T *instr = &cctx->ctx_instr;
1950 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1951
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001952 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1953 // ISN_STORENR.
1954 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955 if (lhs->lhs_lvar->lv_from_outer == 0
1956 && instr->ga_len == instr_count + 1
1957 && isn->isn_type == ISN_PUSHNR)
1958 {
1959 varnumber_T val = isn->isn_arg.number;
1960 garray_T *stack = &cctx->ctx_type_stack;
1961
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001962 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001963 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001964 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001965 --instr->ga_len;
1966 }
1967 else
1968 {
1969 isn->isn_type = ISN_STORENR;
1970 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1971 isn->isn_arg.storenr.stnr_val = val;
1972 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001973 if (stack->ga_len > 0)
1974 --stack->ga_len;
1975 }
1976 else if (lhs->lhs_lvar->lv_from_outer > 0)
1977 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1978 lhs->lhs_lvar->lv_from_outer);
1979 else
1980 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
1981 }
1982 return OK;
1983}
1984
1985#if defined(FEAT_PROFILE) || defined(PROTO)
1986 void
1987may_generate_prof_end(cctx_T *cctx, int prof_lnum)
1988{
1989 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
1990 generate_instr(cctx, ISN_PROF_END);
1991}
1992#endif
1993
1994
1995/*
1996 * Delete an instruction, free what it contains.
1997 */
1998 void
1999delete_instr(isn_T *isn)
2000{
2001 switch (isn->isn_type)
2002 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002003 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002004 case ISN_DEF:
2005 case ISN_EXEC:
2006 case ISN_EXECRANGE:
2007 case ISN_EXEC_SPLIT:
2008 case ISN_LEGACY_EVAL:
2009 case ISN_LOADAUTO:
2010 case ISN_LOADB:
2011 case ISN_LOADENV:
2012 case ISN_LOADG:
2013 case ISN_LOADOPT:
2014 case ISN_LOADT:
2015 case ISN_LOADW:
2016 case ISN_LOCKUNLOCK:
2017 case ISN_PUSHEXC:
2018 case ISN_PUSHFUNC:
2019 case ISN_PUSHS:
2020 case ISN_RANGE:
2021 case ISN_STOREAUTO:
2022 case ISN_STOREB:
2023 case ISN_STOREENV:
2024 case ISN_STOREG:
2025 case ISN_STORET:
2026 case ISN_STOREW:
2027 case ISN_STRINGMEMBER:
2028 vim_free(isn->isn_arg.string);
2029 break;
2030
2031 case ISN_SUBSTITUTE:
2032 {
2033 int idx;
2034 isn_T *list = isn->isn_arg.subs.subs_instr;
2035
2036 vim_free(isn->isn_arg.subs.subs_cmd);
2037 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2038 delete_instr(list + idx);
2039 vim_free(list);
2040 }
2041 break;
2042
2043 case ISN_INSTR:
2044 {
2045 int idx;
2046 isn_T *list = isn->isn_arg.instr;
2047
2048 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2049 delete_instr(list + idx);
2050 vim_free(list);
2051 }
2052 break;
2053
2054 case ISN_LOADS:
2055 case ISN_STORES:
2056 vim_free(isn->isn_arg.loadstore.ls_name);
2057 break;
2058
2059 case ISN_UNLET:
2060 case ISN_UNLETENV:
2061 vim_free(isn->isn_arg.unlet.ul_name);
2062 break;
2063
2064 case ISN_STOREOPT:
2065 case ISN_STOREFUNCOPT:
2066 vim_free(isn->isn_arg.storeopt.so_name);
2067 break;
2068
2069 case ISN_PUSHBLOB: // push blob isn_arg.blob
2070 blob_unref(isn->isn_arg.blob);
2071 break;
2072
2073 case ISN_PUSHJOB:
2074#ifdef FEAT_JOB_CHANNEL
2075 job_unref(isn->isn_arg.job);
2076#endif
2077 break;
2078
2079 case ISN_PUSHCHANNEL:
2080#ifdef FEAT_JOB_CHANNEL
2081 channel_unref(isn->isn_arg.channel);
2082#endif
2083 break;
2084
2085 case ISN_UCALL:
2086 vim_free(isn->isn_arg.ufunc.cuf_name);
2087 break;
2088
2089 case ISN_FUNCREF:
2090 {
2091 if (isn->isn_arg.funcref.fr_func_name == NULL)
2092 {
2093 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2094 + isn->isn_arg.funcref.fr_dfunc_idx;
2095 ufunc_T *ufunc = dfunc->df_ufunc;
2096
2097 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2098 func_ptr_unref(ufunc);
2099 }
2100 else
2101 {
2102 char_u *name = isn->isn_arg.funcref.fr_func_name;
2103
2104 if (name != NULL)
2105 func_unref(name);
2106 vim_free(isn->isn_arg.funcref.fr_func_name);
2107 }
2108 }
2109 break;
2110
2111 case ISN_DCALL:
2112 {
2113 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2114 + isn->isn_arg.dfunc.cdf_idx;
2115
2116 if (dfunc->df_ufunc != NULL
2117 && func_name_refcount(dfunc->df_ufunc->uf_name))
2118 func_ptr_unref(dfunc->df_ufunc);
2119 }
2120 break;
2121
2122 case ISN_NEWFUNC:
2123 {
2124 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002125 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002126
2127 if (ufunc != NULL)
2128 {
2129 unlink_def_function(ufunc);
2130 func_ptr_unref(ufunc);
2131 }
2132
2133 vim_free(lambda);
2134 vim_free(isn->isn_arg.newfunc.nf_global);
2135 }
2136 break;
2137
2138 case ISN_CHECKTYPE:
2139 case ISN_SETTYPE:
2140 free_type(isn->isn_arg.type.ct_type);
2141 break;
2142
2143 case ISN_CMDMOD:
2144 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2145 ->cmod_filter_regmatch.regprog);
2146 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2147 break;
2148
2149 case ISN_LOADSCRIPT:
2150 case ISN_STORESCRIPT:
2151 vim_free(isn->isn_arg.script.scriptref);
2152 break;
2153
2154 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002155 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002156 break;
2157
2158 case ISN_CEXPR_CORE:
2159 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2160 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2161 break;
2162
2163 case ISN_2BOOL:
2164 case ISN_2STRING:
2165 case ISN_2STRING_ANY:
2166 case ISN_ADDBLOB:
2167 case ISN_ADDLIST:
2168 case ISN_ANYINDEX:
2169 case ISN_ANYSLICE:
2170 case ISN_BCALL:
2171 case ISN_BLOBAPPEND:
2172 case ISN_BLOBINDEX:
2173 case ISN_BLOBSLICE:
2174 case ISN_CATCH:
2175 case ISN_CEXPR_AUCMD:
2176 case ISN_CHECKLEN:
2177 case ISN_CHECKNR:
2178 case ISN_CLEARDICT:
2179 case ISN_CMDMOD_REV:
2180 case ISN_COMPAREANY:
2181 case ISN_COMPAREBLOB:
2182 case ISN_COMPAREBOOL:
2183 case ISN_COMPAREDICT:
2184 case ISN_COMPAREFLOAT:
2185 case ISN_COMPAREFUNC:
2186 case ISN_COMPARELIST:
2187 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002188 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002189 case ISN_COMPARESPECIAL:
2190 case ISN_COMPARESTRING:
2191 case ISN_CONCAT:
2192 case ISN_COND2BOOL:
2193 case ISN_DEBUG:
2194 case ISN_DROP:
2195 case ISN_ECHO:
2196 case ISN_ECHOCONSOLE:
2197 case ISN_ECHOERR:
2198 case ISN_ECHOMSG:
2199 case ISN_ENDTRY:
2200 case ISN_EXECCONCAT:
2201 case ISN_EXECUTE:
2202 case ISN_FINALLY:
2203 case ISN_FINISH:
2204 case ISN_FOR:
2205 case ISN_GETITEM:
2206 case ISN_JUMP:
2207 case ISN_JUMP_IF_ARG_SET:
2208 case ISN_LISTAPPEND:
2209 case ISN_LISTINDEX:
2210 case ISN_LISTSLICE:
2211 case ISN_LOAD:
2212 case ISN_LOADBDICT:
2213 case ISN_LOADGDICT:
2214 case ISN_LOADOUTER:
2215 case ISN_LOADREG:
2216 case ISN_LOADTDICT:
2217 case ISN_LOADV:
2218 case ISN_LOADWDICT:
2219 case ISN_LOCKCONST:
2220 case ISN_MEMBER:
2221 case ISN_NEGATENR:
2222 case ISN_NEWDICT:
2223 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002224 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002225 case ISN_OPANY:
2226 case ISN_OPFLOAT:
2227 case ISN_OPNR:
2228 case ISN_PCALL:
2229 case ISN_PCALL_END:
2230 case ISN_PROF_END:
2231 case ISN_PROF_START:
2232 case ISN_PUSHBOOL:
2233 case ISN_PUSHF:
2234 case ISN_PUSHNR:
2235 case ISN_PUSHSPEC:
2236 case ISN_PUT:
2237 case ISN_REDIREND:
2238 case ISN_REDIRSTART:
2239 case ISN_RETURN:
2240 case ISN_RETURN_VOID:
2241 case ISN_SHUFFLE:
2242 case ISN_SLICE:
2243 case ISN_STORE:
2244 case ISN_STOREINDEX:
2245 case ISN_STORENR:
2246 case ISN_STOREOUTER:
2247 case ISN_STORERANGE:
2248 case ISN_STOREREG:
2249 case ISN_STOREV:
2250 case ISN_STRINDEX:
2251 case ISN_STRSLICE:
2252 case ISN_THROW:
2253 case ISN_TRYCONT:
2254 case ISN_UNLETINDEX:
2255 case ISN_UNLETRANGE:
2256 case ISN_UNPACK:
2257 case ISN_USEDICT:
2258 // nothing allocated
2259 break;
2260 }
2261}
2262
2263 void
2264clear_instr_ga(garray_T *gap)
2265{
2266 int idx;
2267
2268 for (idx = 0; idx < gap->ga_len; ++idx)
2269 delete_instr(((isn_T *)gap->ga_data) + idx);
2270 ga_clear(gap);
2271}
2272
2273
2274#endif // defined(FEAT_EVAL)