Specifications / Compiler

iOS code generation

The corresponding iOSTarget contract is here.

Platform selection [IOS-TARGET-TRIPLE]

CLI target LLVM target triple Xcode SDK Minimum OS
--target=ios arm64-apple-ios15.0 iphoneos iOS 15
--target=ios-sim arm64-apple-ios15.0-simulator iphonesimulator iOS 15
--target=macos arm64-apple-macos12.0 macosx macOS 12

Device, simulator and Mac archives are separate products even though all three use ARM64. The host must link the archive for its platform. Intel Macs and simulators, universal archives, and XCFramework packaging are not implemented.

The Mac slice [IOS-TARGET-MACOS]

--target=macos is the iPhone target built for a Mac: the same options, header, thunks, entry, capability rules and runtime sources (libosprey_runtime_macos_arc.a compiles the iOS runtime sources with the iOS flags against the macOS SDK). The Mac runtime also carries the Metal device adapter ([GPU-DEVICE]), so a desktop host such as GPUI runs kernels on the GPU, and a host linking the Mac archive links the Metal and Foundation frameworks. For one program, the Mac slice emits the same adapted LLVM IR as --target=ios and a header that differs only in the slice it names, so Swift @_cdecl imports and calls written against one compile unchanged against the other. A Swift macOS host, command-line or app, links the archive exactly as an iPhone app does and owns every platform API. --run is rejected for this slice too; the host runs the program.

Compiler options [IOS-TARGET-OPTIONS]

osprey app.osp --compile --target=ios -o libApp.a --memory=arc
osprey app.osp --compile --target=ios-sim -o libApp.a --memory=arc

Compilation emits the requested .a and a sibling .h. Other output extensions are rejected. --run is rejected because the result needs an iOS host. ARC is the default memory backend; --memory=arc selects it explicitly. Tracing GC is unsupported for these hosted archives and is rejected before toolchain work. Unmanaged allocation remains an explicit --memory=none opt-out. Native debugger and profiler flags are rejected for these targets. These option restrictions also apply to --check and --llvm. --llvm emits the host-compatible LLVM representation after the same target legality checks; it cannot bypass those checks.

The compiler resolves the selected SDK with xcrun, compiles LLVM IR with Apple clang and the target triple above, and merges the application object and matching Osprey runtime archive using xcrun libtool -static. The generated archive contains the Osprey runtime; the Swift host links this single library. SDK/toolchain failures are errors with a setup hint. OSPREY_XCRUN overrides the xcrun executable.

C function boundary [IOS-HOST-ABI]

The header is derived from inferred types. Every function the entry source declares with a fully resolved supported scalar signature is exposed as osprey_<name>. A single-file program is its own entry source. Functions in a project's other source files are internal, whatever their signatures. If the compiler cannot emit an exposed function, the build fails and names it, with the reason when the effect check has one. The function is never silently missing from the archive and header. Qualified project names use underscores between namespace segments. The application entry is reserved as osprey_main. Invalid or colliding generated C names are compile errors.

--entry-only builds a mobile archive whose host can call only osprey_main. Internal functions remain compiled and checked at their call sites, but they are not C exports. The mobile golden harness uses this mode because its host calls only the entry point; ordinary application archives continue to validate every exported function's effect requirements.

Osprey type C type Swift imported type
int int64_t Int64
float double Double
bool bool Bool
string const char * UnsafePointer<CChar>?
Ptr void * UnsafeMutableRawPointer?
Unit, return only void Void

A Ptr is an opaque host handle (FFI-PTR): Osprey stores it and passes it back unchanged and never reads through it, so a host can hand Osprey a window or a session and receive it in later imports. The host owns the pointee and its lifetime.

Boolean boundary thunks use Apple's ARM64 zero-extension convention. Unit thunks translate the internal unit representation to C void. Integers retain their 64-bit Osprey width. See Apple's ARM64 calling convention and LLVM's parameter attributes.

Records, unions including Result, collections, closures, unconstrained generic functions, and effects are not C values. Such internal helpers are not exported. The compiler rejects unsupported extern fn signatures and scalar exports that require an effect handler supplied by their caller; use a scalar wrapper that handles effects inside Osprey. Compile-time omission of an internal helper from the header does not declare that helper callable by C.

