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
crypto-aes
Damien George 5 years ago
parent 8a513da5a5
commit d97906ca9a

@ -1024,14 +1024,14 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
if (MP_PARSE_NODE_IS_NULL(pn)) {
// empty name (eg, from . import x)
*q_base = MP_QSTR_;
EMIT_ARG(import_name, MP_QSTR_); // import the empty string
EMIT_ARG(import, MP_QSTR_, MP_EMIT_IMPORT_NAME); // import the empty string
} else if (MP_PARSE_NODE_IS_ID(pn)) {
// just a simple name
qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
if (!is_as) {
*q_base = q_full;
}
EMIT_ARG(import_name, q_full);
EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
} else {
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_name)); // should be
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) {
}
qstr q_full = qstr_from_strn(q_ptr, len);
mp_local_free(q_ptr);
EMIT_ARG(import_name, q_full);
EMIT_ARG(import, q_full, MP_EMIT_IMPORT_NAME);
if (is_as) {
for (int i = 1; i < n; i++) {
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) {
// do the import
qstr dummy_q;
do_import_name(comp, pn_import_source, &dummy_q);
EMIT(import_star);
EMIT_ARG(import, MP_QSTR_NULL, MP_EMIT_IMPORT_STAR);
} else {
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) {
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
EMIT_ARG(import_from, id2);
EMIT_ARG(import, id2, MP_EMIT_IMPORT_FROM);
if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
compile_store_id(comp, id2);
} else {

@ -63,6 +63,11 @@ typedef enum {
#define MP_EMIT_IDOP_GLOBAL_NAME (0)
#define MP_EMIT_IDOP_GLOBAL_GLOBAL (1)
// Kind for emit->import()
#define MP_EMIT_IMPORT_NAME (0)
#define MP_EMIT_IMPORT_FROM (1)
#define MP_EMIT_IMPORT_STAR (2)
// Kind for emit->subscr()
#define MP_EMIT_SUBSCR_LOAD (0)
#define MP_EMIT_SUBSCR_STORE (1)
@ -102,9 +107,7 @@ typedef struct _emit_method_table_t {
mp_emit_method_table_id_ops_t delete_id;
void (*label_assign)(emit_t *emit, mp_uint_t l);
void (*import_name)(emit_t *emit, qstr qst);
void (*import_from)(emit_t *emit, qstr qst);
void (*import_star)(emit_t *emit);
void (*import)(emit_t *emit, qstr qst, int kind);
void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok);
void (*load_const_small_int)(emit_t *emit, mp_int_t arg);
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
void mp_emit_bc_delete_global(emit_t *emit, qstr qst, int kind);
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_import(emit_t *emit, qstr qst, int kind);
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);

@ -504,19 +504,19 @@ void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
}
}
void mp_emit_bc_import_name(emit_t *emit, qstr qst) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME, qst);
}
void mp_emit_bc_import_from(emit_t *emit, qstr qst) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_FROM, qst);
}
void mp_emit_bc_import_star(emit_t *emit) {
emit_bc_pre(emit, -1);
emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
void mp_emit_bc_import(emit_t *emit, qstr qst, int kind) {
MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_NAME == MP_BC_IMPORT_NAME);
MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_FROM == MP_BC_IMPORT_FROM);
if (kind == MP_EMIT_IMPORT_FROM) {
emit_bc_pre(emit, 1);
} else {
emit_bc_pre(emit, -1);
}
if (kind == MP_EMIT_IMPORT_STAR) {
emit_write_bytecode_byte(emit, MP_BC_IMPORT_STAR);
} else {
emit_write_bytecode_byte_qstr(emit, MP_BC_IMPORT_NAME + kind, qst);
}
}
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 = {
},
mp_emit_bc_label_assign,
mp_emit_bc_import_name,
mp_emit_bc_import_from,
mp_emit_bc_import_star,
mp_emit_bc_import,
mp_emit_bc_load_const_tok,
mp_emit_bc_load_const_small_int,
mp_emit_bc_load_const_str,

@ -837,6 +837,16 @@ STATIC void emit_native_import_star(emit_t *emit) {
emit_post(emit);
}
STATIC void emit_native_import(emit_t *emit, qstr qst, int kind) {
if (kind == MP_EMIT_IMPORT_NAME) {
emit_native_import_name(emit, qst);
} else if (kind == MP_EMIT_IMPORT_FROM) {
emit_native_import_from(emit, qst);
} else {
emit_native_import_star(emit);
}
}
STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
DEBUG_printf("load_const_tok(tok=%u)\n", tok);
emit_native_pre(emit);
@ -2223,9 +2233,7 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
},
emit_native_label_assign,
emit_native_import_name,
emit_native_import_from,
emit_native_import_star,
emit_native_import,
emit_native_load_const_tok,
emit_native_load_const_small_int,
emit_native_load_const_str,

Loading…
Cancel
Save