On Wed, 2 Apr 2025, Jacek Caban wrote:

On ARM64EC, function declarations have additional nuances:
- Function names are mangled by prefixing them with "#"
- An unmangled symbol is defined as a weak anti-dependency alias to the mangled
 symbol
- An entry thunk is generated to convert from the x86_64 calling convention to
 the ARM64EC calling convention, used by the emulator
- A .hybmp section entry is generated to associate the function with its entry
 thunk

The compiler can handle all of this if provided with the necessary information.
Naked functions are the most convenient way to achieve this.

Use naked functions only on Clang. GCC doesn’t support them on ARM targets and
has broken behavior on x86_64 by emitting .seh_endprologue.
---
mingw-w64-crt/include/internal.h      | 15 ++++++++++++++-
mingw-w64-crt/math/arm64/nearbyint.c  |  2 +-
mingw-w64-crt/math/arm64/nearbyintf.c |  2 +-
mingw-w64-crt/math/arm64/nearbyintl.c |  2 +-
mingw-w64-crt/math/arm64/trunc.c      |  2 +-
mingw-w64-crt/math/arm64/truncf.c     |  2 +-
6 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/mingw-w64-crt/include/internal.h b/mingw-w64-crt/include/internal.h
index 0d75d63e3..0c0106ab2 100644
--- a/mingw-w64-crt/include/internal.h
+++ b/mingw-w64-crt/include/internal.h
@@ -287,7 +287,11 @@ static inline unsigned int __mingw_statusfp(void)
    return flags;
}

-#define __ASM_NAKED_FUNC(name,code)  \
+/* Use naked functions only on Clang. GCC doesn’t support them on ARM targets 
and
+ * has broken behavior on x86_64 by emitting .seh_endprologue. */
+#ifndef __clang__
+
+#define __ASM_NAKED_FUNC(name, rettype, args, code) \
    asm(".text\n\t" \
        ".p2align 2\n\t" \
        ".globl " __MINGW64_STRINGIFY(__MINGW_USYMBOL(name)) "\n\t" \

I'm a little undecided about the naming of the macro; as this can be either a naked function or a global asm function, the macro name feels a bit misleading. But I don't have a different good suggestion either.

diff --git a/mingw-w64-crt/math/arm64/nearbyint.c 
b/mingw-w64-crt/math/arm64/nearbyint.c
index 44c74cf71..091d374f1 100644
--- a/mingw-w64-crt/math/arm64/nearbyint.c
+++ b/mingw-w64-crt/math/arm64/nearbyint.c
@@ -7,7 +7,7 @@
#include <math.h>
#include <internal.h>

-__ASM_NAKED_FUNC(nearbyint,
+__ASM_NAKED_FUNC(nearbyint, double, (double x),

Thanks, this setup looks very much nicer for the individual source files.

I wonder if it would feel even more natural for the declarations, if we'd swap the order of the arguments, so that we'd have __ASM_NAKED_FUNC(double, nearbyint, (double x), ...)

It's of course less natural to not have the most important thing, the symbol name, as the first argument, but from the point of view of the files using the macro, I wonder if that order would feel even more comfortable.

Other than that, this looks good to me!

// Martin

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to