Skip to content

Commit 26b5754

Browse files
committed
py/emit: Combine build tuple/list/map emit funcs into one.
Reduces code size by: bare-arm: -24 minimal x86: -192 unix x64: -288 unix nanbox: -184 stm32: -72 cc3200: -16 esp8266: -148 esp32: -32
1 parent e686c94 commit 26b5754

4 files changed

Lines changed: 44 additions & 58 deletions

File tree

py/compile.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t
273273
}
274274
total += n;
275275
}
276-
EMIT_ARG(build_tuple, total);
276+
EMIT_ARG(build, total, MP_EMIT_BUILD_TUPLE);
277277
}
278278

279279
STATIC void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
@@ -652,12 +652,12 @@ STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn)
652652
// in MicroPython we put the default positional parameters into a tuple using the bytecode
653653
// we need to do this here before we start building the map for the default keywords
654654
if (comp->num_default_params > 0) {
655-
EMIT_ARG(build_tuple, comp->num_default_params);
655+
EMIT_ARG(build, comp->num_default_params, MP_EMIT_BUILD_TUPLE);
656656
} else {
657657
EMIT(load_null); // sentinel indicating empty default positional args
658658
}
659659
// first default dict param, so make the map
660-
EMIT_ARG(build_map, 0);
660+
EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
661661
}
662662

663663
// compile value then key, then store it to the dict
@@ -693,7 +693,7 @@ STATIC void compile_funcdef_lambdef(compiler_t *comp, scope_t *scope, mp_parse_n
693693
// in MicroPython we put the default positional parameters into a tuple using the bytecode
694694
// the default keywords args may have already made the tuple; if not, do it now
695695
if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
696-
EMIT_ARG(build_tuple, comp->num_default_params);
696+
EMIT_ARG(build, comp->num_default_params, MP_EMIT_BUILD_TUPLE);
697697
EMIT(load_null); // sentinel indicating empty default keyword args
698698
}
699699

@@ -1122,7 +1122,7 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
11221122

11231123
// build the "fromlist" tuple
11241124
EMIT_ARG(load_const_str, MP_QSTR__star_);
1125-
EMIT_ARG(build_tuple, 1);
1125+
EMIT_ARG(build, 1, MP_EMIT_BUILD_TUPLE);
11261126

11271127
// do the import
11281128
qstr dummy_q;
@@ -1141,7 +1141,7 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
11411141
qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
11421142
EMIT_ARG(load_const_str, id2);
11431143
}
1144-
EMIT_ARG(build_tuple, n);
1144+
EMIT_ARG(build, n, MP_EMIT_BUILD_TUPLE);
11451145

11461146
// do the import
11471147
qstr dummy_q;
@@ -2397,7 +2397,7 @@ STATIC void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
23972397
STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
23982398
if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
23992399
// empty list
2400-
EMIT_ARG(build_list, 0);
2400+
EMIT_ARG(build, 0, MP_EMIT_BUILD_LIST);
24012401
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
24022402
mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
24032403
if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
@@ -2406,12 +2406,12 @@ STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns)
24062406
// list of one item, with trailing comma
24072407
assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
24082408
compile_node(comp, pns2->nodes[0]);
2409-
EMIT_ARG(build_list, 1);
2409+
EMIT_ARG(build, 1, MP_EMIT_BUILD_LIST);
24102410
} else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
24112411
// list of many items
24122412
compile_node(comp, pns2->nodes[0]);
24132413
compile_generic_all_nodes(comp, pns3);
2414-
EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
2414+
EMIT_ARG(build, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3), MP_EMIT_BUILD_LIST);
24152415
} else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
24162416
// list comprehension
24172417
compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
@@ -2424,25 +2424,25 @@ STATIC void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns)
24242424
list_with_2_items:
24252425
compile_node(comp, pns2->nodes[0]);
24262426
compile_node(comp, pns2->nodes[1]);
2427-
EMIT_ARG(build_list, 2);
2427+
EMIT_ARG(build, 2, MP_EMIT_BUILD_LIST);
24282428
}
24292429
} else {
24302430
// list with 1 item
24312431
compile_node(comp, pns->nodes[0]);
2432-
EMIT_ARG(build_list, 1);
2432+
EMIT_ARG(build, 1, MP_EMIT_BUILD_LIST);
24332433
}
24342434
}
24352435

24362436
STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
24372437
mp_parse_node_t pn = pns->nodes[0];
24382438
if (MP_PARSE_NODE_IS_NULL(pn)) {
24392439
// empty dict
2440-
EMIT_ARG(build_map, 0);
2440+
EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
24412441
} else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
24422442
pns = (mp_parse_node_struct_t*)pn;
24432443
if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
24442444
// dict with one element
2445-
EMIT_ARG(build_map, 1);
2445+
EMIT_ARG(build, 1, MP_EMIT_BUILD_MAP);
24462446
compile_node(comp, pn);
24472447
EMIT(store_map);
24482448
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
@@ -2459,7 +2459,7 @@ STATIC void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
24592459
bool is_dict;
24602460
if (!MICROPY_PY_BUILTINS_SET || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
24612461
// a dictionary
2462-
EMIT_ARG(build_map, 1 + n);
2462+
EMIT_ARG(build, 1 + n, MP_EMIT_BUILD_MAP);
24632463
compile_node(comp, pns->nodes[0]);
24642464
EMIT(store_map);
24652465
is_dict = true;
@@ -3040,9 +3040,9 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
30403040
}
30413041

