blob: 9b176dfb5d6dad4b90b2ee55ea4a2e79c407058a [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 }
400 switch (vartype1 == VAR_SPECIAL ? vartype2 : vartype1)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000401 {
402 case VAR_BLOB: break;
403 case VAR_CHANNEL: break;
404 case VAR_DICT: break;
405 case VAR_FUNC: break;
406 case VAR_JOB: break;
407 case VAR_LIST: break;
408 case VAR_PARTIAL: break;
409 case VAR_STRING: break;
410 default: semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000411 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaar7a222242022-03-01 19:23:24 +0000412 return ISN_DROP;
413 }
414 isntype = ISN_COMPARENULL;
415 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000416
417 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
418 && (isntype == ISN_COMPAREBOOL
419 || isntype == ISN_COMPARESPECIAL
420 || isntype == ISN_COMPARENR
421 || isntype == ISN_COMPAREFLOAT))
422 {
423 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000424 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000425 return ISN_DROP;
426 }
427 if (isntype == ISN_DROP
428 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000429 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
430 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000431 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000432 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000433 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
434 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000435 {
436 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000437 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000438 return ISN_DROP;
439 }
440 return isntype;
441}
442
443 int
444check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
445{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000446 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000447 return FAIL;
448 return OK;
449}
450
451/*
452 * Generate an ISN_COMPARE* instruction with a boolean result.
453 */
454 int
455generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
456{
457 isntype_T isntype;
458 isn_T *isn;
459 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000460
461 RETURN_OK_IF_SKIP(cctx);
462
463 // Get the known type of the two items on the stack. If they are matching
464 // use a type-specific instruction. Otherwise fall back to runtime type
465 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000466 isntype = get_compare_isn(exprtype, NULL, NULL,
467 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000468 if (isntype == ISN_DROP)
469 return FAIL;
470
471 if ((isn = generate_instr(cctx, isntype)) == NULL)
472 return FAIL;
473 isn->isn_arg.op.op_type = exprtype;
474 isn->isn_arg.op.op_ic = ic;
475
476 // takes two arguments, puts one bool back
477 if (stack->ga_len >= 2)
478 {
479 --stack->ga_len;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000480 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000481 }
482
483 return OK;
484}
485
486/*
487 * Generate an ISN_2BOOL instruction.
488 * "offset" is the offset in the type stack.
489 */
490 int
491generate_2BOOL(cctx_T *cctx, int invert, int offset)
492{
493 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000494
495 RETURN_OK_IF_SKIP(cctx);
496 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
497 return FAIL;
498 isn->isn_arg.tobool.invert = invert;
499 isn->isn_arg.tobool.offset = offset;
500
501 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000502 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000503
504 return OK;
505}
506
507/*
508 * Generate an ISN_COND2BOOL instruction.
509 */
510 int
511generate_COND2BOOL(cctx_T *cctx)
512{
513 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000514
515 RETURN_OK_IF_SKIP(cctx);
516 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
517 return FAIL;
518
519 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000520 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000521
522 return OK;
523}
524
525 int
526generate_TYPECHECK(
527 cctx_T *cctx,
528 type_T *expected,
529 int offset,
530 int argidx)
531{
532 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000533
534 RETURN_OK_IF_SKIP(cctx);
535 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
536 return FAIL;
537 isn->isn_arg.type.ct_type = alloc_type(expected);
538 isn->isn_arg.type.ct_off = (int8_T)offset;
539 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
540
541 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000542 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000543
544 return OK;
545}
546
547 int
548generate_SETTYPE(
549 cctx_T *cctx,
550 type_T *expected)
551{
552 isn_T *isn;
553
554 RETURN_OK_IF_SKIP(cctx);
555 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
556 return FAIL;
557 isn->isn_arg.type.ct_type = alloc_type(expected);
558 return OK;
559}
560
561/*
562 * Generate a PUSH instruction for "tv".
563 * "tv" will be consumed or cleared.
564 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
565 */
566 int
567generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
568{
569 if (tv != NULL)
570 {
571 switch (tv->v_type)
572 {
573 case VAR_UNKNOWN:
574 break;
575 case VAR_BOOL:
576 generate_PUSHBOOL(cctx, tv->vval.v_number);
577 break;
578 case VAR_SPECIAL:
579 generate_PUSHSPEC(cctx, tv->vval.v_number);
580 break;
581 case VAR_NUMBER:
582 generate_PUSHNR(cctx, tv->vval.v_number);
583 break;
584#ifdef FEAT_FLOAT
585 case VAR_FLOAT:
586 generate_PUSHF(cctx, tv->vval.v_float);
587 break;
588#endif
589 case VAR_BLOB:
590 generate_PUSHBLOB(cctx, tv->vval.v_blob);
591 tv->vval.v_blob = NULL;
592 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000593 case VAR_LIST:
594 if (tv->vval.v_list != NULL)
595 iemsg("non-empty list constant not supported");
596 generate_NEWLIST(cctx, 0);
597 break;
598 case VAR_DICT:
599 if (tv->vval.v_dict != NULL)
600 iemsg("non-empty dict constant not supported");
601 generate_NEWDICT(cctx, 0);
602 break;
603#ifdef FEAT_JOB_CHANNEL
604 case VAR_JOB:
605 if (tv->vval.v_job != NULL)
606 iemsg("non-null job constant not supported");
607 generate_PUSHJOB(cctx, NULL);
608 break;
609 case VAR_CHANNEL:
610 if (tv->vval.v_channel != NULL)
611 iemsg("non-null channel constant not supported");
612 generate_PUSHCHANNEL(cctx, NULL);
613 break;
614#endif
615 case VAR_FUNC:
616 if (tv->vval.v_string != NULL)
617 iemsg("non-null function constant not supported");
618 generate_PUSHFUNC(cctx, NULL, &t_func_unknown);
619 break;
620 case VAR_PARTIAL:
621 if (tv->vval.v_partial != NULL)
622 iemsg("non-null partial constant not supported");
623 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
624 == NULL)
625 return FAIL;
626 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000627 case VAR_STRING:
628 generate_PUSHS(cctx, &tv->vval.v_string);
629 tv->vval.v_string = NULL;
630 break;
631 default:
632 iemsg("constant type not supported");
633 clear_tv(tv);
634 return FAIL;
635 }
636 tv->v_type = VAR_UNKNOWN;
637 }
638 return OK;
639}
640
641/*
642 * Generate an ISN_PUSHNR instruction.
643 */
644 int
645generate_PUSHNR(cctx_T *cctx, varnumber_T number)
646{
647 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000648
649 RETURN_OK_IF_SKIP(cctx);
650 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
651 return FAIL;
652 isn->isn_arg.number = number;
653
654 if (number == 0 || number == 1)
655 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000656 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000657 return OK;
658}
659
660/*
661 * Generate an ISN_PUSHBOOL instruction.
662 */
663 int
664generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
665{
666 isn_T *isn;
667
668 RETURN_OK_IF_SKIP(cctx);
669 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
670 return FAIL;
671 isn->isn_arg.number = number;
672
673 return OK;
674}
675
676/*
677 * Generate an ISN_PUSHSPEC instruction.
678 */
679 int
680generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
681{
682 isn_T *isn;
683
684 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000685 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
686 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000687 return FAIL;
688 isn->isn_arg.number = number;
689
690 return OK;
691}
692
693#if defined(FEAT_FLOAT) || defined(PROTO)
694/*
695 * Generate an ISN_PUSHF instruction.
696 */
697 int
698generate_PUSHF(cctx_T *cctx, float_T fnumber)
699{
700 isn_T *isn;
701
702 RETURN_OK_IF_SKIP(cctx);
703 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
704 return FAIL;
705 isn->isn_arg.fnumber = fnumber;
706
707 return OK;
708}
709#endif
710
711/*
712 * Generate an ISN_PUSHS instruction.
713 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
714 */
715 int
716generate_PUSHS(cctx_T *cctx, char_u **str)
717{
718 isn_T *isn;
719
720 if (cctx->ctx_skip == SKIP_YES)
721 {
722 if (str != NULL)
723 VIM_CLEAR(*str);
724 return OK;
725 }
726 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
727 {
728 if (str != NULL)
729 VIM_CLEAR(*str);
730 return FAIL;
731 }
732 isn->isn_arg.string = str == NULL ? NULL : *str;
733
734 return OK;
735}
736
737/*
738 * Generate an ISN_PUSHCHANNEL instruction.
739 * Consumes "channel".
740 */
741 int
742generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel)
743{
744 isn_T *isn;
745
746 RETURN_OK_IF_SKIP(cctx);
747 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
748 return FAIL;
749 isn->isn_arg.channel = channel;
750
751 return OK;
752}
753
754/*
755 * Generate an ISN_PUSHJOB instruction.
756 * Consumes "job".
757 */
758 int
759generate_PUSHJOB(cctx_T *cctx, job_T *job)
760{
761 isn_T *isn;
762
763 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000764 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_job)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000765 return FAIL;
766 isn->isn_arg.job = job;
767
768 return OK;
769}
770
771/*
772 * Generate an ISN_PUSHBLOB instruction.
773 * Consumes "blob".
774 */
775 int
776generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
777{
778 isn_T *isn;
779
780 RETURN_OK_IF_SKIP(cctx);
781 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
782 return FAIL;
783 isn->isn_arg.blob = blob;
784
785 return OK;
786}
787
788/*
789 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 */
791 int
792generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
793{
794 isn_T *isn;
795 char_u *funcname;
796
797 RETURN_OK_IF_SKIP(cctx);
798 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
799 return FAIL;
800 if (name == NULL)
801 funcname = NULL;
Bram Moolenaard041f422022-01-12 19:54:00 +0000802 else if (*name == K_SPECIAL // script-local
803 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000804 funcname = vim_strsave(name);
805 else
806 {
807 funcname = alloc(STRLEN(name) + 3);
808 if (funcname != NULL)
809 {
810 STRCPY(funcname, "g:");
811 STRCPY(funcname + 2, name);
812 }
813 }
814
815 isn->isn_arg.string = funcname;
816 return OK;
817}
818
819/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000820 * Generate an ISN_AUTOLOAD instruction.
821 */
822 int
823generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
824{
825 isn_T *isn;
826
827 RETURN_OK_IF_SKIP(cctx);
828 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
829 return FAIL;
830 isn->isn_arg.string = vim_strsave(name);
831 if (isn->isn_arg.string == NULL)
832 return FAIL;
833 return OK;
834}
835
836/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000837 * Generate an ISN_GETITEM instruction with "index".
838 * "with_op" is TRUE for "+=" and other operators, the stack has the current
839 * value below the list with values.
840 */
841 int
842generate_GETITEM(cctx_T *cctx, int index, int with_op)
843{
844 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000845 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846 type_T *item_type = &t_any;
847
848 RETURN_OK_IF_SKIP(cctx);
849
850 if (type->tt_type != VAR_LIST)
851 {
852 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000853 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000854 return FAIL;
855 }
856 item_type = type->tt_member;
857 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
858 return FAIL;
859 isn->isn_arg.getitem.gi_index = index;
860 isn->isn_arg.getitem.gi_with_op = with_op;
861
862 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000863 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864}
865
866/*
867 * Generate an ISN_SLICE instruction with "count".
868 */
869 int
870generate_SLICE(cctx_T *cctx, int count)
871{
872 isn_T *isn;
873
874 RETURN_OK_IF_SKIP(cctx);
875 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
876 return FAIL;
877 isn->isn_arg.number = count;
878 return OK;
879}
880
881/*
882 * Generate an ISN_CHECKLEN instruction with "min_len".
883 */
884 int
885generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
886{
887 isn_T *isn;
888
889 RETURN_OK_IF_SKIP(cctx);
890
891 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
892 return FAIL;
893 isn->isn_arg.checklen.cl_min_len = min_len;
894 isn->isn_arg.checklen.cl_more_OK = more_OK;
895
896 return OK;
897}
898
899/*
900 * Generate an ISN_STORE instruction.
901 */
902 int
903generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
904{
905 isn_T *isn;
906
907 RETURN_OK_IF_SKIP(cctx);
908 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
909 return FAIL;
910 if (name != NULL)
911 isn->isn_arg.string = vim_strsave(name);
912 else
913 isn->isn_arg.number = idx;
914
915 return OK;
916}
917
918/*
919 * Generate an ISN_STOREOUTER instruction.
920 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000921 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922generate_STOREOUTER(cctx_T *cctx, int idx, int level)
923{
924 isn_T *isn;
925
926 RETURN_OK_IF_SKIP(cctx);
927 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
928 return FAIL;
929 isn->isn_arg.outer.outer_idx = idx;
930 isn->isn_arg.outer.outer_depth = level;
931
932 return OK;
933}
934
935/*
936 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
937 */
938 int
939generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
940{
941 isn_T *isn;
942
943 RETURN_OK_IF_SKIP(cctx);
944 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
945 return FAIL;
946 isn->isn_arg.storenr.stnr_idx = idx;
947 isn->isn_arg.storenr.stnr_val = value;
948
949 return OK;
950}
951
952/*
953 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
954 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000955 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000956generate_STOREOPT(
957 cctx_T *cctx,
958 isntype_T isn_type,
959 char_u *name,
960 int opt_flags)
961{
962 isn_T *isn;
963
964 RETURN_OK_IF_SKIP(cctx);
965 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
966 return FAIL;
967 isn->isn_arg.storeopt.so_name = vim_strsave(name);
968 isn->isn_arg.storeopt.so_flags = opt_flags;
969
970 return OK;
971}
972
973/*
974 * Generate an ISN_LOAD or similar instruction.
975 */
976 int
977generate_LOAD(
978 cctx_T *cctx,
979 isntype_T isn_type,
980 int idx,
981 char_u *name,
982 type_T *type)
983{
984 isn_T *isn;
985
986 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000987 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000988 return FAIL;
989 if (name != NULL)
990 isn->isn_arg.string = vim_strsave(name);
991 else
992 isn->isn_arg.number = idx;
993
994 return OK;
995}
996
997/*
998 * Generate an ISN_LOADOUTER instruction
999 */
1000 int
1001generate_LOADOUTER(
1002 cctx_T *cctx,
1003 int idx,
1004 int nesting,
1005 type_T *type)
1006{
1007 isn_T *isn;
1008
1009 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001010 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001011 return FAIL;
1012 isn->isn_arg.outer.outer_idx = idx;
1013 isn->isn_arg.outer.outer_depth = nesting;
1014
1015 return OK;
1016}
1017
1018/*
1019 * Generate an ISN_LOADV instruction for v:var.
1020 */
1021 int
1022generate_LOADV(
1023 cctx_T *cctx,
1024 char_u *name,
1025 int error)
1026{
1027 int di_flags;
1028 int vidx = find_vim_var(name, &di_flags);
1029 type_T *type;
1030
1031 RETURN_OK_IF_SKIP(cctx);
1032 if (vidx < 0)
1033 {
1034 if (error)
1035 semsg(_(e_variable_not_found_str), name);
1036 return FAIL;
1037 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001038 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001039 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1040}
1041
1042/*
1043 * Generate an ISN_UNLET instruction.
1044 */
1045 int
1046generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1047{
1048 isn_T *isn;
1049
1050 RETURN_OK_IF_SKIP(cctx);
1051 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1052 return FAIL;
1053 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1054 isn->isn_arg.unlet.ul_forceit = forceit;
1055
1056 return OK;
1057}
1058
1059/*
1060 * Generate an ISN_LOCKCONST instruction.
1061 */
1062 int
1063generate_LOCKCONST(cctx_T *cctx)
1064{
1065 isn_T *isn;
1066
1067 RETURN_OK_IF_SKIP(cctx);
1068 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1069 return FAIL;
1070 return OK;
1071}
1072
1073/*
1074 * Generate an ISN_LOADS instruction.
1075 */
1076 int
1077generate_OLDSCRIPT(
1078 cctx_T *cctx,
1079 isntype_T isn_type,
1080 char_u *name,
1081 int sid,
1082 type_T *type)
1083{
1084 isn_T *isn;
1085
1086 RETURN_OK_IF_SKIP(cctx);
1087 if (isn_type == ISN_LOADS)
1088 isn = generate_instr_type(cctx, isn_type, type);
1089 else
1090 isn = generate_instr_drop(cctx, isn_type, 1);
1091 if (isn == NULL)
1092 return FAIL;
1093 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1094 isn->isn_arg.loadstore.ls_sid = sid;
1095
1096 return OK;
1097}
1098
1099/*
1100 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1101 */
1102 int
1103generate_VIM9SCRIPT(
1104 cctx_T *cctx,
1105 isntype_T isn_type,
1106 int sid,
1107 int idx,
1108 type_T *type)
1109{
1110 isn_T *isn;
1111 scriptref_T *sref;
1112 scriptitem_T *si = SCRIPT_ITEM(sid);
1113
1114 RETURN_OK_IF_SKIP(cctx);
1115 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001116 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001117 else
1118 isn = generate_instr_drop(cctx, isn_type, 1);
1119 if (isn == NULL)
1120 return FAIL;
1121
1122 // This requires three arguments, which doesn't fit in an instruction, thus
1123 // we need to allocate a struct for this.
1124 sref = ALLOC_ONE(scriptref_T);
1125 if (sref == NULL)
1126 return FAIL;
1127 isn->isn_arg.script.scriptref = sref;
1128 sref->sref_sid = sid;
1129 sref->sref_idx = idx;
1130 sref->sref_seq = si->sn_script_seq;
1131 sref->sref_type = type;
1132 return OK;
1133}
1134
1135/*
1136 * Generate an ISN_NEWLIST instruction.
1137 */
1138 int
1139generate_NEWLIST(cctx_T *cctx, int count)
1140{
1141 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001142 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001143 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001144 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001145
1146 RETURN_OK_IF_SKIP(cctx);
1147 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1148 return FAIL;
1149 isn->isn_arg.number = count;
1150
Bram Moolenaar078a4612022-01-04 15:17:03 +00001151 // Get the member type and the declared member type from all the items on
1152 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001153 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001154 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001155 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156
1157 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001158 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001159
1160 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001161 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001162}
1163
1164/*
1165 * Generate an ISN_NEWDICT instruction.
1166 */
1167 int
1168generate_NEWDICT(cctx_T *cctx, int count)
1169{
1170 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001171 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001172 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001173 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001174
1175 RETURN_OK_IF_SKIP(cctx);
1176 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1177 return FAIL;
1178 isn->isn_arg.number = count;
1179
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001180 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001181 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001182 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183
1184 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001185 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001186
1187 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001188 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001189}
1190
1191/*
1192 * Generate an ISN_FUNCREF instruction.
1193 */
1194 int
1195generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc)
1196{
1197 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001198 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001199
1200 RETURN_OK_IF_SKIP(cctx);
1201 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1202 return FAIL;
1203 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1204 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1205 else
1206 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1207 cctx->ctx_has_closure = 1;
1208
1209 // If the referenced function is a closure, it may use items further up in
1210 // the nested context, including this one.
1211 if (ufunc->uf_flags & FC_CLOSURE)
1212 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1213
Bram Moolenaar078a4612022-01-04 15:17:03 +00001214 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1215 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001216}
1217
1218/*
1219 * Generate an ISN_NEWFUNC instruction.
1220 * "lambda_name" and "func_name" must be in allocated memory and will be
1221 * consumed.
1222 */
1223 int
1224generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1225{
1226 isn_T *isn;
1227
1228 if (cctx->ctx_skip == SKIP_YES)
1229 {
1230 vim_free(lambda_name);
1231 vim_free(func_name);
1232 return OK;
1233 }
1234 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1235 {
1236 vim_free(lambda_name);
1237 vim_free(func_name);
1238 return FAIL;
1239 }
1240 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1241 isn->isn_arg.newfunc.nf_global = func_name;
1242
1243 return OK;
1244}
1245
1246/*
1247 * Generate an ISN_DEF instruction: list functions
1248 */
1249 int
1250generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1251{
1252 isn_T *isn;
1253
1254 RETURN_OK_IF_SKIP(cctx);
1255 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1256 return FAIL;
1257 if (len > 0)
1258 {
1259 isn->isn_arg.string = vim_strnsave(name, len);
1260 if (isn->isn_arg.string == NULL)
1261 return FAIL;
1262 }
1263 return OK;
1264}
1265
1266/*
1267 * Generate an ISN_JUMP instruction.
1268 */
1269 int
1270generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1271{
1272 isn_T *isn;
1273 garray_T *stack = &cctx->ctx_type_stack;
1274
1275 RETURN_OK_IF_SKIP(cctx);
1276 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1277 return FAIL;
1278 isn->isn_arg.jump.jump_when = when;
1279 isn->isn_arg.jump.jump_where = where;
1280
1281 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1282 --stack->ga_len;
1283
1284 return OK;
1285}
1286
1287/*
1288 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1289 */
1290 int
1291generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1292{
1293 isn_T *isn;
1294
1295 RETURN_OK_IF_SKIP(cctx);
1296 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1297 return FAIL;
1298 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1299 // jump_where is set later
1300 return OK;
1301}
1302
1303 int
1304generate_FOR(cctx_T *cctx, int loop_idx)
1305{
1306 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001307
1308 RETURN_OK_IF_SKIP(cctx);
1309 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1310 return FAIL;
1311 isn->isn_arg.forloop.for_idx = loop_idx;
1312
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001313 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001314 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001315}
1316/*
1317 * Generate an ISN_TRYCONT instruction.
1318 */
1319 int
1320generate_TRYCONT(cctx_T *cctx, int levels, int where)
1321{
1322 isn_T *isn;
1323
1324 RETURN_OK_IF_SKIP(cctx);
1325 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1326 return FAIL;
1327 isn->isn_arg.trycont.tct_levels = levels;
1328 isn->isn_arg.trycont.tct_where = where;
1329
1330 return OK;
1331}
1332
1333
1334/*
1335 * Generate an ISN_BCALL instruction.
1336 * "method_call" is TRUE for "value->method()"
1337 * Return FAIL if the number of arguments is wrong.
1338 */
1339 int
1340generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1341{
1342 isn_T *isn;
1343 garray_T *stack = &cctx->ctx_type_stack;
1344 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001345 type2_T *typep;
1346 type2_T *argtypes = NULL;
1347 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1348 type2_T *maptype = NULL;
1349 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001350 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001351
1352 RETURN_OK_IF_SKIP(cctx);
1353 argoff = check_internal_func(func_idx, argcount);
1354 if (argoff < 0)
1355 return FAIL;
1356
1357 if (method_call && argoff > 1)
1358 {
1359 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1360 return FAIL;
1361 isn->isn_arg.shuffle.shfl_item = argcount;
1362 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1363 }
1364
1365 if (argcount > 0)
1366 {
1367 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001368 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001369 if (method_call && argoff > 1)
1370 {
1371 int i;
1372
1373 for (i = 0; i < argcount; ++i)
1374 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001375 ? typep[i + 1]
1376 : (i == argoff - 1) ? typep[0] : typep[i];
1377 argtypes = shuffled_argtypes;
1378 }
1379 else
1380 {
1381 int i;
1382
1383 for (i = 0; i < argcount; ++i)
1384 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001385 argtypes = shuffled_argtypes;
1386 }
1387 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1388 cctx) == FAIL)
1389 return FAIL;
1390 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001391 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392 }
1393
1394 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1395 return FAIL;
1396 isn->isn_arg.bfunc.cbf_idx = func_idx;
1397 isn->isn_arg.bfunc.cbf_argcount = argcount;
1398
1399 // Drop the argument types and push the return type.
1400 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001401 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1402 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001403 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001404
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001405 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1406 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001407 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001408 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001409
1410 return OK;
1411}
1412
1413/*
1414 * Generate an ISN_LISTAPPEND instruction. Works like add().
1415 * Argument count is already checked.
1416 */
1417 int
1418generate_LISTAPPEND(cctx_T *cctx)
1419{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001420 type_T *list_type;
1421 type_T *item_type;
1422 type_T *expected;
1423
1424 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001425 // For checking the item type we use the declared type of the list and the
1426 // current type of the added item, adding a string to [1, 2] is OK.
1427 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001428 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001429 expected = list_type->tt_member;
1430 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1431 return FAIL;
1432
1433 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1434 return FAIL;
1435
Bram Moolenaar078a4612022-01-04 15:17:03 +00001436 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437 return OK;
1438}
1439
1440/*
1441 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1442 * Argument count is already checked.
1443 */
1444 int
1445generate_BLOBAPPEND(cctx_T *cctx)
1446{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001447 type_T *item_type;
1448
1449 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001450 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001451 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1452 return FAIL;
1453
1454 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1455 return FAIL;
1456
Bram Moolenaar078a4612022-01-04 15:17:03 +00001457 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458 return OK;
1459}
1460
1461/*
1462 * Generate an ISN_DCALL or ISN_UCALL instruction.
1463 * Return FAIL if the number of arguments is wrong.
1464 */
1465 int
1466generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1467{
1468 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001469 int regular_args = ufunc->uf_args.ga_len;
1470 int argcount = pushed_argcount;
1471
1472 RETURN_OK_IF_SKIP(cctx);
1473 if (argcount > regular_args && !has_varargs(ufunc))
1474 {
1475 semsg(_(e_too_many_arguments_for_function_str),
1476 printable_func_name(ufunc));
1477 return FAIL;
1478 }
1479 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1480 {
1481 semsg(_(e_not_enough_arguments_for_function_str),
1482 printable_func_name(ufunc));
1483 return FAIL;
1484 }
1485
1486 if (ufunc->uf_def_status != UF_NOT_COMPILED
1487 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1488 {
1489 int i;
1490
1491 for (i = 0; i < argcount; ++i)
1492 {
1493 type_T *expected;
1494 type_T *actual;
1495
Bram Moolenaar078a4612022-01-04 15:17:03 +00001496 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001497 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001498 && i >= regular_args - ufunc->uf_def_args.ga_len)
1499 {
1500 // assume v:none used for default argument value
1501 continue;
1502 }
1503 if (i < regular_args)
1504 {
1505 if (ufunc->uf_arg_types == NULL)
1506 continue;
1507 expected = ufunc->uf_arg_types[i];
1508 }
1509 else if (ufunc->uf_va_type == NULL
1510 || ufunc->uf_va_type == &t_list_any)
1511 // possibly a lambda or "...: any"
1512 expected = &t_any;
1513 else
1514 expected = ufunc->uf_va_type->tt_member;
1515 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1516 TRUE, FALSE) == FAIL)
1517 {
1518 arg_type_mismatch(expected, actual, i + 1);
1519 return FAIL;
1520 }
1521 }
1522 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
1523 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
1524 COMPILE_TYPE(ufunc), NULL) == FAIL)
1525 return FAIL;
1526 }
1527 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1528 {
1529 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1530 ufunc->uf_name);
1531 return FAIL;
1532 }
1533
1534 if ((isn = generate_instr(cctx,
1535 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1536 : ISN_UCALL)) == NULL)
1537 return FAIL;
1538 if (isn->isn_type == ISN_DCALL)
1539 {
1540 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1541 isn->isn_arg.dfunc.cdf_argcount = argcount;
1542 }
1543 else
1544 {
1545 // A user function may be deleted and redefined later, can't use the
1546 // ufunc pointer, need to look it up again at runtime.
1547 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1548 isn->isn_arg.ufunc.cuf_argcount = argcount;
1549 }
1550
Bram Moolenaar078a4612022-01-04 15:17:03 +00001551 // drop the argument types
1552 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001553
Bram Moolenaar078a4612022-01-04 15:17:03 +00001554 // add return type
1555 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001556}
1557
1558/*
1559 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1560 */
1561 int
1562generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1563{
1564 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001565
1566 RETURN_OK_IF_SKIP(cctx);
1567 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1568 return FAIL;
1569 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1570 isn->isn_arg.ufunc.cuf_argcount = argcount;
1571
Bram Moolenaar078a4612022-01-04 15:17:03 +00001572 // drop the argument types
1573 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001574
Bram Moolenaar078a4612022-01-04 15:17:03 +00001575 // add return value
1576 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001577}
1578
1579/*
1580 * Generate an ISN_PCALL instruction.
1581 * "type" is the type of the FuncRef.
1582 */
1583 int
1584generate_PCALL(
1585 cctx_T *cctx,
1586 int argcount,
1587 char_u *name,
1588 type_T *type,
1589 int at_top)
1590{
1591 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001592 type_T *ret_type;
1593
1594 RETURN_OK_IF_SKIP(cctx);
1595
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001596 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001597 ret_type = &t_any;
1598 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1599 {
1600 if (type->tt_argcount != -1)
1601 {
1602 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1603
1604 if (argcount < type->tt_min_argcount - varargs)
1605 {
1606 semsg(_(e_not_enough_arguments_for_function_str), name);
1607 return FAIL;
1608 }
1609 if (!varargs && argcount > type->tt_argcount)
1610 {
1611 semsg(_(e_too_many_arguments_for_function_str), name);
1612 return FAIL;
1613 }
1614 if (type->tt_args != NULL)
1615 {
1616 int i;
1617
1618 for (i = 0; i < argcount; ++i)
1619 {
1620 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001621 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001622 type_T *expected;
1623
1624 if (varargs && i >= type->tt_argcount - 1)
1625 expected = type->tt_args[
1626 type->tt_argcount - 1]->tt_member;
1627 else if (i >= type->tt_min_argcount
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001628 && actual->tt_type == VAR_SPECIAL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001629 expected = &t_any;
1630 else
1631 expected = type->tt_args[i];
1632 if (need_type(actual, expected, offset, i + 1,
1633 cctx, TRUE, FALSE) == FAIL)
1634 {
1635 arg_type_mismatch(expected, actual, i + 1);
1636 return FAIL;
1637 }
1638 }
1639 }
1640 }
1641 ret_type = type->tt_member;
1642 if (ret_type == &t_unknown)
1643 // return type not known yet, use a runtime check
1644 ret_type = &t_any;
1645 }
1646 else
1647 {
1648 semsg(_(e_not_callable_type_str), name);
1649 return FAIL;
1650 }
1651
1652 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1653 return FAIL;
1654 isn->isn_arg.pfunc.cpf_top = at_top;
1655 isn->isn_arg.pfunc.cpf_argcount = argcount;
1656
Bram Moolenaar078a4612022-01-04 15:17:03 +00001657 // drop the arguments and the funcref/partial
1658 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001659
Bram Moolenaar078a4612022-01-04 15:17:03 +00001660 // push the return value
1661 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001662
1663 // If partial is above the arguments it must be cleared and replaced with
1664 // the return value.
1665 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1666 return FAIL;
1667
1668 return OK;
1669}
1670
1671/*
1672 * Generate an ISN_STRINGMEMBER instruction.
1673 */
1674 int
1675generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1676{
1677 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001678 type_T *type;
1679
1680 RETURN_OK_IF_SKIP(cctx);
1681 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1682 return FAIL;
1683 isn->isn_arg.string = vim_strnsave(name, len);
1684
1685 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001686 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001687 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688 {
1689 char *tofree;
1690
1691 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1692 name, type_name(type, &tofree));
1693 vim_free(tofree);
1694 return FAIL;
1695 }
1696 // change dict type to dict member type
1697 if (type->tt_type == VAR_DICT)
1698 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001699 type_T *ntype = type->tt_member == &t_unknown
1700 ? &t_any : type->tt_member;
1701 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001702 }
1703
1704 return OK;
1705}
1706
1707/*
1708 * Generate an ISN_ECHO instruction.
1709 */
1710 int
1711generate_ECHO(cctx_T *cctx, int with_white, int count)
1712{
1713 isn_T *isn;
1714
1715 RETURN_OK_IF_SKIP(cctx);
1716 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1717 return FAIL;
1718 isn->isn_arg.echo.echo_with_white = with_white;
1719 isn->isn_arg.echo.echo_count = count;
1720
1721 return OK;
1722}
1723
1724/*
1725 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1726 */
1727 int
1728generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1729{
1730 isn_T *isn;
1731
1732 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1733 return FAIL;
1734 isn->isn_arg.number = count;
1735
1736 return OK;
1737}
1738
1739/*
1740 * Generate an ISN_PUT instruction.
1741 */
1742 int
1743generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1744{
1745 isn_T *isn;
1746
1747 RETURN_OK_IF_SKIP(cctx);
1748 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1749 return FAIL;
1750 isn->isn_arg.put.put_regname = regname;
1751 isn->isn_arg.put.put_lnum = lnum;
1752 return OK;
1753}
1754
1755/*
1756 * Generate an EXEC instruction that takes a string argument.
1757 * A copy is made of "line".
1758 */
1759 int
1760generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1761{
1762 isn_T *isn;
1763
1764 RETURN_OK_IF_SKIP(cctx);
1765 if ((isn = generate_instr(cctx, isntype)) == NULL)
1766 return FAIL;
1767 isn->isn_arg.string = vim_strsave(line);
1768 return OK;
1769}
1770
1771/*
1772 * Generate an EXEC instruction that takes a string argument.
1773 * "str" must be allocated, it is consumed.
1774 */
1775 int
1776generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1777{
1778 isn_T *isn;
1779
1780 if (cctx->ctx_skip == SKIP_YES)
1781 {
1782 vim_free(str);
1783 return OK;
1784 }
1785 if ((isn = generate_instr(cctx, isntype)) == NULL)
1786 {
1787 vim_free(str);
1788 return FAIL;
1789 }
1790 isn->isn_arg.string = str;
1791 return OK;
1792}
1793
1794 int
1795generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1796{
1797 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001798
1799 RETURN_OK_IF_SKIP(cctx);
1800 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1801 return FAIL;
1802 isn->isn_arg.string = vim_strsave(line);
1803
Bram Moolenaar078a4612022-01-04 15:17:03 +00001804 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001805}
1806
1807 int
1808generate_EXECCONCAT(cctx_T *cctx, int count)
1809{
1810 isn_T *isn;
1811
1812 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1813 return FAIL;
1814 isn->isn_arg.number = count;
1815 return OK;
1816}
1817
1818/*
1819 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1820 */
1821 int
1822generate_RANGE(cctx_T *cctx, char_u *range)
1823{
1824 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825
1826 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1827 return FAIL;
1828 isn->isn_arg.string = range;
1829
Bram Moolenaar078a4612022-01-04 15:17:03 +00001830 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001831}
1832
1833 int
1834generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1835{
1836 isn_T *isn;
1837
1838 RETURN_OK_IF_SKIP(cctx);
1839 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1840 return FAIL;
1841 isn->isn_arg.unpack.unp_count = var_count;
1842 isn->isn_arg.unpack.unp_semicolon = semicolon;
1843 return OK;
1844}
1845
1846/*
1847 * Generate an instruction for any command modifiers.
1848 */
1849 int
1850generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1851{
1852 isn_T *isn;
1853
1854 if (has_cmdmod(cmod, FALSE))
1855 {
1856 cctx->ctx_has_cmdmod = TRUE;
1857
1858 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1859 return FAIL;
1860 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1861 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1862 return FAIL;
1863 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1864 // filter program now belongs to the instruction
1865 cmod->cmod_filter_regmatch.regprog = NULL;
1866 }
1867
1868 return OK;
1869}
1870
1871 int
1872generate_undo_cmdmods(cctx_T *cctx)
1873{
1874 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1875 return FAIL;
1876 cctx->ctx_has_cmdmod = FALSE;
1877 return OK;
1878}
1879
1880/*
1881 * Generate a STORE instruction for "dest", not being "dest_local".
1882 * Return FAIL when out of memory.
1883 */
1884 int
1885generate_store_var(
1886 cctx_T *cctx,
1887 assign_dest_T dest,
1888 int opt_flags,
1889 int vimvaridx,
1890 int scriptvar_idx,
1891 int scriptvar_sid,
1892 type_T *type,
1893 char_u *name)
1894{
1895 switch (dest)
1896 {
1897 case dest_option:
1898 return generate_STOREOPT(cctx, ISN_STOREOPT,
1899 skip_option_env_lead(name), opt_flags);
1900 case dest_func_option:
1901 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1902 skip_option_env_lead(name), opt_flags);
1903 case dest_global:
1904 // include g: with the name, easier to execute that way
1905 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1906 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1907 case dest_buffer:
1908 // include b: with the name, easier to execute that way
1909 return generate_STORE(cctx, ISN_STOREB, 0, name);
1910 case dest_window:
1911 // include w: with the name, easier to execute that way
1912 return generate_STORE(cctx, ISN_STOREW, 0, name);
1913 case dest_tab:
1914 // include t: with the name, easier to execute that way
1915 return generate_STORE(cctx, ISN_STORET, 0, name);
1916 case dest_env:
1917 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1918 case dest_reg:
1919 return generate_STORE(cctx, ISN_STOREREG,
1920 name[1] == '@' ? '"' : name[1], NULL);
1921 case dest_vimvar:
1922 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1923 case dest_script:
1924 if (scriptvar_idx < 0)
1925 // "s:" may be included in the name.
1926 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1927 scriptvar_sid, type);
1928 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1929 scriptvar_sid, scriptvar_idx, type);
1930 case dest_local:
1931 case dest_expr:
1932 // cannot happen
1933 break;
1934 }
1935 return FAIL;
1936}
1937
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001938/*
1939 * Return TRUE when inside a "for" or "while" loop.
1940 */
1941 int
1942inside_loop_scope(cctx_T *cctx)
1943{
1944 scope_T *scope = cctx->ctx_scope;
1945
1946 for (;;)
1947 {
1948 if (scope == NULL)
1949 break;
1950 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1951 return TRUE;
1952 scope = scope->se_outer;
1953 }
1954 return FALSE;
1955}
1956
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001957 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001958generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001959{
1960 if (lhs->lhs_dest != dest_local)
1961 return generate_store_var(cctx, lhs->lhs_dest,
1962 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1963 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1964 lhs->lhs_type, lhs->lhs_name);
1965
1966 if (lhs->lhs_lvar != NULL)
1967 {
1968 garray_T *instr = &cctx->ctx_instr;
1969 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1970
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001971 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1972 // ISN_STORENR.
1973 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001974 if (lhs->lhs_lvar->lv_from_outer == 0
1975 && instr->ga_len == instr_count + 1
1976 && isn->isn_type == ISN_PUSHNR)
1977 {
1978 varnumber_T val = isn->isn_arg.number;
1979 garray_T *stack = &cctx->ctx_type_stack;
1980
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001981 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001982 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001983 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001984 --instr->ga_len;
1985 }
1986 else
1987 {
1988 isn->isn_type = ISN_STORENR;
1989 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1990 isn->isn_arg.storenr.stnr_val = val;
1991 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001992 if (stack->ga_len > 0)
1993 --stack->ga_len;
1994 }
1995 else if (lhs->lhs_lvar->lv_from_outer > 0)
1996 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
1997 lhs->lhs_lvar->lv_from_outer);
1998 else
1999 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2000 }
2001 return OK;
2002}
2003
2004#if defined(FEAT_PROFILE) || defined(PROTO)
2005 void
2006may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2007{
2008 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2009 generate_instr(cctx, ISN_PROF_END);
2010}
2011#endif
2012
2013
2014/*
2015 * Delete an instruction, free what it contains.
2016 */
2017 void
2018delete_instr(isn_T *isn)
2019{
2020 switch (isn->isn_type)
2021 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002022 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002023 case ISN_DEF:
2024 case ISN_EXEC:
2025 case ISN_EXECRANGE:
2026 case ISN_EXEC_SPLIT:
2027 case ISN_LEGACY_EVAL:
2028 case ISN_LOADAUTO:
2029 case ISN_LOADB:
2030 case ISN_LOADENV:
2031 case ISN_LOADG:
2032 case ISN_LOADOPT:
2033 case ISN_LOADT:
2034 case ISN_LOADW:
2035 case ISN_LOCKUNLOCK:
2036 case ISN_PUSHEXC:
2037 case ISN_PUSHFUNC:
2038 case ISN_PUSHS:
2039 case ISN_RANGE:
2040 case ISN_STOREAUTO:
2041 case ISN_STOREB:
2042 case ISN_STOREENV:
2043 case ISN_STOREG:
2044 case ISN_STORET:
2045 case ISN_STOREW:
2046 case ISN_STRINGMEMBER:
2047 vim_free(isn->isn_arg.string);
2048 break;
2049
2050 case ISN_SUBSTITUTE:
2051 {
2052 int idx;
2053 isn_T *list = isn->isn_arg.subs.subs_instr;
2054
2055 vim_free(isn->isn_arg.subs.subs_cmd);
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_INSTR:
2063 {
2064 int idx;
2065 isn_T *list = isn->isn_arg.instr;
2066
2067 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2068 delete_instr(list + idx);
2069 vim_free(list);
2070 }
2071 break;
2072
2073 case ISN_LOADS:
2074 case ISN_STORES:
2075 vim_free(isn->isn_arg.loadstore.ls_name);
2076 break;
2077
2078 case ISN_UNLET:
2079 case ISN_UNLETENV:
2080 vim_free(isn->isn_arg.unlet.ul_name);
2081 break;
2082
2083 case ISN_STOREOPT:
2084 case ISN_STOREFUNCOPT:
2085 vim_free(isn->isn_arg.storeopt.so_name);
2086 break;
2087
2088 case ISN_PUSHBLOB: // push blob isn_arg.blob
2089 blob_unref(isn->isn_arg.blob);
2090 break;
2091
2092 case ISN_PUSHJOB:
2093#ifdef FEAT_JOB_CHANNEL
2094 job_unref(isn->isn_arg.job);
2095#endif
2096 break;
2097
2098 case ISN_PUSHCHANNEL:
2099#ifdef FEAT_JOB_CHANNEL
2100 channel_unref(isn->isn_arg.channel);
2101#endif
2102 break;
2103
2104 case ISN_UCALL:
2105 vim_free(isn->isn_arg.ufunc.cuf_name);
2106 break;
2107
2108 case ISN_FUNCREF:
2109 {
2110 if (isn->isn_arg.funcref.fr_func_name == NULL)
2111 {
2112 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2113 + isn->isn_arg.funcref.fr_dfunc_idx;
2114 ufunc_T *ufunc = dfunc->df_ufunc;
2115
2116 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2117 func_ptr_unref(ufunc);
2118 }
2119 else
2120 {
2121 char_u *name = isn->isn_arg.funcref.fr_func_name;
2122
2123 if (name != NULL)
2124 func_unref(name);
2125 vim_free(isn->isn_arg.funcref.fr_func_name);
2126 }
2127 }
2128 break;
2129
2130 case ISN_DCALL:
2131 {
2132 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2133 + isn->isn_arg.dfunc.cdf_idx;
2134
2135 if (dfunc->df_ufunc != NULL
2136 && func_name_refcount(dfunc->df_ufunc->uf_name))
2137 func_ptr_unref(dfunc->df_ufunc);
2138 }
2139 break;
2140
2141 case ISN_NEWFUNC:
2142 {
2143 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002144 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002145
2146 if (ufunc != NULL)
2147 {
2148 unlink_def_function(ufunc);
2149 func_ptr_unref(ufunc);
2150 }
2151
2152 vim_free(lambda);
2153 vim_free(isn->isn_arg.newfunc.nf_global);
2154 }
2155 break;
2156
2157 case ISN_CHECKTYPE:
2158 case ISN_SETTYPE:
2159 free_type(isn->isn_arg.type.ct_type);
2160 break;
2161
2162 case ISN_CMDMOD:
2163 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2164 ->cmod_filter_regmatch.regprog);
2165 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2166 break;
2167
2168 case ISN_LOADSCRIPT:
2169 case ISN_STORESCRIPT:
2170 vim_free(isn->isn_arg.script.scriptref);
2171 break;
2172
2173 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002174 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002175 break;
2176
2177 case ISN_CEXPR_CORE:
2178 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2179 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2180 break;
2181
2182 case ISN_2BOOL:
2183 case ISN_2STRING:
2184 case ISN_2STRING_ANY:
2185 case ISN_ADDBLOB:
2186 case ISN_ADDLIST:
2187 case ISN_ANYINDEX:
2188 case ISN_ANYSLICE:
2189 case ISN_BCALL:
2190 case ISN_BLOBAPPEND:
2191 case ISN_BLOBINDEX:
2192 case ISN_BLOBSLICE:
2193 case ISN_CATCH:
2194 case ISN_CEXPR_AUCMD:
2195 case ISN_CHECKLEN:
2196 case ISN_CHECKNR:
2197 case ISN_CLEARDICT:
2198 case ISN_CMDMOD_REV:
2199 case ISN_COMPAREANY:
2200 case ISN_COMPAREBLOB:
2201 case ISN_COMPAREBOOL:
2202 case ISN_COMPAREDICT:
2203 case ISN_COMPAREFLOAT:
2204 case ISN_COMPAREFUNC:
2205 case ISN_COMPARELIST:
2206 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002207 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002208 case ISN_COMPARESPECIAL:
2209 case ISN_COMPARESTRING:
2210 case ISN_CONCAT:
2211 case ISN_COND2BOOL:
2212 case ISN_DEBUG:
2213 case ISN_DROP:
2214 case ISN_ECHO:
2215 case ISN_ECHOCONSOLE:
2216 case ISN_ECHOERR:
2217 case ISN_ECHOMSG:
2218 case ISN_ENDTRY:
2219 case ISN_EXECCONCAT:
2220 case ISN_EXECUTE:
2221 case ISN_FINALLY:
2222 case ISN_FINISH:
2223 case ISN_FOR:
2224 case ISN_GETITEM:
2225 case ISN_JUMP:
2226 case ISN_JUMP_IF_ARG_SET:
2227 case ISN_LISTAPPEND:
2228 case ISN_LISTINDEX:
2229 case ISN_LISTSLICE:
2230 case ISN_LOAD:
2231 case ISN_LOADBDICT:
2232 case ISN_LOADGDICT:
2233 case ISN_LOADOUTER:
2234 case ISN_LOADREG:
2235 case ISN_LOADTDICT:
2236 case ISN_LOADV:
2237 case ISN_LOADWDICT:
2238 case ISN_LOCKCONST:
2239 case ISN_MEMBER:
2240 case ISN_NEGATENR:
2241 case ISN_NEWDICT:
2242 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002243 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002244 case ISN_OPANY:
2245 case ISN_OPFLOAT:
2246 case ISN_OPNR:
2247 case ISN_PCALL:
2248 case ISN_PCALL_END:
2249 case ISN_PROF_END:
2250 case ISN_PROF_START:
2251 case ISN_PUSHBOOL:
2252 case ISN_PUSHF:
2253 case ISN_PUSHNR:
2254 case ISN_PUSHSPEC:
2255 case ISN_PUT:
2256 case ISN_REDIREND:
2257 case ISN_REDIRSTART:
2258 case ISN_RETURN:
2259 case ISN_RETURN_VOID:
2260 case ISN_SHUFFLE:
2261 case ISN_SLICE:
2262 case ISN_STORE:
2263 case ISN_STOREINDEX:
2264 case ISN_STORENR:
2265 case ISN_STOREOUTER:
2266 case ISN_STORERANGE:
2267 case ISN_STOREREG:
2268 case ISN_STOREV:
2269 case ISN_STRINDEX:
2270 case ISN_STRSLICE:
2271 case ISN_THROW:
2272 case ISN_TRYCONT:
2273 case ISN_UNLETINDEX:
2274 case ISN_UNLETRANGE:
2275 case ISN_UNPACK:
2276 case ISN_USEDICT:
2277 // nothing allocated
2278 break;
2279 }
2280}
2281
2282 void
2283clear_instr_ga(garray_T *gap)
2284{
2285 int idx;
2286
2287 for (idx = 0; idx < gap->ga_len; ++idx)
2288 delete_instr(((isn_T *)gap->ga_data) + idx);
2289 ga_clear(gap);
2290}
2291
2292
2293#endif // defined(FEAT_EVAL)