Skip to content

Wire Mach-O direct backend and add std.fs.appendBytes - #207

Open
harshav167 wants to merge 2 commits into
vercel-labs:mainfrom
anslhq:anslhq/macho-direct-fs-opcodes
Open

Wire Mach-O direct backend and add std.fs.appendBytes#207
harshav167 wants to merge 2 commits into
vercel-labs:mainfrom
anslhq:anslhq/macho-direct-fs-opcodes

Conversation

@harshav167

Copy link
Copy Markdown

What

Adds the missing AArch64 Mach-O backend coverage for filesystem opcodes that already work on ELF64, and reserves a new std.fs.appendBytes API alongside it.

Two commits:

  1. Wire Mach-O direct backend for std.mem.eql and path-form std.fs helpers — lowers IR_VALUE_BYTE_VIEW_EQ, the FS_EXISTS / FS_IS_DIR / FS_MAKE_DIR / FS_REMOVE / FS_REMOVE_DIR family, and the FS_WRITE_PATH / FS_WRITE_BYTES_PATH / FS_READ_PATH / FS_READ_BYTES_PATH family to inline AArch64 syscalls using the shared z_aarch64_emit primitives. No runtime helper, no extra link plan — matches the ELF64 design.

  2. Add std.fs.appendBytes — new IR_VALUE_FS_APPEND_BYTES_PATH returning Maybe<usize>. Lowered on both ELF64 and Mach-O by reusing the existing write codepath with O_APPEND flags. Bumps the relevant compiler-metrics budgets for the changed files and functions. Includes a conformance test that appends two byte spans and reads the file back.

Why

Before this PR, programs that hit std.mem.eql or any std.fs.* path-form helper on darwin-arm64 errored with CGEN004 even though the IR opcodes existed and the ELF64 backend already lowered them. The exe builds for darwin-arm64 had to route through the object backend (and external linker), which is the slower path. This PR closes that parity gap.

std.fs.appendBytes is a small addition that programs need when they want to accumulate to a file across calls (logging, ledgers, line-by-line accumulation) without first reading the existing contents or threading an owned File handle through every call site.

Verification

Locally on darwin-arm64:

  • make -C native/zero-c — clean build with -std=c11 -Wall -Wextra -Wpedantic -Os.
  • pnpm run conformance:local — provenance guardrails, type core smoke, MIR verifier smoke, row syntax smoke, and conformance all ok.
  • pnpm run native:test:localhttp runtime smoke ok, native conformance ok.
  • pnpm run command-contracts:local — snapshots ok.
  • pnpm run docs:test — pass.
  • pnpm run test:zero — pass.
  • pnpm run reliability:smoke — ok.

The new conformance test conformance/native/pass/std-fs-append.0 builds end-to-end via zero build --backend zero-macho64 --emit exe and prints fs append ok after writing the expected helo\nwo byte sequence.

Notes

  • All syscalls use Darwin's svc #0x80 convention with the syscall number in x16 and the carry flag indicating error.
  • FS_IS_DIR carves a 144-byte struct stat64 from the stack, calls SYS_stat64, and checks S_IFMT & st_mode == S_IFDIR at offset 4.
  • The new std.fs.appendBytes is exposed as (path: String, bytes: Span<u8>) -> Maybe<usize> — matching the shape of std.fs.writeBytes.

- Lower IR_VALUE_BYTE_VIEW_EQ to inline AArch64 byte-by-byte compare.

- Lower IR_VALUE_FS_EXISTS, IR_VALUE_FS_IS_DIR, IR_VALUE_FS_MAKE_DIR,
  IR_VALUE_FS_REMOVE, IR_VALUE_FS_REMOVE_DIR to inline Darwin syscalls.

- Lower IR_VALUE_FS_WRITE_PATH, IR_VALUE_FS_WRITE_BYTES_PATH,
  IR_VALUE_FS_READ_PATH, IR_VALUE_FS_READ_BYTES_PATH to inline
  open + read/write + close sequences.

- Use the shared z_aarch64_emit primitives; no runtime helper, no
  extra link plan, matching the ELF64 design.
- Reserve IR_VALUE_FS_APPEND_BYTES_PATH and register it in the std
  signature table, checker, lowerer, and MIR verifier.

- Lower the ELF64 emit case alongside writeBytes, switching open
  flags between O_CREAT|O_TRUNC and O_CREAT|O_APPEND.

- Lower the Mach-O direct emit case the same way for darwin-arm64.

- Bump the compiler-metrics budgets that grow with the new opcode
  case lines: emit_macho64.c, emit_elf64.c::elf_emit_value,
  ir.c::ir_lower_expr and strcmp count, checker.c::std_call_arg_type,
  mir_verify.c::mir_verify_direct_value_kind_contract, main.c.

- Document the API in docs/articles/modules/fs.md and add a native
  conformance test that appends two byte spans and reads them back.
@vercel

vercel Bot commented May 22, 2026

Copy link
Copy Markdown

@harshav167 is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant