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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
//! The introduction, chapter II: the stack (design/motion.md, "A scene: the
//! introduction", II1 to II4; mock 31). It opens on chapter I's last
//! picture, at the same moment of the `list` recording (reverse's first
//! call, line 17): the address space and the dump's link fade, the words
//! glide into the stack panel's rows where it stands in the working frame
//! and become it; a push walked from the recording (that call's: the
//! address to come back to, rsp's pin climbing onto it); the return
//! address main left, and the offsets; rbp's pin and the panel's tag last;
//! then the registers built over it a row at a time, their tag last. The
//! text stands beside on the left, in the program column, empty until
//! chapter III.
//!
//! What a beat has not reached yet is under the page (`Cover`), lifted as
//! the beat says it: the context's panels draw every column at once.
const std = @import("std");
const anim = @import("anim");
const sk = @import("sketch");
const canim = @import("canim");
const recordings = @import("recordings");
pub const background = canim.background;
pub const scene = anim.Live(frame, struct {});
const t = canim.theme.time;
const row = canim.theme.metric.context.row;
const tag = canim.theme.metric.context.tag;
const pad = canim.theme.metric.context.pad;
fn f(frames: f32) f32 {
return frames / 60;
}
fn place(s: *anim.Frame, data: anytype) anim.Obj {
return s.custom(data, .{ .x = -640, .y = -360, .scale = canim.ui.scale });
}
/// The page over a part of a panel the beat has not reached: lifted (its fade out) as the beat says it.
const Cover = struct {
box: canim.Rect,
pub fn draw(c: *const Cover, fr: *sk.Frame, placed: anim.Placed) void {
(canim.ui.Ui{ .f = fr, .at = placed }).fill(c.box, canim.theme.color.page);
}
};
/// A value framed as read (6 f in with its object's fade).
const Read = struct {
box: canim.Rect,
pub fn draw(r: *const Read, fr: *sk.Frame, placed: anim.Placed) void {
(canim.ui.Ui{ .f = fr, .at = placed }).read(r.box, 1);
}
};
fn cover(s: *anim.Frame, box: canim.Rect) anim.Obj {
const o = place(s, s.new(Cover{ .box = box }));
s.play(.{o.fadeIn()}, .{ .seconds = 0 });
return o;
}
/// A panel's tag and rules under the page, for `frame up`.
fn chrome(s: *anim.Frame, b: canim.Rect, strip: bool) [5]?anim.Obj {
const w = canim.theme.stroke.rule;
return .{
cover(s, .{ .x = b.x, .y = b.y, .w = b.w, .h = tag }),
cover(s, .{ .x = b.x, .y = b.y + tag, .w = w, .h = b.h - tag }),
cover(s, .{ .x = b.x + b.w - w, .y = b.y + tag, .w = w, .h = b.h - tag }),
cover(s, .{ .x = b.x, .y = b.y + b.h - w, .w = b.w, .h = w }),
if (strip) cover(s, .{ .x = b.x + b.w - canim.theme.strip.cost, .y = b.y + tag, .w = canim.theme.strip.cost - w, .h = b.h - tag - w }) else null,
};
}
/// Lifts `covers` one after another, `apart` frames apart, `each` frames each, from the cursor; the cursor after the last.
fn lift(s: *anim.Frame, covers: []const ?anim.Obj, apart: f32, each: f32) void {
const at = s.cursor;
var end = at;
for (covers, 0..) |o, i| if (o) |obj| {
s.cursor = at + f(apart * @as(f32, @floatFromInt(i)));
s.play(.{obj.fadeOut()}, .{ .seconds = f(each), .ease = anim.ease.linear });
end = s.cursor;
};
s.cursor = end;
}
fn on(rec: *canim.Recording, from: u64, text: []const u8) u64 {
var buf: [256]u8 = undefined;
var i = from;
while (i < rec.end()) : (i += 1) if (rec.instruction(i, rec.register(i, "rip"), &buf)) |here| if (std.mem.indexOf(u8, here.text, text) != null) return i;
return from;
}
/// A value at once, from the cursor.
fn set(s: *anim.Frame, v: anim.Value, to: f32) void {
s.play(.{v.to(to)}, .{ .seconds = 0 });
}
fn frame(s: *anim.Frame) void {
const rec = canim.open(recordings.list);
const reverse = rec.call("reverse", 0);
// Chapter I's moment: reverse's first call, whose push is walked here.
const moment = on(rec, reverse.enter, "call");
var at = canim.cursor(s, rec, moment);
at.journey(.moment, moment + 1);
at.levels = .{ .fire = .disasm, .source_rows = 14 };
// The working frame with no backtrace: the stack goes to the stage's foot under the registers.
var split = canim.layout.split.none;
split.backtrace.h = 0;
const ctx = canim.context(s, at, .{ .split = split });
const sb = ctx.stage.first.stack;
const rb = ctx.stage.first.registers;
const rsp = rec.register(moment, "rsp");
const rsp_after = rec.register(moment + 1, "rsp");
// The window has two `free` words over rsp; rows from the panel's tag and padding.
const first = rsp - 16;
const top = sb.y + tag + pad;
const slots = canim.layout.rowsIn(sb.h);
const rowY = struct {
fn y(t0: f32, from: u64, addr: u64) f32 {
return t0 + row * @as(f32, @floatFromInt((addr - from) / 8));
}
}.y;
// What has not been said yet, under the page: the stack's offsets, the
// pins' lane under rsp's row, its tag, rules and strip; the registers' rows, tag and rules.
var offsets: [32]?anim.Obj = @splat(null);
var lane: [32]?anim.Obj = @splat(null);
const rsp_row = (rsp - first) / 8;
for (0..@min(slots, 32)) |k| {
const y = top + row * @as(f32, @floatFromInt(k));
offsets[k] = cover(s, .{ .x = sb.x + 92, .y = y, .w = 84, .h = row });
if (k > rsp_row) lane[k] = cover(s, .{ .x = sb.x + 2, .y = y, .w = 88, .h = row });
}
const stack_chrome = chrome(s, sb, true);
var regs_rows: [16]?anim.Obj = @splat(null);
for (0..@min(canim.layout.rowsIn(rb.h), 16)) |k|
regs_rows[k] = cover(s, .{ .x = rb.x + 2, .y = rb.y + tag + pad + row * @as(f32, @floatFromInt(k)), .w = rb.w - 4, .h = row });
const regs_chrome = chrome(s, rb, false);
// Chapter I's last picture: the address space, the words with their chains, the link standing.
const space = s.new(canim.Segments.init(s, rec, moment));
const dump = s.new(canim.Words.init(s, rec, moment));
dump.panel = .{ .x = sb.x, .top = top, .first = first };
const at_ = canim.regroup.at;
const tie = s.new(canim.segments.Place{ .segments = space, .addr = rsp, .x = at_.bracket, .y0 = at_.top + 2, .y1 = at_.top + 16 * 24 - 2, .clock = s.value(60), .quiet = s.value(24) });
for ([_]anim.Value{ space.form, dump.placed }) |v| set(s, v, 1);
set(s, space.whole, 999);
for (space.rides) |v| set(s, v, 999);
for ([_]anim.Value{ dump.in, dump.ink, dump.chains }) |v| set(s, v, 999);
set(s, dump.regroup, 386);
set(s, dump.left, 24);
const objs = .{ place(s, space), place(s, dump), place(s, tie) };
inline for (objs) |o| s.play(.{o.fadeIn()}, .{ .seconds = 0 });
const main_word: canim.Rect = .{ .x = sb.x + canim.theme.metric.slot.address + 12 * 12 + canim.theme.metric.context.arrow_w, .y = rowY(top, rsp_after - 16, rsp + 8 * dump.walked), .w = 6 * 12, .h = row };
const framed = place(s, s.new(Read{ .box = main_word }));
var asides: canim.Asides = .{};
const left = canim.theme.canvas.margin_x;
// II1: the address space and the link fade (18 f); the words glide into
// the stack's rows (36 f) and become the panel (12 f); then the push.
s.wait(t.beat);
s.play(.{ objs[0].fadeOut(), objs[2].fadeOut() }, .{ .seconds = f(18), .ease = anim.ease.linear });
s.play(.{dump.become.to(1)}, .{ .seconds = f(36), .ease = anim.ease.linear });
s.play(.{ ctx.stack.fadeIn(), objs[1].fadeOut() }, .{ .seconds = f(12), .ease = anim.ease.linear });
asides.say(s, "`rsp` holds the address of the stack's top; a push moves it down a word.", left, rowY(top, first, rsp) + row / 2);
at.walk(s, .moment, moment + 1);
s.wait(t.beat);
// II2: the address the call pushed, and main's under it; the offsets, a column.
asides.say(s, "Each call pushes where to come back to: `main+50` is main's.", left, main_word.y + row / 2);
s.play(.{framed.fadeIn()}, .{ .seconds = f(6), .ease = anim.ease.linear });
s.wait(f(24));
lift(s, &offsets, 2, 12);
s.wait(t.beat);
// II3: a function's frame: rbp's pin, and the panel's tag, last.
asides.say(s, "A function's words are its frame: saved rbp, locals, the way back.", left, rowY(top, rsp_after - 16, rec.register(moment + 1, "rbp")) + row / 2);
s.play(.{framed.fadeOut()}, .{ .seconds = f(18), .ease = anim.ease.linear });
s.cursor -= f(18);
lift(s, &lane, 2, 12);
s.wait(t.beat);
// `frame up`: its rules (12 f), then its tag and strip (12 f).
lift(s, stack_chrome[1..4], 0, 12);
lift(s, &.{ stack_chrome[0], stack_chrome[4] }, 0, 12);
s.wait(t.beat);
// II4: the registers built over the stack, a row at a time, their tag last.
asides.say(s, "The registers are the other place values live: seventeen of them.", left, rb.y + tag + pad + row * 2.5);
s.play(.{ctx.registers.fadeIn()}, .{ .seconds = 0 });
lift(s, ®s_rows, 12, 12);
lift(s, regs_chrome[1..4], 0, 12);
lift(s, &.{regs_chrome[0]}, 0, 12);
// The chapter's last picture.
s.wait(@max(asides.read_by - s.cursor, 0));
s.wait(t.before_cut);
}
|