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:
dev committed 2026-10-06 23:26:41 +03:00
1 parent 4f084decce
commit 4138a52af6
101 files changed
+2377 -71

No files matched your search

+109 -1
View File
@@ -76,6 +76,16 @@ pub const Value = union(enum) {
iref: struct { id: u64, slot: u32 },
/// `atomic<T>`: index into the machine's atomic cells (spec 04 §42).
atomic: u32,
buffer: *Buffer,
pub const Buffer = struct {
// The interpreter stores only the initialized prefix. Capacity is
// charged to Aji authority independently of this host representation;
// push's Value-array bookkeeping does not allocate from that authority.
cell: *Cell,
capacity: u64,
elem: air.Type,
};
/// The guarded value is a separate cell, so a place borrowed through a
/// guard points at an ordinary cell.
@@ -344,6 +354,7 @@ const Copier = struct {
mutexes: std.AutoHashMapUnmanaged(*Value.MutexCell, *Value.MutexCell) = .empty,
payloads: std.AutoHashMapUnmanaged([*]Value, []Value) = .empty,
graphs: std.AutoHashMapUnmanaged(*Value.Graph, *Value.Graph) = .empty,
buffers: std.AutoHashMapUnmanaged(*Value.Buffer, *Value.Buffer) = .empty,
fn value(c: *Copier, v: Value) std.mem.Allocator.Error!Value {
return switch (v) {
@@ -351,6 +362,7 @@ const Copier = struct {
.array => |xs| .{ .array = try c.items(xs) },
.local => |cl| .{ .local = try c.cell(cl) },
.own => |cl| .{ .own = try c.cell(cl) },
.buffer => |b| .{ .buffer = try c.buffer(b) },
.place => |p| .{ .place = try c.place(p) },
.slice => |sl| .{ .slice = .{ .place = try c.place(sl.place), .start = sl.start, .len = sl.len } },
.mutex => |mc| .{ .mutex = try c.mutex(mc) },
@@ -383,6 +395,14 @@ const Copier = struct {
return n;
}
fn buffer(c: *Copier, old: *Value.Buffer) std.mem.Allocator.Error!*Value.Buffer {
if (c.buffers.get(old)) |n| return n;
const n = try c.to.create(Value.Buffer);
try c.buffers.put(c.tmp, old, n);
n.* = .{ .cell = try c.cell(old.cell), .capacity = old.capacity, .elem = old.elem };
return n;
}
fn mutex(c: *Copier, old: *Value.MutexCell) std.mem.Allocator.Error!*Value.MutexCell {
if (c.mutexes.get(old)) |n| return n;
const n = try c.to.create(Value.MutexCell);
@@ -863,6 +883,7 @@ const Machine = struct {
return bind(vals, inst.result, .{ .int = .{ .ty = x.to, .bits = bits } });
},
.assert => |v| return if (vals[v].boolean) .next else .{ .trap = .assert },
.buffer_new, .buffer_len, .buffer_capacity, .buffer_view, .buffer_reserve, .buffer_push, .buffer_pop, .buffer_free => return m.execBuffer(f, vals, inst),
.array_make => |x| return bind(vals, inst.result, .{ .array = try m.gatherMut(vals, x.args) }),
.aggregate_unpack => |x| {
const fields = if (x.ty == .array) vals[x.value].array else vals[x.value].aggregate.fields;
@@ -1763,6 +1784,93 @@ const Machine = struct {
}
}
fn bufferOf(v: Value) *Value.Buffer {
return switch (v) {
.buffer => |b| b,
.place => |p| bufferOf(placePtr(p).*),
else => unreachable,
};
}
fn execBuffer(m: *Machine, f: air.Function, vals: []Value, inst: air.Inst) !Step {
switch (inst.op) {
.buffer_new => |x| {
const capacity = usize_(vals[x.n]);
const size = budget.accountedSize(m.module, x.ty, 0).?;
const bytes = std.math.mul(u64, capacity, size) catch null;
const authority_ = vals[x.cap].cap;
if (!m.charge(authority_, bytes)) return allocFailed(f, vals, x.err);
const cell = try m.heap().create(Value.Cell);
cell.* = .{ .value = .{ .array = &.{} }, .ty = .void };
const b = try m.heap().create(Value.Buffer);
b.* = .{ .cell = cell, .capacity = capacity, .elem = x.ty };
try m.owners.put(m.heap(), cell, .{ .authority = authority_, .bytes = bytes.? });
vals[f.blocks[x.ok].params[0].value] = .{ .buffer = b };
return .{ .jump = x.ok };
},
.buffer_len, .buffer_capacity => |v| {
const b = bufferOf(vals[v]);
if (try m.raceCheck(b.cell, &.{}, false)) |r| return r;
return bind(vals, inst.result, .{ .int = .{ .ty = .usize, .bits = if (inst.op == .buffer_len) b.cell.value.array.len else b.capacity } });
},
.buffer_view => |v| {
const b = bufferOf(vals[v]);
if (try m.raceCheck(b.cell, &.{}, false)) |r| return r;
return bind(vals, inst.result, .{ .slice = .{ .place = .{ .cell = b.cell, .path = &.{} }, .start = 0, .len = b.cell.value.array.len } });
},
.buffer_reserve => |x| {
const b = bufferOf(vals[x.borrow]);
if (try m.raceCheck(b.cell, &.{}, true)) |r| return r;
const capacity = usize_(vals[x.n]);
if (capacity <= b.capacity) return .{ .jump = x.ok };
const size = budget.accountedSize(m.module, b.elem, 0).?;
const bytes = std.math.mul(u64, capacity, size) catch null;
const authority_ = vals[x.cap].cap;
if (!m.charge(authority_, bytes)) return allocFailed(f, vals, x.err);
const old = m.owners.get(b.cell).?;
m.uncharge(old.authority, old.bytes);
try m.owners.put(m.heap(), b.cell, .{ .authority = authority_, .bytes = bytes.? });
b.capacity = capacity;
return .{ .jump = x.ok };
},
.buffer_push => |x| {
const b = bufferOf(vals[x.borrow]);
if (try m.raceCheck(b.cell, &.{}, true)) |r| return r;
const old = b.cell.value.array;
if (old.len == b.capacity) {
vals[f.blocks[x.err].params[0].value] = vals[x.value];
return .{ .jump = x.err };
}
const items = try m.heap().alloc(Value, old.len + 1);
@memcpy(items[0..old.len], old);
items[old.len] = try m.deepCopy(vals[x.value]);
b.cell.value = .{ .array = items };
return .{ .jump = x.ok };
},
.buffer_pop => |x| {
const b = bufferOf(vals[x.value]);
if (try m.raceCheck(b.cell, &.{}, true)) |r| return r;
const old = b.cell.value.array;
if (old.len == 0) return .{ .jump = x.err };
vals[f.blocks[x.ok].params[0].value] = old[old.len - 1];
b.cell.value = .{ .array = old[0 .. old.len - 1] };
return .{ .jump = x.ok };
},
.buffer_free => |x| {
const b = bufferOf(vals[x.value]);
if (try m.raceCheck(b.cell, &.{}, true)) |r| return r;
if (b.cell.value.array.len != 0) {
vals[f.blocks[x.err].params[0].value] = vals[x.value];
return .{ .jump = x.err };
}
const allocation = m.owners.fetchRemove(b.cell).?.value;
m.uncharge(allocation.authority, allocation.bytes);
return .{ .jump = x.ok };
},
else => unreachable,
}
}
fn allocFailed(f: air.Function, vals: []Value, err: u32) Step {
vals[f.blocks[err].params[0].value] = .{ .builtin = .{ .which = .alloc_error, .variant = 0 } };
return .{ .jump = err };
@@ -2234,7 +2342,7 @@ pub fn writeValue(module: air.Module, v: Value, s: *std.json.Stringify) std.Io.W
},
// Storage and borrows cannot be returned from `@main` (spec 08 §11.3).
.local, .place, .slice => try s.write(null),
.resource, .own, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .graph, .iref, .atomic => try s.write(null),
.resource, .own, .buffer, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .graph, .iref, .atomic => try s.write(null),
// A capability is opaque: only its kind is shown, never its identity.
.cap => {
try s.beginObject();