authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-24 17:53:54-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-01-24 17:53:54-05:00
logcb9d00e1af365d5722ff7795aff1e74e78c3aa65
tree10ee072503d3efa8b6a845d4fa2f978095a4a04b
parentdfcedfdca0b1e26d8a735327fe8e42d7887b21b2
parent4888f9d7e1296565cb1c16e1750773e00a1e52b4
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #14402 from kcbanner/cbe_x86_fixes

CBE: x86 support and MSVC ABI fixes

13 files changed, 197 insertions(+), 50 deletions(-)

ci/x86_64-windows-debug.ps1+9-6
......@@ -67,14 +67,16 @@ Write-Output "Testing Autodocs..."
6767 -fno-emit-bin
6868CheckLastExitCode
6969
70Write-Output "Build behavior tests using the C backend..."
70Write-Output "Build x86_64-windows-msvc behavior tests using the C backend..."
7171& "stage3-debug\bin\zig.exe" test `
7272 ..\test\behavior.zig `
7373 --zig-lib-dir "$ZIG_LIB_DIR" `
7474 -I..\test `
7575 -I..\lib `
7676 -ofmt=c `
77 -femit-bin="test_behavior.c"
77 -femit-bin="test-x86_64-windows-msvc.c" `
78 --test-no-exec `
79 -target x86_64-windows-msvc
7880CheckLastExitCode
7981
8082& "stage3-debug\bin\zig.exe" build-obj `
......@@ -83,8 +85,9 @@ CheckLastExitCode
8385 -ofmt=c `
8486 -OReleaseSmall `
8587 --name compiler_rt `
86 -femit-bin="compiler_rt.c" `
87 --pkg-begin build_options config.zig --pkg-end
88 -femit-bin="compiler_rt-x86_64-windows-msvc.c" `
89 --pkg-begin build_options config.zig --pkg-end `
90 -target x86_64-windows-msvc
8891CheckLastExitCode
8992
9093Import-Module "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
......@@ -96,8 +99,8 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files\Microsoft Visual Studio\2022\E
9699CheckLastExitCode
97100
98101Write-Output "Build and run behavior tests with msvc..."
99& cl.exe -I..\lib test_behavior.c compiler_rt.c /W3 /Z7 -link -nologo -debug -subsystem:console -entry:wWinMainCRTStartup kernel32.lib ntdll.lib vcruntime.lib libucrt.lib
102& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console -entry:wWinMainCRTStartup kernel32.lib ntdll.lib vcruntime.lib libucrt.lib
100103CheckLastExitCode
101104
102& .\test_behavior.exe
105& .\test-x86_64-windows-msvc.exe
103106CheckLastExitCode
ci/x86_64-windows-release.ps1+9-6
......@@ -67,14 +67,16 @@ Write-Output "Testing Autodocs..."
6767 -fno-emit-bin
6868CheckLastExitCode
6969
70Write-Output "Build behavior tests using the C backend..."
70Write-Output "Build x86_64-windows-msvc behavior tests using the C backend..."
7171& "stage3-release\bin\zig.exe" test `
7272 ..\test\behavior.zig `
7373 --zig-lib-dir "$ZIG_LIB_DIR" `
7474 -I..\test `
7575 -I..\lib `
7676 -ofmt=c `
77 -femit-bin="test_behavior.c"
77 -femit-bin="test-x86_64-windows-msvc.c" `
78 --test-no-exec `
79 -target x86_64-windows-msvc
7880CheckLastExitCode
7981
8082& "stage3-release\bin\zig.exe" build-obj `
......@@ -83,8 +85,9 @@ CheckLastExitCode
8385 -ofmt=c `
8486 -OReleaseSmall `
8587 --name compiler_rt `
86 -femit-bin="compiler_rt.c" `
87 --pkg-begin build_options config.zig --pkg-end
88 -femit-bin="compiler_rt-x86_64-windows-msvc.c" `
89 --pkg-begin build_options config.zig --pkg-end `
90 -target x86_64-windows-msvc
8891CheckLastExitCode
8992
9093Import-Module "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
......@@ -96,8 +99,8 @@ Enter-VsDevShell -VsInstallPath "C:\Program Files\Microsoft Visual Studio\2022\E
9699CheckLastExitCode
97100
98101Write-Output "Build and run behavior tests with msvc..."
99& cl.exe -I..\lib test_behavior.c compiler_rt.c /W3 /Z7 -link -nologo -debug -subsystem:console -entry:wWinMainCRTStartup kernel32.lib ntdll.lib vcruntime.lib libucrt.lib
102& cl.exe -I..\lib test-x86_64-windows-msvc.c compiler_rt-x86_64-windows-msvc.c /W3 /Z7 -link -nologo -debug -subsystem:console -entry:wWinMainCRTStartup kernel32.lib ntdll.lib vcruntime.lib libucrt.lib
100103CheckLastExitCode
101104
102& .\test_behavior.exe
105& .\test-x86_64-windows-msvc.exe
103106CheckLastExitCode
lib/compiler_rt/aulldiv.zig+1-1
......@@ -7,7 +7,7 @@ const common = @import("common.zig");
77pub const panic = common.panic;
88
99comptime {
10 if (arch == .x86 and abi == .msvc) {
10 if (arch == .x86 and abi == .msvc and builtin.zig_backend != .stage2_c) {
1111 // Don't let LLVM apply the stdcall name mangling on those MSVC builtins
1212 @export(_alldiv, .{ .name = "\x01__alldiv", .linkage = common.linkage, .visibility = common.visibility });
1313 @export(_aulldiv, .{ .name = "\x01__aulldiv", .linkage = common.linkage, .visibility = common.visibility });
lib/compiler_rt/aullrem.zig+1-1
......@@ -7,7 +7,7 @@ const common = @import("common.zig");
77pub const panic = common.panic;
88
99comptime {
10 if (arch == .x86 and abi == .msvc) {
10 if (arch == .x86 and abi == .msvc and builtin.zig_backend != .stage2_c) {
1111 // Don't let LLVM apply the stdcall name mangling on those MSVC builtins
1212 @export(_allrem, .{ .name = "\x01__allrem", .linkage = common.linkage, .visibility = common.visibility });
1313 @export(_aullrem, .{ .name = "\x01__aullrem", .linkage = common.linkage, .visibility = common.visibility });
lib/std/os/windows.zig+11-4
......@@ -1804,14 +1804,21 @@ pub fn UnlockFile(
18041804
18051805/// This is a workaround for the C backend until zig has the ability to put
18061806/// C code in inline assembly.
1807extern fn zig_x86_windows_teb() callconv(.C) *anyopaque;
18071808extern fn zig_x86_64_windows_teb() callconv(.C) *anyopaque;
18081809
18091810pub fn teb() *TEB {
18101811 return switch (native_arch) {
1811 .x86 => asm volatile (
1812 \\ movl %%fs:0x18, %[ptr]
1813 : [ptr] "=r" (-> *TEB),
1814 ),
1812 .x86 => blk: {
1813 if (builtin.zig_backend == .stage2_c) {
1814 break :blk @ptrCast(*TEB, @alignCast(@alignOf(TEB), zig_x86_windows_teb()));
1815 } else {
1816 break :blk asm volatile (
1817 \\ movl %%fs:0x18, %[ptr]
1818 : [ptr] "=r" (-> *TEB),
1819 );
1820 }
1821 },
18151822 .x86_64 => blk: {
18161823 if (builtin.zig_backend == .stage2_c) {
18171824 break :blk @ptrCast(*TEB, @alignCast(@alignOf(TEB), zig_x86_64_windows_teb()));
lib/std/start_windows_tls.zig+2-4
......@@ -7,14 +7,12 @@ export var _tls_end: u8 linksection(".tls$ZZZ") = 0;
77export var __xl_a: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLA") = null;
88export var __xl_z: std.os.windows.PIMAGE_TLS_CALLBACK linksection(".CRT$XLZ") = null;
99
10const tls_array: u32 = 0x2c;
1011comptime {
1112 if (builtin.target.cpu.arch == .x86) {
1213 // The __tls_array is the offset of the ThreadLocalStoragePointer field
1314 // in the TEB block whose base address held in the %fs segment.
14 asm (
15 \\ .global __tls_array
16 \\ __tls_array = 0x2C
17 );
15 @export(tls_array, .{ .name = "_tls_array" });
1816 }
1917}
2018
lib/std/target.zig+1-1
......@@ -1821,7 +1821,7 @@ pub const Target = struct {
18211821 .wasm64,
18221822 => 8,
18231823
1824 .x86 => return switch (target.os.tag) {
1824 .x86 => if (target.ofmt == .c) 16 else return switch (target.os.tag) {
18251825 .windows, .uefi => 8,
18261826 else => 4,
18271827 },
lib/zig.h+86-22
......@@ -52,15 +52,20 @@ typedef char bool;
5252
5353#if _MSC_VER
5454#define zig_const_arr
55#define zig_callconv(c) __##c
5556#else
5657#define zig_const_arr static const
58#define zig_callconv(c) __attribute__((c))
5759#endif
5860
5961#if zig_has_attribute(naked) || defined(zig_gnuc)
62#define zig_naked_decl __attribute__((naked))
6063#define zig_naked __attribute__((naked))
6164#elif defined(_MSC_VER)
65#define zig_naked_decl
6266#define zig_naked __declspec(naked)
6367#else
68#define zig_naked_decl zig_naked_unavailable
6469#define zig_naked zig_naked_unavailable
6570#endif
6671
......@@ -111,8 +116,13 @@ typedef char bool;
111116#if zig_has_attribute(alias)
112117#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(symbol)))
113118#elif _MSC_VER
119#if _M_X64
114120#define zig_export(sig, symbol, name) sig;\
115121 __pragma(comment(linker, "/alternatename:" name "=" symbol ))
122#else /*_M_X64 */
123#define zig_export(sig, symbol, name) sig;\
124 __pragma(comment(linker, "/alternatename:_" name "=_" symbol ))
125#endif /*_M_X64 */
116126#else
117127#define zig_export(sig, symbol, name) __asm(name " = " symbol)
118128#endif
......@@ -220,7 +230,12 @@ typedef char bool;
220230#define zig_atomicrmw_max(obj, arg, order, type) zig_expand_concat(zig_msvc_atomicrmw_max_, type)(obj, arg)
221231#define zig_atomic_store(obj, arg, order, type) zig_expand_concat(zig_msvc_atomic_store_, type)(obj, arg)
222232#define zig_atomic_load(obj, order, type) zig_expand_concat(zig_msvc_atomic_load_, type)(obj)
233#if _M_X64
223234#define zig_fence(order) __faststorefence()
235#else
236#define zig_fence(order) zig_msvc_atomic_barrier()
237#endif
238
224239// TODO: _MSC_VER && (_M_ARM || _M_ARM64)
225240#else
226241#define memory_order_relaxed 0
......@@ -1214,8 +1229,14 @@ typedef struct { zig_align(16) zig_i64 hi; zig_u64 lo; } zig_i128;
12141229
12151230#define zig_as_u128(hi, lo) ((zig_u128){ .h##i = (hi), .l##o = (lo) })
12161231#define zig_as_i128(hi, lo) ((zig_i128){ .h##i = (hi), .l##o = (lo) })
1232
1233#if _MSC_VER
12171234#define zig_as_constant_u128(hi, lo) { .h##i = (hi), .l##o = (lo) }
12181235#define zig_as_constant_i128(hi, lo) { .h##i = (hi), .l##o = (lo) }
1236#else
1237#define zig_as_constant_u128(hi, lo) zig_as_u128(hi, lo)
1238#define zig_as_constant_i128(hi, lo) zig_as_i128(hi, lo)
1239#endif
12191240#define zig_hi_u128(val) ((val).hi)
12201241#define zig_lo_u128(val) ((val).lo)
12211242#define zig_hi_i128(val) ((val).hi)
......@@ -1344,13 +1365,13 @@ static inline zig_u128 zig_shr_u128(zig_u128 lhs, zig_u8 rhs) {
13441365
13451366static inline zig_u128 zig_shl_u128(zig_u128 lhs, zig_u8 rhs) {
13461367 if (rhs == zig_as_u8(0)) return lhs;
1347 if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.lo << rhs, .lo = zig_minInt_u64 };
1368 if (rhs >= zig_as_u8(64)) return (zig_u128){ .hi = lhs.lo << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
13481369 return (zig_u128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
13491370}
13501371
13511372static inline zig_i128 zig_shl_i128(zig_i128 lhs, zig_u8 rhs) {
13521373 if (rhs == zig_as_u8(0)) return lhs;
1353 if (rhs >= zig_as_u8(64)) return (zig_i128){ .hi = lhs.lo << rhs, .lo = zig_minInt_u64 };
1374 if (rhs >= zig_as_u8(64)) return (zig_i128){ .hi = lhs.lo << (rhs - zig_as_u8(64)), .lo = zig_minInt_u64 };
13541375 return (zig_i128){ .hi = lhs.hi << rhs | lhs.lo >> (zig_as_u8(64) - rhs), .lo = lhs.lo << rhs };
13551376}
13561377
......@@ -1379,6 +1400,10 @@ static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) {
13791400}
13801401
13811402zig_extern zig_i128 __multi3(zig_i128 lhs, zig_i128 rhs);
1403static zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) {
1404 return zig_bitcast_u128(__multi3(zig_bitcast_i128(lhs), zig_bitcast_i128(rhs)));
1405}
1406
13821407static zig_i128 zig_mul_i128(zig_i128 lhs, zig_i128 rhs) {
13831408 return __multi3(lhs, rhs);
13841409}
......@@ -1474,17 +1499,6 @@ static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
14741499 return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
14751500}
14761501
1477#if _MSC_VER
1478static zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs) {
1479 zig_u64 lo_carry;
1480 zig_u64 lo = _umul128(lhs.lo, rhs.lo, &lo_carry);
1481 zig_u64 hi = lhs.hi * rhs.lo + lhs.lo * rhs.hi + lo_carry;
1482 return zig_as_u128(hi, lo);
1483}
1484#else
1485static zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs); // TODO
1486#endif
1487
14881502static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
14891503 return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits);
14901504}
......@@ -1857,10 +1871,13 @@ typedef zig_i32 zig_f32;
18571871#define zig_bitSizeOf_f64 64
18581872#define zig_libc_name_f64(name) name
18591873#if _MSC_VER
1874#ifdef ZIG_TARGET_ABI_MSVC
1875#define zig_bitSizeOf_c_longdouble 64
1876#endif
18601877#define zig_as_special_constant_f64(sign, name, arg, repr) sign zig_as_f64(zig_msvc_flt_##name, )
1861#else
1878#else /* _MSC_VER */
18621879#define zig_as_special_constant_f64(sign, name, arg, repr) zig_as_special_f64(sign, name, arg, repr)
1863#endif
1880#endif /* _MSC_VER */
18641881#if FLT_MANT_DIG == 53
18651882typedef float zig_f64;
18661883#define zig_as_f64(fp, repr) fp##f
......@@ -1962,16 +1979,32 @@ typedef zig_i128 zig_f128;
19621979#endif
19631980
19641981#define zig_has_c_longdouble 1
1982
1983#ifdef ZIG_TARGET_ABI_MSVC
1984#define zig_libc_name_c_longdouble(name) name
1985#else
19651986#define zig_libc_name_c_longdouble(name) name##l
1987#endif
1988
19661989#define zig_as_special_constant_c_longdouble(sign, name, arg, repr) zig_as_special_c_longdouble(sign, name, arg, repr)
19671990#ifdef zig_bitSizeOf_c_longdouble
1991
1992#ifdef ZIG_TARGET_ABI_MSVC
1993typedef double zig_c_longdouble;
1994#undef zig_bitSizeOf_c_longdouble
1995#define zig_bitSizeOf_c_longdouble 64
1996#define zig_as_c_longdouble(fp, repr) fp
1997#else
19681998typedef long double zig_c_longdouble;
19691999#define zig_as_c_longdouble(fp, repr) fp##l
1970#else
2000#endif
2001
2002#else /* zig_bitSizeOf_c_longdouble */
2003
19712004#undef zig_has_c_longdouble
2005#define zig_has_c_longdouble 0
19722006#define zig_bitSizeOf_c_longdouble 80
19732007#define zig_compiler_rt_abbrev_c_longdouble zig_compiler_rt_abbrev_f80
1974#define zig_has_c_longdouble 0
19752008#define zig_repr_c_longdouble i128
19762009typedef zig_i128 zig_c_longdouble;
19772010#define zig_as_c_longdouble(fp, repr) repr
......@@ -1979,19 +2012,26 @@ typedef zig_i128 zig_c_longdouble;
19792012#define zig_as_special_c_longdouble(sign, name, arg, repr) repr
19802013#undef zig_as_special_constant_c_longdouble
19812014#define zig_as_special_constant_c_longdouble(sign, name, arg, repr) repr
1982#endif
2015
2016#endif /* zig_bitSizeOf_c_longdouble */
19832017
19842018#if !zig_has_float_builtins
19852019#define zig_float_from_repr(Type, ReprType) \
19862020 static inline zig_##Type zig_float_from_repr_##Type(zig_##ReprType repr) { \
19872021 return *((zig_##Type*)&repr); \
19882022 }
2023
19892024zig_float_from_repr(f16, u16)
19902025zig_float_from_repr(f32, u32)
19912026zig_float_from_repr(f64, u64)
19922027zig_float_from_repr(f80, u128)
19932028zig_float_from_repr(f128, u128)
2029#if zig_bitSizeOf_c_longdouble == 80
19942030zig_float_from_repr(c_longdouble, u128)
2031#else
2032#define zig_expand_float_from_repr(Type, ReprType) zig_float_from_repr(Type, ReprType)
2033zig_expand_float_from_repr(c_longdouble, zig_expand_concat(u, zig_bitSizeOf_c_longdouble))
2034#endif
19952035#endif
19962036
19972037#define zig_cast_f16 (zig_f16)
......@@ -2218,8 +2258,11 @@ zig_msvc_atomics(u16, 16)
22182258zig_msvc_atomics(i16, 16)
22192259zig_msvc_atomics(u32, )
22202260zig_msvc_atomics(i32, )
2261
2262#if _M_X64
22212263zig_msvc_atomics(u64, 64)
22222264zig_msvc_atomics(i64, 64)
2265#endif
22232266
22242267#define zig_msvc_flt_atomics(Type, ReprType, suffix) \
22252268 static inline bool zig_msvc_cmpxchg_##Type(zig_##Type volatile* obj, zig_##Type* expected, zig_##Type desired) { \
......@@ -2259,9 +2302,18 @@ zig_msvc_atomics(i64, 64)
22592302 }
22602303
22612304zig_msvc_flt_atomics(f32, u32, )
2305#if _M_X64
22622306zig_msvc_flt_atomics(f64, u64, 64)
2307#endif
22632308
22642309#if _M_IX86
2310static inline void zig_msvc_atomic_barrier() {
2311 zig_i32 barrier;
2312 __asm {
2313 xchg barrier, eax
2314 }
2315}
2316
22652317static inline void* zig_msvc_atomicrmw_xchg_p32(void** obj, zig_u32* arg) {
22662318 return _InterlockedExchangePointer(obj, arg);
22672319}
......@@ -2270,7 +2322,7 @@ static inline void zig_msvc_atomic_store_p32(void** obj, zig_u32* arg) {
22702322 _InterlockedExchangePointer(obj, arg);
22712323}
22722324
2273static inline void* zig_msvc_atomic_load_p32(void** obj, zig_u32* arg) {
2325static inline void* zig_msvc_atomic_load_p32(void** obj) {
22742326 return (void*)_InterlockedOr((void*)obj, 0);
22752327}
22762328
......@@ -2283,7 +2335,7 @@ static inline bool zig_msvc_cmpxchg_p32(void** obj, void** expected, void* desir
22832335 }
22842336 return exchanged;
22852337}
2286#else
2338#else /* _M_IX86 */
22872339static inline void* zig_msvc_atomicrmw_xchg_p64(void** obj, zig_u64* arg) {
22882340 return _InterlockedExchangePointer(obj, arg);
22892341}
......@@ -2305,7 +2357,6 @@ static inline bool zig_msvc_cmpxchg_p64(void** obj, void** expected, void* desir
23052357 }
23062358 return exchanged;
23072359}
2308#endif
23092360
23102361static inline bool zig_msvc_cmpxchg_u128(zig_u128 volatile* obj, zig_u128* expected, zig_u128 desired) {
23112362 return _InterlockedCompareExchange128((zig_i64 volatile*)obj, desired.hi, desired.lo, (zig_i64*)expected);
......@@ -2350,6 +2401,7 @@ zig_msvc_atomics_128op(u128, and)
23502401zig_msvc_atomics_128op(u128, nand)
23512402zig_msvc_atomics_128op(u128, min)
23522403zig_msvc_atomics_128op(u128, max)
2404#endif /* _M_IX86 */
23532405
23542406#endif /* _MSC_VER && (_M_IX86 || _M_X64) */
23552407
......@@ -2359,7 +2411,7 @@ zig_msvc_atomics_128op(u128, max)
23592411
23602412static inline void* zig_x86_64_windows_teb(void) {
23612413#if _MSC_VER
2362 return __readgsqword(0x30);
2414 return (void*)__readgsqword(0x30);
23632415#else
23642416 void* teb;
23652417 __asm volatile(" movq %%gs:0x30, %[ptr]": [ptr]"=r"(teb)::);
......@@ -2367,6 +2419,18 @@ static inline void* zig_x86_64_windows_teb(void) {
23672419#endif
23682420}
23692421
2422#elif (_MSC_VER && _M_IX86) || defined(__i386__) || defined(__X86__)
2423
2424static inline void* zig_x86_windows_teb(void) {
2425#if _MSC_VER
2426 return (void*)__readfsdword(0x18);
2427#else
2428 void* teb;
2429 __asm volatile(" movl %%fs:0x18, %[ptr]": [ptr]"=r"(teb)::);
2430 return teb;
2431#endif
2432}
2433
23702434#endif
23712435
23722436#if (_MSC_VER && (_M_IX86 || _M_X64)) || defined(__i386__) || defined(__x86_64__)
src/codegen/c.zig+23-2
......@@ -1459,7 +1459,12 @@ pub const DeclGen = struct {
14591459
14601460 fn renderFunctionSignature(dg: *DeclGen, w: anytype, kind: TypedefKind, export_index: u32) !void {
14611461 const fn_info = dg.decl.ty.fnInfo();
1462 if (fn_info.cc == .Naked) try w.writeAll("zig_naked ");
1462 if (fn_info.cc == .Naked) {
1463 switch (kind) {
1464 .Forward => try w.writeAll("zig_naked_decl "),
1465 .Complete => try w.writeAll("zig_naked "),
1466 }
1467 }
14631468 if (dg.decl.val.castTag(.function)) |func_payload|
14641469 if (func_payload.data.is_cold) try w.writeAll("zig_cold ");
14651470
......@@ -1469,6 +1474,13 @@ pub const DeclGen = struct {
14691474
14701475 try dg.renderType(w, ret_ty, kind);
14711476 try w.writeByte(' ');
1477
1478 if (toCallingConvention(fn_info.cc)) |call_conv| {
1479 try w.print("zig_callconv({s}) ", .{call_conv});
1480 }
1481
1482 if (fn_info.alignment > 0 and kind == .Complete) try w.print(" zig_align_fn({})", .{fn_info.alignment});
1483
14721484 try dg.renderDeclName(w, dg.decl_index, export_index);
14731485 try w.writeByte('(');
14741486
......@@ -1488,7 +1500,7 @@ pub const DeclGen = struct {
14881500 try dg.renderType(w, Type.void, kind);
14891501 }
14901502 try w.writeByte(')');
1491 if (fn_info.alignment > 0) try w.print(" zig_align_fn({})", .{fn_info.alignment});
1503 if (fn_info.alignment > 0 and kind == .Forward) try w.print(" zig_align_fn({})", .{fn_info.alignment});
14921504 }
14931505
14941506 fn renderPtrToFnTypedef(dg: *DeclGen, t: Type) error{ OutOfMemory, AnalysisFail }![]const u8 {
......@@ -7035,6 +7047,15 @@ fn writeMemoryOrder(w: anytype, order: std.builtin.AtomicOrder) !void {
70357047 return w.writeAll(toMemoryOrder(order));
70367048}
70377049
7050fn toCallingConvention(call_conv: std.builtin.CallingConvention) ?[]const u8 {
7051 return switch (call_conv) {
7052 .Stdcall => "stdcall",
7053 .Fastcall => "fastcall",
7054 .Vectorcall => "vectorcall",
7055 else => null,
7056 };
7057}
7058
70387059fn toAtomicRmwSuffix(order: std.builtin.AtomicRmwOp) []const u8 {
70397060 return switch (order) {
70407061 .Xchg => "xchg",
src/link/C.zig+14-2
......@@ -231,6 +231,13 @@ pub fn flush(self: *C, comp: *Compilation, prog_node: *std.Progress.Node) !void
231231 return self.flushModule(comp, prog_node);
232232}
233233
234fn abiDefine(comp: *Compilation) ?[]const u8 {
235 return switch (comp.getTarget().abi) {
236 .msvc => "#define ZIG_TARGET_ABI_MSVC\n",
237 else => null,
238 };
239}
240
234241pub fn flushModule(self: *C, comp: *Compilation, prog_node: *std.Progress.Node) !void {
235242 const tracy = trace(@src());
236243 defer tracy.end();
......@@ -248,9 +255,14 @@ pub fn flushModule(self: *C, comp: *Compilation, prog_node: *std.Progress.Node)
248255 var f: Flush = .{};
249256 defer f.deinit(gpa);
250257
251 // Covers zig.h, typedef, and asm.
252 try f.all_buffers.ensureUnusedCapacity(gpa, 2);
258 const abi_define = abiDefine(comp);
259
260 // Covers defines, zig.h, typedef, and asm.
261 var buf_count: usize = 2;
262 if (abi_define != null) buf_count += 1;
263 try f.all_buffers.ensureUnusedCapacity(gpa, buf_count);
253264
265 if (abi_define) |buf| f.appendBufAssumeCapacity(buf);
254266 f.appendBufAssumeCapacity(zig_h);
255267
256268 const typedef_index = f.all_buffers.items.len;
stage1/zig1.wasm
Binary files a/stage1/zig1.wasm and b/stage1/zig1.wasm differ
test/behavior/align.zig+12-1
......@@ -104,7 +104,18 @@ test "alignment and size of structs with 128-bit fields" {
104104 .u129_size = 24,
105105 },
106106
107 .x86 => switch (builtin.os.tag) {
107 .x86 => if (builtin.object_format == .c) .{
108 .a_align = 16,
109 .a_size = 16,
110
111 .b_align = 16,
112 .b_size = 32,
113
114 .u128_align = 16,
115 .u128_size = 16,
116 .u129_align = 16,
117 .u129_size = 32,
118 } else switch (builtin.os.tag) {
108119 .windows => .{
109120 .a_align = 8,
110121 .a_size = 16,
test/behavior/int128.zig+28
......@@ -84,3 +84,31 @@ test "truncate int128" {
8484 try expect(@truncate(i128, buff) == maxInt(i128));
8585 }
8686}
87
88test "shift int128" {
89 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
90 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
91 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
92 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
93 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
94
95 const types = .{ u128, i128 };
96 inline for (types) |t| {
97 try testShlTrunc(t, 0x8, 123);
98 comptime try testShlTrunc(t, 0x8, 123);
99
100 try testShlTrunc(t, 0x40000000_00000000, 64);
101 comptime try testShlTrunc(t, 0x40000000_00000000, 64);
102
103 try testShlTrunc(t, 0x01000000_00000000_00000000, 38);
104 comptime try testShlTrunc(t, 0x01000000_00000000_00000000, 38);
105
106 try testShlTrunc(t, 0x00000008_00000000_00000000_00000000, 27);
107 comptime try testShlTrunc(t, 0x00000008_00000000_00000000_00000000, 27);
108 }
109}
110
111fn testShlTrunc(comptime Type: type, x: Type, rhs: u7) !void {
112 const shifted = x << rhs;
113 try expect(shifted == @as(Type, 0x40000000_00000000_00000000_00000000));
114}