Skip to content

Commit

Permalink
py/emitglue: Include fun_data_len in mp_raw_code_t only when saving.
Browse files Browse the repository at this point in the history
Reduces the size of mp_raw_code_t in the case when MICROPY_DEBUG_PRINTERS
is enabled.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Feb 16, 2024
1 parent 5a3dd8c commit def6ad4
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion py/bc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion py/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions py/emitbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 4 additions & 8 deletions py/emitglue.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,20 @@ 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) {

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

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 3 additions & 9 deletions py/emitglue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions py/persistentcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions py/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions py/showbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
}
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions tools/mpy-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,8 @@ def freeze_raw_code(self, prelude_ptr=None, type_sig=0):
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)
Expand Down

0 comments on commit def6ad4

Please sign in to comment.