summaryrefslogtreecommitdiff
path: root/anim/src/registers.zig
diff options
context:
space:
mode:
Diffstat (limited to 'anim/src/registers.zig')
-rw-r--r--anim/src/registers.zig117
1 files changed, 117 insertions, 0 deletions
diff --git a/anim/src/registers.zig b/anim/src/registers.zig
new file mode 100644
index 0000000..5f08d6f
--- /dev/null
+++ b/anim/src/registers.zig
@@ -0,0 +1,117 @@
+//! The variables of the innermost call, as a table: names, types as C
+//! spells them, values, each struck when memory is (a variable is
+//! memory). The registers are `context.Registers`.
+//! Reads `Moment.variables`, `Moment.read`.
+const std = @import("std");
+const sk = @import("sketch");
+const anim = @import("anim");
+const ui = @import("ui.zig");
+const cursors = @import("cursor.zig");
+const Cursor = cursors.Cursor;
+const Recording = @import("Recording.zig");
+const objects = @import("objects.zig");
+const theme = ui.theme;
+const Rect = ui.Rect;
+const size = theme.size.mono.dense;
+const m = struct {
+ const row_h = theme.metric.dense.row_h;
+ const name_x = theme.metric.panel.pad_x;
+ const value_pad = 4;
+};
+/// Of two things one step does the same to, the second follows the first.
+const second = theme.time.flight.second;
+
+pub const Locals = struct {
+ cursor: Cursor,
+ box: Rect,
+ /// Names to show, in order; empty for every variable of the innermost
+ /// call, then the globals.
+ names: []const []const u8 = &.{},
+ focus: bool = false,
+
+ pub fn draw(l: *const Locals, f: *sk.Frame, at: anim.Placed) void {
+ const u: ui.Ui = .{ .f = f, .at = at };
+ const now = l.cursor.now(at);
+ const rec = l.cursor.rec;
+ const moment = now.moment();
+ const calls = rec.stack(f.scratch, now.stops[0]);
+ const where = if (calls.len > 0) u.print("{s}()", .{rec.name(calls[calls.len - 1].function)}) else "";
+ const body = u.panel(l.box.w, l.box.h, "locals", .{ .focus = l.focus, .note = where });
+ const all = moment.variables(f.scratch) catch return;
+ const depth: u32 = @intCast(calls.len);
+
+ var y = body.y + 8;
+ const room: usize = @intFromFloat(@floor((body.h - 16) / m.row_h));
+ var shown: usize = 0;
+ var struck: f32 = 0;
+ if (l.names.len > 0) {
+ for (l.names) |name| {
+ const v = rec.variable(f.scratch, moment, name) orelse continue;
+ if (shown < room) l.row(u, now, v, y, &struck);
+ y += m.row_h;
+ shown += 1;
+ }
+ return;
+ }
+ // The innermost call's, then the globals.
+ for ([_]u32{ depth, 0 }) |want| for (all) |v| {
+ if (v.depth != want or rec.name(v.name).len == 0 or shown == room) continue;
+ l.row(u, now, v, y, &struck);
+ y += m.row_h;
+ shown += 1;
+ };
+ }
+
+ fn row(l: *const Locals, u: ui.Ui, now: cursors.Now, v: Recording.Variable, y: f32, struck: *f32) void {
+ const rec = l.cursor.rec;
+ const base = theme.baseline(y, m.row_h, size);
+ const adv = ui.advance(size);
+ const small = size;
+ const name = rec.name(v.name);
+ u.text(.mono, size, m.name_x, base, name, theme.color.text.primary);
+ const type_x = m.name_x + @max(5, @as(f32, @floatFromInt(name.len)) + 2) * adv;
+ const kind = if (Recording.types.parse(u.f.scratch, rec.strings.get(v.type) orelse "")) |t| objects.spell(u.f.scratch, t) else |_| "";
+ u.text(.mono, small, type_x, base, kind[0..@min(kind.len, 8)], theme.color.text.faint);
+
+ const value_x = type_x + 9 * ui.advance(small);
+ // A variable is memory: it changes when memory does, the second
+ // one a step wrote a little after the first.
+ const age = now.writeAge(v.addr, v.size);
+ const delay = if (age == 0) struck.* * second else 0;
+ if (age == 0) struck.* += 1;
+ const landed = now.landed(.memory, delay);
+ // Not yet assigned in this call: its bytes are not its value.
+ const text = (if (rec.assigned(v, if (landed) now.stops[0] else now.stops[1])) describe(u, rec, if (landed) now.moment() else now.before(), v) else null) orelse {
+ _ = u.written(.{ .x = value_x - m.value_pad, .y = y + 2, .w = 3 * adv + 2 * m.value_pad, .h = m.row_h - 4 }, now.written(v.addr, v.size, .memory, delay), null);
+ return u.unknown(.{ .x = value_x, .y = y, .w = 3 * adv, .h = m.row_h });
+ };
+ const cell: Rect = .{ .x = value_x - m.value_pad, .y = y + 2, .w = @as(f32, @floatFromInt(text.len)) * adv + 2 * m.value_pad, .h = m.row_h - 4 };
+ const ink = u.written(cell, now.written(v.addr, v.size, .memory, delay), null);
+ if (now.wasRead(v.addr, v.size) and age != 0) u.readMark(cell, now.reading(.memory, 0));
+ u.text(ink.face, size, value_x, base, text, ink.ink);
+ }
+};
+
+/// A variable's value as its type says to read it: a number in decimal, a
+/// pointer in hex, an array as its first elements.
+fn describe(u: ui.Ui, rec: *const Recording, moment: Recording.Moment, v: Recording.Variable) ?[]const u8 {
+ const t = rec.typeOf(u.f.scratch, v.type) orelse return plain(u, moment, v.addr, v.size, false);
+ return switch (t) {
+ .scalar => |s| plain(u, moment, v.addr, v.size, s.kind == .int),
+ .pointer => blk: {
+ const to = (Recording.word(moment, v.addr, 8) catch return null) orelse return null;
+ var buf: [20]u8 = undefined;
+ break :blk if (to == 0) "NULL" else u.print("{s}", .{ui.grouped(&buf, to)});
+ },
+ .array => |a| u.print("[{d}]", .{a.len}),
+ else => null,
+ };
+}
+
+fn plain(u: ui.Ui, moment: Recording.Moment, addr: u64, bytes: u64, signed: bool) ?[]const u8 {
+ if (bytes == 0 or bytes > 8) return null;
+ const raw = (Recording.word(moment, addr, @intCast(bytes)) catch return null) orelse return null;
+ if (!signed) return u.print("{d}", .{raw});
+ const shift: u6 = @intCast(64 - 8 * bytes);
+ return u.print("{d}", .{@as(i64, @bitCast(raw << shift)) >> shift});
+}