Skip to content

Commit 6be0b0a

Browse files
committed
py: Clean up and simplify functions in scope; add STATIC in compiler.
Some small code clean-ups that result in about 80 bytes ROM saving for stmhal.
1 parent bf133f7 commit 6be0b0a

2 files changed

Lines changed: 33 additions & 48 deletions

File tree

py/compile.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
354354
}
355355

356356
STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
357-
void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
357+
STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
358358
STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
359359

360360
STATIC uint comp_next_label(compiler_t *comp) {
@@ -729,9 +729,9 @@ STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int la
729729
}
730730

731731
typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
732-
void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
732+
STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
733733

734-
void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
734+
STATIC void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
735735
if (assign_kind != ASSIGN_AUG_STORE) {
736736
compile_node(comp, pns->nodes[0]);
737737
}
@@ -792,7 +792,7 @@ void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t
792792
}
793793

794794
// we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
795-
void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
795+
STATIC void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
796796
uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
797797

798798
// look for star expression
@@ -832,7 +832,7 @@ void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail,
832832
}
833833

834834
// assigns top of stack to pn
835-
void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
835+
STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
836836
tail_recursion:
837837
if (MP_PARSE_NODE_IS_NULL(pn)) {
838838
assert(0);
@@ -947,7 +947,7 @@ void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
947947
// if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
948948
// if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
949949
// if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
950-
void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
950+
STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
951951
assert(n_pos_defaults >= 0);
952952
assert(n_kw_defaults >= 0);
953953

@@ -982,7 +982,7 @@ void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_d
982982
}
983983
}
984984

