- spec 05 §9-§12: flat effects, checking rule, capability model, instantiation by parameter name, lifetime, fallible narrowing, intrinsic calls, runtime - spec 07 §8.9, 07a (cap.narrow, intrinsic, run result), 08 stage order, 11 not_instantiated outcome - new reference/effects.md and reference/capabilities.md (kinds, scopes, built-in cap_error) - features effects and capabilities published - Q-011, Q-012 resolved
11 KiB
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:
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:
br @loop(%next)
loop(%i: u64):
...
3. Core instruction classes
Values and arithmetic
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
br
cbr
switch
ret
trap
guard
assert
Borrowing and memory
borrow.ref
borrow.mut
borrow.end
load
store
mem.copy
mem.zero
slice.make
slice.sub
Ownership
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
call
call.indirect
intrinsic.call
Concurrency
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:
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. Instruction semantics are in 07a-air-instructions.md.
8.1 Module header
Every text module MUST begin with:
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).
The features line is the semantic compatibility contract of the module: it lists the feature bundles (../reference/air-features.md) an implementation MUST understand to verify and execute the module. features [] is the Phase 0 baseline.
- A module MUST declare every feature whose constructs it uses, together with the full dependency closure of those features; a feature never implies another.
- The same name MUST NOT appear twice.
- An implementation MUST validate the feature list before examining the module body, and MUST NOT examine the body if any feature is unknown, unsupported, duplicated or missing a declared dependency.
- A construct belonging to a feature that the module does not declare is rejected.
- All of these are reported as
E0006with areasonfield (unknown_feature,unsupported_feature,duplicate_feature,missing_dependency,feature_not_declared); see the registry for the fields of each reason.
Features describe what the implementation must understand. They grant no authority and declare no effects: effects (spec 05 §2) say what a function does and capabilities (spec 05 §3) say what an invocation may do.
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
%nameMUST 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,voidandneverare built in.usize/isizeare distinct from all fixed-width types; their width comes from the target description (spec 09 §9). type @T = struct { … }andtype @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<T>in Phase 1) may be move-only or linear; that does not change the types defined here. voidis only valid as a function return type. Avoidfunction returns withretwithout operand; a call to it MUST NOT bind a result. Aneverfunction MUST NOT containret.
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, reason reserved_type.
8.5 Canonical form
The canonical serialization of a module is: the four header lines, with feature names sorted lexicographically; 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:
- every module accepted before the change is still accepted and produces the same run record (spec 07a "Run result") or the same trap record;
- every module rejected before the change is still rejected with the same codes, except where the rejection was
E0004(unknown opcode or type name) orE0006(reserved type or unsupported feature) and the change gives that name a meaning; - the change adds instructions, types, diagnostic codes or trap codes; every added construct belongs to a feature registered in
../reference/air-features.md, and the feature's semantics are specified (spec 07a and the relevant spec files) before the feature is published and implemented; - the canonical form (§8.5) of existing constructs is unchanged.
A published feature's semantics are immutable. Extending a feature with new semantics is not permitted; register a new feature instead.
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.
8.9 Types of the features effects and capabilities
cap<K> (feature capabilities) is an opaque, copyable, authority-bearing value type; K is a registered capability kind (../reference/capabilities.md). Built-in enums such as cap_error are spelled without @, have registered variants and are used with enum.switch <builtin> %v, … like declared enums. Rules: spec 05 §9–§12.
8.8 Types and syntax of the features ownership and arrays
Feature membership: local<T>, ref<T>, mut<T> and the from clause belong to ownership; array<T, N>, slice<T>, slice_mut<T> and multi-result binding belong to arrays. Using them without declaring the feature is E0006 reason feature_not_declared.
| Type | Class | Notes |
|---|---|---|
local<T> |
storage | frame storage holding a T; only an operand of local.* and borrow.* |
ref<T> |
borrow | shared borrow |
mut<T> |
borrow | exclusive borrow |
array<T, N> |
value | array of N values, 1 ≤ N ≤ 2^32; copyable when T is |
slice<T> |
borrow | shared borrow of a contiguous range |
slice_mut<T> |
borrow | exclusive borrow of a contiguous range |
T in these types is a value type: not void, never, a storage type or a borrow type.
Second-class rule. Storage and borrow types MUST NOT appear as struct fields, enum payloads, array elements or the T of another storage or borrow type (E1001). A storage type MUST NOT appear as a function parameter, return type or block parameter (E1001). A borrow type MAY appear as a function parameter and, with from, as a return type (spec 08 §11.3). As a block parameter type it requires the reserved feature borrow_joins (spec 08 §11.6).
All value types of format 0 and Phase 1 are copyable. Move-only values arrive with heap in Phase 2.
Syntax additions (reference/air-text.ebnf): -> type from %param on function signatures, the type forms above, and %a, %b = op for instructions specified with two results (only slice.split).