Commit b05d707b authored by Damien's avatar Damien
Browse files

Further factorise PASS_1 out of specific emit code.

parent 415eb6f8
...@@ -12,6 +12,7 @@ SRC = \ ...@@ -12,6 +12,7 @@ SRC = \
scope.c \ scope.c \
compile.c \ compile.c \
emitcommon.c \ emitcommon.c \
emitpass1.c \
emitcpy.c \ emitcpy.c \
emitbc.c \ emitbc.c \
asmx64.c \ asmx64.c \
...@@ -19,7 +20,7 @@ SRC = \ ...@@ -19,7 +20,7 @@ SRC = \
emitthumb.c \ emitthumb.c \
asmthumb.c \ asmthumb.c \
runtime.c \ runtime.c \
bc.c \ vm.c \
main.c \ main.c \
SRC_ASM = \ SRC_ASM = \
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
typedef struct _asm_x64_t asm_x64_t; typedef struct _asm_x64_t asm_x64_t;
asm_x64_t* asm_x64_new(); asm_x64_t* asm_x64_new(uint max_num_labels);
void asm_x64_free(asm_x64_t* as, bool free_code); void asm_x64_free(asm_x64_t* as, bool free_code);
void asm_x64_start_pass(asm_x64_t *as, int pass); void asm_x64_start_pass(asm_x64_t *as, int pass);
void asm_x64_end_pass(asm_x64_t *as); void asm_x64_end_pass(asm_x64_t *as);
......
...@@ -39,6 +39,9 @@ typedef struct _compiler_t { ...@@ -39,6 +39,9 @@ typedef struct _compiler_t {
pass_kind_t pass; pass_kind_t pass;
int next_label;
int max_num_labels;
int break_label; int break_label;
int continue_label; int continue_label;
int except_nest_level; int except_nest_level;
...@@ -159,8 +162,12 @@ py_parse_node_t fold_constants(py_parse_node_t pn) { ...@@ -159,8 +162,12 @@ py_parse_node_t fold_constants(py_parse_node_t pn) {
void compile_node(compiler_t *comp, py_parse_node_t pn); void compile_node(compiler_t *comp, py_parse_node_t pn);
scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t pn) { static int comp_next_label(compiler_t *comp) {
scope_t *scope = scope_new(kind, pn); return comp->next_label++;
}
static scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t pn) {
scope_t *scope = scope_new(kind, pn, rt_get_new_unique_code_id());
scope->parent = comp->scope_cur; scope->parent = comp->scope_cur;
scope->next = NULL; scope->next = NULL;
if (comp->scope_head == NULL) { 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 ...@@ -175,7 +182,7 @@ scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, py_parse_node_t
return scope; return scope;
} }
int list_len(py_parse_node_t pn, int pn_kind) { static int list_len(py_parse_node_t pn, int pn_kind) {
if (PY_PARSE_NODE_IS_NULL(pn)) { if (PY_PARSE_NODE_IS_NULL(pn)) {
return 0; return 0;
} else if (PY_PARSE_NODE_IS_LEAF(pn)) { } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
...@@ -190,7 +197,7 @@ int list_len(py_parse_node_t pn, int pn_kind) { ...@@ -190,7 +197,7 @@ int list_len(py_parse_node_t pn, int pn_kind) {
} }
} }
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)) { 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)) {
if (PY_PARSE_NODE_IS_STRUCT(pn) && PY_PARSE_NODE_STRUCT_KIND((py_parse_node_struct_t*)pn) == pn_list_kind) { if (PY_PARSE_NODE_IS_STRUCT(pn) && PY_PARSE_NODE_STRUCT_KIND((py_parse_node_struct_t*)pn) == pn_list_kind) {
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); 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 ...@@ -202,7 +209,7 @@ void apply_to_single_or_list(compiler_t *comp, py_parse_node_t pn, int pn_list_k
} }
} }
int list_get(py_parse_node_t *pn, int pn_kind, py_parse_node_t **nodes) { static int list_get(py_parse_node_t *pn, int pn_kind, py_parse_node_t **nodes) {
if (PY_PARSE_NODE_IS_NULL(*pn)) { if (PY_PARSE_NODE_IS_NULL(*pn)) {
*nodes = NULL; *nodes = NULL;
return 0; return 0;
...@@ -353,7 +360,7 @@ void c_if_cond_2(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label, ...@@ -353,7 +360,7 @@ void c_if_cond_2(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label,
int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) { if (PY_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
if (jump_if == false) { if (jump_if == false) {
int label2 = EMIT(label_new); int label2 = comp_next_label(comp);
for (int i = 0; i < n - 1; i++) { for (int i = 0; i < n - 1; i++) {
c_if_cond_2(comp, pns->nodes[i], true, label2, true); c_if_cond_2(comp, pns->nodes[i], true, label2, true);
} }
...@@ -371,7 +378,7 @@ void c_if_cond_2(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label, ...@@ -371,7 +378,7 @@ void c_if_cond_2(compiler_t *comp, py_parse_node_t pn, bool jump_if, int label,
c_if_cond_2(comp, pns->nodes[i], false, label, true); c_if_cond_2(comp, pns->nodes[i], false, label, true);
} }
} else { } else {
int label2 = EMIT(label_new); int label2 = comp_next_label(comp);
for (int i = 0; i < n - 1; i++) { for (int i = 0; i < n - 1; i++) {
c_if_cond_2(comp, pns->nodes[i], false, label2, true); c_if_cond_2(comp, pns->nodes[i], false, label2, true);
} }
...@@ -893,7 +900,7 @@ void compile_return_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { ...@@ -893,7 +900,7 @@ void compile_return_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
py_parse_node_struct_t *pns_test_if_expr = (py_parse_node_struct_t*)pns->nodes[0]; py_parse_node_struct_t *pns_test_if_expr = (py_parse_node_struct_t*)pns->nodes[0];
py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns_test_if_expr->nodes[1]; py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns_test_if_expr->nodes[1];
int l_fail = EMIT(label_new); int l_fail = comp_next_label(comp);
c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
compile_node(comp, pns_test_if_expr->nodes[0]); // success value compile_node(comp, pns_test_if_expr->nodes[0]); // success value
EMIT(return_value); EMIT(return_value);
...@@ -1073,7 +1080,7 @@ void compile_nonlocal_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { ...@@ -1073,7 +1080,7 @@ void compile_nonlocal_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
} }
void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
int l_end = EMIT(label_new); int l_end = comp_next_label(comp);
c_if_cond(comp, pns->nodes[0], true, l_end); c_if_cond(comp, pns->nodes[0], true, l_end);
EMIT_COMMON(load_id, comp->qstr___class__, comp->qstr_assertion_error); EMIT_COMMON(load_id, comp->qstr___class__, comp->qstr_assertion_error);
if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { 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) { ...@@ -1088,9 +1095,9 @@ void compile_assert_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
// TODO proper and/or short circuiting // TODO proper and/or short circuiting
int l_end = EMIT(label_new); int l_end = comp_next_label(comp);
int l_fail = EMIT(label_new); int l_fail = comp_next_label(comp);
c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
compile_node(comp, pns->nodes[1]); // if block 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) { ...@@ -1113,7 +1120,7 @@ void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_elif); int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
py_parse_node_struct_t *pns_elif2 = (py_parse_node_struct_t*)pns_elif->nodes[i]; py_parse_node_struct_t *pns_elif2 = (py_parse_node_struct_t*)pns_elif->nodes[i];
l_fail = EMIT(label_new); l_fail = comp_next_label(comp);
c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
compile_node(comp, pns_elif2->nodes[1]); // elif block 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) { ...@@ -1126,7 +1133,7 @@ void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
} else { } else {
// a single elif block // a single elif block
l_fail = EMIT(label_new); l_fail = comp_next_label(comp);
c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
compile_node(comp, pns_elif->nodes[1]); // elif block 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) { ...@@ -1147,10 +1154,10 @@ void compile_while_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
int old_break_label = comp->break_label; int old_break_label = comp->break_label;
int old_continue_label = comp->continue_label; int old_continue_label = comp->continue_label;
int done_label = EMIT(label_new); int done_label = comp_next_label(comp);
int end_label = EMIT(label_new); int end_label = comp_next_label(comp);
int break_label = EMIT(label_new); int break_label = comp_next_label(comp);
int continue_label = EMIT(label_new); int continue_label = comp_next_label(comp);
comp->break_label = break_label; comp->break_label = break_label;
comp->continue_label = continue_label; comp->continue_label = continue_label;
...@@ -1184,11 +1191,11 @@ void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) { ...@@ -1184,11 +1191,11 @@ void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
int old_break_label = comp->break_label; int old_break_label = comp->break_label;
int old_continue_label = comp->continue_label; int old_continue_label = comp->continue_label;
int for_label = EMIT(label_new); int for_label = comp_next_label(comp);
int pop_label = EMIT(label_new); int pop_label = comp_next_label(comp);
int end_label = EMIT(label_new); int end_label = comp_next_label(comp);
int break_label = EMIT(label_new); int break_label = comp_next_label(comp);
comp->continue_label = for_label; comp->continue_label = for_label;
comp->break_label = break_label; 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, ...@@ -1224,22 +1231,22 @@ void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except,
// setup code // setup code
int stack_size = EMIT(get_stack_size); int stack_size = EMIT(get_stack_size);
int l1 = EMIT(label_new); int l1 = comp_next_label(comp);
int success_label = EMIT(label_new); int success_label = comp_next_label(comp);
comp->except_nest_level += 1; // for correct handling of continue comp->except_nest_level += 1; // for correct handling of continue
EMIT(setup_except, l1); EMIT(setup_except, l1);
compile_node(comp, pn_body); // body compile_node(comp, pn_body); // body
EMIT(pop_block); EMIT(pop_block);
EMIT(jump, success_label); EMIT(jump, success_label);
EMIT(label_assign, l1); EMIT(label_assign, l1);
int l2 = EMIT(label_new); int l2 = comp_next_label(comp);
for (int i = 0; i < n_except; i++) { for (int i = 0; i < n_except; i++) {
assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be assert(PY_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
py_parse_node_struct_t *pns_except = (py_parse_node_struct_t*)pn_excepts[i]; py_parse_node_struct_t *pns_except = (py_parse_node_struct_t*)pn_excepts[i];
qstr qstr_exception_local = 0; qstr qstr_exception_local = 0;
int end_finally_label = EMIT(label_new); int end_finally_label = comp_next_label(comp);
if (PY_PARSE_NODE_IS_NULL(pns_except->nodes[0])) { if (PY_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
// this is a catch all exception handler // 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, ...@@ -1276,7 +1283,7 @@ void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except,
int l3; int l3;
if (qstr_exception_local != 0) { if (qstr_exception_local != 0) {
l3 = EMIT(label_new); l3 = comp_next_label(comp);
EMIT(setup_finally, l3); EMIT(setup_finally, l3);
} }
compile_node(comp, pns_except->nodes[1]); 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, ...@@ -1307,7 +1314,7 @@ void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except,
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) { 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) {
// don't understand how the stack works with exceptions, so we force it to return to the correct value // don't understand how the stack works with exceptions, so we force it to return to the correct value
int stack_size = EMIT(get_stack_size); int stack_size = EMIT(get_stack_size);
int l_finally_block = EMIT(label_new); int l_finally_block = comp_next_label(comp);
EMIT(setup_finally, l_finally_block); EMIT(setup_finally, l_finally_block);
if (n_except == 0) { if (n_except == 0) {
assert(PY_PARSE_NODE_IS_NULL(pn_else)); 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 ...@@ -1357,7 +1364,7 @@ void compile_with_stmt_helper(compiler_t *comp, int n, py_parse_node_t *nodes, p
// no more pre-bits, compile the body of the with // no more pre-bits, compile the body of the with
compile_node(comp, body); compile_node(comp, body);
} else { } else {
int l_end = EMIT(label_new); int l_end = comp_next_label(comp);
if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) { if (PY_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
// this pre-bit is of the form "a as b" // this pre-bit is of the form "a as b"
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)nodes[0]; 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) { ...@@ -1490,8 +1497,8 @@ void compile_test_if_expr(compiler_t *comp, py_parse_node_struct_t *pns) {
py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns->nodes[1]; py_parse_node_struct_t *pns_test_if_else = (py_parse_node_struct_t*)pns->nodes[1];
int stack_size = EMIT(get_stack_size); int stack_size = EMIT(get_stack_size);
int l_fail = EMIT(label_new); int l_fail = comp_next_label(comp);
int l_end = EMIT(label_new); int l_end = comp_next_label(comp);
c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
compile_node(comp, pns->nodes[0]); // success value compile_node(comp, pns->nodes[0]); // success value
EMIT(jump, l_end); EMIT(jump, l_end);
...@@ -1521,7 +1528,7 @@ void compile_lambdef(compiler_t *comp, py_parse_node_struct_t *pns) { ...@@ -1521,7 +1528,7 @@ void compile_lambdef(compiler_t *comp, py_parse_node_struct_t *pns) {
} }
void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) { void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) {
int l_end = EMIT(label_new); int l_end = comp_next_label(comp);
int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
for (int i = 0; i < n; i += 1) { for (int i = 0; i < n; i += 1) {
compile_node(comp, pns->nodes[i]); compile_node(comp, pns->nodes[i]);
...@@ -1533,7 +1540,7 @@ void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) { ...@@ -1533,7 +1540,7 @@ void compile_or_test(compiler_t *comp, py_parse_node_struct_t *pns) {
} }
void compile_and_test(compiler_t *comp, py_parse_node_struct_t *pns) { void compile_and_test(compiler_t *comp, py_parse_node_struct_t *pns) {
int l_end = EMIT(label_new); int l_end = comp_next_label(comp);
int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns); int n = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
for (int i = 0; i < n; i += 1) { for (int i = 0; i < n; i += 1) {
compile_node(comp, pns->nodes[i]); compile_node(comp, pns->nodes[i]);
...@@ -1556,7 +1563,7 @@ void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) { ...@@ -1556,7 +1563,7 @@ void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) {
bool multi = (num_nodes > 3); bool multi = (num_nodes > 3);
int l_fail = 0; int l_fail = 0;
if (multi) { if (multi) {
l_fail = EMIT(label_new); l_fail = comp_next_label(comp);
} }
for (int i = 1; i + 1 < num_nodes; i += 2) { for (int i = 1; i + 1 < num_nodes; i += 2) {
compile_node(comp, pns->nodes[i + 1]); compile_node(comp, pns->nodes[i + 1]);
...@@ -1602,7 +1609,7 @@ void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) { ...@@ -1602,7 +1609,7 @@ void compile_comparison(compiler_t *comp, py_parse_node_struct_t *pns) {
} }
} }
if (multi) { if (multi) {
int l_end = EMIT(label_new); int l_end = comp_next_label(comp);
EMIT(jump, l_end); EMIT(jump, l_end);
EMIT(label_assign, l_fail); EMIT(label_assign, l_fail);
EMIT(rot_two); EMIT(rot_two);
...@@ -2255,8 +2262,8 @@ void compile_scope_comp_iter(compiler_t *comp, py_parse_node_t pn_iter, py_parse ...@@ -2255,8 +2262,8 @@ void compile_scope_comp_iter(compiler_t *comp, py_parse_node_t pn_iter, py_parse
// for loop // for loop
py_parse_node_struct_t *pns_comp_for2 = (py_parse_node_struct_t*)pn_iter; py_parse_node_struct_t *pns_comp_for2 = (py_parse_node_struct_t*)pn_iter;
compile_node(comp, pns_comp_for2->nodes[1]); compile_node(comp, pns_comp_for2->nodes[1]);
int l_end2 = EMIT(label_new); int l_end2 = comp_next_label(comp);
int l_top2 = EMIT(label_new); int l_top2 = comp_next_label(comp);
EMIT(get_iter); EMIT(get_iter);
EMIT(label_assign, l_top2); EMIT(label_assign, l_top2);
EMIT(for_iter, l_end2); EMIT(for_iter, l_end2);
...@@ -2302,6 +2309,7 @@ void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) { ...@@ -2302,6 +2309,7 @@ void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) {
void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
comp->pass = pass; comp->pass = pass;
comp->scope_cur = scope; comp->scope_cur = scope;
comp->next_label = 1;
EMIT(start_pass, pass, scope); EMIT(start_pass, pass, scope);
if (comp->pass == PASS_1) { if (comp->pass == PASS_1) {
...@@ -2377,8 +2385,8 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { ...@@ -2377,8 +2385,8 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
EMIT(build_set, 0); EMIT(build_set, 0);
} }
int l_end = EMIT(label_new); int l_end = comp_next_label(comp);
int l_top = EMIT(label_new); int l_top = comp_next_label(comp);
EMIT_COMMON(load_id, comp->qstr___class__, qstr_arg); EMIT_COMMON(load_id, comp->qstr___class__, qstr_arg);
EMIT(label_assign, l_top); EMIT(label_assign, l_top);
EMIT(for_iter, l_end); EMIT(for_iter, l_end);
...@@ -2431,6 +2439,11 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) { ...@@ -2431,6 +2439,11 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
} }
EMIT(end_pass); EMIT(end_pass);
// update maximim number of labels needed
if (comp->next_label > comp->max_num_labels) {
comp->max_num_labels = comp->next_label;
}
} }
void compile_scope_compute_things(compiler_t *comp, scope_t *scope) { void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
...@@ -2489,15 +2502,14 @@ void py_compile(py_parse_node_t pn) { ...@@ -2489,15 +2502,14 @@ void py_compile(py_parse_node_t pn) {
comp->qstr___doc__ = qstr_from_strn_copy("__doc__", 7); comp->qstr___doc__ = qstr_from_strn_copy("__doc__", 7);
comp->qstr_assertion_error = qstr_from_strn_copy("AssertionError", 14); comp->qstr_assertion_error = qstr_from_strn_copy("AssertionError", 14);
comp->max_num_labels = 0;
comp->break_label = 0; comp->break_label = 0;
comp->continue_label = 0; comp->continue_label = 0;
comp->except_nest_level = 0; comp->except_nest_level = 0;
comp->scope_head = NULL; comp->scope_head = NULL;
comp->scope_cur = NULL; comp->scope_cur = NULL;
emit_new_cpython(&comp->emit, &comp->emit_method_table); emit_pass1_new(&comp->emit, &comp->emit_method_table);
//emit_new_bc(&comp->emit, &comp->emit_method_table);
//emit_new_x64(&comp->emit, &comp->emit_method_table);
pn = fold_constants(pn); pn = fold_constants(pn);
scope_new_and_link(comp, SCOPE_MODULE, pn); scope_new_and_link(comp, SCOPE_MODULE, pn);
...@@ -2510,6 +2522,10 @@ void py_compile(py_parse_node_t pn) { ...@@ -2510,6 +2522,10 @@ void py_compile(py_parse_node_t pn) {
compile_scope_compute_things(comp, s); compile_scope_compute_things(comp, s);
} }
//emit_cpython_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
emit_bc_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
//emit_new_x64(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
for (scope_t *s = comp->scope_head; s != NULL; s = s->next) { for (scope_t *s = comp->scope_head; s != NULL; s = s->next) {
compile_scope(comp, s, PASS_2); compile_scope(comp, s, PASS_2);
compile_scope(comp, s, PASS_3); compile_scope(comp, s, PASS_3);
......
...@@ -113,10 +113,13 @@ void emit_common_load_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const e ...@@ -113,10 +113,13 @@ void emit_common_load_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const e
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); 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);
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); 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);
void emit_new_cpython(emit_t **emit, const emit_method_table_t **emit_method_table); void emit_pass1_new(emit_t **emit, const emit_method_table_t **emit_method_table);
void emit_new_bc(emit_t **emit, const emit_method_table_t **emit_method_table); uint emit_pass1_get_max_num_labels(emit_t *emit);
void emit_new_x64(emit_t **emit, const emit_method_table_t **emit_method_table);
void emit_new_thumb(emit_t **emit, const emit_method_table_t **emit_method_table); void emit_cpython_new(emit_t **emit_out, const emit_method_table_t **emit_method_table_out, uint max_num_labels);
void emit_bc_new(emit_t **emit, const emit_method_table_t **emit_method_table, uint max_num_labels);
void emit_x64_new(emit_t **emit, const emit_method_table_t **emit_method_table, uint max_num_labels);
void emit_thumb_new(emit_t **emit, const emit_method_table_t **emit_method_table, uint max_num_labels);
/* /*
void emit_set_native_types(emitter_t *emit, bool do_native_types); void emit_set_native_types(emitter_t *emit, bool do_native_types);
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#include "scope.h" #include "scope.h"
#include "runtime.h" #include "runtime.h"
#include "emit.h" #include "emit.h"
#include "bc.h" #include "vm.h"
struct _emit_t { struct _emit_t {
pass_kind_t pass; pass_kind_t pass;
...@@ -23,7 +23,7 @@ struct _emit_t { ...@@ -23,7 +23,7 @@ struct _emit_t {
scope_t *scope; scope_t *scope;
int max_num_labels; uint max_num_labels;
uint *label_offsets; uint *label_offsets;
uint code_offset; uint code_offset;
...@@ -49,15 +49,8 @@ static void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) { ...@@ -49,15 +49,8 @@ static void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
emit->stack_size = 0; emit->stack_size = 0;
emit->last_emit_was_return_value = false; emit->last_emit_was_return_value = false;
emit->scope = scope; emit->scope = scope;
if (pass == PASS_1) { if (pass == PASS_2) {
scope->unique_code_id = rt_get_new_unique_code_id(); memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
} else if (pass > PASS_1) {
if (emit->label_offsets == NULL) {
emit->label_offsets = m_new(uint, emit->max_num_labels);
}
if (pass == PASS_2) {
memset(emit->label_offsets, -1, emit->max_num_labels * sizeof(uint));
}
} }
emit->code_offset = 0; emit->code_offset = 0;
} }
...@@ -68,13 +61,7 @@ static void emit_bc_end_pass(emit_t *emit) { ...@@ -68,13 +61,7 @@ static void emit_bc_end_pass(emit_t *emit) {
printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size); printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
} }
if (emit->pass == PASS_1) { if (emit->pass == PASS_2) {
// calculate number of labels need
if (emit->next_label > emit->max_num_labels) {
emit->max_num_labels = emit->next_label;
}
} else if (emit->pass == PASS_2) {
// calculate size of code in bytes // calculate size of code in bytes
emit->code_size = emit->code_offset; emit->code_size = emit->code_offset;
emit->code_base = m_new(byte, emit->code_size); emit->code_base = m_new(byte, emit->code_size);
...@@ -160,17 +147,13 @@ int emit_bc_get_stack_size(emit_t *emit) { ...@@ -160,17 +147,13 @@ int emit_bc_get_stack_size(emit_t *emit) {
} }
static void emit_bc_set_stack_size(emit_t *emit, int size) { static void emit_bc_set_stack_size(emit_t *emit, int size) {
if (emit->pass > PASS_1) { emit->stack_size = size;
emit->stack_size = size;
}
} }
static void emit_pre(emit_t *emit, int stack_size_delta) { static void emit_pre(emit_t *emit