From ad8a204d91444c1eed78b1e1adc801cbe9070841 Mon Sep 17 00:00:00 2001 From: Jun Wu Date: Mon, 6 May 2019 00:31:11 -0700 Subject: [PATCH 1/2] py: define EMIT_ANY_ASM = EMIT_NATIVE || EMIT_INLINE_ASM There are a lot of places testing MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM. Define a new macro for it. --- py/asmbase.c | 4 ++-- py/emitglue.c | 2 +- py/emitglue.h | 4 ++-- py/mpconfig.h | 3 +++ py/nativeglue.c | 2 +- tools/mpy-tool.py | 4 ++-- 6 files changed, 11 insertions(+), 8 deletions(-) diff --git a/py/asmbase.c b/py/asmbase.c index 4c84c3b255d1..7ba574bd34a4 100644 --- a/py/asmbase.c +++ b/py/asmbase.c @@ -31,7 +31,7 @@ #include "py/misc.h" #include "py/asmbase.h" -#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM +#if MICROPY_EMIT_ANY_ASM void mp_asm_base_init(mp_asm_base_t *as, size_t max_num_labels) { as->max_num_labels = max_num_labels; @@ -99,4 +99,4 @@ void mp_asm_base_data(mp_asm_base_t* as, unsigned int bytesize, uintptr_t val) { } } -#endif // MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM +#endif // MICROPY_EMIT_ANY_ASM diff --git a/py/emitglue.c b/py/emitglue.c index c073258f0167..033fc5d35e93 100644 --- a/py/emitglue.c +++ b/py/emitglue.c @@ -88,7 +88,7 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, #endif } -#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM +#if MICROPY_EMIT_ANY_ASM void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *fun_data, mp_uint_t fun_len, const mp_uint_t *const_table, #if MICROPY_PERSISTENT_CODE_SAVE uint16_t prelude_offset, diff --git a/py/emitglue.h b/py/emitglue.h index 058f06018647..e9903780dccd 100644 --- a/py/emitglue.h +++ b/py/emitglue.h @@ -63,13 +63,13 @@ typedef struct _mp_raw_code_t { size_t fun_data_len; uint16_t n_obj; uint16_t n_raw_code; - #if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM + #if MICROPY_EMIT_ANY_ASM uint16_t prelude_offset; uint16_t n_qstr; mp_qstr_link_entry_t *qstr_link; #endif #endif - #if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM + #if MICROPY_EMIT_ANY_ASM mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc #endif } mp_raw_code_t; diff --git a/py/mpconfig.h b/py/mpconfig.h index 893ac7dc7edd..15822dfd38ef 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -329,6 +329,9 @@ // Convenience definition for whether any inline assembler emitter is enabled #define MICROPY_EMIT_INLINE_ASM (MICROPY_EMIT_INLINE_THUMB || MICROPY_EMIT_INLINE_XTENSA) +// Convenience definition for whether any native or inline assembler emitter is enabled +#define MICROPY_EMIT_ANY_ASM (MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM) + /*****************************************************************************/ /* Compiler configuration */ diff --git a/py/nativeglue.c b/py/nativeglue.c index c810f31e60f2..4179bce3388d 100644 --- a/py/nativeglue.c +++ b/py/nativeglue.c @@ -77,7 +77,7 @@ mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type) { #endif -#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM +#if MICROPY_EMIT_ANY_ASM // convert a native value to a MicroPython object based on type mp_obj_t mp_native_to_obj(mp_uint_t val, mp_uint_t type) { diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index c8216bb603f2..f2238c3e45d2 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -410,13 +410,13 @@ def freeze_module(self, qstr_links=(), type_sig=0): print(' .fun_data_len = %u,' % len(self.bytecode)) print(' .n_obj = %u,' % len(self.objs)) print(' .n_raw_code = %u,' % len(self.raw_codes)) - print(' #if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM') + print(' #if MICROPY_EMIT_ANY_ASM') print(' .prelude_offset = %u,' % self.prelude_offset) print(' .n_qstr = %u,' % len(qstr_links)) print(' .qstr_link = NULL,') # TODO print(' #endif') print(' #endif') - print(' #if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM') + print(' #if MICROPY_EMIT_ANY_ASM') print(' .type_sig = %u,' % type_sig) print(' #endif') print('};') From 96e50c56b170491c92a1bf2a1e931076131f1469 Mon Sep 17 00:00:00 2001 From: Jun Wu Date: Sun, 5 May 2019 23:14:25 -0700 Subject: [PATCH 2/2] py: Fix compilation of persistentcode with uncommon configs With the following configuration: #define MICROPY_PERSISTENT_CODE_SAVE (1) #define MICROPY_PERSISTENT_CODE_LOAD (1) The code fails to compile: persistentcode.c: In function 'load_raw_code': persistentcode.c:457:13: error: 'n_obj' undeclared n_obj, n_raw_code, If MICROPY_EMIT_NATIVE disabled, there are more errors due to the use of undefined fields in mp_raw_code_t. This patch fixes compilation by avoiding undefined fields. MICROPY_EMIT_NATIVE was changed to MICROPY_EMIT_ANY_ASM in this file to match mp_raw_code_t definition. --- py/persistentcode.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/py/persistentcode.c b/py/persistentcode.c index 70ec2ddd6553..dce03fe5ec63 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -160,7 +160,7 @@ typedef struct _bytecode_prelude_t { uint code_info_size; } bytecode_prelude_t; -#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_EMIT_NATIVE +#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_EMIT_ANY_ASM // ip will point to start of opcodes // ip2 will point to simple_name, source_file qstrs @@ -186,7 +186,7 @@ STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_ #include "py/parsenum.h" -#if MICROPY_EMIT_NATIVE +#if MICROPY_EMIT_ANY_ASM #if MICROPY_EMIT_THUMB STATIC void asm_thumb_rewrite_mov(uint8_t *pc, uint16_t val) { @@ -330,7 +330,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) { int kind = (kind_len & 3) + MP_CODE_BYTECODE; size_t fun_data_len = kind_len >> 2; - #if !MICROPY_EMIT_NATIVE + #if !MICROPY_EMIT_ANY_ASM if (kind != MP_CODE_BYTECODE) { mp_raise_ValueError("incompatible .mpy file"); } @@ -339,7 +339,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) { uint8_t *fun_data = NULL; byte *ip2; bytecode_prelude_t prelude = {0}; - #if MICROPY_EMIT_NATIVE + #if MICROPY_EMIT_ANY_ASM size_t prelude_offset; mp_uint_t type_sig = 0; size_t n_qstr_link = 0; @@ -356,7 +356,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) { // Load bytecode load_bytecode(reader, qw, ip, fun_data + fun_data_len); - #if MICROPY_EMIT_NATIVE + #if MICROPY_EMIT_ANY_ASM } else { // Allocate memory for native data and load it size_t fun_alloc; @@ -407,13 +407,16 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) { ip2[2] = source_file; ip2[3] = source_file >> 8; } + size_t n_obj = 0; + size_t n_raw_code = 0; mp_uint_t *const_table = NULL; + if (kind != MP_CODE_NATIVE_ASM) { // Load constant table for bytecode, native and viper // Number of entries in constant table - size_t n_obj = read_uint(reader, NULL); - size_t n_raw_code = read_uint(reader, NULL); + n_obj = read_uint(reader, NULL); + n_raw_code = read_uint(reader, NULL); // Allocate constant table size_t n_alloc = prelude.n_pos_args + prelude.n_kwonly_args + n_obj + n_raw_code; @@ -429,7 +432,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) { *ct++ = (mp_uint_t)MP_OBJ_NEW_QSTR(load_qstr(reader, qw)); } - #if MICROPY_EMIT_NATIVE + #if MICROPY_EMIT_ANY_ASM if (kind != MP_CODE_BYTECODE) { // Populate mp_fun_table entry *ct++ = (mp_uint_t)(uintptr_t)mp_fun_table; @@ -458,7 +461,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) { #endif prelude.scope_flags); - #if MICROPY_EMIT_NATIVE + #if MICROPY_EMIT_ANY_ASM } else { #if defined(MP_PLAT_COMMIT_EXEC) fun_data = MP_PLAT_COMMIT_EXEC(fun_data, fun_data_len); @@ -629,6 +632,7 @@ STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc, qstr_window_t *q // Save bytecode save_bytecode(print, qstr_window, ip, ip_top); + #if MICROPY_EMIT_ANY_ASM } else { // Save native code mp_print_bytes(print, rc->fun_data, rc->fun_data_len); @@ -657,6 +661,7 @@ STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc, qstr_window_t *q mp_print_uint(print, rc->type_sig); } } + #endif } if (rc->kind == MP_CODE_BYTECODE || rc->kind == MP_CODE_NATIVE_PY) {