Browse Source

py, compiler: When just bytecode, make explicit calls instead of table.

When just the bytecode emitter is needed there is no need to have a
dynamic method table for the emitter back-end, and we can instead
directly call the mp_emit_bc_XXX functions.  This gives a significant
reduction in code size and a very slight performance boost for the
compiler.

This patch saves 1160 bytes code on Thumb2 and 972 bytes on x86, when
native emitters are disabled.

Overall savings in code over the last 3 commits are:

bare-arm: 1664 bytes.
minimal:  2136 bytes.
stmhal:    584 bytes (it has native emitter enabled).
cc3200:   1736 bytes.
pull/1164/merge
Damien George 10 years ago
parent
commit
4112590a60
  1. 35
      py/compile.c
  2. 91
      py/emit.h
  3. 362
      py/emitbc.c

35
py/compile.c

@ -49,6 +49,11 @@ typedef enum {
PN_const_object, // special node for a constant, generic Python object
} pn_kind_t;
#define NEED_METHOD_TABLE (MICROPY_EMIT_CPYTHON || MICROPY_EMIT_NATIVE)
#if NEED_METHOD_TABLE
// we need a method table to do the lookup for the emitter functions
#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
@ -56,6 +61,16 @@ typedef enum {
#define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
#define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
#else
// if we only have the bytecode emitter enabled then we can do a direct call to the functions
#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
#endif
// elements in this struct are ordered to make it compact
typedef struct _compiler_t {
qstr source_file;
@ -83,7 +98,9 @@ typedef struct _compiler_t {
scope_t *scope_cur;
emit_t *emit; // current emitter
#if NEED_METHOD_TABLE
const emit_method_table_t *emit_method_table; // current emit method table
#endif
#if MICROPY_EMIT_INLINE_THUMB
emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
@ -406,7 +423,11 @@ STATIC void compile_load_id(compiler_t *comp, qstr qst) {
if (comp->pass == MP_PASS_SCOPE) {
mp_emit_common_get_id_for_load(comp->scope_cur, qst);
} else {
#if NEED_METHOD_TABLE
mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst);
#else
mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_load_id_ops, comp->scope_cur, qst);
#endif
}
}
@ -414,7 +435,11 @@ STATIC void compile_store_id(compiler_t *comp, qstr qst) {
if (comp->pass == MP_PASS_SCOPE) {
mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
} else {
#if NEED_METHOD_TABLE
mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst);
#else
mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_store_id_ops, comp->scope_cur, qst);
#endif
}
}
@ -422,7 +447,11 @@ STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
if (comp->pass == MP_PASS_SCOPE) {
mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
} else {
#if NEED_METHOD_TABLE
mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst);
#else
mp_emit_common_id_op(comp->emit, &mp_emit_bc_method_table_delete_id_ops, comp->scope_cur, qst);
#endif
}
}
@ -2963,7 +2992,7 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
}
} else {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
EMIT_ARG(set_line_number, pns->source_line);
EMIT_ARG(set_source_line, pns->source_line);
compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
if (f == NULL) {
#if MICROPY_DEBUG_PRINTERS
@ -3660,8 +3689,10 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
comp->emit_method_table = &emit_cpython_method_table;
#else
comp->emit = emit_bc;
#if MICROPY_EMIT_NATIVE
comp->emit_method_table = &emit_bc_method_table;
#endif
#endif
#if MICROPY_EMIT_INLINE_THUMB
comp->emit_inline_asm = NULL;
comp->emit_inline_asm_method_table = NULL;
@ -3765,7 +3796,9 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
default:
comp->emit = emit_bc;
#if MICROPY_EMIT_NATIVE
comp->emit_method_table = &emit_bc_method_table;
#endif
break;
}
#endif // !MICROPY_EMIT_CPYTHON

91
py/emit.h

@ -72,7 +72,7 @@ typedef struct _emit_method_table_t {
void (*end_pass)(emit_t *emit);
bool (*last_emit_was_return_value)(emit_t *emit);
void (*adjust_stack_size)(emit_t *emit, mp_int_t delta);
void (*set_line_number)(emit_t *emit, mp_uint_t line);
void (*set_source_line)(emit_t *emit, mp_uint_t line);
mp_emit_method_table_id_ops_t load_id;
mp_emit_method_table_id_ops_t store_id;
@ -166,6 +166,10 @@ extern const emit_method_table_t emit_native_x86_method_table;
extern const emit_method_table_t emit_native_thumb_method_table;
extern const emit_method_table_t emit_native_arm_method_table;
extern const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops;
extern const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops;
extern const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops;
emit_t *emit_cpython_new(void);
emit_t *emit_bc_new(void);
emit_t *emit_native_x64_new(mp_uint_t max_num_labels);
@ -183,6 +187,91 @@ void emit_native_x86_free(emit_t *emit);
void emit_native_thumb_free(emit_t *emit);
void emit_native_arm_free(emit_t *emit);
void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope);
void mp_emit_bc_end_pass(emit_t *emit);
bool mp_emit_bc_last_emit_was_return_value(emit_t *emit);
void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta);
void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t line);
void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num);
void mp_emit_bc_load_name(emit_t *emit, qstr qst);
void mp_emit_bc_load_global(emit_t *emit, qstr qst);
void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num);
void mp_emit_bc_store_name(emit_t *emit, qstr qst);
void mp_emit_bc_store_global(emit_t *emit, qstr qst);
void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num);
void mp_emit_bc_delete_name(emit_t *emit, qstr qst);
void mp_emit_bc_delete_global(emit_t *emit, qstr qst);
void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l);
void mp_emit_bc_import_name(emit_t *emit, qstr qst);
void mp_emit_bc_import_from(emit_t *emit, qstr qst);
void mp_emit_bc_import_star(emit_t *emit);
void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok);
void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg);
void mp_emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes);
void mp_emit_bc_load_const_obj(emit_t *emit, void *obj);
void mp_emit_bc_load_null(emit_t *emit);
void mp_emit_bc_load_attr(emit_t *emit, qstr qst);
void mp_emit_bc_load_method(emit_t *emit, qstr qst);
void mp_emit_bc_load_build_class(emit_t *emit);
void mp_emit_bc_load_subscr(emit_t *emit);
void mp_emit_bc_store_attr(emit_t *emit, qstr qst);
void mp_emit_bc_store_subscr(emit_t *emit);
void mp_emit_bc_delete_attr(emit_t *emit, qstr qst);
void mp_emit_bc_delete_subscr(emit_t *emit);
void mp_emit_bc_dup_top(emit_t *emit);
void mp_emit_bc_dup_top_two(emit_t *emit);
void mp_emit_bc_pop_top(emit_t *emit);
void mp_emit_bc_rot_two(emit_t *emit);
void mp_emit_bc_rot_three(emit_t *emit);
void mp_emit_bc_jump(emit_t *emit, mp_uint_t label);
void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label);
void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label);
void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
#define mp_emit_bc_break_loop mp_emit_bc_unwind_jump
#define mp_emit_bc_continue_loop mp_emit_bc_unwind_jump
void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label);
void mp_emit_bc_with_cleanup(emit_t *emit);
void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label);
void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label);
void mp_emit_bc_end_finally(emit_t *emit);
void mp_emit_bc_get_iter(emit_t *emit);
void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label);
void mp_emit_bc_for_iter_end(emit_t *emit);
void mp_emit_bc_pop_block(emit_t *emit);
void mp_emit_bc_pop_except(emit_t *emit);
void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op);
void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op);
void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args);
void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args);
void mp_emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index);
void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args);
void mp_emit_bc_store_map(emit_t *emit);
void mp_emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index);
#if MICROPY_PY_BUILTINS_SET
void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args);
void mp_emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index);
#endif
#if MICROPY_PY_BUILTINS_SLICE
void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args);
#endif
void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args);
void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right);
void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults);
void mp_emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults);
void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags);
void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags);
void mp_emit_bc_return_value(emit_t *emit);
void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args);
void mp_emit_bc_yield_value(emit_t *emit);
void mp_emit_bc_yield_from(emit_t *emit);
void mp_emit_bc_start_except_handler(emit_t *emit);
void mp_emit_bc_end_except_handler(emit_t *emit);
typedef struct _emit_inline_asm_t emit_inline_asm_t;
typedef struct _emit_inline_asm_method_table_t {

362
py/emitbc.c

@ -62,9 +62,6 @@ struct _emit_t {
byte dummy_data[DUMMY_DATA_SIZE];
};
STATIC void emit_bc_rot_two(emit_t *emit);
STATIC void emit_bc_rot_three(emit_t *emit);
emit_t *emit_bc_new(void) {
emit_t *emit = m_new0(emit_t, 1);
return emit;
@ -270,14 +267,16 @@ STATIC void emit_write_bytecode_byte_signed_label(emit_t* emit, byte b1, mp_uint
c[2] = bytecode_offset >> 8;
}
STATIC void emit_bc_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
#if MICROPY_EMIT_NATIVE
STATIC void mp_emit_bc_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
(void)emit;
(void)op;
(void)arg1;
(void)arg2;
}
#endif
STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
emit->pass = pass;
emit->stack_size = 0;
emit->last_emit_was_return_value = false;
@ -340,7 +339,7 @@ STATIC void emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
}
}
STATIC void emit_bc_end_pass(emit_t *emit) {
void mp_emit_bc_end_pass(emit_t *emit) {
if (emit->pass == MP_PASS_SCOPE) {
return;
}
@ -384,15 +383,15 @@ STATIC void emit_bc_end_pass(emit_t *emit) {
}
}
STATIC bool emit_bc_last_emit_was_return_value(emit_t *emit) {
bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
return emit->last_emit_was_return_value;
}
STATIC void emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
emit->stack_size += delta;
}
STATIC void emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
//printf("source: line %d -> %d offset %d -> %d\n", emit->last_source_line, source_line, emit->last_source_line_offset, emit->bytecode_offset);
#if MICROPY_ENABLE_SOURCE_LINE
if (MP_STATE_VM(mp_optimise_value) >= 3) {
@ -421,7 +420,7 @@ STATIC void emit_bc_pre(emit_t *emit, mp_int_t stack_size_delta) {
emit->last_emit_was_return_value = false;
}
STATIC void emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
emit_bc_pre(emit, 0);
if (emit->pass == MP_PASS_SCOPE) {
return;
@ -438,22 +437,22 @@ STATIC void emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
}
}
STATIC void emit_bc_import_name(emit_t *emit, qstr qst) {
void mp_emit_bc_import_name(emit_t *emit, qstr qst) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
}
STATIC void emit_bc_import_from(emit_t *emit, qstr qst) {
void mp_emit_bc_import_from(emit_t *emit, qstr qst) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
}
STATIC void emit_bc_import_star(emit_t *emit) {
void mp_emit_bc_import_star(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
}
STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
emit_bc_pre(emit, 1);
switch (tok) {
case MP_TOKEN_KW_FALSE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_FALSE); break;
@ -465,7 +464,7 @@ STATIC void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
}
}
STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
emit_bc_pre(emit, 1);
if (-16 <= arg && arg <= 47) {
emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_SMALL_INT_MULTI + 16 + arg);
@ -474,7 +473,7 @@ STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
}
}
STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
void mp_emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
emit_bc_pre(emit, 1);
if (bytes) {
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_BYTES, qst);
@ -483,17 +482,17 @@ STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
}
}
STATIC void emit_bc_load_const_obj(emit_t *emit, void *obj) {
void mp_emit_bc_load_const_obj(emit_t *emit, void *obj) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_ptr(emit, MP_BC_LOAD_CONST_OBJ, obj);
}
STATIC void emit_bc_load_null(emit_t *emit) {
void mp_emit_bc_load_null(emit_t *emit) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
};
STATIC void emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
(void)qst;
assert(local_num >= 0);
emit_bc_pre(emit, 1);
@ -504,13 +503,13 @@ STATIC void emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
}
}
STATIC void emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
(void)qst;
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
}
STATIC void emit_bc_load_name(emit_t *emit, qstr qst) {
void mp_emit_bc_load_name(emit_t *emit, qstr qst) {
(void)qst;
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
@ -519,7 +518,7 @@ STATIC void emit_bc_load_name(emit_t *emit, qstr qst) {
}
}
STATIC void emit_bc_load_global(emit_t *emit, qstr qst) {
void mp_emit_bc_load_global(emit_t *emit, qstr qst) {
(void)qst;
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
@ -528,7 +527,7 @@ STATIC void emit_bc_load_global(emit_t *emit, qstr qst) {
}
}
STATIC void emit_bc_load_attr(emit_t *emit, qstr qst) {
void mp_emit_bc_load_attr(emit_t *emit, qstr qst) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_ATTR, qst);
if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
@ -536,22 +535,22 @@ STATIC void emit_bc_load_attr(emit_t *emit, qstr qst) {
}
}
STATIC void emit_bc_load_method(emit_t *emit, qstr qst) {
void mp_emit_bc_load_method(emit_t *emit, qstr qst) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_METHOD, qst);
}
STATIC void emit_bc_load_build_class(emit_t *emit) {
void mp_emit_bc_load_build_class(emit_t *emit) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte(emit, MP_BC_LOAD_BUILD_CLASS);
}
STATIC void emit_bc_load_subscr(emit_t *emit) {
void mp_emit_bc_load_subscr(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
}
STATIC void emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
(void)qst;
assert(local_num >= 0);
emit_bc_pre(emit, -1);
@ -562,23 +561,23 @@ STATIC void emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
}
}
STATIC void emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
(void)qst;
emit_bc_pre(emit, -1);
emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
}
STATIC void emit_bc_store_name(emit_t *emit, qstr qst) {
void mp_emit_bc_store_name(emit_t *emit, qstr qst) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
}
STATIC void emit_bc_store_global(emit_t *emit, qstr qst) {
void mp_emit_bc_store_global(emit_t *emit, qstr qst) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
}
STATIC void emit_bc_store_attr(emit_t *emit, qstr qst) {
void mp_emit_bc_store_attr(emit_t *emit, qstr qst) {
emit_bc_pre(emit, -2);
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_ATTR, qst);
if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
@ -586,74 +585,74 @@ STATIC void emit_bc_store_attr(emit_t *emit, qstr qst) {
}
}
STATIC void emit_bc_store_subscr(emit_t *emit) {
void mp_emit_bc_store_subscr(emit_t *emit) {
emit_bc_pre(emit, -3);
emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
}
STATIC void emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
(void)qst;
emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
}
STATIC void emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
(void)qst;
emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
}
STATIC void emit_bc_delete_name(emit_t *emit, qstr qst) {
void mp_emit_bc_delete_name(emit_t *emit, qstr qst) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
}
STATIC void emit_bc_delete_global(emit_t *emit, qstr qst) {
void mp_emit_bc_delete_global(emit_t *emit, qstr qst) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
}
STATIC void emit_bc_delete_attr(emit_t *emit, qstr qst) {
emit_bc_load_null(emit);
emit_bc_rot_two(emit);
emit_bc_store_attr(emit, qst);
void mp_emit_bc_delete_attr(emit_t *emit, qstr qst) {
mp_emit_bc_load_null(emit);
mp_emit_bc_rot_two(emit);
mp_emit_bc_store_attr(emit, qst);
}
STATIC void emit_bc_delete_subscr(emit_t *emit) {
emit_bc_load_null(emit);
emit_bc_rot_three(emit);
emit_bc_store_subscr(emit);
void mp_emit_bc_delete_subscr(emit_t *emit) {
mp_emit_bc_load_null(emit);
mp_emit_bc_rot_three(emit);
mp_emit_bc_store_subscr(emit);
}
STATIC void emit_bc_dup_top(emit_t *emit) {
void mp_emit_bc_dup_top(emit_t *emit) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte(emit, MP_BC_DUP_TOP);
}
STATIC void emit_bc_dup_top_two(emit_t *emit) {
void mp_emit_bc_dup_top_two(emit_t *emit) {
emit_bc_pre(emit, 2);
emit_write_bytecode_byte(emit, MP_BC_DUP_TOP_TWO);
}
STATIC void emit_bc_pop_top(emit_t *emit) {
void mp_emit_bc_pop_top(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte(emit, MP_BC_POP_TOP);
}
STATIC void emit_bc_rot_two(emit_t *emit) {
void mp_emit_bc_rot_two(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte(emit, MP_BC_ROT_TWO);
}
STATIC void emit_bc_rot_three(emit_t *emit) {
void mp_emit_bc_rot_three(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte(emit, MP_BC_ROT_THREE);
}
STATIC void emit_bc_jump(emit_t *emit, mp_uint_t label) {
void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP, label);
}
STATIC void emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
emit_bc_pre(emit, -1);
if (cond) {
emit_write_bytecode_byte_signed_label(emit, MP_BC_POP_JUMP_IF_TRUE, label);
@ -662,7 +661,7 @@ STATIC void emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
}
}
STATIC void emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
emit_bc_pre(emit, -1);
if (cond) {
emit_write_bytecode_byte_signed_label(emit, MP_BC_JUMP_IF_TRUE_OR_POP, label);
@ -671,7 +670,7 @@ STATIC void emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
}
}
STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
if (except_depth == 0) {
emit_bc_pre(emit, 0);
if (label & MP_EMIT_BREAK_FROM_FOR) {
@ -685,56 +684,56 @@ STATIC void emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_
}
}
STATIC void emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label) {
emit_bc_pre(emit, 7);
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_WITH, label);
}
STATIC void emit_bc_with_cleanup(emit_t *emit) {
void mp_emit_bc_with_cleanup(emit_t *emit) {
emit_bc_pre(emit, -7);
emit_write_bytecode_byte(emit, MP_BC_WITH_CLEANUP);
}
STATIC void emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_EXCEPT, label);
}
STATIC void emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_SETUP_FINALLY, label);
}
STATIC void emit_bc_end_finally(emit_t *emit) {
void mp_emit_bc_end_finally(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte(emit, MP_BC_END_FINALLY);
}
STATIC void emit_bc_get_iter(emit_t *emit) {
void mp_emit_bc_get_iter(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte(emit, MP_BC_GET_ITER);
}
STATIC void emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_unsigned_label(emit, MP_BC_FOR_ITER, label);
}
STATIC void emit_bc_for_iter_end(emit_t *emit) {
void mp_emit_bc_for_iter_end(emit_t *emit) {
emit_bc_pre(emit, -1);
}
STATIC void emit_bc_pop_block(emit_t *emit) {
void mp_emit_bc_pop_block(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte(emit, MP_BC_POP_BLOCK);
}
STATIC void emit_bc_pop_except(emit_t *emit) {
void mp_emit_bc_pop_except(emit_t *emit) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte(emit, MP_BC_POP_EXCEPT);
}
STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
if (op == MP_UNARY_OP_NOT) {
emit_bc_pre(emit, 0);
emit_write_bytecode_byte(emit, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_BOOL);
@ -746,7 +745,7 @@ STATIC void emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
}
}
STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
bool invert = false;
if (op == MP_BINARY_OP_NOT_IN) {
invert = true;
@ -763,66 +762,66 @@ STATIC void emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
}
}
STATIC void emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
emit_bc_pre(emit, 1 - n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
}
STATIC void emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
emit_bc_pre(emit, 1 - n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
}
STATIC void emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
void mp_emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte_uint(emit, MP_BC_LIST_APPEND, list_stack_index);
}
STATIC void emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
}
STATIC void emit_bc_store_map(emit_t *emit) {
void mp_emit_bc_store_map(emit_t *emit) {
emit_bc_pre(emit, -2);
emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
}
STATIC void emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
void mp_emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index) {
emit_bc_pre(emit, -2);
emit_write_bytecode_byte_uint(emit, MP_BC_MAP_ADD, map_stack_index);
}
#if MICROPY_PY_BUILTINS_SET
STATIC void emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args) {
emit_bc_pre(emit, 1 - n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SET, n_args);
}
STATIC void emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
void mp_emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte_uint(emit, MP_BC_SET_ADD, set_stack_index);
}
#endif
#if MICROPY_PY_BUILTINS_SLICE
STATIC void emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args) {
emit_bc_pre(emit, 1 - n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_SLICE, n_args);
}
#endif
STATIC void emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
emit_bc_pre(emit, -1 + n_args);
emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_SEQUENCE, n_args);
}
STATIC void emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
emit_bc_pre(emit, -1 + n_left + n_right + 1);
emit_write_bytecode_byte_uint(emit, MP_BC_UNPACK_EX, n_left | (n_right << 8));
}
STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
if (n_pos_defaults == 0 && n_kw_defaults == 0) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_FUNCTION, scope->raw_code);
@ -832,7 +831,7 @@ STATIC void emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_
}
}
STATIC void emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
void mp_emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
if (n_pos_defaults == 0 && n_kw_defaults == 0) {
emit_bc_pre(emit, -n_closed_over + 1);
emit_write_bytecode_byte_ptr(emit, MP_BC_MAKE_CLOSURE, scope->raw_code);
@ -849,11 +848,11 @@ STATIC void emit_bc_call_function_method_helper(emit_t *emit, mp_int_t stack_adj
if (star_flags) {
if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
// load dummy entry for non-existent pos_seq
emit_bc_load_null(emit);
emit_bc_rot_two(emit);
mp_emit_bc_load_null(emit);
mp_emit_bc_rot_two(emit);
} else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
// load dummy entry for non-existent kw_dict
emit_bc_load_null(emit);
mp_emit_bc_load_null(emit);
}
emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - 2);
emit_write_bytecode_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
@ -863,138 +862,161 @@ STATIC void emit_bc_call_function_method_helper(emit_t *emit, mp_int_t stack_adj
}
}
STATIC void emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
}
STATIC void emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
}
STATIC void emit_bc_return_value(emit_t *emit) {
void mp_emit_bc_return_value(emit_t *emit) {
emit_bc_pre(emit, -1);
emit->last_emit_was_return_value = true;
emit_write_bytecode_byte(emit, MP_BC_RETURN_VALUE);
}
STATIC void emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
assert(0 <= n_args && n_args <= 2);
emit_bc_pre(emit, -n_args);
emit_write_bytecode_byte_byte(emit, MP_BC_RAISE_VARARGS, n_args);
}
STATIC void emit_bc_yield_value(emit_t *emit) {
void mp_emit_bc_yield_value(emit_t *emit) {
emit_bc_pre(emit, 0);
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
emit_write_bytecode_byte(emit, MP_BC_YIELD_VALUE);
}
STATIC void emit_bc_yield_from(emit_t *emit) {
void mp_emit_bc_yield_from(emit_t *emit) {
emit_bc_pre(emit, -1);
emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
emit_write_bytecode_byte(emit, MP_BC_YIELD_FROM);
}
STATIC void emit_bc_start_except_handler(emit_t *emit) {
emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
void mp_emit_bc_start_except_handler(emit_t *emit) {
mp_emit_bc_adjust_stack_size(emit, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
}
STATIC void emit_bc_end_except_handler(emit_t *emit) {
emit_bc_adjust_stack_size(emit, -5); // stack adjust
void mp_emit_bc_end_except_handler(emit_t *emit) {
mp_emit_bc_adjust_stack_size(emit, -5); // stack adjust
}
#if MICROPY_EMIT_NATIVE
const emit_method_table_t emit_bc_method_table = {
emit_bc_set_native_type,
emit_bc_start_pass,
emit_bc_end_pass,
emit_bc_last_emit_was_return_value,
emit_bc_adjust_stack_size,
emit_bc_set_source_line,
mp_emit_bc_set_native_type,
mp_emit_bc_start_pass,
mp_emit_bc_end_pass,
mp_emit_bc_last_emit_was_return_value,
mp_emit_bc_adjust_stack_size,
mp_emit_bc_set_source_line,
{
emit_bc_load_fast,
emit_bc_load_deref,
emit_bc_load_name,
emit_bc_load_global,
mp_emit_bc_load_fast,
mp_emit_bc_load_deref,
mp_emit_bc_load_name,
mp_emit_bc_load_global,
},
{
emit_bc_store_fast,
emit_bc_store_deref,
emit_bc_store_name,
emit_bc_store_global,
mp_emit_bc_store_fast,
mp_emit_bc_store_deref,
mp_emit_bc_store_name,
mp_emit_bc_store_global,
},
{
emit_bc_delete_fast,
emit_bc_delete_deref,
emit_bc_delete_name,
emit_bc_delete_global,
mp_emit_bc_delete_fast,
mp_emit_bc_delete_deref,
mp_emit_bc_delete_name,
mp_emit_bc_delete_global,
},
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_str,
emit_bc_load_const_obj,
emit_bc_load_null,
emit_bc_load_attr,
emit_bc_load_method,
emit_bc_load_build_class,
emit_bc_load_subscr,
emit_bc_store_attr,
emit_bc_store_subscr,
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,
emit_bc_jump_if_or_pop,
emit_bc_unwind_jump,
emit_bc_unwind_jump,
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_build_tuple,
emit_bc_build_list,
emit_bc_list_append,
emit_bc_build_map,
emit_bc_store_map,
emit_bc_map_add,
mp_emit_bc_label_assign,
mp_emit_bc_import_name,
mp_emit_bc_import_from,
mp_emit_bc_import_star,
mp_emit_bc_load_const_tok,
mp_emit_bc_load_const_small_int,
mp_emit_bc_load_const_str,
mp_emit_bc_load_const_obj,
mp_emit_bc_load_null,
mp_emit_bc_load_attr,
mp_emit_bc_load_method,
mp_emit_bc_load_build_class,
mp_emit_bc_load_subscr,
mp_emit_bc_store_attr,
mp_emit_bc_store_subscr,
mp_emit_bc_delete_attr,
mp_emit_bc_delete_subscr,
mp_emit_bc_dup_top,
mp_emit_bc_dup_top_two,
mp_emit_bc_pop_top,
mp_emit_bc_rot_two,
mp_emit_bc_rot_three,
mp_emit_bc_jump,
mp_emit_bc_pop_jump_if,
mp_emit_bc_jump_if_or_pop,
mp_emit_bc_unwind_jump,
mp_emit_bc_unwind_jump,
mp_emit_bc_setup_with,
mp_emit_bc_with_cleanup,
mp_emit_bc_setup_except,
mp_emit_bc_setup_finally,
mp_emit_bc_end_finally,
mp_emit_bc_get_iter,
mp_emit_bc_for_iter,
mp_emit_bc_for_iter_end,
mp_emit_bc_pop_block,
mp_emit_bc_pop_except,
mp_emit_bc_unary_op,
mp_emit_bc_binary_op,
mp_emit_bc_build_tuple,
mp_emit_bc_build_list,
mp_emit_bc_list_append,
mp_emit_bc_build_map,
mp_emit_bc_store_map,
mp_emit_bc_map_add,
#if MICROPY_PY_BUILTINS_SET
emit_bc_build_set,
emit_bc_set_add,
mp_emit_bc_build_set,
mp_emit_bc_set_add,
#endif
#if MICROPY_PY_BUILTINS_SLICE
emit_bc_build_slice,
mp_emit_bc_build_slice,
#endif
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,
emit_bc_start_except_handler,
emit_bc_end_except_handler,
mp_emit_bc_unpack_sequence,
mp_emit_bc_unpack_ex,
mp_emit_bc_make_function,
mp_emit_bc_make_closure,
mp_emit_bc_call_function,
mp_emit_bc_call_method,
mp_emit_bc_return_value,
mp_emit_bc_raise_varargs,
mp_emit_bc_yield_value,
mp_emit_bc_yield_from,
mp_emit_bc_start_except_handler,
mp_emit_bc_end_except_handler,
};
#else
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
mp_emit_bc_load_fast,
mp_emit_bc_load_deref,
mp_emit_bc_load_name,
mp_emit_bc_load_global,
};
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
mp_emit_bc_store_fast,
mp_emit_bc_store_deref,
mp_emit_bc_store_name,
mp_emit_bc_store_global,
};
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
mp_emit_bc_delete_fast,
mp_emit_bc_delete_deref,
mp_emit_bc_delete_name,
mp_emit_bc_delete_global,
};
#endif
#endif // !MICROPY_EMIT_CPYTHON

Loading…
Cancel
Save