1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
//! One line of C, all the way down: the instructions it is, the registers
//! they change, the words they write on the stack. Every level is on
//! screen the whole time; the scene steps an instruction at a time and
//! says which one to look at.
//!
//! How it moves (design/motion.md): a step is an instruction: the pc's
//! row fires in the disassembly, the registers it changed strike, then
//! the stack, and the mark walks down the listing. The camera goes
//! nearer once, announced (its target framed, the rest cleared first),
//! for the call: the instructions and the stack's words in one view, the
//! eight bytes of the way back and the frame built under them; then back,
//! and the line is where it was, one call deeper.
const anim = @import("anim");
const canim = @import("canim");
const recordings = @import("recordings");
pub const background = canim.background;
pub const scene = anim.Live(frame, struct {});
fn frame(s: *anim.Frame) void {
const rec = canim.open(recordings.steps);
// fib(5), about to run line 6: `return fib(n-1) + fib(n-2);`. Its first call is fib(4).
const call = rec.call("fib", 1);
var at = canim.cursor(s, rec, rec.lineMoment(6, rec.call("fib", 0).enter));
const end = rec.lineMoment(6, call.enter);
at.journey(.moment, end);
// A step is an instruction: it fires on the pc's row.
at.levels = .{ .fire = .disasm, .source_rows = 14 };
const split = canim.layout.split.none;
const ctx = canim.context(s, at, .{ .split = split });
// The map's drawing: the calls of fib so far, as the fib scene draws them.
ctx.map.drawing(s, rec, canim.CallTree{ .cursor = at, .stage = ctx.map.stage, .function = "fib" });
var cam: canim.Camera = .{};
var says: canim.Narrator = .{};
const joins = ctx.ties(s);
canim.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.backtrace, ctx.source, ctx.disasm, joins });
says.line(s, "A line of C is a handful of instructions.\nThis one calls `fib(n-1)`: follow it down.");
// The CPU's level: an instruction a step. The argument is worked out in `rdi`.
at.walk(s, .moment, call.enter - 1);
says.line(s, "`call` writes eight bytes on the stack: the way back.");
// The stack holds still under the camera, set before it moves: six free
// words above rsp for the new frame (rsp-30 to rsp+8).
ctx.stage.holdStack(s, at.moment(s), 6);
// Nearer, cause and effect in one view: the instructions as they run
// (mnemonics, operands, what each leaves) and the stack's words they
// write with their chains and roles (`fib+42`, `saved rbp`, `n`): from
// the mnemonics to the roles' end is all the stage holds at x1.16 (at
// x1.5 the stack's names and roles are off it); the rest cleared.
cam.go(s, .{ .x = 190, .y = 342, .w = 1525, .h = 480 }, 1.16, .{ ctx.source, ctx.registers, ctx.backtrace });
at.walk(s, .moment, call.enter);
// The frame built under them, under a caption of its own (12 s at most).
says.line(s, "The new frame saves `rbp` under them,\nthen stores its own `n`.");
at.walk(s, .moment, call.enter + 4);
s.wait(canim.theme.time.beat);
ctx.stage.releaseStack(s);
cam.back(s, .{ ctx.source, ctx.registers, ctx.backtrace });
says.line(s, "Nine instructions and three words of stack later\n`fib(4)` begins: the same lines, one call deeper.");
at.walk(s, .line, end);
says.close(s);
cam.apply(s);
}
|