Skip to content

Commit 542bd6b

Browse files
committed
py, compiler: Refactor load/store/delete_id logic to reduce code size.
Saves around 230 bytes on Thumb2 and 750 bytes on x86.
1 parent 760a6ec commit 542bd6b

7 files changed

Lines changed: 186 additions & 272 deletions

File tree

py/compile.c

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ typedef enum {
5151

5252
#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
5353
#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
54+
#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
55+
#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
5456
#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
5557
#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
5658

@@ -400,6 +402,30 @@ STATIC void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *
400402
}
401403
}
402404

405+
STATIC void compile_load_id(compiler_t *comp, qstr qst) {
406+
if (comp->pass == MP_PASS_SCOPE) {
407+
mp_emit_common_get_id_for_load(comp->scope_cur, qst);
408+
} else {
409+
mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
410+
}
411+
}
412+
413+
STATIC void compile_store_id(compiler_t *comp, qstr qst) {
414+
if (comp->pass == MP_PASS_SCOPE) {
415+
mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
416+
} else {
417+
mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
418+
}
419+
}
420+
421+
STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
422+
if (comp->pass == MP_PASS_SCOPE) {
423+
mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
424+
} else {
425+
mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
426+
}
427+
}
428+
403429
#if MICROPY_EMIT_CPYTHON
404430
STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
405431
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
@@ -823,11 +849,11 @@ STATIC void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_
823849
switch (assign_kind) {
824850
case ASSIGN_STORE:
825851
case ASSIGN_AUG_STORE:
826-
EMIT_ARG(store_id, arg);
852+
compile_store_id(comp, arg);
827853
break;
828854
case ASSIGN_AUG_LOAD:
829855
default:
830-
EMIT_ARG(load_id, arg);
856+
compile_load_id(comp, arg);
831857
break;
832858
}
833859
} else {
@@ -951,7 +977,7 @@ STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int
951977
EMIT_ARG(load_closure, id->qst, id->local_num);
952978
#else
953979
// in Micro Python we load closures using LOAD_FAST
954-
EMIT_ARG(load_fast, id->qst, id->local_num);
980+
EMIT_LOAD_FAST(id->qst, id->local_num);
955981
#endif
956982
nfree += 1;
957983
}
@@ -1226,18 +1252,18 @@ STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
12261252
}
12271253

12281254
// store func/class object into name
1229-
EMIT_ARG(store_id, body_name);
1255+
compile_store_id(comp, body_name);
12301256
}
12311257

12321258
STATIC void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
12331259
qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
12341260
// store function object into function name
1235-
EMIT_ARG(store_id, fname);
1261+
compile_store_id(comp, fname);
12361262
}
12371263

12381264
STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
12391265
if (MP_PARSE_NODE_IS_ID(pn)) {
1240-
EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
1266+
compile_delete_id(comp, MP_PARSE_NODE_LEAF_ARG(pn));
12411267
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
12421268
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
12431269

@@ -1446,7 +1472,7 @@ STATIC void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
14461472
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
14471473
qstr q_base;
14481474
do_import_name(comp, pn, &q_base);
1449-
EMIT_ARG(store_id, q_base);
1475+
compile_store_id(comp, q_base);
14501476
}
14511477

