diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-12 01:44:59 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 03:17:32 -0300 |
| commit | bf0dfa135319f8c7ca49eace4d1f81152ebfd23e (patch) | |
| tree | 9c80ade769dc9a741c9d00f63770d34dc1769ced /anim/src | |
| parent | aefde257db6e7cf683b923498c0d471885ab67a9 (diff) | |
| download | computeranim-bf0dfa135319f8c7ca49eace4d1f81152ebfd23e.tar.gz computeranim-bf0dfa135319f8c7ca49eace4d1f81152ebfd23e.zip | |
canim: the introduction, three chapters
From the user: a first-principles showcase that builds the views. I,
memory: the stack's bytes, words read low byte first, the address space
to true scale with each cluster enlarged and every band's addresses and
size, the bytes tied to their place, ink and chains. II, the stack: the
words become the stack panel, a real push, the return address aimed at
the row it returns to, rbp and the frame, the registers. III, code: an
instruction's bytes decoded into one listing of lines and addresses, then
two columns, then the working frame assembled and the map. Recorded from
main's call of reverse with record --until.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'anim/src')
| -rw-r--r-- | anim/src/aligned.zig | 154 | ||||
| -rw-r--r-- | anim/src/aside.zig | 73 | ||||
| -rw-r--r-- | anim/src/context.zig | 56 | ||||
| -rw-r--r-- | anim/src/descent.zig | 44 | ||||
| -rw-r--r-- | anim/src/regroup.zig | 52 | ||||
| -rw-r--r-- | anim/src/root.zig | 2 | ||||
| -rw-r--r-- | anim/src/segments.zig | 21 |
7 files changed, 321 insertions, 81 deletions
diff --git a/anim/src/aligned.zig b/anim/src/aligned.zig index a7c225d..ce2f5a0 100644 --- a/anim/src/aligned.zig +++ b/anim/src/aligned.zig @@ -19,15 +19,19 @@ //! 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 +//! `align` between them (6. Verbs): the bytes go (8 f), the addresses and +//! instructions glide right into the instruction column (24 f), 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). +//! +//! The lines as the mocks set them, without their indentation; the pc's +//! row says what it reads (its address and chain, mock 33's annotation). const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); const ui = @import("ui.zig"); const disasm = @import("disasm.zig"); +const rows = @import("rows.zig"); const Recording = @import("Recording.zig"); const theme = ui.theme; const Rect = ui.Rect; @@ -47,6 +51,9 @@ const col = struct { const mnemonic = 360; const seam_after = 48; const instr = 156; + /// The pc's row's annotation, from its mnemonic: interleaved, columns (mocks 32, 36). + const note = 540; + const note_columns = 360; }; /// The dump row's first byte (`bytes`: at 132 at least). @@ -83,15 +90,25 @@ pub const Aligned = struct { rows_in: anim.Value, chrome: anim.Value, form: anim.Value, + /// `stack` (6. Verbs, `align` backwards), frames: the heads' paper and + /// rules, the seam and the tag out (12 f), then each line glides to its + /// row of the source panel (36 f, `glide`) and each instruction to its + /// row of the disassembly, down its column first (24 f), then left into + /// the panel (24 f): an instruction never passes over a line. Those the + /// working frame's windows do not show go on past their edge, fading. + /// 60 f in all; the scene then brings the panels in under them and lets this go. + stack: anim.Value, + /// Where they stack to: the working frame's source and disassembly boxes. + to: struct { source: Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 }, disasm: Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 } } = .{}, 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) }; + 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), .stack = s.value(-1) }; } 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"); + while (it.next()) |l| : (k += 1) if (k == n) return std.mem.trim(u8, l, " \t\r"); return ""; } @@ -177,14 +194,32 @@ pub const Aligned = struct { 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; + // The bytes first (interleaved alone has them), then the columns glide: never over them. + const bytes_go = clamp01(form / 8); + const across = glide((form - 8) / 24); + const down = glide((form - 32) / 36); + const bare = clamp01((form - 68) / 12); + // The seam 48 after the longest line, or after a second run's line and its note; the + // mnemonics after the longest instruction's bytes (mock 32's 360 holds five). + var seam = col.text + built.longest + col.seam_after; + var most: usize = 0; + for (built.rows) |r| { + if (r.kind == .head and r.again) seam = @max(seam, col.text + lineWidth(a, r.line) + again_gap + u.width(.sans, theme.size.sans.micro, again_note) + 24); + if (r.insn) |insn| most = @max(most, insn.len); + } + const mnemonic = @max(col.mnemonic, col.bytes + @as(f32, @floatFromInt(3 * most)) * adv + 12); 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 st = placed.value(a.stack); + const kept = 1 - clamp01(st / 12); + const going = glide((st - 12) / 36); + const down_first = glide((st - 12) / 24); + const then_left = glide((st - 36) / 24); + const chrome = clamp01(placed.value(a.chrome) / 12) * kept; + const tag_in = clamp01((placed.value(a.chrome) - 12) / 12) * kept; const v = u.moved(a.box.x, a.box.y); + // Nothing under the stage (y 888): rows `stack` takes past the disassembly's foot fade there, not over the caption. + v.clip(.{ .x = -a.box.x, .y = -a.box.y, .w = theme.canvas.width, .h = theme.canvas.stage.y + theme.canvas.stage.h }); + defer v.unclip(); 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) }; @@ -200,22 +235,53 @@ pub const Aligned = struct { } 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); + if (down > 0) v.faded(down * kept).fill(.{ .x = seam, .y = tag, .w = theme.stroke.rule, .h = h - tag }, theme.color.rule); + // Where `stack` takes them: the source's window and the disassembly's, as those panels have them. + const src_first: f32 = @floatFromInt(a.rec.shownFirst(@intCast(theme.layout.rowsIn(a.to.source.h)), a.rec.shown[a.at])); + const dis_room = theme.layout.rowsIn(a.to.disasm.h); + // The panels' columns right of the line numbers move by as much as the widest file's numbers need. + const widen = rows.numberRoom(a.rec.most_lines); + const dis_rows = if (st >= 0) disasm.listingAt(u, a.rec, a.at, a.rec.register(a.at, "rip"), dis_room, (dis_room -| 1) / 2).rows else &.{}; for (built.rows, 0..) |r, k| { - const y = body + pitch * std.math.lerp(r.at[0], r.at[1], down); + var 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); + // `stack`: its row in the working frame's panel (box-relative), and whether that window shows it. + var to_x: f32 = 0; + var to_y: f32 = 0; + if (st >= 0) { + var inside = true; + if (r.kind == .insn) { + var n: ?usize = null; + for (dis_rows, 0..) |d, i| if (d.addr == r.insn.?.addr) { + n = i; + }; + // Past the window: where it would be, going on from its last row. + const at_row: f32 = if (n) |i| @floatFromInt(i) else @as(f32, @floatFromInt(dis_rows.len)) + order - @as(f32, @floatFromInt(built.pc)); + inside = n != null; + to_x = a.to.disasm.x - a.box.x; + to_y = a.to.disasm.y - a.box.y + body + pitch * at_row; + } else { + const at_row = @as(f32, @floatFromInt(r.line)) - src_first; + inside = !r.again and at_row >= 0 and at_row < @as(f32, @floatFromInt(theme.layout.rowsIn(a.to.source.h))); + to_x = a.to.source.x - a.box.x; + to_y = a.to.source.y - a.box.y + body + pitch * at_row; + } + if (!inside) shows *= 1 - (if (r.kind == .insn) then_left else going); + } if (shows <= 0) continue; const rv = v.faded(shows); + if (r.kind == .insn and st >= 0) y = std.math.lerp(y, to_y, down_first); 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]; + var head_y = if (r.kind == .head) y else body + pitch * r.at[1]; + if (st >= 0) head_y = std.math.lerp(head_y, to_y, going); 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); @@ -223,36 +289,47 @@ pub const Aligned = struct { 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); + // `stack`: the number to the source's number column, the text to its text column at its indent. + const n_end = std.math.lerp(col.place_end, to_x + theme.metric.context.line_number_end + widen, going); + const t_x = std.math.lerp(col.text, to_x + theme.metric.context.line_text + widen + @as(f32, @floatFromInt(indentOf(a, r.line))) * adv, going); + if (is_pc) rv.fill(.{ .x = n_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, n_end - @as(f32, @floatFromInt(number.len)) * adv, hb, number, if (is_pc) t.ink else t.faint); + rv.text(.mono, size, t_x, hb, a.sourceLine(r.line), if (faint) t.faint else t.primary); + if (r.again) rv.faded(kept).text(.sans, theme.size.sans.micro, std.math.lerp(mnemonic + 4, col.text + lineWidth(a, r.line) + again_gap, down), hb, again_note, t.dim); + if (r.kind == .bare_line and r.first) rv.faded(kept).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); + // `stack`: the row's left edge to the disassembly's. + const ix = std.math.lerp(std.math.lerp(0, seam, across), to_x, then_left); + const right = std.math.lerp(a.box.w, to_x + a.to.disasm.w, then_left); 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) })); + wash.current(.{ .x = ix + 2, .y = y, .w = right - ix - 4, .h = pitch }, theme.metric.plaque.pc, 0); + sk.draw.polygon(f, &.{ .{ ix + 20, y + pitch / 2 }, .{ ix + 10, y + 6 }, .{ 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_) }); + // (`stack`: ranged right in its column to the disassembly's address column, from 40.) + _ = rv.address(std.math.lerp(std.math.lerp(0, seam, across) + col.place_end - digits * adv, to_x + theme.metric.insn.address, then_left), 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); + if (bytes_go < 1) rv.faded(1 - bytes_go).text(.mono, size, col.bytes, base, hexBytes(rv, m, insn), t.dim); + const mx = std.math.lerp(std.math.lerp(mnemonic, seam + col.instr + 24, across), to_x + theme.metric.insn.mnemonic + widen, then_left); + disasm.bareRow(rv.moved(mx - theme.metric.insn.mnemonic, 0), insn, y); + // The pc's row: what it reads, its address and the chain from it, once decoded. + if (k == built.pc) if (a.rec.insn_touches[a.at][0]) |touch| { + _ = rows.chain(rv.faded(clamp01((placed.value(a.decode) - 48) / 12) * kept), mx + std.math.lerp(col.note, col.note_columns, across), y, m, touch.addr, a.box.w - 24, .{}); + }; }, } } - a.drawBytesRow(v, m, placed, built); + a.drawBytesRow(v, m, placed, built, mnemonic); } /// 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 { + fn drawBytesRow(a: *const Aligned, v: ui.Ui, m: Recording.Moment, placed: anim.Placed, built: anytype, mnemonic: f32) void { const clock = placed.value(a.bytes); if (clock < 0) return; const d = placed.value(a.decode); @@ -300,10 +377,33 @@ pub const Aligned = struct { 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); + if (text_in > 0) disasm.bareRow(rv.faded(text_in).moved(target_x - col.bytes + mnemonic - theme.metric.insn.mnemonic, 0), insn, y); } }; +const again_note = "again: its second run"; +const again_gap = 36; + +/// How far line `n` is indented in the file (its leading spaces; a tab as four). +fn indentOf(a: *const Aligned, n: u32) usize { + var it = std.mem.splitScalar(u8, a.rec.source, '\n'); + var k: u32 = 1; + while (it.next()) |l| : (k += 1) if (k == n) { + var w: usize = 0; + for (l) |ch| switch (ch) { + ' ' => w += 1, + '\t' => w += 4, + else => break, + }; + return w; + }; + return 0; +} + +fn lineWidth(a: *const Aligned, n: u32) f32 { + return @as(f32, @floatFromInt(a.sourceLine(n).len)) * adv; +} + fn hexBytes(u: ui.Ui, m: Recording.Moment, insn: disasm.Insn) []const u8 { var bytes: [16]u8 = undefined; var known: [16]bool = undefined; diff --git a/anim/src/aside.zig b/anim/src/aside.zig index 7cf6867..5400da5 100644 --- a/anim/src/aside.zig +++ b/anim/src/aside.zig @@ -46,7 +46,8 @@ pub const Aside = struct { /// 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. +/// `exit`, then the caption's gap) 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, @@ -61,15 +62,18 @@ pub const Asides = struct { 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; + var in_the_way = false; 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; + in_the_way = true; } else s.play(.{d.to(1)}, .{ .seconds = 18.0 / 60.0, .ease = easeGlide }); } - s.cursor = at; + // One it would stand on is gone first, then a gap: never two texts over each other. + s.cursor = if (in_the_way) at + time.caption_out + time.caption_gap else at; s.play(.{obj.fadeIn()}, .{ .seconds = time.caption_in, .ease = easeEnter }); a.shown[a.n] = obj; a.dims[a.n] = dim; @@ -100,19 +104,23 @@ fn easeGlide(x: f32) f32 { return theme.ease(.glide, x); } -/// How many lines `wrap` makes of `text`. +/// How many lines `wrap` makes of `text` (a newline breaks one where the author says). 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; + var count: usize = 0; + var paragraphs = std.mem.splitScalar(u8, text, '\n'); + while (paragraphs.next()) |paragraph| { + var it = std.mem.splitScalar(u8, paragraph, ' '); + var n: usize = 0; + count += 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; } - if (n > 0) n += 1; - n += seen; } return count; } @@ -129,25 +137,28 @@ fn width(u: ui.Ui, line: []const u8) f32 { /// 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; + var paragraphs = std.mem.splitScalar(u8, text, '\n'); + while (paragraphs.next()) |paragraph| { + var it = std.mem.splitScalar(u8, paragraph, ' '); + 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 (n > 0) { - line.append(u.f.scratch, ' ') catch break; - n += 1; - } - line.appendSlice(u.f.scratch, word) catch break; - n += seen - ticks; + out.append(u.f.scratch, line.items) catch {}; } - 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; } @@ -157,4 +168,6 @@ test "lines of at most forty characters, backticks not counted" { 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`")); + // A newline breaks where the author says. + try std.testing.expectEqual(@as(usize, 2), lines("And all of it at once:\nthe map.")); } diff --git a/anim/src/context.zig b/anim/src/context.zig index 9b466da..8e0156d 100644 --- a/anim/src/context.zig +++ b/anim/src/context.zig @@ -465,6 +465,23 @@ pub const Stack = struct { above: i32 = 0, /// Which box of its stage it is in: the map's `more` holds the stack continued. part: @import("stage.zig").Part = .stack, + /// The introduction's: its offsets, roles and spans come in a `column` at a time. + reveal: ?Reveal = null, + + /// The stack built a `column` at a time (design/README.md, 6. Verbs: + /// `column`; motion.md, chapter II): each part a clock the scene plays + /// (frames, negative before it begins), its rows coming in top down as + /// shown, 2 f apart, 12 f each; until then under the page. + pub const Reveal = struct { + offsets: anim.Value, + roles: anim.Value, + spans: anim.Value, + + /// How much of row (or span) `k`'s part is still under the page, its clock at `clock`. + fn hidden(clock: f32, k: f32) f32 { + return 1 - std.math.clamp((clock - 2 * @max(k, 0)) / 12, 0, 1); + } + }; pub fn draw(k: *const Stack, f: *sk.Frame, placed: anim.Placed) void { const box = k.stage.box(placed, k.part); @@ -710,6 +727,13 @@ pub const Stack = struct { const y = top + (@as(f32, @floatFromInt(n)) + slide) * m.row; if (y + m.row < body.y or y > box.h) continue; const addr = window[0] +% @as(u64, @bitCast(n * 8)); + // Not come in yet (`reveal`): the offset under the page (from past the pins to 172), the role with its row. + const hide_offset = if (k.reveal) |rv| Reveal.hidden(placed.value(rv.offsets), (y - top) / m.row) else 0; + const hide_role = if (k.reveal) |rv| Reveal.hidden(placed.value(rv.roles), (y - top) / m.row) else 0; + // The page laid over them is opaque whatever the panel's own fade (a faded page over faded text leaves some). + var page_u = u; + page_u.at.pose.opacity = 1; + defer if (hide_offset > 0) page_u.faded(hide_offset).fill(.{ .x = 120, .y = y, .w = theme.metric.slot.offset + 4 - 120, .h = m.row }, theme.color.page); // Who points here: a pin in the lane, landing with its link. const names = pins(u, now, is, was, addr, travel); var marks: rows.Marks = .{ .lead = lead, .first = now.writtenWatched(addr, 8, .memory, 0, watched_from, watched_to) }; @@ -779,6 +803,11 @@ pub const Stack = struct { const fades = ran < 1 and !std.meta.eql(word_was, word_is); if (fades) _ = rows.slot(vs.faded(1 - ran), y, room_w, now.before(), addr, below_rsp, &.{}, 0, marks, role.name, role.local, role_was); const hops = rows.slot(if (fades) vs.faded(ran) else vs, y, room_w, if (fades) now.moment() else now.shownAt(addr, 8), addr, below_rsp, if (gapped) &.{} else names.names, names.shows, marks, role.name, role.local, role_was); + if (hide_role > 0) { + // The role drawn, and the one it crossfades from while it does. + const role_w = @max(if (role_was.going < 1) u.width(.mono, size, role_at[0].name) else 0, u.width(.mono, size, role.name)); + if (role_w > 0) page_u.faded(hide_role).fill(.{ .x = room_w - theme.metric.slot.room - role_w - 2, .y = y, .w = role_w + 4, .h = m.row }, theme.color.page); + } // An arc's ends are rows on show (the row above the window, there for a slide, is not). if (y + m.row > top + 1 and y < box.h - m.pad - 1) at_rows.append(u.f.scratch, .{ .addr = addr, .y = y }) catch {}; const what_was: zt.telescope.Role = if (k_role < roles[0].len) roles[0][k_role].role else .none; @@ -843,12 +872,25 @@ pub const Stack = struct { pinAmount(u, x, top + place * m.row, names_, if (is.rsp < was.rsp) u.print("-{x}", .{bytes}) else u.print("+{x}", .{bytes}), amount_shows); // The rows it crossed, a 3-unit vermilion bracket in the outer arc lane. 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 if (lone != null and joinsBefore(pins(u, now, is, was, is.named(name) orelse 0, travel).names, name)) { + // Joining a pin in front of a name there (rbp onto rsp's row): its own head goes as it lands + // (the joined pin has one head, after the last name), never in the advance's frame. + const y_pin = top + place * m.row; + u.text(.mono_bold, size, x, theme.baseline(y_pin, m.row, size), name, t.primary); + const head = theme.metric.pin; + const hx = x + u.width(.mono, size, name) + head.row_gap; + const mid = y_pin + m.row / 2; + const v_head = u.faded(1 - goes(now, was, is, name)); + sk.draw.polygon(u.f, &.{ .{ hx + head.row_head[0], mid }, .{ hx, mid - head.row_head[1] / 2 }, .{ hx, mid + head.row_head[1] / 2 } }, v_head.at.pen(.{ .fill = ui.rgb(theme.color.region.stack) })); } else u.pinRow(x, top + place * m.row, names_, theme.color.region.stack); } - 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.faded(sp.shows), map.content(box.w), sp.y0, sp.y1, sp.name, sp.newest, sp.open); + for (spans[0..n_spans], 0..) |sp, i| if (sp.shows > 0) { + const sv = if (k.reveal) |rv| v.faded(1 - Reveal.hidden(placed.value(rv.spans), @floatFromInt(i))) else v; + if (sp.was) |was_name| { + rows.span(sv.faded(1 - renamed), map.content(box.w), sp.y0, sp.y1, was_name, sp.newest, sp.open); + rows.span(sv.faded(renamed), map.content(box.w), sp.y0, sp.y1, sp.name, sp.newest, sp.open); + } else rows.span(sv.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); } @@ -896,6 +938,12 @@ pub const Stack = struct { } /// Where `name` goes in a row pin of `others` and it, in the registers' order. + /// Whether `name` joining the pin of `others` comes before one of them (pwndbg's order). + fn joinsBefore(others: []const []const u8, name: []const u8) bool { + for (others) |o| if (index(o).? > index(name).?) return true; + return false; + } + fn pinPlace(u: ui.Ui, others: []const []const u8, name: []const u8) f32 { const size = theme.size.mono.context; var x: f32 = theme.metric.slot.lane; diff --git a/anim/src/descent.zig b/anim/src/descent.zig index ad3a5c5..96f6f2d 100644 --- a/anim/src/descent.zig +++ b/anim/src/descent.zig @@ -71,6 +71,50 @@ pub fn step(cur: cursors.Cursor, s: *anim.Frame, by: Recording.Stop) void { cur.step(s, to, plan, plan.total); } +/// A caller's window standing alone (the introduction, motion.md II2: the +/// first code on screen): a descent's head row (the function, its file +/// ranged right) over its call, the row the call returns to and the next, +/// each said as a place (`row`) for an aim, a call's callee named where the +/// annotation goes. `in` (frames): the head, then the rows 2 f apart, 12 f +/// each; `faint` (0 to 1): to half, as it stays under what follows. +pub const Caller = struct { + rec: *const Recording, + at: u64, + /// The call instruction's address. + call: u64, + box: Rect, + in: anim.Value, + faint: anim.Value, + + pub fn draw(r: *const Caller, fr: *sk.Frame, placed: anim.Placed) void { + const clock = placed.value(r.in); + if (clock < 0) return; + const u = (ui.Ui{ .f = fr, .at = placed }).moved(r.box.x, r.box.y).faded(1 - 0.5 * std.math.clamp(placed.value(r.faint), 0, 1)); + const moment = r.rec.moment(r.at); + var name_buf: [128]u8 = undefined; + const head = u.faded(std.math.clamp(clock / 12, 0, 1)); + const name = (moment.string(r.call, &name_buf) catch null) orelse ""; + head.fill(.{ .x = theme.stroke.rule, .y = 0, .w = r.box.w - 2 * theme.stroke.rule, .h = theme.stroke.rule }, theme.color.rule); + head.text(.sans_medium, theme.size.sans.micro, c.address, 18, u.print("{s}", .{name[0 .. std.mem.indexOfScalar(u8, name, '+') orelse name.len]}), t.primary); + head.text(.sans, theme.size.sans.micro, r.box.w - 12 - head.width(.sans, theme.size.sans.micro, r.rec.file), 18, r.rec.file, t.faint); + var addr = r.call; + var buf: [256]u8 = undefined; + for (0..3) |n| { + const insn = r.rec.instruction(r.at, addr, &buf) orelse return; + const y = m.row * @as(f32, @floatFromInt(n + 1)); + const rv = u.faded(std.math.clamp((clock - 12 - 2 * @as(f32, @floatFromInt(n))) / 12, 0, 1)); + _ = rv.address(c.address, rows.baseline(y), addr, .{ .ink = ui.rgb(theme.color.region.code_) }); + const row: Insn = .{ .addr = addr, .len = insn.len, .text = insn.text, .line = insn.line }; + disasm.bareRow(rv, row, y); + if (disasm.target(row)) |to| if (std.mem.startsWith(u8, insn.text, "call")) { + if (moment.string(to, &name_buf) catch null) |callee| rv.text(.mono, size, c.annotation, rows.baseline(y), ui.symbol(rv, callee[0 .. std.mem.indexOfScalar(u8, callee, '+') orelse callee.len]), t.primary); + }; + ties.anchor(u, .{ .row = addr }, .{ .x = 0, .y = y, .w = r.box.w, .h = m.row }); + addr += insn.len; + } + } +}; + /// The key of the head row over function `name`'s window (a `ties.Place.row`). pub fn headKey(name: []const u8) u64 { return std.hash.Wyhash.hash(0, name) | 1 << 63; diff --git a/anim/src/regroup.zig b/anim/src/regroup.zig index 431abd8..c9f618a 100644 --- a/anim/src/regroup.zig +++ b/anim/src/regroup.zig @@ -92,7 +92,8 @@ pub const Words = struct { } /// 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. + /// points into the heap (or, with none, the first other address), 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"); @@ -109,12 +110,17 @@ pub const Words = struct { const values = read(rec, moment); var heap: ?usize = null; var stack: ?usize = null; + var other: ?usize = null; for (values.words, 0..) |v, i| switch (rows.regionOf(m, v)) { .heap => heap = heap orelse i, .stack => stack = stack orelse i, - else => {}, + .none => {}, + else => if (i != walked) { + other = other orelse i; + }, }; - var three = [3]usize{ heap orelse 0, stack orelse 1, walked }; + // With no heap word in the 128 bytes, the first other address that is not the walked one. + var three = [3]usize{ heap orelse other orelse 1, stack orelse 2, walked }; std.mem.sort(usize, &three, {}, std.sort.asc(usize)); return .{ .walked = walked, .three = three }; } @@ -162,11 +168,11 @@ pub const Words = struct { 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)); + // The veil over every row but the walked one, while it is walked (the header too). + const veil = clamp01((r - 48) / 18) - clamp01((r - 270) / 18); + const header = clamp01(in / 18) * (1 - placed.value(w.placed)) * (1 - veil); 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); @@ -178,9 +184,10 @@ pub const Words = struct { 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); + // The bytes go first (9 f), then the addresses travel right to the slots' column: never over them. + const ax = std.math.lerp(at.address, w.panel.x + theme.metric.slot.address, glide((become - 0.25) / 0.75)); 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.drawBytes(row.faded(1 - clamp01(become * 4)), 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); @@ -258,7 +265,13 @@ pub const Words = struct { // 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); + if (m.string(value, &name_buf) catch null) |name| { + // Cut with `…` at the state column's edge, as the stack's rows cut theirs. + const sym = ui.symbol(u, name); + const fits: usize = @intFromFloat(@max((theme.layout.state.x + theme.layout.state.w - end - 10) / adv, 2)); + const shown = if (sym.len > fits) u.print("{s}\u{2026}", .{sym[0 .. fits - 1]}) else sym; + v.faded(clamp01(since * 10)).text(.mono, size, end + 10, base, shown, t.primary); + } return; } if (since >= 0) { @@ -277,13 +290,20 @@ pub const Words = struct { 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). + // A row near the dump's top has no room over it for the arcs at x2 (the camera's frame + // begins two rows over the header): there the bytes fly under the row, over the veiled + // rows, and the offsets and labels stand over it, the header's place (veiled with the rest). + const flip = w.walked < 4; + const dir: f32 = if (flip) -1 else 1; + // Under the row (over it, flipped), its offsets and labels on the page (the veiled rows there 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 page_x = at.bytes - 12; + const page_w = at.field_end - at.bytes + 24; + if (offsets > 0) u.faded(offsets).fill(.{ .x = page_x, .y = if (flip) y - 2 * pitch - 6 else y + pitch, .w = page_w, .h = 2 * pitch + 6 }, theme.color.page); + if (zeros_mark > 0) u.faded(zeros_mark).fill(.{ .x = page_x, .y = y + pitch, .w = page_w, .h = 2 * pitch + 6 }, theme.color.page); const close = glide((r - 192) / 12); for (0..8) |k| { const kf: f32 = @floatFromInt(k); @@ -294,7 +314,7 @@ pub const Words = struct { 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); + const lift = dir * 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; @@ -310,13 +330,13 @@ pub const Words = struct { } // 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); + if (off > 0 and s > 0 and s < 1) u.faded(off).fill(.{ .x = x - 3, .y = base + dir * pitch - lift - 18, .w = 2 * adv + 6, .h = 24 }, theme.color.page); + if (off > 0) u.faded(off).text(.mono, size, x, base + dir * 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); + u.faded(labels).text(.sans, theme.size.sans.tag, if (k == 0) x else x + 2 * adv - lw, base + dir * 2 * pitch + 2 - lift, label, t.dim); }; } // The zeros in front, bracketed (motion.md: 204 to 228). diff --git a/anim/src/root.zig b/anim/src/root.zig index 596ac57..5482285 100644 --- a/anim/src/root.zig +++ b/anim/src/root.zig @@ -52,6 +52,8 @@ const tying = @import("ties.zig"); pub const relations = @import("relations.zig"); pub const Ties = tying.Ties; pub const Place = tying.Place; +/// Says where a place is this frame, for a scene's own component the ties should find. +pub const anchor = tying.anchor; pub const Array = @import("array.zig").Array; pub const Loupe = @import("loupe.zig").Loupe; pub const syscalls = @import("syscalls.zig"); diff --git a/anim/src/segments.zig b/anim/src/segments.zig index d28d2dd..db9d28f 100644 --- a/anim/src/segments.zig +++ b/anim/src/segments.zig @@ -69,11 +69,18 @@ pub fn read(rec: *const Recording, at: u64, arena: std.mem.Allocator) Space { const maps = mo.mappings(arena) catch &.{}; var bands: std.ArrayList(Band) = .empty; var path_buf: [512]u8 = undefined; + // The tracer's own (its ring, its preloaded agent, and an agent's bss + // after it) are not the program's: left out, as mock 34 has them. + var tracers_end: u64 = 0; 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 (std.mem.indexOf(u8, name, "ztrace") != null or (name.len == 0 and mp.addr == tracers_end)) { + tracers_end = mp.addr + mp.len; + continue; + } 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] != '['))) { @@ -163,6 +170,12 @@ pub fn size(buf: []u8, bytes: u64) []const u8 { return std.fmt.bufPrint(buf, "{d} {s}", .{ @as(u64, @intFromFloat(@round(v))), units[u] }) catch ""; } +/// `name` cut with `…` to fit `room` (a library's long file name in the program column). +fn cut(u: ui.Ui, name: []const u8, room: f32) []const u8 { + const most: usize = @intFromFloat(@max(room / ui.advance(m.mono), 2)); + return if (name.len > most) u.print("{s}\u{2026}", .{name[0 .. most - 1]}) else name; +} + 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 '-' }; } @@ -433,9 +446,9 @@ pub const Segments = struct { 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; + // An edge two units share takes the ink of the one that starts there (its lowest address). + for (out.items) |*r| if (r.kind == .edge) for (l.space.units[c.units[0]..c.units[1]]) |un| if (un.lo == r.addr) { + r.ink = un.ink; }; return out.items; } @@ -484,7 +497,7 @@ pub const Segments = struct { 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); + } else v.text(.mono, m.mono, g.x + m.name, y + 7.2, cut(v, un.name, theme.layout.program.w - 12 - m.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); }, |
