From 882b363564b64204c12726d5d2cd7f596322729f Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 2 Apr 2014 15:56:31 +0100 Subject: [PATCH] py: Move to Python 3.4.0 compatibility. Very little has changed. In Python 3.4 they removed the opcode STORE_LOCALS, but in Micro Python we only ever used this for CPython compatibility, so it was a trivial thing to remove. It also allowed to clean up some dead code (eg the 0xdeadbeef in class construction), and now class builders use 1 less stack word. Python 3.4.0 introduced the LOAD_CLASSDEREF opcode, which I have not yet understood. Still, all tests (apart from bytecode test) still pass. Bytecode tests needs some more attention, but they are not that important anymore. --- py/builtin.c | 2 +- py/compile.c | 15 ++++++--------- py/emit.h | 1 - py/emitbc.c | 7 ------- py/emitcpy.c | 8 -------- py/emitnative.c | 8 -------- py/emitpass1.c | 1 - 7 files changed, 7 insertions(+), 35 deletions(-) diff --git a/py/builtin.c b/py/builtin.c index 678b3c76bb..67b0e46a66 100644 --- a/py/builtin.c +++ b/py/builtin.c @@ -26,7 +26,7 @@ STATIC mp_obj_t mp_builtin___build_class__(uint n_args, const mp_obj_t *args) { mp_locals_set(mp_obj_dict_get_map(class_locals)); // call the class code - mp_obj_t cell = mp_call_function_1(args[0], (mp_obj_t)0xdeadbeef); + mp_obj_t cell = mp_call_function_0(args[0]); // restore old __locals__ object mp_locals_set(old_locals); diff --git a/py/compile.c b/py/compile.c index 25830eb6f9..a27561334a 100644 --- a/py/compile.c +++ b/py/compile.c @@ -2957,15 +2957,8 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added); assert(added); id_info->kind = ID_INFO_KIND_LOCAL; - id_info = scope_find_or_add_id(scope, MP_QSTR___locals__, &added); - assert(added); - id_info->kind = ID_INFO_KIND_LOCAL; - id_info->param = true; - scope->num_params = 1; // __locals__ is the parameter } - EMIT_ARG(load_id, MP_QSTR___locals__); - EMIT(store_locals); EMIT_ARG(load_id, MP_QSTR___name__); EMIT_ARG(store_id, MP_QSTR___module__); EMIT_ARG(load_const_id, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name @@ -3155,8 +3148,10 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) { } // compute scope_flags - //scope->scope_flags = 0; since we set some things in parameters - if (scope->kind != SCOPE_MODULE) { + +#if MICROPY_EMIT_CPYTHON + // these flags computed here are for CPython compatibility only + if (scope->kind == SCOPE_FUNCTION) { scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS; } if (scope->kind == SCOPE_FUNCTION || scope->kind == SCOPE_LAMBDA || scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) { @@ -3169,6 +3164,8 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) { scope->scope_flags |= MP_SCOPE_FLAG_NESTED; } } +#endif + int num_free = 0; for (int i = 0; i < scope->id_info_len; i++) { id_info_t *id = &scope->id_info[i]; diff --git a/py/emit.h b/py/emit.h index 0d95b699e2..20128fc75b 100644 --- a/py/emit.h +++ b/py/emit.h @@ -54,7 +54,6 @@ typedef struct _emit_method_table_t { void (*store_global)(emit_t *emit, qstr qstr); void (*store_attr)(emit_t *emit, qstr qstr); void (*store_subscr)(emit_t *emit); - void (*store_locals)(emit_t *emit); void (*delete_fast)(emit_t *emit, qstr qstr, int local_num); void (*delete_deref)(emit_t *emit, qstr qstr, int local_num); void (*delete_name)(emit_t *emit, qstr qstr); diff --git a/py/emitbc.c b/py/emitbc.c index 0a83448559..11519fdd80 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -486,12 +486,6 @@ STATIC void emit_bc_store_subscr(emit_t *emit) { emit_write_byte_code_byte(emit, MP_BC_STORE_SUBSCR); } -STATIC void emit_bc_store_locals(emit_t *emit) { - // not needed - emit_bc_pre(emit, -1); - emit_write_byte_code_byte(emit, MP_BC_POP_TOP); -} - STATIC void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) { assert(local_num >= 0); emit_bc_pre(emit, 0); @@ -860,7 +854,6 @@ const emit_method_table_t emit_bc_method_table = { emit_bc_store_global, emit_bc_store_attr, emit_bc_store_subscr, - emit_bc_store_locals, emit_bc_delete_fast, emit_bc_delete_deref, emit_bc_delete_name, diff --git a/py/emitcpy.c b/py/emitcpy.c index 4f5a96f2e4..8345c12dca 100644 --- a/py/emitcpy.c +++ b/py/emitcpy.c @@ -325,13 +325,6 @@ STATIC void emit_cpy_store_subscr(emit_t *emit) { } } -STATIC void emit_cpy_store_locals(emit_t *emit) { - emit_pre(emit, -1, 1); - if (emit->pass == PASS_3) { - printf("STORE_LOCALS\n"); - } -} - STATIC void emit_cpy_delete_fast(emit_t *emit, qstr qstr, int local_num) { emit_pre(emit, 0, 3); if (emit->pass == PASS_3) { @@ -833,7 +826,6 @@ const emit_method_table_t emit_cpython_method_table = { emit_cpy_store_global, emit_cpy_store_attr, emit_cpy_store_subscr, - emit_cpy_store_locals, emit_cpy_delete_fast, emit_cpy_delete_deref, emit_cpy_delete_name, diff --git a/py/emitnative.c b/py/emitnative.c index da0aa60381..524c5caa75 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -818,13 +818,6 @@ STATIC void emit_native_store_subscr(emit_t *emit) { emit_call(emit, MP_F_STORE_SUBSCR, mp_store_subscr); } -STATIC void emit_native_store_locals(emit_t *emit) { - // not needed - vtype_kind_t vtype; - emit_pre_pop_reg(emit, &vtype, REG_TEMP0); - emit_post(emit); -} - STATIC void emit_native_delete_fast(emit_t *emit, qstr qstr, int local_num) { // not implemented // could support for Python types, just set to None (so GC can reclaim it) @@ -1290,7 +1283,6 @@ const emit_method_table_t EXPORT_FUN(method_table) = { emit_native_store_global, emit_native_store_attr, emit_native_store_subscr, - emit_native_store_locals, emit_native_delete_fast, emit_native_delete_deref, emit_native_delete_name, diff --git a/py/emitpass1.c b/py/emitpass1.c index e4dbf14121..d2f7aaa53a 100644 --- a/py/emitpass1.c +++ b/py/emitpass1.c @@ -182,5 +182,4 @@ const emit_method_table_t emit_pass1_method_table = { (void*)emit_pass1_dummy, (void*)emit_pass1_dummy, (void*)emit_pass1_dummy, - (void*)emit_pass1_dummy, };