Skip to content

Commit 63f3832

Browse files
committed
py: Combine emit functions for jump true/false to reduce code size.
Saves 116 bytes for stmhal and 56 bytes for cc3200 port.
1 parent 0b2fd91 commit 63f3832

File tree

6 files changed

+53
-86
lines changed

6 files changed

+53
-86
lines changed

py/compile.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,7 @@ STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if
639639

640640
// nothing special, fall back to default compiling for node and jump
641641
compile_node(comp, pn);
642-
if (jump_if == false) {
643-
EMIT_ARG(pop_jump_if_false, label);
644-
} else {
645-
EMIT_ARG(pop_jump_if_true, label);
646-
}
642+
EMIT_ARG(pop_jump_if, jump_if, label);
647643
}
648644
#endif
649645

@@ -711,11 +707,7 @@ STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int la
711707

712708
// nothing special, fall back to default compiling for node and jump
713709
compile_node(comp, pn);
714-
if (jump_if == false) {
715-
EMIT_ARG(pop_jump_if_false, label);
716-
} else {
717-
EMIT_ARG(pop_jump_if_true, label);
718-
}
710+
EMIT_ARG(pop_jump_if, jump_if, label);
719711
#endif
720712
}
721713

@@ -1825,7 +1817,7 @@ STATIC void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t p
18251817
} else {
18261818
EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
18271819
}
1828-
EMIT_ARG(pop_jump_if_true, top_label);
1820+
EMIT_ARG(pop_jump_if, true, top_label);
18291821

18301822
// break/continue apply to outer loop (if any) in the else block
18311823
END_BREAK_CONTINUE_BLOCK
@@ -1971,7 +1963,7 @@ STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_
19711963
EMIT(dup_top);
19721964
compile_node(comp, pns_exception_expr);
19731965
EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
1974-
EMIT_ARG(pop_jump_if_false, end_finally_label);
1966+
EMIT_ARG(pop_jump_if, false, end_finally_label);
19751967
}
19761968

19771969
EMIT(pop_top);
@@ -2267,7 +2259,7 @@ STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
22672259
for (int i = 0; i < n; i += 1) {
22682260
compile_node(comp, pns->nodes[i]);
22692261
if (i + 1 < n) {
2270-
EMIT_ARG(jump_if_true_or_pop, l_end);
2262+
EMIT_ARG(jump_if_or_pop, true, l_end);
22712263
}
22722264
}
22732265
EMIT_ARG(label_assign, l_end);
@@ -2279,7 +2271,7 @@ STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
22792271
for (int i = 0; i < n; i += 1) {
22802272
compile_node(comp, pns->nodes[i]);
22812273
if (i + 1 < n) {
2282-
EMIT_ARG(jump_if_false_or_pop, l_end);
2274+
EMIT_ARG(jump_if_or_pop, false, l_end);
22832275
}
22842276
}
22852277
EMIT_ARG(label_assign, l_end);
@@ -2332,7 +2324,7 @@ STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
23322324
}
23332325
}
23342326
if (i + 2 < num_nodes) {
2335-
EMIT_ARG(jump_if_false_or_pop, l_fail);
2327+
EMIT_ARG(jump_if_or_pop, false, l_fail);
23362328
}
23372329
}
23382330
if (multi) {

py/emit.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,8 @@ typedef struct _emit_method_table_t {
106106
void (*rot_two)(emit_t *emit);
107107
void (*rot_three)(emit_t *emit);
108108
void (*jump)(emit_t *emit, mp_uint_t label);
109-
void (*pop_jump_if_true)(emit_t *emit, mp_uint_t label);
110-
void (*pop_jump_if_false)(emit_t *emit, mp_uint_t label);
111-
void (*jump_if_true_or_pop)(emit_t *emit, mp_uint_t label);
112-
void (*jump_if_false_or_pop)(emit_t *emit, mp_uint_t label);
109+
void (*pop_jump_if)(emit_t *emit, bool cond, mp_uint_t label);
110+
void (*jump_if_or_pop)(emit_t *emit, bool cond, mp_uint_t label);
113111
void (*break_loop)(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
114112
void (*continue_loop)(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
115113
void (*setup_with)(emit_t *emit, mp_uint_t label);

py/emitbc.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -652,24 +652,22 @@ STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) {
652652
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
653653
}
654654

655-
STATIC void emit_bc_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
655+
STATIC void emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
656656
emit_bc_pre(emit, -1);
657-
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
658-
}
659-
660-
STATIC void emit_bc_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
661-
emit_bc_pre(emit, -1);
662-
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
663-
}
664-
665-
STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
666-
emit_bc_pre(emit, -1);
667-
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
657+
if (cond) {
658+
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
659+
} else {
660+
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
661+
}
668662
}
669663

670-
STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
664+
STATIC void emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
671665
emit_bc_pre(emit, -1);
672-
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
666+
if (cond) {
667+
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
668+
} else {
669+
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
670+
}
673671
}
674672

