Skip to content

Commit ef12a4b

Browse files
committed
py: Refactor how native emitter code is compiled with a file per arch.
Instead of emitnative.c having configuration code for each supported architecture, and then compiling this file multiple times with different macros defined, this patch adds a file per architecture with the necessary code to configure the native emitter. These files then #include the emitnative.c file. This simplifies emitnative.c (which is already very large), and simplifies the build system because emitnative.c no longer needs special handling for compilation and qstr extraction.
1 parent 5ad27d4 commit ef12a4b

9 files changed

Lines changed: 132 additions & 124 deletions

File tree

py/asmthumb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#ifndef MICROPY_INCLUDED_PY_ASMTHUMB_H
2727
#define MICROPY_INCLUDED_PY_ASMTHUMB_H
2828

29+
#include <assert.h>
2930
#include "py/misc.h"
3031
#include "py/asmbase.h"
3132

py/emitnarm.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ARM specific stuff
2+
3+
#include "py/mpconfig.h"
4+
5+
#if MICROPY_EMIT_ARM
6+
7+
// This is defined so that the assembler exports generic assembler API macros
8+
#define GENERIC_ASM_API (1)
9+
#include "py/asmarm.h"
10+
11+
#define N_ARM (1)
12+
#define EXPORT_FUN(name) emit_native_arm_##name
13+
#include "py/emitnative.c"
14+
15+
#endif

py/emitnative.c

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,7 @@
5757
#endif
5858

5959
// wrapper around everything in this file
60-
#if (MICROPY_EMIT_X64 && N_X64) \
61-
|| (MICROPY_EMIT_X86 && N_X86) \
62-
|| (MICROPY_EMIT_THUMB && N_THUMB) \
63-
|| (MICROPY_EMIT_ARM && N_ARM) \
64-
|| (MICROPY_EMIT_XTENSA && N_XTENSA) \
65-
66-
// this is defined so that the assembler exports generic assembler API macros
67-
#define GENERIC_ASM_API (1)
60+
#if N_X64 || N_X86 || N_THUMB || N_ARM || N_XTENSA
6861

6962
// define additional generic helper macros
7063
#define ASM_MOV_LOCAL_IMM_VIA(as, local_num, imm, reg_temp) \
@@ -73,94 +66,6 @@
7366
ASM_MOV_LOCAL_REG((as), (local_num), (reg_temp)); \
7467
} while (false)
7568

