Add the error_values feature and error.const through specification, AIR, verification, interpretation and source lowering. Error values are Copy data with no allocation, effects or authority. Keep enum.make semantics frozen. Cover all registered tags, feature negotiation, payload/type errors and authority rejection in AIR/source; verify canonical and hosted CLI behavior. Record reference-bundle costs and the map preflight-validation prerequisite.
297 lines
12 KiB
Markdown
297 lines
12 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`).
|
|
|
|
The `features` line is the semantic compatibility contract of the module: it lists the feature bundles ([`../reference/air-features.md`](../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 `E0006` with a `reason` field (`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 `%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`).
|
|
- Baseline aggregate types are Copy. Under `linear_aggregates`, newly permitted linear members make structs/enums/fixed arrays linear structurally (spec 03 §25); baseline Copy aggregates retain their semantics.
|
|
- `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`, 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:
|
|
|
|
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, diagnostic codes or trap codes; every added construct belongs to a feature registered in [`../reference/air-features.md`](../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;
|
|
4. the canonical form (§8.5) of existing constructs is unchanged.
|
|
|
|
A published feature's semantics are immutable. In particular the variant set of a published built-in enum never changes, since a new variant would make existing exhaustive `enum.switch` instructions non-exhaustive. 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`](../reference/capabilities.md)). Built-in enums such as `cap_error` and `io_error` are spelled without `@`, have registered variants and are used with `enum.switch <builtin> %v, …` like declared enums. Rules: spec 05 §9–§12.
|
|
|
|
Feature `error_values`: `%e = error.const alloc_error.budget_exceeded` creates ordinary Copy error data without authority or effects (spec 05 §14); `enum.make` remains a declared-enum instruction.
|
|
|
|
Feature `resources`: `file<read>`, `file<write>`, `file<append>`, `listener`, `stream` are linear, authority-bearing resource types (spec 06 §6).
|
|
|
|
Feature `heap`: `own<T>` and `own_array<T>` (linear owners, `T` a value type) and the built-in enum `alloc_error`. Feature `regions`: `region` (linear). Rules: spec 03 §18–§21. Linear values — resources, owners and regions — follow the consumption rules of spec 06 §6.2–§6.3.
|
|
|
|
### 8.10 Syntax of the feature `unsafe`
|
|
|
|
A function header may carry, after its `effects` line, a line `trusted "<obligation>"`. String literals exist only in this position: `"` … `"` with the escapes `\"`, `\\` and `\n`, no line breaks inside, not empty (`E0001`). Rules: spec 13 §6.
|
|
|
|
### 8.11 Types and syntax of the features `cancellation`, `channels` and `sync`
|
|
|
|
Types: `shield` (`cancellation`), `sender<T>` and `receiver<T>` (`channels`), `mutex<T>` and `guard<T>` (`sync`). `shield` and `guard<T>` are linear and function-bound; `sender<T>`, `receiver<T>` and `mutex<T>` are linear. Rules: spec 04 §37–§41.
|
|
|
|
`select` is a terminator written on one line: one or more bracketed arms `[recv %b, ok -> @x, closed -> @y]` or `[send %b, %v, ok -> @x, closed -> @y]`, separated by commas, followed by `, cancelled -> @c`. `channel.make` takes a type operand first, like `local.alloc`.
|
|
|
|
### 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).
|
|
|
|
Baseline value types are Copy; owners/resources and feature-gated linear aggregates obey whole-value consumption (spec 03 §25).
|
|
|
|
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 (`slice.split`; `atomic.cmpxchg` with feature `atomics`, spec 04 §42).
|