Skip to content

Commit 0a25fff

Browse files
committed
py/emit: Combine fast and deref into one function for load/store/delete.
Reduces code size by: bare-arm: -16 minimal x86: -208 unix x64: -408 unix nanbox: -248 stm32: -12 cc3200: -24 esp8266: -96 esp32: -44
1 parent 400273a commit 0a25fff

5 files changed

Lines changed: 61 additions & 68 deletions

File tree

py/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ typedef enum {
6565
// we need a method table to do the lookup for the emitter functions
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__))
68-
#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.fast(comp->emit, qst, local_num))
68+
#define EMIT_LOAD_FAST(qst, local_num) (comp->emit_method_table->load_id.local(comp->emit, qst, local_num, MP_EMIT_IDOP_LOCAL_FAST))
6969
#define EMIT_LOAD_GLOBAL(qst) (comp->emit_method_table->load_id.global(comp->emit, qst))
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__))
76-
#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_fast(comp->emit, qst, local_num))
76+
#define EMIT_LOAD_FAST(qst, local_num) (mp_emit_bc_load_local(comp->emit, qst, local_num, MP_EMIT_IDOP_LOCAL_FAST))
7777
#define EMIT_LOAD_GLOBAL(qst) (mp_emit_bc_load_global(comp->emit, qst))
7878

7979
#endif

py/emit.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ typedef enum {
5555
#define MP_EMIT_NATIVE_TYPE_RETURN (1)
5656
#define MP_EMIT_NATIVE_TYPE_ARG (2)
5757

58+
// Kind for emit_id_ops->local()
59+
#define MP_EMIT_IDOP_LOCAL_FAST (0)
60+
#define MP_EMIT_IDOP_LOCAL_DEREF (1)
61+
5862
typedef struct _emit_t emit_t;
5963

6064
typedef struct _mp_emit_method_table_id_ops_t {
61-
void (*fast)(emit_t *emit, qstr qst, mp_uint_t local_num);
62-
void (*deref)(emit_t *emit, qstr qst, mp_uint_t local_num);
65+
void (*local)(emit_t *emit, qstr qst, mp_uint_t local_num, int kind);
6366
void (*name)(emit_t *emit, qstr qst);
6467
void (*global)(emit_t *emit, qstr qst);
6568
} mp_emit_method_table_id_ops_t;
@@ -180,16 +183,13 @@ bool mp_emit_bc_last_emit_was_return_value(emit_t *emit);
180183
void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta);
181184
void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t line);
182185

183-
void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
184-
void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num);
186+
void mp_emit_bc_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind);
185187
void mp_emit_bc_load_name(emit_t *emit, qstr qst);
186188
void mp_emit_bc_load_global(emit_t *emit, qstr qst);
187-
void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
188-
void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num);
189+
void mp_emit_bc_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind);
189190
void mp_emit_bc_store_name(emit_t *emit, qstr qst);
190191
void mp_emit_bc_store_global(emit_t *emit, qstr qst);
191-
void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
192-
void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num);
192+
void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind);
193193
void mp_emit_bc_delete_name(emit_t *emit, qstr qst);
194194
void mp_emit_bc_delete_global(emit_t *emit, qstr qst);
195195

py/emitbc.c

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -556,22 +556,18 @@ void mp_emit_bc_load_null(emit_t *emit) {
556556
emit_write_bytecode_byte(emit, MP_BC_LOAD_NULL);
557557
}
558558

559-
void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
559+
void mp_emit_bc_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
560+
MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_LOAD_FAST_N);
561+
MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_LOAD_DEREF);
560562
(void)qst;
561563
emit_bc_pre(emit, 1);
562-
if (local_num <= 15) {
564+
if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
563565
emit_write_bytecode_byte(emit, MP_BC_LOAD_FAST_MULTI + local_num);
564566
} else {
565-
emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N, local_num);
567+
emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_FAST_N + kind, local_num);
566568
}
567569
}
568570

