1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
//! The catalogue's rows (design/README.md, "2. Rows"): every row is 24
//! high and one line, made of atoms, and none wraps: what does not fit is
//! cut at its far end and says so. A row draws what it is given, in the
//! states it is given; the panels (`context.zig`, `disasm.zig`,
//! `source.zig`, `hexdump.zig`) read the recording and the cursor and say
//! which. x is from the panel's left edge, y is the row's top.
const std = @import("std");
const sk = @import("sketch");
const zt = @import("ztrace");
const ui = @import("ui.zig");
const Recording = @import("Recording.zig");
const theme = ui.theme;
const Rect = ui.Rect;
const t = theme.color.text;
const m = theme.metric.context;
const size = theme.size.mono.context;
const adv = size * 3 / 5;
const row_h = m.row;
/// What an address into `region` is inked in.
pub fn ink(region: zt.telescope.Region) u24 {
const c = theme.color.region;
return switch (region) {
.none => c.number,
.code => c.code_,
// Writable and executable: its region's ink (data's, here) and an underline.
.rwx => c.data,
.stack => c.stack,
.heap => c.heap,
.data => c.data,
.rodata => c.rodata,
};
}
pub fn regionOf(moment: Recording.Moment, value: u64) zt.telescope.Region {
return zt.telescope.region(moment, value) catch .none;
}
pub fn baseline(y: f32) f32 {
return theme.baseline(y, row_h, size);
}
/// The marks on a chain's hops.
pub const Marks = struct {
/// The first hop: a register (or a word) written: armed, struck, held.
first: ui.Heat = .none,
/// A later hop, the memory under a pointer that did not change: its
/// tick sits before that hop alone.
changed: ?usize = null,
changed_heat: ui.Heat = .ember,
/// A hop read, and how much its frame shows.
read: ?usize = null,
read_shows: f32 = 0,
/// `extend`: seconds since the first hop was struck; the hops after it
/// are followed again 4 f apart, each fading in over 6 f.
extend: ?f32 = null,
/// The first hop's `lead` (a column of addresses).
lead: usize = 0,
/// Where the first hop's tick goes, left of its digits: 9 after a
/// name, 5 after an arrow.
tick: f32 = 9,
};
/// Where a chain's hops were drawn: x and width of each one's digits.
pub const Hops = struct {
x: [6]f32 = undefined,
w: [6]f32 = undefined,
n: usize = 0,
end: f32 = 0,
};
/// `chain`: a telescope, `address (points address)* (holds value)?`, of
/// `value` at `moment`, from `x` on the row at `y`, cut where `room`
/// ends. A code address ends it with its name, a string in quotes; depth
/// is the recording's. Cut at its far end: a string is shortened first,
/// then the chain ends early in `more`; nothing is taken from the middle.
pub fn chain(u: ui.Ui, x: f32, y: f32, moment: Recording.Moment, value: u64, room: f32, marks: Marks) Hops {
var links_buf: [6]zt.telescope.Link = undefined;
var text_buf: [48]u8 = undefined;
const links = zt.telescope.chase(moment, value, &links_buf, &text_buf) catch return .{};
const base = baseline(y);
const mid = y + row_h / 2;
var hops: Hops = .{};
var at = x;
for (links, 0..) |link, i| {
const pointer = link.region != .none;
const shown = ui.Value.of(link.value, pointer);
const w = shown.width();
// The hops after a struck first one are followed again, one at a time.
const v = if (i > 0 and marks.extend != null) u.faded(std.math.clamp((marks.extend.? - @as(f32, @floatFromInt(i - 1)) * theme.time.unroll) / (6.0 / 60.0), 0, 1)) else u;
if (i > 0) {
const named: f32 = if (link.region == .code and link.text.len > 0 and !link.string) 10 + @as(f32, @floatFromInt(link.text.len)) * adv else 0;
// Room for it, and for saying there is more.
if (pointer and at + m.arrow_w + w + named + 40 > room) {
_ = v.arrow(at, mid, .more);
break;
}
if (!pointer and at + m.arrow_w + w > room) {
// The value it ends on has no room: it says so.
if (at + 2 * adv <= room) v.text(.mono, size, at + 4, base, "\u{2026}", t.faint);
break;
}
at = v.arrow(at, mid, if (pointer) .points else .holds);
}
const marked: Rect = .{ .x = at, .y = y, .w = w, .h = row_h };
var face_ink: ui.Ink = .{ .ink = ui.rgb(if (pointer) ink(link.region) else t.primary) };
if (i == 0) {
if (v.written(marked, marks.first, marks.tick)) |struck| face_ink = struck;
} else if (marks.changed == i) {
if (v.written(marked, marks.changed_heat, 5)) |struck| face_ink = struck;
}
if (marks.read == i) v.read(marked, marks.read_shows);
if (pointer) {
_ = v.address(at, base, link.value, .{ .ink = face_ink.ink, .face = face_ink.face, .lead = if (i == 0) marks.lead else 0, .rwx = link.region == .rwx });
} else _ = v.value(at, base, link.value, false, face_ink);
if (hops.n < hops.x.len) {
hops.x[hops.n] = at;
hops.w[hops.n] = w;
hops.n += 1;
}
at += w;
if (!pointer and shown.decimal) at += 8 + @as(f32, @floatFromInt(std.fmt.count("{d}", .{link.value}))) * adv;
if (link.text.len == 0) {
// Still a pointer where the recording stopped following.
if (i + 1 == links.len and pointer and link.region != .code and link.region != .rwx and at + m.arrow_w + 24 <= room) at = v.arrow(at, mid, .more);
continue;
}
if (link.string) {
// What a pointer leads to, when it is text: quoted, cut to the room
// there is (never under six characters), with `…` and no closing quote.
const fits: usize = @intFromFloat(@max((room - at - m.arrow_w) / adv, 0));
if (fits < m.string_min + 2) break;
at = v.arrow(at, mid, .holds);
const whole = link.text.len + 2 <= fits;
const text = if (whole) u.print("\"{s}\"", .{link.text}) else u.print("\"{s}\u{2026}", .{link.text[0 .. fits - 2]});
v.text(.mono, size, at, base, text, theme.color.string);
at += @as(f32, @floatFromInt(std.unicode.utf8CountCodepoints(text) catch text.len)) * adv;
} else {
// A code address's name.
const name = ui.symbol(u, link.text);
const fits: usize = @intFromFloat(@max((room - at - 10) / adv, 0));
if (fits >= 3) {
v.text(.mono, size, at + 10, base, name[0..@min(name.len, fits)], t.primary);
at += 10 + @as(f32, @floatFromInt(@min(name.len, fits))) * adv;
}
}
break;
}
hops.end = at;
return hops;
}
/// The cell a mark goes on for `value` drawn at `x` in a row at `y`.
pub fn cell(x: f32, y: f32, moment: Recording.Moment, value: u64) Rect {
return .{ .x = x, .y = y, .w = ui.Value.of(value, regionOf(moment, value) != .none).width(), .h = row_h };
}
/// `reg`: a register's name (dim) 12 in, its chain from 72, room to 12
/// from the edge `w`.
pub fn reg(u: ui.Ui, y: f32, w: f32, name: []const u8, moment: Recording.Moment, value: u64, marks: Marks) Hops {
u.text(.mono, size, m.side, baseline(y), name, t.dim);
return chain(u, m.chain_x, y, moment, value, w - m.side, marks);
}
/// The flags, in rip's row: their names, the value.
pub const Flag = struct { name: []const u8, bit: u6 };
pub const flags = [_]Flag{ .{ .name = "CF", .bit = 0 }, .{ .name = "PF", .bit = 2 }, .{ .name = "AF", .bit = 4 }, .{ .name = "ZF", .bit = 6 }, .{ .name = "SF", .bit = 7 }, .{ .name = "IF", .bit = 9 }, .{ .name = "DF", .bit = 10 }, .{ .name = "OF", .bit = 11 } };
/// `reg` rip: gold plaque on the name, ember across, the address in
/// code's ink and its name; never struck. At its right end the `flags`,
/// eight names on a pitch of 42: set, upper case on a chip, bold, bright;
/// clear, lower case, dim; changed, a tick (`changed`: a bit per flag, and
/// how held). The value of eflags is not drawn: the names are the value.
pub fn rip(u: ui.Ui, y: f32, w: f32, moment: Recording.Moment, pc: u64, eflags: u64, changed: u64, held: f32, fire: f32) void {
const gold = theme.mark(.current);
u.current(.{ .x = theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = row_h }, theme.metric.plaque.rip, fire);
u.text(.mono_bold, size, m.side, baseline(y), "rip", gold.on_solid);
const fl = theme.metric.flags;
var x = w - m.side - @as(f32, flags.len) * fl.pitch + 10;
_ = chain(u, m.chain_x, y, moment, pc, x - 16, .{});
for (flags) |flag| {
const set = eflags >> flag.bit & 1 != 0;
if (changed >> flag.bit & 1 != 0 and held > 0) u.fill(.{ .x = x - 7, .y = y + 3, .w = m.tick, .h = row_h - 6 }, ui.mix(gold.ember, theme.mark(.write).solid, held));
if (set) {
u.chip(.{ .x = x, .y = y + 2, .w = fl.chip[0], .h = fl.chip[1] });
u.text(.mono_bold, size, x + 4, baseline(y), flag.name, t.bright);
} else u.text(.mono, size, x + 4, baseline(y), u.print("{c}{c}", .{ std.ascii.toLower(flag.name[0]), std.ascii.toLower(flag.name[1]) }), ui.mix(t.dim, gold.ember, 0.2));
x += fl.pitch;
}
}
/// `slot`: a word of the stack: a lane 112 wide for `row` pins (`pins`,
/// the registers that hold its address), then its chain from 122,
/// beginning with the slot's own address in stack ink with `lead`, room
/// to 136 from the right edge `w` (the `call` spans').
pub fn slot(u: ui.Ui, y: f32, w: f32, moment: Recording.Moment, addr: u64, pins: []const []const u8, pin_shows: f32, marks: Marks) Hops {
const s = theme.metric.slot;
if (pins.len > 0 and pin_shows > 0) u.faded(pin_shows).pinRow(s.lane, y, pins, theme.color.region.stack);
var lead = marks;
lead.first = .none;
const base = baseline(y);
const end = u.address(s.chain, base, addr, .{ .ink = ui.rgb(theme.color.region.stack), .lead = marks.lead });
const held = (Recording.word(moment, addr, 8) catch null) orelse {
_ = u.arrow(end, y + row_h / 2, .points);
u.unknown(.{ .x = end + m.arrow_w, .y = y, .w = 24, .h = row_h });
return .{ .end = end + m.arrow_w + 24 };
};
// The word there: a pointer, or what the slot holds.
const pointer = regionOf(moment, held) != .none;
const at = u.arrow(end, y + row_h / 2, if (pointer) .points else .holds);
lead.lead = 0;
lead.first = marks.first;
lead.tick = 5;
return chain(u, at, y, moment, held, w - s.room, lead);
}
/// `call` `span`: one frame of the call chain on the stack, a bracket 14
/// in from the panel's right edge `w` down the slots `y0 .. y1` that are
/// the frame, and the function's name left of it (Go Medium 18). `newest`
/// gold and bright; older, rule and dim. `open`: it runs past the panel.
pub fn span(u: ui.Ui, w: f32, y0: f32, y1: f32, name: []const u8, newest: bool, open: bool) void {
const x = w - theme.metric.slot.span;
const c = if (newest) theme.mark(.current).solid else theme.color.rule;
u.bracket(x, y0 + 3, y1 - @as(f32, if (open) 0 else 3), c, theme.metric.bracket.quiet, -1, open);
const micro = theme.size.sans.micro;
const mid = (y0 + y1) / 2 + 6;
u.text(.sans_medium, micro, x - 14 - u.width(.sans_medium, micro, name), mid, name, if (newest) t.bright else t.dim);
}
/// `call` `row`: `#n` (a gold plaque on the newest), the name and offset
/// in primary at 60, file and line in dim at 276.
pub fn callRow(u: ui.Ui, y: f32, w: f32, n: usize, name: []const u8, where: []const u8, newest: bool) void {
const gold = theme.mark(.current);
if (newest) u.current(.{ .x = theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = row_h }, 44, 0);
u.text(if (newest) .mono_bold else .mono, size, m.side, baseline(y), u.print("#{d}", .{n}), if (newest) gold.on_solid else t.faint);
u.text(.mono, size, 60, baseline(y), name[0..@min(name.len, 17)], t.primary);
u.text(.mono, size, 276, baseline(y), where, t.dim);
}
/// `line`: a line of source, its number ranged right in three characters
/// (faint), the text from 64 in the four code inks, 67 characters and
/// then a faint `…`. `current`: a gold plaque 50 wide on the number (its
/// edges out `fire`), ember across; `marked`: the number as on the plaque,
/// which its panel draws where the mark is (it slides). `trail`: the bar
/// (0 newest).
pub fn line(u: ui.Ui, y: f32, w: f32, number: u32, text: []const u8, styles: []const u8, state: union(enum) { plain, current: f32, marked, trail: usize }) void {
const gold = theme.mark(.current);
switch (state) {
.current => |fire| u.current(.{ .x = theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = row_h }, theme.metric.plaque.line, fire),
.trail => |k| u.trail(theme.stroke.rule, y, k),
.plain, .marked => {},
}
const on = state == .current or state == .marked;
const num = u.print("{d}", .{number});
u.text(if (on) .mono_bold else .mono, size, m.line_number_end - @as(f32, @floatFromInt(num.len)) * adv, baseline(y), num, if (on) gold.on_solid else t.faint);
const limit: usize = m.source_chars;
const end = @min(text.len, limit);
var i: usize = 0;
while (i < end) {
var j = i + 1;
while (j < end and styles[j] == styles[i]) j += 1;
if (std.mem.indexOfNone(u8, text[i..j], " ") != null) {
const class = styles[i];
u.text(codeFace(class), size, m.line_text + @as(f32, @floatFromInt(i)) * adv, baseline(y), text[i..j], codeInk(class));
}
i = j;
}
if (text.len > limit) u.text(.mono, size, m.line_text + @as(f32, @floatFromInt(limit)) * adv, baseline(y), "\u{2026}", t.faint);
}
/// pardes's classes: none, keyword, string, number, comment.
fn codeInk(class: u8) u24 {
const c = theme.color.code;
return switch (class) {
1 => c.keyword,
2, 3 => c.literal,
4 => c.comment,
else => c.plain,
};
}
fn codeFace(class: u8) ui.Face {
return switch (class) {
1 => .mono_bold,
4 => .mono_italic,
else => .mono,
};
}
/// A run of a `bytes` row: what the bytes are, from the types of what the
/// program's pointers reach.
pub const Run = struct {
first: u8,
len: u8,
kind: enum { number, pointer, allocator, unread },
/// Its mark: struck (heat), read (shows), held is in the heat.
heat: ui.Heat = .none,
read: f32 = 0,
};
/// `bytes`: a hexdump row of sixteen bytes at `addr`: a bracket at 7
/// (`bracket`: quiet on a row that holds a drawn thing), the address in
/// region ink with `lead` at 18 (at 60% on a row that holds no drawn
/// thing), the bytes from 132 on a pitch of 30, 12 more after the
/// eighth; a field's bytes on raised paper in their ink (a pointer's in
/// the region it points into), an allocator's dim with no paper, never
/// read two dots a byte; a word's padding bytes faint. `reading` is drawn
/// by the caller at 640.
pub fn bytes(u: ui.Ui, y: f32, addr: u64, values: *const [16]u8, known: *const [16]bool, runs: []const Run, region: u24, lead: usize, holds_drawn: bool, moment: Recording.Moment) void {
const b = theme.metric.bytes;
const base = baseline(y);
_ = u.address(b.address, base, addr, .{ .ink = if (holds_drawn) ui.rgb(region) else ui.mix(region, theme.color.page, theme.metric.context.lead_alpha), .lead = lead });
if (holds_drawn) u.bracket(b.bracket, y + 3, y + row_h - 3, ui.mix(region, theme.color.page, theme.metric.context.lead_alpha), theme.metric.bracket.quiet, 1, false);
for (runs) |run| {
const x0 = byteX(run.first) - 4;
const x1 = byteX(run.first + run.len - 1) + 28;
const r: Rect = .{ .x = x0, .y = y + 1, .w = x1 - x0, .h = row_h - 2 };
const field = run.kind == .number or run.kind == .pointer;
if (field) u.fill(r, theme.color.raised);
// Marks per run, never per byte: struck (the paper solid), read (a
// frame 3 out), held (a tick in the gap at the run's left).
var face: ui.Face = .mono;
var struck_ink: ?ui.Color = null;
const h = run.heat;
const held = h.level * (1 - (if (h.level >= 1) h.solid * h.in else 0));
if (held > 0) u.fill(.{ .x = x0 - 7, .y = y + 3, .w = m.tick, .h = row_h - 6 }, ui.mix(theme.color.page, theme.mark(.write).solid, held));
if (h.arm > 0) u.faded(h.arm).frame(.{ .x = x0 - 3, .y = y - 1, .w = x1 - x0 + 6, .h = row_h + 2 }, theme.mark(.write).solid, 2);
const solid = if (h.level >= 1) h.solid * h.in else 0;
if (solid > 0) {
u.fill(.{ .x = r.x - h.out, .y = r.y - h.out, .w = r.w + 2 * h.out, .h = r.h + 2 * h.out }, ui.mix(theme.color.raised, theme.mark(.write).solid, solid));
if (solid >= 0.5) {
face = .mono_bold;
struck_ink = ui.rgb(theme.mark(.write).on_solid);
}
}
if (run.read > 0) u.faded(run.read).frame(.{ .x = x0 - 3, .y = y - 1, .w = x1 - x0 + 6, .h = row_h + 2 }, theme.mark(.read).solid, 2);
// A little-endian word: the bytes above its highest non-zero one are its padding.
var top: usize = 0;
for (0..run.len) |j| if (values[run.first + j] != 0) {
top = j;
};
const word: u64 = if (run.len == 8) std.mem.readInt(u64, values[run.first..][0..8], .little) else 0;
const run_ink: ui.Color = switch (run.kind) {
.number => ui.rgb(t.primary),
.pointer => ui.rgb(if (run.len == 8) ink(regionOf(moment, word)) else t.primary),
.allocator => ui.rgb(t.dim),
.unread => ui.rgb(t.faint),
};
for (0..run.len) |j| {
const i = run.first + j;
const x = byteX(@intCast(i));
if (run.kind == .unread or !known[i]) {
u.fill(.{ .x = x + 5, .y = y + 11, .w = 4, .h = 4 }, t.faint);
u.fill(.{ .x = x + 15, .y = y + 11, .w = 4, .h = 4 }, t.faint);
continue;
}
const c = struck_ink orelse if (j <= top) run_ink else ui.mix(run_ink, theme.color.page, theme.metric.context.pad_alpha);
u.text(face, size, x, base, u.print("{x:0>2}", .{values[i]}), c);
}
}
}
/// Where byte `i` of a `bytes` row is.
pub fn byteX(i: u8) f32 {
const b = theme.metric.bytes;
return b.first + @as(f32, @floatFromInt(i)) * b.pitch + @as(f32, if (i >= 8) b.middle else 0);
}
|