985-
void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
985+
STATIC void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
986986
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
987987
comp->have_star = true;
988988
/* don't need to distinguish bare from named star
@@ -1254,7 +1254,7 @@ void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
12541254
EMIT_ARG(store_id, fname);
12551255
}
12561256

1257-
void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
1257+
STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
12581258
if (MP_PARSE_NODE_IS_ID(pn)) {
12591259
EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
12601260
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
@@ -1406,7 +1406,7 @@ void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
14061406
// q_base holds the base of the name
14071407
// eg a -> q_base=a
14081408
// a.b.c -> q_base=a
1409-
void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
1409+
STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
14101410
bool is_as = false;
14111411
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
14121412
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
@@ -1466,7 +1466,7 @@ void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
14661466
}
14671467
}
14681468

1469-
void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
1469+
STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
14701470
EMIT_ARG(load_const_small_int, 0); // level 0 import
14711471
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
14721472
qstr q_base;
@@ -1748,7 +1748,7 @@ void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
17481748
// TODO preload end and step onto stack if they are not constants
17491749
// Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
17501750
// And, if the loop never runs, the loop variable should never be assigned
1751-
void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
1751+
STATIC void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
17521752
START_BREAK_CONTINUE_BLOCK
17531753
// note that we don't need to pop anything when breaking from an optimise for loop
17541754

@@ -1884,7 +1884,7 @@ void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
18841884
EMIT_ARG(label_assign, end_label);
18851885
}
18861886

1887-
void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_excepts, mp_parse_node_t pn_else) {
1887+
STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_excepts, mp_parse_node_t pn_else) {
18881888
// setup code
18891889
uint l1 = comp_next_label(comp);
18901890
uint success_label = comp_next_label(comp);
@@ -1976,7 +1976,7 @@ void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except,
19761976
EMIT_ARG(label_assign, l2);
19771977
}
19781978

1979-
void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
1979+
STATIC void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
19801980
uint l_finally_block = comp_next_label(comp);
19811981

19821982
EMIT_ARG(setup_finally, l_finally_block);
@@ -2028,7 +2028,7 @@ void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
20282028
}
20292029
}
20302030

2031-
void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
2031+
STATIC void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
20322032
if (n == 0) {
20332033
// no more pre-bits, compile the body of the with
20342034
compile_node(comp, body);
@@ -2178,7 +2178,7 @@ void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
21782178
}
21792179
}
21802180

2181-
void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
2181+
STATIC void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
21822182
int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
21832183
compile_node(comp, pns->nodes[0]);
21842184
for (int i = 1; i < num_nodes; i += 1) {
@@ -2562,7 +2562,7 @@ void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
25622562
}
25632563

25642564
// pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
2565-
void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
2565+
STATIC void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
25662566
assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
25672567
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
25682568
mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
@@ -2857,7 +2857,7 @@ STATIC compile_function_t compile_function[] = {
28572857
#undef DEF_RULE
28582858
};
28592859

2860-
void compile_node(compiler_t *comp, mp_parse_node_t pn) {
2860+
STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
28612861
if (MP_PARSE_NODE_IS_NULL(pn)) {
28622862
// pass
28632863
} else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
@@ -2902,7 +2902,7 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) {
29022902
}
29032903
}
29042904

2905-
void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
2905+
STATIC void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
29062906
// TODO verify that *k and **k are last etc
29072907
qstr param_name = MP_QSTR_NULL;
29082908
uint param_flag = ID_FLAG_IS_PARAM;
@@ -3004,7 +3004,7 @@ STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
30043004
compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
30053005
}
30063006

3007-
void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse_node_t pn_inner_expr, int l_top, int for_depth) {
3007+
STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse_node_t pn_inner_expr, int l_top, int for_depth) {
30083008
tail_recursion:
30093009
if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
30103010
// no more nested if/for; compile inner expression

py/scope.c

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,10 @@ void scope_free(scope_t *scope) {
8383
}
8484

8585
id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
86-
for (int i = 0; i < scope->id_info_len; i++) {
87-
if (scope->id_info[i].qstr == qstr) {
88-
*added = false;
89-
return &scope->id_info[i];
90-
}
86+
id_info_t *id_info = scope_find(scope, qstr);
87+
if (id_info != NULL) {
88+
*added = false;
89+
return id_info;
9190
}
9291

9392
// make sure we have enough memory
@@ -99,7 +98,7 @@ id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
9998
// add new id to end of array of all ids; this seems to match CPython
10099
// important thing is that function arguments are first, but that is
101100
// handled by the compiler because it adds arguments before compiling the body
102-
id_info_t *id_info = &scope->id_info[scope->id_info_len++];
101+
id_info = &scope->id_info[scope->id_info_len++];
103102

104103
id_info->kind = 0;
105104
id_info->flags = 0;
@@ -110,7 +109,7 @@ id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
110109
}
111110

112111
id_info_t *scope_find(scope_t *scope, qstr qstr) {
113-
for (int i = 0; i < scope->id_info_len; i++) {
112+
for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
114113
if (scope->id_info[i].qstr == qstr) {
115114
return &scope->id_info[i];
116115
}
@@ -122,23 +121,17 @@ id_info_t *scope_find_global(scope_t *scope, qstr qstr) {
122121
while (scope->parent != NULL) {
123122
scope = scope->parent;
124123
}
125-
for (int i = 0; i < scope->id_info_len; i++) {
126-
if (scope->id_info[i].qstr == qstr) {
127-
return &scope->id_info[i];
128-
}
129-
}
130-
return NULL;
124+
return scope_find(scope, qstr);
131125
}
132126

133127
id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr) {
134128
if (scope->parent == NULL) {
135129
return NULL;
136130
}
137131
for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
138-
for (int i = 0; i < s->id_info_len; i++) {
139-
if (s->id_info[i].qstr == qstr) {
140-
return &s->id_info[i];
141-
}
132+
id_info_t *id = scope_find(s, qstr);
133+
if (id != NULL) {
134+
return id;
142135
}
143136
}
144137
return NULL;
@@ -147,18 +140,10 @@ id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr) {
147140
void scope_close_over_in_parents(scope_t *scope, qstr qstr) {
148141
assert(scope->parent != NULL); // we should have at least 1 parent
149142
for (scope_t *s = scope->parent; s->parent != NULL; s = s->parent) {
150-
id_info_t *id = NULL;
151-
for (int i = 0; i < s->id_info_len; i++) {
152-
if (s->id_info[i].qstr == qstr) {
153-
id = &s->id_info[i];
154-
break;
155-
}
156-
}
157-
if (id == NULL) {
158-
// variable not declared in this scope, so declare it as free and keep searching parents
159-
bool added;
160-
id = scope_find_or_add_id(s, qstr, &added);
161-
assert(added);
143+
bool added;
144+
id_info_t *id = scope_find_or_add_id(s, qstr, &added);
145+
if (added) {
146+
// variable not previously declared in this scope, so declare it as free and keep searching parents
162147
id->kind = ID_INFO_KIND_FREE;
163148
} else {
164149
// variable is declared in this scope, so finish

0 commit comments

Comments
 (0)