blob: 11e39f131ba4151c69abf43ad3a9d64e18881022 [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/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000342 * Get the instruction to use for comparing two values with specified types.
343 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000344 * Return ISN_DROP when failed.
345 */
346 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000347get_compare_isn(
348 exprtype_T exprtype,
349 typval_T *tv1,
350 typval_T *tv2,
351 type_T *type1,
352 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000353{
354 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000355 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
356 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000357
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000358 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000359 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000360 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000361 {
362 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
363 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
364 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
365 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
366 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
367 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
368 case VAR_LIST: isntype = ISN_COMPARELIST; break;
369 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
370 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
371 default: isntype = ISN_COMPAREANY; break;
372 }
373 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000374 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
375 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
376 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
377 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
378 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000380 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000381 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000382 if ((vartype1 == VAR_SPECIAL
383 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
384 : type1 == &t_none)
385 && vartype2 != VAR_STRING)
386 || (vartype2 == VAR_SPECIAL
387 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
388 : type2 == &t_none)
389 && vartype1 != VAR_STRING))
390 {
391 semsg(_(e_cannot_compare_str_with_str),
392 vartype_name(vartype1), vartype_name(vartype2));
393 return ISN_DROP;
394 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000395 // although comparing null with number, float or bool is not useful, we
396 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000397 isntype = ISN_COMPARENULL;
398 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000399
400 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
401 && (isntype == ISN_COMPAREBOOL
402 || isntype == ISN_COMPARESPECIAL
403 || isntype == ISN_COMPARENR
404 || isntype == ISN_COMPAREFLOAT))
405 {
406 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000407 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000408 return ISN_DROP;
409 }
410 if (isntype == ISN_DROP
411 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000412 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
413 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000414 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000415 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000416 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
417 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000418 {
419 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000420 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000421 return ISN_DROP;
422 }
423 return isntype;
424}
425
426 int
427check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
428{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000429 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000430 return FAIL;
431 return OK;
432}
433
434/*
435 * Generate an ISN_COMPARE* instruction with a boolean result.
436 */
437 int
438generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
439{
440 isntype_T isntype;
441 isn_T *isn;
442 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443
444 RETURN_OK_IF_SKIP(cctx);
445
446 // Get the known type of the two items on the stack. If they are matching
447 // use a type-specific instruction. Otherwise fall back to runtime type
448 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000449 isntype = get_compare_isn(exprtype, NULL, NULL,
450 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000451 if (isntype == ISN_DROP)
452 return FAIL;
453
454 if ((isn = generate_instr(cctx, isntype)) == NULL)
455 return FAIL;
456 isn->isn_arg.op.op_type = exprtype;
457 isn->isn_arg.op.op_ic = ic;
458
459 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100460 --stack->ga_len;
461 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000462
463 return OK;
464}
465
466/*
LemonBoy372bcce2022-04-25 12:43:20 +0100467 * Generate an ISN_CONCAT instruction.
468 * "count" is the number of stack elements to join together and it must be
469 * greater or equal to one.
470 * The caller ensures all the "count" elements on the stack have the right type.
471 */
472 int
473generate_CONCAT(cctx_T *cctx, int count)
474{
475 isn_T *isn;
476 garray_T *stack = &cctx->ctx_type_stack;
477
478 RETURN_OK_IF_SKIP(cctx);
479
LemonBoy372bcce2022-04-25 12:43:20 +0100480 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
481 return FAIL;
482 isn->isn_arg.number = count;
483
484 // drop the argument types
485 stack->ga_len -= count - 1;
486
487 return OK;
488}
489
490/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491 * Generate an ISN_2BOOL instruction.
492 * "offset" is the offset in the type stack.
493 */
494 int
495generate_2BOOL(cctx_T *cctx, int invert, int offset)
496{
497 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000498
499 RETURN_OK_IF_SKIP(cctx);
500 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
501 return FAIL;
502 isn->isn_arg.tobool.invert = invert;
503 isn->isn_arg.tobool.offset = offset;
504
505 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000506 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000507
508 return OK;
509}
510
511/*
512 * Generate an ISN_COND2BOOL instruction.
513 */
514 int
515generate_COND2BOOL(cctx_T *cctx)
516{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000517 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100518 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000519 return FAIL;
520
521 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000522 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000523
524 return OK;
525}
526
527 int
528generate_TYPECHECK(
529 cctx_T *cctx,
530 type_T *expected,
531 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100532 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000533 int argidx)
534{
535 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000536
537 RETURN_OK_IF_SKIP(cctx);
538 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
539 return FAIL;
540 isn->isn_arg.type.ct_type = alloc_type(expected);
541 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100542 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000543 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
544
545 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000546 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000547
548 return OK;
549}
550
551 int
552generate_SETTYPE(
553 cctx_T *cctx,
554 type_T *expected)
555{
556 isn_T *isn;
557
558 RETURN_OK_IF_SKIP(cctx);
559 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
560 return FAIL;
561 isn->isn_arg.type.ct_type = alloc_type(expected);
562 return OK;
563}
564
565/*
566 * Generate a PUSH instruction for "tv".
567 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568 */
569 int
570generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
571{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100572 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000573 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100574 case VAR_BOOL:
575 generate_PUSHBOOL(cctx, tv->vval.v_number);
576 break;
577 case VAR_SPECIAL:
578 generate_PUSHSPEC(cctx, tv->vval.v_number);
579 break;
580 case VAR_NUMBER:
581 generate_PUSHNR(cctx, tv->vval.v_number);
582 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000583#ifdef FEAT_FLOAT
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100584 case VAR_FLOAT:
585 generate_PUSHF(cctx, tv->vval.v_float);
586 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000587#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100588 case VAR_BLOB:
589 generate_PUSHBLOB(cctx, tv->vval.v_blob);
590 tv->vval.v_blob = NULL;
591 break;
592 case VAR_LIST:
593 if (tv->vval.v_list != NULL)
594 iemsg("non-empty list constant not supported");
595 generate_NEWLIST(cctx, 0, TRUE);
596 break;
597 case VAR_DICT:
598 if (tv->vval.v_dict != NULL)
599 iemsg("non-empty dict constant not supported");
600 generate_NEWDICT(cctx, 0, TRUE);
601 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000602#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100603 case VAR_JOB:
604 if (tv->vval.v_job != NULL)
605 iemsg("non-null job constant not supported");
606 generate_PUSHJOB(cctx);
607 break;
608 case VAR_CHANNEL:
609 if (tv->vval.v_channel != NULL)
610 iemsg("non-null channel constant not supported");
611 generate_PUSHCHANNEL(cctx);
612 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000613#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100614 case VAR_FUNC:
615 if (tv->vval.v_string != NULL)
616 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100617 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100618 break;
619 case VAR_PARTIAL:
620 if (tv->vval.v_partial != NULL)
621 iemsg("non-null partial constant not supported");
622 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
623 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000624 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100625 break;
626 case VAR_STRING:
627 generate_PUSHS(cctx, &tv->vval.v_string);
628 tv->vval.v_string = NULL;
629 break;
630 default:
631 siemsg("constant type %d not supported", tv->v_type);
632 clear_tv(tv);
633 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000634 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100635 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636 return OK;
637}
638
639/*
640 * Generate an ISN_PUSHNR instruction.
641 */
642 int
643generate_PUSHNR(cctx_T *cctx, varnumber_T number)
644{
645 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000646
647 RETURN_OK_IF_SKIP(cctx);
648 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
649 return FAIL;
650 isn->isn_arg.number = number;
651
652 if (number == 0 || number == 1)
653 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000654 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000655 return OK;
656}
657
658/*
659 * Generate an ISN_PUSHBOOL instruction.
660 */
661 int
662generate_PUSHBOOL(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_PUSHBOOL, &t_bool)) == NULL)
668 return FAIL;
669 isn->isn_arg.number = number;
670
671 return OK;
672}
673
674/*
675 * Generate an ISN_PUSHSPEC instruction.
676 */
677 int
678generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
679{
680 isn_T *isn;
681
682 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000683 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
684 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000685 return FAIL;
686 isn->isn_arg.number = number;
687
688 return OK;
689}
690
691#if defined(FEAT_FLOAT) || defined(PROTO)
692/*
693 * Generate an ISN_PUSHF instruction.
694 */
695 int
696generate_PUSHF(cctx_T *cctx, float_T fnumber)
697{
698 isn_T *isn;
699
700 RETURN_OK_IF_SKIP(cctx);
701 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
702 return FAIL;
703 isn->isn_arg.fnumber = fnumber;
704
705 return OK;
706}
707#endif
708
709/*
710 * Generate an ISN_PUSHS instruction.
711 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100712 * Note that if "str" is used in the instruction OK is returned and "*str" is
713 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000714 */
715 int
716generate_PUSHS(cctx_T *cctx, char_u **str)
717{
718 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100719 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000720
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100721 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000722 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100723 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
724 ret = FAIL;
725 else
726 {
727 isn->isn_arg.string = str == NULL ? NULL : *str;
728 return OK;
729 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100731 if (str != NULL)
732 VIM_CLEAR(*str);
733 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000734}
735
736/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000737 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000738 */
739 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000740generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000741{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100743#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100744 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100747#else
748 emsg(_(e_channel_job_feature_not_available));
749 return FAIL;
750#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000751}
752
753/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000754 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000755 */
756 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000757generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000758{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100760#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100761 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000762 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000763 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100764#else
765 emsg(_(e_channel_job_feature_not_available));
766 return FAIL;
767#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768}
769
770/*
771 * Generate an ISN_PUSHBLOB instruction.
772 * Consumes "blob".
773 */
774 int
775generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
776{
777 isn_T *isn;
778
779 RETURN_OK_IF_SKIP(cctx);
780 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
781 return FAIL;
782 isn->isn_arg.blob = blob;
783
784 return OK;
785}
786
787/*
788 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100789 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
790 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791 */
792 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100793generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000794{
795 isn_T *isn;
796 char_u *funcname;
797
798 RETURN_OK_IF_SKIP(cctx);
799 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
800 return FAIL;
801 if (name == NULL)
802 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100803 else if (!may_prefix
804 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000805 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806 funcname = vim_strsave(name);
807 else
808 {
809 funcname = alloc(STRLEN(name) + 3);
810 if (funcname != NULL)
811 {
812 STRCPY(funcname, "g:");
813 STRCPY(funcname + 2, name);
814 }
815 }
816
817 isn->isn_arg.string = funcname;
818 return OK;
819}
820
821/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000822 * Generate an ISN_AUTOLOAD instruction.
823 */
824 int
825generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
826{
827 isn_T *isn;
828
829 RETURN_OK_IF_SKIP(cctx);
830 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
831 return FAIL;
832 isn->isn_arg.string = vim_strsave(name);
833 if (isn->isn_arg.string == NULL)
834 return FAIL;
835 return OK;
836}
837
838/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000839 * Generate an ISN_GETITEM instruction with "index".
840 * "with_op" is TRUE for "+=" and other operators, the stack has the current
841 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100842 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000843 */
844 int
845generate_GETITEM(cctx_T *cctx, int index, int with_op)
846{
847 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000848 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000849 type_T *item_type = &t_any;
850
851 RETURN_OK_IF_SKIP(cctx);
852
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853 item_type = type->tt_member;
854 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
855 return FAIL;
856 isn->isn_arg.getitem.gi_index = index;
857 isn->isn_arg.getitem.gi_with_op = with_op;
858
859 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000860 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000861}
862
863/*
864 * Generate an ISN_SLICE instruction with "count".
865 */
866 int
867generate_SLICE(cctx_T *cctx, int count)
868{
869 isn_T *isn;
870
871 RETURN_OK_IF_SKIP(cctx);
872 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
873 return FAIL;
874 isn->isn_arg.number = count;
875 return OK;
876}
877
878/*
879 * Generate an ISN_CHECKLEN instruction with "min_len".
880 */
881 int
882generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
883{
884 isn_T *isn;
885
886 RETURN_OK_IF_SKIP(cctx);
887
888 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
889 return FAIL;
890 isn->isn_arg.checklen.cl_min_len = min_len;
891 isn->isn_arg.checklen.cl_more_OK = more_OK;
892
893 return OK;
894}
895
896/*
897 * Generate an ISN_STORE instruction.
898 */
899 int
900generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
901{
902 isn_T *isn;
903
904 RETURN_OK_IF_SKIP(cctx);
905 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
906 return FAIL;
907 if (name != NULL)
908 isn->isn_arg.string = vim_strsave(name);
909 else
910 isn->isn_arg.number = idx;
911
912 return OK;
913}
914
915/*
916 * Generate an ISN_STOREOUTER instruction.
917 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000918 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100919generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920{
921 isn_T *isn;
922
923 RETURN_OK_IF_SKIP(cctx);
924 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
925 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100926 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
927 {
928 // Store a variable defined in a loop. A copy will be made at the end
929 // of the loop. TODO: how about deeper nesting?
930 isn->isn_arg.outer.outer_idx = idx - loop_idx;
931 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
932 }
933 else
934 {
935 isn->isn_arg.outer.outer_idx = idx;
936 isn->isn_arg.outer.outer_depth = level;
937 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938
939 return OK;
940}
941
942/*
943 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
944 */
945 int
946generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
947{
948 isn_T *isn;
949
950 RETURN_OK_IF_SKIP(cctx);
951 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
952 return FAIL;
953 isn->isn_arg.storenr.stnr_idx = idx;
954 isn->isn_arg.storenr.stnr_val = value;
955
956 return OK;
957}
958
959/*
960 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
961 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000962 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000963generate_STOREOPT(
964 cctx_T *cctx,
965 isntype_T isn_type,
966 char_u *name,
967 int opt_flags)
968{
969 isn_T *isn;
970
971 RETURN_OK_IF_SKIP(cctx);
972 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
973 return FAIL;
974 isn->isn_arg.storeopt.so_name = vim_strsave(name);
975 isn->isn_arg.storeopt.so_flags = opt_flags;
976
977 return OK;
978}
979
980/*
981 * Generate an ISN_LOAD or similar instruction.
982 */
983 int
984generate_LOAD(
985 cctx_T *cctx,
986 isntype_T isn_type,
987 int idx,
988 char_u *name,
989 type_T *type)
990{
991 isn_T *isn;
992
993 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000994 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000995 return FAIL;
996 if (name != NULL)
997 isn->isn_arg.string = vim_strsave(name);
998 else
999 isn->isn_arg.number = idx;
1000
1001 return OK;
1002}
1003
1004/*
1005 * Generate an ISN_LOADOUTER instruction
1006 */
1007 int
1008generate_LOADOUTER(
1009 cctx_T *cctx,
1010 int idx,
1011 int nesting,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001012 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001013 type_T *type)
1014{
1015 isn_T *isn;
1016
1017 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001018 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001019 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001020 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1021 {
1022 // Load a variable defined in a loop. A copy will be made at the end
1023 // of the loop. TODO: how about deeper nesting?
1024 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1025 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1026 }
1027 else
1028 {
1029 isn->isn_arg.outer.outer_idx = idx;
1030 isn->isn_arg.outer.outer_depth = nesting;
1031 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001032
1033 return OK;
1034}
1035
1036/*
1037 * Generate an ISN_LOADV instruction for v:var.
1038 */
1039 int
1040generate_LOADV(
1041 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001042 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001043{
1044 int di_flags;
1045 int vidx = find_vim_var(name, &di_flags);
1046 type_T *type;
1047
1048 RETURN_OK_IF_SKIP(cctx);
1049 if (vidx < 0)
1050 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001051 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001052 return FAIL;
1053 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001054 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001055 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1056}
1057
1058/*
1059 * Generate an ISN_UNLET instruction.
1060 */
1061 int
1062generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1063{
1064 isn_T *isn;
1065
1066 RETURN_OK_IF_SKIP(cctx);
1067 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1068 return FAIL;
1069 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1070 isn->isn_arg.unlet.ul_forceit = forceit;
1071
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_LOCKCONST instruction.
1077 */
1078 int
1079generate_LOCKCONST(cctx_T *cctx)
1080{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001081 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001082 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001083 return FAIL;
1084 return OK;
1085}
1086
1087/*
1088 * Generate an ISN_LOADS instruction.
1089 */
1090 int
1091generate_OLDSCRIPT(
1092 cctx_T *cctx,
1093 isntype_T isn_type,
1094 char_u *name,
1095 int sid,
1096 type_T *type)
1097{
1098 isn_T *isn;
1099
1100 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001101 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001102 isn = generate_instr_type(cctx, isn_type, type);
1103 else
1104 isn = generate_instr_drop(cctx, isn_type, 1);
1105 if (isn == NULL)
1106 return FAIL;
1107 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1108 isn->isn_arg.loadstore.ls_sid = sid;
1109
1110 return OK;
1111}
1112
1113/*
1114 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1115 */
1116 int
1117generate_VIM9SCRIPT(
1118 cctx_T *cctx,
1119 isntype_T isn_type,
1120 int sid,
1121 int idx,
1122 type_T *type)
1123{
1124 isn_T *isn;
1125 scriptref_T *sref;
1126 scriptitem_T *si = SCRIPT_ITEM(sid);
1127
1128 RETURN_OK_IF_SKIP(cctx);
1129 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001130 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001131 else
1132 isn = generate_instr_drop(cctx, isn_type, 1);
1133 if (isn == NULL)
1134 return FAIL;
1135
1136 // This requires three arguments, which doesn't fit in an instruction, thus
1137 // we need to allocate a struct for this.
1138 sref = ALLOC_ONE(scriptref_T);
1139 if (sref == NULL)
1140 return FAIL;
1141 isn->isn_arg.script.scriptref = sref;
1142 sref->sref_sid = sid;
1143 sref->sref_idx = idx;
1144 sref->sref_seq = si->sn_script_seq;
1145 sref->sref_type = type;
1146 return OK;
1147}
1148
1149/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001150 * Generate an ISN_NEWLIST instruction for "count" items.
1151 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001152 */
1153 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001154generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001155{
1156 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001157 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001158 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001159 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001160
1161 RETURN_OK_IF_SKIP(cctx);
1162 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1163 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001164 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165
Bram Moolenaar078a4612022-01-04 15:17:03 +00001166 // Get the member type and the declared member type from all the items on
1167 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001168 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001169 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001170 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001171
1172 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001173 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001174
1175 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001176 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001177}
1178
1179/*
1180 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001181 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001182 */
1183 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001184generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001185{
1186 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001187 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001189 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190
1191 RETURN_OK_IF_SKIP(cctx);
1192 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1193 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001194 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001195
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001196 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001197 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001198 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001199
1200 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001201 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001202
1203 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001204 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001205}
1206
1207/*
1208 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001209 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001210 * If variables were declared inside a loop "loop_var_idx" is the index of the
1211 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001212 */
1213 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001214generate_FUNCREF(
1215 cctx_T *cctx,
1216 ufunc_T *ufunc,
1217 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001219 isn_T *isn;
1220 type_T *type;
1221 funcref_extra_T *extra;
1222 short loop_var_idx;
1223 short loop_var_count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001224
1225 RETURN_OK_IF_SKIP(cctx);
1226 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1227 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001228 if (isnp != NULL)
1229 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001230
1231 loop_var_count = get_loop_var_info(cctx, &loop_var_idx);
1232 if (ufunc->uf_def_status == UF_NOT_COMPILED || loop_var_count > 0)
1233 {
1234 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1235 if (extra == NULL)
1236 return FAIL;
1237 isn->isn_arg.funcref.fr_extra = extra;
1238 extra->fre_loop_var_idx = loop_var_idx;
1239 extra->fre_loop_var_count = loop_var_count;
1240 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001241 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001242 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001243 else
1244 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1245 cctx->ctx_has_closure = 1;
1246
1247 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001248 // the nested context, including this one. But not a function defined at
1249 // the script level.
1250 if ((ufunc->uf_flags & FC_CLOSURE)
1251 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001252 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1253
Bram Moolenaar078a4612022-01-04 15:17:03 +00001254 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1255 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001256}
1257
1258/*
1259 * Generate an ISN_NEWFUNC instruction.
1260 * "lambda_name" and "func_name" must be in allocated memory and will be
1261 * consumed.
1262 */
1263 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001264generate_NEWFUNC(
1265 cctx_T *cctx,
1266 char_u *lambda_name,
1267 char_u *func_name,
1268 short loop_var_idx,
1269 short loop_var_count)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001270{
1271 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001272 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001273
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001274 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001275 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001276 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1277 ret = FAIL;
1278 else
1279 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001280 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1281
1282 if (arg == NULL)
1283 ret = FAIL;
1284 else
1285 {
1286 isn->isn_arg.newfunc.nf_arg = arg;
1287 arg->nfa_lambda = lambda_name;
1288 arg->nfa_global = func_name;
1289 arg->nfa_loop_var_idx = loop_var_idx;
1290 arg->nfa_loop_var_count = loop_var_count;
1291 return OK;
1292 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001293 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001295 vim_free(lambda_name);
1296 vim_free(func_name);
1297 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001298}
1299
1300/*
1301 * Generate an ISN_DEF instruction: list functions
1302 */
1303 int
1304generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1305{
1306 isn_T *isn;
1307
1308 RETURN_OK_IF_SKIP(cctx);
1309 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1310 return FAIL;
1311 if (len > 0)
1312 {
1313 isn->isn_arg.string = vim_strnsave(name, len);
1314 if (isn->isn_arg.string == NULL)
1315 return FAIL;
1316 }
1317 return OK;
1318}
1319
1320/*
1321 * Generate an ISN_JUMP instruction.
1322 */
1323 int
1324generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1325{
1326 isn_T *isn;
1327 garray_T *stack = &cctx->ctx_type_stack;
1328
1329 RETURN_OK_IF_SKIP(cctx);
1330 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1331 return FAIL;
1332 isn->isn_arg.jump.jump_when = when;
1333 isn->isn_arg.jump.jump_where = where;
1334
1335 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1336 --stack->ga_len;
1337
1338 return OK;
1339}
1340
1341/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001342 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1343 */
1344 int
1345generate_WHILE(cctx_T *cctx, int funcref_idx)
1346{
1347 isn_T *isn;
1348 garray_T *stack = &cctx->ctx_type_stack;
1349
1350 RETURN_OK_IF_SKIP(cctx);
1351 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1352 return FAIL;
1353 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1354 isn->isn_arg.whileloop.while_end = 0; // filled in later
1355
1356 if (stack->ga_len > 0)
1357 --stack->ga_len;
1358
1359 return OK;
1360}
1361
1362/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001363 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1364 */
1365 int
1366generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1367{
1368 isn_T *isn;
1369
1370 RETURN_OK_IF_SKIP(cctx);
1371 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1372 return FAIL;
1373 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1374 // jump_where is set later
1375 return OK;
1376}
1377
1378 int
1379generate_FOR(cctx_T *cctx, int loop_idx)
1380{
1381 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382
1383 RETURN_OK_IF_SKIP(cctx);
1384 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1385 return FAIL;
1386 isn->isn_arg.forloop.for_idx = loop_idx;
1387
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001388 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001389 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001390}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001391
1392 int
1393generate_ENDLOOP(
1394 cctx_T *cctx,
1395 int funcref_idx,
1396 int prev_local_count)
1397{
1398 isn_T *isn;
1399
1400 RETURN_OK_IF_SKIP(cctx);
1401 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1402 return FAIL;
1403 isn->isn_arg.endloop.end_funcref_idx = funcref_idx;
1404 isn->isn_arg.endloop.end_var_idx = prev_local_count;
1405 isn->isn_arg.endloop.end_var_count =
1406 cctx->ctx_locals.ga_len - prev_local_count;
1407 return OK;
1408}
1409
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001410/*
1411 * Generate an ISN_TRYCONT instruction.
1412 */
1413 int
1414generate_TRYCONT(cctx_T *cctx, int levels, int where)
1415{
1416 isn_T *isn;
1417
1418 RETURN_OK_IF_SKIP(cctx);
1419 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1420 return FAIL;
1421 isn->isn_arg.trycont.tct_levels = levels;
1422 isn->isn_arg.trycont.tct_where = where;
1423
1424 return OK;
1425}
1426
Bram Moolenaar16900322022-09-08 19:51:45 +01001427/*
1428 * Check "argount" arguments and their types on the type stack.
1429 * Give an error and return FAIL if something is wrong.
1430 * When "method_call" is NULL no code is generated.
1431 */
1432 int
1433check_internal_func_args(
1434 cctx_T *cctx,
1435 int func_idx,
1436 int argcount,
1437 int method_call,
1438 type2_T **argtypes,
1439 type2_T *shuffled_argtypes)
1440{
1441 garray_T *stack = &cctx->ctx_type_stack;
1442 int argoff = check_internal_func(func_idx, argcount);
1443
1444 if (argoff < 0)
1445 return FAIL;
1446
1447 if (method_call && argoff > 1)
1448 {
1449 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1450
1451 if (isn == NULL)
1452 return FAIL;
1453 isn->isn_arg.shuffle.shfl_item = argcount;
1454 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1455 }
1456
1457 if (argcount > 0)
1458 {
1459 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1460
1461 // Check the types of the arguments.
1462 if (method_call && argoff > 1)
1463 {
1464 int i;
1465
1466 for (i = 0; i < argcount; ++i)
1467 shuffled_argtypes[i] = (i < argoff - 1)
1468 ? typep[i + 1]
1469 : (i == argoff - 1) ? typep[0] : typep[i];
1470 *argtypes = shuffled_argtypes;
1471 }
1472 else
1473 {
1474 int i;
1475
1476 for (i = 0; i < argcount; ++i)
1477 shuffled_argtypes[i] = typep[i];
1478 *argtypes = shuffled_argtypes;
1479 }
1480 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1481 cctx) == FAIL)
1482 return FAIL;
1483 }
1484 return OK;
1485}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001486
1487/*
1488 * Generate an ISN_BCALL instruction.
1489 * "method_call" is TRUE for "value->method()"
1490 * Return FAIL if the number of arguments is wrong.
1491 */
1492 int
1493generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1494{
1495 isn_T *isn;
1496 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001497 type2_T *argtypes = NULL;
1498 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1499 type2_T *maptype = NULL;
1500 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001501 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001502
1503 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001504
1505 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1506 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001507 return FAIL;
1508
Bram Moolenaar16900322022-09-08 19:51:45 +01001509 if (internal_func_is_map(func_idx))
1510 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001511
1512 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1513 return FAIL;
1514 isn->isn_arg.bfunc.cbf_idx = func_idx;
1515 isn->isn_arg.bfunc.cbf_argcount = argcount;
1516
1517 // Drop the argument types and push the return type.
1518 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001519 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1520 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001521 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001522
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001523 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1524 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001525 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001526 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001527
1528 return OK;
1529}
1530
1531/*
1532 * Generate an ISN_LISTAPPEND instruction. Works like add().
1533 * Argument count is already checked.
1534 */
1535 int
1536generate_LISTAPPEND(cctx_T *cctx)
1537{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001538 type_T *list_type;
1539 type_T *item_type;
1540 type_T *expected;
1541
1542 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001543 // For checking the item type we use the declared type of the list and the
1544 // current type of the added item, adding a string to [1, 2] is OK.
1545 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001546 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001547 expected = list_type->tt_member;
1548 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1549 return FAIL;
1550
1551 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1552 return FAIL;
1553
Bram Moolenaar078a4612022-01-04 15:17:03 +00001554 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001555 return OK;
1556}
1557
1558/*
1559 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1560 * Argument count is already checked.
1561 */
1562 int
1563generate_BLOBAPPEND(cctx_T *cctx)
1564{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001565 type_T *item_type;
1566
1567 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001568 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1570 return FAIL;
1571
1572 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1573 return FAIL;
1574
Bram Moolenaar078a4612022-01-04 15:17:03 +00001575 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576 return OK;
1577}
1578
1579/*
1580 * Generate an ISN_DCALL or ISN_UCALL instruction.
1581 * Return FAIL if the number of arguments is wrong.
1582 */
1583 int
1584generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1585{
1586 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001587 int regular_args = ufunc->uf_args.ga_len;
1588 int argcount = pushed_argcount;
1589
1590 RETURN_OK_IF_SKIP(cctx);
1591 if (argcount > regular_args && !has_varargs(ufunc))
1592 {
1593 semsg(_(e_too_many_arguments_for_function_str),
1594 printable_func_name(ufunc));
1595 return FAIL;
1596 }
1597 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1598 {
1599 semsg(_(e_not_enough_arguments_for_function_str),
1600 printable_func_name(ufunc));
1601 return FAIL;
1602 }
1603
1604 if (ufunc->uf_def_status != UF_NOT_COMPILED
1605 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1606 {
1607 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001608 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001609
1610 for (i = 0; i < argcount; ++i)
1611 {
1612 type_T *expected;
1613 type_T *actual;
1614
Bram Moolenaar078a4612022-01-04 15:17:03 +00001615 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001616 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001617 && i >= regular_args - ufunc->uf_def_args.ga_len)
1618 {
1619 // assume v:none used for default argument value
1620 continue;
1621 }
1622 if (i < regular_args)
1623 {
1624 if (ufunc->uf_arg_types == NULL)
1625 continue;
1626 expected = ufunc->uf_arg_types[i];
1627 }
1628 else if (ufunc->uf_va_type == NULL
1629 || ufunc->uf_va_type == &t_list_any)
1630 // possibly a lambda or "...: any"
1631 expected = &t_any;
1632 else
1633 expected = ufunc->uf_va_type->tt_member;
1634 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1635 TRUE, FALSE) == FAIL)
1636 {
1637 arg_type_mismatch(expected, actual, i + 1);
1638 return FAIL;
1639 }
1640 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001641 compile_type = get_compile_type(ufunc);
1642 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001643 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001644 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001645 return FAIL;
1646 }
1647 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1648 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001649 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001650 ufunc->uf_name);
1651 return FAIL;
1652 }
1653
1654 if ((isn = generate_instr(cctx,
1655 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1656 : ISN_UCALL)) == NULL)
1657 return FAIL;
1658 if (isn->isn_type == ISN_DCALL)
1659 {
1660 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1661 isn->isn_arg.dfunc.cdf_argcount = argcount;
1662 }
1663 else
1664 {
1665 // A user function may be deleted and redefined later, can't use the
1666 // ufunc pointer, need to look it up again at runtime.
1667 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1668 isn->isn_arg.ufunc.cuf_argcount = argcount;
1669 }
1670
Bram Moolenaar078a4612022-01-04 15:17:03 +00001671 // drop the argument types
1672 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001673
Bram Moolenaar078a4612022-01-04 15:17:03 +00001674 // add return type
1675 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001676}
1677
1678/*
1679 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1680 */
1681 int
1682generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1683{
1684 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001685
1686 RETURN_OK_IF_SKIP(cctx);
1687 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1688 return FAIL;
1689 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1690 isn->isn_arg.ufunc.cuf_argcount = argcount;
1691
Bram Moolenaar078a4612022-01-04 15:17:03 +00001692 // drop the argument types
1693 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001694
Bram Moolenaar078a4612022-01-04 15:17:03 +00001695 // add return value
1696 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001697}
1698
1699/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001700 * Check the arguments of function "type" against the types on the stack.
1701 * Returns OK or FAIL;
1702 */
1703 int
1704check_func_args_from_type(
1705 cctx_T *cctx,
1706 type_T *type,
1707 int argcount,
1708 int at_top,
1709 char_u *name)
1710{
1711 if (type->tt_argcount != -1)
1712 {
1713 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1714
1715 if (argcount < type->tt_min_argcount - varargs)
1716 {
1717 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1718 return FAIL;
1719 }
1720 if (!varargs && argcount > type->tt_argcount)
1721 {
1722 emsg_funcname(e_too_many_arguments_for_function_str, name);
1723 return FAIL;
1724 }
1725 if (type->tt_args != NULL)
1726 {
1727 int i;
1728
1729 for (i = 0; i < argcount; ++i)
1730 {
1731 int offset = -argcount + i - (at_top ? 0 : 1);
1732 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1733 type_T *expected;
1734
1735 if (varargs && i >= type->tt_argcount - 1)
1736 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1737 else if (i >= type->tt_min_argcount
1738 && actual->tt_type == VAR_SPECIAL)
1739 expected = &t_any;
1740 else
1741 expected = type->tt_args[i];
1742 if (need_type(actual, expected, offset, i + 1,
1743 cctx, TRUE, FALSE) == FAIL)
1744 {
1745 arg_type_mismatch(expected, actual, i + 1);
1746 return FAIL;
1747 }
1748 }
1749 }
1750 }
1751
1752 return OK;
1753}
1754/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001755 * Generate an ISN_PCALL instruction.
1756 * "type" is the type of the FuncRef.
1757 */
1758 int
1759generate_PCALL(
1760 cctx_T *cctx,
1761 int argcount,
1762 char_u *name,
1763 type_T *type,
1764 int at_top)
1765{
1766 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001767 type_T *ret_type;
1768
1769 RETURN_OK_IF_SKIP(cctx);
1770
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001771 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001772 ret_type = &t_any;
1773 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1774 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001775 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1776 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001777
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001778 ret_type = type->tt_member;
1779 if (ret_type == &t_unknown)
1780 // return type not known yet, use a runtime check
1781 ret_type = &t_any;
1782 }
1783 else
1784 {
1785 semsg(_(e_not_callable_type_str), name);
1786 return FAIL;
1787 }
1788
1789 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1790 return FAIL;
1791 isn->isn_arg.pfunc.cpf_top = at_top;
1792 isn->isn_arg.pfunc.cpf_argcount = argcount;
1793
Bram Moolenaar078a4612022-01-04 15:17:03 +00001794 // drop the arguments and the funcref/partial
1795 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001796
Bram Moolenaar078a4612022-01-04 15:17:03 +00001797 // push the return value
1798 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001799
1800 // If partial is above the arguments it must be cleared and replaced with
1801 // the return value.
1802 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1803 return FAIL;
1804
1805 return OK;
1806}
1807
1808/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001809 * Generate an ISN_DEFER instruction.
1810 */
1811 int
1812generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1813{
1814 isn_T *isn;
1815
1816 RETURN_OK_IF_SKIP(cctx);
1817 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1818 return FAIL;
1819 isn->isn_arg.defer.defer_var_idx = var_idx;
1820 isn->isn_arg.defer.defer_argcount = argcount;
1821 return OK;
1822}
1823
1824/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 * Generate an ISN_STRINGMEMBER instruction.
1826 */
1827 int
1828generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1829{
1830 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001831 type_T *type;
1832
1833 RETURN_OK_IF_SKIP(cctx);
1834 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1835 return FAIL;
1836 isn->isn_arg.string = vim_strnsave(name, len);
1837
1838 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001839 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001840 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001841 {
1842 char *tofree;
1843
1844 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1845 name, type_name(type, &tofree));
1846 vim_free(tofree);
1847 return FAIL;
1848 }
1849 // change dict type to dict member type
1850 if (type->tt_type == VAR_DICT)
1851 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001852 type_T *ntype = type->tt_member == &t_unknown
1853 ? &t_any : type->tt_member;
1854 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001855 }
1856
1857 return OK;
1858}
1859
1860/*
1861 * Generate an ISN_ECHO instruction.
1862 */
1863 int
1864generate_ECHO(cctx_T *cctx, int with_white, int count)
1865{
1866 isn_T *isn;
1867
1868 RETURN_OK_IF_SKIP(cctx);
1869 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1870 return FAIL;
1871 isn->isn_arg.echo.echo_with_white = with_white;
1872 isn->isn_arg.echo.echo_count = count;
1873
1874 return OK;
1875}
1876
1877/*
1878 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1879 */
1880 int
1881generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1882{
1883 isn_T *isn;
1884
1885 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1886 return FAIL;
1887 isn->isn_arg.number = count;
1888
1889 return OK;
1890}
1891
1892/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001893 * Generate an ISN_SOURCE instruction.
1894 */
1895 int
1896generate_SOURCE(cctx_T *cctx, int sid)
1897{
1898 isn_T *isn;
1899
1900 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1901 return FAIL;
1902 isn->isn_arg.number = sid;
1903
1904 return OK;
1905}
1906
1907/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001908 * Generate an ISN_PUT instruction.
1909 */
1910 int
1911generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1912{
1913 isn_T *isn;
1914
1915 RETURN_OK_IF_SKIP(cctx);
1916 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1917 return FAIL;
1918 isn->isn_arg.put.put_regname = regname;
1919 isn->isn_arg.put.put_lnum = lnum;
1920 return OK;
1921}
1922
1923/*
1924 * Generate an EXEC instruction that takes a string argument.
1925 * A copy is made of "line".
1926 */
1927 int
1928generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1929{
1930 isn_T *isn;
1931
1932 RETURN_OK_IF_SKIP(cctx);
1933 if ((isn = generate_instr(cctx, isntype)) == NULL)
1934 return FAIL;
1935 isn->isn_arg.string = vim_strsave(line);
1936 return OK;
1937}
1938
1939/*
1940 * Generate an EXEC instruction that takes a string argument.
1941 * "str" must be allocated, it is consumed.
1942 */
1943 int
1944generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1945{
1946 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001947 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001948
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001949 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001950 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001951 if ((isn = generate_instr(cctx, isntype)) == NULL)
1952 ret = FAIL;
1953 else
1954 {
1955 isn->isn_arg.string = str;
1956 return OK;
1957 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001958 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001959 vim_free(str);
1960 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001961}
1962
1963 int
1964generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1965{
1966 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001967
1968 RETURN_OK_IF_SKIP(cctx);
1969 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1970 return FAIL;
1971 isn->isn_arg.string = vim_strsave(line);
1972
Bram Moolenaar078a4612022-01-04 15:17:03 +00001973 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001974}
1975
1976 int
1977generate_EXECCONCAT(cctx_T *cctx, int count)
1978{
1979 isn_T *isn;
1980
1981 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1982 return FAIL;
1983 isn->isn_arg.number = count;
1984 return OK;
1985}
1986
1987/*
1988 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1989 */
1990 int
1991generate_RANGE(cctx_T *cctx, char_u *range)
1992{
1993 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001994
1995 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1996 return FAIL;
1997 isn->isn_arg.string = range;
1998
Bram Moolenaar078a4612022-01-04 15:17:03 +00001999 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002000}
2001
2002 int
2003generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2004{
2005 isn_T *isn;
2006
2007 RETURN_OK_IF_SKIP(cctx);
2008 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2009 return FAIL;
2010 isn->isn_arg.unpack.unp_count = var_count;
2011 isn->isn_arg.unpack.unp_semicolon = semicolon;
2012 return OK;
2013}
2014
2015/*
2016 * Generate an instruction for any command modifiers.
2017 */
2018 int
2019generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2020{
2021 isn_T *isn;
2022
2023 if (has_cmdmod(cmod, FALSE))
2024 {
2025 cctx->ctx_has_cmdmod = TRUE;
2026
2027 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2028 return FAIL;
2029 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2030 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2031 return FAIL;
2032 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2033 // filter program now belongs to the instruction
2034 cmod->cmod_filter_regmatch.regprog = NULL;
2035 }
2036
2037 return OK;
2038}
2039
2040 int
2041generate_undo_cmdmods(cctx_T *cctx)
2042{
2043 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2044 return FAIL;
2045 cctx->ctx_has_cmdmod = FALSE;
2046 return OK;
2047}
2048
2049/*
2050 * Generate a STORE instruction for "dest", not being "dest_local".
2051 * Return FAIL when out of memory.
2052 */
2053 int
2054generate_store_var(
2055 cctx_T *cctx,
2056 assign_dest_T dest,
2057 int opt_flags,
2058 int vimvaridx,
2059 int scriptvar_idx,
2060 int scriptvar_sid,
2061 type_T *type,
2062 char_u *name)
2063{
2064 switch (dest)
2065 {
2066 case dest_option:
2067 return generate_STOREOPT(cctx, ISN_STOREOPT,
2068 skip_option_env_lead(name), opt_flags);
2069 case dest_func_option:
2070 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2071 skip_option_env_lead(name), opt_flags);
2072 case dest_global:
2073 // include g: with the name, easier to execute that way
2074 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2075 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2076 case dest_buffer:
2077 // include b: with the name, easier to execute that way
2078 return generate_STORE(cctx, ISN_STOREB, 0, name);
2079 case dest_window:
2080 // include w: with the name, easier to execute that way
2081 return generate_STORE(cctx, ISN_STOREW, 0, name);
2082 case dest_tab:
2083 // include t: with the name, easier to execute that way
2084 return generate_STORE(cctx, ISN_STORET, 0, name);
2085 case dest_env:
2086 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2087 case dest_reg:
2088 return generate_STORE(cctx, ISN_STOREREG,
2089 name[1] == '@' ? '"' : name[1], NULL);
2090 case dest_vimvar:
2091 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2092 case dest_script:
2093 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002094 {
2095 isntype_T isn_type = ISN_STORES;
2096
2097 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01002098 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2099 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2100 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002101 {
2102 // "import autoload './dir/script.vim'" - load script first
2103 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2104 return FAIL;
2105 isn_type = ISN_STOREEXPORT;
2106 }
2107
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002108 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002109 return generate_OLDSCRIPT(cctx, isn_type, name,
2110 scriptvar_sid, type);
2111 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002112 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
2113 scriptvar_sid, scriptvar_idx, type);
2114 case dest_local:
2115 case dest_expr:
2116 // cannot happen
2117 break;
2118 }
2119 return FAIL;
2120}
2121
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002122/*
2123 * Return TRUE when inside a "for" or "while" loop.
2124 */
2125 int
2126inside_loop_scope(cctx_T *cctx)
2127{
2128 scope_T *scope = cctx->ctx_scope;
2129
2130 for (;;)
2131 {
2132 if (scope == NULL)
2133 break;
2134 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2135 return TRUE;
2136 scope = scope->se_outer;
2137 }
2138 return FALSE;
2139}
2140
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002142generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002143{
2144 if (lhs->lhs_dest != dest_local)
2145 return generate_store_var(cctx, lhs->lhs_dest,
2146 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2147 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2148 lhs->lhs_type, lhs->lhs_name);
2149
2150 if (lhs->lhs_lvar != NULL)
2151 {
2152 garray_T *instr = &cctx->ctx_instr;
2153 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2154
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002155 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2156 // ISN_STORENR.
2157 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002158 if (lhs->lhs_lvar->lv_from_outer == 0
2159 && instr->ga_len == instr_count + 1
2160 && isn->isn_type == ISN_PUSHNR)
2161 {
2162 varnumber_T val = isn->isn_arg.number;
2163 garray_T *stack = &cctx->ctx_type_stack;
2164
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002165 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002166 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002167 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002168 --instr->ga_len;
2169 }
2170 else
2171 {
2172 isn->isn_type = ISN_STORENR;
2173 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2174 isn->isn_arg.storenr.stnr_val = val;
2175 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002176 if (stack->ga_len > 0)
2177 --stack->ga_len;
2178 }
2179 else if (lhs->lhs_lvar->lv_from_outer > 0)
2180 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002181 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002182 else
2183 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2184 }
2185 return OK;
2186}
2187
2188#if defined(FEAT_PROFILE) || defined(PROTO)
2189 void
2190may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2191{
2192 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2193 generate_instr(cctx, ISN_PROF_END);
2194}
2195#endif
2196
2197
2198/*
2199 * Delete an instruction, free what it contains.
2200 */
2201 void
2202delete_instr(isn_T *isn)
2203{
2204 switch (isn->isn_type)
2205 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002206 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002207 case ISN_DEF:
2208 case ISN_EXEC:
2209 case ISN_EXECRANGE:
2210 case ISN_EXEC_SPLIT:
2211 case ISN_LEGACY_EVAL:
2212 case ISN_LOADAUTO:
2213 case ISN_LOADB:
2214 case ISN_LOADENV:
2215 case ISN_LOADG:
2216 case ISN_LOADOPT:
2217 case ISN_LOADT:
2218 case ISN_LOADW:
2219 case ISN_LOCKUNLOCK:
2220 case ISN_PUSHEXC:
2221 case ISN_PUSHFUNC:
2222 case ISN_PUSHS:
2223 case ISN_RANGE:
2224 case ISN_STOREAUTO:
2225 case ISN_STOREB:
2226 case ISN_STOREENV:
2227 case ISN_STOREG:
2228 case ISN_STORET:
2229 case ISN_STOREW:
2230 case ISN_STRINGMEMBER:
2231 vim_free(isn->isn_arg.string);
2232 break;
2233
2234 case ISN_SUBSTITUTE:
2235 {
2236 int idx;
2237 isn_T *list = isn->isn_arg.subs.subs_instr;
2238
2239 vim_free(isn->isn_arg.subs.subs_cmd);
2240 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2241 delete_instr(list + idx);
2242 vim_free(list);
2243 }
2244 break;
2245
2246 case ISN_INSTR:
2247 {
2248 int idx;
2249 isn_T *list = isn->isn_arg.instr;
2250
2251 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2252 delete_instr(list + idx);
2253 vim_free(list);
2254 }
2255 break;
2256
2257 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002258 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002259 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002260 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002261 vim_free(isn->isn_arg.loadstore.ls_name);
2262 break;
2263
2264 case ISN_UNLET:
2265 case ISN_UNLETENV:
2266 vim_free(isn->isn_arg.unlet.ul_name);
2267 break;
2268
2269 case ISN_STOREOPT:
2270 case ISN_STOREFUNCOPT:
2271 vim_free(isn->isn_arg.storeopt.so_name);
2272 break;
2273
2274 case ISN_PUSHBLOB: // push blob isn_arg.blob
2275 blob_unref(isn->isn_arg.blob);
2276 break;
2277
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002278 case ISN_UCALL:
2279 vim_free(isn->isn_arg.ufunc.cuf_name);
2280 break;
2281
2282 case ISN_FUNCREF:
2283 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002284 funcref_T *funcref = &isn->isn_arg.funcref;
2285 funcref_extra_T *extra = funcref->fr_extra;
2286
2287 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002288 {
2289 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002290 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002291 ufunc_T *ufunc = dfunc->df_ufunc;
2292
2293 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2294 func_ptr_unref(ufunc);
2295 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002296 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002297 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002298 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002299
2300 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002301 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002302 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002303 vim_free(name);
2304 }
2305 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002306 }
2307 }
2308 break;
2309
2310 case ISN_DCALL:
2311 {
2312 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002313 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002314
2315 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002316 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002317 func_ptr_unref(dfunc->df_ufunc);
2318 }
2319 break;
2320
2321 case ISN_NEWFUNC:
2322 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002323 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002324
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002325 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002326 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002327 ufunc_T *ufunc = find_func_even_dead(
2328 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002329
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002330 if (ufunc != NULL)
2331 {
2332 unlink_def_function(ufunc);
2333 func_ptr_unref(ufunc);
2334 }
2335
2336 vim_free(arg->nfa_lambda);
2337 vim_free(arg->nfa_global);
2338 vim_free(arg);
2339 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002340 }
2341 break;
2342
2343 case ISN_CHECKTYPE:
2344 case ISN_SETTYPE:
2345 free_type(isn->isn_arg.type.ct_type);
2346 break;
2347
2348 case ISN_CMDMOD:
2349 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002350 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002351 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2352 break;
2353
2354 case ISN_LOADSCRIPT:
2355 case ISN_STORESCRIPT:
2356 vim_free(isn->isn_arg.script.scriptref);
2357 break;
2358
2359 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002360 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002361 break;
2362
2363 case ISN_CEXPR_CORE:
2364 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2365 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2366 break;
2367
2368 case ISN_2BOOL:
2369 case ISN_2STRING:
2370 case ISN_2STRING_ANY:
2371 case ISN_ADDBLOB:
2372 case ISN_ADDLIST:
2373 case ISN_ANYINDEX:
2374 case ISN_ANYSLICE:
2375 case ISN_BCALL:
2376 case ISN_BLOBAPPEND:
2377 case ISN_BLOBINDEX:
2378 case ISN_BLOBSLICE:
2379 case ISN_CATCH:
2380 case ISN_CEXPR_AUCMD:
2381 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002382 case ISN_CLEARDICT:
2383 case ISN_CMDMOD_REV:
2384 case ISN_COMPAREANY:
2385 case ISN_COMPAREBLOB:
2386 case ISN_COMPAREBOOL:
2387 case ISN_COMPAREDICT:
2388 case ISN_COMPAREFLOAT:
2389 case ISN_COMPAREFUNC:
2390 case ISN_COMPARELIST:
2391 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002392 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 case ISN_COMPARESPECIAL:
2394 case ISN_COMPARESTRING:
2395 case ISN_CONCAT:
2396 case ISN_COND2BOOL:
2397 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002398 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002399 case ISN_DROP:
2400 case ISN_ECHO:
2401 case ISN_ECHOCONSOLE:
2402 case ISN_ECHOERR:
2403 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002404 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002405 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002406 case ISN_ENDTRY:
2407 case ISN_EXECCONCAT:
2408 case ISN_EXECUTE:
2409 case ISN_FINALLY:
2410 case ISN_FINISH:
2411 case ISN_FOR:
2412 case ISN_GETITEM:
2413 case ISN_JUMP:
2414 case ISN_JUMP_IF_ARG_SET:
2415 case ISN_LISTAPPEND:
2416 case ISN_LISTINDEX:
2417 case ISN_LISTSLICE:
2418 case ISN_LOAD:
2419 case ISN_LOADBDICT:
2420 case ISN_LOADGDICT:
2421 case ISN_LOADOUTER:
2422 case ISN_LOADREG:
2423 case ISN_LOADTDICT:
2424 case ISN_LOADV:
2425 case ISN_LOADWDICT:
2426 case ISN_LOCKCONST:
2427 case ISN_MEMBER:
2428 case ISN_NEGATENR:
2429 case ISN_NEWDICT:
2430 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002431 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002432 case ISN_OPANY:
2433 case ISN_OPFLOAT:
2434 case ISN_OPNR:
2435 case ISN_PCALL:
2436 case ISN_PCALL_END:
2437 case ISN_PROF_END:
2438 case ISN_PROF_START:
2439 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002440 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002442 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002443 case ISN_PUSHNR:
2444 case ISN_PUSHSPEC:
2445 case ISN_PUT:
2446 case ISN_REDIREND:
2447 case ISN_REDIRSTART:
2448 case ISN_RETURN:
2449 case ISN_RETURN_VOID:
2450 case ISN_SHUFFLE:
2451 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002452 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002453 case ISN_STORE:
2454 case ISN_STOREINDEX:
2455 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002456 case ISN_STOREOUTER:
2457 case ISN_STORERANGE:
2458 case ISN_STOREREG:
2459 case ISN_STOREV:
2460 case ISN_STRINDEX:
2461 case ISN_STRSLICE:
2462 case ISN_THROW:
2463 case ISN_TRYCONT:
2464 case ISN_UNLETINDEX:
2465 case ISN_UNLETRANGE:
2466 case ISN_UNPACK:
2467 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002468 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002469 // nothing allocated
2470 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002471 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002472}
2473
2474 void
2475clear_instr_ga(garray_T *gap)
2476{
2477 int idx;
2478
2479 for (idx = 0; idx < gap->ga_len; ++idx)
2480 delete_instr(((isn_T *)gap->ga_data) + idx);
2481 ga_clear(gap);
2482}
2483
2484
2485#endif // defined(FEAT_EVAL)