Files
aji/docs/spec/07-air.md
T
dev 41b764ccfd spec: Phase 0 spec gate (P0-1, P0-2, P0-3)
- text AIR format 0: grammar (reference/air-text.ebnf) and module,
  block, SSA, type and canonical-form rules (spec 07 §8)
- instruction contracts for format 0 (new spec 07a)
- traps: stable T0xxx codes and trap records (spec 01 §6)
- integer edge cases and explicit casts (spec 02 §2.1)
- fixed verifier resource limits (spec 08 §7)
- target description with pointer_bits (spec 09 §9)
- new codes E0001-E0006, E1003, T0001-T0007
- resolves Q-001, Q-003..Q-008, Q-018; Q-002 resolved for format 0
2026-10-04 12:14:25 +03:00

231 lines
5.8 KiB
Markdown

# 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 <path>
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`).
- In format version 0 every value is copyable; ownership categories arrive with format version 1 (Phase 1).
- `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.