Skip to content

Commit d97906c

Browse files
committed
py/emit: Combine import from/name/star into one emit function.
Change in code size is: bare-arm: +4 minimal x86: -88 unix x64: -456 unix nanbox: -88 stm32: -44 cc3200: +0 esp8266: -104 esp32: +8
1 parent 8a513da commit d97906c

4 files changed

Lines changed: 37 additions & 30 deletions

File tree

py/compile.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,14 +1024,14 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
10241024
if (MP_PARSE_NODE_IS_NULL(pn)) {
10251025
// empty name (eg, from . import x)
10261026
*q_base = MP_QSTR_;
1027-
EMIT_ARG(import_name, MP_QSTR_); // import the empty string
1027+
EMIT_ARG(import, MP_QSTR_, MP_EMIT_IMPORT_NAME); // import the empty string
10281028
} else if (MP_PARSE_NODE_IS_ID(pn)) {
10291029
// just a simple name
10301030
qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
10311031
if (!is_as) {
10321032
*q_base = q_full;
10331033
}
1034-
EMIT_ARG(import_name, q_full);
1034+
EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
10351035
} else {
10361036
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
10371037
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
@@ -1058,7 +1058,7 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
10581058
}
10591059
qstr q_full = qstr_from_strn(q_ptr, len);
10601060
mp_local_free(q_ptr);
1061-
EMIT_ARG(import_name, q_full);
1061+
EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
10621062
if (is_as) {
10631063
for (int i = 1; i < n; i++) {
10641064
EMIT_ARG(attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), MP_EMIT_ATTR_LOAD);
@@ -1127,7 +1127,7 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
11271127
// do the import
11281128
qstr dummy_q;
11291129
do_import_name(comp, pn_import_source, &dummy_q);
1130-
EMIT(import_star);
1130+
EMIT_ARG(import, MP_QSTR_NULL, MP_EMIT_IMPORT_STAR);
11311131

11321132
} else {
11331133
EMIT_ARG(load_const_small_int, import_level);
@@ -1150,7 +1150,7 @@ STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
11501150
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
11511151
mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
11521152
qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
1153-
EMIT_ARG(import_from, id2);
1153+
EMIT_ARG(import, id2, MP_EMIT_IMPORT_FROM);
11541154
if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
11551155
compile_store_id(comp, id2);
11561156
} else {

py/emit.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ typedef enum {
6363
#define MP_EMIT_IDOP_GLOBAL_NAME (0)
6464
#define MP_EMIT_IDOP_GLOBAL_GLOBAL (1)
6565

66+
// Kind for emit->import()
67+
#define MP_EMIT_IMPORT_NAME (0)
68+
#define MP_EMIT_IMPORT_FROM (1)
69+
#define MP_EMIT_IMPORT_STAR (2)
70+
6671
// Kind for emit->subscr()
6772
#define MP_EMIT_SUBSCR_LOAD (0)
6873
#define MP_EMIT_SUBSCR_STORE (1)
@@ -102,9 +107,7 @@ typedef struct _emit_method_table_t {
102107
mp_emit_method_table_id_ops_t delete_id;
103108

104109
void (*label_assign)(emit_t *emit, mp_uint_t l);
105-
void (*import_name)(emit_t *emit, qstr qst);
106-
void (*import_from)(emit_t *emit, qstr qst);
107-
void (*import_star)(emit_t *emit);
110+
void (*import)(emit_t *emit, qstr qst, int kind);
108111
void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok);
109112
void (*load_const_small_int)(emit_t *emit, mp_int_t arg);
110113
void (*load_const_str)(emit_t *emit, qstr qst);
@@ -205,9 +208,7 @@ void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int ki
205208
void mp_emit_bc_delete_global(emit_t *emit, qstr qst, int kind);
206209

207210
void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l);
208-
void mp_emit_bc_import_name(emit_t *emit, qstr qst);
209-
void mp_emit_bc_import_from(emit_t *emit, qstr qst);
210-
void mp_emit_bc_import_star(emit_t *emit);
211+
void mp_emit_bc_import(emit_t *emit, qstr qst, int kind);
211212
void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok);
212213
void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg);
213214
void mp_emit_bc_load_const_str(emit_t *emit, qstr qst);

py/emitbc.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -504,19 +504,19 @@ void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
504504
}
505505
}
506506

507-
void mp_emit_bc_import_name(emit_t *emit, qstr qst) {
508-
emit_bc_pre(emit, -1);
509-
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
510-
}
511-
512-
void mp_emit_bc_import_from(emit_t *emit, qstr qst) {
513-
emit_bc_pre(emit, 1);
514-
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
515-
}
516-
517-
void mp_emit_bc_import_star(emit_t *emit) {
518-
emit_bc_pre(emit, -1);
519-
emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
507+
void mp_emit_bc_import(emit_t *emit, qstr qst, int kind) {
508+
MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_NAME == MP_BC_IMPORT_NAME);
509+
MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_FROM == MP_BC_IMPORT_FROM);
510+
if (kind == MP_EMIT_IMPORT_FROM) {
511+
emit_bc_pre(emit, 1);
512+
} else {
513+
emit_bc_pre(emit, -1);
514+
}
515+
if (kind == MP_EMIT_IMPORT_STAR) {
516+
emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
517+
} else {
518+
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME + kind, qst);
519+
}
520520
}
521521

522522
void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
@@ -946,9 +946,7 @@ const emit_method_table_t emit_bc_method_table = {
946946
},
947947

948948
mp_emit_bc_label_assign,
949-
mp_emit_bc_import_name,
950-
mp_emit_bc_import_from,
951-
mp_emit_bc_import_star,
949+
mp_emit_bc_import,
952950
mp_emit_bc_load_const_tok,
953951
mp_emit_bc_load_const_small_int,
954952
mp_emit_bc_load_const_str,

py/emitnative.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,16 @@ STATIC void emit_native_import_star(emit_t *emit) {
837837
emit_post(emit);
838838
}
839839

840+
STATIC void emit_native_import(emit_t *emit, qstr qst, int kind) {
841+
if (kind == MP_EMIT_IMPORT_NAME) {
842+
emit_native_import_name(emit, qst);
843+
} else if (kind == MP_EMIT_IMPORT_FROM) {
844+
emit_native_import_from(emit, qst);
845+
} else {
846+
emit_native_import_star(emit);
847+
}
848+
}
849+
840850
STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
841851
DEBUG_printf("load_const_tok(tok=%u)\n", tok);
842852
emit_native_pre(emit);
@@ -2223,9 +2233,7 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
22232233
},
22242234

22252235
emit_native_label_assign,
2226-
emit_native_import_name,
2227-
emit_native_import_from,
2228-
emit_native_import_star,
2236+
emit_native_import,
22292237
emit_native_load_const_tok,
22302238
emit_native_load_const_small_int,
22312239
emit_native_load_const_str,

0 commit comments

Comments
 (0)