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
+178
-3
@@ -20,13 +20,29 @@ pub const Value = union(enum) {
|
||||
boolean: bool,
|
||||
void,
|
||||
aggregate: Aggregate,
|
||||
array: []Value,
|
||||
/// Frame storage (`local<T>`).
|
||||
local: *Cell,
|
||||
/// `ref<T>` / `mut<T>`: a place inside frame storage.
|
||||
place: Place,
|
||||
/// `slice<T>` / `slice_mut<T>`.
|
||||
slice: Slice,
|
||||
|
||||
pub const Aggregate = struct {
|
||||
decl: u32,
|
||||
/// Active variant for enums; 0 for structs.
|
||||
variant: u32,
|
||||
fields: []const Value,
|
||||
fields: []Value,
|
||||
};
|
||||
|
||||
/// Frame storage. `value` is `.void` while uninitialized.
|
||||
pub const Cell = struct { value: Value, ty: air.Type };
|
||||
|
||||
/// Path of field or element indices from the cell's value.
|
||||
pub const Place = struct { cell: *Cell, path: []const u32 };
|
||||
|
||||
/// Range `[start, start + len)` of the array at `place`.
|
||||
pub const Slice = struct { place: Place, start: u64, len: u64 };
|
||||
};
|
||||
|
||||
pub const Trap = struct {
|
||||
@@ -71,6 +87,7 @@ const TrapKind = enum {
|
||||
shift_out_of_range,
|
||||
cast_out_of_range,
|
||||
call_depth,
|
||||
out_of_bounds,
|
||||
|
||||
fn code(k: TrapKind) *const diag.Code {
|
||||
return switch (k) {
|
||||
@@ -81,6 +98,7 @@ const TrapKind = enum {
|
||||
.shift_out_of_range => diag.lookup("T0005").?,
|
||||
.cast_out_of_range => diag.lookup("T0006").?,
|
||||
.call_depth => diag.lookup("T0007").?,
|
||||
.out_of_bounds => diag.lookup("T0008").?,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -200,13 +218,13 @@ const Machine = struct {
|
||||
.struct_make => |x| return bind(vals, inst.result, .{ .aggregate = .{
|
||||
.decl = x.decl,
|
||||
.variant = 0,
|
||||
.fields = try m.gather(vals, x.args),
|
||||
.fields = try m.gatherMut(vals, x.args),
|
||||
} }),
|
||||
.struct_get => |x| return bind(vals, inst.result, vals[x.a].aggregate.fields[x.field]),
|
||||
.enum_make => |x| return bind(vals, inst.result, .{ .aggregate = .{
|
||||
.decl = x.decl,
|
||||
.variant = x.variant,
|
||||
.fields = try m.gather(vals, x.args),
|
||||
.fields = try m.gatherMut(vals, x.args),
|
||||
} }),
|
||||
.call => |x| {
|
||||
if (m.depth >= call_depth_limit) return .{ .trap = .call_depth };
|
||||
@@ -237,6 +255,7 @@ const Machine = struct {
|
||||
},
|
||||
.ret => |v| return .{ .ret = if (v) |x| vals[x] else .void },
|
||||
.trap => return .{ .trap = .explicit },
|
||||
.local_alloc, .local_store, .local_zero, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .borrow_end, .load, .store, .array_splat, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split, .slice_copy, .guard => return m.execMemory(vals, inst),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,7 +264,156 @@ const Machine = struct {
|
||||
return .next;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ memory (spec 07a, features ownership/arrays)
|
||||
|
||||
/// Values in storage never share structure with SSA values, so every
|
||||
/// transfer between the two copies aggregates and arrays.
|
||||
fn deepCopy(m: *Machine, v: Value) std.mem.Allocator.Error!Value {
|
||||
return switch (v) {
|
||||
.aggregate => |a| .{ .aggregate = .{ .decl = a.decl, .variant = a.variant, .fields = try m.copyAll(a.fields) } },
|
||||
.array => |items| .{ .array = try m.copyAll(items) },
|
||||
else => v,
|
||||
};
|
||||
}
|
||||
|
||||
fn copyAll(m: *Machine, items: []const Value) std.mem.Allocator.Error![]Value {
|
||||
const out = try m.arena.alloc(Value, items.len);
|
||||
for (items, out) |x, *o| o.* = try m.deepCopy(x);
|
||||
return out;
|
||||
}
|
||||
|
||||
fn zeroValue(m: *Machine, t: air.Type) std.mem.Allocator.Error!Value {
|
||||
return switch (t) {
|
||||
.int => |it| .{ .int = .{ .ty = it, .bits = 0 } },
|
||||
.bool => .{ .boolean = false },
|
||||
.array => |a| blk: {
|
||||
const items = try m.arena.alloc(Value, a.len);
|
||||
for (items) |*x| x.* = try m.zeroValue(a.elem);
|
||||
break :blk .{ .array = items };
|
||||
},
|
||||
.named => |i| blk: {
|
||||
const fields = m.module.types[i].kind.@"struct";
|
||||
const out = try m.arena.alloc(Value, fields.len);
|
||||
for (fields, out) |fd, *o| o.* = try m.zeroValue(fd.ty);
|
||||
break :blk .{ .aggregate = .{ .decl = i, .variant = 0, .fields = out } };
|
||||
},
|
||||
else => unreachable, // verified zeroable
|
||||
};
|
||||
}
|
||||
|
||||
fn placePtr(p: Value.Place) *Value {
|
||||
var v = &p.cell.value;
|
||||
for (p.path) |i| v = switch (v.*) {
|
||||
.aggregate => |a| &a.fields[i],
|
||||
.array => |items| &items[i],
|
||||
else => unreachable,
|
||||
};
|
||||
return v;
|
||||
}
|
||||
|
||||
fn extend(m: *Machine, p: Value.Place, i: u64) !Value.Place {
|
||||
const path = try m.arena.alloc(u32, p.path.len + 1);
|
||||
@memcpy(path[0..p.path.len], p.path);
|
||||
path[p.path.len] = @intCast(i);
|
||||
return .{ .cell = p.cell, .path = path };
|
||||
}
|
||||
|
||||
fn asPlace(v: Value) Value.Place {
|
||||
return switch (v) {
|
||||
.local => |c| .{ .cell = c, .path = &.{} },
|
||||
.place => |p| p,
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
|
||||
fn usize_(v: Value) u64 {
|
||||
return @truncate(v.int.bits);
|
||||
}
|
||||
|
||||
fn execMemory(m: *Machine, vals: []Value, inst: air.Inst) std.mem.Allocator.Error!Step {
|
||||
switch (inst.op) {
|
||||
.local_alloc => |t| {
|
||||
const cell = try m.arena.create(Value.Cell);
|
||||
cell.* = .{ .value = .void, .ty = t };
|
||||
return bind(vals, inst.result, .{ .local = cell });
|
||||
},
|
||||
.local_store => |x| {
|
||||
vals[x.local].local.value = try m.deepCopy(vals[x.value]);
|
||||
return .next;
|
||||
},
|
||||
.local_zero => |v| {
|
||||
const cell = vals[v].local;
|
||||
cell.value = try m.zeroValue(cell.ty);
|
||||
return .next;
|
||||
},
|
||||
.local_load => |v| return bind(vals, inst.result, try m.deepCopy(vals[v].local.value)),
|
||||
.borrow_ref, .borrow_mut => |v| return bind(vals, inst.result, switch (vals[v]) {
|
||||
.local => |c| .{ .place = .{ .cell = c, .path = &.{} } },
|
||||
else => |x| x,
|
||||
}),
|
||||
.borrow_field => |x| return bind(vals, inst.result, .{ .place = try m.extend(asPlace(vals[x.borrow]), x.field) }),
|
||||
.borrow_end => return .next,
|
||||
.load => |v| return bind(vals, inst.result, try m.deepCopy(placePtr(asPlace(vals[v])).*)),
|
||||
.store => |x| {
|
||||
placePtr(asPlace(vals[x.borrow])).* = try m.deepCopy(vals[x.value]);
|
||||
return .next;
|
||||
},
|
||||
.array_splat => |x| {
|
||||
const items = try m.arena.alloc(Value, x.ty.array.len);
|
||||
for (items) |*it| it.* = try m.deepCopy(vals[x.value]);
|
||||
return bind(vals, inst.result, .{ .array = items });
|
||||
},
|
||||
.borrow_index => |x| {
|
||||
const i = usize_(vals[x.index]);
|
||||
const place: Value.Place, const start: u64, const len: u64 = switch (vals[x.borrow]) {
|
||||
.slice => |s| .{ s.place, s.start, s.len },
|
||||
else => |b| .{ asPlace(b), 0, placePtr(asPlace(b)).array.len },
|
||||
};
|
||||
// `.proven` was verified in bounds; the check is kept as a
|
||||
// defensive assertion that can never fire on verified AIR.
|
||||
if (i >= len) return .{ .trap = .out_of_bounds };
|
||||
return bind(vals, inst.result, .{ .place = try m.extend(place, start + i) });
|
||||
},
|
||||
.slice_make => |v| {
|
||||
const p = asPlace(vals[v]);
|
||||
return bind(vals, inst.result, .{ .slice = .{ .place = p, .start = 0, .len = placePtr(p).array.len } });
|
||||
},
|
||||
.slice_len => |v| return bind(vals, inst.result, .{ .int = .{ .ty = .usize, .bits = vals[v].slice.len } }),
|
||||
.slice_sub => |x| {
|
||||
const s = vals[x.slice].slice;
|
||||
const start = usize_(vals[x.start]);
|
||||
const len = usize_(vals[x.len]);
|
||||
if (start > s.len or len > s.len - start) return .{ .trap = .out_of_bounds };
|
||||
return bind(vals, inst.result, .{ .slice = .{ .place = s.place, .start = s.start + start, .len = len } });
|
||||
},
|
||||
.slice_split => |x| {
|
||||
const s = vals[x.slice].slice;
|
||||
const at = usize_(vals[x.at]);
|
||||
if (at > s.len) return .{ .trap = .out_of_bounds };
|
||||
vals[inst.result.?] = .{ .slice = .{ .place = s.place, .start = s.start, .len = at } };
|
||||
vals[inst.result2.?] = .{ .slice = .{ .place = s.place, .start = s.start + at, .len = s.len - at } };
|
||||
return .next;
|
||||
},
|
||||
.slice_copy => |x| {
|
||||
const d = vals[x.dst].slice;
|
||||
const s = vals[x.src].slice;
|
||||
if (d.len < s.len) return .{ .trap = .out_of_bounds };
|
||||
const dst = placePtr(d.place).array;
|
||||
const src = placePtr(s.place).array;
|
||||
// Operands never overlap (spec 08 §11.2), so a forward copy is exact.
|
||||
for (0..s.len) |k| dst[d.start + k] = try m.deepCopy(src[s.start + k]);
|
||||
return .next;
|
||||
},
|
||||
.guard => |v| return if (vals[v].boolean) .next else .{ .trap = .assert },
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
fn gather(m: *Machine, vals: []const Value, ids: []const air.ValueId) ![]const Value {
|
||||
return m.gatherMut(vals, ids);
|
||||
}
|
||||
|
||||
fn gatherMut(m: *Machine, vals: []const Value, ids: []const air.ValueId) ![]Value {
|
||||
const out = try m.arena.alloc(Value, ids.len);
|
||||
for (ids, out) |id, *o| o.* = vals[id];
|
||||
return out;
|
||||
@@ -374,6 +542,13 @@ pub fn writeValue(module: air.Module, v: Value, s: *std.json.Stringify) std.Io.W
|
||||
},
|
||||
.boolean => |b| try s.write(b),
|
||||
.void => try s.write(null),
|
||||
.array => |items| {
|
||||
try s.beginArray();
|
||||
for (items) |x| try writeValue(module, x, s);
|
||||
try s.endArray();
|
||||
},
|
||||
// Storage and borrows cannot be returned from `@main` (spec 08 §11.3).
|
||||
.local, .place, .slice => try s.write(null),
|
||||
.aggregate => |agg| {
|
||||
const d = module.types[agg.decl];
|
||||
switch (d.kind) {
|
||||
|
||||
Reference in new issue
Block a user