Merge pull request #18431 from jacobly0/cbe-extern

cbe: fix non-msvc externs and exports
This commit is contained in:
Andrew Kelley 2024-01-03 12:57:01 -08:00 committed by GitHub
commit a9337bef2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 506 additions and 223 deletions

View File

@ -96,9 +96,8 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files\Microsoft Visual Studio\2022\E
CheckLastExitCode
Write-Output "Build and run behavior tests with msvc..."
Write-Output "Skipped due to https://github.com/ziglang/zig/issues/17817"
#& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console kernel32.lib ntdll.lib libcmt.lib
#CheckLastExitCode
#
#& .\test-x86_64-windows-msvc.exe
#CheckLastExitCode
& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console kernel32.lib ntdll.lib libcmt.lib
CheckLastExitCode
& .\test-x86_64-windows-msvc.exe
CheckLastExitCode

View File

@ -95,9 +95,8 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files\Microsoft Visual Studio\2022\E
CheckLastExitCode
Write-Output "Build and run behavior tests with msvc..."
Write-Output "Skipped due to https://github.com/ziglang/zig/issues/17817"
#& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console kernel32.lib ntdll.lib libcmt.lib
#CheckLastExitCode
#
#& .\test-x86_64-windows-msvc.exe
#CheckLastExitCode
& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console kernel32.lib ntdll.lib libcmt.lib
CheckLastExitCode
& .\test-x86_64-windows-msvc.exe
CheckLastExitCode

View File

