aboutsummaryrefslogtreecommitdiff
path: root/py/emitnative.c
diff options
context:
space:
mode:
authorDamien George2019-09-25 15:45:47 +1000
committerDamien George2019-10-01 12:26:22 +1000
commitc8c0fd4ca39fbdcf9ca5f2fc99ca4d6b81a28b65 (patch)
tree0fcbf3a263817be928c0b7db847d94293c79cce5 /py/emitnative.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/emitnative.c')
-rw-r--r--py/emitnative.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/py/emitnative.c b/py/emitnative.c
index d0e758f56..2c976606c 100644
--- a/py/emitnative.c
+++ b/py/emitnative.c
@@ -208,6 +208,7 @@ struct _emit_t {
uint16_t code_state_start;
uint16_t stack_start;
int stack_size;
+ uint16_t n_cell;
uint16_t const_table_cur_obj;
uint16_t const_table_num_obj;
@@ -587,9 +588,14 @@ STATIC void emit_native_end_pass(emit_t *emit) {
size_t n_exc_stack = 0; // exc-stack not needed for native code
MP_BC_PRELUDE_SIG_ENCODE(n_state, n_exc_stack, emit->scope, emit_native_write_code_info_byte, emit);
- // write code info
#if MICROPY_PERSISTENT_CODE
- mp_asm_base_data(&emit->as->base, 1, 5);
+ size_t n_info = 4;
+ #else
+ size_t n_info = 1;
+ #endif
+ MP_BC_PRELUDE_SIZE_ENCODE(n_info, emit->n_cell, emit_native_write_code_info_byte, emit);
+
+ #if MICROPY_PERSISTENT_CODE
mp_asm_base_data(&emit->as->base, 1, emit->scope->simple_name);
mp_asm_base_data(&emit->as->base, 1, emit->scope->simple_name >> 8);
mp_asm_base_data(&emit->as->base, 1, emit->scope->source_file);
@@ -599,14 +605,15 @@ STATIC void emit_native_end_pass(emit_t *emit) {
#endif
// bytecode prelude: initialise closed over variables
+ size_t cell_start = mp_asm_base_get_code_pos(&emit->as->base);
for (int i = 0; i < emit->scope->id_info_len; i++) {
id_info_t *id = &emit->scope->id_info[i];
if (id->kind == ID_INFO_KIND_CELL) {
- assert(id->local_num < 255);
+ assert(id->local_num <= 255);
mp_asm_base_data(&emit->as->base, 1, id->local_num); // write the local which should be converted to a cell
}
}
- mp_asm_base_data(&emit->as->base, 1, 255); // end of list sentinel
+ emit->n_cell = mp_asm_base_get_code_pos(&emit->as->base) - cell_start;
}
ASM_END_PASS(emit->as);