mirror of
https://github.com/ziglang/zig.git
synced 2025-02-12 23:50:18 +00:00
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_<float type>_<operation>`. 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.
This commit is contained in:
parent
58789cb054
commit
ce293c982e
70
lib/zig.h
70
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)
|
||||
|
@ -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(", ");
|
||||
|
Loading…
Reference in New Issue
Block a user