@ -1266,6 +1266,7 @@ pub fn lerp(a: anytype, b: anytype, t: anytype) @TypeOf(a, b, t) {
}
test "lerp" {
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/17884
if (builtin.zig_backend == .stage2_x86_64 and
!comptime std.Target.x86.featureSetHas(builtin.cpu.features, .fma)) return error.SkipZigTest;

249
lib/zig.h
View File

@ -25,11 +25,15 @@ typedef char bool;
#endif
#endif
#define zig_concat(lhs, rhs) lhs##rhs
#define zig_expand_concat(lhs, rhs) zig_concat(lhs, rhs)
#if defined(__has_builtin)
#define zig_has_builtin(builtin) __has_builtin(__builtin_##builtin)
#else
#define zig_has_builtin(builtin) 0
#endif
#define zig_expand_has_builtin(b) zig_has_builtin(b)
#if defined(__has_attribute)
#define zig_has_attribute(attribute) __has_attribute(attribute)
@ -112,7 +116,7 @@ typedef char bool;
#define zig_never_tail zig_never_tail_unavailable
#endif
#if zig_has_attribute(always_inline)
#if zig_has_attribute(musttail)
#define zig_always_tail __attribute__((musttail))
#else
#define zig_always_tail zig_always_tail_unavailable
@ -180,20 +184,58 @@ typedef char bool;
#define zig_extern extern
#endif
#if zig_has_attribute(alias)
#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(symbol)))
#elif _MSC_VER
#if _MSC_VER
#if _M_X64
#define zig_export(sig, symbol, name) sig;\
__pragma(comment(linker, "/alternatename:" name "=" symbol ))
#define zig_mangle_c(symbol) symbol
#else /*_M_X64 */
#define zig_export(sig, symbol, name) sig;\
__pragma(comment(linker, "/alternatename:_" name "=_" symbol ))
#define zig_mangle_c(symbol) "_" symbol
#endif /*_M_X64 */
#else /* _MSC_VER */
#if __APPLE__
#define zig_mangle_c(symbol) "_" symbol
#else /* __APPLE__ */
#define zig_mangle_c(symbol) symbol
#endif /* __APPLE__ */
#endif /* _MSC_VER */
#if zig_has_attribute(alias) && !__APPLE__
#define zig_export(symbol, name) __attribute__((alias(symbol)))
#elif _MSC_VER
#define zig_export(symbol, name) ; \
__pragma(comment(linker, "/alternatename:" zig_mangle_c(name) "=" zig_mangle_c(symbol)))
#else
#define zig_export(sig, symbol, name) __asm(name " = " symbol)
#define zig_export(symbol, name) ; \
__asm(zig_mangle_c(name) " = " zig_mangle_c(symbol))
#endif
#if _MSC_VER
#define zig_mangled_tentative(mangled, unmangled)
#define zig_mangled_final(mangled, unmangled) ; \
zig_export(#mangled, unmangled)
#define zig_mangled_export(mangled, unmangled, symbol) \
zig_export(unmangled, #mangled) \
zig_export(symbol, unmangled)
#else /* _MSC_VER */
#define zig_mangled_tentative(mangled, unmangled) __asm(zig_mangle_c(unmangled))
#define zig_mangled_final(mangled, unmangled) zig_mangled_tentative(mangled, unmangled)
#define zig_mangled_export(mangled, unmangled, symbol) \
zig_mangled_final(mangled, unmangled) \
zig_export(symbol, unmangled)
#endif /* _MSC_VER */
#if _MSC_VER
#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args;\
__pragma(comment(linker, "/alternatename:" zig_mangle_c(#fn_name) "=" zig_mangle_c(#libc_name)));
#define zig_import_builtin(Type, fn_name, libc_name, sig_args, call_args) zig_import(Type, fn_name, sig_args, call_args)
#else /* _MSC_VER */
#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args __asm(zig_mangle_c(#libc_name));
#define zig_import_builtin(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type libc_name sig_args; \
static inline Type fn_name sig_args { return libc_name call_args; }
#endif
#define zig_expand_import_0(Type, fn_name, libc_name, sig_args, call_args) zig_import(Type, fn_name, libc_name, sig_args, call_args)
#define zig_expand_import_1(Type, fn_name, libc_name, sig_args, call_args) zig_import_builtin(Type, fn_name, libc_name, sig_args, call_args)
#if zig_has_attribute(weak) || defined(zig_gnuc)
#define zig_weak_linkage __attribute__((weak))
#define zig_weak_linkage_fn __attribute__((weak))
@ -267,9 +309,6 @@ typedef char bool;
#define zig_wasm_memory_grow(index, delta) zig_unimplemented()
#endif
#define zig_concat(lhs, rhs) lhs##rhs
#define zig_expand_concat(lhs, rhs) zig_concat(lhs, rhs)
#if __STDC_VERSION__ >= 201112L
#define zig_noreturn _Noreturn
#elif zig_has_attribute(noreturn) || defined(zig_gnuc)
@ -2163,7 +2202,7 @@ static inline bool zig_addo_big(void *res, const void *lhs, const void *rhs, boo
const uint8_t *rhs_bytes = rhs;
uint16_t byte_offset = 0;
uint16_t remaining_bytes = zig_int_bytes(bits);
uint16_t top_bits = remaining_bytes * 8 - bits;
uint8_t top_bits = (uint8_t)(remaining_bytes * 8 - bits);
bool overflow = false;
#if zig_big_endian
@ -2171,7 +2210,7 @@ static inline bool zig_addo_big(void *res, const void *lhs, const void *rhs, boo
#endif
while (remaining_bytes >= 128 / CHAR_BIT) {
uint16_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 128 / CHAR_BIT;
@ -2211,7 +2250,7 @@ static inline bool zig_addo_big(void *res, const void *lhs, const void *rhs, boo
}
while (remaining_bytes >= 64 / CHAR_BIT) {
uint16_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 64 / CHAR_BIT;
@ -2251,7 +2290,7 @@ static inline bool zig_addo_big(void *res, const void *lhs, const void *rhs, boo
}
while (remaining_bytes >= 32 / CHAR_BIT) {
uint16_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 32 / CHAR_BIT;
@ -2291,7 +2330,7 @@ static inline bool zig_addo_big(void *res, const void *lhs, const void *rhs, boo
}
while (remaining_bytes >= 16 / CHAR_BIT) {
uint16_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 16 / CHAR_BIT;
@ -2331,7 +2370,7 @@ static inline bool zig_addo_big(void *res, const void *lhs, const void *rhs, boo
}
while (remaining_bytes >= 8 / CHAR_BIT) {
uint16_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 8 / CHAR_BIT;
@ -2379,7 +2418,7 @@ static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, boo
const uint8_t *rhs_bytes = rhs;
uint16_t byte_offset = 0;
uint16_t remaining_bytes = zig_int_bytes(bits);
uint16_t top_bits = remaining_bytes * 8 - bits;
uint8_t top_bits = (uint8_t)(remaining_bytes * 8 - bits);
bool overflow = false;
#if zig_big_endian
@ -2387,7 +2426,7 @@ static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, boo
#endif
while (remaining_bytes >= 128 / CHAR_BIT) {
uint16_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 128 - (remaining_bytes == 128 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 128 / CHAR_BIT;
@ -2427,7 +2466,7 @@ static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, boo
}
while (remaining_bytes >= 64 / CHAR_BIT) {
uint16_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 64 - (remaining_bytes == 64 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 64 / CHAR_BIT;
@ -2467,7 +2506,7 @@ static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, boo
}
while (remaining_bytes >= 32 / CHAR_BIT) {
uint16_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 32 - (remaining_bytes == 32 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 32 / CHAR_BIT;
@ -2507,7 +2546,7 @@ static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, boo
}
while (remaining_bytes >= 16 / CHAR_BIT) {
uint16_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 16 - (remaining_bytes == 16 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 16 / CHAR_BIT;
@ -2547,7 +2586,7 @@ static inline bool zig_subo_big(void *res, const void *lhs, const void *rhs, boo
}
while (remaining_bytes >= 8 / CHAR_BIT) {
uint16_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0);
uint8_t limb_bits = 8 - (remaining_bytes == 8 / CHAR_BIT ? top_bits : 0);
#if zig_big_endian
byte_offset -= 8 / CHAR_BIT;
@ -3093,6 +3132,7 @@ ypedef uint32_t zig_f32;
#define zig_has_f64 1
#define zig_libc_name_f64(name) name
#if _MSC_VER
#define zig_init_special_f64(sign, name, arg, repr) sign zig_make_f64(zig_msvc_flt_##name, )
#else
@ -3336,31 +3376,31 @@ zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0)))
zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, sub, -) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, mul, *) \
zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, div, /) \
zig_extern zig_f##w zig_libc_name_f##w(sqrt)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(sin)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(cos)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(tan)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(exp)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(exp2)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(log)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(log2)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(log10)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fabs)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(floor)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(ceil)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(round)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(trunc)(zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fmod)(zig_f##w, zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fmin)(zig_f##w, zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fmax)(zig_f##w, zig_f##w); \
zig_extern zig_f##w zig_libc_name_f##w(fma)(zig_f##w, zig_f##w, zig_f##w); \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(sqrt)))(zig_f##w, zig_float_fn_f##w##_sqrt, zig_libc_name_f##w(sqrt), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(sin)))(zig_f##w, zig_float_fn_f##w##_sin, zig_libc_name_f##w(sin), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(cos)))(zig_f##w, zig_float_fn_f##w##_cos, zig_libc_name_f##w(cos), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(tan)))(zig_f##w, zig_float_fn_f##w##_tan, zig_libc_name_f##w(tan), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(exp)))(zig_f##w, zig_float_fn_f##w##_exp, zig_libc_name_f##w(exp), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(exp2)))(zig_f##w, zig_float_fn_f##w##_exp2, zig_libc_name_f##w(exp2), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(log)))(zig_f##w, zig_float_fn_f##w##_log, zig_libc_name_f##w(log), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(log2)))(zig_f##w, zig_float_fn_f##w##_log2, zig_libc_name_f##w(log2), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(log10)))(zig_f##w, zig_float_fn_f##w##_log10, zig_libc_name_f##w(log10), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(fabs)))(zig_f##w, zig_float_fn_f##w##_fabs, zig_libc_name_f##w(fabs), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(floor)))(zig_f##w, zig_float_fn_f##w##_floor, zig_libc_name_f##w(floor), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(ceil)))(zig_f##w, zig_float_fn_f##w##_ceil, zig_libc_name_f##w(ceil), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(round)))(zig_f##w, zig_float_fn_f##w##_round, zig_libc_name_f##w(round), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(trunc)))(zig_f##w, zig_float_fn_f##w##_trunc, zig_libc_name_f##w(trunc), (zig_f##w x), (x)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(fmod)))(zig_f##w, zig_float_fn_f##w##_fmod, zig_libc_name_f##w(fmod), (zig_f##w x, zig_f##w y), (x, y)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(fmin)))(zig_f##w, zig_float_fn_f##w##_fmin, zig_libc_name_f##w(fmin), (zig_f##w x, zig_f##w y), (x, y)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(fmax)))(zig_f##w, zig_float_fn_f##w##_fmax, zig_libc_name_f##w(fmax), (zig_f##w x, zig_f##w y), (x, y)) \
zig_expand_concat(zig_expand_import_, zig_expand_has_builtin(zig_libc_name_f##w(fma)))(zig_f##w, zig_float_fn_f##w##_fma, zig_libc_name_f##w(fma), (zig_f##w x, zig_f##w y, zig_f##w z), (x, y, z)) \
\
static inline zig_f##w zig_div_trunc_f##w(zig_f##w lhs, zig_f##w rhs) { \
return zig_libc_name_f##w(trunc)(zig_div_f##w(lhs, rhs)); \
return zig_float_fn_f##w##_trunc(zig_div_f##w(lhs, rhs)); \
} \
\
static inline zig_f##w zig_div_floor_f##w(zig_f##w lhs, zig_f##w rhs) { \
return zig_libc_name_f##w(floor)(zig_div_f##w(lhs, rhs)); \
return zig_float_fn_f##w##_floor(zig_div_f##w(lhs, rhs)); \
} \
\
static inline zig_f##w zig_mod_f##w(zig_f##w lhs, zig_f##w rhs) { \
@ -3437,129 +3477,134 @@ zig_float_builtins(64)
/* Note that zig_atomicrmw_expected is needed to handle aliasing between res and arg. */
#define zig_atomicrmw_xchg_float(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, arg, order, memory_order_relaxed, Type, ReprType)); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, arg, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_add_float(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_add_##Type(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_sub_float(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_sub_##Type(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_min_float(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_libc_name_##Type(fmin)(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
zig_atomicrmw_desired = zig_float_fn_##Type##_fmin(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_max_float(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_libc_name_##Type(fmax)(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
zig_atomicrmw_desired = zig_float_fn_##Type##_fmax(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_xchg_int128(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, arg, order, memory_order_relaxed, Type, ReprType)); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, arg, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_add_int128(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_add_##Type(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_sub_int128(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_sub_##Type(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_and_int128(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_and_##Type(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_nand_int128(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_not_##Type(zig_and_##Type(zig_atomicrmw_expected, arg), 128); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_or_int128(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_or_##Type(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_xor_int128(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_xor_##Type(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_min_int128(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_min_##Type(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#define zig_atomicrmw_max_int128(res, obj, arg, order, Type, ReprType) do { \
zig_##Type zig_atomicrmw_expected; \
zig_##Type zig_atomicrmw_desired; \
zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
zig_atomic_load(zig_atomicrmw_expected, obj, zig_memory_order_relaxed, Type, ReprType); \
do { \
zig_atomicrmw_desired = zig_max_##Type(zig_atomicrmw_expected, arg); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
} while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, zig_memory_order_relaxed, Type, ReprType)); \
res = zig_atomicrmw_expected; \
} while (0)
#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
#include <stdatomic.h>
typedef enum memory_order zig_memory_order;
#define zig_memory_order_relaxed memory_order_relaxed
#define zig_memory_order_acquire memory_order_acquire
#define zig_memory_order_release memory_order_release
#define zig_memory_order_acq_rel memory_order_acq_rel
#define zig_memory_order_seq_cst memory_order_seq_cst
#define zig_atomic(Type) _Atomic(Type)
#define zig_cmpxchg_strong( obj, expected, desired, succ, fail, Type, ReprType) atomic_compare_exchange_strong_explicit(obj, &(expected), desired, succ, fail)
#define zig_cmpxchg_weak( obj, expected, desired, succ, fail, Type, ReprType) atomic_compare_exchange_weak_explicit (obj, &(expected), desired, succ, fail)
@ -3583,12 +3628,11 @@ typedef enum memory_order zig_memory_order;
#define zig_fence(order) atomic_thread_fence(order)
#elif defined(__GNUC__)
typedef int zig_memory_order;
#define memory_order_relaxed __ATOMIC_RELAXED
#define memory_order_consume __ATOMIC_CONSUME
#define memory_order_acquire __ATOMIC_ACQUIRE
#define memory_order_release __ATOMIC_RELEASE
#define memory_order_acq_rel __ATOMIC_ACQ_REL
#define memory_order_seq_cst __ATOMIC_SEQ_CST
#define zig_memory_order_relaxed __ATOMIC_RELAXED
#define zig_memory_order_acquire __ATOMIC_ACQUIRE
#define zig_memory_order_release __ATOMIC_RELEASE
#define zig_memory_order_acq_rel __ATOMIC_ACQ_REL
#define zig_memory_order_seq_cst __ATOMIC_SEQ_CST
#define zig_atomic(Type) Type
#define zig_cmpxchg_strong( obj, expected, desired, succ, fail, Type, ReprType) __atomic_compare_exchange(obj, &(expected), &(desired), false, succ, fail)
#define zig_cmpxchg_weak( obj, expected, desired, succ, fail, Type, ReprType) __atomic_compare_exchange(obj, &(expected), &(desired), true, succ, fail)
@ -3607,12 +3651,11 @@ typedef int zig_memory_order;
#define zig_atomicrmw_xchg_float zig_atomicrmw_xchg
#define zig_fence(order) __atomic_thread_fence(order)
#elif _MSC_VER && (_M_IX86 || _M_X64)
#define memory_order_relaxed 0
#define memory_order_consume 1
#define memory_order_acquire 2
#define memory_order_release 3
#define memory_order_acq_rel 4
#define memory_order_seq_cst 5
#define zig_memory_order_relaxed 0
#define zig_memory_order_acquire 2
#define zig_memory_order_release 3
#define zig_memory_order_acq_rel 4
#define zig_memory_order_seq_cst 5
#define zig_atomic(Type) Type
#define zig_cmpxchg_strong( obj, expected, desired, succ, fail, Type, ReprType) zig_msvc_cmpxchg_##Type(obj, &(expected), desired)
#define zig_cmpxchg_weak( obj, expected, desired, succ, fail, Type, ReprType) zig_cmpxchg_strong(obj, expected, desired, succ, fail, Type, ReprType)
@ -3634,12 +3677,11 @@ typedef int zig_memory_order;
#endif
/* TODO: _MSC_VER && (_M_ARM || _M_ARM64) */
#else
#define memory_order_relaxed 0
#define memory_order_consume 1
#define memory_order_acquire 2
#define memory_order_release 3
#define memory_order_acq_rel 4
#define memory_order_seq_cst 5
#define zig_memory_order_relaxed 0
#define zig_memory_order_acquire 2
#define zig_memory_order_release 3
#define zig_memory_order_acq_rel 4
#define zig_memory_order_seq_cst 5
#define zig_atomic(Type) Type
#define zig_cmpxchg_strong( obj, expected, desired, succ, fail, Type, ReprType) zig_atomics_unavailable
#define zig_cmpxchg_weak( obj, expected, desired, succ, fail, Type, ReprType) zig_atomics_unavailable
@ -3830,9 +3872,32 @@ static inline bool zig_msvc_cmpxchg_u128(zig_u128 volatile* obj, zig_u128* expec
return _InterlockedCompareExchange128((__int64 volatile*)obj, (__int64)zig_hi_u128(desired), (__int64)zig_lo_u128(desired), (__int64*)expected);
}
static inline zig_u128 zig_msvc_atomic_load_u128(zig_u128 volatile* obj) {
zig_u128 expected = zig_make_u128(UINT64_C(0), UINT64_C(0));
(void)zig_cmpxchg_strong(obj, expected, expected, zig_memory_order_seq_cst, zig_memory_order_seq_cst, u128, zig_u128);
return expected;
}
static inline void zig_msvc_atomic_store_u128(zig_u128 volatile* obj, zig_u128 arg) {
zig_u128 expected = zig_make_u128(UINT64_C(0), UINT64_C(0));
while (!zig_cmpxchg_weak(obj, expected, arg, zig_memory_order_seq_cst, zig_memory_order_seq_cst, u128, zig_u128));
}
static inline bool zig_msvc_cmpxchg_i128(zig_i128 volatile* obj, zig_i128* expected, zig_i128 desired) {
return _InterlockedCompareExchange128((__int64 volatile*)obj, (__int64)zig_hi_i128(desired), (__int64)zig_lo_i128(desired), (__int64*)expected);
}
static inline zig_i128 zig_msvc_atomic_load_i128(zig_i128 volatile* obj) {
zig_i128 expected = zig_make_i128(INT64_C(0), UINT64_C(0));
(void)zig_cmpxchg_strong(obj, expected, expected, zig_memory_order_seq_cst, zig_memory_order_seq_cst, i128, zig_i128);
return expected;
}
static inline void zig_msvc_atomic_store_i128(zig_i128 volatile* obj, zig_i128 arg) {
zig_i128 expected = zig_make_i128(INT64_C(0), UINT64_C(0));
while (!zig_cmpxchg_weak(obj, expected, arg, zig_memory_order_seq_cst, zig_memory_order_seq_cst, i128, zig_i128));
}
#endif /* _M_IX86 */
#endif /* _MSC_VER && (_M_IX86 || _M_X64) */

View File

@ -4456,6 +4456,7 @@ fn globalVarDecl(
.align_inst = .none, // passed via the decls data
.init = init_inst,
.is_extern = false,
.is_const = !is_mutable,
.is_threadlocal = is_threadlocal,
});
break :vi var_inst;
@ -4474,6 +4475,7 @@ fn globalVarDecl(
.align_inst = .none, // passed via the decls data
.init = .none,
.is_extern = true,
.is_const = !is_mutable,
.is_threadlocal = is_threadlocal,
});
break :vi var_inst;
@ -11495,6 +11497,7 @@ const GenZir = struct {
var_type: Zir.Inst.Ref,
init: Zir.Inst.Ref,
is_extern: bool,
is_const: bool,
is_threadlocal: bool,
}) !Zir.Inst.Ref {
const astgen = gz.astgen;
@ -11533,6 +11536,7 @@ const GenZir = struct {
.has_align = args.align_inst != .none,
.has_init = args.init != .none,
.is_extern = args.is_extern,
.is_const = args.is_const,
.is_threadlocal = args.is_threadlocal,
}),
.operand = payload_index,

View File

@ -1005,11 +1005,11 @@ pub const Key = union(enum) {
ty: Index,
init: Index,
decl: DeclIndex,
lib_name: OptionalNullTerminatedString = .none,
is_extern: bool = false,
is_const: bool = false,
is_threadlocal: bool = false,
is_weak_linkage: bool = false,
lib_name: OptionalNullTerminatedString,
is_extern: bool,
is_const: bool,
is_threadlocal: bool,
is_weak_linkage: bool,
};
pub const ExternFunc = struct {

View File

@ -24760,7 +24760,9 @@ fn zirVarExtended(
.decl = sema.owner_decl_index,
.lib_name = try mod.intern_pool.getOrPutStringOpt(sema.gpa, lib_name),
.is_extern = small.is_extern,
.is_const = small.is_const,
.is_threadlocal = small.is_threadlocal,
.is_weak_linkage = false,
} })));
}
@ -25264,6 +25266,7 @@ fn zirBuiltinExtern(
if (options.linkage == .Weak and !ty.ptrAllowsZero(mod)) {
ty = try mod.optionalType(ty.toIntern());
}
const ptr_info = ty.ptrInfo(mod);
// TODO check duplicate extern
@ -25274,11 +25277,12 @@ fn zirBuiltinExtern(
{
const new_var = try mod.intern(.{ .variable = .{
.ty = ty.toIntern(),
.ty = ptr_info.child,
.init = .none,
.decl = sema.owner_decl_index,
.lib_name = options.library_name,
.is_extern = true,
.is_const = true,
.is_const = ptr_info.flags.is_const,
.is_threadlocal = options.is_thread_local,
.is_weak_linkage = options.linkage == .Weak,
} });
@ -25286,7 +25290,7 @@ fn zirBuiltinExtern(
new_decl.src_line = sema.owner_decl.src_line;
// We only access this decl through the decl_ref with the correct type created
// below, so this type doesn't matter
new_decl.ty = ty;
new_decl.ty = Type.fromInterned(ptr_info.child);
new_decl.val = Value.fromInterned(new_var);
new_decl.alignment = .none;
new_decl.@"linksection" = .none;

View File

@ -2610,8 +2610,9 @@ pub const Inst = struct {
has_align: bool,
has_init: bool,
is_extern: bool,
is_const: bool,
is_threadlocal: bool,
_: u11 = undefined,
_: u10 = undefined,
};
};

View File

@ -258,6 +258,20 @@ pub fn fmtIdent(ident: []const u8) std.fmt.Formatter(formatIdent) {
return .{ .data = ident };
}
// Returns true if `formatIdent` would make any edits to ident.
// This must be kept in sync with `formatIdent`.
pub fn isMangledIdent(ident: []const u8, solo: bool) bool {
if (solo and isReservedIdent(ident)) return true;
for (ident, 0..) |c, i| {
switch (c) {
'a'...'z', 'A'...'Z', '_' => {},
'0'...'9' => if (i == 0) return true,
else => return true,
}
}
return false;
}
/// This data is available when outputting .c code for a `InternPool.Index`
/// that corresponds to `func`.
/// It is not available when generating .h file.
@ -646,7 +660,7 @@ pub const DeclGen = struct {
if (decl.val.getExternFunc(mod)) |extern_func| if (extern_func.decl != decl_index)
return dg.renderDeclValue(writer, ty, val, extern_func.decl, location);
if (decl.val.getVariable(mod)) |variable| try dg.renderFwdDecl(decl_index, variable);
if (decl.val.getVariable(mod)) |variable| try dg.renderFwdDecl(decl_index, variable, .tentative);
// We shouldn't cast C function pointers as this is UB (when you call
// them). The analysis until now should ensure that the C function
@ -1593,7 +1607,7 @@ pub const DeclGen = struct {
kind: CType.Kind,
name: union(enum) {
export_index: u32,
string: []const u8,
ident: []const u8,
},
) !void {
const store = &dg.ctypes.set;
@ -1615,23 +1629,28 @@ pub const DeclGen = struct {
try w.writeAll("zig_cold ");
if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn ");
const trailing = try renderTypePrefix(dg.pass, store.*, mod, w, fn_cty_idx, .suffix, .{});
try w.print("{}", .{trailing});
var trailing = try renderTypePrefix(dg.pass, store.*, mod, w, fn_cty_idx, .suffix, .{});
if (toCallingConvention(fn_info.cc)) |call_conv| {
try w.print("zig_callconv({s}) ", .{call_conv});
try w.print("{}zig_callconv({s})", .{ trailing, call_conv });
trailing = .maybe_space;
}
switch (kind) {
.forward => {},
.complete => if (fn_info.alignment.toByteUnitsOptional()) |a|
try w.print(" zig_align_fn({})", .{a}),
.complete => if (fn_info.alignment.toByteUnitsOptional()) |a| {
try w.print("{}zig_align_fn({})", .{ trailing, a });
trailing = .maybe_space;
},
else => unreachable,
}
switch (name) {
.export_index => |export_index| try dg.renderDeclName(w, fn_decl_index, export_index),
.string => |string| try w.writeAll(string),
.export_index => |export_index| {
try w.print("{}", .{trailing});
try dg.renderDeclName(w, fn_decl_index, export_index);
},
.ident => |ident| try w.print("{}{ }", .{ trailing, fmtIdent(ident) }),
}
try renderTypeSuffix(
@ -1649,8 +1668,49 @@ pub const DeclGen = struct {
);
switch (kind) {
.forward => if (fn_info.alignment.toByteUnitsOptional()) |a|
try w.print(" zig_align_fn({})", .{a}),
.forward => {
if (fn_info.alignment.toByteUnitsOptional()) |a| {
try w.print(" zig_align_fn({})", .{a});
}
switch (name) {
.export_index => |export_index| mangled: {
const maybe_exports = mod.decl_exports.get(fn_decl_index);
const external_name = ip.stringToSlice(
if (maybe_exports) |exports|
exports.items[export_index].opts.name
else if (fn_decl.isExtern(mod))
fn_decl.name
else
break :mangled,
);
const is_mangled = isMangledIdent(external_name, true);
const is_export = export_index > 0;
if (is_mangled and is_export) {
try w.print(" zig_mangled_export({ }, {s}, {s})", .{
fmtIdent(external_name),
fmtStringLiteral(external_name, null),
fmtStringLiteral(
ip.stringToSlice(maybe_exports.?.items[0].opts.name),
null,
),
});
} else if (is_mangled) {
try w.print(" zig_mangled_final({ }, {s})", .{
fmtIdent(external_name), fmtStringLiteral(external_name, null),
});
} else if (is_export) {
try w.print(" zig_export({s}, {s})", .{
fmtStringLiteral(
ip.stringToSlice(maybe_exports.?.items[0].opts.name),
null,
),
fmtStringLiteral(external_name, null),
});
}
},
.ident => {},
}
},
.complete => {},
else => unreachable,
}
@ -1930,12 +1990,18 @@ pub const DeclGen = struct {
try dg.writeCValue(writer, member);
}
fn renderFwdDecl(dg: *DeclGen, decl_index: InternPool.DeclIndex, variable: InternPool.Key.Variable) !void {
fn renderFwdDecl(
dg: *DeclGen,
decl_index: InternPool.DeclIndex,
variable: InternPool.Key.Variable,
fwd_kind: enum { tentative, final },
) !void {
const decl = dg.module.declPtr(decl_index);
const fwd = dg.fwd_decl.writer();
const is_global = dg.declIsGlobal(.{ .ty = decl.ty, .val = decl.val }) or variable.is_extern;
const is_global = variable.is_extern or dg.declIsGlobal(.{ .ty = decl.ty, .val = decl.val });
try fwd.writeAll(if (is_global) "zig_extern " else "static ");
const export_weak_linkage = if (dg.module.decl_exports.get(decl_index)) |exports|
const maybe_exports = dg.module.decl_exports.get(decl_index);
const export_weak_linkage = if (maybe_exports) |exports|
exports.items[0].opts.linkage == .Weak
else
false;
@ -1949,6 +2015,21 @@ pub const DeclGen = struct {
decl.alignment,
.complete,
);
mangled: {
const external_name = dg.module.intern_pool.stringToSlice(if (maybe_exports) |exports|
exports.items[0].opts.name
else if (variable.is_extern)
decl.name
else
break :mangled);
if (isMangledIdent(external_name, true)) {
try fwd.print(" zig_mangled_{s}({ }, {s})", .{
@tagName(fwd_kind),
fmtIdent(external_name),
fmtStringLiteral(external_name, null),
});
}
}
try fwd.writeAll(";\n");
}
@ -1958,9 +2039,13 @@ pub const DeclGen = struct {
try mod.markDeclAlive(decl);
if (mod.decl_exports.get(decl_index)) |exports| {
try writer.print("{}", .{exports.items[export_index].opts.name.fmt(&mod.intern_pool)});
try writer.print("{ }", .{
fmtIdent(mod.intern_pool.stringToSlice(exports.items[export_index].opts.name)),
});
} else if (decl.getExternDecl(mod).unwrap()) |extern_decl_index| {
try writer.print("{}", .{mod.declPtr(extern_decl_index).name.fmt(&mod.intern_pool)});
try writer.print("{ }", .{
fmtIdent(mod.intern_pool.stringToSlice(mod.declPtr(extern_decl_index).name)),
});
} else {
// MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case),
// expand to 3x the length of its input, but let's cut it off at a much shorter limit.
@ -2592,7 +2677,10 @@ fn genExports(o: *Object) !void {
const mod = o.dg.module;
const ip = &mod.intern_pool;
const decl_index = o.dg.pass.decl;
const decl_index = switch (o.dg.pass) {
.decl => |decl| decl,
.anon, .flush => return,
};
const decl = mod.declPtr(decl_index);
const tv: TypedValue = .{ .ty = decl.ty, .val = Value.fromInterned((try decl.internValue(mod))) };
const fwd = o.dg.fwd_decl.writer();
@ -2600,42 +2688,50 @@ fn genExports(o: *Object) !void {
const exports = mod.decl_exports.get(decl_index) orelse return;
if (exports.items.len < 2) return;
switch (ip.indexToKey(tv.val.toIntern())) {
.func => {
for (exports.items[1..], 1..) |@"export", i| {
try fwd.writeAll("zig_export(");
if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage_fn ");
try o.dg.renderFunctionSignature(fwd, decl_index, .forward, .{ .export_index = @as(u32, @intCast(i)) });
try fwd.print(", {s}, {s});\n", .{
fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null),
fmtStringLiteral(ip.stringToSlice(@"export".opts.name), null),
});
}
const is_variable_const = switch (ip.indexToKey(tv.val.toIntern())) {
.func => return for (exports.items[1..], 1..) |@"export", i| {
try fwd.writeAll("zig_extern ");
if (@"export".opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage_fn ");
try o.dg.renderFunctionSignature(
fwd,
decl_index,
.forward,
.{ .export_index = @intCast(i) },
);
try fwd.writeAll(";\n");
},
.extern_func => {
// TODO: when sema allows re-exporting extern decls
unreachable;
},
.variable => |variable| {
for (exports.items[1..], 1..) |@"export", i| {
try fwd.writeAll("zig_export(");
if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage ");
const alias = ip.stringToSlice(@"export".opts.name);
try o.dg.renderTypeAndName(
fwd,
decl.ty,
.{ .identifier = alias },
CQualifiers.init(.{ .@"const" = variable.is_const }),
decl.alignment,
.complete,
);
try fwd.print(", {s}, {s});\n", .{
fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null),
fmtStringLiteral(alias, null),
});
}
},
else => {},
.variable => |variable| variable.is_const,
else => true,
};
for (exports.items[1..]) |@"export"| {
try fwd.writeAll("zig_extern ");
if (@"export".opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage ");
const export_name = ip.stringToSlice(@"export".opts.name);
try o.dg.renderTypeAndName(
fwd,
decl.ty,
.{ .identifier = export_name },
CQualifiers.init(.{ .@"const" = is_variable_const }),
decl.alignment,
.complete,
);
if (isMangledIdent(export_name, true)) {
try fwd.print(" zig_mangled_export({ }, {s}, {s})", .{
fmtIdent(export_name),
fmtStringLiteral(export_name, null),
fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null),
});
} else {
try fwd.print(" zig_export({s}, {s})", .{
fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null),
fmtStringLiteral(export_name, null),
});
}
try fwd.writeAll(";\n");
}
}
@ -2707,12 +2803,12 @@ pub fn genLazyFn(o: *Object, lazy_fn: LazyFnMap.Entry) !void {
fwd_decl_writer,
fn_decl_index,
.forward,
.{ .string = fn_name },
.{ .ident = fn_name },
);
try fwd_decl_writer.writeAll(";\n");
try w.print("static zig_{s} ", .{@tagName(key)});
try o.dg.renderFunctionSignature(w, fn_decl_index, .complete, .{ .string = fn_name });
try o.dg.renderFunctionSignature(w, fn_decl_index, .complete, .{ .ident = fn_name });
try w.writeAll(" {\n return ");
try o.dg.renderDeclName(w, fn_decl_index, 0);
try w.writeByte('(');
@ -2745,6 +2841,7 @@ pub fn genFunc(f: *Function) !void {
const is_global = o.dg.declIsGlobal(tv);
const fwd_decl_writer = o.dg.fwd_decl.writer();
try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");
if (mod.decl_exports.get(decl_index)) |exports|
if (exports.items[0].opts.linkage == .Weak) try fwd_decl_writer.writeAll("zig_weak_linkage_fn ");
try o.dg.renderFunctionSignature(fwd_decl_writer, decl_index, .forward, .{ .export_index = 0 });
@ -2831,12 +2928,12 @@ pub fn genDecl(o: *Object) !void {
try fwd_decl_writer.writeAll(";\n");
try genExports(o);
} else if (tv.val.getVariable(mod)) |variable| {
try o.dg.renderFwdDecl(decl_index, variable);
try o.dg.renderFwdDecl(decl_index, variable, .final);
try genExports(o);
if (variable.is_extern) return;
const is_global = o.dg.declIsGlobal(tv) or variable.is_extern;
const is_global = variable.is_extern or o.dg.declIsGlobal(tv);
const w = o.writer();
if (!is_global) try w.writeAll("static ");
if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage ");
@ -2853,7 +2950,7 @@ pub fn genDecl(o: *Object) !void {
} else {
const is_global = o.dg.module.decl_exports.contains(decl_index);
const decl_c_value = .{ .decl = decl_index };
return genDeclValue(o, tv, is_global, decl_c_value, decl.alignment, decl.@"linksection");
try genDeclValue(o, tv, is_global, decl_c_value, decl.alignment, decl.@"linksection");
}
}
@ -2870,10 +2967,26 @@ pub fn genDeclValue(
try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");
try o.dg.renderTypeAndName(fwd_decl_writer, tv.ty, decl_c_value, Const, alignment, .complete);
switch (o.dg.pass) {
.decl => |decl_index| {
if (mod.decl_exports.get(decl_index)) |exports| {
const export_name = mod.intern_pool.stringToSlice(exports.items[0].opts.name);
if (isMangledIdent(export_name, true)) {
try fwd_decl_writer.print(" zig_mangled_final({ }, {s})", .{
fmtIdent(export_name), fmtStringLiteral(export_name, null),
});
}
}
},
.anon => {},
.flush => unreachable,
}
try fwd_decl_writer.writeAll(";\n");
try genExports(o);
const w = o.writer();
if (!is_global) try w.writeAll("static ");
if (mod.intern_pool.stringToSliceUnwrap(link_section)) |s|
try w.print("zig_linksection(\"{s}\", ", .{s});
try o.dg.renderTypeAndName(w, tv.ty, decl_c_value, Const, alignment, .complete);
@ -2897,13 +3010,10 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void {
const writer = dg.fwd_decl.writer();
switch (tv.ty.zigTypeTag(mod)) {
.Fn => {
const is_global = dg.declIsGlobal(tv);
if (is_global) {
try writer.writeAll("zig_extern ");
try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 });
try dg.fwd_decl.appendSlice(";\n");
}
.Fn => if (dg.declIsGlobal(tv)) {
try writer.writeAll("zig_extern ");
try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 });
try dg.fwd_decl.appendSlice(";\n");
},
else => {},
}
@ -6895,9 +7005,9 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
try f.writeCValue(writer, accum, .Other);
switch (op) {
.float_op => |func| {
try writer.writeAll(" = zig_libc_name_");
try writer.writeAll(" = zig_float_fn_");
try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
try writer.print("({s})(", .{func.operation});
try writer.print("_{s}(", .{func.operation});
try f.writeCValue(writer, accum, .FunctionArgument);
try writer.writeAll(", ");
try f.writeCValue(writer, operand, .Other);
@ -7229,11 +7339,9 @@ fn unFloatOp(f: *Function, inst: Air.Inst.Index, operand: CValue, ty: Type, oper
const v = try Vectorize.start(f, inst, writer, ty);
try f.writeCValue(writer, local, .Other);
try v.elem(f, writer);
try writer.writeAll(" = zig_libc_name_");
try writer.writeAll(" = zig_float_fn_");
try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
try writer.writeByte('(');
try writer.writeAll(operation);
try writer.writeAll(")(");
try writer.print("_{s}(", .{operation});
try f.writeCValue(writer, operand, .FunctionArgument);
try v.elem(f, writer);
try writer.writeAll(");\n");
@ -7268,11 +7376,9 @@ fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVa
const v = try Vectorize.start(f, inst, writer, inst_ty);
try f.writeCValue(writer, local, .Other);
try v.elem(f, writer);
try writer.writeAll(" = zig_libc_name_");
try writer.writeAll(" = zig_float_fn_");
try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);
try writer.writeByte('(');
try writer.writeAll(operation);
try writer.writeAll(")(");
try writer.print("_{s}(", .{operation});
try f.writeCValue(writer, lhs, .FunctionArgument);
try v.elem(f, writer);
try writer.writeAll(", ");
@ -7302,9 +7408,9 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue {
const v = try Vectorize.start(f, inst, writer, inst_ty);
try f.writeCValue(writer, local, .Other);
try v.elem(f, writer);
try writer.writeAll(" = zig_libc_name_");
try writer.writeAll(" = zig_float_fn_");
try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);
try writer.writeAll("(fma)(");
try writer.writeAll("_fma(");
try f.writeCValue(writer, mulend1, .FunctionArgument);
try v.elem(f, writer);
try writer.writeAll(", ");
@ -7390,11 +7496,11 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue {
fn toMemoryOrder(order: std.builtin.AtomicOrder) [:0]const u8 {
return switch (order) {
// Note: unordered is actually even less atomic than relaxed
.Unordered, .Monotonic => "memory_order_relaxed",
.Acquire => "memory_order_acquire",
.Release => "memory_order_release",
.AcqRel => "memory_order_acq_rel",
.SeqCst => "memory_order_seq_cst",
.Unordered, .Monotonic => "zig_memory_order_relaxed",
.Acquire => "zig_memory_order_acquire",
.Release => "zig_memory_order_release",
.AcqRel => "zig_memory_order_acq_rel",
.SeqCst => "zig_memory_order_seq_cst",
};
}

View File

@ -110,7 +110,6 @@ test {
_ = @import("behavior/bugs/12551.zig");
_ = @import("behavior/bugs/12571.zig");
_ = @import("behavior/bugs/12644.zig");
_ = @import("behavior/bugs/12680.zig");
_ = @import("behavior/bugs/12723.zig");
_ = @import("behavior/bugs/12776.zig");
_ = @import("behavior/bugs/12786.zig");
@ -175,6 +174,7 @@ test {
_ = @import("behavior/hasfield.zig");
_ = @import("behavior/if.zig");
_ = @import("behavior/import.zig");
_ = @import("behavior/import_c_keywords.zig");
_ = @import("behavior/incomplete_struct_param_tld.zig");
_ = @import("behavior/inline_switch.zig");
_ = @import("behavior/int128.zig");
@ -251,9 +251,7 @@ test {
}
if (builtin.zig_backend != .stage2_arm and
builtin.zig_backend != .stage2_x86_64 and
builtin.zig_backend != .stage2_aarch64 and
builtin.zig_backend != .stage2_c and
builtin.zig_backend != .stage2_spirv64)
{
_ = @import("behavior/export_keyword.zig");

View File

@ -1,22 +0,0 @@
const std = @import("std");
const expectEqual = std.testing.expectEqual;
const other_file = @import("12680_other_file.zig");
const builtin = @import("builtin");
extern fn test_func() callconv(.C) usize;
test "export a function twice" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf) return error.SkipZigTest;
if (builtin.os.tag == .macos and builtin.zig_backend == .stage2_c) {
// TODO: test.c: error: aliases are not supported on darwin
return error.SkipZigTest;
}
// If it exports the function correctly, `test_func` and `testFunc` will points to the same address.
try expectEqual(test_func(), other_file.testFunc());
}

View File

@ -1,8 +0,0 @@
// export this function twice
pub export fn testFunc() callconv(.C) usize {
return @intFromPtr(&testFunc);
}
comptime {
@export(testFunc, .{ .name = "test_func", .linkage = .Strong });
}

View File

@ -0,0 +1,44 @@
pub const Id = enum(c_int) {
c_keyword_variable = 12,
non_c_keyword_variable = 34,
c_keyword_constant = 56,
non_c_keyword_constant = 78,
c_keyword_function = 910,
non_c_keyword_function = 1112,
};
export var int: Id = .c_keyword_variable;
export var some_non_c_keyword_variable: Id = .non_c_keyword_variable;
export const @"if": Id = .c_keyword_constant;
export const some_non_c_keyword_constant: Id = .non_c_keyword_constant;
export fn float() Id {
return .c_keyword_function;
}
export fn some_non_c_keyword_function() Id {
return .non_c_keyword_function;
}
comptime {
@export(int, .{ .name = "long" });
@export(int, .{ .name = "an_alias_of_int" });
@export(some_non_c_keyword_variable, .{ .name = "void" });
@export(some_non_c_keyword_variable, .{ .name = "an_alias_of_some_non_c_keyword_variable" });
@export(@"if", .{ .name = "else" });
@export(@"if", .{ .name = "an_alias_of_if" });
@export(some_non_c_keyword_constant, .{ .name = "switch" });
@export(some_non_c_keyword_constant, .{ .name = "an_alias_of_some_non_c_keyword_constant" });
@export(float, .{ .name = "double" });
@export(float, .{ .name = "an_alias_of_float" });
@export(some_non_c_keyword_function, .{ .name = "break" });
@export(some_non_c_keyword_function, .{ .name = "an_alias_of_some_non_c_keyword_function" });
}

View File

@ -0,0 +1,92 @@
const builtin = @import("builtin");
const Id = @import("export_c_keywords.zig").Id;
const std = @import("std");
extern var int: Id;
extern var long: Id;
extern var an_alias_of_int: Id;
extern var some_non_c_keyword_variable: Id;
extern var @"void": Id;
extern var an_alias_of_some_non_c_keyword_variable: Id;
extern const @"if": Id;
extern const @"else": Id;
extern const an_alias_of_if: Id;
extern const some_non_c_keyword_constant: Id;
extern const @"switch": Id;
extern const an_alias_of_some_non_c_keyword_constant: Id;
extern fn float() Id;
extern fn double() Id;
extern fn an_alias_of_float() Id;
extern fn some_non_c_keyword_function() Id;
extern fn @"break"() Id;
extern fn an_alias_of_some_non_c_keyword_function() Id;
test "import c keywords" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf) return error.SkipZigTest;
try std.testing.expect(int == .c_keyword_variable);
try std.testing.expect(long == .c_keyword_variable);
try std.testing.expect(an_alias_of_int == .c_keyword_variable);
try std.testing.expect(some_non_c_keyword_variable == .non_c_keyword_variable);
try std.testing.expect(@"void" == .non_c_keyword_variable);
try std.testing.expect(an_alias_of_some_non_c_keyword_variable == .non_c_keyword_variable);
try std.testing.expect(@"if" == .c_keyword_constant);
try std.testing.expect(@"else" == .c_keyword_constant);
try std.testing.expect(an_alias_of_if == .c_keyword_constant);
try std.testing.expect(some_non_c_keyword_constant == .non_c_keyword_constant);
try std.testing.expect(@"switch" == .non_c_keyword_constant);
try std.testing.expect(an_alias_of_some_non_c_keyword_constant == .non_c_keyword_constant);
try std.testing.expect(float() == .c_keyword_function);
try std.testing.expect(double() == .c_keyword_function);
try std.testing.expect(an_alias_of_float() == .c_keyword_function);
try std.testing.expect(some_non_c_keyword_function() == .non_c_keyword_function);
try std.testing.expect(@"break"() == .non_c_keyword_function);
try std.testing.expect(an_alias_of_some_non_c_keyword_function() == .non_c_keyword_function);
var ptr_id: *const Id = &long;
try std.testing.expect(ptr_id == &int);
ptr_id = &an_alias_of_int;
try std.testing.expect(ptr_id == &int);
ptr_id = &@"void";
try std.testing.expect(ptr_id == &some_non_c_keyword_variable);
ptr_id = &an_alias_of_some_non_c_keyword_variable;
try std.testing.expect(ptr_id == &some_non_c_keyword_variable);
ptr_id = &@"else";
try std.testing.expect(ptr_id == &@"if");
ptr_id = &an_alias_of_if;
try std.testing.expect(ptr_id == &@"if");
ptr_id = &@"switch";
try std.testing.expect(ptr_id == &some_non_c_keyword_constant);
ptr_id = &an_alias_of_some_non_c_keyword_constant;
try std.testing.expect(ptr_id == &some_non_c_keyword_constant);
if (builtin.target.ofmt != .coff and builtin.target.os.tag != .windows) {
var ptr_fn: *const fn () callconv(.C) Id = &double;
try std.testing.expect(ptr_fn == &float);
ptr_fn = &an_alias_of_float;
try std.testing.expect(ptr_fn == &float);
ptr_fn = &@"break";
try std.testing.expect(ptr_fn == &some_non_c_keyword_function);
ptr_fn = &an_alias_of_some_non_c_keyword_function;
try std.testing.expect(ptr_fn == &some_non_c_keyword_function);
}
}