diff --git a/docs/develop/compiler.rst b/docs/develop/compiler.rst index 4b43bc08f..cac92585f 100644 --- a/docs/develop/compiler.rst +++ b/docs/develop/compiler.rst @@ -152,7 +152,9 @@ The most relevant method you should know about is this: context->module.globals = mp_globals_get(); // Compile the input parse_tree to a raw-code structure. - mp_compiled_module_t cm = mp_compile_to_raw_code(parse_tree, source_file, is_repl, context); + mp_compiled_module_t cm; + cm.context = context; + mp_compile_to_raw_code(parse_tree, source_file, is_repl, &cm); // Create and return a function object that executes the outer module. return mp_make_function_from_raw_code(cm.rc, cm.context, NULL); diff --git a/mpy-cross/main.c b/mpy-cross/main.c index fd326439f..4a21d7584 100644 --- a/mpy-cross/main.c +++ b/mpy-cross/main.c @@ -73,8 +73,9 @@ STATIC int compile_and_save(const char *file, const char *output_file, const cha #endif mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT); - mp_module_context_t *ctx = m_new_obj(mp_module_context_t); - mp_compiled_module_t cm = mp_compile_to_raw_code(&parse_tree, source_name, false, ctx); + mp_compiled_module_t cm; + cm.context = m_new_obj(mp_module_context_t); + mp_compile_to_raw_code(&parse_tree, source_name, false, &cm); vstr_t vstr; vstr_init(&vstr, 16); diff --git a/py/bc.h b/py/bc.h index 6350eee52..0e23255f7 100644 --- a/py/bc.h +++ b/py/bc.h @@ -214,7 +214,7 @@ typedef struct _mp_module_context_t { // Outer level struct defining a compiled module. typedef struct _mp_compiled_module_t { - const mp_module_context_t *context; + mp_module_context_t *context; const struct _mp_raw_code_t *rc; #if MICROPY_PERSISTENT_CODE_SAVE bool has_native; diff --git a/py/builtinimport.c b/py/builtinimport.c index aa31b69db..6f1f7b485 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -245,7 +245,9 @@ STATIC void do_load(mp_module_context_t *module_obj, vstr_t *file) { // the correct format and, if so, load and execute the file. #if MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD if (file_str[file->len - 3] == 'm') { - mp_compiled_module_t cm = mp_raw_code_load_file(file_str, module_obj); + mp_compiled_module_t cm; + cm.context = module_obj; + mp_raw_code_load_file(file_str, &cm); do_execute_raw_code(cm.context, cm.rc, file_str); return; } diff --git a/py/compile.c b/py/compile.c index 8aaf88532..76d4c1bf5 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3427,7 +3427,7 @@ STATIC void scope_compute_things(scope_t *scope) { #if !MICROPY_PERSISTENT_CODE_SAVE STATIC #endif -mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl, mp_module_context_t *context) { +void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl, mp_compiled_module_t *cm) { // put compiler state on the stack, it's relatively small compiler_t comp_state = {0}; compiler_t *comp = &comp_state; @@ -3568,26 +3568,24 @@ mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr so } // construct the global qstr/const table for this module - mp_compiled_module_t cm; - cm.rc = module_scope->raw_code; - cm.context = context; + cm->rc = module_scope->raw_code; #if MICROPY_PERSISTENT_CODE_SAVE - cm.has_native = false; + cm->has_native = false; #if MICROPY_EMIT_NATIVE if (emit_native != NULL) { - cm.has_native = true; + cm->has_native = true; } #endif #if MICROPY_EMIT_INLINE_ASM if (comp->emit_inline_asm != NULL) { - cm.has_native = true; + cm->has_native = true; } #endif - cm.n_qstr = comp->emit_common.qstr_map.used; - cm.n_obj = comp->emit_common.const_obj_list.len; + cm->n_qstr = comp->emit_common.qstr_map.used; + cm->n_obj = comp->emit_common.const_obj_list.len; #endif if (comp->compile_error == MP_OBJ_NULL) { - mp_emit_common_populate_module_context(&comp->emit_common, source_file, context); + mp_emit_common_populate_module_context(&comp->emit_common, source_file, cm->context); #if MICROPY_DEBUG_PRINTERS // now that the module context is valid, the raw codes can be printed @@ -3595,7 +3593,7 @@ mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr so for (scope_t *s = comp->scope_head; s != NULL; s = s->next) { mp_raw_code_t *rc = s->raw_code; if (rc->kind == MP_CODE_BYTECODE) { - mp_bytecode_print(&mp_plat_print, rc, &cm.context->constants); + mp_bytecode_print(&mp_plat_print, rc, &cm->context->constants); } } } @@ -3629,14 +3627,13 @@ mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr so if (comp->compile_error != MP_OBJ_NULL) { nlr_raise(comp->compile_error); } - - return cm; } mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl) { - mp_module_context_t *context = m_new_obj(mp_module_context_t); - context->module.globals = mp_globals_get(); - mp_compiled_module_t cm = mp_compile_to_raw_code(parse_tree, source_file, is_repl, context); + mp_compiled_module_t cm; + cm.context = m_new_obj(mp_module_context_t); + cm.context->module.globals = mp_globals_get(); + mp_compile_to_raw_code(parse_tree, source_file, is_repl, &cm); // return function that executes the outer module return mp_make_function_from_raw_code(cm.rc, cm.context, NULL); } diff --git a/py/compile.h b/py/compile.h index ae87bf2a0..5e0fd8b31 100644 --- a/py/compile.h +++ b/py/compile.h @@ -37,7 +37,7 @@ mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl) #if MICROPY_PERSISTENT_CODE_SAVE // this has the same semantics as mp_compile -mp_compiled_module_t mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl, mp_module_context_t *globals); +void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl, mp_compiled_module_t *cm); #endif // this is implemented in runtime.c diff --git a/py/persistentcode.c b/py/persistentcode.c index 5408f756c..fdc87d5cc 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -390,7 +390,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, mp_module_context_t *co return rc; } -mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t *context) { +void mp_raw_code_load(mp_reader_t *reader, mp_compiled_module_t *cm) { byte header[4]; read_bytes(reader, header, sizeof(header)); byte arch = MPY_FEATURE_DECODE_ARCH(header[2]); @@ -414,46 +414,42 @@ mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t * size_t n_qstr = read_uint(reader); size_t n_obj = read_uint(reader); - mp_module_context_alloc_tables(context, n_qstr, n_obj); + mp_module_context_alloc_tables(cm->context, n_qstr, n_obj); // Load qstrs. for (size_t i = 0; i < n_qstr; ++i) { - context->constants.qstr_table[i] = load_qstr(reader); + cm->context->constants.qstr_table[i] = load_qstr(reader); } // Load constant objects. for (size_t i = 0; i < n_obj; ++i) { - context->constants.obj_table[i] = load_obj(reader); + cm->context->constants.obj_table[i] = load_obj(reader); } // Load top-level module. - mp_compiled_module_t cm2; - cm2.rc = load_raw_code(reader, context); - cm2.context = context; + cm->rc = load_raw_code(reader, cm->context); #if MICROPY_PERSISTENT_CODE_SAVE - cm2.has_native = MPY_FEATURE_DECODE_ARCH(header[2]) != MP_NATIVE_ARCH_NONE; - cm2.n_qstr = n_qstr; - cm2.n_obj = n_obj; + cm->has_native = MPY_FEATURE_DECODE_ARCH(header[2]) != MP_NATIVE_ARCH_NONE; + cm->n_qstr = n_qstr; + cm->n_obj = n_obj; #endif reader->close(reader->data); - - return cm2; } -mp_compiled_module_t mp_raw_code_load_mem(const byte *buf, size_t len, mp_module_context_t *context) { +void mp_raw_code_load_mem(const byte *buf, size_t len, mp_compiled_module_t *context) { mp_reader_t reader; mp_reader_new_mem(&reader, buf, len, 0); - return mp_raw_code_load(&reader, context); + mp_raw_code_load(&reader, context); } #if MICROPY_HAS_FILE_READER -mp_compiled_module_t mp_raw_code_load_file(const char *filename, mp_module_context_t *context) { +void mp_raw_code_load_file(const char *filename, mp_compiled_module_t *context) { mp_reader_t reader; mp_reader_new_file(&reader, filename); - return mp_raw_code_load(&reader, context); + mp_raw_code_load(&reader, context); } #endif // MICROPY_HAS_FILE_READER diff --git a/py/persistentcode.h b/py/persistentcode.h index e66435873..d363f544a 100644 --- a/py/persistentcode.h +++ b/py/persistentcode.h @@ -111,9 +111,9 @@ enum { MP_PERSISTENT_OBJ_TUPLE, }; -mp_compiled_module_t mp_raw_code_load(mp_reader_t *reader, mp_module_context_t *ctx); -mp_compiled_module_t mp_raw_code_load_mem(const byte *buf, size_t len, mp_module_context_t *ctx); -mp_compiled_module_t mp_raw_code_load_file(const char *filename, mp_module_context_t *ctx); +void mp_raw_code_load(mp_reader_t *reader, mp_compiled_module_t *ctx); +void mp_raw_code_load_mem(const byte *buf, size_t len, mp_compiled_module_t *ctx); +void mp_raw_code_load_file(const char *filename, mp_compiled_module_t *ctx); void mp_raw_code_save(mp_compiled_module_t *cm, mp_print_t *print); void mp_raw_code_save_file(mp_compiled_module_t *cm, const char *filename);