Skip to content

Commit def6ad4

Browse files
committed
py/emitglue: Include fun_data_len in mp_raw_code_t only when saving.
Reduces the size of mp_raw_code_t in the case when MICROPY_DEBUG_PRINTERS is enabled. Signed-off-by: Damien George <[email protected]>
1 parent 5a3dd8c commit def6ad4

9 files changed

+27
-33
lines changed

py/bc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state,
281281
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);
282282
void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args);
283283
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);
284-
void mp_bytecode_print(const mp_print_t *print, const struct _mp_raw_code_t *rc, const mp_module_constants_t *cm);
284+
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);
285285
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);
286286
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);
287287
#define mp_bytecode_print_inst(print, code, x_table) mp_bytecode_print2(print, code, 1, x_table)

py/compile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3625,7 +3625,7 @@ void mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool
36253625
for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
36263626
mp_raw_code_t *rc = s->raw_code;
36273627
if (rc->kind == MP_CODE_BYTECODE) {
3628-
mp_bytecode_print(&mp_plat_print, rc, &cm->context->constants);
3628+
mp_bytecode_print(&mp_plat_print, rc, s->raw_code_data_len, &cm->context->constants);
36293629
}
36303630
}
36313631
}

py/emitbc.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,18 @@ bool mp_emit_bc_end_pass(emit_t *emit) {
393393
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("bytecode overflow"));
394394
}
395395

396+
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
397+
size_t bytecode_len = emit->code_info_size + emit->bytecode_size;
398+
#if MICROPY_DEBUG_PRINTERS
399+
emit->scope->raw_code_data_len = bytecode_len;
400+
#endif
401+
#endif
402+
396403
// Bytecode is finalised, assign it to the raw code object.
397404
mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
398-
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
399-
emit->code_info_size + emit->bytecode_size,
400-
#endif
401405
emit->emit_common->children,
402406
#if MICROPY_PERSISTENT_CODE_SAVE
407+
bytecode_len,
403408
emit->emit_common->ct_cur_child,
404409
#endif
405410
emit->scope->scope_flags);

py/emitglue.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,20 @@ mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
6161
}
6262

