summaryrefslogtreecommitdiff
path: root/py/compile.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-03-26 15:49:53 +0000
committerDamien George <damien.p.george@gmail.com>2015-03-26 16:52:45 +0000
commita210c774f9cfbcde284f5b8a63442b4d6da1ae5b (patch)
tree1277140fe133c950ac047cb909a01ce7330a0b84 /py/compile.c
parent542bd6b4a1ead90fde42f24d01c82580f9d048de (diff)
py, compiler: Remove emit_pass1 code, using emit_bc to do its job.
First pass for the compiler is computing the scope (eg if an identifier is local or not) and originally had an entire table of methods dedicated to this, most of which did nothing. With changes from previous commit, this set of methods can be removed and the methods from the bytecode emitter used instead, with very little modification -- this is what is done in this commit. This factoring has little to no impact on the speed of the compiler (tested by compiling 3763 Python scripts and timing it). This factoring reduces code size by about 270-300 bytes on Thumb2 archs, and 400 bytes on x86.
Diffstat (limited to 'py/compile.c')
-rw-r--r--py/compile.c40
1 files changed, 28 insertions, 12 deletions
diff --git a/py/compile.c b/py/compile.c
index 4b78b7f37..a41851c3a 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -3647,9 +3647,21 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
module_scope->pn = fold_constants(comp, module_scope->pn, &consts);
mp_map_deinit(&consts);
+ // create standard emitter; it's used at least for MP_PASS_SCOPE
+ #if MICROPY_EMIT_CPYTHON
+ emit_t *emit_cpython = emit_cpython_new();
+ #else
+ emit_t *emit_bc = emit_bc_new();
+ #endif
+
// compile pass 1
- comp->emit = NULL;
- comp->emit_method_table = &emit_pass1_method_table;
+ #if MICROPY_EMIT_CPYTHON
+ comp->emit = emit_cpython;
+ comp->emit_method_table = &emit_cpython_method_table;
+ #else
+ comp->emit = emit_bc;
+ comp->emit_method_table = &emit_bc_method_table;
+ #endif
#if MICROPY_EMIT_INLINE_THUMB
comp->emit_inline_asm = NULL;
comp->emit_inline_asm_method_table = NULL;
@@ -3676,9 +3688,15 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
scope_compute_things(s);
}
+ // set max number of labels now that it's calculated
+ #if MICROPY_EMIT_CPYTHON
+ emit_cpython_set_max_num_labels(emit_cpython, max_num_labels);
+ #else
+ emit_bc_set_max_num_labels(emit_bc, max_num_labels);
+ #endif
+
// compile pass 2 and 3
#if !MICROPY_EMIT_CPYTHON
- emit_t *emit_bc = NULL;
#if MICROPY_EMIT_NATIVE
emit_t *emit_native = NULL;
#endif
@@ -3711,7 +3729,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
// choose the emit type
#if MICROPY_EMIT_CPYTHON
- comp->emit = emit_cpython_new(max_num_labels);
+ comp->emit = emit_cpython;
comp->emit_method_table = &emit_cpython_method_table;
#else
switch (s->emit_options) {
@@ -3746,9 +3764,6 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
#endif // MICROPY_EMIT_NATIVE
default:
- if (emit_bc == NULL) {
- emit_bc = emit_bc_new(max_num_labels);
- }
comp->emit = emit_bc;
comp->emit_method_table = &emit_bc_method_table;
break;
@@ -3771,10 +3786,11 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
}
// free the emitters
-#if !MICROPY_EMIT_CPYTHON
- if (emit_bc != NULL) {
- emit_bc_free(emit_bc);
- }
+
+#if MICROPY_EMIT_CPYTHON
+ emit_cpython_free(emit_cpython);
+#else
+ emit_bc_free(emit_bc);
#if MICROPY_EMIT_NATIVE
if (emit_native != NULL) {
#if MICROPY_EMIT_X64
@@ -3793,7 +3809,7 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
emit_inline_thumb_free(emit_inline_thumb);
}
#endif
-#endif // !MICROPY_EMIT_CPYTHON
+#endif // MICROPY_EMIT_CPYTHON
// free the parse tree
mp_parse_node_free(module_scope->pn);