py: Combine emit functions for jump true/false to reduce code size.

Saves 116 bytes for stmhal and 56 bytes for cc3200 port.
This commit is contained in:
Damien George 2015-02-28 15:04:06 +00:00
parent 0b2fd91890
commit 63f3832e81
6 changed files with 53 additions and 86 deletions

View File

@ -639,11 +639,7 @@ STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if
// nothing special, fall back to default compiling for node and jump // nothing special, fall back to default compiling for node and jump
compile_node(comp, pn); compile_node(comp, pn);
if (jump_if == false) { EMIT_ARG(pop_jump_if, jump_if, label);
EMIT_ARG(pop_jump_if_false, label);
} else {
EMIT_ARG(pop_jump_if_true, label);
}
} }
#endif #endif
@ -711,11 +707,7 @@ STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int la
// nothing special, fall back to default compiling for node and jump // nothing special, fall back to default compiling for node and jump
compile_node(comp, pn); compile_node(comp, pn);
if (jump_if == false) { EMIT_ARG(pop_jump_if, jump_if, label);
EMIT_ARG(pop_jump_if_false, label);
} else {
EMIT_ARG(pop_jump_if_true, label);
}
#endif #endif
} }
@ -1825,7 +1817,7 @@ STATIC void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t p
} else { } else {
EMIT_ARG(binary_op, MP_BINARY_OP_MORE); EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
} }
EMIT_ARG(pop_jump_if_true, top_label); EMIT_ARG(pop_jump_if, true, top_label);
// break/continue apply to outer loop (if any) in the else block // break/continue apply to outer loop (if any) in the else block
END_BREAK_CONTINUE_BLOCK END_BREAK_CONTINUE_BLOCK
@ -1971,7 +1963,7 @@ STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_
EMIT(dup_top); EMIT(dup_top);
compile_node(comp, pns_exception_expr); compile_node(comp, pns_exception_expr);
EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH); EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
EMIT_ARG(pop_jump_if_false, end_finally_label); EMIT_ARG(pop_jump_if, false, end_finally_label);
} }
EMIT(pop_top); EMIT(pop_top);
@ -2267,7 +2259,7 @@ STATIC void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
for (int i = 0; i < n; i += 1) { for (int i = 0; i < n; i += 1) {
compile_node(comp, pns->nodes[i]); compile_node(comp, pns->nodes[i]);
if (i + 1 < n) { if (i + 1 < n) {
EMIT_ARG(jump_if_true_or_pop, l_end); EMIT_ARG(jump_if_or_pop, true, l_end);
} }
} }
EMIT_ARG(label_assign, l_end); EMIT_ARG(label_assign, l_end);
@ -2279,7 +2271,7 @@ STATIC void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
for (int i = 0; i < n; i += 1) { for (int i = 0; i < n; i += 1) {
compile_node(comp, pns->nodes[i]); compile_node(comp, pns->nodes[i]);
if (i + 1 < n) { if (i + 1 < n) {
EMIT_ARG(jump_if_false_or_pop, l_end); EMIT_ARG(jump_if_or_pop, false, l_end);
} }
} }
EMIT_ARG(label_assign, l_end); EMIT_ARG(label_assign, l_end);
@ -2332,7 +2324,7 @@ STATIC void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
} }
} }
if (i + 2 < num_nodes) { if (i + 2 < num_nodes) {
EMIT_ARG(jump_if_false_or_pop, l_fail); EMIT_ARG(jump_if_or_pop, false, l_fail);
} }
} }
if (multi) { if (multi) {

View File

@ -106,10 +106,8 @@ typedef struct _emit_method_table_t {
void (*rot_two)(emit_t *emit); void (*rot_two)(emit_t *emit);
void (*rot_three)(emit_t *emit); void (*rot_three)(emit_t *emit);
void (*jump)(emit_t *emit, mp_uint_t label); void (*jump)(emit_t *emit, mp_uint_t label);
void (*pop_jump_if_true)(emit_t *emit, mp_uint_t label); void (*pop_jump_if)(emit_t *emit, bool cond, mp_uint_t label);
void (*pop_jump_if_false)(emit_t *emit, mp_uint_t label); void (*jump_if_or_pop)(emit_t *emit, bool cond, mp_uint_t label);
void (*jump_if_true_or_pop)(emit_t *emit, mp_uint_t label);
void (*jump_if_false_or_pop)(emit_t *emit, mp_uint_t label);
void (*break_loop)(emit_t *emit, mp_uint_t label, mp_uint_t except_depth); void (*break_loop)(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
void (*continue_loop)(emit_t *emit, mp_uint_t label, mp_uint_t except_depth); void (*continue_loop)(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
void (*setup_with)(emit_t *emit, mp_uint_t label); void (*setup_with)(emit_t *emit, mp_uint_t label);

View File

@ -652,24 +652,22 @@ STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) {
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label); emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
} }
STATIC void emit_bc_pop_jump_if_true(emit_t *emit, mp_uint_t label) { STATIC void emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
emit_bc_pre(emit, -1); emit_bc_pre(emit, -1);
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label); if (cond) {
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
} else {
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label);
}
} }
STATIC void emit_bc_pop_jump_if_false(emit_t *emit, mp_uint_t label) { STATIC void emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
emit_bc_pre(emit, -1); emit_bc_pre(emit, -1);
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_FALSE, label); if (cond) {
} emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
} else {
STATIC void emit_bc_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) { emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
emit_bc_pre(emit, -1); }
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
}
STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label);
} }
STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) { 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 = {
emit_bc_rot_two, emit_bc_rot_two,
emit_bc_rot_three, emit_bc_rot_three,
emit_bc_jump, emit_bc_jump,
emit_bc_pop_jump_if_true, emit_bc_pop_jump_if,
emit_bc_pop_jump_if_false, emit_bc_jump_if_or_pop,
emit_bc_jump_if_true_or_pop,
emit_bc_jump_if_false_or_pop,
emit_bc_unwind_jump, emit_bc_unwind_jump,
emit_bc_unwind_jump, emit_bc_unwind_jump,
emit_bc_setup_with, emit_bc_setup_with,

View File

@ -415,31 +415,25 @@ STATIC void emit_cpy_jump(emit_t *emit, mp_uint_t label) {
} }
} }
STATIC void emit_cpy_pop_jump_if_true(emit_t *emit, mp_uint_t label) { STATIC void emit_cpy_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
emit_pre(emit, -1, 3); emit_pre(emit, -1, 3);
if (emit->pass == MP_PASS_EMIT) { if (emit->pass == MP_PASS_EMIT) {
printf("POP_JUMP_IF_TRUE " UINT_FMT "\n", emit->label_offsets[label]); if (cond) {
printf("POP_JUMP_IF_TRUE " UINT_FMT "\n", emit->label_offsets[label]);
} else {
printf("POP_JUMP_IF_FALSE " UINT_FMT "\n", emit->label_offsets[label]);
}
} }
} }
STATIC void emit_cpy_pop_jump_if_false(emit_t *emit, mp_uint_t label) { STATIC void emit_cpy_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
emit_pre(emit, -1, 3); emit_pre(emit, -1, 3);
if (emit->pass == MP_PASS_EMIT) { if (emit->pass == MP_PASS_EMIT) {
printf("POP_JUMP_IF_FALSE " UINT_FMT "\n", emit->label_offsets[label]); if (cond) {
} printf("JUMP_IF_TRUE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
} } else {
printf("JUMP_IF_FALSE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
STATIC void emit_cpy_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) { }
emit_pre(emit, -1, 3);
if (emit->pass == MP_PASS_EMIT) {
printf("JUMP_IF_TRUE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
}
}
STATIC void emit_cpy_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
emit_pre(emit, -1, 3);
if (emit->pass == MP_PASS_EMIT) {
printf("JUMP_IF_FALSE_OR_POP " UINT_FMT "\n", emit->label_offsets[label]);
} }
} }
@ -854,10 +848,8 @@ const emit_method_table_t emit_cpython_method_table = {
emit_cpy_rot_two, emit_cpy_rot_two,
emit_cpy_rot_three, emit_cpy_rot_three,
emit_cpy_jump, emit_cpy_jump,
emit_cpy_pop_jump_if_true, emit_cpy_pop_jump_if,
emit_cpy_pop_jump_if_false, emit_cpy_jump_if_or_pop,
emit_cpy_jump_if_true_or_pop,
emit_cpy_jump_if_false_or_pop,
emit_cpy_break_loop, emit_cpy_break_loop,
emit_cpy_continue_loop, emit_cpy_continue_loop,
emit_cpy_setup_with, emit_cpy_setup_with,

View File

@ -1744,32 +1744,25 @@ STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
need_stack_settled(emit); need_stack_settled(emit);
} }
STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) { STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label); DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
emit_native_jump_helper(emit, true); emit_native_jump_helper(emit, true);
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label); if (cond) {
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
} else {
ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
}
emit_post(emit); emit_post(emit);
} }
STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) { STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label); DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
emit_native_jump_helper(emit, true);
ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
emit_post(emit);
}
STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
emit_native_jump_helper(emit, false); emit_native_jump_helper(emit, false);
ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label); if (cond) {
adjust_stack(emit, -1); ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
emit_post(emit); } else {
} ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
}
STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
emit_native_jump_helper(emit, false);
ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
adjust_stack(emit, -1); adjust_stack(emit, -1);
emit_post(emit); emit_post(emit);
} }
@ -2329,10 +2322,8 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
emit_native_rot_two, emit_native_rot_two,
emit_native_rot_three, emit_native_rot_three,
emit_native_jump, emit_native_jump,
emit_native_pop_jump_if_true, emit_native_pop_jump_if,
emit_native_pop_jump_if_false, emit_native_jump_if_or_pop,
emit_native_jump_if_true_or_pop,
emit_native_jump_if_false_or_pop,
emit_native_break_loop, emit_native_break_loop,
emit_native_continue_loop, emit_native_continue_loop,
emit_native_setup_with, emit_native_setup_with,

View File

@ -190,8 +190,6 @@ const emit_method_table_t emit_pass1_method_table = {
(void*)emit_pass1_dummy, (void*)emit_pass1_dummy,
(void*)emit_pass1_dummy, (void*)emit_pass1_dummy,
(void*)emit_pass1_dummy, (void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
#if MICROPY_PY_BUILTINS_SET #if MICROPY_PY_BUILTINS_SET
(void*)emit_pass1_dummy, (void*)emit_pass1_dummy,
(void*)emit_pass1_dummy, (void*)emit_pass1_dummy,