summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-13 14:51:56 +0100
committerDamien George <damien.p.george@gmail.com>2014-04-13 14:51:56 +0100
commit73496fbbe4b787a5b4255d6ae98c20255878e012 (patch)
tree4a58715b37c77e3dc951e8930d7defbc01334c39
parentb636d024d2fe950d8dd643c74aa006d1ea88078d (diff)
py: Fix up source-line calculation.
Should address issue #475.
-rw-r--r--py/emitbc.c17
-rw-r--r--py/showbc.c16
-rw-r--r--stmhal/mpconfigport.h1
3 files changed, 27 insertions, 7 deletions
diff --git a/py/emitbc.c b/py/emitbc.c
index 2d71d8685..4dad61bb2 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;
- }
- for (; lines_to_skip > 7; lines_to_skip -= 7) {
- *emit_get_cur_to_write_code_info(emit, 1) = 7 << 5;
+ 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);
}
- *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 25b1b2ffb..d755d3c96 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 552910142..6a3182346 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)