From 51706af908e0c6acb822ef36760b7fe31faf62a6 Mon Sep 17 00:00:00 2001 From: mlugg Date: Sat, 28 Sep 2024 02:10:25 +0100 Subject: compiler: introduce new `CallingConvention` This commit begins implementing accepted proposal #21209 by making `std.builtin.CallingConvention` a tagged union. The stage1 dance here is a little convoluted. This commit introduces the new type as `NewCallingConvention`, keeping the old `CallingConvention` around. The compiler uses `std.builtin.NewCallingConvention` exclusively, but when fetching the type from `std` when running the compiler (e.g. with `getBuiltinType`), the name `CallingConvention` is used. This allows a prior build of Zig to be used to build this commit. The next commit will update `zig1.wasm`, and then the compiler and standard library can be updated to completely replace `CallingConvention` with `NewCallingConvention`. The second half of #21209 is to remove `@setAlignStack`, which will be implemented in another commit after updating `zig1.wasm`. --- src/codegen/c.zig | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/codegen/c.zig') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 98c3c42deb..816913d4bd 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1783,7 +1783,7 @@ pub const DeclGen = struct { const fn_ctype = try dg.ctypeFromType(fn_ty, kind); const fn_info = zcu.typeToFunc(fn_ty).?; - if (fn_info.cc == .Naked) { + if (fn_info.cc == .naked) { switch (kind) { .forward => try w.writeAll("zig_naked_decl "), .complete => try w.writeAll("zig_naked "), @@ -1796,7 +1796,7 @@ pub const DeclGen = struct { var trailing = try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, fn_ctype, .suffix, .{}); - if (toCallingConvention(fn_info.cc)) |call_conv| { + if (toCallingConvention(fn_info.cc, zcu)) |call_conv| { try w.print("{}zig_callconv({s})", .{ trailing, call_conv }); trailing = .maybe_space; } @@ -7604,12 +7604,17 @@ fn writeMemoryOrder(w: anytype, order: std.builtin.AtomicOrder) !void { return w.writeAll(toMemoryOrder(order)); } -fn toCallingConvention(call_conv: std.builtin.CallingConvention) ?[]const u8 { - return switch (call_conv) { - .Stdcall => "stdcall", - .Fastcall => "fastcall", - .Vectorcall => "vectorcall", - else => null, +fn toCallingConvention(cc: std.builtin.NewCallingConvention, zcu: *Zcu) ?[]const u8 { + return switch (cc) { + .auto, .naked => null, + .x86_stdcall => "stdcall", + .x86_fastcall => "fastcall", + .x86_vectorcall, .x86_64_vectorcall => "vectorcall", + else => { + // `Zcu.callconvSupported` means this must be the C callconv. + assert(cc.eql(zcu.getTarget().defaultCCallingConvention().?)); + return null; + }, }; } -- cgit v1.2.3