14521478
STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
@@ -1555,9 +1581,9 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
15551581
qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
15561582
EMIT_ARG(import_from, id2);
15571583
if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
1558-
EMIT_ARG(store_id, id2);
1584+
compile_store_id(comp, id2);
15591585
} else {
1560-
EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
1586+
compile_store_id(comp, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
15611587
}
15621588
}
15631589
EMIT(pop_top);
@@ -1625,7 +1651,7 @@ STATIC void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns)
16251651
STATIC void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
16261652
uint l_end = comp_next_label(comp);
16271653
c_if_cond(comp, pns->nodes[0], true, l_end);
1628-
EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
1654+
EMIT_LOAD_GLOBAL(MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
16291655
if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
16301656
// assertion message
16311657
compile_node(comp, pns->nodes[1]);
@@ -1972,7 +1998,7 @@ STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_
19721998
if (qstr_exception_local == 0) {
19731999
EMIT(pop_top);
19742000
} else {
1975-
EMIT_ARG(store_id, qstr_exception_local);
2001+
compile_store_id(comp, qstr_exception_local);
19762002
}
19772003

19782004
EMIT(pop_top);
@@ -1992,8 +2018,8 @@ STATIC void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_
19922018
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
19932019
EMIT_ARG(label_assign, l3);
19942020
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
1995-
EMIT_ARG(store_id, qstr_exception_local);
1996-
EMIT_ARG(delete_id, qstr_exception_local);
2021+
compile_store_id(comp, qstr_exception_local);
2022+
compile_delete_id(comp, qstr_exception_local);
19972023

19982024
compile_decrease_except_level(comp);
19992025
EMIT(end_finally);
@@ -2107,7 +2133,7 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
21072133
if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
21082134
if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
21092135
// for REPL, evaluate then print the expression
2110-
EMIT_ARG(load_id, MP_QSTR___repl_print__);
2136+
compile_load_id(comp, MP_QSTR___repl_print__);
21112137
compile_node(comp, pns->nodes[0]);
21122138
EMIT_ARG(call_function, 1, 0, 0);
21132139
EMIT(pop_top);
@@ -2424,12 +2450,12 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
24242450
#if !MICROPY_EMIT_CPYTHON
24252451
// this is to handle special super() call
24262452
if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
2427-
EMIT_ARG(load_id, MP_QSTR___class__);
2453+
compile_load_id(comp, MP_QSTR___class__);
24282454
// look for first argument to function (assumes it's "self")
24292455
for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
24302456
if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
24312457
// first argument found; load it and call super
2432-
EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].local_num);
2458+
EMIT_LOAD_FAST(MP_QSTR_, comp->scope_cur->id_info[i].local_num);
24332459
EMIT_ARG(call_function, 2, 0, 0);
24342460
return;
24352461
}
@@ -2853,7 +2879,7 @@ STATIC void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t
28532879
STATIC void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
28542880
qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
28552881
// store class object into class name
2856-
EMIT_ARG(store_id, cname);
2882+
compile_store_id(comp, cname);
28572883
}
28582884

28592885
STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
@@ -2922,7 +2948,7 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
29222948
} else if (MP_PARSE_NODE_IS_LEAF(pn)) {
29232949
mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
29242950
switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
2925-
case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
2951+
case MP_PARSE_NODE_ID: compile_load_id(comp, arg); break;
29262952
case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
29272953
case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
29282954
case MP_PARSE_NODE_TOKEN: default:
@@ -3145,7 +3171,7 @@ STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
31453171
// compile the doc string
31463172
compile_node(comp, pns->nodes[0]);
31473173
// store the doc string
3148-
EMIT_ARG(store_id, MP_QSTR___doc__);
3174+
compile_store_id(comp, MP_QSTR___doc__);
31493175
}
31503176
}
31513177
#else
@@ -3283,7 +3309,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
32833309

32843310
uint l_end = comp_next_label(comp);
32853311
uint l_top = comp_next_label(comp);
3286-
EMIT_ARG(load_id, qstr_arg);
3312+
compile_load_id(comp, qstr_arg);
32873313
EMIT_ARG(label_assign, l_top);
32883314
EMIT_ARG(for_iter, l_end);
32893315
c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
@@ -3309,10 +3335,10 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
33093335
id_info->kind = ID_INFO_KIND_LOCAL;
33103336
}
33113337

3312-
EMIT_ARG(load_id, MP_QSTR___name__);
3313-
EMIT_ARG(store_id, MP_QSTR___module__);
3338+
compile_load_id(comp, MP_QSTR___name__);
3339+
compile_store_id(comp, MP_QSTR___module__);
33143340
EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), false); // 0 is class name
3315-
EMIT_ARG(store_id, MP_QSTR___qualname__);
3341+
compile_store_id(comp, MP_QSTR___qualname__);
33163342

33173343
check_for_doc_string(comp, pns->nodes[2]);
33183344
compile_node(comp, pns->nodes[2]); // 2 is class body
@@ -3325,7 +3351,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
33253351
#if MICROPY_EMIT_CPYTHON
33263352
EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
33273353
#else
3328-
EMIT_ARG(load_fast, MP_QSTR___class__, id->local_num);
3354+
EMIT_LOAD_FAST(MP_QSTR___class__, id->local_num);
33293355
#endif
33303356
}
33313357
EMIT(return_value);
@@ -3622,7 +3648,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
36223648
mp_map_deinit(&consts);
36233649

