Factorise EMIT_COMMON calls, mostly into emit_pass1.

This commit is contained in:
Damien 2013-10-05 14:17:09 +01:00
parent 054848a1b8
commit 4b03e77d4a
7 changed files with 108 additions and 195 deletions

View File

@ -26,7 +26,6 @@ typedef enum {
} pn_kind_t; } pn_kind_t;
#define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg)) #define EMIT(fun, arg...) (comp->emit_method_table->fun(comp->emit, ##arg))
#define EMIT_COMMON(fun, arg...) (emit_common_##fun(comp->pass, comp->scope_cur, comp->emit, comp->emit_method_table, ##arg))
typedef struct _compiler_t { typedef struct _compiler_t {
qstr qstr___class__; qstr qstr___class__;
@ -504,10 +503,10 @@ void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t assign_kind) {
switch (assign_kind) { switch (assign_kind) {
case ASSIGN_STORE: case ASSIGN_STORE:
case ASSIGN_AUG_STORE: case ASSIGN_AUG_STORE:
EMIT_COMMON(store_id, arg); EMIT(store_id, arg);
break; break;
case ASSIGN_AUG_LOAD: case ASSIGN_AUG_LOAD:
EMIT_COMMON(load_id, comp->qstr___class__, arg); EMIT(load_id, arg);
break; break;
} }
} else { } else {
@ -779,18 +778,18 @@ void compile_decorated(compiler_t *comp, py_parse_node_struct_t *pns) {
} }
// store func/class object into name // store func/class object into name
EMIT_COMMON(store_id, body_name); EMIT(store_id, body_name);
} }
void compile_funcdef(compiler_t *comp, py_parse_node_struct_t *pns) { void compile_funcdef(compiler_t *comp, py_parse_node_struct_t *pns) {
qstr fname = compile_funcdef_helper(comp, pns); qstr fname = compile_funcdef_helper(comp, pns);
// store function object into function name // store function object into function name
EMIT_COMMON(store_id, fname); EMIT(store_id, fname);
} }
void c_del_stmt(compiler_t *comp, py_parse_node_t pn) { void c_del_stmt(compiler_t *comp, py_parse_node_t pn) {
if (PY_PARSE_NODE_IS_ID(pn)) { if (PY_PARSE_NODE_IS_ID(pn)) {
EMIT_COMMON(delete_id, PY_PARSE_NODE_LEAF_ARG(pn)); EMIT(delete_id, PY_PARSE_NODE_LEAF_ARG(pn));
} else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) { } else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn; py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
@ -995,7 +994,7 @@ void compile_dotted_as_name(compiler_t *comp, py_parse_node_t pn) {
EMIT(load_const_tok, PY_TOKEN_KW_NONE); EMIT(load_const_tok, PY_TOKEN_KW_NONE);
qstr q1, q2; qstr q1, q2;
do_import_name(comp, pn, &q1, &q2); do_import_name(comp, pn, &q1, &q2);
EMIT_COMMON(store_id, q1); EMIT(store_id, q1);
} }
void compile_import_name(compiler_t *comp, py_parse_node_struct_t *pns) { void compile_import_name(compiler_t *comp, py_parse_node_struct_t *pns) {
@ -1042,9 +1041,9 @@ void compile_import_from(compiler_t *comp, py_parse_node_struct_t *pns) {
qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id qstr id2 = PY_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
EMIT(import_from, id2); EMIT(import_from, id2);
if (PY_PARSE_NODE_IS_NULL(pns3->nodes[1])) { if (PY_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
EMIT_COMMON(store_id, id2); EMIT(store_id, id2);
} else { } else {
EMIT_COMMON(store_id, PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1])); EMIT(store_id, PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
} }
} }
EMIT(pop_top); EMIT(pop_top);
@ -1082,7 +1081,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 = comp_next_label(comp); 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(load_id, comp->qstr_assertion_error);
if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) { if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
// assertion message // assertion message
compile_node(comp, pns->nodes[1]); compile_node(comp, pns->nodes[1]);
@ -1276,7 +1275,7 @@ void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except,
if (qstr_exception_local == 0) { if (qstr_exception_local == 0) {
EMIT(pop_top); EMIT(pop_top);
} else { } else {
EMIT_COMMON(store_id, qstr_exception_local); EMIT(store_id, qstr_exception_local);
} }
EMIT(pop_top); EMIT(pop_top);
@ -1295,8 +1294,8 @@ void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except,
EMIT(load_const_tok, PY_TOKEN_KW_NONE); EMIT(load_const_tok, PY_TOKEN_KW_NONE);
EMIT(label_assign, l3); EMIT(label_assign, l3);
EMIT(load_const_tok, PY_TOKEN_KW_NONE); EMIT(load_const_tok, PY_TOKEN_KW_NONE);
EMIT_COMMON(store_id, qstr_exception_local); EMIT(store_id, qstr_exception_local);
EMIT_COMMON(delete_id, qstr_exception_local); EMIT(delete_id, qstr_exception_local);
EMIT(end_finally); EMIT(end_finally);
} }
EMIT(jump, l2); EMIT(jump, l2);
@ -2051,7 +2050,7 @@ void compile_dictorsetmaker_item(compiler_t *comp, py_parse_node_struct_t *pns)
void compile_classdef(compiler_t *comp, py_parse_node_struct_t *pns) { void compile_classdef(compiler_t *comp, py_parse_node_struct_t *pns) {
qstr cname = compile_classdef_helper(comp, pns); qstr cname = compile_classdef_helper(comp, pns);
// store class object into class name // store class object into class name
EMIT_COMMON(store_id, cname); EMIT(store_id, cname);
} }
void compile_arglist_star(compiler_t *comp, py_parse_node_struct_t *pns) { void compile_arglist_star(compiler_t *comp, py_parse_node_struct_t *pns) {
@ -2129,7 +2128,7 @@ void compile_node(compiler_t *comp, py_parse_node_t pn) {
} else if (PY_PARSE_NODE_IS_LEAF(pn)) { } else if (PY_PARSE_NODE_IS_LEAF(pn)) {
int arg = PY_PARSE_NODE_LEAF_ARG(pn); int arg = PY_PARSE_NODE_LEAF_ARG(pn);
switch (PY_PARSE_NODE_LEAF_KIND(pn)) { switch (PY_PARSE_NODE_LEAF_KIND(pn)) {
case PY_PARSE_NODE_ID: EMIT_COMMON(load_id, comp->qstr___class__, arg); break; case PY_PARSE_NODE_ID: EMIT(load_id, arg); break;
case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break; case PY_PARSE_NODE_SMALL_INT: EMIT(load_const_small_int, arg); break;
case PY_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break; case PY_PARSE_NODE_INTEGER: EMIT(load_const_int, arg); break;
case PY_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break; case PY_PARSE_NODE_DECIMAL: EMIT(load_const_dec, arg); break;
@ -2300,7 +2299,7 @@ void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) {
if (kind == PY_PARSE_NODE_STRING) { if (kind == PY_PARSE_NODE_STRING) {
compile_node(comp, pns->nodes[0]); // a doc string compile_node(comp, pns->nodes[0]); // a doc string
// store doc string // store doc string
EMIT_COMMON(store_id, comp->qstr___doc__); EMIT(store_id, comp->qstr___doc__);
} }
} }
} }
@ -2387,7 +2386,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
int l_end = comp_next_label(comp); int l_end = comp_next_label(comp);
int l_top = comp_next_label(comp); int l_top = comp_next_label(comp);
EMIT_COMMON(load_id, comp->qstr___class__, qstr_arg); EMIT(load_id, qstr_arg);
EMIT(label_assign, l_top); EMIT(label_assign, l_top);
EMIT(for_iter, l_end); EMIT(for_iter, l_end);
c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE); c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
@ -2418,12 +2417,12 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
scope->num_params = 1; // __locals__ is the parameter scope->num_params = 1; // __locals__ is the parameter
} }
EMIT_COMMON(load_id, comp->qstr___class__, comp->qstr___locals__); EMIT(load_id, comp->qstr___locals__);
EMIT(store_locals); EMIT(store_locals);
EMIT_COMMON(load_id, comp->qstr___class__, comp->qstr___name__); EMIT(load_id, comp->qstr___name__);
EMIT_COMMON(store_id, comp->qstr___module__); EMIT(store_id, comp->qstr___module__);
EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
EMIT_COMMON(store_id, comp->qstr___qualname__); EMIT(store_id, comp->qstr___qualname__);
check_for_doc_string(comp, pns->nodes[2]); check_for_doc_string(comp, pns->nodes[2]);
compile_node(comp, pns->nodes[2]); // 2 is class body compile_node(comp, pns->nodes[2]); // 2 is class body
@ -2509,7 +2508,7 @@ void py_compile(py_parse_node_t pn) {
comp->scope_head = NULL; comp->scope_head = NULL;
comp->scope_cur = NULL; comp->scope_cur = NULL;
emit_pass1_new(&comp->emit, &comp->emit_method_table); emit_pass1_new(&comp->emit, &comp->emit_method_table, comp->qstr___class__);
pn = fold_constants(pn); pn = fold_constants(pn);
scope_new_and_link(comp, SCOPE_MODULE, pn); scope_new_and_link(comp, SCOPE_MODULE, pn);
@ -2522,9 +2521,9 @@ 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_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_bc_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels);
emit_x64_new(&comp->emit, &comp->emit_method_table, comp->max_num_labels); //emit_x64_new(&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);