569-
void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
570-
(void)qst;
571-
emit_bc_pre(emit, 1);
572-
emit_write_bytecode_byte_uint(emit, MP_BC_LOAD_DEREF, local_num);
573-
}
574-
575571
void mp_emit_bc_load_name(emit_t *emit, qstr qst) {
576572
(void)qst;
577573
emit_bc_pre(emit, 1);
@@ -613,22 +609,18 @@ void mp_emit_bc_load_subscr(emit_t *emit) {
613609
emit_write_bytecode_byte(emit, MP_BC_LOAD_SUBSCR);
614610
}
615611

616-
void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
612+
void mp_emit_bc_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
613+
MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_STORE_FAST_N);
614+
MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_STORE_DEREF);
617615
(void)qst;
618616
emit_bc_pre(emit, -1);
619-
if (local_num <= 15) {
617+
if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
620618
emit_write_bytecode_byte(emit, MP_BC_STORE_FAST_MULTI + local_num);
621619
} else {
622-
emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N, local_num);
620+
emit_write_bytecode_byte_uint(emit, MP_BC_STORE_FAST_N + kind, local_num);
623621
}
624622
}
625623

626-
void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
627-
(void)qst;
628-
emit_bc_pre(emit, -1);
629-
emit_write_bytecode_byte_uint(emit, MP_BC_STORE_DEREF, local_num);
630-
}
631-
632624
void mp_emit_bc_store_name(emit_t *emit, qstr qst) {
633625
emit_bc_pre(emit, -1);
634626
emit_write_bytecode_byte_qstr(emit, MP_BC_STORE_NAME, qst);
@@ -652,14 +644,11 @@ void mp_emit_bc_store_subscr(emit_t *emit) {
652644
emit_write_bytecode_byte(emit, MP_BC_STORE_SUBSCR);
653645
}
654646

655-
void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
656-
(void)qst;
657-
emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST, local_num);
658-
}
659-
660-
void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
647+
void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
648+
MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_DELETE_FAST);
649+
MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_DELETE_DEREF);
661650
(void)qst;
662-
emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_DEREF, local_num);
651+
emit_write_bytecode_byte_uint(emit, MP_BC_DELETE_FAST + kind, local_num);
663652
}
664653

665654
void mp_emit_bc_delete_name(emit_t *emit, qstr qst) {
@@ -972,20 +961,17 @@ const emit_method_table_t emit_bc_method_table = {
972961
mp_emit_bc_set_source_line,
973962

974963
{
975-
mp_emit_bc_load_fast,
976-
mp_emit_bc_load_deref,
964+
mp_emit_bc_load_local,
977965
mp_emit_bc_load_name,
978966
mp_emit_bc_load_global,
979967
},
980968
{
981-
mp_emit_bc_store_fast,
982-
mp_emit_bc_store_deref,
969+
mp_emit_bc_store_local,
983970
mp_emit_bc_store_name,
984971
mp_emit_bc_store_global,
985972
},
986973
{
987-
mp_emit_bc_delete_fast,
988-
mp_emit_bc_delete_deref,
974+
mp_emit_bc_delete_local,
989975
mp_emit_bc_delete_name,
990976
mp_emit_bc_delete_global,
991977
},
@@ -1056,22 +1042,19 @@ const emit_method_table_t emit_bc_method_table = {
10561042
};
10571043
#else
10581044
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
1059-
mp_emit_bc_load_fast,
1060-
mp_emit_bc_load_deref,
1045+
mp_emit_bc_load_local,
10611046
mp_emit_bc_load_name,
10621047
mp_emit_bc_load_global,
10631048
};
10641049

10651050
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
1066-
mp_emit_bc_store_fast,
1067-
mp_emit_bc_store_deref,
1051+
mp_emit_bc_store_local,
10681052
mp_emit_bc_store_name,
10691053
mp_emit_bc_store_global,
10701054
};
10711055