76-
#if N_X64
77-
78-
// x64 specific stuff
79-
#include "py/asmx64.h"
80-
#define EXPORT_FUN(name) emit_native_x64_##name
81-
82-
#elif N_X86
83-
84-
// x86 specific stuff
85-
86-
STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
87-
[MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
88-
[MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
89-
[MP_F_LOAD_NAME] = 1,
90-
[MP_F_LOAD_GLOBAL] = 1,
91-
[MP_F_LOAD_BUILD_CLASS] = 0,
92-
[MP_F_LOAD_ATTR] = 2,
93-
[MP_F_LOAD_METHOD] = 3,
94-
[MP_F_LOAD_SUPER_METHOD] = 2,
95-
[MP_F_STORE_NAME] = 2,
96-
[MP_F_STORE_GLOBAL] = 2,
97-
[MP_F_STORE_ATTR] = 3,
98-
[MP_F_OBJ_SUBSCR] = 3,
99-
[MP_F_OBJ_IS_TRUE] = 1,
100-
[MP_F_UNARY_OP] = 2,
101-
[MP_F_BINARY_OP] = 3,
102-
[MP_F_BUILD_TUPLE] = 2,
103-
[MP_F_BUILD_LIST] = 2,
104-
[MP_F_LIST_APPEND] = 2,
105-
[MP_F_BUILD_MAP] = 1,
106-
[MP_F_STORE_MAP] = 3,
107-
#if MICROPY_PY_BUILTINS_SET
108-
[MP_F_BUILD_SET] = 2,
109-
[MP_F_STORE_SET] = 2,
110-
#endif
111-
[MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
112-
[MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
113-
[MP_F_CALL_METHOD_N_KW] = 3,
114-
[MP_F_CALL_METHOD_N_KW_VAR] = 3,
115-
[MP_F_NATIVE_GETITER] = 2,
116-
[MP_F_NATIVE_ITERNEXT] = 1,
117-
[MP_F_NLR_PUSH] = 1,
118-
[MP_F_NLR_POP] = 0,
119-
[MP_F_NATIVE_RAISE] = 1,
120-
[MP_F_IMPORT_NAME] = 3,
121-
[MP_F_IMPORT_FROM] = 2,
122-
[MP_F_IMPORT_ALL] = 1,
123-
#if MICROPY_PY_BUILTINS_SLICE
124-
[MP_F_NEW_SLICE] = 3,
125-
#endif
126-
[MP_F_UNPACK_SEQUENCE] = 3,
127-
[MP_F_UNPACK_EX] = 3,
128-
[MP_F_DELETE_NAME] = 1,
129-
[MP_F_DELETE_GLOBAL] = 1,
130-
[MP_F_NEW_CELL] = 1,
131-
[MP_F_MAKE_CLOSURE_FROM_RAW_CODE] = 3,
132-
[MP_F_SETUP_CODE_STATE] = 5,
133-
[MP_F_SMALL_INT_FLOOR_DIVIDE] = 2,
134-
[MP_F_SMALL_INT_MODULO] = 2,
135-
};
136-
137-
#include "py/asmx86.h"
138-
#define EXPORT_FUN(name) emit_native_x86_##name
139-
140-
#elif N_THUMB
141-
142-
// thumb specific stuff
143-
#include "py/asmthumb.h"
144-
#define EXPORT_FUN(name) emit_native_thumb_##name
145-
146-
#elif N_ARM
147-
148-
// ARM specific stuff
149-
#include "py/asmarm.h"
150-
#define EXPORT_FUN(name) emit_native_arm_##name
151-
152-
#elif N_XTENSA
153-
154-
// Xtensa specific stuff
155-
#include "py/asmxtensa.h"
156-
#define EXPORT_FUN(name) emit_native_xtensa_##name
157-
158-
#else
159-
160-
#error unknown native emitter
161-
162-
#endif
163-
16469
#define EMIT_NATIVE_VIPER_TYPE_ERROR(emit, ...) do { \
16570
*emit->error_slot = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, __VA_ARGS__); \
16671
} while (0)

py/emitnthumb.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// thumb specific stuff
2+
3+
#include "py/mpconfig.h"
4+
5+
#if MICROPY_EMIT_THUMB
6+
7+
// this is defined so that the assembler exports generic assembler API macros
8+
#define GENERIC_ASM_API (1)
9+
#include "py/asmthumb.h"
10+
11+
#define N_THUMB (1)
12+
#define EXPORT_FUN(name) emit_native_thumb_##name
13+
#include "py/emitnative.c"
14+
15+
#endif

py/emitnx64.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// x64 specific stuff
2+
3+
#include "py/mpconfig.h"
4+
5+
#if MICROPY_EMIT_X64
6+
7+
// This is defined so that the assembler exports generic assembler API macros
8+
#define GENERIC_ASM_API (1)
9+
#include "py/asmx64.h"
10+
11+
#define N_X64 (1)
12+
#define EXPORT_FUN(name) emit_native_x64_##name
13+
#include "py/emitnative.c"
14+
15+
#endif

py/emitnx86.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// x86 specific stuff
2+
3+
#include "py/mpconfig.h"
4+
5+
#if MICROPY_EMIT_X86
6+
7+
// This is defined so that the assembler exports generic assembler API macros
8+
#define GENERIC_ASM_API (1)
9+
#include "py/asmx86.h"
10+
11+
// x86 needs a table to know how many args a given function has
12+
STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
13+
[MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
14+
[MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
15+
[MP_F_LOAD_NAME] = 1,
16+
[MP_F_LOAD_GLOBAL] = 1,
17+
[MP_F_LOAD_BUILD_CLASS] = 0,
18+
[MP_F_LOAD_ATTR] = 2,
19+
[MP_F_LOAD_METHOD] = 3,
20+
[MP_F_LOAD_SUPER_METHOD] = 2,
21+
[MP_F_STORE_NAME] = 2,
22+
[MP_F_STORE_GLOBAL] = 2,
23+
[MP_F_STORE_ATTR] = 3,
24+
[MP_F_OBJ_SUBSCR] = 3,
25+
[MP_F_OBJ_IS_TRUE] = 1,
26+
[MP_F_UNARY_OP] = 2,
27+
[MP_F_BINARY_OP] = 3,
28+
[MP_F_BUILD_TUPLE] = 2,
29+
[MP_F_BUILD_LIST] = 2,
30+
[MP_F_LIST_APPEND] = 2,
31+
[MP_F_BUILD_MAP] = 1,
32+
[MP_F_STORE_MAP] = 3,
33+
#if MICROPY_PY_BUILTINS_SET
34+
[MP_F_BUILD_SET] = 2,
35+
[MP_F_STORE_SET] = 2,
36+
#endif
37+
[MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
38+
[MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
39+
[MP_F_CALL_METHOD_N_KW] = 3,
40+
[MP_F_CALL_METHOD_N_KW_VAR] = 3,
41+
[MP_F_NATIVE_GETITER] = 2,
42+
[MP_F_NATIVE_ITERNEXT] = 1,
43+
[MP_F_NLR_PUSH] = 1,
44+
[MP_F_NLR_POP] = 0,
45+
[MP_F_NATIVE_RAISE] = 1,
46+
[MP_F_IMPORT_NAME] = 3,
47+
[MP_F_IMPORT_FROM] = 2,
48+
[MP_F_IMPORT_ALL] = 1,
49+
#if MICROPY_PY_BUILTINS_SLICE
50+
[MP_F_NEW_SLICE] = 3,
51+
#endif
52+
[MP_F_UNPACK_SEQUENCE] = 3,
53+
[MP_F_UNPACK_EX] = 3,
54+
[MP_F_DELETE_NAME] = 1,
55+
[MP_F_DELETE_GLOBAL] = 1,
56+
[MP_F_NEW_CELL] = 1,
57+
[MP_F_MAKE_CLOSURE_FROM_RAW_CODE] = 3,
58+
[MP_F_SETUP_CODE_STATE] = 5,
59+
[MP_F_SMALL_INT_FLOOR_DIVIDE] = 2,
60+
[MP_F_SMALL_INT_MODULO] = 2,
61+
};
62+
63+
#define N_X86 (1)
64+
#define EXPORT_FUN(name) emit_native_x86_##name
65+
#include "py/emitnative.c"
66+
67+
#endif

py/emitnxtensa.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Xtensa specific stuff
2+
3+
#include "py/mpconfig.h"
4+
5+
#if MICROPY_EMIT_XTENSA
6+
7+
// this is defined so that the assembler exports generic assembler API macros
8+
#define GENERIC_ASM_API (1)
9+
#include "py/asmxtensa.h"
10+
11+
#define N_XTENSA (1)
12+
#define EXPORT_FUN(name) emit_native_xtensa_##name
13+
#include "py/emitnative.c"
14+
15+
#endif

py/mkrules.mk

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ vpath %.c . $(TOP)
4646
$(BUILD)/%.o: %.c
4747
$(call compile_c)
4848

49-
# List all native flags since the current build system doesn't have
50-
# the MicroPython configuration available. However, these flags are
51-
# needed to extract all qstrings
52-
QSTR_GEN_EXTRA_CFLAGS += -DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM -DN_XTENSA
49+
QSTR_GEN_EXTRA_CFLAGS += -DNO_QSTR
5350
QSTR_GEN_EXTRA_CFLAGS += -I$(BUILD)/tmp
5451

5552
vpath %.c . $(TOP)

py/py.mk

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ PY_O += $(BUILD)/$(BUILD)/frozen_mpy.o
267267
endif
268268

269269
# Sources that may contain qstrings
270-
SRC_QSTR_IGNORE = py/nlr% py/emitnx86% py/emitnx64% py/emitnthumb% py/emitnarm% py/emitnxtensa%
271-
SRC_QSTR = $(SRC_MOD) $(filter-out $(SRC_QSTR_IGNORE),$(PY_CORE_O_BASENAME:.o=.c)) py/emitnative.c $(PY_EXTMOD_O_BASENAME:.o=.c)
270+
SRC_QSTR_IGNORE = py/nlr%
271+
SRC_QSTR = $(SRC_MOD) $(filter-out $(SRC_QSTR_IGNORE),$(PY_CORE_O_BASENAME:.o=.c)) $(PY_EXTMOD_O_BASENAME:.o=.c)
272272

273273
# Anything that depends on FORCE will be considered out-of-date
274274
FORCE:
@@ -295,28 +295,6 @@ $(HEADER_BUILD)/qstrdefs.generated.h: $(PY_QSTR_DEFS) $(QSTR_DEFS) $(QSTR_DEFS_C
295295
# that the function preludes are of a minimal and predictable form.
296296
$(PY_BUILD)/nlr%.o: CFLAGS += -Os
297297

298-
# emitters
299-
300-
$(PY_BUILD)/emitnx64.o: CFLAGS += -DN_X64
301-
$(PY_BUILD)/emitnx64.o: py/emitnative.c
302-
$(call compile_c)
303-
304-
$(PY_BUILD)/emitnx86.o: CFLAGS += -DN_X86
305-
$(PY_BUILD)/emitnx86.o: py/emitnative.c
306-
$(call compile_c)
307-
308-
$(PY_BUILD)/emitnthumb.o: CFLAGS += -DN_THUMB
309-
$(PY_BUILD)/emitnthumb.o: py/emitnative.c
310-
$(call compile_c)
311-
312-
$(PY_BUILD)/emitnarm.o: CFLAGS += -DN_ARM
313-
$(PY_BUILD)/emitnarm.o: py/emitnative.c
314-
$(call compile_c)
315-
316-
$(PY_BUILD)/emitnxtensa.o: CFLAGS += -DN_XTENSA
317-
$(PY_BUILD)/emitnxtensa.o: py/emitnative.c
318-
$(call compile_c)
319-
320298
# optimising gc for speed; 5ms down to 4ms on pybv2
321299
$(PY_BUILD)/gc.o: CFLAGS += $(CSUPEROPT)
322300

0 commit comments

Comments
 (0)