blob: cd5597db6417061bc29ad9f3b1b60729ff70d05a [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 Moolenaardc7c3662021-12-20 15:04:29 +0000135 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
136 * But only for simple types.
137 * When "tolerant" is TRUE convert most types to string, e.g. a List.
138 */
139 int
140may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
141{
142 isn_T *isn;
143 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000144 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000145
146 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000147 type = get_type_on_stack(cctx, -1 - offset);
148 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000149 {
150 // nothing to be done
151 case VAR_STRING: return OK;
152
153 // conversion possible
154 case VAR_SPECIAL:
155 case VAR_BOOL:
156 case VAR_NUMBER:
157 case VAR_FLOAT:
158 break;
159
160 // conversion possible (with runtime check)
161 case VAR_ANY:
162 case VAR_UNKNOWN:
163 isntype = ISN_2STRING_ANY;
164 break;
165
166 // conversion possible when tolerant
167 case VAR_LIST:
168 if (tolerant)
169 {
170 isntype = ISN_2STRING_ANY;
171 break;
172 }
173 // FALLTHROUGH
174
175 // conversion not possible
176 case VAR_VOID:
177 case VAR_BLOB:
178 case VAR_FUNC:
179 case VAR_PARTIAL:
180 case VAR_DICT:
181 case VAR_JOB:
182 case VAR_CHANNEL:
183 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000184 case VAR_CLASS:
185 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000186 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000187 return FAIL;
188 }
189
Bram Moolenaar078a4612022-01-04 15:17:03 +0000190 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000191 if ((isn = generate_instr(cctx, isntype)) == NULL)
192 return FAIL;
193 isn->isn_arg.tostring.offset = offset;
194 isn->isn_arg.tostring.tolerant = tolerant;
195
196 return OK;
197}
198
199 static int
200check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
201{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000202 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
203 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000204 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000205 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000206 {
207 if (*op == '+')
208 emsg(_(e_wrong_argument_type_for_plus));
209 else
210 semsg(_(e_char_requires_number_or_float_arguments), *op);
211 return FAIL;
212 }
213 return OK;
214}
215
216/*
217 * Generate instruction for "+". For a list this creates a new list.
218 */
219 int
220generate_add_instr(
221 cctx_T *cctx,
222 vartype_T vartype,
223 type_T *type1,
224 type_T *type2,
225 exprtype_T expr_type)
226{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000227 isn_T *isn = generate_instr_drop(cctx,
228 vartype == VAR_NUMBER ? ISN_OPNR
229 : vartype == VAR_LIST ? ISN_ADDLIST
230 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000231 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000232 : ISN_OPANY, 1);
233
234 if (vartype != VAR_LIST && vartype != VAR_BLOB
235 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000236 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000237 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000238 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000239 && check_number_or_float(
240 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
241 return FAIL;
242
243 if (isn != NULL)
244 {
245 if (isn->isn_type == ISN_ADDLIST)
246 isn->isn_arg.op.op_type = expr_type;
247 else
248 isn->isn_arg.op.op_type = EXPR_ADD;
249 }
250
251 // When concatenating two lists with different member types the member type
252 // becomes "any".
253 if (vartype == VAR_LIST
254 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
255 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000256 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000257
258 return isn == NULL ? FAIL : OK;
259}
260
261/*
262 * Get the type to use for an instruction for an operation on "type1" and
263 * "type2". If they are matching use a type-specific instruction. Otherwise
264 * fall back to runtime type checking.
265 */
266 vartype_T
267operator_type(type_T *type1, type_T *type2)
268{
269 if (type1->tt_type == type2->tt_type
270 && (type1->tt_type == VAR_NUMBER
271 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000272 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000273 || type1->tt_type == VAR_BLOB))
274 return type1->tt_type;
275 return VAR_ANY;
276}
277
278/*
279 * Generate an instruction with two arguments. The instruction depends on the
280 * type of the arguments.
281 */
282 int
283generate_two_op(cctx_T *cctx, char_u *op)
284{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000285 type_T *type1;
286 type_T *type2;
287 vartype_T vartype;
288 isn_T *isn;
289
290 RETURN_OK_IF_SKIP(cctx);
291
292 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000293 type1 = get_type_on_stack(cctx, 1);
294 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000295 vartype = operator_type(type1, type2);
296
297 switch (*op)
298 {
299 case '+':
300 if (generate_add_instr(cctx, vartype, type1, type2,
301 EXPR_COPY) == FAIL)
302 return FAIL;
303 break;
304
305 case '-':
306 case '*':
307 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
308 op) == FAIL)
309 return FAIL;
310 if (vartype == VAR_NUMBER)
311 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000312 else if (vartype == VAR_FLOAT)
313 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000314 else
315 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
316 if (isn != NULL)
317 isn->isn_arg.op.op_type = *op == '*'
318 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
319 break;
320
321 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000322 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000323 && type1->tt_type != VAR_NUMBER)
324 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000325 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000326 && type2->tt_type != VAR_NUMBER))
327 {
328 emsg(_(e_percent_requires_number_arguments));
329 return FAIL;
330 }
331 isn = generate_instr_drop(cctx,
332 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
333 if (isn != NULL)
334 isn->isn_arg.op.op_type = EXPR_REM;
335 break;
336 }
337
338 // correct type of result
339 if (vartype == VAR_ANY)
340 {
341 type_T *type = &t_any;
342
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000343 // float+number and number+float results in float
344 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100345 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000346 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000347 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000348 }
349
350 return OK;
351}
352
353/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000354 * Get the instruction to use for comparing two values with specified types.
355 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000356 * Return ISN_DROP when failed.
357 */
358 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000359get_compare_isn(
360 exprtype_T exprtype,
361 typval_T *tv1,
362 typval_T *tv2,
363 type_T *type1,
364 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000365{
366 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000367 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
368 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000369
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000370 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000371 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000372 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000373 {
374 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
375 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
376 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
377 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
378 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
379 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
380 case VAR_LIST: isntype = ISN_COMPARELIST; break;
381 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
382 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
383 default: isntype = ISN_COMPAREANY; break;
384 }
385 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000386 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
387 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
388 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
389 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
390 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000391 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000392 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000393 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000394 if ((vartype1 == VAR_SPECIAL
395 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
396 : type1 == &t_none)
397 && vartype2 != VAR_STRING)
398 || (vartype2 == VAR_SPECIAL
399 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
400 : type2 == &t_none)
401 && vartype1 != VAR_STRING))
402 {
403 semsg(_(e_cannot_compare_str_with_str),
404 vartype_name(vartype1), vartype_name(vartype2));
405 return ISN_DROP;
406 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000407 // although comparing null with number, float or bool is not useful, we
408 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000409 isntype = ISN_COMPARENULL;
410 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000411
412 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
413 && (isntype == ISN_COMPAREBOOL
414 || isntype == ISN_COMPARESPECIAL
415 || isntype == ISN_COMPARENR
416 || isntype == ISN_COMPAREFLOAT))
417 {
418 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000419 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000420 return ISN_DROP;
421 }
422 if (isntype == ISN_DROP
423 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000424 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
425 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000426 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000427 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000428 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
429 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000430 {
431 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000432 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000433 return ISN_DROP;
434 }
435 return isntype;
436}
437
438 int
439check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
440{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000441 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000442 return FAIL;
443 return OK;
444}
445
446/*
447 * Generate an ISN_COMPARE* instruction with a boolean result.
448 */
449 int
450generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
451{
452 isntype_T isntype;
453 isn_T *isn;
454 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000455
456 RETURN_OK_IF_SKIP(cctx);
457
458 // Get the known type of the two items on the stack. If they are matching
459 // use a type-specific instruction. Otherwise fall back to runtime type
460 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000461 isntype = get_compare_isn(exprtype, NULL, NULL,
462 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000463 if (isntype == ISN_DROP)
464 return FAIL;
465
466 if ((isn = generate_instr(cctx, isntype)) == NULL)
467 return FAIL;
468 isn->isn_arg.op.op_type = exprtype;
469 isn->isn_arg.op.op_ic = ic;
470
471 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100472 --stack->ga_len;
473 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000474
475 return OK;
476}
477
478/*
LemonBoy372bcce2022-04-25 12:43:20 +0100479 * Generate an ISN_CONCAT instruction.
480 * "count" is the number of stack elements to join together and it must be
481 * greater or equal to one.
482 * The caller ensures all the "count" elements on the stack have the right type.
483 */
484 int
485generate_CONCAT(cctx_T *cctx, int count)
486{
487 isn_T *isn;
488 garray_T *stack = &cctx->ctx_type_stack;
489
490 RETURN_OK_IF_SKIP(cctx);
491
LemonBoy372bcce2022-04-25 12:43:20 +0100492 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
493 return FAIL;
494 isn->isn_arg.number = count;
495
496 // drop the argument types
497 stack->ga_len -= count - 1;
498
499 return OK;
500}
501
502/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000503 * Generate an ISN_2BOOL instruction.
504 * "offset" is the offset in the type stack.
505 */
506 int
507generate_2BOOL(cctx_T *cctx, int invert, int offset)
508{
509 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000510
511 RETURN_OK_IF_SKIP(cctx);
512 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
513 return FAIL;
514 isn->isn_arg.tobool.invert = invert;
515 isn->isn_arg.tobool.offset = offset;
516
517 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000518 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000519
520 return OK;
521}
522
523/*
524 * Generate an ISN_COND2BOOL instruction.
525 */
526 int
527generate_COND2BOOL(cctx_T *cctx)
528{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000529 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100530 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000531 return FAIL;
532
533 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000534 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000535
536 return OK;
537}
538
539 int
540generate_TYPECHECK(
541 cctx_T *cctx,
542 type_T *expected,
543 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100544 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000545 int argidx)
546{
547 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000548
549 RETURN_OK_IF_SKIP(cctx);
550 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
551 return FAIL;
552 isn->isn_arg.type.ct_type = alloc_type(expected);
553 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100554 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000555 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
556
557 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000558 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000559
560 return OK;
561}
562
563 int
564generate_SETTYPE(
565 cctx_T *cctx,
566 type_T *expected)
567{
568 isn_T *isn;
569
570 RETURN_OK_IF_SKIP(cctx);
571 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
572 return FAIL;
573 isn->isn_arg.type.ct_type = alloc_type(expected);
574 return OK;
575}
576
577/*
578 * Generate a PUSH instruction for "tv".
579 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000580 */
581 int
582generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
583{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100584 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000585 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100586 case VAR_BOOL:
587 generate_PUSHBOOL(cctx, tv->vval.v_number);
588 break;
589 case VAR_SPECIAL:
590 generate_PUSHSPEC(cctx, tv->vval.v_number);
591 break;
592 case VAR_NUMBER:
593 generate_PUSHNR(cctx, tv->vval.v_number);
594 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100595 case VAR_FLOAT:
596 generate_PUSHF(cctx, tv->vval.v_float);
597 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100598 case VAR_BLOB:
599 generate_PUSHBLOB(cctx, tv->vval.v_blob);
600 tv->vval.v_blob = NULL;
601 break;
602 case VAR_LIST:
603 if (tv->vval.v_list != NULL)
604 iemsg("non-empty list constant not supported");
605 generate_NEWLIST(cctx, 0, TRUE);
606 break;
607 case VAR_DICT:
608 if (tv->vval.v_dict != NULL)
609 iemsg("non-empty dict constant not supported");
610 generate_NEWDICT(cctx, 0, TRUE);
611 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000612#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100613 case VAR_JOB:
614 if (tv->vval.v_job != NULL)
615 iemsg("non-null job constant not supported");
616 generate_PUSHJOB(cctx);
617 break;
618 case VAR_CHANNEL:
619 if (tv->vval.v_channel != NULL)
620 iemsg("non-null channel constant not supported");
621 generate_PUSHCHANNEL(cctx);
622 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000623#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100624 case VAR_FUNC:
625 if (tv->vval.v_string != NULL)
626 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100627 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100628 break;
629 case VAR_PARTIAL:
630 if (tv->vval.v_partial != NULL)
631 iemsg("non-null partial constant not supported");
632 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
633 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000634 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100635 break;
636 case VAR_STRING:
637 generate_PUSHS(cctx, &tv->vval.v_string);
638 tv->vval.v_string = NULL;
639 break;
640 default:
641 siemsg("constant type %d not supported", tv->v_type);
642 clear_tv(tv);
643 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100645 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000646 return OK;
647}
648
649/*
650 * Generate an ISN_PUSHNR instruction.
651 */
652 int
653generate_PUSHNR(cctx_T *cctx, varnumber_T number)
654{
655 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000656
657 RETURN_OK_IF_SKIP(cctx);
658 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
659 return FAIL;
660 isn->isn_arg.number = number;
661
662 if (number == 0 || number == 1)
663 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000664 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000665 return OK;
666}
667
668/*
669 * Generate an ISN_PUSHBOOL instruction.
670 */
671 int
672generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
673{
674 isn_T *isn;
675
676 RETURN_OK_IF_SKIP(cctx);
677 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
678 return FAIL;
679 isn->isn_arg.number = number;
680
681 return OK;
682}
683
684/*
685 * Generate an ISN_PUSHSPEC instruction.
686 */
687 int
688generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
689{
690 isn_T *isn;
691
692 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000693 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
694 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000695 return FAIL;
696 isn->isn_arg.number = number;
697
698 return OK;
699}
700
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000701/*
702 * Generate an ISN_PUSHF instruction.
703 */
704 int
705generate_PUSHF(cctx_T *cctx, float_T fnumber)
706{
707 isn_T *isn;
708
709 RETURN_OK_IF_SKIP(cctx);
710 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
711 return FAIL;
712 isn->isn_arg.fnumber = fnumber;
713
714 return OK;
715}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000716
717/*
718 * Generate an ISN_PUSHS instruction.
719 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100720 * Note that if "str" is used in the instruction OK is returned and "*str" is
721 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000722 */
723 int
724generate_PUSHS(cctx_T *cctx, char_u **str)
725{
726 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100727 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000728
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100729 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100731 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
732 ret = FAIL;
733 else
734 {
735 isn->isn_arg.string = str == NULL ? NULL : *str;
736 return OK;
737 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000738 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100739 if (str != NULL)
740 VIM_CLEAR(*str);
741 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000742}
743
744/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000745 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746 */
747 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000748generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000749{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100751#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100752 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000753 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000754 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100755#else
756 emsg(_(e_channel_job_feature_not_available));
757 return FAIL;
758#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000759}
760
761/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000762 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000763 */
764 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000765generate_PUSHJOB(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_PUSHJOB, &t_job) == 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/*
779 * Generate an ISN_PUSHBLOB instruction.
780 * Consumes "blob".
781 */
782 int
783generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
784{
785 isn_T *isn;
786
787 RETURN_OK_IF_SKIP(cctx);
788 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
789 return FAIL;
790 isn->isn_arg.blob = blob;
791
792 return OK;
793}
794
795/*
796 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100797 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
798 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000799 */
800 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100801generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000802{
803 isn_T *isn;
804 char_u *funcname;
805
806 RETURN_OK_IF_SKIP(cctx);
807 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
808 return FAIL;
809 if (name == NULL)
810 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100811 else if (!may_prefix
812 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000813 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000814 funcname = vim_strsave(name);
815 else
816 {
817 funcname = alloc(STRLEN(name) + 3);
818 if (funcname != NULL)
819 {
820 STRCPY(funcname, "g:");
821 STRCPY(funcname + 2, name);
822 }
823 }
824
825 isn->isn_arg.string = funcname;
826 return OK;
827}
828
829/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000830 * Generate an ISN_AUTOLOAD instruction.
831 */
832 int
833generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
834{
835 isn_T *isn;
836
837 RETURN_OK_IF_SKIP(cctx);
838 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
839 return FAIL;
840 isn->isn_arg.string = vim_strsave(name);
841 if (isn->isn_arg.string == NULL)
842 return FAIL;
843 return OK;
844}
845
846/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000847 * Generate an ISN_GETITEM instruction with "index".
848 * "with_op" is TRUE for "+=" and other operators, the stack has the current
849 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100850 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851 */
852 int
853generate_GETITEM(cctx_T *cctx, int index, int with_op)
854{
855 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000856 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857 type_T *item_type = &t_any;
858
859 RETURN_OK_IF_SKIP(cctx);
860
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000861 item_type = type->tt_member;
862 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
863 return FAIL;
864 isn->isn_arg.getitem.gi_index = index;
865 isn->isn_arg.getitem.gi_with_op = with_op;
866
867 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000868 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000869}
870
871/*
872 * Generate an ISN_SLICE instruction with "count".
873 */
874 int
875generate_SLICE(cctx_T *cctx, int count)
876{
877 isn_T *isn;
878
879 RETURN_OK_IF_SKIP(cctx);
880 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
881 return FAIL;
882 isn->isn_arg.number = count;
883 return OK;
884}
885
886/*
887 * Generate an ISN_CHECKLEN instruction with "min_len".
888 */
889 int
890generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
891{
892 isn_T *isn;
893
894 RETURN_OK_IF_SKIP(cctx);
895
896 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
897 return FAIL;
898 isn->isn_arg.checklen.cl_min_len = min_len;
899 isn->isn_arg.checklen.cl_more_OK = more_OK;
900
901 return OK;
902}
903
904/*
905 * Generate an ISN_STORE instruction.
906 */
907 int
908generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
909{
910 isn_T *isn;
911
912 RETURN_OK_IF_SKIP(cctx);
913 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
914 return FAIL;
915 if (name != NULL)
916 isn->isn_arg.string = vim_strsave(name);
917 else
918 isn->isn_arg.number = idx;
919
920 return OK;
921}
922
923/*
924 * Generate an ISN_STOREOUTER instruction.
925 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000926 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100927generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928{
929 isn_T *isn;
930
931 RETURN_OK_IF_SKIP(cctx);
932 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
933 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100934 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
935 {
936 // Store a variable defined in a loop. A copy will be made at the end
937 // of the loop. TODO: how about deeper nesting?
938 isn->isn_arg.outer.outer_idx = idx - loop_idx;
939 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
940 }
941 else
942 {
943 isn->isn_arg.outer.outer_idx = idx;
944 isn->isn_arg.outer.outer_depth = level;
945 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000946
947 return OK;
948}
949
950/*
951 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
952 */
953 int
954generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
955{
956 isn_T *isn;
957
958 RETURN_OK_IF_SKIP(cctx);
959 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
960 return FAIL;
961 isn->isn_arg.storenr.stnr_idx = idx;
962 isn->isn_arg.storenr.stnr_val = value;
963
964 return OK;
965}
966
967/*
968 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
969 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000970 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000971generate_STOREOPT(
972 cctx_T *cctx,
973 isntype_T isn_type,
974 char_u *name,
975 int opt_flags)
976{
977 isn_T *isn;
978
979 RETURN_OK_IF_SKIP(cctx);
980 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
981 return FAIL;
982 isn->isn_arg.storeopt.so_name = vim_strsave(name);
983 isn->isn_arg.storeopt.so_flags = opt_flags;
984
985 return OK;
986}
987
988/*
989 * Generate an ISN_LOAD or similar instruction.
990 */
991 int
992generate_LOAD(
993 cctx_T *cctx,
994 isntype_T isn_type,
995 int idx,
996 char_u *name,
997 type_T *type)
998{
999 isn_T *isn;
1000
1001 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001002 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001003 return FAIL;
1004 if (name != NULL)
1005 isn->isn_arg.string = vim_strsave(name);
1006 else
1007 isn->isn_arg.number = idx;
1008
1009 return OK;
1010}
1011
1012/*
1013 * Generate an ISN_LOADOUTER instruction
1014 */
1015 int
1016generate_LOADOUTER(
1017 cctx_T *cctx,
1018 int idx,
1019 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001020 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001021 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001022 type_T *type)
1023{
1024 isn_T *isn;
1025
1026 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001027 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001028 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001029 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1030 {
1031 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001032 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001033 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001034 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001035 }
1036 else
1037 {
1038 isn->isn_arg.outer.outer_idx = idx;
1039 isn->isn_arg.outer.outer_depth = nesting;
1040 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001041
1042 return OK;
1043}
1044
1045/*
1046 * Generate an ISN_LOADV instruction for v:var.
1047 */
1048 int
1049generate_LOADV(
1050 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001051 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001052{
1053 int di_flags;
1054 int vidx = find_vim_var(name, &di_flags);
1055 type_T *type;
1056
1057 RETURN_OK_IF_SKIP(cctx);
1058 if (vidx < 0)
1059 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001060 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001061 return FAIL;
1062 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001063 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001064 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1065}
1066
1067/*
1068 * Generate an ISN_UNLET instruction.
1069 */
1070 int
1071generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1072{
1073 isn_T *isn;
1074
1075 RETURN_OK_IF_SKIP(cctx);
1076 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1077 return FAIL;
1078 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1079 isn->isn_arg.unlet.ul_forceit = forceit;
1080
1081 return OK;
1082}
1083
1084/*
1085 * Generate an ISN_LOCKCONST instruction.
1086 */
1087 int
1088generate_LOCKCONST(cctx_T *cctx)
1089{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001090 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001091 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001092 return FAIL;
1093 return OK;
1094}
1095
1096/*
1097 * Generate an ISN_LOADS instruction.
1098 */
1099 int
1100generate_OLDSCRIPT(
1101 cctx_T *cctx,
1102 isntype_T isn_type,
1103 char_u *name,
1104 int sid,
1105 type_T *type)
1106{
1107 isn_T *isn;
1108
1109 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001110 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001111 isn = generate_instr_type(cctx, isn_type, type);
1112 else
1113 isn = generate_instr_drop(cctx, isn_type, 1);
1114 if (isn == NULL)
1115 return FAIL;
1116 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1117 isn->isn_arg.loadstore.ls_sid = sid;
1118
1119 return OK;
1120}
1121
1122/*
1123 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1124 */
1125 int
1126generate_VIM9SCRIPT(
1127 cctx_T *cctx,
1128 isntype_T isn_type,
1129 int sid,
1130 int idx,
1131 type_T *type)
1132{
1133 isn_T *isn;
1134 scriptref_T *sref;
1135 scriptitem_T *si = SCRIPT_ITEM(sid);
1136
1137 RETURN_OK_IF_SKIP(cctx);
1138 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001139 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001140 else
1141 isn = generate_instr_drop(cctx, isn_type, 1);
1142 if (isn == NULL)
1143 return FAIL;
1144
1145 // This requires three arguments, which doesn't fit in an instruction, thus
1146 // we need to allocate a struct for this.
1147 sref = ALLOC_ONE(scriptref_T);
1148 if (sref == NULL)
1149 return FAIL;
1150 isn->isn_arg.script.scriptref = sref;
1151 sref->sref_sid = sid;
1152 sref->sref_idx = idx;
1153 sref->sref_seq = si->sn_script_seq;
1154 sref->sref_type = type;
1155 return OK;
1156}
1157
1158/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001159 * Generate an ISN_NEWLIST instruction for "count" items.
1160 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001161 */
1162 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001163generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001164{
1165 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001166 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001168 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001169
1170 RETURN_OK_IF_SKIP(cctx);
1171 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1172 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001173 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001174
Bram Moolenaar078a4612022-01-04 15:17:03 +00001175 // Get the member type and the declared member type from all the items on
1176 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001177 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001178 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001179 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001180
1181 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001182 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183
1184 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001185 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001186}
1187
1188/*
1189 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001190 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001191 */
1192 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001193generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001194{
1195 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001196 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001197 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001198 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001199
1200 RETURN_OK_IF_SKIP(cctx);
1201 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1202 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001203 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001204
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001205 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001206 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001207 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001208
1209 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001210 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211
1212 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001213 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001214}
1215
1216/*
1217 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001218 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001219 * If variables were declared inside a loop "loop_var_idx" is the index of the
1220 * first one and "loop_var_count" the number of variables declared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001221 */
1222 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001223generate_FUNCREF(
1224 cctx_T *cctx,
1225 ufunc_T *ufunc,
1226 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001227{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001228 isn_T *isn;
1229 type_T *type;
1230 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001231 loopvarinfo_T loopinfo;
1232 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001233
1234 RETURN_OK_IF_SKIP(cctx);
1235 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1236 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001237 if (isnp != NULL)
1238 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001239
Bram Moolenaarcc341812022-09-19 15:54:34 +01001240 has_vars = get_loop_var_info(cctx, &loopinfo);
1241 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001242 {
1243 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1244 if (extra == NULL)
1245 return FAIL;
1246 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001247 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001248 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001249 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001250 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251 else
1252 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001253
1254 // Reserve an extra variable to keep track of the number of closures
1255 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001256 cctx->ctx_has_closure = 1;
1257
1258 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001259 // the nested context, including this one. But not a function defined at
1260 // the script level.
1261 if ((ufunc->uf_flags & FC_CLOSURE)
1262 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001263 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1264
Bram Moolenaar078a4612022-01-04 15:17:03 +00001265 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1266 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001267}
1268
1269/*
1270 * Generate an ISN_NEWFUNC instruction.
1271 * "lambda_name" and "func_name" must be in allocated memory and will be
1272 * consumed.
1273 */
1274 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001275generate_NEWFUNC(
1276 cctx_T *cctx,
1277 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001278 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001279{
1280 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001281 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001283 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001284 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001285 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1286 ret = FAIL;
1287 else
1288 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001289 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1290
1291 if (arg == NULL)
1292 ret = FAIL;
1293 else
1294 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001295 // Reserve an extra variable to keep track of the number of
1296 // closures created.
1297 cctx->ctx_has_closure = 1;
1298
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001299 isn->isn_arg.newfunc.nf_arg = arg;
1300 arg->nfa_lambda = lambda_name;
1301 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001302 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001303 return OK;
1304 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001305 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001306 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001307 vim_free(lambda_name);
1308 vim_free(func_name);
1309 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001310}
1311
1312/*
1313 * Generate an ISN_DEF instruction: list functions
1314 */
1315 int
1316generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1317{
1318 isn_T *isn;
1319
1320 RETURN_OK_IF_SKIP(cctx);
1321 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1322 return FAIL;
1323 if (len > 0)
1324 {
1325 isn->isn_arg.string = vim_strnsave(name, len);
1326 if (isn->isn_arg.string == NULL)
1327 return FAIL;
1328 }
1329 return OK;
1330}
1331
1332/*
1333 * Generate an ISN_JUMP instruction.
1334 */
1335 int
1336generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1337{
1338 isn_T *isn;
1339 garray_T *stack = &cctx->ctx_type_stack;
1340
1341 RETURN_OK_IF_SKIP(cctx);
1342 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1343 return FAIL;
1344 isn->isn_arg.jump.jump_when = when;
1345 isn->isn_arg.jump.jump_where = where;
1346
1347 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1348 --stack->ga_len;
1349
1350 return OK;
1351}
1352
1353/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001354 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1355 */
1356 int
1357generate_WHILE(cctx_T *cctx, int funcref_idx)
1358{
1359 isn_T *isn;
1360 garray_T *stack = &cctx->ctx_type_stack;
1361
1362 RETURN_OK_IF_SKIP(cctx);
1363 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1364 return FAIL;
1365 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1366 isn->isn_arg.whileloop.while_end = 0; // filled in later
1367
1368 if (stack->ga_len > 0)
1369 --stack->ga_len;
1370
1371 return OK;
1372}
1373
1374/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001375 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1376 */
1377 int
1378generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1379{
1380 isn_T *isn;
1381
1382 RETURN_OK_IF_SKIP(cctx);
1383 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1384 return FAIL;
1385 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1386 // jump_where is set later
1387 return OK;
1388}
1389
1390 int
1391generate_FOR(cctx_T *cctx, int loop_idx)
1392{
1393 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001394
1395 RETURN_OK_IF_SKIP(cctx);
1396 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1397 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001398 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001399
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001400 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001401 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001402}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001403
1404 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001405generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001406{
1407 isn_T *isn;
1408
1409 RETURN_OK_IF_SKIP(cctx);
1410 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1411 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001412 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1413 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1414 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001415 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001416 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001417 return OK;
1418}
1419
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001420/*
1421 * Generate an ISN_TRYCONT instruction.
1422 */
1423 int
1424generate_TRYCONT(cctx_T *cctx, int levels, int where)
1425{
1426 isn_T *isn;
1427
1428 RETURN_OK_IF_SKIP(cctx);
1429 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1430 return FAIL;
1431 isn->isn_arg.trycont.tct_levels = levels;
1432 isn->isn_arg.trycont.tct_where = where;
1433
1434 return OK;
1435}
1436
Bram Moolenaar16900322022-09-08 19:51:45 +01001437/*
1438 * Check "argount" arguments and their types on the type stack.
1439 * Give an error and return FAIL if something is wrong.
1440 * When "method_call" is NULL no code is generated.
1441 */
1442 int
1443check_internal_func_args(
1444 cctx_T *cctx,
1445 int func_idx,
1446 int argcount,
1447 int method_call,
1448 type2_T **argtypes,
1449 type2_T *shuffled_argtypes)
1450{
1451 garray_T *stack = &cctx->ctx_type_stack;
1452 int argoff = check_internal_func(func_idx, argcount);
1453
1454 if (argoff < 0)
1455 return FAIL;
1456
1457 if (method_call && argoff > 1)
1458 {
1459 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1460
1461 if (isn == NULL)
1462 return FAIL;
1463 isn->isn_arg.shuffle.shfl_item = argcount;
1464 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1465 }
1466
1467 if (argcount > 0)
1468 {
1469 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1470
1471 // Check the types of the arguments.
1472 if (method_call && argoff > 1)
1473 {
1474 int i;
1475
1476 for (i = 0; i < argcount; ++i)
1477 shuffled_argtypes[i] = (i < argoff - 1)
1478 ? typep[i + 1]
1479 : (i == argoff - 1) ? typep[0] : typep[i];
1480 *argtypes = shuffled_argtypes;
1481 }
1482 else
1483 {
1484 int i;
1485
1486 for (i = 0; i < argcount; ++i)
1487 shuffled_argtypes[i] = typep[i];
1488 *argtypes = shuffled_argtypes;
1489 }
1490 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1491 cctx) == FAIL)
1492 return FAIL;
1493 }
1494 return OK;
1495}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001496
1497/*
1498 * Generate an ISN_BCALL instruction.
1499 * "method_call" is TRUE for "value->method()"
1500 * Return FAIL if the number of arguments is wrong.
1501 */
1502 int
1503generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1504{
1505 isn_T *isn;
1506 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001507 type2_T *argtypes = NULL;
1508 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1509 type2_T *maptype = NULL;
1510 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001511 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001512
1513 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001514
1515 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1516 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001517 return FAIL;
1518
Bram Moolenaar16900322022-09-08 19:51:45 +01001519 if (internal_func_is_map(func_idx))
1520 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001521
1522 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1523 return FAIL;
1524 isn->isn_arg.bfunc.cbf_idx = func_idx;
1525 isn->isn_arg.bfunc.cbf_argcount = argcount;
1526
1527 // Drop the argument types and push the return type.
1528 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001529 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1530 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001531 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001532
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001533 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1534 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001535 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001536 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001537
1538 return OK;
1539}
1540
1541/*
1542 * Generate an ISN_LISTAPPEND instruction. Works like add().
1543 * Argument count is already checked.
1544 */
1545 int
1546generate_LISTAPPEND(cctx_T *cctx)
1547{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001548 type_T *list_type;
1549 type_T *item_type;
1550 type_T *expected;
1551
1552 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001553 // For checking the item type we use the declared type of the list and the
1554 // current type of the added item, adding a string to [1, 2] is OK.
1555 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001556 if (arg_type_modifiable(list_type, 1) == FAIL)
1557 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001558 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001559 expected = list_type->tt_member;
1560 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1561 return FAIL;
1562
1563 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1564 return FAIL;
1565
Bram Moolenaar078a4612022-01-04 15:17:03 +00001566 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001567 return OK;
1568}
1569
1570/*
1571 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1572 * Argument count is already checked.
1573 */
1574 int
1575generate_BLOBAPPEND(cctx_T *cctx)
1576{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001577 type_T *item_type;
1578
Bram Moolenaarfa103972022-09-29 19:14:42 +01001579 // Caller already checked that blob_type is a blob, check it is modifiable.
1580 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1581 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001582 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001583 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1584 return FAIL;
1585
1586 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1587 return FAIL;
1588
Bram Moolenaar078a4612022-01-04 15:17:03 +00001589 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001590 return OK;
1591}
1592
1593/*
1594 * Generate an ISN_DCALL or ISN_UCALL instruction.
1595 * Return FAIL if the number of arguments is wrong.
1596 */
1597 int
1598generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1599{
1600 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001601 int regular_args = ufunc->uf_args.ga_len;
1602 int argcount = pushed_argcount;
1603
1604 RETURN_OK_IF_SKIP(cctx);
1605 if (argcount > regular_args && !has_varargs(ufunc))
1606 {
1607 semsg(_(e_too_many_arguments_for_function_str),
1608 printable_func_name(ufunc));
1609 return FAIL;
1610 }
1611 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1612 {
1613 semsg(_(e_not_enough_arguments_for_function_str),
1614 printable_func_name(ufunc));
1615 return FAIL;
1616 }
1617
1618 if (ufunc->uf_def_status != UF_NOT_COMPILED
1619 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1620 {
1621 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001622 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001623
1624 for (i = 0; i < argcount; ++i)
1625 {
1626 type_T *expected;
1627 type_T *actual;
1628
Bram Moolenaar078a4612022-01-04 15:17:03 +00001629 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001630 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001631 && i >= regular_args - ufunc->uf_def_args.ga_len)
1632 {
1633 // assume v:none used for default argument value
1634 continue;
1635 }
1636 if (i < regular_args)
1637 {
1638 if (ufunc->uf_arg_types == NULL)
1639 continue;
1640 expected = ufunc->uf_arg_types[i];
1641 }
1642 else if (ufunc->uf_va_type == NULL
1643 || ufunc->uf_va_type == &t_list_any)
1644 // possibly a lambda or "...: any"
1645 expected = &t_any;
1646 else
1647 expected = ufunc->uf_va_type->tt_member;
1648 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1649 TRUE, FALSE) == FAIL)
1650 {
1651 arg_type_mismatch(expected, actual, i + 1);
1652 return FAIL;
1653 }
1654 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001655 compile_type = get_compile_type(ufunc);
1656 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001657 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001658 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001659 return FAIL;
1660 }
1661 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1662 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001663 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001664 ufunc->uf_name);
1665 return FAIL;
1666 }
1667
1668 if ((isn = generate_instr(cctx,
1669 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1670 : ISN_UCALL)) == NULL)
1671 return FAIL;
1672 if (isn->isn_type == ISN_DCALL)
1673 {
1674 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1675 isn->isn_arg.dfunc.cdf_argcount = argcount;
1676 }
1677 else
1678 {
1679 // A user function may be deleted and redefined later, can't use the
1680 // ufunc pointer, need to look it up again at runtime.
1681 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1682 isn->isn_arg.ufunc.cuf_argcount = argcount;
1683 }
1684
Bram Moolenaar078a4612022-01-04 15:17:03 +00001685 // drop the argument types
1686 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001687
Bram Moolenaar078a4612022-01-04 15:17:03 +00001688 // add return type
1689 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001690}
1691
1692/*
1693 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1694 */
1695 int
1696generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1697{
1698 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001699
1700 RETURN_OK_IF_SKIP(cctx);
1701 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1702 return FAIL;
1703 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1704 isn->isn_arg.ufunc.cuf_argcount = argcount;
1705
Bram Moolenaar078a4612022-01-04 15:17:03 +00001706 // drop the argument types
1707 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001708
Bram Moolenaar078a4612022-01-04 15:17:03 +00001709 // add return value
1710 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001711}
1712
1713/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001714 * Check the arguments of function "type" against the types on the stack.
1715 * Returns OK or FAIL;
1716 */
1717 int
1718check_func_args_from_type(
1719 cctx_T *cctx,
1720 type_T *type,
1721 int argcount,
1722 int at_top,
1723 char_u *name)
1724{
1725 if (type->tt_argcount != -1)
1726 {
1727 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1728
1729 if (argcount < type->tt_min_argcount - varargs)
1730 {
1731 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1732 return FAIL;
1733 }
1734 if (!varargs && argcount > type->tt_argcount)
1735 {
1736 emsg_funcname(e_too_many_arguments_for_function_str, name);
1737 return FAIL;
1738 }
1739 if (type->tt_args != NULL)
1740 {
1741 int i;
1742
1743 for (i = 0; i < argcount; ++i)
1744 {
1745 int offset = -argcount + i - (at_top ? 0 : 1);
1746 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1747 type_T *expected;
1748
1749 if (varargs && i >= type->tt_argcount - 1)
1750 expected = type->tt_args[type->tt_argcount - 1]->tt_member;
1751 else if (i >= type->tt_min_argcount
1752 && actual->tt_type == VAR_SPECIAL)
1753 expected = &t_any;
1754 else
1755 expected = type->tt_args[i];
1756 if (need_type(actual, expected, offset, i + 1,
1757 cctx, TRUE, FALSE) == FAIL)
1758 {
1759 arg_type_mismatch(expected, actual, i + 1);
1760 return FAIL;
1761 }
1762 }
1763 }
1764 }
1765
1766 return OK;
1767}
1768/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 * Generate an ISN_PCALL instruction.
1770 * "type" is the type of the FuncRef.
1771 */
1772 int
1773generate_PCALL(
1774 cctx_T *cctx,
1775 int argcount,
1776 char_u *name,
1777 type_T *type,
1778 int at_top)
1779{
1780 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001781 type_T *ret_type;
1782
1783 RETURN_OK_IF_SKIP(cctx);
1784
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001785 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001786 ret_type = &t_any;
1787 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1788 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001789 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1790 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001792 ret_type = type->tt_member;
1793 if (ret_type == &t_unknown)
1794 // return type not known yet, use a runtime check
1795 ret_type = &t_any;
1796 }
1797 else
1798 {
1799 semsg(_(e_not_callable_type_str), name);
1800 return FAIL;
1801 }
1802
1803 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1804 return FAIL;
1805 isn->isn_arg.pfunc.cpf_top = at_top;
1806 isn->isn_arg.pfunc.cpf_argcount = argcount;
1807
Bram Moolenaar078a4612022-01-04 15:17:03 +00001808 // drop the arguments and the funcref/partial
1809 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001810
Bram Moolenaar078a4612022-01-04 15:17:03 +00001811 // push the return value
1812 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001813
1814 // If partial is above the arguments it must be cleared and replaced with
1815 // the return value.
1816 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1817 return FAIL;
1818
1819 return OK;
1820}
1821
1822/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001823 * Generate an ISN_DEFER instruction.
1824 */
1825 int
1826generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
1827{
1828 isn_T *isn;
1829
1830 RETURN_OK_IF_SKIP(cctx);
1831 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
1832 return FAIL;
1833 isn->isn_arg.defer.defer_var_idx = var_idx;
1834 isn->isn_arg.defer.defer_argcount = argcount;
1835 return OK;
1836}
1837
1838/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001839 * Generate an ISN_STRINGMEMBER instruction.
1840 */
1841 int
1842generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1843{
1844 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001845 type_T *type;
1846
1847 RETURN_OK_IF_SKIP(cctx);
1848 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1849 return FAIL;
1850 isn->isn_arg.string = vim_strnsave(name, len);
1851
1852 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001853 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001854 if (type->tt_type != VAR_DICT
1855 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856 {
1857 char *tofree;
1858
1859 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1860 name, type_name(type, &tofree));
1861 vim_free(tofree);
1862 return FAIL;
1863 }
1864 // change dict type to dict member type
1865 if (type->tt_type == VAR_DICT)
1866 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001867 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00001868 ? &t_any : type->tt_member;
1869 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001870 }
1871
1872 return OK;
1873}
1874
1875/*
1876 * Generate an ISN_ECHO instruction.
1877 */
1878 int
1879generate_ECHO(cctx_T *cctx, int with_white, int count)
1880{
1881 isn_T *isn;
1882
1883 RETURN_OK_IF_SKIP(cctx);
1884 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1885 return FAIL;
1886 isn->isn_arg.echo.echo_with_white = with_white;
1887 isn->isn_arg.echo.echo_count = count;
1888
1889 return OK;
1890}
1891
1892/*
1893 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1894 */
1895 int
1896generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1897{
1898 isn_T *isn;
1899
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01001900 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001901 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1902 return FAIL;
1903 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001904 return OK;
1905}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001906
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01001907/*
1908 * Generate an ISN_ECHOWINDOW instruction
1909 */
1910 int
1911generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
1912{
1913 isn_T *isn;
1914
1915 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
1916 return FAIL;
1917 isn->isn_arg.echowin.ewin_count = count;
1918 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001919 return OK;
1920}
1921
1922/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001923 * Generate an ISN_SOURCE instruction.
1924 */
1925 int
1926generate_SOURCE(cctx_T *cctx, int sid)
1927{
1928 isn_T *isn;
1929
1930 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1931 return FAIL;
1932 isn->isn_arg.number = sid;
1933
1934 return OK;
1935}
1936
1937/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001938 * Generate an ISN_PUT instruction.
1939 */
1940 int
1941generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1942{
1943 isn_T *isn;
1944
1945 RETURN_OK_IF_SKIP(cctx);
1946 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1947 return FAIL;
1948 isn->isn_arg.put.put_regname = regname;
1949 isn->isn_arg.put.put_lnum = lnum;
1950 return OK;
1951}
1952
1953/*
1954 * Generate an EXEC instruction that takes a string argument.
1955 * A copy is made of "line".
1956 */
1957 int
1958generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1959{
1960 isn_T *isn;
1961
1962 RETURN_OK_IF_SKIP(cctx);
1963 if ((isn = generate_instr(cctx, isntype)) == NULL)
1964 return FAIL;
1965 isn->isn_arg.string = vim_strsave(line);
1966 return OK;
1967}
1968
1969/*
1970 * Generate an EXEC instruction that takes a string argument.
1971 * "str" must be allocated, it is consumed.
1972 */
1973 int
1974generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1975{
1976 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001977 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001978
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001979 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001980 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001981 if ((isn = generate_instr(cctx, isntype)) == NULL)
1982 ret = FAIL;
1983 else
1984 {
1985 isn->isn_arg.string = str;
1986 return OK;
1987 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001988 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001989 vim_free(str);
1990 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001991}
1992
1993 int
1994generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1995{
1996 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001997
1998 RETURN_OK_IF_SKIP(cctx);
1999 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2000 return FAIL;
2001 isn->isn_arg.string = vim_strsave(line);
2002
Bram Moolenaar078a4612022-01-04 15:17:03 +00002003 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002004}
2005
2006 int
2007generate_EXECCONCAT(cctx_T *cctx, int count)
2008{
2009 isn_T *isn;
2010
2011 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2012 return FAIL;
2013 isn->isn_arg.number = count;
2014 return OK;
2015}
2016
2017/*
2018 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2019 */
2020 int
2021generate_RANGE(cctx_T *cctx, char_u *range)
2022{
2023 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002024
2025 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2026 return FAIL;
2027 isn->isn_arg.string = range;
2028
Bram Moolenaar078a4612022-01-04 15:17:03 +00002029 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002030}
2031
2032 int
2033generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2034{
2035 isn_T *isn;
2036
2037 RETURN_OK_IF_SKIP(cctx);
2038 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2039 return FAIL;
2040 isn->isn_arg.unpack.unp_count = var_count;
2041 isn->isn_arg.unpack.unp_semicolon = semicolon;
2042 return OK;
2043}
2044
2045/*
2046 * Generate an instruction for any command modifiers.
2047 */
2048 int
2049generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2050{
2051 isn_T *isn;
2052
2053 if (has_cmdmod(cmod, FALSE))
2054 {
2055 cctx->ctx_has_cmdmod = TRUE;
2056
2057 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2058 return FAIL;
2059 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2060 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2061 return FAIL;
2062 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2063 // filter program now belongs to the instruction
2064 cmod->cmod_filter_regmatch.regprog = NULL;
2065 }
2066
2067 return OK;
2068}
2069
2070 int
2071generate_undo_cmdmods(cctx_T *cctx)
2072{
2073 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2074 return FAIL;
2075 cctx->ctx_has_cmdmod = FALSE;
2076 return OK;
2077}
2078
2079/*
2080 * Generate a STORE instruction for "dest", not being "dest_local".
2081 * Return FAIL when out of memory.
2082 */
2083 int
2084generate_store_var(
2085 cctx_T *cctx,
2086 assign_dest_T dest,
2087 int opt_flags,
2088 int vimvaridx,
2089 int scriptvar_idx,
2090 int scriptvar_sid,
2091 type_T *type,
2092 char_u *name)
2093{
2094 switch (dest)
2095 {
2096 case dest_option:
2097 return generate_STOREOPT(cctx, ISN_STOREOPT,
2098 skip_option_env_lead(name), opt_flags);
2099 case dest_func_option:
2100 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2101 skip_option_env_lead(name), opt_flags);
2102 case dest_global:
2103 // include g: with the name, easier to execute that way
2104 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2105 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2106 case dest_buffer:
2107 // include b: with the name, easier to execute that way
2108 return generate_STORE(cctx, ISN_STOREB, 0, name);
2109 case dest_window:
2110 // include w: with the name, easier to execute that way
2111 return generate_STORE(cctx, ISN_STOREW, 0, name);
2112 case dest_tab:
2113 // include t: with the name, easier to execute that way
2114 return generate_STORE(cctx, ISN_STORET, 0, name);
2115 case dest_env:
2116 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2117 case dest_reg:
2118 return generate_STORE(cctx, ISN_STOREREG,
2119 name[1] == '@' ? '"' : name[1], NULL);
2120 case dest_vimvar:
2121 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2122 case dest_script:
2123 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002124 {
2125 isntype_T isn_type = ISN_STORES;
2126
2127 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01002128 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2129 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2130 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002131 {
2132 // "import autoload './dir/script.vim'" - load script first
2133 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2134 return FAIL;
2135 isn_type = ISN_STOREEXPORT;
2136 }
2137
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002138 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002139 return generate_OLDSCRIPT(cctx, isn_type, name,
2140 scriptvar_sid, type);
2141 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002142 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
2143 scriptvar_sid, scriptvar_idx, type);
2144 case dest_local:
2145 case dest_expr:
2146 // cannot happen
2147 break;
2148 }
2149 return FAIL;
2150}
2151
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002152/*
2153 * Return TRUE when inside a "for" or "while" loop.
2154 */
2155 int
2156inside_loop_scope(cctx_T *cctx)
2157{
2158 scope_T *scope = cctx->ctx_scope;
2159
2160 for (;;)
2161 {
2162 if (scope == NULL)
2163 break;
2164 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2165 return TRUE;
2166 scope = scope->se_outer;
2167 }
2168 return FALSE;
2169}
2170
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002171 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002172generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002173{
2174 if (lhs->lhs_dest != dest_local)
2175 return generate_store_var(cctx, lhs->lhs_dest,
2176 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2177 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2178 lhs->lhs_type, lhs->lhs_name);
2179
2180 if (lhs->lhs_lvar != NULL)
2181 {
2182 garray_T *instr = &cctx->ctx_instr;
2183 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2184
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002185 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2186 // ISN_STORENR.
2187 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002188 if (lhs->lhs_lvar->lv_from_outer == 0
2189 && instr->ga_len == instr_count + 1
2190 && isn->isn_type == ISN_PUSHNR)
2191 {
2192 varnumber_T val = isn->isn_arg.number;
2193 garray_T *stack = &cctx->ctx_type_stack;
2194
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002195 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002196 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002197 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002198 --instr->ga_len;
2199 }
2200 else
2201 {
2202 isn->isn_type = ISN_STORENR;
2203 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2204 isn->isn_arg.storenr.stnr_val = val;
2205 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002206 if (stack->ga_len > 0)
2207 --stack->ga_len;
2208 }
2209 else if (lhs->lhs_lvar->lv_from_outer > 0)
2210 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002211 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002212 else
2213 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2214 }
2215 return OK;
2216}
2217
2218#if defined(FEAT_PROFILE) || defined(PROTO)
2219 void
2220may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2221{
2222 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2223 generate_instr(cctx, ISN_PROF_END);
2224}
2225#endif
2226
2227
2228/*
2229 * Delete an instruction, free what it contains.
2230 */
2231 void
2232delete_instr(isn_T *isn)
2233{
2234 switch (isn->isn_type)
2235 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002236 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237 case ISN_DEF:
2238 case ISN_EXEC:
2239 case ISN_EXECRANGE:
2240 case ISN_EXEC_SPLIT:
2241 case ISN_LEGACY_EVAL:
2242 case ISN_LOADAUTO:
2243 case ISN_LOADB:
2244 case ISN_LOADENV:
2245 case ISN_LOADG:
2246 case ISN_LOADOPT:
2247 case ISN_LOADT:
2248 case ISN_LOADW:
2249 case ISN_LOCKUNLOCK:
2250 case ISN_PUSHEXC:
2251 case ISN_PUSHFUNC:
2252 case ISN_PUSHS:
2253 case ISN_RANGE:
2254 case ISN_STOREAUTO:
2255 case ISN_STOREB:
2256 case ISN_STOREENV:
2257 case ISN_STOREG:
2258 case ISN_STORET:
2259 case ISN_STOREW:
2260 case ISN_STRINGMEMBER:
2261 vim_free(isn->isn_arg.string);
2262 break;
2263
2264 case ISN_SUBSTITUTE:
2265 {
2266 int idx;
2267 isn_T *list = isn->isn_arg.subs.subs_instr;
2268
2269 vim_free(isn->isn_arg.subs.subs_cmd);
2270 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2271 delete_instr(list + idx);
2272 vim_free(list);
2273 }
2274 break;
2275
2276 case ISN_INSTR:
2277 {
2278 int idx;
2279 isn_T *list = isn->isn_arg.instr;
2280
2281 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2282 delete_instr(list + idx);
2283 vim_free(list);
2284 }
2285 break;
2286
2287 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002288 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002289 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002290 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002291 vim_free(isn->isn_arg.loadstore.ls_name);
2292 break;
2293
2294 case ISN_UNLET:
2295 case ISN_UNLETENV:
2296 vim_free(isn->isn_arg.unlet.ul_name);
2297 break;
2298
2299 case ISN_STOREOPT:
2300 case ISN_STOREFUNCOPT:
2301 vim_free(isn->isn_arg.storeopt.so_name);
2302 break;
2303
2304 case ISN_PUSHBLOB: // push blob isn_arg.blob
2305 blob_unref(isn->isn_arg.blob);
2306 break;
2307
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002308 case ISN_UCALL:
2309 vim_free(isn->isn_arg.ufunc.cuf_name);
2310 break;
2311
2312 case ISN_FUNCREF:
2313 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002314 funcref_T *funcref = &isn->isn_arg.funcref;
2315 funcref_extra_T *extra = funcref->fr_extra;
2316
2317 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002318 {
2319 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002320 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002321 ufunc_T *ufunc = dfunc->df_ufunc;
2322
2323 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2324 func_ptr_unref(ufunc);
2325 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002326 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002327 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002328 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002329
2330 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002331 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002332 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002333 vim_free(name);
2334 }
2335 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002336 }
2337 }
2338 break;
2339
2340 case ISN_DCALL:
2341 {
2342 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002343 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002344
2345 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002346 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002347 func_ptr_unref(dfunc->df_ufunc);
2348 }
2349 break;
2350
2351 case ISN_NEWFUNC:
2352 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002353 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002354
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002355 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002356 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002357 ufunc_T *ufunc = find_func_even_dead(
2358 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002359
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002360 if (ufunc != NULL)
2361 {
2362 unlink_def_function(ufunc);
2363 func_ptr_unref(ufunc);
2364 }
2365
2366 vim_free(arg->nfa_lambda);
2367 vim_free(arg->nfa_global);
2368 vim_free(arg);
2369 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002370 }
2371 break;
2372
2373 case ISN_CHECKTYPE:
2374 case ISN_SETTYPE:
2375 free_type(isn->isn_arg.type.ct_type);
2376 break;
2377
2378 case ISN_CMDMOD:
2379 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002380 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002381 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2382 break;
2383
2384 case ISN_LOADSCRIPT:
2385 case ISN_STORESCRIPT:
2386 vim_free(isn->isn_arg.script.scriptref);
2387 break;
2388
2389 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002390 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002391 break;
2392
2393 case ISN_CEXPR_CORE:
2394 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2395 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2396 break;
2397
2398 case ISN_2BOOL:
2399 case ISN_2STRING:
2400 case ISN_2STRING_ANY:
2401 case ISN_ADDBLOB:
2402 case ISN_ADDLIST:
2403 case ISN_ANYINDEX:
2404 case ISN_ANYSLICE:
2405 case ISN_BCALL:
2406 case ISN_BLOBAPPEND:
2407 case ISN_BLOBINDEX:
2408 case ISN_BLOBSLICE:
2409 case ISN_CATCH:
2410 case ISN_CEXPR_AUCMD:
2411 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002412 case ISN_CLEARDICT:
2413 case ISN_CMDMOD_REV:
2414 case ISN_COMPAREANY:
2415 case ISN_COMPAREBLOB:
2416 case ISN_COMPAREBOOL:
2417 case ISN_COMPAREDICT:
2418 case ISN_COMPAREFLOAT:
2419 case ISN_COMPAREFUNC:
2420 case ISN_COMPARELIST:
2421 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002422 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002423 case ISN_COMPARESPECIAL:
2424 case ISN_COMPARESTRING:
2425 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002426 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002427 case ISN_COND2BOOL:
2428 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002429 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002430 case ISN_DROP:
2431 case ISN_ECHO:
2432 case ISN_ECHOCONSOLE:
2433 case ISN_ECHOERR:
2434 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002435 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002436 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002437 case ISN_ENDTRY:
2438 case ISN_EXECCONCAT:
2439 case ISN_EXECUTE:
2440 case ISN_FINALLY:
2441 case ISN_FINISH:
2442 case ISN_FOR:
2443 case ISN_GETITEM:
2444 case ISN_JUMP:
2445 case ISN_JUMP_IF_ARG_SET:
2446 case ISN_LISTAPPEND:
2447 case ISN_LISTINDEX:
2448 case ISN_LISTSLICE:
2449 case ISN_LOAD:
2450 case ISN_LOADBDICT:
2451 case ISN_LOADGDICT:
2452 case ISN_LOADOUTER:
2453 case ISN_LOADREG:
2454 case ISN_LOADTDICT:
2455 case ISN_LOADV:
2456 case ISN_LOADWDICT:
2457 case ISN_LOCKCONST:
2458 case ISN_MEMBER:
2459 case ISN_NEGATENR:
2460 case ISN_NEWDICT:
2461 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002462 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002463 case ISN_OPANY:
2464 case ISN_OPFLOAT:
2465 case ISN_OPNR:
2466 case ISN_PCALL:
2467 case ISN_PCALL_END:
2468 case ISN_PROF_END:
2469 case ISN_PROF_START:
2470 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002471 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002472 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002473 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002474 case ISN_PUSHNR:
2475 case ISN_PUSHSPEC:
2476 case ISN_PUT:
2477 case ISN_REDIREND:
2478 case ISN_REDIRSTART:
2479 case ISN_RETURN:
2480 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002481 case ISN_RETURN_OBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002482 case ISN_SHUFFLE:
2483 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002484 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002485 case ISN_STORE:
2486 case ISN_STOREINDEX:
2487 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002488 case ISN_STOREOUTER:
2489 case ISN_STORERANGE:
2490 case ISN_STOREREG:
2491 case ISN_STOREV:
2492 case ISN_STRINDEX:
2493 case ISN_STRSLICE:
2494 case ISN_THROW:
2495 case ISN_TRYCONT:
2496 case ISN_UNLETINDEX:
2497 case ISN_UNLETRANGE:
2498 case ISN_UNPACK:
2499 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002500 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002501 // nothing allocated
2502 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002503 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002504}
2505
2506 void
2507clear_instr_ga(garray_T *gap)
2508{
2509 int idx;
2510
2511 for (idx = 0; idx < gap->ga_len; ++idx)
2512 delete_instr(((isn_T *)gap->ga_data) + idx);
2513 ga_clear(gap);
2514}
2515
2516
2517#endif // defined(FEAT_EVAL)