authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-23 13:21:39-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-25 05:11:29-04:00
log15df64ade8e460141d6485340ca0f88d21f03c47
tree1ad5584aa242f04c28a118d656cec0888da22132
parent1bab8548688ec13d29abbf3820bb2f9723e09c66

std: add cbe hacks to more targets

These are needed because clang doesn't support anything in naked functions, not even assembly register inputs.

8 files changed, 80 insertions(+), 36 deletions(-)

lib/std/os/linux/arm64.zig+14-5
......@@ -106,11 +106,20 @@ pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *
106106pub const restore = restore_rt;
107107
108108pub fn restore_rt() callconv(.Naked) void {
109 return asm volatile ("svc #0"
110 :
111 : [number] "{x8}" (@enumToInt(SYS.rt_sigreturn)),
112 : "memory", "cc"
113 );
109 switch (@import("builtin").zig_backend) {
110 .stage2_c => return asm volatile (
111 \\ mov x8, %[number]
112 \\ svc #0
113 :
114 : [number] "i" (@enumToInt(SYS.rt_sigreturn)),
115 : "memory", "cc"
116 ),
117 else => return asm volatile ("svc #0"
118 :
119 : [number] "{x8}" (@enumToInt(SYS.rt_sigreturn)),
120 : "memory", "cc"
121 ),
122 }
114123}
115124
116125pub const O = struct {
lib/std/os/linux/i386.zig+28-10
......@@ -124,19 +124,37 @@ const CloneFn = std.meta.FnPtr(fn (arg: usize) callconv(.C) u8);
124124pub extern fn clone(func: CloneFn, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
125125
126126pub fn restore() callconv(.Naked) void {
127 return asm volatile ("int $0x80"
128 :
129 : [number] "{eax}" (@enumToInt(SYS.sigreturn)),
130 : "memory"
131 );
127 switch (@import("builtin").zig_backend) {
128 .stage2_c => return asm volatile (
129 \\ movl %[number], %%eax
130 \\ int $0x80
131 :
132 : [number] "i" (@enumToInt(SYS.sigreturn)),
133 : "memory"
134 ),
135 else => return asm volatile ("int $0x80"
136 :
137 : [number] "{eax}" (@enumToInt(SYS.sigreturn)),
138 : "memory"
139 ),
140 }
132141}
133142
134143pub fn restore_rt() callconv(.Naked) void {
135 return asm volatile ("int $0x80"
136 :
137 : [number] "{eax}" (@enumToInt(SYS.rt_sigreturn)),
138 : "memory"
139 );
144 switch (@import("builtin").zig_backend) {
145 .stage2_c => return asm volatile (
146 \\ movl %[number], %%eax
147 \\ int $0x80
148 :
149 : [number] "i" (@enumToInt(SYS.rt_sigreturn)),
150 : "memory"
151 ),
152 else => return asm volatile ("int $0x80"
153 :
154 : [number] "{eax}" (@enumToInt(SYS.rt_sigreturn)),
155 : "memory"
156 ),
157 }
140158}
141159
142160pub const O = struct {
lib/std/os/linux/x86_64.zig+8-5
......@@ -109,11 +109,14 @@ pub const restore = restore_rt;
109109
110110pub fn restore_rt() callconv(.Naked) void {
111111 switch (@import("builtin").zig_backend) {
112 .stage2_c => return asm volatile (std.fmt.comptimePrint(
113 \\ movl ${d}, %%eax
114 \\ syscall
115 \\ retq
116 , .{@enumToInt(SYS.rt_sigreturn)}) ::: "rcx", "r11", "memory"),
112 .stage2_c => return asm volatile (
113 \\ movl %[number], %%eax
114 \\ syscall
115 \\ retq
116 :
117 : [number] "i" (@enumToInt(SYS.rt_sigreturn)),
118 : "rcx", "r11", "memory"
119 ),
117120 else => return asm volatile ("syscall"
118121 :
119122 : [number] "{rax}" (@enumToInt(SYS.rt_sigreturn)),
lib/std/start.zig+26-12
......@@ -265,23 +265,37 @@ fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) callconv
265265
266266fn _start() callconv(.Naked) noreturn {
267267 switch (builtin.zig_backend) {
268 .stage2_c => switch (native_arch) {
269 .x86_64 => {
270 @export(argc_argv_ptr, .{ .name = "argc_argv_ptr" });
271 @export(posixCallMainAndExit, .{ .name = "_posixCallMainAndExit" });
272 asm volatile (
273 \\ xor %%rbp, %%rbp
274 \\ mov %%rsp, argc_argv_ptr
268 .stage2_c => {
269 @export(argc_argv_ptr, .{ .name = "argc_argv_ptr" });
270 @export(posixCallMainAndExit, .{ .name = "_posixCallMainAndExit" });
271 switch (native_arch) {
272 .x86_64 => asm volatile (
273 \\ xorl %%ebp, %%ebp
274 \\ movq %%rsp, argc_argv_ptr
275 \\ andq $-16, %%rsp
275276 \\ call _posixCallMainAndExit
276 );
277 unreachable;
278 },
279 else => @compileError("unsupported arch"),
277 ),
278 .i386 => asm volatile (
279 \\ xorl %%ebp, %%ebp
280 \\ movl %%esp, argc_argv_ptr
281 \\ andl $-16, %%esp
282 \\ jmp _posixCallMainAndExit
283 ),
284 .aarch64, .aarch64_be, .arm, .armeb, .thumb => asm volatile (
285 \\ mov fp, #0
286 \\ mov lr, #0
287 \\ str sp, argc_argv_ptr
288 \\ and sp, #-16
289 \\ b _posixCallMainAndExit
290 ),
291 else => @compileError("unsupported arch"),
292 }
293 unreachable;
280294 },
281295 else => switch (native_arch) {
282296 .x86_64 => {
283297 argc_argv_ptr = asm volatile (
284 \\ xor %%rbp, %%rbp
298 \\ xor %%ebp, %%ebp
285299 : [argc] "={rsp}" (-> [*]usize),
286300 );
287301 },
test/cases/aarch64-macos/hello_world_with_updates.0.zig+1-1
......@@ -2,4 +2,4 @@
22// output_mode=Exe
33// target=aarch64-macos
44//
5// :109:9: error: root struct of file 'tmp' has no member named 'main'
5// :108:9: error: root struct of file 'tmp' has no member named 'main'
test/cases/x86_64-linux/hello_world_with_updates.0.zig+1-1
......@@ -2,4 +2,4 @@
22// output_mode=Exe
33// target=x86_64-linux
44//
5// :109:9: error: root struct of file 'tmp' has no member named 'main'
5// :108:9: error: root struct of file 'tmp' has no member named 'main'
test/cases/x86_64-macos/hello_world_with_updates.0.zig+1-1
......@@ -2,4 +2,4 @@
22// output_mode=Exe
33// target=x86_64-macos
44//
5// :109:9: error: root struct of file 'tmp' has no member named 'main'
5// :108:9: error: root struct of file 'tmp' has no member named 'main'
test/cases/x86_64-windows/hello_world_with_updates.0.zig+1-1
......@@ -2,4 +2,4 @@
22// output_mode=Exe
33// target=x86_64-windows
44//
5// :130:9: error: root struct of file 'tmp' has no member named 'main'
5// :129:9: error: root struct of file 'tmp' has no member named 'main'