Skip to content

Commit d66ae18

Browse files
committed
py: Simplify stack get/set to become stack adjust in emitters.
Can do this now that the stack size calculation is improved.
1 parent 069a35e commit d66ae18

6 files changed

Lines changed: 18 additions & 39 deletions

File tree

py/compile.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var,
15951595
EMIT_ARG(label_assign, top_label);
15961596

15971597
// at this point we actually have 1 less element on the stack
1598-
EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1);
1598+
EMIT_ARG(adjust_stack_size, -1);
15991599

16001600
// store next value to var
16011601
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,
17281728
EMIT_ARG(jump, success_label); // jump over exception handler
17291729

17301730
EMIT_ARG(label_assign, l1); // start of exception handler
1731-
EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
1731+
EMIT_ARG(adjust_stack_size, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
17321732

17331733
uint l2 = comp_next_label(comp);
17341734

@@ -1795,12 +1795,12 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except,
17951795
}
17961796
EMIT_ARG(jump, l2);
17971797
EMIT_ARG(label_assign, end_finally_label);
1798-
EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 3); // stack adjust for the 3 exception items
1798+
EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
17991799
}
18001800

18011801
compile_decrease_except_level(comp);
18021802
EMIT(end_finally);
1803-
EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 5); // stack adjust
1803+
EMIT_ARG(adjust_stack_size, -5); // stack adjust
18041804

18051805
EMIT_ARG(label_assign, success_label);
18061806
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
18151815

18161816
if (n_except == 0) {
18171817
assert(MP_PARSE_NODE_IS_NULL(pn_else));
1818-
EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 3); // stack adjust for possible UNWIND_JUMP state
1818+
EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
18191819
compile_node(comp, pn_body);
1820-
EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 3);
1820+
EMIT_ARG(adjust_stack_size, -3);
18211821
} else {
18221822
compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
18231823
}
@@ -2027,7 +2027,7 @@ void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
20272027
compile_node(comp, pns->nodes[0]); // success value
20282028
EMIT_ARG(jump, l_end);
20292029
EMIT_ARG(label_assign, l_fail);
2030-
EMIT_ARG(set_stack_size, EMIT(get_stack_size) - 1); // adjust stack size
2030+
EMIT_ARG(adjust_stack_size, -1); // adjust stack size
20312031
compile_node(comp, pns_test_if_else->nodes[1]); // failure value
20322032
EMIT_ARG(label_assign, l_end);
20332033
}
@@ -2134,7 +2134,7 @@ void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
21342134
uint l_end = comp_next_label(comp);
21352135
EMIT_ARG(jump, l_end);
21362136
EMIT_ARG(label_assign, l_fail);
2137-
EMIT_ARG(set_stack_size, EMIT(get_stack_size) + 1);
2137+
EMIT_ARG(adjust_stack_size, 1);
21382138
EMIT(rot_two);
21392139
EMIT(pop_top);
21402140
EMIT_ARG(label_assign, l_end);

py/emit.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ typedef struct _emit_method_table_t {
2424
void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
2525
void (*end_pass)(emit_t *emit);
2626
bool (*last_emit_was_return_value)(emit_t *emit);
27-
int (*get_stack_size)(emit_t *emit);
28-
void (*set_stack_size)(emit_t *emit, int size);
27+
void (*adjust_stack_size)(emit_t *emit, int delta);
2928
void (*set_line_number)(emit_t *emit, int line);
3029

3130
void (*load_id)(emit_t *emit, qstr qstr);

py/emitbc.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,8 @@ STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
292292
return emit->last_emit_was_return_value;
293293
}
294294

295-
STATIC int emit_bc_get_stack_size(emit_t *emit) {
296-
return emit->stack_size;
297-
}
298-
299-
STATIC void emit_bc_set_stack_size(emit_t *emit, int size) {
300-
emit->stack_size = size;
295+
STATIC void emit_bc_adjust_stack_size(emit_t *emit, int delta) {
296+
emit->stack_size += delta;
301297
}
302298

303299
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 = {
836832
emit_bc_start_pass,
837833
emit_bc_end_pass,
838834
emit_bc_last_emit_was_return_value,
839-
emit_bc_get_stack_size,
840-
emit_bc_set_stack_size,
835+
emit_bc_adjust_stack_size,
841836
emit_bc_set_source_line,
842837

843838
emit_bc_load_id,

py/emitcpy.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,8 @@ STATIC bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
6060
return emit->last_emit_was_return_value;
6161
}
6262

63-
STATIC int emit_cpy_get_stack_size(emit_t *emit) {
64-
return emit->stack_size;
65-
}
66-
67-
STATIC void emit_cpy_set_stack_size(emit_t *emit, int size) {
68-
emit->stack_size = size;
63+
STATIC void emit_cpy_adjust_stack_size(emit_t *emit, int delta) {
64+
emit->stack_size += delta;
6965
}
7066

7167
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 = {
793789
emit_cpy_start_pass,
794790
emit_cpy_end_pass,
795791
emit_cpy_last_emit_was_return_value,
796-
emit_cpy_get_stack_size,
797-
emit_cpy_set_stack_size,
792+
emit_cpy_adjust_stack_size,
798793
emit_cpy_set_source_line,
799794

800795
emit_cpy_load_id,

py/emitnative.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,8 @@ STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
295295
return emit->last_emit_was_return_value;
296296
}
297297

298-
STATIC int emit_native_get_stack_size(emit_t *emit) {
299-
return emit->stack_size;
300-
}
301-
302-
STATIC void emit_native_set_stack_size(emit_t *emit, int size) {
303-
emit->stack_size = size;
298+
STATIC void emit_native_adjust_stack_size(emit_t *emit, int delta) {
299+
emit->stack_size += delta;
304300
}
305301

306302
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) = {
13041300
emit_native_start_pass,
13051301
emit_native_end_pass,
13061302
emit_native_last_emit_was_return_value,
1307-
emit_native_get_stack_size,
1308-
emit_native_set_stack_size,
1303+
emit_native_adjust_stack_size,
13091304
emit_native_set_source_line,
13101305

13111306
emit_native_load_id,

py/emitpass1.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ STATIC bool emit_pass1_last_emit_was_return_value(emit_t *emit) {
3939
return false;
4040
}
4141

42-
STATIC int emit_pass1_get_stack_size(emit_t *emit) {
43-
return 0;
44-
}
45-
4642
STATIC void emit_pass1_load_id(emit_t *emit, qstr qstr) {
4743
// name adding/lookup
4844
bool added;
@@ -108,7 +104,6 @@ const emit_method_table_t emit_pass1_method_table = {
108104
emit_pass1_start_pass,
109105
emit_pass1_end_pass,
110106
emit_pass1_last_emit_was_return_value,
111-
emit_pass1_get_stack_size,
112107
(void*)emit_pass1_dummy,
113108
(void*)emit_pass1_dummy,
114109

0 commit comments

Comments
 (0)