add ivar/fvar/svar lookup funcs in codegen

master
Daniel Kolesa 2021-04-09 03:14:12 +02:00
parent bac186a0c7
commit 51da22be06
3 changed files with 35 additions and 14 deletions

View File

@ -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() { void gen_state::gen_main_null() {
code.reserve(code.size() + 4); code.reserve(code.size() + 4);
code.push_back(BC_INST_START); code.push_back(BC_INST_START);

View File

@ -45,6 +45,10 @@ struct gen_state {
std::size_t line = 0 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_null();
void gen_main_integer(integer_type v); void gen_main_integer(integer_type v);
void gen_main_float(float_type v); void gen_main_float(float_type v);

View File

@ -434,35 +434,30 @@ lookupid:
); );
switch (id.get_type()) { switch (id.get_type()) {
case ident_type::IVAR: case ident_type::IVAR:
gs.gs.code.push_back( gs.gs.gen_lookup_ivar(id, ltype);
BC_INST_IVAR | ret_code(ltype, BC_RET_INT) |
(id.get_index() << 8)
);
switch (ltype) { switch (ltype) {
case VAL_POP: case VAL_POP:
gs.gs.code.pop_back();
break; break;
case VAL_CODE: case VAL_CODE:
gs.gs.gen_lookup_ivar(id, ltype);
gs.gs.code.push_back(BC_INST_COMPILE); gs.gs.code.push_back(BC_INST_COMPILE);
break; break;
case VAL_IDENT: case VAL_IDENT:
gs.gs.gen_lookup_ivar(id, ltype);
gs.gs.code.push_back(BC_INST_IDENT_U); gs.gs.code.push_back(BC_INST_IDENT_U);
break; break;
} }
return; return;
case ident_type::FVAR: case ident_type::FVAR:
gs.gs.code.push_back(
BC_INST_FVAR | ret_code(ltype, BC_RET_FLOAT) |
(id.get_index() << 8)
);
switch (ltype) { switch (ltype) {
case VAL_POP: case VAL_POP:
gs.gs.code.pop_back();
break; break;
case VAL_CODE: case VAL_CODE:
gs.gs.gen_lookup_fvar(id, ltype);
gs.gs.code.push_back(BC_INST_COMPILE); gs.gs.code.push_back(BC_INST_COMPILE);
break; break;
case VAL_IDENT: case VAL_IDENT:
gs.gs.gen_lookup_fvar(id, ltype);
gs.gs.code.push_back(BC_INST_IDENT_U); gs.gs.code.push_back(BC_INST_IDENT_U);
break; break;
} }
@ -472,10 +467,7 @@ lookupid:
case VAL_POP: case VAL_POP:
return; return;
default: default:
gs.gs.code.push_back( gs.gs.gen_lookup_svar(id, ltype);
BC_INST_SVAR | ret_code(ltype, BC_RET_STRING) |
(id.get_index() << 8)
);
break; break;
} }
goto done; goto done;