# 21. Targets: Apple first, then the browser

A target is a place where a compiled program runs: a CPU, an operating
system, and the conventions the two agree on for laying out data and
calling functions. In this chapter we'll look at where Coax programs run
today, what the generated C quietly assumes about its target, and what it
would take to run the same code somewhere new. We'll watch a layout
assertion from Chapter 20 refuse to compile on a 32-bit target, and try
(and fail, instructively) to compile Coax's output for WebAssembly.

The point of the chapter is a habit more than a feature. "It compiles for
that CPU" and "it works on that platform" are different claims, and this
chapter is about the work between them.

> Note: `coax0` runs on macOS (Apple silicon) and Linux x86-64, and cross-builds
> Windows x86-64 desktop executables (`apps/openagents-desktop/windows/`,
> `coax/platform/*-win32.c`). The iOS app compiles Coax's C output with the
> iOS SDK and ships it. **Coax Web** (`coax/web.md`, formerly *Coax Native Web*
> / *Coaxial*) ships Wasm/WebGPU, DOM rendering (`components/backend/dom.coax`),
> **Coax UI Core** (`components/ui/runtime.coax`, `policy.coax`), **Coax UI Kit**
> (`components/ui/kit/{parts,stream_markdown,virtual_list}.coax`), **Coax UI
> Router** (`components/ui/router.coax`), **Native SSR/Hydration**
> (`components/backend/html.coax`, `coax/web/dom-hydrate.js`, zero-browser
> `components/ui/test_harness.coax`), and 0-KB-Wasm SSR books
> (`apps/openagents-web/demos/docs-starter/`). General target-aware layout
> queries and full browser source debugging remain separate gates (spec §17.2
> and §20, stage 7).

## What "supported" means

Emitting code for a CPU doesn't make it a supported target. Coax counts a
target as supported when there is passing evidence for each of these:

- the runtime and program startup on that platform;
- the linker and the C ABI, in both directions: the platform's C calling
  Coax, and Coax calling the platform's C;
- debugging: symbols, stack traces and source locations that lead back to
  the `.coax` file;
- a real program, end to end, on the real platform rather than an
  emulation of it.

Every row of a target matrix needs all four. The bar is high on purpose.
The Jai research behind Coax found that Jai's strongest platform evidence
came from particular shipped projects rather than an official support
list, and that's the kind of evidence we want too: a program people use,
running where they use it.

## Where Coax runs today

The compiler is a C++ program that the repository builds on the Mac with
`coax/build.sh`. Everything it produces in this book, from `coax run` to
`coax lib`, is compiled by that Mac's C compiler for that Mac. The book's
examples, the compiler's `tests/pass` and `tests/fail` suites and the
mutation gates all run there. So the first row of the matrix is the build
host: macOS on arm64.

The second row is the iPhone, and it arrived by a side door. As we saw in
Chapter 20, the iOS app doesn't ask `coax0` for an iPhone binary. It asks
for C source (`coax c --library --header`) and lets Xcode compile that C
with the iOS SDK, alongside the Swift. You can do the same thing by hand
with Xcode's tools. Here's the camera's generated C, compiled for an
iPhone and for the simulator:

```shell
$ xcrun --sdk iphoneos clang -target arm64-apple-ios18.0 -std=c11 -c Generated/Camera.c -o cam-ios.o
$ file cam-ios.o
cam-ios.o: Mach-O 64-bit object arm64
$ xcrun --sdk iphonesimulator clang -target arm64-apple-ios18.0-simulator -std=c11 -c Generated/Camera.c -o cam-sim.o
$ file cam-sim.o
cam-sim.o: Mach-O 64-bit object arm64
```

Both objects are arm64; what differs is the platform recorded inside them,
which is what stops the simulator's object from linking into a device
build.

Measured against the four requirements, the iPhone row is partly filled
in. The app starts and runs on devices through TestFlight, which covers
startup and the end-to-end program. Swift calls Coax and Coax calls the
host's C in `metaverse/platform/host.c`, and the UI tests launch the real
app in the simulator and tap the rendered HUD through the native touch
path, which exercises both directions. Version 0.13.0 adds `#line` mappings
and hashed name/type maps. The actual Simulator app has been attached in LLDB
at `metaverse_cast_pose` in `cast.coax`, with Coax variable names and a stack
through Swift. Chapter 12 explains the commands and limits. This is useful
source-debugging evidence, but does not establish a complete physical-device
symbolication workflow, every ABI case or a general supported-target matrix.

## Apple native, first

The native app is the first target because it's where the speed is. The
path from a touch to a pixel is short:

```jai
UIKit (Swift) → Bridge.h → Coax libraries + host.c → Metal (Swift) → GPU
```

`HomeViewController.swift` receives the touch and calls Coax.
Coax decides what moves and what the frame contains, writing draw packets
through `host.c`. `HomeRenderer.swift` uploads those to Metal, and the GPU
runs shader stages whose scalar math was itself generated by
`coax metal` from `metaverse/light.coax`. There's no JavaScript, WebGL or
ANGLE anywhere in that path.

