101 lines
3.4 KiB
Zig
101 lines
3.4 KiB
Zig
//! Function-local resource relations, independent of SSA spelling.
|
|
//! Facts join by union; edge renaming is simultaneous in both positions.
|
|
//! Shield order additionally has an exact-state join rule (spec 04 §37.3).
|
|
const std = @import("std");
|
|
const air = @import("../air.zig");
|
|
|
|
pub const Kind = enum { loan, scope, guard, inner };
|
|
pub const Fact = struct {
|
|
kind: Kind,
|
|
a: air.ValueId,
|
|
b: air.ValueId,
|
|
// Meaningful only for loans. Never changes their transfer semantics.
|
|
spawn: air.Span = .{ .line = 0, .col = 0 },
|
|
};
|
|
|
|
pub const Event = union(enum) {
|
|
add: Fact,
|
|
kill: air.ValueId,
|
|
// The cancelled join replaces a task handle without ending its loans.
|
|
rename: struct { from: air.ValueId, to: air.ValueId },
|
|
};
|
|
|
|
pub const State = struct {
|
|
items: std.ArrayList(Fact) = .empty,
|
|
set: std.AutoHashMapUnmanaged(Fact, void) = .empty,
|
|
|
|
pub fn add(s: *State, arena: std.mem.Allocator, fact: Fact) !bool {
|
|
const r = try s.set.getOrPut(arena, fact);
|
|
if (r.found_existing) return false;
|
|
try s.items.append(arena, fact);
|
|
return true;
|
|
}
|
|
|
|
pub fn merge(s: *State, arena: std.mem.Allocator, other: State) !bool {
|
|
var changed = false;
|
|
for (other.items.items) |fact| changed = (try s.add(arena, fact)) or changed;
|
|
return changed;
|
|
}
|
|
|
|
pub fn clone(s: State, arena: std.mem.Allocator) !State {
|
|
var out: State = .{};
|
|
_ = try out.merge(arena, s);
|
|
return out;
|
|
}
|
|
|
|
pub fn kill(s: *State, v: air.ValueId) void {
|
|
var n: usize = 0;
|
|
for (s.items.items) |fact| {
|
|
if (fact.a == v or fact.b == v) {
|
|
_ = s.set.remove(fact);
|
|
} else {
|
|
s.items.items[n] = fact;
|
|
n += 1;
|
|
}
|
|
}
|
|
s.items.shrinkRetainingCapacity(n);
|
|
}
|
|
|
|
pub fn apply(s: *State, arena: std.mem.Allocator, event: Event) !void {
|
|
switch (event) {
|
|
.add => |f| _ = try s.add(arena, f),
|
|
.kill => |v| s.kill(v),
|
|
.rename => |r| {
|
|
var out: State = .{};
|
|
for (s.items.items) |old| {
|
|
var f = old;
|
|
if (f.a == r.from) f.a = r.to;
|
|
if (f.b == r.from) f.b = r.to;
|
|
_ = try out.add(arena, f);
|
|
}
|
|
s.* = out;
|
|
},
|
|
}
|
|
}
|
|
|
|
pub fn sameShields(a: State, b: State) bool {
|
|
return a.differentShield(b) == null;
|
|
}
|
|
|
|
pub fn differentShield(a: State, b: State) ?air.ValueId {
|
|
for (a.items.items) |f| if (f.kind == .inner and !b.set.contains(f)) return f.a;
|
|
for (b.items.items) |f| if (f.kind == .inner and !a.set.contains(f)) return f.a;
|
|
return null;
|
|
}
|
|
};
|
|
|
|
test "relations retain loan origin, join once and remove both endpoints" {
|
|
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
|
defer arena.deinit();
|
|
const a = arena.allocator();
|
|
var s: State = .{};
|
|
const loan: Fact = .{ .kind = .loan, .a = 1, .b = 2, .spawn = .{ .line = 7, .col = 3 } };
|
|
try std.testing.expect(try s.add(a, loan));
|
|
try std.testing.expect(!try s.add(a, loan));
|
|
try s.apply(a, .{ .rename = .{ .from = 1, .to = 4 } });
|
|
try std.testing.expectEqual(@as(air.ValueId, 4), s.items.items[0].a);
|
|
try std.testing.expectEqual(loan.spawn, s.items.items[0].spawn);
|
|
s.kill(2);
|
|
try std.testing.expectEqual(@as(usize, 0), s.items.items.len);
|
|
}
|