From 5f6a25fc50503b70af1f3a9a27f13c2a698d0a0e Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 20 Apr 2014 18:02:27 +0100 Subject: [PATCH] py: Wrap #if's around emitter functions that are used only by emitcpy. 3 emitter functions are needed only for emitcpy, and so we can #if them out when compiling with emitcpy support. Also remove unused SETUP_LOOP bytecode. --- py/bc0.h | 1 - py/emit.h | 11 ++++++++--- py/emitbc.c | 22 ++++----------------- py/emitcpy.c | 50 ++++++++++++++++++++++++----------------------- py/emitnative.c | 18 ----------------- py/emitpass1.c | 2 ++ py/showbc.c | 5 ----- py/vm.c | 7 ------- py/vmentrytable.h | 1 - 9 files changed, 40 insertions(+), 77 deletions(-) diff --git a/py/bc0.h b/py/bc0.h index 57c2d2ea64..37bd904147 100644 --- a/py/bc0.h +++ b/py/bc0.h @@ -51,7 +51,6 @@ #define MP_BC_POP_JUMP_IF_FALSE (0x47) // rel byte code offset, 16-bit signed, in excess #define MP_BC_JUMP_IF_TRUE_OR_POP (0x48) // rel byte code offset, 16-bit signed, in excess #define MP_BC_JUMP_IF_FALSE_OR_POP (0x49) // rel byte code offset, 16-bit signed, in excess -#define MP_BC_SETUP_LOOP (0x4a) // rel byte code offset, 16-bit unsigned #define MP_BC_SETUP_WITH (0x4d) // rel byte code offset, 16-bit unsigned #define MP_BC_WITH_CLEANUP (0x4e) #define MP_BC_SETUP_EXCEPT (0x4f) // rel byte code offset, 16-bit unsigned diff --git a/py/emit.h b/py/emit.h index 64c2c85480..5fca153163 100644 --- a/py/emit.h +++ b/py/emit.h @@ -41,11 +41,9 @@ typedef struct _emit_method_table_t { void (*load_const_dec)(emit_t *emit, qstr qstr); void (*load_const_id)(emit_t *emit, qstr qstr); void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes); - void (*load_const_verbatim_str)(emit_t *emit, const char *str); // only needed for emitcpy void (*load_null)(emit_t *emit); void (*load_fast)(emit_t *emit, qstr qstr, uint id_flags, int local_num); void (*load_deref)(emit_t *emit, qstr qstr, int local_num); - void (*load_closure)(emit_t *emit, qstr qstr, int local_num); // only needed for emitcpy void (*load_name)(emit_t *emit, qstr qstr); void (*load_global)(emit_t *emit, qstr qstr); void (*load_attr)(emit_t *emit, qstr qstr); @@ -74,7 +72,6 @@ typedef struct _emit_method_table_t { void (*pop_jump_if_false)(emit_t *emit, uint label); void (*jump_if_true_or_pop)(emit_t *emit, uint label); void (*jump_if_false_or_pop)(emit_t *emit, uint label); - void (*setup_loop)(emit_t *emit, uint label); void (*break_loop)(emit_t *emit, uint label, int except_depth); void (*continue_loop)(emit_t *emit, uint label, int except_depth); void (*setup_with)(emit_t *emit, uint label); @@ -108,6 +105,14 @@ typedef struct _emit_method_table_t { void (*raise_varargs)(emit_t *emit, int n_args); void (*yield_value)(emit_t *emit); void (*yield_from)(emit_t *emit); + +#if MICROPY_EMIT_CPYTHON + // these methods are only needed for emitcpy + void (*load_const_verbatim_str)(emit_t *emit, const char *str); + void (*load_closure)(emit_t *emit, qstr qstr, int local_num); + void (*setup_loop)(emit_t *emit, uint label); +#endif + } emit_method_table_t; void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr); diff --git a/py/emitbc.c b/py/emitbc.c index c445d9748d..f58cec1f1f 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -16,6 +16,8 @@ #include "emit.h" #include "bc0.h" +#if !MICROPY_EMIT_CPYTHON + struct _emit_t { pass_kind_t pass; int stack_size; @@ -421,11 +423,6 @@ STATIC void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) { } } -STATIC void emit_bc_load_const_verbatim_str(emit_t *emit, const char *str) { - // not needed/supported for BC - assert(0); -} - STATIC void emit_bc_load_null(emit_t *emit) { emit_bc_pre(emit, 1); emit_write_byte_code_byte(emit, MP_BC_LOAD_NULL); @@ -447,11 +444,6 @@ STATIC void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) { emit_write_byte_code_byte_uint(emit, MP_BC_LOAD_DEREF, local_num); } -STATIC void emit_bc_load_closure(emit_t *emit, qstr qstr, int local_num) { - // not needed/supported for BC - assert(0); -} - STATIC void emit_bc_load_name(emit_t *emit, qstr qstr) { emit_bc_pre(emit, 1); emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_NAME, qstr); @@ -598,11 +590,6 @@ STATIC void emit_bc_jump_if_false_or_pop(emit_t *emit, uint label) { emit_write_byte_code_byte_signed_label(emit, MP_BC_JUMP_IF_FALSE_OR_POP, label); } -STATIC void emit_bc_setup_loop(emit_t *emit, uint label) { - emit_bc_pre(emit, 0); - emit_write_byte_code_byte_unsigned_label(emit, MP_BC_SETUP_LOOP, label); -} - STATIC void emit_bc_unwind_jump(emit_t *emit, uint label, int except_depth) { if (except_depth == 0) { emit_bc_jump(emit, label); @@ -855,11 +842,9 @@ const emit_method_table_t emit_bc_method_table = { emit_bc_load_const_dec, emit_bc_load_const_id, emit_bc_load_const_str, - emit_bc_load_const_verbatim_str, emit_bc_load_null, emit_bc_load_fast, emit_bc_load_deref, - emit_bc_load_closure, emit_bc_load_name, emit_bc_load_global, emit_bc_load_attr, @@ -888,7 +873,6 @@ const emit_method_table_t emit_bc_method_table = { emit_bc_pop_jump_if_false, emit_bc_jump_if_true_or_pop, emit_bc_jump_if_false_or_pop, - emit_bc_setup_loop, emit_bc_unwind_jump, emit_bc_unwind_jump, emit_bc_setup_with, @@ -923,3 +907,5 @@ const emit_method_table_t emit_bc_method_table = { emit_bc_yield_value, emit_bc_yield_from, }; + +#endif // !MICROPY_EMIT_CPYTHON diff --git a/py/emitcpy.c b/py/emitcpy.c index e90b5c428a..a041c4f1e0 100644 --- a/py/emitcpy.c +++ b/py/emitcpy.c @@ -221,13 +221,6 @@ STATIC void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) { } } -STATIC void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) { - emit_pre(emit, 1, 3); - if (emit->pass == PASS_3) { - printf("LOAD_CONST %s\n", str); - } -} - STATIC void emit_cpy_load_null(emit_t *emit) { // unused for cpy assert(0); @@ -247,13 +240,6 @@ STATIC void emit_cpy_load_deref(emit_t *emit, qstr qstr, int local_num) { } } -STATIC void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) { - emit_pre(emit, 1, 3); - if (emit->pass == PASS_3) { - printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr)); - } -} - STATIC void emit_cpy_load_name(emit_t *emit, qstr qstr) { emit_pre(emit, 1, 3); if (emit->pass == PASS_3) { @@ -452,13 +438,6 @@ STATIC void emit_cpy_jump_if_false_or_pop(emit_t *emit, uint label) { } } -STATIC void emit_cpy_setup_loop(emit_t *emit, uint label) { - emit_pre(emit, 0, 3); - if (emit->pass == PASS_3) { - printf("SETUP_LOOP %d\n", emit->label_offsets[label]); - } -} - STATIC void emit_cpy_break_loop(emit_t *emit, uint label, int except_depth) { emit_pre(emit, 0, 1); if (emit->pass == PASS_3) { @@ -798,6 +777,27 @@ STATIC void emit_cpy_yield_from(emit_t *emit) { } } +STATIC void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) { + emit_pre(emit, 1, 3); + if (emit->pass == PASS_3) { + printf("LOAD_CONST %s\n", str); + } +} + +STATIC void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) { + emit_pre(emit, 1, 3); + if (emit->pass == PASS_3) { + printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr)); + } +} + +STATIC void emit_cpy_setup_loop(emit_t *emit, uint label) { + emit_pre(emit, 0, 3); + if (emit->pass == PASS_3) { + printf("SETUP_LOOP %d\n", emit->label_offsets[label]); + } +} + const emit_method_table_t emit_cpython_method_table = { emit_cpy_set_native_types, emit_cpy_start_pass, @@ -820,11 +820,9 @@ const emit_method_table_t emit_cpython_method_table = { emit_cpy_load_const_dec, emit_cpy_load_const_id, emit_cpy_load_const_str, - emit_cpy_load_const_verbatim_str, emit_cpy_load_null, emit_cpy_load_fast, emit_cpy_load_deref, - emit_cpy_load_closure, emit_cpy_load_name, emit_cpy_load_global, emit_cpy_load_attr, @@ -853,7 +851,6 @@ const emit_method_table_t emit_cpython_method_table = { emit_cpy_pop_jump_if_false, emit_cpy_jump_if_true_or_pop, emit_cpy_jump_if_false_or_pop, - emit_cpy_setup_loop, emit_cpy_break_loop, emit_cpy_continue_loop, emit_cpy_setup_with, @@ -887,6 +884,11 @@ const emit_method_table_t emit_cpython_method_table = { emit_cpy_raise_varargs, emit_cpy_yield_value, emit_cpy_yield_from, + + // emitcpy specific functions + emit_cpy_load_const_verbatim_str, + emit_cpy_load_closure, + emit_cpy_setup_loop, }; #endif // MICROPY_EMIT_CPYTHON diff --git a/py/emitnative.c b/py/emitnative.c index dc6e37c53f..79d6da67c1 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -695,11 +695,6 @@ STATIC void emit_native_load_const_str(emit_t *emit, qstr qstr, bool bytes) { } } -STATIC void emit_native_load_const_verbatim_str(emit_t *emit, const char *str) { - // not supported/needed for viper - assert(0); -} - STATIC void emit_native_load_null(emit_t *emit) { emit_native_pre(emit); emit_post_push_imm(emit, VTYPE_PYOBJ, 0); @@ -740,11 +735,6 @@ STATIC void emit_native_load_deref(emit_t *emit, qstr qstr, int local_num) { assert(0); } -STATIC void emit_native_load_closure(emit_t *emit, qstr qstr, int local_num) { - // not implemented - assert(0); -} - STATIC void emit_native_load_name(emit_t *emit, qstr qstr) { emit_native_pre(emit); emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, mp_load_name, qstr, REG_ARG_1); @@ -990,11 +980,6 @@ STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, uint label) { assert(0); } -STATIC void emit_native_setup_loop(emit_t *emit, uint label) { - emit_native_pre(emit); - emit_post(emit); -} - STATIC void emit_native_break_loop(emit_t *emit, uint label, int except_depth) { emit_native_jump(emit, label); // TODO properly } @@ -1339,11 +1324,9 @@ const emit_method_table_t EXPORT_FUN(method_table) = { emit_native_load_const_dec, emit_native_load_const_id, emit_native_load_const_str, - emit_native_load_const_verbatim_str, emit_native_load_null, emit_native_load_fast, emit_native_load_deref, - emit_native_load_closure, emit_native_load_name, emit_native_load_global, emit_native_load_attr, @@ -1372,7 +1355,6 @@ const emit_method_table_t EXPORT_FUN(method_table) = { emit_native_pop_jump_if_false, emit_native_jump_if_true_or_pop, emit_native_jump_if_false_or_pop, - emit_native_setup_loop, emit_native_break_loop, emit_native_continue_loop, emit_native_setup_with, diff --git a/py/emitpass1.c b/py/emitpass1.c index bccdb4cc87..64b58c4757 100644 --- a/py/emitpass1.c +++ b/py/emitpass1.c @@ -189,7 +189,9 @@ const emit_method_table_t emit_pass1_method_table = { (void*)emit_pass1_dummy, (void*)emit_pass1_dummy, (void*)emit_pass1_dummy, +#if MICROPY_EMIT_CPYTHON (void*)emit_pass1_dummy, (void*)emit_pass1_dummy, (void*)emit_pass1_dummy, +#endif }; diff --git a/py/showbc.c b/py/showbc.c index bf25966e97..4a8e12e68b 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -287,11 +287,6 @@ void mp_byte_code_print(const byte *ip, int len) { printf("JUMP_IF_FALSE_OR_POP " UINT_FMT, ip + unum - ip_start); break; - case MP_BC_SETUP_LOOP: - DECODE_ULABEL; // loop labels are always forward - printf("SETUP_LOOP " UINT_FMT, ip + unum - ip_start); - break; - case MP_BC_SETUP_WITH: DECODE_ULABEL; // loop-like labels are always forward printf("SETUP_WITH " UINT_FMT, ip + unum - ip_start); diff --git a/py/vm.c b/py/vm.c index 78a96a48cc..b1c1719b72 100644 --- a/py/vm.c +++ b/py/vm.c @@ -481,13 +481,6 @@ dispatch_loop: } DISPATCH(); - /* we are trying to get away without using this opcode - ENTRY(MP_BC_SETUP_LOOP): - DECODE_UINT; - // push_block(MP_BC_SETUP_LOOP, ip + unum, sp) - DISPATCH(); - */ - ENTRY(MP_BC_SETUP_WITH): obj1 = TOP(); SET_TOP(mp_load_attr(obj1, MP_QSTR___exit__)); diff --git a/py/vmentrytable.h b/py/vmentrytable.h index 2e1d1fb04f..c4d15427d7 100644 --- a/py/vmentrytable.h +++ b/py/vmentrytable.h @@ -45,7 +45,6 @@ static void* entry_table[256] = { [MP_BC_POP_JUMP_IF_FALSE] = &&entry_MP_BC_POP_JUMP_IF_FALSE, [MP_BC_JUMP_IF_TRUE_OR_POP] = &&entry_MP_BC_JUMP_IF_TRUE_OR_POP, [MP_BC_JUMP_IF_FALSE_OR_POP] = &&entry_MP_BC_JUMP_IF_FALSE_OR_POP, -// [MP_BC_SETUP_LOOP] = &&entry_MP_BC_SETUP_LOOP, [MP_BC_SETUP_WITH] = &&entry_MP_BC_SETUP_WITH, [MP_BC_WITH_CLEANUP] = &&entry_MP_BC_WITH_CLEANUP, [MP_BC_UNWIND_JUMP] = &&entry_MP_BC_UNWIND_JUMP,