py: Simplify bytecode prelude when encoding closed over variables.
This commit is contained in:
parent
78772ada0d
commit
c9aa1883ed
7
py/bc.c
7
py/bc.c
@ -228,9 +228,10 @@ continue2:;
|
|||||||
|
|
||||||
// bytecode prelude: initialise closed over variables
|
// bytecode prelude: initialise closed over variables
|
||||||
const byte *ip = code_state->ip;
|
const byte *ip = code_state->ip;
|
||||||
for (mp_uint_t n_local = *ip++; n_local > 0; n_local--) {
|
mp_uint_t local_num;
|
||||||
mp_uint_t local_num = *ip++;
|
while ((local_num = *ip++) != 255) {
|
||||||
code_state->state[n_state - 1 - local_num] = mp_obj_new_cell(code_state->state[n_state - 1 - local_num]);
|
code_state->state[n_state - 1 - local_num] =
|
||||||
|
mp_obj_new_cell(code_state->state[n_state - 1 - local_num]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// now that we skipped over the prelude, set the ip for the VM
|
// now that we skipped over the prelude, set the ip for the VM
|
||||||
|
11
py/emitbc.c
11
py/emitbc.c
@ -322,21 +322,14 @@ void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// bytecode prelude: initialise closed over variables
|
// bytecode prelude: initialise closed over variables
|
||||||
int num_cell = 0;
|
|
||||||
for (int i = 0; i < scope->id_info_len; i++) {
|
|
||||||
id_info_t *id = &scope->id_info[i];
|
|
||||||
if (id->kind == ID_INFO_KIND_CELL) {
|
|
||||||
num_cell += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert(num_cell <= 255);
|
|
||||||
emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
|
|
||||||
for (int i = 0; i < scope->id_info_len; i++) {
|
for (int i = 0; i < scope->id_info_len; i++) {
|
||||||
id_info_t *id = &scope->id_info[i];
|
id_info_t *id = &scope->id_info[i];
|
||||||
if (id->kind == ID_INFO_KIND_CELL) {
|
if (id->kind == ID_INFO_KIND_CELL) {
|
||||||
|
assert(id->local_num < 255);
|
||||||
emit_write_bytecode_byte(emit, id->local_num); // write the local which should be converted to a cell
|
emit_write_bytecode_byte(emit, id->local_num); // write the local which should be converted to a cell
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
emit_write_bytecode_byte(emit, 255); // end of list sentinel
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_emit_bc_end_pass(emit_t *emit) {
|
void mp_emit_bc_end_pass(emit_t *emit) {
|
||||||
|
@ -95,10 +95,8 @@ void mp_bytecode_print(const void *descr, mp_uint_t n_total_args, const byte *ip
|
|||||||
|
|
||||||
// bytecode prelude: initialise closed over variables
|
// bytecode prelude: initialise closed over variables
|
||||||
{
|
{
|
||||||
uint n_local = *ip++;
|
uint local_num;
|
||||||
printf("(NUM_LOCAL %u)\n", n_local);
|
while ((local_num = *ip++) != 255) {
|
||||||
for (; n_local > 0; n_local--) {
|
|
||||||
uint local_num = *ip++;
|
|
||||||
printf("(INIT_CELL %u)\n", local_num);
|
printf("(INIT_CELL %u)\n", local_num);
|
||||||
}
|
}
|
||||||
len -= ip - mp_showbc_code_start;
|
len -= ip - mp_showbc_code_start;
|
||||||
|
@ -5,7 +5,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names:
|
arg names:
|
||||||
(N_STATE 3)
|
(N_STATE 3)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 0)
|
|
||||||
bc=-3 line=1
|
bc=-3 line=1
|
||||||
########
|
########
|
||||||
bc=\\d\+ line=134
|
bc=\\d\+ line=134
|
||||||
@ -32,7 +31,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names:
|
arg names:
|
||||||
(N_STATE 25)
|
(N_STATE 25)
|
||||||
(N_EXC_STACK 2)
|
(N_EXC_STACK 2)
|
||||||
(NUM_LOCAL 3)
|
|
||||||
(INIT_CELL 14)
|
(INIT_CELL 14)
|
||||||
(INIT_CELL 15)
|
(INIT_CELL 15)
|
||||||
(INIT_CELL 16)
|
(INIT_CELL 16)
|
||||||
@ -304,7 +302,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names: a
|
arg names: a
|
||||||
(N_STATE 5)
|
(N_STATE 5)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 1)
|
|
||||||
(INIT_CELL 0)
|
(INIT_CELL 0)
|
||||||
########
|
########
|
||||||
bc=\\d\+ line=124
|
bc=\\d\+ line=124
|
||||||
@ -323,7 +320,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names:
|
arg names:
|
||||||
(N_STATE 2)
|
(N_STATE 2)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 0)
|
|
||||||
bc=-3 line=1
|
bc=-3 line=1
|
||||||
bc=0 line=129
|
bc=0 line=129
|
||||||
bc=3 line=130
|
bc=3 line=130
|
||||||
@ -348,7 +344,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names:
|
arg names:
|
||||||
(N_STATE 1)
|
(N_STATE 1)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 0)
|
|
||||||
bc=-3 line=1
|
bc=-3 line=1
|
||||||
bc=10 line=135
|
bc=10 line=135
|
||||||
00 LOAD_NAME __name__ (cache=0)
|
00 LOAD_NAME __name__ (cache=0)
|
||||||
@ -364,7 +359,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names: c e
|
arg names: c e
|
||||||
(N_STATE 6)
|
(N_STATE 6)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 0)
|
|
||||||
bc=-\\d\+ line=1
|
bc=-\\d\+ line=1
|
||||||
00 LOAD_FAST 2
|
00 LOAD_FAST 2
|
||||||
01 FOR_ITER 17
|
01 FOR_ITER 17
|
||||||
@ -384,7 +378,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names: c e
|
arg names: c e
|
||||||
(N_STATE 7)
|
(N_STATE 7)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 0)
|
|
||||||
bc=-\\d\+ line=1
|
bc=-\\d\+ line=1
|
||||||
00 BUILD_LIST 0
|
00 BUILD_LIST 0
|
||||||
02 LOAD_FAST 2
|
02 LOAD_FAST 2
|
||||||
@ -403,7 +396,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names: c e
|
arg names: c e
|
||||||
(N_STATE 8)
|
(N_STATE 8)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 0)
|
|
||||||
bc=-\\d\+ line=1
|
bc=-\\d\+ line=1
|
||||||
########
|
########
|
||||||
00 BUILD_MAP 0
|
00 BUILD_MAP 0
|
||||||
@ -424,7 +416,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names: x
|
arg names: x
|
||||||
(N_STATE 4)
|
(N_STATE 4)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 0)
|
|
||||||
bc=-\\d\+ line=1
|
bc=-\\d\+ line=1
|
||||||
########
|
########
|
||||||
bc=\\d\+ line=105
|
bc=\\d\+ line=105
|
||||||
@ -444,7 +435,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names: b a
|
arg names: b a
|
||||||
(N_STATE 4)
|
(N_STATE 4)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 0)
|
|
||||||
bc=-\\d\+ line=1
|
bc=-\\d\+ line=1
|
||||||
########
|
########
|
||||||
bc=\\d\+ line=125
|
bc=\\d\+ line=125
|
||||||
|
@ -7,7 +7,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
|
|||||||
arg names:
|
arg names:
|
||||||
(N_STATE 2)
|
(N_STATE 2)
|
||||||
(N_EXC_STACK 0)
|
(N_EXC_STACK 0)
|
||||||
(NUM_LOCAL 0)
|
|
||||||
bc=-3 line=1
|
bc=-3 line=1
|
||||||
bc=0 line=3
|
bc=0 line=3
|
||||||
00 LOAD_NAME print (cache=0)
|
00 LOAD_NAME print (cache=0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user