summaryrefslogtreecommitdiff
path: root/anim/src/aligned.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-11 23:46:02 -0300
committerGabriel Schneider <[email protected]>2026-11-12 01:40:04 -0300
commit60e07feef401d9044db3a1c925cb9d1f6d20dac9 (patch)
tree98957ba8425193f686c8e8692cd10a10f27dbf4b /anim/src/aligned.zig
parent9c0696447dbc5cf37e56aee562c7ebba4f4d4e6b (diff)
downloadcomputeranim-60e07feef401d9044db3a1c925cb9d1f6d20dac9.tar.gz
computeranim-60e07feef401d9044db3a1c925cb9d1f6d20dac9.zip
canim: relations as one data-oriented subsystem; aim; the ASan pair; the introduction's chapters
From the user. Relations between panes are one subsystem: per-frame tables of places and values in struct-of-arrays, owned by the frame; the kinds one table of data; a resolver of pure passes (join, filter, priority, cut) unit-tested on hand-written tables. Ported kinds draw byte-identical frames; a synthetic map of 4000 places and 6000 values resolves in 0.56 ms against the old table's 5.7. aim, a word holding a code address tied to its row, is the first new kind, off unless a scene turns it on. The ASan pair: one overflow built plain and with ASan, recorded by instruction; the frame as bytes eight a row; the plain run goes on alone in the column, its return address aimed at main's row until the letters reach it; the fault at ret. The introduction's three chapters are built and tested, not yet exported. Annotations, rows and scrolls that changed in one frame now fade; captions stand twelve seconds at most. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'anim/src/aligned.zig')
-rw-r--r--anim/src/aligned.zig325
1 files changed, 325 insertions, 0 deletions
diff --git a/anim/src/aligned.zig b/anim/src/aligned.zig
new file mode 100644
index 0000000..a7c225d
--- /dev/null
+++ b/anim/src/aligned.zig
@@ -0,0 +1,325 @@
+//! `aligned` (design/README.md, "aligned"; mocks 32, 33, 36): the source
+//! with the instructions it compiled to, one panel across both columns,
+//! each line level with its code; and the introduction's way into it
+//! (motion.md, chapter III): the code's bytes as a dump row, one
+//! instruction's bytes framed and `decode`d into its row, the rest at a
+//! run, then `align` from one form to the other. Clocks the scene plays
+//! (frames, linear): `bytes`, `decode`, `rows_in`, `chrome`, `form`.
+//!
+//! The rows: the pc's function from the first run of the line before the
+//! pc's to the function's end, as the recording disassembled it at its
+//! moment, each instruction's bytes as the recording read them.
+//!
+//! - `interleaved` (form 0), as `objdump -S` prints it: each run of a
+//! line's instructions a group, its head the line on `raised` paper, its
+//! rows under it; lines and addresses ranged right at 132 in one column;
+//! a line's second run `again: its second run`, faint.
+//! - `columns` (form 1): the source left, a 2-unit `rule` the seam 48
+//! after the longest line, the instructions right; a line level with the
+//! first row of its run; a line with no code of its own in source order
+//! after the line before it, faint, `no code of their own`.
+//!
+//! `align` between them (6. Verbs): the addresses and instructions glide
+//! right into the instruction column (24 f, the bytes going), then every
+//! row glides to its height (36 f: the verb's 24 read too quick for rows
+//! crossing a column), then the lines with no code fade in (12 f).
+const std = @import("std");
+const sk = @import("sketch");
+const anim = @import("anim");
+const ui = @import("ui.zig");
+const disasm = @import("disasm.zig");
+const Recording = @import("Recording.zig");
+const theme = ui.theme;
+const Rect = ui.Rect;
+const t = theme.color.text;
+
+const size = theme.size.mono.context;
+const adv = ui.advance(size);
+const pitch = theme.metric.context.row;
+const tag = theme.metric.context.tag;
+const pad = theme.metric.context.pad;
+
+/// The columns, from the panel's left (mock 33, 36).
+const col = struct {
+ const place_end = 132;
+ const text = 156;
+ const bytes = 180;
+ const mnemonic = 360;
+ const seam_after = 48;
+ const instr = 156;
+};
+
+/// The dump row's first byte (`bytes`: at 132 at least).
+const dump_first = theme.metric.bytes.first;
+
+fn clamp01(x: f32) f32 {
+ return std.math.clamp(x, 0, 1);
+}
+
+fn glide(x: f32) f32 {
+ return theme.ease(.glide, clamp01(x));
+}
+
+const Row = struct {
+ kind: enum { head, insn, bare_line },
+ line: u32,
+ insn: ?disasm.Insn = null,
+ again: bool = false,
+ /// The first row of its run (columns: where its line stands).
+ first: bool = false,
+ /// Its place in each form, in rows.
+ at: [2]f32 = .{ 0, 0 },
+};
+
+pub const Aligned = struct {
+ rec: *const Recording,
+ at: u64,
+ /// Under a text `above` of two lines (48 to 182).
+ box: Rect = .{ .x = theme.canvas.margin_x, .y = 192, .w = theme.canvas.stage.w, .h = 696 },
+ /// Where the dump row of the code's bytes stands (III1).
+ dump_y: f32 = 192 + tag + pad,
+ bytes: anim.Value,
+ decode: anim.Value,
+ rows_in: anim.Value,
+ chrome: anim.Value,
+ form: anim.Value,
+
+ pub fn init(s: *anim.Frame, rec: *const Recording, moment: u64) Aligned {
+ return .{ .rec = rec, .at = moment, .bytes = s.value(-1), .decode = s.value(-1), .rows_in = s.value(-1), .chrome = s.value(-1), .form = s.value(0) };
+ }
+
+ fn sourceLine(a: *const Aligned, n: u32) []const u8 {
+ var it = std.mem.splitScalar(u8, a.rec.source, '\n');
+ var k: u32 = 1;
+ while (it.next()) |l| : (k += 1) if (k == n) return std.mem.trimEnd(u8, l, " \r");
+ return "";
+ }
+
+ /// The rows in interleaved order, with their places in both forms; the pc's index.
+ fn build(a: *const Aligned, u: ui.Ui) struct { rows: []Row, pc: usize, longest: f32 } {
+ const pc = a.rec.register(a.at, "rip");
+ const listing = disasm.listingAt(u, a.rec, a.at, pc, 64, 64);
+ const all = listing.rows;
+ const pc_line = all[listing.pc].line orelse 0;
+ var start: usize = 0;
+ while (start < all.len and (all[start].line orelse 0) + 1 != pc_line) start += 1;
+ if (start == all.len) start = 0;
+ var end = start;
+ while (end < all.len and !all[end].outside) end += 1;
+ const shown = all[start..end];
+ var out: std.ArrayList(Row) = .empty;
+ const arena = u.f.scratch;
+ var seen: [64]u32 = undefined;
+ var n_seen: usize = 0;
+ var lo: u32 = std.math.maxInt(u32);
+ var hi: u32 = 0;
+ for (shown) |r| if (r.line) |l| {
+ lo = @min(lo, l);
+ hi = @max(hi, l);
+ };
+ var k: usize = 0;
+ var pc_index: usize = 0;
+ while (k < shown.len) {
+ const line = shown[k].line orelse 0;
+ const again = std.mem.indexOfScalar(u32, seen[0..n_seen], line) != null;
+ if (!again and n_seen < seen.len) {
+ seen[n_seen] = line;
+ n_seen += 1;
+ }
+ out.append(arena, .{ .kind = .head, .line = line, .again = again }) catch break;
+ var first = true;
+ while (k < shown.len and (shown[k].line orelse 0) == line) : (k += 1) {
+ if (shown[k].addr == pc) pc_index = out.items.len;
+ out.append(arena, .{ .kind = .insn, .line = line, .insn = shown[k], .first = first }) catch break;
+ first = false;
+ }
+ // Lines after this one in the source with no code of their own (columns only), once.
+ if (!again) {
+ var l = line + 1;
+ while (l < hi and !hasCode(all, l)) : (l += 1)
+ if (l > lo) out.append(arena, .{ .kind = .bare_line, .line = l, .first = l == line + 1 }) catch break;
+ }
+ }
+ // Their places: interleaved, heads and rows in order (no bare lines);
+ // columns, the instructions in order with the bare lines after theirs.
+ var inter: f32 = 0;
+ var cols: f32 = 0;
+ var longest: f32 = 0;
+ for (out.items) |*r| {
+ longest = @max(longest, @as(f32, @floatFromInt(a.sourceLine(r.line).len)) * adv);
+ switch (r.kind) {
+ .head => {
+ r.at = .{ inter, cols };
+ inter += 1;
+ },
+ .insn => {
+ r.at = .{ inter, cols };
+ inter += 1;
+ cols += 1;
+ },
+ .bare_line => {
+ r.at = .{ inter, cols };
+ cols += 1;
+ },
+ }
+ }
+ return .{ .rows = out.items, .pc = pc_index, .longest = longest };
+ }
+
+ fn hasCode(all: []const disasm.Insn, line: u32) bool {
+ for (all) |r| if (!r.outside and r.line == line) return true;
+ return false;
+ }
+
+ pub fn draw(a: *const Aligned, f: *sk.Frame, placed: anim.Placed) void {
+ const u: ui.Ui = .{ .f = f, .at = placed };
+ const built = a.build(u);
+ if (built.rows.len == 0) return;
+ const m = a.rec.moment(a.at);
+ const form = placed.value(a.form);
+ const across = glide(form / 24);
+ const down = glide((form - 24) / 36);
+ const bare = clamp01((form - 60) / 12);
+ const seam = col.text + built.longest + col.seam_after;
+ const rows_in = placed.value(a.rows_in);
+ const chrome = clamp01(placed.value(a.chrome) / 12);
+ const tag_in = clamp01((placed.value(a.chrome) - 12) / 12);
+ const v = u.moved(a.box.x, a.box.y);
+ const body = tag + pad;
+ var counts = [2]f32{ 0, 0 };
+ for (built.rows) |r| counts = .{ @max(counts[0], r.at[0] + 1), @max(counts[1], r.at[1] + 1) };
+ const h = body + pitch * std.math.lerp(counts[0], counts[1], down) + pad;
+ // The panel's rule and tag last (`frame up`).
+ if (tag_in > 0) {
+ const tv = v.faded(tag_in);
+ tv.fill(.{ .x = 0, .y = 0, .w = a.box.w, .h = tag }, theme.color.raised);
+ tv.fill(.{ .x = 8, .y = 6, .w = 12, .h = 12 }, theme.mark(.current).solid);
+ tv.text(.sans_medium, theme.size.sans.tag, 28, tag - 6, v.print("{s} \u{00b7} {s}", .{ a.rec.file, functionName(v, a.rec, a.at) }), t.primary);
+ const notes = [2][]const u8{ "each line, then the instructions it compiled to \u{00b7} lines and addresses in one column", "each line level with its instructions" };
+ for (notes, [2]f32{ 1 - down, down }) |note, k| if (k > 0) tv.faded(k).tagNote(a.box.w, note);
+ }
+ if (chrome > 0) v.faded(chrome).frame(.{ .x = 0, .y = 0, .w = a.box.w, .h = h }, theme.color.rule, theme.stroke.rule);
+ // The seam, with the columns.
+ if (down > 0) v.faded(down).fill(.{ .x = seam, .y = tag, .w = theme.stroke.rule, .h = h - tag }, theme.color.rule);
+
+ for (built.rows, 0..) |r, k| {
+ const y = body + pitch * std.math.lerp(r.at[0], r.at[1], down);
+ const order: f32 = @floatFromInt(k);
+ // At a run: rows top down, 2 f apart, 8 f each; the pc's row is the decoded one.
+ // The pc's row is the decoded one, once it has travelled to its place (48).
+ var shows: f32 = if (k == built.pc) @as(f32, if (placed.value(a.decode) >= 48) 1 else 0) else clamp01((rows_in - 2 * order) / 8);
+ if (r.kind == .bare_line) shows = @min(shows, bare);
+ if (shows <= 0) continue;
+ const rv = v.faded(shows);
+ const base = theme.baseline(y, pitch, size);
+ switch (r.kind) {
+ .head, .bare_line => {
+ // Interleaved: a head across the panel; columns: the line in its cell, level with its run.
+ const head_y = if (r.kind == .head) y else body + pitch * r.at[1];
+ const hb = theme.baseline(head_y, pitch, size);
+ const paper_w = std.math.lerp(a.box.w - 4, seam - 2, down);
+ if (r.kind == .head and !r.again and chrome > 0) rv.faded(chrome).fill(.{ .x = 2, .y = head_y, .w = paper_w, .h = pitch }, theme.color.raised);
+ if (chrome > 0 and r.kind == .head) rv.faded(chrome).fill(.{ .x = 2, .y = head_y - 1, .w = a.box.w - 4, .h = theme.stroke.rule }, theme.color.rule);
+ const faint = r.again or r.kind == .bare_line;
+ const number = rv.print("{d}", .{r.line});
+ const is_pc = !r.again and built.rows[built.pc].line == r.line and r.kind == .head;
+ if (is_pc) rv.fill(.{ .x = col.place_end - @as(f32, @floatFromInt(number.len)) * adv - 4, .y = head_y + 1, .w = @as(f32, @floatFromInt(number.len)) * adv + 6, .h = pitch - 2 }, theme.mark(.current).solid);
+ rv.text(.mono, size, col.place_end - @as(f32, @floatFromInt(number.len)) * adv, hb, number, if (is_pc) t.ink else t.faint);
+ rv.text(.mono, size, col.text, hb, a.sourceLine(r.line), if (faint) t.faint else t.primary);
+ if (r.again) rv.text(.sans, theme.size.sans.micro, std.math.lerp(col.mnemonic + 4, col.text + built.longest - 140, down), hb, "again: its second run", t.dim);
+ if (r.kind == .bare_line and r.first) rv.text(.sans, theme.size.sans.micro, seam + col.instr, hb, "no code of their own", t.dim);
+ },
+ .insn => {
+ const insn = r.insn.?;
+ const ix = std.math.lerp(0, seam, across);
+ if (k == built.pc) {
+ const wash = rv.faded(clamp01((placed.value(a.decode) - 48) / 12));
+ wash.current(.{ .x = ix + 2, .y = y, .w = a.box.w - ix - 4, .h = pitch }, theme.metric.plaque.pc, 0);
+ sk.draw.polygon(f, &.{ wash.at.xf.apply(.{ ix + 20, y + pitch / 2 }), wash.at.xf.apply(.{ ix + 10, y + 6 }), wash.at.xf.apply(.{ ix + 10, y + pitch - 6 }) }, wash.at.pen(.{ .fill = ui.rgb(theme.mark(.current).on_solid) }));
+ }
+ const digits: f32 = @floatFromInt(ui.digits(insn.addr));
+ _ = rv.address(ix + col.place_end - digits * adv, base, insn.addr, .{ .ink = ui.rgb(theme.color.region.code_) });
+ // Its bytes, as the recording read them: interleaved only.
+ if (across < 1) rv.faded(1 - across).text(.mono, size, col.bytes, base, hexBytes(rv, m, insn), t.dim);
+ disasm.bareRow(rv.moved(std.math.lerp(col.mnemonic - theme.metric.insn.mnemonic, seam + col.instr + 24 - theme.metric.insn.mnemonic, across), 0), insn, y);
+ },
+ }
+ }
+ a.drawBytesRow(v, m, placed, built);
+ }
+
+ /// III1 and the decode (III2): the code's sixteen bytes as a dump row,
+ /// in code's ink 2 f apart; the pc's instruction's bytes framed; then
+ /// they gather, its text fades in beside them, and the row travels to
+ /// its place in the panel.
+ fn drawBytesRow(a: *const Aligned, v: ui.Ui, m: Recording.Moment, placed: anim.Placed, built: anytype) void {
+ const clock = placed.value(a.bytes);
+ if (clock < 0) return;
+ const d = placed.value(a.decode);
+ const insn = built.rows[built.pc].insn.?;
+ const row_addr = insn.addr & ~@as(u64, 15);
+ var bytes: [16]u8 = undefined;
+ var known: [16]bool = undefined;
+ m.read(row_addr, &bytes, &known) catch return;
+ const dy = a.dump_y - a.box.y;
+ const target_y = tag + pad + pitch * built.rows[built.pc].at[0];
+ const travel = glide((d - 24) / 24);
+ if (travel >= 1) return;
+ const y = std.math.lerp(dy, target_y, travel);
+ const base = theme.baseline(y, pitch, size);
+ const arrive = clamp01(clock / 12);
+ const gather = glide(d / 12);
+ const others = 1 - clamp01(d / 12);
+ const bx = struct {
+ fn at(k: usize) f32 {
+ return dump_first + @as(f32, @floatFromInt(k)) * theme.metric.bytes.pitch + @as(f32, if (k >= 8) theme.metric.bytes.middle else 0);
+ }
+ }.at;
+ // The row's address: the row's, then the instruction's.
+ const rv = v.faded(arrive);
+ const ad: f32 = @floatFromInt(ui.digits(row_addr));
+ rv.faded(others).text(.mono, size, 34, base, rv.print("{x}", .{row_addr}), t.faint);
+ _ = rv.faded(1 - others).address(std.math.lerp(34, col.place_end - ad * adv, travel), base, insn.addr, .{ .ink = ui.rgb(theme.color.region.code_) });
+ const first: usize = @intCast(insn.addr - row_addr);
+ const target_x = std.math.lerp(bx(first), col.bytes, travel);
+ for (0..16) |k| {
+ const mine = k >= first and k < first + insn.len;
+ const inked = clamp01((clock - 12 - 2 * @as(f32, @floatFromInt(k))) / 12);
+ const ink = ui.mix(t.primary, theme.color.region.code_, inked);
+ const text = if (known[k]) rv.print("{x:0>2}", .{bytes[k]}) else "??";
+ if (!mine) {
+ if (others > 0) rv.faded(others).text(.mono, size, bx(k), base, text, ink);
+ continue;
+ }
+ // Gathered: three characters apart, from where the instruction's first byte stood.
+ const at_x = std.math.lerp(bx(k), target_x + @as(f32, @floatFromInt(k - first)) * 3 * adv, gather);
+ rv.text(.mono, size, at_x, base, text, ui.mix(ink, ui.rgb(t.dim), clamp01((d - 12) / 12)));
+ }
+ // The instruction's bytes framed once inked (6 f), until they gather.
+ const framed = clamp01((clock - 12 - 32 - 12) / 6) * (1 - clamp01(d / 6));
+ if (framed > 0) rv.read(.{ .x = bx(first), .y = y, .w = bx(first + insn.len - 1) - bx(first) + 2 * adv, .h = pitch }, framed);
+ // Its text beside the bytes, at the panel's columns from them (12 f).
+ const text_in = clamp01((d - 12) / 12);
+ if (text_in > 0) disasm.bareRow(rv.faded(text_in).moved(target_x - col.bytes + col.mnemonic - theme.metric.insn.mnemonic, 0), insn, y);
+ }
+};
+
+fn hexBytes(u: ui.Ui, m: Recording.Moment, insn: disasm.Insn) []const u8 {
+ var bytes: [16]u8 = undefined;
+ var known: [16]bool = undefined;
+ const n = @min(insn.len, 16);
+ m.read(insn.addr, bytes[0..n], known[0..n]) catch return "";
+ var out: std.ArrayList(u8) = .empty;
+ for (bytes[0..n], known[0..n], 0..) |b, kn, k| {
+ if (k > 0) out.append(u.f.scratch, ' ') catch {};
+ out.appendSlice(u.f.scratch, if (kn) u.print("{x:0>2}", .{b}) else "??") catch {};
+ }
+ return out.items;
+}
+
+fn functionName(u: ui.Ui, rec: *const Recording, i: u64) []const u8 {
+ var buf: [128]u8 = undefined;
+ const name = (rec.moment(i).string(rec.register(i, "rip"), &buf) catch null) orelse return "";
+ const plus = std.mem.indexOfScalar(u8, name, '+') orelse name.len;
+ return u.print("{s}", .{name[0..plus]});
+}