blob: 4407d5924efa6a758cb825fc1d0baa05431e909c [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 Moolenaarcc341812022-09-19 15:54:34 +01001000 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001001 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001002 type_T *type)
1003{
1004 isn_T *isn;
1005
1006 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001007 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001008 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001009 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1010 {
1011 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001012 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001013 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001014 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001015 }
1016 else
1017 {
1018 isn->isn_arg.outer.outer_idx = idx;
1019 isn->isn_arg.outer.outer_depth = nesting;
1020 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001021
1022 return OK;
1023}
1024
1025/*
1026 * Generate an ISN_LOADV instruction for v:var.
1027 */
1028 int
1029generate_LOADV(
1030 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001031 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001032{
1033 int di_flags;
1034 int vidx = find_vim_var(name, &di_flags);
1035 type_T *type;
1036
1037 RETURN_OK_IF_SKIP(cctx);
1038 if (vidx < 0)
1039 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001040 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001041 return FAIL;
1042 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001043 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001044 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1045}
1046
1047/*
1048 * Generate an ISN_UNLET instruction.
1049 */
1050 int
1051generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1052{
1053 isn_T *isn;
1054
1055 RETURN_OK_IF_SKIP(cctx);
1056 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1057 return FAIL;
1058 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1059 isn->isn_arg.unlet.ul_forceit = forceit;
1060
1061 return OK;
1062}
1063
1064/*
1065 * Generate an ISN_LOCKCONST instruction.
1066 */
1067 int
1068generate_LOCKCONST(cctx_T *cctx)
1069{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001070 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001071 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001072 return FAIL;
1073 return OK;
1074}
1075
1076/*
1077 * Generate an ISN_LOADS instruction.
1078 */
1079 int
1080generate_OLDSCRIPT(
1081 cctx_T *cctx,
1082 isntype_T isn_type,
1083 char_u *name,
1084 int sid,
1085 type_T *type)
1086{
1087 isn_T *isn;
1088
1089 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001090 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001091 isn = generate_instr_type(cctx, isn_type, type);
1092 else
1093 isn = generate_instr_drop(cctx, isn_type, 1);
1094 if (isn == NULL)
1095 return FAIL;
1096 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1097 isn->isn_arg.loadstore.ls_sid = sid;
1098
1099 return OK;
1100}
1101
1102/*
1103 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1104 */
1105 int
1106generate_VIM9SCRIPT(
1107 cctx_T *cctx,
1108 isntype_T isn_type,
1109 int sid,
1110 int idx,
1111 type_T *type)
1112{
1113 isn_T *isn;
1114 scriptref_T *sref;
1115 scriptitem_T *si = SCRIPT_ITEM(sid);
1116
1117 RETURN_OK_IF_SKIP(cctx);
1118 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001119 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001120 else
1121 isn = generate_instr_drop(cctx, isn_type, 1);
1122 if (isn == NULL)
1123 return FAIL;
1124
1125 // This requires three arguments, which doesn't fit in an instruction, thus
1126 // we need to allocate a struct for this.
1127 sref = ALLOC_ONE(scriptref_T);
1128 if (sref == NULL)
1129 return FAIL;
1130 isn->isn_arg.script.scriptref = sref;
1131 sref->sref_sid = sid;
1132 sref->sref_idx = idx;
1133 sref->sref_seq = si->sn_script_seq;
1134 sref->sref_type = type;
1135 return OK;
1136}
1137
1138/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001139 * Generate an ISN_NEWLIST instruction for "count" items.
1140 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141 */
1142 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001143generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144{
1145 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001146 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001147 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001148 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001149
1150 RETURN_OK_IF_SKIP(cctx);
1151 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1152 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001153 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154
Bram Moolenaar078a4612022-01-04 15:17:03 +00001155 // Get the member type and the declared member type from all the items on
1156 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001157 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001158 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001159 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001160
1161 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001162 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001163
1164 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001165 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001166}
1167
1168/*
1169 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001170 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001171 */
1172 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001173generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001174{
1175 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001176 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001177 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001178 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179
1180 RETURN_OK_IF_SKIP(cctx);
1181 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1182 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001183 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001184
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001185 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001186 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001187 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188
1189 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001190 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001191
1192 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001193 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001194}
1195
1196/*
1197 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001198 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001199 * If variables were declared inside a loop "loop_var_idx" is the index of the
1200 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001201 */
1202 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001203generate_FUNCREF(
1204 cctx_T *cctx,
1205 ufunc_T *ufunc,
1206 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001207{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001208 isn_T *isn;
1209 type_T *type;
1210 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001211 loopvarinfo_T loopinfo;
1212 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001213
1214 RETURN_OK_IF_SKIP(cctx);
1215 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1216 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001217 if (isnp != NULL)
1218 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001219
Bram Moolenaarcc341812022-09-19 15:54:34 +01001220 has_vars = get_loop_var_info(cctx, &loopinfo);
1221 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001222 {
1223 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1224 if (extra == NULL)
1225 return FAIL;
1226 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001227 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001228 }
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;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001233
1234 // Reserve an extra variable to keep track of the number of closures
1235 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001236 cctx->ctx_has_closure = 1;
1237
1238 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001239 // the nested context, including this one. But not a function defined at
1240 // the script level.
1241 if ((ufunc->uf_flags & FC_CLOSURE)
1242 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001243 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1244
Bram Moolenaar078a4612022-01-04 15:17:03 +00001245 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1246 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001247}
1248
1249/*
1250 * Generate an ISN_NEWFUNC instruction.
1251 * "lambda_name" and "func_name" must be in allocated memory and will be
1252 * consumed.
1253 */
1254 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001255generate_NEWFUNC(
1256 cctx_T *cctx,
1257 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001258 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001259{
1260 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001261 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001262
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001263 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001264 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001265 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1266 ret = FAIL;
1267 else
1268 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001269 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1270
1271 if (arg == NULL)
1272 ret = FAIL;
1273 else
1274 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001275 // Reserve an extra variable to keep track of the number of
1276 // closures created.
1277 cctx->ctx_has_closure = 1;
1278
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001279 isn->isn_arg.newfunc.nf_arg = arg;
1280 arg->nfa_lambda = lambda_name;
1281 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001282 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001283 return OK;
1284 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001285 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001286 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001287 vim_free(lambda_name);
1288 vim_free(func_name);
1289 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001290}
1291
1292/*
1293 * Generate an ISN_DEF instruction: list functions
1294 */
1295 int
1296generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1297{
1298 isn_T *isn;
1299
1300 RETURN_OK_IF_SKIP(cctx);
1301 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1302 return FAIL;
1303 if (len > 0)
1304 {
1305 isn->isn_arg.string = vim_strnsave(name, len);
1306 if (isn->isn_arg.string == NULL)
1307 return FAIL;
1308 }
1309 return OK;
1310}
1311
1312/*
1313 * Generate an ISN_JUMP instruction.
1314 */
1315 int
1316generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1317{
1318 isn_T *isn;
1319 garray_T *stack = &cctx->ctx_type_stack;
1320
1321 RETURN_OK_IF_SKIP(cctx);
1322 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1323 return FAIL;
1324 isn->isn_arg.jump.jump_when = when;
1325 isn->isn_arg.jump.jump_where = where;
1326
1327 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1328 --stack->ga_len;
1329
1330 return OK;
1331}
1332
1333/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001334 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1335 */
1336 int
1337generate_WHILE(cctx_T *cctx, int funcref_idx)
1338{
1339 isn_T *isn;
1340 garray_T *stack = &cctx->ctx_type_stack;
1341
1342 RETURN_OK_IF_SKIP(cctx);
1343 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1344 return FAIL;
1345 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1346 isn->isn_arg.whileloop.while_end = 0; // filled in later
1347
1348 if (stack->ga_len > 0)
1349 --stack->ga_len;
1350
1351 return OK;
1352}
1353
1354/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001355 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1356 */
1357 int
1358generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1359{
1360 isn_T *isn;
1361
1362 RETURN_OK_IF_SKIP(cctx);
1363 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1364 return FAIL;
1365 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1366 // jump_where is set later
1367 return OK;
1368}
1369
1370 int
1371generate_FOR(cctx_T *cctx, int loop_idx)
1372{
1373 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001374
1375 RETURN_OK_IF_SKIP(cctx);
1376 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1377 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001378 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001379
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001380 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001381 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001383
1384 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001385generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001386{
1387 isn_T *isn;
1388
1389 RETURN_OK_IF_SKIP(cctx);
1390 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1391 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001392 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1393 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1394 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001395 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001396 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001397 return OK;
1398}
1399
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001400/*
1401 * Generate an ISN_TRYCONT instruction.
1402 */
1403 int
1404generate_TRYCONT(cctx_T *cctx, int levels, int where)
1405{
1406 isn_T *isn;
1407
1408 RETURN_OK_IF_SKIP(cctx);
1409 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1410 return FAIL;
1411 isn->isn_arg.trycont.tct_levels = levels;
1412 isn->isn_arg.trycont.tct_where = where;
1413
1414 return OK;
1415}
1416
Bram Moolenaar16900322022-09-08 19:51:45 +01001417/*
1418 * Check "argount" arguments and their types on the type stack.
1419 * Give an error and return FAIL if something is wrong.
1420 * When "method_call" is NULL no code is generated.
1421 */
1422 int
1423check_internal_func_args(
1424 cctx_T *cctx,
1425 int func_idx,
1426 int argcount,
1427 int method_call,
1428 type2_T **argtypes,
1429 type2_T *shuffled_argtypes)
1430{
1431 garray_T *stack = &cctx->ctx_type_stack;
1432 int argoff = check_internal_func(func_idx, argcount);
1433
1434 if (argoff < 0)
1435 return FAIL;
1436
1437 if (method_call && argoff > 1)
1438 {
1439 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1440
1441 if (isn == NULL)
1442 return FAIL;
1443 isn->isn_arg.shuffle.shfl_item = argcount;
1444 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1445 }
1446
1447 if (argcount > 0)
1448 {
1449 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1450
1451 // Check the types of the arguments.
1452 if (method_call && argoff > 1)
1453 {
1454 int i;
1455
1456 for (i = 0; i < argcount; ++i)
1457 shuffled_argtypes[i] = (i < argoff - 1)
1458 ? typep[i + 1]
1459 : (i == argoff - 1) ? typep[0] : typep[i];
1460 *argtypes = shuffled_argtypes;
1461 }
1462 else
1463 {
1464 int i;
1465
1466 for (i = 0; i < argcount; ++i)
1467 shuffled_argtypes[i] = typep[i];
1468 *argtypes = shuffled_argtypes;
1469 }
1470 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1471 cctx) == FAIL)
1472 return FAIL;
1473 }
1474 return OK;
1475}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001476
1477/*
1478 * Generate an ISN_BCALL instruction.
1479 * "method_call" is TRUE for "value->method()"
1480 * Return FAIL if the number of arguments is wrong.
1481 */
1482 int
1483generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1484{
1485 isn_T *isn;
1486 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001487 type2_T *argtypes = NULL;
1488 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1489 type2_T *maptype = NULL;
1490 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001491 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001492
1493 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001494
1495 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1496 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001497 return FAIL;
1498
Bram Moolenaar16900322022-09-08 19:51:45 +01001499 if (internal_func_is_map(func_idx))
1500 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001501
1502 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1503 return FAIL;
1504 isn->isn_arg.bfunc.cbf_idx = func_idx;
1505 isn->isn_arg.bfunc.cbf_argcount = argcount;
1506
1507 // Drop the argument types and push the return type.
1508 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001509 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1510 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001511 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001512
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001513 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1514 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001515 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001516 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001517
1518 return OK;
1519}
1520
1521/*
1522 * Generate an ISN_LISTAPPEND instruction. Works like add().
1523 * Argument count is already checked.
1524 */
1525 int
1526generate_LISTAPPEND(cctx_T *cctx)
1527{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001528 type_T *list_type;
1529 type_T *item_type;
1530 type_T *expected;
1531
1532 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001533 // For checking the item type we use the declared type of the list and the
1534 // current type of the added item, adding a string to [1, 2] is OK.
1535 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001536 if (arg_type_modifiable(list_type, 1) == FAIL)
1537 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001538 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001539 expected = list_type->tt_member;
1540 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1541 return FAIL;
1542
1543 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1544 return FAIL;
1545
Bram Moolenaar078a4612022-01-04 15:17:03 +00001546 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001547 return OK;
1548}
1549
1550/*
1551 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1552 * Argument count is already checked.
1553 */
1554 int
1555generate_BLOBAPPEND(cctx_T *cctx)
1556{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001557 type_T *item_type;
1558
Bram Moolenaarfa103972022-09-29 19:14:42 +01001559 // Caller already checked that blob_type is a blob, check it is modifiable.
1560 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1561 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001562 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001563 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1564 return FAIL;
1565
1566 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1567 return FAIL;
1568
Bram Moolenaar078a4612022-01-04 15:17:03 +00001569 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001570 return OK;
1571}
1572
1573/*
1574 * Generate an ISN_DCALL or ISN_UCALL instruction.
1575 * Return FAIL if the number of arguments is wrong.
1576 */
1577 int
1578generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1579{
1580 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001581 int regular_args = ufunc->uf_args.ga_len;
1582 int argcount = pushed_argcount;
1583
1584 RETURN_OK_IF_SKIP(cctx);
1585 if (argcount > regular_args && !has_varargs(ufunc))
1586 {
1587 semsg(_(e_too_many_arguments_for_function_str),
1588 printable_func_name(ufunc));
1589 return FAIL;
1590 }
1591 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1592 {
1593 semsg(_(e_not_enough_arguments_for_function_str),
1594 printable_func_name(ufunc));
1595 return FAIL;
1596 }
1597
1598 if (ufunc->uf_def_status != UF_NOT_COMPILED
1599 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1600 {
1601 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001602 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001603
1604 for (i = 0; i < argcount; ++i)
1605 {
1606 type_T *expected;
1607 type_T *actual;
1608
Bram Moolenaar078a4612022-01-04 15:17:03 +00001609 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001610 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001611 && i >= regular_args - ufunc->uf_def_args.ga_len)
1612 {
1613 // assume v:none used for default argument value
1614 continue;
1615 }
1616 if (i < regular_args)
1617 {
1618 if (ufunc->uf_arg_types == NULL)
1619 continue;
1620 expected = ufunc->uf_arg_types[i];
1621 }
1622 else if (ufunc->uf_va_type == NULL
1623 || ufunc->uf_va_type == &t_list_any)
1624 // possibly a lambda or "...: any"
1625 expected = &t_any;
1626 else
1627 expected = ufunc->uf_va_type->tt_member;
1628 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1629 TRUE, FALSE) == FAIL)
1630 {
1631 arg_type_mismatch(expected, actual, i + 1);
1632 return FAIL;
1633 }
1634 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001635 compile_type = get_compile_type(ufunc);
1636 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001637 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001638 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001639 return FAIL;
1640 }
1641 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1642 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001643 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001644 ufunc->uf_name);
1645 return FAIL;
1646 }
1647
1648 if ((isn = generate_instr(cctx,
1649 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1650 : ISN_UCALL)) == NULL)
1651 return FAIL;
1652 if (isn->isn_type == ISN_DCALL)
1653 {
1654 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1655 isn->isn_arg.dfunc.cdf_argcount = argcount;
1656 }
1657 else
1658 {
1659 // A user function may be deleted and redefined later, can't use the
1660 // ufunc pointer, need to look it up again at runtime.
1661 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1662 isn->isn_arg.ufunc.cuf_argcount = argcount;
1663 }
1664
Bram Moolenaar078a4612022-01-04 15:17:03 +00001665 // drop the argument types
1666 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001667
Bram Moolenaar078a4612022-01-04 15:17:03 +00001668 // add return type
1669 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001670}
1671
1672/*
1673 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1674 */
1675 int
1676generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1677{
1678 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001679
1680 RETURN_OK_IF_SKIP(cctx);
1681 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1682 return FAIL;
1683 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1684 isn->isn_arg.ufunc.cuf_argcount = argcount;
1685
Bram Moolenaar078a4612022-01-04 15:17:03 +00001686 // drop the argument types
1687 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688
Bram Moolenaar078a4612022-01-04 15:17:03 +00001689 // add return value
1690 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001691}
1692
1693/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001694 * Check the arguments of function "type" against the types on the stack.
1695 * Returns OK or FAIL;
1696 */
1697 int
1698check_func_args_from_type(
1699 cctx_T *cctx,
1700 type_T *type,
1701 int argcount,
1702 int at_top,
1703 char_u *name)
1704{
1705 if (type->tt_argcount != -1)
1706 {
1707 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1708
1709 if (argcount < type->tt_min_argcount - varargs)
1710 {
1711 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1712 return FAIL;
1713 }
1714 if (!varargs && argcount > type->tt_argcount)
1715 {
1716 emsg_funcname(e_too_many_arguments_for_function_str, name);
1717 return FAIL;
1718 }
1719 if (type->tt_args != NULL)
1720 {
1721 int i;
1722
1723 for (i = 0; i < argcount; ++i)
1724 {
1725 int offset = -argcount + i - (at_top ? 0 : 1);
1726 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1727 type_T *expected;
1728
1729 if (varargs && i >= type->tt_argcount - 1)
1730 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1731 else if (i >= type->tt_min_argcount
1732 && actual->tt_type == VAR_SPECIAL)
1733 expected = &t_any;
1734 else
1735 expected = type->tt_args[i];
1736 if (need_type(actual, expected, offset, i + 1,
1737 cctx, TRUE, FALSE) == FAIL)
1738 {
1739 arg_type_mismatch(expected, actual, i + 1);
1740 return FAIL;
1741 }
1742 }
1743 }
1744 }
1745
1746 return OK;
1747}
1748/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001749 * Generate an ISN_PCALL instruction.
1750 * "type" is the type of the FuncRef.
1751 */
1752 int
1753generate_PCALL(
1754 cctx_T *cctx,
1755 int argcount,
1756 char_u *name,
1757 type_T *type,
1758 int at_top)
1759{
1760 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001761 type_T *ret_type;
1762
1763 RETURN_OK_IF_SKIP(cctx);
1764
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001765 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001766 ret_type = &t_any;
1767 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1768 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001769 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1770 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001771
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001772 ret_type = type->tt_member;
1773 if (ret_type == &t_unknown)
1774 // return type not known yet, use a runtime check
1775 ret_type = &t_any;
1776 }
1777 else
1778 {
1779 semsg(_(e_not_callable_type_str), name);
1780 return FAIL;
1781 }
1782
1783 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1784 return FAIL;
1785 isn->isn_arg.pfunc.cpf_top = at_top;
1786 isn->isn_arg.pfunc.cpf_argcount = argcount;
1787
Bram Moolenaar078a4612022-01-04 15:17:03 +00001788 // drop the arguments and the funcref/partial
1789 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001790
Bram Moolenaar078a4612022-01-04 15:17:03 +00001791 // push the return value
1792 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001793
1794 // If partial is above the arguments it must be cleared and replaced with
1795 // the return value.
1796 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1797 return FAIL;
1798
1799 return OK;
1800}
1801
1802/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001803 * Generate an ISN_DEFER instruction.
1804 */
1805 int
1806generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1807{
1808 isn_T *isn;
1809
1810 RETURN_OK_IF_SKIP(cctx);
1811 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1812 return FAIL;
1813 isn->isn_arg.defer.defer_var_idx = var_idx;
1814 isn->isn_arg.defer.defer_argcount = argcount;
1815 return OK;
1816}
1817
1818/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001819 * Generate an ISN_STRINGMEMBER instruction.
1820 */
1821 int
1822generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1823{
1824 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 type_T *type;
1826
1827 RETURN_OK_IF_SKIP(cctx);
1828 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1829 return FAIL;
1830 isn->isn_arg.string = vim_strnsave(name, len);
1831
1832 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001833 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001834 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001835 {
1836 char *tofree;
1837
1838 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1839 name, type_name(type, &tofree));
1840 vim_free(tofree);
1841 return FAIL;
1842 }
1843 // change dict type to dict member type
1844 if (type->tt_type == VAR_DICT)
1845 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001846 type_T *ntype = type->tt_member == &t_unknown
1847 ? &t_any : type->tt_member;
1848 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849 }
1850
1851 return OK;
1852}
1853
1854/*
1855 * Generate an ISN_ECHO instruction.
1856 */
1857 int
1858generate_ECHO(cctx_T *cctx, int with_white, int count)
1859{
1860 isn_T *isn;
1861
1862 RETURN_OK_IF_SKIP(cctx);
1863 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1864 return FAIL;
1865 isn->isn_arg.echo.echo_with_white = with_white;
1866 isn->isn_arg.echo.echo_count = count;
1867
1868 return OK;
1869}
1870
1871/*
1872 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1873 */
1874 int
1875generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1876{
1877 isn_T *isn;
1878
1879 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1880 return FAIL;
1881 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001882 return OK;
1883}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001884
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001885/*
1886 * Generate an ISN_ECHOWINDOW instruction
1887 */
1888 int
1889generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
1890{
1891 isn_T *isn;
1892
1893 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
1894 return FAIL;
1895 isn->isn_arg.echowin.ewin_count = count;
1896 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001897 return OK;
1898}
1899
1900/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001901 * Generate an ISN_SOURCE instruction.
1902 */
1903 int
1904generate_SOURCE(cctx_T *cctx, int sid)
1905{
1906 isn_T *isn;
1907
1908 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1909 return FAIL;
1910 isn->isn_arg.number = sid;
1911
1912 return OK;
1913}
1914
1915/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001916 * Generate an ISN_PUT instruction.
1917 */
1918 int
1919generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1920{
1921 isn_T *isn;
1922
1923 RETURN_OK_IF_SKIP(cctx);
1924 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1925 return FAIL;
1926 isn->isn_arg.put.put_regname = regname;
1927 isn->isn_arg.put.put_lnum = lnum;
1928 return OK;
1929}
1930
1931/*
1932 * Generate an EXEC instruction that takes a string argument.
1933 * A copy is made of "line".
1934 */
1935 int
1936generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1937{
1938 isn_T *isn;
1939
1940 RETURN_OK_IF_SKIP(cctx);
1941 if ((isn = generate_instr(cctx, isntype)) == NULL)
1942 return FAIL;
1943 isn->isn_arg.string = vim_strsave(line);
1944 return OK;
1945}
1946
1947/*
1948 * Generate an EXEC instruction that takes a string argument.
1949 * "str" must be allocated, it is consumed.
1950 */
1951 int
1952generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1953{
1954 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001955 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001956
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001957 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001958 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001959 if ((isn = generate_instr(cctx, isntype)) == NULL)
1960 ret = FAIL;
1961 else
1962 {
1963 isn->isn_arg.string = str;
1964 return OK;
1965 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001966 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001967 vim_free(str);
1968 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001969}
1970
1971 int
1972generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1973{
1974 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001975
1976 RETURN_OK_IF_SKIP(cctx);
1977 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1978 return FAIL;
1979 isn->isn_arg.string = vim_strsave(line);
1980
Bram Moolenaar078a4612022-01-04 15:17:03 +00001981 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001982}
1983
1984 int
1985generate_EXECCONCAT(cctx_T *cctx, int count)
1986{
1987 isn_T *isn;
1988
1989 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1990 return FAIL;
1991 isn->isn_arg.number = count;
1992 return OK;
1993}
1994
1995/*
1996 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1997 */
1998 int
1999generate_RANGE(cctx_T *cctx, char_u *range)
2000{
2001 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002002
2003 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2004 return FAIL;
2005 isn->isn_arg.string = range;
2006
Bram Moolenaar078a4612022-01-04 15:17:03 +00002007 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002008}
2009
2010 int
2011generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2012{
2013 isn_T *isn;
2014
2015 RETURN_OK_IF_SKIP(cctx);
2016 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2017 return FAIL;
2018 isn->isn_arg.unpack.unp_count = var_count;
2019 isn->isn_arg.unpack.unp_semicolon = semicolon;
2020 return OK;
2021}
2022
2023/*
2024 * Generate an instruction for any command modifiers.
2025 */
2026 int
2027generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2028{
2029 isn_T *isn;
2030
2031 if (has_cmdmod(cmod, FALSE))
2032 {
2033 cctx->ctx_has_cmdmod = TRUE;
2034
2035 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2036 return FAIL;
2037 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2038 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2039 return FAIL;
2040 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2041 // filter program now belongs to the instruction
2042 cmod->cmod_filter_regmatch.regprog = NULL;
2043 }
2044
2045 return OK;
2046}
2047
2048 int
2049generate_undo_cmdmods(cctx_T *cctx)
2050{
2051 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2052 return FAIL;
2053 cctx->ctx_has_cmdmod = FALSE;
2054 return OK;
2055}
2056
2057/*
2058 * Generate a STORE instruction for "dest", not being "dest_local".
2059 * Return FAIL when out of memory.
2060 */
2061 int
2062generate_store_var(
2063 cctx_T *cctx,
2064 assign_dest_T dest,
2065 int opt_flags,
2066 int vimvaridx,
2067 int scriptvar_idx,
2068 int scriptvar_sid,
2069 type_T *type,
2070 char_u *name)
2071{
2072 switch (dest)
2073 {
2074 case dest_option:
2075 return generate_STOREOPT(cctx, ISN_STOREOPT,
2076 skip_option_env_lead(name), opt_flags);
2077 case dest_func_option:
2078 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2079 skip_option_env_lead(name), opt_flags);
2080 case dest_global:
2081 // include g: with the name, easier to execute that way
2082 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2083 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2084 case dest_buffer:
2085 // include b: with the name, easier to execute that way
2086 return generate_STORE(cctx, ISN_STOREB, 0, name);
2087 case dest_window:
2088 // include w: with the name, easier to execute that way
2089 return generate_STORE(cctx, ISN_STOREW, 0, name);
2090 case dest_tab:
2091 // include t: with the name, easier to execute that way
2092 return generate_STORE(cctx, ISN_STORET, 0, name);
2093 case dest_env:
2094 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2095 case dest_reg:
2096 return generate_STORE(cctx, ISN_STOREREG,
2097 name[1] == '@' ? '"' : name[1], NULL);
2098 case dest_vimvar:
2099 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2100 case dest_script:
2101 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002102 {
2103 isntype_T isn_type = ISN_STORES;
2104
2105 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01002106 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2107 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2108 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002109 {
2110 // "import autoload './dir/script.vim'" - load script first
2111 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2112 return FAIL;
2113 isn_type = ISN_STOREEXPORT;
2114 }
2115
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002116 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002117 return generate_OLDSCRIPT(cctx, isn_type, name,
2118 scriptvar_sid, type);
2119 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002120 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
2121 scriptvar_sid, scriptvar_idx, type);
2122 case dest_local:
2123 case dest_expr:
2124 // cannot happen
2125 break;
2126 }
2127 return FAIL;
2128}
2129
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002130/*
2131 * Return TRUE when inside a "for" or "while" loop.
2132 */
2133 int
2134inside_loop_scope(cctx_T *cctx)
2135{
2136 scope_T *scope = cctx->ctx_scope;
2137
2138 for (;;)
2139 {
2140 if (scope == NULL)
2141 break;
2142 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2143 return TRUE;
2144 scope = scope->se_outer;
2145 }
2146 return FALSE;
2147}
2148
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002149 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002150generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002151{
2152 if (lhs->lhs_dest != dest_local)
2153 return generate_store_var(cctx, lhs->lhs_dest,
2154 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2155 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2156 lhs->lhs_type, lhs->lhs_name);
2157
2158 if (lhs->lhs_lvar != NULL)
2159 {
2160 garray_T *instr = &cctx->ctx_instr;
2161 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2162
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002163 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2164 // ISN_STORENR.
2165 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002166 if (lhs->lhs_lvar->lv_from_outer == 0
2167 && instr->ga_len == instr_count + 1
2168 && isn->isn_type == ISN_PUSHNR)
2169 {
2170 varnumber_T val = isn->isn_arg.number;
2171 garray_T *stack = &cctx->ctx_type_stack;
2172
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002173 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002174 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002175 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002176 --instr->ga_len;
2177 }
2178 else
2179 {
2180 isn->isn_type = ISN_STORENR;
2181 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2182 isn->isn_arg.storenr.stnr_val = val;
2183 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002184 if (stack->ga_len > 0)
2185 --stack->ga_len;
2186 }
2187 else if (lhs->lhs_lvar->lv_from_outer > 0)
2188 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002189 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002190 else
2191 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2192 }
2193 return OK;
2194}
2195
2196#if defined(FEAT_PROFILE) || defined(PROTO)
2197 void
2198may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2199{
2200 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2201 generate_instr(cctx, ISN_PROF_END);
2202}
2203#endif
2204
2205
2206/*
2207 * Delete an instruction, free what it contains.
2208 */
2209 void
2210delete_instr(isn_T *isn)
2211{
2212 switch (isn->isn_type)
2213 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002214 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002215 case ISN_DEF:
2216 case ISN_EXEC:
2217 case ISN_EXECRANGE:
2218 case ISN_EXEC_SPLIT:
2219 case ISN_LEGACY_EVAL:
2220 case ISN_LOADAUTO:
2221 case ISN_LOADB:
2222 case ISN_LOADENV:
2223 case ISN_LOADG:
2224 case ISN_LOADOPT:
2225 case ISN_LOADT:
2226 case ISN_LOADW:
2227 case ISN_LOCKUNLOCK:
2228 case ISN_PUSHEXC:
2229 case ISN_PUSHFUNC:
2230 case ISN_PUSHS:
2231 case ISN_RANGE:
2232 case ISN_STOREAUTO:
2233 case ISN_STOREB:
2234 case ISN_STOREENV:
2235 case ISN_STOREG:
2236 case ISN_STORET:
2237 case ISN_STOREW:
2238 case ISN_STRINGMEMBER:
2239 vim_free(isn->isn_arg.string);
2240 break;
2241
2242 case ISN_SUBSTITUTE:
2243 {
2244 int idx;
2245 isn_T *list = isn->isn_arg.subs.subs_instr;
2246
2247 vim_free(isn->isn_arg.subs.subs_cmd);
2248 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2249 delete_instr(list + idx);
2250 vim_free(list);
2251 }
2252 break;
2253
2254 case ISN_INSTR:
2255 {
2256 int idx;
2257 isn_T *list = isn->isn_arg.instr;
2258
2259 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2260 delete_instr(list + idx);
2261 vim_free(list);
2262 }
2263 break;
2264
2265 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002266 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002267 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002268 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002269 vim_free(isn->isn_arg.loadstore.ls_name);
2270 break;
2271
2272 case ISN_UNLET:
2273 case ISN_UNLETENV:
2274 vim_free(isn->isn_arg.unlet.ul_name);
2275 break;
2276
2277 case ISN_STOREOPT:
2278 case ISN_STOREFUNCOPT:
2279 vim_free(isn->isn_arg.storeopt.so_name);
2280 break;
2281
2282 case ISN_PUSHBLOB: // push blob isn_arg.blob
2283 blob_unref(isn->isn_arg.blob);
2284 break;
2285
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002286 case ISN_UCALL:
2287 vim_free(isn->isn_arg.ufunc.cuf_name);
2288 break;
2289
2290 case ISN_FUNCREF:
2291 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002292 funcref_T *funcref = &isn->isn_arg.funcref;
2293 funcref_extra_T *extra = funcref->fr_extra;
2294
2295 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002296 {
2297 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002298 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002299 ufunc_T *ufunc = dfunc->df_ufunc;
2300
2301 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2302 func_ptr_unref(ufunc);
2303 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002304 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002305 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002306 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002307
2308 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002309 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002310 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002311 vim_free(name);
2312 }
2313 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002314 }
2315 }
2316 break;
2317
2318 case ISN_DCALL:
2319 {
2320 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002321 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002322
2323 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002324 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002325 func_ptr_unref(dfunc->df_ufunc);
2326 }
2327 break;
2328
2329 case ISN_NEWFUNC:
2330 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002331 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002332
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002333 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002334 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002335 ufunc_T *ufunc = find_func_even_dead(
2336 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002337
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002338 if (ufunc != NULL)
2339 {
2340 unlink_def_function(ufunc);
2341 func_ptr_unref(ufunc);
2342 }
2343
2344 vim_free(arg->nfa_lambda);
2345 vim_free(arg->nfa_global);
2346 vim_free(arg);
2347 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002348 }
2349 break;
2350
2351 case ISN_CHECKTYPE:
2352 case ISN_SETTYPE:
2353 free_type(isn->isn_arg.type.ct_type);
2354 break;
2355
2356 case ISN_CMDMOD:
2357 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002358 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002359 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2360 break;
2361
2362 case ISN_LOADSCRIPT:
2363 case ISN_STORESCRIPT:
2364 vim_free(isn->isn_arg.script.scriptref);
2365 break;
2366
2367 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002368 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002369 break;
2370
2371 case ISN_CEXPR_CORE:
2372 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2373 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2374 break;
2375
2376 case ISN_2BOOL:
2377 case ISN_2STRING:
2378 case ISN_2STRING_ANY:
2379 case ISN_ADDBLOB:
2380 case ISN_ADDLIST:
2381 case ISN_ANYINDEX:
2382 case ISN_ANYSLICE:
2383 case ISN_BCALL:
2384 case ISN_BLOBAPPEND:
2385 case ISN_BLOBINDEX:
2386 case ISN_BLOBSLICE:
2387 case ISN_CATCH:
2388 case ISN_CEXPR_AUCMD:
2389 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002390 case ISN_CLEARDICT:
2391 case ISN_CMDMOD_REV:
2392 case ISN_COMPAREANY:
2393 case ISN_COMPAREBLOB:
2394 case ISN_COMPAREBOOL:
2395 case ISN_COMPAREDICT:
2396 case ISN_COMPAREFLOAT:
2397 case ISN_COMPAREFUNC:
2398 case ISN_COMPARELIST:
2399 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002400 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002401 case ISN_COMPARESPECIAL:
2402 case ISN_COMPARESTRING:
2403 case ISN_CONCAT:
2404 case ISN_COND2BOOL:
2405 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002406 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002407 case ISN_DROP:
2408 case ISN_ECHO:
2409 case ISN_ECHOCONSOLE:
2410 case ISN_ECHOERR:
2411 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002412 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002413 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002414 case ISN_ENDTRY:
2415 case ISN_EXECCONCAT:
2416 case ISN_EXECUTE:
2417 case ISN_FINALLY:
2418 case ISN_FINISH:
2419 case ISN_FOR:
2420 case ISN_GETITEM:
2421 case ISN_JUMP:
2422 case ISN_JUMP_IF_ARG_SET:
2423 case ISN_LISTAPPEND:
2424 case ISN_LISTINDEX:
2425 case ISN_LISTSLICE:
2426 case ISN_LOAD:
2427 case ISN_LOADBDICT:
2428 case ISN_LOADGDICT:
2429 case ISN_LOADOUTER:
2430 case ISN_LOADREG:
2431 case ISN_LOADTDICT:
2432 case ISN_LOADV:
2433 case ISN_LOADWDICT:
2434 case ISN_LOCKCONST:
2435 case ISN_MEMBER:
2436 case ISN_NEGATENR:
2437 case ISN_NEWDICT:
2438 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002439 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002440 case ISN_OPANY:
2441 case ISN_OPFLOAT:
2442 case ISN_OPNR:
2443 case ISN_PCALL:
2444 case ISN_PCALL_END:
2445 case ISN_PROF_END:
2446 case ISN_PROF_START:
2447 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002448 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002449 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002450 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002451 case ISN_PUSHNR:
2452 case ISN_PUSHSPEC:
2453 case ISN_PUT:
2454 case ISN_REDIREND:
2455 case ISN_REDIRSTART:
2456 case ISN_RETURN:
2457 case ISN_RETURN_VOID:
2458 case ISN_SHUFFLE:
2459 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002460 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002461 case ISN_STORE:
2462 case ISN_STOREINDEX:
2463 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002464 case ISN_STOREOUTER:
2465 case ISN_STORERANGE:
2466 case ISN_STOREREG:
2467 case ISN_STOREV:
2468 case ISN_STRINDEX:
2469 case ISN_STRSLICE:
2470 case ISN_THROW:
2471 case ISN_TRYCONT:
2472 case ISN_UNLETINDEX:
2473 case ISN_UNLETRANGE:
2474 case ISN_UNPACK:
2475 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002476 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002477 // nothing allocated
2478 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002479 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002480}
2481
2482 void
2483clear_instr_ga(garray_T *gap)
2484{
2485 int idx;
2486
2487 for (idx = 0; idx < gap->ga_len; ++idx)
2488 delete_instr(((isn_T *)gap->ga_data) + idx);
2489 ga_clear(gap);
2490}
2491
2492
2493#endif // defined(FEAT_EVAL)