Skip to content

Commit bc797a9

Browse files
committed
std: update for new CallingConvention
The old `CallingConvention` type is replaced with the new `NewCallingConvention`. References to `NewCallingConvention` in the compiler are updated accordingly. In addition, a few parts of the standard library are updated to use the new type correctly.
1 parent 36405b9 commit bc797a9

File tree

19 files changed

+109
-160
lines changed

19 files changed

+109
-160
lines changed

lib/std/Target.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ pub const Cpu = struct {
16121612

16131613
/// Returns the array of `Arch` to which a specific `std.builtin.CallingConvention` applies.
16141614
/// Asserts that `cc` is not `.auto`, `.@"async"`, `.naked`, or `.@"inline"`.
1615-
pub fn fromCallconv(cc: std.builtin.NewCallingConvention) []const Arch {
1615+
pub fn fromCallconv(cc: std.builtin.CallingConvention) []const Arch {
16161616
return switch (cc) {
16171617
.auto,
16181618
.@"async",
@@ -3032,7 +3032,7 @@ pub fn cTypePreferredAlignment(target: Target, c_type: CType) u16 {
30323032
);
30333033
}
30343034

3035-
pub fn defaultCCallingConvention(target: Target) ?std.builtin.NewCallingConvention {
3035+
pub fn defaultCCallingConvention(target: Target) ?std.builtin.CallingConvention {
30363036
return switch (target.cpu.arch) {
30373037
.x86_64 => switch (target.os.tag) {
30383038
.windows, .uefi => .{ .x86_64_win = .{} },

lib/std/builtin.zig

Lines changed: 31 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -160,134 +160,82 @@ pub const OptimizeMode = enum {
160160
/// Deprecated; use OptimizeMode.
161161
pub const Mode = OptimizeMode;
162162

163-
/// This data structure is used by the Zig language code generation and
164-
/// therefore must be kept in sync with the compiler implementation.
165-
pub const CallingConvention = enum(u8) {
166-
/// This is the default Zig calling convention used when not using `export` on `fn`
167-
/// and no other calling convention is specified.
168-
Unspecified,
169-
/// Matches the C ABI for the target.
170-
/// This is the default calling convention when using `export` on `fn`
171-
/// and no other calling convention is specified.
172-
C,
173-
/// This makes a function not have any function prologue or epilogue,
174-
/// making the function itself uncallable in regular Zig code.
175-
/// This can be useful when integrating with assembly.
176-
Naked,
177-
/// Functions with this calling convention are called asynchronously,
178-
/// as if called as `async function()`.
179-
Async,
180-
/// Functions with this calling convention are inlined at all call sites.
181-
Inline,
182-
/// x86-only.
183-
Interrupt,
184-
Signal,
185-
/// x86-only.
186-
Stdcall,
187-
/// x86-only.
188-
Fastcall,
189-
/// x86-only.
190-
Vectorcall,
191-
/// x86-only.
192-
Thiscall,
193-
/// ARM Procedure Call Standard (obsolete)
194-
/// ARM-only.
195-
APCS,
196-
/// ARM Architecture Procedure Call Standard (current standard)
197-
/// ARM-only.
198-
AAPCS,
199-
/// ARM Architecture Procedure Call Standard Vector Floating-Point
200-
/// ARM-only.
201-
AAPCSVFP,
202-
/// x86-64-only.
203-
SysV,
204-
/// x86-64-only.
205-
Win64,
206-
/// AMD GPU, NVPTX, or SPIR-V kernel
207-
Kernel,
208-
// Vulkan-only
209-
Fragment,
210-
Vertex,
211-
};
212-
213163
/// The calling convention of a function defines how arguments and return values are passed, as well
214164
/// as any other requirements which callers and callees must respect, such as register preservation
215165
/// and stack alignment.
216166
///
217167
/// This data structure is used by the Zig language code generation and
218168
/// therefore must be kept in sync with the compiler implementation.
219-
///
220-
/// TODO: this will be renamed `CallingConvention` after an initial zig1.wasm update.
221-
pub const NewCallingConvention = union(enum(u8)) {
222-
pub const Tag = @typeInfo(NewCallingConvention).@"union".tag_type.?;
169+
pub const CallingConvention = union(enum(u8)) {
170+
pub const Tag = @typeInfo(CallingConvention).@"union".tag_type.?;
223171

224172
/// This is an alias for the default C calling convention for this target.
225173
/// Functions marked as `extern` or `export` are given this calling convention by default.
226174
pub const c = builtin.target.defaultCCallingConvention().?;
227175

228-
pub const winapi: NewCallingConvention = switch (builtin.target.arch) {
176+
pub const winapi: CallingConvention = switch (builtin.target.arch) {
229177
.x86_64 => .{ .x86_64_win = .{} },
230178
.x86 => .{ .x86_stdcall = .{} },
231179
.aarch64, .aarch64_be => .{ .aarch64_aapcs_win = .{} },
232180
.arm, .armeb, .thumb, .thumbeb => .{ .arm_aapcs_vfp = .{} },
233181
else => unreachable,
234182
};
235183

236-
pub const kernel: NewCallingConvention = switch (builtin.target.cpu.arch) {
184+
pub const kernel: CallingConvention = switch (builtin.target.cpu.arch) {
237185
.amdgcn => .amdgcn_kernel,
238186
.nvptx, .nvptx64 => .nvptx_kernel,
239187
.spirv, .spirv32, .spirv64 => .spirv_kernel,
240188
else => unreachable,
241189
};
242190

243191
/// Deprecated; use `.auto`.
244-
pub const Unspecified: NewCallingConvention = .auto;
192+
pub const Unspecified: CallingConvention = .auto;
245193
/// Deprecated; use `.c`.
246-
pub const C: NewCallingConvention = .c;
194+
pub const C: CallingConvention = .c;
247195
/// Deprecated; use `.naked`.
248-
pub const Naked: NewCallingConvention = .naked;
196+
pub const Naked: CallingConvention = .naked;
249197
/// Deprecated; use `.@"async"`.
250-
pub const Async: NewCallingConvention = .@"async";
198+
pub const Async: CallingConvention = .@"async";
251199
/// Deprecated; use `.@"inline"`.
252-
pub const Inline: NewCallingConvention = .@"inline";
200+
pub const Inline: CallingConvention = .@"inline";
253201
/// Deprecated; use `.x86_64_interrupt`, `.x86_interrupt`, or `.avr_interrupt`.
254-
pub const Interrupt: NewCallingConvention = switch (builtin.target.cpu.arch) {
202+
pub const Interrupt: CallingConvention = switch (builtin.target.cpu.arch) {
255203
.x86_64 => .{ .x86_64_interrupt = .{} },
256204
.x86 => .{ .x86_interrupt = .{} },
257205
.avr => .avr_interrupt,
258206
else => unreachable,
259207
};
260208
/// Deprecated; use `.avr_signal`.
261-
pub const Signal: NewCallingConvention = .avr_signal;
209+
pub const Signal: CallingConvention = .avr_signal;
262210
/// Deprecated; use `.x86_stdcall`.
263-
pub const Stdcall: NewCallingConvention = .{ .x86_stdcall = .{} };
211+
pub const Stdcall: CallingConvention = .{ .x86_stdcall = .{} };
264212
/// Deprecated; use `.x86_fastcall`.
265-
pub const Fastcall: NewCallingConvention = .{ .x86_fastcall = .{} };
213+
pub const Fastcall: CallingConvention = .{ .x86_fastcall = .{} };
266214
/// Deprecated; use `.x86_64_vectorcall`, `.x86_vectorcall`, or `aarch64_vfabi`.
267-
pub const Vectorcall: NewCallingConvention = switch (builtin.target.cpu.arch) {
215+
pub const Vectorcall: CallingConvention = switch (builtin.target.cpu.arch) {
268216
.x86_64 => .{ .x86_64_vectorcall = .{} },
269217
.x86 => .{ .x86_vectorcall = .{} },
270218
.aarch64, .aarch64_be => .{ .aarch64_vfabi = .{} },
271219
else => unreachable,
272220
};
273221
/// Deprecated; use `.x86_thiscall`.
274-
pub const Thiscall: NewCallingConvention = .{ .x86_thiscall = .{} };
222+
pub const Thiscall: CallingConvention = .{ .x86_thiscall = .{} };
275223
/// Deprecated; use `.arm_apcs`.
276-
pub const APCS: NewCallingConvention = .{ .arm_apcs = .{} };
224+
pub const APCS: CallingConvention = .{ .arm_apcs = .{} };
277225
/// Deprecated; use `.arm_aapcs`.
278-
pub const AAPCS: NewCallingConvention = .{ .arm_aapcs = .{} };
226+
pub const AAPCS: CallingConvention = .{ .arm_aapcs = .{} };
279227
/// Deprecated; use `.arm_aapcs_vfp`.
280-
pub const AAPCSVFP: NewCallingConvention = .{ .arm_aapcs_vfp = .{} };
228+
pub const AAPCSVFP: CallingConvention = .{ .arm_aapcs_vfp = .{} };
281229
/// Deprecated; use `.x86_64_sysv`.
282-
pub const SysV: NewCallingConvention = .{ .x86_64_sysv = .{} };
230+
pub const SysV: CallingConvention = .{ .x86_64_sysv = .{} };
283231
/// Deprecated; use `.x86_64_win`.
284-
pub const Win64: NewCallingConvention = .{ .x86_64_win = .{} };
232+
pub const Win64: CallingConvention = .{ .x86_64_win = .{} };
285233
/// Deprecated; use `.kernel`.
286-
pub const Kernel: NewCallingConvention = .kernel;
234+
pub const Kernel: CallingConvention = .kernel;
287235
/// Deprecated; use `.spirv_fragment`.
288-
pub const Fragment: NewCallingConvention = .spirv_fragment;
236+
pub const Fragment: CallingConvention = .spirv_fragment;
289237
/// Deprecated; use `.spirv_vertex`.
290-
pub const Vertex: NewCallingConvention = .spirv_vertex;
238+
pub const Vertex: CallingConvention = .spirv_vertex;
291239

292240
/// The default Zig calling convention when neither `export` nor `inline` is specified.
293241
/// This calling convention makes no guarantees about stack alignment, registers, etc.
@@ -535,9 +483,16 @@ pub const NewCallingConvention = union(enum(u8)) {
535483
/// Asserts that `cc` is not `.auto`, `.@"async"`, `.naked`, or `.@"inline"`.
536484
pub const archs = std.Target.Cpu.Arch.fromCallconv;
537485

538-
pub fn eql(a: NewCallingConvention, b: NewCallingConvention) bool {
486+
pub fn eql(a: CallingConvention, b: CallingConvention) bool {
539487
return std.meta.eql(a, b);
540488
}
489+
490+
pub fn withStackAlign(cc: CallingConvention, incoming_stack_alignment: u64) CallingConvention {
491+
const tag: CallingConvention.Tag = cc;
492+
var result = cc;
493+
@field(result, tag).incoming_stack_alignment = incoming_stack_alignment;
494+
return result;
495+
}
541496
};
542497

543498
/// This data structure is used by the Zig language code generation and

lib/std/crypto/25519/field.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const NonCanonicalError = crypto.errors.NonCanonicalError;
66
const NotSquareError = crypto.errors.NotSquareError;
77

88
// Inline conditionally, when it can result in large code generation.
9-
const bloaty_inline = switch (builtin.mode) {
10-
.ReleaseSafe, .ReleaseFast => .Inline,
11-
.Debug, .ReleaseSmall => .Unspecified,
9+
const bloaty_inline: std.builtin.CallingConvention = switch (builtin.mode) {
10+
.ReleaseSafe, .ReleaseFast => .@"inline",
11+
.Debug, .ReleaseSmall => .auto,
1212
};
1313

1414
pub const Fe = struct {

lib/std/os/windows.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,10 +2824,7 @@ pub const STD_OUTPUT_HANDLE = maxInt(DWORD) - 11 + 1;
28242824
/// The standard error device. Initially, this is the active console screen buffer, CONOUT$.
28252825
pub const STD_ERROR_HANDLE = maxInt(DWORD) - 12 + 1;
28262826

2827-
pub const WINAPI: std.builtin.CallingConvention = if (native_arch == .x86)
2828-
.Stdcall
2829-
else
2830-
.C;
2827+
pub const WINAPI: std.builtin.CallingConvention = .winapi;
28312828

28322829
pub const BOOL = c_int;
28332830
pub const BOOLEAN = BYTE;

lib/std/start.zig

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ comptime {
5555
if (builtin.link_libc and @hasDecl(root, "main")) {
5656
if (native_arch.isWasm()) {
5757
@export(&mainWithoutEnv, .{ .name = "main" });
58-
} else if (@typeInfo(@TypeOf(root.main)).@"fn".calling_convention != .C) {
58+
} else if (!@typeInfo(@TypeOf(root.main)).@"fn".calling_convention.eql(.c)) {
5959
@export(&main, .{ .name = "main" });
6060
}
6161
} else if (native_os == .windows) {
@@ -102,12 +102,11 @@ fn main2() callconv(.C) c_int {
102102
return 0;
103103
}
104104

105-
fn _start2() callconv(.C) noreturn {
105+
fn _start2() callconv(.withStackAlign(.c, 1)) noreturn {
106106
callMain2();
107107
}
108108

109109
fn callMain2() noreturn {
110-
@setAlignStack(16);
111110
root.main();
112111
exit2(0);
113112
}
@@ -428,8 +427,7 @@ fn _start() callconv(.Naked) noreturn {
428427
);
429428
}
430429

431-
fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
432-
@setAlignStack(16);
430+
fn WinStartup() callconv(.withStackAlign(.winapi, 1)) noreturn {
433431
if (!builtin.single_threaded and !builtin.link_libc) {
434432
_ = @import("os/windows/tls.zig");
435433
}
@@ -439,8 +437,7 @@ fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
439437
std.os.windows.ntdll.RtlExitUserProcess(callMain());
440438
}
441439

442-
fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
443-
@setAlignStack(16);
440+
fn wWinMainCRTStartup() callconv(.withStackAlign(.winapi, 1)) noreturn {
444441
if (!builtin.single_threaded and !builtin.link_libc) {
445442
_ = @import("os/windows/tls.zig");
446443
}

src/InternPool.zig

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,7 @@ pub const Key = union(enum) {
19881988
/// Tells whether a parameter is noalias. See `paramIsNoalias` helper
19891989
/// method for accessing this.
19901990
noalias_bits: u32,
1991-
cc: std.builtin.NewCallingConvention,
1991+
cc: std.builtin.CallingConvention,
19921992
is_var_args: bool,
19931993
is_generic: bool,
19941994
is_noinline: bool,
@@ -8526,7 +8526,7 @@ pub const GetFuncTypeKey = struct {
85268526
comptime_bits: u32 = 0,
85278527
noalias_bits: u32 = 0,
85288528
/// `null` means generic.
8529-
cc: ?std.builtin.NewCallingConvention = .auto,
8529+
cc: ?std.builtin.CallingConvention = .auto,
85308530
is_var_args: bool = false,
85318531
is_generic: bool = false,
85328532
is_noinline: bool = false,
@@ -8668,7 +8668,7 @@ pub const GetFuncDeclKey = struct {
86688668
rbrace_line: u32,
86698669
lbrace_column: u32,
86708670
rbrace_column: u32,
8671-
cc: ?std.builtin.NewCallingConvention,
8671+
cc: ?std.builtin.CallingConvention,
86728672
is_noinline: bool,
86738673
};
86748674

@@ -8733,7 +8733,7 @@ pub const GetFuncDeclIesKey = struct {
87338733
comptime_bits: u32,
87348734
bare_return_type: Index,
87358735
/// null means generic.
8736-
cc: ?std.builtin.NewCallingConvention,
8736+
cc: ?std.builtin.CallingConvention,
87378737
/// null means generic.
87388738
alignment: ?Alignment,
87398739
section_is_generic: bool,
@@ -8948,7 +8948,7 @@ pub const GetFuncInstanceKey = struct {
89488948
comptime_args: []const Index,
89498949
noalias_bits: u32,
89508950
bare_return_type: Index,
8951-
cc: std.builtin.NewCallingConvention,
8951+
cc: std.builtin.CallingConvention,
89528952
alignment: Alignment,
89538953
section: OptionalNullTerminatedString,
89548954
is_noinline: bool,
@@ -12226,41 +12226,41 @@ pub fn getErrorValueIfExists(ip: *const InternPool, name: NullTerminatedString)
1222612226
}
1222712227

1222812228
const PackedCallingConvention = packed struct(u18) {
12229-
tag: std.builtin.NewCallingConvention.Tag,
12229+
tag: std.builtin.CallingConvention.Tag,
1223012230
/// May be ignored depending on `tag`.
1223112231
incoming_stack_alignment: Alignment,
1223212232
/// Interpretation depends on `tag`.
1223312233
extra: u4,
1223412234

12235-
fn pack(cc: std.builtin.NewCallingConvention) PackedCallingConvention {
12235+
fn pack(cc: std.builtin.CallingConvention) PackedCallingConvention {
1223612236
return switch (cc) {
1223712237
inline else => |pl, tag| switch (@TypeOf(pl)) {
1223812238
void => .{
1223912239
.tag = tag,
1224012240
.incoming_stack_alignment = .none, // unused
1224112241
.extra = 0, // unused
1224212242
},
12243-
std.builtin.NewCallingConvention.CommonOptions => .{
12243+
std.builtin.CallingConvention.CommonOptions => .{
1224412244
.tag = tag,
1224512245
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1224612246
.extra = 0, // unused
1224712247
},
12248-
std.builtin.NewCallingConvention.X86RegparmOptions => .{
12248+
std.builtin.CallingConvention.X86RegparmOptions => .{
1224912249
.tag = tag,
1225012250
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1225112251
.extra = pl.register_params,
1225212252
},
12253-
std.builtin.NewCallingConvention.ArmInterruptOptions => .{
12253+
std.builtin.CallingConvention.ArmInterruptOptions => .{
1225412254
.tag = tag,
1225512255
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1225612256
.extra = @intFromEnum(pl.type),
1225712257
},
12258-
std.builtin.NewCallingConvention.MipsInterruptOptions => .{
12258+
std.builtin.CallingConvention.MipsInterruptOptions => .{
1225912259
.tag = tag,
1226012260
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1226112261
.extra = @intFromEnum(pl.mode),
1226212262
},
12263-
std.builtin.NewCallingConvention.RiscvInterruptOptions => .{
12263+
std.builtin.CallingConvention.RiscvInterruptOptions => .{
1226412264
.tag = tag,
1226512265
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1226612266
.extra = @intFromEnum(pl.level),
@@ -12270,30 +12270,30 @@ const PackedCallingConvention = packed struct(u18) {
1227012270
};
1227112271
}
1227212272

12273-
fn unpack(cc: PackedCallingConvention) std.builtin.NewCallingConvention {
12273+
fn unpack(cc: PackedCallingConvention) std.builtin.CallingConvention {
1227412274
@setEvalBranchQuota(400_000);
1227512275
return switch (cc.tag) {
1227612276
inline else => |tag| @unionInit(
12277-
std.builtin.NewCallingConvention,
12277+
std.builtin.CallingConvention,
1227812278
@tagName(tag),
12279-
switch (std.meta.FieldType(std.builtin.NewCallingConvention, tag)) {
12279+
switch (std.meta.FieldType(std.builtin.CallingConvention, tag)) {
1228012280
void => {},
12281-
std.builtin.NewCallingConvention.CommonOptions => .{
12281+
std.builtin.CallingConvention.CommonOptions => .{
1228212282
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1228312283
},
12284-
std.builtin.NewCallingConvention.X86RegparmOptions => .{
12284+
std.builtin.CallingConvention.X86RegparmOptions => .{
1228512285
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1228612286
.register_params = @intCast(cc.extra),
1228712287
},
12288-
std.builtin.NewCallingConvention.ArmInterruptOptions => .{
12288+
std.builtin.CallingConvention.ArmInterruptOptions => .{
1228912289
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1229012290
.type = @enumFromInt(cc.extra),
1229112291
},
12292-
std.builtin.NewCallingConvention.MipsInterruptOptions => .{
12292+
std.builtin.CallingConvention.MipsInterruptOptions => .{
1229312293
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1229412294
.mode = @enumFromInt(cc.extra),
1229512295
},
12296-
std.builtin.NewCallingConvention.RiscvInterruptOptions => .{
12296+
std.builtin.CallingConvention.RiscvInterruptOptions => .{
1229712297
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1229812298
.level = @enumFromInt(cc.extra),
1229912299
},

0 commit comments

Comments
 (0)