Skip to content

Commit a4941a8

Browse files
committed
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
1 parent d298013 commit a4941a8

4 files changed

Lines changed: 36 additions & 31 deletions

File tree

py/compile.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,14 @@ STATIC void c_assign_atom_expr(compiler_t *comp, mp_parse_node_struct_t *pns, as
366366
if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
367367
if (assign_kind == ASSIGN_AUG_STORE) {
368368
EMIT(rot_three);
369-
EMIT(store_subscr);
369+
EMIT_ARG(subscr, MP_EMIT_SUBSCR_STORE);
370370
} else {
371371
compile_node(comp, pns1->nodes[0]);
372372
if (assign_kind == ASSIGN_AUG_LOAD) {
373373
EMIT(dup_top_two);
374-
EMIT(load_subscr);
374+
EMIT_ARG(subscr, MP_EMIT_SUBSCR_LOAD);
375375
} else {
376-
EMIT(store_subscr);
376+
EMIT_ARG(subscr, MP_EMIT_SUBSCR_STORE);
377377
}
378378
}
379379
return;
@@ -884,7 +884,7 @@ STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
884884
}
885885
if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
886886
compile_node(comp, pns1->nodes[0]);
887-
EMIT(delete_subscr);
887+
EMIT_ARG(subscr, MP_EMIT_SUBSCR_DELETE);
888888
} else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
889889
assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
890890
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)
25362536
STATIC void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
25372537
// object who's index we want is on top of stack
25382538
compile_node(comp, pns->nodes[0]); // the index
2539-
EMIT(load_subscr);
2539+
EMIT_ARG(subscr, MP_EMIT_SUBSCR_LOAD);
25402540
}
25412541

25422542
STATIC void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {

py/emit.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ typedef enum {
6363
#define MP_EMIT_IDOP_GLOBAL_NAME (0)
6464
#define MP_EMIT_IDOP_GLOBAL_GLOBAL (1)
6565

66+
// Kind for emit->subscr()
67+
#define MP_EMIT_SUBSCR_LOAD (0)
68+
#define MP_EMIT_SUBSCR_STORE (1)
69+
#define MP_EMIT_SUBSCR_DELETE (2)
70+
6671
// Kind for emit->build()
6772
#define MP_EMIT_BUILD_TUPLE (0)
6873
#define MP_EMIT_BUILD_LIST (1)
@@ -103,11 +108,9 @@ typedef struct _emit_method_table_t {
103108
void (*load_attr)(emit_t *emit, qstr qst);
104109
void (*load_method)(emit_t *emit, qstr qst, bool is_super);
105110
void (*load_build_class)(emit_t *emit);
106-
void (*load_subscr)(emit_t *emit);
111+
void (*subscr)(emit_t *emit, int kind);
107112
void (*store_attr)(emit_t *emit, qstr qst);
108-
void (*store_subscr)(emit_t *emit);
109113
void (*delete_attr)(emit_t *emit, qstr qst);
110-
void (*delete_subscr)(emit_t *emit);
111114
void (*dup_top)(emit_t *emit);
112115
void (*dup_top_two)(emit_t *emit);
113116
void (*pop_top)(emit_t *emit);
@@ -211,11 +214,9 @@ void mp_emit_bc_load_null(emit_t *emit);
211214
void mp_emit_bc_load_attr(emit_t *emit, qstr qst);
212215
void mp_emit_bc_load_method(emit_t *emit, qstr qst, bool is_super);
213216
void mp_emit_bc_load_build_class(emit_t *emit);
214-
void mp_emit_bc_load_subscr(emit_t *emit);
217+
void mp_emit_bc_subscr(emit_t *emit, int kind);
215218
void mp_emit_bc_store_attr(emit_t *emit, qstr qst);
216-
void mp_emit_bc_store_subscr(emit_t *emit);
217219
void mp_emit_bc_delete_attr(emit_t *emit, qstr qst);
218-
void mp_emit_bc_delete_subscr(emit_t *emit);
219220
void mp_emit_bc_dup_top(emit_t *emit);
220221
void mp_emit_bc_dup_top_two(emit_t *emit);
221222
void mp_emit_bc_pop_top(emit_t *emit);

py/emitbc.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,18 @@ void mp_emit_bc_load_build_class(emit_t *emit) {
597597
emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
598598
}
599599

600-
void mp_emit_bc_load_subscr(emit_t *emit) {
601-
emit_bc_pre(emit, -1);
602-
emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
600+
void mp_emit_bc_subscr(emit_t *emit, int kind) {
601+
if (kind == MP_EMIT_SUBSCR_LOAD) {
602+
emit_bc_pre(emit, -1);
603+
emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
604+
} else {
605+
if (kind == MP_EMIT_SUBSCR_DELETE) {
606+
mp_emit_bc_load_null(emit);
607+
mp_emit_bc_rot_three(emit);
608+
}
609+
emit_bc_pre(emit, -3);
610+
emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
611+
}
603612
}
604613

605614
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) {
629638
}
630639
}
631640

632-
void mp_emit_bc_store_subscr(emit_t *emit) {
633-
emit_bc_pre(emit, -3);
634-
emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
635-
}
636-
637641
void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
638642
MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_DELETE_FAST);
639643
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) {
654658
mp_emit_bc_store_attr(emit, qst);
655659
}
656660

657-
void mp_emit_bc_delete_subscr(emit_t *emit) {
658-
mp_emit_bc_load_null(emit);
659-
mp_emit_bc_rot_three(emit);
660-
mp_emit_bc_store_subscr(emit);
661-
}
662-
663661
void mp_emit_bc_dup_top(emit_t *emit) {
664662
emit_bc_pre(emit, 1);
665663
emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
@@ -964,11 +962,9 @@ const emit_method_table_t emit_bc_method_table = {
964962
mp_emit_bc_load_attr,
965963
mp_emit_bc_load_method,
966964
mp_emit_bc_load_build_class,
967-
mp_emit_bc_load_subscr,
965+
mp_emit_bc_subscr,
968966
mp_emit_bc_store_attr,
969-
mp_emit_bc_store_subscr,
970967
mp_emit_bc_delete_attr,
971-
mp_emit_bc_delete_subscr,
972968
mp_emit_bc_dup_top,
973969
mp_emit_bc_dup_top_two,
974970
mp_emit_bc_pop_top,

py/emitnative.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,16 @@ STATIC void emit_native_delete_subscr(emit_t *emit) {
14471447
emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3);
14481448
}
14491449

1450+
STATIC void emit_native_subscr(emit_t *emit, int kind) {
1451+
if (kind == MP_EMIT_SUBSCR_LOAD) {
1452+
emit_native_load_subscr(emit);
1453+
} else if (kind == MP_EMIT_SUBSCR_STORE) {
1454+
emit_native_store_subscr(emit);
1455+
} else {
1456+
emit_native_delete_subscr(emit);
1457+
}
1458+
}
1459+
14501460
STATIC void emit_native_dup_top(emit_t *emit) {
14511461
DEBUG_printf("dup_top\n");
14521462
vtype_kind_t vtype;
@@ -2219,11 +2229,9 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
22192229
emit_native_load_attr,
22202230
emit_native_load_method,
22212231
emit_native_load_build_class,
2222-
emit_native_load_subscr,
2232+
emit_native_subscr,
22232233
emit_native_store_attr,
2224-
emit_native_store_subscr,
22252234
emit_native_delete_attr,
2226-
emit_native_delete_subscr,
22272235
emit_native_dup_top,
22282236
emit_native_dup_top_two,
22292237
emit_native_pop_top,

0 commit comments

Comments
 (0)