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:
1 parent
ef6a2c5475
commit
50bf990355
14 files changed
+892
-11
No files matched your search
+98
-5
@@ -1,8 +1,101 @@
|
||||
//! AIR verifier (spec 08). Part of the language semantics, not a lint.
|
||||
//!
|
||||
//! Passes are added in plan order (docs/plan/README.md). Each normative rejection MUST map to a
|
||||
//! code in `diag.registry` and MUST have positive and negative conformance
|
||||
//! tests under tests/conformance/.
|
||||
//! Passes are added in plan order (docs/plan/README.md). Each normative
|
||||
//! rejection maps to a code in `diag.registry` and has accept and reject
|
||||
//! conformance tests under tests/conformance/.
|
||||
//!
|
||||
//! Phase 0: SSA/dominance (E5001), CFG/terminators (E5002), types (E1001),
|
||||
//! deterministic resource budget (E5101).
|
||||
//! Stages run in the order of spec 07 §8.6 and verification stops after the
|
||||
//! first stage that reports an error, so later stages may assume the
|
||||
//! invariants established by earlier ones.
|
||||
|
||||
const std = @import("std");
|
||||
const air = @import("air.zig");
|
||||
const diag = @import("diag.zig");
|
||||
|
||||
pub const budget = @import("verifier/budget.zig");
|
||||
pub const cfg = @import("verifier/cfg.zig");
|
||||
pub const ssa = @import("verifier/ssa.zig");
|
||||
pub const types = @import("verifier/types.zig");
|
||||
|
||||
/// Shared state for one verification run.
|
||||
pub const Context = struct {
|
||||
arena: std.mem.Allocator,
|
||||
file: []const u8,
|
||||
module: air.Module,
|
||||
diags: std.ArrayList(diag.Diagnostic) = .empty,
|
||||
|
||||
pub fn report(
|
||||
c: *Context,
|
||||
id: []const u8,
|
||||
span: air.Span,
|
||||
message: []const u8,
|
||||
fields: []const diag.Field,
|
||||
) std.mem.Allocator.Error!void {
|
||||
try c.diags.append(c.arena, .{
|
||||
.code = diag.lookup(id).?,
|
||||
.span = .{ .file = c.file, .line = span.line, .col = span.col },
|
||||
.message = message,
|
||||
.fields = try c.arena.dupe(diag.Field, fields),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn valueName(c: *Context, f: air.Function, v: air.ValueId) std.mem.Allocator.Error![]const u8 {
|
||||
return std.fmt.allocPrint(c.arena, "%{s}", .{f.values[v].name});
|
||||
}
|
||||
|
||||
pub fn typeName(c: *Context, t: air.Type) std.mem.Allocator.Error![]const u8 {
|
||||
var w: std.Io.Writer.Allocating = .init(c.arena);
|
||||
air.print.printType(c.module, t, &w.writer) catch return error.OutOfMemory;
|
||||
return w.written();
|
||||
}
|
||||
};
|
||||
|
||||
/// Runs all verifier stages. Returns the diagnostics of the first failing
|
||||
/// stage, or an empty slice if the module verifies.
|
||||
pub fn verify(arena: std.mem.Allocator, file: []const u8, module: air.Module) std.mem.Allocator.Error![]const diag.Diagnostic {
|
||||
var c: Context = .{ .arena = arena, .file = file, .module = module };
|
||||
const stages = .{ budget.check, cfg.check, ssa.check, types.check };
|
||||
inline for (stages) |stage| {
|
||||
try stage(&c);
|
||||
if (c.diags.items.len != 0) {
|
||||
std.mem.sort(diag.Diagnostic, c.diags.items, {}, bySpan);
|
||||
return c.diags.items;
|
||||
}
|
||||
}
|
||||
return &.{};
|
||||
}
|
||||
|
||||
fn bySpan(_: void, a: diag.Diagnostic, b: diag.Diagnostic) bool {
|
||||
const sa = a.span.?;
|
||||
const sb = b.span.?;
|
||||
if (sa.line != sb.line) return sa.line < sb.line;
|
||||
if (sa.col != sb.col) return sa.col < sb.col;
|
||||
return std.mem.lessThan(u8, a.code.id, b.code.id);
|
||||
}
|
||||
|
||||
test {
|
||||
_ = budget;
|
||||
_ = cfg;
|
||||
_ = ssa;
|
||||
_ = types;
|
||||
}
|
||||
|
||||
/// Test helper: parses `src` and verifies it, expecting exactly `codes`.
|
||||
pub fn expectCodes(src: []const u8, codes: []const []const u8) !void {
|
||||
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena_state.deinit();
|
||||
const arena = arena_state.allocator();
|
||||
const r = try air.parse.parse(arena, "t.air", src);
|
||||
if (r.diagnostics.len != 0) {
|
||||
for (r.diagnostics) |d| std.debug.print("parse: {s} {s}\n", .{ d.code.id, d.message });
|
||||
return error.ParseFailed;
|
||||
}
|
||||
const ds = try verify(arena, "t.air", r.module.?);
|
||||
if (ds.len != codes.len) {
|
||||
for (ds) |d| std.debug.print("got {s} {s} at {d}:{d}\n", .{ d.code.id, d.message, d.span.?.line, d.span.?.col });
|
||||
return error.TestExpectedEqual;
|
||||
}
|
||||
for (codes, ds) |c, d| try std.testing.expectEqualStrings(c, d.code.id);
|
||||
}
|
||||
|
||||
pub const test_header = "module t\nair 0\nlang 0.1\nfeatures []\n";
|
||||
Reference in new issue
Block a user