From f83b693f65f03d52627d74b165a074ed42ad2da6 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Wed, 5 Jun 2024 23:38:48 +0800 Subject: [PATCH] :memo: code improvement --- src/ast_dumper.cpp | 2 +- src/nasal_ast.h | 6 +-- src/nasal_codegen.cpp | 8 ++-- src/nasal_dbg.cpp | 5 ++- src/nasal_opcode.cpp | 93 +------------------------------------------ src/nasal_opcode.h | 93 ++++++++++++++++++++++++++++++++++++++++++- src/nasal_parse.cpp | 2 +- 7 files changed, 104 insertions(+), 105 deletions(-) diff --git a/src/ast_dumper.cpp b/src/ast_dumper.cpp index 0690068..c570019 100644 --- a/src/ast_dumper.cpp +++ b/src/ast_dumper.cpp @@ -185,7 +185,7 @@ bool ast_dumper::visit_binary_operator(binary_operator* node) { case binary_operator::binary_type::leq: std::cout << "<="; break; case binary_operator::binary_type::condition_and: std::cout << "and"; break; case binary_operator::binary_type::condition_or: std::cout << "or"; break; - case binary_operator::binary_type::nullchain: std::cout << "??"; break; + case binary_operator::binary_type::null_chain: std::cout << "??"; break; } std::cout << "\"" << format_location(node); push_indent(); diff --git a/src/nasal_ast.h b/src/nasal_ast.h index f0b885b..3607e1b 100644 --- a/src/nasal_ast.h +++ b/src/nasal_ast.h @@ -23,7 +23,7 @@ enum class expr_type { ast_pair, // pair of key and value in hashmap ast_call, // mark a sub-tree of calling an identifier ast_callh, // id.name - ast_nullaccess, // id?.name + ast_null_access, // id?.name ast_callv, // id[index] ast_callf, // id() ast_subvec, // id[index:index] @@ -304,7 +304,7 @@ public: bitwise_and, condition_and, condition_or, - nullchain + null_chain }; private: @@ -397,7 +397,7 @@ private: public: null_access(const span& location, const std::string& name): - call(location, expr_type::ast_nullaccess), + call(location, expr_type::ast_null_access), field(name) {} ~null_access() override = default; const std::string& get_field() const {return field;} diff --git a/src/nasal_codegen.cpp b/src/nasal_codegen.cpp index b795a97..3873733 100644 --- a/src/nasal_codegen.cpp +++ b/src/nasal_codegen.cpp @@ -339,7 +339,7 @@ void codegen::call_gen(call_expr* node) { call_vector_gen(reinterpret_cast(i)); break; case expr_type::ast_callf: call_func_gen(reinterpret_cast(i)); break; - case expr_type::ast_nullaccess: + case expr_type::ast_null_access: null_access_gen(reinterpret_cast(i)); break; default: break; } @@ -475,7 +475,7 @@ void codegen::mcall(expr* node) { call_vector_gen(reinterpret_cast(tmp)); break; case expr_type::ast_callf: call_func_gen(reinterpret_cast(tmp)); break; - case expr_type::ast_nullaccess: + case expr_type::ast_null_access: null_access_gen(reinterpret_cast(tmp)); break; default: break; } @@ -489,7 +489,7 @@ void codegen::mcall(expr* node) { mcall_vec(reinterpret_cast(tmp)); break; case expr_type::ast_callf: die("bad left-value: function call", tmp->get_location()); break; - case expr_type::ast_nullaccess: + case expr_type::ast_null_access: die("bad left-value: null access test", tmp->get_location()); break; default: die("bad left-value: unknown call", tmp->get_location()); break; @@ -1083,7 +1083,7 @@ void codegen::binary_gen(binary_operator* node) { calc_gen(node->get_right()); emit(op_btand, 0, node->get_location()); return; - case binary_operator::binary_type::nullchain: + case binary_operator::binary_type::null_chain: null_chain_gen(node); return; default: break; diff --git a/src/nasal_dbg.cpp b/src/nasal_dbg.cpp index 0f7d9e8..4f6c4cb 100644 --- a/src/nasal_dbg.cpp +++ b/src/nasal_dbg.cpp @@ -46,8 +46,9 @@ void operand_line_counter::dump_operand_count() const { if (!rate) { break; } - std::clog << " " << operand_name_table[i.first] << " : "; - std::clog << i.second << " (" << rate << "%)\n"; + std::clog << " "; + std::clog << operand_name_table.at(static_cast(i.first)); + std::clog << " : " << i.second << " (" << rate << "%)\n"; } std::clog << " total : " << total << '\n'; } diff --git a/src/nasal_opcode.cpp b/src/nasal_opcode.cpp index 2b227bf..23b4901 100644 --- a/src/nasal_opcode.cpp +++ b/src/nasal_opcode.cpp @@ -3,97 +3,6 @@ namespace nasal { -const char* operand_name_table[] = { - "exit ", - "repl ", - "intl ", - "loadg ", - "loadl ", - "loadu ", - "dup ", - "pnum ", - "pnil ", - "pstr ", - "newv ", - "newh ", - "newf ", - "happ ", - "para ", - "def ", - "dyn ", - "lnot ", - "usub ", - "bitnot", - "bitor ", - "bitxor", - "bitand", - "add ", - "sub ", - "mult ", - "div ", - "lnk ", - "addc ", - "subc ", - "multc ", - "divc ", - "lnkc ", - "addeq ", - "subeq ", - "muleq ", - "diveq ", - "lnkeq ", - "bandeq", - "boreq ", - "bxoreq", - "addeqc", - "subeqc", - "muleqc", - "diveqc", - "lnkeqc", - "addecp", - "subecp", - "mulecp", - "divecp", - "lnkecp", - "meq ", - "eq ", - "neq ", - "less ", - "leq ", - "grt ", - "geq ", - "lessc ", - "leqc ", - "grtc ", - "geqc ", - "pop ", - "jmp ", - "jt ", - "jf ", - "cnt ", - "findx ", - "feach ", - "callg ", - "calll ", - "upval ", - "callv ", - "callvi", - "callh ", - "callfv", - "callfh", - "callb ", - "slcbeg", - "slcend", - "slice ", - "slice2", - "mcallg", - "mcalll", - "mupval", - "mcallv", - "mcallh", - "ret " -}; - void codestream::set(const f64* number_list, const std::string* string_list, const nasal_builtin_table* native_table, @@ -124,7 +33,7 @@ void codestream::dump(std::ostream& out) const { } // dump operand name - out << " " << operand_name_table[op] << " "; + out << " " << operand_name_table.at(static_cast(op)) << " "; switch(op) { case op_addeq: diff --git a/src/nasal_opcode.h b/src/nasal_opcode.h index 98d7a8a..e64ade5 100644 --- a/src/nasal_opcode.h +++ b/src/nasal_opcode.h @@ -98,6 +98,97 @@ enum op_code_type: u8 { op_ret // return }; +static std::unordered_map operand_name_table = { + {op_code_type::op_exit, "exit "}, + {op_code_type::op_repl, "repl "}, + {op_code_type::op_intl, "intl "}, + {op_code_type::op_loadg, "loadg "}, + {op_code_type::op_loadl, "loadl "}, + {op_code_type::op_loadu, "loadu "}, + {op_code_type::op_dup, "dup "}, + {op_code_type::op_pnum, "pnum "}, + {op_code_type::op_pnil, "pnil "}, + {op_code_type::op_pstr, "pstr "}, + {op_code_type::op_newv, "newv "}, + {op_code_type::op_newh, "newh "}, + {op_code_type::op_newf, "newf "}, + {op_code_type::op_happ, "happ "}, + {op_code_type::op_para, "para "}, + {op_code_type::op_deft, "def "}, + {op_code_type::op_dyn, "dyn "}, + {op_code_type::op_lnot, "lnot "}, + {op_code_type::op_usub, "usub "}, + {op_code_type::op_bnot, "bitnot"}, + {op_code_type::op_btor, "bitor "}, + {op_code_type::op_btxor, "bitxor"}, + {op_code_type::op_btand, "bitand"}, + {op_code_type::op_add, "add "}, + {op_code_type::op_sub, "sub "}, + {op_code_type::op_mul, "mult "}, + {op_code_type::op_div, "div "}, + {op_code_type::op_lnk, "lnk "}, + {op_code_type::op_addc, "addc "}, + {op_code_type::op_subc, "subc "}, + {op_code_type::op_mulc, "multc "}, + {op_code_type::op_divc, "divc "}, + {op_code_type::op_lnkc, "lnkc "}, + {op_code_type::op_addeq, "addeq "}, + {op_code_type::op_subeq, "subeq "}, + {op_code_type::op_muleq, "muleq "}, + {op_code_type::op_diveq, "diveq "}, + {op_code_type::op_lnkeq, "lnkeq "}, + {op_code_type::op_btandeq, "bandeq"}, + {op_code_type::op_btoreq, "boreq "}, + {op_code_type::op_btxoreq, "bxoreq"}, + {op_code_type::op_addeqc, "addeqc"}, + {op_code_type::op_subeqc, "subeqc"}, + {op_code_type::op_muleqc, "muleqc"}, + {op_code_type::op_diveqc, "diveqc"}, + {op_code_type::op_lnkeqc, "lnkeqc"}, + {op_code_type::op_addecp, "addecp"}, + {op_code_type::op_subecp, "subecp"}, + {op_code_type::op_mulecp, "mulecp"}, + {op_code_type::op_divecp, "divecp"}, + {op_code_type::op_lnkecp, "lnkecp"}, + {op_code_type::op_meq, "meq "}, + {op_code_type::op_eq, "eq "}, + {op_code_type::op_neq, "neq "}, + {op_code_type::op_less, "less "}, + {op_code_type::op_leq, "leq "}, + {op_code_type::op_grt, "grt "}, + {op_code_type::op_geq, "geq "}, + {op_code_type::op_lessc, "lessc "}, + {op_code_type::op_leqc, "leqc "}, + {op_code_type::op_grtc, "grtc "}, + {op_code_type::op_geqc, "geqc "}, + {op_code_type::op_pop, "pop "}, + {op_code_type::op_jmp, "jmp "}, + {op_code_type::op_jt, "jt "}, + {op_code_type::op_jf, "jf "}, + {op_code_type::op_cnt, "cnt "}, + {op_code_type::op_findex, "findx "}, + {op_code_type::op_feach, "feach "}, + {op_code_type::op_callg, "callg "}, + {op_code_type::op_calll, "calll "}, + {op_code_type::op_upval, "upval "}, + {op_code_type::op_callv, "callv "}, + {op_code_type::op_callvi, "callvi"}, + {op_code_type::op_callh, "callh "}, + {op_code_type::op_callfv, "callfv"}, + {op_code_type::op_callfh, "callfh"}, + {op_code_type::op_callb, "callb "}, + {op_code_type::op_slcbeg, "slcbeg"}, + {op_code_type::op_slcend, "slcend"}, + {op_code_type::op_slc, "slice "}, + {op_code_type::op_slc2, "slice2"}, + {op_code_type::op_mcallg, "mcallg"}, + {op_code_type::op_mcalll, "mcalll"}, + {op_code_type::op_mupval, "mupval"}, + {op_code_type::op_mcallv, "mcallv"}, + {op_code_type::op_mcallh, "mcallh"}, + {op_code_type::op_ret, "ret "} +}; + struct opcode { u8 op; // opcode u16 fidx; // source code file index @@ -128,6 +219,4 @@ public: std::ostream& operator<<(std::ostream&, const codestream&); -extern const char* operand_name_table[]; - } \ No newline at end of file diff --git a/src/nasal_parse.cpp b/src/nasal_parse.cpp index f43e4d2..e6ea255 100644 --- a/src/nasal_parse.cpp +++ b/src/nasal_parse.cpp @@ -595,7 +595,7 @@ expr* parse::null_chain_expr() { auto node = additive_expr(); while(lookahead(tok::tk_quesques)) { auto tmp = new binary_operator(toks[ptr].loc); - tmp->set_operator_type(binary_operator::binary_type::nullchain); + tmp->set_operator_type(binary_operator::binary_type::null_chain); tmp->set_left(node); match(tok::tk_quesques); tmp->set_right(additive_expr());