blob: f0206211d80cbf5604f00e23563f85fb3f845627 [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");
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000595 generate_PUSHJOB(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000596 break;
597 case VAR_CHANNEL:
598 if (tv->vval.v_channel != NULL)
599 iemsg("non-null channel constant not supported");
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000600 generate_PUSHCHANNEL(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000601 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/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000726 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000727 */
728 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000729generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730{
731 isn_T *isn;
732
733 RETURN_OK_IF_SKIP(cctx);
734 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
735 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000736 return OK;
737}
738
739/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000740 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000741 */
742 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000743generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000744{
745 isn_T *isn;
746
747 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000748 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_job)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000749 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 return OK;
751}
752
753/*
754 * Generate an ISN_PUSHBLOB instruction.
755 * Consumes "blob".
756 */
757 int
758generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
759{
760 isn_T *isn;
761
762 RETURN_OK_IF_SKIP(cctx);
763 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
764 return FAIL;
765 isn->isn_arg.blob = blob;
766
767 return OK;
768}
769
770/*
771 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 */
773 int
774generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
775{
776 isn_T *isn;
777 char_u *funcname;
778
779 RETURN_OK_IF_SKIP(cctx);
780 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
781 return FAIL;
782 if (name == NULL)
783 funcname = NULL;
Bram Moolenaard041f422022-01-12 19:54:00 +0000784 else if (*name == K_SPECIAL // script-local
785 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000786 funcname = vim_strsave(name);
787 else
788 {
789 funcname = alloc(STRLEN(name) + 3);
790 if (funcname != NULL)
791 {
792 STRCPY(funcname, "g:");
793 STRCPY(funcname + 2, name);
794 }
795 }
796
797 isn->isn_arg.string = funcname;
798 return OK;
799}
800
801/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000802 * Generate an ISN_AUTOLOAD instruction.
803 */
804 int
805generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
806{
807 isn_T *isn;
808
809 RETURN_OK_IF_SKIP(cctx);
810 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
811 return FAIL;
812 isn->isn_arg.string = vim_strsave(name);
813 if (isn->isn_arg.string == NULL)
814 return FAIL;
815 return OK;
816}
817
818/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819 * Generate an ISN_GETITEM instruction with "index".
820 * "with_op" is TRUE for "+=" and other operators, the stack has the current
821 * value below the list with values.
822 */
823 int
824generate_GETITEM(cctx_T *cctx, int index, int with_op)
825{
826 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000827 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000828 type_T *item_type = &t_any;
829
830 RETURN_OK_IF_SKIP(cctx);
831
832 if (type->tt_type != VAR_LIST)
833 {
834 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000835 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000836 return FAIL;
837 }
838 item_type = type->tt_member;
839 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
840 return FAIL;
841 isn->isn_arg.getitem.gi_index = index;
842 isn->isn_arg.getitem.gi_with_op = with_op;
843
844 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000845 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846}
847
848/*
849 * Generate an ISN_SLICE instruction with "count".
850 */
851 int
852generate_SLICE(cctx_T *cctx, int count)
853{
854 isn_T *isn;
855
856 RETURN_OK_IF_SKIP(cctx);
857 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
858 return FAIL;
859 isn->isn_arg.number = count;
860 return OK;
861}
862
863/*
864 * Generate an ISN_CHECKLEN instruction with "min_len".
865 */
866 int
867generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
868{
869 isn_T *isn;
870
871 RETURN_OK_IF_SKIP(cctx);
872
873 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
874 return FAIL;
875 isn->isn_arg.checklen.cl_min_len = min_len;
876 isn->isn_arg.checklen.cl_more_OK = more_OK;
877
878 return OK;
879}
880
881/*
882 * Generate an ISN_STORE instruction.
883 */
884 int
885generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
886{
887 isn_T *isn;
888
889 RETURN_OK_IF_SKIP(cctx);
890 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
891 return FAIL;
892 if (name != NULL)
893 isn->isn_arg.string = vim_strsave(name);
894 else
895 isn->isn_arg.number = idx;
896
897 return OK;
898}
899
900/*
901 * Generate an ISN_STOREOUTER instruction.
902 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000903 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000904generate_STOREOUTER(cctx_T *cctx, int idx, int level)
905{
906 isn_T *isn;
907
908 RETURN_OK_IF_SKIP(cctx);
909 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
910 return FAIL;
911 isn->isn_arg.outer.outer_idx = idx;
912 isn->isn_arg.outer.outer_depth = level;
913
914 return OK;
915}
916
917/*
918 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
919 */
920 int
921generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
922{
923 isn_T *isn;
924
925 RETURN_OK_IF_SKIP(cctx);
926 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
927 return FAIL;
928 isn->isn_arg.storenr.stnr_idx = idx;
929 isn->isn_arg.storenr.stnr_val = value;
930
931 return OK;
932}
933
934/*
935 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
936 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000937 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938generate_STOREOPT(
939 cctx_T *cctx,
940 isntype_T isn_type,
941 char_u *name,
942 int opt_flags)
943{
944 isn_T *isn;
945
946 RETURN_OK_IF_SKIP(cctx);
947 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
948 return FAIL;
949 isn->isn_arg.storeopt.so_name = vim_strsave(name);
950 isn->isn_arg.storeopt.so_flags = opt_flags;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_LOAD or similar instruction.
957 */
958 int
959generate_LOAD(
960 cctx_T *cctx,
961 isntype_T isn_type,
962 int idx,
963 char_u *name,
964 type_T *type)
965{
966 isn_T *isn;
967
968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000969 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000970 return FAIL;
971 if (name != NULL)
972 isn->isn_arg.string = vim_strsave(name);
973 else
974 isn->isn_arg.number = idx;
975
976 return OK;
977}
978
979/*
980 * Generate an ISN_LOADOUTER instruction
981 */
982 int
983generate_LOADOUTER(
984 cctx_T *cctx,
985 int idx,
986 int nesting,
987 type_T *type)
988{
989 isn_T *isn;
990
991 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000992 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000993 return FAIL;
994 isn->isn_arg.outer.outer_idx = idx;
995 isn->isn_arg.outer.outer_depth = nesting;
996
997 return OK;
998}
999
1000/*
1001 * Generate an ISN_LOADV instruction for v:var.
1002 */
1003 int
1004generate_LOADV(
1005 cctx_T *cctx,
1006 char_u *name,
1007 int error)
1008{
1009 int di_flags;
1010 int vidx = find_vim_var(name, &di_flags);
1011 type_T *type;
1012
1013 RETURN_OK_IF_SKIP(cctx);
1014 if (vidx < 0)
1015 {
1016 if (error)
1017 semsg(_(e_variable_not_found_str), name);
1018 return FAIL;
1019 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001020 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001021 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1022}
1023
1024/*
1025 * Generate an ISN_UNLET instruction.
1026 */
1027 int
1028generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1029{
1030 isn_T *isn;
1031
1032 RETURN_OK_IF_SKIP(cctx);
1033 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1034 return FAIL;
1035 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1036 isn->isn_arg.unlet.ul_forceit = forceit;
1037
1038 return OK;
1039}
1040
1041/*
1042 * Generate an ISN_LOCKCONST instruction.
1043 */
1044 int
1045generate_LOCKCONST(cctx_T *cctx)
1046{
1047 isn_T *isn;
1048
1049 RETURN_OK_IF_SKIP(cctx);
1050 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1051 return FAIL;
1052 return OK;
1053}
1054
1055/*
1056 * Generate an ISN_LOADS instruction.
1057 */
1058 int
1059generate_OLDSCRIPT(
1060 cctx_T *cctx,
1061 isntype_T isn_type,
1062 char_u *name,
1063 int sid,
1064 type_T *type)
1065{
1066 isn_T *isn;
1067
1068 RETURN_OK_IF_SKIP(cctx);
1069 if (isn_type == ISN_LOADS)
1070 isn = generate_instr_type(cctx, isn_type, type);
1071 else
1072 isn = generate_instr_drop(cctx, isn_type, 1);
1073 if (isn == NULL)
1074 return FAIL;
1075 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1076 isn->isn_arg.loadstore.ls_sid = sid;
1077
1078 return OK;
1079}
1080
1081/*
1082 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1083 */
1084 int
1085generate_VIM9SCRIPT(
1086 cctx_T *cctx,
1087 isntype_T isn_type,
1088 int sid,
1089 int idx,
1090 type_T *type)
1091{
1092 isn_T *isn;
1093 scriptref_T *sref;
1094 scriptitem_T *si = SCRIPT_ITEM(sid);
1095
1096 RETURN_OK_IF_SKIP(cctx);
1097 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001098 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 else
1100 isn = generate_instr_drop(cctx, isn_type, 1);
1101 if (isn == NULL)
1102 return FAIL;
1103
1104 // This requires three arguments, which doesn't fit in an instruction, thus
1105 // we need to allocate a struct for this.
1106 sref = ALLOC_ONE(scriptref_T);
1107 if (sref == NULL)
1108 return FAIL;
1109 isn->isn_arg.script.scriptref = sref;
1110 sref->sref_sid = sid;
1111 sref->sref_idx = idx;
1112 sref->sref_seq = si->sn_script_seq;
1113 sref->sref_type = type;
1114 return OK;
1115}
1116
1117/*
1118 * Generate an ISN_NEWLIST instruction.
1119 */
1120 int
1121generate_NEWLIST(cctx_T *cctx, int count)
1122{
1123 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001124 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001125 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001126 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001127
1128 RETURN_OK_IF_SKIP(cctx);
1129 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1130 return FAIL;
1131 isn->isn_arg.number = count;
1132
Bram Moolenaar078a4612022-01-04 15:17:03 +00001133 // Get the member type and the declared member type from all the items on
1134 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001135 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001136 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001137 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001138
1139 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001140 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141
1142 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001143 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144}
1145
1146/*
1147 * Generate an ISN_NEWDICT instruction.
1148 */
1149 int
1150generate_NEWDICT(cctx_T *cctx, int count)
1151{
1152 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001153 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001155 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156
1157 RETURN_OK_IF_SKIP(cctx);
1158 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1159 return FAIL;
1160 isn->isn_arg.number = count;
1161
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001162 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001163 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001164 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165
1166 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001167 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001168
1169 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001170 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001171}
1172
1173/*
1174 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001175 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001176 */
1177 int
Bram Moolenaara915fa02022-03-23 11:29:15 +00001178generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc, isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179{
1180 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001181 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001182
1183 RETURN_OK_IF_SKIP(cctx);
1184 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1185 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001186 if (isnp != NULL)
1187 *isnp = isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001188 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1189 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1190 else
1191 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1192 cctx->ctx_has_closure = 1;
1193
1194 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001195 // the nested context, including this one. But not a function defined at
1196 // the script level.
1197 if ((ufunc->uf_flags & FC_CLOSURE)
1198 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001199 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1200
Bram Moolenaar078a4612022-01-04 15:17:03 +00001201 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1202 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001203}
1204
1205/*
1206 * Generate an ISN_NEWFUNC instruction.
1207 * "lambda_name" and "func_name" must be in allocated memory and will be
1208 * consumed.
1209 */
1210 int
1211generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1212{
1213 isn_T *isn;
1214
1215 if (cctx->ctx_skip == SKIP_YES)
1216 {
1217 vim_free(lambda_name);
1218 vim_free(func_name);
1219 return OK;
1220 }
1221 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1222 {
1223 vim_free(lambda_name);
1224 vim_free(func_name);
1225 return FAIL;
1226 }
1227 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1228 isn->isn_arg.newfunc.nf_global = func_name;
1229
1230 return OK;
1231}
1232
1233/*
1234 * Generate an ISN_DEF instruction: list functions
1235 */
1236 int
1237generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1238{
1239 isn_T *isn;
1240
1241 RETURN_OK_IF_SKIP(cctx);
1242 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1243 return FAIL;
1244 if (len > 0)
1245 {
1246 isn->isn_arg.string = vim_strnsave(name, len);
1247 if (isn->isn_arg.string == NULL)
1248 return FAIL;
1249 }
1250 return OK;
1251}
1252
1253/*
1254 * Generate an ISN_JUMP instruction.
1255 */
1256 int
1257generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1258{
1259 isn_T *isn;
1260 garray_T *stack = &cctx->ctx_type_stack;
1261
1262 RETURN_OK_IF_SKIP(cctx);
1263 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1264 return FAIL;
1265 isn->isn_arg.jump.jump_when = when;
1266 isn->isn_arg.jump.jump_where = where;
1267
1268 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1269 --stack->ga_len;
1270
1271 return OK;
1272}
1273
1274/*
1275 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1276 */
1277 int
1278generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1279{
1280 isn_T *isn;
1281
1282 RETURN_OK_IF_SKIP(cctx);
1283 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1284 return FAIL;
1285 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1286 // jump_where is set later
1287 return OK;
1288}
1289
1290 int
1291generate_FOR(cctx_T *cctx, int loop_idx)
1292{
1293 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294
1295 RETURN_OK_IF_SKIP(cctx);
1296 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1297 return FAIL;
1298 isn->isn_arg.forloop.for_idx = loop_idx;
1299
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001300 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001301 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001302}
1303/*
1304 * Generate an ISN_TRYCONT instruction.
1305 */
1306 int
1307generate_TRYCONT(cctx_T *cctx, int levels, int where)
1308{
1309 isn_T *isn;
1310
1311 RETURN_OK_IF_SKIP(cctx);
1312 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1313 return FAIL;
1314 isn->isn_arg.trycont.tct_levels = levels;
1315 isn->isn_arg.trycont.tct_where = where;
1316
1317 return OK;
1318}
1319
1320
1321/*
1322 * Generate an ISN_BCALL instruction.
1323 * "method_call" is TRUE for "value->method()"
1324 * Return FAIL if the number of arguments is wrong.
1325 */
1326 int
1327generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1328{
1329 isn_T *isn;
1330 garray_T *stack = &cctx->ctx_type_stack;
1331 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001332 type2_T *typep;
1333 type2_T *argtypes = NULL;
1334 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1335 type2_T *maptype = NULL;
1336 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001337 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338
1339 RETURN_OK_IF_SKIP(cctx);
1340 argoff = check_internal_func(func_idx, argcount);
1341 if (argoff < 0)
1342 return FAIL;
1343
1344 if (method_call && argoff > 1)
1345 {
1346 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1347 return FAIL;
1348 isn->isn_arg.shuffle.shfl_item = argcount;
1349 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1350 }
1351
1352 if (argcount > 0)
1353 {
1354 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001355 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001356 if (method_call && argoff > 1)
1357 {
1358 int i;
1359
1360 for (i = 0; i < argcount; ++i)
1361 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001362 ? typep[i + 1]
1363 : (i == argoff - 1) ? typep[0] : typep[i];
1364 argtypes = shuffled_argtypes;
1365 }
1366 else
1367 {
1368 int i;
1369
1370 for (i = 0; i < argcount; ++i)
1371 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372 argtypes = shuffled_argtypes;
1373 }
1374 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1375 cctx) == FAIL)
1376 return FAIL;
1377 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001378 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001379 }
1380
1381 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1382 return FAIL;
1383 isn->isn_arg.bfunc.cbf_idx = func_idx;
1384 isn->isn_arg.bfunc.cbf_argcount = argcount;
1385
1386 // Drop the argument types and push the return type.
1387 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001388 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1389 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001390 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001391
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001392 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1393 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001394 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001395 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001396
1397 return OK;
1398}
1399
1400/*
1401 * Generate an ISN_LISTAPPEND instruction. Works like add().
1402 * Argument count is already checked.
1403 */
1404 int
1405generate_LISTAPPEND(cctx_T *cctx)
1406{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001407 type_T *list_type;
1408 type_T *item_type;
1409 type_T *expected;
1410
1411 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001412 // For checking the item type we use the declared type of the list and the
1413 // current type of the added item, adding a string to [1, 2] is OK.
1414 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001415 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001416 expected = list_type->tt_member;
1417 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1418 return FAIL;
1419
1420 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1421 return FAIL;
1422
Bram Moolenaar078a4612022-01-04 15:17:03 +00001423 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001424 return OK;
1425}
1426
1427/*
1428 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1429 * Argument count is already checked.
1430 */
1431 int
1432generate_BLOBAPPEND(cctx_T *cctx)
1433{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001434 type_T *item_type;
1435
1436 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001437 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001438 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1439 return FAIL;
1440
1441 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1442 return FAIL;
1443
Bram Moolenaar078a4612022-01-04 15:17:03 +00001444 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001445 return OK;
1446}
1447
1448/*
1449 * Generate an ISN_DCALL or ISN_UCALL instruction.
1450 * Return FAIL if the number of arguments is wrong.
1451 */
1452 int
1453generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1454{
1455 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001456 int regular_args = ufunc->uf_args.ga_len;
1457 int argcount = pushed_argcount;
1458
1459 RETURN_OK_IF_SKIP(cctx);
1460 if (argcount > regular_args && !has_varargs(ufunc))
1461 {
1462 semsg(_(e_too_many_arguments_for_function_str),
1463 printable_func_name(ufunc));
1464 return FAIL;
1465 }
1466 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1467 {
1468 semsg(_(e_not_enough_arguments_for_function_str),
1469 printable_func_name(ufunc));
1470 return FAIL;
1471 }
1472
1473 if (ufunc->uf_def_status != UF_NOT_COMPILED
1474 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1475 {
1476 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001477 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001478
1479 for (i = 0; i < argcount; ++i)
1480 {
1481 type_T *expected;
1482 type_T *actual;
1483
Bram Moolenaar078a4612022-01-04 15:17:03 +00001484 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001485 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001486 && i >= regular_args - ufunc->uf_def_args.ga_len)
1487 {
1488 // assume v:none used for default argument value
1489 continue;
1490 }
1491 if (i < regular_args)
1492 {
1493 if (ufunc->uf_arg_types == NULL)
1494 continue;
1495 expected = ufunc->uf_arg_types[i];
1496 }
1497 else if (ufunc->uf_va_type == NULL
1498 || ufunc->uf_va_type == &t_list_any)
1499 // possibly a lambda or "...: any"
1500 expected = &t_any;
1501 else
1502 expected = ufunc->uf_va_type->tt_member;
1503 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1504 TRUE, FALSE) == FAIL)
1505 {
1506 arg_type_mismatch(expected, actual, i + 1);
1507 return FAIL;
1508 }
1509 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001510 compile_type = get_compile_type(ufunc);
1511 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001512 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001513 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001514 return FAIL;
1515 }
1516 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1517 {
1518 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1519 ufunc->uf_name);
1520 return FAIL;
1521 }
1522
1523 if ((isn = generate_instr(cctx,
1524 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1525 : ISN_UCALL)) == NULL)
1526 return FAIL;
1527 if (isn->isn_type == ISN_DCALL)
1528 {
1529 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1530 isn->isn_arg.dfunc.cdf_argcount = argcount;
1531 }
1532 else
1533 {
1534 // A user function may be deleted and redefined later, can't use the
1535 // ufunc pointer, need to look it up again at runtime.
1536 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1537 isn->isn_arg.ufunc.cuf_argcount = argcount;
1538 }
1539
Bram Moolenaar078a4612022-01-04 15:17:03 +00001540 // drop the argument types
1541 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001542
Bram Moolenaar078a4612022-01-04 15:17:03 +00001543 // add return type
1544 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001545}
1546
1547/*
1548 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1549 */
1550 int
1551generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1552{
1553 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001554
1555 RETURN_OK_IF_SKIP(cctx);
1556 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1557 return FAIL;
1558 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1559 isn->isn_arg.ufunc.cuf_argcount = argcount;
1560
Bram Moolenaar078a4612022-01-04 15:17:03 +00001561 // drop the argument types
1562 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001563
Bram Moolenaar078a4612022-01-04 15:17:03 +00001564 // add return value
1565 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566}
1567
1568/*
1569 * Generate an ISN_PCALL instruction.
1570 * "type" is the type of the FuncRef.
1571 */
1572 int
1573generate_PCALL(
1574 cctx_T *cctx,
1575 int argcount,
1576 char_u *name,
1577 type_T *type,
1578 int at_top)
1579{
1580 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001581 type_T *ret_type;
1582
1583 RETURN_OK_IF_SKIP(cctx);
1584
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001585 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001586 ret_type = &t_any;
1587 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1588 {
1589 if (type->tt_argcount != -1)
1590 {
1591 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1592
1593 if (argcount < type->tt_min_argcount - varargs)
1594 {
1595 semsg(_(e_not_enough_arguments_for_function_str), name);
1596 return FAIL;
1597 }
1598 if (!varargs && argcount > type->tt_argcount)
1599 {
1600 semsg(_(e_too_many_arguments_for_function_str), name);
1601 return FAIL;
1602 }
1603 if (type->tt_args != NULL)
1604 {
1605 int i;
1606
1607 for (i = 0; i < argcount; ++i)
1608 {
1609 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001610 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001611 type_T *expected;
1612
1613 if (varargs && i >= type->tt_argcount - 1)
1614 expected = type->tt_args[
1615 type->tt_argcount - 1]->tt_member;
1616 else if (i >= type->tt_min_argcount
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001617 && actual->tt_type == VAR_SPECIAL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001618 expected = &t_any;
1619 else
1620 expected = type->tt_args[i];
1621 if (need_type(actual, expected, offset, i + 1,
1622 cctx, TRUE, FALSE) == FAIL)
1623 {
1624 arg_type_mismatch(expected, actual, i + 1);
1625 return FAIL;
1626 }
1627 }
1628 }
1629 }
1630 ret_type = type->tt_member;
1631 if (ret_type == &t_unknown)
1632 // return type not known yet, use a runtime check
1633 ret_type = &t_any;
1634 }
1635 else
1636 {
1637 semsg(_(e_not_callable_type_str), name);
1638 return FAIL;
1639 }
1640
1641 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1642 return FAIL;
1643 isn->isn_arg.pfunc.cpf_top = at_top;
1644 isn->isn_arg.pfunc.cpf_argcount = argcount;
1645
Bram Moolenaar078a4612022-01-04 15:17:03 +00001646 // drop the arguments and the funcref/partial
1647 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001648
Bram Moolenaar078a4612022-01-04 15:17:03 +00001649 // push the return value
1650 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001651
1652 // If partial is above the arguments it must be cleared and replaced with
1653 // the return value.
1654 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1655 return FAIL;
1656
1657 return OK;
1658}
1659
1660/*
1661 * Generate an ISN_STRINGMEMBER instruction.
1662 */
1663 int
1664generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1665{
1666 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001667 type_T *type;
1668
1669 RETURN_OK_IF_SKIP(cctx);
1670 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1671 return FAIL;
1672 isn->isn_arg.string = vim_strnsave(name, len);
1673
1674 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001675 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001676 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001677 {
1678 char *tofree;
1679
1680 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1681 name, type_name(type, &tofree));
1682 vim_free(tofree);
1683 return FAIL;
1684 }
1685 // change dict type to dict member type
1686 if (type->tt_type == VAR_DICT)
1687 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001688 type_T *ntype = type->tt_member == &t_unknown
1689 ? &t_any : type->tt_member;
1690 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001691 }
1692
1693 return OK;
1694}
1695
1696/*
1697 * Generate an ISN_ECHO instruction.
1698 */
1699 int
1700generate_ECHO(cctx_T *cctx, int with_white, int count)
1701{
1702 isn_T *isn;
1703
1704 RETURN_OK_IF_SKIP(cctx);
1705 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1706 return FAIL;
1707 isn->isn_arg.echo.echo_with_white = with_white;
1708 isn->isn_arg.echo.echo_count = count;
1709
1710 return OK;
1711}
1712
1713/*
1714 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1715 */
1716 int
1717generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1718{
1719 isn_T *isn;
1720
1721 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1722 return FAIL;
1723 isn->isn_arg.number = count;
1724
1725 return OK;
1726}
1727
1728/*
1729 * Generate an ISN_PUT instruction.
1730 */
1731 int
1732generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1733{
1734 isn_T *isn;
1735
1736 RETURN_OK_IF_SKIP(cctx);
1737 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1738 return FAIL;
1739 isn->isn_arg.put.put_regname = regname;
1740 isn->isn_arg.put.put_lnum = lnum;
1741 return OK;
1742}
1743
1744/*
1745 * Generate an EXEC instruction that takes a string argument.
1746 * A copy is made of "line".
1747 */
1748 int
1749generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1750{
1751 isn_T *isn;
1752
1753 RETURN_OK_IF_SKIP(cctx);
1754 if ((isn = generate_instr(cctx, isntype)) == NULL)
1755 return FAIL;
1756 isn->isn_arg.string = vim_strsave(line);
1757 return OK;
1758}
1759
1760/*
1761 * Generate an EXEC instruction that takes a string argument.
1762 * "str" must be allocated, it is consumed.
1763 */
1764 int
1765generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1766{
1767 isn_T *isn;
1768
1769 if (cctx->ctx_skip == SKIP_YES)
1770 {
1771 vim_free(str);
1772 return OK;
1773 }
1774 if ((isn = generate_instr(cctx, isntype)) == NULL)
1775 {
1776 vim_free(str);
1777 return FAIL;
1778 }
1779 isn->isn_arg.string = str;
1780 return OK;
1781}
1782
1783 int
1784generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1785{
1786 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001787
1788 RETURN_OK_IF_SKIP(cctx);
1789 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1790 return FAIL;
1791 isn->isn_arg.string = vim_strsave(line);
1792
Bram Moolenaar078a4612022-01-04 15:17:03 +00001793 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001794}
1795
1796 int
1797generate_EXECCONCAT(cctx_T *cctx, int count)
1798{
1799 isn_T *isn;
1800
1801 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1802 return FAIL;
1803 isn->isn_arg.number = count;
1804 return OK;
1805}
1806
1807/*
1808 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1809 */
1810 int
1811generate_RANGE(cctx_T *cctx, char_u *range)
1812{
1813 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001814
1815 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1816 return FAIL;
1817 isn->isn_arg.string = range;
1818
Bram Moolenaar078a4612022-01-04 15:17:03 +00001819 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001820}
1821
1822 int
1823generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1824{
1825 isn_T *isn;
1826
1827 RETURN_OK_IF_SKIP(cctx);
1828 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1829 return FAIL;
1830 isn->isn_arg.unpack.unp_count = var_count;
1831 isn->isn_arg.unpack.unp_semicolon = semicolon;
1832 return OK;
1833}
1834
1835/*
1836 * Generate an instruction for any command modifiers.
1837 */
1838 int
1839generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1840{
1841 isn_T *isn;
1842
1843 if (has_cmdmod(cmod, FALSE))
1844 {
1845 cctx->ctx_has_cmdmod = TRUE;
1846
1847 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1848 return FAIL;
1849 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1850 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1851 return FAIL;
1852 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1853 // filter program now belongs to the instruction
1854 cmod->cmod_filter_regmatch.regprog = NULL;
1855 }
1856
1857 return OK;
1858}
1859
1860 int
1861generate_undo_cmdmods(cctx_T *cctx)
1862{
1863 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1864 return FAIL;
1865 cctx->ctx_has_cmdmod = FALSE;
1866 return OK;
1867}
1868
1869/*
1870 * Generate a STORE instruction for "dest", not being "dest_local".
1871 * Return FAIL when out of memory.
1872 */
1873 int
1874generate_store_var(
1875 cctx_T *cctx,
1876 assign_dest_T dest,
1877 int opt_flags,
1878 int vimvaridx,
1879 int scriptvar_idx,
1880 int scriptvar_sid,
1881 type_T *type,
1882 char_u *name)
1883{
1884 switch (dest)
1885 {
1886 case dest_option:
1887 return generate_STOREOPT(cctx, ISN_STOREOPT,
1888 skip_option_env_lead(name), opt_flags);
1889 case dest_func_option:
1890 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1891 skip_option_env_lead(name), opt_flags);
1892 case dest_global:
1893 // include g: with the name, easier to execute that way
1894 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1895 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1896 case dest_buffer:
1897 // include b: with the name, easier to execute that way
1898 return generate_STORE(cctx, ISN_STOREB, 0, name);
1899 case dest_window:
1900 // include w: with the name, easier to execute that way
1901 return generate_STORE(cctx, ISN_STOREW, 0, name);
1902 case dest_tab:
1903 // include t: with the name, easier to execute that way
1904 return generate_STORE(cctx, ISN_STORET, 0, name);
1905 case dest_env:
1906 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1907 case dest_reg:
1908 return generate_STORE(cctx, ISN_STOREREG,
1909 name[1] == '@' ? '"' : name[1], NULL);
1910 case dest_vimvar:
1911 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1912 case dest_script:
1913 if (scriptvar_idx < 0)
1914 // "s:" may be included in the name.
1915 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1916 scriptvar_sid, type);
1917 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1918 scriptvar_sid, scriptvar_idx, type);
1919 case dest_local:
1920 case dest_expr:
1921 // cannot happen
1922 break;
1923 }
1924 return FAIL;
1925}
1926
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001927/*
1928 * Return TRUE when inside a "for" or "while" loop.
1929 */
1930 int
1931inside_loop_scope(cctx_T *cctx)
1932{
1933 scope_T *scope = cctx->ctx_scope;
1934
1935 for (;;)
1936 {
1937 if (scope == NULL)
1938 break;
1939 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1940 return TRUE;
1941 scope = scope->se_outer;
1942 }
1943 return FALSE;
1944}
1945
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001946 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001947generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001948{
1949 if (lhs->lhs_dest != dest_local)
1950 return generate_store_var(cctx, lhs->lhs_dest,
1951 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1952 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1953 lhs->lhs_type, lhs->lhs_name);
1954
1955 if (lhs->lhs_lvar != NULL)
1956 {
1957 garray_T *instr = &cctx->ctx_instr;
1958 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1959
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001960 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1961 // ISN_STORENR.
1962 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001963 if (lhs->lhs_lvar->lv_from_outer == 0
1964 && instr->ga_len == instr_count + 1
1965 && isn->isn_type == ISN_PUSHNR)
1966 {
1967 varnumber_T val = isn->isn_arg.number;
1968 garray_T *stack = &cctx->ctx_type_stack;
1969
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001970 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001971 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001972 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001973 --instr->ga_len;
1974 }
1975 else
1976 {
1977 isn->isn_type = ISN_STORENR;
1978 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1979 isn->isn_arg.storenr.stnr_val = val;
1980 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001981 if (stack->ga_len > 0)
1982 --stack->ga_len;
1983 }
1984 else if (lhs->lhs_lvar->lv_from_outer > 0)
1985 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1986 lhs->lhs_lvar->lv_from_outer);
1987 else
1988 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
1989 }
1990 return OK;
1991}
1992
1993#if defined(FEAT_PROFILE) || defined(PROTO)
1994 void
1995may_generate_prof_end(cctx_T *cctx, int prof_lnum)
1996{
1997 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
1998 generate_instr(cctx, ISN_PROF_END);
1999}
2000#endif
2001
2002
2003/*
2004 * Delete an instruction, free what it contains.
2005 */
2006 void
2007delete_instr(isn_T *isn)
2008{
2009 switch (isn->isn_type)
2010 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002011 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002012 case ISN_DEF:
2013 case ISN_EXEC:
2014 case ISN_EXECRANGE:
2015 case ISN_EXEC_SPLIT:
2016 case ISN_LEGACY_EVAL:
2017 case ISN_LOADAUTO:
2018 case ISN_LOADB:
2019 case ISN_LOADENV:
2020 case ISN_LOADG:
2021 case ISN_LOADOPT:
2022 case ISN_LOADT:
2023 case ISN_LOADW:
2024 case ISN_LOCKUNLOCK:
2025 case ISN_PUSHEXC:
2026 case ISN_PUSHFUNC:
2027 case ISN_PUSHS:
2028 case ISN_RANGE:
2029 case ISN_STOREAUTO:
2030 case ISN_STOREB:
2031 case ISN_STOREENV:
2032 case ISN_STOREG:
2033 case ISN_STORET:
2034 case ISN_STOREW:
2035 case ISN_STRINGMEMBER:
2036 vim_free(isn->isn_arg.string);
2037 break;
2038
2039 case ISN_SUBSTITUTE:
2040 {
2041 int idx;
2042 isn_T *list = isn->isn_arg.subs.subs_instr;
2043
2044 vim_free(isn->isn_arg.subs.subs_cmd);
2045 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2046 delete_instr(list + idx);
2047 vim_free(list);
2048 }
2049 break;
2050
2051 case ISN_INSTR:
2052 {
2053 int idx;
2054 isn_T *list = isn->isn_arg.instr;
2055
2056 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2057 delete_instr(list + idx);
2058 vim_free(list);
2059 }
2060 break;
2061
2062 case ISN_LOADS:
2063 case ISN_STORES:
2064 vim_free(isn->isn_arg.loadstore.ls_name);
2065 break;
2066
2067 case ISN_UNLET:
2068 case ISN_UNLETENV:
2069 vim_free(isn->isn_arg.unlet.ul_name);
2070 break;
2071
2072 case ISN_STOREOPT:
2073 case ISN_STOREFUNCOPT:
2074 vim_free(isn->isn_arg.storeopt.so_name);
2075 break;
2076
2077 case ISN_PUSHBLOB: // push blob isn_arg.blob
2078 blob_unref(isn->isn_arg.blob);
2079 break;
2080
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002081 case ISN_UCALL:
2082 vim_free(isn->isn_arg.ufunc.cuf_name);
2083 break;
2084
2085 case ISN_FUNCREF:
2086 {
2087 if (isn->isn_arg.funcref.fr_func_name == NULL)
2088 {
2089 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2090 + isn->isn_arg.funcref.fr_dfunc_idx;
2091 ufunc_T *ufunc = dfunc->df_ufunc;
2092
2093 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2094 func_ptr_unref(ufunc);
2095 }
2096 else
2097 {
2098 char_u *name = isn->isn_arg.funcref.fr_func_name;
2099
2100 if (name != NULL)
2101 func_unref(name);
2102 vim_free(isn->isn_arg.funcref.fr_func_name);
2103 }
2104 }
2105 break;
2106
2107 case ISN_DCALL:
2108 {
2109 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2110 + isn->isn_arg.dfunc.cdf_idx;
2111
2112 if (dfunc->df_ufunc != NULL
2113 && func_name_refcount(dfunc->df_ufunc->uf_name))
2114 func_ptr_unref(dfunc->df_ufunc);
2115 }
2116 break;
2117
2118 case ISN_NEWFUNC:
2119 {
2120 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002121 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002122
2123 if (ufunc != NULL)
2124 {
2125 unlink_def_function(ufunc);
2126 func_ptr_unref(ufunc);
2127 }
2128
2129 vim_free(lambda);
2130 vim_free(isn->isn_arg.newfunc.nf_global);
2131 }
2132 break;
2133
2134 case ISN_CHECKTYPE:
2135 case ISN_SETTYPE:
2136 free_type(isn->isn_arg.type.ct_type);
2137 break;
2138
2139 case ISN_CMDMOD:
2140 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2141 ->cmod_filter_regmatch.regprog);
2142 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2143 break;
2144
2145 case ISN_LOADSCRIPT:
2146 case ISN_STORESCRIPT:
2147 vim_free(isn->isn_arg.script.scriptref);
2148 break;
2149
2150 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002151 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002152 break;
2153
2154 case ISN_CEXPR_CORE:
2155 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2156 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2157 break;
2158
2159 case ISN_2BOOL:
2160 case ISN_2STRING:
2161 case ISN_2STRING_ANY:
2162 case ISN_ADDBLOB:
2163 case ISN_ADDLIST:
2164 case ISN_ANYINDEX:
2165 case ISN_ANYSLICE:
2166 case ISN_BCALL:
2167 case ISN_BLOBAPPEND:
2168 case ISN_BLOBINDEX:
2169 case ISN_BLOBSLICE:
2170 case ISN_CATCH:
2171 case ISN_CEXPR_AUCMD:
2172 case ISN_CHECKLEN:
2173 case ISN_CHECKNR:
2174 case ISN_CLEARDICT:
2175 case ISN_CMDMOD_REV:
2176 case ISN_COMPAREANY:
2177 case ISN_COMPAREBLOB:
2178 case ISN_COMPAREBOOL:
2179 case ISN_COMPAREDICT:
2180 case ISN_COMPAREFLOAT:
2181 case ISN_COMPAREFUNC:
2182 case ISN_COMPARELIST:
2183 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002184 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002185 case ISN_COMPARESPECIAL:
2186 case ISN_COMPARESTRING:
2187 case ISN_CONCAT:
2188 case ISN_COND2BOOL:
2189 case ISN_DEBUG:
2190 case ISN_DROP:
2191 case ISN_ECHO:
2192 case ISN_ECHOCONSOLE:
2193 case ISN_ECHOERR:
2194 case ISN_ECHOMSG:
2195 case ISN_ENDTRY:
2196 case ISN_EXECCONCAT:
2197 case ISN_EXECUTE:
2198 case ISN_FINALLY:
2199 case ISN_FINISH:
2200 case ISN_FOR:
2201 case ISN_GETITEM:
2202 case ISN_JUMP:
2203 case ISN_JUMP_IF_ARG_SET:
2204 case ISN_LISTAPPEND:
2205 case ISN_LISTINDEX:
2206 case ISN_LISTSLICE:
2207 case ISN_LOAD:
2208 case ISN_LOADBDICT:
2209 case ISN_LOADGDICT:
2210 case ISN_LOADOUTER:
2211 case ISN_LOADREG:
2212 case ISN_LOADTDICT:
2213 case ISN_LOADV:
2214 case ISN_LOADWDICT:
2215 case ISN_LOCKCONST:
2216 case ISN_MEMBER:
2217 case ISN_NEGATENR:
2218 case ISN_NEWDICT:
2219 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002220 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002221 case ISN_OPANY:
2222 case ISN_OPFLOAT:
2223 case ISN_OPNR:
2224 case ISN_PCALL:
2225 case ISN_PCALL_END:
2226 case ISN_PROF_END:
2227 case ISN_PROF_START:
2228 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002229 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002230 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002231 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002232 case ISN_PUSHNR:
2233 case ISN_PUSHSPEC:
2234 case ISN_PUT:
2235 case ISN_REDIREND:
2236 case ISN_REDIRSTART:
2237 case ISN_RETURN:
2238 case ISN_RETURN_VOID:
2239 case ISN_SHUFFLE:
2240 case ISN_SLICE:
2241 case ISN_STORE:
2242 case ISN_STOREINDEX:
2243 case ISN_STORENR:
2244 case ISN_STOREOUTER:
2245 case ISN_STORERANGE:
2246 case ISN_STOREREG:
2247 case ISN_STOREV:
2248 case ISN_STRINDEX:
2249 case ISN_STRSLICE:
2250 case ISN_THROW:
2251 case ISN_TRYCONT:
2252 case ISN_UNLETINDEX:
2253 case ISN_UNLETRANGE:
2254 case ISN_UNPACK:
2255 case ISN_USEDICT:
2256 // nothing allocated
2257 break;
2258 }
2259}
2260
2261 void
2262clear_instr_ga(garray_T *gap)
2263{
2264 int idx;
2265
2266 for (idx = 0; idx < gap->ga_len; ++idx)
2267 delete_instr(((isn_T *)gap->ga_data) + idx);
2268 ga_clear(gap);
2269}
2270
2271
2272#endif // defined(FEAT_EVAL)