Commit a71bfc249d

Andrew Kelley <andrew@ziglang.org>
2019-04-11 00:47:14
compiler-rt: better way to do the ABI required on Windows
This removes the compiler_rt.setXmm0 hack. Instead, for the functions that use i128 or u128 in their parameter and return types, we use `@Vector(2, u64)` which generates the LLVM IR `<2 x i64>` type that matches what Clang generates for `typedef int ti_int __attribute__ ((mode (TI)))` when targeting Windows x86_64.
1 parent 5293485
src/codegen.cpp
@@ -424,7 +424,7 @@ static uint32_t get_err_ret_trace_arg_index(CodeGen *g, ZigFn *fn_table_entry) {
 }
 
 static void maybe_export_dll(CodeGen *g, LLVMValueRef global_value, GlobalLinkageId linkage) {
-    if (linkage != GlobalLinkageIdInternal && g->zig_target->os == OsWindows) {
+    if (linkage != GlobalLinkageIdInternal && g->zig_target->os == OsWindows && g->is_dynamic) {
         LLVMSetDLLStorageClass(global_value, LLVMDLLExportStorageClass);
     }
 }
std/special/compiler_rt/divti3.zig
@@ -16,9 +16,9 @@ pub extern fn __divti3(a: i128, b: i128) i128 {
     return (@bitCast(i128, r) ^ s) -% s;
 }
 
-pub extern fn __divti3_windows_x86_64(a: *const i128, b: *const i128) void {
-    @setRuntimeSafety(builtin.is_test);
-    compiler_rt.setXmm0(i128, __divti3(a.*, b.*));
+const v128 = @Vector(2, u64);
+pub extern fn __divti3_windows_x86_64(a: v128, b: v128) v128 {
+    return @bitCast(v128, @inlineCall(__divti3, @bitCast(i128, a), @bitCast(i128, b)));
 }
 
 test "import divti3" {
std/special/compiler_rt/modti3.zig
@@ -20,9 +20,9 @@ pub extern fn __modti3(a: i128, b: i128) i128 {
     return (@bitCast(i128, r) ^ s_a) -% s_a; // negate if s == -1
 }
 
-pub extern fn __modti3_windows_x86_64(a: *const i128, b: *const i128) void {
-    @setRuntimeSafety(builtin.is_test);
-    compiler_rt.setXmm0(i128, __modti3(a.*, b.*));
+const v128 = @Vector(2, u64);
+pub extern fn __modti3_windows_x86_64(a: v128, b: v128) v128 {
+    return @bitCast(v128, @inlineCall(__modti3, @bitCast(i128, a), @bitCast(i128, b)));
 }
 
 test "import modti3" {
std/special/compiler_rt/muloti4.zig
@@ -44,9 +44,9 @@ pub extern fn __muloti4(a: i128, b: i128, overflow: *c_int) i128 {
     return r;
 }
 
-pub extern fn __muloti4_windows_x86_64(a: *const i128, b: *const i128, overflow: *c_int) void {
-    @setRuntimeSafety(builtin.is_test);
-    compiler_rt.setXmm0(i128, __muloti4(a.*, b.*, overflow));
+const v128 = @Vector(2, u64);
+pub extern fn __muloti4_windows_x86_64(a: v128, b: v128, overflow: *c_int) v128 {
+    return @bitCast(v128, @inlineCall(__muloti4, @bitCast(i128, a), @bitCast(i128, b), overflow));
 }
 
 test "import muloti4" {
std/special/compiler_rt/multi3.zig
@@ -14,9 +14,9 @@ pub extern fn __multi3(a: i128, b: i128) i128 {
     return r.all;
 }
 
-pub extern fn __multi3_windows_x86_64(a: *const i128, b: *const i128) void {
-    @setRuntimeSafety(builtin.is_test);
-    compiler_rt.setXmm0(i128, __multi3(a.*, b.*));
+const v128 = @Vector(2, u64);
+pub extern fn __multi3_windows_x86_64(a: v128, b: v128) v128 {
+    return @bitCast(v128, @inlineCall(__multi3, @bitCast(i128, a), @bitCast(i128, b)));
 }
 
 fn __mulddi3(a: u64, b: u64) i128 {
std/special/compiler_rt/udivmodti4.zig
@@ -7,9 +7,10 @@ pub extern fn __udivmodti4(a: u128, b: u128, maybe_rem: ?*u128) u128 {
     return udivmod(u128, a, b, maybe_rem);
 }
 
-pub extern fn __udivmodti4_windows_x86_64(a: *const u128, b: *const u128, maybe_rem: ?*u128) void {
+const v128 = @Vector(2, u64);
+pub extern fn __udivmodti4_windows_x86_64(a: v128, b: v128, maybe_rem: ?*u128) v128 {
     @setRuntimeSafety(builtin.is_test);
-    compiler_rt.setXmm0(u128, udivmod(u128, a.*, b.*, maybe_rem));
+    return @bitCast(v128, udivmod(u128, @bitCast(u128, a), @bitCast(u128, b), maybe_rem));
 }
 
 test "import udivmodti4" {
std/special/compiler_rt/udivti3.zig
@@ -6,7 +6,8 @@ pub extern fn __udivti3(a: u128, b: u128) u128 {
     return udivmodti4.__udivmodti4(a, b, null);
 }
 
-pub extern fn __udivti3_windows_x86_64(a: *const u128, b: *const u128) void {
+const v128 = @Vector(2, u64);
+pub extern fn __udivti3_windows_x86_64(a: v128, b: v128) v128 {
     @setRuntimeSafety(builtin.is_test);
-    udivmodti4.__udivmodti4_windows_x86_64(a, b, null);
+    return udivmodti4.__udivmodti4_windows_x86_64(a, b, null);
 }
std/special/compiler_rt/umodti3.zig
@@ -9,7 +9,7 @@ pub extern fn __umodti3(a: u128, b: u128) u128 {
     return r;
 }
 
-pub extern fn __umodti3_windows_x86_64(a: *const u128, b: *const u128) void {
-    @setRuntimeSafety(builtin.is_test);
-    compiler_rt.setXmm0(u128, __umodti3(a.*, b.*));
+const v128 = @Vector(2, u64);
+pub extern fn __umodti3_windows_x86_64(a: v128, b: v128) v128 {
+    return @bitCast(v128, @inlineCall(__umodti3, @bitCast(u128, a), @bitCast(u128, b)));
 }
std/special/compiler_rt.zig
@@ -160,6 +160,8 @@ comptime {
                     @export("__chkstk", __chkstk, strong_linkage);
                     @export("___chkstk_ms", ___chkstk_ms, linkage);
                 }
+                // The "ti" functions must use @Vector(2, u64) parameter types to adhere to the ABI
+                // that LLVM expects compiler-rt to have.
                 @export("__divti3", @import("compiler_rt/divti3.zig").__divti3_windows_x86_64, linkage);
                 @export("__modti3", @import("compiler_rt/modti3.zig").__modti3_windows_x86_64, linkage);
                 @export("__multi3", @import("compiler_rt/multi3.zig").__multi3_windows_x86_64, linkage);
@@ -198,17 +200,6 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn
     }
 }
 
-pub fn setXmm0(comptime T: type, value: T) void {
-    comptime assert(builtin.arch == builtin.Arch.x86_64);
-    const aligned_value: T align(16) = value;
-    asm volatile (
-        \\movaps (%[ptr]), %%xmm0
-            :
-        : [ptr] "r" (&aligned_value)
-        : "xmm0"
-    );
-}
-
 extern fn __udivdi3(a: u64, b: u64) u64 {
     @setRuntimeSafety(is_test);
     return __udivmoddi4(a, b, null);