Skip to content

Commit b05d707

Browse files
committed
Further factorise PASS_1 out of specific emit code.
1 parent 415eb6f commit b05d707

13 files changed

Lines changed: 115 additions & 133 deletions

File tree

py/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ SRC = \
1212
scope.c \
1313
compile.c \
1414
emitcommon.c \
15+
emitpass1.c \
1516
emitcpy.c \
1617
emitbc.c \
1718
asmx64.c \
1819
emitx64.c \
1920
emitthumb.c \
2021
asmthumb.c \
2122
runtime.c \
22-
bc.c \
23+
vm.c \
2324
main.c \
2425

2526
SRC_ASM = \

py/asmx64.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
typedef struct _asm_x64_t asm_x64_t;
2828

29-
asm_x64_t* asm_x64_new();
29+
asm_x64_t* asm_x64_new(uint max_num_labels);
3030
void asm_x64_free(asm_x64_t* as, bool free_code);
3131
void asm_x64_start_pass(asm_x64_t *as, int pass);
3232
void asm_x64_end_pass(asm_x64_t *as);

py/compile.c

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ typedef struct _compiler_t {
3939

4040
pass_kind_t pass;
4141

42+
int next_label;
43+
int max_num_labels;
44+
4245
int break_label;
4346
int continue_label;
4447
int except_nest_level;
@@ -159,8 +162,12 @@ py_parse_node_t fold_constants(py_parse_node_t pn) {
159162

160163
void compile_node(compiler_t *comp, py_parse_node_t pn);
161164

162-
scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t pn) {
163-
scope_t *scope = scope_new(kind, pn);
165+
static int comp_next_label(compiler_t *comp) {
166+
return comp->next_label++;
167+
}
168+
169+
static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t pn) {
170+
scope_t *scope = scope_new(kind, pn, rt_get_new_unique_code_id());
164171
scope->parent = comp->scope_cur;
165172
scope->next = NULL;
166173
if (comp->scope_head == NULL) {
@@ -175,7 +182,7 @@ scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t
175182
return scope;
176183
}
177184

178-
int list_len(py_parse_node_t pn, int pn_kind) {
185+
static int list_len(py_parse_node_t pn, int pn_kind) {
179186
if (PY_PARSE_NODE_IS_NULL(pn)) {
180187
return 0;
181188
} else if (PY_PARSE_NODE_IS_LEAF(pn)) {
@@ -190,7 +197,7 @@ int list_len(py_parse_node_t pn, int pn_kind) {
190197
}
191198
}
192199

193-
void apply_to_single_or_list(compiler_t *comp, py_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, py_parse_node_t)) {
200+
static void apply_to_single_or_list(compiler_t *comp, py_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, py_parse_node_t)) {
194201
if (PY_PARSE_NODE_IS_STRUCT(pn) && PY_PARSE_NODE_STRUCT_KIND((py_parse_node_struct_t*)pn) == pn_list_kind) {
195202
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
196203
int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
@@ -202,7 +209,7 @@ void apply_to_single_or_list(compiler_t *comp, py_parse_node_t pn, int pn_list_k
202209
}
203210
}
204211

205-
int list_get(py_parse_node_t *pn, int pn_kind, py_parse_node_t **nodes) {
212+
static int list_get(py_parse_node_t *pn, int pn_kind, py_parse_node_t **nodes) {
206213
if (PY_PARSE_NODE_IS_NULL(*pn)) {
207214
*nodes = NULL;
208215
return 0;
@@ -353,7 +360,7 @@ void c_if_cond_2(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label,
353360
int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
354361
if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
355362
if (jump_if == false) {
356-
int label2 = EMIT(label_new);
363+
int label2 = comp_next_label(comp);
357364
for (int i = 0; i < n - 1; i++) {
358365
c_if_cond_2(comp, pns->nodes[i], true, label2, true);
359366
}
@@ -371,7 +378,7 @@ void c_if_cond_2(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label,
371378
c_if_cond_2(comp, pns->nodes[i], false, label, true);
372379
}
373380
} else {
374-
int label2 = EMIT(label_new);
381+
int label2 = comp_next_label(comp);
375382
for (int i = 0; i < n - 1; i++) {
376383
c_if_cond_2(comp, pns->nodes[i], false, label2, true);
377384
}
@@ -893,7 +900,7 @@ void compile_return_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
893900
py_parse_node_struct_t *pns_test_if_expr = (py_parse_node_struct_t*)pns->nodes[0];
894901
py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns_test_if_expr->nodes[1];
895902

896-
int l_fail = EMIT(label_new);
903+
int l_fail = comp_next_label(comp);
897904
c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
898905
compile_node(comp, pns_test_if_expr->nodes[0]); // success value
899906
EMIT(return_value);
@@ -1073,7 +1080,7 @@ void compile_nonlocal_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
10731080
}
10741081

10751082
void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
1076-
int l_end = EMIT(label_new);
1083+
int l_end = comp_next_label(comp);
10771084
c_if_cond(comp, pns->nodes[0], true, l_end);
10781085
EMIT_COMMON(load_id, comp->qstr___class__, comp->qstr_assertion_error);
10791086
if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
@@ -1088,9 +1095,9 @@ void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
10881095
void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
10891096
// TODO proper and/or short circuiting
10901097

1091-
int l_end = EMIT(label_new);
1098+
int l_end = comp_next_label(comp);
10921099

1093-
int l_fail = EMIT(label_new);
1100+
int l_fail = comp_next_label(comp);
10941101
c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
10951102

10961103
compile_node(comp, pns->nodes[1]); // if block
@@ -1113,7 +1120,7 @@ void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
11131120
int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
11141121
for (int i = 0; i < n; i++) {
11151122
py_parse_node_struct_t *pns_elif2 = (py_parse_node_struct_t*)pns_elif->nodes[i];
1116-
l_fail = EMIT(label_new);
1123+
l_fail = comp_next_label(comp);
11171124
c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
11181125

11191126
compile_node(comp, pns_elif2->nodes[1]); // elif block
@@ -1126,7 +1133,7 @@ void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
11261133
} else {
11271134
// a single elif block
11281135

1129-
l_fail = EMIT(label_new);
1136+
l_fail = comp_next_label(comp);
11301137
c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
11311138

11321139
compile_node(comp, pns_elif->nodes[1]); // elif block
@@ -1147,10 +1154,10 @@ void compile_while_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
11471154
int old_break_label = comp->break_label;
11481155
int old_continue_label = comp->continue_label;
11491156

1150-
int done_label = EMIT(label_new);
1151-
int end_label = EMIT(label_new);
1152-
int break_label = EMIT(label_new);
1153-
int continue_label = EMIT(label_new);
1157+
int done_label = comp_next_label(comp);
1158+
int end_label = comp_next_label(comp);
1159+
int break_label = comp_next_label(comp);
1160+
int continue_label = comp_next_label(comp);
11541161

11551162
comp->break_label = break_label;
11561163
comp->continue_label = continue_label;
@@ -1184,11 +1191,11 @@ void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
11841191
int old_break_label = comp->break_label;
11851192
int old_continue_label = comp->continue_label;
11861193

1187-
int for_label = EMIT(label_new);
1188-
int pop_label = EMIT(label_new);
1189-
int end_label = EMIT(label_new);
1194+
int for_label = comp_next_label(comp);
1195+
int pop_label = comp_next_label(comp);
1196+
int end_label = comp_next_label(comp);
11901197

1191-
int break_label = EMIT(label_new);
1198+
int break_label = comp_next_label(comp);
11921199

11931200
comp->continue_label = for_label;
11941201
comp->break_label = break_label;
@@ -1224,22 +1231,22 @@ void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except,
12241231

12251232
// setup code
12261233
int stack_size = EMIT(get_stack_size);
1227-
int l1 = EMIT(label_new);
1228-
int success_label = EMIT(label_new);
1234+
int l1 = comp_next_label(comp);
1235+
int success_label = comp_next_label(comp);
12291236
comp->except_nest_level += 1; // for correct handling of continue
12301237
EMIT(setup_except, l1);
12311238
compile_node(comp, pn_body); // body
12321239
EMIT(pop_block);
12331240
EMIT(jump, success_label);
12341241
EMIT(label_assign, l1);
1235-
int l2 = EMIT(label_new);
1242+
int l2 = comp_next_label(comp);
12361243

12371244
for (int i = 0; i < n_except; i++) {
12381245
assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
12391246
py_parse_node_struct_t *pns_except = (py_parse_node_struct_t*)pn_excepts[i];
12401247

12411248
qstr qstr_exception_local = 0;
1242-
int end_finally_label = EMIT(label_new);
1249+
int end_finally_label = comp_next_label(comp);
12431250

12441251
if (PY_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
12451252
// this is a catch all exception handler
@@ -1276,7 +1283,7 @@ void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except,
12761283

12771284
int l3;
12781285
if (qstr_exception_local != 0) {
1279-
l3 = EMIT(label_new);
1286+
l3 = comp_next_label(comp);
12801287
EMIT(setup_finally, l3);
12811288
}
12821289
compile_node(comp, pns_except->nodes[1]);
@@ -1307,7 +1314,7 @@ void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except,
13071314
void compile_try_finally(compiler_t *comp, py_parse_node_t pn_body, int n_except, py_parse_node_t *pn_except, py_parse_node_t pn_else, py_parse_node_t pn_finally) {
13081315
// don't understand how the stack works with exceptions, so we force it to return to the correct value
13091316
int stack_size = EMIT(get_stack_size);
1310-
int l_finally_block = EMIT(label_new);
1317+
int l_finally_block = comp_next_label(comp);
13111318
EMIT(setup_finally, l_finally_block);
13121319
if (n_except == 0) {
13131320
assert(PY_PARSE_NODE_IS_NULL(pn_else));
@@ -1357,7 +1364,7 @@ void compile_with_stmt_helper(compiler_t *comp, int n, py_parse_node_t *nodes, p
13571364
// no more pre-bits, compile the body of the with
13581365
compile_node(comp, body);
13591366
} else {
1360-
int l_end = EMIT(label_new);
1367+
int l_end = comp_next_label(comp);
13611368
if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
13621369
// this pre-bit is of the form "a as b"
13631370
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)nodes[0];
@@ -1490,8 +1497,8 @@ void compile_test_if_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
14901497
py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns->nodes[1];
14911498

14921499
int stack_size = EMIT(get_stack_size);
1493-
int l_fail = EMIT(label_new);
1494-
int l_end = EMIT(label_new);
1500+
int l_fail = comp_next_label(comp);
1501+
int l_end = comp_next_label(comp);
14951502
c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
14961503
compile_node(comp, pns->nodes[0]); // success value
14971504
EMIT(jump, l_end);
@@ -1521,7 +1528,7 @@ void compile_lambdef(compiler_t *comp, py_parse_node_struct_t *pns) {
15211528
}
15221529

15231530
void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) {
1524-
int l_end = EMIT(label_new);
1531+
int l_end = comp_next_label(comp);
15251532
int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
15261533
for (int i = 0; i < n; i += 1) {
15271534
compile_node(comp, pns->nodes[i]);
@@ -1533,7 +1540,7 @@ void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) {
15331540
}
15341541

15351542
void compile_and_test(compiler_t *comp, py_parse_node_struct_t *pns) {
1536-
int l_end = EMIT(label_new);
1543+
int l_end = comp_next_label(comp);
15371544
int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
15381545
for (int i = 0; i < n; i += 1) {
15391546
compile_node(comp, pns->nodes[i]);
@@ -1556,7 +1563,7 @@ void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) {
15561563
bool multi = (num_nodes > 3);
15571564
int l_fail = 0;
15581565
if (multi) {
1559-
l_fail = EMIT(label_new);
1566+
l_fail = comp_next_label(comp);
15601567
}
15611568
for (int i = 1; i + 1 < num_nodes; i += 2) {
15621569
compile_node(comp, pns->nodes[i + 1]);
@@ -1602,7 +1609,7 @@ void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) {
16021609
}
16031610
}
16041611
if (multi) {
1605-
int l_end = EMIT(label_new);
1612+
int l_end = comp_next_label(comp);
16061613
EMIT(jump, l_end);
16071614
EMIT(label_assign, l_fail);
16081615
EMIT(rot_two);
@@ -2255,8 +2262,8 @@ void compile_scope_comp_iter(compiler_t *comp, py_parse_node_t pn_iter, py_parse
22552262
// for loop
22562263
py_parse_node_struct_t *pns_comp_for2 = (py_parse_node_struct_t*)pn_iter;
22572264
compile_node(comp, pns_comp_for2->nodes[1]);
2258-
int l_end2 = EMIT(label_new);
2259-
int l_top2 = EMIT(label_new);
2265+
int l_end2 = comp_next_label(comp);
2266+
int l_top2 = comp_next_label(comp);
22602267
EMIT(get_iter);
22612268
EMIT(label_assign, l_top2);
22622269
EMIT(for_iter, l_end2);
@@ -2302,6 +2309,7 @@ void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) {
23022309
void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
23032310
comp->pass = pass;
23042311
comp->scope_cur = scope;
2312+
comp->next_label = 1;
23052313
EMIT(start_pass, pass, scope);
23062314

23072315
if (comp->pass == PASS_1) {
@@ -2377,8 +2385,8 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
23772385
EMIT(build_set, 0);
23782386
}
23792387

2380-
int l_end = EMIT(label_new);
2381-
int l_top = EMIT(label_new);
2388+
int l_end = comp_next_label(comp);
2389+
int l_top = comp_next_label(comp);
23822390
EMIT_COMMON(load_id, comp->qstr___class__, qstr_arg);
23832391
EMIT(label_assign, l_top);
23842392
EMIT(for_iter, l_end);
@@ -2431,6 +2439,11 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
24312439
}
24322440

24332441
EMIT(end_pass);
2442+
2443+
// update maximim number of labels needed
2444+
if (comp->next_label > comp->max_num_labels) {
2445+
comp->max_num_labels = comp->next_label;
2446+
}
24342447
}
24352448

24362449
void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
@@ -2489,15 +2502,14 @@ void py_compile(py_parse_node_t pn) {
24892502
comp->qstr___doc__ = qstr_from_strn_copy("__doc__", 7);
24902503
comp->qstr_assertion_error = qstr_from_strn_copy("AssertionError", 14);
24912504

2505+
comp->max_num_labels = 0;
24922506
comp->break_label = 0;
24932507
comp->continue_label = 0;
24942508
comp->except_nest_level = 0;
24952509
comp->scope_head = NULL;
24962510
comp->scope_cur = NULL;
24972511

2498-
emit_new_cpython(&comp->emit, &comp->emit_method_table);
2499-
//emit_new_bc(&comp->emit, &comp->emit_method_table);
2500-
//emit_new_x64(&comp->emit, &comp->emit_method_table);
2512+
emit_pass1_new(&comp->emit, &comp->emit_method_table);
25012513

25022514
pn = fold_constants(pn);
25032515
scope_new_and_link(comp, SCOPE_MODULE, pn);
@@ -2510,6 +2522,10 @@ void py_compile(py_parse_node_t pn) {
25102522
compile_scope_compute_things(comp, s);
25112523
}
25122524

2525+
//emit_cpython_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
2526+
emit_bc_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
2527+
//emit_new_x64(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
2528+
25132529
for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
25142530
compile_scope(comp, s, PASS_2);
25152531
compile_scope(comp, s, PASS_3);

py/emit.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,13 @@ void emit_common_load_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const e
113113
void emit_common_store_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const emit_method_table_t *emit_method_table, qstr qstr);
114114
void emit_common_delete_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const emit_method_table_t *emit_method_table, qstr qstr);
115115

116-
void emit_new_cpython(emit_t **emit, const emit_method_table_t **emit_method_table);
117-
void emit_new_bc(emit_t **emit, const emit_method_table_t **emit_method_table);
118-
void emit_new_x64(emit_t **emit, const emit_method_table_t **emit_method_table);
119-
void emit_new_thumb(emit_t **emit, const emit_method_table_t **emit_method_table);
116+
void emit_pass1_new(emit_t **emit, const emit_method_table_t **emit_method_table);
117+
uint emit_pass1_get_max_num_labels(emit_t *emit);
118+
119+
void emit_cpython_new(emit_t **emit_out, const emit_method_table_t **emit_method_table_out, uint max_num_labels);
120+
void emit_bc_new(emit_t **emit, const emit_method_table_t **emit_method_table, uint max_num_labels);
121+
void emit_x64_new(emit_t **emit, const emit_method_table_t **emit_method_table, uint max_num_labels);
122+
void emit_thumb_new(emit_t **emit, const emit_method_table_t **emit_method_table, uint max_num_labels);
120123

121124
/*
122125
void emit_set_native_types(emitter_t *emit, bool do_native_types);

0 commit comments

Comments
 (0)