Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1 | /* 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 * |
| 36 | generate_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 * |
| 58 | generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop) |
| 59 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 60 | RETURN_NULL_IF_SKIP(cctx); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 61 | cctx->ctx_type_stack.ga_len -= drop; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 62 | return generate_instr(cctx, isn_type); |
| 63 | } |
| 64 | |
| 65 | /* |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 66 | * Generate instruction "isn_type" and put "type" on the type stack, |
| 67 | * use "decl_type" for the declared type. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 68 | */ |
Yegappan Lakshmanan | 782b43d | 2022-01-08 18:43:40 +0000 | [diff] [blame] | 69 | static isn_T * |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 70 | generate_instr_type2( |
| 71 | cctx_T *cctx, |
| 72 | isntype_T isn_type, |
| 73 | type_T *type, |
| 74 | type_T *decl_type) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 75 | { |
| 76 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 77 | |
| 78 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 79 | return NULL; |
| 80 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 81 | if (push_type_stack2(cctx, type == NULL ? &t_any : type, |
| 82 | decl_type == NULL ? &t_any : decl_type) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 83 | return NULL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 84 | |
| 85 | return isn; |
| 86 | } |
| 87 | |
| 88 | /* |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 89 | * 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 * |
| 94 | generate_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 Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 100 | * Generate an ISN_DEBUG instruction. |
| 101 | */ |
| 102 | isn_T * |
| 103 | generate_instr_debug(cctx_T *cctx) |
| 104 | { |
| 105 | isn_T *isn; |
| 106 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 107 | + cctx->ctx_ufunc->uf_dfunc_idx; |
| 108 | |
| 109 | if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL) |
| 110 | return NULL; |
| 111 | isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len; |
| 112 | isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum; |
| 113 | return isn; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING. |
| 118 | * But only for simple types. |
| 119 | * When "tolerant" is TRUE convert most types to string, e.g. a List. |
| 120 | */ |
| 121 | int |
| 122 | may_generate_2STRING(int offset, int tolerant, cctx_T *cctx) |
| 123 | { |
| 124 | isn_T *isn; |
| 125 | isntype_T isntype = ISN_2STRING; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 126 | type_T *type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 127 | |
| 128 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 129 | type = get_type_on_stack(cctx, -1 - offset); |
| 130 | switch (type->tt_type) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 131 | { |
| 132 | // nothing to be done |
| 133 | case VAR_STRING: return OK; |
| 134 | |
| 135 | // conversion possible |
| 136 | case VAR_SPECIAL: |
| 137 | case VAR_BOOL: |
| 138 | case VAR_NUMBER: |
| 139 | case VAR_FLOAT: |
| 140 | break; |
| 141 | |
| 142 | // conversion possible (with runtime check) |
| 143 | case VAR_ANY: |
| 144 | case VAR_UNKNOWN: |
| 145 | isntype = ISN_2STRING_ANY; |
| 146 | break; |
| 147 | |
| 148 | // conversion possible when tolerant |
| 149 | case VAR_LIST: |
| 150 | if (tolerant) |
| 151 | { |
| 152 | isntype = ISN_2STRING_ANY; |
| 153 | break; |
| 154 | } |
| 155 | // FALLTHROUGH |
| 156 | |
| 157 | // conversion not possible |
| 158 | case VAR_VOID: |
| 159 | case VAR_BLOB: |
| 160 | case VAR_FUNC: |
| 161 | case VAR_PARTIAL: |
| 162 | case VAR_DICT: |
| 163 | case VAR_JOB: |
| 164 | case VAR_CHANNEL: |
| 165 | case VAR_INSTR: |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 166 | to_string_error(type->tt_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 167 | return FAIL; |
| 168 | } |
| 169 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 170 | set_type_on_stack(cctx, &t_string, -1 - offset); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 171 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 172 | return FAIL; |
| 173 | isn->isn_arg.tostring.offset = offset; |
| 174 | isn->isn_arg.tostring.tolerant = tolerant; |
| 175 | |
| 176 | return OK; |
| 177 | } |
| 178 | |
| 179 | static int |
| 180 | check_number_or_float(vartype_T type1, vartype_T type2, char_u *op) |
| 181 | { |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 182 | if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT |
| 183 | || type1 == VAR_ANY || type1 == VAR_UNKNOWN) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 184 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 185 | || type2 == VAR_ANY || type2 == VAR_UNKNOWN))) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 186 | { |
| 187 | if (*op == '+') |
| 188 | emsg(_(e_wrong_argument_type_for_plus)); |
| 189 | else |
| 190 | semsg(_(e_char_requires_number_or_float_arguments), *op); |
| 191 | return FAIL; |
| 192 | } |
| 193 | return OK; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Generate instruction for "+". For a list this creates a new list. |
| 198 | */ |
| 199 | int |
| 200 | generate_add_instr( |
| 201 | cctx_T *cctx, |
| 202 | vartype_T vartype, |
| 203 | type_T *type1, |
| 204 | type_T *type2, |
| 205 | exprtype_T expr_type) |
| 206 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 207 | isn_T *isn = generate_instr_drop(cctx, |
| 208 | vartype == VAR_NUMBER ? ISN_OPNR |
| 209 | : vartype == VAR_LIST ? ISN_ADDLIST |
| 210 | : vartype == VAR_BLOB ? ISN_ADDBLOB |
| 211 | #ifdef FEAT_FLOAT |
| 212 | : vartype == VAR_FLOAT ? ISN_OPFLOAT |
| 213 | #endif |
| 214 | : ISN_OPANY, 1); |
| 215 | |
| 216 | if (vartype != VAR_LIST && vartype != VAR_BLOB |
| 217 | && type1->tt_type != VAR_ANY |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 218 | && type1->tt_type != VAR_UNKNOWN |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 219 | && type2->tt_type != VAR_ANY |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 220 | && type2->tt_type != VAR_UNKNOWN |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 221 | && check_number_or_float( |
| 222 | type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL) |
| 223 | return FAIL; |
| 224 | |
| 225 | if (isn != NULL) |
| 226 | { |
| 227 | if (isn->isn_type == ISN_ADDLIST) |
| 228 | isn->isn_arg.op.op_type = expr_type; |
| 229 | else |
| 230 | isn->isn_arg.op.op_type = EXPR_ADD; |
| 231 | } |
| 232 | |
| 233 | // When concatenating two lists with different member types the member type |
| 234 | // becomes "any". |
| 235 | if (vartype == VAR_LIST |
| 236 | && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST |
| 237 | && type1->tt_member != type2->tt_member) |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 238 | set_type_on_stack(cctx, &t_list_any, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 239 | |
| 240 | return isn == NULL ? FAIL : OK; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Get the type to use for an instruction for an operation on "type1" and |
| 245 | * "type2". If they are matching use a type-specific instruction. Otherwise |
| 246 | * fall back to runtime type checking. |
| 247 | */ |
| 248 | vartype_T |
| 249 | operator_type(type_T *type1, type_T *type2) |
| 250 | { |
| 251 | if (type1->tt_type == type2->tt_type |
| 252 | && (type1->tt_type == VAR_NUMBER |
| 253 | || type1->tt_type == VAR_LIST |
| 254 | #ifdef FEAT_FLOAT |
| 255 | || type1->tt_type == VAR_FLOAT |
| 256 | #endif |
| 257 | || type1->tt_type == VAR_BLOB)) |
| 258 | return type1->tt_type; |
| 259 | return VAR_ANY; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Generate an instruction with two arguments. The instruction depends on the |
| 264 | * type of the arguments. |
| 265 | */ |
| 266 | int |
| 267 | generate_two_op(cctx_T *cctx, char_u *op) |
| 268 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 269 | type_T *type1; |
| 270 | type_T *type2; |
| 271 | vartype_T vartype; |
| 272 | isn_T *isn; |
| 273 | |
| 274 | RETURN_OK_IF_SKIP(cctx); |
| 275 | |
| 276 | // Get the known type of the two items on the stack. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 277 | type1 = get_type_on_stack(cctx, 1); |
| 278 | type2 = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 279 | vartype = operator_type(type1, type2); |
| 280 | |
| 281 | switch (*op) |
| 282 | { |
| 283 | case '+': |
| 284 | if (generate_add_instr(cctx, vartype, type1, type2, |
| 285 | EXPR_COPY) == FAIL) |
| 286 | return FAIL; |
| 287 | break; |
| 288 | |
| 289 | case '-': |
| 290 | case '*': |
| 291 | case '/': if (check_number_or_float(type1->tt_type, type2->tt_type, |
| 292 | op) == FAIL) |
| 293 | return FAIL; |
| 294 | if (vartype == VAR_NUMBER) |
| 295 | isn = generate_instr_drop(cctx, ISN_OPNR, 1); |
| 296 | #ifdef FEAT_FLOAT |
| 297 | else if (vartype == VAR_FLOAT) |
| 298 | isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1); |
| 299 | #endif |
| 300 | else |
| 301 | isn = generate_instr_drop(cctx, ISN_OPANY, 1); |
| 302 | if (isn != NULL) |
| 303 | isn->isn_arg.op.op_type = *op == '*' |
| 304 | ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB; |
| 305 | break; |
| 306 | |
| 307 | case '%': if ((type1->tt_type != VAR_ANY |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 308 | && type1->tt_type != VAR_UNKNOWN |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 309 | && type1->tt_type != VAR_NUMBER) |
| 310 | || (type2->tt_type != VAR_ANY |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 311 | && type2->tt_type != VAR_UNKNOWN |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 312 | && type2->tt_type != VAR_NUMBER)) |
| 313 | { |
| 314 | emsg(_(e_percent_requires_number_arguments)); |
| 315 | return FAIL; |
| 316 | } |
| 317 | isn = generate_instr_drop(cctx, |
| 318 | vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1); |
| 319 | if (isn != NULL) |
| 320 | isn->isn_arg.op.op_type = EXPR_REM; |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | // correct type of result |
| 325 | if (vartype == VAR_ANY) |
| 326 | { |
| 327 | type_T *type = &t_any; |
| 328 | |
| 329 | #ifdef FEAT_FLOAT |
| 330 | // float+number and number+float results in float |
| 331 | if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT) |
| 332 | && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT)) |
| 333 | type = &t_float; |
| 334 | #endif |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 335 | set_type_on_stack(cctx, type, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return OK; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Get the instruction to use for comparing "type1" with "type2" |
| 343 | * Return ISN_DROP when failed. |
| 344 | */ |
| 345 | static isntype_T |
| 346 | get_compare_isn(exprtype_T exprtype, vartype_T type1, vartype_T type2) |
| 347 | { |
| 348 | isntype_T isntype = ISN_DROP; |
| 349 | |
| 350 | if (type1 == VAR_UNKNOWN) |
| 351 | type1 = VAR_ANY; |
| 352 | if (type2 == VAR_UNKNOWN) |
| 353 | type2 = VAR_ANY; |
| 354 | |
| 355 | if (type1 == type2) |
| 356 | { |
| 357 | switch (type1) |
| 358 | { |
| 359 | case VAR_BOOL: isntype = ISN_COMPAREBOOL; break; |
| 360 | case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break; |
| 361 | case VAR_NUMBER: isntype = ISN_COMPARENR; break; |
| 362 | case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break; |
| 363 | case VAR_STRING: isntype = ISN_COMPARESTRING; break; |
| 364 | case VAR_BLOB: isntype = ISN_COMPAREBLOB; break; |
| 365 | case VAR_LIST: isntype = ISN_COMPARELIST; break; |
| 366 | case VAR_DICT: isntype = ISN_COMPAREDICT; break; |
| 367 | case VAR_FUNC: isntype = ISN_COMPAREFUNC; break; |
| 368 | default: isntype = ISN_COMPAREANY; break; |
| 369 | } |
| 370 | } |
| 371 | else if (type1 == VAR_ANY || type2 == VAR_ANY |
| 372 | || ((type1 == VAR_NUMBER || type1 == VAR_FLOAT) |
| 373 | && (type2 == VAR_NUMBER || type2 == VAR_FLOAT))) |
| 374 | isntype = ISN_COMPAREANY; |
| 375 | |
| 376 | if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT) |
| 377 | && (isntype == ISN_COMPAREBOOL |
| 378 | || isntype == ISN_COMPARESPECIAL |
| 379 | || isntype == ISN_COMPARENR |
| 380 | || isntype == ISN_COMPAREFLOAT)) |
| 381 | { |
| 382 | semsg(_(e_cannot_use_str_with_str), |
| 383 | exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(type1)); |
| 384 | return ISN_DROP; |
| 385 | } |
| 386 | if (isntype == ISN_DROP |
| 387 | || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL |
| 388 | && (type1 == VAR_BOOL || type1 == VAR_SPECIAL |
| 389 | || type2 == VAR_BOOL || type2 == VAR_SPECIAL))) |
| 390 | || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL |
| 391 | && exprtype != EXPR_IS && exprtype != EXPR_ISNOT |
| 392 | && (type1 == VAR_BLOB || type2 == VAR_BLOB |
| 393 | || type1 == VAR_LIST || type2 == VAR_LIST)))) |
| 394 | { |
| 395 | semsg(_(e_cannot_compare_str_with_str), |
| 396 | vartype_name(type1), vartype_name(type2)); |
| 397 | return ISN_DROP; |
| 398 | } |
| 399 | return isntype; |
| 400 | } |
| 401 | |
| 402 | int |
| 403 | check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2) |
| 404 | { |
| 405 | if (get_compare_isn(type, tv1->v_type, tv2->v_type) == ISN_DROP) |
| 406 | return FAIL; |
| 407 | return OK; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Generate an ISN_COMPARE* instruction with a boolean result. |
| 412 | */ |
| 413 | int |
| 414 | generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic) |
| 415 | { |
| 416 | isntype_T isntype; |
| 417 | isn_T *isn; |
| 418 | garray_T *stack = &cctx->ctx_type_stack; |
| 419 | vartype_T type1; |
| 420 | vartype_T type2; |
| 421 | |
| 422 | RETURN_OK_IF_SKIP(cctx); |
| 423 | |
| 424 | // Get the known type of the two items on the stack. If they are matching |
| 425 | // use a type-specific instruction. Otherwise fall back to runtime type |
| 426 | // checking. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 427 | type1 = get_type_on_stack(cctx, 1)->tt_type; |
| 428 | type2 = get_type_on_stack(cctx, 0)->tt_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 429 | isntype = get_compare_isn(exprtype, type1, type2); |
| 430 | if (isntype == ISN_DROP) |
| 431 | return FAIL; |
| 432 | |
| 433 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 434 | return FAIL; |
| 435 | isn->isn_arg.op.op_type = exprtype; |
| 436 | isn->isn_arg.op.op_ic = ic; |
| 437 | |
| 438 | // takes two arguments, puts one bool back |
| 439 | if (stack->ga_len >= 2) |
| 440 | { |
| 441 | --stack->ga_len; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 442 | set_type_on_stack(cctx, &t_bool, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | return OK; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Generate an ISN_2BOOL instruction. |
| 450 | * "offset" is the offset in the type stack. |
| 451 | */ |
| 452 | int |
| 453 | generate_2BOOL(cctx_T *cctx, int invert, int offset) |
| 454 | { |
| 455 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 456 | |
| 457 | RETURN_OK_IF_SKIP(cctx); |
| 458 | if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL) |
| 459 | return FAIL; |
| 460 | isn->isn_arg.tobool.invert = invert; |
| 461 | isn->isn_arg.tobool.offset = offset; |
| 462 | |
| 463 | // type becomes bool |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 464 | set_type_on_stack(cctx, &t_bool, -1 - offset); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 465 | |
| 466 | return OK; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Generate an ISN_COND2BOOL instruction. |
| 471 | */ |
| 472 | int |
| 473 | generate_COND2BOOL(cctx_T *cctx) |
| 474 | { |
| 475 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 476 | |
| 477 | RETURN_OK_IF_SKIP(cctx); |
| 478 | if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL) |
| 479 | return FAIL; |
| 480 | |
| 481 | // type becomes bool |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 482 | set_type_on_stack(cctx, &t_bool, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 483 | |
| 484 | return OK; |
| 485 | } |
| 486 | |
| 487 | int |
| 488 | generate_TYPECHECK( |
| 489 | cctx_T *cctx, |
| 490 | type_T *expected, |
| 491 | int offset, |
| 492 | int argidx) |
| 493 | { |
| 494 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 495 | |
| 496 | RETURN_OK_IF_SKIP(cctx); |
| 497 | if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL) |
| 498 | return FAIL; |
| 499 | isn->isn_arg.type.ct_type = alloc_type(expected); |
| 500 | isn->isn_arg.type.ct_off = (int8_T)offset; |
| 501 | isn->isn_arg.type.ct_arg_idx = (int8_T)argidx; |
| 502 | |
| 503 | // type becomes expected |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 504 | set_type_on_stack(cctx, expected, -1 - offset); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 505 | |
| 506 | return OK; |
| 507 | } |
| 508 | |
| 509 | int |
| 510 | generate_SETTYPE( |
| 511 | cctx_T *cctx, |
| 512 | type_T *expected) |
| 513 | { |
| 514 | isn_T *isn; |
| 515 | |
| 516 | RETURN_OK_IF_SKIP(cctx); |
| 517 | if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL) |
| 518 | return FAIL; |
| 519 | isn->isn_arg.type.ct_type = alloc_type(expected); |
| 520 | return OK; |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * Generate a PUSH instruction for "tv". |
| 525 | * "tv" will be consumed or cleared. |
| 526 | * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN; |
| 527 | */ |
| 528 | int |
| 529 | generate_tv_PUSH(cctx_T *cctx, typval_T *tv) |
| 530 | { |
| 531 | if (tv != NULL) |
| 532 | { |
| 533 | switch (tv->v_type) |
| 534 | { |
| 535 | case VAR_UNKNOWN: |
| 536 | break; |
| 537 | case VAR_BOOL: |
| 538 | generate_PUSHBOOL(cctx, tv->vval.v_number); |
| 539 | break; |
| 540 | case VAR_SPECIAL: |
| 541 | generate_PUSHSPEC(cctx, tv->vval.v_number); |
| 542 | break; |
| 543 | case VAR_NUMBER: |
| 544 | generate_PUSHNR(cctx, tv->vval.v_number); |
| 545 | break; |
| 546 | #ifdef FEAT_FLOAT |
| 547 | case VAR_FLOAT: |
| 548 | generate_PUSHF(cctx, tv->vval.v_float); |
| 549 | break; |
| 550 | #endif |
| 551 | case VAR_BLOB: |
| 552 | generate_PUSHBLOB(cctx, tv->vval.v_blob); |
| 553 | tv->vval.v_blob = NULL; |
| 554 | break; |
| 555 | case VAR_STRING: |
| 556 | generate_PUSHS(cctx, &tv->vval.v_string); |
| 557 | tv->vval.v_string = NULL; |
| 558 | break; |
| 559 | default: |
| 560 | iemsg("constant type not supported"); |
| 561 | clear_tv(tv); |
| 562 | return FAIL; |
| 563 | } |
| 564 | tv->v_type = VAR_UNKNOWN; |
| 565 | } |
| 566 | return OK; |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Generate an ISN_PUSHNR instruction. |
| 571 | */ |
| 572 | int |
| 573 | generate_PUSHNR(cctx_T *cctx, varnumber_T number) |
| 574 | { |
| 575 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 576 | |
| 577 | RETURN_OK_IF_SKIP(cctx); |
| 578 | if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL) |
| 579 | return FAIL; |
| 580 | isn->isn_arg.number = number; |
| 581 | |
| 582 | if (number == 0 || number == 1) |
| 583 | // A 0 or 1 number can also be used as a bool. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 584 | set_type_on_stack(cctx, &t_number_bool, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 585 | return OK; |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | * Generate an ISN_PUSHBOOL instruction. |
| 590 | */ |
| 591 | int |
| 592 | generate_PUSHBOOL(cctx_T *cctx, varnumber_T number) |
| 593 | { |
| 594 | isn_T *isn; |
| 595 | |
| 596 | RETURN_OK_IF_SKIP(cctx); |
| 597 | if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL) |
| 598 | return FAIL; |
| 599 | isn->isn_arg.number = number; |
| 600 | |
| 601 | return OK; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * Generate an ISN_PUSHSPEC instruction. |
| 606 | */ |
| 607 | int |
| 608 | generate_PUSHSPEC(cctx_T *cctx, varnumber_T number) |
| 609 | { |
| 610 | isn_T *isn; |
| 611 | |
| 612 | RETURN_OK_IF_SKIP(cctx); |
| 613 | if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC, &t_special)) == NULL) |
| 614 | return FAIL; |
| 615 | isn->isn_arg.number = number; |
| 616 | |
| 617 | return OK; |
| 618 | } |
| 619 | |
| 620 | #if defined(FEAT_FLOAT) || defined(PROTO) |
| 621 | /* |
| 622 | * Generate an ISN_PUSHF instruction. |
| 623 | */ |
| 624 | int |
| 625 | generate_PUSHF(cctx_T *cctx, float_T fnumber) |
| 626 | { |
| 627 | isn_T *isn; |
| 628 | |
| 629 | RETURN_OK_IF_SKIP(cctx); |
| 630 | if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL) |
| 631 | return FAIL; |
| 632 | isn->isn_arg.fnumber = fnumber; |
| 633 | |
| 634 | return OK; |
| 635 | } |
| 636 | #endif |
| 637 | |
| 638 | /* |
| 639 | * Generate an ISN_PUSHS instruction. |
| 640 | * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL. |
| 641 | */ |
| 642 | int |
| 643 | generate_PUSHS(cctx_T *cctx, char_u **str) |
| 644 | { |
| 645 | isn_T *isn; |
| 646 | |
| 647 | if (cctx->ctx_skip == SKIP_YES) |
| 648 | { |
| 649 | if (str != NULL) |
| 650 | VIM_CLEAR(*str); |
| 651 | return OK; |
| 652 | } |
| 653 | if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL) |
| 654 | { |
| 655 | if (str != NULL) |
| 656 | VIM_CLEAR(*str); |
| 657 | return FAIL; |
| 658 | } |
| 659 | isn->isn_arg.string = str == NULL ? NULL : *str; |
| 660 | |
| 661 | return OK; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Generate an ISN_PUSHCHANNEL instruction. |
| 666 | * Consumes "channel". |
| 667 | */ |
| 668 | int |
| 669 | generate_PUSHCHANNEL(cctx_T *cctx, channel_T *channel) |
| 670 | { |
| 671 | isn_T *isn; |
| 672 | |
| 673 | RETURN_OK_IF_SKIP(cctx); |
| 674 | if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL) |
| 675 | return FAIL; |
| 676 | isn->isn_arg.channel = channel; |
| 677 | |
| 678 | return OK; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Generate an ISN_PUSHJOB instruction. |
| 683 | * Consumes "job". |
| 684 | */ |
| 685 | int |
| 686 | generate_PUSHJOB(cctx_T *cctx, job_T *job) |
| 687 | { |
| 688 | isn_T *isn; |
| 689 | |
| 690 | RETURN_OK_IF_SKIP(cctx); |
| 691 | if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_channel)) == NULL) |
| 692 | return FAIL; |
| 693 | isn->isn_arg.job = job; |
| 694 | |
| 695 | return OK; |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * Generate an ISN_PUSHBLOB instruction. |
| 700 | * Consumes "blob". |
| 701 | */ |
| 702 | int |
| 703 | generate_PUSHBLOB(cctx_T *cctx, blob_T *blob) |
| 704 | { |
| 705 | isn_T *isn; |
| 706 | |
| 707 | RETURN_OK_IF_SKIP(cctx); |
| 708 | if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL) |
| 709 | return FAIL; |
| 710 | isn->isn_arg.blob = blob; |
| 711 | |
| 712 | return OK; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Generate an ISN_PUSHFUNC instruction with name "name". |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 717 | */ |
| 718 | int |
| 719 | generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type) |
| 720 | { |
| 721 | isn_T *isn; |
| 722 | char_u *funcname; |
| 723 | |
| 724 | RETURN_OK_IF_SKIP(cctx); |
| 725 | if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL) |
| 726 | return FAIL; |
| 727 | if (name == NULL) |
| 728 | funcname = NULL; |
Bram Moolenaar | d041f42 | 2022-01-12 19:54:00 +0000 | [diff] [blame] | 729 | else if (*name == K_SPECIAL // script-local |
| 730 | || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 731 | funcname = vim_strsave(name); |
| 732 | else |
| 733 | { |
| 734 | funcname = alloc(STRLEN(name) + 3); |
| 735 | if (funcname != NULL) |
| 736 | { |
| 737 | STRCPY(funcname, "g:"); |
| 738 | STRCPY(funcname + 2, name); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | isn->isn_arg.string = funcname; |
| 743 | return OK; |
| 744 | } |
| 745 | |
| 746 | /* |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 747 | * Generate an ISN_AUTOLOAD instruction. |
| 748 | */ |
| 749 | int |
| 750 | generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type) |
| 751 | { |
| 752 | isn_T *isn; |
| 753 | |
| 754 | RETURN_OK_IF_SKIP(cctx); |
| 755 | if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL) |
| 756 | return FAIL; |
| 757 | isn->isn_arg.string = vim_strsave(name); |
| 758 | if (isn->isn_arg.string == NULL) |
| 759 | return FAIL; |
| 760 | return OK; |
| 761 | } |
| 762 | |
| 763 | /* |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 764 | * Generate an ISN_GETITEM instruction with "index". |
| 765 | * "with_op" is TRUE for "+=" and other operators, the stack has the current |
| 766 | * value below the list with values. |
| 767 | */ |
| 768 | int |
| 769 | generate_GETITEM(cctx_T *cctx, int index, int with_op) |
| 770 | { |
| 771 | isn_T *isn; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 772 | type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 773 | type_T *item_type = &t_any; |
| 774 | |
| 775 | RETURN_OK_IF_SKIP(cctx); |
| 776 | |
| 777 | if (type->tt_type != VAR_LIST) |
| 778 | { |
| 779 | // cannot happen, caller has checked the type |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 780 | emsg(_(e_list_required)); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 781 | return FAIL; |
| 782 | } |
| 783 | item_type = type->tt_member; |
| 784 | if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL) |
| 785 | return FAIL; |
| 786 | isn->isn_arg.getitem.gi_index = index; |
| 787 | isn->isn_arg.getitem.gi_with_op = with_op; |
| 788 | |
| 789 | // add the item type to the type stack |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 790 | return push_type_stack(cctx, item_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /* |
| 794 | * Generate an ISN_SLICE instruction with "count". |
| 795 | */ |
| 796 | int |
| 797 | generate_SLICE(cctx_T *cctx, int count) |
| 798 | { |
| 799 | isn_T *isn; |
| 800 | |
| 801 | RETURN_OK_IF_SKIP(cctx); |
| 802 | if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL) |
| 803 | return FAIL; |
| 804 | isn->isn_arg.number = count; |
| 805 | return OK; |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Generate an ISN_CHECKLEN instruction with "min_len". |
| 810 | */ |
| 811 | int |
| 812 | generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK) |
| 813 | { |
| 814 | isn_T *isn; |
| 815 | |
| 816 | RETURN_OK_IF_SKIP(cctx); |
| 817 | |
| 818 | if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL) |
| 819 | return FAIL; |
| 820 | isn->isn_arg.checklen.cl_min_len = min_len; |
| 821 | isn->isn_arg.checklen.cl_more_OK = more_OK; |
| 822 | |
| 823 | return OK; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Generate an ISN_STORE instruction. |
| 828 | */ |
| 829 | int |
| 830 | generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name) |
| 831 | { |
| 832 | isn_T *isn; |
| 833 | |
| 834 | RETURN_OK_IF_SKIP(cctx); |
| 835 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 836 | return FAIL; |
| 837 | if (name != NULL) |
| 838 | isn->isn_arg.string = vim_strsave(name); |
| 839 | else |
| 840 | isn->isn_arg.number = idx; |
| 841 | |
| 842 | return OK; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * Generate an ISN_STOREOUTER instruction. |
| 847 | */ |
Yegappan Lakshmanan | 782b43d | 2022-01-08 18:43:40 +0000 | [diff] [blame] | 848 | static int |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 849 | generate_STOREOUTER(cctx_T *cctx, int idx, int level) |
| 850 | { |
| 851 | isn_T *isn; |
| 852 | |
| 853 | RETURN_OK_IF_SKIP(cctx); |
| 854 | if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL) |
| 855 | return FAIL; |
| 856 | isn->isn_arg.outer.outer_idx = idx; |
| 857 | isn->isn_arg.outer.outer_depth = level; |
| 858 | |
| 859 | return OK; |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE) |
| 864 | */ |
| 865 | int |
| 866 | generate_STORENR(cctx_T *cctx, int idx, varnumber_T value) |
| 867 | { |
| 868 | isn_T *isn; |
| 869 | |
| 870 | RETURN_OK_IF_SKIP(cctx); |
| 871 | if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL) |
| 872 | return FAIL; |
| 873 | isn->isn_arg.storenr.stnr_idx = idx; |
| 874 | isn->isn_arg.storenr.stnr_val = value; |
| 875 | |
| 876 | return OK; |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction |
| 881 | */ |
Yegappan Lakshmanan | 782b43d | 2022-01-08 18:43:40 +0000 | [diff] [blame] | 882 | static int |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 883 | generate_STOREOPT( |
| 884 | cctx_T *cctx, |
| 885 | isntype_T isn_type, |
| 886 | char_u *name, |
| 887 | int opt_flags) |
| 888 | { |
| 889 | isn_T *isn; |
| 890 | |
| 891 | RETURN_OK_IF_SKIP(cctx); |
| 892 | if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL) |
| 893 | return FAIL; |
| 894 | isn->isn_arg.storeopt.so_name = vim_strsave(name); |
| 895 | isn->isn_arg.storeopt.so_flags = opt_flags; |
| 896 | |
| 897 | return OK; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Generate an ISN_LOAD or similar instruction. |
| 902 | */ |
| 903 | int |
| 904 | generate_LOAD( |
| 905 | cctx_T *cctx, |
| 906 | isntype_T isn_type, |
| 907 | int idx, |
| 908 | char_u *name, |
| 909 | type_T *type) |
| 910 | { |
| 911 | isn_T *isn; |
| 912 | |
| 913 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 914 | if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 915 | return FAIL; |
| 916 | if (name != NULL) |
| 917 | isn->isn_arg.string = vim_strsave(name); |
| 918 | else |
| 919 | isn->isn_arg.number = idx; |
| 920 | |
| 921 | return OK; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * Generate an ISN_LOADOUTER instruction |
| 926 | */ |
| 927 | int |
| 928 | generate_LOADOUTER( |
| 929 | cctx_T *cctx, |
| 930 | int idx, |
| 931 | int nesting, |
| 932 | type_T *type) |
| 933 | { |
| 934 | isn_T *isn; |
| 935 | |
| 936 | RETURN_OK_IF_SKIP(cctx); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 937 | if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 938 | return FAIL; |
| 939 | isn->isn_arg.outer.outer_idx = idx; |
| 940 | isn->isn_arg.outer.outer_depth = nesting; |
| 941 | |
| 942 | return OK; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Generate an ISN_LOADV instruction for v:var. |
| 947 | */ |
| 948 | int |
| 949 | generate_LOADV( |
| 950 | cctx_T *cctx, |
| 951 | char_u *name, |
| 952 | int error) |
| 953 | { |
| 954 | int di_flags; |
| 955 | int vidx = find_vim_var(name, &di_flags); |
| 956 | type_T *type; |
| 957 | |
| 958 | RETURN_OK_IF_SKIP(cctx); |
| 959 | if (vidx < 0) |
| 960 | { |
| 961 | if (error) |
| 962 | semsg(_(e_variable_not_found_str), name); |
| 963 | return FAIL; |
| 964 | } |
Bram Moolenaar | d787e40 | 2021-12-24 21:36:12 +0000 | [diff] [blame] | 965 | type = get_vim_var_type(vidx, cctx->ctx_type_list); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 966 | return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type); |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * Generate an ISN_UNLET instruction. |
| 971 | */ |
| 972 | int |
| 973 | generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit) |
| 974 | { |
| 975 | isn_T *isn; |
| 976 | |
| 977 | RETURN_OK_IF_SKIP(cctx); |
| 978 | if ((isn = generate_instr(cctx, isn_type)) == NULL) |
| 979 | return FAIL; |
| 980 | isn->isn_arg.unlet.ul_name = vim_strsave(name); |
| 981 | isn->isn_arg.unlet.ul_forceit = forceit; |
| 982 | |
| 983 | return OK; |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * Generate an ISN_LOCKCONST instruction. |
| 988 | */ |
| 989 | int |
| 990 | generate_LOCKCONST(cctx_T *cctx) |
| 991 | { |
| 992 | isn_T *isn; |
| 993 | |
| 994 | RETURN_OK_IF_SKIP(cctx); |
| 995 | if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL) |
| 996 | return FAIL; |
| 997 | return OK; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * Generate an ISN_LOADS instruction. |
| 1002 | */ |
| 1003 | int |
| 1004 | generate_OLDSCRIPT( |
| 1005 | cctx_T *cctx, |
| 1006 | isntype_T isn_type, |
| 1007 | char_u *name, |
| 1008 | int sid, |
| 1009 | type_T *type) |
| 1010 | { |
| 1011 | isn_T *isn; |
| 1012 | |
| 1013 | RETURN_OK_IF_SKIP(cctx); |
| 1014 | if (isn_type == ISN_LOADS) |
| 1015 | isn = generate_instr_type(cctx, isn_type, type); |
| 1016 | else |
| 1017 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1018 | if (isn == NULL) |
| 1019 | return FAIL; |
| 1020 | isn->isn_arg.loadstore.ls_name = vim_strsave(name); |
| 1021 | isn->isn_arg.loadstore.ls_sid = sid; |
| 1022 | |
| 1023 | return OK; |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction. |
| 1028 | */ |
| 1029 | int |
| 1030 | generate_VIM9SCRIPT( |
| 1031 | cctx_T *cctx, |
| 1032 | isntype_T isn_type, |
| 1033 | int sid, |
| 1034 | int idx, |
| 1035 | type_T *type) |
| 1036 | { |
| 1037 | isn_T *isn; |
| 1038 | scriptref_T *sref; |
| 1039 | scriptitem_T *si = SCRIPT_ITEM(sid); |
| 1040 | |
| 1041 | RETURN_OK_IF_SKIP(cctx); |
| 1042 | if (isn_type == ISN_LOADSCRIPT) |
| 1043 | isn = generate_instr_type(cctx, isn_type, type); |
| 1044 | else |
| 1045 | isn = generate_instr_drop(cctx, isn_type, 1); |
| 1046 | if (isn == NULL) |
| 1047 | return FAIL; |
| 1048 | |
| 1049 | // This requires three arguments, which doesn't fit in an instruction, thus |
| 1050 | // we need to allocate a struct for this. |
| 1051 | sref = ALLOC_ONE(scriptref_T); |
| 1052 | if (sref == NULL) |
| 1053 | return FAIL; |
| 1054 | isn->isn_arg.script.scriptref = sref; |
| 1055 | sref->sref_sid = sid; |
| 1056 | sref->sref_idx = idx; |
| 1057 | sref->sref_seq = si->sn_script_seq; |
| 1058 | sref->sref_type = type; |
| 1059 | return OK; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * Generate an ISN_NEWLIST instruction. |
| 1064 | */ |
| 1065 | int |
| 1066 | generate_NEWLIST(cctx_T *cctx, int count) |
| 1067 | { |
| 1068 | isn_T *isn; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1069 | type_T *member_type; |
| 1070 | type_T *decl_member_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1071 | type_T *type; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1072 | type_T *decl_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1073 | |
| 1074 | RETURN_OK_IF_SKIP(cctx); |
| 1075 | if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL) |
| 1076 | return FAIL; |
| 1077 | isn->isn_arg.number = count; |
| 1078 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1079 | // Get the member type and the declared member type from all the items on |
| 1080 | // the stack. |
| 1081 | member_type = get_member_type_from_stack(count, 1, &decl_member_type, cctx); |
| 1082 | type = get_list_type(member_type, cctx->ctx_type_list); |
| 1083 | decl_type = get_list_type(decl_member_type, cctx->ctx_type_list); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1084 | |
| 1085 | // drop the value types |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1086 | cctx->ctx_type_stack.ga_len -= count; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1087 | |
| 1088 | // add the list type to the type stack |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1089 | return push_type_stack2(cctx, type, decl_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * Generate an ISN_NEWDICT instruction. |
| 1094 | */ |
| 1095 | int |
| 1096 | generate_NEWDICT(cctx_T *cctx, int count) |
| 1097 | { |
| 1098 | isn_T *isn; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1099 | type_T *member_type; |
| 1100 | type_T *decl_member_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1101 | type_T *type; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1102 | type_T *decl_type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1103 | |
| 1104 | RETURN_OK_IF_SKIP(cctx); |
| 1105 | if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL) |
| 1106 | return FAIL; |
| 1107 | isn->isn_arg.number = count; |
| 1108 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1109 | member_type = get_member_type_from_stack(count, 2, |
| 1110 | &decl_member_type, cctx); |
| 1111 | type = get_dict_type(member_type, cctx->ctx_type_list); |
| 1112 | decl_type = get_dict_type(decl_member_type, cctx->ctx_type_list); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1113 | |
| 1114 | // drop the key and value types |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1115 | cctx->ctx_type_stack.ga_len -= 2 * count; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1116 | |
| 1117 | // add the dict type to the type stack |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1118 | return push_type_stack2(cctx, type, decl_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * Generate an ISN_FUNCREF instruction. |
| 1123 | */ |
| 1124 | int |
| 1125 | generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc) |
| 1126 | { |
| 1127 | isn_T *isn; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1128 | type_T *type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1129 | |
| 1130 | RETURN_OK_IF_SKIP(cctx); |
| 1131 | if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL) |
| 1132 | return FAIL; |
| 1133 | if (ufunc->uf_def_status == UF_NOT_COMPILED) |
| 1134 | isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name); |
| 1135 | else |
| 1136 | isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx; |
| 1137 | cctx->ctx_has_closure = 1; |
| 1138 | |
| 1139 | // If the referenced function is a closure, it may use items further up in |
| 1140 | // the nested context, including this one. |
| 1141 | if (ufunc->uf_flags & FC_CLOSURE) |
| 1142 | cctx->ctx_ufunc->uf_flags |= FC_CLOSURE; |
| 1143 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1144 | type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type; |
| 1145 | return push_type_stack(cctx, type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | * Generate an ISN_NEWFUNC instruction. |
| 1150 | * "lambda_name" and "func_name" must be in allocated memory and will be |
| 1151 | * consumed. |
| 1152 | */ |
| 1153 | int |
| 1154 | generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name) |
| 1155 | { |
| 1156 | isn_T *isn; |
| 1157 | |
| 1158 | if (cctx->ctx_skip == SKIP_YES) |
| 1159 | { |
| 1160 | vim_free(lambda_name); |
| 1161 | vim_free(func_name); |
| 1162 | return OK; |
| 1163 | } |
| 1164 | if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL) |
| 1165 | { |
| 1166 | vim_free(lambda_name); |
| 1167 | vim_free(func_name); |
| 1168 | return FAIL; |
| 1169 | } |
| 1170 | isn->isn_arg.newfunc.nf_lambda = lambda_name; |
| 1171 | isn->isn_arg.newfunc.nf_global = func_name; |
| 1172 | |
| 1173 | return OK; |
| 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | * Generate an ISN_DEF instruction: list functions |
| 1178 | */ |
| 1179 | int |
| 1180 | generate_DEF(cctx_T *cctx, char_u *name, size_t len) |
| 1181 | { |
| 1182 | isn_T *isn; |
| 1183 | |
| 1184 | RETURN_OK_IF_SKIP(cctx); |
| 1185 | if ((isn = generate_instr(cctx, ISN_DEF)) == NULL) |
| 1186 | return FAIL; |
| 1187 | if (len > 0) |
| 1188 | { |
| 1189 | isn->isn_arg.string = vim_strnsave(name, len); |
| 1190 | if (isn->isn_arg.string == NULL) |
| 1191 | return FAIL; |
| 1192 | } |
| 1193 | return OK; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * Generate an ISN_JUMP instruction. |
| 1198 | */ |
| 1199 | int |
| 1200 | generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where) |
| 1201 | { |
| 1202 | isn_T *isn; |
| 1203 | garray_T *stack = &cctx->ctx_type_stack; |
| 1204 | |
| 1205 | RETURN_OK_IF_SKIP(cctx); |
| 1206 | if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL) |
| 1207 | return FAIL; |
| 1208 | isn->isn_arg.jump.jump_when = when; |
| 1209 | isn->isn_arg.jump.jump_where = where; |
| 1210 | |
| 1211 | if (when != JUMP_ALWAYS && stack->ga_len > 0) |
| 1212 | --stack->ga_len; |
| 1213 | |
| 1214 | return OK; |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * Generate an ISN_JUMP_IF_ARG_SET instruction. |
| 1219 | */ |
| 1220 | int |
| 1221 | generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off) |
| 1222 | { |
| 1223 | isn_T *isn; |
| 1224 | |
| 1225 | RETURN_OK_IF_SKIP(cctx); |
| 1226 | if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL) |
| 1227 | return FAIL; |
| 1228 | isn->isn_arg.jumparg.jump_arg_off = arg_off; |
| 1229 | // jump_where is set later |
| 1230 | return OK; |
| 1231 | } |
| 1232 | |
| 1233 | int |
| 1234 | generate_FOR(cctx_T *cctx, int loop_idx) |
| 1235 | { |
| 1236 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1237 | |
| 1238 | RETURN_OK_IF_SKIP(cctx); |
| 1239 | if ((isn = generate_instr(cctx, ISN_FOR)) == NULL) |
| 1240 | return FAIL; |
| 1241 | isn->isn_arg.forloop.for_idx = loop_idx; |
| 1242 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1243 | // type doesn't matter, will be stored next |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1244 | return push_type_stack(cctx, &t_any); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1245 | } |
| 1246 | /* |
| 1247 | * Generate an ISN_TRYCONT instruction. |
| 1248 | */ |
| 1249 | int |
| 1250 | generate_TRYCONT(cctx_T *cctx, int levels, int where) |
| 1251 | { |
| 1252 | isn_T *isn; |
| 1253 | |
| 1254 | RETURN_OK_IF_SKIP(cctx); |
| 1255 | if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL) |
| 1256 | return FAIL; |
| 1257 | isn->isn_arg.trycont.tct_levels = levels; |
| 1258 | isn->isn_arg.trycont.tct_where = where; |
| 1259 | |
| 1260 | return OK; |
| 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | /* |
| 1265 | * Generate an ISN_BCALL instruction. |
| 1266 | * "method_call" is TRUE for "value->method()" |
| 1267 | * Return FAIL if the number of arguments is wrong. |
| 1268 | */ |
| 1269 | int |
| 1270 | generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call) |
| 1271 | { |
| 1272 | isn_T *isn; |
| 1273 | garray_T *stack = &cctx->ctx_type_stack; |
| 1274 | int argoff; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1275 | type2_T *typep; |
| 1276 | type2_T *argtypes = NULL; |
| 1277 | type2_T shuffled_argtypes[MAX_FUNC_ARGS]; |
| 1278 | type2_T *maptype = NULL; |
| 1279 | type_T *type; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1280 | |
| 1281 | RETURN_OK_IF_SKIP(cctx); |
| 1282 | argoff = check_internal_func(func_idx, argcount); |
| 1283 | if (argoff < 0) |
| 1284 | return FAIL; |
| 1285 | |
| 1286 | if (method_call && argoff > 1) |
| 1287 | { |
| 1288 | if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL) |
| 1289 | return FAIL; |
| 1290 | isn->isn_arg.shuffle.shfl_item = argcount; |
| 1291 | isn->isn_arg.shuffle.shfl_up = argoff - 1; |
| 1292 | } |
| 1293 | |
| 1294 | if (argcount > 0) |
| 1295 | { |
| 1296 | // Check the types of the arguments. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1297 | typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1298 | if (method_call && argoff > 1) |
| 1299 | { |
| 1300 | int i; |
| 1301 | |
| 1302 | for (i = 0; i < argcount; ++i) |
| 1303 | shuffled_argtypes[i] = (i < argoff - 1) |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1304 | ? typep[i + 1] |
| 1305 | : (i == argoff - 1) ? typep[0] : typep[i]; |
| 1306 | argtypes = shuffled_argtypes; |
| 1307 | } |
| 1308 | else |
| 1309 | { |
| 1310 | int i; |
| 1311 | |
| 1312 | for (i = 0; i < argcount; ++i) |
| 1313 | shuffled_argtypes[i] = typep[i]; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1314 | argtypes = shuffled_argtypes; |
| 1315 | } |
| 1316 | if (internal_func_check_arg_types(argtypes, func_idx, argcount, |
| 1317 | cctx) == FAIL) |
| 1318 | return FAIL; |
| 1319 | if (internal_func_is_map(func_idx)) |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1320 | maptype = argtypes; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL) |
| 1324 | return FAIL; |
| 1325 | isn->isn_arg.bfunc.cbf_idx = func_idx; |
| 1326 | isn->isn_arg.bfunc.cbf_argcount = argcount; |
| 1327 | |
| 1328 | // Drop the argument types and push the return type. |
| 1329 | stack->ga_len -= argcount; |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1330 | type = internal_func_ret_type(func_idx, argcount, argtypes); |
| 1331 | if (push_type_stack(cctx, type) == FAIL) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1332 | return FAIL; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1333 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1334 | if (maptype != NULL && maptype[0].type_curr->tt_member != NULL |
| 1335 | && maptype[0].type_curr->tt_member != &t_any) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1336 | // Check that map() didn't change the item types. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1337 | generate_TYPECHECK(cctx, maptype[0].type_curr, -1, 1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1338 | |
| 1339 | return OK; |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | * Generate an ISN_LISTAPPEND instruction. Works like add(). |
| 1344 | * Argument count is already checked. |
| 1345 | */ |
| 1346 | int |
| 1347 | generate_LISTAPPEND(cctx_T *cctx) |
| 1348 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1349 | type_T *list_type; |
| 1350 | type_T *item_type; |
| 1351 | type_T *expected; |
| 1352 | |
| 1353 | // Caller already checked that list_type is a list. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1354 | list_type = get_type_on_stack(cctx, 1); |
| 1355 | item_type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1356 | expected = list_type->tt_member; |
| 1357 | if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL) |
| 1358 | return FAIL; |
| 1359 | |
| 1360 | if (generate_instr(cctx, ISN_LISTAPPEND) == NULL) |
| 1361 | return FAIL; |
| 1362 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1363 | --cctx->ctx_type_stack.ga_len; // drop the argument |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1364 | return OK; |
| 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | * Generate an ISN_BLOBAPPEND instruction. Works like add(). |
| 1369 | * Argument count is already checked. |
| 1370 | */ |
| 1371 | int |
| 1372 | generate_BLOBAPPEND(cctx_T *cctx) |
| 1373 | { |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1374 | type_T *item_type; |
| 1375 | |
| 1376 | // Caller already checked that blob_type is a blob. |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1377 | item_type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1378 | if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL) |
| 1379 | return FAIL; |
| 1380 | |
| 1381 | if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL) |
| 1382 | return FAIL; |
| 1383 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1384 | --cctx->ctx_type_stack.ga_len; // drop the argument |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1385 | return OK; |
| 1386 | } |
| 1387 | |
| 1388 | /* |
| 1389 | * Generate an ISN_DCALL or ISN_UCALL instruction. |
| 1390 | * Return FAIL if the number of arguments is wrong. |
| 1391 | */ |
| 1392 | int |
| 1393 | generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount) |
| 1394 | { |
| 1395 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1396 | int regular_args = ufunc->uf_args.ga_len; |
| 1397 | int argcount = pushed_argcount; |
| 1398 | |
| 1399 | RETURN_OK_IF_SKIP(cctx); |
| 1400 | if (argcount > regular_args && !has_varargs(ufunc)) |
| 1401 | { |
| 1402 | semsg(_(e_too_many_arguments_for_function_str), |
| 1403 | printable_func_name(ufunc)); |
| 1404 | return FAIL; |
| 1405 | } |
| 1406 | if (argcount < regular_args - ufunc->uf_def_args.ga_len) |
| 1407 | { |
| 1408 | semsg(_(e_not_enough_arguments_for_function_str), |
| 1409 | printable_func_name(ufunc)); |
| 1410 | return FAIL; |
| 1411 | } |
| 1412 | |
| 1413 | if (ufunc->uf_def_status != UF_NOT_COMPILED |
| 1414 | && ufunc->uf_def_status != UF_COMPILE_ERROR) |
| 1415 | { |
| 1416 | int i; |
| 1417 | |
| 1418 | for (i = 0; i < argcount; ++i) |
| 1419 | { |
| 1420 | type_T *expected; |
| 1421 | type_T *actual; |
| 1422 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1423 | actual = get_type_on_stack(cctx, argcount - i - 1); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1424 | if (actual == &t_special |
| 1425 | && i >= regular_args - ufunc->uf_def_args.ga_len) |
| 1426 | { |
| 1427 | // assume v:none used for default argument value |
| 1428 | continue; |
| 1429 | } |
| 1430 | if (i < regular_args) |
| 1431 | { |
| 1432 | if (ufunc->uf_arg_types == NULL) |
| 1433 | continue; |
| 1434 | expected = ufunc->uf_arg_types[i]; |
| 1435 | } |
| 1436 | else if (ufunc->uf_va_type == NULL |
| 1437 | || ufunc->uf_va_type == &t_list_any) |
| 1438 | // possibly a lambda or "...: any" |
| 1439 | expected = &t_any; |
| 1440 | else |
| 1441 | expected = ufunc->uf_va_type->tt_member; |
| 1442 | if (need_type(actual, expected, -argcount + i, i + 1, cctx, |
| 1443 | TRUE, FALSE) == FAIL) |
| 1444 | { |
| 1445 | arg_type_mismatch(expected, actual, i + 1); |
| 1446 | return FAIL; |
| 1447 | } |
| 1448 | } |
| 1449 | if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)) |
| 1450 | && compile_def_function(ufunc, ufunc->uf_ret_type == NULL, |
| 1451 | COMPILE_TYPE(ufunc), NULL) == FAIL) |
| 1452 | return FAIL; |
| 1453 | } |
| 1454 | if (ufunc->uf_def_status == UF_COMPILE_ERROR) |
| 1455 | { |
| 1456 | emsg_funcname(_(e_call_to_function_that_failed_to_compile_str), |
| 1457 | ufunc->uf_name); |
| 1458 | return FAIL; |
| 1459 | } |
| 1460 | |
| 1461 | if ((isn = generate_instr(cctx, |
| 1462 | ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL |
| 1463 | : ISN_UCALL)) == NULL) |
| 1464 | return FAIL; |
| 1465 | if (isn->isn_type == ISN_DCALL) |
| 1466 | { |
| 1467 | isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx; |
| 1468 | isn->isn_arg.dfunc.cdf_argcount = argcount; |
| 1469 | } |
| 1470 | else |
| 1471 | { |
| 1472 | // A user function may be deleted and redefined later, can't use the |
| 1473 | // ufunc pointer, need to look it up again at runtime. |
| 1474 | isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name); |
| 1475 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1476 | } |
| 1477 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1478 | // drop the argument types |
| 1479 | cctx->ctx_type_stack.ga_len -= argcount; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1480 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1481 | // add return type |
| 1482 | return push_type_stack(cctx, ufunc->uf_ret_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * Generate an ISN_UCALL instruction when the function isn't defined yet. |
| 1487 | */ |
| 1488 | int |
| 1489 | generate_UCALL(cctx_T *cctx, char_u *name, int argcount) |
| 1490 | { |
| 1491 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1492 | |
| 1493 | RETURN_OK_IF_SKIP(cctx); |
| 1494 | if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL) |
| 1495 | return FAIL; |
| 1496 | isn->isn_arg.ufunc.cuf_name = vim_strsave(name); |
| 1497 | isn->isn_arg.ufunc.cuf_argcount = argcount; |
| 1498 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1499 | // drop the argument types |
| 1500 | cctx->ctx_type_stack.ga_len -= argcount; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1501 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1502 | // add return value |
| 1503 | return push_type_stack(cctx, &t_any); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Generate an ISN_PCALL instruction. |
| 1508 | * "type" is the type of the FuncRef. |
| 1509 | */ |
| 1510 | int |
| 1511 | generate_PCALL( |
| 1512 | cctx_T *cctx, |
| 1513 | int argcount, |
| 1514 | char_u *name, |
| 1515 | type_T *type, |
| 1516 | int at_top) |
| 1517 | { |
| 1518 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1519 | type_T *ret_type; |
| 1520 | |
| 1521 | RETURN_OK_IF_SKIP(cctx); |
| 1522 | |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 1523 | if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1524 | ret_type = &t_any; |
| 1525 | else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL) |
| 1526 | { |
| 1527 | if (type->tt_argcount != -1) |
| 1528 | { |
| 1529 | int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0; |
| 1530 | |
| 1531 | if (argcount < type->tt_min_argcount - varargs) |
| 1532 | { |
| 1533 | semsg(_(e_not_enough_arguments_for_function_str), name); |
| 1534 | return FAIL; |
| 1535 | } |
| 1536 | if (!varargs && argcount > type->tt_argcount) |
| 1537 | { |
| 1538 | semsg(_(e_too_many_arguments_for_function_str), name); |
| 1539 | return FAIL; |
| 1540 | } |
| 1541 | if (type->tt_args != NULL) |
| 1542 | { |
| 1543 | int i; |
| 1544 | |
| 1545 | for (i = 0; i < argcount; ++i) |
| 1546 | { |
| 1547 | int offset = -argcount + i - (at_top ? 0 : 1); |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1548 | type_T *actual = get_type_on_stack(cctx, -1 - offset); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1549 | type_T *expected; |
| 1550 | |
| 1551 | if (varargs && i >= type->tt_argcount - 1) |
| 1552 | expected = type->tt_args[ |
| 1553 | type->tt_argcount - 1]->tt_member; |
| 1554 | else if (i >= type->tt_min_argcount |
| 1555 | && actual == &t_special) |
| 1556 | expected = &t_any; |
| 1557 | else |
| 1558 | expected = type->tt_args[i]; |
| 1559 | if (need_type(actual, expected, offset, i + 1, |
| 1560 | cctx, TRUE, FALSE) == FAIL) |
| 1561 | { |
| 1562 | arg_type_mismatch(expected, actual, i + 1); |
| 1563 | return FAIL; |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | ret_type = type->tt_member; |
| 1569 | if (ret_type == &t_unknown) |
| 1570 | // return type not known yet, use a runtime check |
| 1571 | ret_type = &t_any; |
| 1572 | } |
| 1573 | else |
| 1574 | { |
| 1575 | semsg(_(e_not_callable_type_str), name); |
| 1576 | return FAIL; |
| 1577 | } |
| 1578 | |
| 1579 | if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL) |
| 1580 | return FAIL; |
| 1581 | isn->isn_arg.pfunc.cpf_top = at_top; |
| 1582 | isn->isn_arg.pfunc.cpf_argcount = argcount; |
| 1583 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1584 | // drop the arguments and the funcref/partial |
| 1585 | cctx->ctx_type_stack.ga_len -= argcount + 1; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1586 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1587 | // push the return value |
| 1588 | push_type_stack(cctx, ret_type); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1589 | |
| 1590 | // If partial is above the arguments it must be cleared and replaced with |
| 1591 | // the return value. |
| 1592 | if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL) |
| 1593 | return FAIL; |
| 1594 | |
| 1595 | return OK; |
| 1596 | } |
| 1597 | |
| 1598 | /* |
| 1599 | * Generate an ISN_STRINGMEMBER instruction. |
| 1600 | */ |
| 1601 | int |
| 1602 | generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len) |
| 1603 | { |
| 1604 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1605 | type_T *type; |
| 1606 | |
| 1607 | RETURN_OK_IF_SKIP(cctx); |
| 1608 | if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL) |
| 1609 | return FAIL; |
| 1610 | isn->isn_arg.string = vim_strnsave(name, len); |
| 1611 | |
| 1612 | // check for dict type |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1613 | type = get_type_on_stack(cctx, 0); |
Bram Moolenaar | 59618fe | 2021-12-21 12:32:17 +0000 | [diff] [blame] | 1614 | if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1615 | { |
| 1616 | char *tofree; |
| 1617 | |
| 1618 | semsg(_(e_expected_dictionary_for_using_key_str_but_got_str), |
| 1619 | name, type_name(type, &tofree)); |
| 1620 | vim_free(tofree); |
| 1621 | return FAIL; |
| 1622 | } |
| 1623 | // change dict type to dict member type |
| 1624 | if (type->tt_type == VAR_DICT) |
| 1625 | { |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1626 | type_T *ntype = type->tt_member == &t_unknown |
| 1627 | ? &t_any : type->tt_member; |
| 1628 | set_type_on_stack(cctx, ntype, 0); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | return OK; |
| 1632 | } |
| 1633 | |
| 1634 | /* |
| 1635 | * Generate an ISN_ECHO instruction. |
| 1636 | */ |
| 1637 | int |
| 1638 | generate_ECHO(cctx_T *cctx, int with_white, int count) |
| 1639 | { |
| 1640 | isn_T *isn; |
| 1641 | |
| 1642 | RETURN_OK_IF_SKIP(cctx); |
| 1643 | if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL) |
| 1644 | return FAIL; |
| 1645 | isn->isn_arg.echo.echo_with_white = with_white; |
| 1646 | isn->isn_arg.echo.echo_count = count; |
| 1647 | |
| 1648 | return OK; |
| 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction. |
| 1653 | */ |
| 1654 | int |
| 1655 | generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count) |
| 1656 | { |
| 1657 | isn_T *isn; |
| 1658 | |
| 1659 | if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL) |
| 1660 | return FAIL; |
| 1661 | isn->isn_arg.number = count; |
| 1662 | |
| 1663 | return OK; |
| 1664 | } |
| 1665 | |
| 1666 | /* |
| 1667 | * Generate an ISN_PUT instruction. |
| 1668 | */ |
| 1669 | int |
| 1670 | generate_PUT(cctx_T *cctx, int regname, linenr_T lnum) |
| 1671 | { |
| 1672 | isn_T *isn; |
| 1673 | |
| 1674 | RETURN_OK_IF_SKIP(cctx); |
| 1675 | if ((isn = generate_instr(cctx, ISN_PUT)) == NULL) |
| 1676 | return FAIL; |
| 1677 | isn->isn_arg.put.put_regname = regname; |
| 1678 | isn->isn_arg.put.put_lnum = lnum; |
| 1679 | return OK; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
| 1683 | * Generate an EXEC instruction that takes a string argument. |
| 1684 | * A copy is made of "line". |
| 1685 | */ |
| 1686 | int |
| 1687 | generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line) |
| 1688 | { |
| 1689 | isn_T *isn; |
| 1690 | |
| 1691 | RETURN_OK_IF_SKIP(cctx); |
| 1692 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 1693 | return FAIL; |
| 1694 | isn->isn_arg.string = vim_strsave(line); |
| 1695 | return OK; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | * Generate an EXEC instruction that takes a string argument. |
| 1700 | * "str" must be allocated, it is consumed. |
| 1701 | */ |
| 1702 | int |
| 1703 | generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str) |
| 1704 | { |
| 1705 | isn_T *isn; |
| 1706 | |
| 1707 | if (cctx->ctx_skip == SKIP_YES) |
| 1708 | { |
| 1709 | vim_free(str); |
| 1710 | return OK; |
| 1711 | } |
| 1712 | if ((isn = generate_instr(cctx, isntype)) == NULL) |
| 1713 | { |
| 1714 | vim_free(str); |
| 1715 | return FAIL; |
| 1716 | } |
| 1717 | isn->isn_arg.string = str; |
| 1718 | return OK; |
| 1719 | } |
| 1720 | |
| 1721 | int |
| 1722 | generate_LEGACY_EVAL(cctx_T *cctx, char_u *line) |
| 1723 | { |
| 1724 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1725 | |
| 1726 | RETURN_OK_IF_SKIP(cctx); |
| 1727 | if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL) |
| 1728 | return FAIL; |
| 1729 | isn->isn_arg.string = vim_strsave(line); |
| 1730 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1731 | return push_type_stack(cctx, &t_any); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | int |
| 1735 | generate_EXECCONCAT(cctx_T *cctx, int count) |
| 1736 | { |
| 1737 | isn_T *isn; |
| 1738 | |
| 1739 | if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL) |
| 1740 | return FAIL; |
| 1741 | isn->isn_arg.number = count; |
| 1742 | return OK; |
| 1743 | } |
| 1744 | |
| 1745 | /* |
| 1746 | * Generate ISN_RANGE. Consumes "range". Return OK/FAIL. |
| 1747 | */ |
| 1748 | int |
| 1749 | generate_RANGE(cctx_T *cctx, char_u *range) |
| 1750 | { |
| 1751 | isn_T *isn; |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1752 | |
| 1753 | if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL) |
| 1754 | return FAIL; |
| 1755 | isn->isn_arg.string = range; |
| 1756 | |
Bram Moolenaar | 078a461 | 2022-01-04 15:17:03 +0000 | [diff] [blame] | 1757 | return push_type_stack(cctx, &t_number); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | int |
| 1761 | generate_UNPACK(cctx_T *cctx, int var_count, int semicolon) |
| 1762 | { |
| 1763 | isn_T *isn; |
| 1764 | |
| 1765 | RETURN_OK_IF_SKIP(cctx); |
| 1766 | if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL) |
| 1767 | return FAIL; |
| 1768 | isn->isn_arg.unpack.unp_count = var_count; |
| 1769 | isn->isn_arg.unpack.unp_semicolon = semicolon; |
| 1770 | return OK; |
| 1771 | } |
| 1772 | |
| 1773 | /* |
| 1774 | * Generate an instruction for any command modifiers. |
| 1775 | */ |
| 1776 | int |
| 1777 | generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod) |
| 1778 | { |
| 1779 | isn_T *isn; |
| 1780 | |
| 1781 | if (has_cmdmod(cmod, FALSE)) |
| 1782 | { |
| 1783 | cctx->ctx_has_cmdmod = TRUE; |
| 1784 | |
| 1785 | if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL) |
| 1786 | return FAIL; |
| 1787 | isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T); |
| 1788 | if (isn->isn_arg.cmdmod.cf_cmdmod == NULL) |
| 1789 | return FAIL; |
| 1790 | mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T)); |
| 1791 | // filter program now belongs to the instruction |
| 1792 | cmod->cmod_filter_regmatch.regprog = NULL; |
| 1793 | } |
| 1794 | |
| 1795 | return OK; |
| 1796 | } |
| 1797 | |
| 1798 | int |
| 1799 | generate_undo_cmdmods(cctx_T *cctx) |
| 1800 | { |
| 1801 | if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL) |
| 1802 | return FAIL; |
| 1803 | cctx->ctx_has_cmdmod = FALSE; |
| 1804 | return OK; |
| 1805 | } |
| 1806 | |
| 1807 | /* |
| 1808 | * Generate a STORE instruction for "dest", not being "dest_local". |
| 1809 | * Return FAIL when out of memory. |
| 1810 | */ |
| 1811 | int |
| 1812 | generate_store_var( |
| 1813 | cctx_T *cctx, |
| 1814 | assign_dest_T dest, |
| 1815 | int opt_flags, |
| 1816 | int vimvaridx, |
| 1817 | int scriptvar_idx, |
| 1818 | int scriptvar_sid, |
| 1819 | type_T *type, |
| 1820 | char_u *name) |
| 1821 | { |
| 1822 | switch (dest) |
| 1823 | { |
| 1824 | case dest_option: |
| 1825 | return generate_STOREOPT(cctx, ISN_STOREOPT, |
| 1826 | skip_option_env_lead(name), opt_flags); |
| 1827 | case dest_func_option: |
| 1828 | return generate_STOREOPT(cctx, ISN_STOREFUNCOPT, |
| 1829 | skip_option_env_lead(name), opt_flags); |
| 1830 | case dest_global: |
| 1831 | // include g: with the name, easier to execute that way |
| 1832 | return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL |
| 1833 | ? ISN_STOREG : ISN_STOREAUTO, 0, name); |
| 1834 | case dest_buffer: |
| 1835 | // include b: with the name, easier to execute that way |
| 1836 | return generate_STORE(cctx, ISN_STOREB, 0, name); |
| 1837 | case dest_window: |
| 1838 | // include w: with the name, easier to execute that way |
| 1839 | return generate_STORE(cctx, ISN_STOREW, 0, name); |
| 1840 | case dest_tab: |
| 1841 | // include t: with the name, easier to execute that way |
| 1842 | return generate_STORE(cctx, ISN_STORET, 0, name); |
| 1843 | case dest_env: |
| 1844 | return generate_STORE(cctx, ISN_STOREENV, 0, name + 1); |
| 1845 | case dest_reg: |
| 1846 | return generate_STORE(cctx, ISN_STOREREG, |
| 1847 | name[1] == '@' ? '"' : name[1], NULL); |
| 1848 | case dest_vimvar: |
| 1849 | return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL); |
| 1850 | case dest_script: |
| 1851 | if (scriptvar_idx < 0) |
| 1852 | // "s:" may be included in the name. |
| 1853 | return generate_OLDSCRIPT(cctx, ISN_STORES, name, |
| 1854 | scriptvar_sid, type); |
| 1855 | return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT, |
| 1856 | scriptvar_sid, scriptvar_idx, type); |
| 1857 | case dest_local: |
| 1858 | case dest_expr: |
| 1859 | // cannot happen |
| 1860 | break; |
| 1861 | } |
| 1862 | return FAIL; |
| 1863 | } |
| 1864 | |
Bram Moolenaar | 38ecd97 | 2022-01-15 21:44:44 +0000 | [diff] [blame] | 1865 | /* |
| 1866 | * Return TRUE when inside a "for" or "while" loop. |
| 1867 | */ |
| 1868 | int |
| 1869 | inside_loop_scope(cctx_T *cctx) |
| 1870 | { |
| 1871 | scope_T *scope = cctx->ctx_scope; |
| 1872 | |
| 1873 | for (;;) |
| 1874 | { |
| 1875 | if (scope == NULL) |
| 1876 | break; |
| 1877 | if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE) |
| 1878 | return TRUE; |
| 1879 | scope = scope->se_outer; |
| 1880 | } |
| 1881 | return FALSE; |
| 1882 | } |
| 1883 | |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1884 | int |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 1885 | generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl) |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1886 | { |
| 1887 | if (lhs->lhs_dest != dest_local) |
| 1888 | return generate_store_var(cctx, lhs->lhs_dest, |
| 1889 | lhs->lhs_opt_flags, lhs->lhs_vimvaridx, |
| 1890 | lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid, |
| 1891 | lhs->lhs_type, lhs->lhs_name); |
| 1892 | |
| 1893 | if (lhs->lhs_lvar != NULL) |
| 1894 | { |
| 1895 | garray_T *instr = &cctx->ctx_instr; |
| 1896 | isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; |
| 1897 | |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 1898 | // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into |
| 1899 | // ISN_STORENR. |
| 1900 | // And "var = 0" does not need any instruction. |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1901 | if (lhs->lhs_lvar->lv_from_outer == 0 |
| 1902 | && instr->ga_len == instr_count + 1 |
| 1903 | && isn->isn_type == ISN_PUSHNR) |
| 1904 | { |
| 1905 | varnumber_T val = isn->isn_arg.number; |
| 1906 | garray_T *stack = &cctx->ctx_type_stack; |
| 1907 | |
Bram Moolenaar | 38ecd97 | 2022-01-15 21:44:44 +0000 | [diff] [blame] | 1908 | if (val == 0 && is_decl && !inside_loop_scope(cctx)) |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 1909 | { |
Bram Moolenaar | 38ecd97 | 2022-01-15 21:44:44 +0000 | [diff] [blame] | 1910 | // zero is the default value, no need to do anything |
Bram Moolenaar | 5cd6479 | 2021-12-25 18:23:24 +0000 | [diff] [blame] | 1911 | --instr->ga_len; |
| 1912 | } |
| 1913 | else |
| 1914 | { |
| 1915 | isn->isn_type = ISN_STORENR; |
| 1916 | isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx; |
| 1917 | isn->isn_arg.storenr.stnr_val = val; |
| 1918 | } |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1919 | if (stack->ga_len > 0) |
| 1920 | --stack->ga_len; |
| 1921 | } |
| 1922 | else if (lhs->lhs_lvar->lv_from_outer > 0) |
| 1923 | generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx, |
| 1924 | lhs->lhs_lvar->lv_from_outer); |
| 1925 | else |
| 1926 | generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL); |
| 1927 | } |
| 1928 | return OK; |
| 1929 | } |
| 1930 | |
| 1931 | #if defined(FEAT_PROFILE) || defined(PROTO) |
| 1932 | void |
| 1933 | may_generate_prof_end(cctx_T *cctx, int prof_lnum) |
| 1934 | { |
| 1935 | if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0) |
| 1936 | generate_instr(cctx, ISN_PROF_END); |
| 1937 | } |
| 1938 | #endif |
| 1939 | |
| 1940 | |
| 1941 | /* |
| 1942 | * Delete an instruction, free what it contains. |
| 1943 | */ |
| 1944 | void |
| 1945 | delete_instr(isn_T *isn) |
| 1946 | { |
| 1947 | switch (isn->isn_type) |
| 1948 | { |
Bram Moolenaar | 06b7722 | 2022-01-25 15:51:56 +0000 | [diff] [blame] | 1949 | case ISN_AUTOLOAD: |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 1950 | case ISN_DEF: |
| 1951 | case ISN_EXEC: |
| 1952 | case ISN_EXECRANGE: |
| 1953 | case ISN_EXEC_SPLIT: |
| 1954 | case ISN_LEGACY_EVAL: |
| 1955 | case ISN_LOADAUTO: |
| 1956 | case ISN_LOADB: |
| 1957 | case ISN_LOADENV: |
| 1958 | case ISN_LOADG: |
| 1959 | case ISN_LOADOPT: |
| 1960 | case ISN_LOADT: |
| 1961 | case ISN_LOADW: |
| 1962 | case ISN_LOCKUNLOCK: |
| 1963 | case ISN_PUSHEXC: |
| 1964 | case ISN_PUSHFUNC: |
| 1965 | case ISN_PUSHS: |
| 1966 | case ISN_RANGE: |
| 1967 | case ISN_STOREAUTO: |
| 1968 | case ISN_STOREB: |
| 1969 | case ISN_STOREENV: |
| 1970 | case ISN_STOREG: |
| 1971 | case ISN_STORET: |
| 1972 | case ISN_STOREW: |
| 1973 | case ISN_STRINGMEMBER: |
| 1974 | vim_free(isn->isn_arg.string); |
| 1975 | break; |
| 1976 | |
| 1977 | case ISN_SUBSTITUTE: |
| 1978 | { |
| 1979 | int idx; |
| 1980 | isn_T *list = isn->isn_arg.subs.subs_instr; |
| 1981 | |
| 1982 | vim_free(isn->isn_arg.subs.subs_cmd); |
| 1983 | for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx) |
| 1984 | delete_instr(list + idx); |
| 1985 | vim_free(list); |
| 1986 | } |
| 1987 | break; |
| 1988 | |
| 1989 | case ISN_INSTR: |
| 1990 | { |
| 1991 | int idx; |
| 1992 | isn_T *list = isn->isn_arg.instr; |
| 1993 | |
| 1994 | for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx) |
| 1995 | delete_instr(list + idx); |
| 1996 | vim_free(list); |
| 1997 | } |
| 1998 | break; |
| 1999 | |
| 2000 | case ISN_LOADS: |
| 2001 | case ISN_STORES: |
| 2002 | vim_free(isn->isn_arg.loadstore.ls_name); |
| 2003 | break; |
| 2004 | |
| 2005 | case ISN_UNLET: |
| 2006 | case ISN_UNLETENV: |
| 2007 | vim_free(isn->isn_arg.unlet.ul_name); |
| 2008 | break; |
| 2009 | |
| 2010 | case ISN_STOREOPT: |
| 2011 | case ISN_STOREFUNCOPT: |
| 2012 | vim_free(isn->isn_arg.storeopt.so_name); |
| 2013 | break; |
| 2014 | |
| 2015 | case ISN_PUSHBLOB: // push blob isn_arg.blob |
| 2016 | blob_unref(isn->isn_arg.blob); |
| 2017 | break; |
| 2018 | |
| 2019 | case ISN_PUSHJOB: |
| 2020 | #ifdef FEAT_JOB_CHANNEL |
| 2021 | job_unref(isn->isn_arg.job); |
| 2022 | #endif |
| 2023 | break; |
| 2024 | |
| 2025 | case ISN_PUSHCHANNEL: |
| 2026 | #ifdef FEAT_JOB_CHANNEL |
| 2027 | channel_unref(isn->isn_arg.channel); |
| 2028 | #endif |
| 2029 | break; |
| 2030 | |
| 2031 | case ISN_UCALL: |
| 2032 | vim_free(isn->isn_arg.ufunc.cuf_name); |
| 2033 | break; |
| 2034 | |
| 2035 | case ISN_FUNCREF: |
| 2036 | { |
| 2037 | if (isn->isn_arg.funcref.fr_func_name == NULL) |
| 2038 | { |
| 2039 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 2040 | + isn->isn_arg.funcref.fr_dfunc_idx; |
| 2041 | ufunc_T *ufunc = dfunc->df_ufunc; |
| 2042 | |
| 2043 | if (ufunc != NULL && func_name_refcount(ufunc->uf_name)) |
| 2044 | func_ptr_unref(ufunc); |
| 2045 | } |
| 2046 | else |
| 2047 | { |
| 2048 | char_u *name = isn->isn_arg.funcref.fr_func_name; |
| 2049 | |
| 2050 | if (name != NULL) |
| 2051 | func_unref(name); |
| 2052 | vim_free(isn->isn_arg.funcref.fr_func_name); |
| 2053 | } |
| 2054 | } |
| 2055 | break; |
| 2056 | |
| 2057 | case ISN_DCALL: |
| 2058 | { |
| 2059 | dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) |
| 2060 | + isn->isn_arg.dfunc.cdf_idx; |
| 2061 | |
| 2062 | if (dfunc->df_ufunc != NULL |
| 2063 | && func_name_refcount(dfunc->df_ufunc->uf_name)) |
| 2064 | func_ptr_unref(dfunc->df_ufunc); |
| 2065 | } |
| 2066 | break; |
| 2067 | |
| 2068 | case ISN_NEWFUNC: |
| 2069 | { |
| 2070 | char_u *lambda = isn->isn_arg.newfunc.nf_lambda; |
Bram Moolenaar | acc4b56 | 2022-01-24 13:54:45 +0000 | [diff] [blame] | 2071 | ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2072 | |
| 2073 | if (ufunc != NULL) |
| 2074 | { |
| 2075 | unlink_def_function(ufunc); |
| 2076 | func_ptr_unref(ufunc); |
| 2077 | } |
| 2078 | |
| 2079 | vim_free(lambda); |
| 2080 | vim_free(isn->isn_arg.newfunc.nf_global); |
| 2081 | } |
| 2082 | break; |
| 2083 | |
| 2084 | case ISN_CHECKTYPE: |
| 2085 | case ISN_SETTYPE: |
| 2086 | free_type(isn->isn_arg.type.ct_type); |
| 2087 | break; |
| 2088 | |
| 2089 | case ISN_CMDMOD: |
| 2090 | vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod |
| 2091 | ->cmod_filter_regmatch.regprog); |
| 2092 | vim_free(isn->isn_arg.cmdmod.cf_cmdmod); |
| 2093 | break; |
| 2094 | |
| 2095 | case ISN_LOADSCRIPT: |
| 2096 | case ISN_STORESCRIPT: |
| 2097 | vim_free(isn->isn_arg.script.scriptref); |
| 2098 | break; |
| 2099 | |
| 2100 | case ISN_TRY: |
Bram Moolenaar | 0d80710 | 2021-12-21 09:42:09 +0000 | [diff] [blame] | 2101 | vim_free(isn->isn_arg.tryref.try_ref); |
Bram Moolenaar | dc7c366 | 2021-12-20 15:04:29 +0000 | [diff] [blame] | 2102 | break; |
| 2103 | |
| 2104 | case ISN_CEXPR_CORE: |
| 2105 | vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline); |
| 2106 | vim_free(isn->isn_arg.cexpr.cexpr_ref); |
| 2107 | break; |
| 2108 | |
| 2109 | case ISN_2BOOL: |
| 2110 | case ISN_2STRING: |
| 2111 | case ISN_2STRING_ANY: |
| 2112 | case ISN_ADDBLOB: |
| 2113 | case ISN_ADDLIST: |
| 2114 | case ISN_ANYINDEX: |
| 2115 | case ISN_ANYSLICE: |
| 2116 | case ISN_BCALL: |
| 2117 | case ISN_BLOBAPPEND: |
| 2118 | case ISN_BLOBINDEX: |
| 2119 | case ISN_BLOBSLICE: |
| 2120 | case ISN_CATCH: |
| 2121 | case ISN_CEXPR_AUCMD: |
| 2122 | case ISN_CHECKLEN: |
| 2123 | case ISN_CHECKNR: |
| 2124 | case ISN_CLEARDICT: |
| 2125 | case ISN_CMDMOD_REV: |
| 2126 | case ISN_COMPAREANY: |
| 2127 | case ISN_COMPAREBLOB: |
| 2128 | case ISN_COMPAREBOOL: |
| 2129 | case ISN_COMPAREDICT: |
| 2130 | case ISN_COMPAREFLOAT: |
| 2131 | case ISN_COMPAREFUNC: |
| 2132 | case ISN_COMPARELIST: |
| 2133 | case ISN_COMPARENR: |
| 2134 | case ISN_COMPARESPECIAL: |
| 2135 | case ISN_COMPARESTRING: |
| 2136 | case ISN_CONCAT: |
| 2137 | case ISN_COND2BOOL: |
| 2138 | case ISN_DEBUG: |
| 2139 | case ISN_DROP: |
| 2140 | case ISN_ECHO: |
| 2141 | case ISN_ECHOCONSOLE: |
| 2142 | case ISN_ECHOERR: |
| 2143 | case ISN_ECHOMSG: |
| 2144 | case ISN_ENDTRY: |
| 2145 | case ISN_EXECCONCAT: |
| 2146 | case ISN_EXECUTE: |
| 2147 | case ISN_FINALLY: |
| 2148 | case ISN_FINISH: |
| 2149 | case ISN_FOR: |
| 2150 | case ISN_GETITEM: |
| 2151 | case ISN_JUMP: |
| 2152 | case ISN_JUMP_IF_ARG_SET: |
| 2153 | case ISN_LISTAPPEND: |
| 2154 | case ISN_LISTINDEX: |
| 2155 | case ISN_LISTSLICE: |
| 2156 | case ISN_LOAD: |
| 2157 | case ISN_LOADBDICT: |
| 2158 | case ISN_LOADGDICT: |
| 2159 | case ISN_LOADOUTER: |
| 2160 | case ISN_LOADREG: |
| 2161 | case ISN_LOADTDICT: |
| 2162 | case ISN_LOADV: |
| 2163 | case ISN_LOADWDICT: |
| 2164 | case ISN_LOCKCONST: |
| 2165 | case ISN_MEMBER: |
| 2166 | case ISN_NEGATENR: |
| 2167 | case ISN_NEWDICT: |
| 2168 | case ISN_NEWLIST: |
| 2169 | case ISN_OPANY: |
| 2170 | case ISN_OPFLOAT: |
| 2171 | case ISN_OPNR: |
| 2172 | case ISN_PCALL: |
| 2173 | case ISN_PCALL_END: |
| 2174 | case ISN_PROF_END: |
| 2175 | case ISN_PROF_START: |
| 2176 | case ISN_PUSHBOOL: |
| 2177 | case ISN_PUSHF: |
| 2178 | case ISN_PUSHNR: |
| 2179 | case ISN_PUSHSPEC: |
| 2180 | case ISN_PUT: |
| 2181 | case ISN_REDIREND: |
| 2182 | case ISN_REDIRSTART: |
| 2183 | case ISN_RETURN: |
| 2184 | case ISN_RETURN_VOID: |
| 2185 | case ISN_SHUFFLE: |
| 2186 | case ISN_SLICE: |
| 2187 | case ISN_STORE: |
| 2188 | case ISN_STOREINDEX: |
| 2189 | case ISN_STORENR: |
| 2190 | case ISN_STOREOUTER: |
| 2191 | case ISN_STORERANGE: |
| 2192 | case ISN_STOREREG: |
| 2193 | case ISN_STOREV: |
| 2194 | case ISN_STRINDEX: |
| 2195 | case ISN_STRSLICE: |
| 2196 | case ISN_THROW: |
| 2197 | case ISN_TRYCONT: |
| 2198 | case ISN_UNLETINDEX: |
| 2199 | case ISN_UNLETRANGE: |
| 2200 | case ISN_UNPACK: |
| 2201 | case ISN_USEDICT: |
| 2202 | // nothing allocated |
| 2203 | break; |
| 2204 | } |
| 2205 | } |
| 2206 | |
| 2207 | void |
| 2208 | clear_instr_ga(garray_T *gap) |
| 2209 | { |
| 2210 | int idx; |
| 2211 | |
| 2212 | for (idx = 0; idx < gap->ga_len; ++idx) |
| 2213 | delete_instr(((isn_T *)gap->ga_data) + idx); |
| 2214 | ga_clear(gap); |
| 2215 | } |
| 2216 | |
| 2217 | |
| 2218 | #endif // defined(FEAT_EVAL) |