Restructure emit so it goes through a method table.

This commit is contained in:
Damien 2013-10-05 12:19:06 +01:00
parent 492d082455
commit 415eb6f850
11 changed files with 837 additions and 445 deletions

2
py/.gitignore vendored
View File

@ -1 +1,3 @@
*.o
py
*.py

View File

@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -Wall -ansi -std=gnu99 -Os #-DNDEBUG
CFLAGS = -Wall -ansi -std=gnu99 -Os -DEMIT_ENABLE_CPY -DEMIT_ENABLE_X64 #-DNDEBUG
LDFLAGS =
SRC = \

View File

@ -113,7 +113,8 @@ asm_x64_t* asm_x64_new() {
void asm_x64_free(asm_x64_t* as, bool free_code) {
if (free_code) {
m_free(as->code_base);
// need to un-mmap
//m_free(as->code_base);
}
/*
if (as->label != NULL) {
@ -154,6 +155,8 @@ void asm_x64_end_pass(asm_x64_t *as) {
// calculate size of code in bytes
as->code_size = as->code_offset;
as->code_base = m_new(byte, as->code_size);
//uint actual_alloc;
//as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
printf("code_size: %u\n", as->code_size);
}
@ -611,11 +614,12 @@ void asm_x64_call_i1(asm_x64_t* as, void* func, int i1)
*/
void asm_x64_call_ind(asm_x64_t* as, void *ptr, int temp_r64) {
/*
asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
asm_x64_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R64(2) | MODRM_RM_REG | MODRM_RM_R64(temp_r64));
*/
// this reduces code size by 2 bytes per call, but doesn't seem to speed it up at all
// doesn't work anymore because calls are 64 bits away
/*
asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
*/
}

View File

@ -15,6 +15,7 @@
#include "emit.h"
// TODO need to mangle __attr names
// TODO add #define to enable/disable CPython compatibility
typedef enum {
PN_none = 0,
@ -24,8 +25,8 @@ typedef enum {
PN_maximum_number_of,
} pn_kind_t;
#define EMIT(fun, arg...) (emit_##fun(comp->emit, ##arg))
#define EMIT_COMMON(fun, arg...) (emit_common_##fun(comp->pass, comp->scope_cur, ##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 {
qstr qstr___class__;
@ -53,7 +54,8 @@ typedef struct _compiler_t {
scope_t *scope_head;
scope_t *scope_cur;
emitter_t *emit;
emit_t *emit;
const emit_method_table_t *emit_method_table;
} compiler_t;
py_parse_node_t fold_constants(py_parse_node_t pn) {
@ -495,10 +497,10 @@ void c_assign(compiler_t *comp, py_parse_node_t pn, assign_kind_t assign_kind) {
switch (assign_kind) {
case ASSIGN_STORE:
case ASSIGN_AUG_STORE:
EMIT_COMMON(store_id, comp->emit, arg);
EMIT_COMMON(store_id, arg);
break;
case ASSIGN_AUG_LOAD:
EMIT_COMMON(load_id, comp->qstr___class__, comp->emit, arg);
EMIT_COMMON(load_id, comp->qstr___class__, arg);
break;
}
} else {
@ -770,18 +772,18 @@ void compile_decorated(compiler_t *comp, py_parse_node_struct_t *pns) {
}
// store func/class object into name
EMIT_COMMON(store_id, comp->emit, body_name);
EMIT_COMMON(store_id, body_name);
}
void compile_funcdef(compiler_t *comp, py_parse_node_struct_t *pns) {
qstr fname = compile_funcdef_helper(comp, pns);
// store function object into function name
EMIT_COMMON(store_id, comp->emit, fname);
EMIT_COMMON(store_id, fname);
}
void c_del_stmt(compiler_t *comp, py_parse_node_t pn) {
if (PY_PARSE_NODE_IS_ID(pn)) {
EMIT_COMMON(delete_id, comp->emit, PY_PARSE_NODE_LEAF_ARG(pn));
EMIT_COMMON(delete_id, PY_PARSE_NODE_LEAF_ARG(pn));
} else if (PY_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
py_parse_node_struct_t *pns = (py_parse_node_struct_t*)pn;
@ -986,7 +988,7 @@ void compile_dotted_as_name(compiler_t *comp, py_parse_node_t pn) {
EMIT(load_const_tok, PY_TOKEN_KW_NONE);
qstr q1, q2;
do_import_name(comp, pn, &q1, &q2);
EMIT_COMMON(store_id, comp->emit, q1);
EMIT_COMMON(store_id, q1);
}
void compile_import_name(compiler_t *comp, py_parse_node_struct_t *pns) {
@ -1033,9 +1035,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
EMIT(import_from, id2);
if (PY_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
EMIT_COMMON(store_id, comp->emit, id2);
EMIT_COMMON(store_id, id2);
} else {
EMIT_COMMON(store_id, comp->emit, PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
EMIT_COMMON(store_id, PY_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
}
}
EMIT(pop_top);
@ -1043,25 +1045,29 @@ void compile_import_from(compiler_t *comp, py_parse_node_struct_t *pns) {
}
void compile_global_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
if (comp->pass == PASS_1) {
if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
EMIT_COMMON(declare_global, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
} else {
pns = (py_parse_node_struct_t*)pns->nodes[0];
int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
for (int i = 0; i < num_nodes; i++) {
EMIT_COMMON(declare_global, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
scope_declare_global(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
}
}
}
}
void compile_nonlocal_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
if (comp->pass == PASS_1) {
if (PY_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
EMIT_COMMON(declare_nonlocal, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
} else {
pns = (py_parse_node_struct_t*)pns->nodes[0];
int num_nodes = PY_PARSE_NODE_STRUCT_NUM_NODES(pns);
for (int i = 0; i < num_nodes; i++) {
EMIT_COMMON(declare_nonlocal, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
scope_declare_nonlocal(comp->scope_cur, PY_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
}
}
}
}
@ -1069,7 +1075,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) {
int l_end = EMIT(label_new);
c_if_cond(comp, pns->nodes[0], true, l_end);
EMIT_COMMON(load_id, comp->qstr___class__, comp->emit, comp->qstr_assertion_error);
EMIT_COMMON(load_id, comp->qstr___class__, comp->qstr_assertion_error);
if (!PY_PARSE_NODE_IS_NULL(pns->nodes[1])) {
// assertion message
compile_node(comp, pns->nodes[1]);
@ -1090,7 +1096,7 @@ void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
compile_node(comp, pns->nodes[1]); // if block
//if (!(PY_PARSE_NODE_IS_NULL(pns->nodes[2]) && PY_PARSE_NODE_IS_NULL(pns->nodes[3]))) { // optimisation; doesn't align with CPython
// jump over elif/else blocks if they exist
if (!emit_last_emit_was_return_value(comp->emit)) { // simple optimisation to align with CPython
if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
EMIT(jump, l_end);
}
//}
@ -1111,7 +1117,7 @@ void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
compile_node(comp, pns_elif2->nodes[1]); // elif block
if (!emit_last_emit_was_return_value(comp->emit)) { // simple optimisation to align with CPython
if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
EMIT(jump, l_end);
}
EMIT(label_assign, l_fail);
@ -1124,7 +1130,7 @@ void compile_if_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
compile_node(comp, pns_elif->nodes[1]); // elif block
if (!emit_last_emit_was_return_value(comp->emit)) { // simple optimisation to align with CPython
if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
EMIT(jump, l_end);
}
EMIT(label_assign, l_fail);
@ -1153,7 +1159,7 @@ void compile_while_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
EMIT(label_assign, continue_label);
c_if_cond(comp, pns->nodes[0], false, done_label); // condition
compile_node(comp, pns->nodes[1]); // body
if (!emit_last_emit_was_return_value(comp->emit)) {
if (!EMIT(last_emit_was_return_value)) {
EMIT(jump, continue_label);
}
EMIT(label_assign, done_label);
@ -1194,7 +1200,7 @@ void compile_for_stmt(compiler_t *comp, py_parse_node_struct_t *pns) {
EMIT(for_iter, pop_label);
c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
compile_node(comp, pns->nodes[2]); // body
if (!emit_last_emit_was_return_value(comp->emit)) {
if (!EMIT(last_emit_was_return_value)) {
EMIT(jump, for_label);
}
EMIT(label_assign, pop_label);
@ -1263,7 +1269,7 @@ void compile_try_except(compiler_t *comp, py_parse_node_t pn_body, int n_except,
if (qstr_exception_local == 0) {
EMIT(pop_top);
} else {
EMIT_COMMON(store_id, comp->emit, qstr_exception_local);
EMIT_COMMON(store_id, qstr_exception_local);
}
EMIT(pop_top);
@ -1282,8 +1288,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(label_assign, l3);
EMIT(load_const_tok, PY_TOKEN_KW_NONE);
EMIT_COMMON(store_id, comp->emit, qstr_exception_local);
EMIT_COMMON(delete_id, comp->emit, qstr_exception_local);
EMIT_COMMON(store_id, qstr_exception_local);
EMIT_COMMON(delete_id, qstr_exception_local);
EMIT(end_finally);
}
EMIT(jump, l2);
@ -2038,7 +2044,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) {
qstr cname = compile_classdef_helper(comp, pns);
// store class object into class name
EMIT_COMMON(store_id, comp->emit, cname);
EMIT_COMMON(store_id, cname);
}
void compile_arglist_star(compiler_t *comp, py_parse_node_struct_t *pns) {
@ -2116,7 +2122,7 @@ void compile_node(compiler_t *comp, py_parse_node_t pn) {
} else if (PY_PARSE_NODE_IS_LEAF(pn)) {
int arg = PY_PARSE_NODE_LEAF_ARG(pn);
switch (PY_PARSE_NODE_LEAF_KIND(pn)) {
case PY_PARSE_NODE_ID: EMIT_COMMON(load_id, comp->qstr___class__, comp->emit, arg); break;
case PY_PARSE_NODE_ID: EMIT_COMMON(load_id, comp->qstr___class__, 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_DECIMAL: EMIT(load_const_dec, arg); break;
@ -2287,7 +2293,7 @@ void check_for_doc_string(compiler_t *comp, py_parse_node_t pn) {
if (kind == PY_PARSE_NODE_STRING) {
compile_node(comp, pns->nodes[0]); // a doc string
// store doc string
EMIT_COMMON(store_id, comp->emit, comp->qstr___doc__);
EMIT_COMMON(store_id, comp->qstr___doc__);
}
}
}
@ -2296,7 +2302,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) {
comp->pass = pass;
comp->scope_cur = scope;
emit_start_pass(comp->emit, pass, scope);
EMIT(start_pass, pass, scope);
if (comp->pass == PASS_1) {
scope->stack_size = 0;
@ -2328,7 +2334,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
compile_node(comp, pns->nodes[3]); // 3 is function body
// emit return if it wasn't the last opcode
if (!emit_last_emit_was_return_value(comp->emit)) {
if (!EMIT(last_emit_was_return_value)) {
EMIT(load_const_tok, PY_TOKEN_KW_NONE);
EMIT(return_value);
}
@ -2373,7 +2379,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
int l_end = EMIT(label_new);
int l_top = EMIT(label_new);
EMIT_COMMON(load_id, comp->qstr___class__, comp->emit, qstr_arg);
EMIT_COMMON(load_id, comp->qstr___class__, qstr_arg);
EMIT(label_assign, l_top);
EMIT(for_iter, l_end);
c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
@ -2404,12 +2410,12 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
scope->num_params = 1; // __locals__ is the parameter
}
EMIT_COMMON(load_id, comp->qstr___class__, comp->emit, comp->qstr___locals__);
EMIT_COMMON(load_id, comp->qstr___class__, comp->qstr___locals__);
EMIT(store_locals);
EMIT_COMMON(load_id, comp->qstr___class__, comp->emit, comp->qstr___name__);
EMIT_COMMON(store_id, comp->emit, comp->qstr___module__);
EMIT_COMMON(load_id, comp->qstr___class__, comp->qstr___name__);
EMIT_COMMON(store_id, comp->qstr___module__);
EMIT(load_const_id, PY_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // 0 is class name
EMIT_COMMON(store_id, comp->emit, comp->qstr___qualname__);
EMIT_COMMON(store_id, comp->qstr___qualname__);
check_for_doc_string(comp, pns->nodes[2]);
compile_node(comp, pns->nodes[2]); // 2 is class body
@ -2424,7 +2430,7 @@ void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
EMIT(return_value);
}
emit_end_pass(comp->emit);
EMIT(end_pass);
}
void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
@ -2489,7 +2495,9 @@ void py_compile(py_parse_node_t pn) {
comp->scope_head = NULL;
comp->scope_cur = NULL;
comp->emit = emit_new(comp->qstr___class__);
emit_new_cpython(&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);
scope_new_and_link(comp, SCOPE_MODULE, pn);

116
py/emit.h
View File

@ -1,8 +1,3 @@
//#define EMIT_DO_CPY
#define EMIT_DO_BC
//#define EMIT_DO_X64
//#define EMIT_DO_THUMB
/* Notes on passes:
* We don't know exactly the opcodes in pass 1 because they depend on the
* closing over of variables (LOAD_CLOSURE, BUILD_TUPLE, MAKE_CLOSURE), which
@ -19,15 +14,111 @@ typedef enum {
PASS_3 = 3, // emit code
} pass_kind_t;
typedef struct _emitter_t emitter_t;
typedef struct _emit_t emit_t;
void emit_common_declare_global(pass_kind_t pass, scope_t *scope, qstr qstr);
void emit_common_declare_nonlocal(pass_kind_t pass, scope_t *scope, qstr qstr);
void emit_common_load_id(pass_kind_t pass, scope_t *scope, qstr qstr___class__, emitter_t *emit, qstr qstr);
void emit_common_store_id(pass_kind_t pass, scope_t *scope, emitter_t *emit, qstr qstr);
void emit_common_delete_id(pass_kind_t pass, scope_t *scope, emitter_t *emit, qstr qstr);
typedef struct _emit_method_table_t {
void (*set_native_types)(emit_t *emit, bool do_native_types);
void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
void (*end_pass)(emit_t *emit);
bool (*last_emit_was_return_value)(emit_t *emit);
int (*get_stack_size)(emit_t *emit);
void (*set_stack_size)(emit_t *emit, int size);
emitter_t *emit_new();
int (*label_new)(emit_t *emit);
void (*label_assign)(emit_t *emit, int l);
void (*import_name)(emit_t *emit, qstr qstr);
void (*import_from)(emit_t *emit, qstr qstr);
void (*import_star)(emit_t *emit);
void (*load_const_tok)(emit_t *emit, py_token_kind_t tok);
void (*load_const_small_int)(emit_t *emit, int arg);
void (*load_const_int)(emit_t *emit, qstr qstr);
void (*load_const_dec)(emit_t *emit, qstr qstr);
void (*load_const_id)(emit_t *emit, qstr qstr);
void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes);
void (*load_const_verbatim_start)(emit_t *emit);
void (*load_const_verbatim_int)(emit_t *emit, int val);
void (*load_const_verbatim_str)(emit_t *emit, const char *str);
void (*load_const_verbatim_strn)(emit_t *emit, const char *str, int len);
void (*load_const_verbatim_quoted_str)(emit_t *emit, qstr qstr, bool bytes);
void (*load_const_verbatim_end)(emit_t *emit);
void (*load_fast)(emit_t *emit, qstr qstr, int local_num);
void (*load_name)(emit_t *emit, qstr qstr);
void (*load_global)(emit_t *emit, qstr qstr);
void (*load_deref)(emit_t *emit, qstr qstr);
void (*load_closure)(emit_t *emit, qstr qstr);
void (*load_attr)(emit_t *emit, qstr qstr);
void (*load_method)(emit_t *emit, qstr qstr);
void (*load_build_class)(emit_t *emit);
void (*store_fast)(emit_t *emit, qstr qstr, int local_num);
void (*store_name)(emit_t *emit, qstr qstr);
void (*store_global)(emit_t *emit, qstr qstr);
void (*store_deref)(emit_t *emit, qstr qstr);
void (*store_attr)(emit_t *emit, qstr qstr);
void (*store_locals)(emit_t *emit);
void (*store_subscr)(emit_t *emit);
void (*delete_fast)(emit_t *emit, qstr qstr, int local_num);
void (*delete_name)(emit_t *emit, qstr qstr);
void (*delete_global)(emit_t *emit, qstr qstr);
void (*delete_deref)(emit_t *emit, qstr qstr);
void (*delete_attr)(emit_t *emit, qstr qstr);
void (*delete_subscr)(emit_t *emit);
void (*dup_top)(emit_t *emit);
void (*dup_top_two)(emit_t *emit);
void (*pop_top)(emit_t *emit);
void (*rot_two)(emit_t *emit);
void (*rot_three)(emit_t *emit);
void (*jump)(emit_t *emit, int label);
void (*pop_jump_if_true)(emit_t *emit, int label);
void (*pop_jump_if_false)(emit_t *emit, int label);
void (*jump_if_true_or_pop)(emit_t *emit, int label);
void (*jump_if_false_or_pop)(emit_t *emit, int label);
void (*setup_loop)(emit_t *emit, int label);
void (*break_loop)(emit_t *emit, int label);
void (*continue_loop)(emit_t *emit, int label);
void (*setup_with)(emit_t *emit, int label);
void (*with_cleanup)(emit_t *emit);
void (*setup_except)(emit_t *emit, int label);
void (*setup_finally)(emit_t *emit, int label);
void (*end_finally)(emit_t *emit);
void (*get_iter)(emit_t *emit); // tos = getiter(tos)
void (*for_iter)(emit_t *emit, int label);
void (*for_iter_end)(emit_t *emit);
void (*pop_block)(emit_t *emit);
void (*pop_except)(emit_t *emit);
void (*unary_op)(emit_t *emit, rt_unary_op_t op);
void (*binary_op)(emit_t *emit, rt_binary_op_t op);
void (*compare_op)(emit_t *emit, rt_compare_op_t op);
void (*build_tuple)(emit_t *emit, int n_args);
void (*build_list)(emit_t *emit, int n_args);
void (*list_append)(emit_t *emit, int list_stack_index);
void (*build_map)(emit_t *emit, int n_args);
void (*store_map)(emit_t *emit);
void (*map_add)(emit_t *emit, int map_stack_index);
void (*build_set)(emit_t *emit, int n_args);
void (*set_add)(emit_t *emit, int set_stack_index);
void (*build_slice)(emit_t *emit, int n_args);
void (*unpack_sequence)(emit_t *emit, int n_args);
void (*unpack_ex)(emit_t *emit, int n_left, int n_right);
void (*make_function)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params);
void (*make_closure)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params);
void (*call_function)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
void (*call_method)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
void (*return_value)(emit_t *emit);
void (*raise_varargs)(emit_t *emit, int n_args);
void (*yield_value)(emit_t *emit);
void (*yield_from)(emit_t *emit);
} 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_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_new_cpython(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);
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_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);
@ -118,3 +209,4 @@ 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

@ -15,10 +15,8 @@
#include "emit.h"
#include "bc.h"
#ifdef EMIT_DO_BC
struct _emitter_t {
int pass;
struct _emit_t {
pass_kind_t pass;
int next_label;
int stack_size;
bool last_emit_was_return_value;
@ -34,25 +32,18 @@ struct _emitter_t {
byte dummy_data[8];
};
emitter_t *emit_new() {
emitter_t *emit = m_new(emitter_t, 1);
emit->max_num_labels = 0;
emit->label_offsets = NULL;
emit->code_offset = 0;
emit->code_size = 0;
emit->code_base = NULL;
return emit;
}
uint emit_get_code_size(emitter_t* emit) {
uint emit_bc_get_code_size(emit_t* emit) {
return emit->code_size;
}
void* emit_get_code(emitter_t* emit) {
void* emit_bc_get_code(emit_t* emit) {
return emit->code_base;
}
void emit_start_pass(emitter_t *emit, pass_kind_t pass, scope_t *scope) {
static void emit_bc_set_native_types(emit_t *emit, bool do_native_types) {
}
static void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
emit->pass = pass;
emit->next_label = 1;
emit->stack_size = 0;
@ -71,7 +62,7 @@ void emit_start_pass(emitter_t *emit, pass_kind_t pass, scope_t *scope) {
emit->code_offset = 0;
}
void emit_end_pass(emitter_t *emit) {
static void emit_bc_end_pass(emit_t *emit) {
// check stack is back to zero size
if (emit->stack_size != 0) {
printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
@ -95,7 +86,7 @@ void emit_end_pass(emitter_t *emit) {
}
// all functions must go through this one to emit bytes
static byte* emit_get_cur_to_write_bytes(emitter_t* emit, int num_bytes_to_write) {
static byte* emit_get_cur_to_write_bytes(emit_t* emit, int num_bytes_to_write) {
//printf("emit %d\n", num_bytes_to_write);
if (emit->pass < PASS_3) {
emit->code_offset += num_bytes_to_write;
@ -108,19 +99,19 @@ static byte* emit_get_cur_to_write_bytes(emitter_t* emit, int num_bytes_to_write
}
}
static void emit_write_byte_1(emitter_t* emit, byte b1) {
static void emit_write_byte_1(emit_t* emit, byte b1) {
byte* c = emit_get_cur_to_write_bytes(emit, 1);
c[0] = b1;
}
static void emit_write_byte_1_byte(emitter_t* emit, byte b1, uint b2) {
static void emit_write_byte_1_byte(emit_t* emit, byte b1, uint b2) {
assert((b2 & (~0xff)) == 0);
byte* c = emit_get_cur_to_write_bytes(emit, 2);
c[0] = b1;
c[1] = b2;
}
static void emit_write_byte_1_int(emitter_t* emit, byte b1, int num) {
static void emit_write_byte_1_int(emit_t* emit, byte b1, int num) {
assert((num & (~0x7fff)) == 0 || (num & (~0x7fff)) == (~0x7fff));
byte* c = emit_get_cur_to_write_bytes(emit, 3);
c[0] = b1;
@ -128,7 +119,7 @@ static void emit_write_byte_1_int(emitter_t* emit, byte b1, int num) {
c[2] = num >> 8;
}
static void emit_write_byte_1_uint(emitter_t* emit, byte b1, uint num) {
static void emit_write_byte_1_uint(emit_t* emit, byte b1, uint num) {
if (num <= 127) { // fits in 0x7f
// fit argument in single byte
byte* c = emit_get_cur_to_write_bytes(emit, 2);
@ -146,11 +137,11 @@ static void emit_write_byte_1_uint(emitter_t* emit, byte b1, uint num) {
}
}
static void emit_write_byte_1_qstr(emitter_t* emit, byte b1, qstr qstr) {
static void emit_write_byte_1_qstr(emit_t* emit, byte b1, qstr qstr) {
emit_write_byte_1_uint(emit, b1, qstr);
}
static void emit_write_byte_1_label(emitter_t* emit, byte b1, int label) {
static void emit_write_byte_1_label(emit_t* emit, byte b1, int label) {
uint code_offset;
if (emit->pass < PASS_3) {
code_offset = 0;
@ -160,21 +151,21 @@ static void emit_write_byte_1_label(emitter_t* emit, byte b1, int label) {
emit_write_byte_1_uint(emit, b1, code_offset);
}
bool emit_last_emit_was_return_value(emitter_t *emit) {
bool emit_bc_last_emit_was_return_value(emit_t *emit) {
return emit->last_emit_was_return_value;
}
int emit_get_stack_size(emitter_t *emit) {
int emit_bc_get_stack_size(emit_t *emit) {
return emit->stack_size;
}
void emit_set_stack_size(emitter_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;
}
}
static void emit_pre(emitter_t *emit, int stack_size_delta) {
static void emit_pre(emit_t *emit, int stack_size_delta) {
if (emit->pass > PASS_1) {
emit->stack_size += stack_size_delta;
if (emit->stack_size > emit->scope->stack_size) {
@ -184,11 +175,11 @@ static void emit_pre(emitter_t *emit, int stack_size_delta) {
emit->last_emit_was_return_value = false;
}
int emit_label_new(emitter_t *emit) {
static int emit_bc_label_new(emit_t *emit) {
return emit->next_label++;
}
void emit_label_assign(emitter_t *emit, int l) {
static void emit_bc_label_assign(emit_t *emit, int l) {
emit_pre(emit, 0);
if (emit->pass > PASS_1) {
assert(l < emit->max_num_labels);
@ -204,22 +195,22 @@ void emit_label_assign(emitter_t *emit, int l) {
}
}
void emit_import_name(emitter_t *emit, qstr qstr) {
static void emit_bc_import_name(emit_t *emit, qstr qstr) {
emit_pre(emit, -1);
emit_write_byte_1_qstr(emit, PYBC_IMPORT_NAME, qstr);
}
void emit_import_from(emitter_t *emit, qstr qstr) {
static void emit_bc_import_from(emit_t *emit, qstr qstr) {
emit_pre(emit, 1);
emit_write_byte_1_qstr(emit, PYBC_IMPORT_FROM, qstr);
}
void emit_import_star(emitter_t *emit) {
static void emit_bc_import_star(emit_t *emit) {
emit_pre(emit, -1);
emit_write_byte_1(emit, PYBC_IMPORT_STAR);
}
void emit_load_const_tok(emitter_t *emit, py_token_kind_t tok) {
static void emit_bc_load_const_tok(emit_t *emit, py_token_kind_t tok) {
emit_pre(emit, 1);
switch (tok) {
case PY_TOKEN_KW_FALSE: emit_write_byte_1(emit, PYBC_LOAD_CONST_FALSE); break;
@ -229,27 +220,27 @@ void emit_load_const_tok(emitter_t *emit, py_token_kind_t tok) {
}
}
void emit_load_const_small_int(emitter_t *emit, int arg) {
static void emit_bc_load_const_small_int(emit_t *emit, int arg) {
emit_pre(emit, 1);
emit_write_byte_1_int(emit, PYBC_LOAD_CONST_SMALL_INT, arg);
}
void emit_load_const_int(emitter_t *emit, qstr qstr) {
static void emit_bc_load_const_int(emit_t *emit, qstr qstr) {
emit_pre(emit, 1);
emit_write_byte_1_qstr(emit, PYBC_LOAD_CONST_INT, qstr);
}
void emit_load_const_dec(emitter_t *emit, qstr qstr) {
static void emit_bc_load_const_dec(emit_t *emit, qstr qstr) {
emit_pre(emit, 1);
emit_write_byte_1_qstr(emit, PYBC_LOAD_CONST_DEC, qstr);
}
void emit_load_const_id(emitter_t *emit, qstr qstr) {
static void emit_bc_load_const_id(emit_t *emit, qstr qstr) {
emit_pre(emit, 1);
emit_write_byte_1_qstr(emit, PYBC_LOAD_CONST_ID, qstr);
}
void emit_load_const_str(emitter_t *emit, qstr qstr, bool bytes) {
static void emit_bc_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
emit_pre(emit, 1);
if (bytes) {
emit_write_byte_1_qstr(emit, PYBC_LOAD_CONST_BYTES, qstr);
@ -258,32 +249,32 @@ void emit_load_const_str(emitter_t *emit, qstr qstr, bool bytes) {
}
}
void emit_load_const_verbatim_start(emitter_t *emit) {
static void emit_bc_load_const_verbatim_start(emit_t *emit) {
emit_pre(emit, 1);
assert(0);
}
void emit_load_const_verbatim_int(emitter_t *emit, int val) {
static void emit_bc_load_const_verbatim_int(emit_t *emit, int val) {
assert(0);
}
void emit_load_const_verbatim_str(emitter_t *emit, const char *str) {
static void emit_bc_load_const_verbatim_str(emit_t *emit, const char *str) {
assert(0);
}
void emit_load_const_verbatim_strn(emitter_t *emit, const char *str, int len) {
static void emit_bc_load_const_verbatim_strn(emit_t *emit, const char *str, int len) {
assert(0);
}
void emit_load_const_verbatim_quoted_str(emitter_t *emit, qstr qstr, bool bytes) {
static void emit_bc_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes) {
assert(0);
}
void emit_load_const_verbatim_end(emitter_t *emit) {
static void emit_bc_load_const_verbatim_end(emit_t *emit) {
assert(0);
}
void emit_load_fast(emitter_t *emit, qstr qstr, int local_num) {
static void emit_bc_load_fast(emit_t *emit, qstr qstr, int local_num) {
assert(local_num >= 0);
emit_pre(emit, 1);
switch (local_num) {
@ -294,42 +285,42 @@ void emit_load_fast(emitter_t *emit, qstr qstr, int local_num) {
}
}
void emit_load_name(emitter_t *emit, qstr qstr) {
static void emit_bc_load_name(emit_t *emit, qstr qstr) {
emit_pre(emit, 1);
emit_write_byte_1_qstr(emit, PYBC_LOAD_NAME, qstr);
}
void emit_load_global(emitter_t *emit, qstr qstr) {
static void emit_bc_load_global(emit_t *emit, qstr qstr) {
emit_pre(emit, 1);
emit_write_byte_1_qstr(emit, PYBC_LOAD_GLOBAL, qstr);
}
void emit_load_deref(emitter_t *emit, qstr qstr) {
static void emit_bc_load_deref(emit_t *emit, qstr qstr) {
emit_pre(emit, 1);
assert(0);
}
void emit_load_closure(emitter_t *emit, qstr qstr) {
static void emit_bc_load_closure(emit_t *emit, qstr qstr) {
emit_pre(emit, 1);
assert(0);
}
void emit_load_attr(emitter_t *emit, qstr qstr) {
static void emit_bc_load_attr(emit_t *emit, qstr qstr) {
emit_pre(emit, 0);
emit_write_byte_1_qstr(emit, PYBC_LOAD_ATTR, qstr);
}
void emit_load_method(emitter_t *emit, qstr qstr) {
static void emit_bc_load_method(emit_t *emit, qstr qstr) {
emit_pre(emit, 0);
emit_write_byte_1_qstr(emit, PYBC_LOAD_METHOD, qstr);
}
void emit_load_build_class(emitter_t *emit) {
static void emit_bc_load_build_class(emit_t *emit) {
emit_pre(emit, 1);
emit_write_byte_1(emit, PYBC_LOAD_BUILD_CLASS);
}
void emit_store_fast(emitter_t *emit, qstr qstr, int local_num) {
static void emit_bc_store_fast(emit_t *emit, qstr qstr, int local_num) {
assert(local_num >= 0);
emit_pre(emit, -1);
switch (local_num) {
@ -340,268 +331,268 @@ void emit_store_fast(emitter_t *emit, qstr qstr, int local_num) {
}
}
void emit_store_name(emitter_t *emit, qstr qstr) {
static void emit_bc_store_name(emit_t *emit, qstr qstr) {
emit_pre(emit, -1);
emit_write_byte_1_qstr(emit, PYBC_STORE_NAME, qstr);
}
void emit_store_global(emitter_t *emit, qstr qstr) {
static void emit_bc_store_global(emit_t *emit, qstr qstr) {
emit_pre(emit, -1);
emit_write_byte_1_qstr(emit, PYBC_STORE_GLOBAL, qstr);
}
void emit_store_deref(emitter_t *emit, qstr qstr) {
static void emit_bc_store_deref(emit_t *emit, qstr qstr) {
emit_pre(emit, -1);
assert(0);
}
void emit_store_attr(emitter_t *emit, qstr qstr) {
static void emit_bc_store_attr(emit_t *emit, qstr qstr) {
emit_pre(emit, -2);
emit_write_byte_1_qstr(emit, PYBC_STORE_ATTR, qstr);
}
void emit_store_locals(emitter_t *emit) {
static void emit_bc_store_locals(emit_t *emit) {
emit_pre(emit, -1);
emit_write_byte_1(emit, PYBC_STORE_LOCALS);
}
void emit_store_subscr(emitter_t *emit) {
static void emit_bc_store_subscr(emit_t *emit) {
emit_pre(emit, -3);
emit_write_byte_1(emit, PYBC_STORE_SUBSCR);
}
void emit_delete_fast(emitter_t *emit, qstr qstr, int local_num) {
static void emit_bc_delete_fast(emit_t *emit, qstr qstr, int local_num) {
assert(local_num >= 0);
emit_pre(emit, 0);
emit_write_byte_1_uint(emit, PYBC_DELETE_FAST_N, local_num);
}
void emit_delete_name(emitter_t *emit, qstr qstr) {
static void emit_bc_delete_name(emit_t *emit, qstr qstr) {
emit_pre(emit, 0);
emit_write_byte_1_qstr(emit, PYBC_DELETE_NAME, qstr);
}
void emit_delete_global(emitter_t *emit, qstr qstr) {
static void emit_bc_delete_global(emit_t *emit, qstr qstr) {
emit_pre(emit, 0);
emit_write_byte_1_qstr(emit, PYBC_DELETE_GLOBAL, qstr);
}
void emit_delete_deref(emitter_t *emit, qstr qstr) {
static void emit_bc_delete_deref(emit_t *emit, qstr qstr) {
emit_pre(emit, 0);
emit_write_byte_1_qstr(emit, PYBC_DELETE_DEREF, qstr);
}
void emit_delete_attr(emitter_t *emit, qstr qstr) {
static void emit_bc_delete_attr(emit_t *emit, qstr qstr) {
emit_pre(emit, -1);
emit_write_byte_1_qstr(emit, PYBC_DELETE_ATTR, qstr);
}
void emit_delete_subscr(emitter_t *emit) {
static void emit_bc_delete_subscr(emit_t *emit) {
emit_pre(emit, -2);
emit_write_byte_1(emit, PYBC_DELETE_SUBSCR);
}
void emit_dup_top(emitter_t *emit) {
static void emit_bc_dup_top(emit_t *emit) {
emit_pre(emit, 1);
emit_write_byte_1(emit, PYBC_DUP_TOP);
}
void emit_dup_top_two(emitter_t *emit) {
static void emit_bc_dup_top_two(emit_t *emit) {
emit_pre(emit, 2);
emit_write_byte_1(emit, PYBC_DUP_TOP_TWO);
}
void emit_pop_top(emitter_t *emit) {
static void emit_bc_pop_top(emit_t *emit) {
emit_pre(emit, -1);
emit_write_byte_1(emit, PYBC_POP_TOP);
}
void emit_rot_two(emitter_t *emit) {
static void emit_bc_rot_two(emit_t *emit) {
emit_pre(emit, 0);
emit_write_byte_1(emit, PYBC_ROT_TWO);
}
void emit_rot_three(emitter_t *emit) {
static void emit_bc_rot_three(emit_t *emit) {
emit_pre(emit, 0);
emit_write_byte_1(emit, PYBC_ROT_THREE);
}
void emit_jump(emitter_t *emit, int label) {
static void emit_bc_jump(emit_t *emit, int label) {
emit_pre(emit, 0);
emit_write_byte_1_label(emit, PYBC_JUMP, label);
}
void emit_pop_jump_if_true(emitter_t *emit, int label) {
static void emit_bc_pop_jump_if_true(emit_t *emit, int label) {
emit_pre(emit, -1);
emit_write_byte_1_label(emit, PYBC_POP_JUMP_IF_TRUE, label);
}
void emit_pop_jump_if_false(emitter_t *emit, int label) {
static void emit_bc_pop_jump_if_false(emit_t *emit, int label) {
emit_pre(emit, -1);
emit_write_byte_1_label(emit, PYBC_POP_JUMP_IF_FALSE, label);
}
void emit_jump_if_true_or_pop(emitter_t *emit, int label) {
static void emit_bc_jump_if_true_or_pop(emit_t *emit, int label) {
emit_pre(emit, -1);
emit_write_byte_1_label(emit, PYBC_JUMP_IF_TRUE_OR_POP, label);
}
void emit_jump_if_false_or_pop(emitter_t *emit, int label) {
static void emit_bc_jump_if_false_or_pop(emit_t *emit, int label) {
emit_pre(emit, -1);
emit_write_byte_1_label(emit, PYBC_JUMP_IF_FALSE_OR_POP, label);
}
void emit_setup_loop(emitter_t *emit, int label) {
static void emit_bc_setup_loop(emit_t *emit, int label) {
emit_pre(emit, 0);
emit_write_byte_1_label(emit, PYBC_SETUP_LOOP, label);
}
void emit_break_loop(emitter_t *emit, int label) {
static void emit_bc_break_loop(emit_t *emit, int label) {
emit_pre(emit, 0);
emit_write_byte_1_label(emit, PYBC_BREAK_LOOP, label);
}
void emit_continue_loop(emitter_t *emit, int label) {
static void emit_bc_continue_loop(emit_t *emit, int label) {
emit_pre(emit, 0);
emit_write_byte_1_label(emit, PYBC_CONTINUE_LOOP, label);
}
void emit_setup_with(emitter_t *emit, int label) {
static void emit_bc_setup_with(emit_t *emit, int label) {
emit_pre(emit, 7);
emit_write_byte_1_label(emit, PYBC_SETUP_WITH, label);
}
void emit_with_cleanup(emitter_t *emit) {
static void emit_bc_with_cleanup(emit_t *emit) {
emit_pre(emit, -7);
emit_write_byte_1(emit, PYBC_WITH_CLEANUP);
}
void emit_setup_except(emitter_t *emit, int label) {
static void emit_bc_setup_except(emit_t *emit, int label) {
emit_pre(emit, 6);
emit_write_byte_1_label(emit, PYBC_SETUP_EXCEPT, label);
}
void emit_setup_finally(emitter_t *emit, int label) {
static void emit_bc_setup_finally(emit_t *emit, int label) {
emit_pre(emit, 6);
emit_write_byte_1_label(emit, PYBC_SETUP_FINALLY, label);
}
void emit_end_finally(emitter_t *emit) {
static void emit_bc_end_finally(emit_t *emit) {
emit_pre(emit, -1);
emit_write_byte_1(emit, PYBC_END_FINALLY);
}
void emit_get_iter(emitter_t *emit) {
static void emit_bc_get_iter(emit_t *emit) {
emit_pre(emit, 0);
emit_write_byte_1(emit, PYBC_GET_ITER);
}
void emit_for_iter(emitter_t *emit, int label) {
static void emit_bc_for_iter(emit_t *emit, int label) {
emit_pre(emit, 1);
emit_write_byte_1_label(emit, PYBC_FOR_ITER, label);
}
void emit_for_iter_end(emitter_t *emit) {
static void emit_bc_for_iter_end(emit_t *emit) {
emit_pre(emit, -1);
}
void emit_pop_block(emitter_t *emit) {
static void emit_bc_pop_block(emit_t *emit) {
emit_pre(emit, 0);
emit_write_byte_1(emit, PYBC_POP_BLOCK);
}
void emit_pop_except(emitter_t *emit) {
static void emit_bc_pop_except(emit_t *emit) {
emit_pre(emit, 0);
emit_write_byte_1(emit, PYBC_POP_EXCEPT);
}
void emit_unary_op(emitter_t *emit, rt_unary_op_t op) {
static void emit_bc_unary_op(emit_t *emit, rt_unary_op_t op) {
emit_pre(emit, 0);
emit_write_byte_1_byte(emit, PYBC_UNARY_OP, op);
}
void emit_binary_op(emitter_t *emit, rt_binary_op_t op) {
static void emit_bc_binary_op(emit_t *emit, rt_binary_op_t op) {
emit_pre(emit, -1);
emit_write_byte_1_byte(emit, PYBC_BINARY_OP, op);
}
void emit_compare_op(emitter_t *emit, rt_compare_op_t op) {
static void emit_bc_compare_op(emit_t *emit, rt_compare_op_t op) {
emit_pre(emit, -1);
emit_write_byte_1_byte(emit, PYBC_COMPARE_OP, op);
}
void emit_build_tuple(emitter_t *emit, int n_args) {
static void emit_bc_build_tuple(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_pre(emit, 1 - n_args);
emit_write_byte_1_uint(emit, PYBC_BUILD_TUPLE, n_args);
}
void emit_build_list(emitter_t *emit, int n_args) {
static void emit_bc_build_list(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_pre(emit, 1 - n_args);
emit_write_byte_1_uint(emit, PYBC_BUILD_LIST, n_args);
}
void emit_list_append(emitter_t *emit, int list_stack_index) {
static void emit_bc_list_append(emit_t *emit, int list_stack_index) {
assert(list_stack_index >= 0);
emit_pre(emit, -1);
emit_write_byte_1_uint(emit, PYBC_LIST_APPEND, list_stack_index);
}
void emit_build_map(emitter_t *emit, int n_args) {
static void emit_bc_build_map(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_pre(emit, 1);
emit_write_byte_1_uint(emit, PYBC_BUILD_MAP, n_args);
}
void emit_store_map(emitter_t *emit) {
static void emit_bc_store_map(emit_t *emit) {
emit_pre(emit, -2);
emit_write_byte_1(emit, PYBC_STORE_MAP);
}
void emit_map_add(emitter_t *emit, int map_stack_index) {
static void emit_bc_map_add(emit_t *emit, int map_stack_index) {
assert(map_stack_index >= 0);
emit_pre(emit, -2);
emit_write_byte_1_uint(emit, PYBC_MAP_ADD, map_stack_index);
}
void emit_build_set(emitter_t *emit, int n_args) {
static void emit_bc_build_set(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_pre(emit, 1 - n_args);
emit_write_byte_1_uint(emit, PYBC_BUILD_SET, n_args);
}
void emit_set_add(emitter_t *emit, int set_stack_index) {
static void emit_bc_set_add(emit_t *emit, int set_stack_index) {
assert(set_stack_index >= 0);
emit_pre(emit, -1);
emit_write_byte_1_uint(emit, PYBC_SET_ADD, set_stack_index);
}
void emit_build_slice(emitter_t *emit, int n_args) {
static void emit_bc_build_slice(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_pre(emit, 1 - n_args);
emit_write_byte_1_uint(emit, PYBC_BUILD_SLICE, n_args);
}
void emit_unpack_sequence(emitter_t *emit, int n_args) {
static void emit_bc_unpack_sequence(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_pre(emit, -1 + n_args);
emit_write_byte_1_uint(emit, PYBC_UNPACK_SEQUENCE, n_args);
}
void emit_unpack_ex(emitter_t *emit, int n_left, int n_right) {
static void emit_bc_unpack_ex(emit_t *emit, int n_left, int n_right) {
assert(n_left >=0 && n_right >= 0);
emit_pre(emit, -1 + n_left + n_right + 1);
emit_write_byte_1_uint(emit, PYBC_UNPACK_EX, n_left | (n_right << 8));
}
void emit_make_function(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
static void emit_bc_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
assert(n_default_params == 0 && n_dict_params == 0);
emit_pre(emit, 1);
emit_write_byte_1_uint(emit, PYBC_MAKE_FUNCTION, scope->unique_code_id);
}
void emit_make_closure(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
static void emit_bc_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
assert(0);
emit_pre(emit, -2 - n_default_params - 2 * n_dict_params);
if (emit->pass == PASS_3) {
@ -609,7 +600,7 @@ void emit_make_closure(emitter_t *emit, scope_t *scope, int n_dict_params, int n
}
}
void emit_call_function(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
static void emit_bc_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
int s = 0;
if (have_star_arg) {
s += 1;
@ -635,7 +626,7 @@ void emit_call_function(emitter_t *emit, int n_positional, int n_keyword, bool h
emit_write_byte_1_uint(emit, op, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints
}
void emit_call_method(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
static void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
int s = 0;
if (have_star_arg) {
s += 1;
@ -661,19 +652,19 @@ void emit_call_method(emitter_t *emit, int n_positional, int n_keyword, bool hav
emit_write_byte_1_uint(emit, op, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints
}
void emit_return_value(emitter_t *emit) {
static void emit_bc_return_value(emit_t *emit) {
emit_pre(emit, -1);
emit->last_emit_was_return_value = true;
emit_write_byte_1(emit, PYBC_RETURN_VALUE);
}
void emit_raise_varargs(emitter_t *emit, int n_args) {
static void emit_bc_raise_varargs(emit_t *emit, int n_args) {
assert(n_args >= 0);
emit_pre(emit, -n_args);
emit_write_byte_1_uint(emit, PYBC_RAISE_VARARGS, n_args);
}
void emit_yield_value(emitter_t *emit) {
static void emit_bc_yield_value(emit_t *emit) {
emit_pre(emit, 0);
if (emit->pass == PASS_2) {
emit->scope->flags |= SCOPE_FLAG_GENERATOR;
@ -681,7 +672,7 @@ void emit_yield_value(emitter_t *emit) {
emit_write_byte_1(emit, PYBC_YIELD_VALUE);
}
void emit_yield_from(emitter_t *emit) {
static void emit_bc_yield_from(emit_t *emit) {
emit_pre(emit, -1);
if (emit->pass == PASS_2) {
emit->scope->flags |= SCOPE_FLAG_GENERATOR;
@ -689,4 +680,107 @@ void emit_yield_from(emitter_t *emit) {
emit_write_byte_1(emit, PYBC_YIELD_FROM);
}
#endif // EMIT_DO_BC
static const emit_method_table_t emit_bc_method_table = {
emit_bc_set_native_types,
emit_bc_start_pass,
emit_bc_end_pass,
emit_bc_last_emit_was_return_value,
emit_bc_get_stack_size,
emit_bc_set_stack_size,
emit_bc_label_new,
emit_bc_label_assign,
emit_bc_import_name,
emit_bc_import_from,
emit_bc_import_star,
emit_bc_load_const_tok,
emit_bc_load_const_small_int,
emit_bc_load_const_int,
emit_bc_load_const_dec,
emit_bc_load_const_id,
emit_bc_load_const_str,
emit_bc_load_const_verbatim_start,
emit_bc_load_const_verbatim_int,
emit_bc_load_const_verbatim_str,
emit_bc_load_const_verbatim_strn,
emit_bc_load_const_verbatim_quoted_str,
emit_bc_load_const_verbatim_end,
emit_bc_load_fast,
emit_bc_load_name,
emit_bc_load_global,
emit_bc_load_deref,
emit_bc_load_closure,
emit_bc_load_attr,
emit_bc_load_method,
emit_bc_load_build_class,
emit_bc_store_fast,
emit_bc_store_name,
emit_bc_store_global,
emit_bc_store_deref,
emit_bc_store_attr,
emit_bc_store_locals,
emit_bc_store_subscr,
emit_bc_delete_fast,
emit_bc_delete_name,
emit_bc_delete_global,
emit_bc_delete_deref,
emit_bc_delete_attr,
emit_bc_delete_subscr,
emit_bc_dup_top,
emit_bc_dup_top_two,
emit_bc_pop_top,
emit_bc_rot_two,
emit_bc_rot_three,
emit_bc_jump,
emit_bc_pop_jump_if_true,
emit_bc_pop_jump_if_false,
emit_bc_jump_if_true_or_pop,
emit_bc_jump_if_false_or_pop,
emit_bc_setup_loop,
emit_bc_break_loop,
emit_bc_continue_loop,
emit_bc_setup_with,
emit_bc_with_cleanup,
emit_bc_setup_except,
emit_bc_setup_finally,
emit_bc_end_finally,
emit_bc_get_iter,
emit_bc_for_iter,
emit_bc_for_iter_end,
emit_bc_pop_block,
emit_bc_pop_except,
emit_bc_unary_op,
emit_bc_binary_op,
emit_bc_compare_op,
emit_bc_build_tuple,
emit_bc_build_list,
emit_bc_list_append,
emit_bc_build_map,
emit_bc_store_map,
emit_bc_map_add,
emit_bc_build_set,
emit_bc_set_add,
emit_bc_build_slice,
emit_bc_unpack_sequence,
emit_bc_unpack_ex,
emit_bc_make_function,
emit_bc_make_closure,
emit_bc_call_function,
emit_bc_call_method,
emit_bc_return_value,
emit_bc_raise_varargs,
emit_bc_yield_value,
emit_bc_yield_from,
};
void emit_new_bc(emit_t **emit_out, const emit_method_table_t **emit_method_table_out) {
emit_t *emit = m_new(emit_t, 1);
emit->max_num_labels = 0;
emit->label_offsets = NULL;
emit->code_offset = 0;
emit->code_size = 0;
emit->code_base = NULL;
*emit_out = emit;
*emit_method_table_out = &emit_bc_method_table;
}

View File

@ -12,53 +12,9 @@
#include "runtime.h"
#include "emit.h"
#define EMIT(fun, arg...) (emit_##fun(emit, ##arg))
#define EMIT(fun, arg...) (emit_method_table->fun(emit, ##arg))
void emit_common_declare_global(pass_kind_t pass, scope_t *scope, qstr qstr) {
if (pass == PASS_1) {
if (scope->kind == SCOPE_MODULE) {
printf("SyntaxError?: can't declare global in outer code\n");
return;
}
bool added;
id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
if (!added) {
printf("SyntaxError?: identifier already declared something\n");
return;
}
id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
// if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
id_info = scope_find_global(scope, qstr);
if (id_info != NULL) {
id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
}
}
}
void emit_common_declare_nonlocal(pass_kind_t pass, scope_t *scope, qstr qstr) {
if (pass == PASS_1) {
if (scope->kind == SCOPE_MODULE) {
printf("SyntaxError?: can't declare nonlocal in outer code\n");
return;
}
bool added;
id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
if (!added) {
printf("SyntaxError?: identifier already declared something\n");
return;
}
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)) {
printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qstr));
return;
}
id_info->kind = ID_INFO_KIND_FREE;
scope_close_over_in_parents(scope, qstr);
}
}
void emit_common_load_id(pass_kind_t pass, scope_t *scope, qstr qstr___class__, emitter_t *emit, qstr qstr) {
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) {
id_info_t *id_info = NULL;
if (pass == PASS_1) {
// name adding/lookup
@ -109,7 +65,7 @@ void emit_common_load_id(pass_kind_t pass, scope_t *scope, qstr qstr___class__,
}
}
static id_info_t *get_id_for_modification(pass_kind_t pass, scope_t *scope, emitter_t *emit, qstr qstr) {
static id_info_t *get_id_for_modification(pass_kind_t pass, scope_t *scope, qstr qstr) {
id_info_t *id_info = NULL;
if (pass == PASS_1) {
// name adding/lookup
@ -134,9 +90,9 @@ static id_info_t *get_id_for_modification(pass_kind_t pass, scope_t *scope, emit
return id_info;
}
void emit_common_store_id(pass_kind_t pass, scope_t *scope, emitter_t *emit, 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) {
// create/get the id info
id_info_t *id = get_id_for_modification(pass, scope, emit, qstr);
id_info_t *id = get_id_for_modification(pass, scope, qstr);
// call the emit backend with the correct code
if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
@ -152,9 +108,9 @@ void emit_common_store_id(pass_kind_t pass, scope_t *scope, emitter_t *emit, qst
}
}
void emit_common_delete_id(pass_kind_t pass, scope_t *scope, emitter_t *emit, 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) {
// create/get the id info
id_info_t *id = get_id_for_modification(pass, scope, emit, qstr);
id_info_t *id = get_id_for_modification(pass, scope, qstr);
// call the emit backend with the correct code
if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {

View File

@ -14,9 +14,9 @@
#include "runtime.h"
#include "emit.h"
#ifdef EMIT_DO_CPY
#ifdef EMIT_ENABLE_CPY
struct _emitter_t {
struct _emit_t {
int pass;
int next_label;
int byte_code_offset;
@ -29,14 +29,10 @@ struct _emitter_t {
int *label_offsets;
};
emitter_t *emit_new() {
emitter_t *emit = m_new(emitter_t, 1);
emit->max_num_labels = 0;
emit->label_offsets = NULL;
return emit;
static void emit_cpy_set_native_types(emit_t *emit, bool do_native_types) {
}
void emit_start_pass(emitter_t *emit, pass_kind_t pass, scope_t *scope) {
static void emit_cpy_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
emit->pass = pass;
emit->next_label = 1;
emit->byte_code_offset = 0;
@ -53,7 +49,7 @@ void emit_start_pass(emitter_t *emit, pass_kind_t pass, scope_t *scope) {
}
}
void emit_end_pass(emitter_t *emit) {
static void emit_cpy_end_pass(emit_t *emit) {
// check stack is back to zero size
if (emit->stack_size != 0) {
printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
@ -67,19 +63,19 @@ void emit_end_pass(emitter_t *emit) {
}
}
bool emit_last_emit_was_return_value(emitter_t *emit) {
static bool emit_cpy_last_emit_was_return_value(emit_t *emit) {
return emit->last_emit_was_return_value;
}
int emit_get_stack_size(emitter_t *emit) {
static int emit_cpy_get_stack_size(emit_t *emit) {
return emit->stack_size;
}
void emit_set_stack_size(emitter_t *emit, int size) {
static void emit_cpy_set_stack_size(emit_t *emit, int size) {
emit->stack_size = size;
}
static void emit_pre(emitter_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;
if (emit->pass > PASS_1 && emit->stack_size > emit->scope->stack_size) {
emit->scope->stack_size = emit->stack_size;
@ -95,11 +91,11 @@ static void emit_pre(emitter_t *emit, int stack_size_delta, int byte_code_size)
emit->byte_code_offset += byte_code_size;
}
int emit_label_new(emitter_t *emit) {
static int emit_cpy_label_new(emit_t *emit) {
return emit->next_label++;
}
void emit_label_assign(emitter_t *emit, int l) {
static void emit_cpy_label_assign(emit_t *emit, int l) {
emit_pre(emit, 0, 0);
if (emit->pass > PASS_1) {
assert(l < emit->max_num_labels);
@ -115,28 +111,28 @@ void emit_label_assign(emitter_t *emit, int l) {
}
}
void emit_import_name(emitter_t *emit, qstr qstr) {
static void emit_cpy_import_name(emit_t *emit, qstr qstr) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("IMPORT_NAME %s\n", qstr_str(qstr));
}
}
void emit_import_from(emitter_t *emit, qstr qstr) {
static void emit_cpy_import_from(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("IMPORT_FROM %s\n", qstr_str(qstr));
}
}
void emit_import_star(emitter_t *emit) {
static void emit_cpy_import_star(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
printf("IMPORT_STAR\n");
}
}
void emit_load_const_tok(emitter_t *emit, py_token_kind_t tok) {
static void emit_cpy_load_const_tok(emit_t *emit, py_token_kind_t tok) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CONST ");
@ -150,69 +146,70 @@ void emit_load_const_tok(emitter_t *emit, py_token_kind_t tok) {
}
}
void emit_load_const_small_int(emitter_t *emit, int arg) {
static void emit_cpy_load_const_small_int(emit_t *emit, int arg) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CONST %d\n", arg);
}
}
void emit_load_const_int(emitter_t *emit, qstr qstr) {
static void emit_cpy_load_const_int(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CONST %s\n", qstr_str(qstr));
}
}
void emit_load_const_dec(emitter_t *emit, qstr qstr) {
static void emit_cpy_load_const_dec(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CONST %s\n", qstr_str(qstr));
}
}
void emit_load_const_id(emitter_t *emit, qstr qstr) {
static void emit_cpy_load_const_id(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CONST '%s'\n", qstr_str(qstr));
}
}
void emit_load_const_str(emitter_t *emit, qstr qstr, bool bytes) {
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) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CONST ");
emit_load_const_verbatim_quoted_str(emit, qstr, bytes);
emit_cpy_load_const_verbatim_quoted_str(emit, qstr, bytes);
printf("\n");
}
}
void emit_load_const_verbatim_start(emitter_t *emit) {
static void emit_cpy_load_const_verbatim_start(emit_t *emit) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CONST ");
}
}
void emit_load_const_verbatim_int(emitter_t *emit, int val) {
static void emit_cpy_load_const_verbatim_int(emit_t *emit, int val) {
if (emit->pass == PASS_3) {
printf("%d", val);
}
}
void emit_load_const_verbatim_str(emitter_t *emit, const char *str) {
static void emit_cpy_load_const_verbatim_str(emit_t *emit, const char *str) {
if (emit->pass == PASS_3) {
printf("%s", str);
}
}
void emit_load_const_verbatim_strn(emitter_t *emit, const char *str, int len) {
static void emit_cpy_load_const_verbatim_strn(emit_t *emit, const char *str, int len) {
if (emit->pass == PASS_3) {
printf("%.*s", len, str);
}
}
void emit_load_const_verbatim_quoted_str(emitter_t *emit, qstr qstr, bool bytes) {
static void emit_cpy_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes) {
// TODO strings should be escaped before we get here
if (emit->pass == PASS_3) {
const char *str = qstr_str(qstr);
@ -260,192 +257,192 @@ void emit_load_const_verbatim_quoted_str(emitter_t *emit, qstr qstr, bool bytes)
}
}
void emit_load_const_verbatim_end(emitter_t *emit) {
static void emit_cpy_load_const_verbatim_end(emit_t *emit) {
if (emit->pass == PASS_3) {
printf("\n");
}
}
void emit_load_name(emitter_t *emit, qstr qstr) {
static void emit_cpy_load_name(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_NAME %s\n", qstr_str(qstr));
}
}
void emit_load_global(emitter_t *emit, qstr qstr) {
static void emit_cpy_load_global(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_GLOBAL %s\n", qstr_str(qstr));
}
}
void emit_load_fast(emitter_t *emit, qstr qstr, int local_num) {
static void emit_cpy_load_fast(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_FAST %s\n", qstr_str(qstr));
}
}
void emit_load_deref(emitter_t *emit, qstr qstr) {
static void emit_cpy_load_deref(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_DEREF %s\n", qstr_str(qstr));
}
}
void emit_load_closure(emitter_t *emit, qstr qstr) {
static void emit_cpy_load_closure(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CLOSURE %s\n", qstr_str(qstr));
}
}
void emit_load_attr(emitter_t *emit, qstr qstr) {
static void emit_cpy_load_attr(emit_t *emit, qstr qstr) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
printf("LOAD_ATTR %s\n", qstr_str(qstr));
}
}
void emit_load_method(emitter_t *emit, qstr qstr) {
emit_load_attr(emit, qstr);
static void emit_cpy_load_method(emit_t *emit, qstr qstr) {
emit_cpy_load_attr(emit, qstr);
}
void emit_load_build_class(emitter_t *emit) {
static void emit_cpy_load_build_class(emit_t *emit) {
emit_pre(emit, 1, 1);
if (emit->pass == PASS_3) {
printf("LOAD_BUILD_CLASS\n");
}
}
void emit_store_name(emitter_t *emit, qstr qstr) {
static void emit_cpy_store_name(emit_t *emit, qstr qstr) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("STORE_NAME %s\n", qstr_str(qstr));
}
}
void emit_store_global(emitter_t *emit, qstr qstr) {
static void emit_cpy_store_global(emit_t *emit, qstr qstr) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("STORE_GLOBAL %s\n", qstr_str(qstr));
}
}
void emit_store_fast(emitter_t *emit, qstr qstr, int local_num) {
static void emit_cpy_store_fast(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("STORE_FAST %s\n", qstr_str(qstr));
}
}
void emit_store_deref(emitter_t *emit, qstr qstr) {
static void emit_cpy_store_deref(emit_t *emit, qstr qstr) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("STORE_DEREF %s\n", qstr_str(qstr));
}
}
void emit_store_attr(emitter_t *emit, qstr qstr) {
static void emit_cpy_store_attr(emit_t *emit, qstr qstr) {
emit_pre(emit, -2, 3);
if (emit->pass == PASS_3) {
printf("STORE_ATTR %s\n", qstr_str(qstr));
}
}
void emit_store_locals(emitter_t *emit) {
static void emit_cpy_store_locals(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
printf("STORE_LOCALS\n");
}
}
void emit_store_subscr(emitter_t *emit) {
static void emit_cpy_store_subscr(emit_t *emit) {
emit_pre(emit, -3, 1);
if (emit->pass == PASS_3) {
printf("STORE_SUBSCR\n");
}
}
void emit_delete_name(emitter_t *emit, qstr qstr) {
static void emit_cpy_delete_name(emit_t *emit, qstr qstr) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
printf("DELETE_NAME %s\n", qstr_str(qstr));
}
}
void emit_delete_global(emitter_t *emit, qstr qstr) {
static void emit_cpy_delete_global(emit_t *emit, qstr qstr) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
printf("DELETE_GLOBAL %s\n", qstr_str(qstr));
}
}
void emit_delete_fast(emitter_t *emit, qstr qstr, int local_num) {
static void emit_cpy_delete_fast(emit_t *emit, qstr qstr, int local_num) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
printf("DELETE_FAST %s\n", qstr_str(qstr));
}
}
void emit_delete_deref(emitter_t *emit, qstr qstr) {
static void emit_cpy_delete_deref(emit_t *emit, qstr qstr) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
printf("DELETE_DEREF %s\n", qstr_str(qstr));
}
}
void emit_delete_attr(emitter_t *emit, qstr qstr) {
static void emit_cpy_delete_attr(emit_t *emit, qstr qstr) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("DELETE_ATTR %s\n", qstr_str(qstr));
}
}
void emit_delete_subscr(emitter_t *emit) {
static void emit_cpy_delete_subscr(emit_t *emit) {
emit_pre(emit, -2, 1);
if (emit->pass == PASS_3) {
printf("DELETE_SUBSCR\n");
}
}
void emit_dup_top(emitter_t *emit) {
static void emit_cpy_dup_top(emit_t *emit) {
emit_pre(emit, 1, 1);
if (emit->pass == PASS_3) {
printf("DUP_TOP\n");
}
}
void emit_dup_top_two(emitter_t *emit) {
static void emit_cpy_dup_top_two(emit_t *emit) {
emit_pre(emit, 2, 1);
if (emit->pass == PASS_3) {
printf("DUP_TOP_TWO\n");
}
}
void emit_pop_top(emitter_t *emit) {
static void emit_cpy_pop_top(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
printf("POP_TOP\n");
}
}
void emit_rot_two(emitter_t *emit) {
static void emit_cpy_rot_two(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
printf("ROT_TWO\n");
}
}
void emit_rot_three(emitter_t *emit) {
static void emit_cpy_rot_three(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
printf("ROT_THREE\n");
}
}
void emit_jump(emitter_t *emit, int label) {
static void emit_cpy_jump(emit_t *emit, int label) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
int dest = emit->label_offsets[label];
@ -457,42 +454,42 @@ void emit_jump(emitter_t *emit, int label) {
}
}
void emit_pop_jump_if_true(emitter_t *emit, int label) {
static void emit_cpy_pop_jump_if_true(emit_t *emit, int label) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("POP_JUMP_IF_TRUE %d\n", emit->label_offsets[label]);
}
}
void emit_pop_jump_if_false(emitter_t *emit, int label) {
static void emit_cpy_pop_jump_if_false(emit_t *emit, int label) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("POP_JUMP_IF_FALSE %d\n", emit->label_offsets[label]);
}
}
void emit_jump_if_true_or_pop(emitter_t *emit, int label) {
static void emit_cpy_jump_if_true_or_pop(emit_t *emit, int label) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("JUMP_IF_TRUE_OR_POP %d\n", emit->label_offsets[label]);
}
}
void emit_jump_if_false_or_pop(emitter_t *emit, int label) {
static void emit_cpy_jump_if_false_or_pop(emit_t *emit, int label) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("JUMP_IF_FALSE_OR_POP %d\n", emit->label_offsets[label]);
}
}
void emit_setup_loop(emitter_t *emit, int label) {
static void emit_cpy_setup_loop(emit_t *emit, int label) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
printf("SETUP_LOOP %d\n", emit->label_offsets[label]);
}
}
void emit_break_loop(emitter_t *emit, int label) {
static void emit_cpy_break_loop(emit_t *emit, int label) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
printf("BREAK_LOOP\n"); // CPython doesn't have label
@ -500,81 +497,81 @@ void emit_break_loop(emitter_t *emit, int label) {
}
}
void emit_continue_loop(emitter_t *emit, int label) {
static void emit_cpy_continue_loop(emit_t *emit, int label) {
emit_pre(emit, 0, 3);
if (emit->pass == PASS_3) {
printf("CONTINUE_LOOP %d\n", emit->label_offsets[label]);
}
}
void emit_setup_with(emitter_t *emit, int label) {
static void emit_cpy_setup_with(emit_t *emit, int label) {
emit_pre(emit, 7, 3);
if (emit->pass == PASS_3) {
printf("SETUP_WITH %d\n", emit->label_offsets[label]);
}
}
void emit_with_cleanup(emitter_t *emit) {
static void emit_cpy_with_cleanup(emit_t *emit) {
emit_pre(emit, -7, 1);
if (emit->pass == PASS_3) {
printf("WITH_CLEANUP\n");
}
}
void emit_setup_except(emitter_t *emit, int label) {
static void emit_cpy_setup_except(emit_t *emit, int label) {
emit_pre(emit, 6, 3);
if (emit->pass == PASS_3) {
printf("SETUP_EXCEPT %d\n", emit->label_offsets[label]);
}
}
void emit_setup_finally(emitter_t *emit, int label) {
static void emit_cpy_setup_finally(emit_t *emit, int label) {
emit_pre(emit, 6, 3);
if (emit->pass == PASS_3) {
printf("SETUP_FINALLY %d\n", emit->label_offsets[label]);
}
}
void emit_end_finally(emitter_t *emit) {
static void emit_cpy_end_finally(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
printf("END_FINALLY\n");
}
}
void emit_get_iter(emitter_t *emit) {
static void emit_cpy_get_iter(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
printf("GET_ITER\n");
}
}
void emit_for_iter(emitter_t *emit, int label) {
static void emit_cpy_for_iter(emit_t *emit, int label) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("FOR_ITER %d\n", emit->label_offsets[label]);
}
}
void emit_for_iter_end(emitter_t *emit) {
static void emit_cpy_for_iter_end(emit_t *emit) {
emit_pre(emit, -1, 0);
}
void emit_pop_block(emitter_t *emit) {
static void emit_cpy_pop_block(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
printf("POP_BLOCK\n");
}
}
void emit_pop_except(emitter_t *emit) {
static void emit_cpy_pop_except(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
printf("POP_EXCEPT\n");
}
}
void emit_unary_op(emitter_t *emit, rt_unary_op_t op) {
static void emit_cpy_unary_op(emit_t *emit, rt_unary_op_t op) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_3) {
switch (op) {
@ -587,7 +584,7 @@ void emit_unary_op(emitter_t *emit, rt_unary_op_t op) {
}
}
void emit_binary_op(emitter_t *emit, rt_binary_op_t op) {
static void emit_cpy_binary_op(emit_t *emit, rt_binary_op_t op) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_3) {
switch (op) {
@ -621,7 +618,7 @@ void emit_binary_op(emitter_t *emit, rt_binary_op_t op) {
}
}
void emit_compare_op(emitter_t *emit, rt_compare_op_t op) {
static void emit_cpy_compare_op(emit_t *emit, rt_compare_op_t op) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
switch (op) {
@ -641,84 +638,84 @@ void emit_compare_op(emitter_t *emit, rt_compare_op_t op) {
}
}
void emit_build_tuple(emitter_t *emit, int n_args) {
static void emit_cpy_build_tuple(emit_t *emit, int n_args) {
emit_pre(emit, 1 - n_args, 3);
if (emit->pass == PASS_3) {
printf("BUILD_TUPLE %d\n", n_args);
}
}
void emit_build_list(emitter_t *emit, int n_args) {
static void emit_cpy_build_list(emit_t *emit, int n_args) {
emit_pre(emit, 1 - n_args, 3);
if (emit->pass == PASS_3) {
printf("BUILD_LIST %d\n", n_args);
}
}
void emit_list_append(emitter_t *emit, int list_index) {
static void emit_cpy_list_append(emit_t *emit, int list_index) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("LIST_APPEND %d\n", list_index);
}
}
void emit_build_map(emitter_t *emit, int n_args) {
static void emit_cpy_build_map(emit_t *emit, int n_args) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("BUILD_MAP %d\n", n_args);
}
}
void emit_store_map(emitter_t *emit) {
static void emit_cpy_store_map(emit_t *emit) {
emit_pre(emit, -2, 1);
if (emit->pass == PASS_3) {
printf("STORE_MAP\n");
}
}
void emit_map_add(emitter_t *emit, int map_index) {
static void emit_cpy_map_add(emit_t *emit, int map_index) {
emit_pre(emit, -2, 3);
if (emit->pass == PASS_3) {
printf("MAP_ADD %d\n", map_index);
}
}
void emit_build_set(emitter_t *emit, int n_args) {
static void emit_cpy_build_set(emit_t *emit, int n_args) {
emit_pre(emit, 1 - n_args, 3);
if (emit->pass == PASS_3) {
printf("BUILD_SET %d\n", n_args);
}
}
void emit_set_add(emitter_t *emit, int set_index) {
static void emit_cpy_set_add(emit_t *emit, int set_index) {
emit_pre(emit, -1, 3);
if (emit->pass == PASS_3) {
printf("SET_ADD %d\n", set_index);
}
}
void emit_build_slice(emitter_t *emit, int n_args) {
static void emit_cpy_build_slice(emit_t *emit, int n_args) {
emit_pre(emit, 1 - n_args, 3);
if (emit->pass == PASS_3) {
printf("BUILD_SLICE %d\n", n_args);
}
}
void emit_unpack_sequence(emitter_t *emit, int n_args) {
static void emit_cpy_unpack_sequence(emit_t *emit, int n_args) {
emit_pre(emit, -1 + n_args, 3);
if (emit->pass == PASS_3) {
printf("UNPACK_SEQUENCE %d\n", n_args);
}
}
void emit_unpack_ex(emitter_t *emit, int n_left, int n_right) {
static void emit_cpy_unpack_ex(emit_t *emit, int n_left, int n_right) {
emit_pre(emit, -1 + n_left + n_right + 1, 3);
if (emit->pass == PASS_3) {
printf("UNPACK_EX %d\n", n_left | (n_right << 8));
}
}
void emit_call_function(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
static void emit_cpy_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
int s = 0;
if (have_star_arg) {
s += 1;
@ -745,11 +742,11 @@ void emit_call_function(emitter_t *emit, int n_positional, int n_keyword, bool h
}
}
void emit_call_method(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
emit_call_function(emit, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
static void emit_cpy_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
emit_cpy_call_function(emit, n_positional, n_keyword, have_star_arg, have_dbl_star_arg);
}
void emit_return_value(emitter_t *emit) {
static void emit_cpy_return_value(emit_t *emit) {
emit_pre(emit, -1, 1);
emit->last_emit_was_return_value = true;
if (emit->pass == PASS_3) {
@ -757,14 +754,14 @@ void emit_return_value(emitter_t *emit) {
}
}
void emit_raise_varargs(emitter_t *emit, int n_args) {
static void emit_cpy_raise_varargs(emit_t *emit, int n_args) {
emit_pre(emit, -n_args, 3);
if (emit->pass == PASS_3) {
printf("RAISE_VARARGS %d\n", n_args);
}
}
void load_const_code_and_name(emitter_t *emit, qstr qstr) {
static void load_cpy_const_code_and_name(emit_t *emit, qstr qstr) {
emit_pre(emit, 1, 3);
if (emit->pass == PASS_3) {
printf("LOAD_CONST code %s\n", qstr_str(qstr));
@ -795,23 +792,23 @@ void load_const_code_and_name(emitter_t *emit, qstr qstr) {
}
}
void emit_make_function(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
load_const_code_and_name(emit, scope->simple_name);
static void emit_cpy_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
load_cpy_const_code_and_name(emit, scope->simple_name);
emit_pre(emit, -1 - n_default_params - 2 * n_dict_params, 3);
if (emit->pass == PASS_3) {
printf("MAKE_FUNCTION %d\n", (n_dict_params << 8) | n_default_params);
}
}
void emit_make_closure(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
load_const_code_and_name(emit, scope->simple_name);
static void emit_cpy_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
load_cpy_const_code_and_name(emit, scope->simple_name);
emit_pre(emit, -2 - n_default_params - 2 * n_dict_params, 3);
if (emit->pass == PASS_3) {
printf("MAKE_CLOSURE %d\n", (n_dict_params << 8) | n_default_params);
}
}
void emit_yield_value(emitter_t *emit) {
static void emit_cpy_yield_value(emit_t *emit) {
emit_pre(emit, 0, 1);
if (emit->pass == PASS_2) {
emit->scope->flags |= SCOPE_FLAG_GENERATOR;
@ -821,7 +818,7 @@ void emit_yield_value(emitter_t *emit) {
}
}
void emit_yield_from(emitter_t *emit) {
static void emit_cpy_yield_from(emit_t *emit) {
emit_pre(emit, -1, 1);
if (emit->pass == PASS_2) {
emit->scope->flags |= SCOPE_FLAG_GENERATOR;
@ -831,4 +828,106 @@ void emit_yield_from(emitter_t *emit) {
}
}
#endif // EMIT_DO_CPY
static const emit_method_table_t emit_cpy_method_table = {
emit_cpy_set_native_types,
emit_cpy_start_pass,
emit_cpy_end_pass,
emit_cpy_last_emit_was_return_value,
emit_cpy_get_stack_size,
emit_cpy_set_stack_size,
emit_cpy_label_new,
emit_cpy_label_assign,
emit_cpy_import_name,
emit_cpy_import_from,
emit_cpy_import_star,
emit_cpy_load_const_tok,
emit_cpy_load_const_small_int,
emit_cpy_load_const_int,
emit_cpy_load_const_dec,
emit_cpy_load_const_id,
emit_cpy_load_const_str,
emit_cpy_load_const_verbatim_start,
emit_cpy_load_const_verbatim_int,
emit_cpy_load_const_verbatim_str,
emit_cpy_load_const_verbatim_strn,
emit_cpy_load_const_verbatim_quoted_str,
emit_cpy_load_const_verbatim_end,
emit_cpy_load_fast,
emit_cpy_load_name,
emit_cpy_load_global,
emit_cpy_load_deref,
emit_cpy_load_closure,
emit_cpy_load_attr,
emit_cpy_load_method,
emit_cpy_load_build_class,
emit_cpy_store_fast,
emit_cpy_store_name,
emit_cpy_store_global,
emit_cpy_store_deref,
emit_cpy_store_attr,
emit_cpy_store_locals,
emit_cpy_store_subscr,
emit_cpy_delete_fast,
emit_cpy_delete_name,
emit_cpy_delete_global,
emit_cpy_delete_deref,
emit_cpy_delete_attr,
emit_cpy_delete_subscr,
emit_cpy_dup_top,
emit_cpy_dup_top_two,
emit_cpy_pop_top,
emit_cpy_rot_two,
emit_cpy_rot_three,
emit_cpy_jump,
emit_cpy_pop_jump_if_true,
emit_cpy_pop_jump_if_false,
emit_cpy_jump_if_true_or_pop,
emit_cpy_jump_if_false_or_pop,
emit_cpy_setup_loop,
emit_cpy_break_loop,
emit_cpy_continue_loop,
emit_cpy_setup_with,
emit_cpy_with_cleanup,
emit_cpy_setup_except,
emit_cpy_setup_finally,
emit_cpy_end_finally,
emit_cpy_get_iter,
emit_cpy_for_iter,
emit_cpy_for_iter_end,
emit_cpy_pop_block,
emit_cpy_pop_except,
emit_cpy_unary_op,
emit_cpy_binary_op,
emit_cpy_compare_op,
emit_cpy_build_tuple,
emit_cpy_build_list,
emit_cpy_list_append,
emit_cpy_build_map,
emit_cpy_store_map,
emit_cpy_map_add,
emit_cpy_build_set,
emit_cpy_set_add,
emit_cpy_build_slice,
emit_cpy_unpack_sequence,
emit_cpy_unpack_ex,
emit_cpy_make_function,
emit_cpy_make_closure,
emit_cpy_call_function,
emit_cpy_call_method,
emit_cpy_return_value,
emit_cpy_raise_varargs,
emit_cpy_yield_value,
emit_cpy_yield_from,
};
void emit_new_cpython(emit_t **emit_out, const emit_method_table_t **emit_method_table_out) {
emit_t *emit = m_new(emit_t, 1);
emit->max_num_labels = 0;
emit->label_offsets = NULL;
*emit_out = emit;
*emit_method_table_out = &emit_cpy_method_table;
}
#endif // EMIT_ENABLE_CPY

View File

@ -27,7 +27,7 @@
#include "emit.h"
#include "asmx64.h"
#ifdef EMIT_DO_X64
#ifdef EMIT_ENABLE_X64
#define REG_LOCAL_1 (REG_RBX)
#define REG_LOCAL_NUM (1)
@ -38,7 +38,7 @@ typedef enum {
NEED_TO_PUSH_I64,
} need_to_push_t;
struct _emitter_t {
struct _emit_t {
int pass;
int stack_start;
int stack_size;
@ -53,18 +53,11 @@ struct _emitter_t {
bool do_native_types;
};
emitter_t *emit_new() {
emitter_t *emit = m_new(emitter_t, 1);
emit->as = asm_x64_new();
emit->do_native_types = false;
return emit;
}
void emit_set_native_types(emitter_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;
}
void emit_start_pass(emitter_t *emit, pass_kind_t pass, scope_t *scope) {
static void emit_x64_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
emit->pass = pass;
emit->stack_start = 0;
emit->stack_size = 0;
@ -104,7 +97,7 @@ void emit_start_pass(emitter_t *emit, pass_kind_t pass, scope_t *scope) {
}
}
void emit_end_pass(emitter_t *emit) {
static void emit_x64_end_pass(emit_t *emit) {
if (!emit->last_emit_was_return_value) {
asm_x64_exit(emit->as);
}
@ -121,19 +114,19 @@ void emit_end_pass(emitter_t *emit) {
}
}
bool emit_last_emit_was_return_value(emitter_t *emit) {
static bool emit_x64_last_emit_was_return_value(emit_t *emit) {
return emit->last_emit_was_return_value;
}
int emit_get_stack_size(emitter_t *emit) {
static int emit_x64_get_stack_size(emit_t *emit) {
return emit->stack_size;
}
void emit_set_stack_size(emitter_t *emit, int size) {
static void emit_x64_set_stack_size(emit_t *emit, int size) {
emit->stack_size = size;
}
static void adjust_stack(emitter_t *emit, int stack_size_delta) {
static void adjust_stack(emit_t *emit, int stack_size_delta) {
emit->stack_size += stack_size_delta;
assert(emit->stack_size >= 0);
if (emit->pass > PASS_1 && emit->stack_size > emit->scope->stack_size) {
@ -141,7 +134,7 @@ static void adjust_stack(emitter_t *emit, int stack_size_delta) {
}
}
static void stack_settle(emitter_t *emit) {
static void stack_settle(emit_t *emit) {
switch (emit->need_to_push) {
case NEED_TO_PUSH_NOTHING:
break;
@ -160,17 +153,17 @@ static void stack_settle(emitter_t *emit) {
emit->need_to_push = NEED_TO_PUSH_NOTHING;
}
static void emit_pre_raw(emitter_t *emit, int stack_size_delta) {
static void emit_pre_raw(emit_t *emit, int stack_size_delta) {
adjust_stack(emit, stack_size_delta);
emit->last_emit_was_return_value = false;
}
static void emit_pre(emitter_t *emit) {
static void emit_pre(emit_t *emit) {
stack_settle(emit);
emit_pre_raw(emit, 0);
}
static void emit_pre_pop_r64(emitter_t *emit, int r64) {
static void emit_pre_pop_r64(emit_t *emit, int r64) {
switch (emit->need_to_push) {
case NEED_TO_PUSH_NOTHING:
asm_x64_mov_local_to_r64(emit->as, emit->stack_start + emit->stack_size - 1, r64);
@ -192,47 +185,47 @@ static void emit_pre_pop_r64(emitter_t *emit, int r64) {
emit->need_to_push = NEED_TO_PUSH_NOTHING;
}
static void emit_pre_pop_r64_r64(emitter_t *emit, int r64a, int r64b) {
static void emit_pre_pop_r64_r64(emit_t *emit, int r64a, int r64b) {
emit_pre_pop_r64(emit, r64a);
asm_x64_mov_local_to_r64(emit->as, emit->stack_start + emit->stack_size - 1, r64b);
adjust_stack(emit, -1);
}
static void emit_pre_pop_r64_r64_r64(emitter_t *emit, int r64a, int r64b, int r64c) {
static void emit_pre_pop_r64_r64_r64(emit_t *emit, int r64a, int r64b, int r64c) {
emit_pre_pop_r64(emit, r64a);
asm_x64_mov_local_to_r64(emit->as, emit->stack_start + emit->stack_size - 1, r64b);
asm_x64_mov_local_to_r64(emit->as, emit->stack_start + emit->stack_size - 2, r64c);
adjust_stack(emit, -2);
}
static void emit_post(emitter_t *emit) {
static void emit_post(emit_t *emit) {
}
static void emit_post_push_r64(emitter_t *emit, int r64) {
static void emit_post_push_r64(emit_t *emit, int r64) {
emit->need_to_push = NEED_TO_PUSH_R64;
emit->last_r64 = r64;
}
static void emit_post_push_i64(emitter_t *emit, int64_t i64) {
static void emit_post_push_i64(emit_t *emit, int64_t i64) {
emit->need_to_push = NEED_TO_PUSH_I64;
emit->last_i64 = i64;
}
static void emit_post_push_r64_r64(emitter_t *emit, int r64a, int r64b) {
static void emit_post_push_r64_r64(emit_t *emit, int r64a, int r64b) {
asm_x64_mov_r64_to_local(emit->as, r64a, emit->stack_start + emit->stack_size);
emit->need_to_push = NEED_TO_PUSH_R64;
emit->last_r64 = r64b;
adjust_stack(emit, 1);
}
static void emit_post_push_r64_r64_r64(emitter_t *emit, int r64a, int r64b, int r64c) {
static void emit_post_push_r64_r64_r64(emit_t *emit, int r64a, int r64b, int r64c) {
asm_x64_mov_r64_to_local(emit->as, r64a, emit->stack_start + emit->stack_size);
asm_x64_mov_r64_to_local(emit->as, r64b, emit->stack_start + emit->stack_size + 1);
asm_x64_mov_r64_to_local(emit->as, r64c, emit->stack_start + emit->stack_size + 2);
adjust_stack(emit, 3);
}
static void emit_post_push_r64_r64_r64_r64(emitter_t *emit, int r64a, int r64b, int r64c, int r64d) {
static void emit_post_push_r64_r64_r64_r64(emit_t *emit, int r64a, int r64b, int r64c, int r64d) {
asm_x64_mov_r64_to_local(emit->as, r64a, emit->stack_start + emit->stack_size);
asm_x64_mov_r64_to_local(emit->as, r64b, emit->stack_start + emit->stack_size + 1);
asm_x64_mov_r64_to_local(emit->as, r64c, emit->stack_start + emit->stack_size + 2);
@ -240,44 +233,44 @@ static void emit_post_push_r64_r64_r64_r64(emitter_t *emit, int r64a, int r64b,
adjust_stack(emit, 4);
}
static void emit_get_stack_pointer_to_r64_for_pop(emitter_t *emit, int r64, int n_pop) {
static void emit_get_stack_pointer_to_r64_for_pop(emit_t *emit, int r64, int n_pop) {
asm_x64_mov_local_addr_to_r64(emit->as, emit->stack_start + emit->stack_size - 1, r64);
adjust_stack(emit, -n_pop);
}
static void emit_get_stack_pointer_to_r64_for_push(emitter_t *emit, int r64, int n_push) {
static void emit_get_stack_pointer_to_r64_for_push(emit_t *emit, int r64, int n_push) {
asm_x64_mov_local_addr_to_r64(emit->as, emit->stack_start + emit->stack_size + n_push - 1, r64);
adjust_stack(emit, n_push);
}
static void emit_call(emitter_t *emit, void *fun) {
static void emit_call(emit_t *emit, void *fun) {
asm_x64_call_ind(emit->as, fun, REG_RAX);
}
static void emit_call_with_i64_arg(emitter_t *emit, void *fun, int64_t arg_val, int arg_r64) {
static void emit_call_with_i64_arg(emit_t *emit, void *fun, int64_t arg_val, int arg_r64) {
asm_x64_mov_i64_to_r64_optimised(emit->as, arg_val, arg_r64);
asm_x64_call_ind(emit->as, fun, REG_RAX);
}
int emit_label_new(emitter_t *emit) {
static int emit_x64_label_new(emit_t *emit) {
return asm_x64_label_new(emit->as);
}
void emit_label_assign(emitter_t *emit, int l) {
static void emit_x64_label_assign(emit_t *emit, int l) {
asm_x64_label_assign(emit->as, l);
}
void emit_import_name(emitter_t *emit, qstr qstr) {
static void emit_x64_import_name(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_import_from(emitter_t *emit, qstr qstr) {
static void emit_x64_import_from(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_import_star(emitter_t *emit) {
static void emit_x64_import_star(emit_t *emit) {
assert(0);
}
void emit_load_const_tok(emitter_t *emit, py_token_kind_t tok) {
static void emit_x64_load_const_tok(emit_t *emit, py_token_kind_t tok) {
emit_pre(emit);
py_obj_t o;
switch (tok) {
@ -289,7 +282,7 @@ void emit_load_const_tok(emitter_t *emit, py_token_kind_t tok) {
emit_post_push_i64(emit, (uint64_t)o);
}
void emit_load_const_small_int(emitter_t *emit, int arg) {
static void emit_x64_load_const_small_int(emit_t *emit, int arg) {
emit_pre(emit);
if (emit->do_native_types) {
emit_post_push_i64(emit, arg);
@ -298,42 +291,42 @@ void emit_load_const_small_int(emitter_t *emit, int arg) {
}
}
void emit_load_const_int(emitter_t *emit, qstr qstr) {
static void emit_x64_load_const_int(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_load_const_dec(emitter_t *emit, qstr qstr) {
static void emit_x64_load_const_dec(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_load_const_id(emitter_t *emit, qstr qstr) {
static void emit_x64_load_const_id(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_load_const_str(emitter_t *emit, qstr qstr, bool bytes) {
static void emit_x64_load_const_str(emit_t *emit, qstr qstr, bool bytes) {
emit_pre(emit);
emit_call_with_i64_arg(emit, rt_load_const_str, qstr, REG_ARG_1);
emit_post_push_r64(emit, REG_RET);
}
void emit_load_const_verbatim_start(emitter_t *emit) {
static void emit_x64_load_const_verbatim_start(emit_t *emit) {
assert(0);
}
void emit_load_const_verbatim_int(emitter_t *emit, int val) {
static void emit_x64_load_const_verbatim_int(emit_t *emit, int val) {
assert(0);
}
void emit_load_const_verbatim_str(emitter_t *emit, const char *str) {
static void emit_x64_load_const_verbatim_str(emit_t *emit, const char *str) {
assert(0);
}
void emit_load_const_verbatim_strn(emitter_t *emit, const char *str, int len) {
static void emit_x64_load_const_verbatim_strn(emit_t *emit, const char *str, int len) {
assert(0);
}
void emit_load_const_verbatim_quoted_str(emitter_t *emit, qstr qstr, bool bytes) {
static void emit_x64_load_const_verbatim_quoted_str(emit_t *emit, qstr qstr, bool bytes) {
assert(0);
}
void emit_load_const_verbatim_end(emitter_t *emit) {
static void emit_x64_load_const_verbatim_end(emit_t *emit) {
assert(0);
}
void emit_load_fast(emitter_t *emit, qstr qstr, int local_num) {
static void emit_x64_load_fast(emit_t *emit, qstr qstr, int local_num) {
if (local_num == 0) {
emit_pre(emit);
emit_post_push_r64(emit, REG_LOCAL_1);
@ -344,42 +337,42 @@ void emit_load_fast(emitter_t *emit, qstr qstr, int local_num) {
}
}
void emit_load_name(emitter_t *emit, qstr qstr) {
static void emit_x64_load_name(emit_t *emit, qstr qstr) {
emit_pre(emit);
emit_call_with_i64_arg(emit, rt_load_name, qstr, REG_ARG_1);
emit_post_push_r64(emit, REG_RET);
}
void emit_load_global(emitter_t *emit, qstr qstr) {
static void emit_x64_load_global(emit_t *emit, qstr qstr) {
emit_pre(emit);
emit_call_with_i64_arg(emit, rt_load_global, qstr, REG_ARG_1);
emit_post_push_r64(emit, REG_RET);
}
void emit_load_deref(emitter_t *emit, qstr qstr) {
static void emit_x64_load_deref(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_load_closure(emitter_t *emit, qstr qstr) {
static void emit_x64_load_closure(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_load_attr(emitter_t *emit, qstr qstr) {
static void emit_x64_load_attr(emit_t *emit, qstr qstr) {
emit_pre_pop_r64(emit, REG_ARG_1); // arg1 = base
emit_call_with_i64_arg(emit, rt_load_attr, qstr, REG_ARG_2); // arg2 = attribute name
emit_post_push_r64(emit, REG_RET);
}
void emit_load_method(emitter_t *emit, qstr qstr) {
static void emit_x64_load_method(emit_t *emit, qstr qstr) {
emit_pre_pop_r64(emit, REG_ARG_1); // arg1 = base
emit_get_stack_pointer_to_r64_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
emit_call_with_i64_arg(emit, rt_load_method, qstr, REG_ARG_2); // arg2 = method name
}
void emit_load_build_class(emitter_t *emit) {
static void emit_x64_load_build_class(emit_t *emit) {
assert(0);
} // basically load __build_class__ from builtins
void emit_store_fast(emitter_t *emit, qstr qstr, int local_num) {
static void emit_x64_store_fast(emit_t *emit, qstr qstr, int local_num) {
if (local_num == 0) {
emit_pre_pop_r64(emit, REG_LOCAL_1);
emit_post(emit);
@ -390,81 +383,81 @@ void emit_store_fast(emitter_t *emit, qstr qstr, int local_num) {
}
}
void emit_store_name(emitter_t *emit, qstr qstr) {
static void emit_x64_store_name(emit_t *emit, qstr qstr) {
emit_pre_pop_r64(emit, REG_ARG_2);
emit_call_with_i64_arg(emit, rt_store_name, qstr, REG_ARG_1); // arg1 = name
emit_post(emit);
}
void emit_store_global(emitter_t *emit, qstr qstr) {
static void emit_x64_store_global(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_store_deref(emitter_t *emit, qstr qstr) {
static void emit_x64_store_deref(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_store_attr(emitter_t *emit, qstr qstr) {
static void emit_x64_store_attr(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_store_locals(emitter_t *emit) {
static void emit_x64_store_locals(emit_t *emit) {
assert(0);
}
void emit_store_subscr(emitter_t *emit) {
static void emit_x64_store_subscr(emit_t *emit) {
emit_pre_pop_r64_r64_r64(emit, REG_ARG_2, REG_ARG_1, REG_ARG_3); // index, base, value to store
emit_call(emit, rt_store_subscr);
}
void emit_delete_fast(emitter_t *emit, qstr qstr, int local_num) {
static void emit_x64_delete_fast(emit_t *emit, qstr qstr, int local_num) {
assert(0);
}
void emit_delete_name(emitter_t *emit, qstr qstr) {
static void emit_x64_delete_name(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_delete_global(emitter_t *emit, qstr qstr) {
static void emit_x64_delete_global(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_delete_deref(emitter_t *emit, qstr qstr) {
static void emit_x64_delete_deref(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_delete_attr(emitter_t *emit, qstr qstr) {
static void emit_x64_delete_attr(emit_t *emit, qstr qstr) {
assert(0);
}
void emit_delete_subscr(emitter_t *emit) {
static void emit_x64_delete_subscr(emit_t *emit) {
assert(0);
}
void emit_dup_top(emitter_t *emit) {
static void emit_x64_dup_top(emit_t *emit) {
emit_pre_pop_r64(emit, REG_RAX);
emit_post_push_r64_r64(emit, REG_RAX, REG_RAX);
}
void emit_dup_top_two(emitter_t *emit) {
static void emit_x64_dup_top_two(emit_t *emit) {
emit_pre_pop_r64_r64(emit, REG_RAX, REG_RDI);
emit_post_push_r64_r64_r64_r64(emit, REG_RDI, REG_RAX, REG_RDI, REG_RAX);
}
void emit_pop_top(emitter_t *emit) {
static void emit_x64_pop_top(emit_t *emit) {
emit_pre_pop_r64(emit, REG_RAX);
emit_post(emit);
}
void emit_rot_two(emitter_t *emit) {
static void emit_x64_rot_two(emit_t *emit) {
assert(0);
}
void emit_rot_three(emitter_t *emit) {
static void emit_x64_rot_three(emit_t *emit) {
emit_pre_pop_r64_r64_r64(emit, REG_RAX, REG_RDI, REG_RSI);
emit_post_push_r64_r64_r64(emit, REG_RAX, REG_RSI, REG_RDI);
}
void emit_jump(emitter_t *emit, int label) {
static void emit_x64_jump(emit_t *emit, int label) {
emit_pre(emit);
asm_x64_jmp_label(emit->as, label);
emit_post(emit);
}
void emit_pop_jump_if_false(emitter_t *emit, int label) {
static void emit_x64_pop_jump_if_false(emit_t *emit, int label) {
if (emit->do_native_types) {
emit_pre_pop_r64(emit, REG_RET);
asm_x64_test_r8_with_r8(emit->as, REG_RET, REG_RET);
@ -479,124 +472,124 @@ void emit_pop_jump_if_false(emitter_t *emit, int label) {
}
}
void emit_pop_jump_if_true(emitter_t *emit, int label) {
static void emit_x64_pop_jump_if_true(emit_t *emit, int label) {
assert(0);
}
void emit_jump_if_true_or_pop(emitter_t *emit, int label) {
static void emit_x64_jump_if_true_or_pop(emit_t *emit, int label) {
assert(0);
}
void emit_jump_if_false_or_pop(emitter_t *emit, int label) {
static void emit_x64_jump_if_false_or_pop(emit_t *emit, int label) {
assert(0);
}
void emit_setup_loop(emitter_t *emit, int label) {
static void emit_x64_setup_loop(emit_t *emit, int label) {
emit_pre(emit);
emit_post(emit);
}
void emit_break_loop(emitter_t *emit, int label) {
static void emit_x64_break_loop(emit_t *emit, int label) {
assert(0);
}
void emit_continue_loop(emitter_t *emit, int label) {
static void emit_x64_continue_loop(emit_t *emit, int label) {
assert(0);
}
void emit_setup_with(emitter_t *emit, int label) {
static void emit_x64_setup_with(emit_t *emit, int label) {
assert(0);
}
void emit_with_cleanup(emitter_t *emit) {
static void emit_x64_with_cleanup(emit_t *emit) {
assert(0);
}
void emit_setup_except(emitter_t *emit, int label) {
static void emit_x64_setup_except(emit_t *emit, int label) {
assert(0);
}
void emit_setup_finally(emitter_t *emit, int label) {
static void emit_x64_setup_finally(emit_t *emit, int label) {
assert(0);
}
void emit_end_finally(emitter_t *emit) {
static void emit_x64_end_finally(emit_t *emit) {
assert(0);
}
void emit_get_iter(emitter_t *emit) {
static void emit_x64_get_iter(emit_t *emit) {
assert(0);
} // tos = getiter(tos)
void emit_for_iter(emitter_t *emit, int label) {
static void emit_x64_for_iter(emit_t *emit, int label) {
assert(0);
}
void emit_for_iter_end(emitter_t *emit) {
static void emit_x64_for_iter_end(emit_t *emit) {
assert(0);
}
void emit_pop_except(emitter_t *emit) {
static void emit_x64_pop_except(emit_t *emit) {
assert(0);
}
void emit_unary_op(emitter_t *emit, rt_unary_op_t op) {
static void emit_x64_unary_op(emit_t *emit, rt_unary_op_t op) {
emit_pre_pop_r64(emit, REG_ARG_2);
emit_call_with_i64_arg(emit, rt_unary_op, op, REG_ARG_1);
emit_post_push_r64(emit, REG_RET);
}
void emit_build_tuple(emitter_t *emit, int n_args) {
static void emit_x64_build_tuple(emit_t *emit, int n_args) {
assert(0);
}
void emit_build_list(emitter_t *emit, int n_args) {
static void emit_x64_build_list(emit_t *emit, int n_args) {
emit_pre(emit);
emit_get_stack_pointer_to_r64_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order
emit_call_with_i64_arg(emit, rt_build_list, n_args, REG_ARG_1);
emit_post_push_r64(emit, REG_RET); // new list
}
void emit_list_append(emitter_t *emit, int list_index) {
static void emit_x64_list_append(emit_t *emit, int list_index) {
assert(0);
}
void emit_build_map(emitter_t *emit, int n_args) {
static void emit_x64_build_map(emit_t *emit, int n_args) {
emit_pre(emit);
emit_call_with_i64_arg(emit, rt_build_map, n_args, REG_ARG_1);
emit_post_push_r64(emit, REG_RET); // new map
}
void emit_store_map(emitter_t *emit) {
static void emit_x64_store_map(emit_t *emit) {
emit_pre_pop_r64_r64_r64(emit, REG_ARG_2, REG_ARG_3, REG_ARG_1); // key, value, map
emit_call(emit, rt_store_map);
emit_post_push_r64(emit, REG_RET); // map
}
void emit_map_add(emitter_t *emit, int map_index) {
static void emit_x64_map_add(emit_t *emit, int map_index) {
assert(0);
}
void emit_build_set(emitter_t *emit, int n_args) {
static void emit_x64_build_set(emit_t *emit, int n_args) {
emit_pre(emit);
emit_get_stack_pointer_to_r64_for_pop(emit, REG_ARG_2, n_args); // pointer to items in reverse order
emit_call_with_i64_arg(emit, rt_build_set, n_args, REG_ARG_1);
emit_post_push_r64(emit, REG_RET); // new set
}
void emit_set_add(emitter_t *emit, int set_index) {
static void emit_x64_set_add(emit_t *emit, int set_index) {
assert(0);
}
void emit_build_slice(emitter_t *emit, int n_args) {
static void emit_x64_build_slice(emit_t *emit, int n_args) {
assert(0);
}
void emit_unpack_sequence(emitter_t *emit, int n_args) {
static void emit_x64_unpack_sequence(emit_t *emit, int n_args) {
assert(0);
}
void emit_unpack_ex(emitter_t *emit, int n_left, int n_right) {
static void emit_x64_unpack_ex(emit_t *emit, int n_left, int n_right) {
assert(0);
}
void emit_make_function(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
static void emit_x64_make_function(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
assert(n_default_params == 0 && n_dict_params == 0);
emit_pre(emit);
emit_call_with_i64_arg(emit, rt_make_function_from_id, scope->unique_code_id, REG_ARG_1);
emit_post_push_r64(emit, REG_RET);
}
void emit_make_closure(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
static void emit_x64_make_closure(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params) {
assert(0);
}
void emit_call_function(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
static void emit_x64_call_function(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
if (n_positional == 0) {
emit_pre_pop_r64(emit, REG_ARG_1); // the function
@ -613,7 +606,7 @@ void emit_call_function(emitter_t *emit, int n_positional, int n_keyword, bool h
emit_post_push_r64(emit, REG_RET);
}
void emit_call_method(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
static void emit_x64_call_method(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg) {
assert(n_keyword == 0 && !have_star_arg && !have_dbl_star_arg);
if (n_positional == 0) {
emit_pre_pop_r64_r64(emit, REG_ARG_2, REG_ARG_1); // the self object (or NULL), the method
@ -627,12 +620,12 @@ void emit_call_method(emitter_t *emit, int n_positional, int n_keyword, bool hav
emit_post_push_r64(emit, REG_RET);
}
void emit_pop_block(emitter_t *emit) {
static void emit_x64_pop_block(emit_t *emit) {
emit_pre(emit);
emit_post(emit);
}
void emit_binary_op(emitter_t *emit, rt_binary_op_t op) {
static void emit_x64_binary_op(emit_t *emit, rt_binary_op_t op) {
if (emit->do_native_types) {
assert(op == RT_BINARY_OP_ADD);
emit_pre_pop_r64_r64(emit, REG_ARG_2, REG_RET);
@ -645,7 +638,7 @@ void emit_binary_op(emitter_t *emit, rt_binary_op_t op) {
}
}
void emit_compare_op(emitter_t *emit, rt_compare_op_t op) {
static void emit_x64_compare_op(emit_t *emit, rt_compare_op_t op) {
if (emit->do_native_types) {
assert(op == RT_COMPARE_OP_LESS);
emit_pre_pop_r64_r64(emit, REG_ARG_3, REG_ARG_2);
@ -660,21 +653,123 @@ void emit_compare_op(emitter_t *emit, rt_compare_op_t op) {
}
}
void emit_return_value(emitter_t *emit) {
static void emit_x64_return_value(emit_t *emit) {
emit_pre_pop_r64(emit, REG_RAX);
emit->last_emit_was_return_value = true;
//asm_x64_call_ind(emit->as, 0, REG_RAX); to seg fault for debugging with gdb
asm_x64_exit(emit->as);
}
void emit_raise_varargs(emitter_t *emit, int n_args) {
static void emit_x64_raise_varargs(emit_t *emit, int n_args) {
assert(0);
}
void emit_yield_value(emitter_t *emit) {
static void emit_x64_yield_value(emit_t *emit) {
assert(0);
}
void emit_yield_from(emitter_t *emit) {
static void emit_x64_yield_from(emit_t *emit) {
assert(0);
}
#endif // EMIT_DO_X64
static const emit_method_table_t emit_x64_method_table = {
emit_x64_set_native_types,
emit_x64_start_pass,
emit_x64_end_pass,
emit_x64_last_emit_was_return_value,
emit_x64_get_stack_size,
emit_x64_set_stack_size,
emit_x64_label_new,
emit_x64_label_assign,
emit_x64_import_name,
emit_x64_import_from,
emit_x64_import_star,
emit_x64_load_const_tok,
emit_x64_load_const_small_int,
emit_x64_load_const_int,
emit_x64_load_const_dec,
emit_x64_load_const_id,
emit_x64_load_const_str,
emit_x64_load_const_verbatim_start,
emit_x64_load_const_verbatim_int,
emit_x64_load_const_verbatim_str,
emit_x64_load_const_verbatim_strn,
emit_x64_load_const_verbatim_quoted_str,
emit_x64_load_const_verbatim_end,
emit_x64_load_fast,
emit_x64_load_name,
emit_x64_load_global,
emit_x64_load_deref,
emit_x64_load_closure,
emit_x64_load_attr,
emit_x64_load_method,
emit_x64_load_build_class,
emit_x64_store_fast,
emit_x64_store_name,
emit_x64_store_global,
emit_x64_store_deref,
emit_x64_store_attr,
emit_x64_store_locals,
emit_x64_store_subscr,
emit_x64_delete_fast,
emit_x64_delete_name,
emit_x64_delete_global,
emit_x64_delete_deref,
emit_x64_delete_attr,
emit_x64_delete_subscr,
emit_x64_dup_top,
emit_x64_dup_top_two,
emit_x64_pop_top,
emit_x64_rot_two,
emit_x64_rot_three,
emit_x64_jump,
emit_x64_pop_jump_if_true,
emit_x64_pop_jump_if_false,
emit_x64_jump_if_true_or_pop,
emit_x64_jump_if_false_or_pop,
emit_x64_setup_loop,
emit_x64_break_loop,
emit_x64_continue_loop,
emit_x64_setup_with,
emit_x64_with_cleanup,
emit_x64_setup_except,
emit_x64_setup_finally,
emit_x64_end_finally,
emit_x64_get_iter,
emit_x64_for_iter,
emit_x64_for_iter_end,
emit_x64_pop_block,
emit_x64_pop_except,
emit_x64_unary_op,
emit_x64_binary_op,
emit_x64_compare_op,
emit_x64_build_tuple,
emit_x64_build_list,
emit_x64_list_append,
emit_x64_build_map,
emit_x64_store_map,
emit_x64_map_add,
emit_x64_build_set,
emit_x64_set_add,
emit_x64_build_slice,
emit_x64_unpack_sequence,
emit_x64_unpack_ex,
emit_x64_make_function,
emit_x64_make_closure,
emit_x64_call_function,
emit_x64_call_method,
emit_x64_return_value,
emit_x64_raise_varargs,
emit_x64_yield_value,
emit_x64_yield_from,
};
void emit_new_x64(emit_t **emit_out, const emit_method_table_t **emit_method_table_out) {
emit_t *emit = m_new(emit_t, 1);
emit->as = asm_x64_new();
emit->do_native_types = false;
*emit_out = emit;
*emit_method_table_out = &emit_x64_method_table;
}
#endif // EMIT_ENABLE_X64

View File

@ -165,6 +165,46 @@ void scope_close_over_in_parents(scope_t *scope, qstr qstr) {
assert(0); // we should have found the variable in one of the parents
}
void scope_declare_global(scope_t *scope, qstr qstr) {
if (scope->kind == SCOPE_MODULE) {
printf("SyntaxError?: can't declare global in outer code\n");
return;
}
bool added;
id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
if (!added) {
printf("SyntaxError?: identifier already declared something\n");
return;
}
id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
// if the id exists in the global scope, set its kind to EXPLICIT_GLOBAL
id_info = scope_find_global(scope, qstr);
if (id_info != NULL) {
id_info->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
}
}
void scope_declare_nonlocal(scope_t *scope, qstr qstr) {
if (scope->kind == SCOPE_MODULE) {
printf("SyntaxError?: can't declare nonlocal in outer code\n");
return;
}
bool added;
id_info_t *id_info = scope_find_or_add_id(scope, qstr, &added);
if (!added) {
printf("SyntaxError?: identifier already declared something\n");
return;
}
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)) {
printf("SyntaxError: no binding for nonlocal '%s' found\n", qstr_str(qstr));
return;
}
id_info->kind = ID_INFO_KIND_FREE;
scope_close_over_in_parents(scope, qstr);
}
void scope_print_info(scope_t *s) {
if (s->kind == SCOPE_MODULE) {
printf("code <module>\n");

View File

@ -55,4 +55,6 @@ id_info_t *scope_find(scope_t *scope, qstr qstr);
id_info_t *scope_find_global(scope_t *scope, qstr qstr);
id_info_t *scope_find_local_in_parent(scope_t *scope, qstr qstr);
void scope_close_over_in_parents(scope_t *scope, qstr qstr);
void scope_declare_global(scope_t *scope, qstr qstr);
void scope_declare_nonlocal(scope_t *scope, qstr qstr);
void scope_print_info(scope_t *s);