From 58789cb054b16e87d0a9b69ed8484cd30664fdb0 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Thu, 2 Nov 2023 00:01:46 -0400 Subject: [PATCH 1/7] Revert "CI: disable MSVC C backend checks" This reverts commit 7c5d01b95e3cb47187726c52dae4abb2e5b4faaf. --- ci/x86_64-windows-debug.ps1 | 11 +++++------ ci/x86_64-windows-release.ps1 | 11 +++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/ci/x86_64-windows-debug.ps1 b/ci/x86_64-windows-debug.ps1 index dec3a4be22..b859cda40b 100644 --- a/ci/x86_64-windows-debug.ps1 +++ b/ci/x86_64-windows-debug.ps1 @@ -97,9 +97,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 diff --git a/ci/x86_64-windows-release.ps1 b/ci/x86_64-windows-release.ps1 index c306756bd2..7ee4eb29d1 100644 --- a/ci/x86_64-windows-release.ps1 +++ b/ci/x86_64-windows-release.ps1 @@ -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 From ce293c982e1b6d9db775e02fefb8905caa4ee748 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Thu, 2 Nov 2023 00:02:37 -0400 Subject: [PATCH 2/7] cbe: avoid collisions with builtins and intrinsics Changes: - Add `isMangledIdent` to determine if `fmtIdent` would make any edits to the identifier - Any function that has a mangled identifier is referred to using the mangled identifer within the current file, but if it is exported the first export will be with the non-mangled name. - Add `zig_import` to import a symbol under a different name - Add a level of indirection to float function names. Now, they are referred to as `zig_float_fn__`. The definitions in zig.h are wrapped with `zig_import` to import the symbol under the real name. The specific problem that sparked this change was the combination of `zig_libc_name_f80(name) __##name##x` with the input `fma`, resulting in `__fmax`, which is a new intrinsic in recent versions of cl.exe. With the above changes in place, compiler_rt can output the following: ``` static zig_weak_linkage_fn zig_f80 zig_e___fmax(zig_f80, zig_f80, zig_f80); zig_export(zig_weak_linkage_fn zig_f80 zig_e___fmax(zig_f80, zig_f80, zig_f80), __fmax, "__fmax"); ``` Within compiler_rt, `zig_e___fmax` is used to refer to the function, but consumers will import `__fmax`, which maps to their `zig_float_fn_f80_fma` definition from zig.h. --- lib/zig.h | 70 +++++++++++++++++++++++++++------------------- src/codegen/c.zig | 71 +++++++++++++++++++++++++++++++---------------- 2 files changed, 89 insertions(+), 52 deletions(-) diff --git a/lib/zig.h b/lib/zig.h index 233c2961cb..ee4a338be7 100644 --- a/lib/zig.h +++ b/lib/zig.h @@ -175,19 +175,33 @@ typedef char bool; #endif #if zig_has_attribute(alias) -#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(symbol))) +#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(#symbol))) #elif _MSC_VER #if _M_X64 -#define zig_export(sig, symbol, name) sig;\ - __pragma(comment(linker, "/alternatename:" name "=" symbol )) +#define zig_export(sig, symbol, name) zig_extern sig;\ + __pragma(comment(linker, "/alternatename:" name "=" #symbol )) #else /*_M_X64 */ -#define zig_export(sig, symbol, name) sig;\ - __pragma(comment(linker, "/alternatename:_" name "=_" symbol )) +#define zig_export(sig, symbol, name) zig_extern sig;\ + __pragma(comment(linker, "/alternatename:" name "=" #symbol )) #endif /*_M_X64 */ #else -#define zig_export(sig, symbol, name) __asm(name " = " symbol) +#define zig_export(sig, symbol, name) __asm(name " = " #symbol) #endif +#if _MSC_VER +#if _M_X64 +#define zig_import(sig, symbol, name) sig;\ + __pragma(comment(linker, "/alternatename:" #symbol "=" #name )) +#else /*_M_X64 */ +#define zig_import(sig, symbol, name) sig;\ + __pragma(comment(linker, "/alternatename:_" #symbol "=_" #name )) +#endif /*_M_X64 */ +#else +#define zig_import(sig, symbol, name) zig_extern sig asm(#name); +#endif + +#define zig_expand_import(sig, symbol, name) zig_import(sig, symbol, name) + #if zig_has_attribute(weak) || defined(zig_gnuc) #define zig_weak_linkage __attribute__((weak)) #define zig_weak_linkage_fn __attribute__((weak)) @@ -3330,31 +3344,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_import(zig_extern zig_f##w zig_float_fn_f##w##_sqrt(zig_f##w), zig_float_fn_f##w##_sqrt, zig_libc_name_f##w(sqrt)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_sin(zig_f##w), zig_float_fn_f##w##_sin, zig_libc_name_f##w(sin)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_cos(zig_f##w), zig_float_fn_f##w##_cos, zig_libc_name_f##w(cos)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_tan(zig_f##w), zig_float_fn_f##w##_tan, zig_libc_name_f##w(tan)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_exp(zig_f##w), zig_float_fn_f##w##_exp, zig_libc_name_f##w(exp)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_exp2(zig_f##w), zig_float_fn_f##w##_exp2, zig_libc_name_f##w(exp2)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_log(zig_f##w), zig_float_fn_f##w##_log, zig_libc_name_f##w(log)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_log2(zig_f##w), zig_float_fn_f##w##_log2, zig_libc_name_f##w(log2)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_log10(zig_f##w), zig_float_fn_f##w##_log10, zig_libc_name_f##w(log10)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fabs(zig_f##w), zig_float_fn_f##w##_fabs, zig_libc_name_f##w(fabs)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_floor(zig_f##w), zig_float_fn_f##w##_floor, zig_libc_name_f##w(floor)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_ceil(zig_f##w), zig_float_fn_f##w##_ceil, zig_libc_name_f##w(ceil)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_round(zig_f##w), zig_float_fn_f##w##_round, zig_libc_name_f##w(round)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_trunc(zig_f##w), zig_float_fn_f##w##_trunc, zig_libc_name_f##w(trunc)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fmod(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmod, zig_libc_name_f##w(fmod)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fmin(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmin, zig_libc_name_f##w(fmin)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fmax(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmax, zig_libc_name_f##w(fmax)) \ + zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fma(zig_f##w, zig_f##w, zig_f##w), zig_float_fn_f##w##_fma, zig_libc_name_f##w(fma)) \ \ 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) { \ @@ -3458,7 +3472,7 @@ zig_float_builtins(64) zig_##Type zig_atomicrmw_desired; \ zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \ do { \ - zig_atomicrmw_desired = zig_libc_name_##Type(fmin)(zig_atomicrmw_expected, arg); \ + zig_atomicrmw_desired = zig_float_fn_##Type##_fmin(zig_atomicrmw_expected, arg); \ } while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \ res = zig_atomicrmw_expected; \ } while (0) @@ -3467,7 +3481,7 @@ zig_float_builtins(64) zig_##Type zig_atomicrmw_desired; \ zig_atomic_load(zig_atomicrmw_expected, obj, memory_order_relaxed, Type, ReprType); \ do { \ - zig_atomicrmw_desired = zig_libc_name_##Type(fmax)(zig_atomicrmw_expected, arg); \ + zig_atomicrmw_desired = zig_float_fn_##Type##_fmax(zig_atomicrmw_expected, arg); \ } while (!zig_cmpxchg_weak(obj, zig_atomicrmw_expected, zig_atomicrmw_desired, order, memory_order_relaxed, Type, ReprType)); \ res = zig_atomicrmw_expected; \ } while (0) diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 0178ffef9a..17aad238cb 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -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. @@ -526,6 +540,7 @@ pub const DeclGen = struct { is_naked_fn: bool, /// This is a borrowed reference from `link.C`. fwd_decl: std.ArrayList(u8), + error_msg: ?*Module.ErrorMsg, ctypes: CType.Store, /// Keeps track of anonymous decls that need to be rendered before this @@ -1598,7 +1613,7 @@ pub const DeclGen = struct { switch (name) { .export_index => |export_index| try dg.renderDeclName(w, fn_decl_index, export_index), - .string => |string| try w.writeAll(string), + .string => |string| try w.print("{ }", .{fmtIdent(string)}), } try renderTypeSuffix( @@ -1813,9 +1828,17 @@ pub const DeclGen = struct { fn declIsGlobal(dg: *DeclGen, tv: TypedValue) bool { const mod = dg.module; return switch (mod.intern_pool.indexToKey(tv.val.ip_index)) { - .variable => |variable| mod.decl_exports.contains(variable.decl), + .variable => |variable| { + if (mod.decl_exports.get(variable.decl)) |exports| { + return !isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true); + } else return false; + }, .extern_func => true, - .func => |func| mod.decl_exports.contains(func.owner_decl), + .func => |func| { + if (mod.decl_exports.get(func.owner_decl)) |exports| { + return !isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true); + } else return false; + }, else => unreachable, }; } @@ -1925,9 +1948,9 @@ 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. @@ -2564,16 +2587,19 @@ fn genExports(o: *Object) !void { const fwd = o.dg.fwd_decl.writer(); const exports = mod.decl_exports.get(decl_index) orelse return; - if (exports.items.len < 2) return; + + const is_mangled = isMangledIdent(ip.stringToSlice(exports.items[0].opts.name), true); + if (exports.items.len < 2 and !is_mangled) return; switch (ip.indexToKey(tv.val.toIntern())) { .func => { - for (exports.items[1..], 1..) |@"export", i| { + const start_i = 1 - @intFromBool(is_mangled); + for (exports.items[start_i..], start_i..) |@"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), + try fwd.print(", { }, {s});\n", .{ + fmtIdent(ip.stringToSlice(exports.items[0].opts.name)), fmtStringLiteral(ip.stringToSlice(@"export".opts.name), null), }); } @@ -2583,7 +2609,8 @@ fn genExports(o: *Object) !void { unreachable; }, .variable => |variable| { - for (exports.items[1..], 1..) |@"export", i| { + const start_i = 1 - @intFromBool(is_mangled); + for (exports.items[start_i..], start_i..) |@"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); @@ -2595,8 +2622,8 @@ fn genExports(o: *Object) !void { decl.alignment, .complete, ); - try fwd.print(", {s}, {s});\n", .{ - fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null), + try fwd.print(", { }, {s});\n", .{ + fmtIdent(ip.stringToSlice(exports.items[0].opts.name)), fmtStringLiteral(alias, null), }); } @@ -6861,9 +6888,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); @@ -7195,11 +7222,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"); @@ -7234,11 +7259,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(", "); @@ -7268,9 +7291,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(", "); From 192e9a315d92133b27d757f18bd4fd619976b755 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sat, 4 Nov 2023 19:37:19 -0400 Subject: [PATCH 3/7] cbe: add DeclVisibility and zig_extern_mangled to handle exporting mangled symbols under a different name --- lib/zig.h | 48 +++++++++++++++------------ src/codegen/c.zig | 84 ++++++++++++++++++++++++++++++++++------------- src/link/C.zig | 2 +- 3 files changed, 89 insertions(+), 45 deletions(-) diff --git a/lib/zig.h b/lib/zig.h index ee4a338be7..c188b6e5c7 100644 --- a/lib/zig.h +++ b/lib/zig.h @@ -174,6 +174,12 @@ typedef char bool; #define zig_extern extern #endif +#if _MSC_VER +#define zig_extern_mangled zig_extern +#else +#define zig_extern_mangled static +#endif + #if zig_has_attribute(alias) #define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(#symbol))) #elif _MSC_VER @@ -190,14 +196,14 @@ typedef char bool; #if _MSC_VER #if _M_X64 -#define zig_import(sig, symbol, name) sig;\ +#define zig_import(sig, symbol, name) zig_extern sig;\ __pragma(comment(linker, "/alternatename:" #symbol "=" #name )) #else /*_M_X64 */ -#define zig_import(sig, symbol, name) sig;\ +#define zig_import(sig, symbol, name) zig_extern sig;\ __pragma(comment(linker, "/alternatename:_" #symbol "=_" #name )) #endif /*_M_X64 */ #else -#define zig_import(sig, symbol, name) zig_extern sig asm(#name); +#define zig_import(sig, symbol, name) zig_extern sig __asm(#name); #endif #define zig_expand_import(sig, symbol, name) zig_import(sig, symbol, name) @@ -3344,24 +3350,24 @@ 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_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_sqrt(zig_f##w), zig_float_fn_f##w##_sqrt, zig_libc_name_f##w(sqrt)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_sin(zig_f##w), zig_float_fn_f##w##_sin, zig_libc_name_f##w(sin)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_cos(zig_f##w), zig_float_fn_f##w##_cos, zig_libc_name_f##w(cos)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_tan(zig_f##w), zig_float_fn_f##w##_tan, zig_libc_name_f##w(tan)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_exp(zig_f##w), zig_float_fn_f##w##_exp, zig_libc_name_f##w(exp)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_exp2(zig_f##w), zig_float_fn_f##w##_exp2, zig_libc_name_f##w(exp2)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_log(zig_f##w), zig_float_fn_f##w##_log, zig_libc_name_f##w(log)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_log2(zig_f##w), zig_float_fn_f##w##_log2, zig_libc_name_f##w(log2)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_log10(zig_f##w), zig_float_fn_f##w##_log10, zig_libc_name_f##w(log10)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fabs(zig_f##w), zig_float_fn_f##w##_fabs, zig_libc_name_f##w(fabs)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_floor(zig_f##w), zig_float_fn_f##w##_floor, zig_libc_name_f##w(floor)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_ceil(zig_f##w), zig_float_fn_f##w##_ceil, zig_libc_name_f##w(ceil)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_round(zig_f##w), zig_float_fn_f##w##_round, zig_libc_name_f##w(round)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_trunc(zig_f##w), zig_float_fn_f##w##_trunc, zig_libc_name_f##w(trunc)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fmod(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmod, zig_libc_name_f##w(fmod)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fmin(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmin, zig_libc_name_f##w(fmin)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fmax(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmax, zig_libc_name_f##w(fmax)) \ - zig_expand_import(zig_extern zig_f##w zig_float_fn_f##w##_fma(zig_f##w, zig_f##w, zig_f##w), zig_float_fn_f##w##_fma, zig_libc_name_f##w(fma)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_sqrt(zig_f##w), zig_float_fn_f##w##_sqrt, zig_libc_name_f##w(sqrt)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_sin(zig_f##w), zig_float_fn_f##w##_sin, zig_libc_name_f##w(sin)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_cos(zig_f##w), zig_float_fn_f##w##_cos, zig_libc_name_f##w(cos)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_tan(zig_f##w), zig_float_fn_f##w##_tan, zig_libc_name_f##w(tan)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_exp(zig_f##w), zig_float_fn_f##w##_exp, zig_libc_name_f##w(exp)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_exp2(zig_f##w), zig_float_fn_f##w##_exp2, zig_libc_name_f##w(exp2)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_log(zig_f##w), zig_float_fn_f##w##_log, zig_libc_name_f##w(log)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_log2(zig_f##w), zig_float_fn_f##w##_log2, zig_libc_name_f##w(log2)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_log10(zig_f##w), zig_float_fn_f##w##_log10, zig_libc_name_f##w(log10)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_fabs(zig_f##w), zig_float_fn_f##w##_fabs, zig_libc_name_f##w(fabs)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_floor(zig_f##w), zig_float_fn_f##w##_floor, zig_libc_name_f##w(floor)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_ceil(zig_f##w), zig_float_fn_f##w##_ceil, zig_libc_name_f##w(ceil)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_round(zig_f##w), zig_float_fn_f##w##_round, zig_libc_name_f##w(round)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_trunc(zig_f##w), zig_float_fn_f##w##_trunc, zig_libc_name_f##w(trunc)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_fmod(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmod, zig_libc_name_f##w(fmod)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_fmin(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmin, zig_libc_name_f##w(fmin)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_fmax(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmax, zig_libc_name_f##w(fmax)) \ + zig_expand_import(zig_f##w zig_float_fn_f##w##_fma(zig_f##w, zig_f##w, zig_f##w), zig_float_fn_f##w##_fma, zig_libc_name_f##w(fma)) \ \ static inline zig_f##w zig_div_trunc_f##w(zig_f##w lhs, zig_f##w rhs) { \ return zig_float_fn_f##w##_trunc(zig_div_f##w(lhs, rhs)); \ diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 17aad238cb..735b58e2dd 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -272,6 +272,28 @@ pub fn isMangledIdent(ident: []const u8, solo: bool) bool { return false; } +const DeclVisibility = enum { + global, + global_mangled, + local, + + fn renderFwd(visibility: DeclVisibility, w: anytype) !void { + try w.writeAll(switch (visibility) { + .global => "zig_extern ", + // MSVC doesn't support exporting `static` functions, so they need special treatment + .global_mangled => "zig_extern_mangled ", + .local => "static ", + }); + } + + fn renderDef(visibility: DeclVisibility, w: anytype) !void { + return switch (visibility) { + .global => {}, + else => visibility.renderFwd(w), + }; + } +}; + /// This data is available when outputting .c code for a `InternPool.Index` /// that corresponds to `func`. /// It is not available when generating .h file. @@ -1825,19 +1847,25 @@ pub const DeclGen = struct { try renderTypeSuffix(dg.pass, store.*, mod, w, cty_idx, .suffix, .{}); } - fn declIsGlobal(dg: *DeclGen, tv: TypedValue) bool { + fn declVisibility(dg: *DeclGen, tv: TypedValue) DeclVisibility { const mod = dg.module; return switch (mod.intern_pool.indexToKey(tv.val.ip_index)) { .variable => |variable| { if (mod.decl_exports.get(variable.decl)) |exports| { - return !isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true); - } else return false; + return if (isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true)) + .global_mangled + else + .global; + } else return .local; }, - .extern_func => true, + .extern_func => .global, .func => |func| { if (mod.decl_exports.get(func.owner_decl)) |exports| { - return !isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true); - } else return false; + return if (isMangledIdent(dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true)) + .global_mangled + else + .global; + } else return .local; }, else => unreachable, }; @@ -1923,8 +1951,8 @@ pub const DeclGen = struct { fn renderFwdDecl(dg: *DeclGen, decl_index: Decl.Index, variable: InternPool.Key.Variable) !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; - try fwd.writeAll(if (is_global) "zig_extern " else "static "); + const visibility = if (variable.is_extern) .global else dg.declVisibility(.{ .ty = decl.ty, .val = decl.val }); + try visibility.renderFwd(fwd); const export_weak_linkage = if (dg.module.decl_exports.get(decl_index)) |exports| exports.items[0].opts.linkage == .Weak else @@ -2735,9 +2763,10 @@ pub fn genFunc(f: *Function) !void { o.code_header = std.ArrayList(u8).init(gpa); defer o.code_header.deinit(); - const is_global = o.dg.declIsGlobal(tv); + const visibility = o.dg.declVisibility(tv); const fwd_decl_writer = o.dg.fwd_decl.writer(); - try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static "); + try visibility.renderFwd(fwd_decl_writer); + 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 }); @@ -2745,7 +2774,7 @@ pub fn genFunc(f: *Function) !void { try genExports(o); try o.indent_writer.insertNewline(); - if (!is_global) try o.writer().writeAll("static "); + try visibility.renderDef(o.writer()); try o.dg.renderFunctionSignature(o.writer(), decl_index, .complete, .{ .export_index = 0 }); try o.writer().writeByte(' '); @@ -2829,9 +2858,9 @@ pub fn genDecl(o: *Object) !void { if (variable.is_extern) return; - const is_global = o.dg.declIsGlobal(tv) or variable.is_extern; + const visibility = if (variable.is_extern) .global else o.dg.declVisibility(tv); const w = o.writer(); - if (!is_global) try w.writeAll("static "); + try visibility.renderDef(w); if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage "); if (variable.is_threadlocal) try w.writeAll("zig_threadlocal "); if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s| @@ -2844,16 +2873,21 @@ pub fn genDecl(o: *Object) !void { try w.writeByte(';'); try o.indent_writer.insertNewline(); } else { - const is_global = o.dg.module.decl_exports.contains(decl_index); + const visibility: DeclVisibility = if (o.dg.module.decl_exports.get(decl_index)) |exports| b: { + break :b if (isMangledIdent(o.dg.module.intern_pool.stringToSlice(exports.items[0].opts.name), true)) + .global_mangled + else + .global; + } else .local; const decl_c_value = .{ .decl = decl_index }; - return genDeclValue(o, tv, is_global, decl_c_value, decl.alignment, decl.@"linksection"); + return genDeclValue(o, tv, visibility, decl_c_value, decl.alignment, decl.@"linksection"); } } pub fn genDeclValue( o: *Object, tv: TypedValue, - is_global: bool, + visibility: DeclVisibility, decl_c_value: CValue, alignment: Alignment, link_section: InternPool.OptionalNullTerminatedString, @@ -2861,12 +2895,13 @@ pub fn genDeclValue( const mod = o.dg.module; const fwd_decl_writer = o.dg.fwd_decl.writer(); - try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static "); + try visibility.renderFwd(fwd_decl_writer); try o.dg.renderTypeAndName(fwd_decl_writer, tv.ty, decl_c_value, Const, alignment, .complete); try fwd_decl_writer.writeAll(";\n"); const w = o.writer(); - if (!is_global) try w.writeAll("static "); + try visibility.renderDef(w); + 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); @@ -2891,11 +2926,14 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void { 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"); + const visibility = dg.declVisibility(tv); + switch (visibility) { + .global, .global_mangled => { + try visibility.renderFwd(writer); + try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 }); + try dg.fwd_decl.appendSlice(";\n"); + }, + .local => {}, } }, else => {}, diff --git a/src/link/C.zig b/src/link/C.zig index 40dfc0771d..53d57c25cb 100644 --- a/src/link/C.zig +++ b/src/link/C.zig @@ -259,7 +259,7 @@ fn updateAnonDecl(self: *C, module: *Module, i: usize) !void { }; const c_value: codegen.CValue = .{ .constant = anon_decl }; const alignment: Alignment = self.aligned_anon_decls.get(anon_decl) orelse .none; - codegen.genDeclValue(&object, tv, false, c_value, alignment, .none) catch |err| switch (err) { + codegen.genDeclValue(&object, tv, .local, c_value, alignment, .none) catch |err| switch (err) { error.AnalysisFail => { @panic("TODO: C backend AnalysisFail on anonymous decl"); //try module.failed_decls.put(gpa, decl_index, object.dg.error_msg.?); From 26dabbf301ce4cd950cd66373dc17f0747c1e813 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 5 Nov 2023 00:27:08 -0400 Subject: [PATCH 4/7] cbe: handle underscore prexfix on macos, don't mangle extern function names --- lib/zig.h | 4 ++++ src/codegen/c.zig | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/zig.h b/lib/zig.h index c188b6e5c7..032ad30053 100644 --- a/lib/zig.h +++ b/lib/zig.h @@ -203,7 +203,11 @@ typedef char bool; __pragma(comment(linker, "/alternatename:_" #symbol "=_" #name )) #endif /*_M_X64 */ #else +#if __APPLE__ +#define zig_import(sig, symbol, name) zig_extern sig __asm("_" #name); +#else /* __APPLE__ */ #define zig_import(sig, symbol, name) zig_extern sig __asm(#name); +#endif /* __APPLE__ */ #endif #define zig_expand_import(sig, symbol, name) zig_import(sig, symbol, name) diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 735b58e2dd..46895d71a7 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1978,7 +1978,7 @@ pub const DeclGen = struct { if (mod.decl_exports.get(decl_index)) |exports| { 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("{ }", .{fmtIdent(mod.intern_pool.stringToSlice(mod.declPtr(extern_decl_index).name))}); + try writer.print("{}", .{mod.declPtr(extern_decl_index).name.fmt(&mod.intern_pool)}); } 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. From a4526d260b6e96866e86c9e47bec5cf6fa5a0105 Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 5 Nov 2023 13:28:49 -0500 Subject: [PATCH 5/7] cbe: fix builtins not being used by zig_import when they are available --- lib/zig.h | 94 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/lib/zig.h b/lib/zig.h index 032ad30053..14fd1a2b9c 100644 --- a/lib/zig.h +++ b/lib/zig.h @@ -106,7 +106,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 @@ -177,40 +177,50 @@ typedef char bool; #if _MSC_VER #define zig_extern_mangled zig_extern #else -#define zig_extern_mangled static -#endif - -#if zig_has_attribute(alias) -#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(#symbol))) -#elif _MSC_VER -#if _M_X64 -#define zig_export(sig, symbol, name) zig_extern sig;\ - __pragma(comment(linker, "/alternatename:" name "=" #symbol )) -#else /*_M_X64 */ -#define zig_export(sig, symbol, name) zig_extern sig;\ - __pragma(comment(linker, "/alternatename:" name "=" #symbol )) -#endif /*_M_X64 */ +#if zig_has_attribute(visibility) +#define zig_extern_mangled zig_extern __attribute__((visibility("hidden"))) #else -#define zig_export(sig, symbol, name) __asm(name " = " #symbol) +#define zig_extern_mangled zig_extern +#endif #endif #if _MSC_VER #if _M_X64 -#define zig_import(sig, symbol, name) zig_extern sig;\ - __pragma(comment(linker, "/alternatename:" #symbol "=" #name )) +#define zig_export(sig, symbol, name) zig_extern sig;\ + __pragma(comment(linker, "/alternatename:" name "=" #symbol )) #else /*_M_X64 */ -#define zig_import(sig, symbol, name) zig_extern sig;\ - __pragma(comment(linker, "/alternatename:_" #symbol "=_" #name )) +#define zig_export(sig, symbol, name) zig_extern sig;\ + __pragma(comment(linker, "/alternatename:" name "=" #symbol )) #endif /*_M_X64 */ -#else +#else /* _MSC_VER */ #if __APPLE__ -#define zig_import(sig, symbol, name) zig_extern sig __asm("_" #name); +#define zig_export(sig, symbol, name) __asm("_" name " = _" #symbol) #else /* __APPLE__ */ -#define zig_import(sig, symbol, name) zig_extern sig __asm(#name); +#define zig_export(sig, symbol, name) __asm(name " = " #symbol) #endif /* __APPLE__ */ +#endif /* _MSC_VER */ + +#if _MSC_VER +#if _M_X64 +#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args;\ + __pragma(comment(linker, "/alternatename:" #fn_name "=" #libc_name )); +#else /*_M_X64 */ +#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args;\ + __pragma(comment(linker, "/alternatename:_" #fn_name "=_" #libc_name )); +#endif /*_M_X64 */ +#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 */ +#if __APPLE__ +#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args __asm("_" #libc_name); +#else /* __APPLE__ */ +#define zig_import(Type, fn_name, libc_name, sig_args, call_args) zig_extern Type fn_name sig_args __asm(#libc_name); +#endif /* __APPLE__ */ +#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(sig, symbol, name) zig_import(sig, symbol, name) +#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)) @@ -3111,6 +3121,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,6 +3347,7 @@ zig_float_negate_builtin(128, zig_make_u128, (UINT64_C(1) << 63, UINT64_C(0))) return lhs operator rhs; \ } +#define zig_expand_has_builtin(b) zig_has_builtin(b) #define zig_common_float_builtins(w) \ zig_convert_builtin( int64_t, int64_t, fix, zig_f##w, zig_f##w, ) \ zig_convert_builtin(zig_i128, zig_i128, fix, zig_f##w, zig_f##w, ) \ @@ -3354,24 +3366,24 @@ 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_expand_import(zig_f##w zig_float_fn_f##w##_sqrt(zig_f##w), zig_float_fn_f##w##_sqrt, zig_libc_name_f##w(sqrt)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_sin(zig_f##w), zig_float_fn_f##w##_sin, zig_libc_name_f##w(sin)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_cos(zig_f##w), zig_float_fn_f##w##_cos, zig_libc_name_f##w(cos)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_tan(zig_f##w), zig_float_fn_f##w##_tan, zig_libc_name_f##w(tan)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_exp(zig_f##w), zig_float_fn_f##w##_exp, zig_libc_name_f##w(exp)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_exp2(zig_f##w), zig_float_fn_f##w##_exp2, zig_libc_name_f##w(exp2)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_log(zig_f##w), zig_float_fn_f##w##_log, zig_libc_name_f##w(log)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_log2(zig_f##w), zig_float_fn_f##w##_log2, zig_libc_name_f##w(log2)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_log10(zig_f##w), zig_float_fn_f##w##_log10, zig_libc_name_f##w(log10)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_fabs(zig_f##w), zig_float_fn_f##w##_fabs, zig_libc_name_f##w(fabs)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_floor(zig_f##w), zig_float_fn_f##w##_floor, zig_libc_name_f##w(floor)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_ceil(zig_f##w), zig_float_fn_f##w##_ceil, zig_libc_name_f##w(ceil)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_round(zig_f##w), zig_float_fn_f##w##_round, zig_libc_name_f##w(round)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_trunc(zig_f##w), zig_float_fn_f##w##_trunc, zig_libc_name_f##w(trunc)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_fmod(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmod, zig_libc_name_f##w(fmod)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_fmin(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmin, zig_libc_name_f##w(fmin)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_fmax(zig_f##w, zig_f##w), zig_float_fn_f##w##_fmax, zig_libc_name_f##w(fmax)) \ - zig_expand_import(zig_f##w zig_float_fn_f##w##_fma(zig_f##w, zig_f##w, zig_f##w), zig_float_fn_f##w##_fma, zig_libc_name_f##w(fma)) \ + 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_float_fn_f##w##_trunc(zig_div_f##w(lhs, rhs)); \ From 8c0e0cd3535d4e59a01dc49f9b93c922ca63114f Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 5 Nov 2023 20:34:05 -0500 Subject: [PATCH 6/7] cbe: skip std.math.lerp test that fails when compiler_rt fma is used --- lib/std/math.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/math.zig b/lib/std/math.zig index b18d5bc3ed..9adfd191d5 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -1265,6 +1265,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; From 50e2fb8fd0e1aae5fc3123d6f26d7420d4baa41c Mon Sep 17 00:00:00 2001 From: kcbanner Date: Sun, 5 Nov 2023 23:36:11 -0500 Subject: [PATCH 7/7] cbe: fixup __asm style exports, re-enable 12680 on macos now that alias isn't used --- lib/zig.h | 6 ++++-- test/behavior/bugs/12680.zig | 5 ----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/zig.h b/lib/zig.h index 14fd1a2b9c..3a3489cc89 100644 --- a/lib/zig.h +++ b/lib/zig.h @@ -194,9 +194,11 @@ typedef char bool; #endif /*_M_X64 */ #else /* _MSC_VER */ #if __APPLE__ -#define zig_export(sig, symbol, name) __asm("_" name " = _" #symbol) +#define zig_export(sig, symbol, name) zig_extern sig;\ + __asm("_" name " = _" #symbol) #else /* __APPLE__ */ -#define zig_export(sig, symbol, name) __asm(name " = " #symbol) +#define zig_export(sig, symbol, name) zig_extern sig;\ + __asm(name " = " #symbol) #endif /* __APPLE__ */ #endif /* _MSC_VER */ diff --git a/test/behavior/bugs/12680.zig b/test/behavior/bugs/12680.zig index e75cf6ec3b..883303c4f8 100644 --- a/test/behavior/bugs/12680.zig +++ b/test/behavior/bugs/12680.zig @@ -12,11 +12,6 @@ test "export a function twice" { 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()); }