Skip to content

Commit d298013

Browse files
committed
py/emit: Combine name and global into one func for load/store/delete.
Reduces code size by: bare-arm: -56 minimal x86: -300 unix x64: -576 unix nanbox: -300 stm32: -164 cc3200: -56 esp8266: -236 esp32: -76
1 parent 26b5754 commit d298013

5 files changed

Lines changed: 74 additions & 94 deletions

File tree

py/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ typedef enum {
6666
#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
6767
#define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
6868
#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.local(comp->emit, qst, local_num, MP_EMIT_IDOP_LOCAL_FAST))
69-
#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
69+
#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst, MP_EMIT_IDOP_GLOBAL_GLOBAL))
7070

7171
#else
7272

7373
// if we only have the bytecode emitter enabled then we can do a direct call to the functions
7474
#define EMIT(fun) (mp_emit_bc_##fun(comp->emit))
7575
#define EMIT_ARG(fun, ...) (mp_emit_bc_##fun(comp->emit, __VA_ARGS__))
7676
#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_local(comp->emit, qst, local_num, MP_EMIT_IDOP_LOCAL_FAST))
77-
#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
77+
#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst, MP_EMIT_IDOP_GLOBAL_GLOBAL))
7878

7979
#endif
8080

py/emit.h

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

62+
// Kind for emit_id_ops->global()
63+
#define MP_EMIT_IDOP_GLOBAL_NAME (0)
64+
#define MP_EMIT_IDOP_GLOBAL_GLOBAL (1)
65+
6266
// Kind for emit->build()
6367
#define MP_EMIT_BUILD_TUPLE (0)
6468
#define MP_EMIT_BUILD_LIST (1)
@@ -72,8 +76,7 @@ typedef struct _emit_t emit_t;
7276

7377
typedef struct _mp_emit_method_table_id_ops_t {
7478
void (*local)(emit_t *emit, qstr qst, mp_uint_t local_num, int kind);
75-
void (*name)(emit_t *emit, qstr qst);
76-
void (*global)(emit_t *emit, qstr qst);
79+
void (*global)(emit_t *emit, qstr qst, int kind);
7780
} mp_emit_method_table_id_ops_t;
7881

7982
typedef struct _emit_method_table_t {
@@ -190,14 +193,11 @@ void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta);
190193
void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t line);
191194

192195
void mp_emit_bc_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind);
193-
void mp_emit_bc_load_name(emit_t *emit, qstr qst);
194-
void mp_emit_bc_load_global(emit_t *emit, qstr qst);
196+
void mp_emit_bc_load_global(emit_t *emit, qstr qst, int kind);
195197
void mp_emit_bc_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind);
196-
void mp_emit_bc_store_name(emit_t *emit, qstr qst);
197-
void mp_emit_bc_store_global(emit_t *emit, qstr qst);
198+
void mp_emit_bc_store_global(emit_t *emit, qstr qst, int kind);
198199
void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind);
199-
void mp_emit_bc_delete_name(emit_t *emit, qstr qst);
200-
void mp_emit_bc_delete_global(emit_t *emit, qstr qst);
200+
void mp_emit_bc_delete_global(emit_t *emit, qstr qst, int kind);
201201

202202
void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l);
203203
void mp_emit_bc_import_name(emit_t *emit, qstr qst);

py/emitbc.c

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -568,19 +568,12 @@ void mp_emit_bc_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind
568568
}
569569
}
570570

571-
void mp_emit_bc_load_name(emit_t *emit, qstr qst) {
571+
void mp_emit_bc_load_global(emit_t *emit, qstr qst, int kind) {
572+
MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_LOAD_NAME);
573+
MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_LOAD_GLOBAL);
572574
(void)qst;
573575
emit_bc_pre(emit, 1);
574-
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME, qst);
575-
if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
576-
emit_write_bytecode_byte(emit, 0);
577-
}
578-
}
579-
580-
void mp_emit_bc_load_global(emit_t *emit, qstr qst) {
581-
(void)qst;
582-
emit_bc_pre(emit, 1);
583-
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_GLOBAL, qst);
576+
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_NAME + kind, qst);
584577
if (MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) {
585578
emit_write_bytecode_byte(emit, 0);
586579
}
@@ -621,14 +614,11 @@ void mp_emit_bc_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kin
621614
}
622615
}
623616

