Adds examples/1651-platform-asm-x86-syscall-write.sx — the canonical
inline-asm use case: `write(2)` via a raw x86_64 `syscall` (SYS_write
in rax, fd/buf/count pinned to rdi/rsi/rdx, rcx+r11+memory clobbered,
byte count returned in rax). Exercises register-pinned inputs, a pinned
value output, a pointer input (*u8 -> rsi), and clobbers(.…) lowering
together.
x86-pinned via .build { "target": "x86_64-linux" }: ir-only on this
aarch64 host (the .ir snapshot locks the exact constraint string
`={rax},{rax},{rdi},{rsi},{rdx},~{rcx},~{r11},~{memory}` — the §II.11
silent-miscompile risk zone), runs natively on x86_64-linux printing
"ok\n" (hand-authored .stdout, asserted only in execute mode).
Pure additive test coverage — no compiler change (lock commit).
zig build test green (660 corpus, 446 unit).
28 lines
1.3 KiB
Plaintext
28 lines
1.3 KiB
Plaintext
// ASM stream — x86_64 Linux `write(2)` via a raw `syscall`. The canonical inline-
|
|
// asm use case: SYS_write (rax=1) with fd/buf/count pinned to rdi/rsi/rdx, the
|
|
// `syscall` instruction clobbering rcx + r11 (+ memory), and the byte count
|
|
// returned in rax. Demonstrates register-pinned inputs, a pinned value output,
|
|
// a pointer input (`*u8` → rsi), and `clobbers(.…)` lowering all at once.
|
|
//
|
|
// x86-pinned via `.build`: ir-only on a non-x86 host — the `.ir` snapshot locks
|
|
// the exact constraint string (`={rax},{rax},{rdi},{rsi},{rdx},~{rcx},~{r11},
|
|
// ~{memory}`), which is the §II.11 silent-miscompile risk zone — and runs
|
|
// natively on x86_64-linux (printing "ok\n"). See 1640 for an x86 multi-output
|
|
// example, 1645/1647/1649/1650 for aarch64 examples that execute on this host.
|
|
sys_write :: (fd: i64, buf: *u8, count: i64) -> i64 {
|
|
return asm volatile {
|
|
"syscall",
|
|
[ret] "={rax}" -> i64, // return: bytes written, in rax
|
|
"{rax}" = 1, // SYS_write (x86_64 Linux)
|
|
"{rdi}" = fd, // fd
|
|
"{rsi}" = buf, // buf
|
|
"{rdx}" = count, // count
|
|
clobbers(.rcx, .r11, .memory),
|
|
};
|
|
}
|
|
|
|
main :: () {
|
|
msg : [3]u8 = .[111, 107, 10]; // "ok\n"
|
|
n := sys_write(1, @msg[0], 3);
|
|
}
|