py/bc: Factor out code to get bytecode line number info into new func.

This commit is contained in:
Damien George 2019-08-30 16:41:08 +10:00
parent c7c6703950
commit dbf35d3da3
2 changed files with 28 additions and 23 deletions

27
py/bc.h
View File

@ -119,4 +119,31 @@ uint mp_opcode_format(const byte *ip, size_t *opcode_size, bool count_var_uint);
#endif
static inline size_t mp_bytecode_get_source_line(const byte *line_info, size_t bc_offset) {
size_t source_line = 1;
size_t c;
while ((c = *line_info)) {
size_t b, l;
if ((c & 0x80) == 0) {
// 0b0LLBBBBB encoding
b = c & 0x1f;
l = c >> 5;
line_info += 1;
} else {
// 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
b = c & 0xf;
l = ((c << 4) & 0x700) | line_info[1];
line_info += 2;
}
if (bc_offset >= b) {
bc_offset -= b;
source_line += l;
} else {
// found source line corresponding to bytecode offset
break;
}
}
return source_line;
}
#endif // MICROPY_INCLUDED_PY_BC_H

24
py/vm.c
View File

@ -1387,29 +1387,7 @@ unwind_loop:
qstr source_file = mp_decode_uint_value(ip);
ip = mp_decode_uint_skip(ip);
#endif
size_t source_line = 1;
size_t c;
while ((c = *ip)) {
size_t b, l;
if ((c & 0x80) == 0) {
// 0b0LLBBBBB encoding
b = c & 0x1f;
l = c >> 5;
ip += 1;
} else {
// 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
b = c & 0xf;
l = ((c << 4) & 0x700) | ip[1];
ip += 2;
}
if (bc >= b) {
bc -= b;
source_line += l;
} else {
// found source line corresponding to bytecode offset
break;
}
}
size_t source_line = mp_bytecode_get_source_line(ip, bc);
mp_obj_exception_add_traceback(MP_OBJ_FROM_PTR(nlr.ret_val), source_file, source_line, block_name);
}