Skip to content

Commit 5ae928a

Browse files
committed
py/persistentcode: Detect the target architecture from compiler defines.
This commit replaces the target architecture definition method, from relying on defines in MicroPython's configuration, to using the host compiler's environment definitions instead. Before these changes the target type was inferred from which kind of native emitter was enabled, since there can be only just one available at all times and it has to be the correct one otherwise generated code would crash the target. However, with the introduction of RV64 there is now a platform without an emitter, and thus RV64 targets would not be able to report their platform via "sys.implementation._mpy". The target is reported only if the interpreter is configured to load external MPY code. Now a series of compile definitions are used to detect the target platform the firmware image is compiled for, making the target definition more accurate and much harder to force the interpreter to report the wrong target architecture due to a misconfiguration. Whilst this is probably not directly affecting user-facing code, some infrastructure components like the testing framework and its associated feature scripts rely on this feature to properly execute test runs. This is also a concern for situations in which loading external binary code is needed but all code generation functions have to be disabled. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
1 parent b0f3ecd commit 5ae928a

1 file changed

Lines changed: 34 additions & 21 deletions

File tree

py/persistentcode.h

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,44 @@
4848
#define MPY_FEATURE_DECODE_ARCH(feat) (((feat) >> 2) & 0x2F)
4949

5050
// Define the host architecture
51-
#if MICROPY_EMIT_X86
52-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86)
53-
#elif MICROPY_EMIT_X64
54-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64)
55-
#elif MICROPY_EMIT_THUMB
56-
#if defined(__thumb2__)
57-
#if defined(__ARM_FP) && (__ARM_FP & 8) == 8
58-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMDP)
59-
#elif defined(__ARM_FP) && (__ARM_FP & 4) == 4
60-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMSP)
51+
#if MICROPY_PERSISTENT_CODE_LOAD_NATIVE
52+
#if defined(__i386__) || defined(_M_IX86)
53+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86)
54+
#elif defined(__x86_64__) || defined(_M_X64)
55+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64)
56+
#elif defined(__thumb2__) || defined(__thumb__)
57+
#if defined(__thumb2__)
58+
#if defined(__ARM_FP) && (__ARM_FP & 8) == 8
59+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMDP)
60+
#elif defined(__ARM_FP) && (__ARM_FP & 4) == 4
61+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMSP)
62+
#else
63+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EM)
64+
#endif
6165
#else
62-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EM)
66+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6M)
67+
#endif
68+
#define MPY_FEATURE_ARCH_TEST(x) (MP_NATIVE_ARCH_ARMV6M <= (x) && (x) <= MPY_FEATURE_ARCH)
69+
#elif defined(__arm__)
70+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6)
71+
#elif defined(__xtensa__)
72+
#include <xtensa/config/core-isa.h>
73+
#if XCHAL_HAVE_WINDOWED
74+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN)
75+
#else
76+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA)
77+
#endif
78+
#elif defined(__riscv)
79+
#if __riscv_xlen == 32
80+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_RV32IMC)
81+
#elif __riscv_xlen == 64
82+
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_RV64IMC)
83+
#else
84+
#error "Unsupported RISC-V architecture."
6385
#endif
6486
#else
65-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6M)
87+
#error "Unsupported native architecture."
6688
#endif
67-
#define MPY_FEATURE_ARCH_TEST(x) (MP_NATIVE_ARCH_ARMV6M <= (x) && (x) <= MPY_FEATURE_ARCH)
68-
#elif MICROPY_EMIT_ARM
69-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6)
70-
#elif MICROPY_EMIT_XTENSA
71-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA)
72-
#elif MICROPY_EMIT_XTENSAWIN
73-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN)
74-
#elif MICROPY_EMIT_RV32
75-
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_RV32IMC)
7689
#else
7790
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_NONE)
7891
#endif

0 commit comments

Comments
 (0)