Skip to content

Commit

Permalink
Merge pull request #21697 from mlugg/callconv
Browse files Browse the repository at this point in the history
Replace `std.builtin.CallingConvention` with a tagged union, eliminating `@setAlignStack`
  • Loading branch information
mlugg authored Oct 23, 2024
2 parents 2d888a8 + f7d679c commit 6bf52b0
Show file tree
Hide file tree
Showing 79 changed files with 2,020 additions and 942 deletions.
3 changes: 2 additions & 1 deletion doc/langref/enum_export_error.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export fn entry(foo: Foo) void {
_ = foo;
}

// obj=parameter of type 'enum_export_error.Foo' not allowed in function with calling convention 'C'
// obj=parameter of type 'enum_export_error.Foo' not allowed in function with calling convention 'x86_64_sysv'
// target=x86_64-linux
68 changes: 60 additions & 8 deletions lib/compiler/aro_translate_c/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,26 @@ pub const Payload = struct {
is_var_args: bool,
name: ?[]const u8,
linksection_string: ?[]const u8,
explicit_callconv: ?std.builtin.CallingConvention,
explicit_callconv: ?CallingConvention,
params: []Param,
return_type: Node,
body: ?Node,
alignment: ?c_uint,
},

pub const CallingConvention = enum {
c,
x86_64_sysv,
x86_64_win,
x86_stdcall,
x86_fastcall,
x86_thiscall,
x86_vectorcall,
aarch64_vfabi,
arm_aapcs,
arm_aapcs_vfp,
m68k_rtd,
};
};

pub const Param = struct {
Expand Down Expand Up @@ -2812,14 +2826,52 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {
const callconv_expr = if (payload.explicit_callconv) |some| blk: {
_ = try c.addToken(.keyword_callconv, "callconv");
_ = try c.addToken(.l_paren, "(");
_ = try c.addToken(.period, ".");
const res = try c.addNode(.{
.tag = .enum_literal,
.main_token = try c.addTokenFmt(.identifier, "{s}", .{@tagName(some)}),
.data = undefined,
});
const cc_node = switch (some) {
.c => cc_node: {
_ = try c.addToken(.period, ".");
break :cc_node try c.addNode(.{
.tag = .enum_literal,
.main_token = try c.addToken(.identifier, "c"),
.data = undefined,
});
},
.x86_64_sysv,
.x86_64_win,
.x86_stdcall,
.x86_fastcall,
.x86_thiscall,
.x86_vectorcall,
.aarch64_vfabi,
.arm_aapcs,
.arm_aapcs_vfp,
.m68k_rtd,
=> cc_node: {
// .{ .foo = .{} }
_ = try c.addToken(.period, ".");
const outer_lbrace = try c.addToken(.l_brace, "{");
_ = try c.addToken(.period, ".");
_ = try c.addToken(.identifier, @tagName(some));
_ = try c.addToken(.equal, "=");
_ = try c.addToken(.period, ".");
const inner_lbrace = try c.addToken(.l_brace, "{");
_ = try c.addToken(.r_brace, "}");
_ = try c.addToken(.r_brace, "}");
break :cc_node try c.addNode(.{
.tag = .struct_init_dot_two,
.main_token = outer_lbrace,
.data = .{
.lhs = try c.addNode(.{
.tag = .struct_init_dot_two,
.main_token = inner_lbrace,
.data = .{ .lhs = 0, .rhs = 0 },
}),
.rhs = 0,
},
});
},
};
_ = try c.addToken(.r_paren, ")");
break :blk res;
break :blk cc_node;
} else 0;

const return_type_expr = try renderNode(c, payload.return_type);
Expand Down
39 changes: 0 additions & 39 deletions lib/compiler_rt/int.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const is_test = builtin.is_test;
const common = @import("common.zig");
const udivmod = @import("udivmod.zig").udivmod;
const __divti3 = @import("divti3.zig").__divti3;
const arm = @import("arm.zig");

pub const panic = common.panic;

Expand Down Expand Up @@ -102,25 +101,6 @@ test "test_divmoddi4" {
}
}

fn test_one_aeabi_ldivmod(a: i64, b: i64, expected_q: i64, expected_r: i64) !void {
const LdivmodRes = extern struct {
q: i64, // r1:r0
r: i64, // r3:r2
};
const actualIdivmod = @as(*const fn (a: i64, b: i64) callconv(.AAPCS) LdivmodRes, @ptrCast(&arm.__aeabi_ldivmod));
const arm_res = actualIdivmod(a, b);
try testing.expectEqual(expected_q, arm_res.q);
try testing.expectEqual(expected_r, arm_res.r);
}

test "arm.__aeabi_ldivmod" {
if (!builtin.cpu.arch.isARM()) return error.SkipZigTest;

for (cases__divmodsi4) |case| {
try test_one_aeabi_ldivmod(case[0], case[1], case[2], case[3]);
}
}

pub fn __udivmoddi4(a: u64, b: u64, maybe_rem: ?*u64) callconv(.C) u64 {
return udivmod(u64, a, b, maybe_rem);
}
Expand Down Expand Up @@ -261,25 +241,6 @@ test "test_divmodsi4" {
}
}

