blob: 48e7edfedab43d04c16737304fbf73f80d50981a [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9instr.c: Dealing with instructions of a compiled function
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24
25/////////////////////////////////////////////////////////////////////
26// Following generate_ functions expect the caller to call ga_grow().
27
28#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
29#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
30
31/*
32 * Generate an instruction without arguments.
33 * Returns a pointer to the new instruction, NULL if failed.
34 */
35 isn_T *
36generate_instr(cctx_T *cctx, isntype_T isn_type)
37{
38 garray_T *instr = &cctx->ctx_instr;
39 isn_T *isn;
40
41 RETURN_NULL_IF_SKIP(cctx);
42 if (GA_GROW_FAILS(instr, 1))
43 return NULL;
44 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
45 isn->isn_type = isn_type;
46 isn->isn_lnum = cctx->ctx_lnum + 1;
47 ++instr->ga_len;
48
49 return isn;
50}
51
52/*
53 * Generate an instruction without arguments.
54 * "drop" will be removed from the stack.
55 * Returns a pointer to the new instruction, NULL if failed.
56 */
57 isn_T *
58generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
59{
Bram Moolenaardc7c3662021-12-20 15:04:29 +000060 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +000061 cctx->ctx_type_stack.ga_len -= drop;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000062 return generate_instr(cctx, isn_type);
63}
64
65/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000066 * Generate instruction "isn_type" and put "type" on the type stack,
67 * use "decl_type" for the declared type.
Bram Moolenaardc7c3662021-12-20 15:04:29 +000068 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +000069 static isn_T *
Bram Moolenaar078a4612022-01-04 15:17:03 +000070generate_instr_type2(
71 cctx_T *cctx,
72 isntype_T isn_type,
73 type_T *type,
74 type_T *decl_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000075{
76 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000077
78 if ((isn = generate_instr(cctx, isn_type)) == NULL)
79 return NULL;
80
Bram Moolenaar078a4612022-01-04 15:17:03 +000081 if (push_type_stack2(cctx, type == NULL ? &t_any : type,
82 decl_type == NULL ? &t_any : decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000083 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084
85 return isn;
86}
87
88/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000089 * Generate instruction "isn_type" and put "type" on the type stack.
90 * Uses "any" for the declared type, which works for constants. For declared
91 * variables use generate_instr_type2().
92 */
93 isn_T *
94generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
95{
96 return generate_instr_type2(cctx, isn_type, type, &t_any);
97}
98
99/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000100 * Generate an ISN_DEBUG instruction.
101 */
102 isn_T *
103generate_instr_debug(cctx_T *cctx)
104{
105 isn_T *isn;
106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
107 + cctx->ctx_ufunc->uf_dfunc_idx;
108
109 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
110 return NULL;
111 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
112 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
113 return isn;
114}
115
116/*
117 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
118 * But only for simple types.
119 * When "tolerant" is TRUE convert most types to string, e.g. a List.
120 */
121 int
122may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
123{
124 isn_T *isn;
125 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000127
128 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000129 type = get_type_on_stack(cctx, -1 - offset);
130 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000131 {
132 // nothing to be done
133 case VAR_STRING: return OK;
134
135 // conversion possible
136 case VAR_SPECIAL:
137 case VAR_BOOL:
138 case VAR_NUMBER:
139 case VAR_FLOAT:
140 break;
141
142 // conversion possible (with runtime check)
143 case VAR_ANY:
144 case VAR_UNKNOWN:
145 isntype = ISN_2STRING_ANY;
146 break;
147
148 // conversion possible when tolerant
149 case VAR_LIST:
150 if (tolerant)
151 {
152 isntype = ISN_2STRING_ANY;
153 break;
154 }
155 // FALLTHROUGH
156
157 // conversion not possible
158 case VAR_VOID:
159 case VAR_BLOB:
160 case VAR_FUNC:
161 case VAR_PARTIAL:
162 case VAR_DICT:
163 case VAR_JOB:
164 case VAR_CHANNEL:
165 case VAR_INSTR:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000166 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000167 return FAIL;
168 }
169
Bram Moolenaar078a4612022-01-04 15:17:03 +0000170 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000171 if ((isn = generate_instr(cctx, isntype)) == NULL)
172 return FAIL;
173 isn->isn_arg.tostring.offset = offset;
174 isn->isn_arg.tostring.tolerant = tolerant;
175
176 return OK;
177}
178
179 static int
180check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
181{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000182 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
183 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000184 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000185 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000186 {
187 if (*op == '+')
188 emsg(_(e_wrong_argument_type_for_plus));
189 else
190 semsg(_(e_char_requires_number_or_float_arguments), *op);
191 return FAIL;
192 }
193 return OK;
194}
195
196/*
197 * Generate instruction for "+". For a list this creates a new list.
198 */
199 int
200generate_add_instr(
201 cctx_T *cctx,
202 vartype_T vartype,
203 type_T *type1,
204 type_T *type2,
205 exprtype_T expr_type)
206{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000207 isn_T *isn = generate_instr_drop(cctx,
208 vartype == VAR_NUMBER ? ISN_OPNR
209 : vartype == VAR_LIST ? ISN_ADDLIST
210 : vartype == VAR_BLOB ? ISN_ADDBLOB
211#ifdef FEAT_FLOAT
212 : vartype == VAR_FLOAT ? ISN_OPFLOAT
213#endif
214 : ISN_OPANY, 1);
215
216 if (vartype != VAR_LIST && vartype != VAR_BLOB
217 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000218 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000220 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000221 && check_number_or_float(
222 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
223 return FAIL;
224
225 if (isn != NULL)
226 {
227 if (isn->isn_type == ISN_ADDLIST)
228 isn->isn_arg.op.op_type = expr_type;
229 else
230 isn->isn_arg.op.op_type = EXPR_ADD;
231 }
232
233 // When concatenating two lists with different member types the member type
234 // becomes "any".
235 if (vartype == VAR_LIST
236 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
237 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000238 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000239
240 return isn == NULL ? FAIL : OK;
241}
242
243/*
244 * Get the type to use for an instruction for an operation on "type1" and
245 * "type2". If they are matching use a type-specific instruction. Otherwise
246 * fall back to runtime type checking.
247 */
248 vartype_T
249operator_type(type_T *type1, type_T *type2)
250{
251 if (type1->tt_type == type2->tt_type
252 && (type1->tt_type == VAR_NUMBER
253 || type1->tt_type == VAR_LIST
254#ifdef FEAT_FLOAT
255 || type1->tt_type == VAR_FLOAT
256#endif
257 || type1->tt_type == VAR_BLOB))
258 return type1->tt_type;
259 return VAR_ANY;
260}
261
262/*
263 * Generate an instruction with two arguments. The instruction depends on the
264 * type of the arguments.
265 */
266 int
267generate_two_op(cctx_T *cctx, char_u *op)
268{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000269 type_T *type1;
270 type_T *type2;
271 vartype_T vartype;
272 isn_T *isn;
273
274 RETURN_OK_IF_SKIP(cctx);
275
276 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000277 type1 = get_type_on_stack(cctx, 1);
278 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000279 vartype = operator_type(type1, type2);
280
281 switch (*op)
282 {
283 case '+':
284 if (generate_add_instr(cctx, vartype, type1, type2,
285 EXPR_COPY) == FAIL)
286 return FAIL;
287 break;
288
289 case '-':
290 case '*':
291 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
292 op) == FAIL)
293 return FAIL;
294 if (vartype == VAR_NUMBER)
295 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
296#ifdef FEAT_FLOAT
297 else if (vartype == VAR_FLOAT)
298 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
299#endif
300 else
301 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
302 if (isn != NULL)
303 isn->isn_arg.op.op_type = *op == '*'
304 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
305 break;
306
307 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000308 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000309 && type1->tt_type != VAR_NUMBER)
310 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000311 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000312 && type2->tt_type != VAR_NUMBER))
313 {
314 emsg(_(e_percent_requires_number_arguments));
315 return FAIL;
316 }
317 isn = generate_instr_drop(cctx,
318 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
319 if (isn != NULL)
320 isn->isn_arg.op.op_type = EXPR_REM;
321 break;
322 }
323
324 // correct type of result
325 if (vartype == VAR_ANY)
326 {
327 type_T *type = &t_any;
328
329#ifdef FEAT_FLOAT
330 // float+number and number+float results in float
331 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
332 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
333 type = &t_float;
334#endif
Bram Moolenaar078a4612022-01-04 15:17:03 +0000335 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000336 }
337
338 return OK;
339}
340
341/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000342 * Get the instruction to use for comparing two values with specified types.
343 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000344 * Return ISN_DROP when failed.
345 */
346 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000347get_compare_isn(
348 exprtype_T exprtype,
349 typval_T *tv1,
350 typval_T *tv2,
351 type_T *type1,
352 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000353{
354 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000355 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
356 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000357
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000358 if (vartype1 == VAR_UNKNOWN)
359 vartype1 = VAR_ANY;
360 if (vartype2 == VAR_UNKNOWN)
361 vartype2 = VAR_ANY;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000362
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000363 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000364 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000365 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000366 {
367 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
368 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
369 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
370 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
371 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
372 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
373 case VAR_LIST: isntype = ISN_COMPARELIST; break;
374 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
375 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
376 default: isntype = ISN_COMPAREANY; break;
377 }
378 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000379 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
380 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
381 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
382 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
383 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000384 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000385 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000386 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000387 if ((vartype1 == VAR_SPECIAL
388 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
389 : type1 == &t_none)
390 && vartype2 != VAR_STRING)
391 || (vartype2 == VAR_SPECIAL
392 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
393 : type2 == &t_none)
394 && vartype1 != VAR_STRING))
395 {
396 semsg(_(e_cannot_compare_str_with_str),
397 vartype_name(vartype1), vartype_name(vartype2));
398 return ISN_DROP;
399 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000400 // although comparing null with number, float or bool is not useful, we
401 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000402 isntype = ISN_COMPARENULL;
403 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404
405 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
406 && (isntype == ISN_COMPAREBOOL
407 || isntype == ISN_COMPARESPECIAL
408 || isntype == ISN_COMPARENR
409 || isntype == ISN_COMPAREFLOAT))
410 {
411 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000412 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000413 return ISN_DROP;
414 }
415 if (isntype == ISN_DROP
416 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000417 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
418 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000419 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000420 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000421 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
422 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000423 {
424 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000425 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000426 return ISN_DROP;
427 }
428 return isntype;
429}
430
431 int
432check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
433{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000434 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000435 return FAIL;
436 return OK;
437}
438
439/*
440 * Generate an ISN_COMPARE* instruction with a boolean result.
441 */
442 int
443generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
444{
445 isntype_T isntype;
446 isn_T *isn;
447 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000448
449 RETURN_OK_IF_SKIP(cctx);
450
451 // Get the known type of the two items on the stack. If they are matching
452 // use a type-specific instruction. Otherwise fall back to runtime type
453 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000454 isntype = get_compare_isn(exprtype, NULL, NULL,
455 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456 if (isntype == ISN_DROP)
457 return FAIL;
458
459 if ((isn = generate_instr(cctx, isntype)) == NULL)
460 return FAIL;
461 isn->isn_arg.op.op_type = exprtype;
462 isn->isn_arg.op.op_ic = ic;
463
464 // takes two arguments, puts one bool back
465 if (stack->ga_len >= 2)
466 {
467 --stack->ga_len;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000468 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469 }
470
471 return OK;
472}
473
474/*
475 * Generate an ISN_2BOOL instruction.
476 * "offset" is the offset in the type stack.
477 */
478 int
479generate_2BOOL(cctx_T *cctx, int invert, int offset)
480{
481 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000482
483 RETURN_OK_IF_SKIP(cctx);
484 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
485 return FAIL;
486 isn->isn_arg.tobool.invert = invert;
487 isn->isn_arg.tobool.offset = offset;
488
489 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000490 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491
492 return OK;
493}
494
495/*
496 * Generate an ISN_COND2BOOL instruction.
497 */
498 int
499generate_COND2BOOL(cctx_T *cctx)
500{
501 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000502
503 RETURN_OK_IF_SKIP(cctx);
504 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
505 return FAIL;
506
507 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000508 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000509
510 return OK;
511}
512
513 int
514generate_TYPECHECK(
515 cctx_T *cctx,
516 type_T *expected,
517 int offset,
518 int argidx)
519{
520 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000521
522 RETURN_OK_IF_SKIP(cctx);
523 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
524 return FAIL;
525 isn->isn_arg.type.ct_type = alloc_type(expected);
526 isn->isn_arg.type.ct_off = (int8_T)offset;
527 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
528
529 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000530 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000531
532 return OK;
533}
534
535 int
536generate_SETTYPE(
537 cctx_T *cctx,
538 type_T *expected)
539{
540 isn_T *isn;
541
542 RETURN_OK_IF_SKIP(cctx);
543 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
544 return FAIL;
545 isn->isn_arg.type.ct_type = alloc_type(expected);
546 return OK;
547}
548
549/*
550 * Generate a PUSH instruction for "tv".
551 * "tv" will be consumed or cleared.
552 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
553 */
554 int
555generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
556{
557 if (tv != NULL)
558 {
559 switch (tv->v_type)
560 {
561 case VAR_UNKNOWN:
562 break;
563 case VAR_BOOL:
564 generate_PUSHBOOL(cctx, tv->vval.v_number);
565 break;
566 case VAR_SPECIAL:
567 generate_PUSHSPEC(cctx, tv->vval.v_number);
568 break;
569 case VAR_NUMBER:
570 generate_PUSHNR(cctx, tv->vval.v_number);
571 break;
572#ifdef FEAT_FLOAT
573 case VAR_FLOAT:
574 generate_PUSHF(cctx, tv->vval.v_float);
575 break;
576#endif
577 case VAR_BLOB:
578 generate_PUSHBLOB(cctx, tv->vval.v_blob);
579 tv->vval.v_blob = NULL;
580 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000581 case VAR_LIST:
582 if (tv->vval.v_list != NULL)
583 iemsg("non-empty list constant not supported");
584 generate_NEWLIST(cctx, 0);
585 break;
586 case VAR_DICT:
587 if (tv->vval.v_dict != NULL)
588 iemsg("non-empty dict constant not supported");
589 generate_NEWDICT(cctx, 0);
590 break;
591#ifdef FEAT_JOB_CHANNEL
592 case VAR_JOB:
593 if (tv->vval.v_job != NULL)
594 iemsg("non-null job constant not supported");
595 generate_PUSHJOB(cctx, NULL);
596 break;
597 case VAR_CHANNEL:
598 if (tv->vval.v_channel != NULL)
599 iemsg("non-null channel constant not supported");
600 generate_PUSHCHANNEL(cctx, NULL);
601 break;
602#endif
603 case VAR_FUNC:
604 if (tv->vval.v_string != NULL)
605 iemsg("non-null function constant not supported");
606 generate_PUSHFUNC(cctx, NULL, &t_func_unknown);
607 break;
608 case VAR_PARTIAL:
609 if (tv->vval.v_partial != NULL)
610 iemsg("non-null partial constant not supported");
611 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
612 == NULL)
613 return FAIL;
614 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 case VAR_STRING:
616 generate_PUSHS(cctx, &tv->vval.v_string);
617 tv->vval.v_string = NULL;
618 break;
619 default:
620 iemsg("constant type not supported");
621 clear_tv(tv);
622 return FAIL;
623 }
624 tv->v_type = VAR_UNKNOWN;
625 }
626 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
681#if defined(FEAT_FLOAT) || defined(PROTO)
682/*
683 * Generate an ISN_PUSHF instruction.
684 */
685 int
686generate_PUSHF(cctx_T *cctx, float_T fnumber)
687{
688 isn_T *isn;
689
690 RETURN_OK_IF_SKIP(cctx);
691 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
692 return FAIL;
693 isn->isn_arg.fnumber = fnumber;
694
695 return OK;
696}
697#endif
698
699/*
700 * Generate an ISN_PUSHS instruction.
701 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
702 */
703 int
704generate_PUSHS(cctx_T *cctx, char_u **str)
705{
706 isn_T *isn;
707
708 if (cctx->ctx_skip == SKIP_YES)
709 {
710 if (str != NULL)
711 VIM_CLEAR(*str);
712 return OK;
713 }
714 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
715 {
716 if (str != NULL)
717 VIM_CLEAR(*str);
718 return FAIL;
719 }
720 isn->isn_arg.string = str == NULL ? NULL : *str;
721
722 return OK;
723}
724
725/*
726 * Generate an ISN_PUSHCHANNEL instruction.
727 * Consumes "channel".
728 */
729 int
730generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
731{
732 isn_T *isn;
733
734 RETURN_OK_IF_SKIP(cctx);
735 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
736 return FAIL;
737 isn->isn_arg.channel = channel;
738
739 return OK;
740}
741
742/*
743 * Generate an ISN_PUSHJOB instruction.
744 * Consumes "job".
745 */
746 int
747generate_PUSHJOB(cctx_T *cctx, job_T *job)
748{
749 isn_T *isn;
750
751 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000752 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_job)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000753 return FAIL;
754 isn->isn_arg.job = job;
755
756 return OK;
757}
758
759/*
760 * Generate an ISN_PUSHBLOB instruction.
761 * Consumes "blob".
762 */
763 int
764generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
765{
766 isn_T *isn;
767
768 RETURN_OK_IF_SKIP(cctx);
769 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
770 return FAIL;
771 isn->isn_arg.blob = blob;
772
773 return OK;
774}
775
776/*
777 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000778 */
779 int
780generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
781{
782 isn_T *isn;
783 char_u *funcname;
784
785 RETURN_OK_IF_SKIP(cctx);
786 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
787 return FAIL;
788 if (name == NULL)
789 funcname = NULL;
Bram Moolenaard041f422022-01-12 19:54:00 +0000790 else if (*name == K_SPECIAL // script-local
791 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000792 funcname = vim_strsave(name);
793 else
794 {
795 funcname = alloc(STRLEN(name) + 3);
796 if (funcname != NULL)
797 {
798 STRCPY(funcname, "g:");
799 STRCPY(funcname + 2, name);
800 }
801 }
802
803 isn->isn_arg.string = funcname;
804 return OK;
805}
806
807/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000808 * Generate an ISN_AUTOLOAD instruction.
809 */
810 int
811generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
812{
813 isn_T *isn;
814
815 RETURN_OK_IF_SKIP(cctx);
816 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
817 return FAIL;
818 isn->isn_arg.string = vim_strsave(name);
819 if (isn->isn_arg.string == NULL)
820 return FAIL;
821 return OK;
822}
823
824/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000825 * Generate an ISN_GETITEM instruction with "index".
826 * "with_op" is TRUE for "+=" and other operators, the stack has the current
827 * value below the list with values.
828 */
829 int
830generate_GETITEM(cctx_T *cctx, int index, int with_op)
831{
832 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000833 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000834 type_T *item_type = &t_any;
835
836 RETURN_OK_IF_SKIP(cctx);
837
838 if (type->tt_type != VAR_LIST)
839 {
840 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000841 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842 return FAIL;
843 }
844 item_type = type->tt_member;
845 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
846 return FAIL;
847 isn->isn_arg.getitem.gi_index = index;
848 isn->isn_arg.getitem.gi_with_op = with_op;
849
850 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000851 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000852}
853
854/*
855 * Generate an ISN_SLICE instruction with "count".
856 */
857 int
858generate_SLICE(cctx_T *cctx, int count)
859{
860 isn_T *isn;
861
862 RETURN_OK_IF_SKIP(cctx);
863 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
864 return FAIL;
865 isn->isn_arg.number = count;
866 return OK;
867}
868
869/*
870 * Generate an ISN_CHECKLEN instruction with "min_len".
871 */
872 int
873generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
874{
875 isn_T *isn;
876
877 RETURN_OK_IF_SKIP(cctx);
878
879 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
880 return FAIL;
881 isn->isn_arg.checklen.cl_min_len = min_len;
882 isn->isn_arg.checklen.cl_more_OK = more_OK;
883
884 return OK;
885}
886
887/*
888 * Generate an ISN_STORE instruction.
889 */
890 int
891generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
892{
893 isn_T *isn;
894
895 RETURN_OK_IF_SKIP(cctx);
896 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
897 return FAIL;
898 if (name != NULL)
899 isn->isn_arg.string = vim_strsave(name);
900 else
901 isn->isn_arg.number = idx;
902
903 return OK;
904}
905
906/*
907 * Generate an ISN_STOREOUTER instruction.
908 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000909 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910generate_STOREOUTER(cctx_T *cctx, int idx, int level)
911{
912 isn_T *isn;
913
914 RETURN_OK_IF_SKIP(cctx);
915 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
916 return FAIL;
917 isn->isn_arg.outer.outer_idx = idx;
918 isn->isn_arg.outer.outer_depth = level;
919
920 return OK;
921}
922
923/*
924 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
925 */
926 int
927generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
928{
929 isn_T *isn;
930
931 RETURN_OK_IF_SKIP(cctx);
932 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
933 return FAIL;
934 isn->isn_arg.storenr.stnr_idx = idx;
935 isn->isn_arg.storenr.stnr_val = value;
936
937 return OK;
938}
939
940/*
941 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
942 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000943 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000944generate_STOREOPT(
945 cctx_T *cctx,
946 isntype_T isn_type,
947 char_u *name,
948 int opt_flags)
949{
950 isn_T *isn;
951
952 RETURN_OK_IF_SKIP(cctx);
953 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
954 return FAIL;
955 isn->isn_arg.storeopt.so_name = vim_strsave(name);
956 isn->isn_arg.storeopt.so_flags = opt_flags;
957
958 return OK;
959}
960
961/*
962 * Generate an ISN_LOAD or similar instruction.
963 */
964 int
965generate_LOAD(
966 cctx_T *cctx,
967 isntype_T isn_type,
968 int idx,
969 char_u *name,
970 type_T *type)
971{
972 isn_T *isn;
973
974 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000975 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000976 return FAIL;
977 if (name != NULL)
978 isn->isn_arg.string = vim_strsave(name);
979 else
980 isn->isn_arg.number = idx;
981
982 return OK;
983}
984
985/*
986 * Generate an ISN_LOADOUTER instruction
987 */
988 int
989generate_LOADOUTER(
990 cctx_T *cctx,
991 int idx,
992 int nesting,
993 type_T *type)
994{
995 isn_T *isn;
996
997 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000998 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000999 return FAIL;
1000 isn->isn_arg.outer.outer_idx = idx;
1001 isn->isn_arg.outer.outer_depth = nesting;
1002
1003 return OK;
1004}
1005
1006/*
1007 * Generate an ISN_LOADV instruction for v:var.
1008 */
1009 int
1010generate_LOADV(
1011 cctx_T *cctx,
1012 char_u *name,
1013 int error)
1014{
1015 int di_flags;
1016 int vidx = find_vim_var(name, &di_flags);
1017 type_T *type;
1018
1019 RETURN_OK_IF_SKIP(cctx);
1020 if (vidx < 0)
1021 {
1022 if (error)
1023 semsg(_(e_variable_not_found_str), name);
1024 return FAIL;
1025 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001026 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001027 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1028}
1029
1030/*
1031 * Generate an ISN_UNLET instruction.
1032 */
1033 int
1034generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1035{
1036 isn_T *isn;
1037
1038 RETURN_OK_IF_SKIP(cctx);
1039 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1040 return FAIL;
1041 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1042 isn->isn_arg.unlet.ul_forceit = forceit;
1043
1044 return OK;
1045}
1046
1047/*
1048 * Generate an ISN_LOCKCONST instruction.
1049 */
1050 int
1051generate_LOCKCONST(cctx_T *cctx)
1052{
1053 isn_T *isn;
1054
1055 RETURN_OK_IF_SKIP(cctx);
1056 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1057 return FAIL;
1058 return OK;
1059}
1060
1061/*
1062 * Generate an ISN_LOADS instruction.
1063 */
1064 int
1065generate_OLDSCRIPT(
1066 cctx_T *cctx,
1067 isntype_T isn_type,
1068 char_u *name,
1069 int sid,
1070 type_T *type)
1071{
1072 isn_T *isn;
1073
1074 RETURN_OK_IF_SKIP(cctx);
1075 if (isn_type == ISN_LOADS)
1076 isn = generate_instr_type(cctx, isn_type, type);
1077 else
1078 isn = generate_instr_drop(cctx, isn_type, 1);
1079 if (isn == NULL)
1080 return FAIL;
1081 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1082 isn->isn_arg.loadstore.ls_sid = sid;
1083
1084 return OK;
1085}
1086
1087/*
1088 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1089 */
1090 int
1091generate_VIM9SCRIPT(
1092 cctx_T *cctx,
1093 isntype_T isn_type,
1094 int sid,
1095 int idx,
1096 type_T *type)
1097{
1098 isn_T *isn;
1099 scriptref_T *sref;
1100 scriptitem_T *si = SCRIPT_ITEM(sid);
1101
1102 RETURN_OK_IF_SKIP(cctx);
1103 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001104 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001105 else
1106 isn = generate_instr_drop(cctx, isn_type, 1);
1107 if (isn == NULL)
1108 return FAIL;
1109
1110 // This requires three arguments, which doesn't fit in an instruction, thus
1111 // we need to allocate a struct for this.
1112 sref = ALLOC_ONE(scriptref_T);
1113 if (sref == NULL)
1114 return FAIL;
1115 isn->isn_arg.script.scriptref = sref;
1116 sref->sref_sid = sid;
1117 sref->sref_idx = idx;
1118 sref->sref_seq = si->sn_script_seq;
1119 sref->sref_type = type;
1120 return OK;
1121}
1122
1123/*
1124 * Generate an ISN_NEWLIST instruction.
1125 */
1126 int
1127generate_NEWLIST(cctx_T *cctx, int count)
1128{
1129 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001130 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001131 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001132 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001133
1134 RETURN_OK_IF_SKIP(cctx);
1135 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1136 return FAIL;
1137 isn->isn_arg.number = count;
1138
Bram Moolenaar078a4612022-01-04 15:17:03 +00001139 // Get the member type and the declared member type from all the items on
1140 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001141 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001142 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001143 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144
1145 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001146 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001147
1148 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001149 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001150}
1151
1152/*
1153 * Generate an ISN_NEWDICT instruction.
1154 */
1155 int
1156generate_NEWDICT(cctx_T *cctx, int count)
1157{
1158 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001159 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001160 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001161 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162
1163 RETURN_OK_IF_SKIP(cctx);
1164 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1165 return FAIL;
1166 isn->isn_arg.number = count;
1167
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001168 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001169 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001170 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001171
1172 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001173 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001174
1175 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001176 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001177}
1178
1179/*
1180 * Generate an ISN_FUNCREF instruction.
1181 */
1182 int
1183generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1184{
1185 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001186 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001187
1188 RETURN_OK_IF_SKIP(cctx);
1189 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1190 return FAIL;
1191 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1192 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1193 else
1194 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1195 cctx->ctx_has_closure = 1;
1196
1197 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001198 // the nested context, including this one. But not a function defined at
1199 // the script level.
1200 if ((ufunc->uf_flags & FC_CLOSURE)
1201 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001202 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1203
Bram Moolenaar078a4612022-01-04 15:17:03 +00001204 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1205 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001206}
1207
1208/*
1209 * Generate an ISN_NEWFUNC instruction.
1210 * "lambda_name" and "func_name" must be in allocated memory and will be
1211 * consumed.
1212 */
1213 int
1214generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1215{
1216 isn_T *isn;
1217
1218 if (cctx->ctx_skip == SKIP_YES)
1219 {
1220 vim_free(lambda_name);
1221 vim_free(func_name);
1222 return OK;
1223 }
1224 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1225 {
1226 vim_free(lambda_name);
1227 vim_free(func_name);
1228 return FAIL;
1229 }
1230 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1231 isn->isn_arg.newfunc.nf_global = func_name;
1232
1233 return OK;
1234}
1235
1236/*
1237 * Generate an ISN_DEF instruction: list functions
1238 */
1239 int
1240generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1241{
1242 isn_T *isn;
1243
1244 RETURN_OK_IF_SKIP(cctx);
1245 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1246 return FAIL;
1247 if (len > 0)
1248 {
1249 isn->isn_arg.string = vim_strnsave(name, len);
1250 if (isn->isn_arg.string == NULL)
1251 return FAIL;
1252 }
1253 return OK;
1254}
1255
1256/*
1257 * Generate an ISN_JUMP instruction.
1258 */
1259 int
1260generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1261{
1262 isn_T *isn;
1263 garray_T *stack = &cctx->ctx_type_stack;
1264
1265 RETURN_OK_IF_SKIP(cctx);
1266 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1267 return FAIL;
1268 isn->isn_arg.jump.jump_when = when;
1269 isn->isn_arg.jump.jump_where = where;
1270
1271 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1272 --stack->ga_len;
1273
1274 return OK;
1275}
1276
1277/*
1278 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1279 */
1280 int
1281generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1282{
1283 isn_T *isn;
1284
1285 RETURN_OK_IF_SKIP(cctx);
1286 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1287 return FAIL;
1288 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1289 // jump_where is set later
1290 return OK;
1291}
1292
1293 int
1294generate_FOR(cctx_T *cctx, int loop_idx)
1295{
1296 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001297
1298 RETURN_OK_IF_SKIP(cctx);
1299 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1300 return FAIL;
1301 isn->isn_arg.forloop.for_idx = loop_idx;
1302
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001303 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001304 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001305}
1306/*
1307 * Generate an ISN_TRYCONT instruction.
1308 */
1309 int
1310generate_TRYCONT(cctx_T *cctx, int levels, int where)
1311{
1312 isn_T *isn;
1313
1314 RETURN_OK_IF_SKIP(cctx);
1315 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1316 return FAIL;
1317 isn->isn_arg.trycont.tct_levels = levels;
1318 isn->isn_arg.trycont.tct_where = where;
1319
1320 return OK;
1321}
1322
1323
1324/*
1325 * Generate an ISN_BCALL instruction.
1326 * "method_call" is TRUE for "value->method()"
1327 * Return FAIL if the number of arguments is wrong.
1328 */
1329 int
1330generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1331{
1332 isn_T *isn;
1333 garray_T *stack = &cctx->ctx_type_stack;
1334 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001335 type2_T *typep;
1336 type2_T *argtypes = NULL;
1337 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1338 type2_T *maptype = NULL;
1339 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001340 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001341
1342 RETURN_OK_IF_SKIP(cctx);
1343 argoff = check_internal_func(func_idx, argcount);
1344 if (argoff < 0)
1345 return FAIL;
1346
1347 if (method_call && argoff > 1)
1348 {
1349 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1350 return FAIL;
1351 isn->isn_arg.shuffle.shfl_item = argcount;
1352 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1353 }
1354
1355 if (argcount > 0)
1356 {
1357 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001358 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001359 if (method_call && argoff > 1)
1360 {
1361 int i;
1362
1363 for (i = 0; i < argcount; ++i)
1364 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001365 ? typep[i + 1]
1366 : (i == argoff - 1) ? typep[0] : typep[i];
1367 argtypes = shuffled_argtypes;
1368 }
1369 else
1370 {
1371 int i;
1372
1373 for (i = 0; i < argcount; ++i)
1374 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001375 argtypes = shuffled_argtypes;
1376 }
1377 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1378 cctx) == FAIL)
1379 return FAIL;
1380 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001381 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382 }
1383
1384 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1385 return FAIL;
1386 isn->isn_arg.bfunc.cbf_idx = func_idx;
1387 isn->isn_arg.bfunc.cbf_argcount = argcount;
1388
1389 // Drop the argument types and push the return type.
1390 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001391 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1392 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001393 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001394
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001395 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1396 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001397 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001398 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001399
1400 return OK;
1401}
1402
1403/*
1404 * Generate an ISN_LISTAPPEND instruction. Works like add().
1405 * Argument count is already checked.
1406 */
1407 int
1408generate_LISTAPPEND(cctx_T *cctx)
1409{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001410 type_T *list_type;
1411 type_T *item_type;
1412 type_T *expected;
1413
1414 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001415 // For checking the item type we use the declared type of the list and the
1416 // current type of the added item, adding a string to [1, 2] is OK.
1417 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001418 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001419 expected = list_type->tt_member;
1420 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1421 return FAIL;
1422
1423 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1424 return FAIL;
1425
Bram Moolenaar078a4612022-01-04 15:17:03 +00001426 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001427 return OK;
1428}
1429
1430/*
1431 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1432 * Argument count is already checked.
1433 */
1434 int
1435generate_BLOBAPPEND(cctx_T *cctx)
1436{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437 type_T *item_type;
1438
1439 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001440 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001441 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1442 return FAIL;
1443
1444 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1445 return FAIL;
1446
Bram Moolenaar078a4612022-01-04 15:17:03 +00001447 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001448 return OK;
1449}
1450
1451/*
1452 * Generate an ISN_DCALL or ISN_UCALL instruction.
1453 * Return FAIL if the number of arguments is wrong.
1454 */
1455 int
1456generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1457{
1458 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001459 int regular_args = ufunc->uf_args.ga_len;
1460 int argcount = pushed_argcount;
1461
1462 RETURN_OK_IF_SKIP(cctx);
1463 if (argcount > regular_args && !has_varargs(ufunc))
1464 {
1465 semsg(_(e_too_many_arguments_for_function_str),
1466 printable_func_name(ufunc));
1467 return FAIL;
1468 }
1469 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1470 {
1471 semsg(_(e_not_enough_arguments_for_function_str),
1472 printable_func_name(ufunc));
1473 return FAIL;
1474 }
1475
1476 if (ufunc->uf_def_status != UF_NOT_COMPILED
1477 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1478 {
1479 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001480 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001481
1482 for (i = 0; i < argcount; ++i)
1483 {
1484 type_T *expected;
1485 type_T *actual;
1486
Bram Moolenaar078a4612022-01-04 15:17:03 +00001487 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001488 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001489 && i >= regular_args - ufunc->uf_def_args.ga_len)
1490 {
1491 // assume v:none used for default argument value
1492 continue;
1493 }
1494 if (i < regular_args)
1495 {
1496 if (ufunc->uf_arg_types == NULL)
1497 continue;
1498 expected = ufunc->uf_arg_types[i];
1499 }
1500 else if (ufunc->uf_va_type == NULL
1501 || ufunc->uf_va_type == &t_list_any)
1502 // possibly a lambda or "...: any"
1503 expected = &t_any;
1504 else
1505 expected = ufunc->uf_va_type->tt_member;
1506 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1507 TRUE, FALSE) == FAIL)
1508 {
1509 arg_type_mismatch(expected, actual, i + 1);
1510 return FAIL;
1511 }
1512 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001513 compile_type = get_compile_type(ufunc);
1514 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001515 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001516 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001517 return FAIL;
1518 }
1519 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1520 {
1521 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1522 ufunc->uf_name);
1523 return FAIL;
1524 }
1525
1526 if ((isn = generate_instr(cctx,
1527 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1528 : ISN_UCALL)) == NULL)
1529 return FAIL;
1530 if (isn->isn_type == ISN_DCALL)
1531 {
1532 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1533 isn->isn_arg.dfunc.cdf_argcount = argcount;
1534 }
1535 else
1536 {
1537 // A user function may be deleted and redefined later, can't use the
1538 // ufunc pointer, need to look it up again at runtime.
1539 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1540 isn->isn_arg.ufunc.cuf_argcount = argcount;
1541 }
1542
Bram Moolenaar078a4612022-01-04 15:17:03 +00001543 // drop the argument types
1544 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001545
Bram Moolenaar078a4612022-01-04 15:17:03 +00001546 // add return type
1547 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001548}
1549
1550/*
1551 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1552 */
1553 int
1554generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1555{
1556 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001557
1558 RETURN_OK_IF_SKIP(cctx);
1559 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1560 return FAIL;
1561 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1562 isn->isn_arg.ufunc.cuf_argcount = argcount;
1563
Bram Moolenaar078a4612022-01-04 15:17:03 +00001564 // drop the argument types
1565 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566
Bram Moolenaar078a4612022-01-04 15:17:03 +00001567 // add return value
1568 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569}
1570
1571/*
1572 * Generate an ISN_PCALL instruction.
1573 * "type" is the type of the FuncRef.
1574 */
1575 int
1576generate_PCALL(
1577 cctx_T *cctx,
1578 int argcount,
1579 char_u *name,
1580 type_T *type,
1581 int at_top)
1582{
1583 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001584 type_T *ret_type;
1585
1586 RETURN_OK_IF_SKIP(cctx);
1587
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001588 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001589 ret_type = &t_any;
1590 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1591 {
1592 if (type->tt_argcount != -1)
1593 {
1594 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1595
1596 if (argcount < type->tt_min_argcount - varargs)
1597 {
1598 semsg(_(e_not_enough_arguments_for_function_str), name);
1599 return FAIL;
1600 }
1601 if (!varargs && argcount > type->tt_argcount)
1602 {
1603 semsg(_(e_too_many_arguments_for_function_str), name);
1604 return FAIL;
1605 }
1606 if (type->tt_args != NULL)
1607 {
1608 int i;
1609
1610 for (i = 0; i < argcount; ++i)
1611 {
1612 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001613 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001614 type_T *expected;
1615
1616 if (varargs && i >= type->tt_argcount - 1)
1617 expected = type->tt_args[
1618 type->tt_argcount - 1]->tt_member;
1619 else if (i >= type->tt_min_argcount
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001620 && actual->tt_type == VAR_SPECIAL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001621 expected = &t_any;
1622 else
1623 expected = type->tt_args[i];
1624 if (need_type(actual, expected, offset, i + 1,
1625 cctx, TRUE, FALSE) == FAIL)
1626 {
1627 arg_type_mismatch(expected, actual, i + 1);
1628 return FAIL;
1629 }
1630 }
1631 }
1632 }
1633 ret_type = type->tt_member;
1634 if (ret_type == &t_unknown)
1635 // return type not known yet, use a runtime check
1636 ret_type = &t_any;
1637 }
1638 else
1639 {
1640 semsg(_(e_not_callable_type_str), name);
1641 return FAIL;
1642 }
1643
1644 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1645 return FAIL;
1646 isn->isn_arg.pfunc.cpf_top = at_top;
1647 isn->isn_arg.pfunc.cpf_argcount = argcount;
1648
Bram Moolenaar078a4612022-01-04 15:17:03 +00001649 // drop the arguments and the funcref/partial
1650 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001651
Bram Moolenaar078a4612022-01-04 15:17:03 +00001652 // push the return value
1653 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001654
1655 // If partial is above the arguments it must be cleared and replaced with
1656 // the return value.
1657 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1658 return FAIL;
1659
1660 return OK;
1661}
1662
1663/*
1664 * Generate an ISN_STRINGMEMBER instruction.
1665 */
1666 int
1667generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1668{
1669 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001670 type_T *type;
1671
1672 RETURN_OK_IF_SKIP(cctx);
1673 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1674 return FAIL;
1675 isn->isn_arg.string = vim_strnsave(name, len);
1676
1677 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001678 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001679 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001680 {
1681 char *tofree;
1682
1683 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1684 name, type_name(type, &tofree));
1685 vim_free(tofree);
1686 return FAIL;
1687 }
1688 // change dict type to dict member type
1689 if (type->tt_type == VAR_DICT)
1690 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001691 type_T *ntype = type->tt_member == &t_unknown
1692 ? &t_any : type->tt_member;
1693 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001694 }
1695
1696 return OK;
1697}
1698
1699/*
1700 * Generate an ISN_ECHO instruction.
1701 */
1702 int
1703generate_ECHO(cctx_T *cctx, int with_white, int count)
1704{
1705 isn_T *isn;
1706
1707 RETURN_OK_IF_SKIP(cctx);
1708 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1709 return FAIL;
1710 isn->isn_arg.echo.echo_with_white = with_white;
1711 isn->isn_arg.echo.echo_count = count;
1712
1713 return OK;
1714}
1715
1716/*
1717 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1718 */
1719 int
1720generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1721{
1722 isn_T *isn;
1723
1724 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1725 return FAIL;
1726 isn->isn_arg.number = count;
1727
1728 return OK;
1729}
1730
1731/*
1732 * Generate an ISN_PUT instruction.
1733 */
1734 int
1735generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1736{
1737 isn_T *isn;
1738
1739 RETURN_OK_IF_SKIP(cctx);
1740 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1741 return FAIL;
1742 isn->isn_arg.put.put_regname = regname;
1743 isn->isn_arg.put.put_lnum = lnum;
1744 return OK;
1745}
1746
1747/*
1748 * Generate an EXEC instruction that takes a string argument.
1749 * A copy is made of "line".
1750 */
1751 int
1752generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1753{
1754 isn_T *isn;
1755
1756 RETURN_OK_IF_SKIP(cctx);
1757 if ((isn = generate_instr(cctx, isntype)) == NULL)
1758 return FAIL;
1759 isn->isn_arg.string = vim_strsave(line);
1760 return OK;
1761}
1762
1763/*
1764 * Generate an EXEC instruction that takes a string argument.
1765 * "str" must be allocated, it is consumed.
1766 */
1767 int
1768generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1769{
1770 isn_T *isn;
1771
1772 if (cctx->ctx_skip == SKIP_YES)
1773 {
1774 vim_free(str);
1775 return OK;
1776 }
1777 if ((isn = generate_instr(cctx, isntype)) == NULL)
1778 {
1779 vim_free(str);
1780 return FAIL;
1781 }
1782 isn->isn_arg.string = str;
1783 return OK;
1784}
1785
1786 int
1787generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1788{
1789 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001790
1791 RETURN_OK_IF_SKIP(cctx);
1792 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1793 return FAIL;
1794 isn->isn_arg.string = vim_strsave(line);
1795
Bram Moolenaar078a4612022-01-04 15:17:03 +00001796 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001797}
1798
1799 int
1800generate_EXECCONCAT(cctx_T *cctx, int count)
1801{
1802 isn_T *isn;
1803
1804 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1805 return FAIL;
1806 isn->isn_arg.number = count;
1807 return OK;
1808}
1809
1810/*
1811 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1812 */
1813 int
1814generate_RANGE(cctx_T *cctx, char_u *range)
1815{
1816 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001817
1818 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1819 return FAIL;
1820 isn->isn_arg.string = range;
1821
Bram Moolenaar078a4612022-01-04 15:17:03 +00001822 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001823}
1824
1825 int
1826generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1827{
1828 isn_T *isn;
1829
1830 RETURN_OK_IF_SKIP(cctx);
1831 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1832 return FAIL;
1833 isn->isn_arg.unpack.unp_count = var_count;
1834 isn->isn_arg.unpack.unp_semicolon = semicolon;
1835 return OK;
1836}
1837
1838/*
1839 * Generate an instruction for any command modifiers.
1840 */
1841 int
1842generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1843{
1844 isn_T *isn;
1845
1846 if (has_cmdmod(cmod, FALSE))
1847 {
1848 cctx->ctx_has_cmdmod = TRUE;
1849
1850 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1851 return FAIL;
1852 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1853 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1854 return FAIL;
1855 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1856 // filter program now belongs to the instruction
1857 cmod->cmod_filter_regmatch.regprog = NULL;
1858 }
1859
1860 return OK;
1861}
1862
1863 int
1864generate_undo_cmdmods(cctx_T *cctx)
1865{
1866 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1867 return FAIL;
1868 cctx->ctx_has_cmdmod = FALSE;
1869 return OK;
1870}
1871
1872/*
1873 * Generate a STORE instruction for "dest", not being "dest_local".
1874 * Return FAIL when out of memory.
1875 */
1876 int
1877generate_store_var(
1878 cctx_T *cctx,
1879 assign_dest_T dest,
1880 int opt_flags,
1881 int vimvaridx,
1882 int scriptvar_idx,
1883 int scriptvar_sid,
1884 type_T *type,
1885 char_u *name)
1886{
1887 switch (dest)
1888 {
1889 case dest_option:
1890 return generate_STOREOPT(cctx, ISN_STOREOPT,
1891 skip_option_env_lead(name), opt_flags);
1892 case dest_func_option:
1893 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1894 skip_option_env_lead(name), opt_flags);
1895 case dest_global:
1896 // include g: with the name, easier to execute that way
1897 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1898 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1899 case dest_buffer:
1900 // include b: with the name, easier to execute that way
1901 return generate_STORE(cctx, ISN_STOREB, 0, name);
1902 case dest_window:
1903 // include w: with the name, easier to execute that way
1904 return generate_STORE(cctx, ISN_STOREW, 0, name);
1905 case dest_tab:
1906 // include t: with the name, easier to execute that way
1907 return generate_STORE(cctx, ISN_STORET, 0, name);
1908 case dest_env:
1909 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1910 case dest_reg:
1911 return generate_STORE(cctx, ISN_STOREREG,
1912 name[1] == '@' ? '"' : name[1], NULL);
1913 case dest_vimvar:
1914 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1915 case dest_script:
1916 if (scriptvar_idx < 0)
1917 // "s:" may be included in the name.
1918 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1919 scriptvar_sid, type);
1920 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1921 scriptvar_sid, scriptvar_idx, type);
1922 case dest_local:
1923 case dest_expr:
1924 // cannot happen
1925 break;
1926 }
1927 return FAIL;
1928}
1929
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001930/*
1931 * Return TRUE when inside a "for" or "while" loop.
1932 */
1933 int
1934inside_loop_scope(cctx_T *cctx)
1935{
1936 scope_T *scope = cctx->ctx_scope;
1937
1938 for (;;)
1939 {
1940 if (scope == NULL)
1941 break;
1942 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1943 return TRUE;
1944 scope = scope->se_outer;
1945 }
1946 return FALSE;
1947}
1948
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001949 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001950generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001951{
1952 if (lhs->lhs_dest != dest_local)
1953 return generate_store_var(cctx, lhs->lhs_dest,
1954 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1955 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1956 lhs->lhs_type, lhs->lhs_name);
1957
1958 if (lhs->lhs_lvar != NULL)
1959 {
1960 garray_T *instr = &cctx->ctx_instr;
1961 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1962
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001963 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1964 // ISN_STORENR.
1965 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001966 if (lhs->lhs_lvar->lv_from_outer == 0
1967 && instr->ga_len == instr_count + 1
1968 && isn->isn_type == ISN_PUSHNR)
1969 {
1970 varnumber_T val = isn->isn_arg.number;
1971 garray_T *stack = &cctx->ctx_type_stack;
1972
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001973 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001974 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001975 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001976 --instr->ga_len;
1977 }
1978 else
1979 {
1980 isn->isn_type = ISN_STORENR;
1981 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1982 isn->isn_arg.storenr.stnr_val = val;
1983 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984 if (stack->ga_len > 0)
1985 --stack->ga_len;
1986 }
1987 else if (lhs->lhs_lvar->lv_from_outer > 0)
1988 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1989 lhs->lhs_lvar->lv_from_outer);
1990 else
1991 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
1992 }
1993 return OK;
1994}
1995
1996#if defined(FEAT_PROFILE) || defined(PROTO)
1997 void
1998may_generate_prof_end(cctx_T *cctx, int prof_lnum)
1999{
2000 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2001 generate_instr(cctx, ISN_PROF_END);
2002}
2003#endif
2004
2005
2006/*
2007 * Delete an instruction, free what it contains.
2008 */
2009 void
2010delete_instr(isn_T *isn)
2011{
2012 switch (isn->isn_type)
2013 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002014 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002015 case ISN_DEF:
2016 case ISN_EXEC:
2017 case ISN_EXECRANGE:
2018 case ISN_EXEC_SPLIT:
2019 case ISN_LEGACY_EVAL:
2020 case ISN_LOADAUTO:
2021 case ISN_LOADB:
2022 case ISN_LOADENV:
2023 case ISN_LOADG:
2024 case ISN_LOADOPT:
2025 case ISN_LOADT:
2026 case ISN_LOADW:
2027 case ISN_LOCKUNLOCK:
2028 case ISN_PUSHEXC:
2029 case ISN_PUSHFUNC:
2030 case ISN_PUSHS:
2031 case ISN_RANGE:
2032 case ISN_STOREAUTO:
2033 case ISN_STOREB:
2034 case ISN_STOREENV:
2035 case ISN_STOREG:
2036 case ISN_STORET:
2037 case ISN_STOREW:
2038 case ISN_STRINGMEMBER:
2039 vim_free(isn->isn_arg.string);
2040 break;
2041
2042 case ISN_SUBSTITUTE:
2043 {
2044 int idx;
2045 isn_T *list = isn->isn_arg.subs.subs_instr;
2046
2047 vim_free(isn->isn_arg.subs.subs_cmd);
2048 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2049 delete_instr(list + idx);
2050 vim_free(list);
2051 }
2052 break;
2053
2054 case ISN_INSTR:
2055 {
2056 int idx;
2057 isn_T *list = isn->isn_arg.instr;
2058
2059 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2060 delete_instr(list + idx);
2061 vim_free(list);
2062 }
2063 break;
2064
2065 case ISN_LOADS:
2066 case ISN_STORES:
2067 vim_free(isn->isn_arg.loadstore.ls_name);
2068 break;
2069
2070 case ISN_UNLET:
2071 case ISN_UNLETENV:
2072 vim_free(isn->isn_arg.unlet.ul_name);
2073 break;
2074
2075 case ISN_STOREOPT:
2076 case ISN_STOREFUNCOPT:
2077 vim_free(isn->isn_arg.storeopt.so_name);
2078 break;
2079
2080 case ISN_PUSHBLOB: // push blob isn_arg.blob
2081 blob_unref(isn->isn_arg.blob);
2082 break;
2083
2084 case ISN_PUSHJOB:
2085#ifdef FEAT_JOB_CHANNEL
2086 job_unref(isn->isn_arg.job);
2087#endif
2088 break;
2089
2090 case ISN_PUSHCHANNEL:
2091#ifdef FEAT_JOB_CHANNEL
2092 channel_unref(isn->isn_arg.channel);
2093#endif
2094 break;
2095
2096 case ISN_UCALL:
2097 vim_free(isn->isn_arg.ufunc.cuf_name);
2098 break;
2099
2100 case ISN_FUNCREF:
2101 {
2102 if (isn->isn_arg.funcref.fr_func_name == NULL)
2103 {
2104 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2105 + isn->isn_arg.funcref.fr_dfunc_idx;
2106 ufunc_T *ufunc = dfunc->df_ufunc;
2107
2108 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2109 func_ptr_unref(ufunc);
2110 }
2111 else
2112 {
2113 char_u *name = isn->isn_arg.funcref.fr_func_name;
2114
2115 if (name != NULL)
2116 func_unref(name);
2117 vim_free(isn->isn_arg.funcref.fr_func_name);
2118 }
2119 }
2120 break;
2121
2122 case ISN_DCALL:
2123 {
2124 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2125 + isn->isn_arg.dfunc.cdf_idx;
2126
2127 if (dfunc->df_ufunc != NULL
2128 && func_name_refcount(dfunc->df_ufunc->uf_name))
2129 func_ptr_unref(dfunc->df_ufunc);
2130 }
2131 break;
2132
2133 case ISN_NEWFUNC:
2134 {
2135 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002136 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002137
2138 if (ufunc != NULL)
2139 {
2140 unlink_def_function(ufunc);
2141 func_ptr_unref(ufunc);
2142 }
2143
2144 vim_free(lambda);
2145 vim_free(isn->isn_arg.newfunc.nf_global);
2146 }
2147 break;
2148
2149 case ISN_CHECKTYPE:
2150 case ISN_SETTYPE:
2151 free_type(isn->isn_arg.type.ct_type);
2152 break;
2153
2154 case ISN_CMDMOD:
2155 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2156 ->cmod_filter_regmatch.regprog);
2157 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2158 break;
2159
2160 case ISN_LOADSCRIPT:
2161 case ISN_STORESCRIPT:
2162 vim_free(isn->isn_arg.script.scriptref);
2163 break;
2164
2165 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002166 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002167 break;
2168
2169 case ISN_CEXPR_CORE:
2170 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2171 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2172 break;
2173
2174 case ISN_2BOOL:
2175 case ISN_2STRING:
2176 case ISN_2STRING_ANY:
2177 case ISN_ADDBLOB:
2178 case ISN_ADDLIST:
2179 case ISN_ANYINDEX:
2180 case ISN_ANYSLICE:
2181 case ISN_BCALL:
2182 case ISN_BLOBAPPEND:
2183 case ISN_BLOBINDEX:
2184 case ISN_BLOBSLICE:
2185 case ISN_CATCH:
2186 case ISN_CEXPR_AUCMD:
2187 case ISN_CHECKLEN:
2188 case ISN_CHECKNR:
2189 case ISN_CLEARDICT:
2190 case ISN_CMDMOD_REV:
2191 case ISN_COMPAREANY:
2192 case ISN_COMPAREBLOB:
2193 case ISN_COMPAREBOOL:
2194 case ISN_COMPAREDICT:
2195 case ISN_COMPAREFLOAT:
2196 case ISN_COMPAREFUNC:
2197 case ISN_COMPARELIST:
2198 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002199 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002200 case ISN_COMPARESPECIAL:
2201 case ISN_COMPARESTRING:
2202 case ISN_CONCAT:
2203 case ISN_COND2BOOL:
2204 case ISN_DEBUG:
2205 case ISN_DROP:
2206 case ISN_ECHO:
2207 case ISN_ECHOCONSOLE:
2208 case ISN_ECHOERR:
2209 case ISN_ECHOMSG:
2210 case ISN_ENDTRY:
2211 case ISN_EXECCONCAT:
2212 case ISN_EXECUTE:
2213 case ISN_FINALLY:
2214 case ISN_FINISH:
2215 case ISN_FOR:
2216 case ISN_GETITEM:
2217 case ISN_JUMP:
2218 case ISN_JUMP_IF_ARG_SET:
2219 case ISN_LISTAPPEND:
2220 case ISN_LISTINDEX:
2221 case ISN_LISTSLICE:
2222 case ISN_LOAD:
2223 case ISN_LOADBDICT:
2224 case ISN_LOADGDICT:
2225 case ISN_LOADOUTER:
2226 case ISN_LOADREG:
2227 case ISN_LOADTDICT:
2228 case ISN_LOADV:
2229 case ISN_LOADWDICT:
2230 case ISN_LOCKCONST:
2231 case ISN_MEMBER:
2232 case ISN_NEGATENR:
2233 case ISN_NEWDICT:
2234 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002235 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002236 case ISN_OPANY:
2237 case ISN_OPFLOAT:
2238 case ISN_OPNR:
2239 case ISN_PCALL:
2240 case ISN_PCALL_END:
2241 case ISN_PROF_END:
2242 case ISN_PROF_START:
2243 case ISN_PUSHBOOL:
2244 case ISN_PUSHF:
2245 case ISN_PUSHNR:
2246 case ISN_PUSHSPEC:
2247 case ISN_PUT:
2248 case ISN_REDIREND:
2249 case ISN_REDIRSTART:
2250 case ISN_RETURN:
2251 case ISN_RETURN_VOID:
2252 case ISN_SHUFFLE:
2253 case ISN_SLICE:
2254 case ISN_STORE:
2255 case ISN_STOREINDEX:
2256 case ISN_STORENR:
2257 case ISN_STOREOUTER:
2258 case ISN_STORERANGE:
2259 case ISN_STOREREG:
2260 case ISN_STOREV:
2261 case ISN_STRINDEX:
2262 case ISN_STRSLICE:
2263 case ISN_THROW:
2264 case ISN_TRYCONT:
2265 case ISN_UNLETINDEX:
2266 case ISN_UNLETRANGE:
2267 case ISN_UNPACK:
2268 case ISN_USEDICT:
2269 // nothing allocated
2270 break;
2271 }
2272}
2273
2274 void
2275clear_instr_ga(garray_T *gap)
2276{
2277 int idx;
2278
2279 for (idx = 0; idx < gap->ga_len; ++idx)
2280 delete_instr(((isn_T *)gap->ga_data) + idx);
2281 ga_clear(gap);
2282}
2283
2284
2285#endif // defined(FEAT_EVAL)