blob: 7f23884f3cf9dfdbea1c448e285c107780baf1a3 [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.
1175 */
1176 int
1177generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1178{
1179 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001180 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001181
1182 RETURN_OK_IF_SKIP(cctx);
1183 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1184 return FAIL;
1185 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1186 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1187 else
1188 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1189 cctx->ctx_has_closure = 1;
1190
1191 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001192 // the nested context, including this one. But not a function defined at
1193 // the script level.
1194 if ((ufunc->uf_flags & FC_CLOSURE)
1195 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001196 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1197
Bram Moolenaar078a4612022-01-04 15:17:03 +00001198 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1199 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001200}
1201
1202/*
1203 * Generate an ISN_NEWFUNC instruction.
1204 * "lambda_name" and "func_name" must be in allocated memory and will be
1205 * consumed.
1206 */
1207 int
1208generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1209{
1210 isn_T *isn;
1211
1212 if (cctx->ctx_skip == SKIP_YES)
1213 {
1214 vim_free(lambda_name);
1215 vim_free(func_name);
1216 return OK;
1217 }
1218 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1219 {
1220 vim_free(lambda_name);
1221 vim_free(func_name);
1222 return FAIL;
1223 }
1224 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1225 isn->isn_arg.newfunc.nf_global = func_name;
1226
1227 return OK;
1228}
1229
1230/*
1231 * Generate an ISN_DEF instruction: list functions
1232 */
1233 int
1234generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1235{
1236 isn_T *isn;
1237
1238 RETURN_OK_IF_SKIP(cctx);
1239 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1240 return FAIL;
1241 if (len > 0)
1242 {
1243 isn->isn_arg.string = vim_strnsave(name, len);
1244 if (isn->isn_arg.string == NULL)
1245 return FAIL;
1246 }
1247 return OK;
1248}
1249
1250/*
1251 * Generate an ISN_JUMP instruction.
1252 */
1253 int
1254generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1255{
1256 isn_T *isn;
1257 garray_T *stack = &cctx->ctx_type_stack;
1258
1259 RETURN_OK_IF_SKIP(cctx);
1260 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1261 return FAIL;
1262 isn->isn_arg.jump.jump_when = when;
1263 isn->isn_arg.jump.jump_where = where;
1264
1265 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1266 --stack->ga_len;
1267
1268 return OK;
1269}
1270
1271/*
1272 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1273 */
1274 int
1275generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1276{
1277 isn_T *isn;
1278
1279 RETURN_OK_IF_SKIP(cctx);
1280 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1281 return FAIL;
1282 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1283 // jump_where is set later
1284 return OK;
1285}
1286
1287 int
1288generate_FOR(cctx_T *cctx, int loop_idx)
1289{
1290 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001291
1292 RETURN_OK_IF_SKIP(cctx);
1293 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1294 return FAIL;
1295 isn->isn_arg.forloop.for_idx = loop_idx;
1296
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001297 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001298 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299}
1300/*
1301 * Generate an ISN_TRYCONT instruction.
1302 */
1303 int
1304generate_TRYCONT(cctx_T *cctx, int levels, int where)
1305{
1306 isn_T *isn;
1307
1308 RETURN_OK_IF_SKIP(cctx);
1309 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1310 return FAIL;
1311 isn->isn_arg.trycont.tct_levels = levels;
1312 isn->isn_arg.trycont.tct_where = where;
1313
1314 return OK;
1315}
1316
1317
1318/*
1319 * Generate an ISN_BCALL instruction.
1320 * "method_call" is TRUE for "value->method()"
1321 * Return FAIL if the number of arguments is wrong.
1322 */
1323 int
1324generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1325{
1326 isn_T *isn;
1327 garray_T *stack = &cctx->ctx_type_stack;
1328 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001329 type2_T *typep;
1330 type2_T *argtypes = NULL;
1331 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1332 type2_T *maptype = NULL;
1333 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001334 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001335
1336 RETURN_OK_IF_SKIP(cctx);
1337 argoff = check_internal_func(func_idx, argcount);
1338 if (argoff < 0)
1339 return FAIL;
1340
1341 if (method_call && argoff > 1)
1342 {
1343 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1344 return FAIL;
1345 isn->isn_arg.shuffle.shfl_item = argcount;
1346 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1347 }
1348
1349 if (argcount > 0)
1350 {
1351 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001352 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001353 if (method_call && argoff > 1)
1354 {
1355 int i;
1356
1357 for (i = 0; i < argcount; ++i)
1358 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001359 ? typep[i + 1]
1360 : (i == argoff - 1) ? typep[0] : typep[i];
1361 argtypes = shuffled_argtypes;
1362 }
1363 else
1364 {
1365 int i;
1366
1367 for (i = 0; i < argcount; ++i)
1368 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001369 argtypes = shuffled_argtypes;
1370 }
1371 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1372 cctx) == FAIL)
1373 return FAIL;
1374 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001375 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001376 }
1377
1378 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1379 return FAIL;
1380 isn->isn_arg.bfunc.cbf_idx = func_idx;
1381 isn->isn_arg.bfunc.cbf_argcount = argcount;
1382
1383 // Drop the argument types and push the return type.
1384 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001385 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1386 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001387 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001388
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001389 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1390 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001391 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001392 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001393
1394 return OK;
1395}
1396
1397/*
1398 * Generate an ISN_LISTAPPEND instruction. Works like add().
1399 * Argument count is already checked.
1400 */
1401 int
1402generate_LISTAPPEND(cctx_T *cctx)
1403{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001404 type_T *list_type;
1405 type_T *item_type;
1406 type_T *expected;
1407
1408 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001409 // For checking the item type we use the declared type of the list and the
1410 // current type of the added item, adding a string to [1, 2] is OK.
1411 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001412 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001413 expected = list_type->tt_member;
1414 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1415 return FAIL;
1416
1417 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1418 return FAIL;
1419
Bram Moolenaar078a4612022-01-04 15:17:03 +00001420 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001421 return OK;
1422}
1423
1424/*
1425 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1426 * Argument count is already checked.
1427 */
1428 int
1429generate_BLOBAPPEND(cctx_T *cctx)
1430{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001431 type_T *item_type;
1432
1433 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001434 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001435 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1436 return FAIL;
1437
1438 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1439 return FAIL;
1440
Bram Moolenaar078a4612022-01-04 15:17:03 +00001441 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001442 return OK;
1443}
1444
1445/*
1446 * Generate an ISN_DCALL or ISN_UCALL instruction.
1447 * Return FAIL if the number of arguments is wrong.
1448 */
1449 int
1450generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1451{
1452 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001453 int regular_args = ufunc->uf_args.ga_len;
1454 int argcount = pushed_argcount;
1455
1456 RETURN_OK_IF_SKIP(cctx);
1457 if (argcount > regular_args && !has_varargs(ufunc))
1458 {
1459 semsg(_(e_too_many_arguments_for_function_str),
1460 printable_func_name(ufunc));
1461 return FAIL;
1462 }
1463 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1464 {
1465 semsg(_(e_not_enough_arguments_for_function_str),
1466 printable_func_name(ufunc));
1467 return FAIL;
1468 }
1469
1470 if (ufunc->uf_def_status != UF_NOT_COMPILED
1471 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1472 {
1473 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001474 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001475
1476 for (i = 0; i < argcount; ++i)
1477 {
1478 type_T *expected;
1479 type_T *actual;
1480
Bram Moolenaar078a4612022-01-04 15:17:03 +00001481 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001482 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483 && i >= regular_args - ufunc->uf_def_args.ga_len)
1484 {
1485 // assume v:none used for default argument value
1486 continue;
1487 }
1488 if (i < regular_args)
1489 {
1490 if (ufunc->uf_arg_types == NULL)
1491 continue;
1492 expected = ufunc->uf_arg_types[i];
1493 }
1494 else if (ufunc->uf_va_type == NULL
1495 || ufunc->uf_va_type == &t_list_any)
1496 // possibly a lambda or "...: any"
1497 expected = &t_any;
1498 else
1499 expected = ufunc->uf_va_type->tt_member;
1500 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1501 TRUE, FALSE) == FAIL)
1502 {
1503 arg_type_mismatch(expected, actual, i + 1);
1504 return FAIL;
1505 }
1506 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001507 compile_type = get_compile_type(ufunc);
1508 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001509 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001510 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001511 return FAIL;
1512 }
1513 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1514 {
1515 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1516 ufunc->uf_name);
1517 return FAIL;
1518 }
1519
1520 if ((isn = generate_instr(cctx,
1521 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1522 : ISN_UCALL)) == NULL)
1523 return FAIL;
1524 if (isn->isn_type == ISN_DCALL)
1525 {
1526 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1527 isn->isn_arg.dfunc.cdf_argcount = argcount;
1528 }
1529 else
1530 {
1531 // A user function may be deleted and redefined later, can't use the
1532 // ufunc pointer, need to look it up again at runtime.
1533 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1534 isn->isn_arg.ufunc.cuf_argcount = argcount;
1535 }
1536
Bram Moolenaar078a4612022-01-04 15:17:03 +00001537 // drop the argument types
1538 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001539
Bram Moolenaar078a4612022-01-04 15:17:03 +00001540 // add return type
1541 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001542}
1543
1544/*
1545 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1546 */
1547 int
1548generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1549{
1550 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001551
1552 RETURN_OK_IF_SKIP(cctx);
1553 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1554 return FAIL;
1555 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1556 isn->isn_arg.ufunc.cuf_argcount = argcount;
1557
Bram Moolenaar078a4612022-01-04 15:17:03 +00001558 // drop the argument types
1559 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001560
Bram Moolenaar078a4612022-01-04 15:17:03 +00001561 // add return value
1562 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001563}
1564
1565/*
1566 * Generate an ISN_PCALL instruction.
1567 * "type" is the type of the FuncRef.
1568 */
1569 int
1570generate_PCALL(
1571 cctx_T *cctx,
1572 int argcount,
1573 char_u *name,
1574 type_T *type,
1575 int at_top)
1576{
1577 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578 type_T *ret_type;
1579
1580 RETURN_OK_IF_SKIP(cctx);
1581
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001582 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001583 ret_type = &t_any;
1584 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1585 {
1586 if (type->tt_argcount != -1)
1587 {
1588 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1589
1590 if (argcount < type->tt_min_argcount - varargs)
1591 {
1592 semsg(_(e_not_enough_arguments_for_function_str), name);
1593 return FAIL;
1594 }
1595 if (!varargs && argcount > type->tt_argcount)
1596 {
1597 semsg(_(e_too_many_arguments_for_function_str), name);
1598 return FAIL;
1599 }
1600 if (type->tt_args != NULL)
1601 {
1602 int i;
1603
1604 for (i = 0; i < argcount; ++i)
1605 {
1606 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001607 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001608 type_T *expected;
1609
1610 if (varargs && i >= type->tt_argcount - 1)
1611 expected = type->tt_args[
1612 type->tt_argcount - 1]->tt_member;
1613 else if (i >= type->tt_min_argcount
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001614 && actual->tt_type == VAR_SPECIAL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001615 expected = &t_any;
1616 else
1617 expected = type->tt_args[i];
1618 if (need_type(actual, expected, offset, i + 1,
1619 cctx, TRUE, FALSE) == FAIL)
1620 {
1621 arg_type_mismatch(expected, actual, i + 1);
1622 return FAIL;
1623 }
1624 }
1625 }
1626 }
1627 ret_type = type->tt_member;
1628 if (ret_type == &t_unknown)
1629 // return type not known yet, use a runtime check
1630 ret_type = &t_any;
1631 }
1632 else
1633 {
1634 semsg(_(e_not_callable_type_str), name);
1635 return FAIL;
1636 }
1637
1638 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1639 return FAIL;
1640 isn->isn_arg.pfunc.cpf_top = at_top;
1641 isn->isn_arg.pfunc.cpf_argcount = argcount;
1642
Bram Moolenaar078a4612022-01-04 15:17:03 +00001643 // drop the arguments and the funcref/partial
1644 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001645
Bram Moolenaar078a4612022-01-04 15:17:03 +00001646 // push the return value
1647 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001648
1649 // If partial is above the arguments it must be cleared and replaced with
1650 // the return value.
1651 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1652 return FAIL;
1653
1654 return OK;
1655}
1656
1657/*
1658 * Generate an ISN_STRINGMEMBER instruction.
1659 */
1660 int
1661generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1662{
1663 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001664 type_T *type;
1665
1666 RETURN_OK_IF_SKIP(cctx);
1667 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1668 return FAIL;
1669 isn->isn_arg.string = vim_strnsave(name, len);
1670
1671 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001672 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001673 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001674 {
1675 char *tofree;
1676
1677 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1678 name, type_name(type, &tofree));
1679 vim_free(tofree);
1680 return FAIL;
1681 }
1682 // change dict type to dict member type
1683 if (type->tt_type == VAR_DICT)
1684 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001685 type_T *ntype = type->tt_member == &t_unknown
1686 ? &t_any : type->tt_member;
1687 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688 }
1689
1690 return OK;
1691}
1692
1693/*
1694 * Generate an ISN_ECHO instruction.
1695 */
1696 int
1697generate_ECHO(cctx_T *cctx, int with_white, int count)
1698{
1699 isn_T *isn;
1700
1701 RETURN_OK_IF_SKIP(cctx);
1702 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1703 return FAIL;
1704 isn->isn_arg.echo.echo_with_white = with_white;
1705 isn->isn_arg.echo.echo_count = count;
1706
1707 return OK;
1708}
1709
1710/*
1711 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1712 */
1713 int
1714generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1715{
1716 isn_T *isn;
1717
1718 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1719 return FAIL;
1720 isn->isn_arg.number = count;
1721
1722 return OK;
1723}
1724
1725/*
1726 * Generate an ISN_PUT instruction.
1727 */
1728 int
1729generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1730{
1731 isn_T *isn;
1732
1733 RETURN_OK_IF_SKIP(cctx);
1734 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1735 return FAIL;
1736 isn->isn_arg.put.put_regname = regname;
1737 isn->isn_arg.put.put_lnum = lnum;
1738 return OK;
1739}
1740
1741/*
1742 * Generate an EXEC instruction that takes a string argument.
1743 * A copy is made of "line".
1744 */
1745 int
1746generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1747{
1748 isn_T *isn;
1749
1750 RETURN_OK_IF_SKIP(cctx);
1751 if ((isn = generate_instr(cctx, isntype)) == NULL)
1752 return FAIL;
1753 isn->isn_arg.string = vim_strsave(line);
1754 return OK;
1755}
1756
1757/*
1758 * Generate an EXEC instruction that takes a string argument.
1759 * "str" must be allocated, it is consumed.
1760 */
1761 int
1762generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1763{
1764 isn_T *isn;
1765
1766 if (cctx->ctx_skip == SKIP_YES)
1767 {
1768 vim_free(str);
1769 return OK;
1770 }
1771 if ((isn = generate_instr(cctx, isntype)) == NULL)
1772 {
1773 vim_free(str);
1774 return FAIL;
1775 }
1776 isn->isn_arg.string = str;
1777 return OK;
1778}
1779
1780 int
1781generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1782{
1783 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001784
1785 RETURN_OK_IF_SKIP(cctx);
1786 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1787 return FAIL;
1788 isn->isn_arg.string = vim_strsave(line);
1789
Bram Moolenaar078a4612022-01-04 15:17:03 +00001790 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791}
1792
1793 int
1794generate_EXECCONCAT(cctx_T *cctx, int count)
1795{
1796 isn_T *isn;
1797
1798 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1799 return FAIL;
1800 isn->isn_arg.number = count;
1801 return OK;
1802}
1803
1804/*
1805 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1806 */
1807 int
1808generate_RANGE(cctx_T *cctx, char_u *range)
1809{
1810 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001811
1812 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1813 return FAIL;
1814 isn->isn_arg.string = range;
1815
Bram Moolenaar078a4612022-01-04 15:17:03 +00001816 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001817}
1818
1819 int
1820generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1821{
1822 isn_T *isn;
1823
1824 RETURN_OK_IF_SKIP(cctx);
1825 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1826 return FAIL;
1827 isn->isn_arg.unpack.unp_count = var_count;
1828 isn->isn_arg.unpack.unp_semicolon = semicolon;
1829 return OK;
1830}
1831
1832/*
1833 * Generate an instruction for any command modifiers.
1834 */
1835 int
1836generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1837{
1838 isn_T *isn;
1839
1840 if (has_cmdmod(cmod, FALSE))
1841 {
1842 cctx->ctx_has_cmdmod = TRUE;
1843
1844 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1845 return FAIL;
1846 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1847 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1848 return FAIL;
1849 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1850 // filter program now belongs to the instruction
1851 cmod->cmod_filter_regmatch.regprog = NULL;
1852 }
1853
1854 return OK;
1855}
1856
1857 int
1858generate_undo_cmdmods(cctx_T *cctx)
1859{
1860 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1861 return FAIL;
1862 cctx->ctx_has_cmdmod = FALSE;
1863 return OK;
1864}
1865
1866/*
1867 * Generate a STORE instruction for "dest", not being "dest_local".
1868 * Return FAIL when out of memory.
1869 */
1870 int
1871generate_store_var(
1872 cctx_T *cctx,
1873 assign_dest_T dest,
1874 int opt_flags,
1875 int vimvaridx,
1876 int scriptvar_idx,
1877 int scriptvar_sid,
1878 type_T *type,
1879 char_u *name)
1880{
1881 switch (dest)
1882 {
1883 case dest_option:
1884 return generate_STOREOPT(cctx, ISN_STOREOPT,
1885 skip_option_env_lead(name), opt_flags);
1886 case dest_func_option:
1887 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1888 skip_option_env_lead(name), opt_flags);
1889 case dest_global:
1890 // include g: with the name, easier to execute that way
1891 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1892 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1893 case dest_buffer:
1894 // include b: with the name, easier to execute that way
1895 return generate_STORE(cctx, ISN_STOREB, 0, name);
1896 case dest_window:
1897 // include w: with the name, easier to execute that way
1898 return generate_STORE(cctx, ISN_STOREW, 0, name);
1899 case dest_tab:
1900 // include t: with the name, easier to execute that way
1901 return generate_STORE(cctx, ISN_STORET, 0, name);
1902 case dest_env:
1903 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1904 case dest_reg:
1905 return generate_STORE(cctx, ISN_STOREREG,
1906 name[1] == '@' ? '"' : name[1], NULL);
1907 case dest_vimvar:
1908 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1909 case dest_script:
1910 if (scriptvar_idx < 0)
1911 // "s:" may be included in the name.
1912 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1913 scriptvar_sid, type);
1914 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1915 scriptvar_sid, scriptvar_idx, type);
1916 case dest_local:
1917 case dest_expr:
1918 // cannot happen
1919 break;
1920 }
1921 return FAIL;
1922}
1923
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001924/*
1925 * Return TRUE when inside a "for" or "while" loop.
1926 */
1927 int
1928inside_loop_scope(cctx_T *cctx)
1929{
1930 scope_T *scope = cctx->ctx_scope;
1931
1932 for (;;)
1933 {
1934 if (scope == NULL)
1935 break;
1936 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1937 return TRUE;
1938 scope = scope->se_outer;
1939 }
1940 return FALSE;
1941}
1942
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001943 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001944generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001945{
1946 if (lhs->lhs_dest != dest_local)
1947 return generate_store_var(cctx, lhs->lhs_dest,
1948 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1949 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1950 lhs->lhs_type, lhs->lhs_name);
1951
1952 if (lhs->lhs_lvar != NULL)
1953 {
1954 garray_T *instr = &cctx->ctx_instr;
1955 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1956
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001957 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1958 // ISN_STORENR.
1959 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001960 if (lhs->lhs_lvar->lv_from_outer == 0
1961 && instr->ga_len == instr_count + 1
1962 && isn->isn_type == ISN_PUSHNR)
1963 {
1964 varnumber_T val = isn->isn_arg.number;
1965 garray_T *stack = &cctx->ctx_type_stack;
1966
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001967 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001968 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001969 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001970 --instr->ga_len;
1971 }
1972 else
1973 {
1974 isn->isn_type = ISN_STORENR;
1975 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1976 isn->isn_arg.storenr.stnr_val = val;
1977 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001978 if (stack->ga_len > 0)
1979 --stack->ga_len;
1980 }
1981 else if (lhs->lhs_lvar->lv_from_outer > 0)
1982 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1983 lhs->lhs_lvar->lv_from_outer);
1984 else
1985 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
1986 }
1987 return OK;
1988}
1989
1990#if defined(FEAT_PROFILE) || defined(PROTO)
1991 void
1992may_generate_prof_end(cctx_T *cctx, int prof_lnum)
1993{
1994 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
1995 generate_instr(cctx, ISN_PROF_END);
1996}
1997#endif
1998
1999
2000/*
2001 * Delete an instruction, free what it contains.
2002 */
2003 void
2004delete_instr(isn_T *isn)
2005{
2006 switch (isn->isn_type)
2007 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002008 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002009 case ISN_DEF:
2010 case ISN_EXEC:
2011 case ISN_EXECRANGE:
2012 case ISN_EXEC_SPLIT:
2013 case ISN_LEGACY_EVAL:
2014 case ISN_LOADAUTO:
2015 case ISN_LOADB:
2016 case ISN_LOADENV:
2017 case ISN_LOADG:
2018 case ISN_LOADOPT:
2019 case ISN_LOADT:
2020 case ISN_LOADW:
2021 case ISN_LOCKUNLOCK:
2022 case ISN_PUSHEXC:
2023 case ISN_PUSHFUNC:
2024 case ISN_PUSHS:
2025 case ISN_RANGE:
2026 case ISN_STOREAUTO:
2027 case ISN_STOREB:
2028 case ISN_STOREENV:
2029 case ISN_STOREG:
2030 case ISN_STORET:
2031 case ISN_STOREW:
2032 case ISN_STRINGMEMBER:
2033 vim_free(isn->isn_arg.string);
2034 break;
2035
2036 case ISN_SUBSTITUTE:
2037 {
2038 int idx;
2039 isn_T *list = isn->isn_arg.subs.subs_instr;
2040
2041 vim_free(isn->isn_arg.subs.subs_cmd);
2042 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2043 delete_instr(list + idx);
2044 vim_free(list);
2045 }
2046 break;
2047
2048 case ISN_INSTR:
2049 {
2050 int idx;
2051 isn_T *list = isn->isn_arg.instr;
2052
2053 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2054 delete_instr(list + idx);
2055 vim_free(list);
2056 }
2057 break;
2058
2059 case ISN_LOADS:
2060 case ISN_STORES:
2061 vim_free(isn->isn_arg.loadstore.ls_name);
2062 break;
2063
2064 case ISN_UNLET:
2065 case ISN_UNLETENV:
2066 vim_free(isn->isn_arg.unlet.ul_name);
2067 break;
2068
2069 case ISN_STOREOPT:
2070 case ISN_STOREFUNCOPT:
2071 vim_free(isn->isn_arg.storeopt.so_name);
2072 break;
2073
2074 case ISN_PUSHBLOB: // push blob isn_arg.blob
2075 blob_unref(isn->isn_arg.blob);
2076 break;
2077
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002078 case ISN_UCALL:
2079 vim_free(isn->isn_arg.ufunc.cuf_name);
2080 break;
2081
2082 case ISN_FUNCREF:
2083 {
2084 if (isn->isn_arg.funcref.fr_func_name == NULL)
2085 {
2086 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2087 + isn->isn_arg.funcref.fr_dfunc_idx;
2088 ufunc_T *ufunc = dfunc->df_ufunc;
2089
2090 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2091 func_ptr_unref(ufunc);
2092 }
2093 else
2094 {
2095 char_u *name = isn->isn_arg.funcref.fr_func_name;
2096
2097 if (name != NULL)
2098 func_unref(name);
2099 vim_free(isn->isn_arg.funcref.fr_func_name);
2100 }
2101 }
2102 break;
2103
2104 case ISN_DCALL:
2105 {
2106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2107 + isn->isn_arg.dfunc.cdf_idx;
2108
2109 if (dfunc->df_ufunc != NULL
2110 && func_name_refcount(dfunc->df_ufunc->uf_name))
2111 func_ptr_unref(dfunc->df_ufunc);
2112 }
2113 break;
2114
2115 case ISN_NEWFUNC:
2116 {
2117 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002118 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002119
2120 if (ufunc != NULL)
2121 {
2122 unlink_def_function(ufunc);
2123 func_ptr_unref(ufunc);
2124 }
2125
2126 vim_free(lambda);
2127 vim_free(isn->isn_arg.newfunc.nf_global);
2128 }
2129 break;
2130
2131 case ISN_CHECKTYPE:
2132 case ISN_SETTYPE:
2133 free_type(isn->isn_arg.type.ct_type);
2134 break;
2135
2136 case ISN_CMDMOD:
2137 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2138 ->cmod_filter_regmatch.regprog);
2139 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2140 break;
2141
2142 case ISN_LOADSCRIPT:
2143 case ISN_STORESCRIPT:
2144 vim_free(isn->isn_arg.script.scriptref);
2145 break;
2146
2147 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002148 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002149 break;
2150
2151 case ISN_CEXPR_CORE:
2152 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2153 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2154 break;
2155
2156 case ISN_2BOOL:
2157 case ISN_2STRING:
2158 case ISN_2STRING_ANY:
2159 case ISN_ADDBLOB:
2160 case ISN_ADDLIST:
2161 case ISN_ANYINDEX:
2162 case ISN_ANYSLICE:
2163 case ISN_BCALL:
2164 case ISN_BLOBAPPEND:
2165 case ISN_BLOBINDEX:
2166 case ISN_BLOBSLICE:
2167 case ISN_CATCH:
2168 case ISN_CEXPR_AUCMD:
2169 case ISN_CHECKLEN:
2170 case ISN_CHECKNR:
2171 case ISN_CLEARDICT:
2172 case ISN_CMDMOD_REV:
2173 case ISN_COMPAREANY:
2174 case ISN_COMPAREBLOB:
2175 case ISN_COMPAREBOOL:
2176 case ISN_COMPAREDICT:
2177 case ISN_COMPAREFLOAT:
2178 case ISN_COMPAREFUNC:
2179 case ISN_COMPARELIST:
2180 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002181 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002182 case ISN_COMPARESPECIAL:
2183 case ISN_COMPARESTRING:
2184 case ISN_CONCAT:
2185 case ISN_COND2BOOL:
2186 case ISN_DEBUG:
2187 case ISN_DROP:
2188 case ISN_ECHO:
2189 case ISN_ECHOCONSOLE:
2190 case ISN_ECHOERR:
2191 case ISN_ECHOMSG:
2192 case ISN_ENDTRY:
2193 case ISN_EXECCONCAT:
2194 case ISN_EXECUTE:
2195 case ISN_FINALLY:
2196 case ISN_FINISH:
2197 case ISN_FOR:
2198 case ISN_GETITEM:
2199 case ISN_JUMP:
2200 case ISN_JUMP_IF_ARG_SET:
2201 case ISN_LISTAPPEND:
2202 case ISN_LISTINDEX:
2203 case ISN_LISTSLICE:
2204 case ISN_LOAD:
2205 case ISN_LOADBDICT:
2206 case ISN_LOADGDICT:
2207 case ISN_LOADOUTER:
2208 case ISN_LOADREG:
2209 case ISN_LOADTDICT:
2210 case ISN_LOADV:
2211 case ISN_LOADWDICT:
2212 case ISN_LOCKCONST:
2213 case ISN_MEMBER:
2214 case ISN_NEGATENR:
2215 case ISN_NEWDICT:
2216 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002217 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002218 case ISN_OPANY:
2219 case ISN_OPFLOAT:
2220 case ISN_OPNR:
2221 case ISN_PCALL:
2222 case ISN_PCALL_END:
2223 case ISN_PROF_END:
2224 case ISN_PROF_START:
2225 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002226 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002227 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002228 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229 case ISN_PUSHNR:
2230 case ISN_PUSHSPEC:
2231 case ISN_PUT:
2232 case ISN_REDIREND:
2233 case ISN_REDIRSTART:
2234 case ISN_RETURN:
2235 case ISN_RETURN_VOID:
2236 case ISN_SHUFFLE:
2237 case ISN_SLICE:
2238 case ISN_STORE:
2239 case ISN_STOREINDEX:
2240 case ISN_STORENR:
2241 case ISN_STOREOUTER:
2242 case ISN_STORERANGE:
2243 case ISN_STOREREG:
2244 case ISN_STOREV:
2245 case ISN_STRINDEX:
2246 case ISN_STRSLICE:
2247 case ISN_THROW:
2248 case ISN_TRYCONT:
2249 case ISN_UNLETINDEX:
2250 case ISN_UNLETRANGE:
2251 case ISN_UNPACK:
2252 case ISN_USEDICT:
2253 // nothing allocated
2254 break;
2255 }
2256}
2257
2258 void
2259clear_instr_ga(garray_T *gap)
2260{
2261 int idx;
2262
2263 for (idx = 0; idx < gap->ga_len; ++idx)
2264 delete_instr(((isn_T *)gap->ga_data) + idx);
2265 ga_clear(gap);
2266}
2267
2268
2269#endif // defined(FEAT_EVAL)