8000 py: Integrate Xtensa assembler into native emitter. · Felipeasg/micropython@8e5aced · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e5aced

Browse files
committed
py: Integrate Xtensa assembler into native emitter.
The config option MICROPY_EMIT_XTENSA can now be enabled to target the Xtensa architecture with @micropython.native and @micropython.viper decorators.
1 parent fcac4b0 commit 8e5aced

File tree

6 files changed

+42
-4
lines changed

6 files changed

+42
-4
lines changed

py/compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ typedef enum {
7979
#define NATIVE_EMITTER(f) emit_native_thumb_##f
8080
#elif MICROPY_EMIT_ARM
8181
#define NATIVE_EMITTER(f) emit_native_arm_##f
82+
#elif MICROPY_EMIT_XTENSA
83+
#define NATIVE_EMITTER(f) emit_native_xtensa_##f
8284
#else
8385
#error "unknown native emitter"
8486
#endif

py/emit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ extern const emit_method_table_t emit_native_x64_method_table;
154154
extern const emit_method_table_t emit_native_x86_method_table;
155155
extern const emit_method_table_t emit_native_thumb_method_table;
156156
extern const emit_method_table_t emit_native_arm_method_table;
157+
extern const emit_method_table_t emit_native_xtensa_method_table;
157158

158159
extern const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops;
159160
extern const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops;
@@ -164,6 +165,7 @@ emit_t *emit_native_x64_new(mp_obj_t *error_slot, mp_uint_t max_num_labels);
164165
emit_t *emit_native_x86_new(mp_obj_t *error_slot, mp_uint_t max_num_labels);
165166
emit_t *emit_native_thumb_new(mp_obj_t *error_slot, mp_uint_t max_num_labels);
166167
emit_t *emit_native_arm_new(mp_obj_t *error_slot, mp_uint_t max_num_labels);
168+
emit_t *emit_native_xtensa_new(mp_obj_t *error_slot, mp_uint_t max_num_labels);
167169

168170
void emit_bc_set_max_num_labels(emit_t* emit, mp_uint_t max_num_labels);
169171

@@ -172,6 +174,7 @@ void emit_native_x64_free(emit_t *emit);
172174
void emit_native_x86_free(emit_t *emit);
173175
void emit_native_thumb_free(emit_t *emit);
174176
void emit_native_arm_free(emit_t *emit);
177+
void emit_native_xtensa_free(emit_t *emit);
175178

176179
void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope);
177180
void mp_emit_bc_end_pass(emit_t *emit);

py/emitnative.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
#if (MICROPY_EMIT_X64 && N_X64) \
6262
|| (MICROPY_EMIT_X86 && N_X86) \
6363
|| (MICROPY_EMIT_THUMB && N_THUMB) \
64-
|| (MICROPY_EMIT_ARM && N_ARM)
64+
|| (MICROPY_EMIT_ARM && N_ARM) \
65+
|| (MICROPY_EMIT_XTENSA && N_XTENSA) \
6566

6667
// this is defined so that the assembler exports generic assembler API macros
6768
#define GENERIC_ASM_API (1)
@@ -139,6 +140,12 @@ STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
139140
#include "py/asmarm.h"
140141
#define EXPORT_FUN(name) emit_native_arm_##name
141142

143+
#elif N_XTENSA
144+
145+
// Xtensa specific stuff
146+
#include "py/asmxtensa.h"
147+
#define EXPORT_FUN(name) emit_native_xtensa_##name
148+
142149
#else
143150

144151
#error unknown native emitter
@@ -1965,6 +1972,21 @@ STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
19651972
ASM_ARM_CC_NE,
19661973
};
19671974
asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
1975+
#elif N_XTENSA
1976+
static uint8_t ccs[6] = {
1977+
ASM_XTENSA_CC_LT,
1978+
0x80 | ASM_XTENSA_CC_LT, // for GT we'll swap args
1979+
ASM_XTENSA_CC_EQ,
1980+
0x80 | ASM_XTENSA_CC_GE, // for LE we'll swap args
1981+
ASM_XTENSA_CC_GE,
1982+
ASM_XTENSA_CC_NE,
1983+
};
1984+
uint8_t cc = ccs[op - MP_BINARY_OP_LESS];
1985+
if ((cc & 0x80) == 0) {
1986+
asm_xtensa_setcc_reg_reg_reg(emit->as, cc, REG_RET, REG_ARG_2, reg_rhs);
1987+
} else {
1988+
asm_xtensa_setcc_reg_reg_reg(emit->as, cc & ~0x80, REG_RET, reg_rhs, REG_ARG_2);
1989+
}
19681990
#else
19691991
#error not implemented
19701992
#endif

py/mkrules.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ $(BUILD)/%.o: %.c
4949
# List all native flags since the current build system doesn't have
5050
# the micropython configuration available. However, these flags are
5151
# needed to extract all qstrings
52-
QSTR_GEN_EXTRA_CFLAGS += -DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM
52+
QSTR_GEN_EXTRA_CFLAGS += -DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM -DN_XTENSA
5353
QSTR_GEN_EXTRA_CFLAGS += -I$(BUILD)/tmp
5454

5555
vpath %.c . $(TOP)

py/mpconfig.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,13 @@
288288
#define MICROPY_EMIT_ARM (0)
289289
#endif
290290

291+
// Whether to emit Xtensa native code
292+
#ifndef MICROPY_EMIT_XTENSA
293+
#define MICROPY_EMIT_XTENSA (0)
294+
#endif
295+
291296
// Convenience definition for whether any native emitter is enabled
292-
#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_X86 || MICROPY_EMIT_THUMB || MICROPY_EMIT_ARM)
297+
#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_X86 || MICROPY_EMIT_THUMB || MICROPY_EMIT_ARM || MICROPY_EMIT_XTENSA)
293298

294299
/*****************************************************************************/
295300
/* Compiler configuration */

py/py.mk

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ PY_O_BASENAME = \
130130
emitinlinethumb.o \
131131
asmarm.o \
132132
emitnarm.o \
133+
asmxtensa.o \
134+
emitnxtensa.o \
133135
formatfloat.o \
134136
parsenumbase.o \
135137
parsenum.o \
@@ -250,7 +252,7 @@ PY_O += $(BUILD)/$(BUILD)/frozen_mpy.o
250252
endif
251253

252254
# Sources that may contain qstrings
253-
SRC_QSTR_IGNORE = nlr% emitnx% emitnthumb% emitnarm%
255+
SRC_QSTR_IGNORE = nlr% emitnx86% emitnx64% emitnthumb% emitnarm% emitnxtensa%
254256
SRC_QSTR = $(SRC_MOD) $(addprefix py/,$(filter-out $(SRC_QSTR_IGNORE),$(PY_O_BASENAME:.o=.c)) emitnative.c)
255257

256258
# Anything that depends on FORCE will be considered out-of-date
@@ -292,6 +294,10 @@ $(PY_BUILD)/emitnarm.o: CFLAGS += -DN_ARM
292294
$(PY_BUILD)/emitnarm.o: py/emitnative.c
293295
$(call compile_c)
294296

297+
$(PY_BUILD)/emitnxtensa.o: CFLAGS += -DN_XTENSA
298+
$(PY_BUILD)/emitnxtensa.o: py/emitnative.c
299+
$(call compile_c)
300+
295301
# optimising gc for speed; 5ms down to 4ms on pybv2
296302
$(PY_BUILD)/gc.o: CFLAGS += $(CSUPEROPT)
297303

0 commit comments

Comments
 (0)
0