624-
void mp_emit_bc_store_name(emit_t *emit, qstr qst) {
617+
void mp_emit_bc_store_global(emit_t *emit, qstr qst, int kind) {
618+
MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_STORE_NAME);
619+
MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_STORE_GLOBAL);
625620
emit_bc_pre(emit, -1);
626-
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
627-
}
628-
629-
void mp_emit_bc_store_global(emit_t *emit, qstr qst) {
630-
emit_bc_pre(emit, -1);
631-
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_GLOBAL, qst);
621+
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME + kind, qst);
632622
}
633623

634624
void mp_emit_bc_store_attr(emit_t *emit, qstr qst) {
@@ -651,14 +641,11 @@ void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int ki
651641
emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST + kind, local_num);
652642
}
653643

654-
void mp_emit_bc_delete_name(emit_t *emit, qstr qst) {
655-
emit_bc_pre(emit, 0);
656-
emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME, qst);
657-
}
658-
659-
void mp_emit_bc_delete_global(emit_t *emit, qstr qst) {
644+
void mp_emit_bc_delete_global(emit_t *emit, qstr qst, int kind) {
645+
MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_DELETE_NAME);
646+
MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_DELETE_GLOBAL);
660647
emit_bc_pre(emit, 0);
661-
emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_GLOBAL, qst);
648+
emit_write_bytecode_byte_qstr(emit, MP_BC_DELETE_NAME + kind, qst);
662649
}
663650

664651
void mp_emit_bc_delete_attr(emit_t *emit, qstr qst) {
@@ -954,17 +941,14 @@ const emit_method_table_t emit_bc_method_table = {
954941

955942
{
956943
mp_emit_bc_load_local,
957-
mp_emit_bc_load_name,
958944
mp_emit_bc_load_global,
959945
},
960946
{
961947
mp_emit_bc_store_local,
962-
mp_emit_bc_store_name,
963948
mp_emit_bc_store_global,
964949
},
965950
{
966951
mp_emit_bc_delete_local,
967-
mp_emit_bc_delete_name,
968952
mp_emit_bc_delete_global,
969953
},
970954

@@ -1032,19 +1016,16 @@ const emit_method_table_t emit_bc_method_table = {
10321016
#else
10331017
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
10341018
mp_emit_bc_load_local,
1035-
mp_emit_bc_load_name,
10361019
mp_emit_bc_load_global,
10371020
};
10381021

10391022
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
10401023
mp_emit_bc_store_local,
1041-
mp_emit_bc_store_name,
10421024
mp_emit_bc_store_global,
10431025
};
10441026

10451027
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
10461028
mp_emit_bc_delete_local,
1047-
mp_emit_bc_delete_name,
10481029
mp_emit_bc_delete_global,
10491030
};
10501031
#endif

py/emitcommon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ void mp_emit_common_id_op(emit_t *emit, const mp_emit_method_table_id_ops_t *emi
6363

6464
// call the emit backend with the correct code
6565
if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
66-
emit_method_table->name(emit, qst);
66+
emit_method_table->global(emit, qst, MP_EMIT_IDOP_GLOBAL_NAME);
6767
} else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
68-
emit_method_table->global(emit, qst);
68+
emit_method_table->global(emit, qst, MP_EMIT_IDOP_GLOBAL_GLOBAL);
6969
} else if (id->kind == ID_INFO_KIND_LOCAL) {
7070
emit_method_table->local(emit, qst, id->local_num, MP_EMIT_IDOP_LOCAL_FAST);
7171
} else {

py/emitnative.c

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -946,33 +946,39 @@ STATIC void emit_native_load_local(emit_t *emit, qstr qst, mp_uint_t local_num,
946946
}
947947
}
948948