Strings are NUL-terminated UTF-8. Host inputs are borrowed for the synchronous call and must remain valid until it returns. The export adapter copies each string input into Osprey-owned storage before calling the function and releases its temporary reference afterwards. The function may retain that copy in persistent state or return it; the host may reuse or release its original input buffer after the call. Each returned string carries an Osprey reference that remains valid until the host calls osprey_release_string(pointer) exactly once. Copy the text first when the host needs its own independent storage; never pass the Osprey pointer to libc free. Embedded NUL bytes are outside this string boundary contract. ARC reclaims internal values when their last owner is released. Application globals remain owned by the library. --entry-only has no callable exports and releases its program globals on return; its corpus must finish with zero live ARC allocations.

Entry and lifetime [IOS-TARGET-ENTRY]

The library exposes int32_t osprey_main(void) and no executable main symbol. The Swift host calls osprey_main() on its main thread before any exported application function. Successful initialization returns zero. Repeated calls return the saved result without running top-level work again. Top-level Osprey values remain valid after initialization so exported functions may use them.

The host serializes entry and exported function calls on one thread. Reentrant initialization returns status one without rerunning top-level work. Concurrent initialization, independently initialized library instances, unloading, and teardown are not part of this contract. Host callbacks must return synchronously and avoid reentering initialization. Swift owns asynchronous iOS work and marshals subsequent calls back to the chosen host thread.

Calling the host [IOS-HOST-IMPORTS]

Osprey declares platform services using extern fn over the scalar types above. The generated header lists those imports with their declared C names, and the final Swift application supplies matching C symbols. Missing host definitions fail the final application link.

The generated header declares int32_t osprey_host_abi_version(void). Version 1 copies every string returned by a host import into the selected Osprey managed memory backend before Osprey continues execution. Osprey may retain that copy in globals, records or collections without retaining the host buffer. The host owns the original buffer and must keep it valid through the callback return and the adapter's immediate copy; it may reuse the buffer on the next callback and release it after the enclosing Osprey call returns. Returning a pointer into an expired stack or Swift withCString scope is invalid.

A host must verify the ownership version before enabling temporary import buffers. An older archive without this contract requires the previous process-lifetime buffer policy. These are deliberate host imports, distinct from an unsupported language or runtime capability, which must fail in the Osprey compiler.

The macOS C ABI oracle is OSPREY_BIN="$PWD/target/release/osprey" bash scripts/mobile/test-macos.sh, using a freshly built compiler and managed runtime archives. It checks retained Unicode, empty and changing 8 KiB import values, nested records and lists, and 20,000 calls whose live ARC object and byte counts remain flat. Export-input checks mutate the caller buffer after the call, assert that retained and returned copies remain live during allocation churn, and require the allocation baseline to recover after release. Its final line is ==> macOS scalar imports/exports and global lifetime passed. Persistent application globals remain live; this is not a zero-allocation exit check or device-platform acceptance.

Target capability checking [IOS-TARGET-CAPABILITIES]

The compiler checks the complete program before generating LLVM IR or invoking a native tool. An unsupported construct in a helper is rejected even when a top-level execution path does not call that helper. The diagnostic names the target, offending construct or operation, and the unsupported capability. --check, --compile, and --llvm enforce the same rules.

Dynamic control operations, including arms that never resume, are rejected for this C ABI target. A suspended Osprey continuation cannot cross or outlive a synchronous host call. Callable value handlers, block-scoped value handlers, and effects eliminated at compile time remain available when their bodies use supported operations. Exported functions must discharge their own effect requirements.

Process launching and the built-in HTTP/WebSocket operations have no iOS implementation and are rejected at compile time. So is an unqualified // @link: or // @linkdir: directive: the archive carries no library, and the host links every import (FFI-LINK-DIRECTIVES). The Swift host supplies platform networking and services through explicit imports. The runtime includes allocation, strings, collections, JSON, file operations, fibers/channels, ordinary effect handling, and the GPU surface on its host oracle; it carries no Metal adapter, so a device selection reports that no GPU adapter is available (GPU-BACKEND-DEVICE). This does not authorize an effect continuation to cross the C boundary. GPU buffers and pure kernels use the existing CPU execution path; Metal execution is not implemented.

The compiler must never present an unsupported target feature as a successful static-library build and defer its discovery to the Swift linker. Missing user-supplied host symbols remain ordinary application link errors. Expanding target support requires implementation and passing target tests before removing a rejection.