Commit 3106998412

Alex Rønne Petersen <alex@alexrp.com>
2024-08-21 14:21:01
compiler_rt: Implement __chkstk() for thumb-windows-gnu.
https://github.com/llvm/llvm-project/blob/ad435bcc14f42dc97286c717cd12446a0facb2ee/compiler-rt/lib/builtins/arm/chkstk.S
1 parent 338730d
Changed files (1)
lib
compiler_rt
lib/compiler_rt/stack_probe.zig
@@ -5,9 +5,6 @@ const arch = builtin.cpu.arch;
 const abi = builtin.abi;
 const is_test = builtin.is_test;
 
-const is_gnu = abi.isGnu();
-const is_mingw = os_tag == .windows and is_gnu;
-
 const linkage: std.builtin.GlobalLinkage = if (builtin.is_test) .internal else .weak;
 const strong_linkage: std.builtin.GlobalLinkage = if (builtin.is_test) .internal else .strong;
 pub const panic = @import("common.zig").panic;
@@ -15,11 +12,11 @@ pub const panic = @import("common.zig").panic;
 comptime {
     if (builtin.os.tag == .windows) {
         // Default stack-probe functions emitted by LLVM
-        if (is_mingw) {
+        if (builtin.target.isMinGW()) {
             @export(&_chkstk, .{ .name = "_alloca", .linkage = linkage });
             @export(&___chkstk_ms, .{ .name = "___chkstk_ms", .linkage = linkage });
 
-            if (arch.isAARCH64()) {
+            if (arch == .thumb or arch == .aarch64) {
                 @export(&__chkstk, .{ .name = "__chkstk", .linkage = linkage });
             }
         } else if (!builtin.link_libc) {
@@ -100,6 +97,35 @@ fn win_probe_stack_only() void {
     @setRuntimeSafety(false);
 
     switch (arch) {
+        .thumb => {
+            asm volatile (
+                \\ lsl r4, r4, #2
+                \\ mov r12, sp
+                \\ push {r5, r6}
+                \\ mov r5, r4
+                \\1:
+                \\ sub r12, r12, #4096
+                \\ subs r5, r5, #4096
+                \\ ldr r6, [r12]
+                \\ bgt 1b
+                \\ pop {r5, r6}
+                \\ bx lr
+            );
+        },
+        .aarch64 => {
+            asm volatile (
+                \\        lsl    x16, x15, #4
+                \\        mov    x17, sp
+                \\1:
+                \\
+                \\        sub    x17, x17, 4096
+                \\        subs   x16, x16, 4096
+                \\        ldr    xzr, [x17]
+                \\        b.gt   1b
+                \\
+                \\        ret
+            );
+        },
         .x86_64 => {
             asm volatile (
                 \\         push   %%rcx
@@ -144,21 +170,6 @@ fn win_probe_stack_only() void {
         },
         else => {},
     }
-    if (comptime arch.isAARCH64()) {
-        // NOTE: page size hardcoded to 4096 for now
-        asm volatile (
-            \\        lsl    x16, x15, #4
-            \\        mov    x17, sp
-            \\1:
-            \\
-            \\        sub    x17, x17, 4096
-            \\        subs   x16, x16, 4096
-            \\        ldr    xzr, [x17]
-            \\        b.gt   1b
-            \\
-            \\        ret
-        );
-    }
 
     unreachable;
 }
@@ -240,7 +251,7 @@ pub fn _chkstk() callconv(.Naked) void {
 }
 pub fn __chkstk() callconv(.Naked) void {
     @setRuntimeSafety(false);
-    if (comptime arch.isAARCH64()) {
+    if (arch == .thumb or arch == .aarch64) {
         @call(.always_inline, win_probe_stack_only, .{});
     } else switch (arch) {
         .x86 => @call(.always_inline, win_probe_stack_adjust_sp, .{}),