aboutsummaryrefslogtreecommitdiff
path: root/py/emitcpy.c
diff options
context:
space:
mode:
authorDamien George2015-03-26 15:49:53 +0000
committerDamien George2015-03-26 16:52:45 +0000
commita210c774f9cfbcde284f5b8a63442b4d6da1ae5b (patch)
tree1277140fe133c950ac047cb909a01ce7330a0b84 /py/emitcpy.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/emitcpy.c')
-rw-r--r--py/emitcpy.c23
1 files changed, 20 insertions, 3 deletions
diff --git a/py/emitcpy.c b/py/emitcpy.c
index 0e040b0e9..48c293091 100644
--- a/py/emitcpy.c
+++ b/py/emitcpy.c
@@ -47,11 +47,19 @@ struct _emit_t {
mp_uint_t *label_offsets;
};
-emit_t *emit_cpython_new(mp_uint_t max_num_labels) {
- emit_t *emit = m_new(emit_t, 1);
+emit_t *emit_cpython_new(void) {
+ emit_t *emit = m_new0(emit_t, 1);
+ return emit;
+}
+
+void emit_cpython_set_max_num_labels(emit_t* emit, mp_uint_t max_num_labels) {
emit->max_num_labels = max_num_labels;
emit->label_offsets = m_new(mp_uint_t, max_num_labels);
- return emit;
+}
+
+void emit_cpython_free(emit_t *emit) {
+ m_del(mp_uint_t, emit->label_offsets, emit->max_num_labels);
+ m_del_obj(emit_t, emit);
}
STATIC void emit_cpy_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
@@ -69,6 +77,9 @@ STATIC void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope)
}
STATIC void emit_cpy_end_pass(emit_t *emit) {
+ if (emit->pass == MP_PASS_SCOPE) {
+ return;
+ }
// check stack is back to zero size
if (emit->stack_size != 0) {
printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
@@ -88,6 +99,9 @@ STATIC void emit_cpy_set_source_line(emit_t *emit, mp_uint_t source_line) {
// TODO: module-polymorphic function (read: name clash if made global)
static void emit_pre(emit_t *emit, int stack_size_delta, int bytecode_size) {
+ if (emit->pass == MP_PASS_SCOPE) {
+ return;
+ }
emit->stack_size += stack_size_delta;
if (emit->stack_size > emit->scope->stack_size) {
emit->scope->stack_size = emit->stack_size;
@@ -105,6 +119,9 @@ static void emit_pre(emit_t *emit, int stack_size_delta, int bytecode_size) {
STATIC void emit_cpy_label_assign(emit_t *emit, mp_uint_t l) {
emit_pre(emit, 0, 0);
+ if (emit->pass == MP_PASS_SCOPE) {
+ return;
+ }
assert(l < emit->max_num_labels);
if (emit->pass < MP_PASS_EMIT) {
// assign label offset