From d66ae1864031d7a369c2030b79d4ac649be5c0db Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 10 Apr 2014 17:28:54 +0000 Subject: [PATCH] py: Simplify stack get/set to become stack adjust in emitters. Can do this now that the stack size calculation is improved. --- py/compile.c | 16 ++++++++-------- py/emit.h | 3 +-- py/emitbc.c | 11 +++-------- py/emitcpy.c | 11 +++-------- py/emitnative.c | 11 +++-------- py/emitpass1.c | 5 ----- 6 files changed, 18 insertions(+), 39 deletions(-) diff --git a/py/compile.c b/py/compile.c index ebc144b341..ca3ee9d6d1 100644 --- a/py/compile.c +++ b/py/compile.c @@ -1595,7 +1595,7 @@ void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, EMIT_ARG(label_assign, top_label); // at this point we actually have 1 less element on the stack - EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1); + EMIT_ARG(adjust_stack_size, -1); // store next value to var c_assign(comp, pn_var, ASSIGN_STORE); @@ -1728,7 +1728,7 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except, EMIT_ARG(jump, success_label); // jump over exception handler EMIT_ARG(label_assign, l1); // start of exception handler - EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state + EMIT_ARG(adjust_stack_size, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state uint l2 = comp_next_label(comp); @@ -1795,12 +1795,12 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except, } EMIT_ARG(jump, l2); EMIT_ARG(label_assign, end_finally_label); - EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 3); // stack adjust for the 3 exception items + EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items } compile_decrease_except_level(comp); EMIT(end_finally); - EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 5); // stack adjust + EMIT_ARG(adjust_stack_size, -5); // stack adjust EMIT_ARG(label_assign, success_label); compile_node(comp, pn_else); // else block, can be null @@ -1815,9 +1815,9 @@ void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except if (n_except == 0) { assert(MP_PARSE_NODE_IS_NULL(pn_else)); - EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 3); // stack adjust for possible UNWIND_JUMP state + EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state compile_node(comp, pn_body); - EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 3); + EMIT_ARG(adjust_stack_size, -3); } else { compile_try_except(comp, pn_body, n_except, pn_except, pn_else); } @@ -2027,7 +2027,7 @@ void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) { compile_node(comp, pns->nodes[0]); // success value EMIT_ARG(jump, l_end); EMIT_ARG(label_assign, l_fail); - EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1); // adjust stack size + EMIT_ARG(adjust_stack_size, -1); // adjust stack size compile_node(comp, pns_test_if_else->nodes[1]); // failure value EMIT_ARG(label_assign, l_end); } @@ -2134,7 +2134,7 @@ void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) { uint l_end = comp_next_label(comp); EMIT_ARG(jump, l_end); EMIT_ARG(label_assign, l_fail); - EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 1); + EMIT_ARG(adjust_stack_size, 1); EMIT(rot_two); EMIT(pop_top); EMIT_ARG(label_assign, l_end); diff --git a/py/emit.h b/py/emit.h index 038b7f8444..738150c656 100644 --- a/py/emit.h +++ b/py/emit.h @@ -24,8 +24,7 @@ typedef struct _emit_method_table_t { void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope); void (*end_pass)(emit_t *emit); bool (*last_emit_was_return_value)(emit_t *emit); - int (*get_stack_size)(emit_t *emit); - void (*set_stack_size)(emit_t *emit, int size); + void (*adjust_stack_size)(emit_t *emit, int delta); void (*set_line_number)(emit_t *emit, int line); void (*load_id)(emit_t *emit, qstr qstr); diff --git a/py/emitbc.c b/py/emitbc.c index b8a47ec45e..fc3e5ed622 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -292,12 +292,8 @@ STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) { return emit->last_emit_was_return_value; } -STATIC int emit_bc_get_stack_size(emit_t *emit) { - return emit->stack_size; -} - -STATIC void emit_bc_set_stack_size(emit_t *emit, int size) { - emit->stack_size = size; +STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) { + emit->stack_size += delta; } STATIC void emit_bc_set_source_line(emit_t *emit, int source_line) { @@ -836,8 +832,7 @@ const emit_method_table_t emit_bc_method_table = { emit_bc_start_pass, emit_bc_end_pass, emit_bc_last_emit_was_return_value, - emit_bc_get_stack_size, - emit_bc_set_stack_size, + emit_bc_adjust_stack_size, emit_bc_set_source_line, emit_bc_load_id, diff --git a/py/emitcpy.c b/py/emitcpy.c index 6928e031bd..5866d474e9 100644 --- a/py/emitcpy.c +++ b/py/emitcpy.c @@ -60,12 +60,8 @@ STATIC bool emit_cpy_last_emit_was_return_value(emit_t *emit) { return emit->last_emit_was_return_value; } -STATIC int emit_cpy_get_stack_size(emit_t *emit) { - return emit->stack_size; -} - -STATIC void emit_cpy_set_stack_size(emit_t *emit, int size) { - emit->stack_size = size; +STATIC void emit_cpy_adjust_stack_size(emit_t *emit, int delta) { + emit->stack_size += delta; } STATIC void emit_cpy_set_source_line(emit_t *emit, int source_line) { @@ -793,8 +789,7 @@ const emit_method_table_t emit_cpython_method_table = { emit_cpy_start_pass, emit_cpy_end_pass, emit_cpy_last_emit_was_return_value, - emit_cpy_get_stack_size, - emit_cpy_set_stack_size, + emit_cpy_adjust_stack_size, emit_cpy_set_source_line, emit_cpy_load_id, diff --git a/py/emitnative.c b/py/emitnative.c index 7f80702fbf..d2a3df25dd 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -295,12 +295,8 @@ STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) { return emit->last_emit_was_return_value; } -STATIC int emit_native_get_stack_size(emit_t *emit) { - return emit->stack_size; -} - -STATIC void emit_native_set_stack_size(emit_t *emit, int size) { - emit->stack_size = size; +STATIC void emit_native_adjust_stack_size(emit_t *emit, int delta) { + emit->stack_size += delta; } STATIC void emit_native_set_source_line(emit_t *emit, int source_line) { @@ -1304,8 +1300,7 @@ const emit_method_table_t EXPORT_FUN(method_table) = { emit_native_start_pass, emit_native_end_pass, emit_native_last_emit_was_return_value, - emit_native_get_stack_size, - emit_native_set_stack_size, + emit_native_adjust_stack_size, emit_native_set_source_line, emit_native_load_id, diff --git a/py/emitpass1.c b/py/emitpass1.c index e529e71b99..301c04ebea 100644 --- a/py/emitpass1.c +++ b/py/emitpass1.c @@ -39,10 +39,6 @@ STATIC bool emit_pass1_last_emit_was_return_value(emit_t *emit) { return false; } -STATIC int emit_pass1_get_stack_size(emit_t *emit) { - return 0; -} - STATIC void emit_pass1_load_id(emit_t *emit, qstr qstr) { // name adding/lookup bool added; @@ -108,7 +104,6 @@ const emit_method_table_t emit_pass1_method_table = { emit_pass1_start_pass, emit_pass1_end_pass, emit_pass1_last_emit_was_return_value, - emit_pass1_get_stack_size, (void*)emit_pass1_dummy, (void*)emit_pass1_dummy,