Skip to content

Commit c9aa188

Browse files
committed
py: Simplify bytecode prelude when encoding closed over variables.
1 parent 78772ad commit c9aa188

5 files changed

Lines changed: 8 additions & 27 deletions

File tree

py/bc.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,10 @@ continue2:;
228228

229229
// bytecode prelude: initialise closed over variables
230230
const byte *ip = code_state->ip;
231-
for (mp_uint_t n_local = *ip++; n_local > 0; n_local--) {
232-
mp_uint_t local_num = *ip++;
233-
code_state->state[n_state - 1 - local_num] = mp_obj_new_cell(code_state->state[n_state - 1 - local_num]);
231+
mp_uint_t local_num;
232+
while ((local_num = *ip++) != 255) {
233+
code_state->state[n_state - 1 - local_num] =
234+
mp_obj_new_cell(code_state->state[n_state - 1 - local_num]);
234235
}
235236

236237
// now that we skipped over the prelude, set the ip for the VM

py/emitbc.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,21 +322,14 @@ void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
322322
}
323323

324324
// bytecode prelude: initialise closed over variables
325-
int num_cell = 0;
326-
for (int i = 0; i < scope->id_info_len; i++) {
327-
id_info_t *id = &scope->id_info[i];
328-
if (id->kind == ID_INFO_KIND_CELL) {
329-
num_cell += 1;
330-
}
331-
}
332-
assert(num_cell <= 255);
333-
emit_write_bytecode_byte(emit, num_cell); // write number of locals that are cells
334325
for (int i = 0; i < scope->id_info_len; i++) {
335326
id_info_t *id = &scope->id_info[i];
336327
if (id->kind == ID_INFO_KIND_CELL) {
328+
assert(id->local_num < 255);
337329
emit_write_bytecode_byte(emit, id->local_num); // write the local which should be converted to a cell
338330
}
339331
}
332+
emit_write_bytecode_byte(emit, 255); // end of list sentinel
340333
}
341334

342335
void mp_emit_bc_end_pass(emit_t *emit) {

py/showbc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ void mp_bytecode_print(const void *descr, mp_uint_t n_total_args, const byte *ip
9595

9696
// bytecode prelude: initialise closed over variables
9797
{
98-
uint n_local = *ip++;
99-
printf("(NUM_LOCAL %u)\n", n_local);
100-
for (; n_local > 0; n_local--) {
101-
uint local_num = *ip++;
98+
uint local_num;
99+
while ((local_num = *ip++) != 255) {
102100
printf("(INIT_CELL %u)\n", local_num);
103101
}
104102
len -= ip - mp_showbc_code_start;

tests/cmdline/cmd_showbc.py.exp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
55
arg names:
66
(N_STATE 3)
77
(N_EXC_STACK 0)
8-
(NUM_LOCAL 0)
98
bc=-3 line=1
109
########
1110
bc=\\d\+ line=134
@@ -32,7 +31,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
3231
arg names:
3332
(N_STATE 25)
3433
(N_EXC_STACK 2)
35-
(NUM_LOCAL 3)
3634
(INIT_CELL 14)
3735
(INIT_CELL 15)
3836
(INIT_CELL 16)
@@ -304,7 +302,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
304302
arg names: a
305303
(N_STATE 5)
306304
(N_EXC_STACK 0)
307-
(NUM_LOCAL 1)
308305
(INIT_CELL 0)
309306
########
310307
bc=\\d\+ line=124
@@ -323,7 +320,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
323320
arg names:
324321
(N_STATE 2)
325322
(N_EXC_STACK 0)
326-
(NUM_LOCAL 0)
327323
bc=-3 line=1
328324
bc=0 line=129
329325
bc=3 line=130
@@ -348,7 +344,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
348344
arg names:
349345
(N_STATE 1)
350346
(N_EXC_STACK 0)
351-
(NUM_LOCAL 0)
352347
bc=-3 line=1
353348
bc=10 line=135
354349
00 LOAD_NAME __name__ (cache=0)
@@ -364,7 +359,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
364359
arg names: c e
365360
(N_STATE 6)
366361
(N_EXC_STACK 0)
367-
(NUM_LOCAL 0)
368362
bc=-\\d\+ line=1
369363
00 LOAD_FAST 2
370364
01 FOR_ITER 17
@@ -384,7 +378,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
384378
arg names: c e
385379
(N_STATE 7)
386380
(N_EXC_STACK 0)
387-
(NUM_LOCAL 0)
388381
bc=-\\d\+ line=1
389382
00 BUILD_LIST 0
390383
02 LOAD_FAST 2
@@ -403,7 +396,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
403396
arg names: c e
404397
(N_STATE 8)
405398
(N_EXC_STACK 0)
406-
(NUM_LOCAL 0)
407399
bc=-\\d\+ line=1
408400
########
409401
00 BUILD_MAP 0
@@ -424,7 +416,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
424416
arg names: x
425417
(N_STATE 4)
426418
(N_EXC_STACK 0)
427-
(NUM_LOCAL 0)
428419
bc=-\\d\+ line=1
429420
########
430421
bc=\\d\+ line=105
@@ -444,7 +435,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
444435
arg names: b a
445436
(N_STATE 4)
446437
(N_EXC_STACK 0)
447-
(NUM_LOCAL 0)
448438
bc=-\\d\+ line=1
449439
########
450440
bc=\\d\+ line=125

tests/cmdline/cmd_verbose.py.exp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
77
arg names:
88
(N_STATE 2)
99
(N_EXC_STACK 0)
10-
(NUM_LOCAL 0)
1110
bc=-3 line=1
1211
bc=0 line=3
1312
00 LOAD_NAME print (cache=0)

0 commit comments

Comments
 (0)