949-
STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
950-
DEBUG_printf("load_name(%s)\n", qstr_str(qst));
949+
STATIC void emit_native_load_global(emit_t *emit, qstr qst, int kind) {
950+
MP_STATIC_ASSERT(MP_F_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_F_LOAD_NAME);
951+
MP_STATIC_ASSERT(MP_F_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_F_LOAD_GLOBAL);
951952
emit_native_pre(emit);
952-
emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
953-
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
954-
}
955-
956-
STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
957-
DEBUG_printf("load_global(%s)\n", qstr_str(qst));
958-
emit_native_pre(emit);
959-
// check for builtin casting operators
960-
if (emit->do_viper_types && qst == MP_QSTR_int) {
961-
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
962-
} else if (emit->do_viper_types && qst == MP_QSTR_uint) {
963-
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
964-
} else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
965-
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
966-
} else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
967-
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
968-
} else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
969-
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
970-
} else if (emit->do_viper_types && qst == MP_QSTR_ptr32) {
971-
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR32);
953+
if (kind == MP_EMIT_IDOP_GLOBAL_NAME) {
954+
DEBUG_printf("load_name(%s)\n", qstr_str(qst));
972955
} else {
973-
emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
974-
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
956+
DEBUG_printf("load_global(%s)\n", qstr_str(qst));
957+
if (emit->do_viper_types) {
958+
// check for builtin casting operators
959+
if (qst == MP_QSTR_int) {
960+
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
961+
return;
962+
} else if (qst == MP_QSTR_uint) {
963+
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
964+
return;
965+
} else if (qst == MP_QSTR_ptr) {
966+
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
967+
return;
968+
} else if (qst == MP_QSTR_ptr8) {
969+
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
970+
return;
971+
} else if (qst == MP_QSTR_ptr16) {
972+
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
973+
return;
974+
} else if (qst == MP_QSTR_ptr32) {
975+
emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR32);
976+
return;
977+
}
978+
}
975979
}
980+
emit_call_with_imm_arg(emit, MP_F_LOAD_NAME + kind, qst, REG_ARG_1);
981+
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
976982
}
977983

978984
STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
@@ -1194,25 +1200,25 @@ STATIC void emit_native_store_local(emit_t *emit, qstr qst, mp_uint_t local_num,
11941200
}
11951201
}
11961202

1197-
STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
1198-
// mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
1199-
vtype_kind_t vtype;
1200-
emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1201-
assert(vtype == VTYPE_PYOBJ);
1202-
emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
1203-
emit_post(emit);
1204-
}
1205-
1206-
STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
1207-
vtype_kind_t vtype = peek_vtype(emit, 0);
1208-
if (vtype == VTYPE_PYOBJ) {
1203+
STATIC void emit_native_store_global(emit_t *emit, qstr qst, int kind) {
1204+
MP_STATIC_ASSERT(MP_F_STORE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_F_STORE_NAME);
1205+
MP_STATIC_ASSERT(MP_F_STORE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_F_STORE_GLOBAL);
1206+
if (kind == MP_EMIT_IDOP_GLOBAL_NAME) {
1207+
// mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
1208+
vtype_kind_t vtype;
12091209
emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1210+
assert(vtype == VTYPE_PYOBJ);
12101211
} else {
1211-
emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1212-
emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype, REG_ARG_2); // arg2 = type
1213-
ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
1212+
vtype_kind_t vtype = peek_vtype(emit, 0);
1213+
if (vtype == VTYPE_PYOBJ) {
1214+
emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1215+
} else {
1216+
emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1217+
emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype, REG_ARG_2); // arg2 = type
1218+
ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
1219+
}
12141220
}
1215-
emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
1221+
emit_call_with_imm_arg(emit, MP_F_STORE_NAME + kind, qst, REG_ARG_1); // arg1 = name
12161222
emit_post(emit);
12171223
}
12181224

@@ -1417,15 +1423,11 @@ STATIC void emit_native_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num
14171423
}
14181424
}
14191425

1420-
STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1421-
emit_native_pre(emit);
1422-
emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1423-
emit_post(emit);
1424-
}
1425-
1426-
STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1426+
STATIC void emit_native_delete_global(emit_t *emit, qstr qst, int kind) {
1427+
MP_STATIC_ASSERT(MP_F_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_F_DELETE_NAME);
1428+
MP_STATIC_ASSERT(MP_F_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_F_DELETE_GLOBAL);
14271429
emit_native_pre(emit);
1428-
emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1430+
emit_call_with_imm_arg(emit, MP_F_DELETE_NAME + kind, qst, REG_ARG_1);
14291431
emit_post(emit);
14301432
}
14311433

@@ -2194,17 +2196,14 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
21942196

21952197
{
21962198
emit_native_load_local,
2197-
emit_native_load_name,
21982199
emit_native_load_global,
21992200
},
22002201
{
22012202
emit_native_store_local,
2202-
emit_native_store_name,
22032203
emit_native_store_global,
22042204
},
22052205
{
22062206
emit_native_delete_local,
2207-
emit_native_delete_name,
22082207
emit_native_delete_global,
22092208
},
22102209

0 commit comments

Comments
 (0)