blob: 47114f08cc47dda217833365acefcd1cbfad561b [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/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000117 * Generate an ISN_CONSTRUCT instruction.
118 * The object will have "size" members.
119 */
120 int
121generate_CONSTRUCT(cctx_T *cctx, class_T *cl)
122{
123 isn_T *isn;
124
125 RETURN_OK_IF_SKIP(cctx);
126 if ((isn = generate_instr(cctx, ISN_CONSTRUCT)) == NULL)
127 return FAIL;
128 isn->isn_arg.construct.construct_size = sizeof(object_T)
129 + cl->class_obj_member_count * sizeof(typval_T);
130 isn->isn_arg.construct.construct_class = cl;
131 return OK;
132}
133
134/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000135 * Generate ISN_GET_OBJ_MEMBER - access member of object at bottom of stack by
136 * index.
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000137 */
138 int
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000139generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000140{
141 RETURN_OK_IF_SKIP(cctx);
142
143 // drop the object type
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000144 isn_T *isn = generate_instr_drop(cctx, ISN_GET_OBJ_MEMBER, 1);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000145 if (isn == NULL)
146 return FAIL;
147
148 isn->isn_arg.number = idx;
149 return push_type_stack2(cctx, type, &t_any);
150}
151
152/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000153 * Generate ISN_STORE_THIS - store value in member of "this" object with member
154 * index "idx".
155 */
156 int
157generate_STORE_THIS(cctx_T *cctx, int idx)
158{
159 RETURN_OK_IF_SKIP(cctx);
160
161 // drop the value type
162 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
163 if (isn == NULL)
164 return FAIL;
165
166 isn->isn_arg.number = idx;
167 return OK;
168}
169
170/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000171 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
172 * But only for simple types.
173 * When "tolerant" is TRUE convert most types to string, e.g. a List.
174 */
175 int
176may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
177{
178 isn_T *isn;
179 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000180 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000181
182 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000183 type = get_type_on_stack(cctx, -1 - offset);
184 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000185 {
186 // nothing to be done
187 case VAR_STRING: return OK;
188
189 // conversion possible
190 case VAR_SPECIAL:
191 case VAR_BOOL:
192 case VAR_NUMBER:
193 case VAR_FLOAT:
194 break;
195
196 // conversion possible (with runtime check)
197 case VAR_ANY:
198 case VAR_UNKNOWN:
199 isntype = ISN_2STRING_ANY;
200 break;
201
202 // conversion possible when tolerant
203 case VAR_LIST:
204 if (tolerant)
205 {
206 isntype = ISN_2STRING_ANY;
207 break;
208 }
209 // FALLTHROUGH
210
211 // conversion not possible
212 case VAR_VOID:
213 case VAR_BLOB:
214 case VAR_FUNC:
215 case VAR_PARTIAL:
216 case VAR_DICT:
217 case VAR_JOB:
218 case VAR_CHANNEL:
219 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000220 case VAR_CLASS:
221 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000222 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000223 return FAIL;
224 }
225
Bram Moolenaar078a4612022-01-04 15:17:03 +0000226 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000227 if ((isn = generate_instr(cctx, isntype)) == NULL)
228 return FAIL;
229 isn->isn_arg.tostring.offset = offset;
230 isn->isn_arg.tostring.tolerant = tolerant;
231
232 return OK;
233}
234
235 static int
236check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
237{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000238 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
239 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000240 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000241 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000242 {
243 if (*op == '+')
244 emsg(_(e_wrong_argument_type_for_plus));
245 else
246 semsg(_(e_char_requires_number_or_float_arguments), *op);
247 return FAIL;
248 }
249 return OK;
250}
251
252/*
253 * Generate instruction for "+". For a list this creates a new list.
254 */
255 int
256generate_add_instr(
257 cctx_T *cctx,
258 vartype_T vartype,
259 type_T *type1,
260 type_T *type2,
261 exprtype_T expr_type)
262{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000263 isn_T *isn = generate_instr_drop(cctx,
264 vartype == VAR_NUMBER ? ISN_OPNR
265 : vartype == VAR_LIST ? ISN_ADDLIST
266 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000267 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000268 : ISN_OPANY, 1);
269
270 if (vartype != VAR_LIST && vartype != VAR_BLOB
271 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000272 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000273 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000274 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000275 && check_number_or_float(
276 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
277 return FAIL;
278
279 if (isn != NULL)
280 {
281 if (isn->isn_type == ISN_ADDLIST)
282 isn->isn_arg.op.op_type = expr_type;
283 else
284 isn->isn_arg.op.op_type = EXPR_ADD;
285 }
286
287 // When concatenating two lists with different member types the member type
288 // becomes "any".
289 if (vartype == VAR_LIST
290 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
291 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000292 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000293
294 return isn == NULL ? FAIL : OK;
295}
296
297/*
298 * Get the type to use for an instruction for an operation on "type1" and
299 * "type2". If they are matching use a type-specific instruction. Otherwise
300 * fall back to runtime type checking.
301 */
302 vartype_T
303operator_type(type_T *type1, type_T *type2)
304{
305 if (type1->tt_type == type2->tt_type
306 && (type1->tt_type == VAR_NUMBER
307 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000308 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000309 || type1->tt_type == VAR_BLOB))
310 return type1->tt_type;
311 return VAR_ANY;
312}
313
314/*
315 * Generate an instruction with two arguments. The instruction depends on the
316 * type of the arguments.
317 */
318 int
319generate_two_op(cctx_T *cctx, char_u *op)
320{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000321 type_T *type1;
322 type_T *type2;
323 vartype_T vartype;
324 isn_T *isn;
325
326 RETURN_OK_IF_SKIP(cctx);
327
328 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000329 type1 = get_type_on_stack(cctx, 1);
330 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000331 vartype = operator_type(type1, type2);
332
333 switch (*op)
334 {
335 case '+':
336 if (generate_add_instr(cctx, vartype, type1, type2,
337 EXPR_COPY) == FAIL)
338 return FAIL;
339 break;
340
341 case '-':
342 case '*':
343 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
344 op) == FAIL)
345 return FAIL;
346 if (vartype == VAR_NUMBER)
347 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000348 else if (vartype == VAR_FLOAT)
349 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000350 else
351 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
352 if (isn != NULL)
353 isn->isn_arg.op.op_type = *op == '*'
354 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
355 break;
356
357 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000358 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000359 && type1->tt_type != VAR_NUMBER)
360 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000361 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000362 && type2->tt_type != VAR_NUMBER))
363 {
364 emsg(_(e_percent_requires_number_arguments));
365 return FAIL;
366 }
367 isn = generate_instr_drop(cctx,
368 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
369 if (isn != NULL)
370 isn->isn_arg.op.op_type = EXPR_REM;
371 break;
372 }
373
374 // correct type of result
375 if (vartype == VAR_ANY)
376 {
377 type_T *type = &t_any;
378
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 // float+number and number+float results in float
380 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100381 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000382 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000383 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000384 }
385
386 return OK;
387}
388
389/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000390 * Get the instruction to use for comparing two values with specified types.
391 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000392 * Return ISN_DROP when failed.
393 */
394 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000395get_compare_isn(
396 exprtype_T exprtype,
397 typval_T *tv1,
398 typval_T *tv2,
399 type_T *type1,
400 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000401{
402 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000403 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
404 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000405
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000406 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000407 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000408 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000409 {
410 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
411 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
412 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
413 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
414 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
415 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
416 case VAR_LIST: isntype = ISN_COMPARELIST; break;
417 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
418 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
419 default: isntype = ISN_COMPAREANY; break;
420 }
421 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000422 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
423 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
424 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
425 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
426 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000427 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000428 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000429 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000430 if ((vartype1 == VAR_SPECIAL
431 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
432 : type1 == &t_none)
433 && vartype2 != VAR_STRING)
434 || (vartype2 == VAR_SPECIAL
435 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
436 : type2 == &t_none)
437 && vartype1 != VAR_STRING))
438 {
439 semsg(_(e_cannot_compare_str_with_str),
440 vartype_name(vartype1), vartype_name(vartype2));
441 return ISN_DROP;
442 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000443 // although comparing null with number, float or bool is not useful, we
444 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000445 isntype = ISN_COMPARENULL;
446 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000447
448 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
449 && (isntype == ISN_COMPAREBOOL
450 || isntype == ISN_COMPARESPECIAL
451 || isntype == ISN_COMPARENR
452 || isntype == ISN_COMPAREFLOAT))
453 {
454 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000455 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456 return ISN_DROP;
457 }
458 if (isntype == ISN_DROP
459 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000460 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
461 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000462 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000463 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000464 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
465 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000466 {
467 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000468 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469 return ISN_DROP;
470 }
471 return isntype;
472}
473
474 int
475check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
476{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000477 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000478 return FAIL;
479 return OK;
480}
481
482/*
483 * Generate an ISN_COMPARE* instruction with a boolean result.
484 */
485 int
486generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
487{
488 isntype_T isntype;
489 isn_T *isn;
490 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491
492 RETURN_OK_IF_SKIP(cctx);
493
494 // Get the known type of the two items on the stack. If they are matching
495 // use a type-specific instruction. Otherwise fall back to runtime type
496 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000497 isntype = get_compare_isn(exprtype, NULL, NULL,
498 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000499 if (isntype == ISN_DROP)
500 return FAIL;
501
502 if ((isn = generate_instr(cctx, isntype)) == NULL)
503 return FAIL;
504 isn->isn_arg.op.op_type = exprtype;
505 isn->isn_arg.op.op_ic = ic;
506
507 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100508 --stack->ga_len;
509 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000510
511 return OK;
512}
513
514/*
LemonBoy372bcce2022-04-25 12:43:20 +0100515 * Generate an ISN_CONCAT instruction.
516 * "count" is the number of stack elements to join together and it must be
517 * greater or equal to one.
518 * The caller ensures all the "count" elements on the stack have the right type.
519 */
520 int
521generate_CONCAT(cctx_T *cctx, int count)
522{
523 isn_T *isn;
524 garray_T *stack = &cctx->ctx_type_stack;
525
526 RETURN_OK_IF_SKIP(cctx);
527
LemonBoy372bcce2022-04-25 12:43:20 +0100528 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
529 return FAIL;
530 isn->isn_arg.number = count;
531
532 // drop the argument types
533 stack->ga_len -= count - 1;
534
535 return OK;
536}
537
538/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539 * Generate an ISN_2BOOL instruction.
540 * "offset" is the offset in the type stack.
541 */
542 int
543generate_2BOOL(cctx_T *cctx, int invert, int offset)
544{
545 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000546
547 RETURN_OK_IF_SKIP(cctx);
548 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
549 return FAIL;
550 isn->isn_arg.tobool.invert = invert;
551 isn->isn_arg.tobool.offset = offset;
552
553 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000554 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000555
556 return OK;
557}
558
559/*
560 * Generate an ISN_COND2BOOL instruction.
561 */
562 int
563generate_COND2BOOL(cctx_T *cctx)
564{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000565 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100566 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000567 return FAIL;
568
569 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000570 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000571
572 return OK;
573}
574
575 int
576generate_TYPECHECK(
577 cctx_T *cctx,
578 type_T *expected,
579 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100580 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000581 int argidx)
582{
583 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000584
585 RETURN_OK_IF_SKIP(cctx);
586 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
587 return FAIL;
588 isn->isn_arg.type.ct_type = alloc_type(expected);
589 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100590 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000591 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
592
593 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000594 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000595
596 return OK;
597}
598
599 int
600generate_SETTYPE(
601 cctx_T *cctx,
602 type_T *expected)
603{
604 isn_T *isn;
605
606 RETURN_OK_IF_SKIP(cctx);
607 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
608 return FAIL;
609 isn->isn_arg.type.ct_type = alloc_type(expected);
610 return OK;
611}
612
613/*
614 * Generate a PUSH instruction for "tv".
615 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000616 */
617 int
618generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
619{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100620 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000621 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100622 case VAR_BOOL:
623 generate_PUSHBOOL(cctx, tv->vval.v_number);
624 break;
625 case VAR_SPECIAL:
626 generate_PUSHSPEC(cctx, tv->vval.v_number);
627 break;
628 case VAR_NUMBER:
629 generate_PUSHNR(cctx, tv->vval.v_number);
630 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100631 case VAR_FLOAT:
632 generate_PUSHF(cctx, tv->vval.v_float);
633 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100634 case VAR_BLOB:
635 generate_PUSHBLOB(cctx, tv->vval.v_blob);
636 tv->vval.v_blob = NULL;
637 break;
638 case VAR_LIST:
639 if (tv->vval.v_list != NULL)
640 iemsg("non-empty list constant not supported");
641 generate_NEWLIST(cctx, 0, TRUE);
642 break;
643 case VAR_DICT:
644 if (tv->vval.v_dict != NULL)
645 iemsg("non-empty dict constant not supported");
646 generate_NEWDICT(cctx, 0, TRUE);
647 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000648#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100649 case VAR_JOB:
650 if (tv->vval.v_job != NULL)
651 iemsg("non-null job constant not supported");
652 generate_PUSHJOB(cctx);
653 break;
654 case VAR_CHANNEL:
655 if (tv->vval.v_channel != NULL)
656 iemsg("non-null channel constant not supported");
657 generate_PUSHCHANNEL(cctx);
658 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000659#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100660 case VAR_FUNC:
661 if (tv->vval.v_string != NULL)
662 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100663 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100664 break;
665 case VAR_PARTIAL:
666 if (tv->vval.v_partial != NULL)
667 iemsg("non-null partial constant not supported");
668 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
669 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000670 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100671 break;
672 case VAR_STRING:
673 generate_PUSHS(cctx, &tv->vval.v_string);
674 tv->vval.v_string = NULL;
675 break;
676 default:
677 siemsg("constant type %d not supported", tv->v_type);
678 clear_tv(tv);
679 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000680 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100681 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000682 return OK;
683}
684
685/*
686 * Generate an ISN_PUSHNR instruction.
687 */
688 int
689generate_PUSHNR(cctx_T *cctx, varnumber_T number)
690{
691 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000692
693 RETURN_OK_IF_SKIP(cctx);
694 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
695 return FAIL;
696 isn->isn_arg.number = number;
697
698 if (number == 0 || number == 1)
699 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000700 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000701 return OK;
702}
703
704/*
705 * Generate an ISN_PUSHBOOL instruction.
706 */
707 int
708generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
709{
710 isn_T *isn;
711
712 RETURN_OK_IF_SKIP(cctx);
713 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
714 return FAIL;
715 isn->isn_arg.number = number;
716
717 return OK;
718}
719
720/*
721 * Generate an ISN_PUSHSPEC instruction.
722 */
723 int
724generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
725{
726 isn_T *isn;
727
728 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000729 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
730 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000731 return FAIL;
732 isn->isn_arg.number = number;
733
734 return OK;
735}
736
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737/*
738 * Generate an ISN_PUSHF instruction.
739 */
740 int
741generate_PUSHF(cctx_T *cctx, float_T fnumber)
742{
743 isn_T *isn;
744
745 RETURN_OK_IF_SKIP(cctx);
746 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
747 return FAIL;
748 isn->isn_arg.fnumber = fnumber;
749
750 return OK;
751}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752
753/*
754 * Generate an ISN_PUSHS instruction.
755 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100756 * Note that if "str" is used in the instruction OK is returned and "*str" is
757 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000758 */
759 int
760generate_PUSHS(cctx_T *cctx, char_u **str)
761{
762 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100763 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000764
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100765 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000766 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100767 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
768 ret = FAIL;
769 else
770 {
771 isn->isn_arg.string = str == NULL ? NULL : *str;
772 return OK;
773 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000774 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100775 if (str != NULL)
776 VIM_CLEAR(*str);
777 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000778}
779
780/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000781 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782 */
783 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000784generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000785{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000786 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100787#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100788 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000789 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100791#else
792 emsg(_(e_channel_job_feature_not_available));
793 return FAIL;
794#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000795}
796
797/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000798 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000799 */
800 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000801generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000802{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000803 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100804#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100805 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000806 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000807 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100808#else
809 emsg(_(e_channel_job_feature_not_available));
810 return FAIL;
811#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000812}
813
814/*
815 * Generate an ISN_PUSHBLOB instruction.
816 * Consumes "blob".
817 */
818 int
819generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
820{
821 isn_T *isn;
822
823 RETURN_OK_IF_SKIP(cctx);
824 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
825 return FAIL;
826 isn->isn_arg.blob = blob;
827
828 return OK;
829}
830
831/*
832 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100833 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
834 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000835 */
836 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100837generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000838{
839 isn_T *isn;
840 char_u *funcname;
841
842 RETURN_OK_IF_SKIP(cctx);
843 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
844 return FAIL;
845 if (name == NULL)
846 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100847 else if (!may_prefix
848 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000849 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000850 funcname = vim_strsave(name);
851 else
852 {
853 funcname = alloc(STRLEN(name) + 3);
854 if (funcname != NULL)
855 {
856 STRCPY(funcname, "g:");
857 STRCPY(funcname + 2, name);
858 }
859 }
860
861 isn->isn_arg.string = funcname;
862 return OK;
863}
864
865/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000866 * Generate an ISN_AUTOLOAD instruction.
867 */
868 int
869generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
870{
871 isn_T *isn;
872
873 RETURN_OK_IF_SKIP(cctx);
874 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
875 return FAIL;
876 isn->isn_arg.string = vim_strsave(name);
877 if (isn->isn_arg.string == NULL)
878 return FAIL;
879 return OK;
880}
881
882/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883 * Generate an ISN_GETITEM instruction with "index".
884 * "with_op" is TRUE for "+=" and other operators, the stack has the current
885 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100886 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000887 */
888 int
889generate_GETITEM(cctx_T *cctx, int index, int with_op)
890{
891 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000892 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893 type_T *item_type = &t_any;
894
895 RETURN_OK_IF_SKIP(cctx);
896
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 item_type = type->tt_member;
898 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
899 return FAIL;
900 isn->isn_arg.getitem.gi_index = index;
901 isn->isn_arg.getitem.gi_with_op = with_op;
902
903 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000904 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000905}
906
907/*
908 * Generate an ISN_SLICE instruction with "count".
909 */
910 int
911generate_SLICE(cctx_T *cctx, int count)
912{
913 isn_T *isn;
914
915 RETURN_OK_IF_SKIP(cctx);
916 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
917 return FAIL;
918 isn->isn_arg.number = count;
919 return OK;
920}
921
922/*
923 * Generate an ISN_CHECKLEN instruction with "min_len".
924 */
925 int
926generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
927{
928 isn_T *isn;
929
930 RETURN_OK_IF_SKIP(cctx);
931
932 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
933 return FAIL;
934 isn->isn_arg.checklen.cl_min_len = min_len;
935 isn->isn_arg.checklen.cl_more_OK = more_OK;
936
937 return OK;
938}
939
940/*
941 * Generate an ISN_STORE instruction.
942 */
943 int
944generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
945{
946 isn_T *isn;
947
948 RETURN_OK_IF_SKIP(cctx);
949 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
950 return FAIL;
951 if (name != NULL)
952 isn->isn_arg.string = vim_strsave(name);
953 else
954 isn->isn_arg.number = idx;
955
956 return OK;
957}
958
959/*
960 * Generate an ISN_STOREOUTER instruction.
961 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000962 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100963generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000964{
965 isn_T *isn;
966
967 RETURN_OK_IF_SKIP(cctx);
968 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
969 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100970 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
971 {
972 // Store a variable defined in a loop. A copy will be made at the end
973 // of the loop. TODO: how about deeper nesting?
974 isn->isn_arg.outer.outer_idx = idx - loop_idx;
975 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
976 }
977 else
978 {
979 isn->isn_arg.outer.outer_idx = idx;
980 isn->isn_arg.outer.outer_depth = level;
981 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000982
983 return OK;
984}
985
986/*
987 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
988 */
989 int
990generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
991{
992 isn_T *isn;
993
994 RETURN_OK_IF_SKIP(cctx);
995 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
996 return FAIL;
997 isn->isn_arg.storenr.stnr_idx = idx;
998 isn->isn_arg.storenr.stnr_val = value;
999
1000 return OK;
1001}
1002
1003/*
1004 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1005 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001006 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001007generate_STOREOPT(
1008 cctx_T *cctx,
1009 isntype_T isn_type,
1010 char_u *name,
1011 int opt_flags)
1012{
1013 isn_T *isn;
1014
1015 RETURN_OK_IF_SKIP(cctx);
1016 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1017 return FAIL;
1018 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1019 isn->isn_arg.storeopt.so_flags = opt_flags;
1020
1021 return OK;
1022}
1023
1024/*
1025 * Generate an ISN_LOAD or similar instruction.
1026 */
1027 int
1028generate_LOAD(
1029 cctx_T *cctx,
1030 isntype_T isn_type,
1031 int idx,
1032 char_u *name,
1033 type_T *type)
1034{
1035 isn_T *isn;
1036
1037 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001038 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001039 return FAIL;
1040 if (name != NULL)
1041 isn->isn_arg.string = vim_strsave(name);
1042 else
1043 isn->isn_arg.number = idx;
1044
1045 return OK;
1046}
1047
1048/*
1049 * Generate an ISN_LOADOUTER instruction
1050 */
1051 int
1052generate_LOADOUTER(
1053 cctx_T *cctx,
1054 int idx,
1055 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001056 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001057 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001058 type_T *type)
1059{
1060 isn_T *isn;
1061
1062 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001063 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001064 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001065 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1066 {
1067 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001068 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001069 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001070 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001071 }
1072 else
1073 {
1074 isn->isn_arg.outer.outer_idx = idx;
1075 isn->isn_arg.outer.outer_depth = nesting;
1076 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001077
1078 return OK;
1079}
1080
1081/*
1082 * Generate an ISN_LOADV instruction for v:var.
1083 */
1084 int
1085generate_LOADV(
1086 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001087 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001088{
1089 int di_flags;
1090 int vidx = find_vim_var(name, &di_flags);
1091 type_T *type;
1092
1093 RETURN_OK_IF_SKIP(cctx);
1094 if (vidx < 0)
1095 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001096 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001097 return FAIL;
1098 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001099 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001100 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1101}
1102
1103/*
1104 * Generate an ISN_UNLET instruction.
1105 */
1106 int
1107generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1108{
1109 isn_T *isn;
1110
1111 RETURN_OK_IF_SKIP(cctx);
1112 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1113 return FAIL;
1114 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1115 isn->isn_arg.unlet.ul_forceit = forceit;
1116
1117 return OK;
1118}
1119
1120/*
1121 * Generate an ISN_LOCKCONST instruction.
1122 */
1123 int
1124generate_LOCKCONST(cctx_T *cctx)
1125{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001126 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001127 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001128 return FAIL;
1129 return OK;
1130}
1131
1132/*
1133 * Generate an ISN_LOADS instruction.
1134 */
1135 int
1136generate_OLDSCRIPT(
1137 cctx_T *cctx,
1138 isntype_T isn_type,
1139 char_u *name,
1140 int sid,
1141 type_T *type)
1142{
1143 isn_T *isn;
1144
1145 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001146 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001147 isn = generate_instr_type(cctx, isn_type, type);
1148 else
1149 isn = generate_instr_drop(cctx, isn_type, 1);
1150 if (isn == NULL)
1151 return FAIL;
1152 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1153 isn->isn_arg.loadstore.ls_sid = sid;
1154
1155 return OK;
1156}
1157
1158/*
1159 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1160 */
1161 int
1162generate_VIM9SCRIPT(
1163 cctx_T *cctx,
1164 isntype_T isn_type,
1165 int sid,
1166 int idx,
1167 type_T *type)
1168{
1169 isn_T *isn;
1170 scriptref_T *sref;
1171 scriptitem_T *si = SCRIPT_ITEM(sid);
1172
1173 RETURN_OK_IF_SKIP(cctx);
1174 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001175 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001176 else
1177 isn = generate_instr_drop(cctx, isn_type, 1);
1178 if (isn == NULL)
1179 return FAIL;
1180
1181 // This requires three arguments, which doesn't fit in an instruction, thus
1182 // we need to allocate a struct for this.
1183 sref = ALLOC_ONE(scriptref_T);
1184 if (sref == NULL)
1185 return FAIL;
1186 isn->isn_arg.script.scriptref = sref;
1187 sref->sref_sid = sid;
1188 sref->sref_idx = idx;
1189 sref->sref_seq = si->sn_script_seq;
1190 sref->sref_type = type;
1191 return OK;
1192}
1193
1194/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001195 * Generate an ISN_NEWLIST instruction for "count" items.
1196 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001197 */
1198 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001199generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001200{
1201 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001202 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001203 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001204 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001205
1206 RETURN_OK_IF_SKIP(cctx);
1207 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1208 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001209 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001210
Bram Moolenaar078a4612022-01-04 15:17:03 +00001211 // Get the member type and the declared member type from all the items on
1212 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001213 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001214 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001215 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001216
1217 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001218 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001219
1220 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001221 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001222}
1223
1224/*
1225 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001226 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001227 */
1228 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001229generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001230{
1231 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001232 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001233 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001234 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001235
1236 RETURN_OK_IF_SKIP(cctx);
1237 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1238 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001239 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001240
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001241 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001242 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001243 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001244
1245 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001246 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001247
1248 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001249 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001250}
1251
1252/*
1253 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001254 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001255 * If variables were declared inside a loop "loop_var_idx" is the index of the
1256 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001257 */
1258 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001259generate_FUNCREF(
1260 cctx_T *cctx,
1261 ufunc_T *ufunc,
1262 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001263{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001264 isn_T *isn;
1265 type_T *type;
1266 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001267 loopvarinfo_T loopinfo;
1268 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001269
1270 RETURN_OK_IF_SKIP(cctx);
1271 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1272 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001273 if (isnp != NULL)
1274 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001275
Bram Moolenaarcc341812022-09-19 15:54:34 +01001276 has_vars = get_loop_var_info(cctx, &loopinfo);
1277 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001278 {
1279 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1280 if (extra == NULL)
1281 return FAIL;
1282 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001283 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001284 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001285 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001286 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001287 else
1288 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001289
1290 // Reserve an extra variable to keep track of the number of closures
1291 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001292 cctx->ctx_has_closure = 1;
1293
1294 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001295 // the nested context, including this one. But not a function defined at
1296 // the script level.
1297 if ((ufunc->uf_flags & FC_CLOSURE)
1298 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1300
Bram Moolenaar078a4612022-01-04 15:17:03 +00001301 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1302 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001303}
1304
1305/*
1306 * Generate an ISN_NEWFUNC instruction.
1307 * "lambda_name" and "func_name" must be in allocated memory and will be
1308 * consumed.
1309 */
1310 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001311generate_NEWFUNC(
1312 cctx_T *cctx,
1313 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001314 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001315{
1316 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001317 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001318
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001319 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001320 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001321 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1322 ret = FAIL;
1323 else
1324 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001325 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1326
1327 if (arg == NULL)
1328 ret = FAIL;
1329 else
1330 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001331 // Reserve an extra variable to keep track of the number of
1332 // closures created.
1333 cctx->ctx_has_closure = 1;
1334
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001335 isn->isn_arg.newfunc.nf_arg = arg;
1336 arg->nfa_lambda = lambda_name;
1337 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001338 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001339 return OK;
1340 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001341 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001342 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001343 vim_free(lambda_name);
1344 vim_free(func_name);
1345 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001346}
1347
1348/*
1349 * Generate an ISN_DEF instruction: list functions
1350 */
1351 int
1352generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1353{
1354 isn_T *isn;
1355
1356 RETURN_OK_IF_SKIP(cctx);
1357 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1358 return FAIL;
1359 if (len > 0)
1360 {
1361 isn->isn_arg.string = vim_strnsave(name, len);
1362 if (isn->isn_arg.string == NULL)
1363 return FAIL;
1364 }
1365 return OK;
1366}
1367
1368/*
1369 * Generate an ISN_JUMP instruction.
1370 */
1371 int
1372generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1373{
1374 isn_T *isn;
1375 garray_T *stack = &cctx->ctx_type_stack;
1376
1377 RETURN_OK_IF_SKIP(cctx);
1378 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1379 return FAIL;
1380 isn->isn_arg.jump.jump_when = when;
1381 isn->isn_arg.jump.jump_where = where;
1382
1383 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1384 --stack->ga_len;
1385
1386 return OK;
1387}
1388
1389/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001390 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1391 */
1392 int
1393generate_WHILE(cctx_T *cctx, int funcref_idx)
1394{
1395 isn_T *isn;
1396 garray_T *stack = &cctx->ctx_type_stack;
1397
1398 RETURN_OK_IF_SKIP(cctx);
1399 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1400 return FAIL;
1401 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1402 isn->isn_arg.whileloop.while_end = 0; // filled in later
1403
1404 if (stack->ga_len > 0)
1405 --stack->ga_len;
1406
1407 return OK;
1408}
1409
1410/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001411 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001412 */
1413 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001414generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001415{
1416 isn_T *isn;
1417
1418 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001419 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001420 return FAIL;
1421 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1422 // jump_where is set later
1423 return OK;
1424}
1425
1426 int
1427generate_FOR(cctx_T *cctx, int loop_idx)
1428{
1429 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001430
1431 RETURN_OK_IF_SKIP(cctx);
1432 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1433 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001434 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001435
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001436 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001437 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001438}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001439
1440 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001441generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001442{
1443 isn_T *isn;
1444
1445 RETURN_OK_IF_SKIP(cctx);
1446 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1447 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001448 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1449 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1450 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001451 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001452 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001453 return OK;
1454}
1455
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001456/*
1457 * Generate an ISN_TRYCONT instruction.
1458 */
1459 int
1460generate_TRYCONT(cctx_T *cctx, int levels, int where)
1461{
1462 isn_T *isn;
1463
1464 RETURN_OK_IF_SKIP(cctx);
1465 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1466 return FAIL;
1467 isn->isn_arg.trycont.tct_levels = levels;
1468 isn->isn_arg.trycont.tct_where = where;
1469
1470 return OK;
1471}
1472
Bram Moolenaar16900322022-09-08 19:51:45 +01001473/*
1474 * Check "argount" arguments and their types on the type stack.
1475 * Give an error and return FAIL if something is wrong.
1476 * When "method_call" is NULL no code is generated.
1477 */
1478 int
1479check_internal_func_args(
1480 cctx_T *cctx,
1481 int func_idx,
1482 int argcount,
1483 int method_call,
1484 type2_T **argtypes,
1485 type2_T *shuffled_argtypes)
1486{
1487 garray_T *stack = &cctx->ctx_type_stack;
1488 int argoff = check_internal_func(func_idx, argcount);
1489
1490 if (argoff < 0)
1491 return FAIL;
1492
1493 if (method_call && argoff > 1)
1494 {
1495 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1496
1497 if (isn == NULL)
1498 return FAIL;
1499 isn->isn_arg.shuffle.shfl_item = argcount;
1500 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1501 }
1502
1503 if (argcount > 0)
1504 {
1505 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1506
1507 // Check the types of the arguments.
1508 if (method_call && argoff > 1)
1509 {
1510 int i;
1511
1512 for (i = 0; i < argcount; ++i)
1513 shuffled_argtypes[i] = (i < argoff - 1)
1514 ? typep[i + 1]
1515 : (i == argoff - 1) ? typep[0] : typep[i];
1516 *argtypes = shuffled_argtypes;
1517 }
1518 else
1519 {
1520 int i;
1521
1522 for (i = 0; i < argcount; ++i)
1523 shuffled_argtypes[i] = typep[i];
1524 *argtypes = shuffled_argtypes;
1525 }
1526 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1527 cctx) == FAIL)
1528 return FAIL;
1529 }
1530 return OK;
1531}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001532
1533/*
1534 * Generate an ISN_BCALL instruction.
1535 * "method_call" is TRUE for "value->method()"
1536 * Return FAIL if the number of arguments is wrong.
1537 */
1538 int
1539generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1540{
1541 isn_T *isn;
1542 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001543 type2_T *argtypes = NULL;
1544 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1545 type2_T *maptype = NULL;
1546 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001547 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001548
1549 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001550
1551 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1552 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001553 return FAIL;
1554
Bram Moolenaar16900322022-09-08 19:51:45 +01001555 if (internal_func_is_map(func_idx))
1556 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001557
1558 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1559 return FAIL;
1560 isn->isn_arg.bfunc.cbf_idx = func_idx;
1561 isn->isn_arg.bfunc.cbf_argcount = argcount;
1562
1563 // Drop the argument types and push the return type.
1564 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001565 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1566 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001567 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001568
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001569 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1570 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001571 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001572 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001573
1574 return OK;
1575}
1576
1577/*
1578 * Generate an ISN_LISTAPPEND instruction. Works like add().
1579 * Argument count is already checked.
1580 */
1581 int
1582generate_LISTAPPEND(cctx_T *cctx)
1583{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001584 type_T *list_type;
1585 type_T *item_type;
1586 type_T *expected;
1587
1588 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001589 // For checking the item type we use the declared type of the list and the
1590 // current type of the added item, adding a string to [1, 2] is OK.
1591 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001592 if (arg_type_modifiable(list_type, 1) == FAIL)
1593 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001594 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001595 expected = list_type->tt_member;
1596 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1597 return FAIL;
1598
1599 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1600 return FAIL;
1601
Bram Moolenaar078a4612022-01-04 15:17:03 +00001602 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001603 return OK;
1604}
1605
1606/*
1607 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1608 * Argument count is already checked.
1609 */
1610 int
1611generate_BLOBAPPEND(cctx_T *cctx)
1612{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001613 type_T *item_type;
1614
Bram Moolenaarfa103972022-09-29 19:14:42 +01001615 // Caller already checked that blob_type is a blob, check it is modifiable.
1616 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1617 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001618 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001619 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1620 return FAIL;
1621
1622 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1623 return FAIL;
1624
Bram Moolenaar078a4612022-01-04 15:17:03 +00001625 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001626 return OK;
1627}
1628
1629/*
1630 * Generate an ISN_DCALL or ISN_UCALL instruction.
1631 * Return FAIL if the number of arguments is wrong.
1632 */
1633 int
1634generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1635{
1636 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001637 int regular_args = ufunc->uf_args.ga_len;
1638 int argcount = pushed_argcount;
1639
1640 RETURN_OK_IF_SKIP(cctx);
1641 if (argcount > regular_args && !has_varargs(ufunc))
1642 {
1643 semsg(_(e_too_many_arguments_for_function_str),
1644 printable_func_name(ufunc));
1645 return FAIL;
1646 }
1647 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1648 {
1649 semsg(_(e_not_enough_arguments_for_function_str),
1650 printable_func_name(ufunc));
1651 return FAIL;
1652 }
1653
1654 if (ufunc->uf_def_status != UF_NOT_COMPILED
1655 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1656 {
1657 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001658 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001659
1660 for (i = 0; i < argcount; ++i)
1661 {
1662 type_T *expected;
1663 type_T *actual;
1664
Bram Moolenaar078a4612022-01-04 15:17:03 +00001665 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001666 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001667 && i >= regular_args - ufunc->uf_def_args.ga_len)
1668 {
1669 // assume v:none used for default argument value
1670 continue;
1671 }
1672 if (i < regular_args)
1673 {
1674 if (ufunc->uf_arg_types == NULL)
1675 continue;
1676 expected = ufunc->uf_arg_types[i];
1677 }
1678 else if (ufunc->uf_va_type == NULL
1679 || ufunc->uf_va_type == &t_list_any)
1680 // possibly a lambda or "...: any"
1681 expected = &t_any;
1682 else
1683 expected = ufunc->uf_va_type->tt_member;
1684 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1685 TRUE, FALSE) == FAIL)
1686 {
1687 arg_type_mismatch(expected, actual, i + 1);
1688 return FAIL;
1689 }
1690 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001691 compile_type = get_compile_type(ufunc);
1692 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001693 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001694 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695 return FAIL;
1696 }
1697 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1698 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001699 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001700 ufunc->uf_name);
1701 return FAIL;
1702 }
1703
1704 if ((isn = generate_instr(cctx,
1705 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1706 : ISN_UCALL)) == NULL)
1707 return FAIL;
1708 if (isn->isn_type == ISN_DCALL)
1709 {
1710 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1711 isn->isn_arg.dfunc.cdf_argcount = argcount;
1712 }
1713 else
1714 {
1715 // A user function may be deleted and redefined later, can't use the
1716 // ufunc pointer, need to look it up again at runtime.
1717 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1718 isn->isn_arg.ufunc.cuf_argcount = argcount;
1719 }
1720
Bram Moolenaar078a4612022-01-04 15:17:03 +00001721 // drop the argument types
1722 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001723
Bram Moolenaar078a4612022-01-04 15:17:03 +00001724 // add return type
1725 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001726}
1727
1728/*
1729 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1730 */
1731 int
1732generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1733{
1734 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001735
1736 RETURN_OK_IF_SKIP(cctx);
1737 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1738 return FAIL;
1739 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1740 isn->isn_arg.ufunc.cuf_argcount = argcount;
1741
Bram Moolenaar078a4612022-01-04 15:17:03 +00001742 // drop the argument types
1743 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001744
Bram Moolenaar078a4612022-01-04 15:17:03 +00001745 // add return value
1746 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001747}
1748
1749/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001750 * Check the arguments of function "type" against the types on the stack.
1751 * Returns OK or FAIL;
1752 */
1753 int
1754check_func_args_from_type(
1755 cctx_T *cctx,
1756 type_T *type,
1757 int argcount,
1758 int at_top,
1759 char_u *name)
1760{
1761 if (type->tt_argcount != -1)
1762 {
1763 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1764
1765 if (argcount < type->tt_min_argcount - varargs)
1766 {
1767 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1768 return FAIL;
1769 }
1770 if (!varargs && argcount > type->tt_argcount)
1771 {
1772 emsg_funcname(e_too_many_arguments_for_function_str, name);
1773 return FAIL;
1774 }
1775 if (type->tt_args != NULL)
1776 {
1777 int i;
1778
1779 for (i = 0; i < argcount; ++i)
1780 {
1781 int offset = -argcount + i - (at_top ? 0 : 1);
1782 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1783 type_T *expected;
1784
1785 if (varargs && i >= type->tt_argcount - 1)
1786 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1787 else if (i >= type->tt_min_argcount
1788 && actual->tt_type == VAR_SPECIAL)
1789 expected = &t_any;
1790 else
1791 expected = type->tt_args[i];
1792 if (need_type(actual, expected, offset, i + 1,
1793 cctx, TRUE, FALSE) == FAIL)
1794 {
1795 arg_type_mismatch(expected, actual, i + 1);
1796 return FAIL;
1797 }
1798 }
1799 }
1800 }
1801
1802 return OK;
1803}
1804/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001805 * Generate an ISN_PCALL instruction.
1806 * "type" is the type of the FuncRef.
1807 */
1808 int
1809generate_PCALL(
1810 cctx_T *cctx,
1811 int argcount,
1812 char_u *name,
1813 type_T *type,
1814 int at_top)
1815{
1816 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001817 type_T *ret_type;
1818
1819 RETURN_OK_IF_SKIP(cctx);
1820
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001821 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822 ret_type = &t_any;
1823 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1824 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001825 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1826 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001827
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001828 ret_type = type->tt_member;
1829 if (ret_type == &t_unknown)
1830 // return type not known yet, use a runtime check
1831 ret_type = &t_any;
1832 }
1833 else
1834 {
1835 semsg(_(e_not_callable_type_str), name);
1836 return FAIL;
1837 }
1838
1839 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1840 return FAIL;
1841 isn->isn_arg.pfunc.cpf_top = at_top;
1842 isn->isn_arg.pfunc.cpf_argcount = argcount;
1843
Bram Moolenaar078a4612022-01-04 15:17:03 +00001844 // drop the arguments and the funcref/partial
1845 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001846
Bram Moolenaar078a4612022-01-04 15:17:03 +00001847 // push the return value
1848 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849
1850 // If partial is above the arguments it must be cleared and replaced with
1851 // the return value.
1852 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1853 return FAIL;
1854
1855 return OK;
1856}
1857
1858/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001859 * Generate an ISN_DEFER instruction.
1860 */
1861 int
1862generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1863{
1864 isn_T *isn;
1865
1866 RETURN_OK_IF_SKIP(cctx);
1867 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1868 return FAIL;
1869 isn->isn_arg.defer.defer_var_idx = var_idx;
1870 isn->isn_arg.defer.defer_argcount = argcount;
1871 return OK;
1872}
1873
1874/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875 * Generate an ISN_STRINGMEMBER instruction.
1876 */
1877 int
1878generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1879{
1880 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001881 type_T *type;
1882
1883 RETURN_OK_IF_SKIP(cctx);
1884 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1885 return FAIL;
1886 isn->isn_arg.string = vim_strnsave(name, len);
1887
1888 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001889 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001890 if (type->tt_type != VAR_DICT
1891 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001892 {
1893 char *tofree;
1894
1895 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1896 name, type_name(type, &tofree));
1897 vim_free(tofree);
1898 return FAIL;
1899 }
1900 // change dict type to dict member type
1901 if (type->tt_type == VAR_DICT)
1902 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001903 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00001904 ? &t_any : type->tt_member;
1905 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001906 }
1907
1908 return OK;
1909}
1910
1911/*
1912 * Generate an ISN_ECHO instruction.
1913 */
1914 int
1915generate_ECHO(cctx_T *cctx, int with_white, int count)
1916{
1917 isn_T *isn;
1918
1919 RETURN_OK_IF_SKIP(cctx);
1920 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1921 return FAIL;
1922 isn->isn_arg.echo.echo_with_white = with_white;
1923 isn->isn_arg.echo.echo_count = count;
1924
1925 return OK;
1926}
1927
1928/*
1929 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1930 */
1931 int
1932generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1933{
1934 isn_T *isn;
1935
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01001936 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001937 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1938 return FAIL;
1939 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001940 return OK;
1941}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001942
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001943/*
1944 * Generate an ISN_ECHOWINDOW instruction
1945 */
1946 int
1947generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
1948{
1949 isn_T *isn;
1950
1951 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
1952 return FAIL;
1953 isn->isn_arg.echowin.ewin_count = count;
1954 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955 return OK;
1956}
1957
1958/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001959 * Generate an ISN_SOURCE instruction.
1960 */
1961 int
1962generate_SOURCE(cctx_T *cctx, int sid)
1963{
1964 isn_T *isn;
1965
1966 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1967 return FAIL;
1968 isn->isn_arg.number = sid;
1969
1970 return OK;
1971}
1972
1973/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001974 * Generate an ISN_PUT instruction.
1975 */
1976 int
1977generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1978{
1979 isn_T *isn;
1980
1981 RETURN_OK_IF_SKIP(cctx);
1982 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1983 return FAIL;
1984 isn->isn_arg.put.put_regname = regname;
1985 isn->isn_arg.put.put_lnum = lnum;
1986 return OK;
1987}
1988
1989/*
1990 * Generate an EXEC instruction that takes a string argument.
1991 * A copy is made of "line".
1992 */
1993 int
1994generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1995{
1996 isn_T *isn;
1997
1998 RETURN_OK_IF_SKIP(cctx);
1999 if ((isn = generate_instr(cctx, isntype)) == NULL)
2000 return FAIL;
2001 isn->isn_arg.string = vim_strsave(line);
2002 return OK;
2003}
2004
2005/*
2006 * Generate an EXEC instruction that takes a string argument.
2007 * "str" must be allocated, it is consumed.
2008 */
2009 int
2010generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2011{
2012 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002013 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002014
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002015 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002016 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002017 if ((isn = generate_instr(cctx, isntype)) == NULL)
2018 ret = FAIL;
2019 else
2020 {
2021 isn->isn_arg.string = str;
2022 return OK;
2023 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002024 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002025 vim_free(str);
2026 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002027}
2028
2029 int
2030generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2031{
2032 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002033
2034 RETURN_OK_IF_SKIP(cctx);
2035 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2036 return FAIL;
2037 isn->isn_arg.string = vim_strsave(line);
2038
Bram Moolenaar078a4612022-01-04 15:17:03 +00002039 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040}
2041
2042 int
2043generate_EXECCONCAT(cctx_T *cctx, int count)
2044{
2045 isn_T *isn;
2046
2047 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2048 return FAIL;
2049 isn->isn_arg.number = count;
2050 return OK;
2051}
2052
2053/*
2054 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2055 */
2056 int
2057generate_RANGE(cctx_T *cctx, char_u *range)
2058{
2059 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002060
2061 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2062 return FAIL;
2063 isn->isn_arg.string = range;
2064
Bram Moolenaar078a4612022-01-04 15:17:03 +00002065 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002066}
2067
2068 int
2069generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2070{
2071 isn_T *isn;
2072
2073 RETURN_OK_IF_SKIP(cctx);
2074 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2075 return FAIL;
2076 isn->isn_arg.unpack.unp_count = var_count;
2077 isn->isn_arg.unpack.unp_semicolon = semicolon;
2078 return OK;
2079}
2080
2081/*
2082 * Generate an instruction for any command modifiers.
2083 */
2084 int
2085generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2086{
2087 isn_T *isn;
2088
2089 if (has_cmdmod(cmod, FALSE))
2090 {
2091 cctx->ctx_has_cmdmod = TRUE;
2092
2093 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2094 return FAIL;
2095 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2096 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2097 return FAIL;
2098 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2099 // filter program now belongs to the instruction
2100 cmod->cmod_filter_regmatch.regprog = NULL;
2101 }
2102
2103 return OK;
2104}
2105
2106 int
2107generate_undo_cmdmods(cctx_T *cctx)
2108{
2109 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2110 return FAIL;
2111 cctx->ctx_has_cmdmod = FALSE;
2112 return OK;
2113}
2114
2115/*
2116 * Generate a STORE instruction for "dest", not being "dest_local".
2117 * Return FAIL when out of memory.
2118 */
2119 int
2120generate_store_var(
2121 cctx_T *cctx,
2122 assign_dest_T dest,
2123 int opt_flags,
2124 int vimvaridx,
2125 int scriptvar_idx,
2126 int scriptvar_sid,
2127 type_T *type,
2128 char_u *name)
2129{
2130 switch (dest)
2131 {
2132 case dest_option:
2133 return generate_STOREOPT(cctx, ISN_STOREOPT,
2134 skip_option_env_lead(name), opt_flags);
2135 case dest_func_option:
2136 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2137 skip_option_env_lead(name), opt_flags);
2138 case dest_global:
2139 // include g: with the name, easier to execute that way
2140 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2141 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2142 case dest_buffer:
2143 // include b: with the name, easier to execute that way
2144 return generate_STORE(cctx, ISN_STOREB, 0, name);
2145 case dest_window:
2146 // include w: with the name, easier to execute that way
2147 return generate_STORE(cctx, ISN_STOREW, 0, name);
2148 case dest_tab:
2149 // include t: with the name, easier to execute that way
2150 return generate_STORE(cctx, ISN_STORET, 0, name);
2151 case dest_env:
2152 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2153 case dest_reg:
2154 return generate_STORE(cctx, ISN_STOREREG,
2155 name[1] == '@' ? '"' : name[1], NULL);
2156 case dest_vimvar:
2157 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2158 case dest_script:
2159 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002160 {
2161 isntype_T isn_type = ISN_STORES;
2162
2163 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01002164 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2165 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2166 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002167 {
2168 // "import autoload './dir/script.vim'" - load script first
2169 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2170 return FAIL;
2171 isn_type = ISN_STOREEXPORT;
2172 }
2173
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002174 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002175 return generate_OLDSCRIPT(cctx, isn_type, name,
2176 scriptvar_sid, type);
2177 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
2179 scriptvar_sid, scriptvar_idx, type);
2180 case dest_local:
2181 case dest_expr:
2182 // cannot happen
2183 break;
2184 }
2185 return FAIL;
2186}
2187
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002188/*
2189 * Return TRUE when inside a "for" or "while" loop.
2190 */
2191 int
2192inside_loop_scope(cctx_T *cctx)
2193{
2194 scope_T *scope = cctx->ctx_scope;
2195
2196 for (;;)
2197 {
2198 if (scope == NULL)
2199 break;
2200 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2201 return TRUE;
2202 scope = scope->se_outer;
2203 }
2204 return FALSE;
2205}
2206
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002207 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002208generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002209{
2210 if (lhs->lhs_dest != dest_local)
2211 return generate_store_var(cctx, lhs->lhs_dest,
2212 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2213 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2214 lhs->lhs_type, lhs->lhs_name);
2215
2216 if (lhs->lhs_lvar != NULL)
2217 {
2218 garray_T *instr = &cctx->ctx_instr;
2219 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2220
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002221 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2222 // ISN_STORENR.
2223 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224 if (lhs->lhs_lvar->lv_from_outer == 0
2225 && instr->ga_len == instr_count + 1
2226 && isn->isn_type == ISN_PUSHNR)
2227 {
2228 varnumber_T val = isn->isn_arg.number;
2229 garray_T *stack = &cctx->ctx_type_stack;
2230
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002231 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002232 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002233 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002234 --instr->ga_len;
2235 }
2236 else
2237 {
2238 isn->isn_type = ISN_STORENR;
2239 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2240 isn->isn_arg.storenr.stnr_val = val;
2241 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002242 if (stack->ga_len > 0)
2243 --stack->ga_len;
2244 }
2245 else if (lhs->lhs_lvar->lv_from_outer > 0)
2246 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002247 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002248 else
2249 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2250 }
2251 return OK;
2252}
2253
2254#if defined(FEAT_PROFILE) || defined(PROTO)
2255 void
2256may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2257{
2258 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2259 generate_instr(cctx, ISN_PROF_END);
2260}
2261#endif
2262
2263
2264/*
2265 * Delete an instruction, free what it contains.
2266 */
2267 void
2268delete_instr(isn_T *isn)
2269{
2270 switch (isn->isn_type)
2271 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002272 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002273 case ISN_DEF:
2274 case ISN_EXEC:
2275 case ISN_EXECRANGE:
2276 case ISN_EXEC_SPLIT:
2277 case ISN_LEGACY_EVAL:
2278 case ISN_LOADAUTO:
2279 case ISN_LOADB:
2280 case ISN_LOADENV:
2281 case ISN_LOADG:
2282 case ISN_LOADOPT:
2283 case ISN_LOADT:
2284 case ISN_LOADW:
2285 case ISN_LOCKUNLOCK:
2286 case ISN_PUSHEXC:
2287 case ISN_PUSHFUNC:
2288 case ISN_PUSHS:
2289 case ISN_RANGE:
2290 case ISN_STOREAUTO:
2291 case ISN_STOREB:
2292 case ISN_STOREENV:
2293 case ISN_STOREG:
2294 case ISN_STORET:
2295 case ISN_STOREW:
2296 case ISN_STRINGMEMBER:
2297 vim_free(isn->isn_arg.string);
2298 break;
2299
2300 case ISN_SUBSTITUTE:
2301 {
2302 int idx;
2303 isn_T *list = isn->isn_arg.subs.subs_instr;
2304
2305 vim_free(isn->isn_arg.subs.subs_cmd);
2306 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2307 delete_instr(list + idx);
2308 vim_free(list);
2309 }
2310 break;
2311
2312 case ISN_INSTR:
2313 {
2314 int idx;
2315 isn_T *list = isn->isn_arg.instr;
2316
2317 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2318 delete_instr(list + idx);
2319 vim_free(list);
2320 }
2321 break;
2322
2323 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002324 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002325 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002326 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002327 vim_free(isn->isn_arg.loadstore.ls_name);
2328 break;
2329
2330 case ISN_UNLET:
2331 case ISN_UNLETENV:
2332 vim_free(isn->isn_arg.unlet.ul_name);
2333 break;
2334
2335 case ISN_STOREOPT:
2336 case ISN_STOREFUNCOPT:
2337 vim_free(isn->isn_arg.storeopt.so_name);
2338 break;
2339
2340 case ISN_PUSHBLOB: // push blob isn_arg.blob
2341 blob_unref(isn->isn_arg.blob);
2342 break;
2343
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002344 case ISN_UCALL:
2345 vim_free(isn->isn_arg.ufunc.cuf_name);
2346 break;
2347
2348 case ISN_FUNCREF:
2349 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002350 funcref_T *funcref = &isn->isn_arg.funcref;
2351 funcref_extra_T *extra = funcref->fr_extra;
2352
2353 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002354 {
2355 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002356 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002357 ufunc_T *ufunc = dfunc->df_ufunc;
2358
2359 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2360 func_ptr_unref(ufunc);
2361 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002362 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002363 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002364 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002365
2366 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002367 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002368 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002369 vim_free(name);
2370 }
2371 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002372 }
2373 }
2374 break;
2375
2376 case ISN_DCALL:
2377 {
2378 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002379 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002380
2381 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002382 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 func_ptr_unref(dfunc->df_ufunc);
2384 }
2385 break;
2386
2387 case ISN_NEWFUNC:
2388 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002389 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002390
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002391 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002392 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002393 ufunc_T *ufunc = find_func_even_dead(
2394 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002395
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002396 if (ufunc != NULL)
2397 {
2398 unlink_def_function(ufunc);
2399 func_ptr_unref(ufunc);
2400 }
2401
2402 vim_free(arg->nfa_lambda);
2403 vim_free(arg->nfa_global);
2404 vim_free(arg);
2405 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002406 }
2407 break;
2408
2409 case ISN_CHECKTYPE:
2410 case ISN_SETTYPE:
2411 free_type(isn->isn_arg.type.ct_type);
2412 break;
2413
2414 case ISN_CMDMOD:
2415 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002416 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002417 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2418 break;
2419
2420 case ISN_LOADSCRIPT:
2421 case ISN_STORESCRIPT:
2422 vim_free(isn->isn_arg.script.scriptref);
2423 break;
2424
2425 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002426 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002427 break;
2428
2429 case ISN_CEXPR_CORE:
2430 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2431 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2432 break;
2433
2434 case ISN_2BOOL:
2435 case ISN_2STRING:
2436 case ISN_2STRING_ANY:
2437 case ISN_ADDBLOB:
2438 case ISN_ADDLIST:
2439 case ISN_ANYINDEX:
2440 case ISN_ANYSLICE:
2441 case ISN_BCALL:
2442 case ISN_BLOBAPPEND:
2443 case ISN_BLOBINDEX:
2444 case ISN_BLOBSLICE:
2445 case ISN_CATCH:
2446 case ISN_CEXPR_AUCMD:
2447 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002448 case ISN_CLEARDICT:
2449 case ISN_CMDMOD_REV:
2450 case ISN_COMPAREANY:
2451 case ISN_COMPAREBLOB:
2452 case ISN_COMPAREBOOL:
2453 case ISN_COMPAREDICT:
2454 case ISN_COMPAREFLOAT:
2455 case ISN_COMPAREFUNC:
2456 case ISN_COMPARELIST:
2457 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002458 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002459 case ISN_COMPARESPECIAL:
2460 case ISN_COMPARESTRING:
2461 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002462 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002463 case ISN_COND2BOOL:
2464 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002465 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002466 case ISN_DROP:
2467 case ISN_ECHO:
2468 case ISN_ECHOCONSOLE:
2469 case ISN_ECHOERR:
2470 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002471 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002472 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002473 case ISN_ENDTRY:
2474 case ISN_EXECCONCAT:
2475 case ISN_EXECUTE:
2476 case ISN_FINALLY:
2477 case ISN_FINISH:
2478 case ISN_FOR:
2479 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002480 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002481 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002482 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002483 case ISN_JUMP_IF_ARG_SET:
2484 case ISN_LISTAPPEND:
2485 case ISN_LISTINDEX:
2486 case ISN_LISTSLICE:
2487 case ISN_LOAD:
2488 case ISN_LOADBDICT:
2489 case ISN_LOADGDICT:
2490 case ISN_LOADOUTER:
2491 case ISN_LOADREG:
2492 case ISN_LOADTDICT:
2493 case ISN_LOADV:
2494 case ISN_LOADWDICT:
2495 case ISN_LOCKCONST:
2496 case ISN_MEMBER:
2497 case ISN_NEGATENR:
2498 case ISN_NEWDICT:
2499 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002500 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002501 case ISN_OPANY:
2502 case ISN_OPFLOAT:
2503 case ISN_OPNR:
2504 case ISN_PCALL:
2505 case ISN_PCALL_END:
2506 case ISN_PROF_END:
2507 case ISN_PROF_START:
2508 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002509 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002510 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002511 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002512 case ISN_PUSHNR:
2513 case ISN_PUSHSPEC:
2514 case ISN_PUT:
2515 case ISN_REDIREND:
2516 case ISN_REDIRSTART:
2517 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002518 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002519 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002520 case ISN_SHUFFLE:
2521 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002522 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002523 case ISN_STORE:
2524 case ISN_STOREINDEX:
2525 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002526 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002527 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002528 case ISN_STORERANGE:
2529 case ISN_STOREREG:
2530 case ISN_STOREV:
2531 case ISN_STRINDEX:
2532 case ISN_STRSLICE:
2533 case ISN_THROW:
2534 case ISN_TRYCONT:
2535 case ISN_UNLETINDEX:
2536 case ISN_UNLETRANGE:
2537 case ISN_UNPACK:
2538 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002539 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002540 // nothing allocated
2541 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002542 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002543}
2544
2545 void
2546clear_instr_ga(garray_T *gap)
2547{
2548 int idx;
2549
2550 for (idx = 0; idx < gap->ga_len; ++idx)
2551 delete_instr(((isn_T *)gap->ga_data) + idx);
2552 ga_clear(gap);
2553}
2554
2555
2556#endif // defined(FEAT_EVAL)