6363
void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
64-
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
65-
size_t len,
66-
#endif
6764
mp_raw_code_t **children,
6865
#if MICROPY_PERSISTENT_CODE_SAVE
66+
size_t len,
6967
uint16_t n_children,
7068
#endif
7169
uint16_t scope_flags) {
7270

7371
rc->kind = MP_CODE_BYTECODE;
7472
rc->is_generator = (scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0;
7573
rc->fun_data = code;
76-
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
77-
rc->fun_data_len = len;
78-
#endif
7974
rc->children = children;
8075

8176
#if MICROPY_PERSISTENT_CODE_SAVE
77+
rc->fun_data_len = len;
8278
rc->n_children = n_children;
8379
#endif
8480

@@ -88,7 +84,7 @@ void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
8884
#endif
8985

9086
#if DEBUG_PRINT
91-
#if !(MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS)
87+
#if !MICROPY_PERSISTENT_CODE_SAVE
9288
const size_t len = 0;
9389
#endif
9490
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
136132
rc->is_generator = (scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0;
137133
rc->fun_data = fun_data;
138134

139-
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
135+
#if MICROPY_PERSISTENT_CODE_SAVE
140136
rc->fun_data_len = fun_len;
141137
#endif
142138
rc->children = children;

py/emitglue.h

+3-9
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@ typedef struct _mp_raw_code_t {
7777
bool is_generator;
7878
const void *fun_data;
7979
struct _mp_raw_code_t **children;
80-
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
81-
uint32_t fun_data_len; // so mp_raw_code_save and mp_bytecode_print work
82-
#endif
8380
#if MICROPY_PERSISTENT_CODE_SAVE
81+
uint32_t fun_data_len; // for mp_raw_code_save
8482
uint16_t n_children;
8583
#if MICROPY_EMIT_MACHINE_CODE
8684
uint16_t prelude_offset;
@@ -109,10 +107,8 @@ typedef struct _mp_raw_code_truncated_t {
109107
bool is_generator;
110108
const void *fun_data;
111109
struct _mp_raw_code_t **children;
112-
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
113-
uint32_t fun_data_len;
114-
#endif
115110
#if MICROPY_PERSISTENT_CODE_SAVE
111+
uint32_t fun_data_len;
116112
uint16_t n_children;
117113
#if MICROPY_EMIT_MACHINE_CODE
118114
uint16_t prelude_offset;
@@ -127,11 +123,9 @@ typedef struct _mp_raw_code_truncated_t {
127123
mp_raw_code_t *mp_emit_glue_new_raw_code(void);
128124

129125
void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
130-
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
131-
size_t len,
132-
#endif
133126
mp_raw_code_t **children,
134127
#if MICROPY_PERSISTENT_CODE_SAVE
128+
size_t len,
135129
uint16_t n_children,
136130
#endif
137131
uint16_t scope_flags);

py/persistentcode.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,9 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, mp_module_context_t *co
324324
MP_BC_PRELUDE_SIG_DECODE(ip);
325325
// Assign bytecode to raw code object
326326
mp_emit_glue_assign_bytecode(rc, fun_data,
327-
#if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
328-
fun_data_len,
329-
#endif
330327
children,
331328
#if MICROPY_PERSISTENT_CODE_SAVE
329+
fun_data_len,
332330
n_children,
333331
#endif
334332
scope_flags);

py/scope.h

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ typedef struct _scope_t {
7676
struct _scope_t *next;
7777
mp_parse_node_t pn;
7878
mp_raw_code_t *raw_code;
79+
#if MICROPY_DEBUG_PRINTERS
80+
size_t raw_code_data_len; // for mp_bytecode_print
81+
#endif
7982
uint16_t simple_name; // a qstr
8083
uint16_t scope_flags; // see runtime0.h
8184
uint16_t emit_options; // see emitglue.h

py/showbc.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
DECODE_UINT; \
8484
unum = (mp_uint_t)obj_table[unum]
8585

86-
void mp_bytecode_print(const mp_print_t *print, const mp_raw_code_t *rc, const mp_module_constants_t *cm) {
86+
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) {
8787
const byte *ip_start = rc->fun_data;
8888
const byte *ip = rc->fun_data;
8989

@@ -100,13 +100,13 @@ void mp_bytecode_print(const mp_print_t *print, const mp_raw_code_t *rc, const m
100100
qstr source_file = cm->source_file;
101101
#endif
102102
mp_printf(print, "File %s, code block '%s' (descriptor: %p, bytecode @%p %u bytes)\n",
103-
qstr_str(source_file), qstr_str(block_name), rc, ip_start, (unsigned)rc->fun_data_len);
103+
qstr_str(source_file), qstr_str(block_name), rc, ip_start, (unsigned)fun_data_len);
104104

105105
// raw bytecode dump
106106
size_t prelude_size = ip - ip_start + n_info + n_cell;
107107
mp_printf(print, "Raw bytecode (code_info_size=%u, bytecode_size=%u):\n",
108-
(unsigned)prelude_size, (unsigned)(rc->fun_data_len - prelude_size));
109-
for (size_t i = 0; i < rc->fun_data_len; i++) {
108+
(unsigned)prelude_size, (unsigned)(fun_data_len - prelude_size));
109+
for (size_t i = 0; i < fun_data_len; i++) {
110110
if (i > 0 && i % 16 == 0) {
111111
mp_printf(print, "\n");
112112
}
@@ -158,7 +158,7 @@ void mp_bytecode_print(const mp_print_t *print, const mp_raw_code_t *rc, const m
158158
mp_printf(print, " bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
159159
}
160160
}
161-
mp_bytecode_print2(print, ip, rc->fun_data_len - prelude_size, rc->children, cm);
161+
mp_bytecode_print2(print, ip, fun_data_len - prelude_size, rc->children, cm);
162162
}
163163

164164
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) {

tools/mpy-tool.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,8 @@ def freeze_raw_code(self, prelude_ptr=None, type_sig=0):
930930
print(" .children = (void *)%s," % prelude_ptr)
931931
else:
932932
print(" .children = NULL,")
933-
print(" #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS")
934-
print(" .fun_data_len = %u," % len(self.fun_data))
935-
print(" #endif")
936933
print(" #if MICROPY_PERSISTENT_CODE_SAVE")
934+
print(" .fun_data_len = %u," % len(self.fun_data))
937935
print(" .n_children = %u," % len(self.children))
938936
print(" #if MICROPY_EMIT_MACHINE_CODE")
939937
print(" .prelude_offset = %u," % self.prelude_offset)

0 commit comments

Comments
 (0)