summaryrefslogtreecommitdiffstats
path: root/src/vim9instr.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-12-25 18:23:24 +0000
committerBram Moolenaar <Bram@vim.org>2021-12-25 18:23:24 +0000
commit5cd647935d0834b3064aa36384b8f6730fadadd6 (patch)
treed0b40261ac7160d723f5bca46cf57d43641e9aed /src/vim9instr.c
parent35cfd793aad7c4bfba4a9bedf5c435c44e4293d0 (diff)
patch 8.2.3893: Vim9: many local variables are initialized with an instructionv8.2.3893
Problem: Vim9: many local variables are initialized with an instruction. Solution: Initialize local variables to zero to avoid the instructions.
Diffstat (limited to 'src/vim9instr.c')
-rw-r--r--src/vim9instr.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/vim9instr.c b/src/vim9instr.c
index 0468f336c7..0483068aa2 100644
--- a/src/vim9instr.c
+++ b/src/vim9instr.c
@@ -1886,7 +1886,7 @@ generate_store_var(
}
int
-generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
+generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
{
if (lhs->lhs_dest != dest_local)
return generate_store_var(cctx, lhs->lhs_dest,
@@ -1899,8 +1899,9 @@ generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
garray_T *instr = &cctx->ctx_instr;
isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
- // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
- // ISN_STORENR
+ // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
+ // ISN_STORENR.
+ // And "var = 0" does not need any instruction.
if (lhs->lhs_lvar->lv_from_outer == 0
&& instr->ga_len == instr_count + 1
&& isn->isn_type == ISN_PUSHNR)
@@ -1908,9 +1909,16 @@ generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
varnumber_T val = isn->isn_arg.number;
garray_T *stack = &cctx->ctx_type_stack;
- isn->isn_type = ISN_STORENR;
- isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
- isn->isn_arg.storenr.stnr_val = val;
+ if (val == 0 && is_decl)
+ {
+ --instr->ga_len;
+ }
+ else
+ {
+ isn->isn_type = ISN_STORENR;
+ isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
+ isn->isn_arg.storenr.stnr_val = val;
+ }
if (stack->ga_len > 0)
--stack->ga_len;
}