aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/c.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/codegen/c.zig')
-rw-r--r--src/codegen/c.zig260
1 files changed, 183 insertions, 77 deletions
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",
};
}