675673
STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
@@ -951,10 +949,8 @@ const emit_method_table_t emit_bc_method_table = {
951949
emit_bc_rot_two,
952950
emit_bc_rot_three,
953951
emit_bc_jump,
954-
emit_bc_pop_jump_if_true,
955-
emit_bc_pop_jump_if_false,
956-
emit_bc_jump_if_true_or_pop,
957-
emit_bc_jump_if_false_or_pop,
952+
emit_bc_pop_jump_if,
953+
emit_bc_jump_if_or_pop,
958954
emit_bc_unwind_jump,
959955
emit_bc_unwind_jump,
960956
emit_bc_setup_with,

py/emitcpy.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -415,31 +415,25 @@ STATIC void emit_cpy_jump(emit_t *emit, mp_uint_t label) {
415415
}
416416
}
417417

418-
STATIC void emit_cpy_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
418+
STATIC void emit_cpy_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
419419
emit_pre(emit, -1, 3);
420420
if (emit->pass == MP_PASS_EMIT) {
421-
printf("POP_JUMP_IF_TRUE " UINT_FMT "\n", emit->label_offsets[label]);
422-
}
423-
}
424-
425-
STATIC void emit_cpy_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
426-
emit_pre(emit, -1, 3);
427-
if (emit->pass == MP_PASS_EMIT) {
428-
printf("POP_JUMP_IF_FALSE " UINT_FMT "\n", emit->label_offsets[label]);
429-
}
430-
}
431-
432-
STATIC void emit_cpy_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
433-
emit_pre(emit, -1, 3);
434-
if (emit->pass == MP_PASS_EMIT) {
435-
printf("JUMP_IF_TRUE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
421+
if (cond) {
422+
printf("POP_JUMP_IF_TRUE " UINT_FMT "\n", emit->label_offsets[label]);
423+
} else {
424+
printf("POP_JUMP_IF_FALSE " UINT_FMT "\n", emit->label_offsets[label]);
425+
}
436426
}
437427
}
438428

439-
STATIC void emit_cpy_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
429+
STATIC void emit_cpy_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
440430
emit_pre(emit, -1, 3);
441431
if (emit->pass == MP_PASS_EMIT) {
442-
printf("JUMP_IF_FALSE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
432+
if (cond) {
433+
printf("JUMP_IF_TRUE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
434+
} else {
435+
printf("JUMP_IF_FALSE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
436+
}
443437
}
444438
}
445439

@@ -854,10 +848,8 @@ const emit_method_table_t emit_cpython_method_table = {
854848
emit_cpy_rot_two,
855849
emit_cpy_rot_three,
856850
emit_cpy_jump,
857-
emit_cpy_pop_jump_if_true,
858-
emit_cpy_pop_jump_if_false,
859-
emit_cpy_jump_if_true_or_pop,
860-
emit_cpy_jump_if_false_or_pop,
851+
emit_cpy_pop_jump_if,
852+
emit_cpy_jump_if_or_pop,
861853
emit_cpy_break_loop,
862854
emit_cpy_continue_loop,
863855
emit_cpy_setup_with,

py/emitnative.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,32 +1744,25 @@ STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
17441744
need_stack_settled(emit);
17451745
}
17461746

1747-
STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
1748-
DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label);
1747+
STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1748+
DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
17491749
emit_native_jump_helper(emit, true);
1750-
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1751-
emit_post(emit);
1752-
}
1753-
1754-
STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
1755-
DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label);
1756-
emit_native_jump_helper(emit, true);
1757-
ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1758-
emit_post(emit);
1759-
}
1760-
1761-
STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
1762-
DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
1763-
emit_native_jump_helper(emit, false);
1764-
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1765-
adjust_stack(emit, -1);
1750+
if (cond) {
1751+
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1752+
} else {
1753+
ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1754+
}
17661755
emit_post(emit);
17671756
}
17681757

1769-
STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
1770-
DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
1758+
STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1759+
DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
17711760
emit_native_jump_helper(emit, false);
1772-
ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1761+
if (cond) {
1762+
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1763+
} else {
1764+
ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1765+
}
17731766
adjust_stack(emit, -1);
17741767
emit_post(emit);
17751768
}
@@ -2329,10 +2322,8 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
23292322
emit_native_rot_two,
23302323
emit_native_rot_three,
23312324
emit_native_jump,
2332-
emit_native_pop_jump_if_true,
2333-
emit_native_pop_jump_if_false,
2334-
emit_native_jump_if_true_or_pop,
2335-
emit_native_jump_if_false_or_pop,
2325+
emit_native_pop_jump_if,
2326+
emit_native_jump_if_or_pop,
23362327
emit_native_break_loop,
23372328
emit_native_continue_loop,
23382329
emit_native_setup_with,

py/emitpass1.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ const emit_method_table_t emit_pass1_method_table = {
190190
(void*)emit_pass1_dummy,
191191
(void*)emit_pass1_dummy,
192192
(void*)emit_pass1_dummy,
193-
(void*)emit_pass1_dummy,
194-
(void*)emit_pass1_dummy,
195193
#if MICROPY_PY_BUILTINS_SET
196194
(void*)emit_pass1_dummy,
197195
(void*)emit_pass1_dummy,

0 commit comments

Comments
 (0)