107
py/emit.h
View File

@ -24,6 +24,10 @@ typedef struct _emit_method_table_t {
int (*get_stack_size)(emit_t *emit); int (*get_stack_size)(emit_t *emit);
void (*set_stack_size)(emit_t *emit, int size); void (*set_stack_size)(emit_t *emit, int size);
void (*load_id)(emit_t *emit, qstr qstr);
void (*store_id)(emit_t *emit, qstr qstr);
void (*delete_id)(emit_t *emit, qstr qstr);
void (*label_assign)(emit_t *emit, int l); void (*label_assign)(emit_t *emit, int l);
void (*import_name)(emit_t *emit, qstr qstr); void (*import_name)(emit_t *emit, qstr qstr);
void (*import_from)(emit_t *emit, qstr qstr); void (*import_from)(emit_t *emit, qstr qstr);
@ -108,107 +112,12 @@ typedef struct _emit_method_table_t {
void (*yield_from)(emit_t *emit); void (*yield_from)(emit_t *emit);
} emit_method_table_t; } emit_method_table_t;
void emit_common_load_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const emit_method_table_t *emit_method_table, qstr qstr___class__, qstr qstr); void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, 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_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, 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(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
void emit_pass1_new(emit_t **emit, const emit_method_table_t **emit_method_table);
uint emit_pass1_get_max_num_labels(emit_t *emit);
void emit_pass1_new(emit_t **emit, const emit_method_table_t **emit_method_table, qstr qstr___class__);
void emit_cpython_new(emit_t **emit_out, const emit_method_table_t **emit_method_table_out, uint max_num_labels); 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_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_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_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_start_pass(emitter_t *emit, pass_kind_t pass, scope_t *scope);
void emit_end_pass(emitter_t *emit);
bool emit_last_emit_was_return_value(emitter_t *emit);
int emit_get_stack_size(emitter_t *emit);
void emit_set_stack_size(emitter_t *emit, int size);
int emit_label_new(emitter_t *emit);
void emit_label_assign(emitter_t *emit, int l);
void emit_import_name(emitter_t *emit, qstr qstr);
void emit_import_from(emitter_t *emit, qstr qstr);
void emit_import_star(emitter_t *emit);
void emit_load_const_tok(emitter_t *emit, py_token_kind_t tok);
void emit_load_const_small_int(emitter_t *emit, int arg);
void emit_load_const_int(emitter_t *emit, qstr qstr);
void emit_load_const_dec(emitter_t *emit, qstr qstr);
void emit_load_const_id(emitter_t *emit, qstr qstr);
void emit_load_const_str(emitter_t *emit, qstr qstr, bool bytes);
void emit_load_const_verbatim_start(emitter_t *emit);
void emit_load_const_verbatim_int(emitter_t *emit, int val);
void emit_load_const_verbatim_str(emitter_t *emit, const char *str);
void emit_load_const_verbatim_strn(emitter_t *emit, const char *str, int len);
void emit_load_const_verbatim_quoted_str(emitter_t *emit, qstr qstr, bool bytes);
void emit_load_const_verbatim_end(emitter_t *emit);
void emit_load_fast(emitter_t *emit, qstr qstr, int local_num);
void emit_load_name(emitter_t *emit, qstr qstr);
void emit_load_global(emitter_t *emit, qstr qstr);
void emit_load_deref(emitter_t *emit, qstr qstr);
void emit_load_closure(emitter_t *emit, qstr qstr);
void emit_load_attr(emitter_t *emit, qstr qstr);
void emit_load_method(emitter_t *emit, qstr qstr);
void emit_load_build_class(emitter_t *emit);
void emit_store_fast(emitter_t *emit, qstr qstr, int local_num);
void emit_store_name(emitter_t *emit, qstr qstr);
void emit_store_global(emitter_t *emit, qstr qstr);
void emit_store_deref(emitter_t *emit, qstr qstr);
void emit_store_attr(emitter_t *emit, qstr qstr);
void emit_store_locals(emitter_t *emit);
void emit_store_subscr(emitter_t *emit);
void emit_delete_fast(emitter_t *emit, qstr qstr, int local_num);
void emit_delete_name(emitter_t *emit, qstr qstr);
void emit_delete_global(emitter_t *emit, qstr qstr);
void emit_delete_deref(emitter_t *emit, qstr qstr);
void emit_delete_attr(emitter_t *emit, qstr qstr);
void emit_delete_subscr(emitter_t *emit);
void emit_dup_top(emitter_t *emit);
void emit_dup_top_two(emitter_t *emit);
void emit_pop_top(emitter_t *emit);
void emit_rot_two(emitter_t *emit);
void emit_rot_three(emitter_t *emit);
void emit_jump(emitter_t *emit, int label);
void emit_pop_jump_if_true(emitter_t *emit, int label);
void emit_pop_jump_if_false(emitter_t *emit, int label);
void emit_jump_if_true_or_pop(emitter_t *emit, int label);
void emit_jump_if_false_or_pop(emitter_t *emit, int label);
void emit_setup_loop(emitter_t *emit, int label);
void emit_break_loop(emitter_t *emit, int label);
void emit_continue_loop(emitter_t *emit, int label);
void emit_setup_with(emitter_t *emit, int label);
void emit_with_cleanup(emitter_t *emit);
void emit_setup_except(emitter_t *emit, int label);
void emit_setup_finally(emitter_t *emit, int label);
void emit_end_finally(emitter_t *emit);
void emit_get_iter(emitter_t *emit); // tos = getiter(tos)
void emit_for_iter(emitter_t *emit, int label);
void emit_for_iter_end(emitter_t *emit);
void emit_pop_block(emitter_t *emit);
void emit_pop_except(emitter_t *emit);
void emit_unary_op(emitter_t *emit, rt_unary_op_t op);
void emit_binary_op(emitter_t *emit, rt_binary_op_t op);
void emit_compare_op(emitter_t *emit, rt_compare_op_t op);
void emit_build_tuple(emitter_t *emit, int n_args);
void emit_build_list(emitter_t *emit, int n_args);
void emit_list_append(emitter_t *emit, int list_stack_index);
void emit_build_map(emitter_t *emit, int n_args);
void emit_store_map(emitter_t *emit);
void emit_map_add(emitter_t *emit, int map_stack_index);
void emit_build_set(emitter_t *emit, int n_args);
void emit_set_add(emitter_t *emit, int set_stack_index);
void emit_build_slice(emitter_t *emit, int n_args);
void emit_unpack_sequence(emitter_t *emit, int n_args);
void emit_unpack_ex(emitter_t *emit, int n_left, int n_right);
void emit_make_function(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params);
void emit_make_closure(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params);
void emit_call_function(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
void emit_call_method(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
void emit_return_value(emitter_t *emit);
void emit_raise_varargs(emitter_t *emit, int n_args);
void emit_yield_value(emitter_t *emit);
void emit_yield_from(emitter_t *emit);
*/

View File

@ -32,6 +32,9 @@ struct _emit_t {
byte dummy_data[8]; byte dummy_data[8];
}; };
// forward declaration
static const emit_method_table_t emit_bc_method_table;
uint emit_bc_get_code_size(emit_t* emit) { uint emit_bc_get_code_size(emit_t* emit) {
return emit->code_size; return emit->code_size;
} }
@ -150,6 +153,18 @@ static void emit_bc_set_stack_size(emit_t *emit, int size) {
emit->stack_size = size; emit->stack_size = size;
} }
static void emit_bc_load_id(emit_t *emit, qstr qstr) {
emit_common_load_id(emit, &emit_bc_method_table, emit->scope, qstr);
}
static void emit_bc_store_id(emit_t *emit, qstr qstr) {
emit_common_store_id(emit, &emit_bc_method_table, emit->scope, qstr);
}
static void emit_bc_delete_id(emit_t *emit, qstr qstr) {
emit_common_delete_id(emit, &emit_bc_method_table, emit->scope, qstr);
}
static void emit_pre(emit_t *emit, int stack_size_delta) { static void emit_pre(emit_t *emit, int stack_size_delta) {
emit->stack_size += stack_size_delta; emit->stack_size += stack_size_delta;
if (emit->stack_size > emit->scope->stack_size) { if (emit->stack_size > emit->scope->stack_size) {
@ -665,6 +680,10 @@ static const emit_method_table_t emit_bc_method_table = {
emit_bc_get_stack_size, emit_bc_get_stack_size,
emit_bc_set_stack_size, emit_bc_set_stack_size,
emit_bc_load_id,
emit_bc_store_id,
emit_bc_delete_id,
emit_bc_label_assign, emit_bc_label_assign,
emit_bc_import_name, emit_bc_import_name,
emit_bc_import_from, emit_bc_import_from,

View File

@ -14,85 +14,31 @@
#define EMIT(fun, arg...) (emit_method_table->fun(emit, ##arg)) #define EMIT(fun, arg...) (emit_method_table->fun(emit, ##arg))
void emit_common_load_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const emit_method_table_t *emit_method_table, qstr qstr___class__, qstr qstr) { void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) {
id_info_t *id_info = NULL; // assumes pass is greater than 1, ie that all identifiers are defined in the scope
if (pass == PASS_1) {
// name adding/lookup
bool added;
id_info = scope_find_or_add_id(scope, qstr, &added);
if (added) {
if (strcmp(qstr_str(qstr), "AssertionError") == 0) {
id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
// TODO how much of a hack is this?
} else if (strcmp(qstr_str(qstr), "super") == 0 && scope->kind == SCOPE_FUNCTION) {
// special case, super is a global, and also counts as use of __class__
id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
id_info_t *id_info2 = scope_find_local_in_parent(scope, qstr___class__);
if (id_info2 != NULL) {
id_info2 = scope_find_or_add_id(scope, qstr___class__, &added);
if (added) {
id_info2->kind = ID_INFO_KIND_FREE;
scope_close_over_in_parents(scope, qstr___class__);
}
}
} else {
id_info_t *id_info2 = scope_find_local_in_parent(scope, qstr);
if (id_info2 != NULL && (id_info2->kind == ID_INFO_KIND_LOCAL || id_info2->kind == ID_INFO_KIND_CELL || id_info2->kind == ID_INFO_KIND_FREE)) {
id_info->kind = ID_INFO_KIND_FREE;
scope_close_over_in_parents(scope, qstr);
} else {
id_info->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
}
}
}
} else {
id_info = scope_find(scope, qstr);
}
assert(id_info != NULL); // TODO can this ever fail? id_info_t *id = scope_find(scope, qstr);
assert(id != NULL); // TODO can this ever fail?
// call the emit backend with the correct code // call the emit backend with the correct code
if (id_info == NULL || id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
EMIT(load_name, qstr); EMIT(load_name, qstr);
} else if (id_info->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) { } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
EMIT(load_global, qstr); EMIT(load_global, qstr);
} else if (id_info->kind == ID_INFO_KIND_LOCAL) { } else if (id->kind == ID_INFO_KIND_LOCAL) {
EMIT(load_fast, qstr, id_info->local_num); EMIT(load_fast, qstr, id->local_num);
} else if (id_info->kind == ID_INFO_KIND_CELL || id_info->kind == ID_INFO_KIND_FREE) { } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
EMIT(load_deref, qstr); EMIT(load_deref, qstr);
} else { } else {
assert(0); assert(0);
} }
} }
static id_info_t *get_id_for_modification(pass_kind_t pass, scope_t *scope, qstr qstr) { void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) {
id_info_t *id_info = NULL; // assumes pass is greater than 1, ie that all identifiers are defined in the scope
if (pass == PASS_1) {
// name adding/lookup
bool added;
id_info = scope_find_or_add_id(scope, qstr, &added);
if (added) {
if (scope->kind == SCOPE_MODULE || scope->kind == SCOPE_CLASS) {
id_info->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
} else {
id_info->kind = ID_INFO_KIND_LOCAL;
}
} else if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id_info->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
// rebind as a local variable
id_info->kind = ID_INFO_KIND_LOCAL;
}
} else {
id_info = scope_find(scope, qstr);
}
assert(id_info != NULL); // TODO can this ever fail? id_info_t *id = scope_find(scope, qstr);
assert(id != NULL); // TODO can this ever fail?
return id_info;
}
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) {
// create/get the id info
id_info_t *id = get_id_for_modification(pass, scope, qstr);
// call the emit backend with the correct code // call the emit backend with the correct code
if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
@ -108,9 +54,11 @@ void emit_common_store_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const
} }
} }
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(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) {
// create/get the id info // assumes pass is greater than 1, ie that all identifiers are defined in the scope
id_info_t *id = get_id_for_modification(pass, scope, qstr);
id_info_t *id = scope_find(scope, qstr);
assert(id != NULL); // TODO can this ever fail?
// call the emit backend with the correct code // call the emit backend with the correct code
if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {

View File

@ -29,6 +29,10 @@ struct _emit_t {
int *label_offsets; int *label_offsets;
}; };
// forward declarations
static const emit_method_table_t emit_cpy_method_table;
static void emit_cpy_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes);
static void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) { static void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
} }
@ -63,6 +67,18 @@ static void emit_cpy_set_stack_size(emit_t *emit, int size) {
emit->stack_size = size; emit->stack_size = size;
} }
static void emit_cpy_load_id(emit_t *emit, qstr qstr) {
emit_common_load_id(emit, &emit_cpy_method_table, emit->scope, qstr);
}
static void emit_cpy_store_id(emit_t *emit, qstr qstr) {
emit_common_store_id(emit, &emit_cpy_method_table, emit->scope, qstr);
}
static void emit_cpy_delete_id(emit_t *emit, qstr qstr) {
emit_common_delete_id(emit, &emit_cpy_method_table, emit->scope, qstr);
}
static void emit_pre(emit_t *emit, int stack_size_delta, int byte_code_size) { static void emit_pre(emit_t *emit, int stack_size_delta, int byte_code_size) {
emit->stack_size += stack_size_delta; emit->stack_size += stack_size_delta;
if (emit->stack_size > emit->scope->stack_size) { if (emit->stack_size > emit->scope->stack_size) {
@ -156,7 +172,6 @@ static void emit_cpy_load_const_id(emit_t *emit, qstr qstr) {
} }
} }
static void emit_cpy_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes);
static void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) { static void emit_cpy_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
emit_pre(emit, 1, 3); emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) { if (emit->pass == PASS_3) {
@ -818,6 +833,10 @@ static const emit_method_table_t emit_cpy_method_table = {
emit_cpy_get_stack_size, emit_cpy_get_stack_size,
emit_cpy_set_stack_size, emit_cpy_set_stack_size,
emit_cpy_load_id,
emit_cpy_store_id,
emit_cpy_delete_id,
emit_cpy_label_assign, emit_cpy_label_assign,
emit_cpy_import_name, emit_cpy_import_name,
emit_cpy_import_from, emit_cpy_import_from,

View File

@ -53,6 +53,9 @@ struct _emit_t {
bool do_native_types; bool do_native_types;
}; };
// forward declaration
static const emit_method_table_t emit_x64_method_table;
static void emit_x64_set_native_types(emit_t *emit, bool do_native_types) { static void emit_x64_set_native_types(emit_t *emit, bool do_native_types) {
emit->do_native_types = do_native_types; emit->do_native_types = do_native_types;
} }
@ -123,6 +126,18 @@ static void emit_x64_set_stack_size(emit_t *emit, int size) {
emit->stack_size = size; emit->stack_size = size;
} }
static void emit_x64_load_id(emit_t *emit, qstr qstr) {
emit_common_load_id(emit, &emit_x64_method_table, emit->scope, qstr);
}
static void emit_x64_store_id(emit_t *emit, qstr qstr) {
emit_common_store_id(emit, &emit_x64_method_table, emit->scope, qstr);
}
static void emit_x64_delete_id(emit_t *emit, qstr qstr) {
emit_common_delete_id(emit, &emit_x64_method_table, emit->scope, qstr);
}
static void adjust_stack(emit_t *emit, int stack_size_delta) { static void adjust_stack(emit_t *emit, int stack_size_delta) {
emit->stack_size += stack_size_delta; emit->stack_size += stack_size_delta;
assert(emit->stack_size >= 0); assert(emit->stack_size >= 0);
@ -671,6 +686,10 @@ static const emit_method_table_t emit_x64_method_table = {
emit_x64_get_stack_size, emit_x64_get_stack_size,
emit_x64_set_stack_size, emit_x64_set_stack_size,
emit_x64_load_id,
emit_x64_store_id,
emit_x64_delete_id,
emit_x64_label_assign, emit_x64_label_assign,
emit_x64_import_name, emit_x64_import_name,
emit_x64_import_from, emit_x64_import_from,

View File

@ -40,7 +40,7 @@ int main(int argc, char **argv) {
py_lexer_free(lex); py_lexer_free(lex);
if (1) { if (0) {
// execute it // execute it
py_obj_t module_fun = rt_make_function_from_id(1); py_obj_t module_fun = rt_make_function_from_id(1);
if (module_fun != py_const_none) { if (module_fun != py_const_none) {