Commit 3014a0d5f1

Veikka Tuominen <git@vexu.eu>
2022-06-30 15:39:54
Sema: validate callconv
1 parent 6d24c40
src/Sema.zig
@@ -7007,6 +7007,7 @@ fn funcCommon(
     noalias_bits: u32,
 ) CompileError!Air.Inst.Ref {
     const ret_ty_src: LazySrcLoc = .{ .node_offset_fn_type_ret_ty = src_node_offset };
+    const cc_src: LazySrcLoc = .{ .node_offset_fn_type_cc = src_node_offset };
 
     var is_generic = bare_return_type.tag() == .generic_poison or
         alignment == null or
@@ -7109,6 +7110,45 @@ fn funcCommon(
         const cc_workaround = cc orelse .Unspecified;
         const align_workaround = alignment orelse 0;
 
+        const arch = sema.mod.getTarget().cpu.arch;
+        if (switch (cc_workaround) {
+            .Unspecified, .C, .Naked, .Async, .Inline => null,
+            .Interrupt => switch (arch) {
+                .i386, .x86_64, .avr, .msp430 => null,
+                else => @as([]const u8, "i386, x86_64, AVR, and MSP430"),
+            },
+            .Signal => switch (arch) {
+                .avr => null,
+                else => @as([]const u8, "AVR"),
+            },
+            .Stdcall, .Fastcall, .Thiscall => switch (arch) {
+                .i386 => null,
+                else => @as([]const u8, "i386"),
+            },
+            .Vectorcall => switch (arch) {
+                .i386, .aarch64, .aarch64_be, .aarch64_32 => null,
+                else => @as([]const u8, "i386 and AArch64"),
+            },
+            .APCS, .AAPCS, .AAPCSVFP => switch (arch) {
+                .arm, .armeb, .aarch64, .aarch64_be, .aarch64_32 => null,
+                else => @as([]const u8, "ARM"),
+            },
+            .SysV, .Win64 => switch (arch) {
+                .x86_64 => null,
+                else => @as([]const u8, "x86_64"),
+            },
+            .PtxKernel => switch (arch) {
+                .nvptx, .nvptx64 => null,
+                else => @as([]const u8, "nvptx and nvptx64"),
+            },
+        }) |allowed_platform| {
+            return sema.fail(block, cc_src, "callconv '{s}' is only available on {s}, not {s}", .{
+                @tagName(cc_workaround),
+                allowed_platform,
+                @tagName(arch),
+            });
+        }
+
         break :fn_ty try Type.Tag.function.create(sema.arena, .{
             .param_types = param_types,
             .comptime_params = comptime_params.ptr,
test/cases/compile_errors/stage1/obj/callconv_apcs_aapcs_aapcsvfp_on_unsupported_platform.zig
@@ -1,11 +0,0 @@
-export fn entry1() callconv(.APCS) void {}
-export fn entry2() callconv(.AAPCS) void {}
-export fn entry3() callconv(.AAPCSVFP) void {}
-
-// error
-// backend=stage1
-// target=x86_64-linux-none
-//
-// tmp.zig:1:29: error: callconv 'APCS' is only available on ARM, not x86_64
-// tmp.zig:2:29: error: callconv 'AAPCS' is only available on ARM, not x86_64
-// tmp.zig:3:29: error: callconv 'AAPCSVFP' is only available on ARM, not x86_64
test/cases/compile_errors/stage1/obj/callconv_interrupt_on_unsupported_platform.zig
@@ -1,7 +0,0 @@
-export fn entry() callconv(.Interrupt) void {}
-
-// error
-// backend=stage1
-// target=aarch64-linux-none
-//
-// tmp.zig:1:28: error: callconv 'Interrupt' is only available on x86, x86_64, AVR, and MSP430, not aarch64
test/cases/compile_errors/stage1/obj/callconv_signal_on_unsupported_platform.zig
@@ -1,7 +0,0 @@
-export fn entry() callconv(.Signal) void {}
-
-// error
-// backend=stage1
-// target=x86_64-linux-none
-//
-// tmp.zig:1:28: error: callconv 'Signal' is only available on AVR, not x86_64
test/cases/compile_errors/stage1/obj/callconv_stdcall_fastcall_thiscall_on_unsupported_platform-1.zig
@@ -1,11 +0,0 @@
-export fn entry1() callconv(.Stdcall) void {}
-export fn entry2() callconv(.Fastcall) void {}
-export fn entry3() callconv(.Thiscall) void {}
-
-// error
-// backend=stage1
-// target=x86_64-linux-none
-//
-// tmp.zig:1:29: error: callconv 'Stdcall' is only available on x86, not x86_64
-// tmp.zig:2:29: error: callconv 'Fastcall' is only available on x86, not x86_64
-// tmp.zig:3:29: error: callconv 'Thiscall' is only available on x86, not x86_64
test/cases/compile_errors/stage1/obj/callconv_vectorcall_on_unsupported_platform.zig
@@ -1,7 +0,0 @@
-export fn entry() callconv(.Vectorcall) void {}
-
-// error
-// backend=stage1
-// target=x86_64-linux-none
-//
-// tmp.zig:1:28: error: callconv 'Vectorcall' is only available on x86 and AArch64, not x86_64
test/cases/compile_errors/stage1/obj/bogus_compile_var.zig → test/cases/compile_errors/bogus_compile_var.zig
@@ -2,7 +2,7 @@ const x = @import("builtin").bogus;
 export fn entry() usize { return @sizeOf(@TypeOf(x)); }
 
 // error
-// backend=stage1
+// backend=stage2
 // target=native
 //
-// tmp.zig:1:29: error: container 'builtin' has no member called 'bogus'
+// :1:29: error: struct 'builtin.builtin' has no member named 'bogus'
test/cases/compile_errors/callconv_apcs_aapcs_aapcsvfp_on_unsupported_platform.zig
@@ -0,0 +1,11 @@
+export fn entry1() callconv(.APCS) void {}
+export fn entry2() callconv(.AAPCS) void {}
+export fn entry3() callconv(.AAPCSVFP) void {}
+
+// error
+// backend=stage2
+// target=x86_64-linux-none
+//
+// :1:30: error: callconv 'APCS' is only available on ARM, not x86_64
+// :2:30: error: callconv 'AAPCS' is only available on ARM, not x86_64
+// :3:30: error: callconv 'AAPCSVFP' is only available on ARM, not x86_64
test/cases/compile_errors/callconv_interrupt_on_unsupported_platform.zig
@@ -0,0 +1,7 @@
+export fn entry() callconv(.Interrupt) void {}
+
+// error
+// backend=stage2
+// target=aarch64-linux-none
+//
+// :1:29: error: callconv 'Interrupt' is only available on i386, x86_64, AVR, and MSP430, not aarch64
test/cases/compile_errors/callconv_signal_on_unsupported_platform.zig
@@ -0,0 +1,7 @@
+export fn entry() callconv(.Signal) void {}
+
+// error
+// backend=stage2
+// target=x86_64-linux-none
+//
+// :1:29: error: callconv 'Signal' is only available on AVR, not x86_64
test/cases/compile_errors/stage1/obj/callconv_stdcall_fastcall_thiscall_on_unsupported_platform-0.zig → test/cases/compile_errors/callconv_stdcall_fastcall_thiscall_on_unsupported_platform.zig
@@ -15,9 +15,9 @@ export fn entry3() void {
 }
 
 // error
-// backend=stage1
+// backend=stage2
 // target=x86_64-linux-none
 //
-// tmp.zig:1:27: error: callconv 'Stdcall' is only available on x86, not x86_64
-// tmp.zig:2:27: error: callconv 'Fastcall' is only available on x86, not x86_64
-// tmp.zig:3:27: error: callconv 'Thiscall' is only available on x86, not x86_64
+// :1:28: error: callconv 'Stdcall' is only available on i386, not x86_64
+// :2:28: error: callconv 'Fastcall' is only available on i386, not x86_64
+// :3:28: error: callconv 'Thiscall' is only available on i386, not x86_64
test/cases/compile_errors/callconv_vectorcall_on_unsupported_platform.zig
@@ -0,0 +1,7 @@
+export fn entry() callconv(.Vectorcall) void {}
+
+// error
+// backend=stage2
+// target=x86_64-linux-none
+//
+// :1:29: error: callconv 'Vectorcall' is only available on i386 and AArch64, not x86_64