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>
This commit is contained in:
Alessandro Gatti
2025-12-19 21:20:49 +01:00
parent b0f3ecd96c
commit 5ae928a2b4

View File

@@ -48,31 +48,44 @@
#define MPY_FEATURE_DECODE_ARCH(feat) (((feat) >> 2) & 0x2F)
// Define the host architecture
#if MICROPY_EMIT_X86
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86)
#elif MICROPY_EMIT_X64
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64)
#elif MICROPY_EMIT_THUMB
#if defined(__thumb2__)
#if defined(__ARM_FP) && (__ARM_FP & 8) == 8
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMDP)
#elif defined(__ARM_FP) && (__ARM_FP & 4) == 4
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMSP)
#if MICROPY_PERSISTENT_CODE_LOAD_NATIVE
#if defined(__i386__) || defined(_M_IX86)
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86)
#elif defined(__x86_64__) || defined(_M_X64)
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64)
#elif defined(__thumb2__) || defined(__thumb__)
#if defined(__thumb2__)
#if defined(__ARM_FP) && (__ARM_FP & 8) == 8
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMDP)
#elif defined(__ARM_FP) && (__ARM_FP & 4) == 4
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMSP)
#else
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EM)
#endif
#else
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EM)
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6M)
#endif
#define MPY_FEATURE_ARCH_TEST(x) (MP_NATIVE_ARCH_ARMV6M <= (x) && (x) <= MPY_FEATURE_ARCH)
#elif defined(__arm__)
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6)
#elif defined(__xtensa__)
#include <xtensa/config/core-isa.h>
#if XCHAL_HAVE_WINDOWED
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN)
#else
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA)
#endif
#elif defined(__riscv)
#if __riscv_xlen == 32
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_RV32IMC)
#elif __riscv_xlen == 64
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_RV64IMC)
#else
#error "Unsupported RISC-V architecture."
#endif
#else
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6M)
#error "Unsupported native architecture."
#endif
#define MPY_FEATURE_ARCH_TEST(x) (MP_NATIVE_ARCH_ARMV6M <= (x) && (x) <= MPY_FEATURE_ARCH)
#elif MICROPY_EMIT_ARM
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6)
#elif MICROPY_EMIT_XTENSA
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA)
#elif MICROPY_EMIT_XTENSAWIN
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN)
#elif MICROPY_EMIT_RV32
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_RV32IMC)
#else
#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_NONE)
#endif