From 73496fbbe4b787a5b4255d6ae98c20255878e012 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 13 Apr 2014 14:51:56 +0100 Subject: [PATCH] py: Fix up source-line calculation. Should address issue #475. --- py/emitbc.c | 17 ++++++++++------- py/showbc.c | 16 ++++++++++++++++ stmhal/mpconfigport.h | 1 + 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/py/emitbc.c b/py/emitbc.c index 2d71d86855..4dad61bb21 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -79,15 +79,18 @@ STATIC void emit_write_code_info_qstr(emit_t* emit, qstr qstr) { c[3] = (qstr >> 24) & 0xff; } +#if MICROPY_ENABLE_SOURCE_LINE STATIC void emit_write_code_info_bytes_lines(emit_t* emit, uint bytes_to_skip, uint lines_to_skip) { - for (; bytes_to_skip > 31; bytes_to_skip -= 31) { - *emit_get_cur_to_write_code_info(emit, 1) = 31; + assert(bytes_to_skip > 0 || lines_to_skip > 0); + while (bytes_to_skip > 0 || lines_to_skip > 0) { + uint b = MIN(bytes_to_skip, 31); + uint l = MIN(lines_to_skip, 7); + bytes_to_skip -= b; + lines_to_skip -= l; + *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5); } - for (; lines_to_skip > 7; lines_to_skip -= 7) { - *emit_get_cur_to_write_code_info(emit, 1) = 7 << 5; - } - *emit_get_cur_to_write_code_info(emit, 1) = bytes_to_skip | (lines_to_skip << 5); } +#endif // all functions must go through this one to emit byte code STATIC byte* emit_get_cur_to_write_byte_code(emit_t* emit, int num_bytes_to_write) { @@ -285,7 +288,7 @@ STATIC void emit_bc_end_pass(emit_t *emit) { printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size); } - emit_write_code_info_bytes_lines(emit, 0, 0); // end of line number info + *emit_get_cur_to_write_code_info(emit, 1) = 0; // end of line number info emit_align_code_info_to_machine_word(emit); // align so that following byte_code is aligned if (emit->pass == PASS_2) { diff --git a/py/showbc.c b/py/showbc.c index 25b1b2ffb6..d755d3c96c 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -33,6 +33,7 @@ void mp_byte_code_print(const byte *ip, int len) { // get code info size machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24); + const byte *code_info = ip; ip += code_info_size; // bytecode prelude: state size and exception stack size; 16 bit uints @@ -56,6 +57,21 @@ void mp_byte_code_print(const byte *ip, int len) { ip_start = ip; } + // print out line number info + { + qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24); + qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24); + printf("File %s, block %s\n", qstr_str(source_file), qstr_str(block_name)); + machine_int_t bc = (code_info + code_info_size) - ip; + machine_uint_t source_line = 1; + printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line); + for (const byte* ci = code_info + 12; *ci; ci++) { + bc += *ci & 31; + source_line += *ci >> 5; + printf(" bc=" INT_FMT " line=" UINT_FMT "\n", bc, source_line); + } + } + machine_uint_t unum; qstr qstr; while (ip - ip_start < len) { diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 5529101425..6a3182346c 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -7,6 +7,7 @@ #define MICROPY_ENABLE_GC (1) #define MICROPY_ENABLE_FINALISER (1) #define MICROPY_ENABLE_REPL_HELPERS (1) +#define MICROPY_ENABLE_SOURCE_LINE (1) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_PATH_MAX (128)