blob: de144dcc10d1f2d55f3304410e4fe43f2bed2f50 [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
Bram Moolenaar139575d2022-03-15 19:29:30 +00001210 // the nested context, including this one. But not a function defined at
1211 // the script level.
1212 if ((ufunc->uf_flags & FC_CLOSURE)
1213 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001214 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1215
Bram Moolenaar078a4612022-01-04 15:17:03 +00001216 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1217 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218}
1219
1220/*
1221 * Generate an ISN_NEWFUNC instruction.
1222 * "lambda_name" and "func_name" must be in allocated memory and will be
1223 * consumed.
1224 */
1225 int
1226generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1227{
1228 isn_T *isn;
1229
1230 if (cctx->ctx_skip == SKIP_YES)
1231 {
1232 vim_free(lambda_name);
1233 vim_free(func_name);
1234 return OK;
1235 }
1236 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1237 {
1238 vim_free(lambda_name);
1239 vim_free(func_name);
1240 return FAIL;
1241 }
1242 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1243 isn->isn_arg.newfunc.nf_global = func_name;
1244
1245 return OK;
1246}
1247
1248/*
1249 * Generate an ISN_DEF instruction: list functions
1250 */
1251 int
1252generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1253{
1254 isn_T *isn;
1255
1256 RETURN_OK_IF_SKIP(cctx);
1257 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1258 return FAIL;
1259 if (len > 0)
1260 {
1261 isn->isn_arg.string = vim_strnsave(name, len);
1262 if (isn->isn_arg.string == NULL)
1263 return FAIL;
1264 }
1265 return OK;
1266}
1267
1268/*
1269 * Generate an ISN_JUMP instruction.
1270 */
1271 int
1272generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1273{
1274 isn_T *isn;
1275 garray_T *stack = &cctx->ctx_type_stack;
1276
1277 RETURN_OK_IF_SKIP(cctx);
1278 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1279 return FAIL;
1280 isn->isn_arg.jump.jump_when = when;
1281 isn->isn_arg.jump.jump_where = where;
1282
1283 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1284 --stack->ga_len;
1285
1286 return OK;
1287}
1288
1289/*
1290 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1291 */
1292 int
1293generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1294{
1295 isn_T *isn;
1296
1297 RETURN_OK_IF_SKIP(cctx);
1298 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1299 return FAIL;
1300 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1301 // jump_where is set later
1302 return OK;
1303}
1304
1305 int
1306generate_FOR(cctx_T *cctx, int loop_idx)
1307{
1308 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001309
1310 RETURN_OK_IF_SKIP(cctx);
1311 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1312 return FAIL;
1313 isn->isn_arg.forloop.for_idx = loop_idx;
1314
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001315 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001316 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001317}
1318/*
1319 * Generate an ISN_TRYCONT instruction.
1320 */
1321 int
1322generate_TRYCONT(cctx_T *cctx, int levels, int where)
1323{
1324 isn_T *isn;
1325
1326 RETURN_OK_IF_SKIP(cctx);
1327 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1328 return FAIL;
1329 isn->isn_arg.trycont.tct_levels = levels;
1330 isn->isn_arg.trycont.tct_where = where;
1331
1332 return OK;
1333}
1334
1335
1336/*
1337 * Generate an ISN_BCALL instruction.
1338 * "method_call" is TRUE for "value->method()"
1339 * Return FAIL if the number of arguments is wrong.
1340 */
1341 int
1342generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1343{
1344 isn_T *isn;
1345 garray_T *stack = &cctx->ctx_type_stack;
1346 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001347 type2_T *typep;
1348 type2_T *argtypes = NULL;
1349 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1350 type2_T *maptype = NULL;
1351 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001352 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001353
1354 RETURN_OK_IF_SKIP(cctx);
1355 argoff = check_internal_func(func_idx, argcount);
1356 if (argoff < 0)
1357 return FAIL;
1358
1359 if (method_call && argoff > 1)
1360 {
1361 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1362 return FAIL;
1363 isn->isn_arg.shuffle.shfl_item = argcount;
1364 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1365 }
1366
1367 if (argcount > 0)
1368 {
1369 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001370 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001371 if (method_call && argoff > 1)
1372 {
1373 int i;
1374
1375 for (i = 0; i < argcount; ++i)
1376 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001377 ? typep[i + 1]
1378 : (i == argoff - 1) ? typep[0] : typep[i];
1379 argtypes = shuffled_argtypes;
1380 }
1381 else
1382 {
1383 int i;
1384
1385 for (i = 0; i < argcount; ++i)
1386 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001387 argtypes = shuffled_argtypes;
1388 }
1389 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1390 cctx) == FAIL)
1391 return FAIL;
1392 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001393 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001394 }
1395
1396 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1397 return FAIL;
1398 isn->isn_arg.bfunc.cbf_idx = func_idx;
1399 isn->isn_arg.bfunc.cbf_argcount = argcount;
1400
1401 // Drop the argument types and push the return type.
1402 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001403 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1404 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001405 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001406
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001407 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1408 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001409 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001410 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001411
1412 return OK;
1413}
1414
1415/*
1416 * Generate an ISN_LISTAPPEND instruction. Works like add().
1417 * Argument count is already checked.
1418 */
1419 int
1420generate_LISTAPPEND(cctx_T *cctx)
1421{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001422 type_T *list_type;
1423 type_T *item_type;
1424 type_T *expected;
1425
1426 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001427 // For checking the item type we use the declared type of the list and the
1428 // current type of the added item, adding a string to [1, 2] is OK.
1429 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001430 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001431 expected = list_type->tt_member;
1432 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1433 return FAIL;
1434
1435 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1436 return FAIL;
1437
Bram Moolenaar078a4612022-01-04 15:17:03 +00001438 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001439 return OK;
1440}
1441
1442/*
1443 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1444 * Argument count is already checked.
1445 */
1446 int
1447generate_BLOBAPPEND(cctx_T *cctx)
1448{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001449 type_T *item_type;
1450
1451 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001452 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001453 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1454 return FAIL;
1455
1456 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1457 return FAIL;
1458
Bram Moolenaar078a4612022-01-04 15:17:03 +00001459 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001460 return OK;
1461}
1462
1463/*
1464 * Generate an ISN_DCALL or ISN_UCALL instruction.
1465 * Return FAIL if the number of arguments is wrong.
1466 */
1467 int
1468generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1469{
1470 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001471 int regular_args = ufunc->uf_args.ga_len;
1472 int argcount = pushed_argcount;
1473
1474 RETURN_OK_IF_SKIP(cctx);
1475 if (argcount > regular_args && !has_varargs(ufunc))
1476 {
1477 semsg(_(e_too_many_arguments_for_function_str),
1478 printable_func_name(ufunc));
1479 return FAIL;
1480 }
1481 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1482 {
1483 semsg(_(e_not_enough_arguments_for_function_str),
1484 printable_func_name(ufunc));
1485 return FAIL;
1486 }
1487
1488 if (ufunc->uf_def_status != UF_NOT_COMPILED
1489 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1490 {
1491 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001492 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001493
1494 for (i = 0; i < argcount; ++i)
1495 {
1496 type_T *expected;
1497 type_T *actual;
1498
Bram Moolenaar078a4612022-01-04 15:17:03 +00001499 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001500 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001501 && i >= regular_args - ufunc->uf_def_args.ga_len)
1502 {
1503 // assume v:none used for default argument value
1504 continue;
1505 }
1506 if (i < regular_args)
1507 {
1508 if (ufunc->uf_arg_types == NULL)
1509 continue;
1510 expected = ufunc->uf_arg_types[i];
1511 }
1512 else if (ufunc->uf_va_type == NULL
1513 || ufunc->uf_va_type == &t_list_any)
1514 // possibly a lambda or "...: any"
1515 expected = &t_any;
1516 else
1517 expected = ufunc->uf_va_type->tt_member;
1518 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1519 TRUE, FALSE) == FAIL)
1520 {
1521 arg_type_mismatch(expected, actual, i + 1);
1522 return FAIL;
1523 }
1524 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001525 compile_type = get_compile_type(ufunc);
1526 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001527 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001528 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001529 return FAIL;
1530 }
1531 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1532 {
1533 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1534 ufunc->uf_name);
1535 return FAIL;
1536 }
1537
1538 if ((isn = generate_instr(cctx,
1539 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1540 : ISN_UCALL)) == NULL)
1541 return FAIL;
1542 if (isn->isn_type == ISN_DCALL)
1543 {
1544 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1545 isn->isn_arg.dfunc.cdf_argcount = argcount;
1546 }
1547 else
1548 {
1549 // A user function may be deleted and redefined later, can't use the
1550 // ufunc pointer, need to look it up again at runtime.
1551 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1552 isn->isn_arg.ufunc.cuf_argcount = argcount;
1553 }
1554
Bram Moolenaar078a4612022-01-04 15:17:03 +00001555 // drop the argument types
1556 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001557
Bram Moolenaar078a4612022-01-04 15:17:03 +00001558 // add return type
1559 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001560}
1561
1562/*
1563 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1564 */
1565 int
1566generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1567{
1568 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569
1570 RETURN_OK_IF_SKIP(cctx);
1571 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1572 return FAIL;
1573 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1574 isn->isn_arg.ufunc.cuf_argcount = argcount;
1575
Bram Moolenaar078a4612022-01-04 15:17:03 +00001576 // drop the argument types
1577 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578
Bram Moolenaar078a4612022-01-04 15:17:03 +00001579 // add return value
1580 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001581}
1582
1583/*
1584 * Generate an ISN_PCALL instruction.
1585 * "type" is the type of the FuncRef.
1586 */
1587 int
1588generate_PCALL(
1589 cctx_T *cctx,
1590 int argcount,
1591 char_u *name,
1592 type_T *type,
1593 int at_top)
1594{
1595 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001596 type_T *ret_type;
1597
1598 RETURN_OK_IF_SKIP(cctx);
1599
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001600 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001601 ret_type = &t_any;
1602 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1603 {
1604 if (type->tt_argcount != -1)
1605 {
1606 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1607
1608 if (argcount < type->tt_min_argcount - varargs)
1609 {
1610 semsg(_(e_not_enough_arguments_for_function_str), name);
1611 return FAIL;
1612 }
1613 if (!varargs && argcount > type->tt_argcount)
1614 {
1615 semsg(_(e_too_many_arguments_for_function_str), name);
1616 return FAIL;
1617 }
1618 if (type->tt_args != NULL)
1619 {
1620 int i;
1621
1622 for (i = 0; i < argcount; ++i)
1623 {
1624 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001625 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001626 type_T *expected;
1627
1628 if (varargs && i >= type->tt_argcount - 1)
1629 expected = type->tt_args[
1630 type->tt_argcount - 1]->tt_member;
1631 else if (i >= type->tt_min_argcount
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001632 && actual->tt_type == VAR_SPECIAL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 expected = &t_any;
1634 else
1635 expected = type->tt_args[i];
1636 if (need_type(actual, expected, offset, i + 1,
1637 cctx, TRUE, FALSE) == FAIL)
1638 {
1639 arg_type_mismatch(expected, actual, i + 1);
1640 return FAIL;
1641 }
1642 }
1643 }
1644 }
1645 ret_type = type->tt_member;
1646 if (ret_type == &t_unknown)
1647 // return type not known yet, use a runtime check
1648 ret_type = &t_any;
1649 }
1650 else
1651 {
1652 semsg(_(e_not_callable_type_str), name);
1653 return FAIL;
1654 }
1655
1656 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1657 return FAIL;
1658 isn->isn_arg.pfunc.cpf_top = at_top;
1659 isn->isn_arg.pfunc.cpf_argcount = argcount;
1660
Bram Moolenaar078a4612022-01-04 15:17:03 +00001661 // drop the arguments and the funcref/partial
1662 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001663
Bram Moolenaar078a4612022-01-04 15:17:03 +00001664 // push the return value
1665 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001666
1667 // If partial is above the arguments it must be cleared and replaced with
1668 // the return value.
1669 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1670 return FAIL;
1671
1672 return OK;
1673}
1674
1675/*
1676 * Generate an ISN_STRINGMEMBER instruction.
1677 */
1678 int
1679generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1680{
1681 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001682 type_T *type;
1683
1684 RETURN_OK_IF_SKIP(cctx);
1685 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1686 return FAIL;
1687 isn->isn_arg.string = vim_strnsave(name, len);
1688
1689 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001690 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001691 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001692 {
1693 char *tofree;
1694
1695 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1696 name, type_name(type, &tofree));
1697 vim_free(tofree);
1698 return FAIL;
1699 }
1700 // change dict type to dict member type
1701 if (type->tt_type == VAR_DICT)
1702 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001703 type_T *ntype = type->tt_member == &t_unknown
1704 ? &t_any : type->tt_member;
1705 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001706 }
1707
1708 return OK;
1709}
1710
1711/*
1712 * Generate an ISN_ECHO instruction.
1713 */
1714 int
1715generate_ECHO(cctx_T *cctx, int with_white, int count)
1716{
1717 isn_T *isn;
1718
1719 RETURN_OK_IF_SKIP(cctx);
1720 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1721 return FAIL;
1722 isn->isn_arg.echo.echo_with_white = with_white;
1723 isn->isn_arg.echo.echo_count = count;
1724
1725 return OK;
1726}
1727
1728/*
1729 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1730 */
1731 int
1732generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1733{
1734 isn_T *isn;
1735
1736 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1737 return FAIL;
1738 isn->isn_arg.number = count;
1739
1740 return OK;
1741}
1742
1743/*
1744 * Generate an ISN_PUT instruction.
1745 */
1746 int
1747generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1748{
1749 isn_T *isn;
1750
1751 RETURN_OK_IF_SKIP(cctx);
1752 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1753 return FAIL;
1754 isn->isn_arg.put.put_regname = regname;
1755 isn->isn_arg.put.put_lnum = lnum;
1756 return OK;
1757}
1758
1759/*
1760 * Generate an EXEC instruction that takes a string argument.
1761 * A copy is made of "line".
1762 */
1763 int
1764generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1765{
1766 isn_T *isn;
1767
1768 RETURN_OK_IF_SKIP(cctx);
1769 if ((isn = generate_instr(cctx, isntype)) == NULL)
1770 return FAIL;
1771 isn->isn_arg.string = vim_strsave(line);
1772 return OK;
1773}
1774
1775/*
1776 * Generate an EXEC instruction that takes a string argument.
1777 * "str" must be allocated, it is consumed.
1778 */
1779 int
1780generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1781{
1782 isn_T *isn;
1783
1784 if (cctx->ctx_skip == SKIP_YES)
1785 {
1786 vim_free(str);
1787 return OK;
1788 }
1789 if ((isn = generate_instr(cctx, isntype)) == NULL)
1790 {
1791 vim_free(str);
1792 return FAIL;
1793 }
1794 isn->isn_arg.string = str;
1795 return OK;
1796}
1797
1798 int
1799generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1800{
1801 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001802
1803 RETURN_OK_IF_SKIP(cctx);
1804 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1805 return FAIL;
1806 isn->isn_arg.string = vim_strsave(line);
1807
Bram Moolenaar078a4612022-01-04 15:17:03 +00001808 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001809}
1810
1811 int
1812generate_EXECCONCAT(cctx_T *cctx, int count)
1813{
1814 isn_T *isn;
1815
1816 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1817 return FAIL;
1818 isn->isn_arg.number = count;
1819 return OK;
1820}
1821
1822/*
1823 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1824 */
1825 int
1826generate_RANGE(cctx_T *cctx, char_u *range)
1827{
1828 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001829
1830 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1831 return FAIL;
1832 isn->isn_arg.string = range;
1833
Bram Moolenaar078a4612022-01-04 15:17:03 +00001834 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001835}
1836
1837 int
1838generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1839{
1840 isn_T *isn;
1841
1842 RETURN_OK_IF_SKIP(cctx);
1843 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1844 return FAIL;
1845 isn->isn_arg.unpack.unp_count = var_count;
1846 isn->isn_arg.unpack.unp_semicolon = semicolon;
1847 return OK;
1848}
1849
1850/*
1851 * Generate an instruction for any command modifiers.
1852 */
1853 int
1854generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1855{
1856 isn_T *isn;
1857
1858 if (has_cmdmod(cmod, FALSE))
1859 {
1860 cctx->ctx_has_cmdmod = TRUE;
1861
1862 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1863 return FAIL;
1864 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1865 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1866 return FAIL;
1867 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1868 // filter program now belongs to the instruction
1869 cmod->cmod_filter_regmatch.regprog = NULL;
1870 }
1871
1872 return OK;
1873}
1874
1875 int
1876generate_undo_cmdmods(cctx_T *cctx)
1877{
1878 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1879 return FAIL;
1880 cctx->ctx_has_cmdmod = FALSE;
1881 return OK;
1882}
1883
1884/*
1885 * Generate a STORE instruction for "dest", not being "dest_local".
1886 * Return FAIL when out of memory.
1887 */
1888 int
1889generate_store_var(
1890 cctx_T *cctx,
1891 assign_dest_T dest,
1892 int opt_flags,
1893 int vimvaridx,
1894 int scriptvar_idx,
1895 int scriptvar_sid,
1896 type_T *type,
1897 char_u *name)
1898{
1899 switch (dest)
1900 {
1901 case dest_option:
1902 return generate_STOREOPT(cctx, ISN_STOREOPT,
1903 skip_option_env_lead(name), opt_flags);
1904 case dest_func_option:
1905 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1906 skip_option_env_lead(name), opt_flags);
1907 case dest_global:
1908 // include g: with the name, easier to execute that way
1909 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1910 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1911 case dest_buffer:
1912 // include b: with the name, easier to execute that way
1913 return generate_STORE(cctx, ISN_STOREB, 0, name);
1914 case dest_window:
1915 // include w: with the name, easier to execute that way
1916 return generate_STORE(cctx, ISN_STOREW, 0, name);
1917 case dest_tab:
1918 // include t: with the name, easier to execute that way
1919 return generate_STORE(cctx, ISN_STORET, 0, name);
1920 case dest_env:
1921 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1922 case dest_reg:
1923 return generate_STORE(cctx, ISN_STOREREG,
1924 name[1] == '@' ? '"' : name[1], NULL);
1925 case dest_vimvar:
1926 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1927 case dest_script:
1928 if (scriptvar_idx < 0)
1929 // "s:" may be included in the name.
1930 return generate_OLDSCRIPT(cctx, ISN_STORES, name,
1931 scriptvar_sid, type);
1932 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1933 scriptvar_sid, scriptvar_idx, type);
1934 case dest_local:
1935 case dest_expr:
1936 // cannot happen
1937 break;
1938 }
1939 return FAIL;
1940}
1941
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001942/*
1943 * Return TRUE when inside a "for" or "while" loop.
1944 */
1945 int
1946inside_loop_scope(cctx_T *cctx)
1947{
1948 scope_T *scope = cctx->ctx_scope;
1949
1950 for (;;)
1951 {
1952 if (scope == NULL)
1953 break;
1954 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1955 return TRUE;
1956 scope = scope->se_outer;
1957 }
1958 return FALSE;
1959}
1960
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001961 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001962generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001963{
1964 if (lhs->lhs_dest != dest_local)
1965 return generate_store_var(cctx, lhs->lhs_dest,
1966 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1967 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1968 lhs->lhs_type, lhs->lhs_name);
1969
1970 if (lhs->lhs_lvar != NULL)
1971 {
1972 garray_T *instr = &cctx->ctx_instr;
1973 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1974
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001975 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1976 // ISN_STORENR.
1977 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001978 if (lhs->lhs_lvar->lv_from_outer == 0
1979 && instr->ga_len == instr_count + 1
1980 && isn->isn_type == ISN_PUSHNR)
1981 {
1982 varnumber_T val = isn->isn_arg.number;
1983 garray_T *stack = &cctx->ctx_type_stack;
1984
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001985 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001986 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001987 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001988 --instr->ga_len;
1989 }
1990 else
1991 {
1992 isn->isn_type = ISN_STORENR;
1993 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1994 isn->isn_arg.storenr.stnr_val = val;
1995 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001996 if (stack->ga_len > 0)
1997 --stack->ga_len;
1998 }
1999 else if (lhs->lhs_lvar->lv_from_outer > 0)
2000 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2001 lhs->lhs_lvar->lv_from_outer);
2002 else
2003 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2004 }
2005 return OK;
2006}
2007
2008#if defined(FEAT_PROFILE) || defined(PROTO)
2009 void
2010may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2011{
2012 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2013 generate_instr(cctx, ISN_PROF_END);
2014}
2015#endif
2016
2017
2018/*
2019 * Delete an instruction, free what it contains.
2020 */
2021 void
2022delete_instr(isn_T *isn)
2023{
2024 switch (isn->isn_type)
2025 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002026 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002027 case ISN_DEF:
2028 case ISN_EXEC:
2029 case ISN_EXECRANGE:
2030 case ISN_EXEC_SPLIT:
2031 case ISN_LEGACY_EVAL:
2032 case ISN_LOADAUTO:
2033 case ISN_LOADB:
2034 case ISN_LOADENV:
2035 case ISN_LOADG:
2036 case ISN_LOADOPT:
2037 case ISN_LOADT:
2038 case ISN_LOADW:
2039 case ISN_LOCKUNLOCK:
2040 case ISN_PUSHEXC:
2041 case ISN_PUSHFUNC:
2042 case ISN_PUSHS:
2043 case ISN_RANGE:
2044 case ISN_STOREAUTO:
2045 case ISN_STOREB:
2046 case ISN_STOREENV:
2047 case ISN_STOREG:
2048 case ISN_STORET:
2049 case ISN_STOREW:
2050 case ISN_STRINGMEMBER:
2051 vim_free(isn->isn_arg.string);
2052 break;
2053
2054 case ISN_SUBSTITUTE:
2055 {
2056 int idx;
2057 isn_T *list = isn->isn_arg.subs.subs_instr;
2058
2059 vim_free(isn->isn_arg.subs.subs_cmd);
2060 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2061 delete_instr(list + idx);
2062 vim_free(list);
2063 }
2064 break;
2065
2066 case ISN_INSTR:
2067 {
2068 int idx;
2069 isn_T *list = isn->isn_arg.instr;
2070
2071 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2072 delete_instr(list + idx);
2073 vim_free(list);
2074 }
2075 break;
2076
2077 case ISN_LOADS:
2078 case ISN_STORES:
2079 vim_free(isn->isn_arg.loadstore.ls_name);
2080 break;
2081
2082 case ISN_UNLET:
2083 case ISN_UNLETENV:
2084 vim_free(isn->isn_arg.unlet.ul_name);
2085 break;
2086
2087 case ISN_STOREOPT:
2088 case ISN_STOREFUNCOPT:
2089 vim_free(isn->isn_arg.storeopt.so_name);
2090 break;
2091
2092 case ISN_PUSHBLOB: // push blob isn_arg.blob
2093 blob_unref(isn->isn_arg.blob);
2094 break;
2095
2096 case ISN_PUSHJOB:
2097#ifdef FEAT_JOB_CHANNEL
2098 job_unref(isn->isn_arg.job);
2099#endif
2100 break;
2101
2102 case ISN_PUSHCHANNEL:
2103#ifdef FEAT_JOB_CHANNEL
2104 channel_unref(isn->isn_arg.channel);
2105#endif
2106 break;
2107
2108 case ISN_UCALL:
2109 vim_free(isn->isn_arg.ufunc.cuf_name);
2110 break;
2111
2112 case ISN_FUNCREF:
2113 {
2114 if (isn->isn_arg.funcref.fr_func_name == NULL)
2115 {
2116 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2117 + isn->isn_arg.funcref.fr_dfunc_idx;
2118 ufunc_T *ufunc = dfunc->df_ufunc;
2119
2120 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2121 func_ptr_unref(ufunc);
2122 }
2123 else
2124 {
2125 char_u *name = isn->isn_arg.funcref.fr_func_name;
2126
2127 if (name != NULL)
2128 func_unref(name);
2129 vim_free(isn->isn_arg.funcref.fr_func_name);
2130 }
2131 }
2132 break;
2133
2134 case ISN_DCALL:
2135 {
2136 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
2137 + isn->isn_arg.dfunc.cdf_idx;
2138
2139 if (dfunc->df_ufunc != NULL
2140 && func_name_refcount(dfunc->df_ufunc->uf_name))
2141 func_ptr_unref(dfunc->df_ufunc);
2142 }
2143 break;
2144
2145 case ISN_NEWFUNC:
2146 {
2147 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002148 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002149
2150 if (ufunc != NULL)
2151 {
2152 unlink_def_function(ufunc);
2153 func_ptr_unref(ufunc);
2154 }
2155
2156 vim_free(lambda);
2157 vim_free(isn->isn_arg.newfunc.nf_global);
2158 }
2159 break;
2160
2161 case ISN_CHECKTYPE:
2162 case ISN_SETTYPE:
2163 free_type(isn->isn_arg.type.ct_type);
2164 break;
2165
2166 case ISN_CMDMOD:
2167 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
2168 ->cmod_filter_regmatch.regprog);
2169 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2170 break;
2171
2172 case ISN_LOADSCRIPT:
2173 case ISN_STORESCRIPT:
2174 vim_free(isn->isn_arg.script.scriptref);
2175 break;
2176
2177 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002178 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002179 break;
2180
2181 case ISN_CEXPR_CORE:
2182 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2183 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2184 break;
2185
2186 case ISN_2BOOL:
2187 case ISN_2STRING:
2188 case ISN_2STRING_ANY:
2189 case ISN_ADDBLOB:
2190 case ISN_ADDLIST:
2191 case ISN_ANYINDEX:
2192 case ISN_ANYSLICE:
2193 case ISN_BCALL:
2194 case ISN_BLOBAPPEND:
2195 case ISN_BLOBINDEX:
2196 case ISN_BLOBSLICE:
2197 case ISN_CATCH:
2198 case ISN_CEXPR_AUCMD:
2199 case ISN_CHECKLEN:
2200 case ISN_CHECKNR:
2201 case ISN_CLEARDICT:
2202 case ISN_CMDMOD_REV:
2203 case ISN_COMPAREANY:
2204 case ISN_COMPAREBLOB:
2205 case ISN_COMPAREBOOL:
2206 case ISN_COMPAREDICT:
2207 case ISN_COMPAREFLOAT:
2208 case ISN_COMPAREFUNC:
2209 case ISN_COMPARELIST:
2210 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002211 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002212 case ISN_COMPARESPECIAL:
2213 case ISN_COMPARESTRING:
2214 case ISN_CONCAT:
2215 case ISN_COND2BOOL:
2216 case ISN_DEBUG:
2217 case ISN_DROP:
2218 case ISN_ECHO:
2219 case ISN_ECHOCONSOLE:
2220 case ISN_ECHOERR:
2221 case ISN_ECHOMSG:
2222 case ISN_ENDTRY:
2223 case ISN_EXECCONCAT:
2224 case ISN_EXECUTE:
2225 case ISN_FINALLY:
2226 case ISN_FINISH:
2227 case ISN_FOR:
2228 case ISN_GETITEM:
2229 case ISN_JUMP:
2230 case ISN_JUMP_IF_ARG_SET:
2231 case ISN_LISTAPPEND:
2232 case ISN_LISTINDEX:
2233 case ISN_LISTSLICE:
2234 case ISN_LOAD:
2235 case ISN_LOADBDICT:
2236 case ISN_LOADGDICT:
2237 case ISN_LOADOUTER:
2238 case ISN_LOADREG:
2239 case ISN_LOADTDICT:
2240 case ISN_LOADV:
2241 case ISN_LOADWDICT:
2242 case ISN_LOCKCONST:
2243 case ISN_MEMBER:
2244 case ISN_NEGATENR:
2245 case ISN_NEWDICT:
2246 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002247 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002248 case ISN_OPANY:
2249 case ISN_OPFLOAT:
2250 case ISN_OPNR:
2251 case ISN_PCALL:
2252 case ISN_PCALL_END:
2253 case ISN_PROF_END:
2254 case ISN_PROF_START:
2255 case ISN_PUSHBOOL:
2256 case ISN_PUSHF:
2257 case ISN_PUSHNR:
2258 case ISN_PUSHSPEC:
2259 case ISN_PUT:
2260 case ISN_REDIREND:
2261 case ISN_REDIRSTART:
2262 case ISN_RETURN:
2263 case ISN_RETURN_VOID:
2264 case ISN_SHUFFLE:
2265 case ISN_SLICE:
2266 case ISN_STORE:
2267 case ISN_STOREINDEX:
2268 case ISN_STORENR:
2269 case ISN_STOREOUTER:
2270 case ISN_STORERANGE:
2271 case ISN_STOREREG:
2272 case ISN_STOREV:
2273 case ISN_STRINDEX:
2274 case ISN_STRSLICE:
2275 case ISN_THROW:
2276 case ISN_TRYCONT:
2277 case ISN_UNLETINDEX:
2278 case ISN_UNLETRANGE:
2279 case ISN_UNPACK:
2280 case ISN_USEDICT:
2281 // nothing allocated
2282 break;
2283 }
2284}
2285
2286 void
2287clear_instr_ga(garray_T *gap)
2288{
2289 int idx;
2290
2291 for (idx = 0; idx < gap->ga_len; ++idx)
2292 delete_instr(((isn_T *)gap->ga_data) + idx);
2293 ga_clear(gap);
2294}
2295
2296
2297#endif // defined(FEAT_EVAL)