From 295c905c32663428868a3ba65566e5d12d5848f7 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 9 Apr 2021 03:34:48 +0200 Subject: [PATCH] more codegen utils --- src/cs_gen.cc | 10 ++++++++++ src/cs_gen.hh | 9 ++++++--- src/cs_parser.cc | 28 ++++++++++------------------ 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/cs_gen.cc b/src/cs_gen.cc index 317b2d2..63fdaec 100644 --- a/src/cs_gen.cc +++ b/src/cs_gen.cc @@ -243,6 +243,16 @@ void gen_state::gen_lookup_svar(ident &id, int ltype) { ); } +void gen_state::gen_lookup_alias(ident &id, int ltype, int dtype) { + code.push_back( + BC_INST_LOOKUP | ret_code(ltype, dtype) | (id.get_index() << 8) + ); +} + +void gen_state::gen_lookup_ident(int ltype) { + code.push_back(BC_INST_LOOKUP_U | ret_code(ltype)); +} + void gen_state::gen_compile(bool cond) { if (cond) { code.push_back(BC_INST_COND); diff --git a/src/cs_gen.hh b/src/cs_gen.hh index e029f23..57c1847 100644 --- a/src/cs_gen.hh +++ b/src/cs_gen.hh @@ -47,9 +47,12 @@ struct gen_state { std::size_t line = 0 ); - void gen_lookup_ivar(ident &id, int ltype); - void gen_lookup_fvar(ident &id, int ltype); - void gen_lookup_svar(ident &id, int ltype); + void gen_lookup_ivar(ident &id, int ltype = 0); + void gen_lookup_fvar(ident &id, int ltype = 0); + void gen_lookup_svar(ident &id, int ltype = 0); + + void gen_lookup_alias(ident &id, int ltype = 0, int dtype = 0); + void gen_lookup_ident(int ltype = 0); void gen_compile(bool cond = false); void gen_ident_lookup(); diff --git a/src/cs_parser.cc b/src/cs_parser.cc index 0ff2cdd..c3931ba 100644 --- a/src/cs_parser.cc +++ b/src/cs_parser.cc @@ -476,16 +476,10 @@ lookupid: case VAL_POP: return; case VAL_COND: - gs.gs.code.push_back( - BC_INST_LOOKUP | (id.get_index() << 8) - ); + gs.gs.gen_lookup_alias(id); break; default: - gs.gs.code.push_back( - BC_INST_LOOKUP | - ret_code(ltype, BC_RET_STRING) | - (id.get_index() << 8) - ); + gs.gs.gen_lookup_alias(id, ltype, BC_RET_STRING); break; } goto done; @@ -574,10 +568,10 @@ lookupid: } switch (ltype) { case VAL_COND: - gs.gs.code.push_back(BC_INST_LOOKUP_U); + gs.gs.gen_lookup_ident(); break; default: - gs.gs.code.push_back(BC_INST_LOOKUP_U | ret_code(ltype)); + gs.gs.gen_lookup_ident(ltype); break; } done: @@ -624,7 +618,7 @@ static bool compileblocksub(parser_state &gs) { if (!compilearg(gs, VAL_STRING)) { return false; } - gs.gs.code.push_back(BC_INST_LOOKUP_U); + gs.gs.gen_lookup_ident(); break; case '\"': lookup = gs.get_str_dup(); @@ -642,24 +636,22 @@ lookupid: ); switch (id.get_type()) { case ident_type::IVAR: - gs.gs.code.push_back(BC_INST_IVAR | (id.get_index() << 8)); + gs.gs.gen_lookup_ivar(id); goto done; case ident_type::FVAR: - gs.gs.code.push_back(BC_INST_FVAR | (id.get_index() << 8)); + gs.gs.gen_lookup_fvar(id); goto done; case ident_type::SVAR: - gs.gs.code.push_back(BC_INST_SVAR | (id.get_index() << 8)); + gs.gs.gen_lookup_svar(id); goto done; case ident_type::ALIAS: - gs.gs.code.push_back( - BC_INST_LOOKUP | (id.get_index() << 8) - ); + gs.gs.gen_lookup_alias(id); goto done; default: break; } gs.gs.gen_val_string(lookup.str_term()); - gs.gs.code.push_back(BC_INST_LOOKUP_U); + gs.gs.gen_lookup_ident(); done: break; }