blob: edc6cb61d7672e1146282b91669f5df31c1fed1b [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
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000211 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000212 : ISN_OPANY, 1);
213
214 if (vartype != VAR_LIST && vartype != VAR_BLOB
215 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000216 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000217 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000218 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 && check_number_or_float(
220 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
221 return FAIL;
222
223 if (isn != NULL)
224 {
225 if (isn->isn_type == ISN_ADDLIST)
226 isn->isn_arg.op.op_type = expr_type;
227 else
228 isn->isn_arg.op.op_type = EXPR_ADD;
229 }
230
231 // When concatenating two lists with different member types the member type
232 // becomes "any".
233 if (vartype == VAR_LIST
234 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
235 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000236 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000237
238 return isn == NULL ? FAIL : OK;
239}
240
241/*
242 * Get the type to use for an instruction for an operation on "type1" and
243 * "type2". If they are matching use a type-specific instruction. Otherwise
244 * fall back to runtime type checking.
245 */
246 vartype_T
247operator_type(type_T *type1, type_T *type2)
248{
249 if (type1->tt_type == type2->tt_type
250 && (type1->tt_type == VAR_NUMBER
251 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000252 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000253 || type1->tt_type == VAR_BLOB))
254 return type1->tt_type;
255 return VAR_ANY;
256}
257
258/*
259 * Generate an instruction with two arguments. The instruction depends on the
260 * type of the arguments.
261 */
262 int
263generate_two_op(cctx_T *cctx, char_u *op)
264{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000265 type_T *type1;
266 type_T *type2;
267 vartype_T vartype;
268 isn_T *isn;
269
270 RETURN_OK_IF_SKIP(cctx);
271
272 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000273 type1 = get_type_on_stack(cctx, 1);
274 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000275 vartype = operator_type(type1, type2);
276
277 switch (*op)
278 {
279 case '+':
280 if (generate_add_instr(cctx, vartype, type1, type2,
281 EXPR_COPY) == FAIL)
282 return FAIL;
283 break;
284
285 case '-':
286 case '*':
287 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
288 op) == FAIL)
289 return FAIL;
290 if (vartype == VAR_NUMBER)
291 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000292 else if (vartype == VAR_FLOAT)
293 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000294 else
295 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
296 if (isn != NULL)
297 isn->isn_arg.op.op_type = *op == '*'
298 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
299 break;
300
301 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000302 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000303 && type1->tt_type != VAR_NUMBER)
304 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000305 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000306 && type2->tt_type != VAR_NUMBER))
307 {
308 emsg(_(e_percent_requires_number_arguments));
309 return FAIL;
310 }
311 isn = generate_instr_drop(cctx,
312 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
313 if (isn != NULL)
314 isn->isn_arg.op.op_type = EXPR_REM;
315 break;
316 }
317
318 // correct type of result
319 if (vartype == VAR_ANY)
320 {
321 type_T *type = &t_any;
322
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000323 // float+number and number+float results in float
324 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100325 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000326 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000327 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000328 }
329
330 return OK;
331}
332
333/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000334 * Get the instruction to use for comparing two values with specified types.
335 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000336 * Return ISN_DROP when failed.
337 */
338 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000339get_compare_isn(
340 exprtype_T exprtype,
341 typval_T *tv1,
342 typval_T *tv2,
343 type_T *type1,
344 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000345{
346 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000347 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
348 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000349
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000350 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000351 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000352 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000353 {
354 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
355 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
356 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
357 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
358 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
359 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
360 case VAR_LIST: isntype = ISN_COMPARELIST; break;
361 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
362 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
363 default: isntype = ISN_COMPAREANY; break;
364 }
365 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000366 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
367 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
368 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
369 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
370 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000371 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000372 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000373 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000374 if ((vartype1 == VAR_SPECIAL
375 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
376 : type1 == &t_none)
377 && vartype2 != VAR_STRING)
378 || (vartype2 == VAR_SPECIAL
379 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
380 : type2 == &t_none)
381 && vartype1 != VAR_STRING))
382 {
383 semsg(_(e_cannot_compare_str_with_str),
384 vartype_name(vartype1), vartype_name(vartype2));
385 return ISN_DROP;
386 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000387 // although comparing null with number, float or bool is not useful, we
388 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000389 isntype = ISN_COMPARENULL;
390 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000391
392 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
393 && (isntype == ISN_COMPAREBOOL
394 || isntype == ISN_COMPARESPECIAL
395 || isntype == ISN_COMPARENR
396 || isntype == ISN_COMPAREFLOAT))
397 {
398 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000399 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000400 return ISN_DROP;
401 }
402 if (isntype == ISN_DROP
403 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000404 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
405 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000406 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000407 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000408 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
409 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000410 {
411 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000412 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000413 return ISN_DROP;
414 }
415 return isntype;
416}
417
418 int
419check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
420{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000421 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000422 return FAIL;
423 return OK;
424}
425
426/*
427 * Generate an ISN_COMPARE* instruction with a boolean result.
428 */
429 int
430generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
431{
432 isntype_T isntype;
433 isn_T *isn;
434 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000435
436 RETURN_OK_IF_SKIP(cctx);
437
438 // Get the known type of the two items on the stack. If they are matching
439 // use a type-specific instruction. Otherwise fall back to runtime type
440 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000441 isntype = get_compare_isn(exprtype, NULL, NULL,
442 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443 if (isntype == ISN_DROP)
444 return FAIL;
445
446 if ((isn = generate_instr(cctx, isntype)) == NULL)
447 return FAIL;
448 isn->isn_arg.op.op_type = exprtype;
449 isn->isn_arg.op.op_ic = ic;
450
451 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100452 --stack->ga_len;
453 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000454
455 return OK;
456}
457
458/*
LemonBoy372bcce2022-04-25 12:43:20 +0100459 * Generate an ISN_CONCAT instruction.
460 * "count" is the number of stack elements to join together and it must be
461 * greater or equal to one.
462 * The caller ensures all the "count" elements on the stack have the right type.
463 */
464 int
465generate_CONCAT(cctx_T *cctx, int count)
466{
467 isn_T *isn;
468 garray_T *stack = &cctx->ctx_type_stack;
469
470 RETURN_OK_IF_SKIP(cctx);
471
LemonBoy372bcce2022-04-25 12:43:20 +0100472 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
473 return FAIL;
474 isn->isn_arg.number = count;
475
476 // drop the argument types
477 stack->ga_len -= count - 1;
478
479 return OK;
480}
481
482/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000483 * Generate an ISN_2BOOL instruction.
484 * "offset" is the offset in the type stack.
485 */
486 int
487generate_2BOOL(cctx_T *cctx, int invert, int offset)
488{
489 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000490
491 RETURN_OK_IF_SKIP(cctx);
492 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
493 return FAIL;
494 isn->isn_arg.tobool.invert = invert;
495 isn->isn_arg.tobool.offset = offset;
496
497 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000498 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000499
500 return OK;
501}
502
503/*
504 * Generate an ISN_COND2BOOL instruction.
505 */
506 int
507generate_COND2BOOL(cctx_T *cctx)
508{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000509 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100510 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000511 return FAIL;
512
513 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000514 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000515
516 return OK;
517}
518
519 int
520generate_TYPECHECK(
521 cctx_T *cctx,
522 type_T *expected,
523 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100524 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000525 int argidx)
526{
527 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528
529 RETURN_OK_IF_SKIP(cctx);
530 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
531 return FAIL;
532 isn->isn_arg.type.ct_type = alloc_type(expected);
533 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100534 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000535 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
536
537 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000538 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539
540 return OK;
541}
542
543 int
544generate_SETTYPE(
545 cctx_T *cctx,
546 type_T *expected)
547{
548 isn_T *isn;
549
550 RETURN_OK_IF_SKIP(cctx);
551 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
552 return FAIL;
553 isn->isn_arg.type.ct_type = alloc_type(expected);
554 return OK;
555}
556
557/*
558 * Generate a PUSH instruction for "tv".
559 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000560 */
561 int
562generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
563{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100564 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000565 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100566 case VAR_BOOL:
567 generate_PUSHBOOL(cctx, tv->vval.v_number);
568 break;
569 case VAR_SPECIAL:
570 generate_PUSHSPEC(cctx, tv->vval.v_number);
571 break;
572 case VAR_NUMBER:
573 generate_PUSHNR(cctx, tv->vval.v_number);
574 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100575 case VAR_FLOAT:
576 generate_PUSHF(cctx, tv->vval.v_float);
577 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100578 case VAR_BLOB:
579 generate_PUSHBLOB(cctx, tv->vval.v_blob);
580 tv->vval.v_blob = NULL;
581 break;
582 case VAR_LIST:
583 if (tv->vval.v_list != NULL)
584 iemsg("non-empty list constant not supported");
585 generate_NEWLIST(cctx, 0, TRUE);
586 break;
587 case VAR_DICT:
588 if (tv->vval.v_dict != NULL)
589 iemsg("non-empty dict constant not supported");
590 generate_NEWDICT(cctx, 0, TRUE);
591 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000592#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100593 case VAR_JOB:
594 if (tv->vval.v_job != NULL)
595 iemsg("non-null job constant not supported");
596 generate_PUSHJOB(cctx);
597 break;
598 case VAR_CHANNEL:
599 if (tv->vval.v_channel != NULL)
600 iemsg("non-null channel constant not supported");
601 generate_PUSHCHANNEL(cctx);
602 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000603#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100604 case VAR_FUNC:
605 if (tv->vval.v_string != NULL)
606 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100607 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100608 break;
609 case VAR_PARTIAL:
610 if (tv->vval.v_partial != NULL)
611 iemsg("non-null partial constant not supported");
612 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
613 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100615 break;
616 case VAR_STRING:
617 generate_PUSHS(cctx, &tv->vval.v_string);
618 tv->vval.v_string = NULL;
619 break;
620 default:
621 siemsg("constant type %d not supported", tv->v_type);
622 clear_tv(tv);
623 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000624 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100625 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000626 return OK;
627}
628
629/*
630 * Generate an ISN_PUSHNR instruction.
631 */
632 int
633generate_PUSHNR(cctx_T *cctx, varnumber_T number)
634{
635 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636
637 RETURN_OK_IF_SKIP(cctx);
638 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
639 return FAIL;
640 isn->isn_arg.number = number;
641
642 if (number == 0 || number == 1)
643 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000644 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000645 return OK;
646}
647
648/*
649 * Generate an ISN_PUSHBOOL instruction.
650 */
651 int
652generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
653{
654 isn_T *isn;
655
656 RETURN_OK_IF_SKIP(cctx);
657 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
658 return FAIL;
659 isn->isn_arg.number = number;
660
661 return OK;
662}
663
664/*
665 * Generate an ISN_PUSHSPEC instruction.
666 */
667 int
668generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
669{
670 isn_T *isn;
671
672 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000673 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
674 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000675 return FAIL;
676 isn->isn_arg.number = number;
677
678 return OK;
679}
680
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000681/*
682 * Generate an ISN_PUSHF instruction.
683 */
684 int
685generate_PUSHF(cctx_T *cctx, float_T fnumber)
686{
687 isn_T *isn;
688
689 RETURN_OK_IF_SKIP(cctx);
690 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
691 return FAIL;
692 isn->isn_arg.fnumber = fnumber;
693
694 return OK;
695}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000696
697/*
698 * Generate an ISN_PUSHS instruction.
699 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100700 * Note that if "str" is used in the instruction OK is returned and "*str" is
701 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000702 */
703 int
704generate_PUSHS(cctx_T *cctx, char_u **str)
705{
706 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100707 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000708
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100709 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000710 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100711 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
712 ret = FAIL;
713 else
714 {
715 isn->isn_arg.string = str == NULL ? NULL : *str;
716 return OK;
717 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000718 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100719 if (str != NULL)
720 VIM_CLEAR(*str);
721 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000722}
723
724/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000725 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000726 */
727 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000728generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000729{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100731#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100732 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000733 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000734 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100735#else
736 emsg(_(e_channel_job_feature_not_available));
737 return FAIL;
738#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000739}
740
741/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000742 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000743 */
744 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000745generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000747 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100748#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100749 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000751 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100752#else
753 emsg(_(e_channel_job_feature_not_available));
754 return FAIL;
755#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000756}
757
758/*
759 * Generate an ISN_PUSHBLOB instruction.
760 * Consumes "blob".
761 */
762 int
763generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
764{
765 isn_T *isn;
766
767 RETURN_OK_IF_SKIP(cctx);
768 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
769 return FAIL;
770 isn->isn_arg.blob = blob;
771
772 return OK;
773}
774
775/*
776 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100777 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
778 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000779 */
780 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100781generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782{
783 isn_T *isn;
784 char_u *funcname;
785
786 RETURN_OK_IF_SKIP(cctx);
787 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
788 return FAIL;
789 if (name == NULL)
790 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100791 else if (!may_prefix
792 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000793 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000794 funcname = vim_strsave(name);
795 else
796 {
797 funcname = alloc(STRLEN(name) + 3);
798 if (funcname != NULL)
799 {
800 STRCPY(funcname, "g:");
801 STRCPY(funcname + 2, name);
802 }
803 }
804
805 isn->isn_arg.string = funcname;
806 return OK;
807}
808
809/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000810 * Generate an ISN_AUTOLOAD instruction.
811 */
812 int
813generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
814{
815 isn_T *isn;
816
817 RETURN_OK_IF_SKIP(cctx);
818 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
819 return FAIL;
820 isn->isn_arg.string = vim_strsave(name);
821 if (isn->isn_arg.string == NULL)
822 return FAIL;
823 return OK;
824}
825
826/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827 * Generate an ISN_GETITEM instruction with "index".
828 * "with_op" is TRUE for "+=" and other operators, the stack has the current
829 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100830 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000831 */
832 int
833generate_GETITEM(cctx_T *cctx, int index, int with_op)
834{
835 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000836 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000837 type_T *item_type = &t_any;
838
839 RETURN_OK_IF_SKIP(cctx);
840
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000841 item_type = type->tt_member;
842 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
843 return FAIL;
844 isn->isn_arg.getitem.gi_index = index;
845 isn->isn_arg.getitem.gi_with_op = with_op;
846
847 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000848 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000849}
850
851/*
852 * Generate an ISN_SLICE instruction with "count".
853 */
854 int
855generate_SLICE(cctx_T *cctx, int count)
856{
857 isn_T *isn;
858
859 RETURN_OK_IF_SKIP(cctx);
860 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
861 return FAIL;
862 isn->isn_arg.number = count;
863 return OK;
864}
865
866/*
867 * Generate an ISN_CHECKLEN instruction with "min_len".
868 */
869 int
870generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
871{
872 isn_T *isn;
873
874 RETURN_OK_IF_SKIP(cctx);
875
876 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
877 return FAIL;
878 isn->isn_arg.checklen.cl_min_len = min_len;
879 isn->isn_arg.checklen.cl_more_OK = more_OK;
880
881 return OK;
882}
883
884/*
885 * Generate an ISN_STORE instruction.
886 */
887 int
888generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
889{
890 isn_T *isn;
891
892 RETURN_OK_IF_SKIP(cctx);
893 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
894 return FAIL;
895 if (name != NULL)
896 isn->isn_arg.string = vim_strsave(name);
897 else
898 isn->isn_arg.number = idx;
899
900 return OK;
901}
902
903/*
904 * Generate an ISN_STOREOUTER instruction.
905 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000906 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100907generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000908{
909 isn_T *isn;
910
911 RETURN_OK_IF_SKIP(cctx);
912 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
913 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100914 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
915 {
916 // Store a variable defined in a loop. A copy will be made at the end
917 // of the loop. TODO: how about deeper nesting?
918 isn->isn_arg.outer.outer_idx = idx - loop_idx;
919 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
920 }
921 else
922 {
923 isn->isn_arg.outer.outer_idx = idx;
924 isn->isn_arg.outer.outer_depth = level;
925 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000926
927 return OK;
928}
929
930/*
931 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
932 */
933 int
934generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
935{
936 isn_T *isn;
937
938 RETURN_OK_IF_SKIP(cctx);
939 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
940 return FAIL;
941 isn->isn_arg.storenr.stnr_idx = idx;
942 isn->isn_arg.storenr.stnr_val = value;
943
944 return OK;
945}
946
947/*
948 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
949 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000950 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000951generate_STOREOPT(
952 cctx_T *cctx,
953 isntype_T isn_type,
954 char_u *name,
955 int opt_flags)
956{
957 isn_T *isn;
958
959 RETURN_OK_IF_SKIP(cctx);
960 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
961 return FAIL;
962 isn->isn_arg.storeopt.so_name = vim_strsave(name);
963 isn->isn_arg.storeopt.so_flags = opt_flags;
964
965 return OK;
966}
967
968/*
969 * Generate an ISN_LOAD or similar instruction.
970 */
971 int
972generate_LOAD(
973 cctx_T *cctx,
974 isntype_T isn_type,
975 int idx,
976 char_u *name,
977 type_T *type)
978{
979 isn_T *isn;
980
981 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000982 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 return FAIL;
984 if (name != NULL)
985 isn->isn_arg.string = vim_strsave(name);
986 else
987 isn->isn_arg.number = idx;
988
989 return OK;
990}
991
992/*
993 * Generate an ISN_LOADOUTER instruction
994 */
995 int
996generate_LOADOUTER(
997 cctx_T *cctx,
998 int idx,
999 int nesting,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001000 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001001 type_T *type)
1002{
1003 isn_T *isn;
1004
1005 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001006 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001008 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1009 {
1010 // Load a variable defined in a loop. A copy will be made at the end
1011 // of the loop. TODO: how about deeper nesting?
1012 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1013 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1014 }
1015 else
1016 {
1017 isn->isn_arg.outer.outer_idx = idx;
1018 isn->isn_arg.outer.outer_depth = nesting;
1019 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001020
1021 return OK;
1022}
1023
1024/*
1025 * Generate an ISN_LOADV instruction for v:var.
1026 */
1027 int
1028generate_LOADV(
1029 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001030 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001031{
1032 int di_flags;
1033 int vidx = find_vim_var(name, &di_flags);
1034 type_T *type;
1035
1036 RETURN_OK_IF_SKIP(cctx);
1037 if (vidx < 0)
1038 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001039 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001040 return FAIL;
1041 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001042 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001043 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1044}
1045
1046/*
1047 * Generate an ISN_UNLET instruction.
1048 */
1049 int
1050generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1051{
1052 isn_T *isn;
1053
1054 RETURN_OK_IF_SKIP(cctx);
1055 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1056 return FAIL;
1057 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1058 isn->isn_arg.unlet.ul_forceit = forceit;
1059
1060 return OK;
1061}
1062
1063/*
1064 * Generate an ISN_LOCKCONST instruction.
1065 */
1066 int
1067generate_LOCKCONST(cctx_T *cctx)
1068{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001069 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001070 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001071 return FAIL;
1072 return OK;
1073}
1074
1075/*
1076 * Generate an ISN_LOADS instruction.
1077 */
1078 int
1079generate_OLDSCRIPT(
1080 cctx_T *cctx,
1081 isntype_T isn_type,
1082 char_u *name,
1083 int sid,
1084 type_T *type)
1085{
1086 isn_T *isn;
1087
1088 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001089 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001090 isn = generate_instr_type(cctx, isn_type, type);
1091 else
1092 isn = generate_instr_drop(cctx, isn_type, 1);
1093 if (isn == NULL)
1094 return FAIL;
1095 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1096 isn->isn_arg.loadstore.ls_sid = sid;
1097
1098 return OK;
1099}
1100
1101/*
1102 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1103 */
1104 int
1105generate_VIM9SCRIPT(
1106 cctx_T *cctx,
1107 isntype_T isn_type,
1108 int sid,
1109 int idx,
1110 type_T *type)
1111{
1112 isn_T *isn;
1113 scriptref_T *sref;
1114 scriptitem_T *si = SCRIPT_ITEM(sid);
1115
1116 RETURN_OK_IF_SKIP(cctx);
1117 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001118 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001119 else
1120 isn = generate_instr_drop(cctx, isn_type, 1);
1121 if (isn == NULL)
1122 return FAIL;
1123
1124 // This requires three arguments, which doesn't fit in an instruction, thus
1125 // we need to allocate a struct for this.
1126 sref = ALLOC_ONE(scriptref_T);
1127 if (sref == NULL)
1128 return FAIL;
1129 isn->isn_arg.script.scriptref = sref;
1130 sref->sref_sid = sid;
1131 sref->sref_idx = idx;
1132 sref->sref_seq = si->sn_script_seq;
1133 sref->sref_type = type;
1134 return OK;
1135}
1136
1137/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001138 * Generate an ISN_NEWLIST instruction for "count" items.
1139 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001140 */
1141 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001142generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001143{
1144 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001145 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001146 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001147 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001148
1149 RETURN_OK_IF_SKIP(cctx);
1150 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1151 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001152 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001153
Bram Moolenaar078a4612022-01-04 15:17:03 +00001154 // Get the member type and the declared member type from all the items on
1155 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001156 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001157 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001158 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001159
1160 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001161 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162
1163 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001164 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165}
1166
1167/*
1168 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001169 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001170 */
1171 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001172generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001173{
1174 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001175 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001176 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001177 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178
1179 RETURN_OK_IF_SKIP(cctx);
1180 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1181 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001182 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001184 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001185 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001186 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001187
1188 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001189 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190
1191 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001192 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001193}
1194
1195/*
1196 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001197 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001198 * If variables were declared inside a loop "loop_var_idx" is the index of the
1199 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001200 */
1201 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001202generate_FUNCREF(
1203 cctx_T *cctx,
1204 ufunc_T *ufunc,
1205 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001206{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001207 isn_T *isn;
1208 type_T *type;
1209 funcref_extra_T *extra;
1210 short loop_var_idx;
1211 short loop_var_count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001212
1213 RETURN_OK_IF_SKIP(cctx);
1214 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1215 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001216 if (isnp != NULL)
1217 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001218
1219 loop_var_count = get_loop_var_info(cctx, &loop_var_idx);
1220 if (ufunc->uf_def_status == UF_NOT_COMPILED || loop_var_count > 0)
1221 {
1222 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1223 if (extra == NULL)
1224 return FAIL;
1225 isn->isn_arg.funcref.fr_extra = extra;
1226 extra->fre_loop_var_idx = loop_var_idx;
1227 extra->fre_loop_var_count = loop_var_count;
1228 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001229 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001230 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001231 else
1232 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1233 cctx->ctx_has_closure = 1;
1234
1235 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001236 // the nested context, including this one. But not a function defined at
1237 // the script level.
1238 if ((ufunc->uf_flags & FC_CLOSURE)
1239 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001240 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1241
Bram Moolenaar078a4612022-01-04 15:17:03 +00001242 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1243 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001244}
1245
1246/*
1247 * Generate an ISN_NEWFUNC instruction.
1248 * "lambda_name" and "func_name" must be in allocated memory and will be
1249 * consumed.
1250 */
1251 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001252generate_NEWFUNC(
1253 cctx_T *cctx,
1254 char_u *lambda_name,
1255 char_u *func_name,
1256 short loop_var_idx,
1257 short loop_var_count)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001258{
1259 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001260 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001261
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001262 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001263 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001264 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1265 ret = FAIL;
1266 else
1267 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001268 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1269
1270 if (arg == NULL)
1271 ret = FAIL;
1272 else
1273 {
1274 isn->isn_arg.newfunc.nf_arg = arg;
1275 arg->nfa_lambda = lambda_name;
1276 arg->nfa_global = func_name;
1277 arg->nfa_loop_var_idx = loop_var_idx;
1278 arg->nfa_loop_var_count = loop_var_count;
1279 return OK;
1280 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001281 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001283 vim_free(lambda_name);
1284 vim_free(func_name);
1285 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001286}
1287
1288/*
1289 * Generate an ISN_DEF instruction: list functions
1290 */
1291 int
1292generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1293{
1294 isn_T *isn;
1295
1296 RETURN_OK_IF_SKIP(cctx);
1297 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1298 return FAIL;
1299 if (len > 0)
1300 {
1301 isn->isn_arg.string = vim_strnsave(name, len);
1302 if (isn->isn_arg.string == NULL)
1303 return FAIL;
1304 }
1305 return OK;
1306}
1307
1308/*
1309 * Generate an ISN_JUMP instruction.
1310 */
1311 int
1312generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1313{
1314 isn_T *isn;
1315 garray_T *stack = &cctx->ctx_type_stack;
1316
1317 RETURN_OK_IF_SKIP(cctx);
1318 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1319 return FAIL;
1320 isn->isn_arg.jump.jump_when = when;
1321 isn->isn_arg.jump.jump_where = where;
1322
1323 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1324 --stack->ga_len;
1325
1326 return OK;
1327}
1328
1329/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001330 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1331 */
1332 int
1333generate_WHILE(cctx_T *cctx, int funcref_idx)
1334{
1335 isn_T *isn;
1336 garray_T *stack = &cctx->ctx_type_stack;
1337
1338 RETURN_OK_IF_SKIP(cctx);
1339 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1340 return FAIL;
1341 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1342 isn->isn_arg.whileloop.while_end = 0; // filled in later
1343
1344 if (stack->ga_len > 0)
1345 --stack->ga_len;
1346
1347 return OK;
1348}
1349
1350/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001351 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1352 */
1353 int
1354generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1355{
1356 isn_T *isn;
1357
1358 RETURN_OK_IF_SKIP(cctx);
1359 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1360 return FAIL;
1361 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1362 // jump_where is set later
1363 return OK;
1364}
1365
1366 int
1367generate_FOR(cctx_T *cctx, int loop_idx)
1368{
1369 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001370
1371 RETURN_OK_IF_SKIP(cctx);
1372 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1373 return FAIL;
1374 isn->isn_arg.forloop.for_idx = loop_idx;
1375
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001376 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001377 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001378}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001379
1380 int
1381generate_ENDLOOP(
1382 cctx_T *cctx,
1383 int funcref_idx,
1384 int prev_local_count)
1385{
1386 isn_T *isn;
1387
1388 RETURN_OK_IF_SKIP(cctx);
1389 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1390 return FAIL;
1391 isn->isn_arg.endloop.end_funcref_idx = funcref_idx;
1392 isn->isn_arg.endloop.end_var_idx = prev_local_count;
1393 isn->isn_arg.endloop.end_var_count =
1394 cctx->ctx_locals.ga_len - prev_local_count;
1395 return OK;
1396}
1397
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001398/*
1399 * Generate an ISN_TRYCONT instruction.
1400 */
1401 int
1402generate_TRYCONT(cctx_T *cctx, int levels, int where)
1403{
1404 isn_T *isn;
1405
1406 RETURN_OK_IF_SKIP(cctx);
1407 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1408 return FAIL;
1409 isn->isn_arg.trycont.tct_levels = levels;
1410 isn->isn_arg.trycont.tct_where = where;
1411
1412 return OK;
1413}
1414
Bram Moolenaar16900322022-09-08 19:51:45 +01001415/*
1416 * Check "argount" arguments and their types on the type stack.
1417 * Give an error and return FAIL if something is wrong.
1418 * When "method_call" is NULL no code is generated.
1419 */
1420 int
1421check_internal_func_args(
1422 cctx_T *cctx,
1423 int func_idx,
1424 int argcount,
1425 int method_call,
1426 type2_T **argtypes,
1427 type2_T *shuffled_argtypes)
1428{
1429 garray_T *stack = &cctx->ctx_type_stack;
1430 int argoff = check_internal_func(func_idx, argcount);
1431
1432 if (argoff < 0)
1433 return FAIL;
1434
1435 if (method_call && argoff > 1)
1436 {
1437 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1438
1439 if (isn == NULL)
1440 return FAIL;
1441 isn->isn_arg.shuffle.shfl_item = argcount;
1442 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1443 }
1444
1445 if (argcount > 0)
1446 {
1447 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1448
1449 // Check the types of the arguments.
1450 if (method_call && argoff > 1)
1451 {
1452 int i;
1453
1454 for (i = 0; i < argcount; ++i)
1455 shuffled_argtypes[i] = (i < argoff - 1)
1456 ? typep[i + 1]
1457 : (i == argoff - 1) ? typep[0] : typep[i];
1458 *argtypes = shuffled_argtypes;
1459 }
1460 else
1461 {
1462 int i;
1463
1464 for (i = 0; i < argcount; ++i)
1465 shuffled_argtypes[i] = typep[i];
1466 *argtypes = shuffled_argtypes;
1467 }
1468 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1469 cctx) == FAIL)
1470 return FAIL;
1471 }
1472 return OK;
1473}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001474
1475/*
1476 * Generate an ISN_BCALL instruction.
1477 * "method_call" is TRUE for "value->method()"
1478 * Return FAIL if the number of arguments is wrong.
1479 */
1480 int
1481generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1482{
1483 isn_T *isn;
1484 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001485 type2_T *argtypes = NULL;
1486 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1487 type2_T *maptype = NULL;
1488 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001489 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001490
1491 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001492
1493 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1494 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001495 return FAIL;
1496
Bram Moolenaar16900322022-09-08 19:51:45 +01001497 if (internal_func_is_map(func_idx))
1498 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001499
1500 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1501 return FAIL;
1502 isn->isn_arg.bfunc.cbf_idx = func_idx;
1503 isn->isn_arg.bfunc.cbf_argcount = argcount;
1504
1505 // Drop the argument types and push the return type.
1506 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001507 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1508 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001509 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001510
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001511 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1512 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001513 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001514 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001515
1516 return OK;
1517}
1518
1519/*
1520 * Generate an ISN_LISTAPPEND instruction. Works like add().
1521 * Argument count is already checked.
1522 */
1523 int
1524generate_LISTAPPEND(cctx_T *cctx)
1525{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001526 type_T *list_type;
1527 type_T *item_type;
1528 type_T *expected;
1529
1530 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001531 // For checking the item type we use the declared type of the list and the
1532 // current type of the added item, adding a string to [1, 2] is OK.
1533 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001534 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001535 expected = list_type->tt_member;
1536 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1537 return FAIL;
1538
1539 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1540 return FAIL;
1541
Bram Moolenaar078a4612022-01-04 15:17:03 +00001542 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001543 return OK;
1544}
1545
1546/*
1547 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1548 * Argument count is already checked.
1549 */
1550 int
1551generate_BLOBAPPEND(cctx_T *cctx)
1552{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001553 type_T *item_type;
1554
1555 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001556 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001557 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1558 return FAIL;
1559
1560 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1561 return FAIL;
1562
Bram Moolenaar078a4612022-01-04 15:17:03 +00001563 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001564 return OK;
1565}
1566
1567/*
1568 * Generate an ISN_DCALL or ISN_UCALL instruction.
1569 * Return FAIL if the number of arguments is wrong.
1570 */
1571 int
1572generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1573{
1574 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001575 int regular_args = ufunc->uf_args.ga_len;
1576 int argcount = pushed_argcount;
1577
1578 RETURN_OK_IF_SKIP(cctx);
1579 if (argcount > regular_args && !has_varargs(ufunc))
1580 {
1581 semsg(_(e_too_many_arguments_for_function_str),
1582 printable_func_name(ufunc));
1583 return FAIL;
1584 }
1585 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1586 {
1587 semsg(_(e_not_enough_arguments_for_function_str),
1588 printable_func_name(ufunc));
1589 return FAIL;
1590 }
1591
1592 if (ufunc->uf_def_status != UF_NOT_COMPILED
1593 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1594 {
1595 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001596 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001597
1598 for (i = 0; i < argcount; ++i)
1599 {
1600 type_T *expected;
1601 type_T *actual;
1602
Bram Moolenaar078a4612022-01-04 15:17:03 +00001603 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001604 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001605 && i >= regular_args - ufunc->uf_def_args.ga_len)
1606 {
1607 // assume v:none used for default argument value
1608 continue;
1609 }
1610 if (i < regular_args)
1611 {
1612 if (ufunc->uf_arg_types == NULL)
1613 continue;
1614 expected = ufunc->uf_arg_types[i];
1615 }
1616 else if (ufunc->uf_va_type == NULL
1617 || ufunc->uf_va_type == &t_list_any)
1618 // possibly a lambda or "...: any"
1619 expected = &t_any;
1620 else
1621 expected = ufunc->uf_va_type->tt_member;
1622 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1623 TRUE, FALSE) == FAIL)
1624 {
1625 arg_type_mismatch(expected, actual, i + 1);
1626 return FAIL;
1627 }
1628 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001629 compile_type = get_compile_type(ufunc);
1630 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001631 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001632 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 return FAIL;
1634 }
1635 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1636 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001637 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001638 ufunc->uf_name);
1639 return FAIL;
1640 }
1641
1642 if ((isn = generate_instr(cctx,
1643 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1644 : ISN_UCALL)) == NULL)
1645 return FAIL;
1646 if (isn->isn_type == ISN_DCALL)
1647 {
1648 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1649 isn->isn_arg.dfunc.cdf_argcount = argcount;
1650 }
1651 else
1652 {
1653 // A user function may be deleted and redefined later, can't use the
1654 // ufunc pointer, need to look it up again at runtime.
1655 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1656 isn->isn_arg.ufunc.cuf_argcount = argcount;
1657 }
1658
Bram Moolenaar078a4612022-01-04 15:17:03 +00001659 // drop the argument types
1660 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001661
Bram Moolenaar078a4612022-01-04 15:17:03 +00001662 // add return type
1663 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001664}
1665
1666/*
1667 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1668 */
1669 int
1670generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1671{
1672 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001673
1674 RETURN_OK_IF_SKIP(cctx);
1675 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1676 return FAIL;
1677 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1678 isn->isn_arg.ufunc.cuf_argcount = argcount;
1679
Bram Moolenaar078a4612022-01-04 15:17:03 +00001680 // drop the argument types
1681 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001682
Bram Moolenaar078a4612022-01-04 15:17:03 +00001683 // add return value
1684 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001685}
1686
1687/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001688 * Check the arguments of function "type" against the types on the stack.
1689 * Returns OK or FAIL;
1690 */
1691 int
1692check_func_args_from_type(
1693 cctx_T *cctx,
1694 type_T *type,
1695 int argcount,
1696 int at_top,
1697 char_u *name)
1698{
1699 if (type->tt_argcount != -1)
1700 {
1701 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1702
1703 if (argcount < type->tt_min_argcount - varargs)
1704 {
1705 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1706 return FAIL;
1707 }
1708 if (!varargs && argcount > type->tt_argcount)
1709 {
1710 emsg_funcname(e_too_many_arguments_for_function_str, name);
1711 return FAIL;
1712 }
1713 if (type->tt_args != NULL)
1714 {
1715 int i;
1716
1717 for (i = 0; i < argcount; ++i)
1718 {
1719 int offset = -argcount + i - (at_top ? 0 : 1);
1720 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1721 type_T *expected;
1722
1723 if (varargs && i >= type->tt_argcount - 1)
1724 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1725 else if (i >= type->tt_min_argcount
1726 && actual->tt_type == VAR_SPECIAL)
1727 expected = &t_any;
1728 else
1729 expected = type->tt_args[i];
1730 if (need_type(actual, expected, offset, i + 1,
1731 cctx, TRUE, FALSE) == FAIL)
1732 {
1733 arg_type_mismatch(expected, actual, i + 1);
1734 return FAIL;
1735 }
1736 }
1737 }
1738 }
1739
1740 return OK;
1741}
1742/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001743 * Generate an ISN_PCALL instruction.
1744 * "type" is the type of the FuncRef.
1745 */
1746 int
1747generate_PCALL(
1748 cctx_T *cctx,
1749 int argcount,
1750 char_u *name,
1751 type_T *type,
1752 int at_top)
1753{
1754 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001755 type_T *ret_type;
1756
1757 RETURN_OK_IF_SKIP(cctx);
1758
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001759 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001760 ret_type = &t_any;
1761 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1762 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001763 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1764 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001765
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001766 ret_type = type->tt_member;
1767 if (ret_type == &t_unknown)
1768 // return type not known yet, use a runtime check
1769 ret_type = &t_any;
1770 }
1771 else
1772 {
1773 semsg(_(e_not_callable_type_str), name);
1774 return FAIL;
1775 }
1776
1777 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1778 return FAIL;
1779 isn->isn_arg.pfunc.cpf_top = at_top;
1780 isn->isn_arg.pfunc.cpf_argcount = argcount;
1781
Bram Moolenaar078a4612022-01-04 15:17:03 +00001782 // drop the arguments and the funcref/partial
1783 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001784
Bram Moolenaar078a4612022-01-04 15:17:03 +00001785 // push the return value
1786 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001787
1788 // If partial is above the arguments it must be cleared and replaced with
1789 // the return value.
1790 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1791 return FAIL;
1792
1793 return OK;
1794}
1795
1796/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001797 * Generate an ISN_DEFER instruction.
1798 */
1799 int
1800generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1801{
1802 isn_T *isn;
1803
1804 RETURN_OK_IF_SKIP(cctx);
1805 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1806 return FAIL;
1807 isn->isn_arg.defer.defer_var_idx = var_idx;
1808 isn->isn_arg.defer.defer_argcount = argcount;
1809 return OK;
1810}
1811
1812/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001813 * Generate an ISN_STRINGMEMBER instruction.
1814 */
1815 int
1816generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1817{
1818 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001819 type_T *type;
1820
1821 RETURN_OK_IF_SKIP(cctx);
1822 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1823 return FAIL;
1824 isn->isn_arg.string = vim_strnsave(name, len);
1825
1826 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001827 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001828 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001829 {
1830 char *tofree;
1831
1832 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1833 name, type_name(type, &tofree));
1834 vim_free(tofree);
1835 return FAIL;
1836 }
1837 // change dict type to dict member type
1838 if (type->tt_type == VAR_DICT)
1839 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001840 type_T *ntype = type->tt_member == &t_unknown
1841 ? &t_any : type->tt_member;
1842 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001843 }
1844
1845 return OK;
1846}
1847
1848/*
1849 * Generate an ISN_ECHO instruction.
1850 */
1851 int
1852generate_ECHO(cctx_T *cctx, int with_white, int count)
1853{
1854 isn_T *isn;
1855
1856 RETURN_OK_IF_SKIP(cctx);
1857 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1858 return FAIL;
1859 isn->isn_arg.echo.echo_with_white = with_white;
1860 isn->isn_arg.echo.echo_count = count;
1861
1862 return OK;
1863}
1864
1865/*
1866 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1867 */
1868 int
1869generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1870{
1871 isn_T *isn;
1872
1873 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1874 return FAIL;
1875 isn->isn_arg.number = count;
1876
1877 return OK;
1878}
1879
1880/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001881 * Generate an ISN_SOURCE instruction.
1882 */
1883 int
1884generate_SOURCE(cctx_T *cctx, int sid)
1885{
1886 isn_T *isn;
1887
1888 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1889 return FAIL;
1890 isn->isn_arg.number = sid;
1891
1892 return OK;
1893}
1894
1895/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001896 * Generate an ISN_PUT instruction.
1897 */
1898 int
1899generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1900{
1901 isn_T *isn;
1902
1903 RETURN_OK_IF_SKIP(cctx);
1904 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1905 return FAIL;
1906 isn->isn_arg.put.put_regname = regname;
1907 isn->isn_arg.put.put_lnum = lnum;
1908 return OK;
1909}
1910
1911/*
1912 * Generate an EXEC instruction that takes a string argument.
1913 * A copy is made of "line".
1914 */
1915 int
1916generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1917{
1918 isn_T *isn;
1919
1920 RETURN_OK_IF_SKIP(cctx);
1921 if ((isn = generate_instr(cctx, isntype)) == NULL)
1922 return FAIL;
1923 isn->isn_arg.string = vim_strsave(line);
1924 return OK;
1925}
1926
1927/*
1928 * Generate an EXEC instruction that takes a string argument.
1929 * "str" must be allocated, it is consumed.
1930 */
1931 int
1932generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1933{
1934 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001935 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001936
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001937 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001939 if ((isn = generate_instr(cctx, isntype)) == NULL)
1940 ret = FAIL;
1941 else
1942 {
1943 isn->isn_arg.string = str;
1944 return OK;
1945 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001946 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001947 vim_free(str);
1948 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001949}
1950
1951 int
1952generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1953{
1954 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955
1956 RETURN_OK_IF_SKIP(cctx);
1957 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1958 return FAIL;
1959 isn->isn_arg.string = vim_strsave(line);
1960
Bram Moolenaar078a4612022-01-04 15:17:03 +00001961 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001962}
1963
1964 int
1965generate_EXECCONCAT(cctx_T *cctx, int count)
1966{
1967 isn_T *isn;
1968
1969 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1970 return FAIL;
1971 isn->isn_arg.number = count;
1972 return OK;
1973}
1974
1975/*
1976 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1977 */
1978 int
1979generate_RANGE(cctx_T *cctx, char_u *range)
1980{
1981 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001982
1983 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1984 return FAIL;
1985 isn->isn_arg.string = range;
1986
Bram Moolenaar078a4612022-01-04 15:17:03 +00001987 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001988}
1989
1990 int
1991generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1992{
1993 isn_T *isn;
1994
1995 RETURN_OK_IF_SKIP(cctx);
1996 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1997 return FAIL;
1998 isn->isn_arg.unpack.unp_count = var_count;
1999 isn->isn_arg.unpack.unp_semicolon = semicolon;
2000 return OK;
2001}
2002
2003/*
2004 * Generate an instruction for any command modifiers.
2005 */
2006 int
2007generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2008{
2009 isn_T *isn;
2010
2011 if (has_cmdmod(cmod, FALSE))
2012 {
2013 cctx->ctx_has_cmdmod = TRUE;
2014
2015 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2016 return FAIL;
2017 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2018 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2019 return FAIL;
2020 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2021 // filter program now belongs to the instruction
2022 cmod->cmod_filter_regmatch.regprog = NULL;
2023 }
2024
2025 return OK;
2026}
2027
2028 int
2029generate_undo_cmdmods(cctx_T *cctx)
2030{
2031 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2032 return FAIL;
2033 cctx->ctx_has_cmdmod = FALSE;
2034 return OK;
2035}
2036
2037/*
2038 * Generate a STORE instruction for "dest", not being "dest_local".
2039 * Return FAIL when out of memory.
2040 */
2041 int
2042generate_store_var(
2043 cctx_T *cctx,
2044 assign_dest_T dest,
2045 int opt_flags,
2046 int vimvaridx,
2047 int scriptvar_idx,
2048 int scriptvar_sid,
2049 type_T *type,
2050 char_u *name)
2051{
2052 switch (dest)
2053 {
2054 case dest_option:
2055 return generate_STOREOPT(cctx, ISN_STOREOPT,
2056 skip_option_env_lead(name), opt_flags);
2057 case dest_func_option:
2058 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2059 skip_option_env_lead(name), opt_flags);
2060 case dest_global:
2061 // include g: with the name, easier to execute that way
2062 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2063 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2064 case dest_buffer:
2065 // include b: with the name, easier to execute that way
2066 return generate_STORE(cctx, ISN_STOREB, 0, name);
2067 case dest_window:
2068 // include w: with the name, easier to execute that way
2069 return generate_STORE(cctx, ISN_STOREW, 0, name);
2070 case dest_tab:
2071 // include t: with the name, easier to execute that way
2072 return generate_STORE(cctx, ISN_STORET, 0, name);
2073 case dest_env:
2074 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2075 case dest_reg:
2076 return generate_STORE(cctx, ISN_STOREREG,
2077 name[1] == '@' ? '"' : name[1], NULL);
2078 case dest_vimvar:
2079 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2080 case dest_script:
2081 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002082 {
2083 isntype_T isn_type = ISN_STORES;
2084
2085 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01002086 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2087 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2088 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002089 {
2090 // "import autoload './dir/script.vim'" - load script first
2091 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2092 return FAIL;
2093 isn_type = ISN_STOREEXPORT;
2094 }
2095
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002096 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002097 return generate_OLDSCRIPT(cctx, isn_type, name,
2098 scriptvar_sid, type);
2099 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002100 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
2101 scriptvar_sid, scriptvar_idx, type);
2102 case dest_local:
2103 case dest_expr:
2104 // cannot happen
2105 break;
2106 }
2107 return FAIL;
2108}
2109
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002110/*
2111 * Return TRUE when inside a "for" or "while" loop.
2112 */
2113 int
2114inside_loop_scope(cctx_T *cctx)
2115{
2116 scope_T *scope = cctx->ctx_scope;
2117
2118 for (;;)
2119 {
2120 if (scope == NULL)
2121 break;
2122 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2123 return TRUE;
2124 scope = scope->se_outer;
2125 }
2126 return FALSE;
2127}
2128
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002129 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002130generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002131{
2132 if (lhs->lhs_dest != dest_local)
2133 return generate_store_var(cctx, lhs->lhs_dest,
2134 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2135 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2136 lhs->lhs_type, lhs->lhs_name);
2137
2138 if (lhs->lhs_lvar != NULL)
2139 {
2140 garray_T *instr = &cctx->ctx_instr;
2141 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2142
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002143 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2144 // ISN_STORENR.
2145 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002146 if (lhs->lhs_lvar->lv_from_outer == 0
2147 && instr->ga_len == instr_count + 1
2148 && isn->isn_type == ISN_PUSHNR)
2149 {
2150 varnumber_T val = isn->isn_arg.number;
2151 garray_T *stack = &cctx->ctx_type_stack;
2152
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002153 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002154 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002155 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002156 --instr->ga_len;
2157 }
2158 else
2159 {
2160 isn->isn_type = ISN_STORENR;
2161 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2162 isn->isn_arg.storenr.stnr_val = val;
2163 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002164 if (stack->ga_len > 0)
2165 --stack->ga_len;
2166 }
2167 else if (lhs->lhs_lvar->lv_from_outer > 0)
2168 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002169 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002170 else
2171 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2172 }
2173 return OK;
2174}
2175
2176#if defined(FEAT_PROFILE) || defined(PROTO)
2177 void
2178may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2179{
2180 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2181 generate_instr(cctx, ISN_PROF_END);
2182}
2183#endif
2184
2185
2186/*
2187 * Delete an instruction, free what it contains.
2188 */
2189 void
2190delete_instr(isn_T *isn)
2191{
2192 switch (isn->isn_type)
2193 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002194 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002195 case ISN_DEF:
2196 case ISN_EXEC:
2197 case ISN_EXECRANGE:
2198 case ISN_EXEC_SPLIT:
2199 case ISN_LEGACY_EVAL:
2200 case ISN_LOADAUTO:
2201 case ISN_LOADB:
2202 case ISN_LOADENV:
2203 case ISN_LOADG:
2204 case ISN_LOADOPT:
2205 case ISN_LOADT:
2206 case ISN_LOADW:
2207 case ISN_LOCKUNLOCK:
2208 case ISN_PUSHEXC:
2209 case ISN_PUSHFUNC:
2210 case ISN_PUSHS:
2211 case ISN_RANGE:
2212 case ISN_STOREAUTO:
2213 case ISN_STOREB:
2214 case ISN_STOREENV:
2215 case ISN_STOREG:
2216 case ISN_STORET:
2217 case ISN_STOREW:
2218 case ISN_STRINGMEMBER:
2219 vim_free(isn->isn_arg.string);
2220 break;
2221
2222 case ISN_SUBSTITUTE:
2223 {
2224 int idx;
2225 isn_T *list = isn->isn_arg.subs.subs_instr;
2226
2227 vim_free(isn->isn_arg.subs.subs_cmd);
2228 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2229 delete_instr(list + idx);
2230 vim_free(list);
2231 }
2232 break;
2233
2234 case ISN_INSTR:
2235 {
2236 int idx;
2237 isn_T *list = isn->isn_arg.instr;
2238
2239 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2240 delete_instr(list + idx);
2241 vim_free(list);
2242 }
2243 break;
2244
2245 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002246 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002247 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002248 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002249 vim_free(isn->isn_arg.loadstore.ls_name);
2250 break;
2251
2252 case ISN_UNLET:
2253 case ISN_UNLETENV:
2254 vim_free(isn->isn_arg.unlet.ul_name);
2255 break;
2256
2257 case ISN_STOREOPT:
2258 case ISN_STOREFUNCOPT:
2259 vim_free(isn->isn_arg.storeopt.so_name);
2260 break;
2261
2262 case ISN_PUSHBLOB: // push blob isn_arg.blob
2263 blob_unref(isn->isn_arg.blob);
2264 break;
2265
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002266 case ISN_UCALL:
2267 vim_free(isn->isn_arg.ufunc.cuf_name);
2268 break;
2269
2270 case ISN_FUNCREF:
2271 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002272 funcref_T *funcref = &isn->isn_arg.funcref;
2273 funcref_extra_T *extra = funcref->fr_extra;
2274
2275 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002276 {
2277 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002278 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002279 ufunc_T *ufunc = dfunc->df_ufunc;
2280
2281 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2282 func_ptr_unref(ufunc);
2283 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002284 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002285 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002286 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287
2288 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002289 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002290 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002291 vim_free(name);
2292 }
2293 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002294 }
2295 }
2296 break;
2297
2298 case ISN_DCALL:
2299 {
2300 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002301 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002302
2303 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002304 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002305 func_ptr_unref(dfunc->df_ufunc);
2306 }
2307 break;
2308
2309 case ISN_NEWFUNC:
2310 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002311 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002312
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002313 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002314 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002315 ufunc_T *ufunc = find_func_even_dead(
2316 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002317
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002318 if (ufunc != NULL)
2319 {
2320 unlink_def_function(ufunc);
2321 func_ptr_unref(ufunc);
2322 }
2323
2324 vim_free(arg->nfa_lambda);
2325 vim_free(arg->nfa_global);
2326 vim_free(arg);
2327 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002328 }
2329 break;
2330
2331 case ISN_CHECKTYPE:
2332 case ISN_SETTYPE:
2333 free_type(isn->isn_arg.type.ct_type);
2334 break;
2335
2336 case ISN_CMDMOD:
2337 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002338 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002339 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2340 break;
2341
2342 case ISN_LOADSCRIPT:
2343 case ISN_STORESCRIPT:
2344 vim_free(isn->isn_arg.script.scriptref);
2345 break;
2346
2347 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002348 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002349 break;
2350
2351 case ISN_CEXPR_CORE:
2352 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2353 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2354 break;
2355
2356 case ISN_2BOOL:
2357 case ISN_2STRING:
2358 case ISN_2STRING_ANY:
2359 case ISN_ADDBLOB:
2360 case ISN_ADDLIST:
2361 case ISN_ANYINDEX:
2362 case ISN_ANYSLICE:
2363 case ISN_BCALL:
2364 case ISN_BLOBAPPEND:
2365 case ISN_BLOBINDEX:
2366 case ISN_BLOBSLICE:
2367 case ISN_CATCH:
2368 case ISN_CEXPR_AUCMD:
2369 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002370 case ISN_CLEARDICT:
2371 case ISN_CMDMOD_REV:
2372 case ISN_COMPAREANY:
2373 case ISN_COMPAREBLOB:
2374 case ISN_COMPAREBOOL:
2375 case ISN_COMPAREDICT:
2376 case ISN_COMPAREFLOAT:
2377 case ISN_COMPAREFUNC:
2378 case ISN_COMPARELIST:
2379 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002380 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002381 case ISN_COMPARESPECIAL:
2382 case ISN_COMPARESTRING:
2383 case ISN_CONCAT:
2384 case ISN_COND2BOOL:
2385 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002386 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002387 case ISN_DROP:
2388 case ISN_ECHO:
2389 case ISN_ECHOCONSOLE:
2390 case ISN_ECHOERR:
2391 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002392 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002393 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002394 case ISN_ENDTRY:
2395 case ISN_EXECCONCAT:
2396 case ISN_EXECUTE:
2397 case ISN_FINALLY:
2398 case ISN_FINISH:
2399 case ISN_FOR:
2400 case ISN_GETITEM:
2401 case ISN_JUMP:
2402 case ISN_JUMP_IF_ARG_SET:
2403 case ISN_LISTAPPEND:
2404 case ISN_LISTINDEX:
2405 case ISN_LISTSLICE:
2406 case ISN_LOAD:
2407 case ISN_LOADBDICT:
2408 case ISN_LOADGDICT:
2409 case ISN_LOADOUTER:
2410 case ISN_LOADREG:
2411 case ISN_LOADTDICT:
2412 case ISN_LOADV:
2413 case ISN_LOADWDICT:
2414 case ISN_LOCKCONST:
2415 case ISN_MEMBER:
2416 case ISN_NEGATENR:
2417 case ISN_NEWDICT:
2418 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002419 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002420 case ISN_OPANY:
2421 case ISN_OPFLOAT:
2422 case ISN_OPNR:
2423 case ISN_PCALL:
2424 case ISN_PCALL_END:
2425 case ISN_PROF_END:
2426 case ISN_PROF_START:
2427 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002428 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002429 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002430 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431 case ISN_PUSHNR:
2432 case ISN_PUSHSPEC:
2433 case ISN_PUT:
2434 case ISN_REDIREND:
2435 case ISN_REDIRSTART:
2436 case ISN_RETURN:
2437 case ISN_RETURN_VOID:
2438 case ISN_SHUFFLE:
2439 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002440 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 case ISN_STORE:
2442 case ISN_STOREINDEX:
2443 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002444 case ISN_STOREOUTER:
2445 case ISN_STORERANGE:
2446 case ISN_STOREREG:
2447 case ISN_STOREV:
2448 case ISN_STRINDEX:
2449 case ISN_STRSLICE:
2450 case ISN_THROW:
2451 case ISN_TRYCONT:
2452 case ISN_UNLETINDEX:
2453 case ISN_UNLETRANGE:
2454 case ISN_UNPACK:
2455 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002456 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002457 // nothing allocated
2458 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002459 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002460}
2461
2462 void
2463clear_instr_ga(garray_T *gap)
2464{
2465 int idx;
2466
2467 for (idx = 0; idx < gap->ga_len; ++idx)
2468 delete_instr(((isn_T *)gap->ga_data) + idx);
2469 ga_clear(gap);
2470}
2471
2472
2473#endif // defined(FEAT_EVAL)