# 07. AIR — Aji Intermediate Representation ## 1. Role AIR is the canonical semantic representation of Aji. Source syntax is optional tooling. AIR is mandatory for conformance. AIR is: - typed; - SSA-oriented; - basic-block based; - explicit about ownership and borrows; - explicit about effects and capabilities; - explicit about synchronization; - deterministic to parse and verify. ## 2. Functions and blocks Illustrative AIR: ```text fn @sum(%a: u64, %b: u64) -> u64 effects [] entry: %0 = add.checked.u64 %a, %b ret %0 ``` Blocks receive values through block parameters rather than implicit phi nodes: ```text br @loop(%next) loop(%i: u64): ... ``` ## 3. Core instruction classes ### Values and arithmetic ```text const copy move add.checked sub.checked mul.checked div.checked add.wrap sub.wrap mul.wrap and or xor shl shr eq ne lt le gt ge ``` ### Control flow ```text br cbr switch ret trap guard assert ``` ### Borrowing and memory ```text borrow.ref borrow.mut borrow.end load store mem.copy mem.zero slice.make slice.sub ``` ### Ownership ```text own.alloc own.free region.create region.alloc region.alloc_uninit region.reset region.destroy island.create island.alloc island.freeze arc.clone arc.release ``` ### Calls ```text call call.indirect intrinsic.call ``` ### Concurrency ```text task.scope.begin task.scope.end task.spawn task.join task.cancel thread.spawn thread.join channel.send channel.recv mutex.lock mutex.unlock rwlock.read rwlock.write atomic.load atomic.store atomic.rmw atomic.cmpxchg atomic.fence ``` ## 4. Instruction contract Every AIR instruction definition must specify: - operand types; - result types; - ownership-state transition; - borrow-state transition; - memory reads/writes; - synchronization effects; - effect requirements; - capability requirements; - trap behavior; - verifier transfer function. ## 5. No hidden semantics AIR does not contain: - hidden destructors; - implicit allocation; - implicit exception edges; - default memory orderings; - implicit task detachment. ## 6. Serialization Canonical AIR should have both text and binary forms. Binary AIR conceptually contains: ```text HEADER TYPE_TABLE CONSTANT_TABLE CAPABILITY_TABLE FUNCTION_TABLE BLOCK_TABLE INSTRUCTION_STREAM CONTRACT_TABLE DEBUG_TABLE ``` Every binary must encode version and required feature bits. ## 7. Verification before execution Native code must not be produced from unverified safe AIR. Unsafe AIR modules are allowed only through explicit trusted/unsafe boundaries and must appear in the unsafe dependency report. ## 8. Text AIR, format version 0 The grammar is [`../reference/air-text.ebnf`](../reference/air-text.ebnf). Instruction semantics are in [`07a-air-instructions.md`](07a-air-instructions.md). ### 8.1 Module header Every text module MUST begin with: ```text module air 0 lang 0.1 features [] ``` An implementation MUST reject a module whose `air` format version or `lang` version it does not implement (`E0005`), and a module that lists a feature it does not implement (`E0006`). Format version 0 defines no features. ### 8.2 Functions and blocks - A function body is a non-empty sequence of blocks. The first block is the entry block; it MUST NOT declare parameters. Function parameters are in scope in the entry block and every block it dominates. - Every block MUST contain at least one instruction, MUST end with exactly one terminator, and MUST NOT contain a terminator before its last instruction (`E5002`). - A branch target MUST name a block of the same function and pass exactly as many arguments as the block declares parameters (`E5002`). Argument binding is simultaneous: all arguments are read before any parameter is written. - Every `%name` MUST be defined exactly once per function, as a function parameter, a block parameter or an instruction result (`E5001`). Every use MUST be dominated by its definition (`E5001`); a use in the same block MUST come after the defining instruction. Uses in blocks unreachable from the entry block are only checked within their own block. - Function and type names MUST be unique within a module, and block labels within a function (`E0003`). Field names MUST be unique within a struct and variant names within an enum (`E0003`). ### 8.3 Types - Integer types, `bool`, `void` and `never` are built in. `usize`/`isize` are distinct from all fixed-width types; their width comes from the target description (spec 09 §9). - `type @T = struct { … }` and `type @T = enum { … }` declare nominal aggregate types. An aggregate MUST NOT contain itself by value, directly or through other aggregates (`E1003`). - Every type defined in this section is copyable. Types added later under §8.7 (for example `Own` in Phase 1) may be move-only or linear; that does not change the types defined here. - `void` is only valid as a function return type. A `void` function returns with `ret` without operand; a call to it MUST NOT bind a result. A `never` function MUST NOT contain `ret`. ### 8.4 Reserved `f32` and `f64` are reserved until floating-point determinism is specified (open question Q-016). A module that uses them MUST be rejected with `E0006`. ### 8.5 Canonical form The canonical serialization of a module is: the four header lines; one blank line; type declarations in declaration order, one per line; for each function, a blank line, the signature line, the `effects` line, then each block label at column 0 followed by its instructions indented by four spaces. Separators are `", "` between list items and single spaces elsewhere. Comments are not preserved. Value, block, function and type names are preserved. Serializing a parsed module and parsing the result MUST yield an equal module. ### 8.6 Diagnostic order Verification stops after the first stage that reports an error, in this order: parsing and name resolution, resource limits, CFG structure, SSA, types. Within a stage, diagnostics are ordered by source position. The result is therefore deterministic for a given input. ### 8.7 Stability of format 0 Format 0 is the frozen semantic baseline of AIR (RFC 0004, Accepted). Its existing constructs keep their meaning for as long as `air 0` exists. A change is permitted under `air 0` only if all of the following hold: 1. every module accepted before the change is still accepted and produces the same run record (spec 07a "Run result") or the same trap record; 2. every module rejected before the change is still rejected with the same codes, **except** where the rejection was `E0004` (unknown opcode or type name) or `E0006` (reserved type or unsupported feature) and the change gives that name a meaning; 3. the change adds instructions, types, header features, diagnostic codes or trap codes, and specifies each new instruction in spec 07a before it is implemented; 4. the canonical form (§8.5) of existing constructs is unchanged. Any other change to format 0 — a different meaning, typing rule, trap behavior, diagnostic code or canonical spelling for an existing construct — requires a new format version (`air 1`) introduced by an RFC. An implementation that supports several format versions selects the semantics by the module's `air` line; it never mixes them.