//! The introduction, chapter III: the stack (design/motion.md, "A scene: //! the introduction", III1 to III5; mock 31), a function the scene `intro` //! calls after II's seam. It goes on from chapter II's last picture, on //! II's context (its registers, folded to the scene's one set; reverse's //! rows at the program column's foot, no panel round them; the clock): the //! 128 bytes, back at the rows II gave them, become the stack panel where //! they stand (sixteen words and four `free` over rsp, the window held //! there so that nothing in the panel moves while the frame is made); the //! convention said there (lower addresses drawn higher), the offsets coming //! down the rows; rsp's pin landing on the top word and its link to it. The //! call first: the way back main left framed, main's three rows round its //! call arriving in the program column, the word aimed at the row it //! returns to (`aim`, drawn head first, standing at 60% through the push). //! Then `push rbp` walked: the pc's mark on its row, the touch to the word, //! rbp's value carried to it and struck, rsp's pin climbing. `mov rbp, rsp` //! and `sub rsp` walked: the frame; its roles (the way back named) and //! spans, a `column` each, the panels' tags last (`frame up`). The text //! stands beside on the left, in the program column, over the code. //! //! What a beat has not reached yet is under the page (`intro.Cover`, and the //! stack's own `reveal`), lifted as the beat says it: the context's panels //! draw every column at once. const std = @import("std"); const anim = @import("anim"); const canim = @import("canim"); const intro = @import("intro.zig"); const registers = @import("intro_registers.zig"); const t = canim.theme.time; const row = canim.theme.metric.context.row; const f = intro.f; const run = intro.run; const place = intro.place; const cover = intro.cover; const lift = intro.lift; /// The chapter's last picture, which chapter IV opens on. pub const Three = struct { /// The state column (the registers, the stack) and the code (main's rows, reverse's, the ties). state: [2]anim.Obj, code: [3]anim.Obj, clock: anim.Obj, /// The cursor the clock counts on (II's and III's). at: canim.Cursor, asides: canim.Asides, e: f32, pub fn texts(three: *const Three, s: *anim.Frame) []const anim.Obj { return registers.standing(s, &three.asides); } }; /// From the seam's opening (the cursor), on chapter II's context. pub fn chapter(s: *anim.Frame, rec: *canim.Recording, two: registers.Two, title: intro.Title) Three { const ctx = two.ctx; var at = two.at; // Chapter I's moment: reverse's first instruction, `push rbp`; then `mov rbp, rsp` and `sub rsp, 0x18`. const moment = rec.call("reverse", 0).enter; const framed = moment + 3; const g = layout(ctx.stage, rec, moment); const sb = g.stack; const rsp = g.rsp; const first = g.first; const top = g.top; const slots = canim.layout.rowsIn(sb.h); // The window held where it is, four `free` words over rsp: the push and `sub rsp` fill them. hold(s, ctx.stage, moment); // The stack, its offsets, roles and spans under the page until their beats. // (Rows 4 f apart: the offsets come down while the convention is read.) const reveal: canim.Stack.Reveal = .{ .offsets = s.value(-1), .roles = s.value(-1), .spans = s.value(-1), .apart = 4 }; const stack = canim.add(s, canim.Stack{ .cursor = ctx.cursor, .stage = ctx.stage, .reveal = reveal }); // The pins' lane: rsp's row's lifted with rsp, the rest (rbp's pin) with the push. var lane: [32]?anim.Obj = @splat(null); const rsp_row: usize = held_free; for (0..@min(slots, 32)) |k| lane[k] = cover(s, .{ .x = sb.x + 2, .y = top + row * @as(f32, @floatFromInt(k)), .w = canim.theme.metric.slot.lane_end - 2, .h = row }); const stack_chrome = intro.chrome(s, sb, true); // The 128 bytes become the stack's rows where they stand (II8 put them there). const dump = two.dump; dump.panel = .{ .x = sb.x, .top = top, .first = first }; // (Over the page that covers the stack's lane: the addresses travel across it.) const dump_obj = place(s, dump); // main's rows, in the program column over reverse's; the aim joins them to the word. const back_y = rowY(top, first, rsp); const main_rows = s.new(canim.descent.Caller{ .rec = rec, .at = moment, .call = rec.register(moment - 1, "rip"), .box = g.caller, .in = s.value(-1), .faint = s.value(0) }); const caller = place(s, main_rows); const main_word: canim.Rect = .{ .x = sb.x + canim.theme.metric.slot.address + 12 * 12 + canim.theme.metric.context.arrow_w, .y = back_y, .w = @as(f32, @floatFromInt(canim.ui.digits(rec.register(moment - 1, "rip") + 5))) * 12, .h = row }; const word_frame = place(s, s.new(intro.Read{ .box = main_word })); // The relations, and rsp's link to its word as III2 names it. const joins, const rsp_link = ctx.tiesWith(s, "rsp", 1.0); const opening = s.cursor; s.play(.{ caller.fadeIn(), joins.fadeIn(), two.dump_obj.fadeOut(), dump_obj.fadeIn() }, .{ .seconds = 0 }); s.cursor = opening; var asides: canim.Asides = .{}; const left = canim.theme.canvas.margin_x; // The opening (126 to 174): the bytes fade, the addresses and the words glide into the // stack's rows (36 f) and become the panel (12 f). Where it first stands, its convention // said, unlike the bar's: the offsets come down its rows (a column, 4 f apart). s.play(.{dump.become.to(1)}, .{ .seconds = f(36), .ease = anim.ease.linear }); s.play(.{ stack.fadeIn(), dump_obj.fadeOut() }, .{ .seconds = f(12), .ease = anim.ease.linear }); title.out(s); asides.say(s, texts[0], left, rowY(top, first, first) + row / 2); const column = reveal.apart * @as(f32, @floatFromInt(slots)) + 12; run(s, reveal.offsets, column); s.wait(t.beat); // III2: rsp's pin lands on the top word (12 f), then rsp's link from its row down to the // word, held, drawn back. Its row was built in II: nothing arrives. asides.say(s, texts[1], left, back_y + row / 2); lift(s, &.{lane[rsp_row]}, 0, 12); rsp_link.play(s); s.wait(rsp_link.total()); s.wait(f(18)); s.wait(t.beat); // III3: the call, first: the way back main left, framed; main's rows; the aim drawn to its row. asides.say(s, texts[2], left, back_y + row / 2); s.play(.{word_frame.fadeIn()}, .{ .seconds = f(6), .ease = anim.ease.linear }); s.wait(f(12)); run(s, main_rows.in, 12 + 2 * 2 + 12); s.wait(f(6)); at.aim(s, .drawn); s.wait(f(24 + 30)); s.wait(t.beat); // III4: the push: rbp's pin; `push rbp` walked (its row fires, the touch to the word, // rbp's value carried there and struck, rsp's pin climbing). Reverse's rows are on screen since II. asides.say(s, texts[3], left, rowY(top, first, rsp - 8) + row / 2); at.aim(s, .standing); s.play(.{ word_frame.fadeOut(), main_rows.faint.to(1) }, .{ .seconds = f(18), .ease = anim.ease.linear }); var lanes: [32]?anim.Obj = lane; lanes[rsp_row] = null; lift(s, &lanes, 0, 12); s.wait(f(12)); at.levels.carry = .after_touch; at.walk(s, .moment, moment + 1); at.levels.carry = .off; s.wait(t.beat); // III5: the frame made (`mov rbp, rsp`, `sub rsp`); its roles, spans, and the tags last. asides.say(s, texts[4], left, g.span_y); at.aim(s, .off); at.walk(s, .moment, moment + 2); at.walk(s, .moment, framed); run(s, reveal.roles, column); run(s, reveal.spans, reveal.apart * 4 + 12); s.wait(t.beat); // `frame up`: the rules (12 f), then the tags and strips (12 f). const at_rules = s.cursor; lift(s, two.disasm_chrome[1..4], 0, 12); s.cursor = at_rules; lift(s, stack_chrome[1..4], 0, 12); const at_tags = s.cursor; lift(s, &.{ two.disasm_chrome[0], two.disasm_chrome[4] }, 0, 12); s.cursor = at_tags; lift(s, &.{ stack_chrome[0], stack_chrome[4] }, 0, 12); // The chapter's last picture, held until its last text is read: then the seam. s.wait(@max(asides.read_by - s.cursor, 0)); return .{ .state = .{ two.registers, stack }, .code = .{ caller, two.disasm, joins }, .clock = two.clock, .at = at, .asides = asides, .e = s.cursor }; } /// The chapter's texts, in order. pub const texts = [_][]const u8{ "Here lower addresses are drawn higher: down a word is up a row.", "`rsp` holds the address of the top of the stack.", "A call pushes the address to come back to: here, `main+50`.", "A push moves `rsp` down a word and stores there: `push rbp` keeps the caller's `rbp`.", "`rbp` marks the frame's base. A function's words are its frame: the saved rbp, its locals, the way back.", }; /// Free words over rsp in the held window. const held_free = 4; /// The stack's window held from here (no scroll to it), `held_free` words over rsp at `moment`. fn hold(s: *anim.Frame, stage: *canim.Stage, moment: u64) void { s.play(.{ stage.hold.to(@floatFromInt(moment)), stage.hold_free.to(held_free), stage.hold_in.to(1) }, .{ .seconds = 0 }); } /// Where the chapter's parts stand: the stack under the registers' eleven /// rows (356 to 868), its window from `first` (four free words over rsp), /// its rows from `top`; main's rows in the program column (604 to 700) over /// reverse's six, where chapter II put them (712 to 888). fn layout(stage: *canim.Stage, rec: *canim.Recording, moment: u64) struct { stack: canim.Rect, rsp: u64, first: u64, top: f32, caller: canim.Rect, span_y: f32 } { const rb = stage.first.registers; // Sixteen slots and the headroom under the registers' box (the words // become them where they stand), not the column's rest. stage.first.stack.h = canim.layout.panelH(16 + held_free); stage.first.stack.y = rb.y + rb.h + canim.theme.canvas.row_gap; const sb = stage.first.stack; const rsp = rec.register(moment, "rsp"); const first = rsp - 8 * held_free; const top = sb.y + canim.theme.metric.context.tag + canim.theme.metric.context.pad; const code = stage.first.disasm; const caller: canim.Rect = .{ .x = code.x, .y = code.y - canim.theme.canvas.row_gap - 4 * row, .w = code.w, .h = 4 * row }; const rsp_framed = rec.register(moment + 3, "rsp"); return .{ .stack = sb, .rsp = rsp, .first = first, .top = top, .caller = caller, .span_y = (rowY(top, first, rsp_framed) + rowY(top, first, rsp + 8)) / 2 }; } fn rowY(t0: f32, from: u64, addr: u64) f32 { return t0 + row * @as(f32, @floatFromInt(@as(i64, @bitCast(addr -% from)) >> 3)); }