30423042
if (scope->kind == SCOPE_LIST_COMP) {
3043-
EMIT_ARG(build_list, 0);
3043+
EMIT_ARG(build, 0, MP_EMIT_BUILD_LIST);
30443044
} else if (scope->kind == SCOPE_DICT_COMP) {
3045-
EMIT_ARG(build_map, 0);
3045+
EMIT_ARG(build, 0, MP_EMIT_BUILD_MAP);
30463046
#if MICROPY_PY_BUILTINS_SET
30473047
} else if (scope->kind == SCOPE_SET_COMP) {
30483048
EMIT_ARG(build_set, 0);

py/emit.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ typedef enum {
5959
#define MP_EMIT_IDOP_LOCAL_FAST (0)
6060
#define MP_EMIT_IDOP_LOCAL_DEREF (1)
6161

62+
// Kind for emit->build()
63+
#define MP_EMIT_BUILD_TUPLE (0)
64+
#define MP_EMIT_BUILD_LIST (1)
65+
#define MP_EMIT_BUILD_MAP (3)
66+
6267
// Kind for emit->yield()
6368
#define MP_EMIT_YIELD_VALUE (0)
6469
#define MP_EMIT_YIELD_FROM (1)
@@ -122,9 +127,7 @@ typedef struct _emit_method_table_t {
122127
void (*pop_except)(emit_t *emit);
123128
void (*unary_op)(emit_t *emit, mp_unary_op_t op);
124129
void (*binary_op)(emit_t *emit, mp_binary_op_t op);
125-
void (*build_tuple)(emit_t *emit, mp_uint_t n_args);
126-
void (*build_list)(emit_t *emit, mp_uint_t n_args);
127-
void (*build_map)(emit_t *emit, mp_uint_t n_args);
130+
void (*build)(emit_t *emit, mp_uint_t n_args, int kind);
128131
void (*store_map)(emit_t *emit);
129132
#if MICROPY_PY_BUILTINS_SET
130133
void (*build_set)(emit_t *emit, mp_uint_t n_args);
@@ -236,9 +239,7 @@ void mp_emit_bc_pop_block(emit_t *emit);
236239
void mp_emit_bc_pop_except(emit_t *emit);
237240
void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op);
238241
void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op);
239-
void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args);
240-
void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args);
241-
void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args);
242+
void mp_emit_bc_build(emit_t *emit, mp_uint_t n_args, int kind);
242243
void mp_emit_bc_store_map(emit_t *emit);
243244
#if MICROPY_PY_BUILTINS_SET
244245
void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args);

py/emitbc.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -816,19 +816,16 @@ void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
816816
}
817817
}
818818

819-
void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args) {
820-
emit_bc_pre(emit, 1 - n_args);
821-
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE, n_args);
822-
}
823-
824-
void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args) {
825-
emit_bc_pre(emit, 1 - n_args);
826-
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_LIST, n_args);
827-
}
828-
829-
void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args) {
830-
emit_bc_pre(emit, 1);
831-
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_MAP, n_args);
819+
void mp_emit_bc_build(emit_t *emit, mp_uint_t n_args, int kind) {
820+
MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_BC_BUILD_TUPLE);
821+
MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_BC_BUILD_LIST);
822+
MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_BC_BUILD_MAP);
823+
if (kind == MP_EMIT_BUILD_MAP) {
824+
emit_bc_pre(emit, 1);
825+
} else {
826+
emit_bc_pre(emit, 1 - n_args);
827+
}
828+
emit_write_bytecode_byte_uint(emit, MP_BC_BUILD_TUPLE + kind, n_args);
832829
}
833830

834831
void mp_emit_bc_store_map(emit_t *emit) {
@@ -1010,9 +1007,7 @@ const emit_method_table_t emit_bc_method_table = {
10101007
mp_emit_bc_pop_except,
10111008
mp_emit_bc_unary_op,
10121009
mp_emit_bc_binary_op,
1013-
mp_emit_bc_build_tuple,
1014-
mp_emit_bc_build_list,
1015-
mp_emit_bc_build_map,
1010+
mp_emit_bc_build,
10161011
mp_emit_bc_store_map,
10171012
#if MICROPY_PY_BUILTINS_SET
10181013
mp_emit_bc_build_set,

py/emitnative.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,26 +1923,18 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
19231923
}
19241924
}
19251925

1926-
STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
1926+
STATIC void emit_native_build(emit_t *emit, mp_uint_t n_args, int kind) {
19271927
// for viper: call runtime, with types of args
19281928
// if wrapped in byte_array, or something, allocates memory and fills it
1929+
MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_F_BUILD_TUPLE);
1930+
MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_F_BUILD_LIST);
1931+
MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_F_BUILD_MAP);
19291932
emit_native_pre(emit);
1930-
emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
1931-
emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
1932-
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
1933-
}
1934-
1935-
STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
1936-
emit_native_pre(emit);
1937-
emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
1938-
emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
1939-
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
1940-
}
1941-
1942-
STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
1943-
emit_native_pre(emit);
1944-
emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
1945-
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1933+
if (kind == MP_EMIT_BUILD_TUPLE || kind == MP_EMIT_BUILD_LIST) {
1934+
emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
1935+
}
1936+
emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE + kind, n_args, REG_ARG_1);
1937+
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple/list/map
19461938
}
19471939

19481940
STATIC void emit_native_store_map(emit_t *emit) {
@@ -2255,9 +2247,7 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
22552247
emit_native_pop_except,
22562248
emit_native_unary_op,
22572249
emit_native_binary_op,
2258-
emit_native_build_tuple,
2259-
emit_native_build_list,
2260-
emit_native_build_map,
2250+
emit_native_build,
22612251
emit_native_store_map,
22622252
#if MICROPY_PY_BUILTINS_SET
22632253
emit_native_build_set,

0 commit comments

Comments
 (0)