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
|
||||
);
|
||||
}
|
||||
@@ -55,6 +55,8 @@ pub const CastMode = enum { widen, checked, wrap };
|
||||
|
||||
pub const Expr = struct {
|
||||
span: Span,
|
||||
/// Complete Copy transfer is admitted only in a specialized template body.
|
||||
generic_transfer: bool = false,
|
||||
kind: Kind,
|
||||
|
||||
pub const Kind = union(enum) {
|
||||
@@ -72,7 +74,7 @@ pub const Expr = struct {
|
||||
move: []const u8,
|
||||
binary: struct { op: BinOp, l: *const Expr, r: *const Expr },
|
||||
cast: struct { mode: CastMode, e: *const Expr, ty: Type },
|
||||
/// `callee(args)`; `type_arg` only for `channel.make<T>`.
|
||||
/// `callee(args)`; `type_arg` for `channel.make<T>` / `buffer.new<T>`.
|
||||
call: struct { callee: *const Expr, type_arg: ?Type, args: []const Expr },
|
||||
field: struct { e: *const Expr, name: []const u8 },
|
||||
/// `e[lo]` or `e[lo..hi]`.
|
||||
|
||||
@@ -303,6 +303,7 @@ const Generator = struct {
|
||||
|
||||
fn expression(g: *Generator, e: ast.Expr, env: Env) Error!ast.Expr {
|
||||
var out = e;
|
||||
if (e.kind == .move and env.params.len != 0) out.generic_transfer = true;
|
||||
switch (e.kind) {
|
||||
.specialize => |x| {
|
||||
const name = path(g.arena, x.base.*) catch return error.OutOfMemory;
|
||||
|
||||
+91
-25
@@ -50,6 +50,7 @@ pub const Ty = union(enum) {
|
||||
slice_mut: *const Ty,
|
||||
own: *const Ty,
|
||||
own_array: *const Ty,
|
||||
buffer: *const Ty,
|
||||
region,
|
||||
/// `cap<K>`: the kind.
|
||||
cap: []const u8,
|
||||
@@ -81,7 +82,7 @@ pub const Ty = union(enum) {
|
||||
/// Linear: consumed exactly once, by `move` (spec 15 §5).
|
||||
fn isLinear(t: Ty) bool {
|
||||
return switch (t) {
|
||||
.own, .own_array, .region, .resource, .task, .sender, .receiver, .scope, .mutex, .guard, .shield, .island, .frozen, .arc, .atomic => true,
|
||||
.own, .own_array, .buffer, .region, .resource, .task, .sender, .receiver, .scope, .mutex, .guard, .shield, .island, .frozen, .arc, .atomic => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
@@ -92,7 +93,7 @@ pub const Ty = union(enum) {
|
||||
.int => |x| std.mem.eql(u8, x, b.int),
|
||||
.named => |i| i == b.named,
|
||||
.array => |x| x.len == b.array.len and x.elem.eql(b.array.elem),
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.eql(inner(b).?),
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.eql(inner(b).?),
|
||||
.cap => |k| std.mem.eql(u8, k, b.cap),
|
||||
.builtin => |e| e == b.builtin,
|
||||
.resource => |r| r == b.resource,
|
||||
@@ -102,7 +103,7 @@ pub const Ty = union(enum) {
|
||||
|
||||
fn inner(t: Ty) ?Ty {
|
||||
return switch (t) {
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.*,
|
||||
.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,
|
||||
};
|
||||
@@ -379,7 +380,7 @@ const Lowerer = struct {
|
||||
.never => "never",
|
||||
.named => |i| if (source_names and l.m.types[i].air_name != null) (l.m.types[i].display_name orelse l.m.types[i].name) else l.typeNames[i],
|
||||
.array => |a| std.fmt.allocPrint(l.arena, "array<{s}, {d}>", .{ l.typeText(a.elem, source_names), a.len }) catch "array",
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| std.fmt.allocPrint(l.arena, "{s}<{s}>", .{ @tagName(t), l.typeText(x.*, source_names) }) catch "borrow",
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| std.fmt.allocPrint(l.arena, "{s}<{s}>", .{ @tagName(t), l.typeText(x.*, source_names) }) catch "borrow",
|
||||
.scope => "scope",
|
||||
.shield => "shield",
|
||||
.region => "region",
|
||||
@@ -487,7 +488,7 @@ const Lowerer = struct {
|
||||
out.* = .{ .elem = try l.airType(a.elem), .len = a.len };
|
||||
break :blk .{ .array = out };
|
||||
},
|
||||
inline .ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |inner, tag| blk: {
|
||||
inline .ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |inner, tag| blk: {
|
||||
const out = try l.arena.create(air_module.Type);
|
||||
out.* = try l.airType(inner.*);
|
||||
break :blk @unionInit(air_module.Type, @tagName(tag), out);
|
||||
@@ -579,6 +580,7 @@ const Lowerer = struct {
|
||||
const rules = [_]Rule{
|
||||
.{ "arrays", &.{ "array<", "slice", "array.splat", "borrow.index" } },
|
||||
.{ "ownership", &.{ "local.", "borrow.", " load ", "= load ", "store ", "ref<", "mut<" } },
|
||||
.{ "linear_buffers", &.{ "buffer<", "buffer." } },
|
||||
.{ "linear_aggregates", &.{ "array.make ", "struct.unpack ", "array.unpack ", "enum.take ", "value.swap ", "own.pack " } },
|
||||
.{ "heap", &.{ "own.", "own<", "own_array<", "alloc_error" } },
|
||||
.{ "regions", &.{"region"} },
|
||||
@@ -663,7 +665,7 @@ const Lowerer = struct {
|
||||
},
|
||||
.array => |a| return l.arrayOf(try l.memberType(a.elem.*), a.len),
|
||||
.wrapped => |w| {
|
||||
const Wr = enum { ref, mut, slice, slice_mut, own, own_array, task, sender, receiver, mutex, island, frozen, arc, iref, atomic };
|
||||
const Wr = enum { ref, mut, slice, slice_mut, own, own_array, buffer, task, sender, receiver, mutex, island, frozen, arc, iref, atomic };
|
||||
const which = std.meta.stringToEnum(Wr, w.wrapper) orelse return l.notYet(t.span, "this type (later layers)");
|
||||
const in = try l.typeOf(w.inner.*);
|
||||
return switch (which) {
|
||||
@@ -673,6 +675,7 @@ const Lowerer = struct {
|
||||
.slice_mut => l.wrap(.slice_mut, in),
|
||||
.own => l.wrap(.own, in),
|
||||
.own_array => l.wrap(.own_array, in),
|
||||
.buffer => l.wrap(.buffer, in),
|
||||
.task => l.wrap(.task, in),
|
||||
.sender => l.wrap(.sender, in),
|
||||
.receiver => l.wrap(.receiver, in),
|
||||
@@ -1082,6 +1085,7 @@ const Lowerer = struct {
|
||||
const scope_start = l.scopeBindingStart();
|
||||
for (l.bindings.items[scope_start..]) |b| if (b.kind == .borrow and std.mem.eql(u8, b.name, x.name))
|
||||
return l.fail("E7006", s.span, "a borrow of this name is already active in this scope", &.{.{ .name = "name", .value = x.name }});
|
||||
if (x.place.kind == .call) if (pathOf(x.place.kind.call.callee.*)) |p| if (std.mem.eql(u8, p, "buffer.view")) return l.bufferView(s, x.name, x.mutable, x.place);
|
||||
if (x.place.kind == .call) if (pathOf(x.place.kind.call.callee.*)) |p| if (std.mem.endsWith(u8, p, ".at"))
|
||||
return l.graphAccess(s, x.name, x.mutable, x.place);
|
||||
const p = try l.uncheckedPlace(x.place, x.mutable, x.unchecked, s.span);
|
||||
@@ -1528,7 +1532,8 @@ const Lowerer = struct {
|
||||
.or_trap => |x| return l.orTrap(e, x.*),
|
||||
.move => |n| {
|
||||
const b = l.lookup(n) orelse return l.unknown(e.span, n);
|
||||
if (!l.isLinear(b.ty)) return l.fail("E7006", e.span, "`move` applies to linear values; copyable values are passed plainly", &.{ .{ .name = "name", .value = n }, .{ .name = "type", .value = l.diagnosticTyText(b.ty) } });
|
||||
if (!l.isLinear(b.ty) and !(e.generic_transfer and b.ty != .void and b.ty != .never and !b.ty.isBorrow() and b.ty != .task and b.ty != .scope and b.ty != .guard and b.ty != .shield)) return l.fail("E7006", e.span, "`move` applies to linear values; copyable values are passed plainly", &.{ .{ .name = "name", .value = n }, .{ .name = "type", .value = l.diagnosticTyText(b.ty) } });
|
||||
if (b.kind == .storage) return l.def(n, b.ty, try l.fmt("local.load %{s}", .{b.value}), e.span);
|
||||
return .{ .name = b.value, .ty = b.ty };
|
||||
},
|
||||
}
|
||||
@@ -1607,6 +1612,14 @@ const Lowerer = struct {
|
||||
}
|
||||
|
||||
fn call(l: *Lowerer, e: ast.Expr, callee: ast.Expr, args: []const ast.Expr) Error!Value {
|
||||
if (pathOf(callee)) |p| {
|
||||
if (std.mem.eql(u8, p, "buffer.len") or std.mem.eql(u8, p, "buffer.capacity")) {
|
||||
if (args.len != 1) return l.fail("E7005", e.span, "buffer metadata read takes one named borrow", &.{});
|
||||
const b = try l.bufferBorrow(args[0], false);
|
||||
return l.def("size", .{ .int = "usize" }, try l.fmt("{s} %{s}", .{ p, b.name }), e.span);
|
||||
}
|
||||
if (std.mem.eql(u8, p, "buffer.view")) return l.fail("E7006", e.span, "bind a buffer view with ref/mut name = buffer.view(borrow)", &.{});
|
||||
}
|
||||
if (pathOf(callee)) |path| if (std.mem.eql(u8, path, "value.swap")) {
|
||||
if (args.len != 2 or args[0].kind != .borrow or !args[0].kind.borrow.mutable) return l.fail("E7006", e.span, "value.swap requires mut place and replacement", &.{});
|
||||
const b = args[0].kind.borrow;
|
||||
@@ -1956,6 +1969,7 @@ const Lowerer = struct {
|
||||
/// without lowering it.
|
||||
fn peekPath(l: *Lowerer, p: []const u8, args: []const ast.Expr) ?Ty {
|
||||
const eq = std.mem.eql;
|
||||
if (eq(u8, p, "buffer.len") or eq(u8, p, "buffer.capacity")) return .{ .int = "usize" };
|
||||
if ((eq(u8, p, "own.take") or eq(u8, p, "task.join")) and args.len == 1) return if (l.peekType(args[0])) |t| t.inner() else null;
|
||||
if (eq(u8, p, "task.cancelled")) return .bool;
|
||||
if (std.mem.startsWith(u8, p, "atomic.") and args.len >= 1) {
|
||||
@@ -2139,6 +2153,7 @@ const Lowerer = struct {
|
||||
.named, .array => if (l.isLinear(b.ty)) (if (mutable) try l.wrap(.mut, b.ty) else try l.wrap(.ref, b.ty)) else null,
|
||||
.own => |in| if (mutable) try l.wrap(.mut, in.*) else try l.wrap(.ref, in.*),
|
||||
.own_array => |in| if (mutable) try l.wrap(.slice_mut, in.*) else try l.wrap(.slice, in.*),
|
||||
.buffer => if (mutable) try l.wrap(.mut, b.ty) else try l.wrap(.ref, b.ty),
|
||||
.region => if (mutable) try l.wrap(.mut, .region) else try l.wrap(.ref, .region),
|
||||
.resource, .mutex, .sender, .receiver, .task, .island => if (mutable) try l.wrap(.mut, b.ty) else try l.wrap(.ref, b.ty),
|
||||
.frozen, .arc => if (mutable) return l.fail("E7005", place.span, "frozen graphs and arcs are immutable: borrow them with `ref`", &.{.{ .name = "name", .value = n }}) else try l.wrap(.ref, b.ty),
|
||||
@@ -2425,7 +2440,7 @@ const Lowerer = struct {
|
||||
|
||||
fn fallible(l: *Lowerer, path: []const u8) ?void {
|
||||
_ = l;
|
||||
const ops = [_][]const u8{ "own.new", "own.pack", "own.array", "region.create", "region.new", "region.array", "cap.narrow.budget", "cap.narrow.prefix", "channel.make", "channel.send", "channel.recv", "channel.close_receiver", "task.check_cancel", "task.join_cancel", "island.create", "island.alloc", "arc.new" };
|
||||
const ops = [_][]const u8{ "buffer.new", "buffer.reserve", "buffer.push", "buffer.pop", "buffer.free", "own.new", "own.pack", "own.array", "region.create", "region.new", "region.array", "cap.narrow.budget", "cap.narrow.prefix", "channel.make", "channel.send", "channel.recv", "channel.close_receiver", "task.check_cancel", "task.join_cancel", "island.create", "island.alloc", "arc.new" };
|
||||
for (ops) |o| if (std.mem.eql(u8, o, path)) return {};
|
||||
if (authority.intrinsicIndex(path)) |i| if (authority.intrinsics[i].fallible) return {};
|
||||
return null;
|
||||
@@ -2860,7 +2875,7 @@ const Lowerer = struct {
|
||||
return switch (t) {
|
||||
.ref, .mut, .slice, .slice_mut, .scope, .task, .shield, .guard => t,
|
||||
.array => |a| l.forbiddenIn(a.elem, depth + 1),
|
||||
.own, .own_array, .mutex => |x| l.forbiddenIn(x.*, depth + 1),
|
||||
.own, .own_array, .buffer, .mutex => |x| l.forbiddenIn(x.*, depth + 1),
|
||||
.named => |i| blk: {
|
||||
const d = l.m.types[i];
|
||||
switch (d.kind) {
|
||||
@@ -2886,8 +2901,33 @@ const Lowerer = struct {
|
||||
return .{ .name = b.value, .ty = b.ty };
|
||||
}
|
||||
|
||||
/// `ref x = island.at(g, r);` / `mut x = island.at(g, r);`, and
|
||||
/// `frozen.at`, `arc.at` (`ref` only): a child borrow of `g`.
|
||||
/// Buffer operations require an explicit named borrow of the handle.
|
||||
fn bufferBorrow(l: *Lowerer, e: ast.Expr, mutable: bool) Error!Value {
|
||||
if (e.kind == .name) if (l.lookup(e.kind.name)) |b| {
|
||||
if ((b.ty == .mut or (!mutable and b.ty == .ref)) and b.ty.inner().? == .buffer) return .{ .name = b.value, .ty = b.ty };
|
||||
};
|
||||
return l.fail("E7005", e.span, if (mutable) "bind a named mut<buffer<T>> first" else "bind a named ref/mut<buffer<T>> first", &.{});
|
||||
}
|
||||
|
||||
fn bufferView(l: *Lowerer, s: ast.Stmt, name_: []const u8, mutable: bool, place: ast.Expr) Error!void {
|
||||
const args = place.kind.call.args;
|
||||
if (args.len != 1) return l.fail("E7005", place.span, "buffer.view takes one named buffer borrow", &.{});
|
||||
const parent = try l.bufferBorrow(args[0], mutable);
|
||||
// A shared view from a mutable parent uses an explicit shared reborrow.
|
||||
var borrow = parent;
|
||||
var shared_end: ?[]const u8 = null;
|
||||
if (!mutable and parent.ty == .mut) {
|
||||
borrow = try l.def("buffer_ref", try l.wrap(.ref, parent.ty.inner().?), try l.fmt("borrow.ref %{s}", .{parent.name}), place.span);
|
||||
shared_end = borrow.name;
|
||||
}
|
||||
const ty = if (mutable) try l.wrap(.slice_mut, parent.ty.inner().?.buffer.*) else try l.wrap(.slice, parent.ty.inner().?.buffer.*);
|
||||
const view = try l.def(name_, ty, try l.fmt("buffer.view %{s}", .{borrow.name}), place.span);
|
||||
if (shared_end) |n| try l.scopeEnds(&.{n}, s.span);
|
||||
try l.scopeEnds(&.{view.name}, s.span);
|
||||
try l.bindings.append(l.arena, .{ .name = name_, .mutable = false, .value = view.name, .ty = ty, .kind = .borrow });
|
||||
}
|
||||
|
||||
/// Graph access binds a child borrow of its named graph borrow.
|
||||
fn graphAccess(l: *Lowerer, s: ast.Stmt, name: []const u8, mutable: bool, place: ast.Expr) Error!void {
|
||||
const ac = place.kind.call;
|
||||
const p = pathOf(ac.callee.*).?;
|
||||
@@ -2996,6 +3036,38 @@ const Lowerer = struct {
|
||||
}.n;
|
||||
const alloc_err: Ty = .{ .builtin = .alloc_error };
|
||||
const usize_ty: Ty = .{ .int = "usize" };
|
||||
if (std.mem.eql(u8, path, "buffer.new")) {
|
||||
try want(l, e.span, args, 2, path);
|
||||
const source_ty = e.kind.call.type_arg orelse return l.fail("E7005", e.span, "buffer.new needs an explicit content type", &.{});
|
||||
const ty = try l.typeOf(source_ty);
|
||||
const n = try l.expr(args[0], usize_ty);
|
||||
try l.checkTy(args[0].span, n.ty, usize_ty);
|
||||
const cap = try l.expectCap(args[1], "mem.alloc");
|
||||
return .{ .text = try l.fmt("buffer.new {s} %{s}, %{s}", .{ l.tyText(ty), n.name, cap.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.buffer, ty)} }, .{ .role = "err", .params = &.{alloc_err} } }) };
|
||||
}
|
||||
if (std.mem.eql(u8, path, "buffer.free")) {
|
||||
try want(l, e.span, args, 1, path);
|
||||
const v = try l.consuming(args[0], null);
|
||||
if (v.ty != .buffer) return l.fail("E7005", e.span, "buffer.free consumes a buffer<T>", &.{});
|
||||
return .{ .text = try l.fmt("buffer.free %{s}", .{v.name}), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{} }, .{ .role = "err", .params = &.{v.ty} } }) };
|
||||
}
|
||||
if (std.mem.eql(u8, path, "buffer.push") or std.mem.eql(u8, path, "buffer.pop") or std.mem.eql(u8, path, "buffer.reserve")) {
|
||||
const is_push = std.mem.eql(u8, path, "buffer.push");
|
||||
const is_pop = std.mem.eql(u8, path, "buffer.pop");
|
||||
try want(l, e.span, args, if (is_push) 2 else if (is_pop) 1 else 3, path);
|
||||
const b = try l.bufferBorrow(args[0], true);
|
||||
const ty = b.ty.inner().?.buffer.*;
|
||||
if (is_push) {
|
||||
const v = try l.consuming(args[1], ty);
|
||||
try l.checkTy(args[1].span, v.ty, ty);
|
||||
return .{ .text = try l.fmt("buffer.push %{s}, %{s}", .{ b.name, v.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{} }, .{ .role = "err", .params = &.{ty} } }) };
|
||||
}
|
||||
if (is_pop) return .{ .text = try l.fmt("buffer.pop %{s}", .{b.name}), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{ty} }, .{ .role = "err", .params = &.{} } }) };
|
||||
const n = try l.expr(args[1], usize_ty);
|
||||
try l.checkTy(args[1].span, n.ty, usize_ty);
|
||||
const cap = try l.expectCap(args[2], "mem.alloc");
|
||||
return .{ .text = try l.fmt("buffer.reserve %{s}, %{s}, %{s}", .{ b.name, n.name, cap.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{} }, .{ .role = "err", .params = &.{alloc_err} } }) };
|
||||
}
|
||||
if (std.mem.eql(u8, path, "own.pack")) {
|
||||
try want(l, e.span, args, 2, path);
|
||||
const v = try l.consuming(args[0], null);
|
||||
@@ -3296,20 +3368,14 @@ fn elemOf(t: Ty) ?Ty {
|
||||
/// `cap.narrow.budget`; null for anything else.
|
||||
fn pathOf(e: ast.Expr) ?[]const u8 {
|
||||
const known = [_][]const u8{
|
||||
"struct.unpack", "array.unpack", "value.swap", "own.pack",
|
||||
"slice.copy", "own.new", "own.array", "own.take",
|
||||
"own.free", "region.create", "region.new", "region.array",
|
||||
"region.reset", "region.destroy", "cap.narrow.budget", "cap.narrow.prefix",
|
||||
"task.spawn", "task.join", "task.cancel", "task.cancelled",
|
||||
"channel.make", "channel.dup", "channel.send", "channel.recv",
|
||||
"channel.close_sender", "channel.close_receiver", "mutex.make", "mutex.take",
|
||||
"task.check_cancel", "task.join_cancel", "island.create", "island.alloc",
|
||||
"island.root", "island.at", "island.freeze", "island.destroy",
|
||||
"frozen.root", "frozen.at", "frozen.release", "arc.from_frozen",
|
||||
"arc.new", "arc.clone", "arc.release", "arc.root",
|
||||
"arc.at", "atomic.new", "atomic.take", "atomic.load",
|
||||
"atomic.store", "atomic.swap", "atomic.add.wrap", "atomic.sub.wrap",
|
||||
"atomic.and", "atomic.or", "atomic.xor", "atomic.compare_exchange",
|
||||
"buffer.new", "buffer.len", "buffer.capacity", "buffer.reserve", "buffer.push", "buffer.pop", "buffer.view", "buffer.free",
|
||||
"struct.unpack", "array.unpack", "value.swap", "own.pack", "slice.copy", "own.new", "own.array", "own.take",
|
||||
"own.free", "region.create", "region.new", "region.array", "region.reset", "region.destroy", "cap.narrow.budget", "cap.narrow.prefix",
|
||||
"task.spawn", "task.join", "task.cancel", "task.cancelled", "channel.make", "channel.dup", "channel.send", "channel.recv",
|
||||
"channel.close_sender", "channel.close_receiver", "mutex.make", "mutex.take", "task.check_cancel", "task.join_cancel", "island.create", "island.alloc",
|
||||
"island.root", "island.at", "island.freeze", "island.destroy", "frozen.root", "frozen.at", "frozen.release", "arc.from_frozen",
|
||||
"arc.new", "arc.clone", "arc.release", "arc.root", "arc.at", "atomic.new", "atomic.take", "atomic.load",
|
||||
"atomic.store", "atomic.swap", "atomic.add.wrap", "atomic.sub.wrap", "atomic.and", "atomic.or", "atomic.xor", "atomic.compare_exchange",
|
||||
"atomic.fence",
|
||||
};
|
||||
var buf: [64]u8 = undefined;
|
||||
|
||||
@@ -13,7 +13,7 @@ const Node = struct { module: ast.Module, active: bool, height: usize = 0 };
|
||||
|
||||
pub fn reserved(name: []const u8) bool {
|
||||
const first = if (std.mem.indexOfScalar(u8, name, '.')) |at| name[0..at] else name;
|
||||
for ([_][]const u8{ "std", "slice", "own", "region", "cap", "task", "channel", "mutex", "island", "frozen", "arc", "atomic" }) |word| {
|
||||
for ([_][]const u8{ "std", "slice", "buffer", "own", "region", "cap", "task", "channel", "mutex", "island", "frozen", "arc", "atomic" }) |word| {
|
||||
if (std.mem.eql(u8, first, word)) return true;
|
||||
}
|
||||
for (authority.intrinsics) |intrinsic| {
|
||||
|
||||
@@ -263,7 +263,7 @@ const Parser = struct {
|
||||
return .{ .span = p.span(start), .kind = if (std.mem.eql(u8, name, "cap")) .{ .cap = value } else .{ .file = value } };
|
||||
}
|
||||
const args = try p.typeArgs();
|
||||
const wrappers = [_][]const u8{ "ref", "mut", "slice", "slice_mut", "own", "own_array", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "iref", "atomic" };
|
||||
const wrappers = [_][]const u8{ "ref", "mut", "slice", "slice_mut", "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "island", "frozen", "arc", "iref", "atomic" };
|
||||
for (wrappers) |wrapper| if (std.mem.eql(u8, name, wrapper)) {
|
||||
if (args.len != 1) return p.fail(t, "one wrapper type argument");
|
||||
return .{ .span = p.span(start), .kind = .{ .wrapped = .{ .wrapper = name, .inner = try p.box(ast.Type, args[0]) } } };
|
||||
@@ -701,7 +701,7 @@ const Parser = struct {
|
||||
const base = try p.box(ast.Expr, e);
|
||||
e = .{ .span = p.span(start), .kind = .{ .field = .{ .e = base, .name = name } } };
|
||||
// `channel.make<T>(…)`: the only explicit type argument.
|
||||
if (std.mem.eql(u8, name, "make") and p.peek().tag == .lt and e.kind.field.e.kind == .name and std.mem.eql(u8, e.kind.field.e.kind.name, "channel")) {
|
||||
if (p.peek().tag == .lt and e.kind.field.e.kind == .name and ((std.mem.eql(u8, name, "make") and std.mem.eql(u8, e.kind.field.e.kind.name, "channel")) or (std.mem.eql(u8, name, "new") and std.mem.eql(u8, e.kind.field.e.kind.name, "buffer")))) {
|
||||
_ = p.next();
|
||||
const ty = try p.typeExpr();
|
||||
try p.closeAngle();
|
||||
|
||||
@@ -11,4 +11,5 @@ pub const registry = [_]Source{
|
||||
.{ .name = "std.option", .file = "std/option.aji", .source = @embedFile("std_option_aji") },
|
||||
.{ .name = "std.result", .file = "std/result.aji", .source = @embedFile("std_result_aji") },
|
||||
.{ .name = "std.utf8", .file = "std/utf8.aji", .source = @embedFile("std_utf8_aji") },
|
||||
.{ .name = "std.vec", .file = "std/vec.aji", .source = @embedFile("std_vec_aji") },
|
||||
};
|
||||
+109
-1
@@ -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();
|
||||
|
||||
+1
-1
@@ -64,7 +64,7 @@ fn analyze(arena: std.mem.Allocator, m: air.Module, f: air.Function) !FnInfo {
|
||||
_ = try addOnce(arena, &info.intrinsics, reg.name);
|
||||
if (reg.effect) |e| _ = try addOnce(arena, &info.direct, e);
|
||||
},
|
||||
.own_new, .own_pack, .own_array, .region_new, .region_array, .region_create, .channel_make, .island_create, .island_alloc, .arc_new => {
|
||||
.buffer_new, .buffer_reserve, .own_new, .own_pack, .own_array, .region_new, .region_array, .region_create, .channel_make, .island_create, .island_alloc, .arc_new => {
|
||||
info.allocations += 1;
|
||||
_ = try addOnce(arena, &info.direct, "mem.alloc");
|
||||
},
|
||||
|
||||
+15
-1
@@ -242,6 +242,11 @@ pub fn overlap(a: []const Seg, b: []const Seg) bool {
|
||||
const Edge = struct { target: u32, args: []const ValueId };
|
||||
|
||||
fn edges(op: air.Op, out: *std.ArrayList(Edge), arena: std.mem.Allocator) !void {
|
||||
if (op.bufferTargets()) |t| {
|
||||
try out.append(arena, .{ .target = t.ok, .args = &.{} });
|
||||
try out.append(arena, .{ .target = t.err, .args = &.{} });
|
||||
return;
|
||||
}
|
||||
switch (op) {
|
||||
.br => |t| try out.append(arena, .{ .target = t.block, .args = t.args }),
|
||||
.cbr => |x| {
|
||||
@@ -385,7 +390,7 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
},
|
||||
.borrow_field => |x| try w.derive(inst.result.?, x.borrow, .{ .field = x.field }),
|
||||
.borrow_index => |x| try w.derive(inst.result.?, x.borrow, .elem),
|
||||
.slice_make => |v| try w.derive(inst.result.?, v, null),
|
||||
.buffer_view, .slice_make => |v| try w.derive(inst.result.?, v, null),
|
||||
.slice_sub => |x| try w.derive(inst.result.?, x.slice, .elem),
|
||||
.slice_split => |x| {
|
||||
const l = inst.result.?;
|
||||
@@ -452,6 +457,15 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
.enum_take => |x| {
|
||||
if (w.isResource(x.value)) try w.consume(x.value, .moved);
|
||||
},
|
||||
.buffer_new => {},
|
||||
.buffer_len, .buffer_capacity => |v| try w.useThrough(v, .shared),
|
||||
.buffer_reserve => |x| try w.useThrough(x.borrow, .exclusive),
|
||||
.buffer_push => |x| {
|
||||
try w.useThrough(x.borrow, .exclusive);
|
||||
if (w.isResource(x.value)) try w.consume(x.value, .moved);
|
||||
},
|
||||
.buffer_pop => |x| try w.useThrough(x.value, .exclusive),
|
||||
.buffer_free => |x| try w.consume(x.value, .moved),
|
||||
.own_pack => |x| {
|
||||
if (w.isResource(x.value)) try w.consume(x.value, .moved);
|
||||
},
|
||||
|
||||
@@ -33,6 +33,7 @@ pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
|
||||
.atomic => |inner| accountedSize(m, inner.*, depth + 1),
|
||||
.mutex => |inner| std.math.add(u64, 8, accountedSize(m, inner.*, depth + 1) orelse return null) catch null,
|
||||
.own_array => 16,
|
||||
.buffer => 24,
|
||||
.slice, .slice_mut => 16,
|
||||
.builtin => |b| tagSize(b.variants().len),
|
||||
.array => |a| std.math.mul(u64, a.len, accountedSize(m, a.elem, depth + 1) orelse return null) catch null,
|
||||
@@ -151,7 +152,7 @@ fn checkValueSizes(c: *verifier.Context) !void {
|
||||
for (b.params) |p| try v.visit(b.span, p.ty);
|
||||
for (b.insts) |inst| switch (inst.op) {
|
||||
.local_alloc => |t| try v.visit(inst.span, t),
|
||||
inline .array_splat, .array_make, .aggregate_unpack, .own_pack => |x| try v.visit(inst.span, x.ty),
|
||||
inline .array_splat, .array_make, .aggregate_unpack, .own_pack, .buffer_new => |x| try v.visit(inst.span, x.ty),
|
||||
else => {},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -83,6 +83,26 @@ fn checkTerminator(c: *Context, f: air.Function, inst: air.Inst) !void {
|
||||
};
|
||||
try paramCount(c, f, inst.span, x.next, n, "unpack target must receive every component");
|
||||
},
|
||||
.buffer_new => |x| {
|
||||
try paramCount(c, f, inst.span, x.ok, 1, "buffer.new success takes buffer");
|
||||
try paramCount(c, f, inst.span, x.err, 1, "buffer.new failure takes alloc_error");
|
||||
},
|
||||
.buffer_reserve => |x| {
|
||||
try paramCount(c, f, inst.span, x.ok, 0, "buffer.reserve success takes no values");
|
||||
try paramCount(c, f, inst.span, x.err, 1, "buffer.reserve failure takes alloc_error");
|
||||
},
|
||||
.buffer_push => |x| {
|
||||
try paramCount(c, f, inst.span, x.ok, 0, "buffer.push success takes no values");
|
||||
try paramCount(c, f, inst.span, x.err, 1, "buffer.push failure returns value");
|
||||
},
|
||||
.buffer_pop => |x| {
|
||||
try paramCount(c, f, inst.span, x.ok, 1, "buffer.pop success takes value");
|
||||
try paramCount(c, f, inst.span, x.err, 0, "buffer.pop empty takes no values");
|
||||
},
|
||||
.buffer_free => |x| {
|
||||
try paramCount(c, f, inst.span, x.ok, 0, "buffer.free success takes no values");
|
||||
try paramCount(c, f, inst.span, x.err, 1, "buffer.free nonempty returns buffer");
|
||||
},
|
||||
.own_pack => |x| {
|
||||
try paramCount(c, f, inst.span, x.ok, 1, "own.pack success takes owner");
|
||||
try paramCount(c, f, inst.span, x.err, 2, "own.pack failure takes error and original value");
|
||||
|
||||
@@ -57,7 +57,7 @@ pub fn check(c: *Context) !void {
|
||||
.atomic_store => |x| try needAtomic(c, f, inst, x.ord != .seq_cst),
|
||||
.atomic_rmw => |x| try needAtomic(c, f, inst, x.ord != .seq_cst),
|
||||
.atomic_cmpxchg => |x| try needAtomic(c, f, inst, x.success != .seq_cst or x.failure != .seq_cst),
|
||||
.own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => if (!contains(f.effects, "mem.alloc")) {
|
||||
.buffer_new, .buffer_reserve, .own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => if (!contains(f.effects, "mem.alloc")) {
|
||||
try c.report("E3101", inst.span, "allocation requires the effect mem.alloc", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "effect", .value = .{ .str = "mem.alloc" } },
|
||||
|
||||
+48
-10
@@ -48,7 +48,7 @@ fn checkDeclarations(c: *Context) !void {
|
||||
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),
|
||||
inline .array_splat, .array_make, .aggregate_unpack, .own_pack => |x| try wellFormed(c, inst.span, x.ty, .param, f.name),
|
||||
inline .array_splat, .array_make, .aggregate_unpack, .own_pack, .buffer_new => |x| try wellFormed(c, inst.span, x.ty, .param, f.name),
|
||||
else => {},
|
||||
};
|
||||
}
|
||||
@@ -85,7 +85,7 @@ fn visitValue(c: *Context, state: anytype, t: Type) std.mem.Allocator.Error!void
|
||||
switch (t) {
|
||||
.named => |i| try visitDecl(c, state, i),
|
||||
.array => |a| try visitValue(c, state, a.elem),
|
||||
.own, .own_array, .sender, .receiver, .mutex, .ref, .mut, .slice, .slice_mut, .guard, .task, .local => |inner| if (c.module.declares("linear_aggregates")) try visitValue(c, state, inner.*),
|
||||
.own, .own_array, .buffer, .sender, .receiver, .mutex, .ref, .mut, .slice, .slice_mut, .guard, .task, .local => |inner| if (c.module.declares("linear_aggregates")) try visitValue(c, state, inner.*),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
@@ -104,7 +104,7 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
|
||||
.local => false,
|
||||
.ref, .mut, .slice, .slice_mut => pos != .member and pos != .borrowed,
|
||||
.scope, .task, .shield, .guard => pos != .member,
|
||||
.resource, .own, .own_array, .region, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => pos != .member or c.module.declares("linear_aggregates"),
|
||||
.resource, .own, .own_array, .buffer, .region, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => pos != .member or c.module.declares("linear_aggregates"),
|
||||
else => true,
|
||||
};
|
||||
if (t == .own_array and c.module.declares("linear_aggregates") and c.module.isLinearType(t.own_array.*)) try c.report("E1001", span, "dynamic heap arrays require Copy content", &.{});
|
||||
@@ -266,7 +266,7 @@ 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 },
|
||||
.slice_len, .buffer_len, .buffer_capacity => .{ .int = .usize },
|
||||
.task_scope_begin => .scope,
|
||||
.task_cancelled => .bool,
|
||||
.task_shield_begin => .shield,
|
||||
@@ -382,7 +382,7 @@ pub fn isCopy(m: air.Module, t: Type) bool {
|
||||
pub fn isLocalType(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.local, .task, .scope, .shield, .guard => true,
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .mutex => |inner| isLocalType(m, inner.*),
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .mutex => |inner| isLocalType(m, inner.*),
|
||||
.array => |a| isLocalType(m, a.elem),
|
||||
.named => |i| switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
@@ -405,7 +405,7 @@ pub fn isSend(m: air.Module, t: Type) bool {
|
||||
.island, .frozen, .arc, .iref => true,
|
||||
// Every access to a cell is an atomic operation (spec 04 §23).
|
||||
.atomic => true,
|
||||
.own, .own_array, .mutex => |inner| isSend(m, inner.*),
|
||||
.own, .own_array, .buffer, .mutex => |inner| isSend(m, inner.*),
|
||||
.ref, .slice => |inner| isShare(m, inner.*),
|
||||
.mut, .slice_mut => |inner| isSend(m, inner.*),
|
||||
.array => |a| isSend(m, a.elem),
|
||||
@@ -422,7 +422,7 @@ pub fn isShare(m: air.Module, t: Type) bool {
|
||||
// Its owner may mutate it: freeze to share (spec 04 §35).
|
||||
.island => false,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never => false,
|
||||
.own, .own_array, .ref, .slice => |inner| isShare(m, inner.*),
|
||||
.own, .own_array, .buffer, .ref, .slice => |inner| isShare(m, inner.*),
|
||||
// The lock synchronizes access (spec 04 §35, §40).
|
||||
.mutex => |inner| isSend(m, inner.*),
|
||||
.array => |a| isShare(m, a.elem),
|
||||
@@ -602,6 +602,13 @@ fn checkSelect(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "select")) !voi
|
||||
_ = f;
|
||||
}
|
||||
|
||||
fn bufferElement(fc: *FnCtx, span: air.Span, v: air.ValueId, mutable: bool) !?Type {
|
||||
const t = fc.typeOf(v);
|
||||
if ((t == .mut or (!mutable and t == .ref)) and t.inner().? == .buffer) return t.inner().?.buffer.*;
|
||||
try fc.kindError(span, v, if (mutable) "mut<buffer<T>>" else "ref<buffer<T>> or mut<buffer<T>>");
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn complete(t: Type) bool {
|
||||
return t != .void and t != .never and t != .local and !t.isBorrow() and !t.isTaskBound() and t != .guard;
|
||||
}
|
||||
@@ -622,7 +629,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.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,
|
||||
.buffer_view, .borrow_ref, .borrow_mut, .load, .slice_make => |v| fc.ty[v] orelse return null,
|
||||
.value_swap => |x| fc.ty[x.borrow] orelse return null,
|
||||
.borrow_field => |x| fc.ty[x.borrow] orelse return null,
|
||||
.borrow_index => |x| fc.ty[x.borrow] orelse return null,
|
||||
@@ -644,12 +651,13 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
};
|
||||
const invalid: Type = .void;
|
||||
return switch (op) {
|
||||
.buffer_view => if ((t == .ref or t == .mut) and t.inner().? == .buffer) (if (t == .mut) .{ .slice_mut = try boxed(a, t.inner().?.buffer.*) } else .{ .slice = try boxed(a, t.inner().?.buffer.*) }) else invalid,
|
||||
.value_swap => if (t == .mut) t.mut.* else invalid,
|
||||
.local_load => if (t == .local) t.local.* else invalid,
|
||||
.borrow_ref => switch (t) {
|
||||
.local, .ref, .mut => .{ .ref = try boxed(a, t.inner().?) },
|
||||
.named, .array => if (m.isLinearType(t)) .{ .ref = try boxed(a, t) } else invalid,
|
||||
.resource, .region, .task, .scope, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => .{ .ref = try boxed(a, t) },
|
||||
.resource, .region, .task, .scope, .sender, .receiver, .mutex, .buffer, .island, .frozen, .arc, .atomic => .{ .ref = try boxed(a, t) },
|
||||
.own => |inner| .{ .ref = inner },
|
||||
.own_array => |inner| .{ .slice = inner },
|
||||
.slice, .slice_mut => .{ .slice = try boxed(a, t.inner().?) },
|
||||
@@ -658,7 +666,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.borrow_mut => switch (t) {
|
||||
.local, .mut => .{ .mut = try boxed(a, t.inner().?) },
|
||||
.named, .array => if (m.isLinearType(t)) .{ .mut = try boxed(a, t) } else invalid,
|
||||
.resource, .region, .task, .scope, .sender, .receiver, .island => .{ .mut = try boxed(a, t) },
|
||||
.resource, .region, .task, .scope, .sender, .receiver, .buffer, .island => .{ .mut = try boxed(a, t) },
|
||||
.own => |inner| .{ .mut = inner },
|
||||
.own_array => |inner| .{ .slice_mut = inner },
|
||||
.slice_mut => .{ .slice_mut = try boxed(a, t.inner().?) },
|
||||
@@ -831,6 +839,36 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
try fc.expectValue(span, x.value, t.mut.*);
|
||||
if (!complete(t.mut.*)) try fc.kindError(span, x.value, "a complete value");
|
||||
},
|
||||
.buffer_new => |x| {
|
||||
try fc.expectValue(span, x.n, .{ .int = .usize });
|
||||
try fc.expectAllocCap(span, x.cap);
|
||||
if (!complete(x.ty)) return fc.kindError(span, x.n, "complete buffer content");
|
||||
try fc.expectParams(span, x.ok, &.{.{ .buffer = try boxed(fc.c.arena, x.ty) }});
|
||||
try fc.expectParams(span, x.err, &.{.{ .builtin = .alloc_error }});
|
||||
},
|
||||
.buffer_len, .buffer_capacity, .buffer_view => |v| {
|
||||
_ = try bufferElement(fc, span, v, false);
|
||||
},
|
||||
.buffer_reserve => |x| {
|
||||
_ = try bufferElement(fc, span, x.borrow, true);
|
||||
try fc.expectValue(span, x.n, .{ .int = .usize });
|
||||
try fc.expectAllocCap(span, x.cap);
|
||||
try fc.expectParams(span, x.err, &.{.{ .builtin = .alloc_error }});
|
||||
},
|
||||
.buffer_push => |x| {
|
||||
const t = try bufferElement(fc, span, x.borrow, true) orelse return;
|
||||
try fc.expectValue(span, x.value, t);
|
||||
try fc.expectParams(span, x.err, &.{t});
|
||||
},
|
||||
.buffer_pop => |x| {
|
||||
const t = try bufferElement(fc, span, x.value, true) orelse return;
|
||||
try fc.expectParams(span, x.ok, &.{t});
|
||||
},
|
||||
.buffer_free => |x| {
|
||||
const t = fc.typeOf(x.value);
|
||||
if (t != .buffer) return fc.kindError(span, x.value, "buffer<T>");
|
||||
try fc.expectParams(span, x.err, &.{t});
|
||||
},
|
||||
.array_make => |x| {
|
||||
if (x.ty != .array) return fc.c.report("E1001", span, "array.make needs an array type", &.{});
|
||||
if (x.args.len != x.ty.array.len) return fc.c.report("E1001", span, "array.make requires every element", &.{});
|
||||
|
||||
Reference in new issue
Block a user