summaryrefslogtreecommitdiff
path: root/anim/src/disasm.zig
diff options
context:
space:
mode:
Diffstat (limited to 'anim/src/disasm.zig')
-rw-r--r--anim/src/disasm.zig210
1 files changed, 208 insertions, 2 deletions
diff --git a/anim/src/disasm.zig b/anim/src/disasm.zig
index 7017453..085982d 100644
--- a/anim/src/disasm.zig
+++ b/anim/src/disasm.zig
@@ -225,6 +225,7 @@ pub const Disasm = struct {
continue;
}
map.row(.disasm, row.addr, u, .{ .x = 0, .y = y, .w = box.w, .h = m.row });
+ ties.anchor(u, .{ .row = row.addr }, .{ .x = 0, .y = y, .w = box.w, .h = m.row });
const is_pc = row.addr == pc[if (going >= 0.5) 1 else 0];
const was = if (is_pc) null else ran[j];
if (was) |r| lit.trail(c.mark, y, r.k);
@@ -287,6 +288,12 @@ pub const Disasm = struct {
return 0;
}
if (!row.bare) _ = at.address(c.address, base_y, row.addr, .{ .ink = ui.rgb(code_ink), .lead = lead });
+ // `tied`: the row the step's touch (or carry) leaves from, its right rule, for the ties.
+ if (!ui.mapping and now.annotation == .tied and now.walking() and (now.plan.touch >= 0 or now.plan.carry >= 0)) if (touchPc(rec, now)) |pc| if (pc == row.addr) {
+ const a = at.at.xf.apply(.{ w + theme.strip.cost, y });
+ const b = at.at.xf.apply(.{ w + theme.strip.cost, y + m.row });
+ ties.touch_row = .{ a[0], a[1], b[0], b[1] };
+ };
if (first_of_line) if (line) |l| {
const num = u.print("{d}", .{l});
const now_line = l == current_line;
@@ -356,6 +363,15 @@ pub const Disasm = struct {
if (state == .ran) if (returned(rec, row, moment, now.stops[base])) |back| {
return a.said(a.cForm(row, b.to, moment, rec.register(back, "rax"), a.room - a.x), t.primary);
};
+ // `tied`: a call to the program's own function in C's form (a PLT stub's callee by name,
+ // its arguments under it), then its move of rsp, struck with rsp: `fib(4) · rsp -8`.
+ if (now.annotation == .tied and state == .current) {
+ const stub = pltName(u, rec, target(row).?).len > 0;
+ var note = if (stub) a.callee(b.to, moment) else a.said(a.cForm(row, b.to, moment, null, a.room - a.x - 10 * adv), t.primary);
+ note.end = a.move(note.end + adv, -8, a.rspHeat(now, base), null, true);
+ if (stub) note.hangs = a.arguments(row, b.to, moment);
+ return note;
+ }
var note = a.callee(b.to, moment);
// Its arguments hang under it, a small `reg` a row, as many as the prototype has.
if (state == .current) note.hangs = a.arguments(row, b.to, moment);
@@ -363,7 +379,10 @@ pub const Disasm = struct {
},
.ret => {
u.text(.mono, size, x, base_y, "\u{2192}", t.faint);
- return .{ .drawn = true, .end = u.address(x + 2 * adv, base_y, b.to, .{ .ink = ui.rgb(code_ink), .name = rec.strings.get(b.to) orelse "" }) };
+ const end = u.address(x + 2 * adv, base_y, b.to, .{ .ink = ui.rgb(code_ink), .name = rec.strings.get(b.to) orelse "" });
+ // `tied`: then its move of rsp, `· rsp +8`.
+ if (now.annotation == .tied) return .{ .drawn = true, .end = a.move(end + adv, 8, if (state == .current) a.rspHeat(now, base) else .none, null, true) };
+ return .{ .drawn = true, .end = end };
},
.jump => {
// Through memory or a register: where it goes. Straight: nothing to say.
@@ -386,6 +405,8 @@ pub const Disasm = struct {
if (std.mem.eql(u8, mn, "jmp")) if (ripSlot(row)) |slot| if ((zt.telescope.got(rec.moment(moment), slot) catch null)) |held| return a.went(held.value, .{ .addr = slot, .len = 8, .how = 1 }, moment);
return .{};
}
+ // `tied`: an instruction that moves rsp or rbp says the move first.
+ if (now.annotation == .tied) if (a.stackMove(now, row, moment, after, state, base)) |note| return note;
// A compare: the two values, then (once it has run) the flags it leaves.
if (std.mem.eql(u8, mn, "cmp") or std.mem.eql(u8, mn, "test")) {
const left = a.compared(row, moment, touches[0]);
@@ -463,6 +484,8 @@ pub const Disasm = struct {
marks.changed_heat = now.written(touch.addr, touch.len, .memory, 0);
}
};
+ // `tied`: the word by its name and the value, one hop (the stack's row holds the rest).
+ if (now.annotation == .tied) if (a.tiedWord(touch, moment, shown, marks, d.stage.first.view.h > 0 or d.stage.last().view.h > 0)) |note| return note;
// Narrower than a word, it is read at its width.
if (touch.len < 8) return .{ .drawn = true, .end = rows.narrow(u, x, y, shown, touch.addr, touch.len, room, marks).end };
return .{ .drawn = true, .end = rows.chain(u, x, y, shown, touch.addr, room, marks).end };
@@ -472,7 +495,8 @@ pub const Disasm = struct {
const at = value_at orelse return .{};
const value = rec.register(at, reg);
u.text(.mono, size, x, base_y, reg, t.dim);
- return .{ .drawn = true, .end = rows.chain(u, x + @as(f32, @floatFromInt(reg.len + 1)) * adv, y, rec.moment(at), value, room, .{}).end };
+ // `tied`: its first value (the registers' row has its chain, struck in the same frame).
+ return .{ .drawn = true, .end = rows.chain(u, x + @as(f32, @floatFromInt(reg.len + 1)) * adv, y, rec.moment(at), value, room, .{ .most = if (now.annotation == .tied) 1 else 6 }).end };
}
};
@@ -509,6 +533,155 @@ const Say = struct {
return bare[0 .. bare.len - 4];
}
+ /// rsp's mark in this step, for the amount an annotation moves it by: an echo of its strike.
+ fn rspHeat(a: Say, now: cursors.Now, base: usize) ui.Heat {
+ _ = a;
+ if (base != 1 or now.resting()) return .none;
+ // The amount is new in this row: no corner of rsp's last change under it.
+ return now.heat(now.registerAge(cursors.sp_index), null, now.since(.cpu, ties.strikeDelay(now, cursors.sp_index)));
+ }
+
+ /// `rsp -8` from `x` (after something else: `· rsp -8`): the amount
+ /// `by` (in bytes) struck with `heat`; then, `· role` faint and its
+ /// role, where the word it writes or reads has one.
+ fn move(a: Say, x: f32, by: i64, heat: ui.Heat, role: ?Role, after: bool) f32 {
+ var end = a.word(if (after) a.word(x, "\u{00b7}", t.faint) + adv else x, "rsp", t.dim);
+ const n: u64 = @abs(by);
+ const amount = if (n <= 9) a.u.print("{c}{d}", .{ @as(u8, if (by < 0) '-' else '+'), n }) else a.u.print("{c}0x{x}", .{ @as(u8, if (by < 0) '-' else '+'), n });
+ end = a.struck(end + adv, amount, heat);
+ if (role) |r| if (r.shows > 0) {
+ const v = a.u.faded(r.shows);
+ const b: Say = .{ .u = v, .rec = a.rec, .y = a.y, .x = a.x, .room = a.room };
+ end = b.word(b.word(end + adv, "\u{00b7}", t.faint) + adv, r.name, if (r.local) t.primary else t.dim);
+ };
+ return end;
+ }
+
+ /// A word's role in an annotation, and how much of it shows.
+ const Role = struct { name: []const u8, local: bool, shows: f32 = 1 };
+
+ /// `text` at `x` in primary, marked as `heat` says (armed, struck, held): where it ends.
+ fn struck(a: Say, x: f32, text: []const u8, heat: ui.Heat) f32 {
+ const w = @as(f32, @floatFromInt(text.len)) * adv;
+ var ink: ui.Ink = .{ .ink = ui.rgb(t.primary) };
+ if (a.u.written(.{ .x = x, .y = a.y, .w = w, .h = m.row }, heat)) |s| ink = s;
+ a.u.text(ink.face, size, x, rows.baseline(a.y), text, ink.ink);
+ return x + w;
+ }
+
+ /// Under `tied` (design/README.md, `insn`, "moves rsp or rbp"): the move
+ /// in its own terms, first, its amount struck with rsp's strike: `rsp -8
+ /// · saved rbp` (a `push`; its word's role once the step has made it),
+ /// `rsp +8 · saved rbp` (a `pop`), `rsp +0x20 · saved rbp` (a `leave`),
+ /// `rsp -0x18 3 words` (`sub`, `add`), `rbp = rsp` (and its value,
+ /// struck as rbp lands). Null for any other instruction.
+ fn stackMove(a: Say, now: cursors.Now, row: Insn, moment: u64, after: ?u64, state: Disasm.State, base: usize) ?Disasm.Note {
+ const mn = row.mnemonic();
+ const ops = row.operands();
+ const eql = std.mem.eql;
+ const regs = a.rec.registers[@min(moment, a.rec.end())];
+ const heat = if (state == .current) a.rspHeat(now, base) else ui.Heat.none;
+ var by: i64 = 0;
+ var touched: ?u64 = null;
+ var word_at = moment;
+ var counts = false;
+ if (eql(u8, mn, "push")) {
+ by = -8;
+ touched = regs.rsp -% 8;
+ // The word it makes has its role at the push's own stop (a recording that says so);
+ // else the moment after's, arriving with the touch.
+ if (wordName(a.u, a.rec, moment, regs.rsp -% 8, 8) == null) if (after) |aa| {
+ word_at = aa;
+ };
+ } else if (eql(u8, mn, "pop")) {
+ by = 8;
+ touched = regs.rsp;
+ } else if (eql(u8, mn, "leave")) {
+ by = @bitCast(regs.rbp +% 8 -% regs.rsp);
+ touched = regs.rbp;
+ } else if ((eql(u8, mn, "sub") or eql(u8, mn, "add")) and std.mem.startsWith(u8, ops, "rsp, ")) {
+ const n: i64 = @bitCast(parseImmediate(std.mem.trim(u8, ops[5..], " ")) orelse return null);
+ by = if (eql(u8, mn, "sub")) -n else n;
+ counts = true;
+ } else if (eql(u8, mn, "mov") and eql(u8, ops, "rbp, rsp")) {
+ var end = a.word(a.x, "rbp = rsp", t.primary);
+ // Its value once rbp has landed, struck with it.
+ const lands = ties.strikeDelay(now, cursors.bp_index);
+ const value_at: ?u64 = switch (state) {
+ .ran => after,
+ .current => if (after != null and now.landed(.cpu, lands)) after.? else null,
+ .ahead, .waiting => null,
+ };
+ if (value_at) |v| end = a.struck(end + adv, a.u.print("{x}", .{a.rec.register(v, "rbp")}), if (state == .current and base == 1) now.heat(now.registerAge(cursors.bp_index), null, now.since(.cpu, lands)) else .none);
+ return .{ .drawn = true, .end = end };
+ } else return null;
+ var role: ?Role = null;
+ if (touched) |w| if (wordName(a.u, a.rec, word_at, w, 8)) |named| {
+ role = .{ .name = named.name, .local = named.local };
+ // A push's word arrives with the touch's landing (12 f), never in a frame.
+ if (word_at != moment and state == .current and !now.resting() and now.plan.touch >= 0) role.?.shows = std.math.clamp((now.time - now.plan.touch - theme.time.tied.touch) / theme.time.quick, 0, 1);
+ };
+ var end = a.move(a.x, by, heat, role, false);
+ if (counts) end = a.word(end + 2 * adv, a.u.print("{d} words", .{@abs(by) / 8}), t.faint);
+ return .{ .drawn = true, .end = end };
+ }
+
+ /// Under `tied` (design/README.md, `insn`, and its fallbacks), a word of
+ /// memory the instruction reads or writes: a stack word by its name, bold
+ /// in the stack's ink (an unnamed one by its address), and the value it
+ /// holds at the access's width, one hop: `next → 405010`, `n ◂ 15`,
+ /// `7fffffffecd0 ◂ 1`; a word a view on screen shows (`view`), its
+ /// address and one hop, `405038 → 405010`. The name (or address) framed
+ /// as read in the step, a write's value struck. Null for a word no pane
+ /// shows: the whole chain stands.
+ fn tiedWord(a: Say, touch: Recording.Touch, moment: u64, shown: Recording.Moment, marks: rows.Marks, view: bool) ?Disasm.Note {
+ const u = a.u;
+ const region = rows.regionOf(a.rec.moment(moment), touch.addr);
+ const named = if (region == .stack) wordName(u, a.rec, moment, touch.addr, touch.len) else null;
+ // The band's view is a dump of the heap. A word no pane shows (a global) keeps its whole
+ // chain, its data name in place of the address where it has one (`head → 405050 ◂ 9`).
+ if (region != .stack and !(view and region == .heap)) return a.globalWord(touch, moment, shown, marks);
+ const base_y = rows.baseline(a.y);
+ const mid = a.y + m.row / 2;
+ var x = a.x;
+ const w = if (named) |n| @as(f32, @floatFromInt(n.name.len)) * adv else ui.Value.of(touch.addr, true).width();
+ if (marks.read == 0) u.read(.{ .x = x, .y = a.y, .w = w, .h = m.row }, marks.read_shows);
+ if (named) |n| u.text(.mono_bold, size, x, base_y, n.name, rows.ink(.stack)) else _ = u.address(x, base_y, touch.addr, .{ .ink = ui.rgb(rows.ink(region)) });
+ x += w;
+ const len: usize = @intCast(@min(touch.len, 8));
+ var raw: [8]u8 = @splat(0);
+ var known: [8]bool = @splat(false);
+ shown.read(touch.addr, raw[0..len], known[0..len]) catch return .{ .drawn = true, .end = x };
+ if (std.mem.indexOfScalar(bool, known[0..len], false) != null) {
+ x = u.arrow(x, mid, .points);
+ u.unknown(.{ .x = x, .y = a.y, .w = 24, .h = m.row });
+ return .{ .drawn = true, .end = x + 24 };
+ }
+ const v = std.mem.readInt(u64, &raw, .little);
+ const pointer = len == 8 and rows.regionOf(shown, v) != .none;
+ x = u.arrow(x, mid, if (pointer) .points else .holds);
+ // The value, its first hop alone (a code address with its name), cut with `…` where the row ends; a write's struck.
+ return .{ .drawn = true, .end = rows.chain(u, x, a.y, shown, v, a.room, .{ .first = if (marks.changed == 1) marks.changed_heat else .none, .most = 1 }).end };
+ }
+
+ /// Under `tied`, a word no pane shows: its whole chain with its data
+ /// name (`head`, `big+4096`) in place of its address, bold in its
+ /// region's ink, read in the step; a write's value struck. Null where it
+ /// has no name (the chain as `whole` draws it).
+ fn globalWord(a: Say, touch: Recording.Touch, moment: u64, shown: Recording.Moment, marks: rows.Marks) ?Disasm.Note {
+ if (touch.len != 8) return null;
+ const full_name = ui.symbol(a.u, a.rec.strings.get(touch.addr) orelse return null);
+ // `head+0` is `head`.
+ const name = if (std.mem.endsWith(u8, full_name, "+0")) full_name[0 .. full_name.len - 2] else full_name;
+ const u = a.u;
+ const w = @as(f32, @floatFromInt(name.len)) * adv;
+ if (marks.read == 0) u.read(.{ .x = a.x, .y = a.y, .w = w, .h = m.row }, marks.read_shows);
+ u.text(.mono_bold, size, a.x, rows.baseline(a.y), name, rows.ink(rows.regionOf(a.rec.moment(moment), touch.addr)));
+ const v = (Recording.word(shown, touch.addr, 8) catch null) orelse return .{ .drawn = true, .end = a.x + w };
+ const x = u.arrow(a.x + w, a.y + m.row / 2, if (rows.regionOf(shown, v) != .none) .points else .holds);
+ return .{ .drawn = true, .end = rows.chain(u, x, a.y, shown, v, a.room, .{ .first = if (marks.changed == 1) marks.changed_heat else .none }).end };
+ }
+
/// A direct call's callee, bright.
fn callee(a: Say, to: u64, moment: u64) Disasm.Note {
return .{ .drawn = true, .end = a.word(a.x, a.functionName(to, moment), t.bright) };
@@ -645,6 +818,39 @@ const Say = struct {
}
};
+/// The pc of the instruction a step's touch leaves from (`cursors.stackTouch`), kept for the step.
+var touch_memo: struct { rec: ?*const Recording = null, from: u64 = 0, to: u64 = 0, pc: ?u64 = null } = .{};
+
+fn touchPc(rec: *const Recording, now: cursors.Now) ?u64 {
+ if (touch_memo.rec != rec or touch_memo.from != now.stops[1] or touch_memo.to != now.stops[0]) {
+ const tt = cursors.stackTouch(rec, now.stops[1], now.stops[0]);
+ touch_memo = .{ .rec = rec, .from = now.stops[1], .to = now.stops[0], .pc = if (tt) |x| rec.register(x.at, "rip") else null };
+ }
+ return touch_memo.pc;
+}
+
+/// A stack word's name under `tied`, its role at moment `i` (a local's
+/// name, primary; `saved rbp`, `argv[0]`, dim), for an access of `len`
+/// bytes at `addr`; null where it has none, or where the local the word is
+/// named by does not hold those bytes (two locals in a word: never a guess).
+fn wordName(u: ui.Ui, rec: *const Recording, i: u64, addr: u64, len: u64) ?struct { name: []const u8, local: bool } {
+ const regs = rec.registers[@min(i, rec.end())];
+ var one: [1]zt.telescope.Slot = undefined;
+ const word = addr & ~@as(u64, 7);
+ const got = zt.telescope.slots(rec.moment(i), &regs, word, &one) catch return null;
+ if (got.len == 0) return null;
+ return switch (got[0].role) {
+ .none, .return_address => null,
+ // Of the locals that share the word (`Slot.also`), the one the access is to.
+ .local => |first| for ([_]?zt.telescope.Local{first} ++ got[0].also) |maybe| {
+ const l = maybe orelse continue;
+ const at = word -% @as(u64, @bitCast(@as(i64, l.offset)));
+ if (if (l.size > 0) addr >= at and addr + len <= at + l.size else addr == at) break .{ .name = l.name, .local = true };
+ } else null,
+ else => |role| .{ .name = context.Stack.roleName(u, role, (Recording.word(rec.moment(i), addr & ~@as(u64, 7), 8) catch null) orelse 1).name, .local = false },
+ };
+}
+
/// How many rows of arguments hang under `row` while it is current at
/// `moment`: a direct call's, a `syscall`'s; 0 for anything else.
pub fn hanging(u: ui.Ui, rec: *const Recording, row: Insn, moment: u64) usize {