36243650
// compile pass 1
3625-
comp->emit = emit_pass1_new();
3651+
comp->emit = NULL;
36263652
comp->emit_method_table = &emit_pass1_method_table;
36273653
#if MICROPY_EMIT_INLINE_THUMB
36283654
comp->emit_inline_asm = NULL;
@@ -3650,9 +3676,6 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
36503676
scope_compute_things(s);
36513677
}
36523678

3653-
// finish with pass 1
3654-
emit_pass1_free(comp->emit);
3655-
36563679
// compile pass 2 and 3
36573680
#if !MICROPY_EMIT_CPYTHON
36583681
emit_t *emit_bc = NULL;

py/emit.h

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ typedef enum {
5959

6060
typedef struct _emit_t emit_t;
6161

62+
typedef struct _mp_emit_method_table_id_ops_t {
63+
void (*fast)(emit_t *emit, qstr qst, mp_uint_t local_num);
64+
void (*deref)(emit_t *emit, qstr qst, mp_uint_t local_num);
65+
void (*name)(emit_t *emit, qstr qst);
66+
void (*global)(emit_t *emit, qstr qst);
67+
} mp_emit_method_table_id_ops_t;
68+
6269
typedef struct _emit_method_table_t {
6370
void (*set_native_type)(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2);
6471
void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
@@ -67,9 +74,9 @@ typedef struct _emit_method_table_t {
6774
void (*adjust_stack_size)(emit_t *emit, mp_int_t delta);
6875
void (*set_line_number)(emit_t *emit, mp_uint_t line);
6976

70-
void (*load_id)(emit_t *emit, qstr qst);
71-
void (*store_id)(emit_t *emit, qstr qst);
72-
void (*delete_id)(emit_t *emit, qstr qst);
77+
mp_emit_method_table_id_ops_t load_id;
78+
mp_emit_method_table_id_ops_t store_id;
79+
mp_emit_method_table_id_ops_t delete_id;
7380

7481
void (*label_assign)(emit_t *emit, mp_uint_t l);
7582
void (*import_name)(emit_t *emit, qstr qst);
@@ -80,24 +87,12 @@ typedef struct _emit_method_table_t {
8087
void (*load_const_str)(emit_t *emit, qstr qst, bool bytes);
8188
void (*load_const_obj)(emit_t *emit, void *obj);
8289
void (*load_null)(emit_t *emit);
83-
void (*load_fast)(emit_t *emit, qstr qst, mp_uint_t local_num);
84-
void (*load_deref)(emit_t *emit, qstr qst, mp_uint_t local_num);
85-
void (*load_name)(emit_t *emit, qstr qst);
86-
void (*load_global)(emit_t *emit, qstr qst);
8790
void (*load_attr)(emit_t *emit, qstr qst);
8891
void (*load_method)(emit_t *emit, qstr qst);
8992
void (*load_build_class)(emit_t *emit);
9093
void (*load_subscr)(emit_t *emit);
91-
void (*store_fast)(emit_t *emit, qstr qst, mp_uint_t local_num);
92-
void (*store_deref)(emit_t *emit, qstr qst, mp_uint_t local_num);
93-
void (*store_name)(emit_t *emit, qstr qst);
94-
void (*store_global)(emit_t *emit, qstr qst);
9594
void (*store_attr)(emit_t *emit, qstr qst);
9695
void (*store_subscr)(emit_t *emit);
97-
void (*delete_fast)(emit_t *emit, qstr qst, mp_uint_t local_num);
98-
void (*delete_deref)(emit_t *emit, qstr qst, mp_uint_t local_num);
99-
void (*delete_name)(emit_t *emit, qstr qst);
100-
void (*delete_global)(emit_t *emit, qstr qst);
10196
void (*delete_attr)(emit_t *emit, qstr qst);
10297
void (*delete_subscr)(emit_t *emit);
10398
void (*dup_top)(emit_t *emit);
@@ -160,9 +155,9 @@ typedef struct _emit_method_table_t {
160155

161156
} emit_method_table_t;
162157

163-
void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qst);
164-
void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qst);
165-
void emit_common_delete_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qst);
158+
void mp_emit_common_get_id_for_load(scope_t *scope, qstr qst);
159+
void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst);
160+
void mp_emit_common_id_op(emit_t *emit, const mp_emit_method_table_id_ops_t *emit_method_table, scope_t *scope, qstr qst);
166161

