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 {...@@ -560,6 +560,7 @@ pub const Payload = struct {
560 pub const CallingConvention = enum {560 pub const CallingConvention = enum {
561 c,561 c,
562 x86_64_sysv,562 x86_64_sysv,
563 x86_64_win,
563 x86_stdcall,564 x86_stdcall,
564 x86_fastcall,565 x86_fastcall,
565 x86_thiscall,566 x86_thiscall,
...@@ -567,6 +568,7 @@ pub const Payload = struct {...@@ -567,6 +568,7 @@ pub const Payload = struct {
567 aarch64_vfabi,568 aarch64_vfabi,
568 arm_aapcs,569 arm_aapcs,
569 arm_aapcs_vfp,570 arm_aapcs_vfp,
571 m68k_rtd,
570 };572 };
571 };573 };
572574
...@@ -2834,6 +2836,7 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {...@@ -2834,6 +2836,7 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {
2834 });2836 });
2835 },2837 },
2836 .x86_64_sysv,2838 .x86_64_sysv,
2839 .x86_64_win,
2837 .x86_stdcall,2840 .x86_stdcall,
2838 .x86_fastcall,2841 .x86_fastcall,
2839 .x86_thiscall,2842 .x86_thiscall,
...@@ -2841,6 +2844,7 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {...@@ -2841,6 +2844,7 @@ fn renderFunc(c: *Context, node: Node) !NodeIndex {
2841 .aarch64_vfabi,2844 .aarch64_vfabi,
2842 .arm_aapcs,2845 .arm_aapcs,
2843 .arm_aapcs_vfp,2846 .arm_aapcs_vfp,
2847 .m68k_rtd,
2844 => cc_node: {2848 => cc_node: {
2845 // .{ .foo = .{} }2849 // .{ .foo = .{} }
2846 _ = try c.addToken(.period, ".");2850 _ = try c.addToken(.period, ".");
src/Zcu.zig+15
...@@ -3568,12 +3568,27 @@ pub fn callconvSupported(zcu: *Zcu, cc: std.builtin.CallingConvention) union(enu...@@ -3568,12 +3568,27 @@ pub fn callconvSupported(zcu: *Zcu, cc: std.builtin.CallingConvention) union(enu
3568 }3568 }
3569 }3569 }
3570 break :ok switch (cc) {3570 break :ok switch (cc) {
3571 .x86_64_sysv,
3572 .x86_64_win,
3571 .x86_64_vectorcall,3573 .x86_64_vectorcall,
3574 .x86_64_regcall_v3_sysv,
3575 .x86_64_regcall_v4_win,
3572 .x86_fastcall,3576 .x86_fastcall,
3573 .x86_thiscall,3577 .x86_thiscall,
3574 .x86_vectorcall,3578 .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,
3575 => |opts| opts.incoming_stack_alignment == null,3588 => |opts| opts.incoming_stack_alignment == null,
35763589
3590 .x86_sysv,
3591 .x86_win,
3577 .x86_stdcall,3592 .x86_stdcall,
3578 => |opts| opts.incoming_stack_alignment == null and opts.register_params == 0,3593 => |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 {...@@ -7605,16 +7605,38 @@ fn writeMemoryOrder(w: anytype, order: std.builtin.AtomicOrder) !void {
7605}7605}
76067606
7607fn toCallingConvention(cc: std.builtin.CallingConvention, zcu: *Zcu) ?[]const u8 {7607fn 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 }
7608 return switch (cc) {7613 return switch (cc) {
7609 .auto, .naked => null,7614 .auto, .naked => null,
7615
7616 .x86_64_sysv, .x86_sysv => "sysv_abi",
7617 .x86_64_win, .x86_win => "ms_abi",
7610 .x86_stdcall => "stdcall",7618 .x86_stdcall => "stdcall",
7611 .x86_fastcall => "fastcall",7619 .x86_fastcall => "fastcall",
7612 .x86_vectorcall, .x86_64_vectorcall => "vectorcall",7620 .x86_thiscall => "thiscall",
7613 else => {7621
7614 // `Zcu.callconvSupported` means this must be the C callconv.7622 .x86_vectorcall,
7615 assert(cc.eql(zcu.getTarget().cCallingConvention().?));7623 .x86_64_vectorcall,
7616 return null;7624 => "vectorcall",
7617 },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`
7618 };7640 };
7619}7641}
76207642
src/translate_c.zig+2
...@@ -5005,6 +5005,7 @@ fn transCC(...@@ -5005,6 +5005,7 @@ fn transCC(
5005 return switch (clang_cc) {5005 return switch (clang_cc) {
5006 .C => .c,5006 .C => .c,
5007 .X86_64SysV => .x86_64_sysv,5007 .X86_64SysV => .x86_64_sysv,
5008 .Win64 => .x86_64_win,
5008 .X86StdCall => .x86_stdcall,5009 .X86StdCall => .x86_stdcall,
5009 .X86FastCall => .x86_fastcall,5010 .X86FastCall => .x86_fastcall,
5010 .X86ThisCall => .x86_thiscall,5011 .X86ThisCall => .x86_thiscall,
...@@ -5012,6 +5013,7 @@ fn transCC(...@@ -5012,6 +5013,7 @@ fn transCC(
5012 .AArch64VectorCall => .aarch64_vfabi,5013 .AArch64VectorCall => .aarch64_vfabi,
5013 .AAPCS => .arm_aapcs,5014 .AAPCS => .arm_aapcs,
5014 .AAPCS_VFP => .arm_aapcs_vfp,5015 .AAPCS_VFP => .arm_aapcs_vfp,
5016 .M68kRTD => .m68k_rtd,
5015 else => return fail(5017 else => return fail(
5016 c,5018 c,
5017 error.UnsupportedType,5019 error.UnsupportedType,