aboutsummaryrefslogtreecommitdiff
path: root/py/showbc.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-09-25 15:45:47 +1000
committerDamien George <damien.p.george@gmail.com>2019-10-01 12:26:22 +1000
commitc8c0fd4ca39fbdcf9ca5f2fc99ca4d6b81a28b65 (patch)
tree0fcbf3a263817be928c0b7db847d94293c79cce5 /py/showbc.c
parentb5ebfadbd615de42c43851f27a062bacd9147996 (diff)
py: Rework and compress second part of bytecode prelude.
This patch compresses the second part of the bytecode prelude which contains the source file name, function name, source-line-number mapping and cell closure information. This part of the prelude now begins with a single varible length unsigned integer which encodes 2 numbers, being the byte-size of the following 2 sections in the header: the "source info section" and the "closure section". After decoding this variable unsigned integer it's possible to skip over one or both of these sections very easily. This scheme saves about 2 bytes for most functions compared to the original format: one in the case that there are no closure cells, and one because padding was eliminated.
Diffstat (limited to 'py/showbc.c')
-rw-r--r--py/showbc.c25
1 files changed, 11 insertions, 14 deletions
diff --git a/py/showbc.c b/py/showbc.c
index 216f35266..d154511dc 100644
--- a/py/showbc.c
+++ b/py/showbc.c
@@ -85,10 +85,8 @@ void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const m
// Decode prelude
MP_BC_PRELUDE_SIG_DECODE(ip);
-
+ MP_BC_PRELUDE_SIZE_DECODE(ip);
const byte *code_info = ip;
- mp_uint_t code_info_size = mp_decode_uint(&code_info);
- ip += code_info_size;
#if MICROPY_PERSISTENT_CODE
qstr block_name = code_info[0] | (code_info[1] << 8);
@@ -102,7 +100,9 @@ void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const m
qstr_str(source_file), qstr_str(block_name), descr, mp_showbc_code_start, len);
// raw bytecode dump
- printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n", code_info_size, len - code_info_size);
+ size_t prelude_size = ip - mp_showbc_code_start + n_info + n_cell;
+ printf("Raw bytecode (code_info_size=" UINT_FMT ", bytecode_size=" UINT_FMT "):\n",
+ prelude_size, len - prelude_size);
for (mp_uint_t i = 0; i < len; i++) {
if (i > 0 && i % 16 == 0) {
printf("\n");
@@ -121,21 +121,18 @@ void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const m
printf("(N_STATE %u)\n", (unsigned)n_state);
printf("(N_EXC_STACK %u)\n", (unsigned)n_exc_stack);
- // for printing line number info
- const byte *bytecode_start = ip;
+ // skip over code_info
+ ip += n_info;
// bytecode prelude: initialise closed over variables
- {
- uint local_num;
- while ((local_num = *ip++) != 255) {
- printf("(INIT_CELL %u)\n", local_num);
- }
- len -= ip - mp_showbc_code_start;
+ for (size_t i = 0; i < n_cell; ++i) {
+ uint local_num = *ip++;
+ printf("(INIT_CELL %u)\n", local_num);
}
// print out line number info
{
- mp_int_t bc = bytecode_start - ip;
+ mp_int_t bc = 0;
mp_uint_t source_line = 1;
printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
for (const byte* ci = code_info; *ci;) {
@@ -153,7 +150,7 @@ void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len, const m
printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line);
}
}
- mp_bytecode_print2(ip, len - 0, const_table);
+ mp_bytecode_print2(ip, len - prelude_size, const_table);
}
const byte *mp_bytecode_print_str(const byte *ip) {