Skip to content

Commit 27fb45e

Browse files
committed
Add local_num skeleton framework to deref/closure emit calls.
1 parent a5185f4 commit 27fb45e

8 files changed

Lines changed: 31 additions & 26 deletions

File tree

py/compile.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_dict_
721721
for (int i = 0; i < this_scope->id_info_len; i++) {
722722
id_info_t *id_info = &this_scope->id_info[i];
723723
if (id_info->kind == ID_INFO_KIND_FREE) {
724-
EMIT(load_closure, id_info->qstr);
724+
EMIT(load_closure, id_info->qstr, id_info->local_num);
725725
nfree += 1;
726726
}
727727
}
@@ -2624,7 +2624,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
26242624
if (id->kind == ID_INFO_KIND_LOCAL) {
26252625
EMIT(load_const_tok, PY_TOKEN_KW_NONE);
26262626
} else {
2627-
EMIT(load_closure, comp->qstr___class__);
2627+
EMIT(load_closure, comp->qstr___class__, 0); // XXX check this is the correct local num
26282628
}
26292629
EMIT(return_value);
26302630
}
@@ -2729,6 +2729,8 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
27292729
}
27302730
}
27312731

2732+
// TODO compute the index of free and cell vars (freevars[idx] in CPython)
2733+
27322734
// compute flags
27332735
//scope->flags = 0; since we set some things in parameters
27342736
if (scope->kind != SCOPE_MODULE) {

py/emit.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,22 @@ typedef struct _emit_method_table_t {
4747
void (*load_fast)(emit_t *emit, qstr qstr, int local_num);
4848
void (*load_name)(emit_t *emit, qstr qstr);
4949
void (*load_global)(emit_t *emit, qstr qstr);
50-
void (*load_deref)(emit_t *emit, qstr qstr);
51-
void (*load_closure)(emit_t *emit, qstr qstr);
50+
void (*load_deref)(emit_t *emit, qstr qstr, int local_num);
51+
void (*load_closure)(emit_t *emit, qstr qstr, int local_num);
5252
void (*load_attr)(emit_t *emit, qstr qstr);
5353
void (*load_method)(emit_t *emit, qstr qstr);
5454
void (*load_build_class)(emit_t *emit);
5555
void (*store_fast)(emit_t *emit, qstr qstr, int local_num);
5656
void (*store_name)(emit_t *emit, qstr qstr);
5757
void (*store_global)(emit_t *emit, qstr qstr);
58-
void (*store_deref)(emit_t *emit, qstr qstr);
58+
void (*store_deref)(emit_t *emit, qstr qstr, int local_num);
5959
void (*store_attr)(emit_t *emit, qstr qstr);
6060
void (*store_subscr)(emit_t *emit);
6161
void (*store_locals)(emit_t *emit);
6262
void (*delete_fast)(emit_t *emit, qstr qstr, int local_num);
6363
void (*delete_name)(emit_t *emit, qstr qstr);
6464
void (*delete_global)(emit_t *emit, qstr qstr);
65-
void (*delete_deref)(emit_t *emit, qstr qstr);
65+
void (*delete_deref)(emit_t *emit, qstr qstr, int local_num);
6666
void (*delete_attr)(emit_t *emit, qstr qstr);
6767
void (*delete_subscr)(emit_t *emit);
6868
void (*dup_top)(emit_t *emit);

py/emitbc.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,12 @@ static void emit_bc_load_global(emit_t *emit, qstr qstr) {
291291
emit_write_byte_1_qstr(emit, PYBC_LOAD_GLOBAL, qstr);
292292
}
293293

294-
static void emit_bc_load_deref(emit_t *emit, qstr qstr) {
294+
static void emit_bc_load_deref(emit_t *emit, qstr qstr, int local_num) {
295295
emit_pre(emit, 1);
296296
assert(0);
297297
}
298298

299-
static void emit_bc_load_closure(emit_t *emit, qstr qstr) {
299+
static void emit_bc_load_closure(emit_t *emit, qstr qstr, int local_num) {
300300
emit_pre(emit, 1);
301301
assert(0);
302302
}
@@ -337,7 +337,7 @@ static void emit_bc_store_global(emit_t *emit, qstr qstr) {
337337
emit_write_byte_1_qstr(emit, PYBC_STORE_GLOBAL, qstr);
338338
}
339339

340-
static void emit_bc_store_deref(emit_t *emit, qstr qstr) {
340+
static void emit_bc_store_deref(emit_t *emit, qstr qstr, int local_num) {
341341
emit_pre(emit, -1);
342342
assert(0);
343343
}
@@ -374,9 +374,10 @@ static void emit_bc_delete_global(emit_t *emit, qstr qstr) {
374374
emit_write_byte_1_qstr(emit, PYBC_DELETE_GLOBAL, qstr);
375375
}
376376

377-
static void emit_bc_delete_deref(emit_t *emit, qstr qstr) {
377+
static void emit_bc_delete_deref(emit_t *emit, qstr qstr, int local_num) {
378378
emit_pre(emit, 0);
379-
emit_write_byte_1_qstr(emit, PYBC_DELETE_DEREF, qstr);
379+
assert(0);
380+
//emit_write_byte_1_qstr(emit, PYBC_DELETE_DEREF, qstr);
380381
}
381382

382383
static void emit_bc_delete_attr(emit_t *emit, qstr qstr) {

py/emitcommon.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_ta
2828
} else if (id->kind == ID_INFO_KIND_LOCAL) {
2929
EMIT(load_fast, qstr, id->local_num);
3030
} else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
31-
EMIT(load_deref, qstr);
31+
EMIT(load_deref, qstr, id->local_num);
3232
} else {
3333
assert(0);
3434
}
@@ -48,7 +48,7 @@ void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_t
4848
} else if (id->kind == ID_INFO_KIND_LOCAL) {
4949
EMIT(store_fast, qstr, id->local_num);
5050
} else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
51-
EMIT(store_deref, qstr);
51+
EMIT(store_deref, qstr, id->local_num);
5252
} else {
5353
assert(0);
5454
}
@@ -68,7 +68,7 @@ void emit_common_delete_id(emit_t *emit, const emit_method_table_t *emit_method_
6868
} else if (id->kind == ID_INFO_KIND_LOCAL) {
6969
EMIT(delete_fast, qstr, id->local_num);
7070
} else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
71-
EMIT(delete_deref, qstr);
71+
EMIT(delete_deref, qstr, id->local_num);
7272
} else {
7373
assert(0);
7474
}

py/emitcpy.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,17 @@ static void emit_cpy_load_global(emit_t *emit, qstr qstr) {
279279
}
280280
}
281281

282-
static void emit_cpy_load_deref(emit_t *emit, qstr qstr) {
282+
static void emit_cpy_load_deref(emit_t *emit, qstr qstr, int local_num) {
283283
emit_pre(emit, 1, 3);
284284
if (emit->pass == PASS_3) {
285-
printf("LOAD_DEREF %s\n", qstr_str(qstr));
285+
printf("LOAD_DEREF %d %s\n", local_num, qstr_str(qstr));
286286
}
287287
}
288288

289-
static void emit_cpy_load_closure(emit_t *emit, qstr qstr) {
289+
static void emit_cpy_load_closure(emit_t *emit, qstr qstr, int local_num) {
290290
emit_pre(emit, 1, 3);
291291
if (emit->pass == PASS_3) {
292-
printf("LOAD_CLOSURE %s\n", qstr_str(qstr));
292+
printf("LOAD_CLOSURE %d %s\n", local_num, qstr_str(qstr));
293293
}
294294
}
295295

@@ -332,10 +332,10 @@ static void emit_cpy_store_global(emit_t *emit, qstr qstr) {
332332
}
333333
}
334334

335-
static void emit_cpy_store_deref(emit_t *emit, qstr qstr) {
335+
static void emit_cpy_store_deref(emit_t *emit, qstr qstr, int local_num) {
336336
emit_pre(emit, -1, 3);
337337
if (emit->pass == PASS_3) {
338-
printf("STORE_DEREF %s\n", qstr_str(qstr));
338+
printf("STORE_DEREF %d %s\n", local_num, qstr_str(qstr));
339339
}
340340
}
341341

@@ -381,10 +381,10 @@ static void emit_cpy_delete_global(emit_t *emit, qstr qstr) {
381381
}
382382
}
383383

384-
static void emit_cpy_delete_deref(emit_t *emit, qstr qstr) {
384+
static void emit_cpy_delete_deref(emit_t *emit, qstr qstr, int local_num) {
385385
emit_pre(emit, 0, 3);
386386
if (emit->pass == PASS_3) {
387-
printf("DELETE_DEREF %s\n", qstr_str(qstr));
387+
printf("DELETE_DEREF %d %s\n", local_num, qstr_str(qstr));
388388
}
389389
}
390390

py/emitnative.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,13 @@ static void emit_native_load_global(emit_t *emit, qstr qstr) {
675675
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
676676
}
677677

678-
static void emit_native_load_deref(emit_t *emit, qstr qstr) {
678+
static void emit_native_load_deref(emit_t *emit, qstr qstr, int local_num) {
679679
// not implemented
680680
// in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
681681
assert(0);
682682
}
683683

684-
static void emit_native_load_closure(emit_t *emit, qstr qstr) {
684+
static void emit_native_load_closure(emit_t *emit, qstr qstr, int local_num) {
685685
// not implemented
686686
assert(0);
687687
}
@@ -760,7 +760,7 @@ static void emit_native_store_global(emit_t *emit, qstr qstr) {
760760
assert(0);
761761
}
762762

763-
static void emit_native_store_deref(emit_t *emit, qstr qstr) {
763+
static void emit_native_store_deref(emit_t *emit, qstr qstr, int local_num) {
764764
// not implemented
765765
assert(0);
766766
}
@@ -812,7 +812,7 @@ static void emit_native_delete_global(emit_t *emit, qstr qstr) {
812812
assert(0);
813813
}
814814

815-
static void emit_native_delete_deref(emit_t *emit, qstr qstr) {
815+
static void emit_native_delete_deref(emit_t *emit, qstr qstr, int local_num) {
816816
// not supported
817817
assert(0);
818818
}

py/scope.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added) {
103103
id_info->param = false;
104104
id_info->kind = 0;
105105
id_info->qstr = qstr;
106+
id_info->local_num = 0;
106107
*added = true;
107108
return id_info;
108109
}

py/scope.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum {
77
};
88

99
typedef struct _id_info_t {
10+
// TODO compress this info to make structure smaller in memory
1011
bool param;
1112
int kind;
1213
qstr qstr;

0 commit comments

Comments
 (0)