diff options
Diffstat (limited to 'anim/src')
| -rw-r--r-- | anim/src/Recording.zig | 4 | ||||
| -rw-r--r-- | anim/src/aligned.zig | 325 | ||||
| -rw-r--r-- | anim/src/aside.zig | 160 | ||||
| -rw-r--r-- | anim/src/column.zig | 119 | ||||
| -rw-r--r-- | anim/src/context.zig | 78 | ||||
| -rw-r--r-- | anim/src/cursor.zig | 32 | ||||
| -rw-r--r-- | anim/src/descent.zig | 102 | ||||
| -rw-r--r-- | anim/src/disasm.zig | 105 | ||||
| -rw-r--r-- | anim/src/graph.zig | 6 | ||||
| -rw-r--r-- | anim/src/hexdump.zig | 28 | ||||
| -rw-r--r-- | anim/src/loupe.zig | 4 | ||||
| -rw-r--r-- | anim/src/nodes.zig | 74 | ||||
| -rw-r--r-- | anim/src/pair.zig | 780 | ||||
| -rw-r--r-- | anim/src/regroup.zig | 348 | ||||
| -rw-r--r-- | anim/src/relations.zig | 341 | ||||
| -rw-r--r-- | anim/src/root.zig | 24 | ||||
| -rw-r--r-- | anim/src/segments.zig | 605 | ||||
| -rw-r--r-- | anim/src/source.zig | 16 | ||||
| -rw-r--r-- | anim/src/stage.zig | 14 | ||||
| -rw-r--r-- | anim/src/ties.zig | 876 |
20 files changed, 3642 insertions, 399 deletions
diff --git a/anim/src/Recording.zig b/anim/src/Recording.zig index 359305c..4bc9c1b 100644 --- a/anim/src/Recording.zig +++ b/anim/src/Recording.zig @@ -288,7 +288,9 @@ pub fn sourced(r: *const Recording, i: u64) bool { /// for every line of that third, so that the text moves only when the /// program has moved on. pub fn viewFirst(fit: u32, lines: u32, line: u32) u32 { - if (lines <= fit or fit < 3) return 1; + if (lines <= fit or fit == 0) return 1; + // One or two lines (a column's pane): the line itself, first. + if (fit < 3) return std.math.clamp(line, 1, lines - fit + 1); const third = fit / 3; const band = (@max(line, 1) - 1) / third; return std.math.clamp(@as(u32, @intCast(@max(@as(i64, band) - 1, 0))) * third + 1, 1, lines - fit + 1); 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]}); +} diff --git a/anim/src/aside.zig b/anim/src/aside.zig new file mode 100644 index 0000000..7cf6867 --- /dev/null +++ b/anim/src/aside.zig @@ -0,0 +1,160 @@ +//! A text beside what it speaks of, for a scene that builds its parts in +//! place rather than in the column's panes (the introduction): the +//! column's `bubble` (design/README.md, "column"), standing alone. Go 40 +//! on 54, code in mono 35, at most 40 characters a line, 16 of padding; +//! the newest on `raised` paper, primary; an older one loses its paper and +//! dims to `dim` (`dim`, 18 f). It arrives rising 8 (its object's fade, +//! 18 f, `enter`) and leaves with its fade (12 f, `exit`). No stem: its +//! level is the tie. +//! shortcut: a second drawing of column.zig's bubble (wrap, paper); fold into one when the column takes custom panes. +const std = @import("std"); +const sk = @import("sketch"); +const anim = @import("anim"); +const ui = @import("ui.zig"); +const words = @import("words.zig"); +const theme = ui.theme; +const t = theme.color.text; + +const time = theme.time; +const pad = 16; +const chars = 40; +const first = 40; +const line_pitch = theme.size.sans.caption_leading; + +pub const Aside = struct { + text: []const u8, + /// The paper's left, and the middle of its first line. + x: f32, + level: f32, + /// 0 newest, 1 the account (no paper, `dim`). + dim: anim.Value, + + pub fn draw(a: *const Aside, f: *sk.Frame, placed: anim.Placed) void { + const u = (ui.Ui{ .f = f, .at = placed }).moved(0, (1 - placed.pose.opacity) * theme.time.caption_rise); + const wrapped = wrap(u, a.text); + const top = a.level - first; + const h = first * 2 + @as(f32, @floatFromInt(wrapped.len - 1)) * line_pitch; + var w: f32 = 0; + for (wrapped) |line| w = @max(w, width(u, line)); + const dim = std.math.clamp(placed.value(a.dim), 0, 1); + if (dim < 1) u.faded(1 - dim).fill(.{ .x = a.x, .y = top, .w = w + 2 * pad, .h = h }, theme.color.raised); + const ink = ui.mix(t.primary, t.dim, dim); + for (wrapped, 0..) |line, n| _ = words.runs(u, a.x + pad, top + first + @as(f32, @floatFromInt(n)) * line_pitch + 14, line, .sans, theme.size.sans.caption, theme.size.mono.value, ink); + } +}; + +/// A scene's texts beside their parts, said one after another: each waits +/// until the last has been read, arrives (18 f, `enter`), and is followed +/// by its lead (60 f); the one before it dims (18 f), or leaves (12 f, +/// `exit`) where the new one would stand on it. `leave`: all go together. +pub const Asides = struct { + shown: [8]?anim.Obj = @splat(null), + dims: [8]anim.Value = undefined, + spans: [8][2]f32 = undefined, + n: usize = 0, + read_by: f32 = 0, + + pub fn say(a: *Asides, s: *anim.Frame, text: []const u8, x: f32, level: f32) void { + s.wait(@max(a.read_by - s.cursor, 0)); + const dim = s.value(0); + const made = s.new(Aside{ .text = text, .x = x, .level = level, .dim = dim }); + const obj = s.custom(made, .{ .x = -640, .y = -360, .scale = ui.scale }); + const span = [2]f32{ level - first, level + first + @as(f32, @floatFromInt(lines(text) - 1)) * line_pitch }; + const at = s.cursor; + for (a.shown[0..a.n], a.dims[0..a.n], a.spans[0..a.n]) |*o, d, other| { + s.cursor = at; + if (o.* == null) continue; + if (other[0] < span[1] + 12 and span[0] < other[1] + 12) { + s.play(.{o.*.?.fadeOut()}, .{ .seconds = time.caption_out, .ease = easeExit }); + o.* = null; + } else s.play(.{d.to(1)}, .{ .seconds = 18.0 / 60.0, .ease = easeGlide }); + } + s.cursor = at; + s.play(.{obj.fadeIn()}, .{ .seconds = time.caption_in, .ease = easeEnter }); + a.shown[a.n] = obj; + a.dims[a.n] = dim; + a.spans[a.n] = span; + a.n += 1; + a.read_by = s.cursor + time.caption(@intCast(std.unicode.utf8CountCodepoints(text) catch text.len)); + s.wait(time.caption_lead); + } + + /// All leave together (12 f), once the last is read. + pub fn leave(a: *Asides, s: *anim.Frame) void { + s.wait(@max(a.read_by - s.cursor, 0)); + const at = s.cursor; + for (a.shown[0..a.n]) |o| if (o) |obj| { + s.cursor = at; + s.play(.{obj.fadeOut()}, .{ .seconds = time.caption_out, .ease = easeExit }); + }; + } +}; + +fn easeExit(x: f32) f32 { + return theme.ease(.exit, x); +} +fn easeEnter(x: f32) f32 { + return theme.ease(.enter, x); +} +fn easeGlide(x: f32) f32 { + return theme.ease(.glide, x); +} + +/// How many lines `wrap` makes of `text`. +fn lines(text: []const u8) usize { + var it = std.mem.splitScalar(u8, text, ' '); + var n: usize = 0; + var count: usize = 1; + while (it.next()) |word| { + const seen = (std.unicode.utf8CountCodepoints(word) catch word.len) - std.mem.count(u8, word, "`"); + if (n > 0 and n + 1 + seen > chars) { + count += 1; + n = 0; + } + if (n > 0) n += 1; + n += seen; + } + return count; +} + +fn width(u: ui.Ui, line: []const u8) f32 { + var w: f32 = 0; + var parts = std.mem.splitScalar(u8, line, '`'); + var code = false; + while (parts.next()) |part| : (code = !code) w += if (code) u.width(.mono, theme.size.mono.value, part) else u.width(.sans, theme.size.sans.caption, part); + return w; +} + +/// Lines of at most `chars` visible characters, broken at spaces; a code +/// run keeps its backticks on each line it spans. +fn wrap(u: ui.Ui, text: []const u8) []const []const u8 { + var out: std.ArrayList([]const u8) = .empty; + var it = std.mem.splitScalar(u8, text, ' '); + var line: std.ArrayList(u8) = .empty; + var n: usize = 0; + while (it.next()) |word| { + const seen = std.unicode.utf8CountCodepoints(word) catch word.len; + const ticks = std.mem.count(u8, word, "`"); + if (n > 0 and n + 1 + seen - ticks > chars) { + out.append(u.f.scratch, line.items) catch break; + line = .empty; + n = 0; + } + if (n > 0) { + line.append(u.f.scratch, ' ') catch break; + n += 1; + } + line.appendSlice(u.f.scratch, word) catch break; + n += seen - ticks; + } + if (line.items.len > 0) out.append(u.f.scratch, line.items) catch {}; + if (out.items.len == 0) out.append(u.f.scratch, "") catch {}; + return out.items; +} + +test "lines of at most forty characters, backticks not counted" { + try std.testing.expectEqual(@as(usize, 1), lines("Its address space, 128 TiB, to scale.")); + try std.testing.expectEqual(@as(usize, 2), lines("Its address space, 128 TiB, to scale: mapped only at each end.")); + // Four code runs of nine: 39 shown, 47 with their backticks. + try std.testing.expectEqual(@as(usize, 1), lines("`012345678` `012345678` `012345678` `012345678`")); +} diff --git a/anim/src/column.zig b/anim/src/column.zig index 32b3d4d..1484796 100644 --- a/anim/src/column.zig +++ b/anim/src/column.zig @@ -19,9 +19,11 @@ //! }); //! //! Between beats, in a rest (design/README.md, "Panes between beats"): the -//! last bubble dims; the panes that leave fade (12 f) and fold to the shelf -//! (24 f, `glide`); those that stay glide (24 f); those that come unfold -//! (24 f) and fade in (12 f); an older bubble in the way glides out of it +//! last bubble dims; the panes that leave fade (6 f) and fold where they +//! stand, their tags travelling to the shelf (18 f, `glide`); those that +//! stay glide (24 f; a pane crossing to the other column 24 f each way), +//! one that changes its height keeping its rows; those that come unfold +//! (18 f) and fade in (6 f); an older bubble in the way glides out of it //! (24 f); the next arrives (18 f, rising 8), its stem drawing (12 f); the //! lead; the stem draws back as the step fires. A bubble never travels //! between placements: the last leaves (12 f), the frame changes (48 f), the @@ -78,6 +80,9 @@ pub const Beat = struct { notes: []const Note = &.{}, /// `on`: the bubble's top left in the frame (design units), in the drawing's free space. where: ?[2]f32 = null, + /// The rest after this beat's steps is meant (a poster's picture): its + /// bubble may still be read there longer than a hold (design/README.md, `bubble`). + held: bool = false, }; pub const Options = struct { @@ -92,10 +97,9 @@ pub const Options = struct { /// Frames, at 60 a second (design/README.md, "Panes between beats"). const f = struct { const dim = 18.0 / 60.0; - const rows = 12.0 / 60.0; - const fold = 24.0 / 60.0; + const rows = 6.0 / 60.0; + const fold = 18.0 / 60.0; const glide = 24.0 / 60.0; - const relayout = theme.time.relayout; const push = 24.0 / 60.0; const arrive = 18.0 / 60.0; const stem_after = 6.0 / 60.0; @@ -164,6 +168,8 @@ pub const Column = struct { zoom: f32 = 1, read_by: f32 = 0, said: bool = false, + /// The last beat's rest is meant: not flagged. + held: bool = false, /// One beat: the panes change (in a rest, never under a step), the text /// arrives where the beat places it, the lead, then the scene's step. @@ -197,8 +203,12 @@ pub const Column = struct { split.band = room; }, } - // Not before the text up has been read. - s.wait(@max(col.read_by - s.cursor, 0)); + // Not before the text up has been read; and its beat should have covered that + // (its lead, its steps and a hold of at most 84 f), unless its rest is meant. + const still = col.read_by - s.cursor; + if (col.said and !col.held and still > 84.0 / 60.0) flag(col.n, still); + s.wait(@max(still, 0)); + col.held = b.held; const kind = frameOf(at) != frameOf(col.placement); const moves = !std.meta.eql(to, col.standing) or !sameBoxes(col.split, split, to) or zoom != col.zoom or kind; const newest = col.newestStanding(); @@ -295,31 +305,25 @@ pub const Column = struct { fn change(col: *Column, s: *anim.Frame, to: [parts.len]bool, split: Split, at: Placement, zoom: f32, kind: bool) void { var leaving: [parts.len]bool = undefined; var coming: [parts.len]bool = undefined; - // A pane that stays but changes its rows: its rows fade out and in round the - // glide (they are laid out for its height), its frame gliding as the column's. - var resized: [parts.len]bool = undefined; + // A pane that stays and changes its height keeps its rows the whole way: + // it glides with the column, gaining or giving up rows at its moving edge. var any_leaving = false; var any_coming = false; - inline for (.{ "band", "view", "source", "disasm", "registers", "stack", "backtrace" }, 0..) |name, i| { + for (parts, 0..) |_, i| { leaving[i] = col.standing[i] and !to[i]; coming[i] = !col.standing[i] and to[i]; - resized[i] = col.standing[i] and to[i] and i > 1 and @field(col.split, name).h != @field(split, name).h; - any_leaving = any_leaving or leaving[i] or resized[i]; - any_coming = any_coming or coming[i] or resized[i]; + any_leaving = any_leaving or leaving[i]; + any_coming = any_coming or coming[i]; } const band = @intFromEnum(Part.band); const view = @intFromEnum(Part.view); if (any_leaving) { var anims: [3 * parts.len]anim.Animation = undefined; var n: usize = 0; - for (parts, 0..) |_, i| if (leaving[i] or resized[i]) { + for (parts, 0..) |_, i| if (leaving[i]) { anims[n] = col.ghost[i].to(1); n += 1; }; - for (parts, 0..) |_, i| if (resized[i]) if (col.objs[i]) |o| { - anims[n] = o.fadeOut(); - n += 1; - }; for (parts, 0..) |_, i| if (leaving[i]) if (col.objs[i]) |o| { // The lens is one object: it goes when neither of its panes stands (once). const lens = i == band or i == view; @@ -356,8 +360,6 @@ pub const Column = struct { if (i == band) next.band.h = 0; }; const glides = !sameBoxes(col.split, next, col.standing) or zoom != col.zoom or kind; - const seconds: f32 = if (!glides) 0 else if (kind) f.relayout else f.glide; - const start = s.cursor; // A pane going over to the other column goes down (or up) its own // first, as the others make room, then across: never over another's rows. var mid = next; @@ -366,6 +368,9 @@ pub const Column = struct { @field(mid, name).x = @field(col.split, name).x; crosses = true; }; + // 24 f; a pane crossing over, 24 f each way. + const seconds: f32 = if (!glides) 0 else if (crosses) 2 * f.glide else f.glide; + const start = s.cursor; if (crosses) col.stage.glide(s, mid, seconds / 2); col.stage.glide(s, next, if (crosses) seconds / 2 else seconds); const end = s.cursor; @@ -392,14 +397,10 @@ pub const Column = struct { }; if (n > 0) together(s, anims[0..n], f.fold, anim.ease.linear); n = 0; - for (parts, 0..) |_, i| if (coming[i] or resized[i]) { + for (parts, 0..) |_, i| if (coming[i]) { anims[n] = col.ghost[i].to(0); n += 1; }; - for (parts, 0..) |_, i| if (resized[i]) if (col.objs[i]) |o| { - anims[n] = o.fadeIn(); - n += 1; - }; for (parts, 0..) |_, i| if (coming[i]) if (col.objs[i]) |o| { const lens = i == band or i == view; if (!lens or !(col.standing[band] or col.standing[view]) and !(i == view and coming[band])) { @@ -492,10 +493,12 @@ pub const Column = struct { /// between its box and its place there. fn drawShelf(col: *const Column, u: ui.Ui, placed: anim.Placed) void { const before, const after, const p = col.notesNow(placed); + shelf_tags = @splat(null); var x: f32 = theme.layout.program.x; for (parts, 0..) |part, i| { if (col.objs[i] == null) continue; - const on = theme.ease(.glide, placed.value(col.shelf[i])); + const raw = placed.value(col.shelf[i]); + const on = theme.ease(.glide, raw); // Its frame drawn in the pane's stead: crossfading with the pane at its box, alone on its way. const ghost = placed.value(col.ghost[i]); if (on <= 0 and ghost <= 0) continue; @@ -504,7 +507,11 @@ pub const Column = struct { const slot: Rect = .{ .x = x, .y = shelf_y, .w = w, .h = m.tag }; const name = col.title(placed, part); const b = col.box(placed, part); - const r: Rect = if (ghost > 0) .{ .x = std.math.lerp(b.x, slot.x, on), .y = std.math.lerp(b.y, slot.y, on), .w = std.math.lerp(b.w, slot.w, on), .h = std.math.lerp(b.h, slot.h, on) } else slot; + // It closes to its tag where it stands, then the tag travels (unfolding, the + // reverse): only a tag ever crosses another pane, never a panel. + const shut = theme.ease(.glide, std.math.clamp(raw / 0.6, 0, 1)); + const go = theme.ease(.glide, std.math.clamp((raw - 0.4) / 0.6, 0, 1)); + const r: Rect = if (ghost > 0) .{ .x = std.math.lerp(b.x, slot.x, go), .y = std.math.lerp(b.y, slot.y, go), .w = std.math.lerp(b.w, slot.w, shut), .h = std.math.lerp(b.h, slot.h, shut) } else slot; const v = u.moved(r.x, r.y).faded(if (on > 0) 1 else ghost); _ = v.panel(r.w, r.h, name, .{}); // Its note comes as it lands on the shelf, and goes as it leaves. @@ -514,6 +521,7 @@ pub const Column = struct { if (p < 1) _ = col.tagNote(v.faded(shows * (1 - p)), placed, part, nx, notes[0]); _ = col.tagNote(v.faded(shows * p), placed, part, nx, notes[1]); } + if (on >= 1) shelf_tags[i] = slot; x += (w + theme.canvas.row_gap) * on; } } @@ -605,8 +613,12 @@ pub const Column = struct { const digits = if (v < 0x10000) u.print("{d}", .{v}) else u.print("{x}", .{v}); const dw = u.width(.mono, size, digits); const heat = if (age == 0) now.register(i, ties.strikeDelay(now, i)) else now.heat(1, null, 0); - const ink = u.written(.{ .x = at, .y = 0, .w = dw, .h = m.tag }, heat) orelse ui.Ink{}; - u.text(ink.face, size, at, base, digits, ink.ink); + _ = u.written(.{ .x = at, .y = 0, .w = dw, .h = m.tag }, heat); + // Struck, bold and bright: crossfading with the fill (a face changing in a frame on a tag this small is a pop). + const solid = if (heat.level >= 1) heat.solid * heat.in else 0; + const ink = ui.mix(t.primary, t.bright, solid); + if (solid < 1) u.faded(1 - solid).text(.mono, size, at, base, digits, ink); + if (solid > 0) u.faded(solid).text(.mono_bold, size, at, base, digits, ink); at += dw; } shown += 1; @@ -631,8 +643,8 @@ pub const Column = struct { }; var with: [48]f32 = undefined; var without: [48]f32 = undefined; - col.layout(placed, inv, newest, true, &with); - col.layout(placed, inv, newest, false, &without); + col.layout(u.f, placed, inv, newest, true, &with); + col.layout(u.f, placed, inv, newest, false, &without); const push = if (newest) |k| theme.ease(.glide, placed.value(col.texts[k].push)) else 1; for (col.texts[0..col.n], 0..) |x, k| { const in = placed.value(x.in); @@ -685,7 +697,7 @@ pub const Column = struct { /// Each beside text's top, the newest's or not: level with what it /// speaks of where it can be; an older one in a newer one's way above it, /// or under it where there is no room above. - fn layout(col: *const Column, placed: anim.Placed, inv: sk.math.Affine, newest: ?usize, with_newest: bool, out: *[48]f32) void { + fn layout(col: *const Column, fr: *sk.Frame, placed: anim.Placed, inv: sk.math.Affine, newest: ?usize, with_newest: bool, out: *[48]f32) void { var taken: [48][2]f32 = undefined; var n_taken: usize = 0; var k = col.n; @@ -699,7 +711,7 @@ pub const Column = struct { // Only what has arrived. if (newest == null or k > newest.?) continue; const h = height(x); - var top = std.math.clamp(levelY(placed, inv, k, x.level) - bubble.first, bubble.top, bubble.bottom - h); + var top = std.math.clamp(levelY(fr, inv, k, x.level) - bubble.first, bubble.top, bubble.bottom - h); // Above what is in its way, while there is room; else under it. var tries: usize = 0; while (tries < n_taken + 1) : (tries += 1) { @@ -725,7 +737,8 @@ pub const Column = struct { fn stemBeside(_: *const Column, u: ui.Ui, inv: sk.math.Affine, x: Text, top: f32, target: Level, placed: anim.Placed) void { const v = placed.value(x.stem_v); if (v <= 0 or v >= 2) return; - const r = find(inv, target) orelse return; + // About a folded pane: its tag on the shelf. + const r = if (paneOf(target)) |pane| if (shelf_tags[@intFromEnum(pane)]) |tag| [4]f32{ tag.x, tag.y, tag.x + tag.w, tag.y + tag.h } else find(u.f, inv, target) orelse return else find(u.f, inv, target) orelse return; const y0 = top + bubble.first; const y1 = (r[1] + r[3]) / 2; const lane = theme.metric.tie.link_lane + 6; @@ -739,7 +752,7 @@ pub const Column = struct { fn stemOn(col: *const Column, u: ui.Ui, inv: sk.math.Affine, x: Text, where: [2]f32, target: Level, placed: anim.Placed) void { const v = placed.value(x.stem_v); if (v <= 0 or v >= 2) return; - const r = find(inv, target) orelse return; + const r = find(u.f, inv, target) orelse return; const w = paperWidth(u, x); const lane = column_top + m.tag + 10; const from_x = where[0] + w - 32; @@ -790,7 +803,7 @@ fn settled(s: *const anim.Frame, v: anim.Value) f32 { } /// Where a level's place is this frame, in the frame's units: left, top, right, bottom. -fn find(inv: sk.math.Affine, level: Level) ?[4]f32 { +fn find(fr: *sk.Frame, inv: sk.math.Affine, level: Level) ?[4]f32 { const place: ties.Place = switch (level) { .pc => .pc, .row => |a| .{ .row = a }, @@ -799,7 +812,7 @@ fn find(inv: sk.math.Affine, level: Level) ?[4]f32 { .block => |a| .{ .block = a }, .y => |y| return .{ 0, y - m.row / 2, theme.layout.program.x + theme.layout.program.w, y + m.row / 2 }, }; - const r = ties.find(place) orelse return null; + const r = ties.find(fr, place) orelse return null; const a = inv.apply(.{ r[0], r[1] }); const b = inv.apply(.{ r[2], r[3] }); return .{ a[0], a[1], b[0], b[1] }; @@ -811,17 +824,39 @@ fn find(inv: sk.math.Affine, level: Level) ?[4]f32 { var last_level: [48]?f32 = @splat(null); /// Once its row has gone from a pane that is drawn, or the panes have changed, a text stays where it is (`latched`). var latched: [48]bool = @splat(false); -fn levelY(_: anim.Placed, inv: sk.math.Affine, k: usize, level: Level) f32 { - if (!latched[k]) if (find(inv, level)) |r| { +fn levelY(fr: *sk.Frame, inv: sk.math.Affine, k: usize, level: Level) f32 { + if (!latched[k]) if (find(fr, inv, level)) |r| { const y = (r[1] + r[3]) / 2; last_level[k] = y; return y; }; // A row of the disassembly, gone while the pane is there (its pc drawn): the text stays. - if (last_level[k] != null and (level == .row or level == .head or level == .floor) and ties.find(.pc) != null) latched[k] = true; + if (last_level[k] != null and (level == .row or level == .head or level == .floor) and ties.find(fr, .pc) != null) latched[k] = true; return last_level[k] orelse 400; } +/// Each folded pane's tag on the shelf this frame (`drawShelf`), for a stem about a folded pane. +var shelf_tags: [parts.len]?Rect = @splat(null); + +/// The pane a level is in. +fn paneOf(level: Level) ?Part { + return switch (level) { + .pc, .row, .head, .floor => .disasm, + .block => .band, + .y => null, + }; +} + +/// A bubble still being read after its beat's steps and an 84 f hold +/// (design/README.md, `bubble`): said once, on stderr. +var flagged: [48]bool = @splat(false); +fn flag(k: usize, still: f32) void { + const i = @min(k, flagged.len) -| 1; + if (flagged[i]) return; + flagged[i] = true; + std.debug.print("column: bubble {d} is still read {d:.1} s after its beat's steps (84 f at most)\n", .{ k, still }); +} + fn height(x: Text) f32 { const n = lineCount(x.text, bubble.chars); return bubble.first * 2 + @as(f32, @floatFromInt(n - 1)) * bubble.pitch; diff --git a/anim/src/context.zig b/anim/src/context.zig index 16278e3..9b466da 100644 --- a/anim/src/context.zig +++ b/anim/src/context.zig @@ -99,6 +99,10 @@ pub const Registers = struct { /// a row each, with its chain; the rest folded three to a row /// (design/README.md, `registers` `quiet`). All of them on the map. moves: u32 = every, + /// The quiet ones left out, not folded, rip under the rest: a `pair`'s four rows. + folds: bool = true, + /// Its tag's title (a `pair`'s names its run). + title: []const u8 = "registers", const Shown = struct { regs: zt.Regs, moment: Recording.Moment }; @@ -115,7 +119,7 @@ pub const Registers = struct { const was: Shown = .{ .regs = (now.before().regs(rec.tid) catch null) orelse is.regs, .moment = now.before() }; // Taller than its place on its way in: its own page under it. if (fold < 1) u.fill(.{ .x = 0, .y = m.tag, .w = shape.w, .h = shape.h - m.tag }, theme.color.page); - const body = u.panel(shape.w, shape.h, "registers", .{ .focus = r.focus, .legend = &legend }); + const body = u.panel(shape.w, shape.h, r.title, .{ .focus = r.focus, .legend = &legend }); // A link leaves from the panel's edge, not through a name. ties.anchor(u, .registers, .{ .x = 0, .y = 0, .w = shape.w, .h = shape.h }); clipBody(u, shape); @@ -124,7 +128,7 @@ pub const Registers = struct { const full = fullRows(r.moves); const top = body.y + m.pad; // The quiet ones under a rule across, in pwndbg's order; rip the last row. - if (full < 16) u.faded(fold).fill(.{ .x = theme.stroke.rule, .y = top + @as(f32, @floatFromInt(full)) * m.row - 1, .w = shape.w - 2 * theme.stroke.rule, .h = theme.stroke.rule }, theme.color.rule); + if (full < 16 and r.folds) u.faded(fold).fill(.{ .x = theme.stroke.rule, .y = top + @as(f32, @floatFromInt(full)) * m.row - 1, .w = shape.w - 2 * theme.stroke.rule, .h = theme.stroke.rule }, theme.color.rule); var rows_before: usize = 0; var quiet_before: usize = 0; for (theme.metric.all_registers[0..16], 0..) |name, i| { @@ -134,12 +138,13 @@ pub const Registers = struct { rows_before += 1; continue; } + if (!r.folds) continue; const y = std.math.lerp(unfolded, top + @as(f32, @floatFromInt(full + quiet_before / q.quiet_per_row)) * m.row, fold); const x = std.math.lerp(theme.metric.context.side, q.quiet_from + @as(f32, @floatFromInt(quiet_before % q.quiet_per_row)) * q.quiet_pitch, fold); quiet_before += 1; r.quietRow(u, now, is, was, name, x, y, shape.w, fold); } - r.rip(u, now, std.math.lerp(top + 16 * m.row, top + @as(f32, @floatFromInt(registerRows(r.moves) - 1)) * m.row, fold), shape.w, is, was); + r.rip(u, now, std.math.lerp(top + 16 * m.row, top + @as(f32, @floatFromInt(if (r.folds) registerRows(r.moves) - 1 else full)) * m.row, fold), shape.w, is, was); veiled(u, shape, now.veil); } @@ -155,7 +160,7 @@ pub const Registers = struct { _ = rows.reg(u.faded(1 - fold).moved(dx, 0), y, w - dx, name, is.moment, value, .{ .hops = 1 - std.math.clamp(fold / 0.4, 0, 1) }); } const hops = rows.quiet(u.faded(fold), x, y, name, is.moment, value, read_shows); - anchor(u, i, hops, y); + anchor(u, i, hops, y, value, w); } /// Whether `name` carries the system call the step makes, holding @@ -242,7 +247,7 @@ pub const Registers = struct { // `fd`, this call's once it is back, the last one's before. const gave = if (result) made else if (syscalls.latest(rec, now.stops[1])) |n| rec.syscalls[n] else null; if (gave) |g| if (g.exit != null and g.result == value and std.mem.eql(u8, zt.syscalls.name(g.nr) orelse "", "open")) fdNote(u, hops, y, rec.openOn(u.f.scratch, value, g.exit.? + 1)); - return anchor(u, i, hops, y); + return anchor(u, i, hops, y, value, w); } else if (r.quiet) { // Unmarked: the values change as the mark moves on. landed = now.advancing() >= 0.5; @@ -269,7 +274,7 @@ pub const Registers = struct { } const hops = rows.reg(if (ran < 1 and value_was != value_now) u.faded(ran) else u, y, w, name, memory, if (landed or ran < 1) value_now else value_was, marks); fdNote(u.faded(note_shows), hops, y, note); - anchor(u, i, hops, y); + anchor(u, i, hops, y, if (landed or ran < 1) value_now else value_was, w); } /// The `fd` class's note after a register's value: faint, what it is open on. @@ -277,8 +282,12 @@ pub const Registers = struct { if (hops.n > 0 and on.len > 0) u.text(.mono, theme.size.mono.context, hops.end + 8, rows.baseline(y), on, t.faint); } - fn anchor(u: ui.Ui, i: usize, hops: rows.Hops, y: f32) void { - if (hops.n > 0) ties.anchor(u, .{ .register = i }, .{ .x = hops.x[0], .y = y, .w = hops.w[0], .h = m.row }); + /// Its place, and its value shown there (a relation's join: `ties.show`). + fn anchor(u: ui.Ui, i: usize, hops: rows.Hops, y: f32, value: u64, w: f32) void { + if (hops.n == 0) return; + const r: Rect = .{ .x = hops.x[0], .y = y, .w = hops.w[0], .h = m.row }; + ties.anchor(u, .{ .register = i }, r); + ties.show(u, value, r, .registers, 0, @intCast(i), w); } }; @@ -605,7 +614,7 @@ pub const Stack = struct { return @as(f32, @floatFromInt(@as(i64, @bitCast(addr -% base)))) / 8; } }.at; - const Span = struct { y0: f32, y1: f32, name: []const u8, newest: bool, open: bool, was: ?[]const u8 = null }; + const Span = struct { y0: f32, y1: f32, name: []const u8, newest: bool, open: bool, was: ?[]const u8 = null, shows: f32 = 1 }; // A frame whose name changes in place (on through a stub) crossfades over the 12 f before the advance. const renamed = now.renaming(); const calls_was = rec.stack(u.f.scratch, now.stops[1]); @@ -614,25 +623,53 @@ pub const Stack = struct { var n_spans: usize = 0; // Under `tied` the spans ride rsp's pin: the newest stretches or shrinks with it. const p_span = pin_sp orelse p; + // Each frame is the same frame before and after the step where it ends at the same + // word (a call puts one on top, a return takes one off): never matched by depth. + const sameAs = struct { + fn of(fr: Frame, in: []const Frame) ?usize { + for (in, 0..) |o, idx| if (std.meta.eql(o.hi, fr.hi)) return idx; + return null; + } + }.of; + const n_old = @min(old_chain.len, 16); + var kept: [16]bool = @splat(false); for (chain, 0..) |frame, i| { - const old_frame = if (i < old_chain.len) old_chain[i] else frame; + const j = sameAs(frame, old_chain); + if (j) |q| if (q < kept.len) { + kept[q] = true; + }; + // A new frame grows from its foot. + const old_frame = if (j) |q| old_chain[q] else Frame{ .pc = frame.pc, .lo = frame.hi orelse frame.lo, .hi = frame.hi }; const first = std.math.lerp(rows_of(old_frame.lo, window[0]), rows_of(frame.lo, window[0]), p_span) + slide; if (first >= @as(f32, @floatFromInt(slots))) break; const last = if (frame.hi) |hi| std.math.lerp(if (old_frame.hi) |oh| rows_of(oh, window[0]) else rows_of(hi, window[0]), rows_of(hi, window[0]), p_span) + slide else std.math.inf(f32); if (last < 0) continue; const open = last >= @as(f32, @floatFromInt(slots)); const y1 = top + (@min(last, @as(f32, @floatFromInt(slots)) - 1) + 1) * m.row; - spans[n_spans] = .{ .y0 = top + @max(first, 0) * m.row, .y1 = y1, .name = spanName(u, rec, open_calls, i, if (i < named.len) named[i].pc else frame.pc), .newest = i == 0, .open = open }; - if (now.walking() and !jumped and i < old_chain.len and old_chain[i].lo == frame.lo) { - const was_name = spanName(u, rec, calls_was, i, old_chain[i].pc); + // Named as the stop the panel shows has it: before the strike by the frame as it was. + const name = if (j == null) spanName(u, rec, calls_is, i, frame.pc) else if (landed) spanName(u, rec, open_calls, i, if (i < named.len) named[i].pc else frame.pc) else spanName(u, rec, open_calls, j.?, old_chain[j.?].pc); + // A new frame comes in as rsp's pin brings it; the newest is the new one from the pin's middle. + spans[n_spans] = .{ .y0 = top + @max(first, 0) * m.row, .y1 = y1, .name = name, .newest = if (p_span < 0.5) (j != null and j.? == 0) or (j == null and i == 0 and old_chain.len == 0) else i == 0, .open = open, .shows = if (j == null and old_chain.len > 0) p_span else 1 }; + if (now.walking() and !jumped) if (j) |q| if (old_chain[q].lo == frame.lo) { + const was_name = spanName(u, rec, calls_was, q, old_chain[q].pc); const is_name = spanName(u, rec, calls_is, i, frame.pc); if (!std.mem.eql(u8, was_name, is_name)) { spans[n_spans].name = is_name; spans[n_spans].was = was_name; } - } + }; n_spans += 1; + if (n_spans == spans.len) break; } + // A frame the step takes off shrinks to its foot as rsp's pin falls, and goes with it. + for (old_chain[0..n_old], 0..) |o, q| if (!kept[q] and n_spans < spans.len and !jumped) { + const hi = o.hi orelse continue; + const first = std.math.lerp(rows_of(o.lo, window[0]), rows_of(hi, window[0]), p_span) + slide; + const last = rows_of(hi, window[0]) + slide; + if (last < 0 or first >= @as(f32, @floatFromInt(slots))) continue; + spans[n_spans] = .{ .y0 = top + @max(first, 0) * m.row, .y1 = top + (@min(last, @as(f32, @floatFromInt(slots)) - 1) + 1) * m.row, .name = spanName(u, rec, calls_was, q, o.pc), .newest = q == 0 and p_span < 0.5, .open = false, .shows = 1 - p_span }; + n_spans += 1; + }; // rsp or rbp travelling alone keeps its place in the pin it leaves and // the one it joins: room there, closing and opening as it goes. It // sets off 18 f after rsp's strike (`time.pin`): 24 f to the next @@ -749,6 +786,8 @@ pub const Stack = struct { // Only a row on show is a place a tie can land. if (y >= top - 1 and y + m.row <= box.h - m.pad + 1) { ties.anchor(u, .{ .word = addr }, .{ .x = theme.metric.slot.address, .y = y, .w = if (hops.n > 0) hops.x[0] + hops.w[0] - theme.metric.slot.address else 120, .h = m.row }); + // Its value, where it may be an address: its first hop. + if (hops.n > 0) if (if (fades) word_is else (Recording.word(now.shownAt(addr, 8), addr, 8) catch null)) |value| ties.show(u, value, .{ .x = hops.x[0], .y = y, .w = hops.w[0], .h = m.row }, .stack, addr, 0, box.w); ties.anchor(u, .{ .slot_pin = addr }, .{ .x = theme.metric.slot.lane, .y = y, .w = theme.metric.slot.lane_end - theme.metric.slot.lane, .h = m.row }); } } @@ -764,7 +803,8 @@ pub const Stack = struct { hexdump.pointerArcs(&arcs, now, word.addr, word.y, at_rows.items, 8, .{ top - m.pad, box.h - m.pad }, false, false); if (arcs.n == before) continue; // Its step's alone: it goes with the advance (18 f), not in the next step's first frame. - if (saved and touched) arcs.items[before].shows *= 1 - std.math.clamp((now.time - now.plan.advance) / time.read.out, 0, 1); + // And it comes as the step begins (12 f), not in its first frame. + if ((saved or saved_was[0]) and touched) arcs.items[before].shows *= @min(1 - std.math.clamp((now.time - now.plan.advance) / time.read.out, 0, 1), if (now.resting()) 1 else std.math.clamp(now.time / time.quick, 0, 1)); // A word becoming a saved rbp, or no longer one, with the step: its arc goes or comes as its role does (12 f). if (!touched and saved != saved_was[0]) arcs.items[before].shows *= if (saved) 1 - recast else recast; } @@ -772,7 +812,7 @@ pub const Stack = struct { // Where the window is, for a link to a word outside it. const lane_at = u.at.xf.apply(.{ theme.metric.slot.lane, top }); const row_px = u.at.xf.apply(.{ 0, top + m.row })[1] - lane_at[1]; - if (!ui.mapping) ties.stack_window = .{ .first = window[0], .slots = slots, .x = lane_at[0], .top = lane_at[1], .row = row_px }; + if (!ui.mapping) ties.of(u.f).stack_window = .{ .first = window[0], .slots = slots, .x = lane_at[0], .top = lane_at[1], .row = row_px }; // The travelling tags: both of them at one word share a pin. var places: [2]f32 = undefined; var olds: [2]f32 = undefined; @@ -786,7 +826,7 @@ pub const Stack = struct { place.* = std.math.lerp(at_old, at_new, goes(now, was, is, name)); } // `tied`: rsp's move on its pin as it travels, counting a row at a time, held to the advance and crossfading to the bare name (12 f). - const amount_shows: f32 = if (tied and now.walking() and pin_sp != null and now.time >= now.plan.pins[0]) 1 - std.math.clamp((now.time - now.plan.advance) / time.quick, 0, 1) else 0; + const amount_shows: f32 = if (tied and now.walking() and pin_sp != null and now.time >= now.plan.pins[0]) @min(std.math.clamp((now.time - now.plan.pins[0]) / time.strike.in, 0, 1), 1 - std.math.clamp((now.time - now.plan.advance) / time.quick, 0, 1)) else 0; for (travel, places[0..travel.len], olds[0..travel.len], 0..) |name, place, old, i| { if (i == 1 and @abs(places[0] - place) < 0.05) continue; if (place < (if (tied) @as(f32, -1) else -0.5) or place > (if (tied) slots_f else slots_f - 0.5)) continue; @@ -805,10 +845,10 @@ pub const Stack = struct { if (crossed > 0.05) u.faded(amount_shows).bracket(theme.metric.arc.lanes[0], top + @min(place, old) * m.row + 2, top + @max(place, old) * m.row - 2, theme.mark(.write).solid, theme.metric.bracket.tie, 1, false); } else u.pinRow(x, top + place * m.row, names_, theme.color.region.stack); } - for (spans[0..n_spans]) |sp| if (sp.was) |was_name| { + for (spans[0..n_spans]) |sp| if (sp.shows > 0) if (sp.was) |was_name| { rows.span(v.faded(1 - renamed), map.content(box.w), sp.y0, sp.y1, was_name, sp.newest, sp.open); rows.span(v.faded(renamed), map.content(box.w), sp.y0, sp.y1, sp.name, sp.newest, sp.open); - } else rows.span(v, map.content(box.w), sp.y0, sp.y1, sp.name, sp.newest, sp.open); + } else rows.span(v.faded(sp.shows), map.content(box.w), sp.y0, sp.y1, sp.name, sp.newest, sp.open); veiled(u, box, now.veil); if (k.part == .stack) map.strip(.stack, u, box); } diff --git a/anim/src/cursor.zig b/anim/src/cursor.zig index 5cb625a..e9aab67 100644 --- a/anim/src/cursor.zig +++ b/anim/src/cursor.zig @@ -32,6 +32,14 @@ pub fn glide(t: f32) f32 { return theme.ease(.glide, t); } +/// `glide` for a scroll of `px` pixels over `seconds`, at least a pixel a +/// frame from rest: a glide's first frames are each under a pixel, and rows +/// drawn on whole pixels would step one and stand still a frame. +pub fn glidePixels(t: f32, seconds: f32, px: f32) f32 { + if (px < 1) return glide(t); + return @min(@max(glide(t), t * seconds * 60 / px), 1); +} + /// For things that answer the program (the mark, cards, tags, an arrow /// growing): off promptly, a long settle. pub fn standard(t: f32) f32 { @@ -292,6 +300,9 @@ pub const Cursor = struct { sprint_veil: anim.Value, /// Steps taken. count: anim.Value, + /// The `aim` relation's knob (design/README.md, "4. Ties"): 0 off (the + /// default), 1 standing (in and out over 18 f), 2 for the step. + aim_knob: anim.Value, /// The same values a little behind (`time.minimap_lag`, then a frame /// later each, `time.minimap_fade` of them): what the minimap draws /// from, secondary action that never starts on a primary onset's frame @@ -321,6 +332,7 @@ pub const Cursor = struct { .veil = s.value(0), .sprint_veil = s.value(0), .count = s.value(0), + .aim_knob = s.value(0), .lags = undefined, .first = start, .last = start, @@ -762,6 +774,20 @@ pub const Cursor = struct { } }; + /// The `aim` relation's knob from here on (design/README.md, "4. Ties": + /// off unless the author turns it on, for a scene or a beat): `.standing` + /// draws the aims of the words on screen, coming and going over 18 f; + /// `.step` those of the words a walked step writes or reads. + pub fn aim(c: Cursor, s: *anim.Frame, knob: enum { off, step, standing }) void { + // Over what follows: the timeline's cursor stays where it was. + const start = s.cursor; + defer s.cursor = start; + switch (knob) { + .off, .standing => s.play(.{c.aim_knob.to(if (knob == .off) 0 else 1)}, .{ .seconds = 18.0 / 60.0, .ease = anim.ease.linear }), + .step => s.play(.{c.aim_knob.to(2)}, .{ .seconds = 0 }), + } + } + /// There at once, with nothing struck: a cut. pub fn jump(c: Cursor, s: *anim.Frame, to: u64) void { const Jump = struct { @@ -797,6 +823,7 @@ pub const Cursor = struct { .crossings = c.levels.crossings, .annotation = c.levels.annotation, .way = .{ c.first, c.last }, + .aim = placed.value(c.aim_knob), }; var plan: [Plan.values]f32 = undefined; for (&plan, c.plan) |*is, v| is.* = placed.value(v); @@ -869,6 +896,8 @@ pub const Now = struct { annotation: @FieldType(Levels, "annotation") = .tied, /// The scene's way through the recording: its first moment and its last. way: [2]u64 = .{ 0, 0 }, + /// The `aim` knob: 0 off, up to 1 standing (coming or going), 2 for the step. + aim: f32 = 0, /// The program's state as shown. pub fn moment(n: Now) Recording.Moment { @@ -1069,7 +1098,8 @@ pub const Now = struct { if (n.resting()) return .ember; if (since_strike < 0) { var armed = was; - if (since_strike >= -s.arm) armed.arm = std.math.clamp((since_strike + s.arm) / time.stagger, 0, 1); + // In over 4 f (the catalogue's 2 f left a frame of a few pixels: a pop). + if (since_strike >= -s.arm) armed.arm = std.math.clamp((since_strike + s.arm) / (2 * time.stagger), 0, 1); return armed; } const in = std.math.clamp(since_strike / s.in, 0, 1); diff --git a/anim/src/descent.zig b/anim/src/descent.zig index ff478df..ad3a5c5 100644 --- a/anim/src/descent.zig +++ b/anim/src/descent.zig @@ -123,8 +123,11 @@ pub const Descent = struct { const now = d.cursor.now(placed); const rec = d.cursor.rec; const room = theme.layout.rowsIn(box.h); - const before = layout(u, rec, now.stops[1], room, d.root); - const after = layout(u, rec, now.stops[0], room, d.root); + // The panel growing or shrinking at rest (a column's change): the rows of + // its two heights' layouts glide between them, the rest fade at the edge; never empty. + const resize = if (now.resting()) d.stage.resizing(placed, .disasm) else null; + const before = layout(u, rec, now.stops[if (resize != null) 0 else 1], if (resize) |r| theme.layout.rowsIn(r.from) else room, d.root); + const after = layout(u, rec, now.stops[0], if (resize) |r| theme.layout.rowsIn(r.to) else room, d.root); // On the map its box is as long as the rows it has wanted: not 69 for a function of 30. if (ui.mapping) map.measure(.disasm, @max(before.items.len, after.items.len)); const going = now.advancing(); @@ -150,8 +153,10 @@ pub const Descent = struct { // How far the rows both lists have are on their way, and how much // of each that leaves or comes shows: the verb's timetable. + // At rest `going` is 1; a resize slides by its own progress. + const slide = if (resize) |r| r.p else going; const move: f32 = switch (kind) { - .same => cursors.glide(going), + .same => if (resize != null) slide else cursors.glide(going), .down => cursors.glide(clamp01((tm - f(78)) / f(18))), .through => cursors.glide(clamp01((tm - f(36)) / f(18))), .up => cursors.glide(clamp01((tm - f(84)) / f(18))), @@ -176,7 +181,7 @@ pub const Descent = struct { const it = before.items[i]; if (find(after.items, it.key)) |_| continue; const shows: f32 = switch (kind) { - .same => 1 - clamp01(2 * going), + .same => 1 - clamp01(2 * slide), .down => 1 - clamp01((tm - f(66)) / f(12)), .through => 1 - clamp01((tm - f(24)) / f(12)), .up => 1 - clamp01((tm - f(48) - @as(f32, @floatFromInt(leaving)) * f(2)) / f(12)), @@ -199,7 +204,7 @@ pub const Descent = struct { } const head = it.what == .head; const shows: f32 = switch (kind) { - .same => clamp01(2 * going - 1), + .same => clamp01(2 * slide - 1), .down => if (head) clamp01((tm - f(108)) / f(12)) else clamp01((tm - f(114) - @as(f32, @floatFromInt(arriving)) * f(2)) / f(12)), .through => if (head) clamp01((tm - f(66)) / f(12)) else clamp01((tm - f(72) - @as(f32, @floatFromInt(arriving)) * f(2)) / f(12)), .up => clamp01((tm - f(102)) / f(12)), @@ -226,7 +231,7 @@ pub const Descent = struct { .up => 1 - clamp01((tm - f(48)) / f(12)), }, // The one it lands on, with the advance. - if (kind == .same) 1 else clamp01(going * 2), + if (kind == .same) 1 else clamp01(going * 2 - 1), }; // A crossing stepped over: the kernel's window opens under the call // row, past the arguments hanging under it; the rows after them make @@ -256,10 +261,10 @@ pub const Descent = struct { // In one window the plaque slides from row to row, with the advance. var mark: ?struct { y: f32, shows: f32 } = null; if (kind == .same) { - if (pc_y[0] != null and pc_y[1] != null) mark = .{ .y = std.math.lerp(pc_y[0].?, pc_y[1].?, cursors.standard(going)), .shows = 1 } else if (pc_y[1]) |y| { - mark = .{ .y = y, .shows = clamp01(2 * going - 1) }; + if (pc_y[0] != null and pc_y[1] != null) mark = .{ .y = std.math.lerp(pc_y[0].?, pc_y[1].?, if (resize != null) slide else cursors.standard(going)), .shows = 1 } else if (pc_y[1]) |y| { + mark = .{ .y = y, .shows = clamp01(2 * slide - 1) }; } else if (pc_y[0]) |y| { - mark = .{ .y = y, .shows = 1 - clamp01(2 * going) }; + mark = .{ .y = y, .shows = 1 - clamp01(2 * slide) }; } } const fire = now.fires(.disasm); @@ -269,7 +274,8 @@ pub const Descent = struct { } // Which rows ran in the last three steps: the trail. - const ran = recentRows(a, now, if (going >= 0.5) 0 else 1); + const ran_before = recentRows(a, now, 1); + const ran_after = recentRows(a, now, 0); // The line the source panel is on, in the file it shows: rows of that file carry their numbers. const current = rec.shown[marked]; const current_line = current.line; @@ -280,7 +286,6 @@ pub const Descent = struct { var last_line: ?u32 = null; var last_window: ?u8 = null; const dd: disasm.Disasm = .{ .cursor = d.cursor, .stage = d.stage }; - const base: usize = if (going >= 0.5) 0 else 1; // Each window's blocks (`graph`), read once: a rule over each one's first row, its visits where the row has room. var rules: [8]?graph.Rules = @splat(null); for (placed_items.items) |p| { @@ -306,39 +311,62 @@ pub const Descent = struct { const window_items = windowOf(placed_items.items, p.item.window); var addrs: [80]u64 = undefined; var n: usize = 0; - for (window_items) |w| if (w.item.what == .insn and n < addrs.len) { + // Its own rows that show: not another window's between them, nor a row gone at rest. + for (window_items) |w| if (w.item.what == .insn and w.item.window == p.item.window and w.shows > 0 and n < addrs.len) { addrs[n] = w.item.key; n += 1; }; const lead = ui.shared(addrs[0..n]); const wide = @as(f32, @floatFromInt(@max(ui.digits(row.addr), 7) - 7)) * adv; - const line = if (row.outside or row.src != current.src) null else row.line; - const state: disasm.Disasm.State = switch (p.item.state) { - // The row the plaque has not reached yet is ahead. - .current => if (p.old == null or p.old.?.state != .current) (if (going >= 0.5) .current else .ahead) else if (p.new == null or p.new.?.state != .current) (if (going < 0.5) .current else .ahead) else .current, - .waiting => .{ .waiting = p.item.at }, - .pass => .{ .ran = .{ .k = 0, .at = p.item.at } }, - .plain => if (ranOf(ran, row.addr)) |r| .{ .ran = r } else .ahead, - }; - // The marks at the row's left: the trail, the ring, the pass. - switch (p.item.state) { - .plain => if (ranOf(ran, row.addr)) |r| v.trail(c.mark, y, r.k), - .pass => v.trail(c.mark, y, 0), - .waiting => { - // The ring comes as the plaque leaves; going up it becomes a trail bar. - const ring: f32 = if (p.old != null and p.old.?.state == .waiting) (if (kind == .up) 1 - clamp01((tm - f(114)) / f(12)) else 1) else if (kind == .down) clamp01((tm - f(84)) / f(12)) else 1; - ringAt(v.faded(ring), y); - if (ring < 1) v.faded(1 - ring).trail(c.mark, y, 0); - }, - .current => {}, + // The row as before the advance and as after it (the pc's row, its trail, + // the line numbers of the file the source shows): where the two differ, the + // one crossfades to the other over the advance's middle, never in a frame. + const after_side = going >= 0.5; + const across: f32 = if (kind == .same and now.resting()) 1 else clamp01((going - 0.25) * 2); + var line: ?u32 = null; + var hangs: usize = 0; + const covers = if (p.twin) covered_before > 0 else covered > 0; + for ([2]bool{ !after_side, after_side }) |side| { + const ran_s = if (side) ran_after else ran_before; + const cur_s = rec.shown[now.marks[@intFromBool(side)]]; + const line_s = if (row.outside or row.src != cur_s.src) null else row.line; + const state: disasm.Disasm.State = switch (p.item.state) { + // The row the plaque has not reached yet is ahead. + .current => if (p.old == null or p.old.?.state != .current) (if (side) .current else .ahead) else if (p.new == null or p.new.?.state != .current) (if (!side) .current else .ahead) else .current, + .waiting => .{ .waiting = p.item.at }, + .pass => .{ .ran = .{ .k = 0, .at = p.item.at } }, + .plain => if (ranOf(ran_s, row.addr)) |r| .{ .ran = r } else .ahead, + }; + // The side the advance is on draws in full where the two are the same. + const main = side == after_side; + const other_line = if (row.outside or row.src != rec.shown[now.marks[@intFromBool(!side)]].src) null else row.line; + const ran_o = if (!side) ran_after else ran_before; + const same = std.meta.eql(line_s, other_line) and cur_s.line == rec.shown[now.marks[@intFromBool(!side)]].line and p.item.state != .current and std.meta.eql(ranOf(ran_s, row.addr), ranOf(ran_o, row.addr)); + const alpha: f32 = if (same) (if (main) 1 else 0) else if (side) across else 1 - across; + if (main) line = line_s; + if (alpha <= 0) continue; + const vs = v.faded(alpha); + const base_s: usize = if (side) 0 else 1; + // The marks at the row's left: the trail, the ring, the pass. + switch (p.item.state) { + .plain => if (ranOf(ran_s, row.addr)) |r| vs.trail(c.mark, y, r.k), + .pass => vs.trail(c.mark, y, 0), + .waiting => { + // The ring comes as the plaque leaves; going up it becomes a trail bar. + const ring: f32 = if (p.old != null and p.old.?.state == .waiting) (if (kind == .up) 1 - clamp01((tm - f(114)) / f(12)) else 1) else if (kind == .down) clamp01((tm - f(84)) / f(12)) else 1; + ringAt(vs.faded(ring), y); + if (ring < 1) vs.faded(1 - ring).trail(c.mark, y, 0); + }, + .current => {}, + } + // A row drawn twice (crossfading) counts once for the arguments that hang over the rows under it. + const h = dd.insn(vs, now, row, y, map.content(box.w), widen0 + wide, line_s, line_s != last_line, cur_s.line, lead, state, base_s, covers, .{ addrs[0], addrs[n -| 1] }, null); + if (main) hangs = h; + const w = @min(p.item.window, rules.len - 1); + if (rules[w] == null) rules[w] = graph.Rules.init(u.f.scratch, rec, now.marks, .{ row.addr, row.addr }, going); + rules[w].?.draw(vs, row.addr, y, map.content(box.w), !covers and !row.outside and !dd.noted(vs, now, row, y, state, base_s, .{ addrs[0], addrs[n -| 1] }, null)); } runs.add(line != null and line.? == current_line, y, y + m.row); - // A row drawn twice (crossfading) counts once for the arguments that hang over the rows under it. - const covers = if (p.twin) covered_before > 0 else covered > 0; - const hangs = dd.insn(v, now, row, y, map.content(box.w), widen0 + wide, line, line != last_line, current_line, lead, state, base, covers, .{ addrs[0], addrs[n -| 1] }, null); - const w = @min(p.item.window, rules.len - 1); - if (rules[w] == null) rules[w] = graph.Rules.init(u.f.scratch, rec, now.marks, .{ row.addr, row.addr }, going); - rules[w].?.draw(v, row.addr, y, map.content(box.w), !covers and !row.outside and !dd.noted(v, now, row, y, state, base, .{ addrs[0], addrs[n -| 1] }, null)); if (!p.twin) covered_before = covered; covered = if (p.twin) @max(covered, hangs) else @max(covered -| 1, hangs); last_line = line; diff --git a/anim/src/disasm.zig b/anim/src/disasm.zig index 085982d..6c9e2dc 100644 --- a/anim/src/disasm.zig +++ b/anim/src/disasm.zig @@ -75,13 +75,15 @@ pub const Disasm = struct { focus: bool = false, /// On the map its box is as long as what it shows (not where it is a strip's column alone). measures: bool = true, + /// Its window this many rows whatever its box (a `pair`'s stopped run: the box shortens over its lower rows). + rows: ?usize = null, pub fn draw(d: *const Disasm, f: *sk.Frame, placed: anim.Placed) void { const box = d.stage.box(placed, .disasm); const u = context.at(f, placed, box); const now = d.cursor.now(placed); const rec = d.cursor.rec; - const room = theme.layout.rowsIn(box.h); + const room = d.rows orelse theme.layout.rowsIn(box.h); // The mark: it moves last, with the source's and rip. const going = now.advancing(); const from = listing(u, rec, now.marks[0], room); @@ -127,7 +129,7 @@ pub const Disasm = struct { fade = if (ui.mapping) 1 else @abs(2 * going - 1); } } - const scroll = std.math.lerp(offsets[0], offsets[1], cursors.glide(going)); + const scroll = std.math.lerp(offsets[0], offsets[1], cursors.glidePixels(going, now.plan.travel, @abs(offsets[1] - offsets[0]) * m.row * u.at.scale)); const lit = if (fade < 1) u.faded(fade) else u; // Where the pc's plaque is: it slides from the row it was on to the @@ -144,7 +146,6 @@ pub const Disasm = struct { const base: usize = if (going >= 0.5) 0 else 1; // The line the source panel is on, in the file it shows: rows of that file carry their numbers. const current = rec.shown[now.marks[if (going >= 0.5) 1 else 0]]; - const current_line = current.line; // The addresses' shared digits, of the window the mark is in: they // change with the advance, not before it. const lead = blk: { @@ -170,17 +171,21 @@ pub const Disasm = struct { // `ran`: what the last three steps ran (every instruction, where the // recording is by instruction), the newest step's mark on each row, // and the moment it last ran at for its annotation. - const ran = u.f.scratch.alloc(?Ran, merged.len) catch return; - @memset(ran, null); - for (0..3) |k| { - const first = now.stops[@min(base + 1 + k, now.stops.len - 1)]; - const last = now.stops[base + k]; - var at = last; - // shortcut: a step through more than 4096 moments marks its last 4096. - while (at > first and last - at < 4096) { - at -= 1; - const j = rowOf(merged, rec.register(at, "rip")) orelse continue; - if (ran[j] == null) ran[j] = .{ .k = k, .at = at }; + // As before the advance and as after it (`base` 1 and 0): the row crossfades between them. + var rans: [2][]?Ran = undefined; + for (&rans, [2]usize{ 1, 0 }) |*ran, b| { + ran.* = u.f.scratch.alloc(?Ran, merged.len) catch return; + @memset(ran.*, null); + for (0..3) |k| { + const first = now.stops[@min(b + 1 + k, now.stops.len - 1)]; + const last = now.stops[b + k]; + var at = last; + // shortcut: a step through more than 4096 moments marks its last 4096. + while (at > first and last - at < 4096) { + at -= 1; + const j = rowOf(merged, rec.register(at, "rip")) orelse continue; + if (ran.*[j] == null) ran.*[j] = .{ .k = k, .at = at }; + } } } // The jump that brought control here: the instruction just before @@ -227,11 +232,31 @@ pub const Disasm = struct { map.row(.disasm, row.addr, u, .{ .x = 0, .y = y, .w = box.w, .h = m.row }); ties.anchor(u, .{ .row = row.addr }, .{ .x = 0, .y = y, .w = box.w, .h = m.row }); const is_pc = row.addr == pc[if (going >= 0.5) 1 else 0]; - const was = if (is_pc) null else ran[j]; - if (was) |r| lit.trail(c.mark, y, r.k); - const state: State = if (is_pc) .current else if (was) |r| .{ .ran = r } else .ahead; - const hangs = d.insn(lit, now, row, y, map.content(box.w), widen, line, line != last_line, current_line, lead, state, base, covered > 0, window, echo); - rules.draw(lit, row.addr, y, map.content(box.w), covered == 0 and !row.outside and !d.noted(lit, now, row, y, state, base, window, echo)); + // The row as before the advance and as after it (its state, its trail, its + // line number): where they differ, the one crossfades to the other over the + // advance's middle, never in a frame. + const across: f32 = if (now.resting()) 1 else std.math.clamp((going - 0.25) * 2, 0, 1); + var hangs: usize = 0; + for ([2]usize{ 1, 0 }) |b| { + const cur_s = rec.shown[now.marks[1 - b]]; + const line_s = if (row.outside or row.src != cur_s.src) null else row.line; + const pc_s = row.addr == pc[1 - b]; + const was_s = if (pc_s) null else rans[1 - b][j]; + const state: State = if (pc_s) .current else if (was_s) |r| .{ .ran = r } else .ahead; + const main = b == base; + const o = 1 - b; + const cur_o = rec.shown[now.marks[1 - o]]; + const line_o = if (row.outside or row.src != cur_o.src) null else row.line; + const pc_o = row.addr == pc[1 - o]; + const same = pc_s == pc_o and std.meta.eql(line_s, line_o) and cur_s.line == cur_o.line and std.meta.eql(was_s, if (pc_o) null else rans[1 - o][j]); + const alpha: f32 = if (same) (if (main) 1 else 0) else if (b == 0) across else 1 - across; + if (alpha <= 0) continue; + const v = lit.faded(alpha); + if (was_s) |r| v.trail(c.mark, y, r.k); + const h = d.insn(v, now, row, y, map.content(box.w), widen, line_s, line_s != last_line, cur_s.line, lead, state, b, covered > 0, window, echo); + if (main) hangs = h; + rules.draw(v, row.addr, y, map.content(box.w), covered == 0 and !row.outside and !d.noted(v, now, row, y, state, b, window, echo)); + } covered = @max(covered -| 1, hangs); last_line = if (above_body) null else line; if (jumped == row.addr and !is_pc) lanes[0] = y; @@ -292,7 +317,7 @@ pub const Disasm = struct { if (!ui.mapping and now.annotation == .tied and now.walking() and (now.plan.touch >= 0 or now.plan.carry >= 0)) if (touchPc(rec, now)) |pc| if (pc == row.addr) { const a = at.at.xf.apply(.{ w + theme.strip.cost, y }); const b = at.at.xf.apply(.{ w + theme.strip.cost, y + m.row }); - ties.touch_row = .{ a[0], a[1], b[0], b[1] }; + ties.of(at.f).touch_row = .{ a[0], a[1], b[0], b[1] }; }; if (first_of_line) if (line) |l| { const num = u.print("{d}", .{l}); @@ -407,37 +432,36 @@ pub const Disasm = struct { } // `tied`: an instruction that moves rsp or rbp says the move first. if (now.annotation == .tied) if (a.stackMove(now, row, moment, after, state, base)) |note| return note; + // What it left behind is shown once it has run; in the step that + // makes it, it fades in from the CPU's strike (12 f), never in a frame. + const appears: f32 = if (state == .current and after != null) now.moving(.cpu, 0, theme.time.quick) else 1; + const value_at: ?u64 = switch (state) { + .ran => after, + .current => if (after != null and appears > 0) after.? else null, + .ahead, .waiting => null, + }; + const new = Say{ .u = u.faded(appears), .rec = rec, .y = y, .x = x, .room = room }; // A compare: the two values, then (once it has run) the flags it leaves. if (std.mem.eql(u8, mn, "cmp") or std.mem.eql(u8, mn, "test")) { const left = a.compared(row, moment, touches[0]); var end = a.word(x, left[0], t.primary); end = a.word(end, " : ", t.faint); end = a.word(end, left[1], t.primary); - const left_at: ?u64 = switch (state) { - .ran => after, - .current => if (after != null and now.landed(.cpu, 0)) after else null, - .ahead, .waiting => null, - }; - if (left_at) |l| { + if (value_at) |l| { var buf: [256]u8 = undefined; const next = rec.instruction(moment, row.addr + row.len, &buf); var mask = if (next) |n| flagsRead((Insn{ .addr = 0, .len = 0, .text = n.text }).mnemonic()) else 0; if (mask == 0) mask = flag_bits.cf | flag_bits.zf | flag_bits.sf | flag_bits.of; - end = flags(u, end + 2 * adv, y, rec.register(l, "eflags"), mask, if (echo) |e| if (e.addr == row.addr) e.shows else 0 else 0); + end = flags(new.u, end + 2 * adv, y, rec.register(l, "eflags"), mask, if (echo) |e| if (e.addr == row.addr) e.shows else 0 else 0); } return .{ .drawn = true, .end = end }; } // The rest is what it left behind, or where it reads: by what it is. - const value_at: ?u64 = switch (state) { - .ran => after, - .current => if (after != null and now.landed(.cpu, 0)) after.? else null, - .ahead, .waiting => null, - }; // A place the C library's layout names: the canary. if (std.mem.indexOf(u8, row.text, "fs:[0x28]") != null) { const end = a.word(x, if (x + 35 * adv <= room) "the stack's canary" else "canary", t.dim); const word_ = if (touches[0]) |touch| Recording.word(rec.moment(value_at orelse moment), touch.addr, 8) catch null else null; - if (word_) |v| return .{ .drawn = true, .end = a.word(end + adv, u.print("{x}", .{v}), t.primary) }; + if (word_) |v| return .{ .drawn = true, .end = (if (value_at != null) new else a).word(end + adv, u.print("{x}", .{v}), t.primary) }; u.unknown(.{ .x = end + adv, .y = y, .w = 24, .h = m.row }); return .{ .drawn = true, .end = end + adv + 24 }; } @@ -445,8 +469,8 @@ pub const Disasm = struct { if (std.mem.eql(u8, mn, "lea")) { const at = value_at orelse return .{}; const reg = written(row) orelse return .{}; - const end = a.word(x, "=", t.faint); - return .{ .drawn = true, .end = rows.chain(u, end + adv, y, rec.moment(at), rec.register(at, reg), room, .{}).end }; + const end = new.word(x, "=", t.faint); + return .{ .drawn = true, .end = rows.chain(new.u, end + adv, y, rec.moment(at), rec.register(at, reg), room, .{}).end }; } // A `rep`: how many times, how wide each, and where it goes. if (std.mem.startsWith(u8, mn, "rep")) { @@ -494,9 +518,9 @@ pub const Disasm = struct { const reg = written(row) orelse return .{}; const at = value_at orelse return .{}; const value = rec.register(at, reg); - u.text(.mono, size, x, base_y, reg, t.dim); + new.u.text(.mono, size, x, base_y, reg, t.dim); // `tied`: its first value (the registers' row has its chain, struck in the same frame). - return .{ .drawn = true, .end = rows.chain(u, x + @as(f32, @floatFromInt(reg.len + 1)) * adv, y, rec.moment(at), value, room, .{ .most = if (now.annotation == .tied) 1 else 6 }).end }; + return .{ .drawn = true, .end = rows.chain(new.u, x + @as(f32, @floatFromInt(reg.len + 1)) * adv, y, rec.moment(at), value, room, .{ .most = if (now.annotation == .tied) 1 else 6 }).end }; } }; @@ -607,12 +631,15 @@ const Say = struct { var end = a.word(a.x, "rbp = rsp", t.primary); // Its value once rbp has landed, struck with it. const lands = ties.strikeDelay(now, cursors.bp_index); + // In from rbp's strike (12 f), never in a frame. + const shows: f32 = if (state == .current and after != null) now.moving(.cpu, -lands, theme.time.quick) else 1; const value_at: ?u64 = switch (state) { .ran => after, - .current => if (after != null and now.landed(.cpu, lands)) after.? else null, + .current => if (after != null and shows > 0) after.? else null, .ahead, .waiting => null, }; - if (value_at) |v| end = a.struck(end + adv, a.u.print("{x}", .{a.rec.register(v, "rbp")}), if (state == .current and base == 1) now.heat(now.registerAge(cursors.bp_index), null, now.since(.cpu, lands)) else .none); + const b: Say = .{ .u = a.u.faded(shows), .rec = a.rec, .y = a.y, .x = a.x, .room = a.room }; + if (value_at) |v| end = b.struck(end + adv, a.u.print("{x}", .{a.rec.register(v, "rbp")}), if (state == .current and base == 1) now.heat(now.registerAge(cursors.bp_index), null, now.since(.cpu, lands)) else .none); return .{ .drawn = true, .end = end }; } else return null; var role: ?Role = null; diff --git a/anim/src/graph.zig b/anim/src/graph.zig index 01b4ac7..9eb6f2d 100644 --- a/anim/src/graph.zig +++ b/anim/src/graph.zig @@ -2035,7 +2035,11 @@ pub const Blocks = struct { const name = ui.symbol(u, full); const short = name[0 .. std.mem.lastIndexOfScalar(u8, name, '+') orelse name.len]; const title = if (std.mem.indexOfScalar(u8, full, '!')) |bang| u.print("the blocks of {s} \u{00b7} {s}", .{ short, full[0..bang] }) else u.print("the blocks of {s}", .{short}); - _ = u.panel(box.w, box.h, title, .{ .focus = b.focus, .note = if (v.g.rings.len > 0) "its blocks \u{00b7} the visits so far \u{00b7} the path \u{00b7} the loops' turns" else "its blocks \u{00b7} the visits so far \u{00b7} the path" }); + const note = if (v.g.rings.len > 0) "its blocks \u{00b7} the visits so far \u{00b7} the path \u{00b7} the loops' turns" else "its blocks \u{00b7} the visits so far \u{00b7} the path"; + _ = u.panel(box.w, box.h, title, .{ .focus = b.focus }); + // Its labels stay inside its box at every size: the note fades where it no longer fits beside the title. + const need = 28 + u.width(.sans_medium, theme.size.sans.tag, title) + 24 + u.width(.sans, theme.size.sans.micro, note) + 10; + u.faded(std.math.clamp((box.w - need) / 48, 0, 1)).tagNote(box.w, note); u.clip(.{ .x = rule, .y = m.tag, .w = room[0] * z, .h = room[1] * z }); var big = u.moved(rule, m.tag); big.at.xf = big.at.xf.mul(.scale(z, z)); diff --git a/anim/src/hexdump.zig b/anim/src/hexdump.zig index a35f091..9b39bf6 100644 --- a/anim/src/hexdump.zig +++ b/anim/src/hexdump.zig @@ -46,10 +46,15 @@ pub fn kindOf(things: []const objects.Object, moment: Recording.Moment, addr: u6 /// The rows of a region (`lo .. hi`, sixteen-aligned) a dump of `count` /// rows shows: all of them, or a window round the first the step touched. pub fn window(now: cursors.Now, lo: u64, hi: u64, count: usize) u64 { + return windowAt(now.rec, now.stops[1], now.stops[0], lo, hi, count); +} + +/// The same for the step from moment `from` to `to`. +pub fn windowAt(rec: *const Recording, from: u64, to: u64, lo: u64, hi: u64, count: usize) u64 { const all = (hi - lo) / 16; if (all <= count) return lo; var touched: ?u64 = null; - for ([_][]const Recording.Span{ now.rec.written(now.stops[1], now.stops[0]), now.rec.read(now.stops[1], now.stops[0]) }) |spans| for (spans) |span| { + for ([_][]const Recording.Span{ rec.written(from, to), rec.read(from, to) }) |spans| for (spans) |span| { if (touched == null and span.addr >= lo and span.addr < hi) touched = span.addr & ~@as(u64, 15); }; const at = touched orelse lo; @@ -368,6 +373,20 @@ pub fn pointerArcs(arcs: *rows.Arcs, now: cursors.Now, field: u64, from: f32, sh const time_arc = theme.time.arc; +/// What the kernel fills strikes a byte every 2 f, as it lands: the +/// kernel's fill is no write the recording has, so by its own times, +/// cooling with the advance (`run` from `here`). +fn givenStrike(now: cursors.Now, of: anytype, here: u64, run: *rows.Run) void { + const g = of.given orelse return; + const since = g.since orelse return; + if (here + run.len <= g.addr or here >= g.addr + g.bytes.len) return; + const first = @max(here, g.addr) - g.addr; + const struck = since - @as(f32, @floatFromInt(first)) * @import("syscalls.zig").Plan.byte; + if (struck >= 0) run.heat = .{ .level = 1, .solid = 1 - std.math.clamp((now.time - now.plan.advance) / theme.time.strike.cool, 0, 1), .in = std.math.clamp(struck / theme.time.strike.in, 0, 1) }; + run.changed = .{ @intCast(@max(here, g.addr) - here), @intCast(@min(here + run.len, g.addr + g.bytes.len) - 1 - here) }; + run.apart = 2; +} + /// One arc from the row at `from` to the row of `shown` that holds /// `target`; a stub toward the rule for one in the region past them, only /// for the step's (`state.hot`). @@ -391,6 +410,8 @@ fn aim(arcs: *rows.Arcs, now: cursors.Now, field: u64, target: u64, from: f32, s // Past the rows: a stub, in the step that touches it, when it is the same region's. if (!state.hot or rows.regionOf(moment, target) != rows.regionOf(moment, field) or rows.regionOf(moment, target) == .none) return; arc.stub = true; + // Its step's alone (not a loupe's, always full): in as the step begins (12 f), out with the advance, never in a frame. + if (state.full < 1) arc.shows *= if (now.resting()) 0 else @min(std.math.clamp(now.time / theme.time.quick, 0, 1), 1 - std.math.clamp((now.time - now.plan.advance) / theme.time.read.out, 0, 1)); arc.to = if (target < shown[0].addr) rule[0] else rule[1]; arcs.add(arc); } @@ -606,7 +627,8 @@ fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Mom if (of.given) |g| for (0..16) |i| { const a = addr + i; if (a < g.addr or a >= g.addr + g.bytes.len) continue; - if (g.since) |since| if (since < @as(f32, @floatFromInt(a - g.addr)) * @import("syscalls.zig").Plan.byte) continue; + // Each byte as its strike lands (s+3), the fill coming in under it: never in a frame. + if (g.since) |since| if (since < @as(f32, @floatFromInt(a - g.addr)) * @import("syscalls.zig").Plan.byte + theme.time.strike.in / 2.0) continue; given_values[i] = g.bytes[a - g.addr]; given_known[i] = true; gives = true; @@ -643,6 +665,7 @@ fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Mom if (of.struck) |since| run.heat = now.writtenAt(here, run.len, since - @as(f32, @floatFromInt(field_k)) * cursors.write_apart); // What the kernel fills lands a byte every 2 f. if (@import("syscalls.zig").filledAt(now, here) != null) run.apart = 2; + givenStrike(now, of, here, &run); var framed: f32 = 0; if (fl.mark) |k| if (k < of.marks.len) { framed = of.marks[k]; @@ -669,6 +692,7 @@ fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Mom }; run.len = @intCast(end - at); readings[n] = .{ .at = at, .len = run.len, .reads = .bytes }; + givenStrike(now, of, here, &run); } runs[n] = run; n += 1; diff --git a/anim/src/loupe.zig b/anim/src/loupe.zig index d3ae878..ae709d5 100644 --- a/anim/src/loupe.zig +++ b/anim/src/loupe.zig @@ -94,7 +94,7 @@ pub const Loupe = struct { switch (l.from) { .register => |name| { const i = context.index(name) orelse return null; - const px = ties.find(.{ .register = i }) orelse return null; + const px = ties.find(u.f, .{ .register = i }) orelse return null; const back = u.at.xf.invert(); const a = back.apply(.{ px[0], px[1] }); const b = back.apply(.{ px[2], px[3] }); @@ -112,7 +112,7 @@ pub const Loupe = struct { right = parent.box.x + 120; }, .row => |row| { - const px = ties.find(.{ .dump_run = row.at }) orelse return null; + const px = ties.find(u.f, .{ .dump_run = row.at }) orelse return null; const back = u.at.xf.invert(); const a = back.apply(.{ px[0], px[1] }); const b = back.apply(.{ px[2], px[3] }); diff --git a/anim/src/nodes.zig b/anim/src/nodes.zig index 7c842d3..5218d7f 100644 --- a/anim/src/nodes.zig +++ b/anim/src/nodes.zig @@ -73,10 +73,22 @@ pub const Nodes = struct { if (lo < hi) { @import("map.zig").measure(.view, (hi - lo) / 16); const count = theme.layout.rowsIn(g.stage.box(placed, .view).h); - const first = hexdump.window(now, lo, hi, count); + // Its window round the rows the step touches: from the last step's, scrolled + // (whole rows, 24 f, as the step begins), never a jump. + const first_new = hexdump.window(now, lo, hi, count); + const first_old = hexdump.windowAt(rec, now.stops[2], now.stops[1], lo, hi, count); + const scroll = if (now.resting() or first_old == first_new) 1 else cursors.glide(std.math.clamp(now.time / time.move, 0, 1)); + const first = if (scroll >= 0.5) first_new else first_old; const shown = @min(count, (hi - first) / 16); const v = hexdump.panel(f, placed, g.stage, u.print("the heap, {x} to {x}", .{ first, first + 16 * shown - 1 }), &legend); - if (v.rows > 0) hexdump.draw(v.u, now, 0, m.tag + m.pad, first, @min(shown, v.rows), .{ .things = things }); + if (v.rows > 0) { + const top = @min(first_old, first_new); + const rows_apart: f32 = @floatFromInt((@max(first_old, first_new) - top) / 16); + const at = std.math.lerp(@as(f32, @floatFromInt((first_old - top) / 16)), @as(f32, @floatFromInt((first_new - top) / 16)), scroll); + v.u.clip(.{ .x = 0, .y = m.tag, .w = g.stage.box(placed, .view).w, .h = @as(f32, @floatFromInt(@min(shown, v.rows))) * m.row + 2 * m.pad }); + hexdump.draw(v.u, now, 0, m.tag + m.pad - at * m.row, top, @min(@min(shown, v.rows) + @as(usize, @intFromFloat(rows_apart)), (hi - top) / 16), .{ .things = things }); + v.u.unclip(); + } if (v.rows > 0) @import("map.zig").window(.view, .{ first, first + 16 * (@min(shown, v.rows) -| 1) }, .{ first, first + 16 * (@min(shown, v.rows) -| 1) }, 1); v.veil(now); u.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h }); @@ -335,6 +347,27 @@ pub const Nodes = struct { std.mem.swap(u24, &inks.items[k - 1], &inks.items[k]); } } + // Two names whose order the step turns (one reassigned to the same node): the names + // fade out in the old order and in the new one over the step's first 24 f, never swapped in a frame. + if (out.items.len == 2 and !now.resting() and now.stops[1] != now.stops[2]) both: { + // Both on the pin before the step too. + const then = g.namesAt(u, now.stops[1], addr); + for (out.items) |name| { + if (for (then) |o| { + if (std.mem.eql(u8, o, name)) break false; + } else true) break :both; + } + const a = g.arrivalAt(u, now.stops[1], now.stops[2], out.items[0], addr); + const b = g.arrivalAt(u, now.stops[1], now.stops[2], out.items[1], addr); + if (a > b) { + const p = std.math.clamp(now.time / (2 * time.quick), 0, 1); + text = @min(text, @abs(2 * p - 1)); + if (p < 0.5) { + std.mem.swap([]const u8, &out.items[0], &out.items[1]); + std.mem.swap(u24, &inks.items[0], &inks.items[1]); + } + } + } // Both sides taken: a variable the scene names (what the narration // follows) takes the right from a register, which is counted. if (out.items.len > 2 and context.index(out.items[1]) != null) for (out.items[2..], 2..) |name, j| { @@ -348,7 +381,13 @@ pub const Nodes = struct { }; if (out.items.len == 1) { const first_before = g.firstBefore(u, now, addr, was); - if (first_before != null and !std.mem.eql(u8, first_before.?, out.items[0])) { + // Kept on the right in the step before (the other had just left): it goes over + // to the left at the start of this one, fading out and in (24 f), never in a frame. + const older = g.namesAt(u, now.stops[2], addr); + const was_right = now.stops[2] != now.stops[1] and older.len >= 2 and !std.mem.eql(u8, older[0], out.items[0]); + const over = if (now.resting()) 1 else std.math.clamp(now.time / (2 * time.quick), 0, 1); + if (was_right and first_before == null) text = @min(text, @abs(2 * over - 1)); + if ((first_before != null and !std.mem.eql(u8, first_before.?, out.items[0])) or (was_right and first_before == null and over < 0.5)) { out.insert(u.f.scratch, 0, "") catch {}; inks.insert(u.f.scratch, 0, t.primary) catch {}; } @@ -360,14 +399,19 @@ pub const Nodes = struct { /// became it (a variable's, its last assignment), as of the stop shown /// (or the one before, for a name that is leaving). fn arrival(g: *const Nodes, u: ui.Ui, now: cursors.Now, name: []const u8, addr: u64) u64 { + return g.arrivalAt(u, now.stops[0], now.stops[1], name, addr); + } + + /// The same, as of stop `here` (the one before it `before`). + fn arrivalAt(g: *const Nodes, u: ui.Ui, here: u64, before: u64, name: []const u8, addr: u64) u64 { const rec = g.cursor.rec; if (context.index(name)) |_| { - var i = if (rec.registers[now.stops[0]].named(name) == addr) now.stops[0] else now.stops[1]; + var i = if (rec.registers[here].named(name) == addr) here else before; while (i > 0 and rec.registers[i - 1].named(name) == addr) i -= 1; return i; } - const v = rec.variable(u.f.scratch, now.moment(), name) orelse return now.stops[0]; - var i = if ((Recording.word(now.moment(), v.addr, 8) catch null) == addr) now.stops[0] else now.stops[1]; + const v = rec.variable(u.f.scratch, rec.moment(here), name) orelse return here; + var i = if ((Recording.word(rec.moment(here), v.addr, 8) catch null) == addr) here else before; // Its assignment: a store, of a new value or of the one its slot held. while (i > 0 and !Recording.touches(rec.written(i - 1, i), v.addr, 8) and !Recording.touches(rec.storedSame(i - 1, i), v.addr, 8)) i -= 1; return i; @@ -395,6 +439,24 @@ pub const Nodes = struct { return out.items; } + /// The names on `addr`'s pin at moment `at`. + fn namesAt(g: *const Nodes, u: ui.Ui, at: u64, addr: u64) []const []const u8 { + const rec = g.cursor.rec; + const mo = rec.moment(at); + var out: std.ArrayList([]const u8) = .empty; + for (g.track) |name| { + const v = rec.variable(u.f.scratch, mo, name) orelse continue; + if (!rec.assigned(v, mo.index)) continue; + if ((Recording.word(mo, v.addr, 8) catch null) != addr) continue; + out.append(u.f.scratch, name) catch break; + } + const regs = (mo.regs(rec.tid) catch null) orelse return out.items; + if (!g.quiet) for (theme.metric.all_registers[0..16]) |name| if (regs.named(name) == addr) { + out.append(u.f.scratch, name) catch break; + }; + return out.items; + } + /// The ink of where variable `name` lives: stack for a local, data for a global. fn home(g: *const Nodes, u: ui.Ui, now: cursors.Now, name: []const u8) u24 { const v = g.cursor.rec.variable(u.f.scratch, now.moment(), name) orelse return t.primary; diff --git a/anim/src/pair.zig b/anim/src/pair.zig new file mode 100644 index 0000000..0f9cfe0 --- /dev/null +++ b/anim/src/pair.zig @@ -0,0 +1,780 @@ +//! `pair` (design/README.md, "pair"): two runs of one program seen against +//! each other, a split of the working frame. The shared source across both +//! columns (the left run's mark on its number, the right run's ranged right), +//! each run's disassembly, registers and frame on its side of the seam (the +//! gutter, 944 to 976); a row one run has and the other has not (`only +//! here`) a `dim` bar on its seam-side edge, a value that differs from its +//! counterpart (`differs`) a `bright` one; one clock with both moments, one +//! caption. Each side is a `Stage` of its own (`split`), so a panel of the +//! context (`Disasm`, `Registers`, `Source`) draws there as anywhere, on its +//! run's cursor; what the pair adds is here: the frame as bytes eight a row +//! (`hexdump` `eight`: `Frame`), a stopped run's output (`Report`), the +//! marks across the seam (`Marks`), the clock (`Clock`). +const std = @import("std"); +const sk = @import("sketch"); +const anim = @import("anim"); +const zt = @import("ztrace"); +const ui = @import("ui.zig"); +const rows = @import("rows.zig"); +const cursors = @import("cursor.zig"); +const context = @import("context.zig"); +const ties = @import("ties.zig"); +const words = @import("words.zig"); +const Stage = @import("stage.zig").Stage; +const Recording = @import("Recording.zig"); +const theme = ui.theme; +const Rect = ui.Rect; +const Split = theme.layout.Split; +const t = theme.color.text; +const m = theme.metric.context; +const size = theme.size.mono.context; +const adv = size * 3 / 5; + +pub const Side = enum { left, right }; + +/// The pair's boxes on one side of the seam (`pair` in design/README.md): +/// the source shared, five lines across both columns (48 to 200, the left +/// side's; the right has none), then in each column the disassembly, nine +/// rows (212 to 460), the registers, four (472 to 600), the frame, ten (612 +/// to 884). `stopped`: the right run once it has stopped, its disassembly +/// down to the call that stopped it (five rows, to 364) and its output in +/// `view` (six rows, 376 to 552), in place of its registers. +pub fn split(side: Side, stopped: bool) Split { + const ph = theme.layout.panelH; + const gap = theme.canvas.row_gap; + const x: f32 = if (side == .left) theme.layout.program.x else theme.layout.state.x; + const w = theme.layout.program.w; + const top = theme.canvas.stage.y; + const none: Rect = .{ .x = x, .y = top, .w = w, .h = 0 }; + const source: Rect = .{ .x = theme.canvas.stage.x, .y = top, .w = theme.canvas.stage.w, .h = if (side == .left) ph(5) else 0 }; + const disasm: Rect = .{ .x = x, .y = top + ph(5) + gap, .w = w, .h = ph(if (stopped) 5 else 9) }; + const registers: Rect = .{ .x = x, .y = top + ph(5) + ph(9) + 2 * gap, .w = w, .h = ph(4) }; + const stack: Rect = .{ .x = x, .y = registers.y + registers.h + gap, .w = w, .h = ph(10) }; + const view: Rect = if (stopped) .{ .x = x, .y = disasm.y + disasm.h + gap, .w = w, .h = ph(6) } else none; + return .{ .band = none, .view = view, .source = source, .disasm = disasm, .registers = registers, .stack = stack, .backtrace = none, .dump_rows = 0 }; +} + +test "the pair's boxes are the catalogue's" { + const l = split(.left, false); + try std.testing.expectEqual(@as(f32, 200), l.source.y + l.source.h); + try std.testing.expectEqual(@as(f32, 460), l.disasm.y + l.disasm.h); + try std.testing.expectEqual(@as(f32, 600), l.registers.y + l.registers.h); + try std.testing.expectEqual(@as(f32, 884), l.stack.y + l.stack.h); + const r = split(.right, true); + try std.testing.expectEqual(@as(f32, 364), r.disasm.y + r.disasm.h); + try std.testing.expectEqual(@as(f32, 552), r.view.y + r.view.h); + try std.testing.expectEqual(@as(f32, 976), r.stack.x); +} + +/// A field of a frame, from the debug info and the call frame information +/// (its locals, the saved rbp, the return address; an ASan fake frame's +/// header, buffer and redzones), by address. +pub const Field = struct { + at: u64, + len: u64, + name: []const u8, + reads: enum { number, pointer, text, bytes } = .number, + /// The moment it is the frame's from (the call pushes the return address, + /// the prologue the saved rbp and the locals): before it its bytes are + /// loose, and in the step that makes it its paper and reading fade in. + from: u64 = 0, +}; + +/// How much a thing that is the frame's from moment `from` shows now: none +/// before, all after, and over the step that makes it, from memory's slot (12 f). +fn live(now: cursors.Now, from: u64) f32 { + if (now.stops[0] < from) return 0; + if (now.stops[1] >= from or now.resting()) return 1; + return std.math.clamp(now.since(.memory, 0) / theme.time.quick, 0, 1); +} + +/// `hexdump` `eight` (design/README.md): a stack frame as bytes, eight a +/// row, so that a row is one word of the stack: rsp's and rbp's `row` pins +/// at 12, the row's shadow byte at 62 (a run built with ASan), the address +/// at 118, the bytes from 274 on a pitch of 30 (a field's on raised paper, +/// what no field covers `loose`), the reading at 522 (the field's name in Go +/// 18 dim, then what its bytes make), the frame's `call` span at 866. A byte +/// the loop stores past `buf` keeps the string's ink in the field it lands +/// in, whose reading says what the CPU will make of it. A store's byte is +/// struck as in `bytes`; the field's reading crossfades from 6 f after it +/// (12 f). Fixed rows: the pair scrolls each frame so that `buf` faces `buf`. +pub const Frame = struct { + cursor: cursors.Cursor, + stage: *const Stage, + title: []const u8, + note: []const u8 = "", + /// Its rows, eight bytes each from `first`; then, under a rule, `more` + /// rows from `then` (ASan's real stack, far from its fake frame). + first: u64, + rows: usize, + then: u64 = 0, + more: usize = 0, + fields: []const Field, + /// Where the loop's letters begin: a byte from there on, up to the first + /// that is not one, is drawn in a string's ink whatever field holds it. + letters: u64 = 0, + letter: u8 = 'A', + /// Each row of the first stretch with its shadow byte beside it. + shadow: bool = false, + /// The frame's `call` span, from its first word to past its last, its + /// name, and the moment it is made. + span: [2]u64 = .{ 0, 0 }, + span_from: u64 = 0, + /// In a box that holds more rows than it has (the column's): from this + /// word, as many rows as the box holds, and no second stretch; its rows + /// then the places a text is level with and a touch goes to (`ties`). + tall: u64 = 0, + name: []const u8 = "", + side: Side, + /// How much the first stretch shows (an ASan fake frame comes in as it is made). + shows: anim.Value, + /// How much the `differs` bars on rows `differ` show. + differs: anim.Value, + differ: []const u64 = &.{}, + + pub fn draw(frame: *const Frame, f: *sk.Frame, placed: anim.Placed) void { + const box = frame.stage.box(placed, .stack); + if (box.h < m.tag + m.row) return; + const fit = theme.layout.rowsIn(box.h); + var tall = frame.*; + const is_tall = frame.tall != 0 and fit > frame.rows + frame.more; + if (is_tall) { + tall.first = frame.tall; + tall.rows = fit; + tall.more = 0; + } + const fr = &tall; + const u = context.at(f, placed, box); + _ = u.panel(box.w, box.h, fr.title, .{ .note = fr.note }); + context.clipBody(u, box); + defer u.unclip(); + const now = fr.cursor.now(placed); + const top = m.tag + m.pad; + const shows = placed.value(fr.shows); + const passes = crossfade(now); + const n = fr.rows + fr.more; + for (0..n) |k| { + const second = k >= fr.rows; + const addr = fr.rowAddr(k); + const y = top + @as(f32, @floatFromInt(k)) * m.row; + const v = if (second) u else u.faded(shows); + if (is_tall) { + ties.anchor(u, .{ .row = addr }, .{ .x = 0, .y = y, .w = box.w, .h = m.row }); + ties.anchor(u, .{ .word = addr }, .{ .x = eight.address, .y = y, .w = eight.first - eight.address, .h = m.row }); + ties.anchor(u, .{ .slot_pin = addr }, .{ .x = theme.metric.slot.lane, .y = y, .w = theme.metric.slot.lane_end - theme.metric.slot.lane, .h = m.row }); + // The word its eight bytes make, as shown: what an `aim` leaves from. + if (Recording.word(now.shownAt(addr, 8), addr, 8) catch null) |word| ties.show(u, word, .{ .x = eight.first, .y = y, .w = box.w - eight.first, .h = m.row }, .stack, addr, 0, box.w); + } + // A rule across between the two stretches: they are far apart. + if (second and k == fr.rows) u.fill(.{ .x = theme.stroke.rule, .y = y - 1, .w = box.w - 2 * theme.stroke.rule, .h = theme.stroke.rule }, theme.color.rule); + const lead = ui.shared(if (second) &.{ fr.then, fr.then + 8 * (fr.more -| 1) } else &.{ fr.first, fr.first + 8 * (fr.rows -| 1) }); + // A row whose fields the step makes the frame's: its loose bytes going, its fields coming. + var q: f32 = 1; + for (fr.fields) |fl| if (fl.at < addr + 8 and fl.at + fl.len > addr) { + q = @min(q, live(now, fl.from)); + }; + for (passes.moments[0..passes.n], passes.shows[0..passes.n]) |moment, amount| { + if (q > 0) fr.row(v.faded(amount * q), now, moment, addr, y, lead, !second and fr.shadow, k, true); + if (q < 1) fr.row(v.faded(amount * (1 - q)), now, moment, addr, y, lead, !second and fr.shadow, k, false); + } + } + fr.pins(u, now, top); + if (fr.rowOf(fr.span[0])) |a| if (fr.rowOf(fr.span[1] - 8)) |b| { + rows.span(u.faded(live(now, fr.span_from)), box.w, top + @as(f32, @floatFromInt(a)) * m.row, top + @as(f32, @floatFromInt(b + 1)) * m.row, fr.name, true, false); + }; + // `differs`: a bright bar on the seam-side edge of each row whose value its counterpart does not hold. + const bars = placed.value(fr.differs); + if (bars > 0) for (fr.differ) |addr| if (fr.rowOf(addr)) |k| { + const x: f32 = if (fr.side == .left) box.w - theme.stroke.rule - 4 else theme.stroke.rule; + u.faded(bars).fill(.{ .x = x, .y = top + @as(f32, @floatFromInt(k)) * m.row + 1, .w = 4, .h = m.row - 2 }, t.bright); + }; + context.veiled(u, box, now.veil); + } + + fn rowAddr(fr: *const Frame, k: usize) u64 { + return if (k >= fr.rows) fr.then + 8 * (k - fr.rows) else fr.first + 8 * k; + } + + fn rowOf(fr: *const Frame, addr: u64) ?usize { + for (0..fr.rows + fr.more) |k| if (addr >= fr.rowAddr(k) and addr < fr.rowAddr(k) + 8) return k; + return null; + } + + /// The field that holds byte `a`, if any. + fn fieldAt(fr: *const Frame, a: u64) ?Field { + for (fr.fields) |fl| if (a >= fl.at and a < fl.at + fl.len) return fl; + return null; + } + + /// How many of the loop's letters stand from `letters` at `moment`. + fn lettered(fr: *const Frame, moment: Recording.Moment) u64 { + if (fr.letters == 0) return 0; + var buf: [64]u8 = undefined; + var known: [64]bool = undefined; + moment.read(fr.letters, &buf, &known) catch return 0; + var n: u64 = 0; + while (n < buf.len and known[n] and buf[n] == fr.letter) n += 1; + return n; + } + + /// One row of eight bytes at `addr`, its values from `moment` (a write of + /// the step keeping its old bytes until its strike lands). + fn row(fr: *const Frame, u: ui.Ui, now: cursors.Now, moment: Recording.Moment, addr: u64, y: f32, lead: usize, shadowed: bool, k: usize, fielded: bool) void { + const b = eight; + const base = rows.baseline(y); + var values: [8]u8 = undefined; + var known: [8]bool = undefined; + moment.read(addr, &values, &known) catch return; + var was: [8]u8 = undefined; + var known_was: [8]bool = undefined; + now.before().read(addr, &was, &known_was) catch { + was = values; + known_was = known; + }; + const letters_is = fr.lettered(moment); + const letters_was = fr.lettered(now.before()); + if (shadowed) fr.shadowCell(u, now, moment, addr, y, k); + const region = rows.regionOf(moment, addr); + _ = u.address(b.address, base, addr, .{ .ink = ui.rgb(rows.ink(region)), .lead = lead }); + // The runs: each field's part of the row, and the loose bytes between. + var j: u8 = 0; + var reading_x: f32 = b.reading; + while (j < 8) { + const here = addr + j; + const field = if (fielded) fr.fieldAt(here) else null; + var len: u8 = 1; + if (field) |fl| { + len = @intCast(@min(fl.at + fl.len - here, 8 - @as(u64, j))); + } else while (j + len < 8 and (!fielded or fr.fieldAt(here + len) == null)) len += 1; + const lo = j; + const hi = j + len; + // Until its strike lands (s+3) the run keeps its old bytes. + var shown = values; + var shown_known = known; + var letters_shown = letters_is; + if (now.struckAgo(here, len)) |ago| if (ago < theme.time.strike.in / 2.0) { + @memcpy(shown[lo..hi], was[lo..hi]); + @memcpy(shown_known[lo..hi], known_was[lo..hi]); + letters_shown = letters_was; + }; + const heat = now.written(here, len, .memory, 0); + const changed = now.changedBytes(here, len); + var read: f32 = 0; + if (now.wasRead(here, len) and now.writeAge(here, len) != 0) read = now.reading(.memory, 0); + // Until this step's strike lands, the held corner stays on what the last write changed. + const held = if (now.struckAgo(here, len)) |ago| (if (ago < theme.time.strike.in / 2.0) changedBefore(now, here, len) else changed) else changed; + fr.bytes(u, y, addr, lo, hi, &shown, &shown_known, field, heat, changed, held, read, letters_shown, moment); + if (field) |fl| reading_x = fr.reading(u, now, y, reading_x, addr, lo, hi, fl, &values, &known, &was, &known_was, moment); + j = hi; + } + } + + /// The bytes `lo .. hi` of the row at `addr`: paper for a field, the + /// strike's marks on the bytes the step changed, the digits in their ink. + fn bytes(fr: *const Frame, u: ui.Ui, y: f32, addr: u64, lo: u8, hi: u8, values: *const [8]u8, known: *const [8]bool, field: ?Field, h: ui.Heat, changed: ?[2]u8, held: ?[2]u8, read: f32, letters: u64, moment: Recording.Moment) void { + const base = rows.baseline(y); + const x0 = byteX(lo) - 4; + const x1 = byteX(hi - 1) + 28; + const r: Rect = .{ .x = x0, .y = y + 1, .w = x1 - x0, .h = m.row - 2 }; + if (field != null) u.fill(r, theme.color.raised); + const c_lo: u8 = lo + if (changed) |c| c[0] else 0; + const c_hi: u8 = lo + if (changed) |c| c[1] else hi - lo - 1; + const s0 = byteX(c_lo) - 4; + const s1 = byteX(c_hi) + 28; + if (h.arm > 0) u.faded(h.arm).frame(.{ .x = s0 - 3, .y = y - 1, .w = s1 - s0 + 6, .h = m.row + 2 }, theme.mark(.write).solid, 2); + var solid_of: [8]f32 = @splat(0); + if (h.level >= 1 and h.solid > 0) for (c_lo..@as(usize, c_hi) + 1) |i| { + const solid = h.solid * std.math.clamp(h.in - @as(f32, @floatFromInt(i - c_lo)) / (60.0 * theme.time.strike.in), 0, 1); + solid_of[i] = solid; + if (solid <= 0) continue; + const bx0 = byteX(@intCast(i)) - 4 - if (i == c_lo) h.out else 0; + const bx1 = (if (i < c_hi) byteX(@intCast(i + 1)) - 4 else byteX(@intCast(i)) + 28) + if (i == c_hi) h.out else 0; + u.fill(.{ .x = bx0, .y = r.y - h.out, .w = bx1 - bx0, .h = r.h + 2 * h.out }, ui.mix(theme.color.raised, theme.color.struck, solid)); + }; + u.corner(byteX(lo + if (held) |c| c[0] else 0) - 4, y + 2, m.corner, h.level * h.corner); + if (read > 0) u.faded(read).frame(.{ .x = x0 - 3, .y = y - 1, .w = x1 - x0 + 6, .h = m.row + 2 }, theme.mark(.read).solid, 2); + // A pointer's bytes in the ink of where the whole word points; a number's primary; padding faint. + var word: u64 = 0; + if (field) |fl| if (fl.len == 8 and fl.reads == .pointer and fl.at >= addr and fl.at + 8 <= addr + 8) { + for (0..8) |i| word |= @as(u64, values[i]) << @intCast(8 * i); + }; + const field_ink: u24 = if (field) |fl| switch (fl.reads) { + .pointer => rows.ink(rows.regionOf(moment, word)), + .text => theme.color.string, + .number => t.primary, + .bytes => t.primary, + } else t.dim; + var top: usize = lo; + for (lo..hi) |i| if (values[i] != 0) { + top = i; + }; + for (lo..hi) |i| { + const x = byteX(@intCast(i)); + if (!known[i]) { + u.fill(.{ .x = x + 5, .y = y + 11, .w = 4, .h = 4 }, t.faint); + u.fill(.{ .x = x + 15, .y = y + 11, .w = 4, .h = 4 }, t.faint); + continue; + } + const a = addr + i; + const letter = fr.letters != 0 and a >= fr.letters and a < fr.letters + letters; + const ink: u24 = if (letter) theme.color.string else field_ink; + const padding = field != null and field.?.reads != .text and !letter and i > top; + // Bright with the strike's paper, back to its ink as the paper cools (the face at the middle, where the colour moves fastest). + const c = ui.mix(if (padding) ui.mix(ink, theme.color.page, m.pad_alpha) else ui.rgb(ink), t.bright, solid_of[i]); + u.text(if (solid_of[i] >= 0.5) .mono_bold else .mono, size, x, base, u.print("{x:0>2}", .{values[i]}), c); + } + } + + /// The reading of field `fl`'s part `lo .. hi` of the row at `addr`, from + /// `x`: its name, then what its bytes make; as the step's strike lands it + /// crossfades from what they made (6 f after, over 12 f). Returns where it ends. + fn reading(fr: *const Frame, u: ui.Ui, now: cursors.Now, y: f32, x: f32, addr: u64, lo: u8, hi: u8, fl: Field, values: *const [8]u8, known: *const [8]bool, was: *const [8]u8, known_was: *const [8]bool, moment: Recording.Moment) f32 { + _ = fr; + const whole = fl.at >= addr and fl.at + fl.len <= addr + 8; + const name = if (whole) fl.name else u.print("{s}[{d}..{d}]", .{ fl.name, addr + lo - fl.at, addr + hi - 1 - fl.at }); + const micro = theme.size.sans.micro; + u.text(.sans, micro, x, rows.baseline(y) - 1, name, t.dim); + const vx = x + u.width(.sans, micro, name) + adv; + if (fl.reads == .bytes) return vx; + var q: f32 = 1; + if (now.struckAgo(addr + lo, hi - lo)) |ago| q = std.math.clamp((ago - 6.0 / 60.0) / theme.time.quick, 0, 1); + const same = std.mem.eql(u8, values[lo..hi], was[lo..hi]) and std.mem.eql(bool, known[lo..hi], known_was[lo..hi]); + if (same or q >= 1) return value(u.faded(1), vx, y, values[lo..hi], known[lo..hi], fl, moment); + const end_was = value(u.faded(1 - q), vx, y, was[lo..hi], known_was[lo..hi], fl, moment); + const end_is = value(u.faded(q), vx, y, values[lo..hi], known[lo..hi], fl, moment); + return @max(end_was, end_is); + } + + /// What `bytes` (a field's, or its part on a row) make, read as `fl` says, at `x`. + fn value(u: ui.Ui, x: f32, y: f32, bytes_: []const u8, known: []const bool, fl: Field, moment: Recording.Moment) f32 { + if (std.mem.indexOfScalar(bool, known, false) != null) return x; + const base = rows.baseline(y); + var word: u64 = 0; + for (bytes_[0..@min(bytes_.len, 8)], 0..) |c, i| word |= @as(u64, c) << @intCast(8 * i); + switch (fl.reads) { + .bytes => return x, + .text => { + const text = quoted(u, bytes_); + u.text(.mono, size, x, base, text, theme.color.string); + return x + u.width(.mono, size, text); + }, + .number => { + const bits: u6 = @intCast(64 - 8 * @as(u64, @min(bytes_.len, 8))); + const signed = @as(i64, @bitCast(word << bits)) >> bits; + const text = u.print("{d}", .{signed}); + u.text(.mono, size, x, base, text, t.primary); + return x + u.width(.mono, size, text); + }, + .pointer => { + // An address in its region's ink, and its name; once it is none, the number it is. + const region = rows.regionOf(moment, word); + if (region == .none) return u.value(x, base, word, false, .{}); + // Its first hop, as a chain has it: a code address's name, a string's text. + const at = u.arrow(x, y + m.row / 2, .points); + return rows.chain(u, at, y, moment, word, eight.room, .{ .most = 1 }).end; + }, + } + } + + /// The row's shadow byte, at 62: its value (`f1` a frame's left + /// redzone, `f3` its right, `00` addressable) primary where it poisons, + /// faint where not; struck where the step wrote it, 2 f a row apart; framed + /// as read while a check loads it. + fn shadowCell(fr: *const Frame, u: ui.Ui, now: cursors.Now, moment: Recording.Moment, addr: u64, y: f32, k: usize) void { + _ = fr; + const sh = (zt.telescope.shadow(moment, addr) catch return) orelse return; + const delay = @as(f32, @floatFromInt(k)) * 2.0 / 60.0; + var shown = sh.value orelse return; + if (now.struckAgo(sh.addr, 1)) |ago| if (ago - delay < theme.time.strike.in / 2.0) { + if (zt.telescope.shadow(now.before(), addr) catch null) |old| shown = old.value orelse shown; + }; + const cell: Rect = .{ .x = eight.shadow, .y = y, .w = 2 * adv, .h = m.row }; + const h = now.written(sh.addr, 1, .memory, delay); + const ink = u.written(cell, h); + if (now.wasRead(sh.addr, 1)) u.read(cell, now.reading(.memory, 0)); + const face: ui.Face = if (ink) |i| i.face else if (shown != 0) .mono_bold else .mono; + const c = if (ink) |i| i.ink else ui.rgb(if (shown != 0) t.primary else t.faint); + u.text(face, size, eight.shadow, rows.baseline(y), u.print("{x:0>2}", .{shown}), c); + } + + /// rsp's and rbp's `row` pins, as the stack's: each at the row it points + /// into, travelling there with the step (as the stack's pins do, on + /// `glide`); one that leaves the rows fades. + fn pins(fr: *const Frame, u: ui.Ui, now: cursors.Now, top: f32) void { + const rec = fr.cursor.rec; + const names = [2][]const u8{ "rsp", "rbp" }; + var ys: [2]?f32 = .{ null, null }; + var shows: [2]f32 = .{ 0, 0 }; + for (names, 0..) |name, k| { + const is = rec.register(now.stops[0], name); + const was = rec.register(now.stops[1], name); + const p = now.pinGone(k) orelse (if (now.resting()) 1 else std.math.clamp(now.since(.cpu, 0) / theme.time.tied.stack_pin, 0, 1)); + const a = fr.rowOf(was); + const b = fr.rowOf(is); + const at = struct { + fn y(t0: f32, row_: usize) f32 { + return t0 + @as(f32, @floatFromInt(row_)) * m.row; + } + }.y; + if (a != null and b != null) { + ys[k] = std.math.lerp(at(top, a.?), at(top, b.?), cursors.glide(p)); + shows[k] = 1; + } else if (b) |row_| { + ys[k] = at(top, row_); + shows[k] = p; + } else if (a) |row_| { + ys[k] = at(top, row_); + shows[k] = 1 - p; + } + } + const ink = theme.color.region.stack; + // rbp and rsp on one row: one pin, in pwndbg's order; a name joining or leaving it crossfades. + if (ys[0] != null and ys[1] != null and @abs(ys[0].? - ys[1].?) < 1) { + const both = @min(shows[0], shows[1]); + u.faded(both).pinRow(eight.pin, ys[0].?, &.{ "rbp", "rsp" }, ink); + const alone: usize = if (shows[0] > shows[1]) 0 else 1; + u.faded(@max(shows[0], shows[1]) - both).pinRow(eight.pin, ys[0].?, &.{names[alone]}, ink); + return; + } + for ([2]usize{ 1, 0 }) |k| if (ys[k]) |py| u.faded(shows[k]).pinRow(eight.pin, py, &.{names[k]}, ink); + } +}; + +/// Of `addr .. addr + len` (8 at most), the first and last byte (from +/// `addr`) the newest write before this step changed: where its held corner is. +fn changedBefore(now: cursors.Now, addr: u64, len: u64) ?[2]u8 { + const rec = now.rec; + for (1..now.stops.len - 1) |age| { + if (!Recording.touches(rec.written(now.stops[age + 1], now.stops[age]), addr, len)) continue; + var was: [8]u8 = undefined; + var is: [8]u8 = undefined; + var kw: [8]bool = undefined; + var ki: [8]bool = undefined; + const l: usize = @intCast(@min(len, 8)); + rec.moment(now.stops[age + 1]).read(addr, was[0..l], kw[0..l]) catch return null; + rec.moment(now.stops[age]).read(addr, is[0..l], ki[0..l]) catch return null; + var out: ?[2]u8 = null; + for (0..l) |i| if (kw[i] and ki[i] and was[i] != is[i]) { + const first: u8 = if (out) |o| o[0] else @intCast(i); + out = .{ first, @intCast(i) }; + }; + return out; + } + return null; +} + +/// `eight`'s columns (design/README.md, `hexdump`). +const eight = struct { + const pin = 12; + const shadow = 62; + const address = 118; + const first = 274; + const pitch = 30; + const reading = 522; + /// Where a reading ends: before the span's name (`fill`) and its bracket. + const room = 820; +}; + +fn byteX(i: u8) f32 { + return eight.first + @as(f32, @floatFromInt(i)) * eight.pitch; +} + +/// The memory a frame draws now, and how much each shows: at a run, what +/// the step changed crossfades over 8 f, unmarked (`hexdump`'s own). +const Passes = struct { moments: [2]Recording.Moment, shows: [2]f32, n: usize }; +fn crossfade(now: cursors.Now) Passes { + if (now.plan.gait == .run and !now.resting()) { + const q = std.math.clamp(now.since(.memory, 0) / theme.time.run_ease, 0, 1); + if (q > 0 and q < 1) return .{ .moments = .{ now.before(), now.moment() }, .shows = .{ 1 - q, q }, .n = 2 }; + } + return .{ .moments = .{ now.shown(.memory), undefined }, .shows = .{ 1, 0 }, .n = 1 }; +} + +/// Bytes as C text, in quotes, to the first NUL. +fn quoted(u: ui.Ui, bytes_: []const u8) []const u8 { + var out: std.ArrayList(u8) = .empty; + const a = u.f.scratch; + out.append(a, '"') catch return ""; + for (bytes_) |c| { + if (c == 0) break; + if (std.ascii.isPrint(c) and c != '"' and c != '\\') out.append(a, c) catch {} else out.print(a, "\\x{x:0>2}", .{c}) catch {}; + } + out.append(a, '"') catch {}; + return out.items; +} + +/// The address of ASan's shadow for `addr`'s granule: `(addr >> 3) + offset`, as the recording says it. +pub fn shadowOffset(rec: *const Recording) ?u64 { + const sh = (zt.telescope.shadow(rec.moment(0), 0) catch return null) orelse return null; + return sh.addr; +} + +/// A run's output as it wrote it (`write(2, …)`, from a recording that has +/// its system calls), in place of its registers and its disassembly's lower +/// rows once it has stopped (the stage's `view`): the lines the scene +/// names (`lines`: each the first that holds the text), as written, cut at +/// the panel's edge with `…`; the words the narration names framed bright, +/// one after another as `framed` (0 to their count) goes. +pub const Report = struct { + /// The recording of the run by system call. + rec: *const Recording, + stage: *const Stage, + title: []const u8, + lines: []const []const u8, + words: []const []const u8 = &.{}, + framed: anim.Value, + + /// What it wrote to fd 2, all of it. + pub fn stderr(rec: *const Recording, a: std.mem.Allocator) []const u8 { + var out: std.ArrayList(u8) = .empty; + for (rec.syscalls) |c| if (c.nr == 1 and c.args[0] == 2) out.appendSlice(a, c.text) catch {}; + return out.items; + } + + /// How it ended: the status it gave `exit_group`. + pub fn status(rec: *const Recording) ?u64 { + for (rec.syscalls) |c| if (c.nr == 231) return c.args[0]; + return null; + } + + pub fn draw(r: *const Report, f: *sk.Frame, placed: anim.Placed) void { + const box = r.stage.box(placed, .view); + if (box.h < m.tag + m.row) return; + const u = context.at(f, placed, box); + const exit = if (status(r.rec)) |st| u.print("its report, as written \u{00b7} exit {d}", .{st}) else "its report, as written"; + _ = u.panel(box.w, box.h, r.title, .{ .note = exit }); + context.clipBody(u, box); + defer u.unclip(); + const all = stderr(r.rec, u.f.scratch); + const room = box.w - 2 * m.side; + const framed = placed.value(r.framed); + var k: usize = 0; + var word: usize = 0; + for (r.lines) |want| { + var it = std.mem.splitScalar(u8, all, '\n'); + const line = while (it.next()) |l| { + if (std.mem.indexOf(u8, l, want) != null) break l; + } else continue; + const y = m.tag + m.pad + @as(f32, @floatFromInt(k)) * m.row; + const shown = rows.fitted(u, line, room); + u.text(.mono, size, m.side, rows.baseline(y), shown, t.primary); + // The words the narration names, framed bright where the line shows them whole. + for (r.words) |w| if (std.mem.indexOf(u8, line, w)) |at| { + defer word += 1; + const chars = std.unicode.utf8CountCodepoints(line[0..at]) catch at; + if (@as(f32, @floatFromInt(chars + w.len)) * adv > room - adv) continue; + const shows = std.math.clamp(framed - @as(f32, @floatFromInt(word)), 0, 1); + if (shows > 0) u.faded(shows).frame(.{ .x = m.side + @as(f32, @floatFromInt(chars)) * adv - 3, .y = y, .w = @as(f32, @floatFromInt(w.len)) * adv + 6, .h = m.row }, t.bright, theme.stroke.rule); + }; + k += 1; + } + } +}; + +/// The pair's marks across the seam, over its panels: the right run's mark +/// on the shared source (ranged right, a gold plaque pointing left, `asan` +/// before it), the `only here` bars on the right run's rows that no row of +/// the left faces, the two first rows that differ framed `attention` where +/// they part, and a fault on the left run's row (`fault`: hazard bands round +/// an ink plaque naming the signal; rip stays). +pub const Marks = struct { + left: cursors.Cursor, + right: cursors.Cursor, + stages: [2]*const Stage, + /// The right run's name, by its mark on the source. + name: []const u8, + /// The right run's rows no row of the left faces. + only: []const u64, + /// The rows framed `attention` (the left's, the right's), and how much. + attend: [2]u64 = .{ 0, 0 }, + attention: anim.Value, + /// How much the right run's marks show (it folds to the shelf while the left goes on alone). + right_shows: anim.Value, + /// The left run's fault: the moment its signal stops it, the row, its name. + fault: ?struct { at: u64, pc: u64, name: []const u8 } = null, + + pub fn draw(mk: *const Marks, f: *sk.Frame, placed: anim.Placed) void { + mk.sourceMark(f, placed); + // `only here`: a dim bar on the seam-side (left) edge of the right run's rows. + const dbox = mk.stages[1].box(placed, .disasm); + const du = context.at(f, placed, dbox); + const rnow = mk.right.now(placed); + const right = placed.value(mk.right_shows); + context.clipBody(du, dbox); + const k = placed.scale; + const origin = du.at.xf.apply(.{ 0, 0 }); + for (mk.only) |addr| if (ties.find(f, .{ .row = addr })) |r| { + const y = (r[1] - origin[1]) / k; + du.faded(right * (1 - 0.5 * rnow.veil)).fill(.{ .x = theme.stroke.rule, .y = y + 1, .w = 4, .h = m.row - 2 }, t.dim); + }; + du.unclip(); + const shows = placed.value(mk.attention); + if (shows > 0) for (mk.attend, 0..) |addr, side| if (ties.find(f, .{ .row = addr })) |r| { + const b = mk.stages[side].box(placed, .disasm); + const v = context.at(f, placed, b); + const o = v.at.xf.apply(.{ 0, 0 }); + v.faded(shows).frame(.{ .x = 0, .y = (r[1] - o[1]) / k - 1, .w = b.w, .h = m.row + 2 }, theme.mark(.attention).solid, theme.stroke.rule); + }; + if (mk.fault) |fault| mk.faultMark(f, placed, fault.at, fault.pc, fault.name); + } + + /// The right run's mark on the shared source: its line's row, gliding with its advance. + fn sourceMark(mk: *const Marks, f: *sk.Frame, placed: anim.Placed) void { + const box = mk.stages[0].box(placed, .source); + if (box.h < m.tag + m.row) return; + const u = context.at(f, placed, box); + const lnow = mk.left.now(placed); + const rnow = mk.right.now(placed); + const lrec = mk.left.rec; + const rrec = mk.right.rec; + const fit: u32 = @intCast(theme.layout.rowsIn(box.h)); + // The window, as the left's source has it (`Source`). + const going = lnow.advancing(); + const windows = [2]f32{ @floatFromInt(lrec.shownFirst(fit, lrec.shown[lnow.marks[0]])), @floatFromInt(lrec.shownFirst(fit, lrec.shown[lnow.marks[1]])) }; + const first = std.math.lerp(windows[0], windows[1], cursors.glide(going)); + const at = [2]Recording.Shown{ rrec.shown[rnow.marks[0]], rrec.shown[rnow.marks[1]] }; + if (at[1].line == 0) return; + const p = rnow.advancing(); + var line: f32 = @floatFromInt(at[1].line); + var shows: f32 = 1; + if (at[0].line != at[1].line and p < 1 and at[0].line != 0) { + if (cursors.jumps(rrec, rnow.marks[0], rnow.marks[1])) { + // Into a call or out of one: out there, then in here. + line = @floatFromInt(if (p < 0.5) at[0].line else at[1].line); + shows = @abs(2 * p - 1); + } else line = std.math.lerp(@as(f32, @floatFromInt(at[0].line)), line, cursors.standard(p)); + } + const y = m.tag + m.pad + (line - first) * m.row; + if (y < m.tag or y + m.row > box.h) return; + const gold = theme.mark(.current); + const w = theme.metric.plaque.pc; + const x = box.w - theme.stroke.rule - w; + const v = u.faded(shows * placed.value(mk.right_shows)); + v.fill(.{ .x = x, .y = y, .w = w, .h = m.row }, gold.solid); + sk.draw.polygon(u.f, &.{ .{ x + 7, y + m.row / 2 }, .{ x + 17, y + 6 }, .{ x + 17, y + m.row - 6 } }, v.at.pen(.{ .fill = ui.rgb(gold.on_solid) })); + const micro = theme.size.sans.micro; + v.text(.sans_medium, micro, x - 10 - u.width(.sans_medium, micro, mk.name), rows.baseline(y) - 1, mk.name, t.dim); + } + + /// `fault` on the row at `pc` in the left run's disassembly, from the + /// step that stops it at `at`: hazard bands round an ink plaque naming + /// the signal, in 12 f once the step has fired, at the row's right end. + fn faultMark(mk: *const Marks, f: *sk.Frame, placed: anim.Placed, at: u64, pc: u64, name: []const u8) void { + const now = mk.left.now(placed); + if (now.stops[0] < at) return; + const r = ties.find(f, .{ .row = pc }) orelse return; + var shows: f32 = 1; + if (now.stops[0] == at and !now.resting()) shows = std.math.clamp((now.time - theme.time.fire - theme.time.snap) / theme.time.quick, 0, 1); + if (shows <= 0) return; + const box = mk.stages[0].box(placed, .disasm); + const u = context.at(f, placed, box); + const o = u.at.xf.apply(.{ 0, 0 }); + const y = (r[1] - o[1]) / placed.scale; + const band = theme.stroke.hazard; + const tw = u.width(.mono_bold, size, name); + const w = tw + 2 * 10 + 2 * band; + // Where its operands would be: `ret` has none, and its annotation goes on after it. + const x: f32 = theme.metric.insn.operands - band; + const v = u.faded(shows); + const outer: Rect = .{ .x = x, .y = y - 2, .w = w, .h = m.row + 4 }; + // The bands: vermilion and ink at 45 degrees, 12 each, clipped to the plaque's frame. + v.fill(outer, t.ink); + v.clip(outer); + const fault = theme.mark(.fault).solid; + var bx: f32 = x - outer.h; + while (bx < x + w) : (bx += 2 * band) { + sk.draw.polygon(u.f, &.{ .{ bx, outer.y + outer.h }, .{ bx + band, outer.y + outer.h }, .{ bx + band + outer.h, outer.y }, .{ bx + outer.h, outer.y } }, v.at.pen(.{ .fill = ui.rgb(fault) })); + } + v.unclip(); + const inner: Rect = .{ .x = x + band, .y = y + 1, .w = w - 2 * band, .h = m.row - 2 }; + v.fill(inner, t.ink); + v.text(.mono_bold, size, inner.x + 10, rows.baseline(y), name, t.bright); + } +}; + +/// A run that has stopped, folded to one tag on the column's shelf (y 48, +/// ranged right in the panes' column) while the other goes on alone: its +/// panes' frame crossfading in at their boxes as they go (`fold` 0 to 1), +/// then travelling to the tag (1 to 2, `glide`), the note coming as it lands; +/// unfolding, the same backwards. +pub const Shelf = struct { + stage: *const Stage, + title: []const u8, + note: []const u8, + fold: anim.Value, + + pub fn draw(sh: *const Shelf, f: *sk.Frame, placed: anim.Placed) void { + const fold = placed.value(sh.fold); + if (fold <= 0) return; + const disasm = sh.stage.box(placed, .disasm); + const frame = sh.stage.box(placed, .stack); + const from: Rect = .{ .x = disasm.x, .y = disasm.y, .w = disasm.w, .h = frame.y + frame.h - disasm.y }; + const u = context.at(f, placed, .{ .x = 0, .y = 0, .w = theme.canvas.width, .h = theme.canvas.height }); + const sans = theme.size.sans; + const w = 28 + u.width(.sans_medium, sans.tag, sh.title) + 12 + u.width(.sans, sans.micro, sh.note) + 12; + const slot: Rect = .{ .x = theme.layout.program.x + theme.layout.program.w - w, .y = theme.canvas.stage.y, .w = w, .h = m.tag }; + const on = theme.ease(.glide, std.math.clamp(fold - 1, 0, 1)); + const r: Rect = .{ .x = std.math.lerp(from.x, slot.x, on), .y = std.math.lerp(from.y, slot.y, on), .w = std.math.lerp(from.w, slot.w, on), .h = std.math.lerp(from.h, slot.h, on) }; + const v = u.moved(r.x, r.y).faded(std.math.clamp(fold, 0, 1)); + _ = v.panel(r.w, r.h, sh.title, .{}); + const shows = std.math.clamp((on - 0.75) * 4, 0, 1); + if (shows > 0) v.faded(shows).text(.sans, sans.micro, 28 + v.width(.sans_medium, sans.tag, sh.title) + 12, m.tag - 6, sh.note, t.faint); + } +}; + +/// The pair's clock: one rule, gold along it as far as the left run has +/// come (its steps are the pair's), still while a step plays, the step's +/// piece fading in once its mark has landed (`words.Clock`); under its right +/// end the turn and each run's moment. +pub const Clock = struct { + left: cursors.Cursor, + right: cursors.Cursor, + names: [2][]const u8, + /// The loop's counter, where the left run keeps it (0: none), from the moment it counts. + counter: u64 = 0, + counting: u64 = 0, + /// The left run's stage: in the column (`Stage.column`) the clock is under the text column alone. + stage: ?*const Stage = null, + box: Rect = .{ .x = theme.canvas.stage.x, .y = theme.canvas.clock_y - 12, .w = theme.canvas.stage.w, .h = 24 }, + + pub fn draw(k: *const Clock, f: *sk.Frame, at: anim.Placed) void { + const u = words.fixed(f, at, 0); + const c = theme.canvas; + const now = k.left.now(at); + const rnow = k.right.now(at); + var stage = c.stage; + const in_column = if (k.stage) |st| at.value(st.column) else 0; + stage.x = std.math.lerp(c.stage.x, theme.layout.state.x, theme.ease(.glide, in_column)); + stage.w = c.stage.x + c.stage.w - stage.x; + u.fill(.{ .x = stage.x, .y = c.clock_y - 1, .w = stage.w, .h = theme.stroke.rule }, theme.color.rule); + const gold = theme.mark(.current).solid; + const place = struct { + fn of(n: cursors.Now, moment: u64) f32 { + return std.math.clamp(@as(f32, @floatFromInt(moment -| n.way[0])) / @as(f32, @floatFromInt(@max(n.way[1] -| n.way[0], 1))), 0, 1); + } + }.of; + if (now.plan.gait == .sprint or now.resting()) { + u.fill(.{ .x = stage.x, .y = c.clock_y - 2, .w = stage.w * now.place, .h = 4 }, gold); + } else { + const landed = now.plan.advance + now.plan.travel; + const fade = std.math.clamp((now.time - landed) / @as(f32, if (now.plan.gait == .run) theme.time.run_ease else theme.time.quick), 0, 1); + const from = place(now, now.stops[1]); + const to = place(now, now.stops[0]); + u.fill(.{ .x = stage.x, .y = c.clock_y - 2, .w = stage.w * from, .h = 4 }, gold); + if (fade > 0) u.fill(.{ .x = stage.x + stage.w * from, .y = c.clock_y - 2, .w = stage.w * (to - from), .h = 4 }, ui.mix(theme.color.page, gold, fade)); + } + // Each count changes as its run's step lands, never in the middle of a step. + const counts = [2]u64{ counted(now), counted(rnow) }; + var text = u.print("{s} moment {d} \u{00b7} {s} moment {d}", .{ k.names[0], counts[0] -| now.way[0], k.names[1], counts[1] -| rnow.way[0] }); + if (k.counter != 0 and counts[0] >= k.counting) if (Recording.word(k.left.rec.moment(counts[0]), k.counter, 4) catch null) |i| { + const turn: i32 = @bitCast(@as(u32, @truncate(i))); + if (turn >= 0 and turn <= 64) text = u.print("i {d} \u{00b7} {s}", .{ turn, text }); + }; + u.text(.mono, size, stage.x + stage.w - u.width(.mono, size, text), c.clock_y + 28, text, t.faint); + } + + fn counted(n: cursors.Now) u64 { + if (n.resting() or n.plan.gait == .sprint) return n.marked(); + return if (n.time >= n.plan.advance + n.plan.travel) n.stops[0] else n.stops[1]; + } +}; diff --git a/anim/src/regroup.zig b/anim/src/regroup.zig new file mode 100644 index 0000000..431abd8 --- /dev/null +++ b/anim/src/regroup.zig @@ -0,0 +1,348 @@ +//! The introduction's dump (design/motion.md, "A scene: the introduction", +//! chapter I; "A word read"; mocks 29, 30, 37): the 128 bytes at the top of +//! the stack, where the stack panel will stand (the state column), and the +//! verbs that make words of them, each a clock the scene plays (frames, +//! linear): +//! +//! - `in`: the rows arrive, sixteen bytes a row, 18 f each, 12 f apart; no +//! ink, the addresses faint. +//! - `regroup` (design/README.md, 6. Verbs): sixteen a row to eight, the +//! return address walked (its offsets, `low byte`, `high byte`, its bytes +//! flying low byte first on nested arcs to its word's field, the gaps +//! closing, `zeros in front`), the other fifteen at a run. +//! - `framed`: three words that hold addresses framed as read, 30 f apart. +//! - `fold`: the rows out (6 f), the dump's tag to the shelf (18 f, `glide`); back. +//! - `ink` (6. Verbs): a word's digits to its region's ink, the walked one +//! framed with its bytes (its band in `segments` is the scene's echo); +//! the rest at a run, 2 f apart. +//! - `left`: the words to their field's left (24 f, `glide`); `chains`: +//! three chains grow (`extend`), 36 f apart. +//! +//! Every byte and word from the recording at its moment. +const std = @import("std"); +const sk = @import("sketch"); +const anim = @import("anim"); +const zt = @import("ztrace"); +const ui = @import("ui.zig"); +const rows = @import("rows.zig"); +const Recording = @import("Recording.zig"); +const theme = ui.theme; +const Rect = ui.Rect; +const t = theme.color.text; + +pub const words = 16; +const size = theme.size.mono.context; +const adv = ui.advance(size); +const pitch = theme.metric.context.row; + +/// Where it stands: the state column, its rows from `top`; the bytes 12 +/// after the address, 30 apart, 12 more after the eighth; the word's field +/// eight pairs wide, 48 after the bytes, its words ranged right. +pub const at = struct { + pub const column = theme.layout.state.x; + pub const address = column + 24; + pub const bytes = address + 12 * adv + 12; + pub const pair = theme.metric.bytes.pitch; + pub const middle = theme.metric.bytes.middle; + pub const field = bytes + 7 * pair + 24 + 48; + pub const field_end = field + 7 * pair + 24; + pub const top = 96; + /// The tie's bracket, left of the addresses. + pub const bracket = address - 12; +}; + +/// Bytes drawn 50% toward the page: what stays of where a word's bytes were. +fn dimmed() ui.Color { + return ui.mix(theme.color.page, t.primary, 0.5); +} + +fn clamp01(x: f32) f32 { + return std.math.clamp(x, 0, 1); +} + +fn glide(x: f32) f32 { + return theme.ease(.glide, clamp01(x)); +} + +pub const Words = struct { + rec: *const Recording, + at: u64, + in: anim.Value, + regroup: anim.Value, + framed: anim.Value, + fold: anim.Value, + /// 0 to 1: the rows' addresses take the stack's ink (the dump tied to its place). + placed: anim.Value, + ink: anim.Value, + left: anim.Value, + chains: anim.Value, + /// The word walked (the return address), the three framed and chained, top down. + walked: usize, + three: [3]usize, + /// 0 to 1: the rows glide to the stack panel's (chapter II): the panel's + /// left, its first row's top and that row's address; the bytes and the + /// chains' hops go in the first half, each address to the slot's + /// address, each word to where the slot's chain has it. + become: anim.Value, + panel: struct { x: f32 = 0, top: f32 = 0, first: u64 = 0 } = .{}, + + pub fn init(s: *anim.Frame, rec: *const Recording, moment: u64) Words { + const picked = pick(rec, moment); + return .{ .rec = rec, .at = moment, .in = s.value(-1), .regroup = s.value(-1), .framed = s.value(-1), .fold = s.value(0), .placed = s.value(0), .ink = s.value(-1), .left = s.value(-1), .chains = s.value(-1), .walked = picked.walked, .three = picked.three, .become = s.value(0) }; + } + + /// The return address the newest frame has, and the first word that + /// points into the heap, into the stack, and the return address, top down. + pub fn pick(rec: *const Recording, moment: u64) struct { walked: usize, three: [3]usize } { + const m = rec.moment(moment); + const rsp = rec.register(moment, "rsp"); + var walked: usize = 0; + if (m.regs(rec.tid) catch null) |regs| { + var slots: [words]zt.telescope.Slot = undefined; + if (zt.telescope.slots(m, ®s, rsp, &slots)) |found| { + for (found, 0..) |slot, i| if (slot.role == .return_address) { + walked = i; + break; + }; + } else |_| {} + } + const values = read(rec, moment); + var heap: ?usize = null; + var stack: ?usize = null; + for (values.words, 0..) |v, i| switch (rows.regionOf(m, v)) { + .heap => heap = heap orelse i, + .stack => stack = stack orelse i, + else => {}, + }; + var three = [3]usize{ heap orelse 0, stack orelse 1, walked }; + std.mem.sort(usize, &three, {}, std.sort.asc(usize)); + return .{ .walked = walked, .three = three }; + } + + pub const Bytes = struct { base: u64, bytes: [8 * words]u8, known: [8 * words]bool, words: [words]u64 }; + + pub fn read(rec: *const Recording, moment: u64) Bytes { + var out: Bytes = .{ .base = rec.register(moment, "rsp"), .bytes = undefined, .known = undefined, .words = undefined }; + rec.moment(moment).read(out.base, &out.bytes, &out.known) catch @memset(&out.known, false); + for (&out.words, 0..) |*w, i| w.* = std.mem.readInt(u64, out.bytes[i * 8 ..][0..8], .little); + return out; + } + + /// Word `i`'s row's top, its bytes' first x, and how much its address shows, at regroup clock `r`. + fn where(i: usize, r: f32) struct { y: f32, x: f32, address: f32 } { + const row: f32 = @floatFromInt(i / 2); + const half: f32 = @floatFromInt(i % 2); + const a = glide(r / 18); + const b = glide((r - 18) / 12); + const c = glide((r - 30) / 18); + return .{ + .y = at.top + pitch * (row + row * a + half * b), + .x = at.bytes + half * (8 * at.pair + at.middle) * (1 - c), + .address = if (half == 0) 1 else clamp01((r - 36) / 12), + }; + } + + /// The word's text: its hex without the zeros in front. + fn digits(u: ui.Ui, v: u64) []const u8 { + return u.print("{x}", .{v}); + } + + pub fn draw(w: *const Words, f: *sk.Frame, placed: anim.Placed) void { + const u: ui.Ui = .{ .f = f, .at = placed }; + const in = placed.value(w.in); + if (in < 0) return; + const was = @import("segments.zig").clipStage(f, placed); + defer @import("segments.zig").unclipStage(f, was); + const r = placed.value(w.regroup); + const fold = placed.value(w.fold); + const shown = 1 - clamp01(fold / 6); + w.tag(u, fold); + if (shown <= 0) return; + const v = u.faded(shown); + const data = read(w.rec, w.at); + const m = w.rec.moment(w.at); + // The header (mock 29), with the first row; gone as the dump is tied to its place. + const header = clamp01(in / 18) * (1 - placed.value(w.placed)); + if (header > 0) v.faded(header).text(.sans, theme.size.sans.micro, at.address, at.top - 12, "128 bytes, from where rsp points", t.faint); + + // The veil over every row but the walked one, while it is walked. + const veil = clamp01((r - 48) / 18) - clamp01((r - 270) / 18); + const address_ink = ui.mix(t.faint, theme.color.region.stack, placed.value(w.placed)); + const become = placed.value(w.become); + const g = glide(become); + for (0..words) |i| { + const p = where(i, r); + const arrive = clamp01((in - 12 * @as(f32, @floatFromInt(i / 2))) / 18); + if (arrive <= 0) continue; + const row = v.faded(arrive * (if (i == w.walked) 1 else 1 - theme.color.veil_alpha * veil)); + const slot: f32 = @floatFromInt(@as(i64, @bitCast(data.base + 8 * i -% w.panel.first)) >> 3); + const y = std.math.lerp(p.y, w.panel.top + pitch * slot, g); + const base = theme.baseline(y, pitch, size); + const ax = std.math.lerp(at.address, w.panel.x + theme.metric.slot.address, g); + if (p.address > 0) _ = row.faded(p.address).address(ax, base, data.base + 8 * i, .{ .ink = address_ink }); + w.drawBytes(row.faded(1 - clamp01(become * 2)), data, i, p.x, base, r, placed); + w.drawWord(row, m, data.words[i], i, y, r, placed); + } + if (r >= 0) w.walk(v, data, r); + // The three words framed as read, 30 f apart, each in over 6 f. + const framed = placed.value(w.framed); + if (framed >= 0) for (w.three, 0..) |i, k| { + const shows = clamp01((framed - 30 * @as(f32, @floatFromInt(k))) / 6); + if (shows > 0) v.read(w.wordRect(i, data.words[i], placed), shows); + }; + } + + /// Word `i`'s digits as they stand in the field (ranged right, or at its left). + fn wordRect(w: *const Words, i: usize, value: u64, placed: anim.Placed) Rect { + const n: f32 = @floatFromInt(ui.digits(value)); + const left = glide(placed.value(w.left) / 24); + const x = std.math.lerp(at.field_end - n * adv, at.field, left); + return .{ .x = x, .y = at.top + pitch * @as(f32, @floatFromInt(i)), .w = n * adv, .h = pitch }; + } + + fn drawBytes(w: *const Words, u: ui.Ui, data: Bytes, i: usize, x: f32, base: f32, r: f32, placed: anim.Placed) void { + // Its bytes dim as its word comes in (the run), or are left faint where they flew from. + const dim = if (i == w.walked) 0 else w.runIn(i, r); + for (0..8) |k| { + const bx = x + @as(f32, @floatFromInt(k)) * at.pair; + const b = i * 8 + k; + if (i == w.walked and r >= 108) { + // In flight or landed: a faint copy fades into its place from a third of its flight. + const copy = clamp01((r - 120 - 6 * @as(f32, @floatFromInt(k))) / 12); + if (copy > 0) u.faded(copy).text(.mono, size, bx, base, u.print("{x:0>2}", .{data.bytes[b]}), dimmed()); + continue; + } + const text = if (data.known[b]) u.print("{x:0>2}", .{data.bytes[b]}) else "??"; + u.text(.mono, size, bx, base, text, ui.mix(ui.rgb(t.primary), dimmed(), dim)); + } + // The walked word's bytes framed with its word as it takes its ink. + if (i == w.walked) { + const shows = w.walkedFrame(placed); + if (shows > 0) u.read(.{ .x = x, .y = base - 7.2 - pitch / 2, .w = 7 * at.pair + 2 * adv, .h = pitch }, shows); + } + } + + /// How far the run has brought word `i` in (its word in, its bytes dim): 8 f, rows 6 f apart. + fn runIn(w: *const Words, i: usize, r: f32) f32 { + const q: f32 = @floatFromInt(if (i > w.walked) i - 1 else i); + return clamp01((r - 288 - 6 * q) / 8); + } + + fn walkedFrame(w: *const Words, placed: anim.Placed) f32 { + const ink = placed.value(w.ink); + if (ink < 0) return 0; + return clamp01(ink / 6) * (1 - clamp01((ink - 72 - 2 * words - 12) / 18)); + } + + fn drawWord(w: *const Words, u: ui.Ui, m: Recording.Moment, value: u64, i: usize, y: f32, r: f32, placed: anim.Placed) void { + const shows = if (i == w.walked) @as(f32, if (r >= 240) 1 else 0) else w.runIn(i, r); + if (shows <= 0) return; + const v = u.faded(shows); + const kind = rows.regionOf(m, value); + // `ink`: the walked word first (12 f), the rest at a run, 2 f apart, rows top down. + const ink_clock = placed.value(w.ink); + const order: f32 = @floatFromInt(if (i == w.walked) 0 else 72 + 2 * (if (i > w.walked) i - 1 else i)); + const inked = if (ink_clock < 0) 0 else clamp01((ink_clock - order) / 12); + const ink = ui.mix(t.primary, rows.ink(kind), inked); + var rect = w.wordRect(i, value, placed); + const g = glide(placed.value(w.become)); + rect.y = y; + rect.x = std.math.lerp(rect.x, w.panel.x + theme.metric.slot.address + 12 * adv + theme.metric.context.arrow_w, g); + const base = theme.baseline(y, pitch, size); + // `extend`: three chains grow from their word, 36 f apart. + const chains = placed.value(w.chains); + const k = std.mem.indexOfScalar(usize, &w.three, i); + if (chains >= 0 and k != null and kind != .none) { + const since = (chains - 36 * @as(f32, @floatFromInt(k.?))) / 60; + if (since >= 0 and (kind == .code or kind == .rwx)) { + // A code address's chain is its name: it comes in as a hop does (6 f). + const end = v.address(rect.x, base, value, .{ .ink = ink, .rwx = kind == .rwx }); + var name_buf: [128]u8 = undefined; + if (m.string(value, &name_buf) catch null) |name| v.faded(clamp01(since * 10)).text(.mono, size, end + 10, base, ui.symbol(u, name), t.primary); + return; + } + if (since >= 0) { + _ = rows.chain(v, rect.x, y, m, value, theme.layout.state.x + theme.layout.state.w, .{ .extend = since, .hops = 1 - clamp01(placed.value(w.become) * 2) }); + return; + } + } + if (kind == .none) v.text(.mono, size, rect.x, base, digits(u, value), ink) else _ = v.address(rect.x, base, value, .{ .ink = ink, .rwx = kind == .rwx }); + const frame = if (i == w.walked) w.walkedFrame(placed) else 0; + if (frame > 0) v.read(rect, frame); + } + + /// The walked word, from its offsets to its sixteen digits (motion.md, "A word read"). + fn walk(w: *const Words, u: ui.Ui, data: Bytes, r: f32) void { + if (r < 66 or r >= 240) return; + const y = at.top + pitch * @as(f32, @floatFromInt(w.walked)); + const base = theme.baseline(y, pitch, size); + const value = data.words[w.walked]; + // Under the row, its offsets and labels on the page (the veiled rows under them give way). + // They go as the gaps close (192), not after: closed, their pairs are 24 apart and they would touch. + const labels = clamp01((r - 84) / 12) * (1 - clamp01((r - 192) / 12)); + const offsets = clamp01((r - 66) / 12) * (1 - clamp01((r - 192) / 12)); + const zeros_mark = clamp01((r - 204) / 12) * (1 - clamp01((r - 228) / 12)); + const under = @max(offsets, zeros_mark); + if (under > 0) u.faded(under).fill(.{ .x = at.bytes - 12, .y = y + pitch, .w = at.field_end - at.bytes + 24, .h = 2 * pitch + 6 }, theme.color.page); + const close = glide((r - 192) / 12); + for (0..8) |k| { + const kf: f32 = @floatFromInt(k); + const s = clamp01((r - 108 - 6 * kf) / 36); + const from = at.bytes + kf * at.pair; + const pair = 7 - kf; + const to = std.math.lerp(at.field + pair * at.pair, at.field_end - (8 - pair) * 2 * adv, close); + const long = to - from; + const high = std.math.clamp(long / 4, theme.time.flight.arc_min, theme.time.flight.arc_max); + const x = std.math.lerp(from, to, theme.ease(.glide, s)); + const lift = high * @sin(std.math.pi * s); + // The byte itself: bright and bold in the air (from 108 the walk draws it, not the row). + if (r >= 108) { + const air = s > 0 and s < 1; + const text = u.print("{x:0>2}", .{data.bytes[w.walked * 8 + k]}); + // In the air it carries its ground: over the veiled rows it reads alone. + if (air) u.fill(.{ .x = x - 3, .y = base - lift - 18, .w = 2 * adv + 6, .h = 24 }, theme.color.page); + // Each digit of a pair on its own: a zero in front of the word fades on its own. + for (0..2) |d| { + const lead = 2 * (7 - k) + d < 16 - ui.digits(value); + const fade: f32 = if (lead and r >= 228) 1 - clamp01((r - 228) / 12) else 1; + if (fade > 0) u.faded(fade).text(if (air) .mono_bold else .mono, size, x + @as(f32, @floatFromInt(d)) * adv, base - lift, text[d .. d + 1], if (air) t.bright else t.primary); + } + } + // Its offset under it, going with it. + const off = offsets * clamp01((r - 66 - 2 * kf) / 12); + if (off > 0 and s > 0 and s < 1) u.faded(off).fill(.{ .x = x - 3, .y = base + pitch - lift - 18, .w = 2 * adv + 6, .h = 24 }, theme.color.page); + if (off > 0) u.faded(off).text(.mono, size, x, base + pitch - lift, u.print("+{d}", .{k}), t.dim); + // `low byte` from its byte's left, `high byte` to its byte's right: under the bytes, never the addresses. + if (k == 0 or k == 7) if (labels > 0) { + const label = if (k == 0) "low byte" else "high byte"; + const lw = u.width(.sans, theme.size.sans.tag, label); + u.faded(labels).text(.sans, theme.size.sans.tag, if (k == 0) x else x + 2 * adv - lw, base + 2 * pitch + 2 - lift, label, t.dim); + }; + } + // The zeros in front, bracketed (motion.md: 204 to 228). + if (zeros_mark > 0) { + const lead: f32 = @floatFromInt(16 - ui.digits(value)); + const x0 = at.field_end - 16 * adv; + const v = u.faded(zeros_mark); + v.fill(.{ .x = x0, .y = y + pitch - 2, .w = lead * adv, .h = 2 }, t.dim); + v.fill(.{ .x = x0, .y = y + pitch - 8, .w = 2, .h = 6 }, t.dim); + v.fill(.{ .x = x0 + lead * adv - 2, .y = y + pitch - 8, .w = 2, .h = 6 }, t.dim); + v.text(.sans, theme.size.sans.tag, x0, base + pitch + 2, "zeros in front", t.dim); + } + } + + /// The dump folded to its tag on the shelf (mock 49's shelf, y 48): the + /// rows out over 6 f, the tag from over the rows to the shelf (18 f, `glide`). + fn tag(_: *const Words, u: ui.Ui, fold: f32) void { + if (fold <= 0) return; + const title = "the stack's 128 bytes"; + const p = glide((fold - 6) / 18); + const w = u.width(.sans_medium, theme.size.sans.tag, title) + 40; + const x = std.math.lerp(at.address - 8, theme.canvas.margin_x, p); + const y = std.math.lerp(at.top - 30, theme.canvas.stage.y, p); + const v = u.faded(clamp01(fold / 6)).moved(x, y); + v.fill(.{ .x = 0, .y = 0, .w = w, .h = theme.metric.context.tag }, theme.color.raised); + v.frame(.{ .x = 8, .y = 6, .w = 12, .h = 12 }, t.faint, theme.stroke.rule); + v.text(.sans_medium, theme.size.sans.tag, 28, theme.metric.context.tag - 6, title, t.dim); + } +}; diff --git a/anim/src/relations.zig b/anim/src/relations.zig new file mode 100644 index 0000000..03165d5 --- /dev/null +++ b/anim/src/relations.zig @@ -0,0 +1,341 @@ +//! The relations' data (design/README.md, "4. Ties"), one table set a +//! frame, and the passes over it. The panels say, as they draw, where each +//! of their things is (`places`: a key, the kind of thing and its address +//! packed in one integer, a box, a pane) and which values they show that +//! could be addresses (`values`: the value, its box, its pane, a reference +//! such as a register's index). A relation is a join of the two on +//! address, or a step's own fact (a touch, a carry, the thread), and the +//! resolver (`ties.zig`) runs it as a pipeline of passes over slices: +//! candidates, `filter` by the knobs, `order` by priority, `cut` to the +//! budget, route, draw. `filter`, `order`, `cut` and `join` are pure, and +//! tested here with tables written by hand. +//! +//! Memory: the tables live in the frame's scratch arena (`sk.Frame.user`), +//! made at the frame's first place and gone with it: no process globals, +//! no allocation per relation, indices rather than pointers. +const std = @import("std"); + +/// The pane a place or a value is drawn in. +pub const Pane = enum(u8) { other, source, disasm, registers, stack, view, drawing, loupe }; + +/// A key: the kind of thing in its top byte, an address (or an index) in the rest. +pub const Key = u64; +pub const kind_shift = 56; +pub const address_mask: u64 = (1 << kind_shift) - 1; + +pub fn keyOf(kind: u8, address: u64) Key { + return @as(u64, kind) << kind_shift | (address & address_mask); +} + +pub fn kindOf(key: Key) u8 { + return @intCast(key >> kind_shift); +} + +pub const Place = struct { key: Key, box: [4]f32, pane: Pane }; +/// `at`: the address of the word that holds it (0: a register's); `ref`: a +/// register's index; `rules`: its panel's left and right rules. +pub const Value = struct { value: u64, box: [4]f32, pane: Pane, ref: u32 = 0, at: u64 = 0, rules: [2]f32 = .{ 0, 0 } }; + +/// One layer's tables: the working frame's, or the map's. +pub const Tables = struct { + places: std.MultiArrayList(Place) = .{}, + values: std.MultiArrayList(Value) = .{}, + /// `places` by key (then by the order they were said), and `values` by + /// value: built once, on the first lookup after a change. + places_by_key: std.ArrayList(u32) = .empty, + values_by_value: std.ArrayList(u32) = .empty, + places_sorted: bool = true, + values_sorted: bool = true, + /// Nanoseconds this frame spent sorting. + sort_ns: u64 = 0, + + pub fn add(t: *Tables, gpa: std.mem.Allocator, place: Place) void { + t.places.append(gpa, place) catch return; + t.places_sorted = false; + } + + pub fn show(t: *Tables, gpa: std.mem.Allocator, value: Value) void { + t.values.append(gpa, value) catch return; + t.values_sorted = false; + } + + /// The index of places by key: sorted once after a change. + pub fn byKey(t: *Tables, gpa: std.mem.Allocator) []const u32 { + if (!t.places_sorted) { + const start = clock(); + sortIndex(gpa, &t.places_by_key, t.places.items(.key)); + t.places_sorted = true; + t.sort_ns += clock() - start; + } + return t.places_by_key.items; + } + + pub fn byValue(t: *Tables, gpa: std.mem.Allocator) []const u32 { + if (!t.values_sorted) { + const start = clock(); + sortIndex(gpa, &t.values_by_value, t.values.items(.value)); + t.values_sorted = true; + t.sort_ns += clock() - start; + } + return t.values_by_value.items; + } + + /// The box of the first place said with `key`: a binary search. + pub fn find(t: *Tables, gpa: std.mem.Allocator, key: Key) ?[4]f32 { + const order = t.byKey(gpa); + const keys = t.places.items(.key); + const i = lowerBound(order, keys, key); + if (i == order.len or keys[order[i]] != key) return null; + return t.places.items(.box)[order[i]]; + } + + /// The indices (into `places`) of the keys in `[lo, hi)`, in key order. + pub fn range(t: *Tables, gpa: std.mem.Allocator, lo: Key, hi: Key) []const u32 { + const order = t.byKey(gpa); + const keys = t.places.items(.key); + return order[lowerBound(order, keys, lo)..lowerBound(order, keys, hi)]; + } + + /// The greatest key of kind `kind` at or below `address`, within `within` of it. + pub fn atOrBelow(t: *Tables, gpa: std.mem.Allocator, kind: u8, address: u64, within: u64) ?u64 { + const order = t.byKey(gpa); + const keys = t.places.items(.key); + const i = lowerBound(order, keys, keyOf(kind, address) + 1); + if (i == 0) return null; + const k = keys[order[i - 1]]; + if (kindOf(k) != kind) return null; + const a = k & address_mask; + return if (address - a < within) a else null; + } +}; + +/// `index` made the permutation of `keys` that sorts them, ties in their order. +fn sortIndex(gpa: std.mem.Allocator, index: *std.ArrayList(u32), keys: []const u64) void { + index.resize(gpa, keys.len) catch return; + for (index.items, 0..) |*v, i| v.* = @intCast(i); + std.sort.pdq(u32, index.items, keys, struct { + fn less(k: []const u64, a: u32, b: u32) bool { + return k[a] < k[b] or (k[a] == k[b] and a < b); + } + }.less); +} + +/// The first position in `order` whose key is not below `key`. +fn lowerBound(order: []const u32, keys: []const u64, key: u64) usize { + var lo: usize = 0; + var hi: usize = order.len; + while (lo < hi) { + const mid = (lo + hi) / 2; + if (keys[order[mid]] < key) lo = mid + 1 else hi = mid; + } + return lo; +} + +/// A value shown and the place of kind `kind` at its address (masked by +/// `mask`: a view's row is its first byte's): indices into the tables. +pub const Pair = struct { value: u32, place: u32 }; + +/// The join of `values` (by value, `by_value` their order) with the +/// places of kind `kind` (by key, `by_key` their order) on address: a merge +/// of the two sorted sides, each value with the first place said at its +/// address. Appends to `out`; a value that is no place's address has none. +pub fn join(values: []const u64, by_value: []const u32, keys: []const Key, by_key: []const u32, kind: u8, mask: u64, out: *std.ArrayList(Pair), gpa: std.mem.Allocator) void { + // The places of the kind: one run of the key order. + const lo = lowerBound(by_key, keys, keyOf(kind, 0)); + const hi = lowerBound(by_key, keys, keyOf(kind + 1, 0)); + const run = by_key[lo..hi]; + // Masking keeps the values' order (a mask clears low bits): one walk. + var p: usize = 0; + for (by_value) |v| { + const want = values[v] & mask & address_mask; + while (p < run.len and keys[run[p]] & address_mask < want) p += 1; + if (p == run.len) break; + if (keys[run[p]] & address_mask == want) out.append(gpa, .{ .value = v, .place = run[p] }) catch return; + } +} + +// ---- The resolver's pure passes. + +/// The kinds of relation, in the order they are drawn. +pub const Kind = enum(u8) { thread, beat, link, projection, touch, carry, code }; + +/// What turns a kind on. `always`: whenever the step has one (the scene's +/// beat, its own facts); the rest only where the author has turned it on +/// (or, for the thread and links, has not turned it off). +pub const Knob = enum(u8) { thread, links, carry, code, always }; + +/// A budget's class: how many of it a frame draws at most. +pub const Class = enum(u8) { standing, beat, step, carry, code }; + +/// A kind's row of data: its knob, its class, its priority (lower first) +/// and how many of it a frame draws at most. +pub const Spec = struct { knob: Knob, class: Class, priority: u8, most: u8 }; + +pub const specs = std.enums.EnumArray(Kind, Spec).init(.{ + .thread = .{ .knob = .thread, .class = .standing, .priority = 0, .most = 1 }, + .beat = .{ .knob = .always, .class = .beat, .priority = 1, .most = 1 }, + .link = .{ .knob = .links, .class = .step, .priority = 2, .most = 1 }, + .projection = .{ .knob = .always, .class = .step, .priority = 3, .most = 1 }, + // The first to give way: three lines at most, so not with a link and a projection. + .touch = .{ .knob = .always, .class = .step, .priority = 4, .most = 1 }, + .carry = .{ .knob = .carry, .class = .carry, .priority = 5, .most = 1 }, + // One at a time: a standing row across panes, or the step's. + .code = .{ .knob = .code, .class = .code, .priority = 6, .most = 1 }, +}); + +/// How many of a class a frame draws at most. +pub const budget = std.enums.EnumArray(Class, u8).init(.{ .standing = 1, .beat = 1, .step = 2, .carry = 1, .code = 1 }); + +/// The knobs as the scene has them this frame. +pub const Knobs = std.enums.EnumSet(Knob); + +/// A relation that could be drawn: its kind, its rank within its kind (a +/// register's place in pwndbg's order, a span's in the step), and the +/// indices of what it ties (`from`, `to`: into a table, or a kind's own). +pub const Candidate = struct { kind: Kind, rank: u32 = 0, from: u32 = 0, to: u32 = 0 }; + +/// Those whose kind's knob is on, in place. +pub fn filter(cands: []Candidate, knobs: Knobs) []Candidate { + var n: usize = 0; + for (cands) |c| if (knobs.contains(specs.get(c.kind).knob)) { + cands[n] = c; + n += 1; + }; + return cands[0..n]; +} + +/// By priority, then rank, then as they came (`from`, each its own). +pub fn byPriority(cands: []Candidate) void { + std.sort.pdq(Candidate, cands, {}, struct { + fn less(_: void, a: Candidate, b: Candidate) bool { + const pa = specs.get(a.kind).priority; + const pb = specs.get(b.kind).priority; + if (pa != pb) return pa < pb; + if (a.rank != b.rank) return a.rank < b.rank; + return a.from < b.from; + } + }.less); +} + +/// The first of each kind up to its most, and of each class up to its +/// budget, in place: what is cut is the least urgent. +pub fn cut(cands: []Candidate) []Candidate { + var per_kind = std.enums.EnumArray(Kind, u8).initFill(0); + var per_class = std.enums.EnumArray(Class, u8).initFill(0); + var n: usize = 0; + for (cands) |c| { + const spec = specs.get(c.kind); + if (per_kind.get(c.kind) >= spec.most or per_class.get(spec.class) >= budget.get(spec.class)) continue; + per_kind.set(c.kind, per_kind.get(c.kind) + 1); + per_class.set(spec.class, per_class.get(spec.class) + 1); + cands[n] = c; + n += 1; + } + return cands[0..n]; +} + +/// Counts and nanoseconds of a frame's relations (`CANIM_RELATIONS=1`: a line a frame on stderr). +pub const Stats = struct { + places: usize = 0, + values: usize = 0, + candidates: usize = 0, + kept: usize = 0, + drawn: usize = 0, + map_places: usize = 0, + map_values: usize = 0, + map_pairs: usize = 0, + ns: struct { sort: u64 = 0, candidates: u64 = 0, passes: u64 = 0, draw: u64 = 0, map: u64 = 0 } = .{}, +}; + +/// Whether to say each frame's counts and times on stderr (`CANIM_RELATIONS=1`), read once. +pub fn reporting() bool { + if (report_on == null) report_on = if (comptime @import("builtin").link_libc) std.c.getenv("CANIM_RELATIONS") != null else false; + return report_on.?; +} +var report_on: ?bool = null; + +pub fn clock() u64 { + var ts: std.os.linux.timespec = undefined; + _ = std.os.linux.clock_gettime(.MONOTONIC, &ts); + return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec)); +} + +// ---- Tests: hand-written tables in, tables out. + +test "a lookup is the first place said with its key, by binary search" { + const gpa = std.testing.allocator; + var t: Tables = .{}; + defer { + t.places.deinit(gpa); + t.places_by_key.deinit(gpa); + } + t.add(gpa, .{ .key = keyOf(3, 0x30), .box = .{ 1, 0, 0, 0 }, .pane = .stack }); + t.add(gpa, .{ .key = keyOf(1, 0x10), .box = .{ 2, 0, 0, 0 }, .pane = .registers }); + t.add(gpa, .{ .key = keyOf(3, 0x30), .box = .{ 3, 0, 0, 0 }, .pane = .stack }); + try std.testing.expectEqual(@as(f32, 1), t.find(gpa, keyOf(3, 0x30)).?[0]); + try std.testing.expectEqual(@as(f32, 2), t.find(gpa, keyOf(1, 0x10)).?[0]); + try std.testing.expect(t.find(gpa, keyOf(3, 0x38)) == null); + // Said after a lookup: the order is made again. + t.add(gpa, .{ .key = keyOf(2, 0x20), .box = .{ 4, 0, 0, 0 }, .pane = .view }); + try std.testing.expectEqual(@as(f32, 4), t.find(gpa, keyOf(2, 0x20)).?[0]); + // A range of one kind, and the nearest at or below an address. + t.add(gpa, .{ .key = keyOf(3, 0x50), .box = .{ 5, 0, 0, 0 }, .pane = .stack }); + try std.testing.expectEqual(@as(usize, 3), t.range(gpa, keyOf(3, 0), keyOf(4, 0)).len); + try std.testing.expectEqual(@as(?u64, 0x30), t.atOrBelow(gpa, 3, 0x48, 64)); + try std.testing.expectEqual(@as(?u64, null), t.atOrBelow(gpa, 3, 0x48, 8)); + try std.testing.expectEqual(@as(?u64, null), t.atOrBelow(gpa, 3, 0x10, 64)); +} + +test "a join pairs each value with the first place at its address, of one kind" { + const gpa = std.testing.allocator; + // Places: rows (kind 14) at 401000, 401004 (twice), 401010; a word (kind 3) at 401004. + const keys = [_]Key{ keyOf(14, 0x401004), keyOf(3, 0x401004), keyOf(14, 0x401000), keyOf(14, 0x401010), keyOf(14, 0x401004) }; + var by_key: std.ArrayList(u32) = .empty; + defer by_key.deinit(gpa); + sortIndex(gpa, &by_key, &keys); + // Values: 401004 (twice), 7fff0000, 401010, 401008. + const values = [_]u64{ 0x401004, 0x7fff0000, 0x401010, 0x401004, 0x401008 }; + var by_value: std.ArrayList(u32) = .empty; + defer by_value.deinit(gpa); + sortIndex(gpa, &by_value, &values); + var out: std.ArrayList(Pair) = .empty; + defer out.deinit(gpa); + join(&values, by_value.items, &keys, by_key.items, 14, ~@as(u64, 0), &out, gpa); + try std.testing.expectEqualSlices(Pair, &.{ .{ .value = 0, .place = 0 }, .{ .value = 3, .place = 0 }, .{ .value = 2, .place = 3 } }, out.items); + // Masked: a view's row is its first byte's (sixteen-aligned). + out.clearRetainingCapacity(); + join(&values, by_value.items, &keys, by_key.items, 14, ~@as(u64, 15), &out, gpa); + try std.testing.expectEqualSlices(Pair, &.{ .{ .value = 0, .place = 2 }, .{ .value = 3, .place = 2 }, .{ .value = 4, .place = 2 }, .{ .value = 2, .place = 3 } }, out.items); +} + +test "the passes: off unless its knob is on, by priority, cut to the budget" { + var cands = [_]Candidate{ + .{ .kind = .touch }, + .{ .kind = .code, .rank = 2 }, + .{ .kind = .link, .rank = 5 }, + .{ .kind = .projection }, + .{ .kind = .link, .rank = 1 }, + .{ .kind = .thread }, + .{ .kind = .code, .rank = 0 }, + }; + // The code kind is off by default: it goes; the rest are on. + var knobs = Knobs.initMany(&.{ .thread, .links, .always }); + var kept = filter(&cands, knobs); + try std.testing.expectEqual(@as(usize, 5), kept.len); + byPriority(kept); + try std.testing.expectEqual(Kind.thread, kept[0].kind); + try std.testing.expectEqual(@as(u32, 1), kept[1].rank); + kept = cut(kept); + // One thread, one link (the first in the registers' order), one projection; the touch gives way. + try std.testing.expectEqual(@as(usize, 3), kept.len); + try std.testing.expectEqualSlices(Kind, &.{ .thread, .link, .projection }, &.{ kept[0].kind, kept[1].kind, kept[2].kind }); + try std.testing.expectEqual(@as(u32, 1), kept[1].rank); + // With no projection the touch is drawn; with the knob on, one code pointer, the first in rank. + var more = [_]Candidate{ .{ .kind = .touch }, .{ .kind = .link }, .{ .kind = .code, .rank = 3 }, .{ .kind = .code, .rank = 1 } }; + knobs.insert(.code); + kept = filter(&more, knobs); + byPriority(kept); + kept = cut(kept); + try std.testing.expectEqualSlices(Kind, &.{ .link, .touch, .code }, &.{ kept[0].kind, kept[1].kind, kept[2].kind }); + try std.testing.expectEqual(@as(u32, 1), kept[2].rank); +} diff --git a/anim/src/root.zig b/anim/src/root.zig index fcdb572..596ac57 100644 --- a/anim/src/root.zig +++ b/anim/src/root.zig @@ -48,6 +48,8 @@ pub const CallTree = calls.Tree; pub const Nodes = @import("nodes.zig").Nodes; pub const hexdump = @import("hexdump.zig"); const tying = @import("ties.zig"); +/// The relations' tables and the resolver's pure passes (design/README.md, "4. Ties"). +pub const relations = @import("relations.zig"); pub const Ties = tying.Ties; pub const Place = tying.Place; pub const Array = @import("array.zig").Array; @@ -71,6 +73,22 @@ pub const Column = columns.Column; pub const column = columns.column; /// A column pane's rows: what the column has left. pub const column_rest = columns.rest; +/// Two runs against each other (design/README.md, `pair`): each side's +/// stage (`pair.split`), the frame as bytes (`pair.Frame`), a stopped +/// run's output (`pair.Report`), the marks across the seam, the clock. +pub const pair = @import("pair.zig"); +/// The introduction's blocks (design/motion.md, "A scene: the +/// introduction"): the address space to scale (`segments`), the bytes at +/// the stack's top made words (`regroup`), a text beside its part (`Aside`). +pub const segments = @import("segments.zig"); +pub const Segments = segments.Segments; +pub const regroup = @import("regroup.zig"); +pub const Words = regroup.Words; +pub const Aside = @import("aside.zig").Aside; +pub const Asides = @import("aside.zig").Asides; +/// The source with its instructions, one panel, each line level with its code (`aligned`). +pub const aligned = @import("aligned.zig"); +pub const Aligned = aligned.Aligned; /// The page behind every scene: `pub const background = canim.background;`. pub const background: u32 = theme.color.page; @@ -406,6 +424,7 @@ test { _ = @import("descent.zig"); _ = graph; _ = tying; + _ = relations; _ = @import("hexdump.zig"); _ = calls; _ = @import("nodes.zig"); @@ -416,4 +435,9 @@ test { _ = objects; _ = words; _ = columns; + _ = pair; + _ = segments; + _ = regroup; + _ = @import("aside.zig"); + _ = aligned; } diff --git a/anim/src/segments.zig b/anim/src/segments.zig new file mode 100644 index 0000000..d28d2dd --- /dev/null +++ b/anim/src/segments.zig @@ -0,0 +1,605 @@ +//! `segments` (design/README.md, "segments"; motion.md, "The address space +//! enlarged"; mocks 30 and 34, treatment A): the user half of the address +//! space to true scale, a slim bar, and each cluster of mappings enlarged +//! once, linear, a box beside it with every named band's start and end +//! addresses and its size. Everything from the recording's mappings at one +//! moment (`Moment.mappings`, `mapping`): nothing is placed by hand. +//! +//! Drawn in the program column (x 64, 880 wide), between `top` and `foot` +//! (the column's 84 to 888, the working frame's 80 to 864: `form`); the +//! scene plays its clocks (frames, linear): `whole` (the bar, its ends, the +//! mappings' lines), each cluster's `enlarge`, a band's `ink` echo. `Place` +//! is a dump's place in it: the `tie` bracket and the `link` onto the +//! stack's box. +const std = @import("std"); +const sk = @import("sketch"); +const anim = @import("anim"); +const zt = @import("ztrace"); +const ui = @import("ui.zig"); +const Recording = @import("Recording.zig"); +const theme = ui.theme; +const Rect = ui.Rect; +const Vec = ui.Vec; +const t = theme.color.text; +const region = theme.color.region; + +/// The user half: 0 to 7fffffffffff. +pub const user_end: u64 = 1 << 47; + +/// Touching mappings of one file and one permission (an anonymous one right +/// after a file's, its bss, with it). +pub const Band = struct { lo: u64, hi: u64, prot: u64, name: []const u8, ink: u24 }; +/// What has a row of names: a file once (the program by segment, each band its own). +pub const Unit = struct { lo: u64, hi: u64, prot: ?u64, name: []const u8, ink: u24, program: bool }; +pub const Cluster = struct { lo: u64, hi: u64, bands: [2]u16, units: [2]u16 }; + +pub const Space = struct { + bands: []Band, + units: []Unit, + clusters: []Cluster, + program: []const u8, +}; + +const Kind = zt.telescope.Region; + +fn inkOf(kind: Kind) u24 { + return switch (kind) { + .code, .rwx => region.code_, + .stack => region.stack, + .heap => region.heap, + .data => region.data, + .rodata => region.rodata, + .none => region.number, + }; +} + +/// A mapping's file as a name: its path's last part, without the kernel's ` (deleted)`. +fn nameOf(path: []const u8) []const u8 { + const base = if (std.mem.lastIndexOfScalar(u8, path, '/')) |k| path[k + 1 ..] else path; + return if (std.mem.endsWith(u8, base, " (deleted)")) base[0 .. base.len - " (deleted)".len] else base; +} + +/// The address space at moment `at`: its bands, the units named, the +/// clusters top down (highest first). A cluster is mappings with less than +/// 16 MiB between them; a band more than eight times the rest of its +/// cluster is a cluster of its own (the tracer's ring under libc: in the +/// libraries' box it would leave libc 12 units tall). +pub fn read(rec: *const Recording, at: u64, arena: std.mem.Allocator) Space { + const mo = rec.moment(at); + const maps = mo.mappings(arena) catch &.{}; + var bands: std.ArrayList(Band) = .empty; + var path_buf: [512]u8 = undefined; + for (maps) |mp| { + if (mp.addr >= user_end) continue; + var path: []const u8 = ""; + _ = mo.mapping(mp.addr, &path, &path_buf) catch null; + const name = arena.dupe(u8, nameOf(path)) catch ""; + if (bands.items.len > 0) { + const last = &bands.items[bands.items.len - 1]; + if (last.hi == mp.addr and last.prot == mp.prot and (std.mem.eql(u8, last.name, name) or (name.len == 0 and last.name.len > 0 and last.name[0] != '['))) { + last.hi = mp.addr + mp.len; + continue; + } + } + bands.append(arena, .{ .lo = mp.addr, .hi = mp.addr + mp.len, .prot = mp.prot, .name = name, .ink = inkOf(Kind.of(mp, name)) }) catch break; + } + // The program: the file the pc is in. + var pc_path: []const u8 = ""; + _ = mo.mapping(rec.register(at, "rip"), &pc_path, &path_buf) catch null; + const program = arena.dupe(u8, nameOf(pc_path)) catch ""; + + // Units: the program's bands each, every other file once. + var units: std.ArrayList(Unit) = .empty; + var k: usize = 0; + while (k < bands.items.len) { + const b = bands.items[k]; + if (b.name.len == 0) { + k += 1; + continue; + } + if (std.mem.eql(u8, b.name, program)) { + units.append(arena, .{ .lo = b.lo, .hi = b.hi, .prot = b.prot, .name = b.name, .ink = b.ink, .program = true }) catch break; + k += 1; + continue; + } + var u: Unit = .{ .lo = b.lo, .hi = b.hi, .prot = b.prot, .name = b.name, .ink = b.ink, .program = false }; + k += 1; + while (k < bands.items.len and bands.items[k].lo == u.hi and std.mem.eql(u8, bands.items[k].name, u.name)) : (k += 1) { + u.hi = bands.items[k].hi; + if (u.prot != bands.items[k].prot) { + u.prot = null; + u.ink = region.rodata; + } + } + units.append(arena, u) catch break; + } + + // Clusters, low to high first, then turned top down. + var clusters: std.ArrayList(Cluster) = .empty; + var first: usize = 0; + for (bands.items, 0..) |b, i| { + const last = i + 1 == bands.items.len or bands.items[i + 1].lo - b.hi >= 16 << 20; + if (!last) continue; + splitOff(arena, &clusters, bands.items, first, i + 1); + first = i + 1; + } + std.mem.reverse(Cluster, clusters.items); + for (clusters.items) |*c| { + var lo_u: u16 = @intCast(units.items.len); + var hi_u: u16 = 0; + for (units.items, 0..) |u, i| if (u.lo >= c.lo and u.hi <= c.hi) { + lo_u = @min(lo_u, @as(u16, @intCast(i))); + hi_u = @max(hi_u, @as(u16, @intCast(i + 1))); + }; + c.units = if (hi_u > lo_u) .{ lo_u, hi_u } else .{ 0, 0 }; + } + return .{ .bands = bands.items, .units = units.items, .clusters = clusters.items, .program = program }; +} + +/// Bands `a` to `b` as a cluster, or three where one band outweighs the rest eightfold. +fn splitOff(arena: std.mem.Allocator, out: *std.ArrayList(Cluster), bands: []const Band, a: usize, b: usize) void { + var big = a; + for (a..b) |i| if (bands[i].hi - bands[i].lo > bands[big].hi - bands[big].lo) { + big = i; + }; + const whole = bands[b - 1].hi - bands[a].lo; + const len = bands[big].hi - bands[big].lo; + if (b - a > 1 and len > 8 * (whole - len)) { + for ([_][2]usize{ .{ a, big }, .{ big, big + 1 }, .{ big + 1, b } }) |r| if (r[1] > r[0]) { + out.append(arena, .{ .lo = bands[r[0]].lo, .hi = bands[r[1] - 1].hi, .bands = .{ @intCast(r[0]), @intCast(r[1]) }, .units = .{ 0, 0 } }) catch {}; + }; + return; + } + out.append(arena, .{ .lo = bands[a].lo, .hi = bands[b - 1].hi, .bands = .{ @intCast(a), @intCast(b) }, .units = .{ 0, 0 } }) catch {}; +} + +/// A size as the names give it: `4 KiB`, `132 KiB`, `2.1 MiB`, `64 MiB`, `128 TiB`. +pub fn size(buf: []u8, bytes: u64) []const u8 { + const units = [_][]const u8{ "KiB", "MiB", "GiB", "TiB" }; + var v: f64 = @as(f64, @floatFromInt(bytes)) / 1024; + var u: usize = 0; + while (v >= 1024 and u + 1 < units.len) : (u += 1) v /= 1024; + if (u > 0 and v < 10 and @abs(v - @round(v)) >= 0.05) return std.fmt.bufPrint(buf, "{d:.1} {s}", .{ v, units[u] }) catch ""; + return std.fmt.bufPrint(buf, "{d} {s}", .{ @as(u64, @intFromFloat(@round(v))), units[u] }) catch ""; +} + +fn perms(prot: u64) [3]u8 { + return .{ if (prot & 1 != 0) 'r' else '-', if (prot & 2 != 0) 'w' else '-', if (prot & 4 != 0) 'x' else '-' }; +} + +/// Rows `pitch` apart, each as near its target as the others let it +/// (crowded ones pooled round their mean), in order, centres in `lo` to `hi`. +pub fn pool(targets: []const f32, out: []f32, lo: f32, hi: f32, pitch: f32) void { + const n = targets.len; + if (n == 0) return; + var sums: [64]f32 = undefined; + var counts: [64]f32 = undefined; + var nb: usize = 0; + for (targets, 0..) |target, i| { + sums[nb] = target - pitch * @as(f32, @floatFromInt(i)); + counts[nb] = 1; + nb += 1; + while (nb > 1 and sums[nb - 2] / counts[nb - 2] > sums[nb - 1] / counts[nb - 1]) { + sums[nb - 2] += sums[nb - 1]; + counts[nb - 2] += counts[nb - 1]; + nb -= 1; + } + } + var i: usize = 0; + const top = @max(hi - pitch * @as(f32, @floatFromInt(n - 1)), lo); + for (sums[0..nb], counts[0..nb]) |s, c| for (0..@intFromFloat(c)) |_| { + out[i] = std.math.clamp(s / c, lo, top) + pitch * @as(f32, @floatFromInt(i)); + i += 1; + }; +} + +// ------------------------------------------------------------- the drawing + +/// The program column's measures (mock 30): the bar 20 wide (inside 16), +/// the boxes 108 wide inside, the leaders' room, then the names. +const m = struct { + const bar = 160; + const bar_w = 20; + const box = 304; + const box_w = 108; + const leader = 8; + const address = 476; + const indent = 24; + const size_end = 632; + const name = 656; + const pitch = 24; + const mono = theme.size.mono.context; + /// Box heights: a cluster of one band; the rest share what is left. + const one = 72; + const gap_min = 24; +}; + +/// Frames of `enlarge` (motion.md): the line framed, the ride, the page lines, the names. +const e = struct { + const frame = 6; + const ride = 72; + const pages = 12; + const names = 90; + const apart = 2; + const leader = 6; + const fade = 12; +}; + +/// Under the camera (nearer than x1), nothing drawn under the stage (y 888): +/// the caption's band is the caption's. Returns the clip it replaced. +pub fn clipStage(f: *sk.Frame, placed: anim.Placed) ??sk.Rect { + const w = @floor(f.in.window_width); + const h = @floor(f.in.window_height); + const one = @min(w / theme.canvas.width, h / theme.canvas.height); + if (one <= 0 or placed.scale / one < 1.01) return null; + const was = ui.bound; + const stage: sk.Rect = .{ .x = 0, .y = 0, .w = w, .h = (h - theme.canvas.height * one) / 2 + (theme.canvas.stage.y + theme.canvas.stage.h) * one }; + ui.bound = stage; + f.clip(stage); + return was; +} + +pub fn unclipStage(f: *sk.Frame, was: ??sk.Rect) void { + const old = was orelse return; + ui.bound = old; + if (old) |r| f.clip(r) else f.clip(null); +} + +pub const Segments = struct { + rec: *const Recording, + at: u64, + x: f32 = theme.layout.program.x, + /// 0: the column's height (84 to 888); 1: the working frame's (80 to 864). + form: anim.Value, + /// Frames into the whole's build (negative: not begun). + whole: anim.Value, + /// Frames into each cluster's `enlarge`, top down (negative: not begun). + rides: [6]anim.Value, + /// `ink`'s echoes: the band holding `addr` framed as read, `shows` of it. + echoes: [16]Echo = undefined, + n_echoes: usize = 0, + + pub const Echo = struct { addr: u64, shows: anim.Value }; + + pub fn init(s: *anim.Frame, rec: *const Recording, at: u64) Segments { + var rides: [6]anim.Value = undefined; + for (&rides) |*r| r.* = s.value(-1); + return .{ .rec = rec, .at = at, .form = s.value(0), .whole = s.value(-1), .rides = rides }; + } + + /// The bar's top and foot at `form` (0 the column, 1 the working frame). + fn span(form: f32) [2]f32 { + const k = theme.ease(.glide, form); + return .{ std.math.lerp(84, 80, k), std.math.lerp(888, 864, k) }; + } + + pub const Layout = struct { + space: Space, + top: f32, + foot: f32, + /// Each cluster's box, as it lands (inside its frame). + boxes: [6]Rect, + /// Each cluster's line on the whole. + lines: [6]f32, + }; + + /// Where everything is at `form` (`Segments.form`'s value). + pub fn layout(g: *const Segments, form: f32, arena: std.mem.Allocator) Layout { + const ends = span(form); + const space = read(g.rec, g.at, arena); + var out: Layout = .{ .space = space, .top = ends[0], .foot = ends[1], .boxes = undefined, .lines = undefined }; + const n = @min(space.clusters.len, 6); + // Heights: a cluster of one band 72; the program's as its rows; the + // libraries' (the most units but the program's) what is left. + var heights: [6]f32 = @splat(0); + var fixed: f32 = 0; + var rest: ?usize = null; + for (space.clusters[0..n], 0..) |c, i| { + const lines: f32 = @floatFromInt(2 * (c.units[1] - c.units[0]) + 1); + const has_program = for (space.units[c.units[0]..c.units[1]]) |u| { + if (u.program) break true; + } else false; + if (c.bands[1] - c.bands[0] == 1 or lines <= 3) heights[i] = m.one else if (has_program) heights[i] = m.pitch * lines - 12 else if (rest == null) { + rest = i; + continue; + } else heights[i] = m.pitch * lines - 12; + fixed += heights[i]; + } + const gaps: f32 = @floatFromInt(@max(n, 1) - 1); + const room = ends[1] - ends[0]; + var gap: f32 = 44; + if (rest) |r| heights[r] = @max(room - fixed - gap * gaps, m.one) else gap = @max((room - fixed) / @max(gaps, 1), m.gap_min); + var y = ends[0]; + for (0..n) |i| { + out.boxes[i] = .{ .x = g.x + m.box, .y = y, .w = m.box_w, .h = heights[i] }; + y += heights[i] + gap; + // The lines on the whole: the top clusters 2 apart under its top, the foot's over its foot. + out.lines[i] = if (space.clusters[i].lo > user_end / 2) ends[0] + 2 + 2 * @as(f32, @floatFromInt(i)) else ends[1] - 4 - 2 * @as(f32, @floatFromInt(n - 1 - i)); + } + return out; + } + + /// Where `addr` is in its cluster's box once it has landed; null outside every box. + pub fn place(l: Layout, addr: u64) ?Vec { + for (l.space.clusters, 0..) |c, i| if (i < 6 and addr >= c.lo and addr < c.hi) { + const b = l.boxes[i]; + return .{ b.x + b.w / 2, b.y + b.h * @as(f32, @floatCast(@as(f64, @floatFromInt(c.hi - addr)) / @as(f64, @floatFromInt(c.hi - c.lo)))) }; + }; + return null; + } + + pub fn draw(g: *const Segments, f: *sk.Frame, placed: anim.Placed) void { + const u: ui.Ui = .{ .f = f, .at = placed }; + const l = g.layout(placed.value(g.form), f.scratch); + const whole = placed.value(g.whole); + if (whole < 0) return; + const was = clipStage(f, placed); + defer unclipStage(f, was); + const bar_x = g.x + m.bar; + const h = l.foot - l.top; + // The bar, from the top down (72 f), framed 2 in `dim`, coming in over its first 6 f. + const grown = h * theme.ease(.standard, std.math.clamp(whole / 72, 0, 1)); + u.faded(std.math.clamp(whole / 6, 0, 1)).frame(.{ .x = bar_x, .y = l.top, .w = m.bar_w, .h = @max(grown, 4) }, t.dim, theme.stroke.rule); + // Its ends' addresses and its size (18 f), ranged right 12 left of it. + const ends_in = std.math.clamp((whole - 72) / 18, 0, 1); + if (ends_in > 0) { + const v = u.faded(ends_in); + var buf: [16]u8 = undefined; + for ([_]struct { []const u8, f32 }{ .{ "7fffffffffff", l.top }, .{ "0", l.foot }, .{ size(&buf, user_end), (l.top + l.foot) / 2 } }) |row| + v.text(.mono, m.mono, bar_x - 12 - v.width(.mono, m.mono, row[0]), row[1] + 7.2, row[0], t.dim); + } + // Every mapping's line inside its end (12 f each, across 72 f). + const n = @min(l.space.clusters.len, 6); + for (0..n) |i| { + const shows = std.math.clamp((whole - 90 - 60 * @as(f32, @floatFromInt(i)) / @as(f32, @floatFromInt(@max(n, 2) - 1))) / 12, 0, 1); + if (shows > 0) u.faded(shows).fill(.{ .x = bar_x + 2, .y = l.lines[i], .w = m.bar_w - 4, .h = 2 }, biggest(l.space, l.space.clusters[i]).ink); + } + for (0..n) |i| g.enlarged(u, l, i, placed.value(g.rides[i])); + for (g.echoes[0..g.n_echoes]) |echo| g.echoed(u, l, echo.addr, placed.value(echo.shows)); + } + + /// The ink of a cluster's largest band (its line on the whole). + fn biggest(space: Space, c: Cluster) Band { + var best = space.bands[c.bands[0]]; + for (space.bands[c.bands[0]..c.bands[1]]) |b| if (b.hi - b.lo > best.hi - best.lo) { + best = b; + }; + return best; + } + + /// Cluster `i`'s `enlarge`, `clock` frames in. + fn enlarged(g: *const Segments, line_u: ui.Ui, l: Layout, i: usize, clock: f32) void { + if (clock < 0) return; + const c = l.space.clusters[i]; + const land = l.boxes[i]; + const bar_r = g.x + m.bar + m.bar_w; + const line_y = l.lines[i] + 1; + // The line framed bright (2 units, 3 out), going as the box lands. + const bright = std.math.clamp(clock / e.frame, 0, 1) * (1 - std.math.clamp((clock - 66) / 12, 0, 1)); + if (bright > 0) line_u.faded(bright).frame(.{ .x = g.x + m.bar - 1, .y = line_y - 5, .w = m.bar_w + 2, .h = 10 }, t.bright, theme.stroke.rule); + // The ride: the box's left corners along the magnification's lines. + const p = theme.ease(.glide, std.math.clamp((clock - e.frame) / e.ride, 0, 1)); + if (p <= 0) return; + const box: Rect = .{ + .x = std.math.lerp(bar_r, land.x, p), + .y = std.math.lerp(line_y, land.y, p), + .w = std.math.lerp(m.bar_w - 4, land.w, p), + .h = std.math.lerp(0, land.h, p), + }; + // Out of the line: the box comes in over the ride's first frames, never in one. + const u = line_u.faded(std.math.clamp(p * 8, 0, 1)); + u.line(.{ bar_r, line_y }, .{ box.x - 2, box.y - 1 }, theme.color.rule, theme.stroke.rule); + u.line(.{ bar_r, line_y }, .{ box.x - 2, box.y + box.h + 1 }, theme.color.rule, theme.stroke.rule); + // Its bands at its scale all the way: a mapping a 2-unit line until it is taller. + const scale = box.h / @as(f32, @floatFromInt(c.hi - c.lo)); + const pages = std.math.clamp((clock - e.frame - e.ride) / e.pages, 0, 1); + u.clip(.{ .x = box.x, .y = box.y - 1, .w = box.w, .h = box.h + 2 }); + for (l.space.bands[c.bands[0]..c.bands[1]], c.bands[0]..) |b, k| { + const top = box.y + @as(f32, @floatFromInt(c.hi - b.hi)) * scale; + const bh = @max(@as(f32, @floatFromInt(b.hi - b.lo)) * scale, 2); + u.fill(.{ .x = box.x, .y = top, .w = box.w, .h = bh }, ui.mix(theme.color.page, b.ink, 0.3)); + // Between two bands of one file a page line, where both are 6 tall. + if (k + 1 < c.bands[1] and pages > 0) { + const below = l.space.bands[k + 1]; + if (below.hi == b.lo and std.mem.eql(u8, below.name, b.name) and b.name.len > 0 and bh >= 6 and @as(f32, @floatFromInt(below.hi - below.lo)) * scale >= 6) + u.faded(pages).fill(.{ .x = box.x, .y = top + bh - 1, .w = box.w, .h = 2 }, theme.color.page); + } + } + u.unclip(); + u.frame(.{ .x = box.x - 2, .y = box.y - 2, .w = box.w + 4, .h = box.h + 4 }, t.dim, theme.stroke.rule); + // Its names, rows top down, once it has landed. + g.names(u, l, i, clock - e.names, scale); + } + + const Row = struct { kind: enum { edge, unit }, addr: u64, unit: usize, target: f32, ink: u24 }; + + /// Cluster `i`'s rows of names: an edge of every named unit, each unit + /// between its edges, top down (highest address first). + fn rows(l: Layout, i: usize, arena: std.mem.Allocator) []Row { + const c = l.space.clusters[i]; + const box = l.boxes[i]; + const scale = box.h / @as(f32, @floatFromInt(c.hi - c.lo)); + var out: std.ArrayList(Row) = .empty; + var k = c.units[1]; + while (k > c.units[0]) { + k -= 1; + const un = l.space.units[k]; + const tall = @as(f32, @floatFromInt(un.hi - un.lo)) * scale; + if (!un.program and tall < 6) continue; + const yh = box.y + @as(f32, @floatFromInt(c.hi - un.hi)) * scale; + const yl = box.y + @as(f32, @floatFromInt(c.hi - un.lo)) * scale; + const last = if (out.items.len > 0) out.items[out.items.len - 1] else null; + if (last == null or last.?.kind != .edge or last.?.addr != un.hi) out.append(arena, .{ .kind = .edge, .addr = un.hi, .unit = k, .target = yh, .ink = un.ink }) catch {}; + out.append(arena, .{ .kind = .unit, .addr = un.lo, .unit = k, .target = std.math.clamp((yh + yl) / 2, box.y, box.y + box.h), .ink = un.ink }) catch {}; + out.append(arena, .{ .kind = .edge, .addr = un.lo, .unit = k, .target = yl, .ink = un.ink }) catch {}; + } + // An edge two units share takes the ink of the one that starts there. + for (out.items, 0..) |*r, j| if (r.kind == .edge and j + 1 < out.items.len and out.items[j + 1].kind == .unit) { + r.ink = l.space.units[out.items[j + 1].unit].ink; + }; + return out.items; + } + + /// Where cluster `i`'s rows may stand: its share of the column, down to the gaps' middles. + fn share(l: Layout, i: usize) [2]f32 { + const n = @min(l.space.clusters.len, 6); + const lo = if (i == 0) l.top else (l.boxes[i - 1].y + l.boxes[i - 1].h + l.boxes[i].y) / 2 + 12; + const hi = if (i + 1 == n) l.foot + 12 else (l.boxes[i].y + l.boxes[i].h + l.boxes[i + 1].y) / 2 - 12; + return .{ lo, hi }; + } + + fn names(g: *const Segments, u: ui.Ui, l: Layout, i: usize, clock: f32, scale: f32) void { + if (clock < 0) return; + const all = rows(l, i, u.f.scratch); + if (all.len == 0) return; + var targets: [64]f32 = undefined; + var ys: [64]f32 = undefined; + const n = @min(all.len, 64); + for (all[0..n], 0..) |r, k| targets[k] = r.target; + const room = share(l, i); + pool(targets[0..n], ys[0..n], room[0], room[1], m.pitch); + const box = l.boxes[i]; + const right = box.x + box.w + 2; + const ax = g.x + m.address; + var buf: [32]u8 = undefined; + var first_program: ?usize = null; + var last_program: usize = 0; + for (all[0..n], 0..) |r, k| { + const at = clock - @as(f32, @floatFromInt(k)) * e.apart; + if (at <= 0) break; + const y = ys[k]; + const un = l.space.units[r.unit]; + switch (r.kind) { + .edge => { + const drawn = std.math.clamp(at / e.leader, 0, 1); + u.route(&.{ .{ right, r.target }, .{ right + m.leader, r.target }, .{ ax - 20, y }, .{ ax - 8, y } }, theme.color.rule, .{ .width = theme.stroke.rule, .head = null, .progress = drawn, .radius = 0 }); + const shows = std.math.clamp((at - e.leader) / e.fade, 0, 1); + if (shows > 0) _ = u.faded(shows).address(ax, y + 7.2, r.addr, .{ .ink = ui.rgb(r.ink) }); + }, + .unit => { + const v = u.faded(std.math.clamp(at / e.fade, 0, 1)); + if (un.prot) |p| v.text(.mono, m.mono, ax + m.indent, y + 7.2, &perms(p), t.dim); + const sz = size(&buf, un.hi - un.lo); + v.text(.mono, m.mono, g.x + m.size_end - v.width(.mono, m.mono, sz), y + 7.2, sz, t.dim); + if (un.program) { + if (first_program == null) first_program = k; + last_program = k; + } else v.text(.mono, m.mono, g.x + m.name, y + 7.2, un.name, t.primary); + // A file whose permissions differ: each part's written inside its band, where it is 28 tall. + if (un.prot == null) g.insides(v, l, i, un, scale); + }, + } + } + // The program's bracket and name, with its last row. + if (first_program) |a| { + const at = clock - @as(f32, @floatFromInt(last_program)) * e.apart; + const v = u.faded(std.math.clamp(at / e.fade, 0, 1)); + const bx = g.x + m.name - 14; + v.bracket(bx, ys[a] - 8, ys[last_program] + 8, theme.color.rule, theme.metric.bracket.quiet, -1, false); + v.text(.mono, m.mono, g.x + m.name, (ys[a] + ys[last_program]) / 2 + 7.2, l.space.program, t.primary); + } + } + + fn insides(_: *const Segments, v: ui.Ui, l: Layout, i: usize, un: Unit, scale: f32) void { + const c = l.space.clusters[i]; + const box = l.boxes[i]; + for (l.space.bands[c.bands[0]..c.bands[1]]) |b| if (b.lo >= un.lo and b.hi <= un.hi) { + const bh = @as(f32, @floatFromInt(b.hi - b.lo)) * scale; + if (bh < 28) continue; + const top = box.y + @as(f32, @floatFromInt(c.hi - b.hi)) * scale; + v.label(.mono, m.mono, .{ .x = box.x, .y = top, .w = box.w, .h = bh }, .center, &perms(b.prot), t.primary); + }; + } + + /// `ink`'s echo: the band holding `addr` framed as read inside its box + /// (2 units, 4 in from its sides, 6 over and under), its row of names with it. + fn echoed(g: *const Segments, u: ui.Ui, l: Layout, addr: u64, shows: f32) void { + if (shows <= 0) return; + const v = u.faded(shows); + const read_ink = theme.mark(.read).solid; + for (l.space.clusters, 0..) |c, i| if (i < 6 and addr >= c.lo and addr < c.hi) { + const box = l.boxes[i]; + const scale = box.h / @as(f32, @floatFromInt(c.hi - c.lo)); + for (l.space.bands[c.bands[0]..c.bands[1]]) |b| if (addr >= b.lo and addr < b.hi) { + const top = box.y + @as(f32, @floatFromInt(c.hi - b.hi)) * scale; + const bh = @max(@as(f32, @floatFromInt(b.hi - b.lo)) * scale, 2); + v.frame(.{ .x = box.x + 4, .y = top - 6, .w = box.w - 8, .h = bh + 12 }, read_ink, theme.stroke.rule); + }; + // Its row of names: the unit holding it. + const all = rows(l, i, u.f.scratch); + var targets: [64]f32 = undefined; + var ys: [64]f32 = undefined; + const n = @min(all.len, 64); + for (all[0..n], 0..) |r, k| targets[k] = r.target; + const room = share(l, i); + pool(targets[0..n], ys[0..n], room[0], room[1], m.pitch); + for (all[0..n], 0..) |r, k| if (r.kind == .unit) { + const un = l.space.units[r.unit]; + if (addr < un.lo or addr >= un.hi) continue; + const x0 = g.x + m.address + m.indent - 6; + v.frame(.{ .x = x0, .y = ys[k] - 12, .w = g.x + m.size_end + 6 - x0, .h = 24 }, read_ink, theme.stroke.rule); + }; + }; + } +}; + +/// A dump's place (design/README.md, `segments`, "a dump's place"): the +/// `tie` bracket on its rows in the stack's ink (6 f), then the `link` +/// from the bracket's top up into the stage's top margin (y 56), across, +/// and down onto the stack's box at the dump's address, head first (36 f, +/// `standard`); the stack's band framed bright as it lands (6 f in, 12 +/// out); then both quiet to standing (24 f, 60% toward the page). +pub const Place = struct { + segments: *const Segments, + addr: u64, + /// The bracket: its x and its rows' top and foot. + x: f32, + y0: f32, + y1: f32, + /// Frames into the tie (negative: not begun); frames into its quieting. + clock: anim.Value, + quiet: anim.Value, + + pub fn draw(p: *const Place, f: *sk.Frame, placed: anim.Placed) void { + const clock = placed.value(p.clock); + if (clock < 0) return; + const was = clipStage(f, placed); + defer unclipStage(f, was); + const u: ui.Ui = .{ .f = f, .at = placed }; + const k = 1 - (1 - theme.metric.arc.standing) * theme.ease(.glide, std.math.clamp(placed.value(p.quiet) / 24, 0, 1)); + const ink = ui.mix(theme.color.page, region.stack, k); + const v = u.faded(std.math.clamp(clock / 6, 0, 1)); + v.bracket(p.x, p.y0, p.y1, ink, theme.metric.bracket.tie, 1, false); + const l = p.segments.layout(placed.value(p.segments.form), f.scratch); + const to = Segments.place(l, p.addr) orelse return; + const top: f32 = 56; + const drawn = theme.ease(.standard, std.math.clamp((clock - 6) / 36, 0, 1)); + u.route(&.{ .{ p.x, p.y0 }, .{ p.x, top }, .{ to[0], top }, .{ to[0], to[1] } }, ink, .{ .width = theme.stroke.tie.link, .head = .{ 10, 10 }, .progress = drawn, .radius = 8 }); + // The stack's band framed bright as the head lands. + const bright = std.math.clamp((clock - 42) / 6, 0, 1) * (1 - std.math.clamp((clock - 48) / 12, 0, 1)); + if (bright > 0) for (l.space.clusters, 0..) |c, i| if (i < 6 and p.addr >= c.lo and p.addr < c.hi) { + const b = l.boxes[i]; + u.faded(bright).frame(.{ .x = b.x - 5, .y = b.y - 5, .w = b.w + 10, .h = b.h + 10 }, t.bright, theme.stroke.line); + }; + } +}; + +test "sizes as the names give them" { + var buf: [16]u8 = undefined; + try std.testing.expectEqualStrings("4 KiB", size(&buf, 4096)); + try std.testing.expectEqualStrings("132 KiB", size(&buf, 0x21000)); + try std.testing.expectEqualStrings("2.1 MiB", size(&buf, 0x223000)); + try std.testing.expectEqualStrings("64 MiB", size(&buf, 0x4002000)); + try std.testing.expectEqualStrings("128 TiB", size(&buf, user_end)); +} + +test "rows pooled round their mean, in order, inside their room" { + var ys: [4]f32 = undefined; + pool(&.{ 100, 100, 100, 300 }, &ys, 0, 1000, 24); + try std.testing.expectEqual(@as(f32, 76), ys[0]); + try std.testing.expectEqual(@as(f32, 124), ys[2]); + try std.testing.expectEqual(@as(f32, 300), ys[3]); + pool(&.{ 0, 0, 0, 0 }, &ys, 50, 1000, 24); + try std.testing.expectEqual(@as(f32, 50), ys[0]); + try std.testing.expectEqual(@as(f32, 122), ys[3]); +} diff --git a/anim/src/source.zig b/anim/src/source.zig index ab49bc0..aedbb3e 100644 --- a/anim/src/source.zig +++ b/anim/src/source.zig @@ -34,6 +34,8 @@ pub const Source = struct { descent: bool = false, /// Which box of its stage it is in: on the map the source may go on in `source_more`. part: @import("stage.zig").Part = .source, + /// Words after the file's name in its tag (a `pair`'s shared source: `both builds`). + tag: []const u8 = "", pub fn draw(p: *const Source, f: *sk.Frame, placed: anim.Placed) void { const box = p.stage.box(placed, p.part); @@ -56,7 +58,13 @@ pub const Source = struct { // The window: where the line it is on has it, and (as the mark // moves on) gliding to where the next one does. It never cuts. const going = now.advancing(); - const windows = [2]f32{ @as(f32, @floatFromInt(rec.shownFirst(span_fit, at[0]))) + skip, @as(f32, @floatFromInt(rec.shownFirst(span_fit, at[1]))) + skip }; + var windows = [2]f32{ @as(f32, @floatFromInt(rec.shownFirst(span_fit, at[0]))) + skip, @as(f32, @floatFromInt(rec.shownFirst(span_fit, at[1]))) + skip }; + // The panel growing or shrinking at rest (a column's change): its window glides + // from the one its old height has to its new one's, never a jump. + if (now.resting()) if (p.stage.resizing(placed, p.part)) |r| { + const ends = [2]f32{ @floatFromInt(rec.shownFirst(@intCast(theme.layout.rowsIn(r.from) + tail), at[1])), @floatFromInt(rec.shownFirst(@intCast(theme.layout.rowsIn(r.to) + tail), at[1])) }; + windows = @splat(std.math.lerp(ends[0], ends[1], r.p) + skip); + }; // Another file: the panel fades through (12 f out, 12 f in), its tag // naming the file it comes to. Farther than its own height in one // file (another function far off): its strip shows where it goes @@ -80,7 +88,7 @@ pub const Source = struct { const side: usize = if (jump) |j| @intFromBool(j.travel >= 0.5) else if (turn) @intFromBool(going >= 0.5) else if (far) @intFromBool(if (ui.mapping) going > 0 else going >= 0.75) else @intFromBool(going >= 0.5); const file = rec.sources[at[side].src]; const text = prepared(file.text, file.path) orelse return; - const first = if (jump) |j| (if (ui.mapping) std.math.lerp(windows[0], windows[1], cursors.glide(j.travel)) else windows[side]) else if (turn or far) windows[side] else std.math.lerp(windows[0], windows[1], cursors.glide(going)); + const first = if (jump) |j| (if (ui.mapping) std.math.lerp(windows[0], windows[1], cursors.glide(j.travel)) else windows[side]) else if (turn or far) windows[side] else std.math.lerp(windows[0], windows[1], cursors.glidePixels(going, now.plan.travel, @abs(windows[1] - windows[0]) * m.row * u.at.scale)); const fade: f32 = if (jump) |j| (if (ui.mapping) 1 else 1 - theme.ease(.glide, j.grow)) else if (turn) @abs(2 * going - 1) else if (far and !ui.mapping) (if (going < 0.5) 1 else @abs(4 * going - 3)) else 1; const lit = u.faded(fade); const from: u32 = @intFromFloat(@floor(first)); @@ -99,7 +107,7 @@ pub const Source = struct { for (now.marks, at, ¬es, lines_notes) |mk, a, *note, lines_note| note.* = if (p.descent and !a.here and a.line != 0) u.print("{s} waits at line {d} \u{00b7} the pc is in {s}", .{ context.frameName(u, rec, rec.register(lastShown(rec, mk), "rip")), a.line, whereIs(u, rec, mk) }) else lines_note; const swap = !std.mem.eql(u8, notes[0], notes[1]) and now.walking(); const titles = [2][]const u8{ rec.sources[at[0].src].name(), rec.sources[at[1].src].name() }; - const title = if (p.part == .source_more) u.print("{s}, continued", .{file.name()}) else file.name(); + const title = if (p.part == .source_more) u.print("{s}, continued", .{file.name()}) else if (p.tag.len > 0) u.print("{s} \u{00b7} {s}", .{ file.name(), p.tag }) else file.name(); const body = u.panel(box.w, box.h, if (turn) "" else title, .{ .focus = p.focus, .note = if (swap) "" else notes[side] }); // Its tag's file and note change with the fade through it; a note otherwise over the 12 f before the advance. if (turn) for (titles, [2]f32{ 1 - 2 * going, 2 * going - 1 }) |t, shows| u.faded(shows).tagTitle(t, p.focus); @@ -162,7 +170,7 @@ pub const Source = struct { if (waits) { // The number ringed: a 2-unit primary frame where the plaque would be, no wash. u.faded(shows).frame(.{ .x = theme.stroke.rule + 1, .y = mark_y + 1, .w = theme.metric.plaque.line + widen - 2, .h = m.row - 2 }, theme.color.text.primary, theme.stroke.rule); - if (!ui.mapping) ties.thread_waits = true; + if (!ui.mapping) ties.of(u.f).thread_waits = true; } else if (shows > 0) u.faded(shows).current(.{ .x = theme.stroke.rule, .y = mark_y, .w = box.w - 2 * theme.stroke.rule, .h = m.row }, theme.metric.plaque.line + widen, now.fires(.source)); if (shows >= 1 and mark_y >= body.y and mark_y + m.row <= box.h) ties.anchor(u, .source, .{ .x = 0, .y = mark_y, .w = box.w, .h = m.row }); diff --git a/anim/src/stage.zig b/anim/src/stage.zig index bbf5887..7b18064 100644 --- a/anim/src/stage.zig +++ b/anim/src/stage.zig @@ -102,6 +102,20 @@ pub const Stage = struct { return false; } + /// A move under way that changes `part`'s height: its height before and + /// after, and how far it has got (eased as the boxes are). Null otherwise. + pub fn resizing(st: *const Stage, at: anim.Placed, part: Part) ?struct { from: f32, to: f32, p: f32 } { + for (st.moves[0..st.count]) |m| { + const p = at.value(m.p); + if (p <= 0 or p >= 1) continue; + const a, const b = switch (part) { + inline else => |tag| .{ @field(m.from, @tagName(tag)).h, @field(m.to, @tagName(tag)).h }, + }; + if (a != b) return .{ .from = a, .to = b, .p = theme.ease(.glide, p) }; + } + return null; + } + pub fn box(st: *const Stage, at: anim.Placed, part: Part) Rect { const split = st.now(at); return switch (part) { diff --git a/anim/src/ties.zig b/anim/src/ties.zig index 1b6a6ed..b5214e3 100644 --- a/anim/src/ties.zig +++ b/anim/src/ties.zig @@ -1,8 +1,15 @@ //! Ties (design/README.md, "4. Ties"): the panes are one machine at //! different levels, and a tie says which thing in one pane a thing in -//! another is. Panels publish the `place`s of their things as they draw -//! (`anchor`); `Ties`, added after them, draws between places, along the -//! channels only, never across a letter or another tie: +//! another is. Every tie is a relation, and the relations are one data +//! problem (`relations.zig`): per frame, a table of the places the panels +//! drew their things at (`anchor`) and of the values they show that could +//! be addresses (`show`), both in the frame's own scratch (`sk.Frame.user`, +//! `of`); `Ties`, added after them, is the resolver: six passes over those +//! tables, each a loop over one (`Ties.draw`): candidates (a join of values +//! with places by address, or the step's own events against places), the +//! knobs (off unless the scene turned the kind on, where its default is +//! off), priority, the budget (the thread always; two active lines across +//! panes, the touch giving way first; one standing), route, draw. The kinds: //! //! - `thread` (standing): the source's current line is these //! instructions. Gold, 3: a bracket on the line, one on each run of its @@ -15,9 +22,10 @@ //! drawing. In the mark's colour, 2 wide: a bracket on the dump's row, //! a line down the gutter's right lane and along the lens's top lane, //! a bracket over the node's address. +//! - `touch`, `carry` (under `tied`; the carry the scene's knob), and +//! `aim` (the scene's knob, off by default): a word holding the address +//! of an instruction whose row is on screen, tied to that row. //! - `echo`: the same mark at the same instant in two panels; no line. -//! -//! Budget: one thread; for a step one link and one projection. const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); @@ -27,9 +35,11 @@ const cursors = @import("cursor.zig"); const rows = @import("rows.zig"); const Recording = @import("Recording.zig"); const Stage = @import("stage.zig").Stage; +const relations = @import("relations.zig"); const theme = ui.theme; const time = theme.time; const Rect = ui.Rect; +pub const Pane = relations.Pane; /// A thing with a place on screen. pub const Place = union(enum) { @@ -64,59 +74,106 @@ pub const Place = union(enum) { floor, block: u64, - fn key(p: Place) u64 { - const low = (1 << 56) - 1; + /// Its kind in the key's top byte (`relations.keyOf`), its address or index below. + pub fn key(p: Place) u64 { + return switch (p) { + .register => |i| relations.keyOf(1, i), + .registers => relations.keyOf(2, 0), + .word => |a| relations.keyOf(3, a), + .slot_pin => |a| relations.keyOf(4, a), + .node => |a| relations.keyOf(5, a), + .node_address => |a| relations.keyOf(6, a), + .node_pin => |a| relations.keyOf(7, a), + .node_part => |a| relations.keyOf(8, a), + .dump_run => |a| relations.keyOf(9, a), + .source => relations.keyOf(10, 0), + .pc => relations.keyOf(11, 0), + .run => |k| relations.keyOf(12, k), + .dump_pin => |a| relations.keyOf(13, a), + .row => |a| relations.keyOf(14, a), + .floor => relations.keyOf(15, 0), + .block => |a| relations.keyOf(16, a), + }; + } + + /// The pane it is drawn in. + pub fn pane(p: Place) Pane { return switch (p) { - .register => |i| 1 << 56 | @as(u64, i), - .registers => 2 << 56, - .word => |a| 3 << 56 | (a & low), - .slot_pin => |a| 4 << 56 | (a & low), - .node => |a| 5 << 56 | (a & low), - .node_address => |a| 6 << 56 | (a & low), - .node_pin => |a| 7 << 56 | (a & low), - .node_part => |a| 8 << 56 | (a & low), - .dump_run => |a| 9 << 56 | (a & low), - .source => 10 << 56, - .pc => 11 << 56, - .run => |k| 12 << 56 | @as(u64, k), - .dump_pin => |a| 13 << 56 | (a & low), - .row => |a| 14 << 56 | (a & low), - .floor => 15 << 56, - .block => |a| 16 << 56 | (a & low), + .register, .registers => .registers, + .word, .slot_pin => .stack, + .node, .node_address, .node_pin, .node_part, .block => .drawing, + .dump_run, .dump_pin => .view, + .source => .source, + .pc, .run, .row, .floor => .disasm, }; } }; -/// The stack's window this frame: its first word, how many it shows, and -/// where its pin lane runs (canvas pixels: x, the first row's top, the -/// row's height). A link to a word outside it ends at its edge. -pub var stack_window: ?struct { first: u64, slots: usize, x: f32, top: f32, row: f32 } = null; -/// The disassembly's row the step's `touch` and `carry` leave from or reach, -/// this frame (canvas pixels: its right rule's x, the row's top, that x, its -/// bottom), where it is drawn (`Disasm.insn`). -pub var touch_row: ?[4]f32 = null; -/// The source's line waits (a `descent`: the pc is in code without source): -/// the thread waits with it, primary and 2 units. -pub var thread_waits = false; +/// The kinds of place keys, by number (`Place.key`). +const key_kind = struct { + const node_address = 6; + const node_pin = 7; + const slot_pin = 4; + const dump_pin = 13; + const run = 12; + const row = 14; +}; -/// What this frame's components have said so far, in canvas pixels: left, top, right, bottom. -/// shortcut: one table for the process, emptied by `Ties.draw`; a scene with two players at once wants it on the frame. -var table: [512]struct { key: u64, box: [4]f32 } = undefined; -var said: usize = 0; +/// A frame's relations: its tables (the working frame's, and the map's for +/// what the map's panels say) and the facts the panels publish beside them. +pub const State = struct { + work: relations.Tables = .{}, + map: relations.Tables = .{}, + /// The stack's window this frame: its first word, how many it shows, and + /// where its pin lane runs (canvas pixels: x, the first row's top, the + /// row's height). A link to a word outside it ends at its edge. + stack_window: ?struct { first: u64, slots: usize, x: f32, top: f32, row: f32 } = null, + /// The disassembly's row the step's `touch` and `carry` leave from or reach, + /// this frame (canvas pixels: its right rule's x, the row's top, that x, its + /// bottom), where it is drawn (`Disasm.insn`). + touch_row: ?[4]f32 = null, + /// The source's line waits (a `descent`: the pc is in code without source): + /// the thread waits with it, primary and 2 units. + thread_waits: bool = false, + stats: relations.Stats = .{}, +}; -/// `r` (in `u`'s units) is where `place` is, this frame. +/// The frame's relations, made in its scratch at the first place said. +pub fn of(f: *sk.Frame) *State { + if (f.user) |p| return @ptrCast(@alignCast(p)); + const st = f.scratch.create(State) catch @panic("ties: out of scratch memory"); + st.* = .{}; + f.user = st; + return st; +} + +/// `r` (in `u`'s units) is where `place` is, this frame. On the map (not +/// its strips) into the map's table. pub fn anchor(u: ui.Ui, place: Place, r: Rect) void { - if (said == table.len or ui.mapping) return; + if (ui.mapping and ui.strokes) return; const a = u.at.xf.apply(.{ r.x, r.y }); const b = u.at.xf.apply(.{ r.x + r.w, r.y + r.h }); - table[said] = .{ .key = place.key(), .box = .{ a[0], a[1], b[0], b[1] } }; - said += 1; + const st = of(u.f); + (if (ui.mapping) &st.map else &st.work).add(u.f.scratch, .{ .key = place.key(), .box = .{ a[0], a[1], b[0], b[1] }, .pane = place.pane() }); +} + +/// `value`, which may be an address, is shown at `r` (in `u`'s units) in +/// `pane`, a panel `panel_w` wide from `u`'s origin: `at` the address of +/// the word holding it (0: none), `ref` a register's index. +pub fn show(u: ui.Ui, value: u64, r: Rect, pane: Pane, at: u64, ref: u32, panel_w: f32) void { + if (ui.mapping and ui.strokes) return; + const a = u.at.xf.apply(.{ r.x, r.y }); + const b = u.at.xf.apply(.{ r.x + r.w, r.y + r.h }); + // Its panel's rules: a tie leaves by the one nearer its lane. + const rules = [2]f32{ u.at.xf.apply(.{ 0, 0 })[0], u.at.xf.apply(.{ panel_w, 0 })[0] }; + const st = of(u.f); + (if (ui.mapping) &st.map else &st.work).show(u.f.scratch, .{ .value = value, .box = .{ a[0], a[1], b[0], b[1] }, .pane = pane, .ref = ref, .at = at, .rules = rules }); } /// Where `place` is this frame, in canvas pixels (left, top, right, bottom), if a panel said. -pub fn find(place: Place) ?[4]f32 { - for (table[0..said]) |entry| if (entry.key == place.key()) return entry.box; - return null; +pub fn find(f: *sk.Frame, place: Place) ?[4]f32 { + const st = of(f); + return st.work.find(f.scratch, place.key()); } /// When register `i` strikes in this step: the registers one step wrote @@ -193,6 +250,34 @@ pub fn riseTime(n: usize) f32 { return @as(f32, @floatFromInt(n -| 1)) * apart + time.quick; } +/// What a candidate ties, found in the tables: the boxes (canvas pixels) +/// its kind's route and drawing read, and what it is of. +const Line = struct { + kind: relations.Kind, + /// Its source and target, and a third place its route needs (the + /// registers panel's edge, a slot's pin lane, a node's address). + from: [4]f32 = @splat(0), + to: [4]f32 = @splat(0), + third: [4]f32 = @splat(0), + value: u64 = 0, + /// The register a link or a beat leaves from. + name: []const u8 = "", + /// A link's target: a node's pin, a view's row, a slot's pin, or past the stack's window `words` away. + target: enum { node, row, slot, beyond } = .slot, + words: i64 = 0, + /// A projection: from the stack to a call's node (else from the view to a node's part); a write (else a read). + stacked: bool = false, + write: bool = false, + /// The word a projection, a touch or an aim is of; an aim's value before the step. + word: u64 = 0, + was: u64 = 0, + /// An aim's lanes into its word's panel: its panel's rules. + rules: [2]f32 = .{ 0, 0 }, +}; + +/// The most candidates a frame keeps: a few of each kind. +const max_lines = 64; + pub const Ties = struct { cursor: cursors.Cursor, stage: *const Stage, @@ -204,11 +289,11 @@ pub const Ties = struct { /// A link the scene plays on a beat of its own (`Context.tiesWith`). beat: ?*const Beat = null, + /// The resolver: six passes over the frame's tables, each a loop over one. pub fn draw(g: *const Ties, f: *sk.Frame, at: anim.Placed) void { - defer said = 0; - defer stack_window = null; - defer thread_waits = false; - defer touch_row = null; + const st = of(f); + const began = relations.clock(); + defer report(f, st, began); const now = g.cursor.now(at); // Diving or surfacing, the panels are on their way: no ties; nor while the map alone is looked at. for (@import("context.zig").zoomed) |z| if (z != null) return; @@ -216,16 +301,255 @@ pub const Ties = struct { // In canvas pixels, `k` of them a unit: places come from panels anywhere. var raw: ui.Ui = .{ .f = f, .at = at }; raw.at.xf = .identity; - const k = at.scale; - const split = g.stage.now(at); - if (g.threaded) thread(raw.faded(@import("context.zig").thread_shows), at, k); - if (g.beat) |b| named(raw, at, now, k, split, b); + const cx: Cx = .{ .g = g, .f = f, .st = st, .raw = raw, .at = at, .now = now, .k = at.scale, .split = g.stage.now(at) }; + // 1. Candidates: each kind's join, or the step's events against the places. + var lines: [max_lines]Line = undefined; + var cands: [max_lines]relations.Candidate = undefined; + var n: usize = 0; + cx.candidates(&lines, &cands, &n); + const found = relations.clock(); + // 2. Knobs: a kind whose default is off only where the scene turned it on. + var knobs = relations.Knobs.initOne(.always); + if (g.threaded) knobs.insert(.thread); + if (!g.quiet) knobs.insert(.links); + if (now.plan.carry >= 0) knobs.insert(.carry); + if (now.aim > 0) knobs.insert(.code); + var kept = relations.filter(cands[0..n], knobs); + // 3. Priority, then 4. the budget: it only removes. + relations.byPriority(kept); + kept = relations.cut(kept); + const passed = relations.clock(); + // 5. Route and 6. draw, in priority order: each kind's verb at its frames. + for (kept) |c| cx.drawLine(lines[c.from]); + st.stats.candidates = n; + st.stats.kept = kept.len; + st.stats.ns.candidates = found - began; + st.stats.ns.passes = passed - found; + st.stats.ns.draw = relations.clock() - passed; + } +}; + +/// A frame's resolver at work: the frame, its tables, the step. +const Cx = struct { + g: *const Ties, + f: *sk.Frame, + st: *State, + raw: ui.Ui, + at: anim.Placed, + now: cursors.Now, + k: f32, + split: theme.layout.Split, + + fn find(cx: Cx, place: Place) ?[4]f32 { + return cx.st.work.find(cx.f.scratch, place.key()); + } + + /// The node a field's address belongs to: the drawn node at or below it, within a record. + fn nodeOf(cx: Cx, addr: u64) ?u64 { + return cx.st.work.atOrBelow(cx.f.scratch, key_kind.node_address, addr, 64); + } + + /// Pass 1: every kind's candidates, into `lines` and `cands` (`n` of them). + fn candidates(cx: Cx, lines: *[max_lines]Line, cands: *[max_lines]relations.Candidate, n: *usize) void { + const now = cx.now; + const add = struct { + fn one(ls: *[max_lines]Line, cs: *[max_lines]relations.Candidate, at: *usize, line: Line, rank: u32) void { + if (at.* == max_lines) return; + ls[at.*] = line; + cs[at.*] = .{ .kind = line.kind, .rank = rank, .from = @intCast(at.*) }; + at.* += 1; + } + }.one; + if (cx.find(.source)) |line| add(lines, cands, n, .{ .kind = .thread, .from = line }, 0); + if (cx.g.beat) |b| { + const t = cx.at.value(b.t); + if (t >= 0 and t < b.total()) add(lines, cands, n, .{ .kind = .beat }, 0); + } + // Standing aims, at rest as in a step. + if (now.aim > 0) cx.aims(lines, cands, n, add); if (!now.walking()) return; - const linked = !g.quiet and g.link(raw, at, now, k, split); - const projected = g.projection(raw, at, now, k, split); - // Three lines at most: the touch is the first to give way (to an echo), where a step draws a link and a projection. - if (!(linked and projected)) g.touch(raw, at, now, k, split); - g.carry(raw, at, now, k); + cx.links(lines, cands, n, add); + cx.projections(lines, cands, n, add); + if (cx.touchLine()) |line| add(lines, cands, n, line, 0); + if (now.plan.carry >= 0) add(lines, cands, n, .{ .kind = .carry }, 0); + } + + /// `link`'s join: the registers' values the step struck ⋈ the places + /// with a pin lane at their address (a node's pin, a slot's, a view's + /// row), or past the stack's window; ranked in pwndbg's order. + fn links(cx: Cx, lines: *[max_lines]Line, cands: *[max_lines]relations.Candidate, n: *usize, add: anytype) void { + const t = &cx.st.work; + const gpa = cx.f.scratch; + const values = t.values.items(.value); + const by_value = t.byValue(gpa); + const keys = t.places.items(.key); + const by_key = t.byKey(gpa); + // One merge per kind of target, the nearer first: a node's pin, a slot's, a view's row. + var pairs: std.ArrayList(relations.Pair) = .empty; + const kinds = [3]struct { u8, u64 }{ .{ key_kind.node_pin, ~@as(u64, 0) }, .{ key_kind.slot_pin, ~@as(u64, 0) }, .{ key_kind.dump_pin, ~@as(u64, 15) } }; + var starts: [4]usize = undefined; + for (kinds, 0..) |kind, j| { + starts[j] = pairs.items.len; + relations.join(values, by_value, keys, by_key, kind[0], kind[1], &pairs, gpa); + } + starts[3] = pairs.items.len; + const panes = t.values.items(.pane); + const refs = t.values.items(.ref); + for (theme.metric.all_registers[0..16], 0..) |name, rank| { + const i = @import("context.zig").index(name).?; + if (cx.now.registerAge(i) != 0 or std.mem.eql(u8, name, "rsp")) continue; + // `tied`: rbp's move is its pin's flight in the stack's lane, not a link. + if (i == cursors.bp_index and cx.now.pinGone(1) != null) continue; + const regs = (cx.now.moment().regs(cx.now.rec.tid) catch null) orelse return; + const value = regs.named(name) orelse continue; + var line: Line = .{ .kind = .link, .name = name, .value = value }; + line.from = cx.find(.{ .register = i }) orelse continue; + line.third = cx.find(.registers) orelse continue; + // The value's targets, as the join found them. + const hit: ?struct { usize, u32 } = for (0..3) |j| { + const found = for (pairs.items[starts[j]..starts[j + 1]]) |pr| { + if (panes[pr.value] == .registers and refs[pr.value] == i) break pr.place; + } else null; + if (found) |p| break .{ j, p }; + } else null; + if (hit) |h| { + line.to = t.places.items(.box)[h[1]]; + line.target = switch (h[0]) { + 0 => .node, + 1 => .slot, + else => .row, + }; + } else if (cx.beyond(value)) |b| { + line.to = b.box; + line.target = .beyond; + line.words = b.words; + } else continue; + add(lines, cands, n, line, @intCast(rank)); + } + } + + /// A word of the stack outside its window: the link ends at the + /// window's edge, with a stub and how many words on it is. + fn beyond(cx: Cx, value: u64) ?struct { box: [4]f32, words: i64 } { + const w = cx.st.stack_window orelse return null; + if (value % 8 != 0 or rows.regionOf(cx.now.moment(), value) != .stack) return null; + const words = @as(i64, @bitCast(value -% w.first)) >> 3; + if (words >= 0 and words < w.slots) return null; + if (@abs(words) > 64) return null; + // Its nearest row's pin lane. + const y = if (words < 0) w.top else w.top + @as(f32, @floatFromInt(w.slots - 1)) * w.row; + return .{ .box = .{ w.x, y, w.x + 10 * cx.k, y + w.row }, .words = if (words < 0) words else words - @as(i64, @intCast(w.slots)) + 1 }; + } + + /// `projection`'s join: the step's events (its writes, then its reads) + /// ⋈ the runs of the lens's dump and the parts of the drawing made of + /// them; or a word of the stack ⋈ the call it is the way back of. + fn projections(cx: Cx, lines: *[max_lines]Line, cands: *[max_lines]relations.Candidate, n: *usize, add: anytype) void { + const rec = cx.now.rec; + const events = [2]struct { spans: []const Recording.Span, write: bool }{ + .{ .spans = rec.written(cx.now.stops[1], cx.now.stops[0]), .write = true }, + .{ .spans = rec.read(cx.now.stops[1], cx.now.stops[0]), .write = false }, + }; + var rank: u32 = 0; + for (events) |kind| for (kind.spans) |span| { + defer rank += 1; + // The run that begins where the bytes do (an element of an + // array), else the word they are in (a field of a node). + const word = if (cx.find(.{ .dump_run = span.addr }) != null) span.addr else span.addr & ~@as(u64, 7); + const slot = cx.find(.{ .word = span.addr }); + // (A return draws no line: its node's badge is struck with rax's digits.) + const stacked = kind.write and slot != null and cx.find(.{ .node_address = span.addr }) != null and cx.find(.{ .dump_run = word }) == null; + const run = if (stacked) slot.? else cx.find(.{ .dump_run = word }) orelse continue; + if (!stacked) _ = cx.find(.{ .node_part = word }) orelse continue; + const node_addr = if (stacked) span.addr else cx.nodeOf(word) orelse continue; + const over = cx.find(.{ .node_address = node_addr }) orelse continue; + add(lines, cands, n, .{ .kind = .projection, .from = run, .to = over, .stacked = stacked, .write = kind.write, .word = word }, rank); + }; + } + + /// `touch`: the step's touch event ⋈ the word's slot, from the row it leaves. + fn touchLine(cx: Cx) ?Line { + const now = cx.now; + if (now.plan.touch < 0 or now.time < now.plan.touch) return null; + const row = cx.st.touch_row orelse return null; + const tt = cursors.stackTouch(now.rec, now.stops[1], now.stops[0]) orelse return null; + const word = cx.find(.{ .word = tt.addr & ~@as(u64, 7) }) orelse return null; + const lane_pin = cx.find(.{ .slot_pin = tt.addr & ~@as(u64, 7) }) orelse return null; + return .{ .kind = .touch, .from = row, .to = word, .third = lane_pin, .write = tt.write }; + } + + /// `aim`'s join: the values on screen in memory that are code ⋈ the + /// disassembly's rows by address; standing, every one; for the step, + /// those of the words it writes or reads. Ranked by where they are, top first. + fn aims(cx: Cx, lines: *[max_lines]Line, cands: *[max_lines]relations.Candidate, n: *usize, add: anytype) void { + const t = &cx.st.work; + const gpa = cx.f.scratch; + const values = t.values.items(.value); + var pairs: std.ArrayList(relations.Pair) = .empty; + relations.join(values, t.byValue(gpa), t.places.items(.key), t.byKey(gpa), key_kind.row, ~@as(u64, 0), &pairs, gpa); + const panes = t.values.items(.pane); + const ats = t.values.items(.at); + const boxes = t.values.items(.box); + const step = cx.now.aim > 1.5; + const moment = cx.now.moment(); + // The words whose value was code before the step and is not now: the aim frays from its old row. + for (t.values.items(.value), 0..) |v, j| { + if (ats[j] == 0 or panes[j] == .registers) continue; + const was = (Recording.word(cx.now.before(), ats[j], 8) catch null) orelse continue; + if (was == v) continue; + // (Still code with a row: the join below has it, gliding from its old row.) + if (cx.find(.{ .row = v }) != null) continue; + const row = cx.find(.{ .row = was }) orelse continue; + if (step and !cx.touched(ats[j])) continue; + add(lines, cands, n, .{ .kind = .code, .from = boxes[j], .to = row, .value = v, .was = was, .word = ats[j], .rules = t.values.items(.rules)[j] }, @intFromFloat(@max(boxes[j][1], 0))); + } + for (pairs.items) |pr| { + const j = pr.value; + if (ats[j] == 0 or panes[j] == .registers) continue; + if (rows.regionOf(moment, values[j]) != .code) continue; + if (step and !cx.touched(ats[j])) continue; + const was = (Recording.word(cx.now.before(), ats[j], 8) catch null) orelse values[j]; + add(lines, cands, n, .{ .kind = .code, .from = boxes[j], .to = t.places.items(.box)[pr.place], .value = values[j], .was = was, .word = ats[j], .rules = t.values.items(.rules)[j] }, @intFromFloat(@max(boxes[j][1], 0))); + } + } + + /// Whether the step wrote or read the word at `addr`. + fn touched(cx: Cx, addr: u64) bool { + return cx.now.writeAge(addr, 8) == 0 or cx.now.wasRead(addr, 8); + } + + /// Pass 6 for one relation: its kind's verb at its frames. + fn drawLine(cx: Cx, line: Line) void { + switch (line.kind) { + .thread => cx.thread(line), + .beat => cx.named(), + .link => cx.link(line), + .projection => cx.projection(line), + .touch => cx.touch(line), + .carry => cx.carry(), + .code => cx.aim(line), + } + cx.st.stats.drawn += 1; + } + + /// The gold thread: the current line and its instructions, one mark. + fn thread(cx: Cx, line_box: Line) void { + const raw = cx.raw.faded(@import("context.zig").thread_shows); + const k = cx.k; + const line = line_box.from; + const x = cx.at.xf.apply(.{ theme.metric.tie.thread_x, 0 })[0]; + const gold = if (cx.st.thread_waits) ui.rgb(theme.color.text.primary) else ui.rgb(theme.mark(.current).solid); + const kind = if (cx.st.thread_waits) [2]f32{ theme.metric.bracket.quiet[0] * k, theme.metric.bracket.quiet[1] * k } else [2]f32{ theme.metric.bracket.thread[0] * k, theme.metric.bracket.thread[1] * k }; + raw.bracket(x, line[1] + 2 * k, line[3] - 2 * k, gold, kind, 1, false); + var bottom = line[3] - 2 * k; + var top = line[1] + 2 * k; + var n: usize = 0; + while (cx.find(.{ .run = n })) |run| : (n += 1) { + raw.bracket(x, run[1] + 2 * k, run[3] - 2 * k, gold, kind, 1, false); + bottom = @max(bottom, run[3] - 2 * k); + top = @min(top, run[1] + 2 * k); + } + if (n > 0) raw.fill(.{ .x = x - kind[0] / 2, .y = top, .w = kind[0], .h = bottom - top }, gold); } /// `touch` (design/README.md, "4. Ties"), `tied`, in a walked step: this @@ -236,26 +560,26 @@ pub const Ties = struct { /// rows), `standard`, from `Plan.touch`; held to the advance and drawn /// back into the row with it (12 f), or, a load carried, as the carry /// leaves the word. Only to a word on show: the window is there first. - fn touch(g: *const Ties, raw: ui.Ui, at: anim.Placed, now: cursors.Now, k: f32, split: theme.layout.Split) void { - if (now.plan.touch < 0 or now.time < now.plan.touch) return; - const row = touch_row orelse return; - const tt = cursors.stackTouch(g.cursor.rec, now.stops[1], now.stops[0]) orelse return; - const word = find(.{ .word = tt.addr & ~@as(u64, 7) }) orelse return; - const lane_pin = find(.{ .slot_pin = tt.addr & ~@as(u64, 7) }) orelse return; - const lane = at.xf.apply(.{ theme.metric.tie.touch_lane, 0 })[0]; + fn touch(cx: Cx, line: Line) void { + const now = cx.now; + const k = cx.k; + const row = line.from; + const word = line.to; + const lane_pin = line.third; + const lane = cx.at.xf.apply(.{ theme.metric.tie.touch_lane, 0 })[0]; // Onto the stack's rule nearer the lane: its left in the state column, its right // where it stands under the disassembly (a column's beat), the head pointing back. const left = lane_pin[0] - theme.metric.slot.lane * k; - const rule = if (left > lane) left else at.xf.apply(.{ split.stack.x + split.stack.w, 0 })[0]; + const rule = if (left > lane) left else cx.at.xf.apply(.{ cx.split.stack.x + cx.split.stack.w, 0 })[0]; const ya = (row[1] + row[3]) / 2; const yb = (word[1] + word[3]) / 2; const long = @abs(yb - ya) > 6 * theme.metric.context.row * k; const drawn = cursors.standard(std.math.clamp((now.time - now.plan.touch) / @as(f32, if (long) time.tied.touch_long else time.tied.touch), 0, 1)); - const back_at = if (now.plan.carry >= 0 and !tt.write) now.plan.carry else now.plan.advance; + const back_at = if (now.plan.carry >= 0 and !line.write) now.plan.carry else now.plan.advance; const back = cursors.standard(std.math.clamp((now.time - back_at) / time.quick, 0, 1)); if (back >= 1) return; - const color = theme.mark(if (tt.write) .write else .read).solid; - raw.route(&.{ .{ row[0], ya }, .{ lane, ya }, .{ lane, yb }, .{ rule, yb } }, color, .{ .width = theme.stroke.tie.projection * k, .head = .{ 7 * k, 8 * k }, .progress = @min(drawn, 1 - back), .radius = theme.metric.tie.radius * k }); + const color = theme.mark(if (line.write) .write else .read).solid; + cx.raw.route(&.{ .{ row[0], ya }, .{ lane, ya }, .{ lane, yb }, .{ rule, yb } }, color, .{ .width = theme.stroke.tie.projection * k, .head = .{ 7 * k, 8 * k }, .progress = @min(drawn, 1 - back), .radius = theme.metric.tie.radius * k }); } /// `carry` (design/README.md, "4. Ties"; `Levels.carry`), in a walked @@ -268,16 +592,19 @@ pub const Ties = struct { /// store's register framed as read the 6 f before; drawn back as the /// destination strikes (18 f). With `carry_value` the value's digits ride /// its head on a `raised` chip, landing as the strike. - fn carry(g: *const Ties, raw: ui.Ui, at: anim.Placed, now: cursors.Now, k: f32) void { - if (now.plan.carry < 0) return; - const rec = g.cursor.rec; + fn carry(cx: Cx) void { + const now = cx.now; + const k = cx.k; + const raw = cx.raw; + const at = cx.at; + const rec = now.rec; const tt = cursors.stackTouch(rec, now.stops[1], now.stops[0]) orelse return; const how = cursors.carried(rec, tt) orelse return; const i = @import("context.zig").index(how.reg) orelse return; - const reg = find(.{ .register = i }) orelse return; - const panel = find(.registers) orelse return; - const word = find(.{ .word = tt.addr & ~@as(u64, 7) }) orelse return; - const lane_pin = find(.{ .slot_pin = tt.addr & ~@as(u64, 7) }) orelse return; + const reg = cx.find(.{ .register = i }) orelse return; + const panel = cx.find(.registers) orelse return; + const word = cx.find(.{ .word = tt.addr & ~@as(u64, 7) }) orelse return; + const lane_pin = cx.find(.{ .slot_pin = tt.addr & ~@as(u64, 7) }) orelse return; const stack_rule = lane_pin[0] - theme.metric.slot.lane * k; const lane = at.xf.apply(.{ theme.metric.tie.projection_lane, 0 })[0]; const y_reg = (reg[1] + reg[3]) / 2; @@ -324,132 +651,95 @@ pub const Ties = struct { } } - /// The gold thread: the current line and its instructions, one mark. - fn thread(raw: ui.Ui, at: anim.Placed, k: f32) void { - const line = find(.source) orelse return; - const x = at.xf.apply(.{ theme.metric.tie.thread_x, 0 })[0]; - const gold = if (thread_waits) ui.rgb(theme.color.text.primary) else ui.rgb(theme.mark(.current).solid); - const kind = if (thread_waits) [2]f32{ theme.metric.bracket.quiet[0] * k, theme.metric.bracket.quiet[1] * k } else [2]f32{ theme.metric.bracket.thread[0] * k, theme.metric.bracket.thread[1] * k }; - raw.bracket(x, line[1] + 2 * k, line[3] - 2 * k, gold, kind, 1, false); - var bottom = line[3] - 2 * k; - var top = line[1] + 2 * k; - var n: usize = 0; - while (find(.{ .run = n })) |run| : (n += 1) { - raw.bracket(x, run[1] + 2 * k, run[3] - 2 * k, gold, kind, 1, false); - bottom = @max(bottom, run[3] - 2 * k); - top = @min(top, run[1] + 2 * k); - } - if (n > 0) raw.fill(.{ .x = x - kind[0] / 2, .y = top, .w = kind[0], .h = bottom - top }, gold); - } - /// The step's link: the first register it struck that now holds the /// address of a pinned thing (a stack slot, a node, a view's row). rip /// and rsp are where execution and the stack are, and have none. - fn link(g: *const Ties, raw: ui.Ui, at: anim.Placed, now: cursors.Now, k: f32, split: theme.layout.Split) bool { - const rec = g.cursor.rec; - const regs = (now.moment().regs(rec.tid) catch null) orelse return false; - for (theme.metric.all_registers[0..16]) |name| { - const i = @import("context.zig").index(name).?; - if (now.registerAge(i) != 0 or std.mem.eql(u8, name, "rsp")) continue; - // `tied`: rbp's move is its pin's flight in the stack's lane, not a link. - if (i == cursors.bp_index and now.pinGone(1) != null) continue; - const value = regs.named(name) orelse continue; - const since = linkSince(now, i); - const l = time.link; - const node = find(.{ .node_pin = value }) != null; - const drawn = cursors.standard(std.math.clamp((since - l.after) / linkTime(if (node) .node else .slot), 0, 1)); - const back = std.math.clamp((now.time - now.plan.advance) / l.out, 0, 1); - if (linkTo(raw, at, now, k, split, name, value, drawn, back)) return true; - } - return false; + fn link(cx: Cx, line: Line) void { + const now = cx.now; + const i = @import("context.zig").index(line.name).?; + const since = linkSince(now, i); + const l = time.link; + const drawn = cursors.standard(std.math.clamp((since - l.after) / linkTime(if (line.target == .node) .node else .slot), 0, 1)); + const back = std.math.clamp((now.time - now.plan.advance) / l.out, 0, 1); + cx.linkTo(line, drawn, back); } - /// A link from register `name` to what it points at (`value`): a node's + /// A link from register `line.name` to what it points at: a node's /// pin, a slot's, a view's row, or a slot past the stack's window (a - /// stub and a row count), `drawn` of the way and drawing back `back` of - /// it. False where `value` is no such place. - fn linkTo(raw: ui.Ui, at: anim.Placed, now: cursors.Now, k: f32, split: theme.layout.Split, name: []const u8, value: u64, drawn: f32, back: f32) bool { - const i = @import("context.zig").index(name) orelse return false; - const edge = find(.registers) orelse return false; - const from = find(.{ .register = i }) orelse return false; - const node = find(.{ .node_pin = value }); - // A view's row it points into: up the gutter to the row's left edge. - const row = if (node == null) find(.{ .dump_pin = value & ~@as(u64, 15) }) else null; - // A word of the stack outside its window: the link ends at the - // window's edge, with a stub and how many words on it is. - var beyond: ?i64 = null; - const target = node orelse find(.{ .slot_pin = value }) orelse row orelse edge: { - const w = stack_window orelse return false; - if (value % 8 != 0 or rows.regionOf(now.moment(), value) != .stack) return false; - const words = @as(i64, @bitCast(value -% w.first)) >> 3; - if (words >= 0 and words < w.slots) return false; - if (@abs(words) > 64) return false; - beyond = if (words < 0) words else words - @as(i64, @intCast(w.slots)) + 1; - // Its nearest row's pin lane. - const y = if (words < 0) w.top else w.top + @as(f32, @floatFromInt(w.slots - 1)) * w.row; - break :edge [4]f32{ w.x, y, w.x + 10 * k, y + w.row }; - }; - const l = time.link; - if (drawn <= 0 or back >= 1) return true; - const color = rows.ink(rows.regionOf(now.moment(), value)); + /// stub and a row count), `drawn` of the way and drawing back `back` of it. + fn linkTo(cx: Cx, line: Line, drawn: f32, back: f32) void { + const now = cx.now; + const k = cx.k; + const raw = cx.raw; + const at = cx.at; + const edge = line.third; + const from = line.from; + const target = line.to; + if (drawn <= 0 or back >= 1) return; + const color = rows.ink(rows.regionOf(now.moment(), line.value)); const y = (from[1] + from[3]) / 2; // What climbs into the band takes the gutter's left lane; what // goes down to a slot, the right. - const lane = at.xf.apply(.{ if (node != null or row != null) theme.metric.tie.link_lane else theme.metric.tie.projection_lane, 0 })[0]; + const lane = at.xf.apply(.{ if (line.target == .node or line.target == .row) theme.metric.tie.link_lane else theme.metric.tie.projection_lane, 0 })[0]; var points: [5]ui.Vec = undefined; var n: usize = 0; points[0] = .{ edge[0] + 2 * k, y }; points[1] = .{ lane, y }; - if (node) |pin| { - // Up the gutter, along the lens's bottom lane, up into the pin under the node. - const bottom = at.xf.apply(.{ 0, split.band.y + split.band.h - theme.metric.lens.bottom_lane })[1]; - const cx = (pin[0] + pin[2]) / 2; - points[2] = .{ lane, bottom }; - points[3] = .{ cx, bottom }; - points[4] = .{ cx, pin[1] }; - n = 5; - } else if (beyond) |words| { - // Off the window: to its nearest row, into the pin lane, out - // to the panel's rule, where a head points on. - const mid = (target[1] + target[3]) / 2; - const rule = if (words < 0) target[1] - 6 * k else target[3] + 6 * k; - points[2] = .{ lane, mid }; - points[3] = .{ target[0] + 6 * k, mid }; - points[4] = .{ target[0] + 6 * k, rule }; - n = 5; - } else { - // Along the gutter to the row, and into its pin lane (a view's row: to its edge). - const mid = (target[1] + target[3]) / 2; - points[2] = .{ lane, mid }; - points[3] = .{ target[0] + (if (row != null) @as(f32, 2) else -2) * k, mid }; - n = 4; + switch (line.target) { + .node => { + // Up the gutter, along the lens's bottom lane, up into the pin under the node. + const bottom = at.xf.apply(.{ 0, cx.split.band.y + cx.split.band.h - theme.metric.lens.bottom_lane })[1]; + const cx_ = (target[0] + target[2]) / 2; + points[2] = .{ lane, bottom }; + points[3] = .{ cx_, bottom }; + points[4] = .{ cx_, target[1] }; + n = 5; + }, + .beyond => { + // Off the window: to its nearest row, into the pin lane, out + // to the panel's rule, where a head points on. + const mid = (target[1] + target[3]) / 2; + const rule = if (line.words < 0) target[1] - 6 * k else target[3] + 6 * k; + points[2] = .{ lane, mid }; + points[3] = .{ target[0] + 6 * k, mid }; + points[4] = .{ target[0] + 6 * k, rule }; + n = 5; + }, + .slot, .row => { + // Along the gutter to the row, and into its pin lane (a view's row: to its edge). + const mid = (target[1] + target[3]) / 2; + points[2] = .{ lane, mid }; + points[3] = .{ target[0] + (if (line.target == .row) @as(f32, 2) else -2) * k, mid }; + n = 4; + }, } - raw.route(points[0..n], color, .{ .width = theme.stroke.tie.link * k, .head = if (beyond == null) .{ 10 * k, 10 * k } else .{ 9 * k, 12 * k }, .progress = drawn, .from = cursors.standard(back), .radius = theme.metric.tie.radius * k }); - if (beyond) |words| if (drawn >= 1) { + const past = line.target == .beyond; + raw.route(points[0..n], color, .{ .width = theme.stroke.tie.link * k, .head = if (!past) .{ 10 * k, 10 * k } else .{ 9 * k, 12 * k }, .progress = drawn, .from = cursors.standard(back), .radius = theme.metric.tie.radius * k }); + if (past and drawn >= 1) { // How far the target is, faint, 6 past the head. const v = raw.faded(1 - back); const tip = points[4]; - v.text(.mono, theme.size.mono.context, tip[0] + 12 * k, tip[1] + (if (words < 0) @as(f32, 6) else 14) * k, v.print("{d} rows", .{@abs(words)}), theme.color.text.faint); - }; + v.text(.mono, theme.size.mono.context, tip[0] + 12 * k, tip[1] + (if (line.words < 0) @as(f32, 6) else 14) * k, v.print("{d} rows", .{@abs(line.words)}), theme.color.text.faint); + } // The pin rides the line's head: the register's name beside it. - if (drawn > 0 and drawn < 1 and beyond == null) { + if (drawn > 0 and drawn < 1 and !past) { const tip = along(points[0..n], drawn); - raw.text(.mono_bold, theme.size.mono.context, tip[0] + 8 * k, tip[1] - 6 * k, name, theme.color.text.primary); + raw.text(.mono_bold, theme.size.mono.context, tip[0] + 8 * k, tip[1] - 6 * k, line.name, theme.color.text.primary); } - _ = l; - return true; } /// The scene's `Beat`: its register framed as read, then its link. - fn named(raw: ui.Ui, at: anim.Placed, now: cursors.Now, k: f32, split: theme.layout.Split, b: *const Beat) void { + fn named(cx: Cx) void { + const b = cx.g.beat.?; + const now = cx.now; + const at = cx.at; const t = at.value(b.t); - if (t < 0 or t >= b.total()) return; const i = @import("context.zig").index(b.name) orelse return; - const px = find(.{ .register = i }) orelse return; + const px = cx.find(.{ .register = i }) orelse return; const l = time.link; const back = std.math.clamp((t - (b.total() - l.out)) / l.out, 0, 1); // Its value framed as read, in the panel's units. - const u: ui.Ui = .{ .f = raw.f, .at = at }; + const u: ui.Ui = .{ .f = cx.raw.f, .at = at }; const inv = at.xf.invert(); const a = inv.apply(.{ px[0], px[1] }); const c = inv.apply(.{ px[2], px[3] }); @@ -458,75 +748,182 @@ pub const Ties = struct { const regs = (now.moment().regs(rec.tid) catch null) orelse return; const value = regs.named(b.name) orelse return; const drawn = cursors.standard(std.math.clamp((t - l.after) / l.in_far, 0, 1)); - _ = linkTo(raw, at, now, k, split, b.name, value, drawn, back); + // Its target as a link's: a node's pin, a slot's, a view's row, or past the window. + var line: Line = .{ .kind = .beat, .name = b.name, .value = value, .from = px }; + line.third = cx.find(.registers) orelse return; + if (cx.find(.{ .node_pin = value })) |p| { + line.to = p; + line.target = .node; + } else if (cx.find(.{ .slot_pin = value })) |p| { + line.to = p; + line.target = .slot; + } else if (cx.find(.{ .dump_pin = value & ~@as(u64, 15) })) |p| { + line.to = p; + line.target = .row; + } else if (cx.beyond(value)) |bb| { + line.to = bb.box; + line.target = .beyond; + line.words = bb.words; + } else return; + cx.linkTo(line, drawn, back); } /// The step's projection: the first run of bytes it read or wrote in /// the lens's dump that a part of the drawing is made of. - fn projection(g: *const Ties, raw: ui.Ui, at: anim.Placed, now: cursors.Now, k: f32, split: theme.layout.Split) bool { - const rec = g.cursor.rec; - const kinds = [2]struct { spans: []const @import("Recording.zig").Span, role: theme.Role }{ - .{ .spans = rec.written(now.stops[1], now.stops[0]), .role = .write }, - .{ .spans = rec.read(now.stops[1], now.stops[0]), .role = .read }, - }; - for (kinds) |kind| for (kind.spans) |span| { - // The run that begins where the bytes do (an element of an - // array), else the word they are in (a field of a node). - const word = if (find(.{ .dump_run = span.addr }) != null) span.addr else span.addr & ~@as(u64, 7); - // From the dump's run to the part of the drawing made of it; or - // from a word of the stack to the call it is the way back of. - const slot = find(.{ .word = span.addr }); - // (A return draws no line: its node's badge is struck with rax's digits.) - const stacked = kind.role == .write and slot != null and find(.{ .node_address = span.addr }) != null and find(.{ .dump_run = word }) == null; - const run = if (stacked) slot.? else find(.{ .dump_run = word }) orelse continue; - if (!stacked) _ = find(.{ .node_part = word }) orelse continue; - const node_addr = if (stacked) span.addr else nodeOf(word) orelse continue; - const over = find(.{ .node_address = node_addr }) orelse continue; - const p = time.project; - // Under `tied`, once what made them has shown (a call's push, rsp's pin landed: `Plan.project`). - const since = if (now.plan.project >= 0) now.time - now.plan.project else now.since(.memory, 0); - const bracket = std.math.clamp(since / p.bracket, 0, 1); - const drawn = cursors.standard(std.math.clamp((since - p.bracket) / @as(f32, if (stacked) p.far else p.draw), 0, 1)); - const out = 1 - std.math.clamp((now.time - now.plan.advance) / p.out, 0, 1); - if (bracket <= 0 or out <= 0) return true; - const color = theme.mark(kind.role).solid; - const v = raw.faded(out); - const tie = [2]f32{ theme.metric.bracket.tie[0] * k, theme.metric.bracket.tie[1] * k }; - // The bracket takes the row: x 7 of the dump, the row's height; - // on the stack, the panel's left padding. - const bx = if (stacked) run[0] - (theme.metric.slot.address - 5) * k else run[0] - (rows.byteX(@intCast(word % 16)) - 4 - theme.metric.bytes.bracket) * k; - v.faded(bracket).bracket(bx, run[1] + 2 * k, run[3] - 2 * k, color, tie, 1, false); - const cx = (over[0] + over[2]) / 2; - const y = (run[1] + run[3]) / 2; - if (stacked) { - // A frame's way back to its call's node: up the gutter's - // left lane, along the drawing's bottom lane, up to a - // bracket under the node, serifs up. - const lane = at.xf.apply(.{ theme.metric.tie.link_lane, 0 })[0]; - const bottom = at.xf.apply(.{ 0, split.band.y + split.band.h - theme.metric.lens.bottom_lane })[1]; - v.route(&.{ .{ bx - 1 * k, y }, .{ lane, y }, .{ lane, bottom }, .{ cx, bottom }, .{ cx, over[3] + 5 * k } }, color, .{ .width = theme.stroke.tie.projection * k, .head = null, .progress = drawn, .radius = theme.metric.tie.projection_radius * k }); - if (drawn >= 1) { - v.fill(.{ .x = over[0], .y = over[3] + 2 * k, .w = over[2] - over[0], .h = 3 * k }, color); - v.fill(.{ .x = over[0], .y = over[3] - 3 * k, .w = 3 * k, .h = 8 * k }, color); - v.fill(.{ .x = over[2] - 3 * k, .y = over[3] - 3 * k, .w = 3 * k, .h = 8 * k }, color); - } - return true; - } - const lane = at.xf.apply(.{ theme.metric.tie.projection_lane, 0 })[0]; - const top = at.xf.apply(.{ 0, split.band.y + theme.metric.context.tag + theme.metric.lens.top_lane })[1]; - v.route(&.{ .{ bx - 1 * k, y }, .{ lane, y }, .{ lane, top }, .{ cx, top }, .{ cx, over[1] - 4 * k } }, color, .{ .width = theme.stroke.tie.projection * k, .head = null, .progress = drawn, .radius = theme.metric.tie.projection_radius * k }); - // Over the node's address: a bracket, serifs down. + fn projection(cx: Cx, line: Line) void { + const now = cx.now; + const k = cx.k; + const at = cx.at; + const run = line.from; + const over = line.to; + const stacked = line.stacked; + const word = line.word; + const p = time.project; + // Under `tied`, once what made them has shown (a call's push, rsp's pin landed: `Plan.project`). + const since = if (now.plan.project >= 0) now.time - now.plan.project else now.since(.memory, 0); + const bracket = std.math.clamp(since / p.bracket, 0, 1); + const drawn = cursors.standard(std.math.clamp((since - p.bracket) / @as(f32, if (stacked) p.far else p.draw), 0, 1)); + const out = 1 - std.math.clamp((now.time - now.plan.advance) / p.out, 0, 1); + if (bracket <= 0 or out <= 0) return; + const color = theme.mark(if (line.write) .write else .read).solid; + const v = cx.raw.faded(out); + const tie = [2]f32{ theme.metric.bracket.tie[0] * k, theme.metric.bracket.tie[1] * k }; + // The bracket takes the row: x 7 of the dump, the row's height; + // on the stack, the panel's left padding. + const bx = if (stacked) run[0] - (theme.metric.slot.address - 5) * k else run[0] - (rows.byteX(@intCast(word % 16)) - 4 - theme.metric.bytes.bracket) * k; + v.faded(bracket).bracket(bx, run[1] + 2 * k, run[3] - 2 * k, color, tie, 1, false); + const cx_ = (over[0] + over[2]) / 2; + const y = (run[1] + run[3]) / 2; + if (stacked) { + // A frame's way back to its call's node: up the gutter's + // left lane, along the drawing's bottom lane, up to a + // bracket under the node, serifs up. + const lane = at.xf.apply(.{ theme.metric.tie.link_lane, 0 })[0]; + const bottom = at.xf.apply(.{ 0, cx.split.band.y + cx.split.band.h - theme.metric.lens.bottom_lane })[1]; + v.route(&.{ .{ bx - 1 * k, y }, .{ lane, y }, .{ lane, bottom }, .{ cx_, bottom }, .{ cx_, over[3] + 5 * k } }, color, .{ .width = theme.stroke.tie.projection * k, .head = null, .progress = drawn, .radius = theme.metric.tie.projection_radius * k }); if (drawn >= 1) { - v.fill(.{ .x = over[0], .y = over[1] - 5 * k, .w = over[2] - over[0], .h = 3 * k }, color); - v.fill(.{ .x = over[0], .y = over[1] - 5 * k, .w = 3 * k, .h = 8 * k }, color); - v.fill(.{ .x = over[2] - 3 * k, .y = over[1] - 5 * k, .w = 3 * k, .h = 8 * k }, color); + v.fill(.{ .x = over[0], .y = over[3] + 2 * k, .w = over[2] - over[0], .h = 3 * k }, color); + v.fill(.{ .x = over[0], .y = over[3] - 3 * k, .w = 3 * k, .h = 8 * k }, color); + v.fill(.{ .x = over[2] - 3 * k, .y = over[3] - 3 * k, .w = 3 * k, .h = 8 * k }, color); } - return true; - }; - return false; + return; + } + const lane = at.xf.apply(.{ theme.metric.tie.projection_lane, 0 })[0]; + const top = at.xf.apply(.{ 0, cx.split.band.y + theme.metric.context.tag + theme.metric.lens.top_lane })[1]; + v.route(&.{ .{ bx - 1 * k, y }, .{ lane, y }, .{ lane, top }, .{ cx_, top }, .{ cx_, over[1] - 4 * k } }, color, .{ .width = theme.stroke.tie.projection * k, .head = null, .progress = drawn, .radius = theme.metric.tie.projection_radius * k }); + // Over the node's address: a bracket, serifs down. + if (drawn >= 1) { + v.fill(.{ .x = over[0], .y = over[1] - 5 * k, .w = over[2] - over[0], .h = 3 * k }, color); + v.fill(.{ .x = over[0], .y = over[1] - 5 * k, .w = 3 * k, .h = 8 * k }, color); + v.fill(.{ .x = over[2] - 3 * k, .y = over[1] - 5 * k, .w = 3 * k, .h = 8 * k }, color); + } + } + + /// `aim` (design/README.md, "4. Ties"; the scene's knob, off by default): + /// this word holds the address of that instruction. 2 units in code's + /// ink, a filled head: out of the word's panel's rule nearer the gutter, + /// along its left lane (x 950), onto the disassembly's right rule level + /// with the row, whose address is framed in code's ink as the head lands. + /// Standing, 60% toward the page, in and out with the knob (18 f); for + /// the step, full, after the word's mark (18 f, 24 past ten rows), back + /// with the advance. Written again: still code with its row shown, the + /// head glides to it (24 f) after the strike; no longer code, it frays + /// (dashed over 12 f, gone over 18 more). The disassembly never moves for it. + fn aim(cx: Cx, line: Line) void { + const now = cx.now; + const k = cx.k; + const at = cx.at; + const step = now.aim > 1.5; + const lane = at.xf.apply(.{ theme.metric.tie.link_lane, 0 })[0]; + // Leaves by its panel's rule nearer the lane. + const rule = if (@abs(line.rules[0] - lane) < @abs(line.rules[1] - lane)) line.rules[0] else line.rules[1]; + const y_word = (line.from[1] + line.from[3]) / 2; + // Where the word changed this step: its strike, and the row it held. + const struck: ?f32 = if (line.was != line.value) now.struckAgo(line.word, 8) else null; + const old_row = if (line.was != line.value) cx.find(.{ .row = line.was }) else null; + const code_now = rows.regionOf(now.moment(), line.value) == .code; + const new_row = if (code_now) cx.find(.{ .row = line.value }) else null; + // The head: on the old row until the strike, then gliding to the new one (24 f); fraying where there is none. + var y_row = (line.to[1] + line.to[3]) / 2; + var fray: f32 = 0; + var row_box = line.to; + if (old_row) |o| { + const after = if (struck) |sa| sa else if (now.resting()) std.math.inf(f32) else -1; + if (new_row) |nr| { + const p = cursors.glide(std.math.clamp(after / (24.0 / 60.0), 0, 1)); + y_row = std.math.lerp((o[1] + o[3]) / 2, (nr[1] + nr[3]) / 2, p); + row_box = if (p < 0.5) o else nr; + } else { + y_row = (o[1] + o[3]) / 2; + row_box = o; + fray = std.math.clamp(after / (30.0 / 60.0), 0, 1); + if (fray >= 1) return; + } + } + const right = row_box[2]; + // How much it shows: standing with the knob (18 f in and out), for the step from the word's mark to the advance. + var drawn: f32 = 1; + var shows: f32 = if (step) 1 else @min(now.aim, 1); + if (step) { + if (now.resting()) return; + const from = @max(now.plan.at[1], 0) + time.strike.in; + const long = @abs(y_row - y_word) > 10 * theme.metric.context.row * k; + drawn = cursors.standard(std.math.clamp((now.time - from) / @as(f32, if (long) 24.0 / 60.0 else 18.0 / 60.0), 0, 1)); + shows = 1 - std.math.clamp((now.time - now.plan.advance) / time.quick, 0, 1); + } + if (drawn <= 0 or shows <= 0) return; + // Standing, 60% toward the page; fraying, dashed over its first 12 f, then fading. + const ink = if (step) ui.rgb(theme.color.region.code_) else ui.mix(theme.color.region.code_, theme.color.page, 0.6); + const v = cx.raw.faded(shows * (1 - std.math.clamp((fray - 0.4) / 0.6, 0, 1))); + const points = [4]ui.Vec{ .{ rule, y_word }, .{ lane, y_word }, .{ lane, y_row }, .{ right, y_row } }; + if (fray > 0) { + // Dashed, 12 on 8, the gaps opening over the fray's first 12 f. + const open = std.math.clamp(fray / 0.4, 0, 1); + dashed(v, &points, ink, theme.stroke.rule * k, 12 * k, 8 * k * open); + } else v.route(&points, ink, .{ .width = theme.stroke.rule * k, .head = .{ 8 * k, 8 * k }, .progress = drawn, .radius = theme.metric.tie.radius * k }); + // The row's address framed in code's ink as the head lands. + if (drawn >= 1 and fray == 0) { + const adv = ui.advance(theme.size.mono.context) * k; + const digits: f32 = @floatFromInt(ui.digits(if (row_box[1] == line.to[1]) line.value else line.was)); + const x0 = row_box[0] + (theme.metric.insn.address - 4) * k; + v.frame(.{ .x = x0, .y = row_box[1], .w = digits * adv + 8 * k, .h = row_box[3] - row_box[1] }, ink, theme.stroke.rule * k); + } } }; +/// `points` as dashes `on` long with gaps `off` wide, `width` thick. +fn dashed(u: ui.Ui, points: []const ui.Vec, ink: anytype, width: f32, on: f32, off: f32) void { + for (points[0 .. points.len - 1], points[1..]) |a, b| { + const d = sk.math.distance(a, b); + if (d <= 0) continue; + var s: f32 = 0; + while (s < d) : (s += on + off) { + const e = @min(s + on, d); + u.line(sk.math.mix(a, b, s / d), sk.math.mix(a, b, e / d), ink, width); + } + } +} + +/// The frame's counts and times, on stderr with `CANIM_RELATIONS=1`; and, +/// for the measure, the map's join of its values with its rows. +fn report(f: *sk.Frame, st: *State, began: u64) void { + if (!relations.reporting()) return; + st.stats.places = st.work.places.len; + st.stats.values = st.work.values.len; + st.stats.ns.sort = st.work.sort_ns; + st.stats.map_places = st.map.places.len; + st.stats.map_values = st.map.values.len; + if (st.map.places.len > 0) { + const start = relations.clock(); + var pairs: std.ArrayList(relations.Pair) = .empty; + relations.join(st.map.values.items(.value), st.map.byValue(f.scratch), st.map.places.items(.key), st.map.byKey(f.scratch), key_kind.row, ~@as(u64, 0), &pairs, f.scratch); + st.stats.map_pairs = pairs.items.len; + st.stats.ns.map = relations.clock() - start; + } + const x = st.stats; + std.debug.print("relations frame {d} places {d} values {d} candidates {d} kept {d} drawn {d} ns sort {d} candidates {d} passes {d} draw {d} all {d} map places {d} values {d} pairs {d} ns {d}\n", .{ f.in.frame, x.places, x.values, x.candidates, x.kept, x.drawn, x.ns.sort, x.ns.candidates, x.ns.passes, x.ns.draw, relations.clock() - began, x.map_places, x.map_values, x.map_pairs, x.ns.map }); +} + /// A link the narration plays on a beat the caption names, not a step's: /// register `name` framed as read (6 f), then its link to what it points at /// (6 f after, 36 f, `standard`, the name riding its head), held `hold`, @@ -565,17 +962,6 @@ fn along(points: []const ui.Vec, p: f32) ui.Vec { return points[points.len - 1]; } -/// The node a field's address belongs to: the drawn node at or below it, within a record. -fn nodeOf(addr: u64) ?u64 { - var best: ?u64 = null; - for (table[0..said]) |entry| { - if (entry.key >> 56 != 6) continue; - const a = entry.key & ((1 << 56) - 1); - if (a <= addr and addr - a < 64 and (best == null or a > best.?)) best = a; - } - return best; -} - test "places have keys of their own" { try std.testing.expect((Place{ .word = 0x1000 }).key() != (Place{ .node = 0x1000 }).key()); try std.testing.expect((Place{ .source = {} }).key() != (Place{ .pc = {} }).key()); |
