summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-25 11:43:20 +0000
committerDamien George <damien.p.george@gmail.com>2014-01-25 11:43:20 +0000
commit28eb57786d789cf0dc20390b7afb86fc09d1b0f3 (patch)
tree533f31a0e49d220dd98d8711824fc67da657cb57
parent449dd0a69ecee497c5a7da1da23d8ff53778b363 (diff)
downloadmicropython-28eb57786d789cf0dc20390b7afb86fc09d1b0f3.tar.gz
micropython-28eb57786d789cf0dc20390b7afb86fc09d1b0f3.zip
py: Optimise generated code for working out line numbers.
-rw-r--r--py/emitbc.c26
-rw-r--r--py/vm.c9
2 files changed, 15 insertions, 20 deletions
diff --git a/py/emitbc.c b/py/emitbc.c
index 10a95fbcfa..9fa2880ecb 100644
--- a/py/emitbc.c
+++ b/py/emitbc.c
@@ -71,10 +71,14 @@ static void emit_write_code_info_qstr(emit_t* emit, qstr qstr) {
c[3] = (qstr >> 24) & 0xff;
}
-static void emit_write_code_info_byte_byte(emit_t* emit, byte b1, uint b2) {
- byte* c = emit_get_cur_to_write_code_info(emit, 2);
- c[0] = b1;
- c[1] = b2;
+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;
+ }
+ *emit_get_cur_to_write_code_info(emit, 1) = bytes_to_skip | (lines_to_skip << 5);
}
// all functions must go through this one to emit byte code
@@ -218,7 +222,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_byte_byte(emit, 0, 0); // end of line number info
+ emit_write_code_info_bytes_lines(emit, 0, 0); // end of line number info
if (emit->pass == PASS_2) {
// calculate size of code in bytes
@@ -246,15 +250,9 @@ static void emit_bc_set_stack_size(emit_t *emit, int size) {
static void emit_bc_set_source_line(emit_t *emit, int source_line) {
//printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->byte_code_offset);
if (source_line > emit->last_source_line) {
- int bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
- for (; bytes_to_skip > 255; bytes_to_skip -= 255) {
- emit_write_code_info_byte_byte(emit, 255, 0);
- }
- int lines_to_skip = source_line - emit->last_source_line;
- for (; lines_to_skip > 255; lines_to_skip -= 255) {
- emit_write_code_info_byte_byte(emit, 0, 255);
- }
- emit_write_code_info_byte_byte(emit, bytes_to_skip, lines_to_skip);
+ uint bytes_to_skip = emit->byte_code_offset - emit->last_source_line_offset;
+ uint lines_to_skip = source_line - emit->last_source_line;
+ emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
//printf(" %d %d\n", bytes_to_skip, lines_to_skip);
emit->last_source_line_offset = emit->byte_code_offset;
emit->last_source_line = source_line;
diff --git a/py/vm.c b/py/vm.c
index affa5943bd..82a9f893f3 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -550,12 +550,9 @@ bool mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_ob
machine_uint_t source_line = 1;
machine_uint_t bc = save_ip - code_info - code_info_size;
//printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
- for (const byte* ci = code_info + 12; bc >= ci[0]; ci += 2) {
- bc -= ci[0];
- source_line += ci[1];
- if (ci[0] == 0 && ci[1] == 0) {
- break;
- }
+ for (const byte* ci = code_info + 12; *ci && bc >= ((*ci) & 31); ci++) {
+ bc -= *ci & 31;
+ source_line += *ci >> 5;
}
mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
}