Platforms
Which operating systems the compiler builds programs and objects for, how
well each is supported, and what building for one may need. A target is
chosen with --target (lowering.md §7), and one machine
builds for every target.
Support
Two tiers. A first-tier platform is one every change must keep working; a lower-tier one is built and tested the same way, but gets less attention, and a problem there may wait.
- Linux and Windows are the first tier. Both are fully supported by
zig cc, which can link for either from any machine, so building for them needs no SDK of their own.- On Linux every test runs, building, linking and running programs.
- On Windows, objects are built and rewritten
(
separate-compilation.mdC9), and the tests hold them to what the stamped build defines. Every codegen test program, plain and optimized, and a program linked against a dependency’s prebuilt objects are built forx86_64-windows-gnuon Linux, linked byzig cc, and run on Windows itself in CI, each held to the output its test expects, and run with the arguments its fixture lists.just test-windowsruns the same programs under Wine.
- macOS is the lower tier. Its objects are built, rewritten and tested
as Windows ones are, and the same programs are built for
aarch64-macosandx86_64-macoson Linux and run on macOS itself in CI, held as Windows’ are.
Each platform’s released toolchain is built, packaged and checked on that platform by the same steps: a program built by the extracted archive runs there plain, optimized and tiered.
On every one of them zanec itself is built natively from the same pixi
environment, and just test-compiler runs there in CI, held to the triple a
build without --target is for, and its image format and C library. The rest
of what just test runs, the grammar, ambiguity and grammar-generation
suites, passes on all three, but CI runs it on Linux alone: what those suites
check does not depend on the host. A pull request runs the Linux suites
alone, for quick feedback; Windows, Apple Silicon macOS and Intel macOS run,
with the programs built for each, on main, in the merge queue and when CI
is started by hand, so every change is checked on all three before it
merges. Two suites run on Linux alone: the
highlighting consumers, whose Neovim and Typst read the same generated
files on every system, and just test-perf, whose Callgrind counts exist
on Linux only.
64-bit targets only
The compiler builds for 64-bit targets only. Lowering lays out every type
for a target whose pointers are 8 bytes and which aligns i64 and double
to 8 bytes: a handle is 24 bytes, a struct C would pad is packed instead
(lowering.md L5), and a scope’s
record and a spawned call’s header
are 224 and 96 bytes (lib/cgt/nodes.ml, runtime/zane_abi.h). Every
offset and size of the runtime’s records that emitted code relies on is
written once, in runtime/zane_abi.h: runtime/zane_internal.h asserts
each against the structs, and codegen reads them from it as Abi
(lib/codegen/dune). tests/runtime/abi/probe.c prints what the structs
measure; the unit tests hold it to Abi on every host the compiler suite
runs on, and dev/bin/cross-programs holds the Windows and macOS builds it
makes on Linux to the header. On a target with 4-byte pointers, such as
i386-unknown-linux-gnu or armv7-unknown-linux-gnueabihf, those offsets
would disagree with what LLVM and the C compiler lay out.
So once LLVM has made the target machine, Build.target_machine
(lib/codegen/build.ml) reads its data layout and refuses the target
unless the pointer size is 8 and the ABI alignment of i64 and of double
is 8. The refusal is an error before anything is emitted: “the target
i386-unknown-linux-gnu has 4-byte pointers; this compiler builds for
64-bit targets only”. Byte order is not part of the check, since nothing
depends on it: every value is read at the width it was written, and the
object rewriters read either byte order.
Building for macOS needs no Apple SDK
Linking for macOS needs the declarations of its C library, libSystem, and
nothing else from Apple. Zig carries those itself, as the text stubs
libSystem.tbd, which the LLD in the compiler links with
(docs/design/lowering.md §7), so any machine can build a macOS program without
the macOS SDK, whose license allows its use only on Apple hardware.
That holds as long as the compiler, the runtime and the standard library use
only what libSystem provides. They keep to it: a feature that would need an
Apple framework, such as a GUI or the GPU, belongs in a package of its own,
built on a Mac by those who want it.
A macOS program names the oldest macOS it runs on, and the toolchain picks
the oldest one it builds for on the target’s architecture, 13.0 on arm64.
On a Mac with an SDK installed, the link reads the SDK’s stubs, which give
each system library the SDK’s own version. LLD then warns that the library
is newer than the program’s oldest macOS. The program uses the system
libraries of the Mac it runs on, so the compiler drops that warning for a
library under /usr/lib/ or /System/Library/, and keeps it for a
library of the program’s own.
The macOS 27.2 SDK’s stubs list the architecture arm64e.x1, which the
LLVM in the compiler (21) does not know, so its LLD cannot read them. zig cc links instead, and the compiler notes that in one line and keeps
linking with zig cc for that target and those flags
(#419). LLVM 23 knows
the architecture.
The C math library
The approximate math functions of @math$ are the C math library’s where
the program runs (lowering.md L17). Every platform’s C
library holds it: libSystem on macOS and MinGW’s C library on Windows,
which a program links already, and glibc on Linux, where it is a library
of its own and a program is linked with -lm. Their results can differ in
the last digit, which the spec allows of an approximate function
(math.md
§2.6); the math codegen fixture checks them within a tolerance.
Addresses near the program
A program’s frames, and the chunks its regions take, are reserved close to
its image, so that a stored reference or box is a 32-bit distance from the
image’s start (experimental, docs/experiments/goose-comparison). The image’s
start is the symbol each linker defines: __ehdr_start for ELF,
__mh_execute_header for Mach-O and __ImageBase for PE. The runtime
reserves those addresses with mmap and MAP_FIXED_NOREPLACE on Linux, an
mmap hint it checks on macOS, and VirtualAlloc at the address on
Windows; a program that cannot place them stops with an error. It reserves
both as the program starts, before its own code runs: macOS places a mapping
made without an address right above the image, so a large list’s reservation
made first could take the addresses the window of chunks needs.
Machine stack sizes
A program’s main thread uses the stack its launch environment provides on
Linux and macOS, commonly 8 MiB. On Linux RLIMIT_STACK, set through
ulimit -s, can make it smaller or larger. A Windows program is linked
with an 8 MiB main stack, and the pool’s threads are explicitly started
with 8 MiB on every platform. Recursion depth can therefore differ between
the main thread and workers. A full stack stops the program with
“recursion too deep” (lowering.md §9).
On POSIX systems the runtime’s fault handler runs on a stack of its own,
since one that overflowed leaves none, and finds the thread’s stack with
pthread_getattr_np, or pthread_get_stackaddr_np on macOS. A protection
fault is SIGSEGV on Linux and SIGBUS on macOS, so both are handled. A
child that C forks takes the forking thread’s handler stack again, since
macOS gives a forked child none.
A stop from the handler first flushes what the program printed, which waits
in the runtime’s own buffer (lowering.md §9), unless
another thread holds the buffer. It never blocks on a full pipe: on Linux
it writes a pipe through a non-blocking descriptor of its own, opened from
/proc/self/fd/1, and drops what the pipe cannot take, while macOS drops a
pipe’s output and Windows writes only a terminal or a file.
Address range supported by the runtime
Slots, references and dynamic blocks use native pointers. The current
runtime’s chunk map indexes only the low 48 bits of an address: an allocation
at or above 2^48 stops with “memory beyond a 48-bit address”. This is an
implementation limit, separate from the size the operating system permits
a process to reserve. It applies to dynamic chunks and the fixed-size range,
including its guard. Supporting higher addresses requires extending the
chunk map; native pointers themselves impose no 32 GiB ceiling.
The same bound limits the size of a type: lowering refuses a type that takes
2^48 bytes or more where it is named, since no value of it could be
placed (lowering.md §9). The limit is Ty.limit in
lib/cgt/nodes.ml, and it rises with the chunk map if the map is extended.
What a Windows build uses
A Windows program links with MinGW’s C library and its POSIX threads,
winpthreads, both of which zig cc carries; ZANE_CC=dev/bin/zig-cc links
with it (lowering.md §7). It also compiles the runtime’s
bitcode, whose bodies an optimized build inlines, so the zig is the release
whose clang has the compiler’s own LLVM version (pixi.toml): bitcode from a
newer LLVM is bitcode the compiler cannot read. The runtime differs there in five
places, each marked _WIN32: a dynamic chunk is carved from a 64 MiB
reservation, decommitted with VirtualFree(MEM_DECOMMIT) when it turns
cold, and released with its whole reservation
(lowering.md §9), as are the chunks of a box’s payload
past 1 MiB, a context’s range of frames and a list’s
growing oversized span are reserved with
VirtualAlloc(MEM_RESERVE) and made usable with MEM_COMMIT, a span a
list has moved out of is decommitted with MEM_DECOMMIT, the range
from a vectored exception handler, which also reports a full machine stack
(EXCEPTION_STACK_OVERFLOW), winpthreads counts the processors, stdout
and stderr are set to binary mode, so a program writes \n and not \r\n,
as it does everywhere else, and the program’s arguments are read from
GetCommandLineW through CommandLineToArgvW and converted from UTF-16 to
UTF-8 (effects.md §6.6), which links the program with shell32. The program
is linked with --stack for an 8 MiB main thread, where the linker would
give it 1 MiB. Scalar float
formatting and parseF64 also use explicit C numeric-locale CRT calls (a
parseF64 of at most 19 digits that rounds exactly with one division is read
by the runtime itself, with no CRT call); POSIX temporarily selects that
locale on the calling thread. Both leave the host’s
locale intact.
A target is spelled as zig cc reads it, such as x86_64-windows-gnu. The
compiler hands LLVM that triple’s normal form, x86_64-unknown-windows-gnu,
and the C compiler the triple as written. A build on a Windows host without
--target is for x86_64-windows-gnu, since the host’s own triple is
MSVC’s, whose C library has no POSIX threads. On a Windows host the
compiler reads \ in a path as it reads /, so a package directory’s name
is its last component either way, and its reports write every path with /,
so they read as they do on any other system. It ends each line it writes with
\n, as the programs it builds do.
Tiered builds
A tiered build (tiers.md) loads tier 1 into the running
program as a shared library, through each platform’s own loader:
| Platform | Tier 1 | Loaded with | Notes |
|---|---|---|---|
| Linux | .so |
dlopen |
programs link -ldl as needed: only glibc before 2.34 keeps dlopen there |
| macOS | .dylib |
dlopen |
libSystem holds the loader; LLD signs arm64 code ad hoc, as macOS requires |
| Windows | .dll |
LoadLibraryW |
the path is converted from UTF-8 to UTF-16, as the arguments are; zig cc names no entry for a MinGW DLL, so the link names MinGW’s, DllMainCRTStartup |
Tier 1 links against the C library alone and takes everything else from tier
0 through its import table (T5), so no platform needs the program to export
a symbol, nor an import library on Windows. Every codegen fixture runs tiered
on Linux and, as Windows programs do, on Windows; a tiered hello, linked by
each platform’s released toolchain, runs on that platform.