authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-10-15 15:11:02+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-10-19 19:15:24+01:00
logcb48376bec1957997532c627db2caf2dc970c397
tree724383ff256ec9e7c8a9a9e050c929a5bc93a191
parent67580ede5ef7e087d030b96ad6c71c3fc5760e4c
signaturelock-open Commit is signed but in an unrecognized format.

cbe,translate-c: support more callconvs

There are several more that we could support here, but I didn't feel like going down the rabbit-hole of figuring them out. In particular, some of the Clang enum fields aren't specific enough for us, so we'll have to switch on the target to figure out how to translate-c them. That can be a future enhancement.

4 files changed, 49 insertions(+), 6 deletions(-)

lib/compiler/aro_translate_c/ast.zig+4
......@@ -560,6 +560,7 @@ pub const Payload = struct {
560560 pub const CallingConvention = enum {
561561 c,
562562 x86_64_sysv,
563 x86_64_win,
563564 x86_stdcall,
564565 x86_fastcall,
565566 x86_thiscall,
......@@ -567,6 +568,7 @@ pub const Payload = struct {
567568 aarch64_vfabi,
568569 arm_aapcs,
569570 arm_aapcs_vfp,
571 m68k_rtd,
570572 };
571573 };
572574
......@@ -2834,6 +2836,7 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {
28342836 });
28352837 },
28362838 .x86_64_sysv,
2839 .x86_64_win,
28372840 .x86_stdcall,
28382841 .x86_fastcall,
28392842 .x86_thiscall,
......@@ -2841,6 +2844,7 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {
28412844 .aarch64_vfabi,
28422845 .arm_aapcs,
28432846 .arm_aapcs_vfp,
2847 .m68k_rtd,
28442848 => cc_node: {
28452849 // .{ .foo = .{} }
28462850 _ = try c.addToken(.period, ".");
src/Zcu.zig+15
......@@ -3568,12 +3568,27 @@ pub fn callconvSupported(zcu: *Zcu, cc: std.builtin.CallingConvention) union(enu
35683568 }
35693569 }
35703570 break :ok switch (cc) {
3571 .x86_64_sysv,
3572 .x86_64_win,
35713573 .x86_64_vectorcall,
3574 .x86_64_regcall_v3_sysv,
3575 .x86_64_regcall_v4_win,
35723576 .x86_fastcall,
35733577 .x86_thiscall,
35743578 .x86_vectorcall,
3579 .x86_regcall_v3,
3580 .x86_regcall_v4_win,
3581 .aarch64_vfabi,
3582 .aarch64_vfabi_sve,
3583 .arm_aapcs,
3584 .arm_aapcs_vfp,
3585 .riscv64_lp64_v,
3586 .riscv32_ilp32_v,
3587 .m68k_rtd,
35753588 => |opts| opts.incoming_stack_alignment == null,
35763589
3590 .x86_sysv,
3591 .x86_win,
35773592 .x86_stdcall,
35783593 => |opts| opts.incoming_stack_alignment == null and opts.register_params == 0,
35793594
src/codegen/c.zig+28-6
......@@ -7605,16 +7605,38 @@ fn writeMemoryOrder(w: anytype, order: std.builtin.AtomicOrder) !void {
76057605}
76067606
76077607fn toCallingConvention(cc: std.builtin.CallingConvention, zcu: *Zcu) ?[]const u8 {
7608 if (zcu.getTarget().cCallingConvention()) |ccc| {
7609 if (cc.eql(ccc)) {
7610 return null;
7611 }
7612 }
76087613 return switch (cc) {
76097614 .auto, .naked => null,
7615
7616 .x86_64_sysv, .x86_sysv => "sysv_abi",
7617 .x86_64_win, .x86_win => "ms_abi",
76107618 .x86_stdcall => "stdcall",
76117619 .x86_fastcall => "fastcall",
7612 .x86_vectorcall, .x86_64_vectorcall => "vectorcall",
7613 else => {
7614 // `Zcu.callconvSupported` means this must be the C callconv.
7615 assert(cc.eql(zcu.getTarget().cCallingConvention().?));
7616 return null;
7617 },
7620 .x86_thiscall => "thiscall",
7621
7622 .x86_vectorcall,
7623 .x86_64_vectorcall,
7624 => "vectorcall",
7625
7626 .x86_64_regcall_v3_sysv,
7627 .x86_64_regcall_v4_win,
7628 .x86_regcall_v3,
7629 .x86_regcall_v4_win,
7630 => "regcall",
7631
7632 .aarch64_vfabi => "aarch64_vector_pcs",
7633 .aarch64_vfabi_sve => "aarch64_sve_pcs",
7634 .arm_aapcs => "pcs(\"aapcs\")",
7635 .arm_aapcs_vfp => "pcs(\"aapcs-vfp\")",
7636 .riscv64_lp64_v, .riscv32_ilp32_v => "riscv_vector_cc",
7637 .m68k_rtd => "m68k_rtd",
7638
7639 else => unreachable, // `Zcu.callconvSupported`
76187640 };
76197641}
76207642
src/translate_c.zig+2
......@@ -5005,6 +5005,7 @@ fn transCC(
50055005 return switch (clang_cc) {
50065006 .C => .c,
50075007 .X86_64SysV => .x86_64_sysv,
5008 .Win64 => .x86_64_win,
50085009 .X86StdCall => .x86_stdcall,
50095010 .X86FastCall => .x86_fastcall,
50105011 .X86ThisCall => .x86_thiscall,
......@@ -5012,6 +5013,7 @@ fn transCC(
50125013 .AArch64VectorCall => .aarch64_vfabi,
50135014 .AAPCS => .arm_aapcs,
50145015 .AAPCS_VFP => .arm_aapcs_vfp,
5016 .M68kRTD => .m68k_rtd,
50155017 else => return fail(
50165018 c,
50175019 error.UnsupportedType,