Development iteration stays on the Mac too. The plan for hot reload
(Chapter 19) reloads code on the Mac host, never on a device: an iPhone
runs only what was compiled, signed and installed.

## What the generated C assumes

Since the C output is what actually travels to each target, let's read
what it asks of a target. These are the first lines of any file `coax c`
produces (here for `path.coax` from Chapter 20):

```c
/* Generated by coax0 from path.coax. Do not edit: change the .coax file. */
static const char *coax_source_path = "path.coax";
/* Coax float arithmetic is plain IEEE, one rounding per operation: no fused
   multiply-add, so results match compile-time evaluation and other compilers. */
#pragma STDC FP_CONTRACT OFF
/* Generated code carries the whole runtime and keeps every declared local. */
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct { const char *data; int64_t count; } coax_string;
#define COAX_STR(lit, n) ((coax_string){(lit), (n)})

static void coax_trap(const char *what, int32_t line) {
  fflush(stdout);
  fprintf(stderr, "%s:%d: %s\n", coax_source_path, line, what);
  abort();
}
```

*Listing 21-1: The start of every generated C file*

Each piece is a question a new target has to answer.

The includes ask for a hosted C library: `stdio.h` for `print`,
`stdlib.h` for `abort`, `math.h` for `sqrt` and friends. Apple's
platforms, Linux and Android all have one. A browser doesn't.

`coax_trap` is how every failed check ends: a message on standard error
and `abort()`. On iOS that's a crash report. Somewhere without standard
error or `abort`, a trap needs another way to stop and say why.

`FP_CONTRACT OFF` is what makes float results the same everywhere
(Chapter 16). A C compiler that ignores the pragma could fuse operations
and give different answers, so a new compiler is checked for it, not
assumed. GCC is such a compiler: it ignores the pragma (and says so
under `-Wall`), so a GCC build of Coax output passes `-ffp-contract=off`
to get the same rounding, as the book's own Linux runs do.

Further down, checked integer arithmetic is written with
`__builtin_add_overflow` and its relatives, which GCC and Clang provide.
A target whose C compiler lacks them needs those helpers spelled another
way before it can build Coax's output at all.

## Layout is a property of the target

The layout assertions in a generated header record sizes and offsets as
`coax0` computes them, and `coax0` computes them for a 64-bit target where
`f64` is aligned to 8 bytes. On every Apple platform the app ships to,
that's right. It isn't right everywhere.

Let's test it. This one-line C file includes the camera's real header from
the iOS app:

Filename: cam.c

```c
#include "Camera.h"
```

The header's `Dolly` is a `Shot` (six `f32`, 24 bytes), then an `f64`,
then an `f32`:

```c
struct coax_t_Dolly { coax_t_Shot from; double start; float length; };
COAX_LAYOUT(sizeof(coax_t_Dolly) == 40 && COAX_ALIGNOF(coax_t_Dolly) == 8, "layout of Dolly");
```

Clang can check a file for targets it can't link for. We already compiled
the camera for the iPhone above. Here's the header against an Intel Mac,
32-bit ARM Linux and WebAssembly, and then 32-bit x86 Linux:

```shell
$ for t in x86_64-apple-macos11 armv7-unknown-linux-gnueabihf wasm32-unknown-unknown; do
>   clang -target $t -ffreestanding -std=c11 -fsyntax-only -I Generated cam.c
> done
$ clang -target i386-unknown-linux-gnu -ffreestanding -std=c11 -fsyntax-only -I Generated cam.c
In file included from cam.c:1:
Generated/Camera.h:38:13: error: static assertion failed due to requirement 'sizeof(struct coax_t_Dolly) == 40': layout of Dolly
   38 | COAX_LAYOUT(sizeof(coax_t_Dolly) == 40 && COAX_ALIGNOF(coax_t_Dolly) == 8, "layout of Dolly");
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~
Generated/Camera.h:16:42: note: expanded from macro 'COAX_LAYOUT'
   16 | #define COAX_LAYOUT(c, m) _Static_assert(c, m)
      |                                          ^
Generated/Camera.h:38:34: note: expression evaluates to '36 == 40'
   38 | COAX_LAYOUT(sizeof(coax_t_Dolly) == 40 && COAX_ALIGNOF(coax_t_Dolly) == 8, "layout of Dolly");
      |             ~~~~~~~~~~~~~~~~~~~~~^~~~~
Generated/Camera.h:16:42: note: expanded from macro 'COAX_LAYOUT'
   16 | #define COAX_LAYOUT(c, m) _Static_assert(c, m)
      |                                          ^
1 error generated.
```

The first three print nothing, which means they agree. (`-ffreestanding`
lets Clang check the header without a C library for each target, which is
all a header needs.)

On 32-bit x86 Linux, Clang tells us the size it got: 36. The C ABI there
aligns a `double` inside a struct to 4 bytes, not 8, so C lays `Dolly` out
with no padding after the final `float`. The field offsets happen to
match, but Coax's side assumes 40 bytes. In an array of `Dolly` values,
every element after the first would start in a different place for C than
for Coax, and the two could disagree about how a `Dolly` is passed by
value. Without the assertion, a host on that target would compile and link
and then misread its data. With it, the build stops at the first line that
disagrees.

