- 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
6.1 KiB
07a. AIR Instruction Contracts
Normative. Each instruction lists the fields required by spec 07 §4. Syntax is in ../reference/air-text.ebnf.
This file covers format version 0 (Phase 0). Later phases add instructions here before implementing them.
Common contract for format version 0
All format-version-0 instructions operate on copyable values only. Unless an entry says otherwise:
| Field | Value |
|---|---|
| Ownership transition | none; operands remain available |
| Borrow transition | none |
| Memory reads/writes | none |
| Synchronization | none |
| Effects required | none |
| Capabilities required | none |
| Traps | none |
Notation: T is the type suffix of the opcode; I ranges over integer types; type(%x) is the type of value %x. "Rejects with E1001" means the verifier reports type.mismatch at the instruction.
Values
const.T lit
- Operands: an integer literal (
T∈I) ortrue/false(T=bool). - Result:
T. - Verifier: the literal MUST be representable in
T, otherwiseE0002. - Interpreter: produces the literal value.
copy %a
- Operands: any value.
- Result:
type(%a). - Interpreter: produces a value equal to
%a.
Integer arithmetic
add|sub|mul.checked.I %a, %b
- Operands:
type(%a) = type(%b) = I, otherwiseE1001. - Result:
I. - Traps:
T0003if the mathematical result is not representable inI. - Interpreter: mathematical result.
add|sub|mul.wrap.I %a, %b
- Operands and result as above. No traps.
- Interpreter: mathematical result modulo 2^N, reinterpreted in
I.
div|rem.checked.I %a, %b
- Operands and result as above.
- Traps:
T0004if%b = 0;T0003for signedMINdivided by-1. - Interpreter: truncating division; remainder has the sign of the dividend (spec 02 §2.1).
Bitwise
and|or|xor.T %a, %b
- Operands:
T∈I ∪ {bool}; both operands of typeT, otherwiseE1001. - Result:
T. - Interpreter: bitwise operation (logical for
bool).
shl|shr.I %a, %b
- Operands: both of type
I, otherwiseE1001. - Result:
I. - Traps:
T0005if%b < 0or%b ≥ N. - Interpreter: spec 02 §2.1.
Comparisons
eq|ne.T %a, %b
- Operands:
T∈I ∪ {bool}; both of typeT, otherwiseE1001. - Result:
bool.
lt|le|gt|ge.I %a, %b
- Operands: both of type
I, otherwiseE1001. - Result:
bool. Signed comparison for signedI, unsigned otherwise.
Casts
cast.widen.I %a
- Operands:
type(%a)∈I; the conversion MUST be lossless for every value (spec 02 §2.1), otherwiseE1002. - Result:
I.
cast.checked.I %a
- Operands:
type(%a)∈I, otherwiseE1002. - Result:
I. - Traps:
T0006if the value of%ais not representable inI.
cast.wrap.I %a
- Operands:
type(%a)∈I, otherwiseE1002. - Result:
I, the low N bits of the two's-complement representation of%a.
Assertions
assert %c
- Operands:
type(%c) = bool, otherwiseE1001. - Result: none.
- Traps:
T0002if%cis false.
Aggregates
struct.make @S(%f1, …, %fn)
- Operands:
@Sis a struct type with exactly n fields (E1001otherwise);type(%fi)equals the type of field i. - Result:
@S.
struct.get @S.f, %s
- Operands:
@Sis a struct type with a fieldf(E0004otherwise);type(%s) = @S(E1001otherwise). - Result: the type of field
f.
enum.make @E.V(%p1, …, %pn)
- Operands:
@Eis an enum type with a variantV(E0004otherwise) whose payload has exactly n types;type(%pi)equals payload type i (E1001otherwise). - Result:
@E.
Calls
call @f(%a1, …, %an)
- Operands:
@fis a function of the module (E0004otherwise) with n parameters;type(%ai)equals parameter type i (E1001otherwise). - Result: the return type of
@f. A result MUST be bound unless the return type isvoidornever, and MUST NOT be bound when it isvoid(E1001). - Effects: the callee's effect set (checked from Phase 2).
- Traps: whatever the callee traps;
T0007if the call would exceed the call-depth limit of 1 024 active frames. - Interpreter: evaluates the callee with fresh parameter bindings; returns its result.
Terminators
A terminator ends a block (spec 07 §8.2). Terminators bind no result.
br @b(%a1, …, %an)
- Operands:
@bhas n parameters (E5002);type(%ai)equals parameter type i (E1001). - Interpreter: binds the arguments simultaneously and continues at
@b.
cbr %c, @t(…), @f(…)
- Operands:
type(%c) = bool(E1001); both targets as forbr. - Interpreter: continues at
@tif%cis true, otherwise at@f.
switch.I %v, k1 -> @b1(…), …, default -> @d(…)
- Operands:
type(%v) = I(E1001); eachkirepresentable inI(E0002); case values pairwise distinct (E5002); targets as forbr. - Interpreter: continues at the case whose value equals
%v, otherwise atdefault.
enum.switch @E %v, V1 -> @b1, …[, default -> @d(…)]
- Operands:
type(%v) = @E(E1001); eachViis a variant of@E(E0004) and appears at most once (E5002); every variant appears unlessdefaultis present (E5002); the parameters of@biMUST equal the payload types ofViin number (E5002) and type (E1001). - Interpreter: continues at the arm for the active variant, binding its payload to the target block's parameters; otherwise at
default.
ret [%v]
- Operands:
type(%v)equals the function's return type (E1001). Without operand only invoidfunctions (E1001). Not allowed inneverfunctions (E1001). - Interpreter: returns from the function.
trap
- Traps:
T0001, always.
Run result
aji run executes the function @main, which MUST take no parameters. On return it reports {"status":"returned","value":V} where V renders the returned value as JSON:
| Type | Rendering |
|---|---|
| integer | decimal number |
bool |
true / false |
void |
null |
| struct | object of fields in declaration order |
| enum | {"variant":"Name","payload":[…]} |
On a trap it reports {"status":"trapped", "code":…, "kind":…, "function":…, "block":…, "inst":…} (spec 01 §6).