blob: 88bd2ca42a3e36a0400249ed5b8d510e3e00d2fb [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/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000117 * Generate an ISN_CONSTRUCT instruction.
118 * The object will have "size" members.
119 */
120 int
121generate_CONSTRUCT(cctx_T *cctx, class_T *cl)
122{
123 isn_T *isn;
124
125 RETURN_OK_IF_SKIP(cctx);
126 if ((isn = generate_instr(cctx, ISN_CONSTRUCT)) == NULL)
127 return FAIL;
128 isn->isn_arg.construct.construct_size = sizeof(object_T)
129 + cl->class_obj_member_count * sizeof(typval_T);
130 isn->isn_arg.construct.construct_class = cl;
131 return OK;
132}
133
134/*
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000135 * Generate ISN_OBJ_MEMBER - access object member by indes.
136 */
137 int
138generate_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type)
139{
140 RETURN_OK_IF_SKIP(cctx);
141
142 // drop the object type
143 isn_T *isn = generate_instr_drop(cctx, ISN_OBJ_MEMBER, 1);
144 if (isn == NULL)
145 return FAIL;
146
147 isn->isn_arg.number = idx;
148 return push_type_stack2(cctx, type, &t_any);
149}
150
151/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000152 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
153 * But only for simple types.
154 * When "tolerant" is TRUE convert most types to string, e.g. a List.
155 */
156 int
157may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
158{
159 isn_T *isn;
160 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000161 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000162
163 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000164 type = get_type_on_stack(cctx, -1 - offset);
165 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000166 {
167 // nothing to be done
168 case VAR_STRING: return OK;
169
170 // conversion possible
171 case VAR_SPECIAL:
172 case VAR_BOOL:
173 case VAR_NUMBER:
174 case VAR_FLOAT:
175 break;
176
177 // conversion possible (with runtime check)
178 case VAR_ANY:
179 case VAR_UNKNOWN:
180 isntype = ISN_2STRING_ANY;
181 break;
182
183 // conversion possible when tolerant
184 case VAR_LIST:
185 if (tolerant)
186 {
187 isntype = ISN_2STRING_ANY;
188 break;
189 }
190 // FALLTHROUGH
191
192 // conversion not possible
193 case VAR_VOID:
194 case VAR_BLOB:
195 case VAR_FUNC:
196 case VAR_PARTIAL:
197 case VAR_DICT:
198 case VAR_JOB:
199 case VAR_CHANNEL:
200 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000201 case VAR_CLASS:
202 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000203 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000204 return FAIL;
205 }
206
Bram Moolenaar078a4612022-01-04 15:17:03 +0000207 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 if ((isn = generate_instr(cctx, isntype)) == NULL)
209 return FAIL;
210 isn->isn_arg.tostring.offset = offset;
211 isn->isn_arg.tostring.tolerant = tolerant;
212
213 return OK;
214}
215
216 static int
217check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
218{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000219 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
220 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000221 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000222 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000223 {
224 if (*op == '+')
225 emsg(_(e_wrong_argument_type_for_plus));
226 else
227 semsg(_(e_char_requires_number_or_float_arguments), *op);
228 return FAIL;
229 }
230 return OK;
231}
232
233/*
234 * Generate instruction for "+". For a list this creates a new list.
235 */
236 int
237generate_add_instr(
238 cctx_T *cctx,
239 vartype_T vartype,
240 type_T *type1,
241 type_T *type2,
242 exprtype_T expr_type)
243{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000244 isn_T *isn = generate_instr_drop(cctx,
245 vartype == VAR_NUMBER ? ISN_OPNR
246 : vartype == VAR_LIST ? ISN_ADDLIST
247 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000248 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000249 : ISN_OPANY, 1);
250
251 if (vartype != VAR_LIST && vartype != VAR_BLOB
252 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000253 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000254 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000255 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000256 && check_number_or_float(
257 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
258 return FAIL;
259
260 if (isn != NULL)
261 {
262 if (isn->isn_type == ISN_ADDLIST)
263 isn->isn_arg.op.op_type = expr_type;
264 else
265 isn->isn_arg.op.op_type = EXPR_ADD;
266 }
267
268 // When concatenating two lists with different member types the member type
269 // becomes "any".
270 if (vartype == VAR_LIST
271 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
272 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000273 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000274
275 return isn == NULL ? FAIL : OK;
276}
277
278/*
279 * Get the type to use for an instruction for an operation on "type1" and
280 * "type2". If they are matching use a type-specific instruction. Otherwise
281 * fall back to runtime type checking.
282 */
283 vartype_T
284operator_type(type_T *type1, type_T *type2)
285{
286 if (type1->tt_type == type2->tt_type
287 && (type1->tt_type == VAR_NUMBER
288 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000289 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000290 || type1->tt_type == VAR_BLOB))
291 return type1->tt_type;
292 return VAR_ANY;
293}
294
295/*
296 * Generate an instruction with two arguments. The instruction depends on the
297 * type of the arguments.
298 */
299 int
300generate_two_op(cctx_T *cctx, char_u *op)
301{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000302 type_T *type1;
303 type_T *type2;
304 vartype_T vartype;
305 isn_T *isn;
306
307 RETURN_OK_IF_SKIP(cctx);
308
309 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000310 type1 = get_type_on_stack(cctx, 1);
311 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000312 vartype = operator_type(type1, type2);
313
314 switch (*op)
315 {
316 case '+':
317 if (generate_add_instr(cctx, vartype, type1, type2,
318 EXPR_COPY) == FAIL)
319 return FAIL;
320 break;
321
322 case '-':
323 case '*':
324 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
325 op) == FAIL)
326 return FAIL;
327 if (vartype == VAR_NUMBER)
328 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000329 else if (vartype == VAR_FLOAT)
330 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000331 else
332 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
333 if (isn != NULL)
334 isn->isn_arg.op.op_type = *op == '*'
335 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
336 break;
337
338 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000339 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000340 && type1->tt_type != VAR_NUMBER)
341 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000342 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000343 && type2->tt_type != VAR_NUMBER))
344 {
345 emsg(_(e_percent_requires_number_arguments));
346 return FAIL;
347 }
348 isn = generate_instr_drop(cctx,
349 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
350 if (isn != NULL)
351 isn->isn_arg.op.op_type = EXPR_REM;
352 break;
353 }
354
355 // correct type of result
356 if (vartype == VAR_ANY)
357 {
358 type_T *type = &t_any;
359
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000360 // float+number and number+float results in float
361 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100362 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000363 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000364 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000365 }
366
367 return OK;
368}
369
370/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000371 * Get the instruction to use for comparing two values with specified types.
372 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000373 * Return ISN_DROP when failed.
374 */
375 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000376get_compare_isn(
377 exprtype_T exprtype,
378 typval_T *tv1,
379 typval_T *tv2,
380 type_T *type1,
381 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000382{
383 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000384 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
385 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000386
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000387 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000388 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000389 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000390 {
391 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
392 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
393 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
394 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
395 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
396 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
397 case VAR_LIST: isntype = ISN_COMPARELIST; break;
398 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
399 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
400 default: isntype = ISN_COMPAREANY; break;
401 }
402 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000403 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
404 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
405 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
406 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
407 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000408 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000409 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000410 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000411 if ((vartype1 == VAR_SPECIAL
412 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
413 : type1 == &t_none)
414 && vartype2 != VAR_STRING)
415 || (vartype2 == VAR_SPECIAL
416 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
417 : type2 == &t_none)
418 && vartype1 != VAR_STRING))
419 {
420 semsg(_(e_cannot_compare_str_with_str),
421 vartype_name(vartype1), vartype_name(vartype2));
422 return ISN_DROP;
423 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000424 // although comparing null with number, float or bool is not useful, we
425 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000426 isntype = ISN_COMPARENULL;
427 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000428
429 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
430 && (isntype == ISN_COMPAREBOOL
431 || isntype == ISN_COMPARESPECIAL
432 || isntype == ISN_COMPARENR
433 || isntype == ISN_COMPAREFLOAT))
434 {
435 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000436 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000437 return ISN_DROP;
438 }
439 if (isntype == ISN_DROP
440 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000441 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
442 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000444 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000445 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
446 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000447 {
448 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000449 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000450 return ISN_DROP;
451 }
452 return isntype;
453}
454
455 int
456check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
457{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000458 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000459 return FAIL;
460 return OK;
461}
462
463/*
464 * Generate an ISN_COMPARE* instruction with a boolean result.
465 */
466 int
467generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
468{
469 isntype_T isntype;
470 isn_T *isn;
471 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000472
473 RETURN_OK_IF_SKIP(cctx);
474
475 // Get the known type of the two items on the stack. If they are matching
476 // use a type-specific instruction. Otherwise fall back to runtime type
477 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000478 isntype = get_compare_isn(exprtype, NULL, NULL,
479 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000480 if (isntype == ISN_DROP)
481 return FAIL;
482
483 if ((isn = generate_instr(cctx, isntype)) == NULL)
484 return FAIL;
485 isn->isn_arg.op.op_type = exprtype;
486 isn->isn_arg.op.op_ic = ic;
487
488 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100489 --stack->ga_len;
490 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491
492 return OK;
493}
494
495/*
LemonBoy372bcce2022-04-25 12:43:20 +0100496 * Generate an ISN_CONCAT instruction.
497 * "count" is the number of stack elements to join together and it must be
498 * greater or equal to one.
499 * The caller ensures all the "count" elements on the stack have the right type.
500 */
501 int
502generate_CONCAT(cctx_T *cctx, int count)
503{
504 isn_T *isn;
505 garray_T *stack = &cctx->ctx_type_stack;
506
507 RETURN_OK_IF_SKIP(cctx);
508
LemonBoy372bcce2022-04-25 12:43:20 +0100509 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
510 return FAIL;
511 isn->isn_arg.number = count;
512
513 // drop the argument types
514 stack->ga_len -= count - 1;
515
516 return OK;
517}
518
519/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000520 * Generate an ISN_2BOOL instruction.
521 * "offset" is the offset in the type stack.
522 */
523 int
524generate_2BOOL(cctx_T *cctx, int invert, int offset)
525{
526 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000527
528 RETURN_OK_IF_SKIP(cctx);
529 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
530 return FAIL;
531 isn->isn_arg.tobool.invert = invert;
532 isn->isn_arg.tobool.offset = offset;
533
534 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000535 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000536
537 return OK;
538}
539
540/*
541 * Generate an ISN_COND2BOOL instruction.
542 */
543 int
544generate_COND2BOOL(cctx_T *cctx)
545{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000546 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100547 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000548 return FAIL;
549
550 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000551 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000552
553 return OK;
554}
555
556 int
557generate_TYPECHECK(
558 cctx_T *cctx,
559 type_T *expected,
560 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100561 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000562 int argidx)
563{
564 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000565
566 RETURN_OK_IF_SKIP(cctx);
567 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
568 return FAIL;
569 isn->isn_arg.type.ct_type = alloc_type(expected);
570 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100571 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000572 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
573
574 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000575 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000576
577 return OK;
578}
579
580 int
581generate_SETTYPE(
582 cctx_T *cctx,
583 type_T *expected)
584{
585 isn_T *isn;
586
587 RETURN_OK_IF_SKIP(cctx);
588 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
589 return FAIL;
590 isn->isn_arg.type.ct_type = alloc_type(expected);
591 return OK;
592}
593
594/*
595 * Generate a PUSH instruction for "tv".
596 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000597 */
598 int
599generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
600{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100601 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000602 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100603 case VAR_BOOL:
604 generate_PUSHBOOL(cctx, tv->vval.v_number);
605 break;
606 case VAR_SPECIAL:
607 generate_PUSHSPEC(cctx, tv->vval.v_number);
608 break;
609 case VAR_NUMBER:
610 generate_PUSHNR(cctx, tv->vval.v_number);
611 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100612 case VAR_FLOAT:
613 generate_PUSHF(cctx, tv->vval.v_float);
614 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100615 case VAR_BLOB:
616 generate_PUSHBLOB(cctx, tv->vval.v_blob);
617 tv->vval.v_blob = NULL;
618 break;
619 case VAR_LIST:
620 if (tv->vval.v_list != NULL)
621 iemsg("non-empty list constant not supported");
622 generate_NEWLIST(cctx, 0, TRUE);
623 break;
624 case VAR_DICT:
625 if (tv->vval.v_dict != NULL)
626 iemsg("non-empty dict constant not supported");
627 generate_NEWDICT(cctx, 0, TRUE);
628 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000629#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100630 case VAR_JOB:
631 if (tv->vval.v_job != NULL)
632 iemsg("non-null job constant not supported");
633 generate_PUSHJOB(cctx);
634 break;
635 case VAR_CHANNEL:
636 if (tv->vval.v_channel != NULL)
637 iemsg("non-null channel constant not supported");
638 generate_PUSHCHANNEL(cctx);
639 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000640#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100641 case VAR_FUNC:
642 if (tv->vval.v_string != NULL)
643 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100644 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100645 break;
646 case VAR_PARTIAL:
647 if (tv->vval.v_partial != NULL)
648 iemsg("non-null partial constant not supported");
649 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
650 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000651 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100652 break;
653 case VAR_STRING:
654 generate_PUSHS(cctx, &tv->vval.v_string);
655 tv->vval.v_string = NULL;
656 break;
657 default:
658 siemsg("constant type %d not supported", tv->v_type);
659 clear_tv(tv);
660 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000661 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100662 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000663 return OK;
664}
665
666/*
667 * Generate an ISN_PUSHNR instruction.
668 */
669 int
670generate_PUSHNR(cctx_T *cctx, varnumber_T number)
671{
672 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000673
674 RETURN_OK_IF_SKIP(cctx);
675 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
676 return FAIL;
677 isn->isn_arg.number = number;
678
679 if (number == 0 || number == 1)
680 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000681 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000682 return OK;
683}
684
685/*
686 * Generate an ISN_PUSHBOOL instruction.
687 */
688 int
689generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
690{
691 isn_T *isn;
692
693 RETURN_OK_IF_SKIP(cctx);
694 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
695 return FAIL;
696 isn->isn_arg.number = number;
697
698 return OK;
699}
700
701/*
702 * Generate an ISN_PUSHSPEC instruction.
703 */
704 int
705generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
706{
707 isn_T *isn;
708
709 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000710 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
711 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000712 return FAIL;
713 isn->isn_arg.number = number;
714
715 return OK;
716}
717
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000718/*
719 * Generate an ISN_PUSHF instruction.
720 */
721 int
722generate_PUSHF(cctx_T *cctx, float_T fnumber)
723{
724 isn_T *isn;
725
726 RETURN_OK_IF_SKIP(cctx);
727 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
728 return FAIL;
729 isn->isn_arg.fnumber = fnumber;
730
731 return OK;
732}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000733
734/*
735 * Generate an ISN_PUSHS instruction.
736 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100737 * Note that if "str" is used in the instruction OK is returned and "*str" is
738 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000739 */
740 int
741generate_PUSHS(cctx_T *cctx, char_u **str)
742{
743 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100744 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000745
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100746 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000747 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100748 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
749 ret = FAIL;
750 else
751 {
752 isn->isn_arg.string = str == NULL ? NULL : *str;
753 return OK;
754 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000755 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100756 if (str != NULL)
757 VIM_CLEAR(*str);
758 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759}
760
761/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000762 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000763 */
764 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000765generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000766{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000767 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100768#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100769 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000770 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000771 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100772#else
773 emsg(_(e_channel_job_feature_not_available));
774 return FAIL;
775#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000776}
777
778/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000779 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000780 */
781 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000782generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000783{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000784 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100785#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100786 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000787 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000788 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100789#else
790 emsg(_(e_channel_job_feature_not_available));
791 return FAIL;
792#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793}
794
795/*
796 * Generate an ISN_PUSHBLOB instruction.
797 * Consumes "blob".
798 */
799 int
800generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
801{
802 isn_T *isn;
803
804 RETURN_OK_IF_SKIP(cctx);
805 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
806 return FAIL;
807 isn->isn_arg.blob = blob;
808
809 return OK;
810}
811
812/*
813 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100814 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
815 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000816 */
817 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100818generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819{
820 isn_T *isn;
821 char_u *funcname;
822
823 RETURN_OK_IF_SKIP(cctx);
824 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
825 return FAIL;
826 if (name == NULL)
827 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100828 else if (!may_prefix
829 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000830 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000831 funcname = vim_strsave(name);
832 else
833 {
834 funcname = alloc(STRLEN(name) + 3);
835 if (funcname != NULL)
836 {
837 STRCPY(funcname, "g:");
838 STRCPY(funcname + 2, name);
839 }
840 }
841
842 isn->isn_arg.string = funcname;
843 return OK;
844}
845
846/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000847 * Generate an ISN_AUTOLOAD instruction.
848 */
849 int
850generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
851{
852 isn_T *isn;
853
854 RETURN_OK_IF_SKIP(cctx);
855 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
856 return FAIL;
857 isn->isn_arg.string = vim_strsave(name);
858 if (isn->isn_arg.string == NULL)
859 return FAIL;
860 return OK;
861}
862
863/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 * Generate an ISN_GETITEM instruction with "index".
865 * "with_op" is TRUE for "+=" and other operators, the stack has the current
866 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100867 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000868 */
869 int
870generate_GETITEM(cctx_T *cctx, int index, int with_op)
871{
872 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000873 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000874 type_T *item_type = &t_any;
875
876 RETURN_OK_IF_SKIP(cctx);
877
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000878 item_type = type->tt_member;
879 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
880 return FAIL;
881 isn->isn_arg.getitem.gi_index = index;
882 isn->isn_arg.getitem.gi_with_op = with_op;
883
884 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000885 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000886}
887
888/*
889 * Generate an ISN_SLICE instruction with "count".
890 */
891 int
892generate_SLICE(cctx_T *cctx, int count)
893{
894 isn_T *isn;
895
896 RETURN_OK_IF_SKIP(cctx);
897 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
898 return FAIL;
899 isn->isn_arg.number = count;
900 return OK;
901}
902
903/*
904 * Generate an ISN_CHECKLEN instruction with "min_len".
905 */
906 int
907generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
908{
909 isn_T *isn;
910
911 RETURN_OK_IF_SKIP(cctx);
912
913 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
914 return FAIL;
915 isn->isn_arg.checklen.cl_min_len = min_len;
916 isn->isn_arg.checklen.cl_more_OK = more_OK;
917
918 return OK;
919}
920
921/*
922 * Generate an ISN_STORE instruction.
923 */
924 int
925generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
926{
927 isn_T *isn;
928
929 RETURN_OK_IF_SKIP(cctx);
930 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
931 return FAIL;
932 if (name != NULL)
933 isn->isn_arg.string = vim_strsave(name);
934 else
935 isn->isn_arg.number = idx;
936
937 return OK;
938}
939
940/*
941 * Generate an ISN_STOREOUTER instruction.
942 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000943 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100944generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000945{
946 isn_T *isn;
947
948 RETURN_OK_IF_SKIP(cctx);
949 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
950 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100951 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
952 {
953 // Store a variable defined in a loop. A copy will be made at the end
954 // of the loop. TODO: how about deeper nesting?
955 isn->isn_arg.outer.outer_idx = idx - loop_idx;
956 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
957 }
958 else
959 {
960 isn->isn_arg.outer.outer_idx = idx;
961 isn->isn_arg.outer.outer_depth = level;
962 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000963
964 return OK;
965}
966
967/*
968 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
969 */
970 int
971generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
972{
973 isn_T *isn;
974
975 RETURN_OK_IF_SKIP(cctx);
976 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
977 return FAIL;
978 isn->isn_arg.storenr.stnr_idx = idx;
979 isn->isn_arg.storenr.stnr_val = value;
980
981 return OK;
982}
983
984/*
985 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
986 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000987 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000988generate_STOREOPT(
989 cctx_T *cctx,
990 isntype_T isn_type,
991 char_u *name,
992 int opt_flags)
993{
994 isn_T *isn;
995
996 RETURN_OK_IF_SKIP(cctx);
997 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
998 return FAIL;
999 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1000 isn->isn_arg.storeopt.so_flags = opt_flags;
1001
1002 return OK;
1003}
1004
1005/*
1006 * Generate an ISN_LOAD or similar instruction.
1007 */
1008 int
1009generate_LOAD(
1010 cctx_T *cctx,
1011 isntype_T isn_type,
1012 int idx,
1013 char_u *name,
1014 type_T *type)
1015{
1016 isn_T *isn;
1017
1018 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001019 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001020 return FAIL;
1021 if (name != NULL)
1022 isn->isn_arg.string = vim_strsave(name);
1023 else
1024 isn->isn_arg.number = idx;
1025
1026 return OK;
1027}
1028
1029/*
1030 * Generate an ISN_LOADOUTER instruction
1031 */
1032 int
1033generate_LOADOUTER(
1034 cctx_T *cctx,
1035 int idx,
1036 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001037 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001038 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001039 type_T *type)
1040{
1041 isn_T *isn;
1042
1043 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001044 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001045 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001046 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1047 {
1048 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001049 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001050 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001051 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001052 }
1053 else
1054 {
1055 isn->isn_arg.outer.outer_idx = idx;
1056 isn->isn_arg.outer.outer_depth = nesting;
1057 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001058
1059 return OK;
1060}
1061
1062/*
1063 * Generate an ISN_LOADV instruction for v:var.
1064 */
1065 int
1066generate_LOADV(
1067 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001068 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001069{
1070 int di_flags;
1071 int vidx = find_vim_var(name, &di_flags);
1072 type_T *type;
1073
1074 RETURN_OK_IF_SKIP(cctx);
1075 if (vidx < 0)
1076 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001077 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001078 return FAIL;
1079 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001080 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001081 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1082}
1083
1084/*
1085 * Generate an ISN_UNLET instruction.
1086 */
1087 int
1088generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1089{
1090 isn_T *isn;
1091
1092 RETURN_OK_IF_SKIP(cctx);
1093 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1094 return FAIL;
1095 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1096 isn->isn_arg.unlet.ul_forceit = forceit;
1097
1098 return OK;
1099}
1100
1101/*
1102 * Generate an ISN_LOCKCONST instruction.
1103 */
1104 int
1105generate_LOCKCONST(cctx_T *cctx)
1106{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001107 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001108 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001109 return FAIL;
1110 return OK;
1111}
1112
1113/*
1114 * Generate an ISN_LOADS instruction.
1115 */
1116 int
1117generate_OLDSCRIPT(
1118 cctx_T *cctx,
1119 isntype_T isn_type,
1120 char_u *name,
1121 int sid,
1122 type_T *type)
1123{
1124 isn_T *isn;
1125
1126 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001127 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001128 isn = generate_instr_type(cctx, isn_type, type);
1129 else
1130 isn = generate_instr_drop(cctx, isn_type, 1);
1131 if (isn == NULL)
1132 return FAIL;
1133 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1134 isn->isn_arg.loadstore.ls_sid = sid;
1135
1136 return OK;
1137}
1138
1139/*
1140 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1141 */
1142 int
1143generate_VIM9SCRIPT(
1144 cctx_T *cctx,
1145 isntype_T isn_type,
1146 int sid,
1147 int idx,
1148 type_T *type)
1149{
1150 isn_T *isn;
1151 scriptref_T *sref;
1152 scriptitem_T *si = SCRIPT_ITEM(sid);
1153
1154 RETURN_OK_IF_SKIP(cctx);
1155 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001156 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001157 else
1158 isn = generate_instr_drop(cctx, isn_type, 1);
1159 if (isn == NULL)
1160 return FAIL;
1161
1162 // This requires three arguments, which doesn't fit in an instruction, thus
1163 // we need to allocate a struct for this.
1164 sref = ALLOC_ONE(scriptref_T);
1165 if (sref == NULL)
1166 return FAIL;
1167 isn->isn_arg.script.scriptref = sref;
1168 sref->sref_sid = sid;
1169 sref->sref_idx = idx;
1170 sref->sref_seq = si->sn_script_seq;
1171 sref->sref_type = type;
1172 return OK;
1173}
1174
1175/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001176 * Generate an ISN_NEWLIST instruction for "count" items.
1177 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 */
1179 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001180generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001181{
1182 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001183 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001184 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001185 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001186
1187 RETURN_OK_IF_SKIP(cctx);
1188 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1189 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001190 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001191
Bram Moolenaar078a4612022-01-04 15:17:03 +00001192 // Get the member type and the declared member type from all the items on
1193 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001194 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001195 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001196 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001197
1198 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001199 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001200
1201 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001202 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001203}
1204
1205/*
1206 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001207 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001208 */
1209 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001210generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211{
1212 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001213 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001214 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001215 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001216
1217 RETURN_OK_IF_SKIP(cctx);
1218 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1219 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001220 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001221
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001222 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001223 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001224 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001225
1226 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001227 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001228
1229 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001230 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001231}
1232
1233/*
1234 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001235 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001236 * If variables were declared inside a loop "loop_var_idx" is the index of the
1237 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001238 */
1239 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001240generate_FUNCREF(
1241 cctx_T *cctx,
1242 ufunc_T *ufunc,
1243 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001244{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001245 isn_T *isn;
1246 type_T *type;
1247 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001248 loopvarinfo_T loopinfo;
1249 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001250
1251 RETURN_OK_IF_SKIP(cctx);
1252 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1253 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001254 if (isnp != NULL)
1255 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001256
Bram Moolenaarcc341812022-09-19 15:54:34 +01001257 has_vars = get_loop_var_info(cctx, &loopinfo);
1258 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001259 {
1260 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1261 if (extra == NULL)
1262 return FAIL;
1263 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001264 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001265 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001266 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001267 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001268 else
1269 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001270
1271 // Reserve an extra variable to keep track of the number of closures
1272 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001273 cctx->ctx_has_closure = 1;
1274
1275 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001276 // the nested context, including this one. But not a function defined at
1277 // the script level.
1278 if ((ufunc->uf_flags & FC_CLOSURE)
1279 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001280 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1281
Bram Moolenaar078a4612022-01-04 15:17:03 +00001282 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1283 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001284}
1285
1286/*
1287 * Generate an ISN_NEWFUNC instruction.
1288 * "lambda_name" and "func_name" must be in allocated memory and will be
1289 * consumed.
1290 */
1291 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001292generate_NEWFUNC(
1293 cctx_T *cctx,
1294 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001295 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001296{
1297 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001298 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001300 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001301 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001302 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1303 ret = FAIL;
1304 else
1305 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001306 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1307
1308 if (arg == NULL)
1309 ret = FAIL;
1310 else
1311 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001312 // Reserve an extra variable to keep track of the number of
1313 // closures created.
1314 cctx->ctx_has_closure = 1;
1315
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001316 isn->isn_arg.newfunc.nf_arg = arg;
1317 arg->nfa_lambda = lambda_name;
1318 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001319 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001320 return OK;
1321 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001322 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001323 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001324 vim_free(lambda_name);
1325 vim_free(func_name);
1326 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001327}
1328
1329/*
1330 * Generate an ISN_DEF instruction: list functions
1331 */
1332 int
1333generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1334{
1335 isn_T *isn;
1336
1337 RETURN_OK_IF_SKIP(cctx);
1338 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1339 return FAIL;
1340 if (len > 0)
1341 {
1342 isn->isn_arg.string = vim_strnsave(name, len);
1343 if (isn->isn_arg.string == NULL)
1344 return FAIL;
1345 }
1346 return OK;
1347}
1348
1349/*
1350 * Generate an ISN_JUMP instruction.
1351 */
1352 int
1353generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1354{
1355 isn_T *isn;
1356 garray_T *stack = &cctx->ctx_type_stack;
1357
1358 RETURN_OK_IF_SKIP(cctx);
1359 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1360 return FAIL;
1361 isn->isn_arg.jump.jump_when = when;
1362 isn->isn_arg.jump.jump_where = where;
1363
1364 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1365 --stack->ga_len;
1366
1367 return OK;
1368}
1369
1370/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001371 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1372 */
1373 int
1374generate_WHILE(cctx_T *cctx, int funcref_idx)
1375{
1376 isn_T *isn;
1377 garray_T *stack = &cctx->ctx_type_stack;
1378
1379 RETURN_OK_IF_SKIP(cctx);
1380 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1381 return FAIL;
1382 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1383 isn->isn_arg.whileloop.while_end = 0; // filled in later
1384
1385 if (stack->ga_len > 0)
1386 --stack->ga_len;
1387
1388 return OK;
1389}
1390
1391/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1393 */
1394 int
1395generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1396{
1397 isn_T *isn;
1398
1399 RETURN_OK_IF_SKIP(cctx);
1400 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1401 return FAIL;
1402 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1403 // jump_where is set later
1404 return OK;
1405}
1406
1407 int
1408generate_FOR(cctx_T *cctx, int loop_idx)
1409{
1410 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001411
1412 RETURN_OK_IF_SKIP(cctx);
1413 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1414 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001415 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001416
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001417 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001418 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001419}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001420
1421 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001422generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001423{
1424 isn_T *isn;
1425
1426 RETURN_OK_IF_SKIP(cctx);
1427 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1428 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001429 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1430 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1431 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001432 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001433 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001434 return OK;
1435}
1436
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437/*
1438 * Generate an ISN_TRYCONT instruction.
1439 */
1440 int
1441generate_TRYCONT(cctx_T *cctx, int levels, int where)
1442{
1443 isn_T *isn;
1444
1445 RETURN_OK_IF_SKIP(cctx);
1446 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1447 return FAIL;
1448 isn->isn_arg.trycont.tct_levels = levels;
1449 isn->isn_arg.trycont.tct_where = where;
1450
1451 return OK;
1452}
1453
Bram Moolenaar16900322022-09-08 19:51:45 +01001454/*
1455 * Check "argount" arguments and their types on the type stack.
1456 * Give an error and return FAIL if something is wrong.
1457 * When "method_call" is NULL no code is generated.
1458 */
1459 int
1460check_internal_func_args(
1461 cctx_T *cctx,
1462 int func_idx,
1463 int argcount,
1464 int method_call,
1465 type2_T **argtypes,
1466 type2_T *shuffled_argtypes)
1467{
1468 garray_T *stack = &cctx->ctx_type_stack;
1469 int argoff = check_internal_func(func_idx, argcount);
1470
1471 if (argoff < 0)
1472 return FAIL;
1473
1474 if (method_call && argoff > 1)
1475 {
1476 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1477
1478 if (isn == NULL)
1479 return FAIL;
1480 isn->isn_arg.shuffle.shfl_item = argcount;
1481 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1482 }
1483
1484 if (argcount > 0)
1485 {
1486 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1487
1488 // Check the types of the arguments.
1489 if (method_call && argoff > 1)
1490 {
1491 int i;
1492
1493 for (i = 0; i < argcount; ++i)
1494 shuffled_argtypes[i] = (i < argoff - 1)
1495 ? typep[i + 1]
1496 : (i == argoff - 1) ? typep[0] : typep[i];
1497 *argtypes = shuffled_argtypes;
1498 }
1499 else
1500 {
1501 int i;
1502
1503 for (i = 0; i < argcount; ++i)
1504 shuffled_argtypes[i] = typep[i];
1505 *argtypes = shuffled_argtypes;
1506 }
1507 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1508 cctx) == FAIL)
1509 return FAIL;
1510 }
1511 return OK;
1512}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001513
1514/*
1515 * Generate an ISN_BCALL instruction.
1516 * "method_call" is TRUE for "value->method()"
1517 * Return FAIL if the number of arguments is wrong.
1518 */
1519 int
1520generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1521{
1522 isn_T *isn;
1523 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001524 type2_T *argtypes = NULL;
1525 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1526 type2_T *maptype = NULL;
1527 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001528 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001529
1530 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001531
1532 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1533 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001534 return FAIL;
1535
Bram Moolenaar16900322022-09-08 19:51:45 +01001536 if (internal_func_is_map(func_idx))
1537 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001538
1539 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1540 return FAIL;
1541 isn->isn_arg.bfunc.cbf_idx = func_idx;
1542 isn->isn_arg.bfunc.cbf_argcount = argcount;
1543
1544 // Drop the argument types and push the return type.
1545 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001546 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1547 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001548 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001549
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001550 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1551 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001552 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001553 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001554
1555 return OK;
1556}
1557
1558/*
1559 * Generate an ISN_LISTAPPEND instruction. Works like add().
1560 * Argument count is already checked.
1561 */
1562 int
1563generate_LISTAPPEND(cctx_T *cctx)
1564{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001565 type_T *list_type;
1566 type_T *item_type;
1567 type_T *expected;
1568
1569 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001570 // For checking the item type we use the declared type of the list and the
1571 // current type of the added item, adding a string to [1, 2] is OK.
1572 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001573 if (arg_type_modifiable(list_type, 1) == FAIL)
1574 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001575 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576 expected = list_type->tt_member;
1577 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1578 return FAIL;
1579
1580 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1581 return FAIL;
1582
Bram Moolenaar078a4612022-01-04 15:17:03 +00001583 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001584 return OK;
1585}
1586
1587/*
1588 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1589 * Argument count is already checked.
1590 */
1591 int
1592generate_BLOBAPPEND(cctx_T *cctx)
1593{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001594 type_T *item_type;
1595
Bram Moolenaarfa103972022-09-29 19:14:42 +01001596 // Caller already checked that blob_type is a blob, check it is modifiable.
1597 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1598 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001599 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001600 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1601 return FAIL;
1602
1603 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1604 return FAIL;
1605
Bram Moolenaar078a4612022-01-04 15:17:03 +00001606 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001607 return OK;
1608}
1609
1610/*
1611 * Generate an ISN_DCALL or ISN_UCALL instruction.
1612 * Return FAIL if the number of arguments is wrong.
1613 */
1614 int
1615generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1616{
1617 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001618 int regular_args = ufunc->uf_args.ga_len;
1619 int argcount = pushed_argcount;
1620
1621 RETURN_OK_IF_SKIP(cctx);
1622 if (argcount > regular_args && !has_varargs(ufunc))
1623 {
1624 semsg(_(e_too_many_arguments_for_function_str),
1625 printable_func_name(ufunc));
1626 return FAIL;
1627 }
1628 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1629 {
1630 semsg(_(e_not_enough_arguments_for_function_str),
1631 printable_func_name(ufunc));
1632 return FAIL;
1633 }
1634
1635 if (ufunc->uf_def_status != UF_NOT_COMPILED
1636 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1637 {
1638 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001639 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001640
1641 for (i = 0; i < argcount; ++i)
1642 {
1643 type_T *expected;
1644 type_T *actual;
1645
Bram Moolenaar078a4612022-01-04 15:17:03 +00001646 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001647 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001648 && i >= regular_args - ufunc->uf_def_args.ga_len)
1649 {
1650 // assume v:none used for default argument value
1651 continue;
1652 }
1653 if (i < regular_args)
1654 {
1655 if (ufunc->uf_arg_types == NULL)
1656 continue;
1657 expected = ufunc->uf_arg_types[i];
1658 }
1659 else if (ufunc->uf_va_type == NULL
1660 || ufunc->uf_va_type == &t_list_any)
1661 // possibly a lambda or "...: any"
1662 expected = &t_any;
1663 else
1664 expected = ufunc->uf_va_type->tt_member;
1665 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1666 TRUE, FALSE) == FAIL)
1667 {
1668 arg_type_mismatch(expected, actual, i + 1);
1669 return FAIL;
1670 }
1671 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001672 compile_type = get_compile_type(ufunc);
1673 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001674 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001675 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001676 return FAIL;
1677 }
1678 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1679 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001680 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001681 ufunc->uf_name);
1682 return FAIL;
1683 }
1684
1685 if ((isn = generate_instr(cctx,
1686 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1687 : ISN_UCALL)) == NULL)
1688 return FAIL;
1689 if (isn->isn_type == ISN_DCALL)
1690 {
1691 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1692 isn->isn_arg.dfunc.cdf_argcount = argcount;
1693 }
1694 else
1695 {
1696 // A user function may be deleted and redefined later, can't use the
1697 // ufunc pointer, need to look it up again at runtime.
1698 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1699 isn->isn_arg.ufunc.cuf_argcount = argcount;
1700 }
1701
Bram Moolenaar078a4612022-01-04 15:17:03 +00001702 // drop the argument types
1703 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001704
Bram Moolenaar078a4612022-01-04 15:17:03 +00001705 // add return type
1706 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001707}
1708
1709/*
1710 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1711 */
1712 int
1713generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1714{
1715 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001716
1717 RETURN_OK_IF_SKIP(cctx);
1718 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1719 return FAIL;
1720 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1721 isn->isn_arg.ufunc.cuf_argcount = argcount;
1722
Bram Moolenaar078a4612022-01-04 15:17:03 +00001723 // drop the argument types
1724 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001725
Bram Moolenaar078a4612022-01-04 15:17:03 +00001726 // add return value
1727 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001728}
1729
1730/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001731 * Check the arguments of function "type" against the types on the stack.
1732 * Returns OK or FAIL;
1733 */
1734 int
1735check_func_args_from_type(
1736 cctx_T *cctx,
1737 type_T *type,
1738 int argcount,
1739 int at_top,
1740 char_u *name)
1741{
1742 if (type->tt_argcount != -1)
1743 {
1744 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1745
1746 if (argcount < type->tt_min_argcount - varargs)
1747 {
1748 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1749 return FAIL;
1750 }
1751 if (!varargs && argcount > type->tt_argcount)
1752 {
1753 emsg_funcname(e_too_many_arguments_for_function_str, name);
1754 return FAIL;
1755 }
1756 if (type->tt_args != NULL)
1757 {
1758 int i;
1759
1760 for (i = 0; i < argcount; ++i)
1761 {
1762 int offset = -argcount + i - (at_top ? 0 : 1);
1763 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1764 type_T *expected;
1765
1766 if (varargs && i >= type->tt_argcount - 1)
1767 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1768 else if (i >= type->tt_min_argcount
1769 && actual->tt_type == VAR_SPECIAL)
1770 expected = &t_any;
1771 else
1772 expected = type->tt_args[i];
1773 if (need_type(actual, expected, offset, i + 1,
1774 cctx, TRUE, FALSE) == FAIL)
1775 {
1776 arg_type_mismatch(expected, actual, i + 1);
1777 return FAIL;
1778 }
1779 }
1780 }
1781 }
1782
1783 return OK;
1784}
1785/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001786 * Generate an ISN_PCALL instruction.
1787 * "type" is the type of the FuncRef.
1788 */
1789 int
1790generate_PCALL(
1791 cctx_T *cctx,
1792 int argcount,
1793 char_u *name,
1794 type_T *type,
1795 int at_top)
1796{
1797 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001798 type_T *ret_type;
1799
1800 RETURN_OK_IF_SKIP(cctx);
1801
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001802 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001803 ret_type = &t_any;
1804 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1805 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001806 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1807 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001808
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001809 ret_type = type->tt_member;
1810 if (ret_type == &t_unknown)
1811 // return type not known yet, use a runtime check
1812 ret_type = &t_any;
1813 }
1814 else
1815 {
1816 semsg(_(e_not_callable_type_str), name);
1817 return FAIL;
1818 }
1819
1820 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1821 return FAIL;
1822 isn->isn_arg.pfunc.cpf_top = at_top;
1823 isn->isn_arg.pfunc.cpf_argcount = argcount;
1824
Bram Moolenaar078a4612022-01-04 15:17:03 +00001825 // drop the arguments and the funcref/partial
1826 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001827
Bram Moolenaar078a4612022-01-04 15:17:03 +00001828 // push the return value
1829 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001830
1831 // If partial is above the arguments it must be cleared and replaced with
1832 // the return value.
1833 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1834 return FAIL;
1835
1836 return OK;
1837}
1838
1839/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001840 * Generate an ISN_DEFER instruction.
1841 */
1842 int
1843generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1844{
1845 isn_T *isn;
1846
1847 RETURN_OK_IF_SKIP(cctx);
1848 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1849 return FAIL;
1850 isn->isn_arg.defer.defer_var_idx = var_idx;
1851 isn->isn_arg.defer.defer_argcount = argcount;
1852 return OK;
1853}
1854
1855/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856 * Generate an ISN_STRINGMEMBER instruction.
1857 */
1858 int
1859generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1860{
1861 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001862 type_T *type;
1863
1864 RETURN_OK_IF_SKIP(cctx);
1865 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1866 return FAIL;
1867 isn->isn_arg.string = vim_strnsave(name, len);
1868
1869 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001870 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001871 if (type->tt_type != VAR_DICT
1872 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001873 {
1874 char *tofree;
1875
1876 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1877 name, type_name(type, &tofree));
1878 vim_free(tofree);
1879 return FAIL;
1880 }
1881 // change dict type to dict member type
1882 if (type->tt_type == VAR_DICT)
1883 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001884 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00001885 ? &t_any : type->tt_member;
1886 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001887 }
1888
1889 return OK;
1890}
1891
1892/*
1893 * Generate an ISN_ECHO instruction.
1894 */
1895 int
1896generate_ECHO(cctx_T *cctx, int with_white, int count)
1897{
1898 isn_T *isn;
1899
1900 RETURN_OK_IF_SKIP(cctx);
1901 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1902 return FAIL;
1903 isn->isn_arg.echo.echo_with_white = with_white;
1904 isn->isn_arg.echo.echo_count = count;
1905
1906 return OK;
1907}
1908
1909/*
1910 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1911 */
1912 int
1913generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1914{
1915 isn_T *isn;
1916
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01001917 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001918 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1919 return FAIL;
1920 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001921 return OK;
1922}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001924/*
1925 * Generate an ISN_ECHOWINDOW instruction
1926 */
1927 int
1928generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
1929{
1930 isn_T *isn;
1931
1932 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
1933 return FAIL;
1934 isn->isn_arg.echowin.ewin_count = count;
1935 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001936 return OK;
1937}
1938
1939/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001940 * Generate an ISN_SOURCE instruction.
1941 */
1942 int
1943generate_SOURCE(cctx_T *cctx, int sid)
1944{
1945 isn_T *isn;
1946
1947 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1948 return FAIL;
1949 isn->isn_arg.number = sid;
1950
1951 return OK;
1952}
1953
1954/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955 * Generate an ISN_PUT instruction.
1956 */
1957 int
1958generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1959{
1960 isn_T *isn;
1961
1962 RETURN_OK_IF_SKIP(cctx);
1963 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1964 return FAIL;
1965 isn->isn_arg.put.put_regname = regname;
1966 isn->isn_arg.put.put_lnum = lnum;
1967 return OK;
1968}
1969
1970/*
1971 * Generate an EXEC instruction that takes a string argument.
1972 * A copy is made of "line".
1973 */
1974 int
1975generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1976{
1977 isn_T *isn;
1978
1979 RETURN_OK_IF_SKIP(cctx);
1980 if ((isn = generate_instr(cctx, isntype)) == NULL)
1981 return FAIL;
1982 isn->isn_arg.string = vim_strsave(line);
1983 return OK;
1984}
1985
1986/*
1987 * Generate an EXEC instruction that takes a string argument.
1988 * "str" must be allocated, it is consumed.
1989 */
1990 int
1991generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1992{
1993 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001994 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001995
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001996 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001997 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001998 if ((isn = generate_instr(cctx, isntype)) == NULL)
1999 ret = FAIL;
2000 else
2001 {
2002 isn->isn_arg.string = str;
2003 return OK;
2004 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002005 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002006 vim_free(str);
2007 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002008}
2009
2010 int
2011generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2012{
2013 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002014
2015 RETURN_OK_IF_SKIP(cctx);
2016 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2017 return FAIL;
2018 isn->isn_arg.string = vim_strsave(line);
2019
Bram Moolenaar078a4612022-01-04 15:17:03 +00002020 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002021}
2022
2023 int
2024generate_EXECCONCAT(cctx_T *cctx, int count)
2025{
2026 isn_T *isn;
2027
2028 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2029 return FAIL;
2030 isn->isn_arg.number = count;
2031 return OK;
2032}
2033
2034/*
2035 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2036 */
2037 int
2038generate_RANGE(cctx_T *cctx, char_u *range)
2039{
2040 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002041
2042 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2043 return FAIL;
2044 isn->isn_arg.string = range;
2045
Bram Moolenaar078a4612022-01-04 15:17:03 +00002046 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002047}
2048
2049 int
2050generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2051{
2052 isn_T *isn;
2053
2054 RETURN_OK_IF_SKIP(cctx);
2055 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2056 return FAIL;
2057 isn->isn_arg.unpack.unp_count = var_count;
2058 isn->isn_arg.unpack.unp_semicolon = semicolon;
2059 return OK;
2060}
2061
2062/*
2063 * Generate an instruction for any command modifiers.
2064 */
2065 int
2066generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2067{
2068 isn_T *isn;
2069
2070 if (has_cmdmod(cmod, FALSE))
2071 {
2072 cctx->ctx_has_cmdmod = TRUE;
2073
2074 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2075 return FAIL;
2076 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2077 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2078 return FAIL;
2079 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2080 // filter program now belongs to the instruction
2081 cmod->cmod_filter_regmatch.regprog = NULL;
2082 }
2083
2084 return OK;
2085}
2086
2087 int
2088generate_undo_cmdmods(cctx_T *cctx)
2089{
2090 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2091 return FAIL;
2092 cctx->ctx_has_cmdmod = FALSE;
2093 return OK;
2094}
2095
2096/*
2097 * Generate a STORE instruction for "dest", not being "dest_local".
2098 * Return FAIL when out of memory.
2099 */
2100 int
2101generate_store_var(
2102 cctx_T *cctx,
2103 assign_dest_T dest,
2104 int opt_flags,
2105 int vimvaridx,
2106 int scriptvar_idx,
2107 int scriptvar_sid,
2108 type_T *type,
2109 char_u *name)
2110{
2111 switch (dest)
2112 {
2113 case dest_option:
2114 return generate_STOREOPT(cctx, ISN_STOREOPT,
2115 skip_option_env_lead(name), opt_flags);
2116 case dest_func_option:
2117 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2118 skip_option_env_lead(name), opt_flags);
2119 case dest_global:
2120 // include g: with the name, easier to execute that way
2121 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2122 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2123 case dest_buffer:
2124 // include b: with the name, easier to execute that way
2125 return generate_STORE(cctx, ISN_STOREB, 0, name);
2126 case dest_window:
2127 // include w: with the name, easier to execute that way
2128 return generate_STORE(cctx, ISN_STOREW, 0, name);
2129 case dest_tab:
2130 // include t: with the name, easier to execute that way
2131 return generate_STORE(cctx, ISN_STORET, 0, name);
2132 case dest_env:
2133 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2134 case dest_reg:
2135 return generate_STORE(cctx, ISN_STOREREG,
2136 name[1] == '@' ? '"' : name[1], NULL);
2137 case dest_vimvar:
2138 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2139 case dest_script:
2140 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002141 {
2142 isntype_T isn_type = ISN_STORES;
2143
2144 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01002145 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2146 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2147 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002148 {
2149 // "import autoload './dir/script.vim'" - load script first
2150 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2151 return FAIL;
2152 isn_type = ISN_STOREEXPORT;
2153 }
2154
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002155 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002156 return generate_OLDSCRIPT(cctx, isn_type, name,
2157 scriptvar_sid, type);
2158 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002159 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
2160 scriptvar_sid, scriptvar_idx, type);
2161 case dest_local:
2162 case dest_expr:
2163 // cannot happen
2164 break;
2165 }
2166 return FAIL;
2167}
2168
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002169/*
2170 * Return TRUE when inside a "for" or "while" loop.
2171 */
2172 int
2173inside_loop_scope(cctx_T *cctx)
2174{
2175 scope_T *scope = cctx->ctx_scope;
2176
2177 for (;;)
2178 {
2179 if (scope == NULL)
2180 break;
2181 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2182 return TRUE;
2183 scope = scope->se_outer;
2184 }
2185 return FALSE;
2186}
2187
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002188 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002189generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002190{
2191 if (lhs->lhs_dest != dest_local)
2192 return generate_store_var(cctx, lhs->lhs_dest,
2193 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2194 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2195 lhs->lhs_type, lhs->lhs_name);
2196
2197 if (lhs->lhs_lvar != NULL)
2198 {
2199 garray_T *instr = &cctx->ctx_instr;
2200 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2201
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002202 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2203 // ISN_STORENR.
2204 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002205 if (lhs->lhs_lvar->lv_from_outer == 0
2206 && instr->ga_len == instr_count + 1
2207 && isn->isn_type == ISN_PUSHNR)
2208 {
2209 varnumber_T val = isn->isn_arg.number;
2210 garray_T *stack = &cctx->ctx_type_stack;
2211
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002212 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002213 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002214 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002215 --instr->ga_len;
2216 }
2217 else
2218 {
2219 isn->isn_type = ISN_STORENR;
2220 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2221 isn->isn_arg.storenr.stnr_val = val;
2222 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002223 if (stack->ga_len > 0)
2224 --stack->ga_len;
2225 }
2226 else if (lhs->lhs_lvar->lv_from_outer > 0)
2227 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002228 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229 else
2230 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2231 }
2232 return OK;
2233}
2234
2235#if defined(FEAT_PROFILE) || defined(PROTO)
2236 void
2237may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2238{
2239 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2240 generate_instr(cctx, ISN_PROF_END);
2241}
2242#endif
2243
2244
2245/*
2246 * Delete an instruction, free what it contains.
2247 */
2248 void
2249delete_instr(isn_T *isn)
2250{
2251 switch (isn->isn_type)
2252 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002253 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002254 case ISN_DEF:
2255 case ISN_EXEC:
2256 case ISN_EXECRANGE:
2257 case ISN_EXEC_SPLIT:
2258 case ISN_LEGACY_EVAL:
2259 case ISN_LOADAUTO:
2260 case ISN_LOADB:
2261 case ISN_LOADENV:
2262 case ISN_LOADG:
2263 case ISN_LOADOPT:
2264 case ISN_LOADT:
2265 case ISN_LOADW:
2266 case ISN_LOCKUNLOCK:
2267 case ISN_PUSHEXC:
2268 case ISN_PUSHFUNC:
2269 case ISN_PUSHS:
2270 case ISN_RANGE:
2271 case ISN_STOREAUTO:
2272 case ISN_STOREB:
2273 case ISN_STOREENV:
2274 case ISN_STOREG:
2275 case ISN_STORET:
2276 case ISN_STOREW:
2277 case ISN_STRINGMEMBER:
2278 vim_free(isn->isn_arg.string);
2279 break;
2280
2281 case ISN_SUBSTITUTE:
2282 {
2283 int idx;
2284 isn_T *list = isn->isn_arg.subs.subs_instr;
2285
2286 vim_free(isn->isn_arg.subs.subs_cmd);
2287 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2288 delete_instr(list + idx);
2289 vim_free(list);
2290 }
2291 break;
2292
2293 case ISN_INSTR:
2294 {
2295 int idx;
2296 isn_T *list = isn->isn_arg.instr;
2297
2298 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2299 delete_instr(list + idx);
2300 vim_free(list);
2301 }
2302 break;
2303
2304 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002305 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002306 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002307 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002308 vim_free(isn->isn_arg.loadstore.ls_name);
2309 break;
2310
2311 case ISN_UNLET:
2312 case ISN_UNLETENV:
2313 vim_free(isn->isn_arg.unlet.ul_name);
2314 break;
2315
2316 case ISN_STOREOPT:
2317 case ISN_STOREFUNCOPT:
2318 vim_free(isn->isn_arg.storeopt.so_name);
2319 break;
2320
2321 case ISN_PUSHBLOB: // push blob isn_arg.blob
2322 blob_unref(isn->isn_arg.blob);
2323 break;
2324
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002325 case ISN_UCALL:
2326 vim_free(isn->isn_arg.ufunc.cuf_name);
2327 break;
2328
2329 case ISN_FUNCREF:
2330 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002331 funcref_T *funcref = &isn->isn_arg.funcref;
2332 funcref_extra_T *extra = funcref->fr_extra;
2333
2334 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002335 {
2336 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002337 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002338 ufunc_T *ufunc = dfunc->df_ufunc;
2339
2340 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2341 func_ptr_unref(ufunc);
2342 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002343 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002344 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002345 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002346
2347 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002348 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002349 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002350 vim_free(name);
2351 }
2352 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002353 }
2354 }
2355 break;
2356
2357 case ISN_DCALL:
2358 {
2359 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002360 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002361
2362 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002363 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002364 func_ptr_unref(dfunc->df_ufunc);
2365 }
2366 break;
2367
2368 case ISN_NEWFUNC:
2369 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002370 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002371
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002372 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002373 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002374 ufunc_T *ufunc = find_func_even_dead(
2375 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002376
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002377 if (ufunc != NULL)
2378 {
2379 unlink_def_function(ufunc);
2380 func_ptr_unref(ufunc);
2381 }
2382
2383 vim_free(arg->nfa_lambda);
2384 vim_free(arg->nfa_global);
2385 vim_free(arg);
2386 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002387 }
2388 break;
2389
2390 case ISN_CHECKTYPE:
2391 case ISN_SETTYPE:
2392 free_type(isn->isn_arg.type.ct_type);
2393 break;
2394
2395 case ISN_CMDMOD:
2396 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002397 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002398 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2399 break;
2400
2401 case ISN_LOADSCRIPT:
2402 case ISN_STORESCRIPT:
2403 vim_free(isn->isn_arg.script.scriptref);
2404 break;
2405
2406 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002407 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002408 break;
2409
2410 case ISN_CEXPR_CORE:
2411 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2412 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2413 break;
2414
2415 case ISN_2BOOL:
2416 case ISN_2STRING:
2417 case ISN_2STRING_ANY:
2418 case ISN_ADDBLOB:
2419 case ISN_ADDLIST:
2420 case ISN_ANYINDEX:
2421 case ISN_ANYSLICE:
2422 case ISN_BCALL:
2423 case ISN_BLOBAPPEND:
2424 case ISN_BLOBINDEX:
2425 case ISN_BLOBSLICE:
2426 case ISN_CATCH:
2427 case ISN_CEXPR_AUCMD:
2428 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002429 case ISN_CLEARDICT:
2430 case ISN_CMDMOD_REV:
2431 case ISN_COMPAREANY:
2432 case ISN_COMPAREBLOB:
2433 case ISN_COMPAREBOOL:
2434 case ISN_COMPAREDICT:
2435 case ISN_COMPAREFLOAT:
2436 case ISN_COMPAREFUNC:
2437 case ISN_COMPARELIST:
2438 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002439 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002440 case ISN_COMPARESPECIAL:
2441 case ISN_COMPARESTRING:
2442 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002443 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 case ISN_COND2BOOL:
2445 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002446 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002447 case ISN_DROP:
2448 case ISN_ECHO:
2449 case ISN_ECHOCONSOLE:
2450 case ISN_ECHOERR:
2451 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002452 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002453 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002454 case ISN_ENDTRY:
2455 case ISN_EXECCONCAT:
2456 case ISN_EXECUTE:
2457 case ISN_FINALLY:
2458 case ISN_FINISH:
2459 case ISN_FOR:
2460 case ISN_GETITEM:
2461 case ISN_JUMP:
2462 case ISN_JUMP_IF_ARG_SET:
2463 case ISN_LISTAPPEND:
2464 case ISN_LISTINDEX:
2465 case ISN_LISTSLICE:
2466 case ISN_LOAD:
2467 case ISN_LOADBDICT:
2468 case ISN_LOADGDICT:
2469 case ISN_LOADOUTER:
2470 case ISN_LOADREG:
2471 case ISN_LOADTDICT:
2472 case ISN_LOADV:
2473 case ISN_LOADWDICT:
2474 case ISN_LOCKCONST:
2475 case ISN_MEMBER:
2476 case ISN_NEGATENR:
2477 case ISN_NEWDICT:
2478 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002479 case ISN_NEWPARTIAL:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002480 case ISN_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002481 case ISN_OPANY:
2482 case ISN_OPFLOAT:
2483 case ISN_OPNR:
2484 case ISN_PCALL:
2485 case ISN_PCALL_END:
2486 case ISN_PROF_END:
2487 case ISN_PROF_START:
2488 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002489 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002490 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002491 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002492 case ISN_PUSHNR:
2493 case ISN_PUSHSPEC:
2494 case ISN_PUT:
2495 case ISN_REDIREND:
2496 case ISN_REDIRSTART:
2497 case ISN_RETURN:
2498 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002499 case ISN_RETURN_OBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002500 case ISN_SHUFFLE:
2501 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002502 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002503 case ISN_STORE:
2504 case ISN_STOREINDEX:
2505 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002506 case ISN_STOREOUTER:
2507 case ISN_STORERANGE:
2508 case ISN_STOREREG:
2509 case ISN_STOREV:
2510 case ISN_STRINDEX:
2511 case ISN_STRSLICE:
2512 case ISN_THROW:
2513 case ISN_TRYCONT:
2514 case ISN_UNLETINDEX:
2515 case ISN_UNLETRANGE:
2516 case ISN_UNPACK:
2517 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002518 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002519 // nothing allocated
2520 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002521 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002522}
2523
2524 void
2525clear_instr_ga(garray_T *gap)
2526{
2527 int idx;
2528
2529 for (idx = 0; idx < gap->ga_len; ++idx)
2530 delete_instr(((isn_T *)gap->ga_data) + idx);
2531 ga_clear(gap);
2532}
2533
2534
2535#endif // defined(FEAT_EVAL)