10721056
const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
1073-
mp_emit_bc_delete_fast,
1074-
mp_emit_bc_delete_deref,
1057+
mp_emit_bc_delete_local,
10751058
mp_emit_bc_delete_name,
10761059
mp_emit_bc_delete_global,
10771060
};

py/emitcommon.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ void mp_emit_common_id_op(emit_t *emit, const mp_emit_method_table_id_ops_t *emi
6767
} else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
6868
emit_method_table->global(emit, qst);
6969
} else if (id->kind == ID_INFO_KIND_LOCAL) {
70-
emit_method_table->fast(emit, qst, id->local_num);
70+
emit_method_table->local(emit, qst, id->local_num, MP_EMIT_IDOP_LOCAL_FAST);
7171
} else {
7272
assert(id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE);
73-
emit_method_table->deref(emit, qst, id->local_num);
73+
emit_method_table->local(emit, qst, id->local_num, MP_EMIT_IDOP_LOCAL_DEREF);
7474
}
7575
}
7676

py/emitnative.c

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,14 @@ STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num)
938938
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
939939
}
940940

941+
STATIC void emit_native_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
942+
if (kind == MP_EMIT_IDOP_LOCAL_FAST) {
943+
emit_native_load_fast(emit, qst, local_num);
944+
} else {
945+
emit_native_load_deref(emit, qst, local_num);
946+
}
947+
}
948+
941949
STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
942950
DEBUG_printf("load_name(%s)\n", qstr_str(qst));
943951
emit_native_pre(emit);
@@ -1178,6 +1186,14 @@ STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num)
11781186
emit_post(emit);
11791187
}
11801188

1189+
STATIC void emit_native_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
1190+
if (kind == MP_EMIT_IDOP_LOCAL_FAST) {
1191+
emit_native_store_fast(emit, qst, local_num);
1192+
} else {
1193+
emit_native_store_deref(emit, qst, local_num);
1194+
}
1195+
}
1196+
11811197
STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
11821198
// mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
11831199
vtype_kind_t vtype;
@@ -1389,19 +1405,16 @@ STATIC void emit_native_store_subscr(emit_t *emit) {
13891405
}
13901406
}
13911407

1392-
STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1393-
// TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1394-
// to mark deleted vars but then every var would need to be checked on
1395-
// each access. Very inefficient, so just set value to None to enable GC.
1396-
emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1397-
emit_native_store_fast(emit, qst, local_num);
1398-
}
1399-
1400-
STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
1401-
// TODO implement me!
1402-
(void)emit;
1403-
(void)qst;
1404-
(void)local_num;
1408+
STATIC void emit_native_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
1409+
if (kind == MP_EMIT_IDOP_LOCAL_FAST) {
1410+
// TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1411+
// to mark deleted vars but then every var would need to be checked on
1412+
// each access. Very inefficient, so just set value to None to enable GC.
1413+
emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1414+
emit_native_store_fast(emit, qst, local_num);
1415+
} else {
1416+
// TODO implement me!
1417+
}
14051418
}
14061419

14071420
STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
@@ -2192,20 +2205,17 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
21922205
emit_native_set_source_line,
21932206

21942207
{
2195-
emit_native_load_fast,
2196-
emit_native_load_deref,
2208+
emit_native_load_local,
21972209
emit_native_load_name,
21982210
emit_native_load_global,
21992211
},
22002212
{
2201-
emit_native_store_fast,
2202-
emit_native_store_deref,
2213+
emit_native_store_local,
22032214
emit_native_store_name,
22042215
emit_native_store_global,
22052216
},
22062217
{
2207-
emit_native_delete_fast,
2208-
emit_native_delete_deref,
2218+
emit_native_delete_local,
22092219
emit_native_delete_name,
22102220
emit_native_delete_global,
22112221
},

0 commit comments

Comments
 (0)