Skip to content

Commit 18e6358

Browse files
committed
py/emit: Combine setup with/except/finally into one emit function.
This patch reduces code size by: bare-arm: -16 minimal x86: -156 unix x64: -288 unix nanbox: -184 stm32: -48 cc3200: -16 esp8266: -96 esp32: -16 The last 10 patches combined reduce code size by: bare-arm: -164 minimal x86: -1260 unix x64: -3416 unix nanbox: -1616 stm32: -676 cc3200: -232 esp8266: -1144 esp32: -268
1 parent 436e0d4 commit 18e6358

4 files changed

Lines changed: 42 additions & 47 deletions

File tree

py/compile.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,7 @@ STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_
15161516
uint l1 = comp_next_label(comp);
15171517
uint success_label = comp_next_label(comp);
15181518

1519-
EMIT_ARG(setup_except, l1);
1519+
EMIT_ARG(setup_block, l1, MP_EMIT_SETUP_BLOCK_EXCEPT);
15201520
compile_increase_except_level(comp);
15211521

15221522
compile_node(comp, pn_body); // body
@@ -1571,7 +1571,7 @@ STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_
15711571
uint l3 = 0;
15721572
if (qstr_exception_local != 0) {
15731573
l3 = comp_next_label(comp);
1574-
EMIT_ARG(setup_finally, l3);
1574+
EMIT_ARG(setup_block, l3, MP_EMIT_SETUP_BLOCK_FINALLY);
15751575
compile_increase_except_level(comp);
15761576
}
15771577
compile_node(comp, pns_except->nodes[1]);
@@ -1606,7 +1606,7 @@ STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_
16061606
STATIC void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
16071607
uint l_finally_block = comp_next_label(comp);
16081608

1609-
EMIT_ARG(setup_finally, l_finally_block);
1609+
EMIT_ARG(setup_block, l_finally_block, MP_EMIT_SETUP_BLOCK_FINALLY);
16101610
compile_increase_except_level(comp);
16111611

16121612
if (n_except == 0) {
@@ -1668,12 +1668,12 @@ STATIC void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *n
16681668
// this pre-bit is of the form "a as b"
16691669
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
16701670
compile_node(comp, pns->nodes[0]);
1671-
EMIT_ARG(setup_with, l_end);
1671+
EMIT_ARG(setup_block, l_end, MP_EMIT_SETUP_BLOCK_WITH);
16721672
c_assign(comp, pns->nodes[1], ASSIGN_STORE);
16731673
} else {
16741674
// this pre-bit is just an expression
16751675
compile_node(comp, nodes[0]);
1676-
EMIT_ARG(setup_with, l_end);
1676+
EMIT_ARG(setup_block, l_end, MP_EMIT_SETUP_BLOCK_WITH);
16771677
EMIT(pop_top);
16781678
}
16791679
compile_increase_except_level(comp);
@@ -1726,7 +1726,7 @@ STATIC void compile_async_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns
17261726

17271727
EMIT_ARG(label_assign, continue_label);
17281728

1729-
EMIT_ARG(setup_except, try_exception_label);
1729+
EMIT_ARG(setup_block, try_exception_label, MP_EMIT_SETUP_BLOCK_EXCEPT);
17301730
compile_increase_except_level(comp);
17311731

17321732
compile_load_id(comp, context);
@@ -1797,7 +1797,7 @@ STATIC void compile_async_with_stmt_helper(compiler_t *comp, int n, mp_parse_nod
17971797
compile_load_id(comp, context);
17981798
EMIT_ARG(load_method, MP_QSTR___aexit__, false);
17991799

1800-
EMIT_ARG(setup_except, try_exception_label);
1800+
EMIT_ARG(setup_block, try_exception_label, MP_EMIT_SETUP_BLOCK_EXCEPT);
18011801
compile_increase_except_level(comp);
18021802
// compile additional pre-bits and the body
18031803
compile_async_with_stmt_helper(comp, n - 1, nodes + 1, body);

py/emit.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ typedef enum {
7878
#define MP_EMIT_ATTR_STORE (1)
7979
#define MP_EMIT_ATTR_DELETE (2)
8080

81+
// Kind for emit->setup_block()
82+
#define MP_EMIT_SETUP_BLOCK_WITH (0)
83+
#define MP_EMIT_SETUP_BLOCK_EXCEPT (2)
84+
#define MP_EMIT_SETUP_BLOCK_FINALLY (3)
85+
8186
// Kind for emit->build()
8287
#define MP_EMIT_BUILD_TUPLE (0)
8388
#define MP_EMIT_BUILD_LIST (1)
@@ -128,10 +133,8 @@ typedef struct _emit_method_table_t {
128133
void (*pop_jump_if)(emit_t *emit, bool cond, mp_uint_t label);
129134
void (*jump_if_or_pop)(emit_t *emit, bool cond, mp_uint_t label);
130135
void (*unwind_jump)(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
131-
void (*setup_with)(emit_t *emit, mp_uint_t label);
136+
void (*setup_block)(emit_t *emit, mp_uint_t label, int kind);
132137
void (*with_cleanup)(emit_t *emit, mp_uint_t label);
133-
void (*setup_except)(emit_t *emit, mp_uint_t label);
134-
void (*setup_finally)(emit_t *emit, mp_uint_t label);
135138
void (*end_finally)(emit_t *emit);
136139
void (*get_iter)(emit_t *emit, bool use_stack);
137140
void (*for_iter)(emit_t *emit, mp_uint_t label);
@@ -223,10 +226,8 @@ void mp_emit_bc_jump(emit_t *emit, mp_uint_t label);
223226
void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label);
224227
void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label);
225228
void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
226-
void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label);
229+
void mp_emit_bc_setup_block(emit_t *emit, mp_uint_t label, int kind);
227230
void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label);
228-
void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label);
229-
void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label);
230231
void mp_emit_bc_end_finally(emit_t *emit);
231232
void mp_emit_bc_get_iter(emit_t *emit, bool use_stack);
232233
void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label);

