diff --git a/py/bc.h b/py/bc.h index a1ab7918f2..718ba4a684 100644 --- a/py/bc.h +++ b/py/bc.h @@ -281,7 +281,7 @@ mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state, mp_code_state_t *mp_obj_fun_bc_prepare_codestate(mp_obj_t func, size_t n_args, size_t n_kw, const mp_obj_t *args); void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args); void mp_setup_code_state_native(mp_code_state_native_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args); -void mp_bytecode_print(const mp_print_t *print, const struct _mp_raw_code_t *rc, const mp_module_constants_t *cm); +void mp_bytecode_print(const mp_print_t *print, const struct _mp_raw_code_t *rc, size_t fun_data_len, const mp_module_constants_t *cm); void mp_bytecode_print2(const mp_print_t *print, const byte *ip, size_t len, struct _mp_raw_code_t *const *child_table, const mp_module_constants_t *cm); const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start, const byte *ip, struct _mp_raw_code_t *const *child_table, const mp_module_constants_t *cm); #define mp_bytecode_print_inst(print, code, x_table) mp_bytecode_print2(print, code, 1, x_table) diff --git a/py/compile.c b/py/compile.c index ad8cffc9d5..8246a9241c 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3625,7 +3625,7 @@ void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool 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, s->raw_code_data_len, &cm->context->constants); } } } diff --git a/py/emitbc.c b/py/emitbc.c index a07657408f..ca5f07085b 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -393,13 +393,18 @@ bool mp_emit_bc_end_pass(emit_t *emit) { mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("bytecode overflow")); } + #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS + size_t bytecode_len = emit->code_info_size + emit->bytecode_size; + #if MICROPY_DEBUG_PRINTERS + emit->scope->raw_code_data_len = bytecode_len; + #endif + #endif + // Bytecode is finalised, assign it to the raw code object. mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base, - #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS - emit->code_info_size + emit->bytecode_size, - #endif emit->emit_common->children, #if MICROPY_PERSISTENT_CODE_SAVE + bytecode_len, emit->emit_common->ct_cur_child, #endif emit->scope->scope_flags); diff --git a/py/emitglue.c b/py/emitglue.c index 9d671b710e..7dc3080870 100644 --- a/py/emitglue.c +++ b/py/emitglue.c @@ -61,11 +61,9 @@ mp_raw_code_t *mp_emit_glue_new_raw_code(void) { } void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, - #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS - size_t len, - #endif mp_raw_code_t **children, #if MICROPY_PERSISTENT_CODE_SAVE + size_t len, uint16_t n_children, #endif uint16_t scope_flags) { @@ -73,12 +71,10 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, rc->kind = MP_CODE_BYTECODE; rc->is_generator = (scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0; rc->fun_data = code; - #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS - rc->fun_data_len = len; - #endif rc->children = children; #if MICROPY_PERSISTENT_CODE_SAVE + rc->fun_data_len = len; rc->n_children = n_children; #endif @@ -88,7 +84,7 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, #endif #if DEBUG_PRINT - #if !(MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS) + #if !MICROPY_PERSISTENT_CODE_SAVE const size_t len = 0; #endif DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " flags=%x\n", code, len, (uint)scope_flags); @@ -136,7 +132,7 @@ void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void rc->is_generator = (scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0; rc->fun_data = fun_data; - #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS + #if MICROPY_PERSISTENT_CODE_SAVE rc->fun_data_len = fun_len; #endif rc->children = children; diff --git a/py/emitglue.h b/py/emitglue.h index b1c10a1ee4..b7368d697d 100644 --- a/py/emitglue.h +++ b/py/emitglue.h @@ -77,10 +77,8 @@ typedef struct _mp_raw_code_t { bool is_generator; const void *fun_data; struct _mp_raw_code_t **children; - #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS - uint32_t fun_data_len; // so mp_raw_code_save and mp_bytecode_print work - #endif #if MICROPY_PERSISTENT_CODE_SAVE + uint32_t fun_data_len; // for mp_raw_code_save uint16_t n_children; #if MICROPY_EMIT_MACHINE_CODE uint16_t prelude_offset; @@ -109,10 +107,8 @@ typedef struct _mp_raw_code_truncated_t { bool is_generator; const void *fun_data; struct _mp_raw_code_t **children; - #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS - uint32_t fun_data_len; - #endif #if MICROPY_PERSISTENT_CODE_SAVE + uint32_t fun_data_len; uint16_t n_children; #if MICROPY_EMIT_MACHINE_CODE uint16_t prelude_offset; @@ -127,11 +123,9 @@ typedef struct _mp_raw_code_truncated_t { mp_raw_code_t *mp_emit_glue_new_raw_code(void); void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, - #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS - size_t len, - #endif mp_raw_code_t **children, #if MICROPY_PERSISTENT_CODE_SAVE + size_t len, uint16_t n_children, #endif uint16_t scope_flags); diff --git a/py/persistentcode.c b/py/persistentcode.c index 7b002e7fcf..1fe9a9a23f 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -324,11 +324,9 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, mp_module_context_t *co MP_BC_PRELUDE_SIG_DECODE(ip); // Assign bytecode to raw code object mp_emit_glue_assign_bytecode(rc, fun_data, - #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS - fun_data_len, - #endif children, #if MICROPY_PERSISTENT_CODE_SAVE + fun_data_len, n_children, #endif scope_flags); diff --git a/py/scope.h b/py/scope.h index e7d2a304f7..927c4a7b93 100644 --- a/py/scope.h +++ b/py/scope.h @@ -76,6 +76,9 @@ typedef struct _scope_t { struct _scope_t *next; mp_parse_node_t pn; mp_raw_code_t *raw_code; + #if MICROPY_DEBUG_PRINTERS + size_t raw_code_data_len; // for mp_bytecode_print + #endif uint16_t simple_name; // a qstr uint16_t scope_flags; // see runtime0.h uint16_t emit_options; // see emitglue.h diff --git a/py/showbc.c b/py/showbc.c index f9c334b93b..6913d18c1c 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -83,7 +83,7 @@ DECODE_UINT; \ unum = (mp_uint_t)obj_table[unum] -void mp_bytecode_print(const mp_print_t *print, const mp_raw_code_t *rc, const mp_module_constants_t *cm) { +void mp_bytecode_print(const mp_print_t *print, const mp_raw_code_t *rc, size_t fun_data_len, const mp_module_constants_t *cm) { const byte *ip_start = rc->fun_data; const byte *ip = rc->fun_data; @@ -100,13 +100,13 @@ void mp_bytecode_print(const mp_print_t *print, const mp_raw_code_t *rc, const m qstr source_file = cm->source_file; #endif mp_printf(print, "File %s, code block '%s' (descriptor: %p, bytecode @%p %u bytes)\n", - qstr_str(source_file), qstr_str(block_name), rc, ip_start, (unsigned)rc->fun_data_len); + qstr_str(source_file), qstr_str(block_name), rc, ip_start, (unsigned)fun_data_len); // raw bytecode dump size_t prelude_size = ip - ip_start + n_info + n_cell; mp_printf(print, "Raw bytecode (code_info_size=%u, bytecode_size=%u):\n", - (unsigned)prelude_size, (unsigned)(rc->fun_data_len - prelude_size)); - for (size_t i = 0; i < rc->fun_data_len; i++) { + (unsigned)prelude_size, (unsigned)(fun_data_len - prelude_size)); + for (size_t i = 0; i < fun_data_len; i++) { if (i > 0 && i % 16 == 0) { mp_printf(print, "\n"); } @@ -158,7 +158,7 @@ void mp_bytecode_print(const mp_print_t *print, const mp_raw_code_t *rc, const m mp_printf(print, " bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line); } } - mp_bytecode_print2(print, ip, rc->fun_data_len - prelude_size, rc->children, cm); + mp_bytecode_print2(print, ip, fun_data_len - prelude_size, rc->children, cm); } const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start, const byte *ip, mp_raw_code_t *const *child_table, const mp_module_constants_t *cm) { diff --git a/tools/mpy-tool.py b/tools/mpy-tool.py index 38567b37e5..35c06a3028 100755 --- a/tools/mpy-tool.py +++ b/tools/mpy-tool.py @@ -930,10 +930,8 @@ class RawCode(object): print(" .children = (void *)%s," % prelude_ptr) else: print(" .children = NULL,") - print(" #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS") - print(" .fun_data_len = %u," % len(self.fun_data)) - print(" #endif") print(" #if MICROPY_PERSISTENT_CODE_SAVE") + print(" .fun_data_len = %u," % len(self.fun_data)) print(" .n_children = %u," % len(self.children)) print(" #if MICROPY_EMIT_MACHINE_CODE") print(" .prelude_offset = %u," % self.prelude_offset)