summaryrefslogtreecommitdiff
path: root/anim
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-11 13:22:07 -0300
committerGabriel Schneider <[email protected]>2026-11-11 13:26:44 -0300
commit2f27fbe0aa2da0f85d599c1e5f3ac14d025f590c (patch)
tree140a291011e453888ed87b7a43d75f538abb9fae /anim
parent576a78b4de02c4c6566c327d85c465554d293975 (diff)
downloadcomputeranim-2f27fbe0aa2da0f85d599c1e5f3ac14d025f590c.tar.gz
computeranim-2f27fbe0aa2da0f85d599c1e5f3ac14d025f590c.zip
canim: the graph of a function's blocks
From the user. A blocks lens draws a function's basic blocks as boxes with their offsets and visit counts, loops in their lanes with the turn count, the path's last edges gold, the pc's block gold, and the disassembly as its view; leave and turn carry the mark along an edge, with the compare's flags framed for the edge chosen. On the map the graph takes the disassembly's place with its rows. The layout is a pure function, tested on sort, reverse and libc's open (twelve blocks the run never reached fold into six boxes). The sort scene walks one trip of its inner loop block by block and sprints the middle on the map. Every scene's linear rows carry a rule and a count at each block's head. The stack's panel no longer reads registers from the file on every draw: a frame is 20 ms where it was 57. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'anim')
-rw-r--r--anim/README.md156
-rw-r--r--anim/build.zig5
-rw-r--r--anim/scenes/gallery.zig107
-rw-r--r--anim/scenes/sort.zig100
-rw-r--r--anim/src/context.zig8
-rw-r--r--anim/src/descent.zig9
-rw-r--r--anim/src/disasm.zig37
-rw-r--r--anim/src/graph.zig1436
-rw-r--r--anim/src/map.zig59
-rw-r--r--anim/src/root.zig10
-rw-r--r--anim/src/theme.zig90
-rw-r--r--anim/src/ties.zig4
-rw-r--r--anim/test/gallery.script8
-rw-r--r--anim/test/gallery/atoms.pngbin104960 -> 104822 bytes
-rw-r--r--anim/test/gallery/graph-map.pngbin0 -> 158401 bytes
-rw-r--r--anim/test/gallery/graph.pngbin0 -> 76868 bytes
-rw-r--r--anim/test/gallery/hello.ztracebin0 -> 86647 bytes
-rw-r--r--anim/test/gallery/loupe.pngbin237245 -> 237479 bytes
-rw-r--r--anim/test/gallery/none.pngbin226785 -> 226735 bytes
-rw-r--r--anim/test/gallery/rest.pngbin233677 -> 233721 bytes
-rw-r--r--anim/test/gallery/rows.pngbin140384 -> 140530 bytes
-rw-r--r--anim/test/gallery/sort.ztracebin0 -> 775616 bytes
-rw-r--r--anim/test/gallery/step.pngbin232337 -> 232385 bytes
-rw-r--r--anim/test/gallery/subject.pngbin209937 -> 210007 bytes
-rw-r--r--anim/test/recordings.zig105
-rw-r--r--anim/tools/chart.py12
26 files changed, 2001 insertions, 145 deletions
diff --git a/anim/README.md b/anim/README.md
index cffb7aa..1401ad7 100644
--- a/anim/README.md
+++ b/anim/README.md
@@ -98,12 +98,12 @@ on a primary onset's frame and nothing on it changes in one.
| `title` | the series (six programs, named) and the legend of the marks the scenes use | a card | |
| `fib` | recursion: each call a frame and a node | `subject`, `calls` | `steps` |
| `list` | a list reversed: each `next` turned; it opens on the map and dives into the frame; after the first load the pointer loaded opens in a loupe, and the `next` in it one deeper | `subject`, `nodes` | `list` |
-| `sort` | insertion sort: compares and swaps | `subject`, `array` | `sort` |
+| `sort` | insertion sort as its blocks: one trip of the inner loop walked a block at a time (each block's rows a cascade onto its last, then `leave` along the edge it takes: the compare's chips framed, the edge drawn gold, the mark along it, the target's count struck; `turn` up the inner loop's lane), the next trip at a run, the long middle a sprint on the map (`surface`, the gold going block to block, the counts growing, `dive`), the last value's trip walked again, the way out run | `blocks` (`graph.Blocks`; the disassembly its view over the state column, the source alone in the program column); on the map the graph with its rows (`Context.Options.graph`) | `sort` |
| `syscalls` | glibc's `hello`, by instruction from main: `open` followed down (main's call ringed, the PLT's stub passed, libc's wrapper run to its last three instructions and those walked, the kernel's window at the floor with the call in C's form and `returns`, and back up), then `read`, `close`, `write` stepped over, each in three parts: the instructions before the call a cascade onto the call row, the crossing from that row with the kernel's window opening under it (the rows after it making room), the stores after it 18 f after rax | `beside`, `syscalls` (the calls so far, as strace writes them); the disassembly a `descent` (`Context.Options.descent`) | `hello` |
| `heap` | glibc's malloc under a list of three: three pushes, two pops, a push, told by motion.md's storyboard (nine captions; pushes 2 and 3 at a run; each `free` and the `malloc` that takes the head back walked; what the storyboard does not narrate runs: the calls into push and pop with their prologues, the returns to main but the one under the second free's long caption); the chunks by fields (`size`, `protected`, `key`), the program's list over the tcache's free list with its `bin` (a chunk in neither list drawn in the program's row, unarrowed), the tcache in a loupe over the stack, its stem down the gutter from its chunk's row, opened in each step that changes the tcache and closed at its advance | `subject`, the scene's own drawing (`nodes` in two rows) and view, both on its map too | `heap` |
| `zoom` | one line of C as its instructions, the call's bytes | `none`, the camera at x1.16 on the instructions and the stack in one view, cause and effect (the stack's window set before the move and held); the map's drawing the calls of `fib` (`Map.drawing`) | `steps` |
| `sqlite` | a row's way to disk in SQLite: from `sqlite3_step` to `sqlite3BtreeInsert` (three continues, the row's struct on the stack), the commit's `unixWrite` nine calls under `main`, `pwrite64` of page 2 into the kernel, and the page read as a B-tree leaf | `subject`, `backtrace` (`Context.Options.backtrace`), then `syscalls`; its view a `hexdump.Dump`; as the caption names them, rdi's `BtCursor` opened in a loupe, then rsi linked to its row in the view (`Context.tiesWith`); the page's fields named a group at a time, 2 s apart; the backtrace lens drawn on its map | `sqlite` |
-| `gallery` | every block in its states, a page or a state each 3 s | all three | `gallery` |
+| `gallery` | every block in its states, a page or a state each 3 s; at 21 s the `blocks` lens (at rest, `leave` mid-way, `turn` landing, libc's `open` folded), at 24 s the graph with its rows as the map draws it, at half size (sort's panned to its loop; `open` at its `syscall`, the blocks never reached folded) | all three, `blocks` | `gallery`, and for the graph pages `test/gallery/sort.ztrace` and `hello.ztrace` |
## The blocks
@@ -121,15 +121,17 @@ every state the code has in the `gallery` scene.
| | `descent` | `src/descent.zig` (`Descent`, `step`) | the disassembly a call deep: the root's window on top (main), each callee's under the row that called it (that row `waiting`, ringed, its annotation the call in C's form), a PLT stub gone through kept as its `jmp`, `pass`; a head row over each window (the name at 40, Go Medium 18; what it is ranged right: `libc.so.6 · no source`, `the PLT · its jmp reads open@got, 403fd8`, `the PLT, jumped through`); the call lane at x 14; the nearest caller takes the rows the deepest does not use, all above its call row (one row after it, the return row, the callee's head directly under that), and where its function starts first the rest go back to the deepest window; three code windows at most, the rest one `more` row; each window's addresses its own width; the tag the deepest function and its module (`open · libc.so.6`); at a `syscall` (and the stop after it) the floor's three rows are the kernel's: in a crossing the lane down from the plaque (3, vellum), the window filling down, `the kernel` and `openat, system call 257`, the call in C's form, `returns` struck, `rcx, r11: clobbered`, the answer climbing the lane (24 f, `glide`), the window drawing back at the advance. A crossing stepped over opens the same window under the call row, past the arguments hanging under it, the rows after them making room (18 f) and closing up with the window. Every row keyed (an address, a head), so a step is the rows both stops have gliding and the rest leaving or arriving on its verb's timetable, in two moves, never at once: `descend` (2.9 s: the given-up rows fade 66–78, the kept ones glide 78–96, ring 84–96, lane 96–114, head 108–120, rows 114–138), on through a stub's `jmp` (2.2 s), `ascend` (2.5 s: the window closes 48–72, lane back 72–84, the kept rows glide 84–102, the given-up ones fade in 102–114), `cross` (`syscalls.step`); a row whose state the verb changes (the call row to `waiting` with its hanging arguments, a jmp to `pass`, a waiting call taking its result) crossfades; with it the source's line `waiting` (ringed, the tag `main waits at line 6 · the pc is in libc's open`, crossfading over the 12 f before the advance, as a stack span's name does) and the thread waiting (primary, 2) |
| 3 panel | `registers`, `stack`, `backtrace` | `src/context.zig` | rip's value crossfading over the advance, a flag set or cleared over the 6 f it lands in (`rows.RipWas`); the backtrace newest at the bottom, `#0` last, the oldest dropped from the top (`newest at the bottom · 2 more above`), as a panel, the calls lens's view, the backtrace lens and on the map, all one block (`backtraceRows`: frames leave, slide and arrive; what a kept row says that changes, its depth, its pc's offset, its line, and the tag's count, crossfades, 12 f before the advance); the stack's window on `[rsp, rbp+16)` keeping the touched word in view, its tag the first and last word shown (`rsp+20 to rsp+48`), a word's role and offset, and the tag, crossfading (12 f from memory's strike) where the step changes them, rsp and rbp's pins travelling 18 f after rsp's strike (`time.pin`, keeping their place among the names they leave and join), at rest only where the register points; a window that moves farther than its own height (six or eight slots) fades out and in round memory's strike (12 f each; at a run half its step each), and what it brings, and what changed before it, is plain; while rows slide they keep their roles (those of both windows are read), and a word becoming a saved rbp, or no longer one, has its arc go or come with its role; a frame's name longer than the spans' room shortens the chains beside it; `call` spans and the backtrace (`#n`, name, arguments at 384, file and line) from `zt.telescope.frames`, the frames two stops share sliding to their new depth; the backtrace as a lens's drawing with `Context.Options.backtrace` |
| | `source` | `src/source.zig` | the whole file in a window, its middle third; the trail's bars crossfading with the advance; numbers as wide as the program's largest line (SQLite's six digits), the text moved right by as much; a window going farther than its own height fades out and in |
-| | `disasm` | `src/disasm.zig` | 17 rows (9 as subject), fewer above near a function's start; the line column as wide as the source's; two windows that overlap scroll, others fade out and in; its tag's function (a descent's tag too) crossfading with the advance |
+| | `disasm` | `src/disasm.zig` | 17 rows (9 as subject), fewer above near a function's start; the line column as wide as the source's; two windows that overlap scroll, others fade out and in; its tag's function (a descent's tag too) crossfading with the advance; a 2-unit `rule` across over each block's first row (not its function's), and the block's visits by the moment shown ranged right, faint (`×3`), where the row's annotation leaves room, crossfading with the advance (`graph.Rules`, a descent's windows too); the rows clipped above the panel's padding (a row scrolling out takes its rule with it) |
+| | `graph` | `src/graph.zig` (`read`, `fold`, `place`, `layout`, `Blocks`, `Map`, `Rules`) | the function's blocks from the recording (`zt.telescope.blocks`; a block of `nop` padding nothing goes to left out), their edges by how each ends, and the run over them by the moment shown: each block's visits, each edge's count, the last three edges taken, the pc's block; counted where one stop in the function follows another across an edge, and only for a recording that stops at every instruction (`exact`; by line, visits only, every edge `rule`). Laid out (`place`, pure, by the whole recording's counts, so it never moves as they grow): back edges by a walk from the entry in address order, layers by the longest forward path, the blocks that return last; in a layer a block under the predecessor that reached it most often, then under the one it falls from, a taken target never taken right of it, the more travelled first where two want one column; columns a pitch apart (the map's as wide as the pc's block with its annotation, 928; the lens's 300); a forward edge out of the bottom on its target's side (40 in, 16 in the lens; the middle when straight below), along the gap's middle, into the target's top (arrivals 48 apart), through the right channel (32 out) where it would cross a block; a back edge down 14, out to its loop's lane (16 apart from 24, the outermost farthest), up, into its header's head. Too tall for its place (`layout`), in the catalogue's order: each run of blocks the whole recording never reached (neighbours in address order joined by an edge; split where folding would make a loop the code does not have) one dashed box, `+5b to +b9 · 5 blocks never reached`; then blocks over 16 rows to four, `more`, four (not the pc's); then it pans, whole layers, to the largest loop the pc's block is in that fits, else the pc's block in the middle. The static part kept per function and form (`memo`), the moment's counts counted again each frame. `Blocks`: the lens's drawing, boxes 24 high, 60 and 10 a row wide (132 at least), the offset, the visits (`dim`), reached `raised`, not yet on the page, a fold dashed, the pc's framed gold 3; the edges never taken `rule`, taken `dim`, the trail's golds (the newest 3), a loop's turns on its lane (Go 18). `Map`: on the map two columns wide beside the source, each block a head (its address, `lines a to b`, `×n` primary) over `insn`'s rows moved 52 left, the offset where the address was (ranged right at 72); the annotation only in the pc's block (and its rows that ran in the last three steps, with their trail bars), which widens to its column, its rows' annotations kept inside it; a conditional's counts by its ports; a direct call's hook out of the block's side and back a row down, the callee named (`swap`); under a return the way out (stub 14, bar 28), a tail call's stub and name, an indirect jump's `targets not known` |
+| | `blocks` lens | `src/graph.zig` (`Blocks`), `theme.layout.split.blocks` | its drawing the graph as shapes over the program column (296 high), which then holds the source alone (twenty lines); its view the disassembly itself, seven rows over the state column; the registers over six words; no thread (the disassembly is not under the source: it fades with the distance between them) |
| | `hexdump` | `src/hexdump.zig` | malloc's words: a field read as `size` (`0x20 a m P`, the bits as rip's flags) or `protected` (`→ 405050 ^ 405`, `NULL ^ 405`, its arc to the pointer it protects), and a field's `note` (Go 18 faint: `key`, `top`, `slots left`); a lens's view (`hexdump.panel`): `bytes` rows and their reading, veiled at a sprint; a row whose bytes the step changed where the recording saw no write (memory nothing watches, read anew at a stop), or that became known, crossfading over 12 f (`rowPasses`); one reading for every dump (`hexdump.reading`), a list a character apart, a typed number as its type reads (`char` quoted, `bool` by name); the address room once a dump, by its widest; `Dump`, a view of a region by the fields a scene gives it (`Field`: where, how long, read as a number, big-endian or not, a pointer, text, or left to the eye), the rows that hold a field and a `more` row for each stretch left out, and where those rows outnumber the window, a window kept round the first row a mark or the step is on, the rest folded into `more` rows; marks the scene plays, and the step's only from the stop it came in at (`since`); the bytes a syscall was given as the kernel took them (`given`), and only what the debugger read at the stop (`seen`) |
| | `loupe` | `src/loupe.zig` (`canim.loupe`, `Loupe.open`, `.close`) | from a register, a loupe's field, or a dump's row read by the scene's fields (`.row`: malloc's tcache, `num_slots[0]` and `entries[0]`, the rest `more`); five rows round a pointer's target, lifted (`color.lift`, `lift_tag`, `shade` 12 and 16; 20 and 26 deeper), hanging from its pointer (its tag on the row's foot, 92 left of the digits, clamped to the stage), a 4-high bracket under the digits its stem, the target's fields framed bright; one opened from the band's view lies over the stack, never the registers, its stem down the gutter's right lane (x 962), its fields struck once it has opened (6 f after, 6 f apart: `Fields.struck`); a deeper one with no room to the right hangs under the one it opens from, its stem down to its tag; a note that does not fit is cut at a word with `…`; `open` 36 f (read 6, stem and rise 18 with the shadow, the frame 6 f after), `close` 12 f; it opens again after a close |
| | lenses: `nodes`, `array`, `calls`, `syscalls`, `backtrace` | `src/nodes.zig`, `src/array.zig`, `src/calls.zig` (`Tree`), `src/syscalls.zig` (`Crossing`), `src/context.zig` (`Backtrace`, `lens`) | the drawing in `band` (over the program column), its view in `view` (over the state column): `nodes` and `array` a dump (`hexdump.panel`), `calls` the backtrace with arguments, `syscalls` the calls so far as strace writes them (oldest on top, the newest coming in once it is back, held; the rows make room for it, a full log scrolling a row, 24 f, `glide`, the count above crossfading; out of room a row gives up its faint names first, then its arguments from the right, and keeps what it returned; a path by its string, the kernel's capture where the stop did not read it) over the bytes the call crossing in the step is given or fills (in with the argument framing, 12 f out and 12 f in; what a read fills landing a byte every 2 f, struck, the stack's words with them; a write's buffer framed as read while the kernel takes it; cleared with the log's new row: between crossings `no call crossing now`), `backtrace` (eleven rows as subject, five beside) a `Dump` the scene gives; an `under` pin's names in the order they came, the first left, faded out through the middle of a register's joining or leaving (`Pins.text`); a call node's look (gold, ringed, done) crossfading as the tree's moment changes (6 f), and at a sprint a new node coming in over its step; a left child's result on its left shoulder, off its parent; a view's row a register points into carries the register's `pin` at the row's end (`hexdump` `rowPin`), the reading ending before it; at a sprint the array's pins hold and move where a phrase ends |
| 4 tie | `thread`, `link` (and its `pin`), `projection`, `echo` | `src/ties.zig` | panels publish places (`anchor`); `Ties`, added last, draws in the channels: a link climbs to a node by the gutter's left lane and goes down to a slot by the right, its name riding its head, a target past the stack's window a stub and a row count; to a view's row (`dump_pin`) by the left lane, into the row's edge; on a beat the narration names (`Context.tiesWith`, `ties.Beat`): the register framed as read, then its link, held, drawn back with the frame; a projection from the view's bytes, or from a frame's return address to its call's node |
-| 5 frame | `map`, `minimap`, `dive`, `surface` | `src/map.zig` (`Map`, `split`, `minimap`), `Context.map` | the working frame's panels bound to a stage of 3840 by 2160 (`map.split`, packed by content: each box as long as the most rows its panel has wanted, `measure`, the backtrace's as long as the deepest call chain of the scene's way, so that it is packed once; the drawing, the source and under it the backtrace; the disassembly; the view (none where nothing draws one), the registers, and in the column's rest the stack going on, `stack, continued`; the stack from 16 words above rsp; and the first two columns' rest (`Split.spare`) given, in the catalogue's order, to `map.Spare`: a five-row dump round each place the registers (pwndbg's order, not rsp or rbp) and the 64 words up from rsp point at outside the stack and the code, none within another's rows, then the loupes open (`Map.loupes`, lifted, nine rows), then the key (the recording and the moment, the marks as drawn, the inks); only then page); its rows published while `ui.mapping` (the dumps' too); the working windows outlined (2 `dim`, 4 out), each window published with where its step took it from and how far its scroll has gone, so its outline glides; the thread across the gutter (x 1008), gold; a scene without a band can put a drawing on its map (`Map.drawing`); the minimap the same at a twentieth under the clock (text as 1-unit strokes at 45% toward the page, marks as blocks; `ui.strokes`), drawn six times into the host's images, each from the cursor's mirror a frame later (6 to 11 f behind: `Cursor.lagInto`, `time.minimap_fade`) and averaged, so that every change on it fades in over 6 f and nothing on it changes in a frame; its outlines on whole pixels at rest and between them only as they glide; a mark no working window shows (a word struck or read on the stack or in a dump the map draws) flashing on it (a ring 8 to 14 in the mark's colour, 12 f), the count ranged right at 1648; `Map.show`, `.dive` (outlines bright 6 f, veil to 24, each panel from its window to its place 60 f, 6 f apart, a panel drawn over another setting off first (drawing, view, disassembly, stack, source, registers) so that none in its place is crossed by one on its way, opaque on the way, its rows on the window's rows, its tag and rules in at the end; the map falls into the minimap's corner over 72 f, its scale on the logarithm, its corner going toward the minimap's as the scale does, small it is drawn over the panels with its edge, and crossfades into the minimap's drawing at 90–96; 120 f and 30 f still), `.surface` (the dive back, 84 f, then 30 f still); per-panel transforms through `context.at` (`context.zoomed`) |
+| 5 frame | `map`, `minimap`, `dive`, `surface` | `src/map.zig` (`Map`, `split`, `minimap`), `Context.map` | the working frame's panels bound to a stage of 3840 by 2160 (`map.split`, packed by content: each box as long as the most rows its panel has wanted, `measure`, the backtrace's as long as the deepest call chain of the scene's way, so that it is packed once; the drawing, the source and under it the backtrace; the disassembly; the view (none where nothing draws one), the registers, and in the column's rest the stack going on, `stack, continued`; the stack from 16 words above rsp; and the first two columns' rest (`Split.spare`) given, in the catalogue's order, to `map.Spare`: a five-row dump round each place the registers (pwndbg's order, not rsp or rbp) and the 64 words up from rsp point at outside the stack and the code, none within another's rows, then the loupes open (`Map.loupes`, lifted, nine rows), then the key (the recording and the moment, the marks as drawn, the inks); only then page); its rows published while `ui.mapping` (the dumps' too); the working windows outlined (2 `dim`, 4 out), each window published with where its step took it from and how far its scroll has gone, so its outline glides; the thread across the gutter (x 1008), gold; a scene without a band can put a drawing on its map (`Map.drawing`); with `Context.Options.graph` the disassembly is the function's graph (`graph.Map`), two columns wide beside the source, the registers over the stack in the last column, no band (the lens's window on it is the graph whole, the disassembly's the box round the rows it shows), no thread line (the gold line number is the thread); the minimap the same at a twentieth under the clock (a graph's blocks outlined, its edges a pixel, the pc's block framed gold) (text as 1-unit strokes at 45% toward the page, marks as blocks; `ui.strokes`), drawn six times into the host's images, each from the cursor's mirror a frame later (6 to 11 f behind: `Cursor.lagInto`, `time.minimap_fade`) and averaged, so that every change on it fades in over 6 f and nothing on it changes in a frame; its outlines on whole pixels at rest and between them only as they glide; a mark no working window shows (a word struck or read on the stack or in a dump the map draws) flashing on it (a ring 8 to 14 in the mark's colour, 12 f), the count ranged right at 1648; `Map.show`, `.dive` (outlines bright 6 f, veil to 24, each panel from its window to its place 60 f, 6 f apart, a panel drawn over another setting off first (drawing, view, disassembly, stack, source, registers) so that none in its place is crossed by one on its way, opaque on the way, its rows on the window's rows, its tag and rules in at the end; the map falls into the minimap's corner over 72 f, its scale on the logarithm, its corner going toward the minimap's as the scale does, small it is drawn over the panels with its edge, and crossfades into the minimap's drawing at 90–96; 120 f and 30 f still), `.surface` (the dive back, 84 f, then 30 f still); per-panel transforms through `context.at` (`context.zoomed`) |
| | `stage` and `relayout` | `src/stage.zig` | splits `none`, `beside`, `subject` (`theme.layout.split`): the band is `band` (the drawing) and `view`; `relayout` fades the drawing round its move; `holdStack` holds the stack's window for the camera, with `free` slots |
| | `clock`, `caption`, `veil`, `card` | `src/words.zig`, `src/root.zig` (`Narrator`, `Camera`) | the clock still while a step plays, its piece fading in once the mark lands; a caption stays its reading time; the title card is `scenes/title.zig` |
-| 6 verb | `fire`, `arm`, `strike`, `cool`, `read`, `extend`, `link`, `project`, `travel` (pins), `advance`, `cascade`, `continue` (`Cursor.continue`, `Plan.wave`), `run`, `sprint`, `establish` (`Context.establish`: a drawing's elements fade in rising, no lines), `scroll`, `relayout`, `visit`, `say` | `src/cursor.zig` (the timetable), the blocks (each draws its state at the instant) | tokens in `theme.time`; a level gives up its slot when nothing it touched is drawn (`Recording.drawn`); a label that changes in place with a step crossfades over the 12 f before the advance, or from the step's start where that is sooner (`Now.renaming`); at a run what marked a value goes with the run's ease (8 f) and a pin changes over it; at a sprint an ember and what grows come in over the step (4 f), never in a frame |
+| 6 verb | `fire`, `arm`, `strike`, `cool`, `read`, `extend`, `link`, `project`, `travel` (pins), `advance`, `cascade`, `continue` (`Cursor.continue`, `Plan.wave`), `run`, `sprint`, `establish` (`Context.establish`: a drawing's elements fade in rising, no lines), `scroll`, `relayout`, `visit`, `say`; `leave` and `turn` (`graph.leave`, from a block's last row, `graph.toEnd` to get there: fire 0–12; the compare's chips framed as read from 18 (the CPU's slot, the jump's echo), held while the edge draws gold, head first, 24–42; the mark (a gold plaque, half size in the lens) along the edge 42 to 42 + 12 f and 2 f a 100 units of the lens's edge (30 f at most), up the lane for a turn; landing, the advance; the target's count armed and struck 6 f after (a turn's lane count as it lands), cooled to nothing by the step's end; the trail recoloured over 18 f from the landing; a rest of 42 f from the landing); over a graph at a walk of several blocks, a run or a sprint the trail, the counts and the gold crossfade with the advance (8 f at a run, the step at a sprint), the pan gliding with it | `src/cursor.zig` (the timetable), `src/graph.zig` (`leave`, `instant`), the blocks (each draws its state at the instant) | tokens in `theme.time`; a level gives up its slot when nothing it touched is drawn (`Recording.drawn`); a label that changes in place with a step crossfades over the 12 f before the advance, or from the step's start where that is sooner (`Now.renaming`); at a run what marked a value goes with the run's ease (8 f) and a pin changes over it; at a sprint an ember and what grows come in over the step (4 f), never in a frame |
`Recording` (`src/Recording.zig`) is the index every block reads: lines,
calls, syscalls, allocations, which registers and bytes each moment changed
@@ -157,9 +159,14 @@ and no canary stepped through that the source does not say.
| `heap` | `heap.c` (design/mock/heap.c), the system's clang and glibc, dynamic, as `hello` | every line, `--from main`, and pop's `call free@plt` (`break_call`: a breakpoint objdump finds, which a line-stepping recorder now stops at) | 77, 59 KB | `--heap` (`ztrace.heap.glibc`: chunks, the tcache, bins, the arena); `-w 0x405000,1024` (the chunks' bodies) and `-w head` (the global the list hangs from: unwatched, a recording keeps a global as last seen) |
| `sort` | `sort.c`, `ztrace cc --instr vars --only sort,a` | every instruction, `--from main` | 4396, 751 KB (5154, 799 KB at `--stack 256`) | `--ring`: the variables' names and types; each `__zt_var` call is stepped (4 of them) |
| `list` | `list.c`, `ztrace cc --instr vars --only push,reverse,head` | every instruction, `--from reverse` | 2920, 743 KB (2913 the pass before: the count differs by a few moments from one recording to the next, not looked into; 3369, 782 KB at `--stack 256`) | `--alloc --preload` the agent: allocator headers are read; the pushes (the agent's and the runtime's first calls) run through |
-| `gallery` | the old `list` by line, kept in `test/gallery/list.ztrace` | | 134, 88 KB | the gallery's pictures depend on the drawing only |
+| `gallery` | the old `list` by line, kept in `test/gallery/list.ztrace`; for the graph pages `sort` and `hello` as recorded on 11 November (with blocks), kept in `test/gallery/sort.ztrace` and `hello.ztrace` (`gallery_sort`, `gallery_hello`) | | 134, 88 KB; 776 KB; 87 KB | the gallery's pictures depend on the drawing only |
| `sqlite` | `sqlite.c` (three rows into a table, committed) with SQLite 3.50.4's amalgamation, system clang `-O2 -g -fno-omit-frame-pointer -no-pie -fno-stack-protector -Wl,-z,now`, glibc | `--by break`: `sqlite3_step`, `sqlite3BtreeInsert`, `unixWrite`, and the instruction after its `pwrite64` (found by objdump); every syscall recorded, `-s 4096` | 181 (42 stops, 138 syscalls), 233 KB (139 KB) | made only with `-Dsqlite=DIR` (default `~/05-genizah/sqlite/sqlite-amalgamation-3500400`), in a new temporary directory each time; `sqlite3.c` is embedded as the source |
+Every recording the build makes carries each function's blocks
+(`zt.telescope.blocks`, at the first stop in it) and a `branch` event at
+each stop on a jump: the graph and the linear rows' block rules read them.
+The old `list` of the gallery has none, and draws none.
+
A recording that begins inside a call (`--from`) has that call open from
its first stop (`Recording.index`, from the frame chain). Writes are the
bytes whose value changed, to the element the instruction stored (its
@@ -173,7 +180,9 @@ holds is named from its module's first mapping (`list+2c89`).
`tools/chart.py chart` on the 1080p exports (the designer's
`design/motion/motion.py`, its checks and its regions told where canim's
-panels are; the clock, ranged right at 1648, its own region, whose count
+panels are (sort's the `blocks` split's: the drawing, the view that is
+the disassembly, the source alone, the registers and the stack); the
+clock, ranged right at 1648, its own region, whose count
changing in a frame is no POP; the caption's box from 940, under the
clock's count and a run's step counter; the minimap its own region,
secondary action, and its one-frame changes POPs). POP: any region still
@@ -198,67 +207,43 @@ under the caption, which does not change.
| | fib | list | sort | syscalls | zoom | sqlite | heap | gallery |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
-| POP, before / after | 14 / 11 | 14 / 13 | 8 / 6 | 26 / 25 | 7 / 11 | 7 / 8 | 21 / 17 | 5 / 8 |
-| of them the minimap's, before / after | 5 / 5 | 7 / 6 | 2 / 1 | 16 / 17 | 4 / 6 | 3 / 3 | 9 / 8 | 3 / 4 |
-| POP exempt, before / after | | keyframe 3 / | | keyframe 2 / | | keyframe 2 / | | cut 3 / cut 3 |
-| CROWD, before / after | 4 / 4 | 8 / 9 | 6 / 7 | 3 / 3 | 3 / 1 | 0 / 1 | 15 / 16 | 4 / 4 |
+| POP, before / after | 11 / 12 | 13 / 13 | 6 / 3 | 25 / 25 | 11 / 12 | 8 / 8 | 17 / 19 | 8 / 9 |
+| of them the minimap's, before / after | 5 / 5 | 6 / 6 | 1 / 2 | 17 / 17 | 6 / 6 | 3 / 3 | 8 / 10 | 4 / 4 |
+| POP exempt, before / after | | | | | | | | cut 3 / cut 5, keyframe 1 |
+| CROWD, before / after | 4 / 3 | 9 / 8 | 7 / 10 | 3 / 3 | 1 / 1 | 1 / 1 | 16 / 16 | 4 / 5 |
| METRONOME, before / after | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 |
| CAPTION up over 720 f, before / after | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 |
-| CAPTION under its reading time, before / after | not paired / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | |
-| CAPTION picture moving within 60 f, before / after | 1 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 |
-| AIR, before / after | 0 / 0 | 2 / 0 | 0 / 1 | 2 / 0 | 1 / 1 | 5 / 2 | 0 / 0 | 0 / 1 |
-| seconds, before / after | 42.0 / 42.0 | 84.5 / 81.7 | 42.2 / 42.2 | 89.2 / 83.8 | 33.6 / 33.5 | 64.6 / 62.9 | 78.1 / 78.6 | 11.0 / 21.0 |
+| CAPTION under its reading time, before / after | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | |
+| CAPTION picture moving within 60 f, before / after | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 | 0 / 0 |
+| AIR, before / after | 0 / 0 | 0 / 0 | 1 / 1 | 0 / 0 | 1 / 1 | 2 / 2 | 0 / 0 | 1 / 1 |
+| seconds, before / after | 42.0 / 42.0 | 81.7 / 81.7 | 42.2 / 63.8 | 83.8 / 83.8 | 33.5 / 33.5 | 62.9 / 62.9 | 78.6 / 78.6 | 21.0 / 27.0 |
-"Before" is the last pass's "after", measured with x264's adaptive
-quantization, whose keyframes re-quantized still regions (the `keyframe`
-exemptions, and a still picture broken in two: sort's last 4.6 s, an AIR
-now). zoom's POPs grew because its camera now shows the disassembly and
-the stack's chains with the stack (annotations taking their value in
-place, 18.10 and 19.60 s; offsets crossfading, 12.80 and 16.40 s);
-the gallery's because each state is held 3 s and its view re-windows at a
-state's first frame (9.00, 12.62, 15.40 s), as before.
+"Before" is the last pass's "after". The sort scene is new (the `blocks`
+lens and the map's graph, against the array scene's 42.2 s), and the
+gallery has two pages more; the other scenes changed only by the linear
+rows' block rules, and their counts are within one of the last pass's:
+fib's 27.53 (the ran rows' annotations as the sprint begins), zoom's 19.25
+and 21.85 and syscalls' 28.23 are in the last pass's exports at the same
+frames, counted now; the heap's minimap has two more.
-What this pass fixed at the block (every scene that draws the block gets
-it), an effect shown before its cause first, found on the last pass's
-exports:
+What this pass built (`graph`, design/README.md's "graph" and motion.md's
+"A block left"), and what it fixed at the block on the way:
-- A write's bytes took their new value at memory's landing, before their
- own arm (any write 6 f or more after the first of its step: sort's first
- swap, the heap's first free, its chunk's words and the tcache's header).
- Each run now keeps its old bytes, and their reading (by its old field
- where the step retypes it), through its arm until its own strike lands
- (`hexdump.keepOld`, `Now.shownAt`, a stack word too); a write on a frame
- the step has already left (a callee's words under rsp) takes no place
- among the strikes, so sort's two elements strike 6 f apart, not on one frame.
-- A register's chain read memory as of the stop after the step from the
- CPU's strike: rsi's `Hello, k` 2 s before `read` wrote it. It reads it as
- the panels show it; its later hops fade out from the bytes' strike and
- are followed again 4 f after the word's last byte lands (`Now.following`),
- never gone in a frame.
-- What a crossing's first part struck had its cool cut as the crossing
- began (read, write): the crossing's plan carries the cool on
- (`Plan.cooling`); an fd's `"note"` fades in with its framing.
-- The list's dive played twice: the minimap's drawings overwrote where the
- falling map's windows are, which the panels set off from (`map.minimap`
- keeps `window_px`); the thread, which came in a frame at the landing,
- fades in after it (`context.thread_shows`).
-- A stack window going far at a run faded out in a frame (the 12 f before
- memory's strike lie before a run step's start): it fades out and in round
- a turn that fits the step (syscalls 56 s).
-- Rows sliding out of the stack's window lost their roles (fib 6.50,
- `saved rbp`, `saved rbx`), and an arc was drawn from the row above the
- window that is there only for a slide: the roles of both windows are
- read; arcs join rows on show; a word becoming a saved rbp has its arc go
- with its role.
-- The system calls' log scrolled a row in a frame as a crossing began
- (sqlite 38.67): it scrolls with the row's arrival, its count crossfading.
-- sort's source jumped 3 lines at a sprint's phrase end: the mark travels
- over the sprint's step.
-- Under the camera a free word rsp reached became a slot in a frame (zoom
- 19.6 s): it crossfades as memory strikes.
-- `changedBytes` read its own result as it wrote it (a Zig result-location
- hazard): a strike could cover bytes that kept their value (a Debug build
- panicked on it).
+- The graph: its layout (`graph.place`, unit-tested on sort's, reverse's
+ and libc's `open` recordings in `test/recordings.zig`), the `blocks`
+ lens, the graph with its rows on the map and its blocks on the minimap,
+ the block rules on every scene's linear rows, `leave` and `turn`, the
+ run and the sprint over it (the gold, the trail and the counts
+ crossfading with each step's advance), and the gallery's two pages.
+- `Stack.watched` read each stop's registers from the trace file, ten
+ reads a stack drawn and seven stacks a frame with the minimap's: most of
+ a frame's time once the scene had run a while (sort's draft 57 ms a frame,
+ about 20 after). It reads the recording's index.
+- The disassembly's rows were clipped at the panel's rule, not above its
+ padding: a row scrolling out showed in the padding (a block's rule there
+ came and went in a frame). They are clipped above it.
+- `surface` did nothing in a scene that had not opened on the map: the
+ map's object was never shown. It shows it.
Right in one frame, and exempted by name: the encoder's keyframes and the
gallery's cuts (above).
@@ -273,25 +258,60 @@ What fires still, by kind (located on this pass's exports):
- The stack: a few pixels in the arcs' lanes and the pins (fib 8.20,
19.50, 24.90, 39.13; syscalls 29.13, 57.72, 70.17; sqlite 20.43, 31.75).
- Registers: a held corner, a few pixels at a value's top left (fib 21.10,
- sort 14.88, syscalls 44.32, sqlite 38.67).
+ syscalls 44.32, sqlite 38.67).
- The source and the disassembly: syscalls 17.92 and 55.62, the line
going `waiting` and back with the descent's rows; zoom 18.10 and 19.60,
an annotation taking its value in place under the camera.
- The view: syscalls 64.77, the read's first byte landing over the
buffer's dots; the gallery's view re-windowing at a state's first frame.
-- The drawing: fib 33.63 the calls tree's badge corner; sort's index pins
- sliding where a sprint's phrase ends; the heap's runs (its 16 CROWDs
- too: the calls and returns it does not narrate).
+- The drawing: fib 33.63 the calls tree's badge corner; the heap's runs
+ (its 16 CROWDs too: the calls and returns it does not narrate); the
+ gallery's 22.38, a struck count's digits going to their ordinary face at
+ the middle of its cool (the catalogue's `cool`).
+- The map filling the frame: sort 32.25, at the sprint on it, a few pixels
+ of the map's panels changing in place at the sprint's pace.
- AIR: sort's and zoom's last picture (the last caption read, then held);
sqlite 32.95, after the continue to `unixWrite`, and 46.02, where the
chart misses the read frame on one byte (`0d`) at 47.5 s: the picture is
still 1.5 s and then 2 s; the gallery's atoms page, 3 s by design.
- CROWD: the advance (thread, plaques and rip as one mark, the source and
the disassembly scrolling with it), the veil over three panels at a
- run's first step.
+ run's first step; sort's `surface` and `dive`, every panel at once by
+ design.
## What is not done
+- The graph (`graph`): the map draws the disassembly as its graph only
+ where the scene asks (`Context.Options.graph`: sort); the catalogue has
+ it whenever the recording has the function's blocks. Not built for the
+ rest: the descent on the map (graphs stacked, the caller folded to its
+ call), a list scene that opens on the map and dives out of a graph, a
+ 2 510-block `sqlite3VdbeExec`. `spread` and `gather` are not built: in a
+ `surface` and a `dive` the working rows and the lens fade on their way
+ (`context.Zoom.shows`) instead of gliding into their blocks. A finished
+ loop is not folded; a fold is not named (`multi-threaded: a cancellable
+ call`) since no scene names one; an indirect jump's taken targets do not
+ become edges. The pan is by whole layers to the largest loop the pc's
+ block is in that fits: sort's ten layers do not fit the lens (its outer
+ loop is 284 of 270), so the lens pans three times a trip round the outer
+ loop (24 f, `glide`). A function change (the pc going into another
+ function with blocks) is not crossfaded: the graph changes in a frame,
+ and sort ends at its `ret` for that. A count struck as the mark lands
+ cools to nothing by the step's end (no held corner: the steps after do
+ not keep it); at a walked step across several blocks the counts, the
+ gold and the trail crossfade with the advance, nothing struck. On the
+ map a `leave`'s plaque fades out of its row (6 f) as the mark sets off
+ from the block's port, and in at the landing; it does not slide along
+ the rows to the port. The pc's block widens no further than the right
+ channel: a block in the last column has no annotation room. A
+ `syscall`'s or call's arguments hang only as far as the block's last row.
+- Sort's recording is the instrumented build: eleven blocks (`setg`, a
+ `test`/`jne`, two blocks of a lone `jmp`), not mock 22's eight; its
+ prologue (25 rows, the instrumentation's calls) folds on the map to four,
+ `more`, four, and the map pans to the outer loop. Mock 22's exit sits in
+ the second column; here it is under its fall-through's block, as the
+ catalogue's rule has it, and its edge runs in the right channel.
+
- The descent: the kernel's three rows are kept at the floor from the
`syscall` to the stop after it, so the rows under the call move up again
a step later. The fold past three windows (`2 calls: fib fib`), a lazy
diff --git a/anim/build.zig b/anim/build.zig
index 54ff589..4a2ec76 100644
--- a/anim/build.zig
+++ b/anim/build.zig
@@ -227,6 +227,11 @@ pub fn build(b: *std.Build) void {
// pictures change when the drawing does and not when a new recording does.
_ = embedded.addCopyFile(b.path("test/gallery/list.ztrace"), "gallery.ztrace");
table.print(b.allocator, "pub const gallery: Program = .{{ .trace = @embedFile(\"gallery.ztrace\"), .file = \"list.c\", .source = @embedFile(\"list.c\") }};\n", .{}) catch @panic("OOM");
+ // And for its graph pages, sort's and hello's, kept as they were recorded with blocks.
+ for ([_][2][]const u8{ .{ "sort", "sort.c" }, .{ "hello", "hello.c" } }) |kept| {
+ _ = embedded.addCopyFile(b.path(b.fmt("test/gallery/{s}.ztrace", .{kept[0]})), b.fmt("gallery-{s}.ztrace", .{kept[0]}));
+ table.print(b.allocator, "pub const gallery_{s}: Program = .{{ .trace = @embedFile(\"gallery-{s}.ztrace\"), .file = \"{s}\", .source = @embedFile(\"{s}\") }};\n", .{ kept[0], kept[0], kept[1], kept[1] }) catch @panic("OOM");
+ }
const recordings = b.createModule(.{ .root_source_file = embedded.add("recordings.zig", b.fmt(
\\//! Generated by canim's build.zig: the recordings and the sources they ran.
\\pub const Program = struct {{ trace: []const u8, file: []const u8, source: []const u8 }};
diff --git a/anim/scenes/gallery.zig b/anim/scenes/gallery.zig
index 693553e..48fba99 100644
--- a/anim/scenes/gallery.zig
+++ b/anim/scenes/gallery.zig
@@ -10,6 +10,11 @@
//! 3.6 s a step, mid-way: struck, read, projection, echo (mock 10)
//! 6 s `subject` (mock 11); 8 s `none`, with the backtrace
//! 10.8 s two loupes in depth, from rax and its node's `next` (mock 21)
+//! 21 s the `blocks` lens: at rest (blocks reached and not yet, the trail,
+//! an edge never taken), `leave` mid-way, `turn` landing, folds
+//! 24 s the graph with its rows, as the map draws it (here at half size):
+//! sort's (panned to its loop, the pc's block annotated) and libc's
+//! `open` at its syscall (the blocks never reached folded)
const std = @import("std");
const sk = @import("sketch");
const anim = @import("anim");
@@ -306,6 +311,101 @@ fn rowsPage(u: ui.Ui, p: *const Pages) void {
_ = canim.hexdump.reading(panel, 3 * row, 0, m, &values, &known, &.{ .{ .at = 0, .len = 1, .type = char }, .{ .at = 1, .len = 1, .type = boolean }, .{ .at = 4, .len = 4, .type = int }, .{ .at = 8, .len = 8, .reads = .pointer } });
}
+/// The graph pages' names, over their drawings.
+const GraphPages = struct {
+ box: canim.Rect = .{ .x = 0, .y = 0, .w = 1920, .h = 1080 },
+
+ pub fn draw(_: *const GraphPages, f: *sk.Frame, at: anim.Placed) void {
+ const u: ui.Ui = .{ .f = f, .at = at };
+ if (at.t >= 21 and at.t < 24) {
+ title(u, 64, 40, "graph: the blocks lens");
+ for ([_][2]f32{ .{ 64, 72 }, .{ 976, 72 }, .{ 64, 488 }, .{ 976, 488 } }, [_][]const u8{ "at rest: reached, not yet, the trail, never taken", "leave: the edge drawn gold, the mark on it", "turn: landed, the lane's count and the header's struck", "folds: libc's open, the blocks never reached" }) |p, words| note(u, p[0], p[1], words);
+ } else if (at.t >= 24 and at.t < 27) {
+ title(u, 64, 40, "graph: with its rows, as on the map (half size)");
+ }
+ }
+};
+
+/// A component drawn at half its size (the map's rows as the map shows them filling a 1080p frame).
+fn Half(comptime T: type) type {
+ return struct {
+ inner: T,
+ box: canim.Rect = .{ .x = 0, .y = 0, .w = 1920, .h = 1080 },
+
+ pub fn draw(h: *const @This(), f: *sk.Frame, at: anim.Placed) void {
+ var p = at;
+ p.xf = p.xf.mul(.scale(0.5, 0.5));
+ p.scale *= 0.5;
+ h.inner.draw(f, p);
+ }
+ };
+}
+
+/// The graph pages: sort's blocks in the lens, at rest and in `leave` and
+/// `turn`, and libc's `open` folded; then the map's form of both.
+fn graphPages(s: *anim.Frame, at_time: f32) [3]anim.Obj {
+ const graph = canim.graph;
+ const sort = canim.open(recordings.gallery_sort);
+ const hello = canim.open(recordings.gallery_hello);
+ const sort0 = sort.register(sort.call("sort", 0).enter, "rip");
+ // Where the opening of the sort scene is: the inner loop's test, about to compare 31 with 12.
+ var start = sort.call("sort", 0).enter;
+ while (sort.next(.over, sort.next(.over, start)) < sort.call("swap", 0).enter) start = sort.next(.over, start);
+ // The first `jne` to the swap, and the first jump back to the test.
+ var jne = start;
+ while (!(sort.insn_branches[jne] != null and sort.insn_branches[jne].?.taken and sort.insn_branches[jne].?.to == sort0 + 0xc5)) jne += 1;
+ var back = jne;
+ while (!(sort.insn_branches[back] != null and sort.insn_branches[back].?.to == sort0 + 0x8a)) back += 1;
+ var syscall_at: u64 = 0;
+ var buf: [256]u8 = undefined;
+ for (hello.registers, 0..) |r, k| if (hello.strings.get(r.rip)) |name| if (std.mem.startsWith(u8, name, "libc.so.6!open+") and std.mem.eql(u8, (hello.instruction(k, r.rip, &buf) orelse continue).text, "syscall")) {
+ syscall_at = k;
+ break;
+ };
+ const place = struct {
+ fn stage(s_: *anim.Frame, band: canim.Rect, disasm: canim.Rect) *canim.Stage {
+ var split = canim.layout.split.blocks;
+ split.band = band;
+ split.disasm = disasm;
+ return canim.Stage.init(s_, split);
+ }
+ }.stage;
+ const nowhere: canim.Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 };
+ const rest = canim.cursor(s, sort, start);
+ const leaving = canim.cursor(s, sort, jne);
+ const turning = canim.cursor(s, sort, back);
+ const open_at = canim.cursor(s, hello, syscall_at);
+ const lenses = .{
+ canim.add(s, graph.Blocks{ .cursor = rest, .stage = place(s, .{ .x = 64, .y = 84, .w = 880, .h = 296 }, nowhere) }),
+ canim.add(s, graph.Blocks{ .cursor = leaving, .stage = place(s, .{ .x = 976, .y = 84, .w = 880, .h = 296 }, nowhere) }),
+ canim.add(s, graph.Blocks{ .cursor = turning, .stage = place(s, .{ .x = 64, .y = 500, .w = 880, .h = 296 }, nowhere) }),
+ canim.add(s, graph.Blocks{ .cursor = open_at, .stage = place(s, .{ .x = 976, .y = 500, .w = 880, .h = 296 }, nowhere) }),
+ };
+ // Each step played so that the page shows it at `at_time`: the leave with its mark mid-way, the turn just landed.
+ const l = canim.theme.time.leave;
+ s.cursor = at_time - l.sets_off - l.mark / 2.0 - 0.05;
+ graph.leave(leaving, s);
+ s.cursor = at_time - l.sets_off - 0.30 - l.count;
+ graph.leave(turning, s);
+ // The map's form, at half size: the map's units, two columns wide each.
+ const maps = .{
+ canim.add(s, Half(graph.Map){ .inner = .{ .cursor = canim.cursor(s, sort, jne), .stage = place(s, nowhere, .{ .x = 128, .y = 176, .w = 1792, .h = 1688 }) } }),
+ canim.add(s, Half(graph.Map){ .inner = .{ .cursor = canim.cursor(s, hello, syscall_at), .stage = place(s, nowhere, .{ .x = 1952, .y = 176, .w = 1792, .h = 1688 }) } }),
+ };
+ const pages = canim.add(s, GraphPages{});
+ var all: [lenses.len]anim.Animation = undefined;
+ inline for (lenses, 0..) |o, i| all[i] = o.fadeIn();
+ s.cursor = 21;
+ s.play(.{pages.fadeIn()}, .{ .seconds = 0 });
+ s.play(all, .{ .seconds = 0 });
+ s.cursor = 24;
+ inline for (lenses, 0..) |o, i| all[i] = o.fadeOut();
+ s.play(all, .{ .seconds = 0 });
+ s.play(.{ maps[0].fadeIn(), maps[1].fadeIn() }, .{ .seconds = 0 });
+ s.cursor = 27;
+ return .{ pages, maps[0], maps[1] };
+}
+
fn frame(s: *anim.Frame) void {
const rec = canim.open(recordings.gallery);
const reverse = rec.call("reverse", 0);
@@ -341,5 +441,12 @@ fn frame(s: *anim.Frame) void {
near.open(s);
s.wait(18.8 - s.cursor);
deeper.open(s);
+ s.wait(20.6 - s.cursor);
+ deeper.close(s);
+ near.close(s);
s.wait(21 - s.cursor);
+ // The graph's pages: the context stands aside.
+ inline for (panels, 0..) |o, i| all[i] = o.fadeOut();
+ s.play(all, .{ .seconds = 0 });
+ _ = graphPages(s, 21.8);
}
diff --git a/anim/scenes/sort.zig b/anim/scenes/sort.zig
index 82a99e1..81d55dd 100644
--- a/anim/scenes/sort.zig
+++ b/anim/scenes/sort.zig
@@ -1,11 +1,17 @@
-//! Insertion sort: neighbours compared, and swapped while the left is larger.
+//! Insertion sort as its blocks: the graph of `sort` in the band (the
+//! `blocks` lens), the disassembly its view over the state column, the
+//! source alone in the program column; on the map the same graph with its
+//! rows.
//!
-//! How it moves (design/motion.md): it opens where `i` has reached 12, the
-//! first value out of order; the compare is walked (its two cells framed:
-//! the swap's anticipation), the caption about the swap stands two
-//! seconds before it, and the swap is two flights on arcs, the second a
-//! little behind the first, each cell struck as its value lands. The next
-//! trips are a run, the long middle a sprint, and the last swap is walked.
+//! How it moves (design/motion.md, "A block left"): it opens where `i` has
+//! reached 12, the first value out of order. One trip round the inner
+//! loop is walked a block at a time: each block's instructions a cascade
+//! onto its last row, then `leave` along the edge that row takes (the
+//! compare's chips framed, the edge drawn gold, the mark along it, the
+//! target's count struck), and `turn` up the inner loop's lane. The next
+//! trip is a run; the long middle is a sprint on the map (`surface`, the
+//! gold going block to block, `dive`); the last value's trip is walked
+//! again, and the way out run.
const anim = @import("anim");
const canim = @import("canim");
const recordings = @import("recordings");
@@ -15,52 +21,56 @@ pub const scene = anim.Live(frame, struct {});
fn frame(s: *anim.Frame) void {
const rec = canim.open(recordings.sort);
+ const graph = canim.graph;
const t = canim.theme.time;
- const sort = rec.call("sort", 0);
// The `while`, about to compare 31 with 12: the stop before the one that calls swap.
- var start = sort.enter;
+ var start = rec.call("sort", 0).enter;
while (rec.next(.over, rec.next(.over, start)) < rec.call("swap", 0).enter) start = rec.next(.over, start);
+ // The inner loop's test (`j > 0`): where each of its trips begins.
+ const test_pc = rec.register(start, "rip");
var at = canim.cursor(s, rec, start);
- // The array is the idea; `i` and `j` are memory, and strike a beat before their tags slide.
- at.levels = .{ .idea = .data, .follow = t.quick, .source_rows = 10 };
- // Its last compare: nothing larger is left of 20.
+ at.levels = .{ .fire = .disasm, .source_rows = 20 };
const last = rec.call("swap", 9);
- const end = rec.next(.over, rec.next(.over, last.exit.?));
- at.journey(.over, end);
- // The context is the instrumented program's: its registers are unmarked.
- const split = canim.layout.split.subject;
- const ctx = canim.context(s, at, .{ .split = split });
- // The lens: the array as cells, and the two indices that walk it.
- const cells = canim.array(s, .{ .cursor = at, .stage = ctx.stage, .variable = "a", .indices = &.{ "i", "j" }, .focus = true });
- ctx.map.add(s, canim.Array{ .cursor = at, .stage = ctx.map.stage, .variable = "a", .indices = &.{ "i", "j" } });
+ // The last value's trip: from its test, before the compare that leads to the last swap.
+ var last_trip = last.enter;
+ while (rec.register(last_trip, "rip") != test_pc) last_trip -= 1;
+ at.journey(.over, rec.call("sort", 0).exit.? - 1);
+ const ctx = canim.context(s, at, .{ .split = canim.layout.split.blocks, .graph = true });
+ const blocks = canim.add(s, graph.Blocks{ .cursor = at, .stage = ctx.stage, .focus = true });
var says: canim.Narrator = .{};
- // A swap, start to finish, as one step of sort(): over the call.
- const swap = struct {
- fn step(c: canim.Cursor, frame_: *anim.Frame, r: *canim.Recording) void {
- c.step(frame_, r.next(.over, c.moment(frame_)), canim.Array.swap, canim.Array.swap.total);
+ const joins = ctx.ties(s);
+ // A block at a time: its rows a cascade onto its last, then the edge it takes.
+ const block = struct {
+ fn walk(c: canim.Cursor, f: *anim.Frame) void {
+ graph.toEnd(c, f);
+ graph.leave(c, f);
}
- }.step;
+ }.walk;
- const joins = ctx.ties(s);
- // The cells come in beside their bytes, one by one.
- ctx.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.source, ctx.disasm, cells, joins }, 8);
- says.line(s, "`i` walks right. Everything left of it is in order,\nand 12 is not yet in its place.");
- // The compare: both cells framed. It is the swap's anticipation.
- at.walk(s, .over, rec.next(.over, start));
- says.line(s, "While the one on the left is larger, they trade places,\nand `j` follows the smaller value down.");
- // Arcs: the two values fly past each other, one over, one under. The
- // whole context stays: the swap is bytes changing, then cards moving.
- swap(at, s, rec);
- at.walk(s, .over, rec.next(.over, at.moment(s)));
+ canim.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.source, ctx.disasm, blocks, joins });
+ says.line(s, "`sort` as its blocks: two loops. `j > 0`, so its `jle`\nis not taken, and the inner loop goes on to its compare.");
+ block(at, s);
+ says.line(s, "31 > 12: `setg` leaves 1, and `jne` takes the swap.");
+ block(at, s);
+ block(at, s);
+ says.line(s, "The swap and `j--`; then the inner loop turns,\nback up to `j > 0`.");
+ block(at, s);
+
+ // Pacing: the next trip at a run; then the long middle on the map, at a sprint.
+ says.line(s, "Once more at a run: 17 > 12, a second swap.");
+ at.hurry(s, .over, rec.lineMoment(12, rec.call("swap", 1).exit.?), .{ .settle = false });
+ says.line(s, "The rest on the map: the gold goes block to block,\nand the counts grow with every trip.");
+ ctx.map.surface(s);
+ at.hurry(s, .over, last_trip, .{ .pace = t.sprint, .from = t.run });
+ ctx.map.dive(s);
- // Pacing: the same again, so at a run; then the long way 5 has to go,
- // at a sprint; slowing for the last swap.
- says.line(s, "And so on: each value is carried left\nuntil the one before it is not larger.");
- at.hurry(s, .over, rec.call("swap", 3).enter - 1, .{ .to = t.run, .settle = false });
- at.hurry(s, .over, last.enter - 1, .{ .pace = t.sprint, .from = t.run });
- says.line(s, "The last swap: 20 goes one place left, past 26.");
- swap(at, s, rec);
- at.walk(s, .over, end);
- says.line(s, "Nothing larger is left of 20. The array is sorted.");
+ says.line(s, "The last value, 20: 26 > 20, so `jne` takes the swap.");
+ block(at, s);
+ block(at, s);
+ block(at, s);
+ says.line(s, "The swap, and the inner loop turns once more.");
+ block(at, s);
+ says.line(s, "17 > 20 is false: the inner loop is done, and with `i` at 8\nso is the outer. The array is sorted.");
+ at.hurry(s, .over, rec.call("sort", 0).exit.? - 1, .{});
says.close(s);
}
diff --git a/anim/src/context.zig b/anim/src/context.zig
index 9829fa7..55ff9d6 100644
--- a/anim/src/context.zig
+++ b/anim/src/context.zig
@@ -44,7 +44,7 @@ pub const legend = blk: {
/// While a `dive` or a `surface` plays (`map.Map`): each working panel's
/// box, and the rect it is drawn into instead, on its way between its
/// window on the map and its place; its tags, rules and lanes how much shown.
-pub const Zoom = struct { box: Rect, to: Rect, chrome: f32 };
+pub const Zoom = struct { box: Rect, to: Rect, chrome: f32, shows: f32 = 1 };
pub var zoomed: [8]?Zoom = @splat(null);
/// How much the thread shows: it fades in (12 f) once a dive has landed, never in a frame.
pub var thread_shows: f32 = 1;
@@ -58,6 +58,7 @@ pub fn at(f: *sk.Frame, placed: anim.Placed, box: Rect) ui.Ui {
const k = zz.to.w / box.w;
p.xf = p.xf.mul(.translate(zz.to.x, zz.to.y)).mul(.scale(k, k)).mul(.translate(-box.x, -box.y));
p.scale *= k;
+ p.pose.opacity *= zz.shows;
// On its way it is opaque, over the panels that set off before it; its tag and rules come in at the end.
const u = (ui.Ui{ .f = f, .at = p }).moved(box.x, box.y);
u.fill(.{ .x = 0, .y = m.tag, .w = box.w, .h = box.h - m.tag }, theme.color.page);
@@ -667,8 +668,9 @@ pub const Stack = struct {
fn watched(now: cursors.Now, slots: usize, age: usize) usize {
const rec = now.rec;
for (age..now.stops.len - 1) |a| {
- const to = (rec.moment(now.stops[a]).regs(rec.tid) catch null) orelse return a;
- const from = (rec.moment(now.stops[a + 1]).regs(rec.tid) catch null) orelse return a;
+ // The index's registers: reading them from the trace was most of a frame's time.
+ const to = rec.registers[@min(now.stops[a], rec.end())];
+ const from = rec.registers[@min(now.stops[a + 1], rec.end())];
if (far(windowAt(now, to, slots, a), windowAt(now, from, slots, a + 1), slots)) return a;
}
return now.stops.len;
diff --git a/anim/src/descent.zig b/anim/src/descent.zig
index d435b59..53d46c5 100644
--- a/anim/src/descent.zig
+++ b/anim/src/descent.zig
@@ -22,6 +22,7 @@ const ties = @import("ties.zig");
const map = @import("map.zig");
const context = @import("context.zig");
const disasm = @import("disasm.zig");
+const graph = @import("graph.zig");
const syscalls = @import("syscalls.zig");
const Stage = @import("stage.zig").Stage;
const theme = ui.theme;
@@ -255,6 +256,8 @@ pub const Descent = struct {
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| {
const v = u.faded(p.shows);
const y = p.y + p.rise;
@@ -305,7 +308,11 @@ pub const Descent = struct {
}
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 hangs = dd.insn(v, now, row, y, box.w, widen0 + wide, line, line != last_line, current_line, lead, state, base, if (p.twin) covered_before > 0 else covered > 0, .{ addrs[0], addrs[n -| 1] }, null);
+ const covers = if (p.twin) covered_before > 0 else covered > 0;
+ const hangs = dd.insn(v, now, row, y, 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, 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 4324b42..282c66e 100644
--- a/anim/src/disasm.zig
+++ b/anim/src/disasm.zig
@@ -19,6 +19,7 @@ const Recording = @import("Recording.zig");
const ties = @import("ties.zig");
const map = @import("map.zig");
const context = @import("context.zig");
+const graph = @import("graph.zig");
const Stage = @import("stage.zig").Stage;
const theme = ui.theme;
const time = theme.time;
@@ -40,6 +41,8 @@ pub const Insn = struct {
line: ?u32 = null,
/// A direct call's callee's parameter count.
params: ?u8 = null,
+ /// A graph's row: its block's head carries the address, the row its offset.
+ bare: bool = false,
/// Its mnemonic, with a prefix that is part of it (`rep stosq`).
pub fn mnemonic(i: Insn) []const u8 {
@@ -98,7 +101,8 @@ pub const Disasm = struct {
u.faded(going).tagNote(box.w, names[1]);
}
const top = body.y + m.pad;
- u.clip(.{ .x = theme.stroke.rule, .y = m.tag, .w = box.w - 2 * theme.stroke.rule, .h = box.h - m.tag - theme.stroke.rule });
+ // The rows' own room: a row going out of the window passes under the padding, its block's rule too.
+ u.clip(.{ .x = theme.stroke.rule, .y = m.tag, .w = box.w - 2 * theme.stroke.rule, .h = box.h - m.tag - m.pad });
defer u.unclip();
// The two windows as one list, if they are of one function: the
@@ -198,6 +202,8 @@ pub const Disasm = struct {
// What the window holds: a jump out of it says where it goes.
const window = [2]u64{ if (merged.len > 0) merged[0].addr else 0, if (merged.len > 0) merged[merged.len - 1].addr else 0 };
const echo = jumpEcho(now, base);
+ // The blocks (`graph`): a rule over each one's first row, its visits where the row has room.
+ const rules = graph.Rules.init(u.f.scratch, rec, now.marks, pc, going);
for (merged, 0..) |row, j| {
const y = top + (@as(f32, @floatFromInt(j)) - scroll) * m.row;
const line = if (row.outside) null else row.line;
@@ -215,7 +221,9 @@ pub const Disasm = struct {
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 hangs = d.insn(lit, now, row, y, box.w, widen, line, line != last_line, current_line, lead, if (is_pc) .current else if (was) |r| .{ .ran = r } else .ahead, base, covered > 0, window, echo);
+ const state: State = if (is_pc) .current else if (was) |r| .{ .ran = r } else .ahead;
+ const hangs = d.insn(lit, now, row, y, box.w, widen, line, line != last_line, current_line, lead, state, base, covered > 0, window, echo);
+ rules.draw(lit, row.addr, y, box.w, covered == 0 and !row.outside and !d.noted(lit, now, row, y, state, base, 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;
@@ -268,7 +276,7 @@ pub const Disasm = struct {
u.text(.mono, size, x, base_y, name, t.faint);
return 0;
}
- _ = at.address(c.address, base_y, row.addr, .{ .ink = ui.rgb(code_ink), .lead = lead });
+ if (!row.bare) _ = at.address(c.address, base_y, row.addr, .{ .ink = ui.rgb(code_ink), .lead = lead });
if (first_of_line) if (line) |l| {
const num = u.print("{d}", .{l});
const now_line = l == current_line;
@@ -290,10 +298,15 @@ pub const Disasm = struct {
return if (need.drawn and !covered) d.annotation(u, now, row, y, x, room, state, base, window, echo).hangs else 0;
}
+ /// Whether `row` has an annotation: where it has none, its block's visits may be ranged right.
+ pub fn noted(d: *const Disasm, u: ui.Ui, now: cursors.Now, row: Insn, y: f32, state: State, base: usize, window: [2]u64, echo: ?Echo) bool {
+ return d.annotation(u.faded(0), now, row, y, c.annotation, std.math.inf(f32), state, base, window, echo).drawn;
+ }
+
const Note = struct { drawn: bool = false, end: f32 = 0, hangs: usize = 0 };
/// The row that set the flags a conditional jump reads, while the jump
/// runs: its chips are framed as read with it (an `echo`).
- const Echo = struct { addr: u64, shows: f32 };
+ pub const Echo = struct { addr: u64, shows: f32 };
/// The annotation: what it does, now, from `x`, cut where `room` ends.
/// What it left behind is shown only where the step ran this
@@ -781,7 +794,7 @@ fn flags(u: ui.Ui, x: f32, y: f32, eflags: u64, mask: u16, read: f32) f32 {
/// While a conditional jump runs (the mark on it, its step under way), the
/// row that set the flags it reads: the instruction just before it ran.
-fn jumpEcho(now: cursors.Now, base: usize) ?Disasm.Echo {
+pub fn jumpEcho(now: cursors.Now, base: usize) ?Disasm.Echo {
if (base != 1 or now.resting()) return null;
const flags_ = readsFlags(now.rec, now.stops[1]);
if (flags_ == 0 or now.stops[1] == 0) return null;
@@ -876,13 +889,17 @@ pub fn target(row: Insn) ?u64 {
/// (faint for padding) and its operands as canim draws them, at `y` in `u`
/// (the panel's origin). For the gallery's page of rows.
pub fn plainRow(u: ui.Ui, row: Insn, y: f32) void {
- const base_y = rows.baseline(y);
- _ = u.address(c.address, base_y, row.addr, .{ .ink = ui.rgb(theme.color.region.code_) });
+ _ = u.address(c.address, rows.baseline(y), row.addr, .{ .ink = ui.rgb(theme.color.region.code_) });
// A library's twelve digits move what is right of them, 12 a digit past seven.
- const v = u.moved(@as(f32, @floatFromInt(@max(ui.digits(row.addr), 7) - 7)) * adv, 0);
+ bareRow(u.moved(@as(f32, @floatFromInt(@max(ui.digits(row.addr), 7) - 7)) * adv, 0), row, y);
+}
+
+/// Its mnemonic and operands alone, at their columns (a graph's row, whose head carries the address).
+pub fn bareRow(u: ui.Ui, row: Insn, y: f32) void {
+ const base_y = rows.baseline(y);
const pad = row.padding();
- v.text(.mono_bold, size, c.mnemonic, base_y, row.mnemonic(), if (pad) t.faint else theme.color.code.keyword);
- operands(v, row, shorn(v, row), "", base_y, c.annotation - gap - c.operands, if (pad) t.faint else theme.color.code.plain);
+ u.text(.mono_bold, size, c.mnemonic, base_y, row.mnemonic(), if (pad) t.faint else theme.color.code.keyword);
+ operands(u, row, shorn(u, row), "", base_y, c.annotation - gap - c.operands, if (pad) t.faint else theme.color.code.plain);
}
/// Operands as canim draws them (design/README.md, `insn` and `width`):
diff --git a/anim/src/graph.zig b/anim/src/graph.zig
new file mode 100644
index 0000000..f4e61ea
--- /dev/null
+++ b/anim/src/graph.zig
@@ -0,0 +1,1436 @@
+//! `graph` (design/README.md, "graph"): the disassembly's other form, a
+//! function as its basic blocks with what the run did on them: how often
+//! each was entered, how often each edge was taken, the edges never taken,
+//! the last three taken (the trail), the block the pc is in. Execution
+//! flows down; the one thing that climbs is a loop's back edge.
+//!
+//! `read` takes a function's blocks from the recording (`zt.telescope.blocks`)
+//! and counts the run over them; `place` lays them out, a pure function of
+//! the blocks, their sizes and the counts of the whole recording (so the
+//! picture does not move as the counts grow): back edges by a walk from the
+//! entry in address order, layers by the longest forward path, in a layer
+//! each block under the predecessor that reached it most often (the hot
+//! path straight down), then under the one it falls from, a taken target
+//! to the right; back edges in their loops' lanes on the left, the
+//! outermost farthest out; an edge that would cross a block in the right
+//! channel. `layout` does both for a form (the map's rows, the lens's
+//! shapes), folding what is too big for its place in the catalogue's
+//! order: the runs of blocks the whole recording never reached, then long
+//! blocks. `Blocks` draws it as the `blocks` lens, `Map` with its rows on
+//! the map, `Rules` its blocks on the linear rows; `leave` (and `turn`) is
+//! the step across one edge.
+const std = @import("std");
+const sk = @import("sketch");
+const anim = @import("anim");
+const zt = @import("ztrace");
+const Recording = @import("Recording.zig");
+const ui = @import("ui.zig");
+const rows = @import("rows.zig");
+const cursors = @import("cursor.zig");
+const context = @import("context.zig");
+const disasm = @import("disasm.zig");
+const Stage = @import("stage.zig").Stage;
+const theme = ui.theme;
+const time = theme.time;
+const t = theme.color.text;
+
+pub const End = zt.Debugger.End;
+
+/// A block, or a fold of blocks never reached, and where it is drawn.
+pub const Node = struct {
+ start: u64,
+ len: u64,
+ end: End,
+ /// Where a tail call or a call that does not return goes (null: none, or not known).
+ out: ?u64 = null,
+ /// Its instructions (a fold's: of all its blocks).
+ rows: u32 = 0,
+ /// The rows drawn: a long block folded to its first four, `more` and its last four.
+ shown: u32 = 0,
+ /// How many blocks the whole recording never reached are folded into it (0: a block of its own).
+ folded: u32 = 0,
+ /// Times the run entered it, by the moment the graph is of; and in the whole recording.
+ visits: u32 = 0,
+ reached: bool = false,
+ layer: u32 = 0,
+ col: u32 = 0,
+ x: f32 = 0,
+ y: f32 = 0,
+ w: f32 = 0,
+ h: f32 = 0,
+
+ pub fn contains(n: Node, addr: u64) bool {
+ return addr -% n.start < n.len;
+ }
+};
+
+pub const Kind = enum { fall, taken, jump };
+
+pub const Edge = struct {
+ from: u32,
+ to: u32,
+ kind: Kind,
+ /// Times taken by the moment the graph is of; in the whole recording (what places the blocks).
+ count: u32 = 0,
+ total: u32 = 0,
+ /// To a block at its own layer or above: a loop's turn, in lane `lane` (0 the outermost).
+ back: bool = false,
+ lane: u32 = 0,
+ /// Its way, out of its block's bottom and into its target.
+ points: [6][2]f32 = undefined,
+ n: u8 = 0,
+
+ pub fn path(e: *const Edge) []const [2]f32 {
+ return e.points[0..e.n];
+ }
+
+ /// Its length along its way.
+ pub fn length(e: *const Edge) f32 {
+ var total: f32 = 0;
+ for (e.path()[0 .. e.n -| 1], e.path()[@min(1, e.n)..]) |a, b| total += @abs(b[0] - a[0]) + @abs(b[1] - a[1]);
+ return total;
+ }
+
+ /// The point `d` units along its way, and which way it goes there.
+ pub fn at(e: *const Edge, d: f32) struct { [2]f32, [2]f32 } {
+ var left = @max(d, 0);
+ const pts = e.path();
+ for (pts[0 .. pts.len - 1], pts[1..]) |a, b| {
+ const l = @abs(b[0] - a[0]) + @abs(b[1] - a[1]);
+ const dir = [2]f32{ std.math.sign(b[0] - a[0]), std.math.sign(b[1] - a[1]) };
+ if (left <= l or b[0] == pts[pts.len - 1][0] and b[1] == pts[pts.len - 1][1]) {
+ const f = if (l > 0) @min(left / l, 1) else 1;
+ return .{ .{ a[0] + (b[0] - a[0]) * f, a[1] + (b[1] - a[1]) * f }, dir };
+ }
+ left -= l;
+ }
+ return .{ pts[pts.len - 1], .{ 0, 1 } };
+ }
+};
+
+/// A function's graph at a moment.
+pub const Graph = struct {
+ start: u64,
+ len: u64,
+ nodes: []Node,
+ edges: []Edge,
+ /// The node the pc is in.
+ here: ?u32,
+ /// The last three edges taken, newest first.
+ trail: [3]?u32 = @splat(null),
+ /// Every instruction's text is recorded (a function of up to 2 048).
+ text: bool,
+ /// The recording stops at every instruction: the edges' counts and the trail are known.
+ exact: bool,
+ /// Its size, from its top left (the lanes' side).
+ w: f32 = 0,
+ h: f32 = 0,
+ /// The right channel's x, and how many lanes the back edges take.
+ channel: f32 = 0,
+ lanes: u32 = 0,
+ /// The blocks the function has, before any fold.
+ blocks: usize = 0,
+ /// How far apart the columns are.
+ pitch: f32 = 0,
+
+ pub fn nodeOf(g: Graph, addr: u64) ?u32 {
+ for (g.nodes, 0..) |n, i| if (n.contains(addr)) return @intCast(i);
+ return null;
+ }
+
+ pub fn edgeOf(g: Graph, from: u32, to: u32) ?u32 {
+ for (g.edges, 0..) |e, i| if (e.from == from and e.to == to) return @intCast(i);
+ return null;
+ }
+
+ /// How many blocks the folds hold.
+ pub fn foldedBlocks(g: Graph) usize {
+ var n: usize = 0;
+ for (g.nodes) |node| n += node.folded;
+ return n;
+ }
+};
+
+/// The sizes a form lays its graph out by (design/README.md, `metric.graph`).
+pub const Metric = struct {
+ layer_gap: f32,
+ block_gap: f32,
+ /// Columns are at least this far apart.
+ pitch: f32 = 0,
+ /// Lanes: the first this far in, then this far apart; the blocks this far right of the last.
+ lane_in: f32 = 24,
+ lane: f32,
+ blocks_in: f32,
+ /// The first layer, under the graph's top.
+ top: f32,
+ /// A port this far in from its block's side; arrivals into one top this far apart.
+ port: f32,
+ arrivals: f32,
+ /// The right channel, right of the widest layer.
+ channel: f32,
+ /// A back edge goes down this far before it turns to its lane, and enters its header this far down.
+ back_down: f32,
+ head_in: f32 = 12,
+ /// Edges sharing a gap run this far apart.
+ track: f32,
+};
+
+/// The disassembly's rows on the map, and the `blocks` lens's shapes.
+pub const Form = enum { map, lens };
+
+pub fn metricOf(form: Form) Metric {
+ const g = theme.metric.graph;
+ return switch (form) {
+ .map => .{ .layer_gap = g.layer_gap, .block_gap = g.block_gap, .pitch = theme.metric.insn.annotation - g.shift + g.note + g.block_gap, .lane = g.lane, .blocks_in = g.lane_in, .top = g.top, .port = g.port, .arrivals = g.arrivals, .channel = g.channel, .back_down = g.back_down, .track = 6 },
+ .lens => .{ .layer_gap = g.lens.layer_gap, .block_gap = g.block_gap, .pitch = g.lens.pitch, .lane = g.lens.lane, .blocks_in = g.lens.blocks_in, .top = g.lens.top, .port = g.lens.port, .arrivals = g.lens.port, .channel = g.lens.channel, .back_down = g.lens.back_down, .track = 4 },
+ };
+}
+
+// ---------------------------------------------------------------- reading
+
+/// The graph of the function that holds `pc`, at moment `i`: its blocks
+/// (padding no edge reaches left out), their edges by how each block ends,
+/// and what the run did on them by then (visits, edge counts, the trail)
+/// and in the whole recording (what it reached; the counts the layout is
+/// by). Unplaced. Null where the recording has no blocks for it.
+pub fn read(arena: std.mem.Allocator, rec: *const Recording, i: u64, pc: u64) ?Graph {
+ return readBlocks(arena, rec, i, pc) catch null;
+}
+
+fn readBlocks(arena: std.mem.Allocator, rec: *const Recording, i: u64, pc: u64) !?Graph {
+ const m = rec.moment(i);
+ const probe = zt.telescope.blocks(m, pc, &.{}) orelse return null;
+ const blocks = try arena.alloc(zt.telescope.Block, probe.count);
+ const found = zt.telescope.blocks(m, pc, blocks) orelse return null;
+ var nodes = try arena.alloc(Node, blocks.len);
+ for (blocks, nodes) |b, *n| n.* = .{ .start = b.start, .len = b.len, .end = b.end, .out = if (b.end == .tail or b.end == .noreturn) b.to else null };
+ // Every instruction's row, where the text is recorded; a block of
+ // padding alone (`nop`s between paths) that nothing goes to is none.
+ var reached_by = try arena.alloc(bool, blocks.len);
+ @memset(reached_by, false);
+ for (blocks) |b| for ([2]?u64{ b.to, b.falls }) |to| if (to) |at| if (blockAt(nodes, at)) |k| {
+ reached_by[k] = true;
+ };
+ var keep: usize = 0;
+ var buf: [256]u8 = undefined;
+ for (nodes, 0..) |*n, k| {
+ var at = n.start;
+ var padding = found.text;
+ while (found.text and at < n.start + n.len) : (n.rows += 1) {
+ const one = rec.instruction(i, at, &buf) orelse break;
+ padding = padding and std.mem.startsWith(u8, one.text, "nop");
+ at += one.len;
+ }
+ if (!found.text or n.rows == 0) n.rows = @intCast(@max(n.len / 4, 1));
+ n.shown = n.rows;
+ if (padding and !reached_by[k] and k > 0) continue;
+ nodes[keep] = n.*;
+ keep += 1;
+ }
+ nodes = nodes[0..keep];
+ var g: Graph = .{ .start = found.start, .len = found.len, .nodes = nodes, .edges = &.{}, .here = blockAt(nodes, pc), .text = found.text, .exact = false, .blocks = keep };
+ var edges: std.ArrayList(Edge) = .empty;
+ for (blocks) |b| {
+ const from = blockAt(nodes, b.start) orelse continue;
+ if (nodes[from].start != b.start) continue;
+ if (b.to) |to| if (b.end == .jump or b.end == .branch) if (blockAt(nodes, to)) |at| try edges.append(arena, .{ .from = from, .to = at, .kind = if (b.end == .jump) .jump else .taken });
+ if (b.falls) |to| if (blockAt(nodes, to)) |at| try edges.append(arena, .{ .from = from, .to = at, .kind = .fall });
+ }
+ g.edges = edges.items;
+ g.exact = stepsEach(rec, g);
+ count(rec, &g, i);
+ return g;
+}
+
+/// Whether the recording stopped at every instruction of the function:
+/// wherever one stop in it follows another in another block, the first was
+/// on the jump that went there, or on the last instruction of a block that
+/// falls into it. (By line a stop is where a line begins.)
+fn stepsEach(rec: *const Recording, g: Graph) bool {
+ var pairs: usize = 0;
+ var k: u64 = 0;
+ while (k + 1 < rec.len()) : (k += 1) {
+ const a = rec.registers[k].rip;
+ const b = rec.registers[k + 1].rip;
+ if (a == b) continue;
+ const from = blockAt(g.nodes, a) orelse continue;
+ const to = blockAt(g.nodes, b) orelse continue;
+ if (from == to and b != g.nodes[to].start) continue;
+ pairs += 1;
+ // A jump, a call of itself, a return into its caller.
+ if (rec.insn_branches[k]) |br| if (br.to == b or br.kind == .jump and !br.taken) continue;
+ const f = g.nodes[from];
+ if (f.end == .fall and b == f.start + f.len) continue;
+ return false;
+ }
+ return pairs > 0;
+}
+
+/// The function's blocks at moment `i` and how often the run had entered
+/// each by then: no rows, no edges. What the linear rows' block rules need.
+pub fn light(arena: std.mem.Allocator, rec: *const Recording, i: u64, pc: u64) ?Graph {
+ const m = rec.moment(i);
+ const probe = zt.telescope.blocks(m, pc, &.{}) orelse return null;
+ const blocks = arena.alloc(zt.telescope.Block, probe.count) catch return null;
+ const found = zt.telescope.blocks(m, pc, blocks) orelse return null;
+ const nodes = arena.alloc(Node, blocks.len) catch return null;
+ for (blocks, nodes) |b, *n| n.* = .{ .start = b.start, .len = b.len, .end = b.end };
+ var g: Graph = .{ .start = found.start, .len = found.len, .nodes = nodes, .edges = &.{}, .here = blockAt(nodes, pc), .text = found.text, .exact = false, .blocks = nodes.len };
+ g.exact = stepsEach(rec, g);
+ count(rec, &g, i);
+ return g;
+}
+
+/// The run over the graph: each block's visits and each edge's count, by
+/// moment `i` and in the whole recording; the blocks it ever reached; the
+/// last three edges taken by `i`. A block is entered at its start (by line,
+/// whenever a stop is in it after one in another block); an edge is
+/// counted where one stop in the function follows another across it (by
+/// instruction only: by line the stops between are not known).
+fn count(rec: *const Recording, g: *Graph, i: u64) void {
+ countBy(rec, g, i, true);
+}
+
+/// `count`; `whole`: the whole recording's too (what it reached, the totals the layout is by).
+fn countBy(rec: *const Recording, g: *Graph, i: u64, whole: bool) void {
+ var prev: ?u32 = null;
+ var prev_pc: u64 = 0;
+ var trail: [3]?u32 = @splat(null);
+ for (rec.registers, 0..) |regs, k| {
+ if (!whole and k > i) break;
+ const pc = regs.rip;
+ if (pc -% g.start >= g.len) continue;
+ const b = blockAt(g.nodes, pc) orelse continue;
+ const n = &g.nodes[b];
+ if (whole) n.reached = true;
+ // The first stop in it (a recording that begins inside it) is an entry too.
+ const entered = prev == null or if (g.exact) pc == n.start and prev_pc != pc else prev.? != b;
+ if (entered) {
+ if (k <= i) n.visits += 1;
+ if (g.exact) if (prev) |p| if (g.edgeOf(p, b)) |e| {
+ if (whole) g.edges[e].total += 1;
+ if (k <= i) {
+ g.edges[e].count += 1;
+ trail = .{ e, trail[0], trail[1] };
+ }
+ };
+ }
+ prev = b;
+ prev_pc = pc;
+ }
+ g.trail = trail;
+}
+
+/// The node holding `addr`, in nodes in address order.
+fn blockAt(nodes: []const Node, addr: u64) ?u32 {
+ var lo: usize = 0;
+ var hi: usize = nodes.len;
+ while (lo < hi) {
+ const mid = (lo + hi) / 2;
+ if (nodes[mid].start <= addr) lo = mid + 1 else hi = mid;
+ }
+ if (lo == 0 or !nodes[lo - 1].contains(addr)) return null;
+ return @intCast(lo - 1);
+}
+
+/// The blocks the whole recording never reached, each connected run of them
+/// (neighbours in address order joined by an edge) one node (`folded` its
+/// blocks, its rows all theirs), the edges to and from them joined. A run
+/// that would make a loop the code does not have (an edge into its middle
+/// from a block its rest cannot reach again) is split there.
+pub fn fold(arena: std.mem.Allocator, g: *Graph) !void {
+ const n = g.nodes.len;
+ // Where a run begins: at every unreached block not joined to the one before it.
+ const begins = try arena.alloc(bool, n);
+ for (begins, 0..) |*b, k| b.* = g.nodes[k].reached or k == 0 or g.nodes[k - 1].reached or !joined(g.*, @intCast(k - 1), @intCast(k));
+ const into = try arena.alloc(u32, n);
+ var folded: Graph = undefined;
+ for (0..8) |_| {
+ folded = try foldRuns(arena, g.*, begins, into);
+ var buf: [1 << 14]u8 = undefined;
+ var fba = std.heap.FixedBufferAllocator.init(&buf);
+ try backEdges(&folded, fba.allocator());
+ var split = false;
+ for (g.edges) |e| {
+ const a = into[e.from];
+ const b = into[e.to];
+ if (a == b or begins[e.to] or folded.nodes[b].folded == 0) continue;
+ const f = folded.edgeOf(a, b) orelse continue;
+ if (!folded.edges[f].back or reaches(g.*, e.to, e.from)) continue;
+ begins[e.to] = true;
+ split = true;
+ }
+ if (!split) break;
+ }
+ g.* = folded;
+}
+
+/// Whether blocks `a` and `b` have an edge between them, either way.
+fn joined(g: Graph, a: u32, b: u32) bool {
+ for (g.edges) |e| if (e.from == a and e.to == b or e.from == b and e.to == a) return true;
+ return false;
+}
+
+/// Whether block `to` can be reached from block `from` along the edges.
+fn reaches(g: Graph, from: u32, to: u32) bool {
+ var buf: [1 << 14]u8 = undefined;
+ var fba = std.heap.FixedBufferAllocator.init(&buf);
+ const seen = fba.allocator().alloc(bool, g.nodes.len) catch return true;
+ @memset(seen, false);
+ var stack: std.ArrayList(u32) = .empty;
+ stack.append(fba.allocator(), from) catch return true;
+ seen[from] = true;
+ while (stack.pop()) |at| {
+ if (at == to) return true;
+ for (g.edges) |e| if (e.from == at and !seen[e.to]) {
+ seen[e.to] = true;
+ stack.append(fba.allocator(), e.to) catch return true;
+ };
+ }
+ return false;
+}
+
+/// `g` with each run of unreached blocks (from a block `begins` to the next) one node; `into` each block's node.
+fn foldRuns(arena: std.mem.Allocator, g: Graph, begins: []const bool, into: []u32) !Graph {
+ var nodes: std.ArrayList(Node) = .empty;
+ for (g.nodes, 0..) |node, k| {
+ if (begins[k]) {
+ into[k] = @intCast(nodes.items.len);
+ var kept = node;
+ if (!node.reached) kept.folded = 1;
+ try nodes.append(arena, kept);
+ continue;
+ }
+ const f = &nodes.items[nodes.items.len - 1];
+ into[k] = @intCast(nodes.items.len - 1);
+ f.len = node.start + node.len - f.start;
+ f.rows += node.rows;
+ f.folded += 1;
+ }
+ for (nodes.items) |*f| if (f.folded > 0) {
+ f.end = .fall;
+ f.out = null;
+ f.shown = 1;
+ };
+ var edges: std.ArrayList(Edge) = .empty;
+ for (g.edges) |e| {
+ const a = into[e.from];
+ const b = into[e.to];
+ if (a == b) continue;
+ const dup = for (edges.items) |*d| {
+ if (d.from == a and d.to == b) break d;
+ } else null;
+ if (dup) |d| {
+ d.count += e.count;
+ d.total += e.total;
+ continue;
+ }
+ var kept = e;
+ kept.from = a;
+ kept.to = b;
+ kept.back = false;
+ try edges.append(arena, kept);
+ }
+ var out = g;
+ out.nodes = nodes.items;
+ out.edges = edges.items;
+ // The trail and the pc's block, renumbered.
+ for (&out.trail) |*tr| if (tr.*) |e| {
+ const old = g.edges[e];
+ tr.* = out.edgeOf(into[old.from], into[old.to]);
+ };
+ if (g.here) |h| out.here = into[h];
+ return out;
+}
+
+/// How far down a graph `room` high is scrolled with the pc in node
+/// `here`: not at all where it fits; else the largest loop the pc's block
+/// is in that fits, in the middle; else the pc's block in the middle.
+pub fn pan(g: Graph, room: f32, m: Metric) f32 {
+ if (g.h <= room) return 0;
+ const h = g.here orelse return 0;
+ const pc = g.nodes[h];
+ var top = pc.y - m.layer_gap / 2;
+ var bottom = pc.y + pc.h + m.layer_gap / 2;
+ var widest: f32 = 0;
+ for (g.edges) |e| if (e.back) {
+ const head = g.nodes[e.to];
+ const tail = g.nodes[e.from];
+ if (pc.layer < head.layer or pc.layer > tail.layer) continue;
+ var lb: f32 = 0;
+ for (g.nodes) |o| if (o.layer >= head.layer and o.layer <= tail.layer) {
+ lb = @max(lb, o.y + o.h);
+ };
+ const lt = head.y - m.layer_gap / 2;
+ lb += m.back_down + m.layer_gap / 2;
+ if (lb - lt <= room and lb - lt > widest) {
+ widest = lb - lt;
+ top = lt;
+ bottom = lb;
+ }
+ };
+ // Whole layers: the top of the one nearest where that would put the view's top.
+ const want = std.math.clamp((top + bottom - room) / 2, 0, g.h - room);
+ var best: f32 = 0;
+ for (g.nodes) |o| {
+ const at = std.math.clamp(o.y - m.top, 0, g.h - room);
+ if (@abs(at - want) < @abs(best - want)) best = at;
+ }
+ return best;
+}
+
+/// The linear rows' blocks (`disasm`, `descent`): a 2-unit rule across
+/// over each block's first row (not its function's), and where the row's
+/// annotation leaves it free the times the run had entered the block,
+/// ranged right, faint (`×11`), crossfading from the moment the mark
+/// leaves to the one it goes to.
+pub const Rules = struct {
+ of: [2]?Graph,
+ going: f32,
+
+ /// The blocks of the functions that hold `pc[0]` at `marks[0]` and `pc[1]` at `marks[1]`, `going` of the way between.
+ pub fn init(arena: std.mem.Allocator, rec: *const Recording, marks: [2]u64, pc: [2]u64, going: f32) Rules {
+ return .{ .of = .{ light(arena, rec, marks[0], pc[0]), light(arena, rec, marks[1], pc[1]) }, .going = going };
+ }
+
+ pub fn draw(r: Rules, u: ui.Ui, addr: u64, y: f32, w: f32, free: bool) void {
+ var visits: [2]?u32 = .{ null, null };
+ for (r.of, &visits) |maybe, *v| if (maybe) |g| if (blockAt(g.nodes, addr)) |k| if (g.nodes[k].start == addr and addr != g.start) {
+ v.* = g.nodes[k].visits;
+ };
+ if (visits[0] == null and visits[1] == null) return;
+ u.fill(.{ .x = theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = theme.stroke.rule }, theme.color.rule);
+ if (!free) return;
+ const size = theme.size.mono.context;
+ // The count the mark leaves, then the one it goes to; one that does not change, once.
+ const same = (visits[0] orelse 0) == (visits[1] orelse 0);
+ for (visits, [2]f32{ 1 - r.going, r.going }, 0..) |v, shows, k| {
+ const n = v orelse 0;
+ if (n == 0 or (same and k == 0)) continue;
+ const text = u.print("\u{00d7}{d}", .{n});
+ u.faded(if (same) 1 else shows).text(.mono, size, w - theme.metric.context.side - u.width(.mono, size, text), rows.baseline(y), text, theme.color.text.faint);
+ }
+ }
+};
+
+// ---------------------------------------------------------------- layout
+
+/// The graph of the pc's function at moment `i`, laid out for `form` in a
+/// place `room` high: folded first where it does not fit (the runs never
+/// reached, then long blocks: over 16 rows, their first four and last
+/// four, unless the pc is in it). Sizes from the form (`sizes`).
+/// shortcut: a loop the pc is not in and will not enter again is not folded, and nothing pans: a graph still too tall runs past its place.
+pub fn layout(arena: std.mem.Allocator, rec: *const Recording, i: u64, pc: u64, form: Form, room: f32) ?Graph {
+ const start = (zt.telescope.blocks(rec.moment(i), pc, &.{}) orelse return null).start;
+ for (memo) |maybe| if (maybe) |k| if (k.rec == rec and k.start == start and k.form == form and k.room == room) {
+ const here = blockAt(k.g.nodes, pc);
+ const spare: ?u32 = if (here) |h| (if (k.g.nodes[h].rows > theme.metric.graph.long) h else null) else null;
+ if (!k.long or spare == k.spared) return recount(arena, rec, k.g, i, pc) catch null;
+ };
+ var g = read(arena, rec, i, pc) orelse return null;
+ const m = metricOf(form);
+ sizes(rec, i, &g, form);
+ place(&g, m);
+ var long = false;
+ if (g.h > room) {
+ fold(arena, &g) catch return null;
+ sizes(rec, i, &g, form);
+ place(&g, m);
+ }
+ if (g.h > room) {
+ long = true;
+ for (g.nodes, 0..) |*n, k| if (n.folded == 0 and n.rows > theme.metric.graph.long and (g.here == null or g.here.? != k)) {
+ n.shown = 2 * theme.metric.graph.keep + 1;
+ };
+ sizes(rec, i, &g, form);
+ place(&g, m);
+ }
+ remember(.{ .rec = rec, .start = start, .form = form, .room = room, .long = long, .spared = if (g.here) |h| (if (g.nodes[h].rows > theme.metric.graph.long) h else null) else null, .g = g });
+ return g;
+}
+
+/// What `layout` made, kept: the blocks, their sizes and their places are
+/// the function's and the whole recording's, not the moment's (but for a
+/// long block the pc is in, which does not fold); a frame counts its own
+/// moment's visits again (`recount`).
+/// shortcut: process-wide, the last eight kept and never freed (a few KB each); two players at once would only share it.
+const Kept = struct { rec: *const Recording, start: u64, form: Form, room: f32, long: bool, spared: ?u32, g: Graph };
+var memo: [8]?Kept = @splat(null);
+var memo_next: usize = 0;
+var memo_memory: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
+
+fn remember(k: Kept) void {
+ var copy = k;
+ const a = memo_memory.allocator();
+ copy.g.nodes = a.dupe(Node, k.g.nodes) catch return;
+ copy.g.edges = a.dupe(Edge, k.g.edges) catch return;
+ memo[memo_next] = copy;
+ memo_next = (memo_next + 1) % memo.len;
+}
+
+/// A kept graph with the run counted again by moment `i`, the pc at `pc`.
+fn recount(arena: std.mem.Allocator, rec: *const Recording, kept_g: Graph, i: u64, pc: u64) !Graph {
+ var g = kept_g;
+ g.nodes = try arena.dupe(Node, kept_g.nodes);
+ g.edges = try arena.dupe(Edge, kept_g.edges);
+ for (g.nodes) |*n| n.visits = 0;
+ for (g.edges) |*e| e.count = 0;
+ g.here = blockAt(g.nodes, pc);
+ countBy(rec, &g, i, false);
+ return g;
+}
+
+/// Each node's size in `form`: on the map a head and its rows, as wide as
+/// its widest row, 300 at least; in the lens a box 24 high, 10 a row. A
+/// fold: a dashed box of a head and a row.
+pub fn sizes(rec: *const Recording, i: u64, g: *Graph, form: Form) void {
+ const gm = theme.metric.graph;
+ const cm = theme.metric.context;
+ const adv = theme.size.mono.context * 3 / 5;
+ var buf: [256]u8 = undefined;
+ for (g.nodes) |*n| switch (form) {
+ .lens => {
+ n.w = @max(gm.lens.least, gm.lens.lead + gm.lens.per_row * @as(f32, @floatFromInt(n.rows)));
+ n.h = gm.lens.box;
+ },
+ .map => {
+ if (n.folded > 0) {
+ n.w = gm.fold_w;
+ n.h = cm.tag + cm.row + cm.pad;
+ continue;
+ }
+ var widest: usize = 0;
+ var at = n.start;
+ while (g.text and at < n.start + n.len) {
+ const one = rec.instruction(i, at, &buf) orelse break;
+ widest = @max(widest, operandChars(one.text));
+ at += one.len;
+ }
+ n.w = @max(gm.least, gm.operands + @as(f32, @floatFromInt(widest)) * adv + 2 * cm.side);
+ n.h = cm.tag + @as(f32, @floatFromInt(n.shown)) * cm.row + cm.pad;
+ },
+ };
+}
+
+/// How many characters an instruction's operands take as `insn` draws them:
+/// the size words gone, a jump's or call's target its digits, byte cells a few more.
+fn operandChars(text: []const u8) usize {
+ const space = std.mem.indexOfScalar(u8, text, ' ') orelse return 0;
+ var ops = std.mem.trim(u8, text[space..], " ");
+ // A prefix (`rep stosq`, `notrack jmp`): the mnemonic is the next word.
+ for ([_][]const u8{ "rep", "repe", "repz", "repne", "repnz", "lock", "notrack", "bnd" }) |p| if (std.mem.eql(u8, text[0..space], p)) {
+ ops = std.mem.trim(u8, ops[std.mem.indexOfScalar(u8, ops, ' ') orelse ops.len ..], " ");
+ };
+ var n = ops.len;
+ var rest = ops;
+ while (std.mem.indexOf(u8, rest, " ptr ")) |p| {
+ var word = p;
+ while (word > 0 and rest[word - 1] != ' ' and rest[word - 1] != ',') word -= 1;
+ // The word and " ptr " go; the width's cells come.
+ n = n - (p - word + 5) + 5;
+ rest = rest[p + 5 ..];
+ }
+ return n;
+}
+
+/// Lays `g` out (its nodes' `w` and `h` given): layers, columns, every
+/// edge's way. Pure: the same blocks, sizes and whole-recording counts
+/// give the same picture at every moment.
+pub fn place(g: *Graph, m: Metric) void {
+ const nodes = g.nodes;
+ if (nodes.len == 0) return;
+ var buf: [1 << 16]u8 = undefined;
+ var fba = std.heap.FixedBufferAllocator.init(&buf);
+ const tmp = fba.allocator();
+ backEdges(g, tmp) catch return;
+ layers(g, tmp) catch return;
+ columns(g, tmp) catch return;
+ // Lanes: the outermost loop (the longest span) farthest out.
+ var order: [64]u32 = undefined;
+ var nb: u32 = 0;
+ for (g.edges, 0..) |e, k| if (e.back and nb < order.len) {
+ order[nb] = @intCast(k);
+ nb += 1;
+ };
+ std.mem.sort(u32, order[0..nb], g, struct {
+ fn wider(gr: *Graph, a: u32, b: u32) bool {
+ const ea = gr.edges[a];
+ const eb = gr.edges[b];
+ const sa = gr.nodes[ea.from].layer - gr.nodes[ea.to].layer;
+ const sb = gr.nodes[eb.from].layer - gr.nodes[eb.to].layer;
+ return if (sa != sb) sa > sb else ea.to < eb.to;
+ }
+ }.wider);
+ for (order[0..nb], 0..) |e, k| g.edges[e].lane = @intCast(k);
+ g.lanes = nb;
+ // Columns a pitch apart, right of the lanes; layers a gap apart.
+ var widest: f32 = 0;
+ var last_layer: u32 = 0;
+ for (nodes) |n| {
+ widest = @max(widest, n.w);
+ last_layer = @max(last_layer, n.layer);
+ }
+ const pitch = @max(m.pitch, widest + m.block_gap);
+ g.pitch = pitch;
+ const x0 = m.lane_in + @as(f32, @floatFromInt(nb)) * m.lane + m.blocks_in;
+ const tops = tmp.alloc(f32, last_layer + 2) catch return;
+ const heights = tmp.alloc(f32, last_layer + 1) catch return;
+ @memset(heights, 0);
+ for (nodes) |n| heights[n.layer] = @max(heights[n.layer], n.h);
+ tops[0] = m.top;
+ for (heights, 0..) |h, k| tops[k + 1] = tops[k] + h + m.layer_gap;
+ var right: f32 = 0;
+ var bottom: f32 = 0;
+ for (nodes) |*n| {
+ n.x = x0 + @as(f32, @floatFromInt(n.col)) * pitch;
+ n.y = tops[n.layer];
+ right = @max(right, n.x + n.w);
+ bottom = @max(bottom, n.y + n.h);
+ }
+ g.channel = right + m.channel;
+ route(g, m, tops, heights, tmp) catch return;
+ g.w = g.channel + m.lane_in;
+ // Under the last layer the way out: a stub and a bar.
+ g.h = bottom + m.layer_gap;
+}
+
+/// Back edges: those a walk from the entry (then from each block not yet
+/// walked, in address order) finds going to a block on the walk's way.
+fn backEdges(g: *Graph, tmp: std.mem.Allocator) !void {
+ const n = g.nodes.len;
+ const state = try tmp.alloc(u8, n);
+ @memset(state, 0);
+ // Each node's out edges, by their targets' addresses.
+ const outs = try outEdges(g, tmp);
+ const Frame = struct { node: u32, next: u32 };
+ var stack: std.ArrayList(Frame) = .empty;
+ for (0..n) |root| {
+ if (state[root] != 0) continue;
+ state[root] = 1;
+ try stack.append(tmp, .{ .node = @intCast(root), .next = 0 });
+ while (stack.items.len > 0) {
+ const top = &stack.items[stack.items.len - 1];
+ const list = outs[top.node];
+ if (top.next == list.len) {
+ state[top.node] = 2;
+ _ = stack.pop();
+ continue;
+ }
+ const e = list[top.next];
+ top.next += 1;
+ const to = g.edges[e].to;
+ if (state[to] == 1) {
+ g.edges[e].back = true;
+ } else if (state[to] == 0) {
+ state[to] = 1;
+ try stack.append(tmp, .{ .node = to, .next = 0 });
+ }
+ }
+ }
+}
+
+fn outEdges(g: *const Graph, tmp: std.mem.Allocator) ![][]u32 {
+ const outs = try tmp.alloc([]u32, g.nodes.len);
+ const counts = try tmp.alloc(u32, g.nodes.len);
+ @memset(counts, 0);
+ for (g.edges) |e| counts[e.from] += 1;
+ for (outs, counts) |*o, c| o.* = try tmp.alloc(u32, c);
+ @memset(counts, 0);
+ for (g.edges, 0..) |e, k| {
+ outs[e.from][counts[e.from]] = @intCast(k);
+ counts[e.from] += 1;
+ }
+ for (outs) |o| std.mem.sort(u32, o, g, struct {
+ fn first(gr: *const Graph, a: u32, b: u32) bool {
+ return gr.nodes[gr.edges[a].to].start < gr.nodes[gr.edges[b].to].start;
+ }
+ }.first);
+ return outs;
+}
+
+/// Layers by the longest forward path from a block with none into it; the
+/// blocks that return in the last (one more where another is there).
+fn layers(g: *Graph, tmp: std.mem.Allocator) !void {
+ const n = g.nodes.len;
+ const ins = try tmp.alloc(u32, n);
+ @memset(ins, 0);
+ for (g.edges) |e| if (!e.back) {
+ ins[e.to] += 1;
+ };
+ var ready: std.ArrayList(u32) = .empty;
+ for (ins, 0..) |c, k| if (c == 0) try ready.append(tmp, @intCast(k));
+ for (g.nodes) |*node| node.layer = 0;
+ var head: usize = 0;
+ while (head < ready.items.len) : (head += 1) {
+ const a = ready.items[head];
+ for (g.edges) |e| if (!e.back and e.from == a) {
+ g.nodes[e.to].layer = @max(g.nodes[e.to].layer, g.nodes[a].layer + 1);
+ ins[e.to] -= 1;
+ if (ins[e.to] == 0) try ready.append(tmp, e.to);
+ };
+ }
+ var last: u32 = 0;
+ for (g.nodes) |node| if (node.end != .ret) {
+ last = @max(last, node.layer);
+ };
+ var any_ret = false;
+ for (g.nodes) |node| any_ret = any_ret or node.end == .ret;
+ if (!any_ret) return;
+ var deepest_ret: u32 = 0;
+ for (g.nodes) |node| if (node.end == .ret) {
+ deepest_ret = @max(deepest_ret, node.layer);
+ };
+ const others_last = for (g.nodes) |node| {
+ if (node.end != .ret and node.layer == @max(last, deepest_ret)) break true;
+ } else false;
+ const at = @max(last, deepest_ret) + @intFromBool(others_last);
+ for (g.nodes) |*node| if (node.end == .ret) {
+ node.layer = at;
+ };
+}
+
+/// Columns, a layer at a time: a block under the predecessor that reached
+/// it most often in the whole recording; else under the one it falls from;
+/// else (a taken target never taken) right of it. Where two want one
+/// column the more travelled has it, then the fall-through, then the
+/// lower address; the other the next free column to the right.
+fn columns(g: *Graph, tmp: std.mem.Allocator) !void {
+ const n = g.nodes.len;
+ var last: u32 = 0;
+ for (g.nodes) |node| last = @max(last, node.layer);
+ const Want = struct { node: u32, col: u32, total: u32, fall: bool };
+ var wants: std.ArrayList(Want) = .empty;
+ var used: std.ArrayList(u32) = .empty;
+ const placed = try tmp.alloc(bool, n);
+ @memset(placed, false);
+ for (0..last + 1) |layer| {
+ wants.clearRetainingCapacity();
+ for (g.nodes, 0..) |node, k| if (node.layer == layer) {
+ var best: ?Edge = null;
+ for (g.edges) |e| if (!e.back and e.to == k and placed[e.from]) {
+ const better = if (best) |b| e.total > b.total or (e.total == b.total and (e.kind == .fall and b.kind != .fall or (e.kind == .fall) == (b.kind == .fall) and g.nodes[e.from].start < g.nodes[b.from].start)) else true;
+ if (better) best = e;
+ };
+ const col: u32, const total: u32, const fall = if (best) |b|
+ .{ g.nodes[b.from].col + @intFromBool(b.total == 0 and b.kind != .fall), b.total, b.kind == .fall }
+ else
+ .{ 0, 0, false };
+ try wants.append(tmp, .{ .node = @intCast(k), .col = col, .total = total, .fall = fall });
+ };
+ std.mem.sort(Want, wants.items, g, struct {
+ fn first(gr: *Graph, a: Want, b: Want) bool {
+ if (a.total != b.total) return a.total > b.total;
+ if (a.fall != b.fall) return a.fall;
+ return gr.nodes[a.node].start < gr.nodes[b.node].start;
+ }
+ }.first);
+ used.clearRetainingCapacity();
+ for (wants.items) |w| {
+ var col = w.col;
+ while (std.mem.indexOfScalar(u32, used.items, col) != null) col += 1;
+ try used.append(tmp, col);
+ g.nodes[w.node].col = col;
+ placed[w.node] = true;
+ }
+ }
+}
+
+/// Every edge's way. Forward: out of the bottom on the side its target is
+/// on (`port` in from it, or the middle when it is straight below), along
+/// the middle of the gap under its layer, into its target's top (several
+/// into one top `arrivals` apart, in their sources' order); through the
+/// right channel where it would cross a block. Back: down `back_down`, out
+/// to its lane, up it, into its header's head from the left.
+fn route(g: *Graph, m: Metric, tops: []const f32, heights: []const f32, tmp: std.mem.Allocator) !void {
+ const n = g.nodes.len;
+ const tracks = try tmp.alloc(u32, heights.len);
+ @memset(tracks, 0);
+ const back_tracks = try tmp.alloc(u32, heights.len);
+ @memset(back_tracks, 0);
+ // The nodes by layer, then left to right: the edges of one gap take its tracks in that order.
+ const order = try tmp.alloc(u32, n);
+ for (order, 0..) |*o, k| o.* = @intCast(k);
+ std.mem.sort(u32, order, g, struct {
+ fn first(gr: *Graph, a: u32, b: u32) bool {
+ const na = gr.nodes[a];
+ const nb = gr.nodes[b];
+ return if (na.layer != nb.layer) na.layer < nb.layer else na.x < nb.x;
+ }
+ }.first);
+ for (order) |a| for (g.edges) |*e| if (e.from == a) {
+ const src = g.nodes[e.from];
+ const dst = g.nodes[e.to];
+ const under = tops[src.layer] + heights[src.layer];
+ if (e.back) {
+ const lx = m.lane_in + @as(f32, @floatFromInt(e.lane)) * m.lane;
+ const by = under + m.back_down + @as(f32, @floatFromInt(back_tracks[src.layer])) * 8;
+ back_tracks[src.layer] += 1;
+ const sx = src.x + m.port;
+ const hy = dst.y + m.head_in;
+ e.points[0..5].* = .{ .{ sx, src.y + src.h }, .{ sx, by }, .{ lx, by }, .{ lx, hy }, .{ dst.x - 1, hy } };
+ e.n = 5;
+ continue;
+ }
+ // Its place among the arrivals into its target's top.
+ var srcs: usize = 0;
+ var slot: usize = 0;
+ for (g.edges) |o| if (!o.back and o.to == e.to) {
+ srcs += 1;
+ const os = g.nodes[o.from];
+ if (os.x < src.x or (os.x == src.x and o.from < e.from)) slot += 1;
+ };
+ const tx = dst.x + dst.w / 2 + (@as(f32, @floatFromInt(slot)) - @as(f32, @floatFromInt(srcs - 1)) / 2) * m.arrivals;
+ const crosses = for (g.nodes) |o| {
+ if (o.layer > src.layer and o.layer < dst.layer and o.x - 8 < tx and tx < o.x + o.w + 8) break true;
+ } else false;
+ const side = if (crosses) 1 else (dst.x + dst.w / 2) - (src.x + src.w / 2);
+ const sx = if (!crosses and @abs(side) < src.w / 2 - m.port) src.x + src.w / 2 else if (side > 0) src.x + src.w - m.port else src.x + m.port;
+ const gy = under + m.layer_gap / 2 + @as(f32, @floatFromInt(tracks[src.layer])) * m.track;
+ tracks[src.layer] += 1;
+ if (crosses) {
+ const gy2 = tops[dst.layer] - m.layer_gap / 2;
+ e.points = .{ .{ sx, src.y + src.h }, .{ sx, gy }, .{ g.channel, gy }, .{ g.channel, gy2 }, .{ tx, gy2 }, .{ tx, dst.y } };
+ e.n = 6;
+ } else {
+ e.points[0..4].* = .{ .{ sx, src.y + src.h }, .{ sx, gy }, .{ tx, gy }, .{ tx, dst.y } };
+ e.n = 4;
+ }
+ };
+}
+
+// ---------------------------------------------------------------- an instant
+
+/// The graph as a frame shows it: at the stop shown (`g`, where everything
+/// is) and the stop the step came from (`was`, the same function), `p` of
+/// the way from the one to the other; a walked step across one edge is
+/// `leave`'s (or `turn`'s) frames; the panel's scroll.
+pub const Instant = struct {
+ g: Graph,
+ was: Graph,
+ p: f32,
+ leave: ?Leave = null,
+ pan: f32 = 0,
+ now: cursors.Now,
+};
+
+/// A step across one edge, at a walk (`leave`, `turn`): the edge drawn gold
+/// (`draw`, 0 to 1), the mark along it (`travel`), seconds since it landed.
+pub const Leave = struct { edge: u32, draw: f32, travel: f32, landed: f32 };
+
+/// The graph of the function the pc is in, in `form`, in a place `room` high, now.
+pub fn instant(arena: std.mem.Allocator, now: cursors.Now, form: Form, room: f32) ?Instant {
+ const rec = now.rec;
+ const at = [2]u64{ now.stops[1], now.stops[0] };
+ const pc = [2]u64{ rec.register(at[0], "rip"), rec.register(at[1], "rip") };
+ const g = layout(arena, rec, at[1], pc[1], form, room) orelse return null;
+ var was = g;
+ if (at[0] != at[1] and pc[0] -% g.start < g.len) was = layout(arena, rec, at[0], pc[0], form, room) orelse g;
+ // shortcut: a step into another function changes the graph in a frame (no crossfade between two); scenes keep to one.
+ if (was.nodes.len != g.nodes.len or was.edges.len != g.edges.len) was = g;
+ var out: Instant = .{ .g = g, .was = was, .p = now.advancing(), .now = now };
+ const m = metricOf(form);
+ var q = out.p;
+ // One instruction across an edge, walked: its frames are `leave`'s.
+ if (now.walking() and at[1] == at[0] + 1) if (was.here) |a| if (g.here) |b| if (a != b or pc[1] == g.nodes[b].start) if (g.edgeOf(a, b)) |e| {
+ const l = time.leave;
+ const plan = now.plan;
+ out.leave = .{
+ .edge = e,
+ .draw = cursors.standard(clamp01((now.time - (plan.advance - l.edge)) / l.edge)),
+ .travel = cursors.glide(clamp01((now.time - plan.advance) / @max(plan.travel, 1.0 / 60.0))),
+ .landed = now.time - plan.advance - plan.travel,
+ };
+ q = out.leave.?.travel;
+ };
+ out.pan = std.math.lerp(pan(was, room, m), pan(g, room, m), cursors.glide(q));
+ return out;
+}
+
+fn clamp01(v: f32) f32 {
+ return std.math.clamp(v, 0, 1);
+}
+
+/// How long the mark takes along edge `e`: 12 f and 2 f a 100 units, 30 f at most.
+pub fn markTime(e: *const Edge) f32 {
+ const l = time.leave;
+ return @min(l.mark + l.per_100 * e.length() / 100, l.mark_most);
+}
+
+/// `leave` (and `turn`, the same along a loop's lane): from a stop on a
+/// block's last instruction (a jump; or one that falls into the next
+/// block), one step across the edge it takes, to motion.md's frames: fire;
+/// the compare's chips framed as read (the CPU's slot, 18 f), the cause;
+/// the edge drawn gold, head first (24 to 42 f); the mark along it, 12 f
+/// and 2 f a 100 units of the lens's edge (30 f at most); landing, the
+/// advance; the target's count struck 6 f after (a turn's lane count as it
+/// lands); the oldest trail edge cooled (18 f); a rest to 42 f after the landing.
+pub fn leave(c: cursors.Cursor, s: *anim.Frame) void {
+ const from = c.moment(s);
+ const to = from + 1;
+ const l = time.leave;
+ var buf: [1 << 18]u8 = undefined;
+ var fba = std.heap.FixedBufferAllocator.init(&buf);
+ const rec = c.rec;
+ var mark: f32 = l.mark;
+ if (layout(fba.allocator(), rec, to, rec.register(to, "rip"), .lens, std.math.inf(f32))) |g| {
+ if (blockAt(g.nodes, rec.register(from, "rip"))) |a| if (g.here) |b| if (g.edgeOf(a, b)) |e| {
+ mark = markTime(&g.edges[e]);
+ };
+ }
+ const plan: cursors.Plan = .{ .at = @splat(l.chips), .advance = l.sets_off, .travel = mark, .total = l.sets_off + mark + l.rest };
+ c.step(s, to, plan, plan.total);
+}
+
+/// The stop on the last instruction of the block the cursor is in (where
+/// a `leave` sets off): the first moment from `from` at a jump in its
+/// function, or before a stop in another of its blocks.
+pub fn blockEnd(rec: *const Recording, from: u64) u64 {
+ var buf: [1 << 16]u8 = undefined;
+ var fba = std.heap.FixedBufferAllocator.init(&buf);
+ const g = light(fba.allocator(), rec, from, rec.register(from, "rip")) orelse return from;
+ var k = from;
+ while (k + 1 < rec.len()) : (k += 1) {
+ const pc = rec.register(k, "rip");
+ const here = blockAt(g.nodes, pc) orelse continue;
+ if (rec.insn_branches[k]) |b| if (b.kind == .jump or b.kind == .ret) return k;
+ const next = blockAt(g.nodes, rec.register(k + 1, "rip")) orelse continue;
+ if (next != here) return k;
+ }
+ return k;
+}
+
+/// One step from where the cursor is to the end of its block (`blockEnd`), walked: what the block does, as a cascade.
+pub fn toEnd(c: cursors.Cursor, s: *anim.Frame) void {
+ const from = c.moment(s);
+ const to = blockEnd(c.rec, from);
+ if (to == from) return;
+ const plan = c.reading(from, to);
+ c.step(s, to, plan, plan.total);
+}
+
+/// An edge's look in `g`: never taken `rule`, taken `dim`, the last three
+/// taken the trail's golds (the newest 3 wide). By line, all `rule`.
+fn edgeLook(g: Graph, e: u32) struct { ink: u24, w: f32 } {
+ const tr = theme.color.trail;
+ if (!g.exact) return .{ .ink = theme.color.rule, .w = theme.stroke.rule };
+ for (g.trail, 0..) |maybe, k| if (maybe) |te| if (te == e) return .{ .ink = tr[k], .w = if (k == 0) 3 else theme.stroke.rule };
+ return .{ .ink = if (g.edges[e].count > 0) t.dim else theme.color.rule, .w = theme.stroke.rule };
+}
+
+/// A count written as the mark lands: armed 12 f before `s`, struck at
+/// `s` (the fill in over 6 f, its edges out and back over 12 f), solid
+/// 12 f, then cooled to nothing by `end` (the step's end): no corner, the
+/// next step has nothing of it to keep.
+fn landing(now: cursors.Now, s: f32) ui.Heat {
+ const st = time.strike;
+ const tm = now.time;
+ if (tm < s - st.arm) return .none;
+ if (tm < s) return .{ .arm = clamp01((tm - (s - st.arm)) / time.stagger) };
+ const cool_from = s + st.pulse;
+ const end = @max(now.plan.total, cool_from + 6.0 / 60.0);
+ return .{ .level = 1, .solid = 1 - cursors.glide(clamp01((tm - cool_from) / (end - cool_from))), .in = clamp01((tm - s) / st.in), .out = if (tm - s < st.pulse) theme.pulse((tm - s) / st.pulse) else 0, .corner = 0 };
+}
+
+/// What a count shows: the old number until a strike's fill is half in,
+/// then the new; a strike's paper under it, on its `ground` (a box's,
+/// or the page). Crossfading (`p`) where it changes unstruck. `prefix`
+/// before the digits; ranged right of `x` when `right`.
+fn number(u: ui.Ui, face: sk.Face, size: f32, x: f32, base_y: f32, right: bool, was: u32, is: u32, ink: u24, heat: ?ui.Heat, ground: ?u24, p: f32, prefix: []const u8) void {
+ const one = struct {
+ fn draw(v: ui.Ui, fc: sk.Face, sz: f32, xx: f32, by: f32, r: bool, n: u32, c: anytype, pre: []const u8) void {
+ if (n == 0) return;
+ const text = v.print("{s}{d}", .{ pre, n });
+ v.text(fc, sz, if (r) xx - v.width(fc, sz, text) else xx, by, text, c);
+ }
+ }.draw;
+ if (was == is) return one(u, face, size, x, base_y, right, is, ink, prefix);
+ if (heat) |h| {
+ const text = u.print("{s}{d}", .{ prefix, is });
+ const w = u.width(face, size, text);
+ const left = if (right) x - w else x;
+ // Nothing under it but while it is armed or its paper shows: its ground is the box's own, or the page.
+ // Inside its box's frame: the frame never goes under the paper and back.
+ if (h.level > 0 and h.solid > 0 or h.arm > 0) _ = u.cellWritten(.{ .x = left - 3, .y = base_y - size * 0.7, .w = w + 6, .h = size * 0.8 }, h, ground);
+ if (h.level < 1 or h.in < 0.5) return one(u, face, size, x, base_y, right, was, ink, prefix);
+ // Bold and bright on the paper, cooling to its own ink and face with it.
+ const hot = h.solid * h.in;
+ const bold = hot >= 0.5;
+ return one(u, if (bold) (if (face == .sans) .sans_bold else .mono_bold) else face, size, x, base_y, right, is, ui.mix(ink, t.bright, hot), prefix);
+ }
+ one(u.faded(1 - p), face, size, x, base_y, right, was, ink, prefix);
+ one(u.faded(p), face, size, x, base_y, right, is, ink, prefix);
+}
+
+/// The edges of `v`, in `u` (the graph's origin): each in its look, mixed
+/// from the one before as the step lands; the one a leave takes drawn
+/// gold, head first, over its old look, and cooling to the trail's newest
+/// after the landing; each loop's turns under its turn into its header.
+fn drawEdges(u: ui.Ui, v: Instant, form: Form) void {
+ const g = v.g;
+ const lens = form == .lens;
+ const gm = theme.metric.graph;
+ const head: [2]f32 = if (lens) gm.lens.head else gm.head;
+ const radius: f32 = if (lens) gm.lens.corner else gm.corner;
+ // Recoloured with the landing (a leave's 18 f), else with the advance.
+ const q = if (v.leave) |l| clamp01(l.landed / time.leave.trail) else v.p;
+ for (g.edges, 0..) |e, k| {
+ const a = edgeLook(v.was, @intCast(k));
+ const b = edgeLook(g, @intCast(k));
+ const ink = ui.mix(a.ink, b.ink, q);
+ const w = std.math.lerp(a.w, b.w, q);
+ var pts: [6]ui.Vec = undefined;
+ for (e.path(), 0..) |pt, n| pts[n] = .{ pt[0], pt[1] };
+ u.route(pts[0..e.n], ink, .{ .width = if (ui.strokes) 20 else w, .head = if (ui.strokes) null else head, .radius = radius });
+ if (v.leave) |l| if (l.edge == k) {
+ const gold = theme.mark(.current).solid;
+ u.faded(1 - q).route(pts[0..e.n], gold, .{ .width = if (ui.strokes) 20 else 3, .head = if (ui.strokes) null else head, .radius = radius, .progress = l.draw });
+ };
+ // A loop's turns so far, on its lane under the turn into its header; struck as a turn lands.
+ if (e.back and (e.count > 0 or v.was.edges[k].count > 0) and !ui.strokes) {
+ const lx = pts[2][0];
+ const hy = pts[3][1];
+ const strike: ?ui.Heat = if (v.leave) |l| (if (l.edge == k) landing(v.now, v.now.plan.advance + v.now.plan.travel) else null) else null;
+ number(u, .sans, theme.size.sans.micro, lx + 5, hy + 22, false, v.was.edges[k].count, e.count, t.faint, strike, null, v.p, "\u{00d7}");
+ }
+ // On the map a conditional's counts by its ports.
+ if (!lens and !e.back and e.kind != .jump and g.exact and !ui.strokes) {
+ const outs = for (g.edges) |o| {
+ if (o.from == e.from and !o.back and o.to != e.to) break true;
+ } else false;
+ if (outs) number(u, .mono, theme.size.mono.context, pts[0][0] + 8, pts[0][1] + 20, false, v.was.edges[k].count, e.count, t.faint, null, null, v.p, "");
+ }
+ }
+}
+
+/// The mark on its way along the edge a leave takes: a gold plaque (half
+/// size in the lens) with an ink head pointing the way it goes.
+fn drawMark(u: ui.Ui, v: Instant, form: Form) void {
+ const l = v.leave orelse return;
+ if (l.travel <= 0 or l.travel >= 1) return;
+ const e = &v.g.edges[l.edge];
+ const at, const dir = e.at(l.travel * e.length());
+ const side: f32 = if (form == .lens) 12 else theme.metric.plaque.pc;
+ const gold = theme.mark(.current);
+ u.fill(.{ .x = at[0] - side / 2, .y = at[1] - side / 2, .w = side, .h = side }, gold.solid);
+ if (ui.strokes) return;
+ const h = side / 4;
+ const tip: ui.Vec = .{ at[0] + dir[0] * h, at[1] + dir[1] * h };
+ const back: ui.Vec = .{ at[0] - dir[0] * h, at[1] - dir[1] * h };
+ const across: ui.Vec = .{ -dir[1] * h, dir[0] * h };
+ sk.draw.polygon(u.f, &.{ tip, back + across, back - across }, u.at.pen(.{ .fill = ui.rgb(gold.on_solid) }));
+}
+
+/// How much node `k` is the pc's: as the mark leaves it and lands, or with the advance.
+fn golden(v: Instant, k: u32) f32 {
+ const was: f32 = if (v.was.here == k) 1 else 0;
+ const is: f32 = if (v.g.here == k) 1 else 0;
+ if (v.leave) |l| {
+ if (was > is) return 1 - l.travel;
+ if (is > was) return clamp01(l.landed / time.leave.count);
+ return is;
+ }
+ return std.math.lerp(was, is, v.p);
+}
+
+/// The `blocks` lens (design/README.md, `graph`): over the program column
+/// the function's graph without its text, a block a box 24 high, as wide
+/// as its rows (60 and 10 a row, 132 at least), its offset and its visits,
+/// the pc's framed gold 3; the edges, the lanes and the trail at half
+/// measure. Its view is the disassembly itself, over the state column
+/// (the split `blocks`).
+pub const Blocks = struct {
+ cursor: cursors.Cursor,
+ stage: *const Stage,
+ focus: bool = false,
+
+ pub fn draw(b: *const Blocks, f: *sk.Frame, placed: anim.Placed) void {
+ const box = b.stage.box(placed, .band);
+ const m = theme.metric.context;
+ if (box.h < m.tag + 24) return;
+ const u = context.at(f, placed, box).faded(placed.value(b.stage.drawing));
+ u.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h });
+ defer u.unclip();
+ const now = b.cursor.now(placed);
+ const room = box.h - m.tag - theme.stroke.rule;
+ const v = instant(f.scratch, now, .lens, room) orelse {
+ _ = u.panel(box.w, box.h, "its blocks", .{ .focus = b.focus });
+ return;
+ };
+ const name = ui.symbol(u, b.cursor.rec.strings.get(v.g.start) orelse "");
+ _ = u.panel(box.w, box.h, u.print("the blocks of {s}", .{name[0 .. std.mem.lastIndexOfScalar(u8, name, '+') orelse name.len]}), .{ .focus = b.focus, .note = "its blocks \u{00b7} the visits \u{00b7} the path" });
+ u.clip(.{ .x = theme.stroke.rule, .y = m.tag, .w = box.w - 2 * theme.stroke.rule, .h = room });
+ const o = u.moved(0, m.tag - v.pan);
+ drawEdges(o, v, .lens);
+ const gm = theme.metric.graph.lens;
+ const size = theme.size.mono.context;
+ for (v.g.nodes, v.was.nodes, 0..) |n, was, k| {
+ const r: ui.Rect = .{ .x = n.x, .y = n.y, .w = n.w, .h = n.h };
+ const gold = golden(v, @intCast(k));
+ const seen = std.math.lerp(@as(f32, if (was.visits > 0) 1 else 0), @as(f32, if (n.visits > 0) 1 else 0), if (v.leave != null) clamp01(v.leave.?.landed / time.leave.count) else v.p);
+ o.fill(r, ui.mix(theme.color.page, theme.color.raised, seen));
+ if (n.folded > 0) o.dashedFrame(r, theme.color.rule, theme.stroke.rule) else o.frame(r, ui.mix(theme.color.rule, theme.mark(.current).solid, gold), std.math.lerp(theme.stroke.rule, gm.gold, gold));
+ const base_y = rows.baseline(n.y);
+ const off = o.print("+{x}", .{n.start - v.g.start});
+ o.text(.mono, size, n.x + 8, base_y, off, ui.mix(t.faint, t.primary, seen));
+ const strike: ?ui.Heat = if (v.leave) |l| (if (v.g.edges[l.edge].to == k) landing(now, now.plan.advance + now.plan.travel + time.leave.count) else null) else null;
+ number(o, .mono, size, n.x + n.w - 8, base_y, true, was.visits, n.visits, t.dim, strike, if (seen >= 0.5) theme.color.raised else theme.color.page, v.p, "\u{00d7}");
+ }
+ drawMark(o, v, .lens);
+ u.unclip();
+ }
+};
+
+/// The disassembly as its graph, on the map (design/README.md, `graph`;
+/// mock 22): two columns wide beside the source. A block is a head (its
+/// first address, its lines, how often the run entered it) over its rows,
+/// `insn`'s with the offset where the address was; the annotation only in
+/// the pc's block, which widens to its column; a call a hook out of the
+/// block's side and back a row down; under a return, the way out. Too tall,
+/// the runs never reached fold, then long blocks, then it pans.
+pub const Map = struct {
+ cursor: cursors.Cursor,
+ stage: *const Stage,
+
+ pub fn draw(gm: *const Map, f: *sk.Frame, placed: anim.Placed) void {
+ const box = gm.stage.box(placed, .disasm);
+ const m = theme.metric.context;
+ if (box.h < m.tag + m.row) return;
+ const u = context.at(f, placed, box);
+ const now = gm.cursor.now(placed);
+ const rec = gm.cursor.rec;
+ const room = box.h - m.tag - theme.stroke.rule;
+ const v = instant(f.scratch, now, .map, room) orelse {
+ _ = u.panel(box.w, box.h, "disassembly", .{});
+ return;
+ };
+ const g = v.g;
+ const full = rec.strings.get(g.start) orelse "";
+ const sym = ui.symbol(u, full);
+ const name = sym[0 .. std.mem.lastIndexOfScalar(u8, sym, '+') orelse sym.len];
+ const module: []const u8 = if (std.mem.indexOfScalar(u8, full, '!')) |bang| full[0..bang] else "";
+ const folded = g.foldedBlocks();
+ const title = if (module.len > 0) u.print("disassembly \u{00b7} {s} \u{00b7} {s}", .{ name, module }) else u.print("disassembly \u{00b7} {s}", .{name});
+ const note = if (folded > 0) u.print("{s}{d} blocks \u{00b7} folded: {d} never reached", .{ if (rec.sourced(now.stops[0])) "" else "no source \u{00b7} ", g.blocks, folded }) else if (g.exact) "its blocks \u{00b7} visits so far \u{00b7} the edges the run took" else "its blocks \u{00b7} visits so far";
+ _ = u.panel(box.w, box.h, title, .{ .note = note });
+ u.clip(.{ .x = theme.stroke.rule, .y = m.tag, .w = box.w - 2 * theme.stroke.rule, .h = room });
+ defer u.unclip();
+ const o = u.moved(0, m.tag - v.pan);
+ drawEdges(o, v, .map);
+ const d: disasm.Disasm = .{ .cursor = gm.cursor, .stage = gm.stage };
+ const body: ui.Rect = .{ .x = theme.stroke.rule, .y = v.pan, .w = box.w - 2 * theme.stroke.rule, .h = room };
+ for (g.nodes, 0..) |_, k| block(o, v, @intCast(k), d, body);
+ drawMark(o, v, .map);
+ }
+
+ /// Block `k`: its head, its rows, its hooks and its way out.
+ fn block(o: ui.Ui, v: Instant, k: u32, d: disasm.Disasm, body: ui.Rect) void {
+ const gm = theme.metric.graph;
+ const m = theme.metric.context;
+ const size = theme.size.mono.context;
+ const adv = size * 3 / 5;
+ const now = v.now;
+ const rec = now.rec;
+ const g = v.g;
+ const n = g.nodes[k];
+ const was = v.was.nodes[k];
+ const here = g.here == k or v.was.here == k;
+ // The pc's block widens to its column for the annotation, never into the channel.
+ const w = if (here and n.folded == 0) @max(n.w, @min(g.pitch - gm.block_gap, g.channel - gm.channel / 2 - n.x)) else n.w;
+ const r: ui.Rect = .{ .x = n.x, .y = n.y, .w = w, .h = n.h };
+ const landing_q = if (v.leave) |l| clamp01(l.landed / time.leave.count) else v.p;
+ const seen = std.math.lerp(@as(f32, if (was.visits > 0) 1 else 0), @as(f32, if (n.visits > 0) 1 else 0), landing_q);
+ const gold = golden(v, k);
+ o.fill(r, theme.color.page);
+ if (n.folded > 0) {
+ // Blocks the whole recording never reached: one dashed box, its offsets and its count.
+ o.dashedFrame(r, theme.color.rule, theme.stroke.rule);
+ const last = n.start + n.len - 1;
+ o.text(.mono, size, n.x + m.side, rows.baseline(n.y), o.print("+{x} to +{x}", .{ n.start - g.start, last - g.start }), t.faint);
+ const words = if (n.folded > 1) o.print("{d} blocks never reached", .{n.folded}) else "never reached";
+ const micro = theme.size.sans.micro;
+ o.text(.sans, micro, n.x + w - m.side - o.width(.sans, micro, words), n.y + m.tag - 6, words, t.faint);
+ return;
+ }
+ o.fill(.{ .x = r.x, .y = r.y, .w = r.w, .h = m.tag }, ui.mix(theme.color.page, theme.color.raised, seen));
+ o.frame(r, ui.mix(theme.color.rule, theme.mark(.current).solid, gold), theme.stroke.rule);
+ // The head: its first address, its lines, its visits.
+ const head_y = rows.baseline(n.y);
+ const end = o.address(n.x + m.side, head_y, n.start, .{ .ink = ui.rgb(theme.color.region.code_) });
+ var buf: [256]u8 = undefined;
+ var first_line: ?u32 = null;
+ var last_line: ?u32 = null;
+ var at = n.start;
+ const i = now.stops[0];
+ // Its rows, as `insn` takes them (a long block: four, `more`, four).
+ var list: std.ArrayList(disasm.Insn) = .empty;
+ while (at < n.start + n.len) {
+ const one = rec.instruction(i, at, &buf) orelse break;
+ list.append(o.f.scratch, .{ .addr = at, .len = one.len, .text = o.print("{s}", .{one.text}), .line = one.line, .params = one.params, .bare = true }) catch break;
+ if (one.line) |l| {
+ first_line = @min(first_line orelse l, l);
+ last_line = @max(last_line orelse l, l);
+ }
+ at += one.len;
+ }
+ const micro = theme.size.sans.micro;
+ if (first_line) |a| o.text(.sans, micro, end + 16, n.y + m.tag - 6, if (a == last_line.?) o.print("line {d}", .{a}) else o.print("lines {d} to {d}", .{ a, last_line.? }), t.faint);
+ const strike: ?ui.Heat = if (v.leave) |l| (if (v.g.edges[l.edge].to == k) landing(now, now.plan.advance + now.plan.travel + time.leave.count) else null) else null;
+ number(o, .mono, size, n.x + w - m.side, head_y, true, was.visits, n.visits, t.primary, strike, if (seen >= 0.5) theme.color.raised else theme.color.page, v.p, "\u{00d7}");
+
+ // Where the mark is, and was: the plaque leaves its row as the mark sets off, and lands with it.
+ const pcs = [2]u64{ rec.register(now.stops[1], "rip"), rec.register(now.stops[0], "rip") };
+ var shows = [2]f32{ 1 - v.p, v.p };
+ if (v.leave) |l| shows = .{ 1 - clamp01((now.time - now.plan.advance) / time.leave.count), clamp01(l.landed / time.leave.count) };
+ if (pcs[0] == pcs[1]) shows = .{ 0, 1 };
+ // What the annotation reads as current (the moment the mark stands for), and the rows that ran in the last three steps.
+ const going = now.advancing();
+ const base: usize = if (going >= 0.5) 0 else 1;
+ const current_line = rec.own[now.marked()];
+ const annotated = g.here == k and now.plan.gait != .sprint;
+ const sprinting = now.plan.gait == .sprint and !now.resting();
+ const echo = disasm.jumpEcho(now, base);
+ var covered: usize = 0;
+ var line_before: ?u32 = null;
+ const long = n.shown < n.rows;
+ // Its rows' annotations stay in it: a call's arguments hang no further than its last row.
+ const top = @max(n.y, body.y);
+ const under = @min(n.y + n.h, body.y + body.h);
+ if (under > top) o.clip(.{ .x = body.x, .y = top, .w = body.w, .h = under - top });
+ var y = n.y + m.tag;
+ for (list.items, 0..) |row, j| {
+ if (long and j >= gm.keep and j < list.items.len - gm.keep) {
+ if (j == gm.keep) {
+ _ = o.arrow(n.x + gm.offset - 36, y + m.row / 2, .more);
+ o.text(.mono, size, n.x + gm.mnemonic, rows.baseline(y), o.print("{d} rows", .{list.items.len - 2 * gm.keep}), t.faint);
+ y += m.row;
+ }
+ continue;
+ }
+ const rr: ui.Rect = .{ .x = n.x, .y = y, .w = w, .h = m.row };
+ @import("map.zig").row(.disasm, row.addr, o, rr);
+ for (pcs, shows) |pc, a| if (pc == row.addr and a > 0) {
+ const lit = o.faded(a);
+ lit.current(.{ .x = n.x + theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = m.row }, theme.metric.plaque.pc, now.fires(.disasm));
+ sk.draw.polygon(o.f, &.{ .{ n.x + 20, y + m.row / 2 }, .{ n.x + 10, y + 6 }, .{ n.x + 10, y + m.row - 6 } }, lit.at.pen(.{ .fill = ui.rgb(theme.mark(.current).on_solid) }));
+ };
+ const ran = if (annotated) ranAt(now, base, row.addr) else null;
+ if (ran) |rn| if (row.addr != pcs[1]) o.trail(n.x + theme.metric.insn.mark, y, rn.k);
+ // The offset where the address was.
+ const off = o.print("+{x}", .{row.addr - g.start});
+ o.text(.mono, size, n.x + gm.offset - o.width(.mono, size, off), rows.baseline(y), off, t.faint);
+ const lit = o.moved(n.x - gm.shift, 0);
+ if (annotated and !sprinting) {
+ const state: disasm.Disasm.State = if (row.addr == rec.register(now.marks[if (going >= 0.5) 1 else 0], "rip")) .current else if (ran) |rn| .{ .ran = rn } else .ahead;
+ const hangs = d.insn(lit, now, row, y, w + gm.shift, 0, row.line, row.line != line_before, current_line, 0, state, base, covered > 0, .{ g.start, g.start + g.len }, echo);
+ covered = @max(covered -| 1, hangs);
+ } else {
+ if (row.line) |l| if (row.line != line_before) {
+ const num = o.print("{d}", .{l});
+ o.text(.mono, size, n.x + gm.line - o.width(.mono, size, num), rows.baseline(y), num, t.faint);
+ };
+ disasm.bareRow(lit, row, y);
+ }
+ line_before = row.line;
+ // A call that comes back: out of the block's side and in again a row down, the callee named.
+ if (std.mem.eql(u8, row.mnemonic(), "call") and !ui.strokes) if (disasm.target(row)) |to| {
+ const ink = if (n.visits > 0) t.dim else theme.color.rule;
+ const back = !(j + 1 == list.items.len and n.end == .noreturn);
+ const hx = n.x + w;
+ if (back) o.route(&.{ .{ hx, y + m.row / 2 }, .{ hx + gm.hook + 6, y + m.row / 2 }, .{ hx + gm.hook + 6, y + 1.5 * m.row }, .{ hx + 1, y + 1.5 * m.row } }, ink, .{ .width = theme.stroke.rule, .head = .{ 7, 8 }, .radius = 6 }) else o.route(&.{ .{ hx, y + m.row / 2 }, .{ hx + gm.hook + 6, y + m.row / 2 } }, ink, .{ .width = theme.stroke.rule, .head = null });
+ const callee = ui.symbol(o, rec.strings.get(to) orelse "");
+ o.text(.mono, size, hx + gm.hook + 6 + adv, rows.baseline(y) + m.row / 2, callee[0 .. std.mem.lastIndexOfScalar(u8, callee, '+') orelse callee.len], t.primary);
+ };
+ y += m.row;
+ }
+ o.clip(body);
+ // How it ends, under it: the way out (a return, a call that does not return), a tail call, an indirect jump.
+ const ink = if (n.visits > 0) t.dim else theme.color.rule;
+ const cx = n.x + n.w / 2;
+ const bottom = n.y + n.h;
+ switch (n.end) {
+ .ret, .noreturn => {
+ o.fill(.{ .x = cx - 1, .y = bottom, .w = theme.stroke.rule, .h = gm.exit_stub }, ink);
+ o.fill(.{ .x = cx - gm.exit_bar / 2, .y = bottom + gm.exit_stub, .w = gm.exit_bar, .h = 3 }, ink);
+ },
+ .tail => {
+ o.fill(.{ .x = cx - 1, .y = bottom, .w = theme.stroke.rule, .h = 24 }, ink);
+ if (n.out) |to| {
+ const callee = ui.symbol(o, rec.strings.get(to) orelse "");
+ o.text(.mono, size, cx + 8, rows.baseline(bottom + 6), callee[0 .. std.mem.lastIndexOfScalar(u8, callee, '+') orelse callee.len], t.primary);
+ }
+ },
+ .indirect => {
+ o.fill(.{ .x = cx - 1, .y = bottom, .w = theme.stroke.rule, .h = 24 }, ink);
+ _ = o.arrow(cx - 8, bottom + 30, .more);
+ o.text(.sans, micro, cx + 60, bottom + 36, "targets not known", t.faint);
+ },
+ else => {},
+ }
+ }
+};
+
+/// The step (`k` steps ago, 0 newest) a row last ran in, of the three the
+/// mark's trail is (by the stops `base` on), and the moment it ran at.
+fn ranAt(now: cursors.Now, base: usize, addr: u64) ?disasm.Disasm.Ran {
+ 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;
+ while (at > first and last - at < 4096) {
+ at -= 1;
+ if (now.rec.register(at, "rip") == addr) return .{ .k = k, .at = at };
+ }
+ }
+ return null;
+}
+
+test "a loop's back edge climbs, the hot path is straight down, a never-taken target sits right of its block" {
+ // sort's shape: entry, the outer loop's test, its body, the inner test,
+ // the compare, the swap, the outer step, the exit.
+ var nodes = [_]Node{
+ .{ .start = 0x00, .len = 0x17, .end = .jump }, // B0 jmp B6
+ .{ .start = 0x17, .len = 0x08, .end = .jump }, // B1 jmp B3
+ .{ .start = 0x1f, .len = 0x40, .end = .fall }, // B2 (swap) falls B3
+ .{ .start = 0x5f, .len = 0x06, .end = .branch }, // B3 jle B5, falls B4
+ .{ .start = 0x65, .len = 0x35, .end = .branch }, // B4 jg B2, falls B5
+ .{ .start = 0x9a, .len = 0x04, .end = .fall }, // B5 falls B6
+ .{ .start = 0x9e, .len = 0x0c, .end = .branch }, // B6 jl B1, falls B7
+ .{ .start = 0xaa, .len = 0x04, .end = .ret }, // B7
+ };
+ for (&nodes) |*nd| {
+ nd.w = 300;
+ nd.h = 52;
+ }
+ var edges = [_]Edge{
+ .{ .from = 0, .to = 6, .kind = .jump, .total = 1 },
+ .{ .from = 1, .to = 3, .kind = .jump, .total = 7 },
+ .{ .from = 2, .to = 3, .kind = .fall, .total = 10 },
+ .{ .from = 3, .to = 5, .kind = .taken, .total = 0 },
+ .{ .from = 3, .to = 4, .kind = .fall, .total = 17 },
+ .{ .from = 4, .to = 2, .kind = .taken, .total = 10 },
+ .{ .from = 4, .to = 5, .kind = .fall, .total = 7 },
+ .{ .from = 5, .to = 6, .kind = .fall, .total = 7 },
+ .{ .from = 6, .to = 1, .kind = .taken, .total = 7 },
+ .{ .from = 6, .to = 7, .kind = .fall, .total = 1 },
+ };
+ var g: Graph = .{ .start = 0, .len = 0xae, .nodes = &nodes, .edges = &edges, .here = null, .text = true, .exact = true };
+ place(&g, metricOf(.map));
+ // Two loops: the inner turns from the swap, the outer from its step.
+ try std.testing.expect(edges[2].back and edges[7].back);
+ for (edges, 0..) |e, k| if (k != 2 and k != 7) try std.testing.expect(!e.back);
+ // Layers by the longest path; the exit last, alone.
+ const want = [_]u32{ 0, 2, 5, 3, 4, 5, 1, 6 };
+ for (nodes, want) |nd, l| try std.testing.expectEqual(l, nd.layer);
+ // The hot path straight down; the step beside the swap.
+ for ([_]usize{ 0, 1, 2, 3, 4, 6, 7 }) |k| try std.testing.expectEqual(@as(u32, 0), nodes[k].col);
+ try std.testing.expectEqual(@as(u32, 1), nodes[5].col);
+ // The outer loop's lane outermost; a back edge climbs into its header's head from the left.
+ try std.testing.expect(edges[7].lane == 0 and edges[2].lane == 1);
+ const turn = edges[7].path();
+ try std.testing.expect(turn[3][1] < turn[2][1] and turn[4][0] < nodes[6].x);
+ // The exit's edge would cross the loop's blocks: it runs in the right channel.
+ try std.testing.expectEqual(@as(u8, 6), edges[9].n);
+ try std.testing.expectEqual(g.channel, edges[9].points[2][0]);
+ // Every forward edge ends on its target's top.
+ for (edges) |e| if (!e.back) try std.testing.expectEqual(nodes[e.to].y, e.points[e.n - 1][1]);
+}
diff --git a/anim/src/map.zig b/anim/src/map.zig
index c870d60..108bafd 100644
--- a/anim/src/map.zig
+++ b/anim/src/map.zig
@@ -96,7 +96,7 @@ pub fn row(panel: Panel, key: u64, u: ui.Ui, r: Rect) void {
/// lens's view, the registers, and in the rest the stack going on
/// (`continued`), in the third; the stack in the fourth, whole.
// shortcut: a column's rest after the disassembly is page: the 5-row dumps round what the registers point at, the open loupes and the key are not built.
-pub fn split(rec: *const Recording, lens: bool, way: [2]u64) Split {
+pub fn split(rec: *const Recording, lens: bool, way: [2]u64, graph: bool) Split {
const st = theme.map.stage;
const c = theme.map.columns;
const w = theme.map.column;
@@ -136,6 +136,25 @@ pub fn split(rec: *const Recording, lens: bool, way: [2]u64) Split {
return .{ .x = r.x, .y = y, .w = r.w, .h = @max(bottom - y, 0) };
}
}.under;
+ // The disassembly as its graph (`graph.Map`): two columns wide beside the
+ // source, the drawing it is (no band); the registers over the stack in the last.
+ if (graph) {
+ const regs: Rect = .{ .x = c[3], .y = st.y, .w = w, .h = panelH(17) };
+ const nothing: Rect = .{ .x = c[3], .y = st.y, .w = w, .h = 0 };
+ const src: Rect = .{ .x = c[0], .y = st.y, .w = w, .h = panelH(@floatFromInt(@min(rec.line_count, theme.layout.rowsIn(foot - st.y - gap - panelH(bt_rows))))) };
+ const bt: Rect = .{ .x = c[0], .y = src.y + src.h + gap, .w = w, .h = @min(panelH(bt_rows), foot - (src.y + src.h + gap)) };
+ return .{
+ .band = .{ .x = c[0], .y = st.y, .w = w, .h = 0 },
+ .view = nothing,
+ .source = src,
+ .disasm = .{ .x = c[1], .y = st.y, .w = c[2] + w - c[1], .h = st.h },
+ .registers = regs,
+ .stack = rest(regs, foot),
+ .backtrace = bt,
+ .dump_rows = theme.map.memory_rows,
+ .spare = .{ rest(bt, foot), .{ .x = c[1], .y = foot, .w = w, .h = 0 } },
+ };
+ }
return .{
.band = band,
.view = view,
@@ -322,6 +341,10 @@ pub const Map = struct {
cursor: ?@import("cursor.zig").Cursor = null,
/// The scene's loupes: open, each is a panel of its own on the map (`Spare`).
loupes: [2]?*const @import("loupe.zig").Loupe = .{ null, null },
+ /// The disassembly is its graph (`graph.Map`): the thread is its digits,
+ /// no line crosses the lanes; the band's window is the whole graph; the
+ /// disassembly's, the rows it shows.
+ graph: bool = false,
const Part = struct { ctx: *const anyopaque, draw: *const fn (*const anyopaque, *sk.Frame, anim.Placed) void };
@@ -340,7 +363,7 @@ pub const Map = struct {
/// Puts a drawing on the map, in a band of its own over the source,
/// for a scene whose working frame has none (its map is split anew).
pub fn drawing(m: *Map, s: *anim.Frame, rec: *const Recording, component: anytype) void {
- m.stage.first = split(rec, true, if (m.cursor) |c| .{ c.first, c.last } else .{ 0, 0 });
+ m.stage.first = split(rec, true, if (m.cursor) |c| .{ c.first, c.last } else .{ 0, 0 }, m.graph);
m.add(s, component);
}
@@ -371,7 +394,8 @@ pub const Map = struct {
/// `surface`: the dive backwards, a little quicker; then the map is
/// still before anything plays on it.
pub fn surface(m: *const Map, s: *anim.Frame) void {
- s.play(.{m.t.to(diving.land)}, .{ .seconds = 0 });
+ // A scene that did not open on it has not shown it yet: under the panels until it rises.
+ s.play(.{ m.t.to(diving.land), m.obj.fadeIn() }, .{ .seconds = 0 });
s.play(.{m.t.to(0)}, .{ .seconds = @as(f32, diving.surface) / 60.0, .ease = anim.ease.linear });
s.wait(@as(f32, diving.still) / 60.0);
}
@@ -414,7 +438,9 @@ pub const Map = struct {
const y = std.math.lerp(from.y, box.y, p);
// The tags, rules and lanes come in as the map lands.
const chrome = std.math.clamp((t - diving.lift) / 12, 0, 1);
- context.zoomed[i] = .{ .box = box, .to = .{ .x = x, .y = y, .w = box.w * scale, .h = box.h * scale }, .chrome = chrome };
+ // Out of a graph the rows and the lens are another form of what the map shows there: they come in on their way.
+ const other = m.graph and (i == @intFromEnum(Panel.disasm) or i == @intFromEnum(Panel.band));
+ context.zoomed[i] = .{ .box = box, .to = .{ .x = x, .y = y, .w = box.w * scale, .h = box.h * scale }, .chrome = chrome, .shows = if (other) p else 1 };
};
}
}
@@ -505,7 +531,7 @@ pub const Map = struct {
ui.strokes = mini;
counts = @splat(0);
for (m.parts[0..m.n]) |p| p.draw(p.ctx, f, at);
- if (m.cursor) |c| m.thread(f, at, c.now(at), mini);
+ if (m.cursor) |c| if (!m.graph) m.thread(f, at, c.now(at), mini);
ui.mapping = false;
ui.strokes = false;
}
@@ -559,13 +585,17 @@ pub const Map = struct {
/// The top and bottom of the map's rows of panel `i` with keys from `keys[0]` to `keys[1]`, in canvas pixels.
fn spanOf(i: usize, keys: [2]u64) ?[2]f32 {
- var top: f32 = std.math.inf(f32);
- var bottom: f32 = -std.math.inf(f32);
+ const r = boxOf(i, keys) orelse return null;
+ return .{ r[1], r[3] };
+ }
+
+ /// The box round the map's rows of panel `i` with keys from `keys[0]` to `keys[1]`, in canvas pixels.
+ fn boxOf(i: usize, keys: [2]u64) ?[4]f32 {
+ var r: [4]f32 = .{ std.math.inf(f32), std.math.inf(f32), -std.math.inf(f32), -std.math.inf(f32) };
for (rows[i][0..counts[i]]) |rw| if (rw.key >= keys[0] and rw.key <= keys[1]) {
- top = @min(top, rw.box[1]);
- bottom = @max(bottom, rw.box[3]);
+ r = .{ @min(r[0], rw.box[0]), @min(r[1], rw.box[1]), @max(r[2], rw.box[2]), @max(r[3], rw.box[3]) };
};
- return if (top < bottom) .{ top, bottom } else null;
+ return if (r[1] < r[3]) r else null;
}
/// Where the map's row of key `key` in `panel` is this frame, in canvas pixels.
@@ -585,7 +615,8 @@ pub const Map = struct {
inline for (.{ "band", "view", "source", "disasm", "registers", "stack", "backtrace" }) |name| {
const panel = @field(Panel, name);
const i = @intFromEnum(panel);
- const box = @field(split_, name);
+ // The lens drawing the graph is: its window is the graph whole.
+ const box = if (m.graph and panel == .band) split_.disasm else @field(split_, name);
if (box.h > 0) {
const a = at.xf.apply(.{ box.x, box.y });
const b = at.xf.apply(.{ box.x + box.w, box.y + box.h });
@@ -606,6 +637,12 @@ pub const Map = struct {
r[1] = std.math.lerp(snap(from[0], mini), snap(to[0], mini), win.p);
r[3] = std.math.lerp(snap(from[1], mini), snap(to[1], mini), win.p);
shown = true;
+ // In a graph the rows a window shows are in blocks: round them, not the panel's width.
+ if (m.graph and panel == .disasm) if (boxOf(i, win.now)) |round| {
+ const was_box = boxOf(i, win.was) orelse round;
+ r[0] = std.math.lerp(snap(was_box[0], mini), snap(round[0], mini), win.p);
+ r[2] = std.math.lerp(snap(was_box[2], mini), snap(round[2], mini), win.p);
+ };
}
}
// On the minimap on whole pixels (a window's ends already are, at rest).
diff --git a/anim/src/root.zig b/anim/src/root.zig
index 60ae6dc..b7be9b4 100644
--- a/anim/src/root.zig
+++ b/anim/src/root.zig
@@ -42,6 +42,7 @@ pub const contextLegend = contexts.legend;
pub const disasm = @import("disasm.zig");
pub const Disasm = disasm.Disasm;
pub const Descent = @import("descent.zig").Descent;
+pub const graph = @import("graph.zig");
pub const calls = @import("calls.zig");
pub const CallTree = calls.Tree;
pub const Nodes = @import("nodes.zig").Nodes;
@@ -246,6 +247,10 @@ pub const Context = struct {
/// callee's window under the call row, and a system call the
/// kernel's under the `syscall` (step with `descent.step`).
descent: bool = false,
+ /// On the map the disassembly as the function's graph (`graph.Map`),
+ /// two columns wide beside the source, the registers and the stack
+ /// in the last column.
+ graph: bool = false,
};
/// The ties between the panes, drawn over everything added before.
@@ -284,11 +289,11 @@ pub fn context(s: *anim.Frame, cursor_: Cursor, options: Context.Options) Contex
var at = cursor_;
at.levels.crossings = options.syscalls;
// The map first: while it fills the frame or falls to the minimap it is under the panels diving out of it.
- const whole = s.new(map.Map{ .stage = Stage.init(s, map.split(at.rec, options.split.band.h > 0, .{ at.first, at.last })), .frame = stage, .t = s.value(map.diving.land + map.diving.still), .cursor = at });
+ const whole = s.new(map.Map{ .stage = Stage.init(s, map.split(at.rec, options.split.band.h > 0, .{ at.first, at.last }, options.graph)), .frame = stage, .t = s.value(map.diving.land + map.diving.still), .cursor = at, .graph = options.graph });
whole.obj = s.custom(whole, .{ .x = -640, .y = -360, .scale = ui.scale });
whole.add(s, Source{ .cursor = at, .stage = whole.stage, .descent = options.descent });
whole.add(s, Source{ .cursor = at, .stage = whole.stage, .descent = options.descent, .part = .source_more });
- if (options.descent) whole.add(s, Descent{ .cursor = at, .stage = whole.stage }) else whole.add(s, Disasm{ .cursor = at, .stage = whole.stage });
+ if (options.graph) whole.add(s, graph.Map{ .cursor = at, .stage = whole.stage }) else if (options.descent) whole.add(s, Descent{ .cursor = at, .stage = whole.stage }) else whole.add(s, Disasm{ .cursor = at, .stage = whole.stage });
whole.add(s, Registers{ .cursor = at, .stage = whole.stage, .syscalls = options.syscalls, .quiet = options.quiet });
whole.add(s, Stack{ .cursor = at, .stage = whole.stage, .above = theme.map.stack_above });
// The stack goes on in the rest of the memory's column, from where its own column ends.
@@ -367,6 +372,7 @@ test {
_ = contexts;
_ = @import("disasm.zig");
_ = @import("descent.zig");
+ _ = graph;
_ = tying;
_ = @import("hexdump.zig");
_ = calls;
diff --git a/anim/src/theme.zig b/anim/src/theme.zig
index cb52287..7d505d7 100644
--- a/anim/src/theme.zig
+++ b/anim/src/theme.zig
@@ -134,6 +134,17 @@ pub const layout = struct {
/// its view 200 (a dump of seven); ten lines over nine
/// instructions; the registers over six words, a small frame whole.
pub const subject: Split = shared(296, 200, 10, 9, 6, 7);
+ /// The `blocks` lens: its drawing (the graph as shapes) 296 high
+ /// over the program column, which then holds the source alone
+ /// (twenty lines); its view the disassembly itself, seven rows over
+ /// the state column; the registers over six words.
+ pub const blocks: Split = blk: {
+ var s = subject;
+ s.disasm = .{ .x = state.x, .y = canvas.stage.y, .w = state.w, .h = panelH(7) };
+ s.view = .{ .x = state.x, .y = canvas.stage.y, .w = state.w, .h = 0 };
+ s.source.h = panelH(20);
+ break :blk s;
+ };
fn shared(drawing_h: f32, view_h: f32, source_rows: f32, disasm_rows: f32, slots: f32, dump_rows: u32) Split {
const band: Rect = .{ .x = program.x, .y = canvas.stage.y, .w = program.w, .h = drawing_h };
@@ -675,6 +686,66 @@ pub const metric = struct {
pub const bottom_lane = 8;
};
+ /// `graph` (design/README.md, "graph"): on the map a block is a head of
+ /// 24 and its rows: the offset ranged right at 72, the line at 116, the
+ /// mnemonic at 128, the operands at 212; 300 wide at least; over 16
+ /// rows, where the graph is too tall, its first four, `more` and its
+ /// last four. Layers 48 apart, blocks 56; lanes 16 apart from 24 in, the
+ /// blocks 24 right of the last; the first layer 36 under the tag; a back
+ /// edge down 14; ports 40 in; arrivals 48 apart; the right channel 32
+ /// out; corners 8; the way out's stub 14 and bar 28; a call's hook 14
+ /// out and back a row down. A fold of blocks never reached: 520 wide.
+ /// Columns are as far apart as the pc's block is wide with its annotation.
+ /// The `blocks` lens at half measure: boxes 24 high, 60 and 10 a row
+ /// wide, 132 at least, columns 300 apart; layers 12 apart, lanes 10.
+ pub const graph = struct {
+ pub const offset = 72;
+ pub const line = 116;
+ pub const mnemonic = 128;
+ pub const operands = 212;
+ pub const least = 300;
+ pub const long = 16;
+ pub const keep = 4;
+ pub const layer_gap = 48;
+ pub const block_gap = 56;
+ pub const lane = 16;
+ pub const lane_in = 24;
+ pub const top = 36;
+ pub const back_down = 14;
+ pub const port = 40;
+ pub const arrivals = 48;
+ pub const channel = 32;
+ pub const corner = 8;
+ pub const exit_stub = 14;
+ pub const exit_bar = 28;
+ pub const hook = 14;
+ pub const fold_w = 520;
+ pub const head = [2]f32{ 10, 10 };
+ /// The rows are `insn`'s, `shift` left (its address column given to
+ /// the offset); the pc's block widens to its column for the
+ /// annotation, 30 characters from where `insn` puts it.
+ pub const shift = 52;
+ pub const note = 360;
+ pub const lens = struct {
+ pub const box = 24;
+ pub const lead = 60;
+ pub const per_row = 10;
+ pub const least = 132;
+ pub const pitch = 300;
+ pub const layer_gap = 12;
+ pub const lane = 10;
+ pub const blocks_in = 76;
+ pub const top = 14;
+ pub const port = 16;
+ pub const channel = 20;
+ pub const back_down = 8;
+ pub const corner = 4;
+ pub const head = [2]f32{ 7, 8 };
+ /// The pc's box: framed gold this wide.
+ pub const gold = 3;
+ };
+ };
+
/// The ties' channels: the thread in the left margin, the gutter's two
/// lanes (a link comes up the left, a projection down the right).
pub const tie = struct {
@@ -932,6 +1003,25 @@ pub const time = struct {
pub const out = 0.20;
pub const names = 0.20;
};
+ /// `leave` (and `turn`, along a loop's lane): the jump fires; the
+ /// compare's chips framed as read from `chips` (the CPU's slot); the
+ /// edge it takes drawn gold over `edge`, ending as the mark sets off at
+ /// `sets_off`; the mark along it `mark` and `per_100` a 100 units,
+ /// `mark_most` at most (`run` at a run); the target's count struck
+ /// `count` after it lands; the oldest trail edge cooled over `trail`;
+ /// `rest` from the landing to the step's end.
+ pub const leave = struct {
+ pub const chips = 0.30;
+ pub const edge = 0.30;
+ pub const sets_off = 0.70;
+ pub const mark = 0.20;
+ pub const per_100 = 2.0 / 60.0;
+ pub const mark_most = 0.50;
+ pub const count = 0.10;
+ pub const trail = 0.30;
+ pub const rest = 0.70;
+ pub const run = 8.0 / 60.0;
+ };
/// A return in the calls tree: its frame leaves the backtrace as the
/// idea strikes, and the result on the node's shoulder this long after.
pub const badge_after = 0.20;
diff --git a/anim/src/ties.zig b/anim/src/ties.zig
index 58ada36..36dc635 100644
--- a/anim/src/ties.zig
+++ b/anim/src/ties.zig
@@ -197,7 +197,9 @@ pub const Ties = struct {
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);
+ // No thread where the disassembly is not under the source (the `blocks` lens has it over the state column).
+ const apart = @abs(split.disasm.x - split.source.x) / (theme.layout.state.x - theme.layout.program.x);
+ if (g.threaded and apart < 1) thread(raw.faded(@import("context.zig").thread_shows * (1 - apart)), at, k);
if (g.beat) |b| named(raw, at, now, k, split, b);
if (!now.walking()) return;
if (!g.quiet) g.link(raw, at, now, k, split);
diff --git a/anim/test/gallery.script b/anim/test/gallery.script
index 993ff7e..461f100 100644
--- a/anim/test/gallery.script
+++ b/anim/test/gallery.script
@@ -31,3 +31,11 @@ console set t 19.8
screenshot {dir}/loupe.png
frames 1
expect-same-screenshot {dir}/loupe.png test/gallery/loupe.png 24 0 0 1920 1040
+console set t 21.8
+screenshot {dir}/graph.png
+frames 1
+expect-same-screenshot {dir}/graph.png test/gallery/graph.png 24 0 0 1920 1040
+console set t 25.5
+screenshot {dir}/graph-map.png
+frames 1
+expect-same-screenshot {dir}/graph-map.png test/gallery/graph-map.png 24 0 0 1920 1040
diff --git a/anim/test/gallery/atoms.png b/anim/test/gallery/atoms.png
index 6e64609..8da5c01 100644
--- a/anim/test/gallery/atoms.png
+++ b/anim/test/gallery/atoms.png
Binary files differ
diff --git a/anim/test/gallery/graph-map.png b/anim/test/gallery/graph-map.png
new file mode 100644
index 0000000..b16caf6
--- /dev/null
+++ b/anim/test/gallery/graph-map.png
Binary files differ
diff --git a/anim/test/gallery/graph.png b/anim/test/gallery/graph.png
new file mode 100644
index 0000000..ea9695e
--- /dev/null
+++ b/anim/test/gallery/graph.png
Binary files differ
diff --git a/anim/test/gallery/hello.ztrace b/anim/test/gallery/hello.ztrace
new file mode 100644
index 0000000..2ea1f5c
--- /dev/null
+++ b/anim/test/gallery/hello.ztrace
Binary files differ
diff --git a/anim/test/gallery/loupe.png b/anim/test/gallery/loupe.png
index 346bd44..7f07d94 100644
--- a/anim/test/gallery/loupe.png
+++ b/anim/test/gallery/loupe.png
Binary files differ
diff --git a/anim/test/gallery/none.png b/anim/test/gallery/none.png
index 39f0206..bac000d 100644
--- a/anim/test/gallery/none.png
+++ b/anim/test/gallery/none.png
Binary files differ
diff --git a/anim/test/gallery/rest.png b/anim/test/gallery/rest.png
index 9dfdbe2..c368a30 100644
--- a/anim/test/gallery/rest.png
+++ b/anim/test/gallery/rest.png
Binary files differ
diff --git a/anim/test/gallery/rows.png b/anim/test/gallery/rows.png
index 7e6ba84..dd85690 100644
--- a/anim/test/gallery/rows.png
+++ b/anim/test/gallery/rows.png
Binary files differ
diff --git a/anim/test/gallery/sort.ztrace b/anim/test/gallery/sort.ztrace
new file mode 100644
index 0000000..a7aced1
--- /dev/null
+++ b/anim/test/gallery/sort.ztrace
Binary files differ
diff --git a/anim/test/gallery/step.png b/anim/test/gallery/step.png
index b85cba0..19ffc00 100644
--- a/anim/test/gallery/step.png
+++ b/anim/test/gallery/step.png
Binary files differ
diff --git a/anim/test/gallery/subject.png b/anim/test/gallery/subject.png
index d912474..182f8ba 100644
--- a/anim/test/gallery/subject.png
+++ b/anim/test/gallery/subject.png
Binary files differ
diff --git a/anim/test/recordings.zig b/anim/test/recordings.zig
index 38798af..ce5a9db 100644
--- a/anim/test/recordings.zig
+++ b/anim/test/recordings.zig
@@ -105,3 +105,108 @@ test "a walked step is as long as what it changed: fire, a slot a level, the hol
const line = quiet.reading(rec.lineMoment(4, from), rec.lineMoment(6, from));
try std.testing.expectApproxEqAbs(t.fire + t.step + t.rest + t.step_per_row, line.total, 1e-3);
}
+
+/// The first moment whose pc is in the function named `name` (as the recording names it: `libc.so.6!open`).
+fn firstIn(rec: *const Recording, name: []const u8) u64 {
+ for (rec.registers, 0..) |regs, i| {
+ const at = rec.strings.get(regs.rip) orelse continue;
+ if (std.mem.startsWith(u8, at, name) and at.len > name.len and at[name.len] == '+') return i;
+ }
+ unreachable;
+}
+
+test "sort's graph: two loops in their lanes, the hot path straight down, the jle never taken" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const graph = canim.graph;
+ const rec = canim.open(recordings.sort);
+ const at = firstIn(rec, "sort");
+ var g = graph.read(arena.allocator(), rec, rec.end(), rec.registers[at].rip).?;
+ try expect(g.exact and g.text);
+ try expectEqual(@as(usize, 11), g.nodes.len);
+ for (g.nodes) |n| try expect(n.reached);
+ // The inner loop's `j > 0` is never false: its jle never taken.
+ const jle = g.edgeOf(g.nodeOf(g.start + 0x8a).?, g.nodeOf(g.start + 0xbc).?).?;
+ try expect(g.edges[jle].kind == .taken and g.edges[jle].total == 0);
+ // The compare's jne to the swap, ten times; the swap entered as often.
+ const swap = g.nodeOf(g.start + 0xc5).?;
+ try expectEqual(@as(u32, 10), g.edges[g.edgeOf(g.nodeOf(g.start + 0xbc).?, swap).?].count);
+ try expectEqual(@as(u32, 10), g.nodes[swap].visits);
+ graph.sizes(rec, rec.end(), &g, .map);
+ graph.place(&g, graph.metricOf(.map));
+ var backs: usize = 0;
+ for (g.edges) |e| backs += @intFromBool(e.back);
+ try expectEqual(@as(usize, 2), backs);
+ // The outer loop (i++ back to the test) outermost, the inner (the swap back to `j > 0`) inside it.
+ const outer = g.edges[g.edgeOf(g.nodeOf(g.start + 0xff).?, g.nodeOf(g.start + 0x78).?).?];
+ const inner = g.edges[g.edgeOf(swap, g.nodeOf(g.start + 0x8a).?).?];
+ try expect(outer.back and inner.back and outer.lane == 0 and inner.lane == 1);
+ // The swap straight under the compare that goes to it ten times in ten; the way on beside it.
+ try expectEqual(g.nodes[g.nodeOf(g.start + 0xbc).?].col, g.nodes[swap].col);
+ try expect(g.nodes[g.nodeOf(g.start + 0xc3).?].col > g.nodes[swap].col);
+ // The jle and the loop's exit would cross blocks: the right channel.
+ try expectEqual(@as(u8, 6), g.edges[jle].n);
+ try expectEqual(g.channel, g.edges[jle].points[2][0]);
+ // On the map (1688 high): too tall; the long prologue folds, and the outer loop is panned into view whole.
+ var buf: [1 << 20]u8 = undefined;
+ var fba = std.heap.FixedBufferAllocator.init(&buf);
+ const room = 1688 - 24;
+ const in_loop = rec.lineMoment(12, at);
+ const laid = graph.layout(fba.allocator(), rec, in_loop, rec.registers[in_loop].rip, .map, room).?;
+ try expectEqual(@as(u32, 9), laid.nodes[0].shown);
+ const off = graph.pan(laid, room, graph.metricOf(.map));
+ for (laid.nodes[1 .. laid.nodes.len - 1]) |n| try expect(n.y >= off and n.y + n.h <= off + room);
+}
+
+test "reverse's graph: one loop, its exit in the right channel" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const graph = canim.graph;
+ const rec = canim.open(recordings.list);
+ const at = firstIn(rec, "reverse");
+ var g = graph.read(arena.allocator(), rec, rec.end(), rec.registers[at].rip).?;
+ try expectEqual(@as(usize, 4), g.nodes.len);
+ graph.sizes(rec, rec.end(), &g, .map);
+ graph.place(&g, graph.metricOf(.map));
+ const layers = [_]u32{ 0, 1, 2, 3 };
+ for (g.nodes, layers) |n, l| try expect(n.layer == l and n.col == 0);
+ // The body's jmp back to the test: the one edge that climbs, three turns.
+ const turn = g.edges[g.edgeOf(2, 1).?];
+ try expect(turn.back and turn.count == 3);
+ try expectEqual(@as(u8, 6), g.edges[g.edgeOf(1, 3).?].n);
+ // The recording begins inside it: its first block was entered once.
+ try expectEqual(@as(u32, 1), g.nodes[0].visits);
+ try expect(g.h <= 1688 - 24);
+}
+
+test "libc's open at its syscall: the blocks never reached folded, no loop made by folding" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ const graph = canim.graph;
+ const rec = canim.open(recordings.hello);
+ var at = firstIn(rec, "libc.so.6!open");
+ var buf: [256]u8 = undefined;
+ while (!std.mem.eql(u8, rec.instruction(at, rec.registers[at].rip, &buf).?.text, "syscall")) at += 1;
+ const g = graph.layout(arena.allocator(), rec, at, rec.registers[at].rip, .map, 1688 - 24).?;
+ // Twenty blocks (two of `nop` padding left out), eight of them reached.
+ try expectEqual(@as(usize, 20), g.blocks);
+ var reached: usize = 0;
+ var folds: usize = 0;
+ for (g.nodes) |n| {
+ reached += @intFromBool(n.reached);
+ folds += @intFromBool(n.folded > 0);
+ }
+ try expectEqual(@as(usize, 8), reached);
+ try expectEqual(@as(usize, 12), g.foldedBlocks());
+ try expect(folds >= 4 and folds < 12);
+ // open has no loop, and the folds make none.
+ for (g.edges) |e| try expect(!e.back);
+ // At its syscall: the blocks before entered once, those after not yet.
+ try expectEqual(g.nodeOf(rec.registers[at].rip).?, g.here.?);
+ for (g.nodes) |n| if (n.reached) try expectEqual(@as(u32, @intFromBool(n.start <= rec.registers[at].rip)), n.visits);
+ // Still taller than the map: it pans, the pc's block in view; as wide as two columns at most.
+ const room = 1688 - 24;
+ const off = graph.pan(g, room, graph.metricOf(.map));
+ const here = g.nodes[g.here.?];
+ try expect(here.y >= off and here.y + here.h <= off + room and g.w <= 1792);
+}
diff --git a/anim/tools/chart.py b/anim/tools/chart.py
index 1384800..7cc0925 100644
--- a/anim/tools/chart.py
+++ b/anim/tools/chart.py
@@ -45,9 +45,13 @@ def split(drawing, view, source, disasm, slots, backtrace=0):
# and the run's step counter (912 to 934).
words = {"clock": (0, 888, 1656, 46), "caption": (0, 940, 1464, 140), "minimap": (1664, 912, 192, 108)}
none, beside, subject = split(0, 0, 14, 17, 8, 4), split(152, 152, 8, 17, 8), split(296, 200, 10, 9, 6)
+# The `blocks` lens: the graph over the program column, the source alone under it; the disassembly
+# (its view) over the state column, seven rows, the registers and six words under it.
+blocks = {**subject, "view": (976, 48, 880, ph(7)), "source": (64, 356, 880, ph(20))}
+del blocks["disasm"]
motion.REGIONS.update({
"fib": {**subject, **words},
- "sort": {**subject, **words},
+ "sort": {**blocks, **words},
# The band is the calls so far; the disassembly a descent.
"syscalls": {**beside, **words},
"heap": {**subject, **words},
@@ -57,9 +61,9 @@ motion.REGIONS.update({
"zoom": {**none, **words},
"gallery": {**beside, **words},
})
-# `cut`: the gallery is the catalogue's pages, cut from one to the next (scenes/gallery.zig: 3, 6, 9.6, 13, 16 and
-# 19.8 s), not a scene: a page's first frame changes everything at once by design.
-PAGES = {180, 360, 576, 780, 960, 1188}
+# `cut`: the gallery is the catalogue's pages, cut from one to the next (scenes/gallery.zig: 3, 6, 9.6, 13, 16,
+# 19.8, 21 and 24 s), not a scene: a page's first frame changes everything at once by design.
+PAGES = {180, 360, 576, 780, 960, 1188, 1260, 1440}
motion.EXEMPT["cut"] = lambda name, t, moved: lint.scene == "gallery" and any(abs(t - p) <= 1 for p in PAGES)
_regions_for = motion.regions_for
scene = lambda video: os.path.basename(video).split("-")[0].split(".")[0]