blob: 85a49a893446f9624748c4986b64841509196c63 [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 Moolenaar078a4612022-01-04 15:17:03 +00001536 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001537 expected = list_type->tt_member;
1538 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1539 return FAIL;
1540
1541 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1542 return FAIL;
1543
Bram Moolenaar078a4612022-01-04 15:17:03 +00001544 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001545 return OK;
1546}
1547
1548/*
1549 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1550 * Argument count is already checked.
1551 */
1552 int
1553generate_BLOBAPPEND(cctx_T *cctx)
1554{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001555 type_T *item_type;
1556
1557 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001558 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001559 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1560 return FAIL;
1561
1562 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1563 return FAIL;
1564
Bram Moolenaar078a4612022-01-04 15:17:03 +00001565 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566 return OK;
1567}
1568
1569/*
1570 * Generate an ISN_DCALL or ISN_UCALL instruction.
1571 * Return FAIL if the number of arguments is wrong.
1572 */
1573 int
1574generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1575{
1576 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001577 int regular_args = ufunc->uf_args.ga_len;
1578 int argcount = pushed_argcount;
1579
1580 RETURN_OK_IF_SKIP(cctx);
1581 if (argcount > regular_args && !has_varargs(ufunc))
1582 {
1583 semsg(_(e_too_many_arguments_for_function_str),
1584 printable_func_name(ufunc));
1585 return FAIL;
1586 }
1587 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1588 {
1589 semsg(_(e_not_enough_arguments_for_function_str),
1590 printable_func_name(ufunc));
1591 return FAIL;
1592 }
1593
1594 if (ufunc->uf_def_status != UF_NOT_COMPILED
1595 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1596 {
1597 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001598 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001599
1600 for (i = 0; i < argcount; ++i)
1601 {
1602 type_T *expected;
1603 type_T *actual;
1604
Bram Moolenaar078a4612022-01-04 15:17:03 +00001605 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001606 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001607 && i >= regular_args - ufunc->uf_def_args.ga_len)
1608 {
1609 // assume v:none used for default argument value
1610 continue;
1611 }
1612 if (i < regular_args)
1613 {
1614 if (ufunc->uf_arg_types == NULL)
1615 continue;
1616 expected = ufunc->uf_arg_types[i];
1617 }
1618 else if (ufunc->uf_va_type == NULL
1619 || ufunc->uf_va_type == &t_list_any)
1620 // possibly a lambda or "...: any"
1621 expected = &t_any;
1622 else
1623 expected = ufunc->uf_va_type->tt_member;
1624 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1625 TRUE, FALSE) == FAIL)
1626 {
1627 arg_type_mismatch(expected, actual, i + 1);
1628 return FAIL;
1629 }
1630 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001631 compile_type = get_compile_type(ufunc);
1632 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001634 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001635 return FAIL;
1636 }
1637 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1638 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001639 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001640 ufunc->uf_name);
1641 return FAIL;
1642 }
1643
1644 if ((isn = generate_instr(cctx,
1645 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1646 : ISN_UCALL)) == NULL)
1647 return FAIL;
1648 if (isn->isn_type == ISN_DCALL)
1649 {
1650 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1651 isn->isn_arg.dfunc.cdf_argcount = argcount;
1652 }
1653 else
1654 {
1655 // A user function may be deleted and redefined later, can't use the
1656 // ufunc pointer, need to look it up again at runtime.
1657 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1658 isn->isn_arg.ufunc.cuf_argcount = argcount;
1659 }
1660
Bram Moolenaar078a4612022-01-04 15:17:03 +00001661 // drop the argument types
1662 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001663
Bram Moolenaar078a4612022-01-04 15:17:03 +00001664 // add return type
1665 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001666}
1667
1668/*
1669 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1670 */
1671 int
1672generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1673{
1674 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001675
1676 RETURN_OK_IF_SKIP(cctx);
1677 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1678 return FAIL;
1679 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1680 isn->isn_arg.ufunc.cuf_argcount = argcount;
1681
Bram Moolenaar078a4612022-01-04 15:17:03 +00001682 // drop the argument types
1683 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001684
Bram Moolenaar078a4612022-01-04 15:17:03 +00001685 // add return value
1686 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001687}
1688
1689/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001690 * Check the arguments of function "type" against the types on the stack.
1691 * Returns OK or FAIL;
1692 */
1693 int
1694check_func_args_from_type(
1695 cctx_T *cctx,
1696 type_T *type,
1697 int argcount,
1698 int at_top,
1699 char_u *name)
1700{
1701 if (type->tt_argcount != -1)
1702 {
1703 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1704
1705 if (argcount < type->tt_min_argcount - varargs)
1706 {
1707 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1708 return FAIL;
1709 }
1710 if (!varargs && argcount > type->tt_argcount)
1711 {
1712 emsg_funcname(e_too_many_arguments_for_function_str, name);
1713 return FAIL;
1714 }
1715 if (type->tt_args != NULL)
1716 {
1717 int i;
1718
1719 for (i = 0; i < argcount; ++i)
1720 {
1721 int offset = -argcount + i - (at_top ? 0 : 1);
1722 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1723 type_T *expected;
1724
1725 if (varargs && i >= type->tt_argcount - 1)
1726 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1727 else if (i >= type->tt_min_argcount
1728 && actual->tt_type == VAR_SPECIAL)
1729 expected = &t_any;
1730 else
1731 expected = type->tt_args[i];
1732 if (need_type(actual, expected, offset, i + 1,
1733 cctx, TRUE, FALSE) == FAIL)
1734 {
1735 arg_type_mismatch(expected, actual, i + 1);
1736 return FAIL;
1737 }
1738 }
1739 }
1740 }
1741
1742 return OK;
1743}
1744/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001745 * Generate an ISN_PCALL instruction.
1746 * "type" is the type of the FuncRef.
1747 */
1748 int
1749generate_PCALL(
1750 cctx_T *cctx,
1751 int argcount,
1752 char_u *name,
1753 type_T *type,
1754 int at_top)
1755{
1756 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001757 type_T *ret_type;
1758
1759 RETURN_OK_IF_SKIP(cctx);
1760
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001761 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001762 ret_type = &t_any;
1763 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1764 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001765 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1766 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001767
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001768 ret_type = type->tt_member;
1769 if (ret_type == &t_unknown)
1770 // return type not known yet, use a runtime check
1771 ret_type = &t_any;
1772 }
1773 else
1774 {
1775 semsg(_(e_not_callable_type_str), name);
1776 return FAIL;
1777 }
1778
1779 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1780 return FAIL;
1781 isn->isn_arg.pfunc.cpf_top = at_top;
1782 isn->isn_arg.pfunc.cpf_argcount = argcount;
1783
Bram Moolenaar078a4612022-01-04 15:17:03 +00001784 // drop the arguments and the funcref/partial
1785 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001786
Bram Moolenaar078a4612022-01-04 15:17:03 +00001787 // push the return value
1788 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001789
1790 // If partial is above the arguments it must be cleared and replaced with
1791 // the return value.
1792 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1793 return FAIL;
1794
1795 return OK;
1796}
1797
1798/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001799 * Generate an ISN_DEFER instruction.
1800 */
1801 int
1802generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1803{
1804 isn_T *isn;
1805
1806 RETURN_OK_IF_SKIP(cctx);
1807 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1808 return FAIL;
1809 isn->isn_arg.defer.defer_var_idx = var_idx;
1810 isn->isn_arg.defer.defer_argcount = argcount;
1811 return OK;
1812}
1813
1814/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001815 * Generate an ISN_STRINGMEMBER instruction.
1816 */
1817 int
1818generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1819{
1820 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001821 type_T *type;
1822
1823 RETURN_OK_IF_SKIP(cctx);
1824 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1825 return FAIL;
1826 isn->isn_arg.string = vim_strnsave(name, len);
1827
1828 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001829 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001830 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001831 {
1832 char *tofree;
1833
1834 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1835 name, type_name(type, &tofree));
1836 vim_free(tofree);
1837 return FAIL;
1838 }
1839 // change dict type to dict member type
1840 if (type->tt_type == VAR_DICT)
1841 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001842 type_T *ntype = type->tt_member == &t_unknown
1843 ? &t_any : type->tt_member;
1844 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001845 }
1846
1847 return OK;
1848}
1849
1850/*
1851 * Generate an ISN_ECHO instruction.
1852 */
1853 int
1854generate_ECHO(cctx_T *cctx, int with_white, int count)
1855{
1856 isn_T *isn;
1857
1858 RETURN_OK_IF_SKIP(cctx);
1859 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1860 return FAIL;
1861 isn->isn_arg.echo.echo_with_white = with_white;
1862 isn->isn_arg.echo.echo_count = count;
1863
1864 return OK;
1865}
1866
1867/*
1868 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1869 */
1870 int
1871generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1872{
1873 isn_T *isn;
1874
1875 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1876 return FAIL;
1877 isn->isn_arg.number = count;
1878
1879 return OK;
1880}
1881
1882/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001883 * Generate an ISN_SOURCE instruction.
1884 */
1885 int
1886generate_SOURCE(cctx_T *cctx, int sid)
1887{
1888 isn_T *isn;
1889
1890 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1891 return FAIL;
1892 isn->isn_arg.number = sid;
1893
1894 return OK;
1895}
1896
1897/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001898 * Generate an ISN_PUT instruction.
1899 */
1900 int
1901generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1902{
1903 isn_T *isn;
1904
1905 RETURN_OK_IF_SKIP(cctx);
1906 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1907 return FAIL;
1908 isn->isn_arg.put.put_regname = regname;
1909 isn->isn_arg.put.put_lnum = lnum;
1910 return OK;
1911}
1912
1913/*
1914 * Generate an EXEC instruction that takes a string argument.
1915 * A copy is made of "line".
1916 */
1917 int
1918generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1919{
1920 isn_T *isn;
1921
1922 RETURN_OK_IF_SKIP(cctx);
1923 if ((isn = generate_instr(cctx, isntype)) == NULL)
1924 return FAIL;
1925 isn->isn_arg.string = vim_strsave(line);
1926 return OK;
1927}
1928
1929/*
1930 * Generate an EXEC instruction that takes a string argument.
1931 * "str" must be allocated, it is consumed.
1932 */
1933 int
1934generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1935{
1936 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001937 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001939 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001940 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001941 if ((isn = generate_instr(cctx, isntype)) == NULL)
1942 ret = FAIL;
1943 else
1944 {
1945 isn->isn_arg.string = str;
1946 return OK;
1947 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001948 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001949 vim_free(str);
1950 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001951}
1952
1953 int
1954generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1955{
1956 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001957
1958 RETURN_OK_IF_SKIP(cctx);
1959 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1960 return FAIL;
1961 isn->isn_arg.string = vim_strsave(line);
1962
Bram Moolenaar078a4612022-01-04 15:17:03 +00001963 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964}
1965
1966 int
1967generate_EXECCONCAT(cctx_T *cctx, int count)
1968{
1969 isn_T *isn;
1970
1971 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1972 return FAIL;
1973 isn->isn_arg.number = count;
1974 return OK;
1975}
1976
1977/*
1978 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1979 */
1980 int
1981generate_RANGE(cctx_T *cctx, char_u *range)
1982{
1983 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984
1985 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1986 return FAIL;
1987 isn->isn_arg.string = range;
1988
Bram Moolenaar078a4612022-01-04 15:17:03 +00001989 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001990}
1991
1992 int
1993generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1994{
1995 isn_T *isn;
1996
1997 RETURN_OK_IF_SKIP(cctx);
1998 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1999 return FAIL;
2000 isn->isn_arg.unpack.unp_count = var_count;
2001 isn->isn_arg.unpack.unp_semicolon = semicolon;
2002 return OK;
2003}
2004
2005/*
2006 * Generate an instruction for any command modifiers.
2007 */
2008 int
2009generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2010{
2011 isn_T *isn;
2012
2013 if (has_cmdmod(cmod, FALSE))
2014 {
2015 cctx->ctx_has_cmdmod = TRUE;
2016
2017 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2018 return FAIL;
2019 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2020 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2021 return FAIL;
2022 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2023 // filter program now belongs to the instruction
2024 cmod->cmod_filter_regmatch.regprog = NULL;
2025 }
2026
2027 return OK;
2028}
2029
2030 int
2031generate_undo_cmdmods(cctx_T *cctx)
2032{
2033 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2034 return FAIL;
2035 cctx->ctx_has_cmdmod = FALSE;
2036 return OK;
2037}
2038
2039/*
2040 * Generate a STORE instruction for "dest", not being "dest_local".
2041 * Return FAIL when out of memory.
2042 */
2043 int
2044generate_store_var(
2045 cctx_T *cctx,
2046 assign_dest_T dest,
2047 int opt_flags,
2048 int vimvaridx,
2049 int scriptvar_idx,
2050 int scriptvar_sid,
2051 type_T *type,
2052 char_u *name)
2053{
2054 switch (dest)
2055 {
2056 case dest_option:
2057 return generate_STOREOPT(cctx, ISN_STOREOPT,
2058 skip_option_env_lead(name), opt_flags);
2059 case dest_func_option:
2060 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2061 skip_option_env_lead(name), opt_flags);
2062 case dest_global:
2063 // include g: with the name, easier to execute that way
2064 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2065 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2066 case dest_buffer:
2067 // include b: with the name, easier to execute that way
2068 return generate_STORE(cctx, ISN_STOREB, 0, name);
2069 case dest_window:
2070 // include w: with the name, easier to execute that way
2071 return generate_STORE(cctx, ISN_STOREW, 0, name);
2072 case dest_tab:
2073 // include t: with the name, easier to execute that way
2074 return generate_STORE(cctx, ISN_STORET, 0, name);
2075 case dest_env:
2076 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2077 case dest_reg:
2078 return generate_STORE(cctx, ISN_STOREREG,
2079 name[1] == '@' ? '"' : name[1], NULL);
2080 case dest_vimvar:
2081 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2082 case dest_script:
2083 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002084 {
2085 isntype_T isn_type = ISN_STORES;
2086
2087 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01002088 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2089 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2090 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002091 {
2092 // "import autoload './dir/script.vim'" - load script first
2093 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2094 return FAIL;
2095 isn_type = ISN_STOREEXPORT;
2096 }
2097
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002098 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002099 return generate_OLDSCRIPT(cctx, isn_type, name,
2100 scriptvar_sid, type);
2101 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002102 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
2103 scriptvar_sid, scriptvar_idx, type);
2104 case dest_local:
2105 case dest_expr:
2106 // cannot happen
2107 break;
2108 }
2109 return FAIL;
2110}
2111
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002112/*
2113 * Return TRUE when inside a "for" or "while" loop.
2114 */
2115 int
2116inside_loop_scope(cctx_T *cctx)
2117{
2118 scope_T *scope = cctx->ctx_scope;
2119
2120 for (;;)
2121 {
2122 if (scope == NULL)
2123 break;
2124 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2125 return TRUE;
2126 scope = scope->se_outer;
2127 }
2128 return FALSE;
2129}
2130
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002131 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002132generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002133{
2134 if (lhs->lhs_dest != dest_local)
2135 return generate_store_var(cctx, lhs->lhs_dest,
2136 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2137 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2138 lhs->lhs_type, lhs->lhs_name);
2139
2140 if (lhs->lhs_lvar != NULL)
2141 {
2142 garray_T *instr = &cctx->ctx_instr;
2143 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2144
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002145 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2146 // ISN_STORENR.
2147 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002148 if (lhs->lhs_lvar->lv_from_outer == 0
2149 && instr->ga_len == instr_count + 1
2150 && isn->isn_type == ISN_PUSHNR)
2151 {
2152 varnumber_T val = isn->isn_arg.number;
2153 garray_T *stack = &cctx->ctx_type_stack;
2154
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002155 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002156 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002157 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002158 --instr->ga_len;
2159 }
2160 else
2161 {
2162 isn->isn_type = ISN_STORENR;
2163 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2164 isn->isn_arg.storenr.stnr_val = val;
2165 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002166 if (stack->ga_len > 0)
2167 --stack->ga_len;
2168 }
2169 else if (lhs->lhs_lvar->lv_from_outer > 0)
2170 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002171 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002172 else
2173 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2174 }
2175 return OK;
2176}
2177
2178#if defined(FEAT_PROFILE) || defined(PROTO)
2179 void
2180may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2181{
2182 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2183 generate_instr(cctx, ISN_PROF_END);
2184}
2185#endif
2186
2187
2188/*
2189 * Delete an instruction, free what it contains.
2190 */
2191 void
2192delete_instr(isn_T *isn)
2193{
2194 switch (isn->isn_type)
2195 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002196 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002197 case ISN_DEF:
2198 case ISN_EXEC:
2199 case ISN_EXECRANGE:
2200 case ISN_EXEC_SPLIT:
2201 case ISN_LEGACY_EVAL:
2202 case ISN_LOADAUTO:
2203 case ISN_LOADB:
2204 case ISN_LOADENV:
2205 case ISN_LOADG:
2206 case ISN_LOADOPT:
2207 case ISN_LOADT:
2208 case ISN_LOADW:
2209 case ISN_LOCKUNLOCK:
2210 case ISN_PUSHEXC:
2211 case ISN_PUSHFUNC:
2212 case ISN_PUSHS:
2213 case ISN_RANGE:
2214 case ISN_STOREAUTO:
2215 case ISN_STOREB:
2216 case ISN_STOREENV:
2217 case ISN_STOREG:
2218 case ISN_STORET:
2219 case ISN_STOREW:
2220 case ISN_STRINGMEMBER:
2221 vim_free(isn->isn_arg.string);
2222 break;
2223
2224 case ISN_SUBSTITUTE:
2225 {
2226 int idx;
2227 isn_T *list = isn->isn_arg.subs.subs_instr;
2228
2229 vim_free(isn->isn_arg.subs.subs_cmd);
2230 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2231 delete_instr(list + idx);
2232 vim_free(list);
2233 }
2234 break;
2235
2236 case ISN_INSTR:
2237 {
2238 int idx;
2239 isn_T *list = isn->isn_arg.instr;
2240
2241 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2242 delete_instr(list + idx);
2243 vim_free(list);
2244 }
2245 break;
2246
2247 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002248 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002249 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002250 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002251 vim_free(isn->isn_arg.loadstore.ls_name);
2252 break;
2253
2254 case ISN_UNLET:
2255 case ISN_UNLETENV:
2256 vim_free(isn->isn_arg.unlet.ul_name);
2257 break;
2258
2259 case ISN_STOREOPT:
2260 case ISN_STOREFUNCOPT:
2261 vim_free(isn->isn_arg.storeopt.so_name);
2262 break;
2263
2264 case ISN_PUSHBLOB: // push blob isn_arg.blob
2265 blob_unref(isn->isn_arg.blob);
2266 break;
2267
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002268 case ISN_UCALL:
2269 vim_free(isn->isn_arg.ufunc.cuf_name);
2270 break;
2271
2272 case ISN_FUNCREF:
2273 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002274 funcref_T *funcref = &isn->isn_arg.funcref;
2275 funcref_extra_T *extra = funcref->fr_extra;
2276
2277 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002278 {
2279 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002280 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002281 ufunc_T *ufunc = dfunc->df_ufunc;
2282
2283 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2284 func_ptr_unref(ufunc);
2285 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002286 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002287 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002288 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002289
2290 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002291 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002292 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002293 vim_free(name);
2294 }
2295 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002296 }
2297 }
2298 break;
2299
2300 case ISN_DCALL:
2301 {
2302 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002303 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304
2305 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002306 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002307 func_ptr_unref(dfunc->df_ufunc);
2308 }
2309 break;
2310
2311 case ISN_NEWFUNC:
2312 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002313 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002314
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002315 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002316 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002317 ufunc_T *ufunc = find_func_even_dead(
2318 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002319
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002320 if (ufunc != NULL)
2321 {
2322 unlink_def_function(ufunc);
2323 func_ptr_unref(ufunc);
2324 }
2325
2326 vim_free(arg->nfa_lambda);
2327 vim_free(arg->nfa_global);
2328 vim_free(arg);
2329 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002330 }
2331 break;
2332
2333 case ISN_CHECKTYPE:
2334 case ISN_SETTYPE:
2335 free_type(isn->isn_arg.type.ct_type);
2336 break;
2337
2338 case ISN_CMDMOD:
2339 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002340 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002341 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2342 break;
2343
2344 case ISN_LOADSCRIPT:
2345 case ISN_STORESCRIPT:
2346 vim_free(isn->isn_arg.script.scriptref);
2347 break;
2348
2349 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002350 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002351 break;
2352
2353 case ISN_CEXPR_CORE:
2354 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2355 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2356 break;
2357
2358 case ISN_2BOOL:
2359 case ISN_2STRING:
2360 case ISN_2STRING_ANY:
2361 case ISN_ADDBLOB:
2362 case ISN_ADDLIST:
2363 case ISN_ANYINDEX:
2364 case ISN_ANYSLICE:
2365 case ISN_BCALL:
2366 case ISN_BLOBAPPEND:
2367 case ISN_BLOBINDEX:
2368 case ISN_BLOBSLICE:
2369 case ISN_CATCH:
2370 case ISN_CEXPR_AUCMD:
2371 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002372 case ISN_CLEARDICT:
2373 case ISN_CMDMOD_REV:
2374 case ISN_COMPAREANY:
2375 case ISN_COMPAREBLOB:
2376 case ISN_COMPAREBOOL:
2377 case ISN_COMPAREDICT:
2378 case ISN_COMPAREFLOAT:
2379 case ISN_COMPAREFUNC:
2380 case ISN_COMPARELIST:
2381 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002382 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 case ISN_COMPARESPECIAL:
2384 case ISN_COMPARESTRING:
2385 case ISN_CONCAT:
2386 case ISN_COND2BOOL:
2387 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002388 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002389 case ISN_DROP:
2390 case ISN_ECHO:
2391 case ISN_ECHOCONSOLE:
2392 case ISN_ECHOERR:
2393 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002394 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002395 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002396 case ISN_ENDTRY:
2397 case ISN_EXECCONCAT:
2398 case ISN_EXECUTE:
2399 case ISN_FINALLY:
2400 case ISN_FINISH:
2401 case ISN_FOR:
2402 case ISN_GETITEM:
2403 case ISN_JUMP:
2404 case ISN_JUMP_IF_ARG_SET:
2405 case ISN_LISTAPPEND:
2406 case ISN_LISTINDEX:
2407 case ISN_LISTSLICE:
2408 case ISN_LOAD:
2409 case ISN_LOADBDICT:
2410 case ISN_LOADGDICT:
2411 case ISN_LOADOUTER:
2412 case ISN_LOADREG:
2413 case ISN_LOADTDICT:
2414 case ISN_LOADV:
2415 case ISN_LOADWDICT:
2416 case ISN_LOCKCONST:
2417 case ISN_MEMBER:
2418 case ISN_NEGATENR:
2419 case ISN_NEWDICT:
2420 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002421 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002422 case ISN_OPANY:
2423 case ISN_OPFLOAT:
2424 case ISN_OPNR:
2425 case ISN_PCALL:
2426 case ISN_PCALL_END:
2427 case ISN_PROF_END:
2428 case ISN_PROF_START:
2429 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002430 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002432 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002433 case ISN_PUSHNR:
2434 case ISN_PUSHSPEC:
2435 case ISN_PUT:
2436 case ISN_REDIREND:
2437 case ISN_REDIRSTART:
2438 case ISN_RETURN:
2439 case ISN_RETURN_VOID:
2440 case ISN_SHUFFLE:
2441 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002442 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002443 case ISN_STORE:
2444 case ISN_STOREINDEX:
2445 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002446 case ISN_STOREOUTER:
2447 case ISN_STORERANGE:
2448 case ISN_STOREREG:
2449 case ISN_STOREV:
2450 case ISN_STRINDEX:
2451 case ISN_STRSLICE:
2452 case ISN_THROW:
2453 case ISN_TRYCONT:
2454 case ISN_UNLETINDEX:
2455 case ISN_UNLETRANGE:
2456 case ISN_UNPACK:
2457 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002458 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002459 // nothing allocated
2460 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002461 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002462}
2463
2464 void
2465clear_instr_ga(garray_T *gap)
2466{
2467 int idx;
2468
2469 for (idx = 0; idx < gap->ga_len; ++idx)
2470 delete_instr(((isn_T *)gap->ga_data) + idx);
2471 ga_clear(gap);
2472}
2473
2474
2475#endif // defined(FEAT_EVAL)