fn test_one_aeabi_idivmod(a: i32, b: i32, expected_q: i32, expected_r: i32) !void {
const IdivmodRes = extern struct {
q: i32, // r0
r: i32, // r1
};
const actualIdivmod = @as(*const fn (a: i32, b: i32) callconv(.AAPCS) IdivmodRes, @ptrCast(&arm.__aeabi_idivmod));
const arm_res = actualIdivmod(a, b);
try testing.expectEqual(expected_q, arm_res.q);
try testing.expectEqual(expected_r, arm_res.r);
}

test "arm.__aeabi_idivmod" {
if (!builtin.cpu.arch.isARM()) return error.SkipZigTest;

for (cases__divmodsi4) |case| {
try test_one_aeabi_idivmod(case[0], case[1], case[2], case[3]);
}
}

pub fn __udivmodsi4(a: u32, b: u32, rem: *u32) callconv(.C) u32 {
const d = __udivsi3(a, b);
rem.* = @bitCast(@as(i32, @bitCast(a)) -% (@as(i32, @bitCast(d)) * @as(i32, @bitCast(b))));
Expand Down
21 changes: 0 additions & 21 deletions lib/compiler_rt/udivmoddi4_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const testing = @import("std").testing;
const builtin = @import("builtin");
const __udivmoddi4 = @import("int.zig").__udivmoddi4;
const __aeabi_uldivmod = @import("arm.zig").__aeabi_uldivmod;

fn test__udivmoddi4(a: u64, b: u64, expected_q: u64, expected_r: u64) !void {
var r: u64 = undefined;
Expand All @@ -18,26 +17,6 @@ test "udivmoddi4" {
}
}

const ARMRes = extern struct {
q: u64, // r1:r0
r: u64, // r3:r2
};

fn test__aeabi_uldivmod(a: u64, b: u64, expected_q: u64, expected_r: u64) !void {
const actualUldivmod = @as(*const fn (a: u64, b: u64) callconv(.AAPCS) ARMRes, @ptrCast(&__aeabi_uldivmod));
const arm_res = actualUldivmod(a, b);
try testing.expectEqual(expected_q, arm_res.q);
try testing.expectEqual(expected_r, arm_res.r);
}

test "arm.__aeabi_uldivmod" {
if (!builtin.cpu.arch.isARM()) return error.SkipZigTest;

for (cases) |case| {
try test__aeabi_uldivmod(case[0], case[1], case[2], case[3]);
}
}

const cases = [_][4]u64{
[_]u64{0x0000000000000000, 0x0000000000000001, 0x0000000000000000, 0x0000000000000000},
[_]u64{0x0000000000000000, 0x0000000000000002, 0x0000000000000000, 0x0000000000000000},
Expand Down
25 changes: 8 additions & 17 deletions lib/compiler_rt/udivmodsi4_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@
// zig fmt: off
const testing = @import("std").testing;
const builtin = @import("builtin");
const __aeabi_uidivmod = @import("arm.zig").__aeabi_uidivmod;
const __udivmodsi4 = @import("int.zig").__udivmodsi4;

const ARMRes = extern struct {
q: u32, // r0
r: u32, // r1
};

fn test__aeabi_uidivmod(a: u32, b: u32, expected_q: u32, expected_r: u32) !void {
const actualUidivmod = @as(*const fn (a: u32, b: u32) callconv(.AAPCS) ARMRes, @ptrCast(&__aeabi_uidivmod));
const arm_res = actualUidivmod(a, b);
try testing.expectEqual(expected_q, arm_res.q);
try testing.expectEqual(expected_r, arm_res.r);
fn test__udivmodsi4(a: u32, b: u32, expected_q: u32, expected_r: u32) !void {
var r: u32 = undefined;
const q = __udivmodsi4(a, b, &r);
try testing.expectEqual(expected_q, q);
try testing.expectEqual(expected_r, r);
}

test "arm.__aeabi_uidivmod" {
if (!builtin.cpu.arch.isARM()) return error.SkipZigTest;

var i: i32 = 0;
test "udivmodsi4" {
for (cases) |case| {
try test__aeabi_uidivmod(case[0], case[1], case[2], case[3]);
i+=1;
try test__udivmodsi4(case[0], case[1], case[2], case[3]);
}
}

Expand Down
Loading

0 comments on commit 6bf52b0

Please sign in to comment.