comparison src/vim9instr.c @ 26729:b969fdb8cd46 v8.2.3893

patch 8.2.3893: Vim9: many local variables are initialized with an instruction Commit: https://github.com/vim/vim/commit/5cd647935d0834b3064aa36384b8f6730fadadd6 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Dec 25 18:23:24 2021 +0000 patch 8.2.3893: Vim9: many local variables are initialized with an instruction Problem: Vim9: many local variables are initialized with an instruction. Solution: Initialize local variables to zero to avoid the instructions.
author Bram Moolenaar <Bram@vim.org>
date Sat, 25 Dec 2021 19:30:03 +0100
parents c3cffd372c6f
children 06a137af96f8
comparison
equal deleted inserted replaced
26728:71ae885c1426 26729:b969fdb8cd46
1884 } 1884 }
1885 return FAIL; 1885 return FAIL;
1886 } 1886 }
1887 1887
1888 int 1888 int
1889 generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count) 1889 generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
1890 { 1890 {
1891 if (lhs->lhs_dest != dest_local) 1891 if (lhs->lhs_dest != dest_local)
1892 return generate_store_var(cctx, lhs->lhs_dest, 1892 return generate_store_var(cctx, lhs->lhs_dest,
1893 lhs->lhs_opt_flags, lhs->lhs_vimvaridx, 1893 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1894 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid, 1894 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1897 if (lhs->lhs_lvar != NULL) 1897 if (lhs->lhs_lvar != NULL)
1898 { 1898 {
1899 garray_T *instr = &cctx->ctx_instr; 1899 garray_T *instr = &cctx->ctx_instr;
1900 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1; 1900 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1901 1901
1902 // optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into 1902 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1903 // ISN_STORENR 1903 // ISN_STORENR.
1904 // And "var = 0" does not need any instruction.
1904 if (lhs->lhs_lvar->lv_from_outer == 0 1905 if (lhs->lhs_lvar->lv_from_outer == 0
1905 && instr->ga_len == instr_count + 1 1906 && instr->ga_len == instr_count + 1
1906 && isn->isn_type == ISN_PUSHNR) 1907 && isn->isn_type == ISN_PUSHNR)
1907 { 1908 {
1908 varnumber_T val = isn->isn_arg.number; 1909 varnumber_T val = isn->isn_arg.number;
1909 garray_T *stack = &cctx->ctx_type_stack; 1910 garray_T *stack = &cctx->ctx_type_stack;
1910 1911
1911 isn->isn_type = ISN_STORENR; 1912 if (val == 0 && is_decl)
1912 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx; 1913 {
1913 isn->isn_arg.storenr.stnr_val = val; 1914 --instr->ga_len;
1915 }
1916 else
1917 {
1918 isn->isn_type = ISN_STORENR;
1919 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
1920 isn->isn_arg.storenr.stnr_val = val;
1921 }
1914 if (stack->ga_len > 0) 1922 if (stack->ga_len > 0)
1915 --stack->ga_len; 1923 --stack->ga_len;
1916 } 1924 }
1917 else if (lhs->lhs_lvar->lv_from_outer > 0) 1925 else if (lhs->lhs_lvar->lv_from_outer > 0)
1918 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx, 1926 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,