167162
extern const emit_method_table_t emit_pass1_method_table;
168163
extern const emit_method_table_t emit_cpython_method_table;
@@ -172,15 +167,13 @@ extern const emit_method_table_t emit_native_x86_method_table;
172167
extern const emit_method_table_t emit_native_thumb_method_table;
173168
extern const emit_method_table_t emit_native_arm_method_table;
174169

175-
emit_t *emit_pass1_new(void);
176170
emit_t *emit_cpython_new(mp_uint_t max_num_labels);
177171
emit_t *emit_bc_new(mp_uint_t max_num_labels);
178172
emit_t *emit_native_x64_new(mp_uint_t max_num_labels);
179173
emit_t *emit_native_x86_new(mp_uint_t max_num_labels);
180174
emit_t *emit_native_thumb_new(mp_uint_t max_num_labels);
181175
emit_t *emit_native_arm_new(mp_uint_t max_num_labels);
182176

183-
void emit_pass1_free(emit_t *emit);
184177
void emit_bc_free(emit_t *emit);
185178
void emit_native_x64_free(emit_t *emit);
186179
void emit_native_x86_free(emit_t *emit);

py/emitbc.c

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -402,18 +402,6 @@ STATIC void emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
402402
#endif
403403
}
404404

405-
STATIC void emit_bc_load_id(emit_t *emit, qstr qst) {
406-
emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qst);
407-
}
408-
409-
STATIC void emit_bc_store_id(emit_t *emit, qstr qst) {
410-
emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qst);
411-
}
412-
413-
STATIC void emit_bc_delete_id(emit_t *emit, qstr qst) {
414-
emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qst);
415-
}
416-
417405
STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
418406
assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
419407
emit->stack_size += stack_size_delta;
@@ -910,9 +898,24 @@ const emit_method_table_t emit_bc_method_table = {
910898
emit_bc_adjust_stack_size,
911899
emit_bc_set_source_line,
912900

913-
emit_bc_load_id,
914-
emit_bc_store_id,
915-
emit_bc_delete_id,
901+
{
902+
emit_bc_load_fast,
903+
emit_bc_load_deref,
904+
emit_bc_load_name,
905+
emit_bc_load_global,
906+
},
907+
{
908+
emit_bc_store_fast,
909+
emit_bc_store_deref,
910+
emit_bc_store_name,
911+
emit_bc_store_global,
912+
},
913+
{
914+
emit_bc_delete_fast,
915+
emit_bc_delete_deref,
916+
emit_bc_delete_name,
917+
emit_bc_delete_global,
918+
},
916919

917920
emit_bc_label_assign,
918921
emit_bc_import_name,
@@ -923,24 +926,12 @@ const emit_method_table_t emit_bc_method_table = {
923926
emit_bc_load_const_str,
924927
emit_bc_load_const_obj,
925928
emit_bc_load_null,
926-
emit_bc_load_fast,
927-
emit_bc_load_deref,
928-
emit_bc_load_name,
929-
emit_bc_load_global,
930929
emit_bc_load_attr,
931930
emit_bc_load_method,
932931
emit_bc_load_build_class,
933932
emit_bc_load_subscr,
934-
emit_bc_store_fast,
935-
emit_bc_store_deref,
936-
emit_bc_store_name,
937-
emit_bc_store_global,
938933
emit_bc_store_attr,
939934
emit_bc_store_subscr,
940-
emit_bc_delete_fast,
941-
emit_bc_delete_deref,
942-
emit_bc_delete_name,
943-
emit_bc_delete_global,
944935
emit_bc_delete_attr,
945936
emit_bc_delete_subscr,
946937
emit_bc_dup_top,

0 commit comments

Comments
 (0)