py/emit: Merge build set/slice into existing build emit function.

Reduces code size by:

   bare-arm:    +0
minimal x86:    +0
   unix x64:  -368
unix nanbox:  -248
      stm32:  -128
     cc3200:   -48
    esp8266:  -184
      esp32:   -40
crypto-aes
Damien George 5 years ago
parent d97906ca9a
commit 436e0d4c54

@ -2499,7 +2499,7 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
#if MICROPY_PY_BUILTINS_SET
// if it's a set, build it
if (!is_dict) {
EMIT_ARG(build_set, 1 + n);
EMIT_ARG(build, 1 + n, MP_EMIT_BUILD_SET);
}
#endif
} else {
@ -2522,7 +2522,7 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
set_with_one_element:
#if MICROPY_PY_BUILTINS_SET
compile_node(comp, pn);
EMIT_ARG(build_set, 1);
EMIT_ARG(build, 1, MP_EMIT_BUILD_SET);
#else
assert(0);
#endif
@ -2551,7 +2551,7 @@ STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t
if (MP_PARSE_NODE_IS_NULL(pn)) {
// [?:]
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
EMIT_ARG(build_slice, 2);
EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
} else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
pns = (mp_parse_node_struct_t*)pn;
if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
@ -2559,11 +2559,11 @@ STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t
pn = pns->nodes[0];
if (MP_PARSE_NODE_IS_NULL(pn)) {
// [?::]
EMIT_ARG(build_slice, 2);
EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
} else {
// [?::x]
compile_node(comp, pn);
EMIT_ARG(build_slice, 3);
EMIT_ARG(build, 3, MP_EMIT_BUILD_SLICE);
}
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
compile_node(comp, pns->nodes[0]);
@ -2572,21 +2572,21 @@ STATIC void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t
assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
// [?:x:]
EMIT_ARG(build_slice, 2);
EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
} else {
// [?:x:x]
compile_node(comp, pns->nodes[0]);
EMIT_ARG(build_slice, 3);
EMIT_ARG(build, 3, MP_EMIT_BUILD_SLICE);
}
} else {
// [?:x]
compile_node(comp, pn);
EMIT_ARG(build_slice, 2);
EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
}
} else {
// [?:x]
compile_node(comp, pn);
EMIT_ARG(build_slice, 2);
EMIT_ARG(build, 2, MP_EMIT_BUILD_SLICE);
}
}
@ -3045,7 +3045,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
#if MICROPY_PY_BUILTINS_SET
} else if (scope->kind == SCOPE_SET_COMP) {
EMIT_ARG(build_set, 0);
EMIT_ARG(build, 0, MP_EMIT_BUILD_SET);
#endif
}

@ -82,6 +82,8 @@ typedef enum {
#define MP_EMIT_BUILD_TUPLE (0)
#define MP_EMIT_BUILD_LIST (1)
#define MP_EMIT_BUILD_MAP (3)
#define MP_EMIT_BUILD_SET (6)
#define MP_EMIT_BUILD_SLICE (8)
// Kind for emit->yield()
#define MP_EMIT_YIELD_VALUE (0)
@ -140,12 +142,6 @@ typedef struct _emit_method_table_t {
void (*binary_op)(emit_t *emit, mp_binary_op_t op);
void (*build)(emit_t *emit, mp_uint_t n_args, int kind);
void (*store_map)(emit_t *emit);
#if MICROPY_PY_BUILTINS_SET
void (*build_set)(emit_t *emit, mp_uint_t n_args);
#endif
#if MICROPY_PY_BUILTINS_SLICE
void (*build_slice)(emit_t *emit, mp_uint_t n_args);
#endif
void (*store_comp)(emit_t *emit, scope_kind_t kind, mp_uint_t set_stack_index);
void (*unpack_sequence)(emit_t *emit, mp_uint_t n_args);
void (*unpack_ex)(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right);
@ -241,12 +237,6 @@ 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(emit_t *emit, mp_uint_t n_args, int kind);
void mp_emit_bc_store_map(emit_t *emit);
#if MICROPY_PY_BUILTINS_SET
void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args);
#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_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t list_stack_index);
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);

