prefix assembly functions with _ on macos

This commit is contained in:
Brendan Hansknecht 2023-06-02 15:10:14 -07:00
parent 60babcc56e
commit 341ef9bb7b
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
7 changed files with 84 additions and 72 deletions

View file

@ -0,0 +1,7 @@
const builtin = @import("builtin");
const os = builtin.os;
pub const function_prefix = switch (os.tag) {
.macos => "_",
else => "",
};

View file

@ -1,67 +1,69 @@
const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const arch = builtin.cpu.arch; const arch = builtin.cpu.arch;
const function_prefix = @import("assembly_util.zig").function_prefix;
// I couldn't manage to define this in a PIE friendly way with inline assembly. // I couldn't manage to define this in a PIE friendly way with inline assembly.
// Instead, I am defining it as global assembly functions. // Instead, I am defining it as global assembly functions.
comptime { comptime {
switch (arch) { switch (arch) {
.x86_64 => { .x86_64 => {
asm ( asm (std.fmt.comptimePrint(
\\ # Check if AVX2 is supported. \\ # Check if AVX2 is supported.
\\ # Returns 1 if AVX2 is supported, 0 otherwise. \\ # Returns 1 if AVX2 is supported, 0 otherwise.
\\ .global supports_avx2; \\ .global {[function_prefix]s}supports_avx2;
\\ supports_avx2: \\ {[function_prefix]s}supports_avx2:
\\ # Save the EBX register. \\ # Save the EBX register.
\\ push %rbx \\ push %rbx
\\ \\
\\ # Call the CPUID instruction with the EAX register set to 7 and ECX set to 0. \\ # Call the CPUID instruction with the EAX register set to 7 and ECX set to 0.
\\ # This will get the CPUID information for the current CPU. \\ # This will get the CPUID information for the current CPU.
\\ mov $7, %eax \\ mov $7, %eax
\\ mov $0, %ecx \\ mov $0, %ecx
\\ cpuid \\ cpuid
\\ \\
\\ # The AVX2 feature flag is located in the EBX register at bit 5. \\ # The AVX2 feature flag is located in the EBX register at bit 5.
\\ bt $5, %ebx \\ bt $5, %ebx
\\ jc .avx2_supported \\ jc .avx2_supported
\\ \\
\\ # AVX2 is not supported. \\ # AVX2 is not supported.
\\ pop %rbx \\ pop %rbx
\\ mov $0, %eax \\ mov $0, %eax
\\ ret \\ ret
\\ \\
\\ .avx2_supported: \\ .avx2_supported:
\\ pop %rbx \\ pop %rbx
\\ mov $1, %eax \\ mov $1, %eax
\\ ret \\ ret
); , .{ .function_prefix = function_prefix }));
asm ( asm (std.fmt.comptimePrint(
\\ # Check if prefetchw is supported. \\ # Check if prefetchw is supported.
\\ # Returns 1 if the prefetchw instruction is supported, 0 otherwise. \\ # Returns 1 if the prefetchw instruction is supported, 0 otherwise.
\\ .global supports_prefetchw; \\ .global {[function_prefix]s}supports_prefetchw;
\\ supports_prefetchw: \\ {[function_prefix]s}supports_prefetchw:
\\ # Save the EBX register. \\ # Save the EBX register.
\\ push %rbx \\ push %rbx
\\ \\
\\ # Call the CPUID instruction with the EAX register set to 0x80000001 and ECX set to 0. \\ # Call the CPUID instruction with the EAX register set to 0x80000001 and ECX set to 0.
\\ # This will get the CPUID information for the current CPU. \\ # This will get the CPUID information for the current CPU.
\\ mov $0x80000001, %eax \\ mov $0x80000001, %eax
\\ mov $0, %ecx \\ mov $0, %ecx
\\ cpuid \\ cpuid
\\ \\
\\ # The prefetchw feature flag is located in the ECX register at bit 8. \\ # The prefetchw feature flag is located in the ECX register at bit 8.
\\ bt $8, %ecx \\ bt $8, %ecx
\\ jc .prefetchw_supported \\ jc .prefetchw_supported
\\ \\
\\ # AVX2 is not supported. \\ # AVX2 is not supported.
\\ pop %rbx \\ pop %rbx
\\ mov $0, %eax \\ mov $0, %eax
\\ ret \\ ret
\\ \\
\\ .prefetchw_supported: \\ .prefetchw_supported:
\\ pop %rbx \\ pop %rbx
\\ mov $1, %eax \\ mov $1, %eax
\\ ret \\ ret
); , .{ .function_prefix = function_prefix }));
}, },
else => unreachable, else => unreachable,
} }

View file

@ -71,8 +71,8 @@
*/ */
# .type __folly_memcpy_short_{[prefetch]s}, @function not supported by windows # .type {[function_prefix]s}__folly_memcpy_short_{[prefetch]s}, @function not supported by windows
__folly_memcpy_short_{[prefetch]s}: {[function_prefix]s}__folly_memcpy_short_{[prefetch]s}:
.cfi_startproc .cfi_startproc
.L_GE1_LE7_{[prefetch]s}: .L_GE1_LE7_{[prefetch]s}:
@ -106,7 +106,7 @@ __folly_memcpy_short_{[prefetch]s}:
ret ret
.cfi_endproc .cfi_endproc
# .size __folly_memcpy_short_{[prefetch]s}, .-__folly_memcpy_short_{[prefetch]s} not supported by windows # .size {[function_prefix]s}__folly_memcpy_short_{[prefetch]s}, .-{[function_prefix]s}__folly_memcpy_short_{[prefetch]s} not supported by windows
// memcpy is an alternative entrypoint into the function named __folly_memcpy. // memcpy is an alternative entrypoint into the function named __folly_memcpy.
// The compiler is able to call memcpy since the name is global while // The compiler is able to call memcpy since the name is global while
@ -114,10 +114,10 @@ __folly_memcpy_short_{[prefetch]s}:
// This is intended to aid in debugging by making it obvious which version of // This is intended to aid in debugging by making it obvious which version of
// memcpy is being used. // memcpy is being used.
.balign 64 .balign 64
.globl __folly_memcpy_{[prefetch]s} .globl {[function_prefix]s}__folly_memcpy_{[prefetch]s}
# .type __folly_memcpy_{[prefetch]s}, @function not supported by windows # .type {[function_prefix]s}__folly_memcpy_{[prefetch]s}, @function not supported by windows
__folly_memcpy_{[prefetch]s}: {[function_prefix]s}__folly_memcpy_{[prefetch]s}:
.cfi_startproc .cfi_startproc
mov %rdi, %rax # return: $rdi mov %rdi, %rax # return: $rdi
@ -434,4 +434,4 @@ __folly_memcpy_{[prefetch]s}:
ret ret
.cfi_endproc .cfi_endproc
# .size __folly_memcpy_{[prefetch]s}, .-__folly_memcpy_{[prefetch]s} not supported by windows # .size {[function_prefix]s}__folly_memcpy_{[prefetch]s}, .-{[function_prefix]s}__folly_memcpy_{[prefetch]s} not supported by windows

View file

@ -1,12 +1,13 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const arch = builtin.cpu.arch; const arch = builtin.cpu.arch;
const function_prefix = @import("../assembly_util.zig").function_prefix;
comptime { comptime {
switch (arch) { switch (arch) {
.x86_64 => { .x86_64 => {
inline for ([_][]const u8{ "prefetchw", "prefetcht0" }) |prefetch| { inline for ([_][]const u8{ "prefetchw", "prefetcht0" }) |prefetch| {
asm (std.fmt.comptimePrint(@embedFile("memcpy-x86_64.S"), .{ .prefetch = prefetch })); asm (std.fmt.comptimePrint(@embedFile("memcpy-x86_64.S"), .{ .prefetch = prefetch, .function_prefix = function_prefix }));
} }
}, },
else => unreachable, else => unreachable,

View file

@ -1,7 +1,7 @@
.global musl_memcpy .global {[function_prefix]s}musl_memcpy
# Windows does not support the type directive. # Windows does not support the type directive.
# .type memcpy,@function # .type {[function_prefix]s}musl_memcpy,@function
musl_memcpy: {[function_prefix]s}musl_memcpy:
push %esi push %esi
push %edi push %edi
mov 12(%esp),%edi mov 12(%esp),%edi

View file

@ -1,7 +1,7 @@
.global musl_memcpy .global {[function_prefix]s}musl_memcpy
# Windows does not support the type directive. # Windows does not support the type directive.
# .type musl_memcpy,@function # .type {[function_prefix]s}musl_memcpy,@function
musl_memcpy: {[function_prefix]s}musl_memcpy:
mov %rdi,%rax mov %rdi,%rax
cmp $8,%rdx cmp $8,%rdx
jc 1f jc 1f

View file

@ -1,13 +1,15 @@
const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const arch = builtin.cpu.arch; const arch = builtin.cpu.arch;
const function_prefix = @import("../assembly_util.zig").function_prefix;
comptime { comptime {
switch (arch) { switch (arch) {
.x86_64 => { .x86_64 => {
asm (@embedFile("memcpy-x86_64.S")); asm (std.fmt.comptimePrint(@embedFile("memcpy-x86_64.S"), .{ .function_prefix = function_prefix }));
}, },
.i386 => { .i386 => {
asm (@embedFile("memcpy-i386.S")); asm (std.fmt.comptimePrint(@embedFile("memcpy-i386.S"), .{ .function_prefix = function_prefix }));
}, },
// TODO: add assembly implementations for other platforms. // TODO: add assembly implementations for other platforms.
else => {}, else => {},