py/emit: Combine load/store/delete subscr into one emit function.

Reduces code size by:

   bare-arm:    -8
minimal x86:  -104
   unix x64:  -312
unix nanbox:  -120
      stm32:   -60
     cc3200:   -16
    esp8266:   -92
      esp32:   -24
This commit is contained in:
Damien George 2018-05-22 21:31:56 +10:00
parent d298013939
commit a4941a8ba4
4 changed files with 36 additions and 31 deletions

View File

@ -366,14 +366,14 @@ STATIC void c_assign_atom_expr(compiler_t *comp, mp_parse_node_struct_t *pns, as
if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
if (assign_kind == ASSIGN_AUG_STORE) {
EMIT(rot_three);
EMIT(store_subscr);
EMIT_ARG(subscr, MP_EMIT_SUBSCR_STORE);
} else {
compile_node(comp, pns1->nodes[0]);
if (assign_kind == ASSIGN_AUG_LOAD) {
EMIT(dup_top_two);
EMIT(load_subscr);
EMIT_ARG(subscr, MP_EMIT_SUBSCR_LOAD);
} else {
EMIT(store_subscr);
EMIT_ARG(subscr, MP_EMIT_SUBSCR_STORE);
}
}
return;
@ -884,7 +884,7 @@ STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
}
if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
compile_node(comp, pns1->nodes[0]);
EMIT(delete_subscr);
EMIT_ARG(subscr, MP_EMIT_SUBSCR_DELETE);
} else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
@ -2536,7 +2536,7 @@ STATIC void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns)
STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
// object who's index we want is on top of stack
compile_node(comp, pns->nodes[0]); // the index
EMIT(load_subscr);
EMIT_ARG(subscr, MP_EMIT_SUBSCR_LOAD);
}
STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {

View File

@ -63,6 +63,11 @@ typedef enum {
#define MP_EMIT_IDOP_GLOBAL_NAME (0)
#define MP_EMIT_IDOP_GLOBAL_GLOBAL (1)
// Kind for emit->subscr()
#define MP_EMIT_SUBSCR_LOAD (0)
#define MP_EMIT_SUBSCR_STORE (1)
#define MP_EMIT_SUBSCR_DELETE (2)
// Kind for emit->build()
#define MP_EMIT_BUILD_TUPLE (0)
#define MP_EMIT_BUILD_LIST (1)
@ -103,11 +108,9 @@ typedef struct _emit_method_table_t {
void (*load_attr)(emit_t *emit, qstr qst);
void (*load_method)(emit_t *emit, qstr qst, bool is_super);
void (*load_build_class)(emit_t *emit);
void (*load_subscr)(emit_t *emit);
void (*subscr)(emit_t *emit, int kind);
void (*store_attr)(emit_t *emit, qstr qst);
void (*store_subscr)(emit_t *emit);
void (*delete_attr)(emit_t *emit, qstr qst);
void (*delete_subscr)(emit_t *emit);
void (*dup_top)(emit_t *emit);
void (*dup_top_two)(emit_t *emit);
void (*pop_top)(emit_t *emit);
@ -211,11 +214,9 @@ void mp_emit_bc_load_null(emit_t *emit);
void mp_emit_bc_load_attr(emit_t *emit, qstr qst);
void mp_emit_bc_load_method(emit_t *emit, qstr qst, bool is_super);
void mp_emit_bc_load_build_class(emit_t *emit);
void mp_emit_bc_load_subscr(emit_t *emit);
void mp_emit_bc_subscr(emit_t *emit, int kind);
void mp_emit_bc_store_attr(emit_t *emit, qstr qst);
void mp_emit_bc_store_subscr(emit_t *emit);
void mp_emit_bc_delete_attr(emit_t *emit, qstr qst);
void mp_emit_bc_delete_subscr(emit_t *emit);
void mp_emit_bc_dup_top(emit_t *emit);
void mp_emit_bc_dup_top_two(emit_t *emit);
void mp_emit_bc_pop_top(emit_t *emit);

View File

@ -597,9 +597,18 @@ void mp_emit_bc_load_build_class(emit_t *emit) {
emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
}
void mp_emit_bc_load_subscr(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
void mp_emit_bc_subscr(emit_t *emit, int kind) {
if (kind == MP_EMIT_SUBSCR_LOAD) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
} else {
if (kind == MP_EMIT_SUBSCR_DELETE) {
mp_emit_bc_load_null(emit);
mp_emit_bc_rot_three(emit);
}
emit_bc_pre(emit, -3);
emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
}
}
void mp_emit_bc_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
@ -629,11 +638,6 @@ void mp_emit_bc_store_attr(emit_t *emit, qstr qst) {
}
}
void mp_emit_bc_store_subscr(emit_t *emit) {
emit_bc_pre(emit, -3);
emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
}
void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_DELETE_FAST);
MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_DELETE_DEREF);
@ -654,12 +658,6 @@ void mp_emit_bc_delete_attr(emit_t *emit, qstr qst) {
mp_emit_bc_store_attr(emit, qst);
}
void mp_emit_bc_delete_subscr(emit_t *emit) {
mp_emit_bc_load_null(emit);
mp_emit_bc_rot_three(emit);
mp_emit_bc_store_subscr(emit);
}
void mp_emit_bc_dup_top(emit_t *emit) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
@ -964,11 +962,9 @@ const emit_method_table_t emit_bc_method_table = {
mp_emit_bc_load_attr,
mp_emit_bc_load_method,
mp_emit_bc_load_build_class,
mp_emit_bc_load_subscr,
mp_emit_bc_subscr,
mp_emit_bc_store_attr,
mp_emit_bc_store_subscr,
mp_emit_bc_delete_attr,
mp_emit_bc_delete_subscr,
mp_emit_bc_dup_top,
mp_emit_bc_dup_top_two,
mp_emit_bc_pop_top,

View File

@ -1447,6 +1447,16 @@ STATIC void emit_native_delete_subscr(emit_t *emit) {
emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3);
}
STATIC void emit_native_subscr(emit_t *emit, int kind) {
if (kind == MP_EMIT_SUBSCR_LOAD) {
emit_native_load_subscr(emit);
} else if (kind == MP_EMIT_SUBSCR_STORE) {
emit_native_store_subscr(emit);
} else {
emit_native_delete_subscr(emit);
}
}
STATIC void emit_native_dup_top(emit_t *emit) {
DEBUG_printf("dup_top\n");
vtype_kind_t vtype;
@ -2219,11 +2229,9 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
emit_native_load_attr,
emit_native_load_method,
emit_native_load_build_class,
emit_native_load_subscr,
emit_native_subscr,
emit_native_store_attr,
emit_native_store_subscr,
emit_native_delete_attr,
emit_native_delete_subscr,
emit_native_dup_top,
emit_native_dup_top_two,
emit_native_pop_top,