Phase 1: frame storage, second-class borrows, arrays (P1-3..P1-10)
- air: types local/ref/mut/array/slice/slice_mut, from-returns, multi-result binding, 18 new instructions; feature gating reports E0006 feature_not_declared; borrow-typed block parameters gated by borrow_joins; printer and round-trip tests for all new forms - verifier: second-class and zeroable type rules (E1001); local.alloc only in entry (E5002); frame size accounting (E5101); stages init (E1010), borrow (E2101, E2102, E2105-E2108) and bounds facts (E2103); value types shared between stages - interp: frame cells, places, slices, deep copies between storage and SSA values, T0008 bounds traps - features arrays and ownership supported; implemented_phase = 1 - conformance: 5 accept, 18 reject, 11 run tests for Phase 1, including the file-copy core exit program; 6000 mutants without a crash
This commit is contained in:
1 parent
be9dd0e1ed
commit
1ae6b485d8
50 files changed
+2376
-37
No files matched your search
+209
-16
@@ -14,7 +14,9 @@ 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);
|
||||
const all = try c.arena.alloc([]const Type, c.module.functions.len);
|
||||
for (c.module.functions, all) |f, *out| out.* = try checkFunction(c, f);
|
||||
c.value_types = all;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- declarations
|
||||
@@ -22,12 +24,30 @@ pub fn check(c: *Context) !void {
|
||||
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),
|
||||
.@"struct" => |fs| for (fs) |fld| try wellFormed(c, d.span, fld.ty, .member, d.name),
|
||||
.@"enum" => |vs| for (vs) |v| for (v.payload) |t| try wellFormed(c, d.span, t, .member, 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);
|
||||
for (f.params) |p| try wellFormed(c, f.span, p.ty, .param, f.name);
|
||||
try wellFormed(c, f.span, f.ret, .ret, f.name);
|
||||
if (f.ret_from) |i| {
|
||||
const p = f.params[i].ty;
|
||||
if (!f.ret.isBorrow() or !p.isBorrow() or (f.ret.isMutBorrow() and !p.isMutBorrow())) {
|
||||
try c.report("E1001", f.span, "`from` requires a borrow return and a borrow parameter whose mode allows the result mode", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "ret", .value = .{ .str = try c.typeName(f.ret) } },
|
||||
.{ .name = "from", .value = .{ .str = try c.typeName(p) } },
|
||||
});
|
||||
}
|
||||
}
|
||||
for (f.blocks) |b| {
|
||||
for (b.params) |p| try wellFormed(c, b.span, p.ty, .block_param, f.name);
|
||||
for (b.insts) |inst| switch (inst.op) {
|
||||
.local_alloc => |t| try wellFormed(c, inst.span, t, .member, f.name),
|
||||
.array_splat => |x| try wellFormed(c, inst.span, x.ty, .member, f.name),
|
||||
else => {},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// E1003: an aggregate that contains itself by value has no finite size.
|
||||
@@ -52,13 +72,42 @@ fn visitDecl(c: *Context, state: anytype, i: u32) !void {
|
||||
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", &.{
|
||||
const Position = enum { member, param, ret, block_param };
|
||||
|
||||
/// Where a type may appear (spec 07 §8.3, §8.8):
|
||||
/// - `void`/`never` only as return types;
|
||||
/// - storage (`local<T>`) only as the result of `local.alloc`;
|
||||
/// - borrows only as parameters, returns and (gated) block parameters,
|
||||
/// never as members (fields, payloads, elements, locals);
|
||||
/// - the inner type of every compound type is a value type.
|
||||
fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []const u8) !void {
|
||||
const ok = switch (t) {
|
||||
.void, .never => pos == .ret,
|
||||
.local => false,
|
||||
.ref, .mut, .slice, .slice_mut => pos != .member,
|
||||
else => true,
|
||||
};
|
||||
if (!ok) try c.report("E1001", span, "type not allowed in this position", &.{
|
||||
.{ .name = "in", .value = .{ .str = owner } },
|
||||
.{ .name = "found", .value = .{ .str = @tagName(t) } },
|
||||
.{ .name = "position", .value = .{ .str = @tagName(pos) } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t) } },
|
||||
});
|
||||
if (t.inner()) |inner| try wellFormed(c, span, inner, .member, owner);
|
||||
}
|
||||
|
||||
/// `local.alloc` results are checked separately: their own type is storage.
|
||||
fn zeroable(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool => true,
|
||||
.array => |a| zeroable(m, a.elem),
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!zeroable(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => false,
|
||||
},
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- functions
|
||||
@@ -86,6 +135,17 @@ const FnCtx = struct {
|
||||
});
|
||||
}
|
||||
|
||||
fn kindError(fc: *FnCtx, span: air.Span, v: air.ValueId, expected: []const u8) !void {
|
||||
try fc.c.report("E1001", span, "operand has the wrong kind of type", &.{
|
||||
.{ .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 = try fc.c.valueName(fc.f, v) } },
|
||||
.{ .name = "expected", .value = .{ .str = expected } },
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(fc.typeOf(v)) } },
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -121,16 +181,84 @@ fn resultType(m: air.Module, op: air.Op) ?Type {
|
||||
.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,
|
||||
.slice_len => .{ .int = .usize },
|
||||
.array_splat => |x| x.ty,
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
fn checkFunction(c: *Context, f: air.Function) !void {
|
||||
fn boxed(arena: std.mem.Allocator, t: Type) !*const Type {
|
||||
const b = try arena.create(Type);
|
||||
b.* = t;
|
||||
return b;
|
||||
}
|
||||
|
||||
/// Result type of an instruction whose result depends on operand types.
|
||||
/// Returns null while an operand is unresolved, and `.void` (which no later
|
||||
/// check accepts) if the operand has the wrong kind; `checkInst` reports that.
|
||||
fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
const a = fc.c.arena;
|
||||
const m = fc.c.module;
|
||||
const t = switch (op) {
|
||||
.copy => |v| return fc.ty[v],
|
||||
.local_alloc => |inner| return .{ .local = try boxed(a, inner) },
|
||||
.local_load => |v| fc.ty[v] orelse return null,
|
||||
.borrow_ref, .borrow_mut, .load, .slice_make => |v| fc.ty[v] orelse return null,
|
||||
.borrow_field => |x| fc.ty[x.borrow] orelse return null,
|
||||
.borrow_index => |x| fc.ty[x.borrow] orelse return null,
|
||||
.slice_sub => |x| fc.ty[x.slice] orelse return null,
|
||||
.slice_split => |x| fc.ty[x.slice] orelse return null,
|
||||
else => return null,
|
||||
};
|
||||
const invalid: Type = .void;
|
||||
return switch (op) {
|
||||
.local_load => if (t == .local) t.local.* else invalid,
|
||||
.borrow_ref => switch (t) {
|
||||
.local, .ref, .mut => .{ .ref = try boxed(a, t.inner().?) },
|
||||
.slice, .slice_mut => .{ .slice = try boxed(a, t.inner().?) },
|
||||
else => invalid,
|
||||
},
|
||||
.borrow_mut => switch (t) {
|
||||
.local, .mut => .{ .mut = try boxed(a, t.inner().?) },
|
||||
.slice_mut => .{ .slice_mut = try boxed(a, t.inner().?) },
|
||||
else => invalid,
|
||||
},
|
||||
.borrow_field => |x| switch (t) {
|
||||
.ref, .mut => |inner| if (inner.* == .named and inner.named == x.decl) blk: {
|
||||
const fty = try boxed(a, m.types[x.decl].kind.@"struct"[x.field].ty);
|
||||
break :blk if (t == .ref) Type{ .ref = fty } else Type{ .mut = fty };
|
||||
} else invalid,
|
||||
else => invalid,
|
||||
},
|
||||
.load => if (t == .ref or t == .mut) t.inner().? else invalid,
|
||||
.borrow_index => switch (t) {
|
||||
.ref, .mut => |inner| if (inner.* == .array) blk: {
|
||||
const e = try boxed(a, inner.array.elem);
|
||||
break :blk if (t == .ref) Type{ .ref = e } else Type{ .mut = e };
|
||||
} else invalid,
|
||||
.slice => |e| .{ .ref = e },
|
||||
.slice_mut => |e| .{ .mut = e },
|
||||
else => invalid,
|
||||
},
|
||||
.slice_make => switch (t) {
|
||||
.ref, .mut => |inner| if (inner.* == .array) blk: {
|
||||
const e = try boxed(a, inner.array.elem);
|
||||
break :blk if (t == .ref) Type{ .slice = e } else Type{ .slice_mut = e };
|
||||
} else invalid,
|
||||
else => invalid,
|
||||
},
|
||||
.slice_sub, .slice_split => if (t.isSlice()) t else invalid,
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
fn checkFunction(c: *Context, f: air.Function) ![]const Type {
|
||||
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.
|
||||
// Seed types from declarations and opcodes, then resolve types that
|
||||
// depend on operand types (`copy`, borrows, slices) to a fixpoint.
|
||||
for (f.params) |p| fc.ty[p.value] = p.ty;
|
||||
for (f.blocks) |b| {
|
||||
for (b.params) |p| fc.ty[p.value] = p.ty;
|
||||
@@ -143,14 +271,15 @@ fn checkFunction(c: *Context, f: air.Function) !void {
|
||||
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| {
|
||||
if (fc.ty[r] != null) continue;
|
||||
if (try derivedType(&fc, inst.op)) |t| {
|
||||
fc.ty[r] = t;
|
||||
if (inst.result2) |r2| fc.ty[r2] = t;
|
||||
progress = true;
|
||||
}
|
||||
};
|
||||
}
|
||||
// A `copy` cycle can only occur in unreachable code; give it a type that
|
||||
// A 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;
|
||||
@@ -163,6 +292,9 @@ fn checkFunction(c: *Context, f: air.Function) !void {
|
||||
try checkInst(&fc, inst);
|
||||
}
|
||||
}
|
||||
const out = try c.arena.alloc(Type, f.values.len);
|
||||
for (fc.ty, out) |t, *o| o.* = t.?;
|
||||
return out;
|
||||
}
|
||||
|
||||
fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
@@ -170,7 +302,64 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
const f = fc.f;
|
||||
const span = inst.span;
|
||||
switch (inst.op) {
|
||||
.@"const", .copy, .trap => {},
|
||||
.@"const", .trap => {},
|
||||
.copy => |v| if (!fc.typeOf(v).isValue()) try fc.c.report("E1001", span, "only value types can be copied", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(fc.typeOf(v)) } },
|
||||
}),
|
||||
.local_alloc, .slice_len => {},
|
||||
.local_store => |x| {
|
||||
const t = fc.typeOf(x.local);
|
||||
if (t != .local) return fc.kindError(span, x.local, "local<T>");
|
||||
try fc.expectValue(span, x.value, t.local.*);
|
||||
},
|
||||
.local_zero => |v| {
|
||||
const t = fc.typeOf(v);
|
||||
if (t != .local) return fc.kindError(span, v, "local<T>");
|
||||
if (!zeroable(m, t.local.*)) try fc.c.report("E1001", span, "type is not zeroable", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(t.local.*) } },
|
||||
});
|
||||
},
|
||||
.local_load => |v| if (fc.typeOf(v) != .local) try fc.kindError(span, v, "local<T>"),
|
||||
.borrow_ref => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "local<T>, ref<T>, mut<T>, slice<T> or slice_mut<T>"),
|
||||
.borrow_mut => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "local<T>, mut<T> or slice_mut<T>"),
|
||||
.borrow_field => |x| if (resultOf(fc, inst) == .void) try fc.kindError(span, x.borrow, "ref or mut of the named struct"),
|
||||
.borrow_end => |v| if (!fc.typeOf(v).isBorrow()) try fc.kindError(span, v, "a borrow"),
|
||||
.load => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<T> or mut<T>"),
|
||||
.store => |x| {
|
||||
const t = fc.typeOf(x.borrow);
|
||||
if (t != .mut) return fc.kindError(span, x.borrow, "mut<T>");
|
||||
try fc.expectValue(span, x.value, t.mut.*);
|
||||
},
|
||||
.array_splat => |x| {
|
||||
if (x.ty != .array) return fc.c.report("E1001", span, "array.splat needs an array type", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(x.ty) } },
|
||||
});
|
||||
try fc.expectValue(span, x.value, x.ty.array.elem);
|
||||
},
|
||||
.borrow_index => |x| {
|
||||
if (resultOf(fc, inst) == .void) return fc.kindError(span, x.borrow, "a borrow of an array, or a slice");
|
||||
try fc.expectValue(span, x.index, .{ .int = .usize });
|
||||
},
|
||||
.slice_make => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref or mut of an array"),
|
||||
.slice_sub => |x| {
|
||||
if (!fc.typeOf(x.slice).isSlice()) return fc.kindError(span, x.slice, "a slice");
|
||||
try fc.expectValue(span, x.start, .{ .int = .usize });
|
||||
try fc.expectValue(span, x.len, .{ .int = .usize });
|
||||
},
|
||||
.slice_split => |x| {
|
||||
if (!fc.typeOf(x.slice).isSlice()) return fc.kindError(span, x.slice, "a slice");
|
||||
try fc.expectValue(span, x.at, .{ .int = .usize });
|
||||
},
|
||||
.slice_copy => |x| {
|
||||
const d = fc.typeOf(x.dst);
|
||||
if (d != .slice_mut) return fc.kindError(span, x.dst, "slice_mut<T>");
|
||||
const s = fc.typeOf(x.src);
|
||||
if (!s.isSlice() or !s.inner().?.eql(d.inner().?)) return fc.kindError(span, x.src, try std.fmt.allocPrint(fc.c.arena, "slice<{s}> or slice_mut<{s}>", .{ try fc.c.typeName(d.inner().?), try fc.c.typeName(d.inner().?) }));
|
||||
},
|
||||
.guard => |v| try fc.expectValue(span, v, .bool),
|
||||
.arith => |x| {
|
||||
try fc.expectValue(span, x.a, .{ .int = x.ty });
|
||||
try fc.expectValue(span, x.b, .{ .int = x.ty });
|
||||
@@ -251,6 +440,10 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
}
|
||||
}
|
||||
|
||||
fn resultOf(fc: *FnCtx, inst: air.Inst) Type {
|
||||
return fc.typeOf(inst.result.?);
|
||||
}
|
||||
|
||||
/// 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;
|
||||
|
||||
Reference in new issue
Block a user