Skip to content

Commit 57725d0

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 976b647 commit 57725d0

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
@@ -1605,7 +1605,7 @@ pub const Cpu = struct {
16051605

16061606
/// Returns the array of `Arch` to which a specific `std.builtin.CallingConvention` applies.
16071607
/// Asserts that `cc` is not `.auto`, `.@"async"`, `.naked`, or `.@"inline"`.
1608-
pub fn fromCallconv(cc: std.builtin.NewCallingConvention) []const Arch {
1608+
pub fn fromCallconv(cc: std.builtin.CallingConvention) []const Arch {
16091609
return switch (cc) {
16101610
.auto,
16111611
.@"async",
@@ -3022,7 +3022,7 @@ pub fn cTypePreferredAlignment(target: Target, c_type: CType) u16 {
30223022
);
30233023
}
30243024

3025-
pub fn defaultCCallingConvention(target: Target) ?std.builtin.NewCallingConvention {
3025+
pub fn defaultCCallingConvention(target: Target) ?std.builtin.CallingConvention {
30263026
return switch (target.cpu.arch) {
30273027
.x86_64 => switch (target.os.tag) {
30283028
.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
@@ -1977,7 +1977,7 @@ pub const Key = union(enum) {
19771977
/// Tells whether a parameter is noalias. See `paramIsNoalias` helper
19781978
/// method for accessing this.
19791979
noalias_bits: u32,
1980-
cc: std.builtin.NewCallingConvention,
1980+
cc: std.builtin.CallingConvention,
19811981
is_var_args: bool,
19821982
is_generic: bool,
19831983
is_noinline: bool,
@@ -8514,7 +8514,7 @@ pub const GetFuncTypeKey = struct {
85148514
comptime_bits: u32 = 0,
85158515
noalias_bits: u32 = 0,
85168516
/// `null` means generic.
8517-
cc: ?std.builtin.NewCallingConvention = .auto,
8517+
cc: ?std.builtin.CallingConvention = .auto,
85188518
is_var_args: bool = false,
85198519
is_generic: bool = false,
85208520
is_noinline: bool = false,
@@ -8656,7 +8656,7 @@ pub const GetFuncDeclKey = struct {
86568656
rbrace_line: u32,
86578657
lbrace_column: u32,
86588658
rbrace_column: u32,
8659-
cc: ?std.builtin.NewCallingConvention,
8659+
cc: ?std.builtin.CallingConvention,
86608660
is_noinline: bool,
86618661
};
86628662

@@ -8721,7 +8721,7 @@ pub const GetFuncDeclIesKey = struct {
87218721
comptime_bits: u32,
87228722
bare_return_type: Index,
87238723
/// null means generic.
8724-
cc: ?std.builtin.NewCallingConvention,
8724+
cc: ?std.builtin.CallingConvention,
87258725
/// null means generic.
87268726
alignment: ?Alignment,
87278727
section_is_generic: bool,
@@ -8936,7 +8936,7 @@ pub const GetFuncInstanceKey = struct {
89368936
comptime_args: []const Index,
89378937
noalias_bits: u32,
89388938
bare_return_type: Index,
8939-
cc: std.builtin.NewCallingConvention,
8939+
cc: std.builtin.CallingConvention,
89408940
alignment: Alignment,
89418941
section: OptionalNullTerminatedString,
89428942
is_noinline: bool,
@@ -12214,41 +12214,41 @@ pub fn getErrorValueIfExists(ip: *const InternPool, name: NullTerminatedString)
1221412214
}
1221512215

1221612216
const PackedCallingConvention = packed struct(u18) {
12217-
tag: std.builtin.NewCallingConvention.Tag,
12217+
tag: std.builtin.CallingConvention.Tag,
1221812218
/// May be ignored depending on `tag`.
1221912219
incoming_stack_alignment: Alignment,
1222012220
/// Interpretation depends on `tag`.
1222112221
extra: u4,
1222212222

12223-
fn pack(cc: std.builtin.NewCallingConvention) PackedCallingConvention {
12223+
fn pack(cc: std.builtin.CallingConvention) PackedCallingConvention {
1222412224
return switch (cc) {
1222512225
inline else => |pl, tag| switch (@TypeOf(pl)) {
1222612226
void => .{
1222712227
.tag = tag,
1222812228
.incoming_stack_alignment = .none, // unused
1222912229
.extra = 0, // unused
1223012230
},
12231-
std.builtin.NewCallingConvention.CommonOptions => .{
12231+
std.builtin.CallingConvention.CommonOptions => .{
1223212232
.tag = tag,
1223312233
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1223412234
.extra = 0, // unused
1223512235
},
12236-
std.builtin.NewCallingConvention.X86RegparmOptions => .{
12236+
std.builtin.CallingConvention.X86RegparmOptions => .{
1223712237
.tag = tag,
1223812238
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1223912239
.extra = pl.register_params,
1224012240
},
12241-
std.builtin.NewCallingConvention.ArmInterruptOptions => .{
12241+
std.builtin.CallingConvention.ArmInterruptOptions => .{
1224212242
.tag = tag,
1224312243
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1224412244
.extra = @intFromEnum(pl.type),
1224512245
},
12246-
std.builtin.NewCallingConvention.MipsInterruptOptions => .{
12246+
std.builtin.CallingConvention.MipsInterruptOptions => .{
1224712247
.tag = tag,
1224812248
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1224912249
.extra = @intFromEnum(pl.mode),
1225012250
},
12251-
std.builtin.NewCallingConvention.RiscvInterruptOptions => .{
12251+
std.builtin.CallingConvention.RiscvInterruptOptions => .{
1225212252
.tag = tag,
1225312253
.incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1225412254
.extra = @intFromEnum(pl.level),
@@ -12258,30 +12258,30 @@ const PackedCallingConvention = packed struct(u18) {
1225812258
};
1225912259
}
1226012260

12261-
fn unpack(cc: PackedCallingConvention) std.builtin.NewCallingConvention {
12261+
fn unpack(cc: PackedCallingConvention) std.builtin.CallingConvention {
1226212262
@setEvalBranchQuota(400_000);
1226312263
return switch (cc.tag) {
1226412264
inline else => |tag| @unionInit(
12265-
std.builtin.NewCallingConvention,
12265+
std.builtin.CallingConvention,
1226612266
@tagName(tag),
12267-
switch (std.meta.FieldType(std.builtin.NewCallingConvention, tag)) {
12267+
switch (std.meta.FieldType(std.builtin.CallingConvention, tag)) {
1226812268
void => {},
12269-
std.builtin.NewCallingConvention.CommonOptions => .{
12269+
std.builtin.CallingConvention.CommonOptions => .{
1227012270
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1227112271
},
12272-
std.builtin.NewCallingConvention.X86RegparmOptions => .{
12272+
std.builtin.CallingConvention.X86RegparmOptions => .{
1227312273
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1227412274
.register_params = @intCast(cc.extra),
1227512275
},
12276-
std.builtin.NewCallingConvention.ArmInterruptOptions => .{
12276+
std.builtin.CallingConvention.ArmInterruptOptions => .{
1227712277
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1227812278
.type = @enumFromInt(cc.extra),
1227912279
},
12280-
std.builtin.NewCallingConvention.MipsInterruptOptions => .{
12280+
std.builtin.CallingConvention.MipsInterruptOptions => .{
1228112281
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1228212282
.mode = @enumFromInt(cc.extra),
1228312283
},
12284-
std.builtin.NewCallingConvention.RiscvInterruptOptions => .{
12284+
std.builtin.CallingConvention.RiscvInterruptOptions => .{
1228512285
.incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1228612286
.level = @enumFromInt(cc.extra),
1228712287
},

0 commit comments

Comments
 (0)