authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 15:36:16-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-01-01 15:36:16-08:00
log7161ed79c4abcaccdd56fe0b4fbd3d93472d41b8
tree0844cb58bfcb9ac48fc517738c0b78ff677007ab
parent3f2a65594e1d3c0a4f4943a4ea522e8405db81e0
parent50e2fb8fd0e1aae5fc3123d6f26d7420d4baa41c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #17824 from kcbanner/fixup_msvc_fmax

cbe: add a system for avoiding collisions with C compiler intrinsics

7 files changed, 185 insertions(+), 92 deletions(-)

ci/x86_64-windows-debug.ps1+5-6
...@@ -96,9 +96,8 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files\Microsoft Visual Studio\2022\E...@@ -96,9 +96,8 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files\Microsoft Visual Studio\2022\E
96CheckLastExitCode96CheckLastExitCode
9797
98Write-Output "Build and run behavior tests with msvc..."98Write-Output "Build and run behavior tests with msvc..."
99Write-Output "Skipped due to https://github.com/ziglang/zig/issues/17817"99& 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
100#& 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.lib100CheckLastExitCode
101#CheckLastExitCode101
102#102& .\test-x86_64-windows-msvc.exe
103#& .\test-x86_64-windows-msvc.exe103CheckLastExitCode
104#CheckLastExitCode
ci/x86_64-windows-release.ps1+5-6
...@@ -95,9 +95,8 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files\Microsoft Visual Studio\2022\E...@@ -95,9 +95,8 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files\Microsoft Visual Studio\2022\E
95CheckLastExitCode95CheckLastExitCode
9696
97Write-Output "Build and run behavior tests with msvc..."97Write-Output "Build and run behavior tests with msvc..."
98Write-Output "Skipped due to https://github.com/ziglang/zig/issues/17817"98& 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
99#& 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.lib99CheckLastExitCode
100#CheckLastExitCode100
101#101& .\test-x86_64-windows-msvc.exe
102#& .\test-x86_64-windows-msvc.exe102CheckLastExitCode
103#CheckLastExitCode
lib/std/math.zig+1
...@@ -1266,6 +1266,7 @@ pub fn lerp(a: anytype, b: anytype, t: anytype) @TypeOf(a, b, t) {...@@ -1266,6 +1266,7 @@ pub fn lerp(a: anytype, b: anytype, t: anytype) @TypeOf(a, b, t) {
1266}1266}
12671267
1268test "lerp" {1268test "lerp" {
1269 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/17884
1269 if (builtin.zig_backend == .stage2_x86_64 and1270 if (builtin.zig_backend == .stage2_x86_64 and
1270 !comptime std.Target.x86.featureSetHas(builtin.cpu.features, .fma)) return error.SkipZigTest;1271 !comptime std.Target.x86.featureSetHas(builtin.cpu.features, .fma)) return error.SkipZigTest;
12711272
lib/zig.h+70-32
...@@ -112,7 +112,7 @@ typedef char bool;...@@ -112,7 +112,7 @@ typedef char bool;
112#define zig_never_tail zig_never_tail_unavailable112#define zig_never_tail zig_never_tail_unavailable
113#endif113#endif
114114
115#if zig_has_attribute(always_inline)115#if zig_has_attribute(musttail)
116#define zig_always_tail __attribute__((musttail))116#define zig_always_tail __attribute__((musttail))
117#else117#else
118#define zig_always_tail zig_always_tail_unavailable118#define zig_always_tail zig_always_tail_unavailable
...@@ -180,20 +180,56 @@ typedef char bool;...@@ -180,20 +180,56 @@ typedef char bool;
180#define zig_extern extern180#define zig_extern extern
181#endif181#endif
182182
183#if zig_has_attribute(alias)183#if _MSC_VER
184#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(symbol)))184#define zig_extern_mangled zig_extern
185#elif _MSC_VER185#else
186#if zig_has_attribute(visibility)
187#define zig_extern_mangled zig_extern __attribute__((visibility("hidden")))
188#else
189#define zig_extern_mangled zig_extern
190#endif
191#endif
192
193#if _MSC_VER
186#if _M_X64194#if _M_X64
187#define zig_export(sig, symbol, name) sig;\195#define zig_export(sig, symbol, name) zig_extern sig;\
188 __pragma(comment(linker, "/alternatename:" name "=" symbol ))196 __pragma(comment(linker, "/alternatename:" name "=" #symbol ))
189#else /*_M_X64 */197#else /*_M_X64 */
190#define zig_export(sig, symbol, name) sig;\198#define zig_export(sig, symbol, name) zig_extern sig;\
191 __pragma(comment(linker, "/alternatename:_" name "=_" symbol ))199 __pragma(comment(linker, "/alternatename:" name "=" #symbol ))
192#endif /*_M_X64 */200#endif /*_M_X64 */
193#else201#else /* _MSC_VER */
194#define zig_export(sig, symbol, name) __asm(name " = " symbol)202#if __APPLE__
203#define zig_export(sig, symbol, name) zig_extern sig;\
204 __asm("_" name " = _" #symbol)
205#else /* __APPLE__ */
206#define zig_export(sig, symbol, name) zig_extern sig;\
207 __asm(name " = " #symbol)
208#endif /* __APPLE__ */
209#endif /* _MSC_VER */
210
211#if _MSC_VER
212#if _M_X64
213#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args;\
214 __pragma(comment(linker, "/alternatename:" #fn_name "=" #libc_name ));
215#else /*_M_X64 */
216#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args;\
217 __pragma(comment(linker, "/alternatename:_" #fn_name "=_" #libc_name ));
218#endif /*_M_X64 */
219#define zig_import_builtin(Type, fn_name, libc_name, sig_args, call_args) zig_import(Type, fn_name, sig_args, call_args)
220#else /* _MSC_VER */
221#if __APPLE__
222#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args __asm("_" #libc_name);
223#else /* __APPLE__ */
224#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args __asm(#libc_name);
225#endif /* __APPLE__ */
226#define zig_import_builtin(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type libc_name sig_args; \
227 static inline Type fn_name sig_args { return libc_name call_args; }
195#endif228#endif
196229
230#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)
231#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)
232
197#if zig_has_attribute(weak) || defined(zig_gnuc)233#if zig_has_attribute(weak) || defined(zig_gnuc)
198#define zig_weak_linkage __attribute__((weak))234#define zig_weak_linkage __attribute__((weak))
199#define zig_weak_linkage_fn __attribute__((weak))235#define zig_weak_linkage_fn __attribute__((weak))
...@@ -3093,6 +3129,7 @@ ypedef uint32_t zig_f32;...@@ -3093,6 +3129,7 @@ ypedef uint32_t zig_f32;
30933129
3094#define zig_has_f64 13130#define zig_has_f64 1
3095#define zig_libc_name_f64(name) name3131#define zig_libc_name_f64(name) name
3132
3096#if _MSC_VER3133#if _MSC_VER
3097#define zig_init_special_f64(sign, name, arg, repr) sign zig_make_f64(zig_msvc_flt_##name, )3134#define zig_init_special_f64(sign, name, arg, repr) sign zig_make_f64(zig_msvc_flt_##name, )
3098#else3135#else
...@@ -3318,6 +3355,7 @@ zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0)))...@@ -3318,6 +3355,7 @@ zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0)))
3318 return lhs operator rhs; \3355 return lhs operator rhs; \
3319 }3356 }
33203357
3358#define zig_expand_has_builtin(b) zig_has_builtin(b)
3321#define zig_common_float_builtins(w) \3359#define zig_common_float_builtins(w) \
3322 zig_convert_builtin( int64_t, int64_t, fix, zig_f##w, zig_f##w, ) \3360 zig_convert_builtin( int64_t, int64_t, fix, zig_f##w, zig_f##w, ) \
3323 zig_convert_builtin(zig_i128, zig_i128, fix, zig_f##w, zig_f##w, ) \3361 zig_convert_builtin(zig_i128, zig_i128, fix, zig_f##w, zig_f##w, ) \
...@@ -3336,31 +3374,31 @@ zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0)))...@@ -3336,31 +3374,31 @@ zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0)))
3336 zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, sub, -) \3374 zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, sub, -) \
3337 zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, mul, *) \3375 zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, mul, *) \
3338 zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, div, /) \3376 zig_expand_concat(zig_float_binary_builtin_, zig_has_f##w)(f##w, div, /) \
3339 zig_extern zig_f##w zig_libc_name_f##w(sqrt)(zig_f##w); \3377 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)) \
3340 zig_extern zig_f##w zig_libc_name_f##w(sin)(zig_f##w); \3378 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)) \
3341 zig_extern zig_f##w zig_libc_name_f##w(cos)(zig_f##w); \3379 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)) \
3342 zig_extern zig_f##w zig_libc_name_f##w(tan)(zig_f##w); \3380 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)) \
3343 zig_extern zig_f##w zig_libc_name_f##w(exp)(zig_f##w); \3381 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)) \
3344 zig_extern zig_f##w zig_libc_name_f##w(exp2)(zig_f##w); \3382 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)) \
3345 zig_extern zig_f##w zig_libc_name_f##w(log)(zig_f##w); \3383 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)) \
3346 zig_extern zig_f##w zig_libc_name_f##w(log2)(zig_f##w); \3384 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)) \
3347 zig_extern zig_f##w zig_libc_name_f##w(log10)(zig_f##w); \3385 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)) \
3348 zig_extern zig_f##w zig_libc_name_f##w(fabs)(zig_f##w); \3386 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)) \
3349 zig_extern zig_f##w zig_libc_name_f##w(floor)(zig_f##w); \3387 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)) \
3350 zig_extern zig_f##w zig_libc_name_f##w(ceil)(zig_f##w); \3388 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)) \
3351 zig_extern zig_f##w zig_libc_name_f##w(round)(zig_f##w); \3389 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)) \
3352 zig_extern zig_f##w zig_libc_name_f##w(trunc)(zig_f##w); \3390 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)) \
3353 zig_extern zig_f##w zig_libc_name_f##w(fmod)(zig_f##w, zig_f##w); \3391 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)) \
3354 zig_extern zig_f##w zig_libc_name_f##w(fmin)(zig_f##w, zig_f##w); \3392 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)) \
3355 zig_extern zig_f##w zig_libc_name_f##w(fmax)(zig_f##w, zig_f##w); \3393 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)) \
3356 zig_extern zig_f##w zig_libc_name_f##w(fma)(zig_f##w, zig_f##w, zig_f##w); \3394 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)) \
3357\3395\
3358 static inline zig_f##w zig_div_trunc_f##w(zig_f##w lhs, zig_f##w rhs) { \3396 static inline zig_f##w zig_div_trunc_f##w(zig_f##w lhs, zig_f##w rhs) { \
3359 return zig_libc_name_f##w(trunc)(zig_div_f##w(lhs, rhs)); \3397 return zig_float_fn_f##w##_trunc(zig_div_f##w(lhs, rhs)); \
3360 } \3398 } \
3361\3399\
3362 static inline zig_f##w zig_div_floor_f##w(zig_f##w lhs, zig_f##w rhs) { \3400 static inline zig_f##w zig_div_floor_f##w(zig_f##w lhs, zig_f##w rhs) { \
3363 return zig_libc_name_f##w(floor)(zig_div_f##w(lhs, rhs)); \3401 return zig_float_fn_f##w##_floor(zig_div_f##w(lhs, rhs)); \
3364 } \3402 } \
3365\3403\
3366 static inline zig_f##w zig_mod_f##w(zig_f##w lhs, zig_f##w rhs) { \3404 static inline zig_f##w zig_mod_f##w(zig_f##w lhs, zig_f##w rhs) { \
...@@ -3464,7 +3502,7 @@ zig_float_builtins(64)...@@ -3464,7 +3502,7 @@ zig_float_builtins(64)
3464 zig_##Type zig_atomicrmw_desired; \3502 zig_##Type zig_atomicrmw_desired; \
3465 zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \3503 zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
3466 do { \3504 do { \
3467 zig_atomicrmw_desired = zig_libc_name_##Type(fmin)(zig_atomicrmw_expected, arg); \3505 zig_atomicrmw_desired = zig_float_fn_##Type##_fmin(zig_atomicrmw_expected, arg); \
3468 } while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \3506 } while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
3469 res = zig_atomicrmw_expected; \3507 res = zig_atomicrmw_expected; \
3470} while (0)3508} while (0)
...@@ -3473,7 +3511,7 @@ zig_float_builtins(64)...@@ -3473,7 +3511,7 @@ zig_float_builtins(64)
3473 zig_##Type zig_atomicrmw_desired; \3511 zig_##Type zig_atomicrmw_desired; \
3474 zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \3512 zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \
3475 do { \3513 do { \
3476 zig_atomicrmw_desired = zig_libc_name_##Type(fmax)(zig_atomicrmw_expected, arg); \3514 zig_atomicrmw_desired = zig_float_fn_##Type##_fmax(zig_atomicrmw_expected, arg); \
3477 } while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \3515 } while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \
3478 res = zig_atomicrmw_expected; \3516 res = zig_atomicrmw_expected; \
3479} while (0)3517} while (0)
src/codegen/c.zig+103-42
...@@ -258,6 +258,42 @@ pub fn fmtIdent(ident: []const u8) std.fmt.Formatter(formatIdent) {...@@ -258,6 +258,42 @@ pub fn fmtIdent(ident: []const u8) std.fmt.Formatter(formatIdent) {
258 return .{ .data = ident };258 return .{ .data = ident };
259}259}
260260
261// Returns true if `formatIdent` would make any edits to ident.
262// This must be kept in sync with `formatIdent`.
263pub fn isMangledIdent(ident: []const u8, solo: bool) bool {
264 if (solo and isReservedIdent(ident)) return true;
265 for (ident, 0..) |c, i| {
266 switch (c) {
267 'a'...'z', 'A'...'Z', '_' => {},
268 '0'...'9' => if (i == 0) return true,
269 else => return true,
270 }
271 }
272 return false;
273}
274
275const DeclVisibility = enum {
276 global,
277 global_mangled,
278 local,
279
280 fn renderFwd(visibility: DeclVisibility, w: anytype) !void {
281 try w.writeAll(switch (visibility) {
282 .global => "zig_extern ",
283 // MSVC doesn't support exporting `static` functions, so they need special treatment
284 .global_mangled => "zig_extern_mangled ",
285 .local => "static ",
286 });
287 }
288
289 fn renderDef(visibility: DeclVisibility, w: anytype) !void {
290 return switch (visibility) {
291 .global => {},
292 else => visibility.renderFwd(w),
293 };
294 }
295};
296
261/// This data is available when outputting .c code for a `InternPool.Index`297/// This data is available when outputting .c code for a `InternPool.Index`
262/// that corresponds to `func`.298/// that corresponds to `func`.
263/// It is not available when generating .h file.299/// It is not available when generating .h file.
...@@ -530,6 +566,7 @@ pub const DeclGen = struct {...@@ -530,6 +566,7 @@ pub const DeclGen = struct {
530 is_naked_fn: bool,566 is_naked_fn: bool,
531 /// This is a borrowed reference from `link.C`.567 /// This is a borrowed reference from `link.C`.
532 fwd_decl: std.ArrayList(u8),568 fwd_decl: std.ArrayList(u8),
569
533 error_msg: ?*Module.ErrorMsg,570 error_msg: ?*Module.ErrorMsg,
534 ctypes: CType.Store,571 ctypes: CType.Store,
535 /// Keeps track of anonymous decls that need to be rendered before this572 /// Keeps track of anonymous decls that need to be rendered before this
...@@ -1631,7 +1668,7 @@ pub const DeclGen = struct {...@@ -1631,7 +1668,7 @@ pub const DeclGen = struct {
16311668
1632 switch (name) {1669 switch (name) {
1633 .export_index => |export_index| try dg.renderDeclName(w, fn_decl_index, export_index),1670 .export_index => |export_index| try dg.renderDeclName(w, fn_decl_index, export_index),
1634 .string => |string| try w.writeAll(string),1671 .string => |string| try w.print("{ }", .{fmtIdent(string)}),
1635 }1672 }
16361673
1637 try renderTypeSuffix(1674 try renderTypeSuffix(
...@@ -1843,12 +1880,26 @@ pub const DeclGen = struct {...@@ -1843,12 +1880,26 @@ pub const DeclGen = struct {
1843 try renderTypeSuffix(dg.pass, store.*, mod, w, cty_idx, .suffix, .{});1880 try renderTypeSuffix(dg.pass, store.*, mod, w, cty_idx, .suffix, .{});
1844 }1881 }
18451882
1846 fn declIsGlobal(dg: *DeclGen, tv: TypedValue) bool {1883 fn declVisibility(dg: *DeclGen, tv: TypedValue) DeclVisibility {
1847 const mod = dg.module;1884 const mod = dg.module;
1848 return switch (mod.intern_pool.indexToKey(tv.val.ip_index)) {1885 return switch (mod.intern_pool.indexToKey(tv.val.ip_index)) {
1849 .variable => |variable| mod.decl_exports.contains(variable.decl),1886 .variable => |variable| {
1850 .extern_func => true,1887 if (mod.decl_exports.get(variable.decl)) |exports| {
1851 .func => |func| mod.decl_exports.contains(func.owner_decl),1888 return if (isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true))
1889 .global_mangled
1890 else
1891 .global;
1892 } else return .local;
1893 },
1894 .extern_func => .global,
1895 .func => |func| {
1896 if (mod.decl_exports.get(func.owner_decl)) |exports| {
1897 return if (isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true))
1898 .global_mangled
1899 else
1900 .global;
1901 } else return .local;
1902 },
1852 else => unreachable,1903 else => unreachable,
1853 };1904 };
1854 }1905 }
...@@ -1933,8 +1984,8 @@ pub const DeclGen = struct {...@@ -1933,8 +1984,8 @@ pub const DeclGen = struct {
1933 fn renderFwdDecl(dg: *DeclGen, decl_index: InternPool.DeclIndex, variable: InternPool.Key.Variable) !void {1984 fn renderFwdDecl(dg: *DeclGen, decl_index: InternPool.DeclIndex, variable: InternPool.Key.Variable) !void {
1934 const decl = dg.module.declPtr(decl_index);1985 const decl = dg.module.declPtr(decl_index);
1935 const fwd = dg.fwd_decl.writer();1986 const fwd = dg.fwd_decl.writer();
1936 const is_global = dg.declIsGlobal(.{ .ty = decl.ty, .val = decl.val }) or variable.is_extern;1987 const visibility = if (variable.is_extern) .global else dg.declVisibility(.{ .ty = decl.ty, .val = decl.val });
1937 try fwd.writeAll(if (is_global) "zig_extern " else "static ");1988 try visibility.renderFwd(fwd);
1938 const export_weak_linkage = if (dg.module.decl_exports.get(decl_index)) |exports|1989 const export_weak_linkage = if (dg.module.decl_exports.get(decl_index)) |exports|
1939 exports.items[0].opts.linkage == .Weak1990 exports.items[0].opts.linkage == .Weak
1940 else1991 else
...@@ -1958,7 +2009,7 @@ pub const DeclGen = struct {...@@ -1958,7 +2009,7 @@ pub const DeclGen = struct {
1958 try mod.markDeclAlive(decl);2009 try mod.markDeclAlive(decl);
19592010
1960 if (mod.decl_exports.get(decl_index)) |exports| {2011 if (mod.decl_exports.get(decl_index)) |exports| {
1961 try writer.print("{}", .{exports.items[export_index].opts.name.fmt(&mod.intern_pool)});2012 try writer.print("{ }", .{fmtIdent(mod.intern_pool.stringToSlice(exports.items[export_index].opts.name))});
1962 } else if (decl.getExternDecl(mod).unwrap()) |extern_decl_index| {2013 } else if (decl.getExternDecl(mod).unwrap()) |extern_decl_index| {
1963 try writer.print("{}", .{mod.declPtr(extern_decl_index).name.fmt(&mod.intern_pool)});2014 try writer.print("{}", .{mod.declPtr(extern_decl_index).name.fmt(&mod.intern_pool)});
1964 } else {2015 } else {
...@@ -2598,16 +2649,19 @@ fn genExports(o: *Object) !void {...@@ -2598,16 +2649,19 @@ fn genExports(o: *Object) !void {
2598 const fwd = o.dg.fwd_decl.writer();2649 const fwd = o.dg.fwd_decl.writer();
25992650
2600 const exports = mod.decl_exports.get(decl_index) orelse return;2651 const exports = mod.decl_exports.get(decl_index) orelse return;
2601 if (exports.items.len < 2) return;2652
2653 const is_mangled = isMangledIdent(ip.stringToSlice(exports.items[0].opts.name), true);
2654 if (exports.items.len < 2 and !is_mangled) return;
26022655
2603 switch (ip.indexToKey(tv.val.toIntern())) {2656 switch (ip.indexToKey(tv.val.toIntern())) {
2604 .func => {2657 .func => {
2605 for (exports.items[1..], 1..) |@"export", i| {2658 const start_i = 1 - @intFromBool(is_mangled);
2659 for (exports.items[start_i..], start_i..) |@"export", i| {
2606 try fwd.writeAll("zig_export(");2660 try fwd.writeAll("zig_export(");
2607 if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage_fn ");2661 if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage_fn ");
2608 try o.dg.renderFunctionSignature(fwd, decl_index, .forward, .{ .export_index = @as(u32, @intCast(i)) });2662 try o.dg.renderFunctionSignature(fwd, decl_index, .forward, .{ .export_index = @as(u32, @intCast(i)) });
2609 try fwd.print(", {s}, {s});\n", .{2663 try fwd.print(", { }, {s});\n", .{
2610 fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null),2664 fmtIdent(ip.stringToSlice(exports.items[0].opts.name)),
2611 fmtStringLiteral(ip.stringToSlice(@"export".opts.name), null),2665 fmtStringLiteral(ip.stringToSlice(@"export".opts.name), null),
2612 });2666 });
2613 }2667 }
...@@ -2617,7 +2671,8 @@ fn genExports(o: *Object) !void {...@@ -2617,7 +2671,8 @@ fn genExports(o: *Object) !void {
2617 unreachable;2671 unreachable;
2618 },2672 },
2619 .variable => |variable| {2673 .variable => |variable| {
2620 for (exports.items[1..], 1..) |@"export", i| {2674 const start_i = 1 - @intFromBool(is_mangled);
2675 for (exports.items[start_i..], start_i..) |@"export", i| {
2621 try fwd.writeAll("zig_export(");2676 try fwd.writeAll("zig_export(");
2622 if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage ");2677 if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage ");
2623 const alias = ip.stringToSlice(@"export".opts.name);2678 const alias = ip.stringToSlice(@"export".opts.name);
...@@ -2629,8 +2684,8 @@ fn genExports(o: *Object) !void {...@@ -2629,8 +2684,8 @@ fn genExports(o: *Object) !void {
2629 decl.alignment,2684 decl.alignment,
2630 .complete,2685 .complete,
2631 );2686 );
2632 try fwd.print(", {s}, {s});\n", .{2687 try fwd.print(", { }, {s});\n", .{
2633 fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null),2688 fmtIdent(ip.stringToSlice(exports.items[0].opts.name)),
2634 fmtStringLiteral(alias, null),2689 fmtStringLiteral(alias, null),
2635 });2690 });
2636 }2691 }
...@@ -2742,9 +2797,10 @@ pub fn genFunc(f: *Function) !void {...@@ -2742,9 +2797,10 @@ pub fn genFunc(f: *Function) !void {
2742 o.code_header = std.ArrayList(u8).init(gpa);2797 o.code_header = std.ArrayList(u8).init(gpa);
2743 defer o.code_header.deinit();2798 defer o.code_header.deinit();
27442799
2745 const is_global = o.dg.declIsGlobal(tv);2800 const visibility = o.dg.declVisibility(tv);
2746 const fwd_decl_writer = o.dg.fwd_decl.writer();2801 const fwd_decl_writer = o.dg.fwd_decl.writer();
2747 try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");2802 try visibility.renderFwd(fwd_decl_writer);
2803
2748 if (mod.decl_exports.get(decl_index)) |exports|2804 if (mod.decl_exports.get(decl_index)) |exports|
2749 if (exports.items[0].opts.linkage == .Weak) try fwd_decl_writer.writeAll("zig_weak_linkage_fn ");2805 if (exports.items[0].opts.linkage == .Weak) try fwd_decl_writer.writeAll("zig_weak_linkage_fn ");
2750 try o.dg.renderFunctionSignature(fwd_decl_writer, decl_index, .forward, .{ .export_index = 0 });2806 try o.dg.renderFunctionSignature(fwd_decl_writer, decl_index, .forward, .{ .export_index = 0 });
...@@ -2752,7 +2808,7 @@ pub fn genFunc(f: *Function) !void {...@@ -2752,7 +2808,7 @@ pub fn genFunc(f: *Function) !void {
2752 try genExports(o);2808 try genExports(o);
27532809
2754 try o.indent_writer.insertNewline();2810 try o.indent_writer.insertNewline();
2755 if (!is_global) try o.writer().writeAll("static ");2811 try visibility.renderDef(o.writer());
2756 try o.dg.renderFunctionSignature(o.writer(), decl_index, .complete, .{ .export_index = 0 });2812 try o.dg.renderFunctionSignature(o.writer(), decl_index, .complete, .{ .export_index = 0 });
2757 try o.writer().writeByte(' ');2813 try o.writer().writeByte(' ');
27582814
...@@ -2836,9 +2892,9 @@ pub fn genDecl(o: *Object) !void {...@@ -2836,9 +2892,9 @@ pub fn genDecl(o: *Object) !void {
28362892
2837 if (variable.is_extern) return;2893 if (variable.is_extern) return;
28382894
2839 const is_global = o.dg.declIsGlobal(tv) or variable.is_extern;2895 const visibility = if (variable.is_extern) .global else o.dg.declVisibility(tv);
2840 const w = o.writer();2896 const w = o.writer();
2841 if (!is_global) try w.writeAll("static ");2897 try visibility.renderDef(w);
2842 if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage ");2898 if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage ");
2843 if (variable.is_threadlocal) try w.writeAll("zig_threadlocal ");2899 if (variable.is_threadlocal) try w.writeAll("zig_threadlocal ");
2844 if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s|2900 if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s|
...@@ -2851,16 +2907,21 @@ pub fn genDecl(o: *Object) !void {...@@ -2851,16 +2907,21 @@ pub fn genDecl(o: *Object) !void {
2851 try w.writeByte(';');2907 try w.writeByte(';');
2852 try o.indent_writer.insertNewline();2908 try o.indent_writer.insertNewline();
2853 } else {2909 } else {
2854 const is_global = o.dg.module.decl_exports.contains(decl_index);2910 const visibility: DeclVisibility = if (o.dg.module.decl_exports.get(decl_index)) |exports| b: {
2911 break :b if (isMangledIdent(o.dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true))
2912 .global_mangled
2913 else
2914 .global;
2915 } else .local;
2855 const decl_c_value = .{ .decl = decl_index };2916 const decl_c_value = .{ .decl = decl_index };
2856 return genDeclValue(o, tv, is_global, decl_c_value, decl.alignment, decl.@"linksection");2917 return genDeclValue(o, tv, visibility, decl_c_value, decl.alignment, decl.@"linksection");
2857 }2918 }
2858}2919}
28592920
2860pub fn genDeclValue(2921pub fn genDeclValue(
2861 o: *Object,2922 o: *Object,
2862 tv: TypedValue,2923 tv: TypedValue,
2863 is_global: bool,2924 visibility: DeclVisibility,
2864 decl_c_value: CValue,2925 decl_c_value: CValue,
2865 alignment: Alignment,2926 alignment: Alignment,
2866 link_section: InternPool.OptionalNullTerminatedString,2927 link_section: InternPool.OptionalNullTerminatedString,
...@@ -2868,12 +2929,13 @@ pub fn genDeclValue(...@@ -2868,12 +2929,13 @@ pub fn genDeclValue(
2868 const mod = o.dg.module;2929 const mod = o.dg.module;
2869 const fwd_decl_writer = o.dg.fwd_decl.writer();2930 const fwd_decl_writer = o.dg.fwd_decl.writer();
28702931
2871 try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");2932 try visibility.renderFwd(fwd_decl_writer);
2872 try o.dg.renderTypeAndName(fwd_decl_writer, tv.ty, decl_c_value, Const, alignment, .complete);2933 try o.dg.renderTypeAndName(fwd_decl_writer, tv.ty, decl_c_value, Const, alignment, .complete);
2873 try fwd_decl_writer.writeAll(";\n");2934 try fwd_decl_writer.writeAll(";\n");
28742935
2875 const w = o.writer();2936 const w = o.writer();
2876 if (!is_global) try w.writeAll("static ");2937 try visibility.renderDef(w);
2938
2877 if (mod.intern_pool.stringToSliceUnwrap(link_section)) |s|2939 if (mod.intern_pool.stringToSliceUnwrap(link_section)) |s|
2878 try w.print("zig_linksection(\"{s}\", ", .{s});2940 try w.print("zig_linksection(\"{s}\", ", .{s});
2879 try o.dg.renderTypeAndName(w, tv.ty, decl_c_value, Const, alignment, .complete);2941 try o.dg.renderTypeAndName(w, tv.ty, decl_c_value, Const, alignment, .complete);
...@@ -2898,11 +2960,14 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void {...@@ -2898,11 +2960,14 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void {
28982960
2899 switch (tv.ty.zigTypeTag(mod)) {2961 switch (tv.ty.zigTypeTag(mod)) {
2900 .Fn => {2962 .Fn => {
2901 const is_global = dg.declIsGlobal(tv);2963 const visibility = dg.declVisibility(tv);
2902 if (is_global) {2964 switch (visibility) {
2903 try writer.writeAll("zig_extern ");2965 .global, .global_mangled => {
2904 try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 });2966 try visibility.renderFwd(writer);
2905 try dg.fwd_decl.appendSlice(";\n");2967 try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 });
2968 try dg.fwd_decl.appendSlice(";\n");
2969 },
2970 .local => {},
2906 }2971 }
2907 },2972 },
2908 else => {},2973 else => {},
...@@ -6895,9 +6960,9 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6895,9 +6960,9 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
6895 try f.writeCValue(writer, accum, .Other);6960 try f.writeCValue(writer, accum, .Other);
6896 switch (op) {6961 switch (op) {
6897 .float_op => |func| {6962 .float_op => |func| {
6898 try writer.writeAll(" = zig_libc_name_");6963 try writer.writeAll(" = zig_float_fn_");
6899 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);6964 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
6900 try writer.print("({s})(", .{func.operation});6965 try writer.print("_{s}(", .{func.operation});
6901 try f.writeCValue(writer, accum, .FunctionArgument);6966 try f.writeCValue(writer, accum, .FunctionArgument);
6902 try writer.writeAll(", ");6967 try writer.writeAll(", ");
6903 try f.writeCValue(writer, operand, .Other);6968 try f.writeCValue(writer, operand, .Other);
...@@ -7229,11 +7294,9 @@ fn unFloatOp(f: *Function, inst: Air.Inst.Index, operand: CValue, ty: Type, oper...@@ -7229,11 +7294,9 @@ fn unFloatOp(f: *Function, inst: Air.Inst.Index, operand: CValue, ty: Type, oper
7229 const v = try Vectorize.start(f, inst, writer, ty);7294 const v = try Vectorize.start(f, inst, writer, ty);
7230 try f.writeCValue(writer, local, .Other);7295 try f.writeCValue(writer, local, .Other);
7231 try v.elem(f, writer);7296 try v.elem(f, writer);
7232 try writer.writeAll(" = zig_libc_name_");7297 try writer.writeAll(" = zig_float_fn_");
7233 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);7298 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
7234 try writer.writeByte('(');7299 try writer.print("_{s}(", .{operation});
7235 try writer.writeAll(operation);
7236 try writer.writeAll(")(");
7237 try f.writeCValue(writer, operand, .FunctionArgument);7300 try f.writeCValue(writer, operand, .FunctionArgument);
7238 try v.elem(f, writer);7301 try v.elem(f, writer);
7239 try writer.writeAll(");\n");7302 try writer.writeAll(");\n");
...@@ -7268,11 +7331,9 @@ fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVa...@@ -7268,11 +7331,9 @@ fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVa
7268 const v = try Vectorize.start(f, inst, writer, inst_ty);7331 const v = try Vectorize.start(f, inst, writer, inst_ty);
7269 try f.writeCValue(writer, local, .Other);7332 try f.writeCValue(writer, local, .Other);
7270 try v.elem(f, writer);7333 try v.elem(f, writer);
7271 try writer.writeAll(" = zig_libc_name_");7334 try writer.writeAll(" = zig_float_fn_");
7272 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);7335 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);
7273 try writer.writeByte('(');7336 try writer.print("_{s}(", .{operation});
7274 try writer.writeAll(operation);
7275 try writer.writeAll(")(");
7276 try f.writeCValue(writer, lhs, .FunctionArgument);7337 try f.writeCValue(writer, lhs, .FunctionArgument);
7277 try v.elem(f, writer);7338 try v.elem(f, writer);
7278 try writer.writeAll(", ");7339 try writer.writeAll(", ");
...@@ -7302,9 +7363,9 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -7302,9 +7363,9 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue {
7302 const v = try Vectorize.start(f, inst, writer, inst_ty);7363 const v = try Vectorize.start(f, inst, writer, inst_ty);
7303 try f.writeCValue(writer, local, .Other);7364 try f.writeCValue(writer, local, .Other);
7304 try v.elem(f, writer);7365 try v.elem(f, writer);
7305 try writer.writeAll(" = zig_libc_name_");7366 try writer.writeAll(" = zig_float_fn_");
7306 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);7367 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty);
7307 try writer.writeAll("(fma)(");7368 try writer.writeAll("_fma(");
7308 try f.writeCValue(writer, mulend1, .FunctionArgument);7369 try f.writeCValue(writer, mulend1, .FunctionArgument);
7309 try v.elem(f, writer);7370 try v.elem(f, writer);
7310 try writer.writeAll(", ");7371 try writer.writeAll(", ");
src/link/C.zig+1-1
...@@ -262,7 +262,7 @@ fn updateAnonDecl(self: *C, module: *Module, i: usize) !void {...@@ -262,7 +262,7 @@ fn updateAnonDecl(self: *C, module: *Module, i: usize) !void {
262 };262 };
263 const c_value: codegen.CValue = .{ .constant = anon_decl };263 const c_value: codegen.CValue = .{ .constant = anon_decl };
264 const alignment: Alignment = self.aligned_anon_decls.get(anon_decl) orelse .none;264 const alignment: Alignment = self.aligned_anon_decls.get(anon_decl) orelse .none;
265 codegen.genDeclValue(&object, tv, false, c_value, alignment, .none) catch |err| switch (err) {265 codegen.genDeclValue(&object, tv, .local, c_value, alignment, .none) catch |err| switch (err) {
266 error.AnalysisFail => {266 error.AnalysisFail => {
267 @panic("TODO: C backend AnalysisFail on anonymous decl");267 @panic("TODO: C backend AnalysisFail on anonymous decl");
268 //try module.failed_decls.put(gpa, decl_index, object.dg.error_msg.?);268 //try module.failed_decls.put(gpa, decl_index, object.dg.error_msg.?);
test/behavior/bugs/12680.zig-5
...@@ -12,11 +12,6 @@ test "export a function twice" {...@@ -12,11 +12,6 @@ test "export a function twice" {
12 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;12 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
13 if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf) return error.SkipZigTest;13 if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf) return error.SkipZigTest;
1414
15 if (builtin.os.tag == .macos and builtin.zig_backend == .stage2_c) {
16 // TODO: test.c: error: aliases are not supported on darwin
17 return error.SkipZigTest;
18 }
19
20 // If it exports the function correctly, `test_func` and `testFunc` will points to the same address.15 // If it exports the function correctly, `test_func` and `testFunc` will points to the same address.
21 try expectEqual(test_func(), other_file.testFunc());16 try expectEqual(test_func(), other_file.testFunc());
22}17}