That's the assertions doing their job, and it also shows their limit. They
can tell you that the host's layout differs from Coax's. They can't make
Coax compute every target's layout. The compiler now reports an explicit
`native-lp64` target description (spec §10.2), while other target queries
refuse with E0612. Schema-1 `size_of` still describes the LP64 CPU layout.
A clean header check is evidence that the particular C compiler agrees
with those declarations, not general target qualification.

## The browser, proved separately

The web build of the engine runs through WebAssembly, and that's the next
target Coax is meant to prove. This bare-target experiment still illustrates
why a host/runtime matters:

```shell
$ clang --target=wasm32 -std=c11 -c path.c -o path.wasm.o
path.c:13:10: fatal error: 'stdio.h' file not found
   13 | #include <stdio.h>
      |          ^~~~~~~~~
1 error generated.
```

Not far, and that's the expected answer. Listing 21-1 asks for a hosted C
library, and a bare WebAssembly target has none. You could point Clang at
a WASI sysroot and get an object, but a browser isn't a WASI host either:
there's no standard error for `coax_trap` to write to, and `print` has to
become a call into JavaScript. A browser target needs its own small
runtime, with traps, printing and memory provided as explicit host
imports. Coax Web's `coax/web/wasm.sh` supplies upstream LLVM, a WASI
sysroot and application-specific C faces; `host.js` and `dom-host.js` /
`dom-hydrate.js` supply memory, WebGPU, DOM, and native SSR hydration for the
site, Coax UI demos, and Metaverse consumers. That application path
does not make this bare invocation work or establish general target-aware
layouts and browser source debugging. Its current scope and consumer
evidence are recorded in `coax/web.md`.

And getting a `.wasm` file to link is the beginning, not the proof. A
browser target has to show each of these working in a real browser:

| Boundary | What to prove |
| --- | --- |
| Memory layout | Pointers are 4 bytes on wasm32, so any struct holding one changes; every struct shared with JavaScript is checked |
| Memory growth | JavaScript typed-array views go stale when Wasm memory grows; code that holds them refreshes them |
| Scheduling | Async loading, who owns the main loop, communication with workers |
| Threads | Which browser features and headers (COOP/COEP) the design needs |
| Graphics | WebGPU or WebGL resource lifetimes, layouts and device loss |
| Debugging | Source maps, stack traces, release symbols |
| Iteration | Incremental assets, state kept across reloads, deployment timing |

The `Camera.h` check above passed on `wasm32` because none of its structs
holds a pointer. A view would: `coax_v_t_Point` has a 4-byte `data` on
wasm32 instead of 8, and JavaScript reading one from linear memory has to
know where `count` starts.

The model to study is Polygon, a procedural engine rewritten in Jai. Its
author first moved geometry generation into Jai compiled to WebAssembly,
sharing pointers and sizes with JavaScript typed arrays, and only later
built a renderer, on wgpu natively and WebGPU in the browser. The order is
the lesson. Move expensive CPU work behind a small boundary first, prove
that boundary in a real browser, and decide about the renderer afterward.
For Coax, that means a browser vertical slice with real geometry, input
and rendering (spec §20, stage 7), not a Wasm link and a promise.

## Later targets

Android, Linux and Windows follow the same rule, each with its own
runtime, linker, ABI and debugging evidence. Linux x86-64 runs both `coax0`
and the Vulkan desktop host (`apps/openagents-desktop/linux/`), Android links
compiled Coax via `apps/shared/host/android/`, and Windows ships the Win32 +
Vulkan desktop carrier (`apps/openagents-desktop/windows/`, `#14046`) over
Win32 platform shims (`coax/platform/{process,socket,store-file,tls}-win32.c`).
Full cross-target layout queries (`native-lp64` remains the sole named layout
profile) and native Windows/Android CI release qualification remain separate
gates.

Until each row has its evidence, the accurate description of Coax's
output is narrow: C11 that builds and runs on the tested Mac, and that the
iOS app compiles and ships through Xcode.

## Summary

A target is supported when its runtime, ABI in both directions, debugging
and a real end-to-end program all have evidence, not when a compiler can
emit code for its CPU. Today the build Mac is the tested host, and the
iPhone runs Coax through Xcode compiling the generated C. Original-source
line mapping and LLDB names are verified on Mac and iOS Simulator; physical
device debugging and broader ABI cases still need their own evidence. The generated C asks for a hosted C
library, `abort`, the overflow builtins and an honored `FP_CONTRACT`
pragma, and its layout assertions encode a 64-bit Apple layout, which is
exactly why they fire on 32-bit x86. Coax Web's Wasm/WebGPU, DOM, and native SSR runtime is
a production browser and web-publishing consumer; full cross-target engine layout and debugging
proof beyond the qualified Apple hosts remains a separate vertical slice.

So far the reader of Coax code has been a person. In practice most Coax in
this repository is written by agents, and the next chapter looks at the
tools that make that work: machine-readable diagnostics, `coax explain`,
and checks that an agent can't talk its way past.
