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
This commit is contained in:
1 parent
11ce6cb38c
commit
41b764ccfd
13 files changed
+425
-18
No files matched your search
@@ -88,6 +88,8 @@ Common trap sources:
|
||||
|
||||
Safe programs SHOULD prefer `Result` for expected failures.
|
||||
|
||||
Every trap has a stable code in the `T0xxx` range of [`../reference/error-codes.md`](../reference/error-codes.md). A trap terminates the program; in format version 0 there is no way to observe a trap from inside the program. The implementation reports a trap record containing the code, kind, function, block and the zero-based index of the trapping instruction within its block. Trap records are deterministic: the same module and inputs produce the same record.
|
||||
|
||||
## 7. Generics
|
||||
|
||||
Aji supports monomorphized generics.
|
||||
|
||||
@@ -50,6 +50,16 @@ checked_add(a, b) // Option<T>
|
||||
|
||||
Release/debug modes MUST NOT change these semantics.
|
||||
|
||||
### 2.1 Edge cases
|
||||
|
||||
- Division and remainder truncate toward zero; the remainder has the sign of the dividend.
|
||||
- Division or remainder by zero traps (`T0004`). Signed `MIN / -1` and `MIN % -1` trap as overflow (`T0003`).
|
||||
- `shl` and `shr` take a shift amount of the same type as the shifted value. An amount that is negative or not less than the bit width traps (`T0005`). `shl` discards bits shifted out; `shr` is arithmetic for signed types and logical for unsigned types.
|
||||
- There are no implicit conversions. Explicit integer casts:
|
||||
- `widen`: only between types where every source value is representable in the target (unsigned to wider unsigned or wider signed, signed to wider signed); anything else is rejected statically (`E1002`). `usize` and `isize` are never valid `widen` operands or targets, so verification stays independent of the target; use `checked`.
|
||||
- `checked`: converts the numeric value; traps (`T0006`) if it is not representable in the target type.
|
||||
- `wrap`: takes the two's-complement bit pattern modulo 2^N of the target width.
|
||||
|
||||
## 3. Composite types
|
||||
|
||||
```aji
|
||||
|
||||
@@ -182,3 +182,49 @@ Every binary must encode version and required feature bits.
|
||||
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.
|
||||
@@ -0,0 +1,183 @@
|
||||
# 07a. AIR Instruction Contracts
|
||||
|
||||
Normative. Each instruction lists the fields required by spec 07 §4. Syntax is in [`../reference/air-text.ebnf`](../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`) or `true`/`false` (`T` = `bool`).
|
||||
- Result: `T`.
|
||||
- Verifier: the literal MUST be representable in `T`, otherwise `E0002`.
|
||||
- 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`, otherwise `E1001`.
|
||||
- Result: `I`.
|
||||
- Traps: `T0003` if the mathematical result is not representable in `I`.
|
||||
- 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: `T0004` if `%b = 0`; `T0003` for signed `MIN` divided 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 type `T`, otherwise `E1001`.
|
||||
- Result: `T`.
|
||||
- Interpreter: bitwise operation (logical for `bool`).
|
||||
|
||||
### `shl|shr.I %a, %b`
|
||||
|
||||
- Operands: both of type `I`, otherwise `E1001`.
|
||||
- Result: `I`.
|
||||
- Traps: `T0005` if `%b < 0` or `%b ≥ N`.
|
||||
- Interpreter: spec 02 §2.1.
|
||||
|
||||
## Comparisons
|
||||
|
||||
### `eq|ne.T %a, %b`
|
||||
|
||||
- Operands: `T` ∈ `I ∪ {bool}`; both of type `T`, otherwise `E1001`.
|
||||
- Result: `bool`.
|
||||
|
||||
### `lt|le|gt|ge.I %a, %b`
|
||||
|
||||
- Operands: both of type `I`, otherwise `E1001`.
|
||||
- Result: `bool`. Signed comparison for signed `I`, unsigned otherwise.
|
||||
|
||||
## Casts
|
||||
|
||||
### `cast.widen.I %a`
|
||||
|
||||
- Operands: `type(%a)` ∈ `I`; the conversion MUST be lossless for every value (spec 02 §2.1), otherwise `E1002`.
|
||||
- Result: `I`.
|
||||
|
||||
### `cast.checked.I %a`
|
||||
|
||||
- Operands: `type(%a)` ∈ `I`, otherwise `E1002`.
|
||||
- Result: `I`.
|
||||
- Traps: `T0006` if the value of `%a` is not representable in `I`.
|
||||
|
||||
### `cast.wrap.I %a`
|
||||
|
||||
- Operands: `type(%a)` ∈ `I`, otherwise `E1002`.
|
||||
- Result: `I`, the low N bits of the two's-complement representation of `%a`.
|
||||
|
||||
## Assertions
|
||||
|
||||
### `assert %c`
|
||||
|
||||
- Operands: `type(%c) = bool`, otherwise `E1001`.
|
||||
- Result: none.
|
||||
- Traps: `T0002` if `%c` is false.
|
||||
|
||||
## Aggregates
|
||||
|
||||
### `struct.make @S(%f1, …, %fn)`
|
||||
|
||||
- Operands: `@S` is a struct type with exactly n fields (`E1001` otherwise); `type(%fi)` equals the type of field i.
|
||||
- Result: `@S`.
|
||||
|
||||
### `struct.get @S.f, %s`
|
||||
|
||||
- Operands: `@S` is a struct type with a field `f` (`E0004` otherwise); `type(%s) = @S` (`E1001` otherwise).
|
||||
- Result: the type of field `f`.
|
||||
|
||||
### `enum.make @E.V(%p1, …, %pn)`
|
||||
|
||||
- Operands: `@E` is an enum type with a variant `V` (`E0004` otherwise) whose payload has exactly n types; `type(%pi)` equals payload type i (`E1001` otherwise).
|
||||
- Result: `@E`.
|
||||
|
||||
## Calls
|
||||
|
||||
### `call @f(%a1, …, %an)`
|
||||
|
||||
- Operands: `@f` is a function of the module (`E0004` otherwise) with n parameters; `type(%ai)` equals parameter type i (`E1001` otherwise).
|
||||
- Result: the return type of `@f`. A result MUST be bound unless the return type is `void` or `never`, and MUST NOT be bound when it is `void` (`E1001`).
|
||||
- Effects: the callee's effect set (checked from Phase 2).
|
||||
- Traps: whatever the callee traps; `T0007` if 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: `@b` has 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 for `br`.
|
||||
- Interpreter: continues at `@t` if `%c` is true, otherwise at `@f`.
|
||||
|
||||
### `switch.I %v, k1 -> @b1(…), …, default -> @d(…)`
|
||||
|
||||
- Operands: `type(%v) = I` (`E1001`); each `ki` representable in `I` (`E0002`); case values pairwise distinct (`E5002`); targets as for `br`.
|
||||
- Interpreter: continues at the case whose value equals `%v`, otherwise at `default`.
|
||||
|
||||
### `enum.switch @E %v, V1 -> @b1, …[, default -> @d(…)]`
|
||||
|
||||
- Operands: `type(%v) = @E` (`E1001`); each `Vi` is a variant of `@E` (`E0004`) and appears at most once (`E5002`); every variant appears unless `default` is present (`E5002`); the parameters of `@bi` MUST equal the payload types of `Vi` in 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 in `void` functions (`E1001`). Not allowed in `never` functions (`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).
|
||||
@@ -85,7 +85,21 @@ It may justify elimination of later runtime checks.
|
||||
|
||||
Verifier algorithms must have deterministic resource budgets.
|
||||
|
||||
If verification exceeds configured/spec-defined complexity bounds, the verifier emits a stable failure rather than silently weakening checks.
|
||||
If verification exceeds the spec-defined complexity bounds, the verifier emits a stable failure (`E5101`) rather than silently weakening checks.
|
||||
|
||||
Bounds are fixed by this specification, not configurable: a configurable bound would let a flag change which programs are accepted. Format version 0 defines:
|
||||
|
||||
| Limit | Value |
|
||||
|---|---|
|
||||
| functions per module | 65 536 |
|
||||
| type declarations per module | 65 536 |
|
||||
| fields per struct, variants per enum, payload values per variant | 256 |
|
||||
| parameters per function or block, arguments per call or branch | 64 |
|
||||
| blocks per function | 16 384 |
|
||||
| instructions per function | 262 144 |
|
||||
| cases per `switch` | 4 096 |
|
||||
|
||||
The `E5101` diagnostic carries the fields `limit` (name of the limit), `max` and `found`.
|
||||
|
||||
## 8. Contracts
|
||||
|
||||
|
||||
@@ -70,3 +70,15 @@ Signal and interrupt handlers use restricted capabilities/effects. Allocation, o
|
||||
The initial native backend should lower AIR to LLVM IR.
|
||||
|
||||
A custom native backend is deferred until AIR and verifier semantics stabilize.
|
||||
|
||||
## 9. Target description
|
||||
|
||||
Every execution, interpreted or native, runs against an explicit target description. Target-dependent semantics come only from it.
|
||||
|
||||
Format version 0 defines one field:
|
||||
|
||||
| Field | Meaning |
|
||||
|---|---|
|
||||
| `pointer_bits` | width of `usize`/`isize` in bits: 32 or 64 |
|
||||
|
||||
The reference interpreter uses the target `air64` (`pointer_bits = 64`) unless a module declares target requirements (a later format version).
|
||||
+2
-1
@@ -15,7 +15,8 @@ The specification is split by semantic domain. Rationale for major decisions liv
|
||||
| [04-concurrency-and-atomics.md](04-concurrency-and-atomics.md) | Normative draft | Tasks, synchronization, memory model, atomics |
|
||||
| [05-effects-and-capabilities.md](05-effects-and-capabilities.md) | Normative draft | Authority and effect tracking |
|
||||
| [06-resources-and-errors.md](06-resources-and-errors.md) | Normative draft | Linear resources and error handling |
|
||||
| [07-air.md](07-air.md) | Normative draft | Canonical IR |
|
||||
| [07-air.md](07-air.md) | Normative draft | Canonical IR, text format |
|
||||
| [07a-air-instructions.md](07a-air-instructions.md) | Normative draft | Per-instruction contracts |
|
||||
| [08-verifier.md](08-verifier.md) | Normative draft | Verification rules |
|
||||
| [09-runtime-and-abi.md](09-runtime-and-abi.md) | Normative draft | Runtime, FFI, MMIO/DMA |
|
||||
| [10-diagnostics-and-agent-protocol.md](10-diagnostics-and-agent-protocol.md) | Normative draft | Stable diagnostics and `ajid` |
|
||||
|
||||
Reference in new issue
Block a user