From 51da22be066c3ee706d14510ff46608f9919d8a1 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Fri, 9 Apr 2021 03:14:12 +0200 Subject: [PATCH] add ivar/fvar/svar lookup funcs in codegen --- src/cs_gen.cc | 25 +++++++++++++++++++++++++ src/cs_gen.hh | 4 ++++ src/cs_parser.cc | 20 ++++++-------------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/cs_gen.cc b/src/cs_gen.cc index d0efd05..2fda80b 100644 --- a/src/cs_gen.cc +++ b/src/cs_gen.cc @@ -214,6 +214,31 @@ void gen_state::gen_val( } } +static inline int ret_code(int type, int def = 0) { + if (type >= VAL_ANY) { + return def; + } + return type << BC_INST_RET; +} + +void gen_state::gen_lookup_ivar(ident &id, int ltype) { + code.push_back( + BC_INST_IVAR | ret_code(ltype, BC_RET_INT) | (id.get_index() << 8) + ); +} + +void gen_state::gen_lookup_fvar(ident &id, int ltype) { + code.push_back( + BC_INST_FVAR | ret_code(ltype, BC_RET_FLOAT) | (id.get_index() << 8) + ); +} + +void gen_state::gen_lookup_svar(ident &id, int ltype) { + code.push_back( + BC_INST_SVAR | ret_code(ltype, BC_RET_STRING) | (id.get_index() << 8) + ); +} + void gen_state::gen_main_null() { code.reserve(code.size() + 4); code.push_back(BC_INST_START); diff --git a/src/cs_gen.hh b/src/cs_gen.hh index 821751e..91dc9bb 100644 --- a/src/cs_gen.hh +++ b/src/cs_gen.hh @@ -45,6 +45,10 @@ 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_main_null(); void gen_main_integer(integer_type v); void gen_main_float(float_type v); diff --git a/src/cs_parser.cc b/src/cs_parser.cc index a74ca95..0117f0a 100644 --- a/src/cs_parser.cc +++ b/src/cs_parser.cc @@ -434,35 +434,30 @@ lookupid: ); switch (id.get_type()) { case ident_type::IVAR: - gs.gs.code.push_back( - BC_INST_IVAR | ret_code(ltype, BC_RET_INT) | - (id.get_index() << 8) - ); + gs.gs.gen_lookup_ivar(id, ltype); switch (ltype) { case VAL_POP: - gs.gs.code.pop_back(); break; case VAL_CODE: + gs.gs.gen_lookup_ivar(id, ltype); gs.gs.code.push_back(BC_INST_COMPILE); break; case VAL_IDENT: + gs.gs.gen_lookup_ivar(id, ltype); gs.gs.code.push_back(BC_INST_IDENT_U); break; } return; case ident_type::FVAR: - gs.gs.code.push_back( - BC_INST_FVAR | ret_code(ltype, BC_RET_FLOAT) | - (id.get_index() << 8) - ); switch (ltype) { case VAL_POP: - gs.gs.code.pop_back(); break; case VAL_CODE: + gs.gs.gen_lookup_fvar(id, ltype); gs.gs.code.push_back(BC_INST_COMPILE); break; case VAL_IDENT: + gs.gs.gen_lookup_fvar(id, ltype); gs.gs.code.push_back(BC_INST_IDENT_U); break; } @@ -472,10 +467,7 @@ lookupid: case VAL_POP: return; default: - gs.gs.code.push_back( - BC_INST_SVAR | ret_code(ltype, BC_RET_STRING) | - (id.get_index() << 8) - ); + gs.gs.gen_lookup_svar(id, ltype); break; } goto done;