Commit a5624bf3 authored by Damien George's avatar Damien George
Browse files

py: Combine 3 comprehension emit functions (list/dict/set) into 1.

The 3 kinds of comprehensions are similar enough that merging their emit
functions reduces code size.  Decreases in code size in bytes are:
bare-arm:24, minimal:96, unix(NDEBUG,x86-64):328, stmhal:80, esp8266:76.
parent 4b3f1d71
......@@ -2869,17 +2869,11 @@ STATIC void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_struct_t *pn
if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
// no more nested if/for; compile inner expression
compile_node(comp, pn_inner_expr);
if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
EMIT_ARG(list_append, for_depth + 2);
} else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
EMIT_ARG(map_add, for_depth + 2);
#if MICROPY_PY_BUILTINS_SET
} else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
EMIT_ARG(set_add, for_depth + 2);
#endif
} else {
if (comp->scope_cur->kind == SCOPE_GEN_EXPR) {
EMIT(yield_value);
EMIT(pop_top);
} else {
EMIT_ARG(store_comp, comp->scope_cur->kind, for_depth + 2);
}
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
// if condition
......
......@@ -119,17 +119,15 @@ typedef struct _emit_method_table_t {
void (*binary_op)(emit_t *emit, mp_binary_op_t op);
void (*build_tuple)(emit_t *emit, mp_uint_t n_args);
void (*build_list)(emit_t *emit, mp_uint_t n_args);
void (*list_append)(emit_t *emit, mp_uint_t list_stack_index);
void (*build_map)(emit_t *emit, mp_uint_t n_args);
void (*store_map)(emit_t *emit);
void (*map_add)(emit_t *emit, mp_uint_t map_stack_index);
#if MICROPY_PY_BUILTINS_SET
void (*build_set)(emit_t *emit, mp_uint_t n_args);
void (*set_add)(emit_t *emit, mp_uint_t set_stack_index);
#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);
void (*make_function)(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults);
......@@ -240,17 +238,15 @@ 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_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);
void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults);
......
......@@ -837,11 +837,6 @@ void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
}
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);
}
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);
......@@ -852,21 +847,11 @@ void mp_emit_bc_store_map(emit_t *emit) {
emit_write_bytecode_byte(emit, MP_BC_STORE_MAP);
}
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
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);
}
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
......@@ -876,6 +861,24 @@ 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 collection_stack_index) {
int n;
byte opcode;
if (kind == SCOPE_LIST_COMP) {
n = -1;
opcode = MP_BC_LIST_APPEND;
} else if (MICROPY_PY_BUILTINS_SET && kind == SCOPE_SET_COMP) {
n = -1;
opcode = MP_BC_SET_ADD;
} else {
// scope == SCOPE_DICT_COMP
n = -2;
opcode = MP_BC_MAP_ADD;
}
emit_bc_pre(emit, n);
emit_write_bytecode_byte_uint(emit, opcode, collection_stack_index);
}
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);
......@@ -1028,17 +1031,15 @@ const emit_method_table_t emit_bc_method_table = {
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
mp_emit_bc_build_set,
mp_emit_bc_set_add,
#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,
mp_emit_bc_make_function,
......
......@@ -2344,17 +2344,6 @@ STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
}
STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
// only used in list comprehension
vtype_kind_t vtype_list, vtype_item;
emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
assert(vtype_list == VTYPE_PYOBJ);
assert(vtype_item == VTYPE_PYOBJ);
emit_call(emit, MP_F_LIST_APPEND);
emit_post(emit);
}
STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
emit_native_pre(emit);
emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
......@@ -2371,18 +2360,6 @@ STATIC void emit_native_store_map(emit_t *emit) {
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
}
STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
// only used in list comprehension
vtype_kind_t vtype_map, vtype_key, vtype_value;
emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
assert(vtype_map == VTYPE_PYOBJ);
assert(vtype_key == VTYPE_PYOBJ);
assert(vtype_value == VTYPE_PYOBJ);
emit_call(emit, MP_F_STORE_MAP);
emit_post(emit);
}
#if MICROPY_PY_BUILTINS_SET
STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
emit_native_pre(emit);
......@@ -2390,17 +2367,6 @@ STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
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
}
STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
// only used in set comprehension
vtype_kind_t vtype_set, vtype_item;
emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
assert(vtype_set == VTYPE_PYOBJ);
assert(vtype_item == VTYPE_PYOBJ);
emit_call(emit, MP_F_STORE_SET);
emit_post(emit);
}
#endif
#if MICROPY_PY_BUILTINS_SLICE
......@@ -2426,6 +2392,35 @@ STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
}
#endif
STATIC void emit_native_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_index) {
mp_fun_kind_t f;
if (kind == SCOPE_LIST_COMP) {
vtype_kind_t vtype_item;
emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
assert(vtype_item == VTYPE_PYOBJ);
f = MP_F_LIST_APPEND;
#if MICROPY_PY_BUILTINS_SET
} else if (kind == SCOPE_SET_COMP) {
vtype_kind_t vtype_item;
emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
assert(vtype_item == VTYPE_PYOBJ);
f = MP_F_STORE_SET;
#endif
} else {
// SCOPE_DICT_COMP
vtype_kind_t vtype_key, vtype_value;
emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
assert(vtype_key == VTYPE_PYOBJ);
assert(vtype_value == VTYPE_PYOBJ);
f = MP_F_STORE_MAP;
}
vtype_kind_t vtype_collection;
emit_access_stack(emit, collection_index, &vtype_collection, REG_ARG_1);
assert(vtype_collection == VTYPE_PYOBJ);
emit_call(emit, f);
emit_post(emit);
}
STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
DEBUG_printf("unpack_sequence %d\n", n_args);
vtype_kind_t vtype_base;
......@@ -2674,17 +2669,15 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
emit_native_binary_op,
emit_native_build_tuple,
emit_native_build_list,
emit_native_list_append,
emit_native_build_map,
emit_native_store_map,
emit_native_map_add,
#if MICROPY_PY_BUILTINS_SET
emit_native_build_set,
emit_native_set_add,
#endif
#if MICROPY_PY_BUILTINS_SLICE
emit_native_build_slice,
#endif
emit_native_store_comp,
emit_native_unpack_sequence,
emit_native_unpack_ex,
emit_native_make_function,
......
Markdown is supported
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