py/emitbc.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -719,11 +719,18 @@ void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_dept
719719
}
720720
}
721721

722-
void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
722+
void mp_emit_bc_setup_block(emit_t *emit, mp_uint_t label, int kind) {
723+
MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_WITH == MP_BC_SETUP_WITH);
724+
MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_EXCEPT == MP_BC_SETUP_EXCEPT);
725+
MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_FINALLY == MP_BC_SETUP_FINALLY);
726+
if (kind == MP_EMIT_SETUP_BLOCK_WITH) {
723727
// The SETUP_WITH opcode pops ctx_mgr from the top of the stack
724728
// and then pushes 3 entries: __exit__, ctx_mgr, as_value.
725-
emit_bc_pre(emit, 2);
726-
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
729+
emit_bc_pre(emit, 2);
730+
} else {
731+
emit_bc_pre(emit, 0);
732+
}
733+
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH + kind, label);
727734
}
728735

729736
void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label) {
@@ -732,17 +739,7 @@ void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label) {
732739
mp_emit_bc_label_assign(emit, label);
733740
emit_bc_pre(emit, 2); // ensure we have enough stack space to call the __exit__ method
734741
emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
735-
emit_bc_pre(emit, -4); // cancel the 2 above, plus the 2 from mp_emit_bc_setup_with
736-
}
737-
738-
void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
739-
emit_bc_pre(emit, 0);
740-
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
741-
}
742-
743-
void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
744-
emit_bc_pre(emit, 0);
745-
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
742+
emit_bc_pre(emit, -4); // cancel the 2 above, plus the 2 from mp_emit_bc_setup_block(MP_EMIT_SETUP_BLOCK_WITH)
746743
}
747744

748745
void mp_emit_bc_end_finally(emit_t *emit) {
@@ -953,10 +950,8 @@ const emit_method_table_t emit_bc_method_table = {
953950
mp_emit_bc_pop_jump_if,
954951
mp_emit_bc_jump_if_or_pop,
955952
mp_emit_bc_unwind_jump,
956-
mp_emit_bc_setup_with,
953+
mp_emit_bc_setup_block,
957954
mp_emit_bc_with_cleanup,
958-
mp_emit_bc_setup_except,
959-
mp_emit_bc_setup_finally,
960955
mp_emit_bc_end_finally,
961956
mp_emit_bc_get_iter,
962957
mp_emit_bc_for_iter,

py/emitnative.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,21 @@ STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
16171617
// stack: (..., __exit__, self, as_value, nlr_buf, as_value)
16181618
}
16191619

1620+
STATIC void emit_native_setup_block(emit_t *emit, mp_uint_t label, int kind) {
1621+
if (kind == MP_EMIT_SETUP_BLOCK_WITH) {
1622+
emit_native_setup_with(emit, label);
1623+
} else {
1624+
// Set up except and finally
1625+
emit_native_pre(emit);
1626+
// need to commit stack because we may jump elsewhere
1627+
need_stack_settled(emit);
1628+
emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_1, sizeof(nlr_buf_t) / sizeof(mp_uint_t)); // arg1 = pointer to nlr buf
1629+
emit_call(emit, MP_F_NLR_PUSH);
1630+
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1631+
emit_post(emit);
1632+
}
1633+
}
1634+
16201635
STATIC void emit_native_with_cleanup(emit_t *emit, mp_uint_t label) {
16211636
// note: label+1 is available as an auxiliary label
16221637

@@ -1686,20 +1701,6 @@ STATIC void emit_native_with_cleanup(emit_t *emit, mp_uint_t label) {
16861701
emit_native_label_assign(emit, label + 1);
16871702
}
16881703

1689-
STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
1690-
emit_native_pre(emit);
1691-
// need to commit stack because we may jump elsewhere
1692-
need_stack_settled(emit);
1693-
emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_1, sizeof(nlr_buf_t) / sizeof(mp_uint_t)); // arg1 = pointer to nlr buf
1694-
emit_call(emit, MP_F_NLR_PUSH);
1695-
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1696-
emit_post(emit);
1697-
}
1698-
1699-
STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
1700-
emit_native_setup_except(emit, label);
1701-
}
1702-
17031704
STATIC void emit_native_end_finally(emit_t *emit) {
17041705
// logic:
17051706
// exc = pop_stack
@@ -2254,10 +2255,8 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
22542255
emit_native_pop_jump_if,
22552256
emit_native_jump_if_or_pop,
22562257
emit_native_unwind_jump,
2257-
emit_native_setup_with,
2258+
emit_native_setup_block,
22582259
emit_native_with_cleanup,
2259-
emit_native_setup_except,
2260-
emit_native_setup_finally,
22612260
emit_native_end_finally,
22622261
emit_native_get_iter,
22632262
emit_native_for_iter,

0 commit comments

Comments
 (0)