cli: add initialized buffers and safe owning vectors
Introduce linear_buffers through the specification, AIR parser/serializer, verifier, deterministic interpreter and source lowering. A buffer owns an initialized prefix with explicit reserve, bounded push, moving pop/views and recoverable empty-only free. Growth charges old+new capacity; failed growth, full push and nonempty free preserve or return complete ownership. Implement compiler-pinned safe std.vec with nine operations for Copy values, owners, files and channel endpoints. Permit complete Copy moves inside specialized generic bodies without weakening monomorphic or borrow rules. Update RFC 0024, Q-036, STD-6a/b, reference grammar, Quick Reference, bundles, source coverage and CI. Remaining collections stay STD-6c. Validation: unit tests and AIR round-trip; 757 conformance cases; 56 dedicated buffer cases with per-instruction collection and 32-seed schedule stress; extended memory test; hosted smoke; 36 module, 20 generic, 49 linear and 118 buffer CLI checks; 91/91 source constructs; Zig fmt and reference bundles. Full source bundle: 49,589 -> 52,384 cl100k tokens (+5.6%); AIR 175,736 bytes. The reference interpreter's push copies its initialized prefix; no native performance or empirical MCC benefit is claimed.
This commit is contained in:
101 files changed
+2377
-71
No files matched your search
@@ -35,6 +35,7 @@ pub const registry = [_]Feature{
|
||||
.{ .name = "heap", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .published, .phase = 2 },
|
||||
.{ .name = "islands", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 4 },
|
||||
.{ .name = "linear_aggregates", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .published, .phase = 6 },
|
||||
.{ .name = "linear_buffers", .depends = &.{ "arrays", "capabilities", "effects", "heap", "linear_aggregates", "ownership", "resources" }, .status = .published, .phase = 6 },
|
||||
.{ .name = "ownership", .depends = &.{}, .status = .published, .phase = 1 },
|
||||
.{ .name = "receiver_dup", .depends = &.{ "cancellation", "capabilities", "channels", "effects", "heap", "ownership", "resources", "tasks" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "regions", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 2 },
|
||||
@@ -49,7 +50,7 @@ pub const registry = [_]Feature{
|
||||
};
|
||||
|
||||
/// Features implemented by this toolchain, sorted.
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "atomics", "cancellation", "capabilities", "channels", "effects", "heap", "islands", "linear_aggregates", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "atomics", "cancellation", "capabilities", "channels", "effects", "heap", "islands", "linear_aggregates", "linear_buffers", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
|
||||
pub const Reason = enum {
|
||||
unknown_feature,
|
||||
|
||||
+42
-4
@@ -82,6 +82,7 @@ pub const Type = union(enum) {
|
||||
// Features `heap` and `regions` (spec 03 §18–§20).
|
||||
own: *const Type,
|
||||
own_array: *const Type,
|
||||
buffer: *const Type,
|
||||
region,
|
||||
// Feature `tasks` (spec 04 §33).
|
||||
scope,
|
||||
@@ -109,7 +110,7 @@ pub const Type = union(enum) {
|
||||
return switch (a) {
|
||||
.int => |x| b.int == x,
|
||||
.named => |x| b.named == x,
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.eql(b.inner().?),
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.eql(b.inner().?),
|
||||
.region, .scope, .shield => true,
|
||||
.array => |x| x.len == b.array.len and x.elem.eql(b.array.elem),
|
||||
.cap => |k| b.cap == k,
|
||||
@@ -122,7 +123,7 @@ pub const Type = union(enum) {
|
||||
/// Element or pointee type of storage, borrow and slice types.
|
||||
pub fn inner(t: Type) ?Type {
|
||||
return switch (t) {
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.*,
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.*,
|
||||
.array => |a| a.elem,
|
||||
else => null,
|
||||
};
|
||||
@@ -139,7 +140,7 @@ pub const Type = union(enum) {
|
||||
/// Linear values: consumed exactly once (spec 06 §6.2–§6.3).
|
||||
pub fn isLinear(t: Type) bool {
|
||||
return switch (t) {
|
||||
.resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .atomic => true,
|
||||
.resource, .own, .own_array, .buffer, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .atomic => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
@@ -173,7 +174,7 @@ pub const Type = union(enum) {
|
||||
/// Linear resources are neither values nor borrows: they are moved.
|
||||
pub fn isValue(t: Type) bool {
|
||||
return switch (t) {
|
||||
.void, .never, .local, .ref, .mut, .slice, .slice_mut, .resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .atomic => false,
|
||||
.void, .never, .local, .ref, .mut, .slice, .slice_mut, .resource, .own, .own_array, .buffer, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .atomic => false,
|
||||
else => true,
|
||||
};
|
||||
}
|
||||
@@ -340,6 +341,15 @@ pub const Op = union(enum) {
|
||||
region_array: struct { region: ValueId, n: ValueId, value: ValueId, ok: u32, err: u32 },
|
||||
region_reset: ValueId,
|
||||
region_destroy: ValueId,
|
||||
// Feature linear_buffers (spec 03 §26).
|
||||
buffer_new: struct { ty: Type, n: ValueId, cap: ValueId, ok: u32, err: u32 },
|
||||
buffer_len: ValueId,
|
||||
buffer_capacity: ValueId,
|
||||
buffer_view: ValueId,
|
||||
buffer_reserve: struct { borrow: ValueId, n: ValueId, cap: ValueId, ok: u32, err: u32 },
|
||||
buffer_push: struct { borrow: ValueId, value: ValueId, ok: u32, err: u32 },
|
||||
buffer_pop: struct { value: ValueId, ok: u32, err: u32 },
|
||||
buffer_free: struct { value: ValueId, ok: u32, err: u32 },
|
||||
// Feature `tasks` (spec 04 §33).
|
||||
task_scope_begin,
|
||||
task_spawn: struct { scope: ValueId, callee: u32, args: []const ValueId },
|
||||
@@ -413,7 +423,15 @@ pub const Op = union(enum) {
|
||||
return buf[0..n];
|
||||
}
|
||||
|
||||
pub fn bufferTargets(op: Op) ?struct { ok: u32, err: u32 } {
|
||||
return switch (op) {
|
||||
inline .buffer_new, .buffer_reserve, .buffer_push, .buffer_pop, .buffer_free => |x| .{ .ok = x.ok, .err = x.err },
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isTerminator(op: Op) bool {
|
||||
if (op.bufferTargets() != null) return true;
|
||||
return switch (op) {
|
||||
inline .br, .cbr, .@"switch", .enum_switch, .enum_take, .aggregate_unpack, .cap_narrow, .ret, .trap => true,
|
||||
inline .own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => true,
|
||||
@@ -526,6 +544,21 @@ pub const Module = struct {
|
||||
/// of terminators are included.
|
||||
pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) void) void {
|
||||
switch (op) {
|
||||
.buffer_new => |x| {
|
||||
f(ctx, x.n);
|
||||
f(ctx, x.cap);
|
||||
},
|
||||
.buffer_reserve => |x| {
|
||||
f(ctx, x.borrow);
|
||||
f(ctx, x.n);
|
||||
f(ctx, x.cap);
|
||||
},
|
||||
.buffer_push => |x| {
|
||||
f(ctx, x.borrow);
|
||||
f(ctx, x.value);
|
||||
},
|
||||
inline .buffer_pop, .buffer_free => |x| f(ctx, x.value),
|
||||
.buffer_len, .buffer_capacity, .buffer_view => |v| f(ctx, v),
|
||||
.@"const", .trap => {},
|
||||
.copy, .assert => |v| f(ctx, v),
|
||||
.arith => |x| {
|
||||
@@ -689,6 +722,11 @@ pub fn allocTargets(op: Op) ?struct { ok: u32, err: u32 } {
|
||||
}
|
||||
|
||||
pub fn successors(op: Op, buf: *std.ArrayList(u32), gpa: std.mem.Allocator) std.mem.Allocator.Error!void {
|
||||
if (op.bufferTargets()) |t| {
|
||||
try buf.append(gpa, t.ok);
|
||||
try buf.append(gpa, t.err);
|
||||
return;
|
||||
}
|
||||
switch (op) {
|
||||
.br => |t| try buf.append(gpa, t.block),
|
||||
.cbr => |x| {
|
||||
|
||||
+37
-2
@@ -560,7 +560,7 @@ const Parser = struct {
|
||||
boxed.* = inner;
|
||||
return if (eql(t.text, "own")) .{ .own = boxed } else .{ .own_array = boxed };
|
||||
}
|
||||
const Wrapper = enum { task, sender, receiver, mutex, guard, island, frozen, arc, iref, atomic };
|
||||
const Wrapper = enum { task, sender, receiver, mutex, guard, island, frozen, arc, iref, atomic, buffer };
|
||||
if (std.meta.stringToEnum(Wrapper, t.text)) |which| {
|
||||
try p.requireFeature(switch (which) {
|
||||
.task => "tasks",
|
||||
@@ -569,6 +569,7 @@ const Parser = struct {
|
||||
.island, .frozen, .iref => "islands",
|
||||
.arc => "arc",
|
||||
.atomic => "atomics",
|
||||
.buffer => "linear_buffers",
|
||||
}, t.span, t.text);
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
// Element rules (spec 04 §38.1) are checked by the type stage.
|
||||
@@ -587,6 +588,7 @@ const Parser = struct {
|
||||
.arc => .{ .arc = boxed },
|
||||
.iref => .{ .iref = boxed },
|
||||
.atomic => .{ .atomic = boxed },
|
||||
.buffer => .{ .buffer = boxed },
|
||||
};
|
||||
}
|
||||
if (eql(t.text, "cap")) {
|
||||
@@ -909,6 +911,39 @@ const Parser = struct {
|
||||
const it = std.meta.stringToEnum(air.IntType, parts[2]) orelse return p.unknownOpcode(op_tok);
|
||||
break :op .{ .cast = .{ .mode = mode, .to = it, .a = try p.useValue() } };
|
||||
}
|
||||
if (std.mem.startsWith(u8, op_tok.text, "buffer.")) {
|
||||
try p.requireFeature("linear_buffers", op_tok.span, op_tok.text);
|
||||
const Name = enum { new, len, capacity, view, reserve, push, pop, free };
|
||||
const name = std.meta.stringToEnum(Name, op_tok.text[7..]) orelse return p.unknownOpcode(op_tok);
|
||||
switch (name) {
|
||||
.new => {
|
||||
const ty = try p.parseType();
|
||||
const n, const cap = try p.twoOperands();
|
||||
const ok, const err = try p.okErr();
|
||||
break :op .{ .buffer_new = .{ .ty = ty, .n = n, .cap = cap, .ok = ok, .err = err } };
|
||||
},
|
||||
.len => break :op .{ .buffer_len = try p.useValue() },
|
||||
.capacity => break :op .{ .buffer_capacity = try p.useValue() },
|
||||
.view => break :op .{ .buffer_view = try p.useValue() },
|
||||
.reserve => {
|
||||
const borrow, const n = try p.twoOperands();
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
const cap = try p.useValue();
|
||||
const ok, const err = try p.okErr();
|
||||
break :op .{ .buffer_reserve = .{ .borrow = borrow, .n = n, .cap = cap, .ok = ok, .err = err } };
|
||||
},
|
||||
.push => {
|
||||
const borrow, const value = try p.twoOperands();
|
||||
const ok, const err = try p.okErr();
|
||||
break :op .{ .buffer_push = .{ .borrow = borrow, .value = value, .ok = ok, .err = err } };
|
||||
},
|
||||
.pop, .free => {
|
||||
const value = try p.useValue();
|
||||
const ok, const err = try p.okErr();
|
||||
break :op if (name == .pop) .{ .buffer_pop = .{ .value = value, .ok = ok, .err = err } } else .{ .buffer_free = .{ .value = value, .ok = ok, .err = err } };
|
||||
},
|
||||
}
|
||||
}
|
||||
if (eql(op_tok.text, "array.make")) {
|
||||
try p.requireFeature("linear_aggregates", op_tok.span, op_tok.text);
|
||||
const ty = try p.parseType();
|
||||
@@ -1077,7 +1112,7 @@ const Parser = struct {
|
||||
.atomic_new, .atomic_take, .atomic_load, .atomic_rmw, .atomic_cmpxchg => true,
|
||||
.@"const", .copy, .arith, .bit, .cmp, .cast, .struct_make, .struct_get, .enum_make => true,
|
||||
.local_alloc, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .load => true,
|
||||
.array_splat, .array_make, .value_swap, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split => true,
|
||||
.buffer_len, .buffer_capacity, .buffer_view, .array_splat, .array_make, .value_swap, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split => true,
|
||||
else => false,
|
||||
};
|
||||
const may_bind = produces_value or op == .call or op == .intrinsic or op == .task_join;
|
||||
|
||||
+72
-1
@@ -115,7 +115,7 @@ pub fn printType(m: air.Module, t: air.Type, w: *Writer) Writer.Error!void {
|
||||
.cap => |k| try w.print("cap<{s}>", .{air.authority.kinds[k].name}),
|
||||
.builtin => |b| try w.writeAll(@tagName(b)),
|
||||
.resource => |r| try w.writeAll(r.text()),
|
||||
.own, .own_array => |inner| {
|
||||
.own, .own_array, .buffer => |inner| {
|
||||
try w.print("{s}<", .{@tagName(t)});
|
||||
try printType(m, inner.*, w);
|
||||
try w.writeByte('>');
|
||||
@@ -323,6 +323,30 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
try values(f, x.args, w);
|
||||
if (x.ok) |ok| try w.print(", ok -> @{s}, err -> @{s}", .{ f.blocks[ok].name, f.blocks[x.err.?].name });
|
||||
},
|
||||
.buffer_new => |x| {
|
||||
try w.writeAll("buffer.new ");
|
||||
try printType(m, x.ty, w);
|
||||
try w.print(" %{s}, %{s}", .{ f.values[x.n].name, f.values[x.cap].name });
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
inline .buffer_len, .buffer_capacity, .buffer_view => |v, tag| try one(f, w, switch (tag) {
|
||||
.buffer_len => "buffer.len",
|
||||
.buffer_capacity => "buffer.capacity",
|
||||
.buffer_view => "buffer.view",
|
||||
else => unreachable,
|
||||
}, v),
|
||||
.buffer_reserve => |x| {
|
||||
try w.print("buffer.reserve %{s}, %{s}, %{s}", .{ f.values[x.borrow].name, f.values[x.n].name, f.values[x.cap].name });
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
.buffer_push => |x| {
|
||||
try two(f, w, "buffer.push", x.borrow, x.value);
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
inline .buffer_pop, .buffer_free => |x, tag| {
|
||||
try one(f, w, if (tag == .buffer_pop) "buffer.pop" else "buffer.free", x.value);
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
inline .own_new, .own_pack => |x, tag| {
|
||||
if (tag == .own_pack) {
|
||||
try w.writeAll("own.pack ");
|
||||
@@ -692,3 +716,50 @@ test "round-trip whole-value linear operations" {
|
||||
\\ ret %n
|
||||
);
|
||||
}
|
||||
|
||||
test "round-trip initialized buffers" {
|
||||
try roundTrip(
|
||||
\\module test.buffers
|
||||
\\ air 0
|
||||
\\ lang 0.1
|
||||
\\ features [arrays, capabilities, effects, heap, linear_aggregates, linear_buffers, ownership, resources]
|
||||
\\fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
\\effects [mem.alloc]
|
||||
\\entry:
|
||||
\\ %capacity = const.usize 1
|
||||
\\ %n = const.u64 42
|
||||
\\ buffer.new own<u64> %capacity, %heap, ok -> @made, err -> @failed
|
||||
\\made(%data: buffer<own<u64>>):
|
||||
\\ own.new %n, %heap, ok -> @owned, err -> @owner_failed
|
||||
\\owned(%owner: own<u64>):
|
||||
\\ %b = borrow.mut %data
|
||||
\\ buffer.push %b, %owner, ok -> @pushed, err -> @full
|
||||
\\full(%returned: own<u64>):
|
||||
\\ trap
|
||||
\\pushed:
|
||||
\\ %next_capacity = const.usize 2
|
||||
\\ buffer.reserve %b, %next_capacity, %heap, ok -> @grown, err -> @reserve_failed
|
||||
\\grown:
|
||||
\\ %length = buffer.len %b
|
||||
\\ %capacity_after = buffer.capacity %b
|
||||
\\ %view = buffer.view %b
|
||||
\\ borrow.end %view
|
||||
\\ buffer.pop %b, ok -> @popped, err -> @empty
|
||||
\\popped(%part: own<u64>):
|
||||
\\ %answer = own.take %part
|
||||
\\ borrow.end %b
|
||||
\\ buffer.free %data, ok -> @done, err -> @nonempty
|
||||
\\nonempty(%original: buffer<own<u64>>):
|
||||
\\ trap
|
||||
\\done:
|
||||
\\ ret %answer
|
||||
\\empty:
|
||||
\\ trap
|
||||
\\failed(%error: alloc_error):
|
||||
\\ trap
|
||||
\\owner_failed(%owner_error: alloc_error):
|
||||
\\ trap
|
||||
\\reserve_failed(%reserve_error: alloc_error):
|
||||
\\ trap
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user