interp: deterministic interpreter, aji run, run tests (P0-11, P0-12)

- src/interp/machine.zig: format-0 interpreter on the air64 target;
  checked/wrapping arithmetic, shifts, casts, aggregates, calls with a
  1024-frame limit, traps as records with T0xxx codes
- aji run: prints one JSON run record; exit code 4 on trap
- conformance runner: `expect: run` with `result:`/`trap:`, every run
  executed twice and compared byte-for-byte; header keys must be single
  lowercase words
- Phase 0 corpus: 3 accept, 19 reject, 15 run tests (41 total)
This commit is contained in:
dev committed 2026-10-04 12:28:27 +03:00
1 parent 50bf990355
commit 1f5008bb37
45 files changed
+1463 -23

No files matched your search

+9 -4
View File
@@ -8,6 +8,7 @@ Run with `zig build conformance` (add `-- --json` for JSON Lines, `-- --filter <
tests/conformance/
phase<N>/accept/*.air modules a conforming verifier MUST accept
phase<N>/reject/*.air modules a conforming verifier MUST reject
phase<N>/run/*.air modules that MUST verify and produce a given run record
```
`phase<N>` is the implementation phase (`docs/plan/README.md`) that first requires the test to pass.
@@ -18,11 +19,15 @@ Every test starts with a block of `;` lines. Keys:
| Key | Required | Value |
|---|---|---|
| `expect` | yes | `accept`, or `reject` followed by one or more codes from `docs/reference/error-codes.md` |
| `expect` | yes | `accept`; `reject` followed by one or more codes from `docs/reference/error-codes.md`; or `run` |
| `result` | with `run` | expected `value` of the run record as canonical JSON, e.g. `42` or `{"x":1}` |
| `trap` | with `run` | expected trap code, e.g. `T0003` |
| `phase` | yes | implementation phase number |
| `spec` | yes | spec section that justifies the expectation |
Header lines without a `key:` are free-form description. The runner blanks the header before handing the module to the pipeline, so line numbers in diagnostics are unchanged and the AIR grammar does not need to know about test metadata.
A key is a single lowercase word; header lines without a `key:` prefix are free-form description. The runner blanks the header before handing the module to the pipeline, so line numbers in diagnostics are unchanged and the AIR grammar does not need to know about test metadata.
`run` executes `@main` twice and fails if the two run records differ (spec 11); otherwise it compares the record with `result` or `trap`. Exactly one of them is required.
`reject` compares the exact multiset of error codes. Listing a code twice means two diagnostics with that code are expected.
@@ -32,11 +37,11 @@ Header lines without a `key:` are free-form description. The runner blanks the h
|---|---|
| `PASS` | result matches `expect` |
| `FAIL` | result differs, or the header is malformed; the run exits 1 |
| `PENDING` | the pipeline stage needed for this input is not implemented yet |
| `PENDING` | the test's `phase` is later than `aji.implemented_phase`, or the pipeline stage it needs is not implemented yet |
## Rules
- Every verifier rule gets at least one `accept` and one `reject` test (AGENTS.md, testing policy).
- One defect per `reject` test. A test that triggers several unrelated errors is hard to repair against.
- Keep tests minimal: the smallest module that exhibits the rule.
- Text AIR syntax in the seed tests is provisional until the text AIR grammar is specified (Phase 0, task P0-1). Update the tests when the grammar lands.
- Tests are written in text AIR format 0 (`docs/reference/air-text.ebnf`) and start with the four-line module header.
@@ -0,0 +1,212 @@
; Checked and wrapping arithmetic on every integer type.
; expect: accept
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.all_integer_widths
air 0
lang 0.1
features []
fn @f_u8(%a: u8, %b: u8) -> u8
effects []
entry:
%s = add.checked.u8 %a, %b
%d = sub.wrap.u8 %s, %b
%m = mul.wrap.u8 %d, %a
%q = div.checked.u8 %m, %b
%r = rem.checked.u8 %q, %b
%x = xor.u8 %r, %a
%l = shl.u8 %x, %b
%c = lt.u8 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_u16(%a: u16, %b: u16) -> u16
effects []
entry:
%s = add.checked.u16 %a, %b
%d = sub.wrap.u16 %s, %b
%m = mul.wrap.u16 %d, %a
%q = div.checked.u16 %m, %b
%r = rem.checked.u16 %q, %b
%x = xor.u16 %r, %a
%l = shl.u16 %x, %b
%c = lt.u16 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_u32(%a: u32, %b: u32) -> u32
effects []
entry:
%s = add.checked.u32 %a, %b
%d = sub.wrap.u32 %s, %b
%m = mul.wrap.u32 %d, %a
%q = div.checked.u32 %m, %b
%r = rem.checked.u32 %q, %b
%x = xor.u32 %r, %a
%l = shl.u32 %x, %b
%c = lt.u32 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_u64(%a: u64, %b: u64) -> u64
effects []
entry:
%s = add.checked.u64 %a, %b
%d = sub.wrap.u64 %s, %b
%m = mul.wrap.u64 %d, %a
%q = div.checked.u64 %m, %b
%r = rem.checked.u64 %q, %b
%x = xor.u64 %r, %a
%l = shl.u64 %x, %b
%c = lt.u64 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_u128(%a: u128, %b: u128) -> u128
effects []
entry:
%s = add.checked.u128 %a, %b
%d = sub.wrap.u128 %s, %b
%m = mul.wrap.u128 %d, %a
%q = div.checked.u128 %m, %b
%r = rem.checked.u128 %q, %b
%x = xor.u128 %r, %a
%l = shl.u128 %x, %b
%c = lt.u128 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_usize(%a: usize, %b: usize) -> usize
effects []
entry:
%s = add.checked.usize %a, %b
%d = sub.wrap.usize %s, %b
%m = mul.wrap.usize %d, %a
%q = div.checked.usize %m, %b
%r = rem.checked.usize %q, %b
%x = xor.usize %r, %a
%l = shl.usize %x, %b
%c = lt.usize %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_i8(%a: i8, %b: i8) -> i8
effects []
entry:
%s = add.checked.i8 %a, %b
%d = sub.wrap.i8 %s, %b
%m = mul.wrap.i8 %d, %a
%q = div.checked.i8 %m, %b
%r = rem.checked.i8 %q, %b
%x = xor.i8 %r, %a
%l = shl.i8 %x, %b
%c = lt.i8 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_i16(%a: i16, %b: i16) -> i16
effects []
entry:
%s = add.checked.i16 %a, %b
%d = sub.wrap.i16 %s, %b
%m = mul.wrap.i16 %d, %a
%q = div.checked.i16 %m, %b
%r = rem.checked.i16 %q, %b
%x = xor.i16 %r, %a
%l = shl.i16 %x, %b
%c = lt.i16 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_i32(%a: i32, %b: i32) -> i32
effects []
entry:
%s = add.checked.i32 %a, %b
%d = sub.wrap.i32 %s, %b
%m = mul.wrap.i32 %d, %a
%q = div.checked.i32 %m, %b
%r = rem.checked.i32 %q, %b
%x = xor.i32 %r, %a
%l = shl.i32 %x, %b
%c = lt.i32 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_i64(%a: i64, %b: i64) -> i64
effects []
entry:
%s = add.checked.i64 %a, %b
%d = sub.wrap.i64 %s, %b
%m = mul.wrap.i64 %d, %a
%q = div.checked.i64 %m, %b
%r = rem.checked.i64 %q, %b
%x = xor.i64 %r, %a
%l = shl.i64 %x, %b
%c = lt.i64 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_i128(%a: i128, %b: i128) -> i128
effects []
entry:
%s = add.checked.i128 %a, %b
%d = sub.wrap.i128 %s, %b
%m = mul.wrap.i128 %d, %a
%q = div.checked.i128 %m, %b
%r = rem.checked.i128 %q, %b
%x = xor.i128 %r, %a
%l = shl.i128 %x, %b
%c = lt.i128 %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
fn @f_isize(%a: isize, %b: isize) -> isize
effects []
entry:
%s = add.checked.isize %a, %b
%d = sub.wrap.isize %s, %b
%m = mul.wrap.isize %d, %a
%q = div.checked.isize %m, %b
%r = rem.checked.isize %q, %b
%x = xor.isize %r, %a
%l = shl.isize %x, %b
%c = lt.isize %l, %a
cbr %c, @yes(), @no()
yes:
ret %l
no:
ret %a
@@ -0,0 +1,33 @@
; Recursive calls, void callees and never-returning functions.
; expect: accept
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.recursion_and_void_calls
air 0
lang 0.1
features []
fn @fact(%n: u64) -> u64
effects []
entry:
%one = const.u64 1
%base = le.u64 %n, %one
cbr %base, @done(), @step()
done:
ret %one
step:
%m = sub.checked.u64 %n, %one
%r = call @fact(%m)
%p = mul.checked.u64 %n, %r
ret %p
fn @check(%c: bool) -> void
effects []
entry:
assert %c
ret
fn @fail() -> never
effects []
entry:
trap
@@ -0,0 +1,38 @@
; switch over integers and exhaustive enum.switch with payload binding.
; expect: accept
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.switch_and_enums
air 0
lang 0.1
features []
type @Shape = enum { Empty, Square(u32), Rect(u32, u32) }
fn @area(%s: @Shape) -> u32
effects []
entry:
enum.switch @Shape %s, Empty -> @empty, Square -> @square, Rect -> @rect
empty:
%z = const.u32 0
ret %z
square(%side: u32):
%a = mul.checked.u32 %side, %side
ret %a
rect(%w: u32, %h: u32):
%b = mul.checked.u32 %w, %h
ret %b
fn @classify(%n: i8) -> u8
effects []
entry:
switch.i8 %n, -1 -> @neg(), 0 -> @zero(), default -> @other()
neg:
%a = const.u8 0
ret %a
zero:
%b = const.u8 1
ret %b
other:
%c = const.u8 2
ret %c
@@ -0,0 +1,15 @@
; Passes u16 to a block parameter of type u8.
; expect: reject E1001
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.block_argument_type
air 0
lang 0.1
features []
fn @f(%a: u16) -> void
effects []
entry:
br @next(%a)
next(%x: u8):
ret
@@ -0,0 +1,15 @@
; Branch passes two arguments to a block with one parameter.
; expect: reject E5002
; phase: 0
; spec: docs/spec/07-air.md
module test.branch_arity
air 0
lang 0.1
features []
fn @f(%a: u8) -> u8
effects []
entry:
br @next(%a, %a)
next(%x: u8):
ret %x
@@ -0,0 +1,19 @@
; Calls @g with an i32 where u32 is expected.
; expect: reject E1001
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.call_argument_type
air 0
lang 0.1
features []
fn @f(%a: i32) -> void
effects []
entry:
call @g(%a)
ret
fn @g(%x: u32) -> void
effects []
entry:
ret
@@ -0,0 +1,15 @@
; %a is defined twice.
; expect: reject E5001
; phase: 0
; spec: docs/spec/07-air.md
module test.double_definition
air 0
lang 0.1
features []
fn @f() -> u8
effects []
entry:
%a = const.u8 1
%a = const.u8 2
ret %a
@@ -0,0 +1,18 @@
; Two functions named @f.
; expect: reject E0003
; phase: 0
; spec: docs/spec/07-air.md
module test.duplicate_function
air 0
lang 0.1
features []
fn @f() -> void
effects []
entry:
ret
fn @f() -> void
effects []
entry:
ret
@@ -0,0 +1,13 @@
; f64 is reserved in format version 0.
; expect: reject E0006
; phase: 0
; spec: docs/spec/07-air.md
module test.float_reserved
air 0
lang 0.1
features []
fn @f(%x: f64) -> void
effects []
entry:
ret
@@ -0,0 +1,14 @@
; 256 does not fit in u8.
; expect: reject E0002
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.literal_out_of_range
air 0
lang 0.1
features []
fn @f() -> u8
effects []
entry:
%a = const.u8 256
ret %a
@@ -0,0 +1,14 @@
; widen from i8 to u64 loses negative values.
; expect: reject E1002
; phase: 0
; spec: docs/spec/02-types-and-values.md
module test.lossy_widen
air 0
lang 0.1
features []
fn @f(%a: i8) -> u64
effects []
entry:
%b = cast.widen.u64 %a
ret %b
@@ -0,0 +1,17 @@
; enum.switch misses variant B and has no default.
; expect: reject E5002
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.non_exhaustive_enum_switch
air 0
lang 0.1
features []
type @E = enum { A, B }
fn @f(%e: @E) -> void
effects []
entry:
enum.switch @E %e, A -> @x
x:
ret
@@ -0,0 +1,15 @@
; A struct contains itself by value.
; expect: reject E1003
; phase: 0
; spec: docs/spec/07-air.md
module test.recursive_type
air 0
lang 0.1
features []
type @Node = struct { value: u8, next: @Node }
fn @f() -> void
effects []
entry:
ret
@@ -0,0 +1,13 @@
; Returns u64 from a function declared to return u32.
; expect: reject E1001
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.return_type_mismatch
air 0
lang 0.1
features []
fn @f(%a: u64) -> u32
effects []
entry:
ret %a
@@ -0,0 +1,14 @@
; Missing comma between operands.
; expect: reject E0001
; phase: 0
; spec: docs/reference/air-text.ebnf
module test.syntax_error
air 0
lang 0.1
features []
fn @f(%a: u8) -> u8
effects []
entry:
%b = add.checked.u8 %a %a
ret %b
@@ -0,0 +1,14 @@
; A terminator is followed by another instruction.
; expect: reject E5002
; phase: 0
; spec: docs/spec/07-air.md
module test.terminator_mid_block
air 0
lang 0.1
features []
fn @f() -> void
effects []
entry:
ret
ret
@@ -0,0 +1,13 @@
; 65 parameters exceed the limit of 64.
; expect: reject E5101
; phase: 0
; spec: docs/spec/08-verifier.md
module test.too_many_params
air 0
lang 0.1
features []
fn @f(%p0: u8, %p1: u8, %p2: u8, %p3: u8, %p4: u8, %p5: u8, %p6: u8, %p7: u8, %p8: u8, %p9: u8, %p10: u8, %p11: u8, %p12: u8, %p13: u8, %p14: u8, %p15: u8, %p16: u8, %p17: u8, %p18: u8, %p19: u8, %p20: u8, %p21: u8, %p22: u8, %p23: u8, %p24: u8, %p25: u8, %p26: u8, %p27: u8, %p28: u8, %p29: u8, %p30: u8, %p31: u8, %p32: u8, %p33: u8, %p34: u8, %p35: u8, %p36: u8, %p37: u8, %p38: u8, %p39: u8, %p40: u8, %p41: u8, %p42: u8, %p43: u8, %p44: u8, %p45: u8, %p46: u8, %p47: u8, %p48: u8, %p49: u8, %p50: u8, %p51: u8, %p52: u8, %p53: u8, %p54: u8, %p55: u8, %p56: u8, %p57: u8, %p58: u8, %p59: u8, %p60: u8, %p61: u8, %p62: u8, %p63: u8, %p64: u8) -> void
effects []
entry:
ret
@@ -0,0 +1,13 @@
; Branch to a label that does not exist.
; expect: reject E5002
; phase: 0
; spec: docs/spec/07-air.md
module test.unknown_block
air 0
lang 0.1
features []
fn @f() -> void
effects []
entry:
br @nowhere()
@@ -0,0 +1,14 @@
; frobnicate is not an AIR opcode; %a is then never defined.
; expect: reject E0004 E5001
; phase: 0
; spec: docs/spec/07-air.md
module test.unknown_opcode
air 0
lang 0.1
features []
fn @f() -> u8
effects []
entry:
%a = frobnicate.u8 1
ret %a
@@ -0,0 +1,8 @@
; AIR format version 7 is not implemented.
; expect: reject E0005
; phase: 0
; spec: docs/spec/07-air.md
module test.unsupported_version
air 7
lang 0.1
features []
@@ -0,0 +1,20 @@
; %x is defined on one path only but used after the join.
; expect: reject E5001
; phase: 0
; spec: docs/spec/07-air.md
module test.use_not_dominated
air 0
lang 0.1
features []
fn @f(%c: bool) -> u8
effects []
entry:
cbr %c, @a(), @b()
a:
%x = const.u8 1
br @join()
b:
br @join()
join:
ret %x
@@ -0,0 +1,21 @@
; Structs and enums are rendered in the run record.
; expect: run
; result: {"variant":"Err","payload":[{"code":404,"fatal":false}]}
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.aggregates
air 0
lang 0.1
features []
type @Error = struct { code: u16, fatal: bool }
type @Result = enum { Ok(u32), Err(@Error) }
fn @main() -> @Result
effects []
entry:
%code = const.u16 404
%fatal = const.bool false
%e = struct.make @Error(%code, %fatal)
%r = enum.make @Result.Err(%e)
ret %r
@@ -0,0 +1,17 @@
; shr on signed values is arithmetic.
; expect: run
; result: -1
; phase: 0
; spec: docs/spec/02-types-and-values.md
module test.arithmetic_shift
air 0
lang 0.1
features []
fn @main() -> i16
effects []
entry:
%a = const.i16 -32768
%b = const.i16 15
%c = shr.i16 %a, %b
ret %c
@@ -0,0 +1,16 @@
; A false assertion traps.
; expect: run
; trap: T0002
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.assert_failure
air 0
lang 0.1
features []
fn @main() -> void
effects []
entry:
%f = const.bool false
assert %f
ret
@@ -0,0 +1,21 @@
; Unbounded recursion hits the call-depth limit.
; expect: run
; trap: T0007
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.call_depth
air 0
lang 0.1
features []
fn @main() -> void
effects []
entry:
call @down()
ret
fn @down() -> void
effects []
entry:
call @down()
ret
@@ -0,0 +1,16 @@
; Checked cast of 300 to u8 traps.
; expect: run
; trap: T0006
; phase: 0
; spec: docs/spec/02-types-and-values.md
module test.checked_cast
air 0
lang 0.1
features []
fn @main() -> u8
effects []
entry:
%a = const.u32 300
%b = cast.checked.u8 %a
ret %b
@@ -0,0 +1,17 @@
; u8 200 + 56 overflows.
; expect: run
; trap: T0003
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.checked_overflow
air 0
lang 0.1
features []
fn @main() -> u8
effects []
entry:
%a = const.u8 200
%b = const.u8 56
%c = add.checked.u8 %a, %b
ret %c
@@ -0,0 +1,17 @@
; Division by zero traps.
; expect: run
; trap: T0004
; phase: 0
; spec: docs/spec/02-types-and-values.md
module test.division_by_zero
air 0
lang 0.1
features []
fn @main() -> i32
effects []
entry:
%a = const.i32 1
%b = const.i32 0
%c = div.checked.i32 %a, %b
ret %c
@@ -0,0 +1,14 @@
; The trap instruction traps.
; expect: run
; trap: T0001
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.explicit_trap
air 0
lang 0.1
features []
fn @main() -> void
effects []
entry:
trap
@@ -0,0 +1,27 @@
; Iterative Fibonacci with loop-carried block parameters.
; expect: run
; result: 2880067194370816120
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.fibonacci
air 0
lang 0.1
features []
fn @main() -> u64
effects []
entry:
%n = const.u64 90
%z = const.u64 0
%o = const.u64 1
br @loop(%z, %z, %o)
loop(%i: u64, %a: u64, %b: u64):
%d = ge.u64 %i, %n
cbr %d, @done(), @step()
step:
%c = add.checked.u64 %a, %b
%one = const.u64 1
%j = add.checked.u64 %i, %one
br @loop(%j, %b, %c)
done:
ret %a
+15
View File
@@ -0,0 +1,15 @@
; Phase 0 hello: @main returns a value that the run record reports (Q-008).
; expect: run
; result: 42
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.hello
air 0
lang 0.1
features []
fn @main() -> u32
effects []
entry:
%v = const.u32 42
ret %v
@@ -0,0 +1,17 @@
; Shift by the bit width traps.
; expect: run
; trap: T0005
; phase: 0
; spec: docs/spec/02-types-and-values.md
module test.shift_out_of_range
air 0
lang 0.1
features []
fn @main() -> u32
effects []
entry:
%a = const.u32 1
%b = const.u32 32
%c = shl.u32 %a, %b
ret %c
@@ -0,0 +1,17 @@
; i64 MIN / -1 traps as overflow.
; expect: run
; trap: T0003
; phase: 0
; spec: docs/spec/02-types-and-values.md
module test.signed_min_div
air 0
lang 0.1
features []
fn @main() -> i64
effects []
entry:
%a = const.i64 -9223372036854775808
%b = const.i64 -1
%c = div.checked.i64 %a, %b
ret %c
@@ -0,0 +1,17 @@
; Remainder has the sign of the dividend.
; expect: run
; result: -1
; phase: 0
; spec: docs/spec/02-types-and-values.md
module test.truncating_remainder
air 0
lang 0.1
features []
fn @main() -> i32
effects []
entry:
%a = const.i32 -7
%b = const.i32 3
%c = rem.checked.i32 %a, %b
ret %c
@@ -0,0 +1,17 @@
; Wrapping add does not trap.
; expect: run
; result: 0
; phase: 0
; spec: docs/spec/07a-air-instructions.md
module test.wrapping_add
air 0
lang 0.1
features []
fn @main() -> u8
effects []
entry:
%a = const.u8 200
%b = const.u8 56
%c = add.wrap.u8 %a, %b
ret %c
@@ -0,0 +1,16 @@
; Wrapping cast keeps the low bits of the two's-complement value.
; expect: run
; result: 254
; phase: 0
; spec: docs/spec/02-types-and-values.md
module test.wrapping_cast
air 0
lang 0.1
features []
fn @main() -> u8
effects []
entry:
%a = const.i32 -2
%b = cast.wrap.u8 %a
ret %b