verifier: resource limits, CFG, SSA dominance and types (P0-7..P0-10)

- verifier.zig: staged pipeline per spec 07 §8.6, deterministic order
- budget.zig: fixed limits from spec 08 §7 (E5101)
- cfg.zig: terminators, branch arity, switch/enum.switch shape (E5002)
- ssa.zig: Cooper-Harvey-Kennedy dominators, use dominance (E5001)
- types.zig: operand/result types, casts, calls, returns, recursive
  aggregates (E1001, E1002, E1003)
- check.zig runs parse -> verify; seed conformance tests get module
  headers; tests for unimplemented phases report PENDING
This commit is contained in:
dev committed 2026-10-04 12:24:41 +03:00
1 parent ef6a2c5475
commit 50bf990355
14 files changed
+892 -11

No files matched your search

+319
View File
@@ -0,0 +1,319 @@
//! Type verification for format version 0 (spec 07 §8.3, spec 07a).
//! Reports E1001, E1002 and E1003.
//!
//! Runs after CFG and SSA verification, so block shapes and branch arities
//! are known to be valid and every use has a dominating definition.
const std = @import("std");
const air = @import("../air.zig");
const verifier = @import("../verifier.zig");
const Context = verifier.Context;
const Type = air.Type;
pub fn check(c: *Context) !void {
try checkDeclarations(c);
if (c.diags.items.len != 0) return;
for (c.module.functions) |f| try checkFunction(c, f);
}
// ---------------------------------------------------------------- declarations
fn checkDeclarations(c: *Context) !void {
const m = c.module;
for (m.types) |d| switch (d.kind) {
.@"struct" => |fs| for (fs) |fld| try valueType(c, d.span, fld.ty, d.name),
.@"enum" => |vs| for (vs) |v| for (v.payload) |t| try valueType(c, d.span, t, d.name),
};
for (m.functions) |f| {
for (f.params) |p| try valueType(c, f.span, p.ty, f.name);
for (f.blocks) |b| for (b.params) |p| try valueType(c, b.span, p.ty, f.name);
}
// E1003: an aggregate that contains itself by value has no finite size.
const state = try c.arena.alloc(enum { white, grey, black }, m.types.len);
@memset(state, .white);
for (0..m.types.len) |i| try visitDecl(c, state, @intCast(i));
}
fn visitDecl(c: *Context, state: anytype, i: u32) !void {
if (state[i] == .black) return;
if (state[i] == .grey) {
const d = c.module.types[i];
try c.report("E1003", d.span, "aggregate type contains itself by value", &.{.{ .name = "type", .value = .{ .str = d.name } }});
state[i] = .black;
return;
}
state[i] = .grey;
switch (c.module.types[i].kind) {
.@"struct" => |fs| for (fs) |f| if (f.ty == .named) try visitDecl(c, state, f.ty.named),
.@"enum" => |vs| for (vs) |v| for (v.payload) |t| if (t == .named) try visitDecl(c, state, t.named),
}
state[i] = .black;
}
/// `void` and `never` are only valid as function return types (spec 07 §8.3).
fn valueType(c: *Context, span: air.Span, t: Type, owner: []const u8) !void {
if (t != .void and t != .never) return;
try c.report("E1001", span, "`void` and `never` are only valid as function return types", &.{
.{ .name = "in", .value = .{ .str = owner } },
.{ .name = "found", .value = .{ .str = @tagName(t) } },
});
}
// ---------------------------------------------------------------- functions
const FnCtx = struct {
c: *Context,
f: air.Function,
/// Type of each value; null while unresolved.
ty: []?Type,
block: u32 = 0,
index: u32 = 0,
fn typeOf(fc: *FnCtx, v: air.ValueId) Type {
return fc.ty[v].?;
}
fn mismatch(fc: *FnCtx, span: air.Span, what: []const u8, expected: Type, found: Type) !void {
try fc.c.report("E1001", span, "type mismatch", &.{
.{ .name = "function", .value = .{ .str = fc.f.name } },
.{ .name = "block", .value = .{ .str = fc.f.blocks[fc.block].name } },
.{ .name = "inst", .value = .{ .int = fc.index } },
.{ .name = "operand", .value = .{ .str = what } },
.{ .name = "expected", .value = .{ .str = try fc.c.typeName(expected) } },
.{ .name = "found", .value = .{ .str = try fc.c.typeName(found) } },
});
}
fn expectValue(fc: *FnCtx, span: air.Span, v: air.ValueId, expected: Type) !void {
const found = fc.typeOf(v);
if (!found.eql(expected)) try fc.mismatch(span, try fc.c.valueName(fc.f, v), expected, found);
}
fn expectArgs(fc: *FnCtx, span: air.Span, args: []const air.ValueId, params: []const Type) !void {
if (args.len != params.len) {
try fc.c.report("E1001", span, "wrong number of operands", &.{
.{ .name = "function", .value = .{ .str = fc.f.name } },
.{ .name = "expected", .value = .{ .int = @intCast(params.len) } },
.{ .name = "found", .value = .{ .int = @intCast(args.len) } },
});
return;
}
for (args, params) |a, p| try fc.expectValue(span, a, p);
}
fn expectTarget(fc: *FnCtx, span: air.Span, t: air.Target) !void {
const params = fc.f.blocks[t.block].params;
for (t.args, params) |a, p| try fc.expectValue(span, a, p.ty);
}
};
/// Result type of a value-producing instruction. `copy` is resolved by the caller.
fn resultType(m: air.Module, op: air.Op) ?Type {
return switch (op) {
.@"const" => |x| x.ty,
.arith => |x| .{ .int = x.ty },
.bit => |x| x.ty,
.cmp => .bool,
.cast => |x| .{ .int = x.to },
.struct_make => |x| .{ .named = x.decl },
.struct_get => |x| m.types[x.decl].kind.@"struct"[x.field].ty,
.enum_make => |x| .{ .named = x.decl },
.call => |x| m.functions[x.callee].ret,
else => null,
};
}
fn checkFunction(c: *Context, f: air.Function) !void {
const m = c.module;
var fc: FnCtx = .{ .c = c, .f = f, .ty = try c.arena.alloc(?Type, f.values.len) };
@memset(fc.ty, null);
// Seed types from declarations and opcodes, then resolve `copy` chains.
for (f.params) |p| fc.ty[p.value] = p.ty;
for (f.blocks) |b| {
for (b.params) |p| fc.ty[p.value] = p.ty;
for (b.insts) |inst| if (inst.result) |r| {
fc.ty[r] = resultType(m, inst.op);
};
}
var progress = true;
while (progress) {
progress = false;
for (f.blocks) |b| for (b.insts) |inst| {
const r = inst.result orelse continue;
if (fc.ty[r] != null or inst.op != .copy) continue;
if (fc.ty[inst.op.copy]) |t| {
fc.ty[r] = t;
progress = true;
}
};
}
// A `copy` cycle can only occur in unreachable code; give it a type that
// cannot satisfy any later check rather than leaving it unresolved.
for (fc.ty) |*t| if (t.* == null) {
t.* = .void;
};
for (f.blocks, 0..) |b, bi| {
fc.block = @intCast(bi);
for (b.insts, 0..) |inst, ii| {
fc.index = @intCast(ii);
try checkInst(&fc, inst);
}
}
}
fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
const m = fc.c.module;
const f = fc.f;
const span = inst.span;
switch (inst.op) {
.@"const", .copy, .trap => {},
.arith => |x| {
try fc.expectValue(span, x.a, .{ .int = x.ty });
try fc.expectValue(span, x.b, .{ .int = x.ty });
},
.bit => |x| {
try fc.expectValue(span, x.a, x.ty);
try fc.expectValue(span, x.b, x.ty);
},
.cmp => |x| {
try fc.expectValue(span, x.a, x.ty);
try fc.expectValue(span, x.b, x.ty);
},
.assert => |v| try fc.expectValue(span, v, .bool),
.cast => |x| {
const from = fc.typeOf(x.a);
if (from != .int) {
try invalidCast(fc, span, from, x.to, "operand is not an integer");
} else if (x.mode == .widen and !lossless(from.int, x.to)) {
try invalidCast(fc, span, from, x.to, "widen is not lossless for every value");
}
},
.struct_make => |x| {
const fields = m.types[x.decl].kind.@"struct";
const tys = try fc.c.arena.alloc(Type, fields.len);
for (fields, tys) |fld, *t| t.* = fld.ty;
try fc.expectArgs(span, x.args, tys);
},
.struct_get => |x| try fc.expectValue(span, x.a, .{ .named = x.decl }),
.enum_make => |x| try fc.expectArgs(span, x.args, m.types[x.decl].kind.@"enum"[x.variant].payload),
.call => |x| {
const callee = m.functions[x.callee];
const tys = try fc.c.arena.alloc(Type, callee.params.len);
for (callee.params, tys) |p, *t| t.* = p.ty;
try fc.expectArgs(span, x.args, tys);
if (inst.result != null and callee.ret == .void) {
try fc.c.report("E1001", span, "call to a void function must not bind a result", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "callee", .value = .{ .str = callee.name } },
});
} else if (inst.result == null and callee.ret != .void and callee.ret != .never) {
try fc.c.report("E1001", span, "call result must be bound", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "callee", .value = .{ .str = callee.name } },
});
}
},
.br => |t| try fc.expectTarget(span, t),
.cbr => |x| {
try fc.expectValue(span, x.cond, .bool);
try fc.expectTarget(span, x.then);
try fc.expectTarget(span, x.@"else");
},
.@"switch" => |x| {
try fc.expectValue(span, x.value, .{ .int = x.ty });
for (x.cases) |case| try fc.expectTarget(case.span, case.target);
try fc.expectTarget(span, x.default);
},
.enum_switch => |x| {
try fc.expectValue(span, x.value, .{ .named = x.decl });
const variants = m.types[x.decl].kind.@"enum";
for (x.arms) |arm| {
const params = f.blocks[arm.block].params;
for (params, variants[arm.variant].payload) |p, t| {
if (!p.ty.eql(t)) try fc.mismatch(arm.span, try fc.c.valueName(f, p.value), t, p.ty);
}
}
if (x.default) |d| try fc.expectTarget(span, d);
},
.ret => |v| switch (f.ret) {
.never => try fc.c.report("E1001", span, "`ret` in a function returning never", &.{
.{ .name = "function", .value = .{ .str = f.name } },
}),
.void => if (v) |x| try fc.mismatch(span, try fc.c.valueName(f, x), .void, fc.typeOf(x)),
else => if (v) |x| {
try fc.expectValue(span, x, f.ret);
} else try fc.mismatch(span, "return value", f.ret, .void),
},
}
}
/// Every value of `from` is representable in `to` (spec 02 §2.1).
pub fn lossless(from: air.IntType, to: air.IntType) bool {
if (from.pointerSized() or to.pointerSized()) return false;
if (from.signed() and !to.signed()) return false;
if (!from.signed() and to.signed()) return to.bits() > from.bits();
return to.bits() >= from.bits();
}
fn invalidCast(fc: *FnCtx, span: air.Span, from: Type, to: air.IntType, message: []const u8) !void {
try fc.c.report("E1002", span, message, &.{
.{ .name = "function", .value = .{ .str = fc.f.name } },
.{ .name = "from", .value = .{ .str = try fc.c.typeName(from) } },
.{ .name = "to", .value = .{ .str = @tagName(to) } },
});
}
// ---------------------------------------------------------------- tests
const h = verifier.test_header;
inline fn body(comptime sig: []const u8, comptime insts: []const u8) []const u8 {
return comptime h ++ "fn @f" ++ sig ++ "\neffects []\nentry:\n" ++ insts;
}
test "arithmetic operand types" {
try verifier.expectCodes(body("(%a: u64, %b: u64) -> u64", " %c = add.checked.u64 %a, %b\n ret %c\n"), &.{});
try verifier.expectCodes(body("(%a: u64, %b: u32) -> u64", " %c = add.checked.u64 %a, %b\n ret %c\n"), &.{"E1001"});
try verifier.expectCodes(body("(%a: u64) -> u32", " ret %a\n"), &.{"E1001"});
try verifier.expectCodes(body("(%a: usize, %b: u64) -> bool", " %c = eq.u64 %a, %b\n ret %c\n"), &.{"E1001"});
try verifier.expectCodes(body("(%a: u8) -> void", " cbr %a, @x(), @x()\nx:\n ret\n"), &.{"E1001"});
try verifier.expectCodes(body("(%a: u8) -> void", " assert %a\n ret\n"), &.{"E1001"});
}
test "casts" {
try verifier.expectCodes(body("(%a: u8) -> i16", " %b = cast.widen.i16 %a\n ret %b\n"), &.{});
try verifier.expectCodes(body("(%a: u8) -> i8", " %b = cast.widen.i8 %a\n ret %b\n"), &.{"E1002"});
try verifier.expectCodes(body("(%a: i8) -> u64", " %b = cast.widen.u64 %a\n ret %b\n"), &.{"E1002"});
try verifier.expectCodes(body("(%a: u32) -> usize", " %b = cast.widen.usize %a\n ret %b\n"), &.{"E1002"});
try verifier.expectCodes(body("(%a: u32) -> usize", " %b = cast.checked.usize %a\n ret %b\n"), &.{});
try verifier.expectCodes(body("(%a: bool) -> u8", " %b = cast.wrap.u8 %a\n ret %b\n"), &.{"E1002"});
}
test "returns and calls" {
try verifier.expectCodes(body("() -> void", " ret\n"), &.{});
try verifier.expectCodes(body("() -> u8", " ret\n"), &.{"E1001"});
try verifier.expectCodes(body("() -> never", " ret\n"), &.{"E1001"});
try verifier.expectCodes(body("() -> never", " trap\n"), &.{});
const g = "fn @g(%x: u8) -> void\neffects []\nentry:\n ret\n";
try verifier.expectCodes(body("(%a: u8) -> void", " call @g(%a)\n ret\n") ++ g, &.{});
try verifier.expectCodes(body("(%a: u8) -> void", " %r = call @g(%a)\n ret\n") ++ g, &.{"E1001"});
try verifier.expectCodes(body("(%a: u16) -> void", " call @g(%a)\n ret\n") ++ g, &.{"E1001"});
try verifier.expectCodes(body("(%a: u8) -> void", " call @g(%a, %a)\n ret\n") ++ g, &.{"E1001"});
const k = "fn @k() -> u8\neffects []\nentry:\n %z = const.u8 0\n ret %z\n";
try verifier.expectCodes(body("() -> void", " call @k()\n ret\n") ++ k, &.{"E1001"});
}
test "aggregates and declarations" {
const s = h ++ "type @P = struct { x: u8, y: bool }\ntype @E = enum { A, B(u8) }\n";
try verifier.expectCodes(s ++ "fn @f(%a: u8, %t: bool) -> u8\neffects []\nentry:\n %p = struct.make @P(%a, %t)\n %x = struct.get @P.x, %p\n ret %x\n", &.{});
try verifier.expectCodes(s ++ "fn @f(%a: u8) -> void\neffects []\nentry:\n %p = struct.make @P(%a, %a)\n ret\n", &.{"E1001"});
try verifier.expectCodes(s ++ "fn @f(%a: u8) -> void\neffects []\nentry:\n %p = struct.make @P(%a)\n ret\n", &.{"E1001"});
try verifier.expectCodes(s ++ "fn @f(%e: @E) -> void\neffects []\nentry:\n enum.switch @E %e, A -> @x, B -> @y\nx:\n ret\ny(%v: u16):\n ret\n", &.{"E1001"});
try verifier.expectCodes(h ++ "type @R = struct { next: @R }\nfn @f() -> void\neffects []\nentry:\n ret\n", &.{"E1003"});
try verifier.expectCodes(h ++ "type @A = struct { b: @B }\ntype @B = enum { X(@A) }\nfn @f() -> void\neffects []\nentry:\n ret\n", &.{"E1003"});
try verifier.expectCodes(h ++ "fn @f(%v: void) -> void\neffects []\nentry:\n ret\n", &.{"E1001"});
}