@ -800,6 +800,8 @@ void mp_emit_bc_build(emit_t *emit, mp_uint_t n_args, int kind) {
MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_BC_BUILD_TUPLE);
MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_BC_BUILD_LIST);
MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_BC_BUILD_MAP);
MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SET == MP_BC_BUILD_SET);
MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SLICE == MP_BC_BUILD_SLICE);
if (kind == MP_EMIT_BUILD_MAP) {
emit_bc_pre(emit, 1);
} else {
@ -813,20 +815,6 @@ void mp_emit_bc_store_map(emit_t *emit) {
emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
}
#if MICROPY_PY_BUILTINS_SET
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);
}
#endif
#if MICROPY_PY_BUILTINS_SLICE
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
void mp_emit_bc_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_stack_index) {
int t;
int n;
@ -979,12 +967,6 @@ const emit_method_table_t emit_bc_method_table = {
mp_emit_bc_binary_op,
mp_emit_bc_build,
mp_emit_bc_store_map,
#if MICROPY_PY_BUILTINS_SET
mp_emit_bc_build_set,
#endif
#if MICROPY_PY_BUILTINS_SLICE
mp_emit_bc_build_slice,
#endif
mp_emit_bc_store_comp,
mp_emit_bc_unpack_sequence,
mp_emit_bc_unpack_ex,

@ -1950,18 +1950,29 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
}
}
#if MICROPY_PY_BUILTINS_SLICE
STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args);
#endif
STATIC void emit_native_build(emit_t *emit, mp_uint_t n_args, int kind) {
// for viper: call runtime, with types of args
// if wrapped in byte_array, or something, allocates memory and fills it
MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_F_BUILD_TUPLE);
MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_F_BUILD_LIST);
MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_F_BUILD_MAP);
MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_SET == MP_F_BUILD_SET);
#if MICROPY_PY_BUILTINS_SLICE
if (kind == MP_EMIT_BUILD_SLICE) {
emit_native_build_slice(emit, n_args);
return;
}
#endif
emit_native_pre(emit);
if (kind == MP_EMIT_BUILD_TUPLE || kind == MP_EMIT_BUILD_LIST) {
if (kind == MP_EMIT_BUILD_TUPLE || kind == MP_EMIT_BUILD_LIST || kind == MP_EMIT_BUILD_SET) {
emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
}
emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE + kind, n_args, REG_ARG_1);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple/list/map
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple/list/map/set
}
STATIC void emit_native_store_map(emit_t *emit) {
@ -1974,15 +1985,6 @@ STATIC void emit_native_store_map(emit_t *emit) {
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
}
#if MICROPY_PY_BUILTINS_SET
STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
emit_native_pre(emit);
emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
}
#endif
#if MICROPY_PY_BUILTINS_SLICE
STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
DEBUG_printf("build_slice %d\n", n_args);
@ -2266,12 +2268,6 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
emit_native_binary_op,
emit_native_build,
emit_native_store_map,
#if MICROPY_PY_BUILTINS_SET
emit_native_build_set,
#endif
#if MICROPY_PY_BUILTINS_SLICE
emit_native_build_slice,
#endif
emit_native_store_comp,
emit_native_unpack_sequence,
emit_native_unpack_ex,

@ -146,8 +146,8 @@ void *const mp_fun_table[MP_F_NUMBER_OF] = {
mp_obj_new_dict,
mp_obj_dict_store,
#if MICROPY_PY_BUILTINS_SET
mp_obj_new_set,
mp_obj_set_store,
mp_obj_new_set,
#endif
mp_make_function_from_raw_code,
mp_native_call_function_n_kw,

@ -164,8 +164,8 @@ typedef enum {
MP_F_BUILD_MAP,
MP_F_STORE_MAP,
#if MICROPY_PY_BUILTINS_SET
MP_F_BUILD_SET,
MP_F_STORE_SET,
MP_F_BUILD_SET,
#endif
MP_F_MAKE_FUNCTION_FROM_RAW_CODE,
MP_F_NATIVE_CALL_FUNCTION_N_KW,

Loading…
Cancel
Save