//! 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); }