//! `aligned` (design/README.md, "aligned"; mocks 32, 33, 36): the source //! with the instructions it compiled to, one panel across both columns, //! each line level with its code; and the introduction's way into it //! (motion.md, chapter III): the code's bytes as a dump row, one //! instruction's bytes framed and `decode`d into its row, the rest at a //! run, then `align` from one form to the other. Clocks the scene plays //! (frames, linear): `bytes`, `decode`, `rows_in`, `chrome`, `form`. //! //! The rows: the pc's function from the first run of the line before the //! pc's to the function's end, as the recording disassembled it at its //! moment, each instruction's bytes as the recording read them. //! //! - `interleaved` (form 0), as `objdump -S` prints it: each run of a //! line's instructions a group, its head the line on `raised` paper, its //! rows under it; lines and addresses ranged right at 132 in one column; //! a line's second run `again: its second run`, faint. //! - `columns` (form 1): the source left, a 2-unit `rule` the seam 48 //! after the longest line, the instructions right; a line level with the //! first row of its run; a line with no code of its own in source order //! after the line before it, faint, `no code of their own`. //! //! `align` between them (6. Verbs): the bytes go (8 f), the addresses and //! instructions glide right into the instruction column (24 f), then every //! row glides to its height (36 f: the verb's 24 read too quick for rows //! crossing a column), then the lines with no code fade in (12 f). //! //! The lines as the mocks set them, without their indentation; the pc's //! row says what it reads (its address and chain, mock 33's annotation). const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); const ui = @import("ui.zig"); const disasm = @import("disasm.zig"); const rows = @import("rows.zig"); const Recording = @import("Recording.zig"); const theme = ui.theme; const Rect = ui.Rect; const t = theme.color.text; const size = theme.size.mono.context; const adv = ui.advance(size); const pitch = theme.metric.context.row; const tag = theme.metric.context.tag; const pad = theme.metric.context.pad; /// The columns, from the panel's left (mock 33, 36). const col = struct { const place_end = 132; const text = 156; const bytes = 180; const mnemonic = 360; const seam_after = 48; const instr = 156; /// The pc's row's annotation, from its mnemonic: interleaved, columns (mocks 32, 36). const note = 540; const note_columns = 360; }; /// The dump row's first byte (`bytes`: at 132 at least). const dump_first = theme.metric.bytes.first; fn clamp01(x: f32) f32 { return std.math.clamp(x, 0, 1); } fn glide(x: f32) f32 { return theme.ease(.glide, clamp01(x)); } const Row = struct { kind: enum { head, insn, bare_line }, line: u32, insn: ?disasm.Insn = null, again: bool = false, /// The first row of its run (columns: where its line stands). first: bool = false, /// Its place in each form, in rows. at: [2]f32 = .{ 0, 0 }, }; pub const Aligned = struct { rec: *const Recording, at: u64, /// Under a text `above` of two lines (48 to 182). box: Rect = .{ .x = theme.canvas.margin_x, .y = 192, .w = theme.canvas.stage.w, .h = 696 }, /// Where the dump row of the code's bytes stands (III1). dump_y: f32 = 192 + tag + pad, bytes: anim.Value, decode: anim.Value, rows_in: anim.Value, chrome: anim.Value, form: anim.Value, /// `stack` (6. Verbs, `align` backwards), frames: the heads' paper and /// rules, the seam and the tag out (12 f), then each line glides to its /// row of the source panel (36 f, `glide`) and each instruction to its /// row of the disassembly, down its column first (24 f), then left into /// the panel (24 f): an instruction never passes over a line. Those the /// working frame's windows do not show go on past their edge, fading. /// 60 f in all; the scene then brings the panels in under them and lets this go. stack: anim.Value, /// Where they stack to: the working frame's source and disassembly boxes. to: struct { source: Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 }, disasm: Rect = .{ .x = 0, .y = 0, .w = 0, .h = 0 } } = .{}, pub fn init(s: *anim.Frame, rec: *const Recording, moment: u64) Aligned { return .{ .rec = rec, .at = moment, .bytes = s.value(-1), .decode = s.value(-1), .rows_in = s.value(-1), .chrome = s.value(-1), .form = s.value(0), .stack = s.value(-1) }; } fn sourceLine(a: *const Aligned, n: u32) []const u8 { var it = std.mem.splitScalar(u8, a.rec.source, '\n'); var k: u32 = 1; while (it.next()) |l| : (k += 1) if (k == n) return std.mem.trim(u8, l, " \t\r"); return ""; } /// The rows in interleaved order, with their places in both forms; the pc's index. fn build(a: *const Aligned, u: ui.Ui) struct { rows: []Row, pc: usize, longest: f32 } { const pc = a.rec.register(a.at, "rip"); const listing = disasm.listingAt(u, a.rec, a.at, pc, 64, 64); const all = listing.rows; const pc_line = all[listing.pc].line orelse 0; var start: usize = 0; while (start < all.len and (all[start].line orelse 0) + 1 != pc_line) start += 1; if (start == all.len) start = 0; var end = start; while (end < all.len and !all[end].outside) end += 1; const shown = all[start..end]; var out: std.ArrayList(Row) = .empty; const arena = u.f.scratch; var seen: [64]u32 = undefined; var n_seen: usize = 0; var lo: u32 = std.math.maxInt(u32); var hi: u32 = 0; for (shown) |r| if (r.line) |l| { lo = @min(lo, l); hi = @max(hi, l); }; var k: usize = 0; var pc_index: usize = 0; while (k < shown.len) { const line = shown[k].line orelse 0; const again = std.mem.indexOfScalar(u32, seen[0..n_seen], line) != null; if (!again and n_seen < seen.len) { seen[n_seen] = line; n_seen += 1; } out.append(arena, .{ .kind = .head, .line = line, .again = again }) catch break; var first = true; while (k < shown.len and (shown[k].line orelse 0) == line) : (k += 1) { if (shown[k].addr == pc) pc_index = out.items.len; out.append(arena, .{ .kind = .insn, .line = line, .insn = shown[k], .first = first }) catch break; first = false; } // Lines after this one in the source with no code of their own (columns only), once. if (!again) { var l = line + 1; while (l < hi and !hasCode(all, l)) : (l += 1) if (l > lo) out.append(arena, .{ .kind = .bare_line, .line = l, .first = l == line + 1 }) catch break; } } // Their places: interleaved, heads and rows in order (no bare lines); // columns, the instructions in order with the bare lines after theirs. var inter: f32 = 0; var cols: f32 = 0; var longest: f32 = 0; for (out.items) |*r| { longest = @max(longest, @as(f32, @floatFromInt(a.sourceLine(r.line).len)) * adv); switch (r.kind) { .head => { r.at = .{ inter, cols }; inter += 1; }, .insn => { r.at = .{ inter, cols }; inter += 1; cols += 1; }, .bare_line => { r.at = .{ inter, cols }; cols += 1; }, } } return .{ .rows = out.items, .pc = pc_index, .longest = longest }; } fn hasCode(all: []const disasm.Insn, line: u32) bool { for (all) |r| if (!r.outside and r.line == line) return true; return false; } pub fn draw(a: *const Aligned, f: *sk.Frame, placed: anim.Placed) void { const u: ui.Ui = .{ .f = f, .at = placed }; const built = a.build(u); if (built.rows.len == 0) return; const m = a.rec.moment(a.at); const form = placed.value(a.form); // The bytes first (interleaved alone has them), then the columns glide: never over them. const bytes_go = clamp01(form / 8); const across = glide((form - 8) / 24); const down = glide((form - 32) / 36); const bare = clamp01((form - 68) / 12); // The seam 48 after the longest line, or after a second run's line and its note; the // mnemonics after the longest instruction's bytes (mock 32's 360 holds five). var seam = col.text + built.longest + col.seam_after; var most: usize = 0; for (built.rows) |r| { if (r.kind == .head and r.again) seam = @max(seam, col.text + lineWidth(a, r.line) + again_gap + u.width(.sans, theme.size.sans.micro, again_note) + 24); if (r.insn) |insn| most = @max(most, insn.len); } const mnemonic = @max(col.mnemonic, col.bytes + @as(f32, @floatFromInt(3 * most)) * adv + 12); const rows_in = placed.value(a.rows_in); const st = placed.value(a.stack); const kept = 1 - clamp01(st / 12); const going = glide((st - 12) / 36); const down_first = glide((st - 12) / 24); const then_left = glide((st - 36) / 24); const chrome = clamp01(placed.value(a.chrome) / 12) * kept; const tag_in = clamp01((placed.value(a.chrome) - 12) / 12) * kept; const v = u.moved(a.box.x, a.box.y); // Nothing under the stage (y 888): rows `stack` takes past the disassembly's foot fade there, not over the caption. v.clip(.{ .x = -a.box.x, .y = -a.box.y, .w = theme.canvas.width, .h = theme.canvas.stage.y + theme.canvas.stage.h }); defer v.unclip(); const body = tag + pad; var counts = [2]f32{ 0, 0 }; for (built.rows) |r| counts = .{ @max(counts[0], r.at[0] + 1), @max(counts[1], r.at[1] + 1) }; const h = body + pitch * std.math.lerp(counts[0], counts[1], down) + pad; // The panel's rule and tag last (`frame up`). if (tag_in > 0) { const tv = v.faded(tag_in); tv.fill(.{ .x = 0, .y = 0, .w = a.box.w, .h = tag }, theme.color.raised); tv.fill(.{ .x = 8, .y = 6, .w = 12, .h = 12 }, theme.mark(.current).solid); tv.text(.sans_medium, theme.size.sans.tag, 28, tag - 6, v.print("{s} \u{00b7} {s}", .{ a.rec.file, functionName(v, a.rec, a.at) }), t.primary); const notes = [2][]const u8{ "each line, then the instructions it compiled to \u{00b7} lines and addresses in one column", "each line level with its instructions" }; for (notes, [2]f32{ 1 - down, down }) |note, k| if (k > 0) tv.faded(k).tagNote(a.box.w, note); } if (chrome > 0) v.faded(chrome).frame(.{ .x = 0, .y = 0, .w = a.box.w, .h = h }, theme.color.rule, theme.stroke.rule); // The seam, with the columns. if (down > 0) v.faded(down * kept).fill(.{ .x = seam, .y = tag, .w = theme.stroke.rule, .h = h - tag }, theme.color.rule); // Where `stack` takes them: the source's window and the disassembly's, as those panels have them. const src_first: f32 = @floatFromInt(a.rec.shownFirst(@intCast(theme.layout.rowsIn(a.to.source.h)), a.rec.shown[a.at])); const dis_room = theme.layout.rowsIn(a.to.disasm.h); // The panels' columns right of the line numbers move by as much as the widest file's numbers need. const widen = rows.numberRoom(a.rec.most_lines); const dis_rows = if (st >= 0) disasm.listingAt(u, a.rec, a.at, a.rec.register(a.at, "rip"), dis_room, (dis_room -| 1) / 2).rows else &.{}; for (built.rows, 0..) |r, k| { var y = body + pitch * std.math.lerp(r.at[0], r.at[1], down); const order: f32 = @floatFromInt(k); // At a run: rows top down, 2 f apart, 8 f each; the pc's row is the decoded one. // The pc's row is the decoded one, once it has travelled to its place (48). var shows: f32 = if (k == built.pc) @as(f32, if (placed.value(a.decode) >= 48) 1 else 0) else clamp01((rows_in - 2 * order) / 8); if (r.kind == .bare_line) shows = @min(shows, bare); // `stack`: its row in the working frame's panel (box-relative), and whether that window shows it. var to_x: f32 = 0; var to_y: f32 = 0; if (st >= 0) { var inside = true; if (r.kind == .insn) { var n: ?usize = null; for (dis_rows, 0..) |d, i| if (d.addr == r.insn.?.addr) { n = i; }; // Past the window: where it would be, going on from its last row. const at_row: f32 = if (n) |i| @floatFromInt(i) else @as(f32, @floatFromInt(dis_rows.len)) + order - @as(f32, @floatFromInt(built.pc)); inside = n != null; to_x = a.to.disasm.x - a.box.x; to_y = a.to.disasm.y - a.box.y + body + pitch * at_row; } else { const at_row = @as(f32, @floatFromInt(r.line)) - src_first; inside = !r.again and at_row >= 0 and at_row < @as(f32, @floatFromInt(theme.layout.rowsIn(a.to.source.h))); to_x = a.to.source.x - a.box.x; to_y = a.to.source.y - a.box.y + body + pitch * at_row; } if (!inside) shows *= 1 - (if (r.kind == .insn) then_left else going); } if (shows <= 0) continue; const rv = v.faded(shows); if (r.kind == .insn and st >= 0) y = std.math.lerp(y, to_y, down_first); const base = theme.baseline(y, pitch, size); switch (r.kind) { .head, .bare_line => { // Interleaved: a head across the panel; columns: the line in its cell, level with its run. var head_y = if (r.kind == .head) y else body + pitch * r.at[1]; if (st >= 0) head_y = std.math.lerp(head_y, to_y, going); const hb = theme.baseline(head_y, pitch, size); const paper_w = std.math.lerp(a.box.w - 4, seam - 2, down); if (r.kind == .head and !r.again and chrome > 0) rv.faded(chrome).fill(.{ .x = 2, .y = head_y, .w = paper_w, .h = pitch }, theme.color.raised); if (chrome > 0 and r.kind == .head) rv.faded(chrome).fill(.{ .x = 2, .y = head_y - 1, .w = a.box.w - 4, .h = theme.stroke.rule }, theme.color.rule); const faint = r.again or r.kind == .bare_line; const number = rv.print("{d}", .{r.line}); const is_pc = !r.again and built.rows[built.pc].line == r.line and r.kind == .head; // `stack`: the number to the source's number column, the text to its text column at its indent. const n_end = std.math.lerp(col.place_end, to_x + theme.metric.context.line_number_end + widen, going); const t_x = std.math.lerp(col.text, to_x + theme.metric.context.line_text + widen + @as(f32, @floatFromInt(indentOf(a, r.line))) * adv, going); if (is_pc) rv.fill(.{ .x = n_end - @as(f32, @floatFromInt(number.len)) * adv - 4, .y = head_y + 1, .w = @as(f32, @floatFromInt(number.len)) * adv + 6, .h = pitch - 2 }, theme.mark(.current).solid); rv.text(.mono, size, n_end - @as(f32, @floatFromInt(number.len)) * adv, hb, number, if (is_pc) t.ink else t.faint); rv.text(.mono, size, t_x, hb, a.sourceLine(r.line), if (faint) t.faint else t.primary); if (r.again) rv.faded(kept).text(.sans, theme.size.sans.micro, std.math.lerp(mnemonic + 4, col.text + lineWidth(a, r.line) + again_gap, down), hb, again_note, t.dim); if (r.kind == .bare_line and r.first) rv.faded(kept).text(.sans, theme.size.sans.micro, seam + col.instr, hb, "no code of their own", t.dim); }, .insn => { const insn = r.insn.?; // `stack`: the row's left edge to the disassembly's. const ix = std.math.lerp(std.math.lerp(0, seam, across), to_x, then_left); const right = std.math.lerp(a.box.w, to_x + a.to.disasm.w, then_left); if (k == built.pc) { const wash = rv.faded(clamp01((placed.value(a.decode) - 48) / 12)); wash.current(.{ .x = ix + 2, .y = y, .w = right - ix - 4, .h = pitch }, theme.metric.plaque.pc, 0); sk.draw.polygon(f, &.{ .{ ix + 20, y + pitch / 2 }, .{ ix + 10, y + 6 }, .{ ix + 10, y + pitch - 6 } }, wash.at.pen(.{ .fill = ui.rgb(theme.mark(.current).on_solid) })); } const digits: f32 = @floatFromInt(ui.digits(insn.addr)); // (`stack`: ranged right in its column to the disassembly's address column, from 40.) _ = rv.address(std.math.lerp(std.math.lerp(0, seam, across) + col.place_end - digits * adv, to_x + theme.metric.insn.address, then_left), base, insn.addr, .{ .ink = ui.rgb(theme.color.region.code_) }); // Its bytes, as the recording read them: interleaved only. if (bytes_go < 1) rv.faded(1 - bytes_go).text(.mono, size, col.bytes, base, hexBytes(rv, m, insn), t.dim); const mx = std.math.lerp(std.math.lerp(mnemonic, seam + col.instr + 24, across), to_x + theme.metric.insn.mnemonic + widen, then_left); disasm.bareRow(rv.moved(mx - theme.metric.insn.mnemonic, 0), insn, y); // The pc's row: what it reads, its address and the chain from it, once decoded. if (k == built.pc) if (a.rec.insn_touches[a.at][0]) |touch| { _ = rows.chain(rv.faded(clamp01((placed.value(a.decode) - 48) / 12) * kept), mx + std.math.lerp(col.note, col.note_columns, across), y, m, touch.addr, a.box.w - 24, .{}); }; }, } } a.drawBytesRow(v, m, placed, built, mnemonic); } /// III1 and the decode (III2): the code's sixteen bytes as a dump row, /// in code's ink 2 f apart; the pc's instruction's bytes framed; then /// they gather, its text fades in beside them, and the row travels to /// its place in the panel. fn drawBytesRow(a: *const Aligned, v: ui.Ui, m: Recording.Moment, placed: anim.Placed, built: anytype, mnemonic: f32) void { const clock = placed.value(a.bytes); if (clock < 0) return; const d = placed.value(a.decode); const insn = built.rows[built.pc].insn.?; const row_addr = insn.addr & ~@as(u64, 15); var bytes: [16]u8 = undefined; var known: [16]bool = undefined; m.read(row_addr, &bytes, &known) catch return; const dy = a.dump_y - a.box.y; const target_y = tag + pad + pitch * built.rows[built.pc].at[0]; const travel = glide((d - 24) / 24); if (travel >= 1) return; const y = std.math.lerp(dy, target_y, travel); const base = theme.baseline(y, pitch, size); const arrive = clamp01(clock / 12); const gather = glide(d / 12); const others = 1 - clamp01(d / 12); const bx = struct { fn at(k: usize) f32 { return dump_first + @as(f32, @floatFromInt(k)) * theme.metric.bytes.pitch + @as(f32, if (k >= 8) theme.metric.bytes.middle else 0); } }.at; // The row's address: the row's, then the instruction's. const rv = v.faded(arrive); const ad: f32 = @floatFromInt(ui.digits(row_addr)); rv.faded(others).text(.mono, size, 34, base, rv.print("{x}", .{row_addr}), t.faint); _ = rv.faded(1 - others).address(std.math.lerp(34, col.place_end - ad * adv, travel), base, insn.addr, .{ .ink = ui.rgb(theme.color.region.code_) }); const first: usize = @intCast(insn.addr - row_addr); const target_x = std.math.lerp(bx(first), col.bytes, travel); for (0..16) |k| { const mine = k >= first and k < first + insn.len; const inked = clamp01((clock - 12 - 2 * @as(f32, @floatFromInt(k))) / 12); const ink = ui.mix(t.primary, theme.color.region.code_, inked); const text = if (known[k]) rv.print("{x:0>2}", .{bytes[k]}) else "??"; if (!mine) { if (others > 0) rv.faded(others).text(.mono, size, bx(k), base, text, ink); continue; } // Gathered: three characters apart, from where the instruction's first byte stood. const at_x = std.math.lerp(bx(k), target_x + @as(f32, @floatFromInt(k - first)) * 3 * adv, gather); rv.text(.mono, size, at_x, base, text, ui.mix(ink, ui.rgb(t.dim), clamp01((d - 12) / 12))); } // The instruction's bytes framed once inked (6 f), until they gather. const framed = clamp01((clock - 12 - 32 - 12) / 6) * (1 - clamp01(d / 6)); if (framed > 0) rv.read(.{ .x = bx(first), .y = y, .w = bx(first + insn.len - 1) - bx(first) + 2 * adv, .h = pitch }, framed); // Its text beside the bytes, at the panel's columns from them (12 f). const text_in = clamp01((d - 12) / 12); if (text_in > 0) disasm.bareRow(rv.faded(text_in).moved(target_x - col.bytes + mnemonic - theme.metric.insn.mnemonic, 0), insn, y); } }; const again_note = "again: its second run"; const again_gap = 36; /// How far line `n` is indented in the file (its leading spaces; a tab as four). fn indentOf(a: *const Aligned, n: u32) usize { var it = std.mem.splitScalar(u8, a.rec.source, '\n'); var k: u32 = 1; while (it.next()) |l| : (k += 1) if (k == n) { var w: usize = 0; for (l) |ch| switch (ch) { ' ' => w += 1, '\t' => w += 4, else => break, }; return w; }; return 0; } fn lineWidth(a: *const Aligned, n: u32) f32 { return @as(f32, @floatFromInt(a.sourceLine(n).len)) * adv; } fn hexBytes(u: ui.Ui, m: Recording.Moment, insn: disasm.Insn) []const u8 { var bytes: [16]u8 = undefined; var known: [16]bool = undefined; const n = @min(insn.len, 16); m.read(insn.addr, bytes[0..n], known[0..n]) catch return ""; var out: std.ArrayList(u8) = .empty; for (bytes[0..n], known[0..n], 0..) |b, kn, k| { if (k > 0) out.append(u.f.scratch, ' ') catch {}; out.appendSlice(u.f.scratch, if (kn) u.print("{x:0>2}", .{b}) else "??") catch {}; } return out.items; } fn functionName(u: ui.Ui, rec: *const Recording, i: u64) []const u8 { var buf: [128]u8 = undefined; const name = (rec.moment(i).string(rec.register(i, "rip"), &buf) catch null) orelse return ""; const plus = std.mem.indexOfScalar(u8, name, '+') orelse name.len; return u.print("{s}", .{name[0..plus]}); }