Commit 15df64ade8

Jacob Young <jacobly0@users.noreply.github.com>
2022-10-23 19:21:39
std: add cbe hacks to more targets
These are needed because clang doesn't support anything in naked functions, not even assembly register inputs.
1 parent 1bab854
lib/std/os/linux/arm64.zig
@@ -106,11 +106,20 @@ pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *
 pub const restore = restore_rt;
 
 pub fn restore_rt() callconv(.Naked) void {
-    return asm volatile ("svc #0"
-        :
-        : [number] "{x8}" (@enumToInt(SYS.rt_sigreturn)),
-        : "memory", "cc"
-    );
+    switch (@import("builtin").zig_backend) {
+        .stage2_c => return asm volatile (
+            \\ mov x8, %[number]
+            \\ svc #0
+            :
+            : [number] "i" (@enumToInt(SYS.rt_sigreturn)),
+            : "memory", "cc"
+        ),
+        else => return asm volatile ("svc #0"
+            :
+            : [number] "{x8}" (@enumToInt(SYS.rt_sigreturn)),
+            : "memory", "cc"
+        ),
+    }
 }
 
 pub const O = struct {
lib/std/os/linux/i386.zig
@@ -124,19 +124,37 @@ const CloneFn = std.meta.FnPtr(fn (arg: usize) callconv(.C) u8);
 pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
 
 pub fn restore() callconv(.Naked) void {
-    return asm volatile ("int $0x80"
-        :
-        : [number] "{eax}" (@enumToInt(SYS.sigreturn)),
-        : "memory"
-    );
+    switch (@import("builtin").zig_backend) {
+        .stage2_c => return asm volatile (
+            \\ movl %[number], %%eax
+            \\ int $0x80
+            :
+            : [number] "i" (@enumToInt(SYS.sigreturn)),
+            : "memory"
+        ),
+        else => return asm volatile ("int $0x80"
+            :
+            : [number] "{eax}" (@enumToInt(SYS.sigreturn)),
+            : "memory"
+        ),
+    }
 }
 
 pub fn restore_rt() callconv(.Naked) void {
-    return asm volatile ("int $0x80"
-        :
-        : [number] "{eax}" (@enumToInt(SYS.rt_sigreturn)),
-        : "memory"
-    );
+    switch (@import("builtin").zig_backend) {
+        .stage2_c => return asm volatile (
+            \\ movl %[number], %%eax
+            \\ int $0x80
+            :
+            : [number] "i" (@enumToInt(SYS.rt_sigreturn)),
+            : "memory"
+        ),
+        else => return asm volatile ("int $0x80"
+            :
+            : [number] "{eax}" (@enumToInt(SYS.rt_sigreturn)),
+            : "memory"
+        ),
+    }
 }
 
 pub const O = struct {
lib/std/os/linux/x86_64.zig
@@ -109,11 +109,14 @@ pub const restore = restore_rt;
 
 pub fn restore_rt() callconv(.Naked) void {
     switch (@import("builtin").zig_backend) {
-        .stage2_c => return asm volatile (std.fmt.comptimePrint(
-                \\ movl ${d}, %%eax
-                \\ syscall
-                \\ retq
-            , .{@enumToInt(SYS.rt_sigreturn)}) ::: "rcx", "r11", "memory"),
+        .stage2_c => return asm volatile (
+            \\ movl %[number], %%eax
+            \\ syscall
+            \\ retq
+            :
+            : [number] "i" (@enumToInt(SYS.rt_sigreturn)),
+            : "rcx", "r11", "memory"
+        ),
         else => return asm volatile ("syscall"
             :
             : [number] "{rax}" (@enumToInt(SYS.rt_sigreturn)),
lib/std/start.zig
@@ -265,23 +265,37 @@ fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) callconv
 
 fn _start() callconv(.Naked) noreturn {
     switch (builtin.zig_backend) {
-        .stage2_c => switch (native_arch) {
-            .x86_64 => {
-                @export(argc_argv_ptr, .{ .name = "argc_argv_ptr" });
-                @export(posixCallMainAndExit, .{ .name = "_posixCallMainAndExit" });
-                asm volatile (
-                    \\ xor %%rbp, %%rbp
-                    \\ mov %%rsp, argc_argv_ptr
+        .stage2_c => {
+            @export(argc_argv_ptr, .{ .name = "argc_argv_ptr" });
+            @export(posixCallMainAndExit, .{ .name = "_posixCallMainAndExit" });
+            switch (native_arch) {
+                .x86_64 => asm volatile (
+                    \\ xorl %%ebp, %%ebp
+                    \\ movq %%rsp, argc_argv_ptr
+                    \\ andq $-16, %%rsp
                     \\ call _posixCallMainAndExit
-                );
-                unreachable;
-            },
-            else => @compileError("unsupported arch"),
+                ),
+                .i386 => asm volatile (
+                    \\ xorl %%ebp, %%ebp
+                    \\ movl %%esp, argc_argv_ptr
+                    \\ andl $-16, %%esp
+                    \\ jmp _posixCallMainAndExit
+                ),
+                .aarch64, .aarch64_be, .arm, .armeb, .thumb => asm volatile (
+                    \\ mov fp, #0
+                    \\ mov lr, #0
+                    \\ str sp, argc_argv_ptr
+                    \\ and sp, #-16
+                    \\ b _posixCallMainAndExit
+                ),
+                else => @compileError("unsupported arch"),
+            }
+            unreachable;
         },
         else => switch (native_arch) {
             .x86_64 => {
                 argc_argv_ptr = asm volatile (
-                    \\ xor %%rbp, %%rbp
+                    \\ xor %%ebp, %%ebp
                     : [argc] "={rsp}" (-> [*]usize),
                 );
             },
test/cases/aarch64-macos/hello_world_with_updates.0.zig
@@ -2,4 +2,4 @@
 // output_mode=Exe
 // target=aarch64-macos
 //
-// :109:9: error: root struct of file 'tmp' has no member named 'main'
+// :108:9: error: root struct of file 'tmp' has no member named 'main'
test/cases/x86_64-linux/hello_world_with_updates.0.zig
@@ -2,4 +2,4 @@
 // output_mode=Exe
 // target=x86_64-linux
 //
-// :109:9: error: root struct of file 'tmp' has no member named 'main'
+// :108:9: error: root struct of file 'tmp' has no member named 'main'
test/cases/x86_64-macos/hello_world_with_updates.0.zig
@@ -2,4 +2,4 @@
 // output_mode=Exe
 // target=x86_64-macos
 //
-// :109:9: error: root struct of file 'tmp' has no member named 'main'
+// :108:9: error: root struct of file 'tmp' has no member named 'main'
test/cases/x86_64-windows/hello_world_with_updates.0.zig
@@ -2,4 +2,4 @@
 // output_mode=Exe
 // target=x86_64-windows
 //
-// :130:9: error: root struct of file 'tmp' has no member named 'main'
+// :129:9: error: root struct of file 'tmp' has no member named 'main'