diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-01-03 12:57:01 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-03 12:57:01 -0800 |
| commit | a9337bef2d54201ab304bb3428479339ecaacbac (patch) | |
| tree | e253679e591e86d84a29139b7d7987eab3c3718f /src | |
| parent | 9a56228c2b90d94d01bb76784c77fdec5710cf0a (diff) | |
| parent | 047d6d996e540eaa50ec24b0751d147bfe2cacdb (diff) | |
| download | zig-a9337bef2d54201ab304bb3428479339ecaacbac.tar.gz zig-a9337bef2d54201ab304bb3428479339ecaacbac.zip | |
Merge pull request #18431 from jacobly0/cbe-extern
cbe: fix non-msvc externs and exports
Diffstat (limited to 'src')
| -rw-r--r-- | src/AstGen.zig | 4 | ||||
| -rw-r--r-- | src/InternPool.zig | 10 | ||||
| -rw-r--r-- | src/Sema.zig | 10 | ||||
| -rw-r--r-- | src/Zir.zig | 3 | ||||
| -rw-r--r-- | src/codegen/c.zig | 260 |
5 files changed, 201 insertions, 86 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig index 4fceb5d84e..06afe48a75 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -4456,6 +4456,7 @@ fn globalVarDecl( .align_inst = .none, // passed via the decls data .init = init_inst, .is_extern = false, + .is_const = !is_mutable, .is_threadlocal = is_threadlocal, }); break :vi var_inst; @@ -4474,6 +4475,7 @@ fn globalVarDecl( .align_inst = .none, // passed via the decls data .init = .none, .is_extern = true, + .is_const = !is_mutable, .is_threadlocal = is_threadlocal, }); break :vi var_inst; @@ -11495,6 +11497,7 @@ const GenZir = struct { var_type: Zir.Inst.Ref, init: Zir.Inst.Ref, is_extern: bool, + is_const: bool, is_threadlocal: bool, }) !Zir.Inst.Ref { const astgen = gz.astgen; @@ -11533,6 +11536,7 @@ const GenZir = struct { .has_align = args.align_inst != .none, .has_init = args.init != .none, .is_extern = args.is_extern, + .is_const = args.is_const, .is_threadlocal = args.is_threadlocal, }), .operand = payload_index, diff --git a/src/InternPool.zig b/src/InternPool.zig index e06597373e..2e690652a3 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -1005,11 +1005,11 @@ pub const Key = union(enum) { ty: Index, init: Index, decl: DeclIndex, - lib_name: OptionalNullTerminatedString = .none, - is_extern: bool = false, - is_const: bool = false, - is_threadlocal: bool = false, - is_weak_linkage: bool = false, + lib_name: OptionalNullTerminatedString, + is_extern: bool, + is_const: bool, + is_threadlocal: bool, + is_weak_linkage: bool, }; pub const ExternFunc = struct { diff --git a/src/Sema.zig b/src/Sema.zig index c6648b3d90..a2ed81161b 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -24760,7 +24760,9 @@ fn zirVarExtended( .decl = sema.owner_decl_index, .lib_name = try mod.intern_pool.getOrPutStringOpt(sema.gpa, lib_name), .is_extern = small.is_extern, + .is_const = small.is_const, .is_threadlocal = small.is_threadlocal, + .is_weak_linkage = false, } }))); } @@ -25264,6 +25266,7 @@ fn zirBuiltinExtern( if (options.linkage == .Weak and !ty.ptrAllowsZero(mod)) { ty = try mod.optionalType(ty.toIntern()); } + const ptr_info = ty.ptrInfo(mod); // TODO check duplicate extern @@ -25274,11 +25277,12 @@ fn zirBuiltinExtern( { const new_var = try mod.intern(.{ .variable = .{ - .ty = ty.toIntern(), + .ty = ptr_info.child, .init = .none, .decl = sema.owner_decl_index, + .lib_name = options.library_name, .is_extern = true, - .is_const = true, + .is_const = ptr_info.flags.is_const, .is_threadlocal = options.is_thread_local, .is_weak_linkage = options.linkage == .Weak, } }); @@ -25286,7 +25290,7 @@ fn zirBuiltinExtern( new_decl.src_line = sema.owner_decl.src_line; // We only access this decl through the decl_ref with the correct type created // below, so this type doesn't matter - new_decl.ty = ty; + new_decl.ty = Type.fromInterned(ptr_info.child); new_decl.val = Value.fromInterned(new_var); new_decl.alignment = .none; new_decl.@"linksection" = .none; diff --git a/src/Zir.zig b/src/Zir.zig index 8d885ff692..3faffabb40 100644 --- a/src/Zir.zig +++ b/src/Zir.zig @@ -2610,8 +2610,9 @@ pub const Inst = struct { has_align: bool, has_init: bool, is_extern: bool, + is_const: bool, is_threadlocal: bool, - _: u11 = undefined, + _: u10 = undefined, }; }; diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 4b7ac15b94..7fd367bb49 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. @@ -646,7 +660,7 @@ pub const DeclGen = struct { if (decl.val.getExternFunc(mod)) |extern_func| if (extern_func.decl != decl_index) return dg.renderDeclValue(writer, ty, val, extern_func.decl, location); - if (decl.val.getVariable(mod)) |variable| try dg.renderFwdDecl(decl_index, variable); + if (decl.val.getVariable(mod)) |variable| try dg.renderFwdDecl(decl_index, variable, .tentative); // We shouldn't cast C function pointers as this is UB (when you call // them). The analysis until now should ensure that the C function @@ -1593,7 +1607,7 @@ pub const DeclGen = struct { kind: CType.Kind, name: union(enum) { export_index: u32, - string: []const u8, + ident: []const u8, }, ) !void { const store = &dg.ctypes.set; @@ -1615,23 +1629,28 @@ pub const DeclGen = struct { try w.writeAll("zig_cold "); if (fn_info.return_type == .noreturn_type) try w.writeAll("zig_noreturn "); - const trailing = try renderTypePrefix(dg.pass, store.*, mod, w, fn_cty_idx, .suffix, .{}); - try w.print("{}", .{trailing}); + var trailing = try renderTypePrefix(dg.pass, store.*, mod, w, fn_cty_idx, .suffix, .{}); if (toCallingConvention(fn_info.cc)) |call_conv| { - try w.print("zig_callconv({s}) ", .{call_conv}); + try w.print("{}zig_callconv({s})", .{ trailing, call_conv }); + trailing = .maybe_space; } switch (kind) { .forward => {}, - .complete => if (fn_info.alignment.toByteUnitsOptional()) |a| - try w.print(" zig_align_fn({})", .{a}), + .complete => if (fn_info.alignment.toByteUnitsOptional()) |a| { + try w.print("{}zig_align_fn({})", .{ trailing, a }); + trailing = .maybe_space; + }, else => unreachable, } switch (name) { - .export_index => |export_index| try dg.renderDeclName(w, fn_decl_index, export_index), - .string => |string| try w.writeAll(string), + .export_index => |export_index| { + try w.print("{}", .{trailing}); + try dg.renderDeclName(w, fn_decl_index, export_index); + }, + .ident => |ident| try w.print("{}{ }", .{ trailing, fmtIdent(ident) }), } try renderTypeSuffix( @@ -1649,8 +1668,49 @@ pub const DeclGen = struct { ); switch (kind) { - .forward => if (fn_info.alignment.toByteUnitsOptional()) |a| - try w.print(" zig_align_fn({})", .{a}), + .forward => { + if (fn_info.alignment.toByteUnitsOptional()) |a| { + try w.print(" zig_align_fn({})", .{a}); + } + switch (name) { + .export_index => |export_index| mangled: { + const maybe_exports = mod.decl_exports.get(fn_decl_index); + const external_name = ip.stringToSlice( + if (maybe_exports) |exports| + exports.items[export_index].opts.name + else if (fn_decl.isExtern(mod)) + fn_decl.name + else + break :mangled, + ); + const is_mangled = isMangledIdent(external_name, true); + const is_export = export_index > 0; + if (is_mangled and is_export) { + try w.print(" zig_mangled_export({ }, {s}, {s})", .{ + fmtIdent(external_name), + fmtStringLiteral(external_name, null), + fmtStringLiteral( + ip.stringToSlice(maybe_exports.?.items[0].opts.name), + null, + ), + }); + } else if (is_mangled) { + try w.print(" zig_mangled_final({ }, {s})", .{ + fmtIdent(external_name), fmtStringLiteral(external_name, null), + }); + } else if (is_export) { + try w.print(" zig_export({s}, {s})", .{ + fmtStringLiteral( + ip.stringToSlice(maybe_exports.?.items[0].opts.name), + null, + ), + fmtStringLiteral(external_name, null), + }); + } + }, + .ident => {}, + } + }, .complete => {}, else => unreachable, } @@ -1930,12 +1990,18 @@ pub const DeclGen = struct { try dg.writeCValue(writer, member); } - fn renderFwdDecl(dg: *DeclGen, decl_index: InternPool.DeclIndex, variable: InternPool.Key.Variable) !void { + fn renderFwdDecl( + dg: *DeclGen, + decl_index: InternPool.DeclIndex, + variable: InternPool.Key.Variable, + fwd_kind: enum { tentative, final }, + ) !void { const decl = dg.module.declPtr(decl_index); const fwd = dg.fwd_decl.writer(); - const is_global = dg.declIsGlobal(.{ .ty = decl.ty, .val = decl.val }) or variable.is_extern; + const is_global = variable.is_extern or dg.declIsGlobal(.{ .ty = decl.ty, .val = decl.val }); try fwd.writeAll(if (is_global) "zig_extern " else "static "); - const export_weak_linkage = if (dg.module.decl_exports.get(decl_index)) |exports| + const maybe_exports = dg.module.decl_exports.get(decl_index); + const export_weak_linkage = if (maybe_exports) |exports| exports.items[0].opts.linkage == .Weak else false; @@ -1949,6 +2015,21 @@ pub const DeclGen = struct { decl.alignment, .complete, ); + mangled: { + const external_name = dg.module.intern_pool.stringToSlice(if (maybe_exports) |exports| + exports.items[0].opts.name + else if (variable.is_extern) + decl.name + else + break :mangled); + if (isMangledIdent(external_name, true)) { + try fwd.print(" zig_mangled_{s}({ }, {s})", .{ + @tagName(fwd_kind), + fmtIdent(external_name), + fmtStringLiteral(external_name, null), + }); + } + } try fwd.writeAll(";\n"); } @@ -1958,9 +2039,13 @@ pub const DeclGen = struct { try mod.markDeclAlive(decl); if (mod.decl_exports.get(decl_index)) |exports| { - try writer.print("{}", .{exports.items[export_index].opts.name.fmt(&mod.intern_pool)}); + try writer.print("{ }", .{ + fmtIdent(mod.intern_pool.stringToSlice(exports.items[export_index].opts.name)), + }); } else if (decl.getExternDecl(mod).unwrap()) |extern_decl_index| { - try writer.print("{}", .{mod.declPtr(extern_decl_index).name.fmt(&mod.intern_pool)}); + try writer.print("{ }", .{ + fmtIdent(mod.intern_pool.stringToSlice(mod.declPtr(extern_decl_index).name)), + }); } else { // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case), // expand to 3x the length of its input, but let's cut it off at a much shorter limit. @@ -2592,7 +2677,10 @@ fn genExports(o: *Object) !void { const mod = o.dg.module; const ip = &mod.intern_pool; - const decl_index = o.dg.pass.decl; + const decl_index = switch (o.dg.pass) { + .decl => |decl| decl, + .anon, .flush => return, + }; const decl = mod.declPtr(decl_index); const tv: TypedValue = .{ .ty = decl.ty, .val = Value.fromInterned((try decl.internValue(mod))) }; const fwd = o.dg.fwd_decl.writer(); @@ -2600,42 +2688,50 @@ fn genExports(o: *Object) !void { const exports = mod.decl_exports.get(decl_index) orelse return; if (exports.items.len < 2) return; - switch (ip.indexToKey(tv.val.toIntern())) { - .func => { - for (exports.items[1..], 1..) |@"export", i| { - try fwd.writeAll("zig_export("); - if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage_fn "); - try o.dg.renderFunctionSignature(fwd, decl_index, .forward, .{ .export_index = @as(u32, @intCast(i)) }); - try fwd.print(", {s}, {s});\n", .{ - fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null), - fmtStringLiteral(ip.stringToSlice(@"export".opts.name), null), - }); - } + const is_variable_const = switch (ip.indexToKey(tv.val.toIntern())) { + .func => return for (exports.items[1..], 1..) |@"export", i| { + try fwd.writeAll("zig_extern "); + if (@"export".opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage_fn "); + try o.dg.renderFunctionSignature( + fwd, + decl_index, + .forward, + .{ .export_index = @intCast(i) }, + ); + try fwd.writeAll(";\n"); }, .extern_func => { // TODO: when sema allows re-exporting extern decls unreachable; }, - .variable => |variable| { - for (exports.items[1..], 1..) |@"export", i| { - try fwd.writeAll("zig_export("); - if (exports.items[i].opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage "); - const alias = ip.stringToSlice(@"export".opts.name); - try o.dg.renderTypeAndName( - fwd, - decl.ty, - .{ .identifier = alias }, - CQualifiers.init(.{ .@"const" = variable.is_const }), - decl.alignment, - .complete, - ); - try fwd.print(", {s}, {s});\n", .{ - fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null), - fmtStringLiteral(alias, null), - }); - } - }, - else => {}, + .variable => |variable| variable.is_const, + else => true, + }; + for (exports.items[1..]) |@"export"| { + try fwd.writeAll("zig_extern "); + if (@"export".opts.linkage == .Weak) try fwd.writeAll("zig_weak_linkage "); + const export_name = ip.stringToSlice(@"export".opts.name); + try o.dg.renderTypeAndName( + fwd, + decl.ty, + .{ .identifier = export_name }, + CQualifiers.init(.{ .@"const" = is_variable_const }), + decl.alignment, + .complete, + ); + if (isMangledIdent(export_name, true)) { + try fwd.print(" zig_mangled_export({ }, {s}, {s})", .{ + fmtIdent(export_name), + fmtStringLiteral(export_name, null), + fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null), + }); + } else { + try fwd.print(" zig_export({s}, {s})", .{ + fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null), + fmtStringLiteral(export_name, null), + }); + } + try fwd.writeAll(";\n"); } } @@ -2707,12 +2803,12 @@ pub fn genLazyFn(o: *Object, lazy_fn: LazyFnMap.Entry) !void { fwd_decl_writer, fn_decl_index, .forward, - .{ .string = fn_name }, + .{ .ident = fn_name }, ); try fwd_decl_writer.writeAll(";\n"); try w.print("static zig_{s} ", .{@tagName(key)}); - try o.dg.renderFunctionSignature(w, fn_decl_index, .complete, .{ .string = fn_name }); + try o.dg.renderFunctionSignature(w, fn_decl_index, .complete, .{ .ident = fn_name }); try w.writeAll(" {\n return "); try o.dg.renderDeclName(w, fn_decl_index, 0); try w.writeByte('('); @@ -2745,6 +2841,7 @@ pub fn genFunc(f: *Function) !void { const is_global = o.dg.declIsGlobal(tv); const fwd_decl_writer = o.dg.fwd_decl.writer(); try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static "); + if (mod.decl_exports.get(decl_index)) |exports| if (exports.items[0].opts.linkage == .Weak) try fwd_decl_writer.writeAll("zig_weak_linkage_fn "); try o.dg.renderFunctionSignature(fwd_decl_writer, decl_index, .forward, .{ .export_index = 0 }); @@ -2831,12 +2928,12 @@ pub fn genDecl(o: *Object) !void { try fwd_decl_writer.writeAll(";\n"); try genExports(o); } else if (tv.val.getVariable(mod)) |variable| { - try o.dg.renderFwdDecl(decl_index, variable); + try o.dg.renderFwdDecl(decl_index, variable, .final); try genExports(o); if (variable.is_extern) return; - const is_global = o.dg.declIsGlobal(tv) or variable.is_extern; + const is_global = variable.is_extern or o.dg.declIsGlobal(tv); const w = o.writer(); if (!is_global) try w.writeAll("static "); if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage "); @@ -2853,7 +2950,7 @@ pub fn genDecl(o: *Object) !void { } else { const is_global = o.dg.module.decl_exports.contains(decl_index); const decl_c_value = .{ .decl = decl_index }; - return genDeclValue(o, tv, is_global, decl_c_value, decl.alignment, decl.@"linksection"); + try genDeclValue(o, tv, is_global, decl_c_value, decl.alignment, decl.@"linksection"); } } @@ -2870,10 +2967,26 @@ pub fn genDeclValue( try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static "); try o.dg.renderTypeAndName(fwd_decl_writer, tv.ty, decl_c_value, Const, alignment, .complete); + switch (o.dg.pass) { + .decl => |decl_index| { + if (mod.decl_exports.get(decl_index)) |exports| { + const export_name = mod.intern_pool.stringToSlice(exports.items[0].opts.name); + if (isMangledIdent(export_name, true)) { + try fwd_decl_writer.print(" zig_mangled_final({ }, {s})", .{ + fmtIdent(export_name), fmtStringLiteral(export_name, null), + }); + } + } + }, + .anon => {}, + .flush => unreachable, + } try fwd_decl_writer.writeAll(";\n"); + try genExports(o); const w = o.writer(); if (!is_global) try w.writeAll("static "); + if (mod.intern_pool.stringToSliceUnwrap(link_section)) |s| try w.print("zig_linksection(\"{s}\", ", .{s}); try o.dg.renderTypeAndName(w, tv.ty, decl_c_value, Const, alignment, .complete); @@ -2897,13 +3010,10 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void { const writer = dg.fwd_decl.writer(); switch (tv.ty.zigTypeTag(mod)) { - .Fn => { - const is_global = dg.declIsGlobal(tv); - if (is_global) { - try writer.writeAll("zig_extern "); - try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 }); - try dg.fwd_decl.appendSlice(";\n"); - } + .Fn => if (dg.declIsGlobal(tv)) { + try writer.writeAll("zig_extern "); + try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 }); + try dg.fwd_decl.appendSlice(";\n"); }, else => {}, } @@ -6895,9 +7005,9 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { try f.writeCValue(writer, accum, .Other); switch (op) { .float_op => |func| { - try writer.writeAll(" = zig_libc_name_"); + try writer.writeAll(" = zig_float_fn_"); try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty); - try writer.print("({s})(", .{func.operation}); + try writer.print("_{s}(", .{func.operation}); try f.writeCValue(writer, accum, .FunctionArgument); try writer.writeAll(", "); try f.writeCValue(writer, operand, .Other); @@ -7229,11 +7339,9 @@ fn unFloatOp(f: *Function, inst: Air.Inst.Index, operand: CValue, ty: Type, oper const v = try Vectorize.start(f, inst, writer, ty); try f.writeCValue(writer, local, .Other); try v.elem(f, writer); - try writer.writeAll(" = zig_libc_name_"); + try writer.writeAll(" = zig_float_fn_"); try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty); - try writer.writeByte('('); - try writer.writeAll(operation); - try writer.writeAll(")("); + try writer.print("_{s}(", .{operation}); try f.writeCValue(writer, operand, .FunctionArgument); try v.elem(f, writer); try writer.writeAll(");\n"); @@ -7268,11 +7376,9 @@ fn airBinFloatOp(f: *Function, inst: Air.Inst.Index, operation: []const u8) !CVa const v = try Vectorize.start(f, inst, writer, inst_ty); try f.writeCValue(writer, local, .Other); try v.elem(f, writer); - try writer.writeAll(" = zig_libc_name_"); + try writer.writeAll(" = zig_float_fn_"); try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty); - try writer.writeByte('('); - try writer.writeAll(operation); - try writer.writeAll(")("); + try writer.print("_{s}(", .{operation}); try f.writeCValue(writer, lhs, .FunctionArgument); try v.elem(f, writer); try writer.writeAll(", "); @@ -7302,9 +7408,9 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { const v = try Vectorize.start(f, inst, writer, inst_ty); try f.writeCValue(writer, local, .Other); try v.elem(f, writer); - try writer.writeAll(" = zig_libc_name_"); + try writer.writeAll(" = zig_float_fn_"); try f.object.dg.renderTypeForBuiltinFnName(writer, inst_scalar_ty); - try writer.writeAll("(fma)("); + try writer.writeAll("_fma("); try f.writeCValue(writer, mulend1, .FunctionArgument); try v.elem(f, writer); try writer.writeAll(", "); @@ -7390,11 +7496,11 @@ fn airCVaCopy(f: *Function, inst: Air.Inst.Index) !CValue { fn toMemoryOrder(order: std.builtin.AtomicOrder) [:0]const u8 { return switch (order) { // Note: unordered is actually even less atomic than relaxed - .Unordered, .Monotonic => "memory_order_relaxed", - .Acquire => "memory_order_acquire", - .Release => "memory_order_release", - .AcqRel => "memory_order_acq_rel", - .SeqCst => "memory_order_seq_cst", + .Unordered, .Monotonic => "zig_memory_order_relaxed", + .Acquire => "zig_memory_order_acquire", + .Release => "zig_memory_order_release", + .AcqRel => "zig_memory_order_acq_rel", + .SeqCst => "zig_memory_order_seq_cst", }; } |
