Skip to content

Conversation

taylordotfish
Copy link
Contributor

On PowerPC, Clang fails to compile code generated by the C backend, due to the implementation of restore_rt:

$ cat main.zig
pub fn main() void {}
$ zig build-exe -ofmt=c -OReleaseSmall main.zig
$ clang -I/opt/zig/lib -nostartfiles main.c
main.c:1488:2: error: non-ASM statement in naked function is not supported
 1488 |  register uintptr_t const t0 __asm("r0") = (uintptr_t)172ul;
      |  ^
main.c:1487:8: note: attribute is here
 1487 | static zig_naked zig_noreturn void os_linux_powerpc64_restore_rt__3126(void) {
      |        ^

This is because restore_rt on PowerPC uses a register input operand for the syscall number, which is compiled as a separate register uintptr_t declaration, disallowed in naked functions.

Following the same approach as x86 and ARM, when using the C backend, this PR employs alternative inline assembly that avoids using non-immediate input operands.

This PR also adds some registers that were missing from the list of clobbers. As with all syscalls, the registers r0, r3, ctr, and xer should also be specified as clobbers or outputs (see musl).

* Clang fails to compile the CBE translation of this code ("non-ASM
  statement in naked function"). Similar to the implementations of
  `restore_rt` on x86 and ARM, when the CBE is in use, this commit
  employs alternative inline assembly that avoids using non-immediate
  input operands.
* As with all syscalls, the registers r0, r3, ctr, and xer should also
  be specified as clobbers.
As far as I can tell, this has only been a problem with the C backend
(see ziglang#25229), which is specifically excluded from this inline asm call.
However, it's probably best to avoid it anyway and use output registers
instead. In this case, because we're ignoring r3, which is the real
"output" of the syscall, we can specify r0 as the sole `->` output
instead, avoiding the need for a separate variable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants