authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-10 18:47:14-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-10 18:47:14-04:00
loga71bfc249d3f814d7d659fe5cf4cb582483e8938
tree0b1b26bbb96a02eebbd05dbe9cf16fd06412d8d6
parent52934851f2b7a5cb28a08beafa1ec1aa24a3a4cb

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.

9 files changed, 24 insertions(+), 31 deletions(-)

src/codegen.cpp+1-1
......@@ -424,7 +424,7 @@ static uint32_t get_err_ret_trace_arg_index(CodeGen *g, ZigFn *fn_table_entry) {
424424}
425425
426426static void maybe_export_dll(CodeGen *g, LLVMValueRef global_value, GlobalLinkageId linkage) {
427 if (linkage != GlobalLinkageIdInternal && g->zig_target->os == OsWindows) {
427 if (linkage != GlobalLinkageIdInternal && g->zig_target->os == OsWindows && g->is_dynamic) {
428428 LLVMSetDLLStorageClass(global_value, LLVMDLLExportStorageClass);
429429 }
430430}
std/special/compiler_rt.zig+2-11
......@@ -160,6 +160,8 @@ comptime {
160160 @export("__chkstk", __chkstk, strong_linkage);
161161 @export("___chkstk_ms", ___chkstk_ms, linkage);
162162 }
163 // The "ti" functions must use @Vector(2, u64) parameter types to adhere to the ABI
164 // that LLVM expects compiler-rt to have.
163165 @export("__divti3", @import("compiler_rt/divti3.zig").__divti3_windows_x86_64, linkage);
164166 @export("__modti3", @import("compiler_rt/modti3.zig").__modti3_windows_x86_64, linkage);
165167 @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
198200 }
199201}
200202
201pub fn setXmm0(comptime T: type, value: T) void {
202 comptime assert(builtin.arch == builtin.Arch.x86_64);
203 const aligned_value: T align(16) = value;
204 asm volatile (
205 \\movaps (%[ptr]), %%xmm0
206 :
207 : [ptr] "r" (&aligned_value)
208 : "xmm0"
209 );
210}
211
212203extern fn __udivdi3(a: u64, b: u64) u64 {
213204 @setRuntimeSafety(is_test);
214205 return __udivmoddi4(a, b, null);
std/special/compiler_rt/divti3.zig+3-3
......@@ -16,9 +16,9 @@ pub extern fn __divti3(a: i128, b: i128) i128 {
1616 return (@bitCast(i128, r) ^ s) -% s;
1717}
1818
19pub extern fn __divti3_windows_x86_64(a: *const i128, b: *const i128) void {
20 @setRuntimeSafety(builtin.is_test);
21 compiler_rt.setXmm0(i128, __divti3(a.*, b.*));
19const v128 = @Vector(2, u64);
20pub extern fn __divti3_windows_x86_64(a: v128, b: v128) v128 {
21 return @bitCast(v128, @inlineCall(__divti3, @bitCast(i128, a), @bitCast(i128, b)));
2222}
2323
2424test "import divti3" {
std/special/compiler_rt/modti3.zig+3-3
......@@ -20,9 +20,9 @@ pub extern fn __modti3(a: i128, b: i128) i128 {
2020 return (@bitCast(i128, r) ^ s_a) -% s_a; // negate if s == -1
2121}
2222
23pub extern fn __modti3_windows_x86_64(a: *const i128, b: *const i128) void {
24 @setRuntimeSafety(builtin.is_test);
25 compiler_rt.setXmm0(i128, __modti3(a.*, b.*));
23const v128 = @Vector(2, u64);
24pub extern fn __modti3_windows_x86_64(a: v128, b: v128) v128 {
25 return @bitCast(v128, @inlineCall(__modti3, @bitCast(i128, a), @bitCast(i128, b)));
2626}
2727
2828test "import modti3" {
std/special/compiler_rt/muloti4.zig+3-3
......@@ -44,9 +44,9 @@ pub extern fn __muloti4(a: i128, b: i128, overflow: *c_int) i128 {
4444 return r;
4545}
4646
47pub extern fn __muloti4_windows_x86_64(a: *const i128, b: *const i128, overflow: *c_int) void {
48 @setRuntimeSafety(builtin.is_test);
49 compiler_rt.setXmm0(i128, __muloti4(a.*, b.*, overflow));
47const v128 = @Vector(2, u64);
48pub extern fn __muloti4_windows_x86_64(a: v128, b: v128, overflow: *c_int) v128 {
49 return @bitCast(v128, @inlineCall(__muloti4, @bitCast(i128, a), @bitCast(i128, b), overflow));
5050}
5151
5252test "import muloti4" {
std/special/compiler_rt/multi3.zig+3-3
......@@ -14,9 +14,9 @@ pub extern fn __multi3(a: i128, b: i128) i128 {
1414 return r.all;
1515}
1616
17pub extern fn __multi3_windows_x86_64(a: *const i128, b: *const i128) void {
18 @setRuntimeSafety(builtin.is_test);
19 compiler_rt.setXmm0(i128, __multi3(a.*, b.*));
17const v128 = @Vector(2, u64);
18pub extern fn __multi3_windows_x86_64(a: v128, b: v128) v128 {
19 return @bitCast(v128, @inlineCall(__multi3, @bitCast(i128, a), @bitCast(i128, b)));
2020}
2121
2222fn __mulddi3(a: u64, b: u64) i128 {
std/special/compiler_rt/udivmodti4.zig+3-2
......@@ -7,9 +7,10 @@ pub extern fn __udivmodti4(a: u128, b: u128, maybe_rem: ?*u128) u128 {
77 return udivmod(u128, a, b, maybe_rem);
88}
99
10pub extern fn __udivmodti4_windows_x86_64(a: *const u128, b: *const u128, maybe_rem: ?*u128) void {
10const v128 = @Vector(2, u64);
11pub extern fn __udivmodti4_windows_x86_64(a: v128, b: v128, maybe_rem: ?*u128) v128 {
1112 @setRuntimeSafety(builtin.is_test);
12 compiler_rt.setXmm0(u128, udivmod(u128, a.*, b.*, maybe_rem));
13 return @bitCast(v128, udivmod(u128, @bitCast(u128, a), @bitCast(u128, b), maybe_rem));
1314}
1415
1516test "import udivmodti4" {
std/special/compiler_rt/udivti3.zig+3-2
......@@ -6,7 +6,8 @@ pub extern fn __udivti3(a: u128, b: u128) u128 {
66 return udivmodti4.__udivmodti4(a, b, null);
77}
88
9pub extern fn __udivti3_windows_x86_64(a: *const u128, b: *const u128) void {
9const v128 = @Vector(2, u64);
10pub extern fn __udivti3_windows_x86_64(a: v128, b: v128) v128 {
1011 @setRuntimeSafety(builtin.is_test);
11 udivmodti4.__udivmodti4_windows_x86_64(a, b, null);
12 return udivmodti4.__udivmodti4_windows_x86_64(a, b, null);
1213}
std/special/compiler_rt/umodti3.zig+3-3
......@@ -9,7 +9,7 @@ pub extern fn __umodti3(a: u128, b: u128) u128 {
99 return r;
1010}
1111
12pub extern fn __umodti3_windows_x86_64(a: *const u128, b: *const u128) void {
13 @setRuntimeSafety(builtin.is_test);
14 compiler_rt.setXmm0(u128, __umodti3(a.*, b.*));
12const v128 = @Vector(2, u64);
13pub extern fn __umodti3_windows_x86_64(a: v128, b: v128) v128 {
14 return @bitCast(v128, @inlineCall(__umodti3, @bitCast(u128, a), @bitCast(u128, b)));
1515}