Commit 4112590a authored by Damien George's avatar Damien George
Browse files

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.
parent a210c774
...@@ -49,6 +49,11 @@ typedef enum { ...@@ -49,6 +49,11 @@ typedef enum {
PN_const_object, // special node for a constant, generic Python object PN_const_object, // special node for a constant, generic Python object
} pn_kind_t; } 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(fun) (comp->emit_method_table->fun(comp->emit))
#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__)) #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)) #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 { ...@@ -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(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__)) #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 // elements in this struct are ordered to make it compact
typedef struct _compiler_t { typedef struct _compiler_t {
qstr source_file; qstr source_file;
...@@ -83,7 +98,9 @@ typedef struct _compiler_t { ...@@ -83,7 +98,9 @@ typedef struct _compiler_t {
scope_t *scope_cur; scope_t *scope_cur;
emit_t *emit; // current emitter emit_t *emit; // current emitter
#if NEED_METHOD_TABLE
const emit_method_table_t *emit_method_table; // current emit method table const emit_method_table_t *emit_method_table; // current emit method table
#endif
#if MICROPY_EMIT_INLINE_THUMB #if MICROPY_EMIT_INLINE_THUMB
emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm 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) { ...@@ -406,7 +423,11 @@ STATIC void compile_load_id(compiler_t *comp, qstr qst) {
if (comp->pass == MP_PASS_SCOPE) { if (comp->pass == MP_PASS_SCOPE) {
mp_emit_common_get_id_for_load(comp->scope_cur, qst); mp_emit_common_get_id_for_load(comp->scope_cur, qst);
} else { } else {
#if NEED_METHOD_TABLE
mp_emit_common_id_op(comp->emit, &comp->emit_method_table->load_id, comp->scope_cur, qst); 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) { ...@@ -414,7 +435,11 @@ STATIC void compile_store_id(compiler_t *comp, qstr qst) {
if (comp->pass == MP_PASS_SCOPE) { if (comp->pass == MP_PASS_SCOPE) {
mp_emit_common_get_id_for_modification(comp->scope_cur, qst); mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
} else { } else {
#if NEED_METHOD_TABLE
mp_emit_common_id_op(comp->emit, &comp->emit_method_table->store_id, comp->scope_cur, qst); 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) { ...@@ -422,7 +447,11 @@ STATIC void compile_delete_id(compiler_t *comp, qstr qst) {
if (comp->pass == MP_PASS_SCOPE) { if (comp->pass == MP_PASS_SCOPE) {
mp_emit_common_get_id_for_modification(comp->scope_cur, qst); mp_emit_common_get_id_for_modification(comp->scope_cur, qst);
} else { } else {
#if NEED_METHOD_TABLE
mp_emit_common_id_op(comp->emit, &comp->emit_method_table->delete_id, comp->scope_cur, qst); 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) { ...@@ -2963,7 +2992,7 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
} }
} else { } else {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn; 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)]; compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
if (f == NULL) { if (f == NULL) {
#if MICROPY_DEBUG_PRINTERS #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 ...@@ -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; comp->emit_method_table = &emit_cpython_method_table;
#else #else
comp->emit = emit_bc; comp->emit = emit_bc;
#if MICROPY_EMIT_NATIVE
comp->emit_method_table = &emit_bc_method_table; comp->emit_method_table = &emit_bc_method_table;
#endif #endif
#endif
#if MICROPY_EMIT_INLINE_THUMB #if MICROPY_EMIT_INLINE_THUMB
comp->emit_inline_asm = NULL; comp->emit_inline_asm = NULL;
comp->emit_inline_asm_method_table = 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 ...@@ -3765,7 +3796,9 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is
default: default:
comp->emit = emit_bc; comp->emit = emit_bc;
#if MICROPY_EMIT_NATIVE
comp->emit_method_table = &emit_bc_method_table; comp->emit_method_table = &emit_bc_method_table;
#endif
break; break;
} }
#endif // !MICROPY_EMIT_CPYTHON #endif // !MICROPY_EMIT_CPYTHON
......
...@@ -72,7 +72,7 @@ typedef struct _emit_method_table_t { ...@@ -72,7 +72,7 @@ typedef struct _emit_method_table_t {
void (*end_pass)(emit_t *emit); void (*end_pass)(emit_t *emit);
bool (*last_emit_was_return_value)(emit_t *emit); bool (*last_emit_was_return_value)(emit_t *emit);
void (*adjust_stack_size)(emit_t *emit, mp_int_t delta); 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 load_id;
mp_emit_method_table_id_ops_t store_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; ...@@ -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_thumb_method_table;
extern const emit_method_table_t emit_native_arm_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_cpython_new(void);
emit_t *emit_bc_new(void); emit_t *emit_bc_new(void);
emit_t *emit_native_x64_new(mp_uint_t max_num_labels); emit_t *emit_native_x64_new(mp_uint_t max_num_labels);
...@@ -183,6 +187,91 @@ void emit_native_x86_free(emit_t *emit); ...@@ -183,6 +187,91 @@ void emit_native_x86_free(emit_t *emit);
void emit_native_thumb_free(emit_t *emit); void emit_native_thumb_free(emit_t *emit);
void emit_native_arm_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_t emit_inline_asm_t;
typedef struct _emit_inline_asm_method_table_t { typedef struct _emit_inline_asm_method_table_t {
......
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment