spec: accept RFC 0005 and integrate Phase 1 memory model (P1-1, P1-2)
- RFC 0005 Accepted (revision 2) - spec 07 §8.8 types and syntax; spec 07a contracts for features ownership and arrays; spec 08 §11-§14 borrows, facts, initialization, stage order; spec 03 frame storage, second-class borrows, static region generations - grammar: from-returns, multi-result, new types and opcodes - feature registry: ownership and arrays published; heap added and regions moved to Phase 2; borrow_joins reserved and unscheduled - codes E2105-E2108, T0008 - Q-009, Q-010 resolved; Phase 1 and Phase 2 plans updated; Own test moved to phase 2
This commit is contained in:
15 files changed
+269
-55
No files matched your search
@@ -58,6 +58,8 @@ Conceptually:
|
||||
fn first<'a, T>(values: slice<'a, T>) -> Option<ref<'a, T>>
|
||||
```
|
||||
|
||||
In AIR, borrows are second-class (spec 07 §8.8): they live only in SSA values and call arguments, never in aggregates, and a function returns a borrow only with an explicit `from %param` naming the borrow parameter it derives from (spec 08 §11.3). Every borrow created in a function is ended explicitly on every normal path (`borrow.end`). Source-level lifetimes lower to this form; source programs that need more are rejected by the frontend.
|
||||
|
||||
## 5. Provenance
|
||||
|
||||
Every safe reference has logical provenance including:
|
||||
@@ -78,6 +80,8 @@ Normal locals live in the current activation frame.
|
||||
|
||||
Their references cannot escape the frame unless the language proves that the target execution context completes before the frame ends, as with structured child tasks.
|
||||
|
||||
In AIR, frame storage is `local.alloc`, permitted only in a function's entry block so the frame size is known before execution and bounded (spec 08 §11.5). A borrow derived from frame storage never leaves its call (`E2108`, spec 08 §11.3).
|
||||
|
||||
## 7. Own<T>
|
||||
|
||||
`Own<T>` is a move-only handle to an individually allocated object.
|
||||
@@ -123,9 +127,7 @@ region.destroy(move r);
|
||||
|
||||
A reset logically increments the region generation.
|
||||
|
||||
Any borrow or reference associated with a prior generation becomes invalid.
|
||||
|
||||
Optimized builds may remove generation checks when statically proven unnecessary.
|
||||
Any borrow or reference associated with a prior generation becomes invalid. Generations are checked statically only: a region cannot be reset or destroyed while a borrow derived from it is live, and use of a borrow from a prior generation is rejected (`E2104`). Safe code performs no runtime generation checks. Cyclic or arena-style graphs use `Island<T>` or indices (§14). (Decision Q-010; specified with the `regions` feature in Phase 2.)
|
||||
|
||||
### 8.2 Reset precondition
|
||||
|
||||
|
||||
@@ -253,3 +253,24 @@ A change is permitted under `air 0` only if all of the following hold:
|
||||
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.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`; `[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 |
|
||||
| `[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`).
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
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.
|
||||
This file covers the format-0 baseline (Phase 0) and the features `ownership` and `arrays` (Phase 1). Later phases add instructions here, grouped by feature, before implementing them. An instruction of a feature is only valid in a module that declares the feature (spec 07 §8.1).
|
||||
|
||||
## Common contract for format version 0
|
||||
|
||||
@@ -168,6 +168,51 @@ A terminator ends a block (spec 07 §8.2). Terminators bind no result.
|
||||
|
||||
- Traps: `T0001`, always.
|
||||
|
||||
## Feature `ownership` and feature `arrays`
|
||||
|
||||
Types, borrow rules and bounds facts used below: spec 07 §8.8 (types), spec 08 §11 (borrows), spec 08 §12 (facts), spec 08 §13 (initialization). Decision record: RFC 0005.
|
||||
|
||||
Common contract unless stated: no effects, no capabilities, no synchronization. "Owned borrow" means a borrow created in the current function (§5.1).
|
||||
|
||||
### `ownership`
|
||||
|
||||
| Instruction | Operands → result | Verifier | Runtime |
|
||||
|---|---|---|---|
|
||||
| `%s = local.alloc T` | → `local<T>` | only in the entry block (`E5002`); storage is uninitialized; counts toward the frame limit (spec 08 §11.5) | allocates frame storage once per call, freed at return |
|
||||
| `local.store %s, %v` | `local<T>`, `T` | no live borrow of `%s` (`E2101`); initializes `%s` | writes `%v` |
|
||||
| `local.zero %s` | `local<T>` | `T` zeroable: integers, `bool`, arrays and structs of zeroable types (`E1001`); no live borrow (`E2101`); initializes | writes all-zero value |
|
||||
| `%v = local.load %s` | `local<T>` → `T` | initialized (`E1010`); no live `mut` borrow of `%s` (`E2101`) | reads |
|
||||
| `%r = borrow.ref %x` | `local<T>`, `ref<T>`, `mut<T>` → `ref<T>`; `slice<T>`, `slice_mut<T>` → `slice<T>` | spec 08 §11.2; locals must be initialized (`E1010`); child of `%x` unless `%x` is a local | shared view of the same place |
|
||||
| `%m = borrow.mut %x` | `local<T>`, `mut<T>` → `mut<T>`; `slice_mut<T>` → `slice_mut<T>` | spec 08 §11.2; locals must be initialized (`E1010`); child of `%x` unless `%x` is a local | exclusive view of the same place |
|
||||
| `%f = borrow.field @S.f, %b` | `ref<@S>`/`mut<@S>` → `ref<F>`/`mut<F>` | child of `%b`; distinct fields are disjoint | reference to the field |
|
||||
| `borrow.end %b` | owned borrow | no live children (`E2102`); not a parameter (`E2107`); not already ended (`E2106`) | none |
|
||||
| `%v = load %b` | `ref<T>`/`mut<T>` → `T` | `%b` usable for reading (spec 08 §11.2) | reads |
|
||||
| `store %b, %v` | `mut<T>`, `T` | `%b` usable for writing (spec 08 §11.2) | writes |
|
||||
|
||||
### `arrays`
|
||||
|
||||
| Instruction | Operands → result | Verifier | Runtime |
|
||||
|---|---|---|---|
|
||||
| `%a = array.splat [T; N] %v` | `T` → `[T; N]` | — | every element is `%v` |
|
||||
| `%e = borrow.index %b, %i` | `ref`/`mut` of `[T; N]`, or `slice`/`slice_mut` of `T`; `usize` → `ref<T>`/`mut<T>` | child of `%b` | traps `T0008` if `%i ≥ length` |
|
||||
| `%e = borrow.index.proven %b, %i` | as above | `%i < length` proven (spec 08 §12), otherwise `E2103` | no check |
|
||||
| `%s = slice.make %b` | `ref<[T; N]>`/`mut<[T; N]>` → `slice<T>`/`slice_mut<T>` | child of `%b` | whole array |
|
||||
| `%n = slice.len %s` | `slice<T>`/`slice_mut<T>` → `usize` | — | length |
|
||||
| `%t = slice.sub %s, %start, %len` | slice, `usize`, `usize` → same slice type | child of `%s` | traps `T0008` unless `start ≤ slice.len(s)` and `len ≤ slice.len(s) − start` (evaluated without overflow); result covers `[start, start + len)` |
|
||||
| `%l, %r = slice.split %s, %at` | slice, `usize` → two slices of the same type | two children of `%s`, disjoint | traps `T0008` unless `at ≤ slice.len(s)`; `l` covers `[0, at)`, `r` covers `[at, len)` |
|
||||
| `slice.copy %dst, %src` | `slice_mut<T>`, `slice<T>`/`slice_mut<T>` | both usable (spec 08 §11.2); overlapping operands are rejected by the aliasing rules (spec 08 §11.2), so this is never a `memmove` | traps `T0008` unless `len(dst) ≥ len(src)`; copies exactly `len(src)` elements into the start of `dst` |
|
||||
| `guard %c` | `bool` | adds a fact (spec 08 §12) | traps `T0002` if false |
|
||||
|
||||
New trap code: `T0008 trap.out_of_bounds`.
|
||||
|
||||
`slice.make` creates a slice only from an existing borrow of an array, covering exactly that array and inheriting its provenance. No instruction creates a slice from an address and a length. An overlap-safe copy, if ever needed, will be a separate instruction (`slice.move`).
|
||||
|
||||
### Runtime
|
||||
|
||||
- Frame storage is allocated by `local.alloc` and released at return; no code runs at release.
|
||||
- The interpreter represents a borrow as (storage, path) and a slice as (storage, path, offset, length). It performs no alias or liveness checks at runtime: the verifier guarantees them. Bounds checks run exactly where §4 says.
|
||||
- Behavior is deterministic; storage addresses are never observable.
|
||||
|
||||
## 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:
|
||||
|
||||
@@ -131,3 +131,100 @@ The verifier must reject:
|
||||
Every normative verifier rejection has a stable machine-readable code.
|
||||
|
||||
Diagnostics should include enough structured state for an agent to repair the program without parsing prose.
|
||||
|
||||
## 11. Borrow verification (feature `ownership`)
|
||||
|
||||
Normative rules for frame storage and second-class borrows. Decision record: RFC 0005.
|
||||
|
||||
### 11.1 Owned and lent borrows
|
||||
|
||||
- A borrow created in the function (`borrow.*`, `slice.make`, `slice.sub`, `slice.split`, or the result of a `call` to a function with `from`) is **owned** by the function.
|
||||
- A borrow-typed function parameter is **lent**: the caller keeps it alive for the whole call. A lent borrow is never ended by the callee (`E2107`) and need not be ended.
|
||||
- **Path linearity.** On every reachable normal path from the creation of an owned borrow to a `ret`, the borrow MUST be either ended exactly once or transferred to the caller by a valid `ret … from` (§11.3); otherwise `E2105`. The rule is per path, not per instruction text: a borrow may have several `borrow.end` instructions in different branches, as long as each path executes exactly one.
|
||||
- `trap` ends a path abnormally. Aji has no unwinding, so paths ending in `trap` carry no obligation.
|
||||
- Using or ending a borrow on a path where it has already been ended is `E2106`.
|
||||
|
||||
### 11.2 Places, overlap and access
|
||||
|
||||
Every borrow has a **root** (a `local` or a lent parameter), a **path** from the root (field names, a slice range, or an element), a **mode** (`ref`/`mut`) and a **parent** (none for borrows taken directly from a local or a parameter).
|
||||
|
||||
- Two paths overlap unless they diverge at different fields of the same struct, or they are the two results of one `slice.split` (and descendants of each).
|
||||
- Element borrows (`borrow.index`) and sub-slices of the same parent all overlap each other.
|
||||
- Creating a borrow conflicts with every live borrow whose path overlaps when either is `mut` (`E2101`).
|
||||
- A borrow with live children is **frozen**: it may still create further children that do not conflict, but it cannot be read, written or ended while a live child conflicts with that use (`E2101`, `E2102` for ending).
|
||||
- A `ref` borrow is usable for reading; a `mut` borrow is usable for reading and writing, in both cases only if not frozen by a conflicting child.
|
||||
- There is no implicit conversion between borrow types: a `mut`/`slice_mut` borrow is passed where a shared one is expected only through an explicit `borrow.ref` (`E1001` otherwise).
|
||||
- `call`: each borrow argument is lent to the callee for the duration of the call. Arguments are checked as if each were a new borrow of its operand at the call: a `mut` argument conflicts with any other argument that overlaps it (`E2101`).
|
||||
|
||||
### 11.3 Returning a borrow
|
||||
|
||||
`fn @f(…, %p: slice<u8>, …) -> ref<u8> from %p` declares that the result borrows from the argument passed for `%p`.
|
||||
|
||||
- `from` names a borrow-typed parameter; its mode must allow the result mode (`mut` result needs a `mut`/`slice_mut` parameter) (`E1001`).
|
||||
- A borrow-typed return without `from` is `E2102`.
|
||||
- `ret %b` in such a function requires `%b` to be `%p` or a descendant of `%p`. If `%b`'s provenance leads to a `local.alloc` of this function, the return is `E2108` (frame escape); if it leads to another parameter, `E2102`. At `ret`, the only live owned borrows may be `%b` and its owned ancestors, which are transferred to the caller with it; any other live owned borrow is `E2105`.
|
||||
- Escape through aggregates is impossible by construction: borrow types cannot be fields, payloads or array elements (spec 07 §8.8), so `struct.make`, `enum.make` and returned aggregates never contain a borrow.
|
||||
- `from` may name only a borrow parameter. Later features may add other escapable sources (for example region-derived borrows in Phase 2); frame-derived borrows never escape.
|
||||
- At the call site, the result is a new owned borrow whose parent is the argument passed for `%p`; the argument cannot be ended before the result (`E2102`).
|
||||
|
||||
### 11.4 Joins and loops
|
||||
|
||||
The complete borrow state MUST be identical on every edge into a block (`E2105`, reported at the block). The state of each borrow consists of its identity (SSA value), kind (`ref`/`mut`), provenance (root), path, parent borrow, and whether it is live or ended. In particular a borrow created inside a loop body is ended before the back edge.
|
||||
|
||||
Only the set of accepted programs is normative. Because states must match exactly at joins, an implementation can verify a function in one pass over a reverse postorder with one comparison per back edge, but it is not required to work that way.
|
||||
|
||||
### 11.5 Frame size
|
||||
|
||||
Each function's frame storage is the sum of the accounted sizes of its `local.alloc` types. It MUST NOT exceed `max_local_frame` = 1 048 576 bytes (`E5101`, limit `local frame bytes`). Accounted size is a normative counting rule, not a memory layout:
|
||||
|
||||
| Type | Accounted bytes |
|
||||
|---|---|
|
||||
| `u8`/`i8`/`bool` | 1 |
|
||||
| `u16`/`i16` | 2 |
|
||||
| `u32`/`i32` | 4 |
|
||||
| `u64`/`i64` | 8 |
|
||||
| `u128`/`i128` | 16 |
|
||||
| `usize`/`isize` | 8 (the largest supported pointer width, so the result is target-independent) |
|
||||
| struct | sum of its fields |
|
||||
| enum | 4 + the largest sum over a variant's payload |
|
||||
| `[T; N]` | `N ×` accounted size of `T` |
|
||||
|
||||
### 11.6 Borrow-typed block parameters
|
||||
|
||||
Borrow-typed block parameters are reserved for the feature `borrow_joins`. Without it they are rejected with `E0006` reason `feature_not_declared`. Using a feature gate, rather than a permanent type error, keeps the door open without breaking the format-0 freeze (spec 07 §8.7).
|
||||
|
||||
## 12. Bounds facts (feature `arrays`)
|
||||
|
||||
A **fact** `%i < %n` is established only by the following table. `%i` and `%n` are SSA values of type `usize`; `%c` is the comparison result.
|
||||
|
||||
| Comparison defining `%c` | Established by | Fact |
|
||||
|---|---|---|
|
||||
| `%c = lt.usize %i, %n` | true edge of `cbr %c`, or `guard %c` | `%i < %n` |
|
||||
| `%c = gt.usize %n, %i` | true edge of `cbr %c`, or `guard %c` | `%i < %n` |
|
||||
| `%c = ge.usize %i, %n` | false edge of `cbr %c` | `%i < %n` |
|
||||
| `%c = le.usize %n, %i` | false edge of `cbr %c` | `%i < %n` |
|
||||
|
||||
Nothing else establishes a fact. In particular `le.usize %i, %n` and `ge.usize %n, %i` (that is, `i ≤ n`) do not, since `i = n` is out of bounds; comparisons of other types do not; and no arithmetic is performed.
|
||||
|
||||
Scope of a fact:
|
||||
|
||||
- from a `cbr` edge: the target block and every block it dominates, provided the target block's only predecessor is the block containing the `cbr` (so the edge is the only way in);
|
||||
- from `guard %c`: the instructions after the `guard` in its block and every block that block dominates.
|
||||
|
||||
`borrow.index.proven %b, %i` is **proven** exactly when one of these holds at the instruction:
|
||||
|
||||
| Case | Condition |
|
||||
|---|---|
|
||||
| fact on the same slice | a fact `%i < %n` holds where `%n = slice.len %b`, with `%i` and `%b` the same SSA values as the operands |
|
||||
| constant index | `%i = const.usize k` and `%b` has a static length `N` (a borrow of `[T; N]`) with `k < N` |
|
||||
| fact against a constant | a fact `%i < %n` holds where `%n = const.usize k`, `%b` has a static length `N` and `k ≤ N` |
|
||||
|
||||
This is the complete normative analysis: an implementation MUST NOT accept more or fewer programs.
|
||||
|
||||
## 13. Initialization (feature `ownership`)
|
||||
|
||||
A `local<T>` is uninitialized after `local.alloc` and becomes initialized by `local.store` or `local.zero`. `local.load`, `borrow.ref` and `borrow.mut` of a local require it to be initialized on every path from the entry block (`E1010`). Initialization is tracked for the whole local; there is no partial initialization in format 0.
|
||||
|
||||
## 14. Stage order
|
||||
|
||||
Verification stages run in this order, stopping after the first stage that reports an error (spec 07 §8.6): resource limits (including frame size), CFG structure (including `local.alloc` placement), SSA, types, initialization, borrows, bounds.
|
||||
Reference in new issue
Block a user