diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-11 12:08:00 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-11 12:49:15 -0300 |
| commit | 4c7dcadeddaa50fb52fd44a2cc71df1f570e092a (patch) | |
| tree | f28491261b55b81638b4bb1d1091b80cec407abe /design/mock/intro.py | |
| parent | 92aea44cb7dd0a7995327d2f276563b8058afbc9 (diff) | |
| download | computeranim-4c7dcadeddaa50fb52fd44a2cc71df1f570e092a.tar.gz computeranim-4c7dcadeddaa50fb52fd44a2cc71df1f570e092a.zip | |
design: the address space said with less; a word read low byte first; aligned in two forms
From the user. The address space is a slim bar to true scale with its
two ends written, each cluster enlarged once beside it, and every band
carrying the addresses where it starts and stops and its size; no
rulers, no factors. The stack's bytes on screen are tied to their place
in it. regroup is drawn: a word's eight bytes fly out low byte first and
land reversed as its sixteen digits. aligned has two forms a scene asks
for by name: interleaved, a line as the head of its instructions, and
columns, the source beside them with each line as tall as its run.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'design/mock/intro.py')
| -rw-r--r-- | design/mock/intro.py | 739 |
1 files changed, 501 insertions, 238 deletions
diff --git a/design/mock/intro.py b/design/mock/intro.py index a0684fd..87d22bf 100644 --- a/design/mock/intro.py +++ b/design/mock/intro.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 """The introduction: the frame built from its bytes up, on the recording of targets/list that mocks 9 to 21 use -(list-dbg.txt: the stack from rsp, the heap, the registers; the code's bytes from objdump of the binary). Four -instants of the build, each as the camera frames it. +(list-dbg.txt: the stack from rsp, the heap, the registers; the code's bytes from objdump of the binary; the +mappings from `ztrace show` of a fresh recording of the same build). Instants of the build, each as the camera +frames it, and the blocks it introduces: `segments` (34, its treatments 35), `aligned` in its two forms (33, 36), +`regroup` walked (37). - python3 -B intro.py # 29-intro-bytes, 30-intro-words, 31-intro-stack, 32-intro-code (.svg) + python3 -B intro.py # 29-intro-bytes ... 37-regroup (.svg) """ import gen import dense as D @@ -12,262 +14,274 @@ from context import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, GOLD, KW, C A, S, ROW, TAG, PAD, MONO, BOLD, b, ph, mix, Svg) RSP = D.RSP -MAPS = [(0x1000000, 0x1001000, "list", "headers, read-only", VELLUM), (0x1001000, 0x1002000, "list", "code", CODE), - (0x1002000, 0x1003000, "list", "read-only data", VELLUM), (0x1003000, 0x1004000, "list", "data", DATA), - (0x1004000, 0x1025000, "heap", "malloc's", HEAP), None, - (0x7ffff7c00000, 0x7ffff7c24000, "libc.so.6", "read-only", VELLUM), - (0x7ffff7c24000, 0x7ffff7d9f000, "libc.so.6", "code", CODE), - (0x7ffff7d9f000, 0x7ffff7e15000, "libc.so.6", "read-only data", VELLUM), - (0x7ffff7e15000, 0x7ffff7e23000, "libc.so.6", "data", DATA), (0x7ffff7fbc000, 0x7ffff7fbe000, "[vdso]", "code", CODE), - (0x7ffff7fbf000, 0x7ffff7fec000, "ld.so", "code", CODE), (0x7ffff7ffb000, 0x7ffff7fff000, "ld.so", "data", DATA), - None, (0x7ffffffde000, 0x7ffffffff000, "[stack]", "the stack", STACK)] -CODE_BYTES = [(0x1001920, "48 89 c1"), (0x1001923, "48 89 f8"), (0x1001926, "48 8b 7f 08"), (0x100192a, "48 89 48 08"), - (0x100192e, "48 85 ff"), (0x1001931, "75 ed")] +END = 0x800000000000 # the user half's end: 7fffffffffff + 1, 128 TiB + +# The mappings at the stop, low to high, as `ztrace show` prints them (range, permissions, file); the kernel's +# [vsyscall] page is above the user half and not drawn. A fresh recording of the same build under `env -i +# setarch -R` (the kernel now splits [vvar] in two; nothing else moved). +MAPS = [(0x1000000, 0x1001000, "r--", "list"), (0x1001000, 0x1002000, "r-x", "list"), + (0x1002000, 0x1003000, "r--", "list"), (0x1003000, 0x1004000, "rw-", "list"), + (0x1004000, 0x1025000, "rw-", "[heap]"), + (0x7ffff7c00000, 0x7ffff7c24000, "r--", "libc.so.6"), (0x7ffff7c24000, 0x7ffff7d9f000, "r-x", "libc.so.6"), + (0x7ffff7d9f000, 0x7ffff7e15000, "r--", "libc.so.6"), (0x7ffff7e15000, 0x7ffff7e19000, "r--", "libc.so.6"), + (0x7ffff7e19000, 0x7ffff7e1b000, "rw-", "libc.so.6"), (0x7ffff7e1b000, 0x7ffff7e23000, "rw-", ""), + (0x7ffff7f70000, 0x7ffff7f75000, "rw-", ""), (0x7ffff7f75000, 0x7ffff7fb6000, "r--", "ld.so.cache"), + (0x7ffff7fb6000, 0x7ffff7fba000, "r--", "[vvar]"), (0x7ffff7fba000, 0x7ffff7fbc000, "r--", "[vvar_vclock]"), + (0x7ffff7fbc000, 0x7ffff7fbe000, "r-x", "[vdso]"), (0x7ffff7fbe000, 0x7ffff7fbf000, "r--", "ld.so"), + (0x7ffff7fbf000, 0x7ffff7fec000, "r-x", "ld.so"), (0x7ffff7fec000, 0x7ffff7ffb000, "r--", "ld.so"), + (0x7ffff7ffb000, 0x7ffff7ffd000, "r--", "ld.so"), (0x7ffff7ffd000, 0x7ffff7ffe000, "rw-", "ld.so"), + (0x7ffff7ffe000, 0x7ffff7fff000, "rw-", ""), (0x7ffffffde000, 0x7ffffffff000, "rw-", "[stack]")] +INK = {"r-x": CODE, "r--": VELLUM, "rw-": DATA} + + +def ink_of(a): + """The ink of the mapping a is in (None: none).""" + for lo, hi, perm, name in MAPS: + if lo <= a < hi: + return STACK if name == "[stack]" else HEAP if name == "[heap]" else INK[perm] + return None + + +# The three clusters: what each enlargement spans. +CLUSTERS = {"stack": (0x7ffffffde000, 0x7ffffffff000), "libraries": (0x7ffff7c00000, 0x7ffff7fff000), + "program": (0x1000000, 0x1025000)} + + +def runs_of(lo, hi): + """The bands an enlargement names: its mappings, touching ones of one file and one permission as one (a + file's anonymous rest, its bss, with the file's rw-). Low to high: (lo, hi, perm, file).""" + out = [] + for lo_, hi_, perm, name in MAPS: + if hi_ <= lo or lo_ >= hi: + continue + if out and out[-1][1] == lo_ and out[-1][2] == perm and (out[-1][3] == name or not name and out[-1][3]): + out[-1] = (out[-1][0], hi_, perm, out[-1][3]) + else: + out.append((lo_, hi_, perm, name)) + return out + + +def files_of(lo, hi): + """The bands named by file: a file's mappings as one (its anonymous rest with it), the vDSO with its data; its + permissions when they are one for the whole file (the stack, the heap), else None. Low to high.""" + out = [] + for lo_, hi_, perm, name in MAPS: + if hi_ <= lo or lo_ >= hi: + continue + name = "[vdso]" if name.startswith("[vvar") else name + if out and out[-1][1] == lo_ and (out[-1][3] == name or not name and out[-1][3]): + out[-1] = (out[-1][0], hi_, perm if out[-1][2] == perm else None, out[-1][3]) + else: + out.append((lo_, hi_, perm, name)) + return out def size(n): + """A size in its largest whole unit: 132 KiB, 1.5 MiB, 128 TiB.""" for unit, k in (("EiB", 1 << 60), ("PiB", 1 << 50), ("TiB", 1 << 40), ("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10)): if n >= k: v = n / k - return ("%d %s" if v == int(v) or v >= 10 else "%.1f %s") % (v, unit) + return ("%d %s" % (round(v), unit)) if v >= 10 or round(v, 1) == round(v) else "%.1f %s" % (v, unit) return "%d B" % n -def segment_rows(g, x, y, w, hot=()): - """`segments`: the process's address space as its mappings, low addresses on top, a row each: a bar in the - region's ink, the first address, what it is, its size; a hole between them one row, how big it is. Rows, not - a scale: the stack and the code are 127 TiB apart. hot: the mappings framed as read (a word just inked).""" - rows = len(MAPS) - y0 = C.panel(g, x, y, w, rows, "the address space", note="the recording's mappings, low on top") +def spread(ideal, pitch, lo, hi): + """Rows wanting to be at `ideal` (in order, top down), at least `pitch` apart, each as near its want as the + others let it (adjacent rows pooled at their mean), the whole kept inside lo..hi.""" + groups = [] # [first index, count, top] + for i, y in enumerate(ideal): + groups.append([i, 1, y]) + while len(groups) > 1 and groups[-2][2] + groups[-2][1] * pitch > groups[-1][2]: + b_, a_ = groups.pop(), groups.pop() + n = a_[1] + b_[1] + top = sum(ideal[a_[0]:a_[0] + n]) / n - (n - 1) * pitch / 2 + groups.append([a_[0], n, top]) + out = [g_[2] + k * pitch for g_ in groups for k in range(g_[1])] + for i in range(len(out) - 1, -1, -1): # then inside the bounds: up from the foot, down from the top + out[i] = min(out[i], hi if i == len(out) - 1 else out[i + 1] - pitch) + for i in range(len(out)): + out[i] = max(out[i], lo if i == 0 else out[i - 1] + pitch) + return out + + +def whole(g, x, y0, y1, w=20, labels=True): + """`segments`, the whole: the user half, 0 to 7fffffffffff, to scale in a slim bar. Every mapping is where it + is, and at this scale that is a 2-unit line at one end or the other. Its ends and its size, nothing else.""" + g.frame(x, y0, w, y1 - y0, DIM, 2) + g.rect(x + 2, y0 + 2, w - 4, 2, STACK) # the stack, its 132 KiB at the very top + g.rect(x + 2, y0 + 4, w - 4, 2, CODE) # the libraries, 128 MiB under it: the same line + g.rect(x + 2, y1 - 6, w - 4, 2, HEAP) # the heap over the program, 16 MiB above 0 + g.rect(x + 2, y1 - 4, w - 4, 2, CODE) + if labels: + C.addr(g, x - 12 - 12 * A, y0 - ROW / 2, END - 1, DIM) + g.t(x - 12, b(y1 - ROW / 2), "0", S, DIM, MONO, "end") + g.t(x - 12, b((y0 + y1) / 2 - ROW / 2), size(END), S, DIM, MONO, "end") + return {"stack": (x + w, y0 + 3), "libraries": (x + w, y0 + 5), "program": (x + w, y1 - 4)} + + +def enlargement(g, x, y, w, h, lo, hi, live=(), perms=(), hot=()): + """`segments`, magnified: lo..hi to a linear scale, high on top. Each mapping a band in its ink a third of the + way over the page; a change of permission is a change of ink, a new mapping of one file a 2-unit page line + where both are 6 tall. Permissions written inside a band 28 tall or more. live: pins (name, address).""" + yof = lambda a: y + h - (a - lo) / (hi - lo) * h + g.frame(x - 2, y - 2, w + 4, h + 4, DIM, 2) last = None - for i, m in enumerate(MAPS): - yy = y0 + i * ROW - if m is None: # a hole: what is not mapped - nxt = next(mm for mm in MAPS[i + 1:] if mm) - hole = nxt[0] - last - g.path(f"M{x + 14},{yy + 8} l6,4 l-6,4 l6,4", FAINT, 2) - g.t(x + 40, b(yy), "nothing mapped · %s" % size(hole), 18, FAINT, "sans") + for lo_, hi_, perm, name in MAPS: + if hi_ <= lo or lo_ >= hi: continue - lo, hi, name, what, ink = m - g.rect(x + 12, yy + 3, 8, ROW - 6, ink) - C.addr(g, x + 32, yy, lo, ink) - g.t(x + 32 + 13 * A, b(yy), name, S, VELLUM, MONO) - g.t(x + 32 + 13 * A + 11 * A, b(yy), what, 18, DIM, "sans") - g.t(x + w - 12, b(yy), size(hi - lo), S, FAINT, MONO, "end") - if lo in hot: - g.frame(x + 6, yy, w - 12, ROW, READ, 2) - last = hi - return y0 + ink = ink_of(lo_) + y0, y1 = yof(hi_), yof(lo_) + g.rect(x, y0, w, max(y1 - y0, 2), mix(ink, PAGE, 0.3)) + if last and last[0] == lo_ and y1 - y0 >= 6 and last[1] >= 6: + g.rect(x, y1 - 1, w, 2, PAGE) + if any(a <= lo_ < z for a, z in perms) and y1 - y0 >= 28: # a mixed file's parts: their permissions inside + g.t(x + w / 2, (y0 + y1) / 2 + 7, perm, S, VELLUM, MONO, "middle") + last = (hi_, y1 - y0) + if any(lo_ <= a < hi_ for a in hot): # `ink`'s echo: the mapping a word points into, framed as read + g.frame(x + 4, y0 - 6, w - 8, max(y1 - y0, 2) + 12, READ, 2) + for name, a in live: # a register's pin, under what it points to, in its band + py = yof(a) + C.pin_up(g, x + 24, py, [None, name], ink_of(a) if name != "rip" else CODE, S) + return yof -# the mappings at the stop, as `ztrace show` prints them (range, permissions, file offset, file); high on top -MAPPINGS = [(0xffffffffff600000, 0xffffffffff601000, "--x", "[vsyscall]"), - (0x7ffffffde000, 0x7ffffffff000, "rw-", "[stack]"), - (0x7ffff7ffe000, 0x7ffff7fff000, "rw-", ""), (0x7ffff7ffd000, 0x7ffff7ffe000, "rw-", "ld.so"), - (0x7ffff7ffb000, 0x7ffff7ffd000, "r--", "ld.so"), (0x7ffff7fec000, 0x7ffff7ffb000, "r--", "ld.so"), - (0x7ffff7fbf000, 0x7ffff7fec000, "r-x", "ld.so"), (0x7ffff7fbe000, 0x7ffff7fbf000, "r--", "ld.so"), - (0x7ffff7fbc000, 0x7ffff7fbe000, "r-x", "[vdso]"), (0x7ffff7fb6000, 0x7ffff7fbc000, "r--", "[vvar]"), - (0x7ffff7f75000, 0x7ffff7fb6000, "r--", "ld.so.cache"), (0x7ffff7f70000, 0x7ffff7f75000, "rw-", ""), - (0x7ffff7e1b000, 0x7ffff7e23000, "rw-", ""), (0x7ffff7e19000, 0x7ffff7e1b000, "rw-", "libc.so.6"), - (0x7ffff7e15000, 0x7ffff7e19000, "r--", "libc.so.6"), (0x7ffff7d9f000, 0x7ffff7e15000, "r--", "libc.so.6"), - (0x7ffff7c24000, 0x7ffff7d9f000, "r-x", "libc.so.6"), (0x7ffff7c00000, 0x7ffff7c24000, "r--", "libc.so.6"), - (0x1004000, 0x1025000, "rw-", "[heap]"), (0x1003000, 0x1004000, "rw-", "list"), - (0x1002000, 0x1003000, "r--", "list"), (0x1001000, 0x1002000, "r-x", "list"), (0x1000000, 0x1001000, "r--", "list")] -INK = {"r-x": CODE, "--x": CODE, "r--": VELLUM, "rw-": DATA} -WHAT = {"r-x": "code", "--x": "code", "r--": "read-only", "rw-": "data"} +def table(g, x, w, yof, runs, col, lo_b, hi_b, pins=None, hot=()): + """The enlargement's names, right of its bar. A band 6 tall or more is named: a row for each of its edges (the + address, in the ink of the band it bounds, a leader from the edge to it) and between them a row for the band + (permissions, size), indented under the addresses; the bands of one file share its name, on a bracket right + of their rows. Thinner bands are drawn and not named. Rows 24 apart, each as near its place as the others + let it. pins: {address: name}, a register's pin at the end of its band's row; hot: addresses whose band's row + is framed as read (`ink`'s echo).""" + named = [r for r in runs if yof(r[0]) - yof(r[1]) >= 6] + rows = [] # (kind, ideal y, payload), top down + for lo_, hi_, perm, name in reversed(named): + if not (rows and rows[-1][0] == "edge" and rows[-1][2][0] == hi_): + rows.append(("edge", yof(hi_), (hi_, lo_))) + rows.append(("run", (yof(hi_) + yof(lo_)) / 2, (lo_, hi_, perm, name))) + rows.append(("edge", yof(lo_), (lo_, lo_))) + ys = spread([r[1] for r in rows], ROW, lo_b, hi_b) + files = {} + for (kind, iy, p), ry in zip(rows, ys): + if kind == "edge": + a, of = p + ex = x + w + 2 + if abs(ry - iy) < 1: + g.path(f"M{ex},{iy} H{col - 8}", RULE, 2) + else: + g.path(f"M{ex},{iy} h8 L{col - 20},{ry} H{col - 8}", RULE, 2) + C.addr(g, col, ry - ROW / 2, a, ink_of(of)) + else: + lo_, hi_, perm, name = p + xx = col + 2 * A + if any(lo_ <= a < hi_ for a in hot): + g.frame(xx - 6, ry - ROW / 2, 12 * A, ROW, READ, 2) + if perm: + g.t(xx, b(ry - ROW / 2), perm, S, DIM, MONO) + g.t(xx + 11 * A, b(ry - ROW / 2), size(hi_ - lo_), S, DIM, MONO, "end") + files.setdefault(name, []).append(ry) + for pa, pn in (pins or {}).items(): + if lo_ <= pa < hi_: + px = xx + 11 * A + 12 + g.path(f"M{px},{ry} l9,-6 v12 Z", fill=ink_of(pa)) + g.t(px + 14, b(ry - ROW / 2), pn, S, VELLUM, BOLD) + bx = col + 19 * A + 12 # the files: a bracket over their rows, the name beside + for name, rys in files.items(): + if len(rys) > 1: + C.bracket(g, bx, min(rys) - 8, max(rys) + 8, RULE, 2, 5, side=-1) + g.t(bx + 12, b((min(rys) + max(rys)) / 2 - ROW / 2), name, S, VELLUM, MONO) + else: + g.t(bx + 12, b(rys[0] - ROW / 2), name, S, VELLUM, MONO) + return ys -def band_h(n): - """A band's height: 24 at least (a label's row), and 4 a doubling past 4 KiB: schematic, not to scale.""" - import math - return 24 + 4 * max(math.log2(n / 4096), 0) +def magnify(g, ax, ay, x, y, h): + """The two lines of a magnification: from the place on the whole to the enlargement's corners.""" + g.path(f"M{ax},{ay} L{x - 2},{y - 2} M{ax},{ay} L{x - 2},{y + h + 2}", RULE, 2) -def segments(g, x, y, hot=(), rsp=None, rip=None, label_x=None): - """`segments`: the address space as one tall rectangle, high addresses on top, as the textbook draws it. - Bands in their mapping's ink (55% toward the page), a file's mappings touching, its parts split by a page - line; a hole a fixed 40 with a break and its size. Labels beside with leaders: the left the band's first - address, the right its permissions, what it is, its size; a file's parts bracketed with the file's name. - Live: rsp's and rip's pins at their place; a band framed when a word is inked into it; growth arrows.""" - W = 140 - lx = label_x or x + W + 40 - yy, last_lo, groups, pos = y, None, [], {} - g.t(x - 12, yy - 6, "ffffffffffffffff", 18, FAINT, "sans", "end") - for lo, hi, perm, name in MAPPINGS: - if last_lo is not None and hi < last_lo: # a hole: compressed to 40, a break and its size - gap = last_lo - hi - g.rect(x, yy, W, 40, PAGE) - g.frame(x, yy - 1, W, 42, RULE, 2) - for dy in (14, 24): - g.path(" ".join(["M%d,%d" % (x - 6, yy + dy)] + ["l%d,%d" % (10, -6 if k % 2 == 0 else 6) - for k in range(16)]), RULE, 2) - for kk, (unit, k) in enumerate((("TiB", 1 << 40), ("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10))): - if gap >= k: - g.t(lx, yy + 26, "nothing mapped · %s" % size(gap), 18, FAINT, "sans") - break - yy += 40 - h = band_h(hi - lo) - ink = INK[perm] if name not in ("[stack]", "[heap]") else (STACK if name == "[stack]" else HEAP) - g.rect(x, yy, W, h, mix(ink, PAGE, 0.55)) - g.rect(x, yy, W, 2, PAGE) # a mapping's edge - mid = yy + h / 2 - g.path(f"M{x + W + 4},{mid} H{lx - 8}", RULE, 2) # the leader - g.t(lx, mid + 7, perm, S, ink if perm != "r--" else VELLUM, MONO) - g.t(lx + 4 * A, mid + 7, (name if name.startswith("[") else WHAT[perm]) + ("" if name else " (anonymous)"), - 18, DIM, "sans") - g.t(lx + 4 * A + 170, mid + 7, size(hi - lo), S, FAINT, MONO) - C.addr(g, x - 12 - len("%x" % lo) * A, yy + h - ROW, lo, ink) - if lo in hot: - g.frame(x - 4, yy - 2, W + 8, h + 4, READ, 3) - pos[name, lo] = (yy, h) - if name and not name.startswith("[") and (not groups or groups[-1][0] != name): - groups.append([name, yy, yy + h]) - elif name and groups and groups[-1][0] == name: - groups[-1][2] = yy + h - yy += h - last_lo = lo - for name, a, z in groups: # a file's mappings: one thing, with parts - C.bracket(g, lx + 4 * A + 280, a + 3, z - 3, DIM, 2, 6, side=-1) - g.t(lx + 4 * A + 292, (a + z) / 2 + 7, name, S, VELLUM, MONO) - g.t(x - 12, yy + 18, "0", 18, FAINT, "sans", "end") - # growth: the stack down from its top, the heap up from its base - sy, sh = pos["[stack]", 0x7ffffffde000] - gen.arrow(g, [(x + W - 24, sy + 8), (x + W - 24, sy + sh - 6)], BRIGHT, 2, hl=8, hw=8) - hy, hh = pos["[heap]", 0x1004000] - gen.arrow(g, [(x + W - 24, hy + hh - 6), (x + W - 24, hy + 8)], BRIGHT, 2, hl=8, hw=8) - if rsp: # a register's pin at its place in its band - f = (0x7ffffffff000 - rsp) / (0x7ffffffff000 - 0x7ffffffde000) - py = sy + f * sh - g.t(x + 8, py + 7, "rsp", S, VELLUM, BOLD) - g.path(f"M{x + 48},{py} l9,-6 v12 Z", fill=STACK) - if rip: - cy, ch = pos["list", 0x1001000] - g.t(x + 8, cy + ch / 2 + 7, "rip", S, VELLUM, BOLD) - g.path(f"M{x + 48},{cy + ch / 2} l9,-6 v12 Z", fill=CODE) - g.t(x, y - 34, "the address space", 20, VELLUM, "sansm") - g.t(x + 200, y - 34, "high addresses on top · not to scale: holes 40, bands by the log of their size", 18, - FAINT, "sans") - return yy +EW = 180 # an enlargement's width -TOP = 0x800000000000 # the user half's end: 0x00007fffffffffff + 1, 128 TiB +def segments(g, wx, wy0, wy1, ex, boxes, col, says="all", live=True, bounds=None, hot=(), ew=EW): + """`segments`: the whole as a slim bar at wx, and an enlargement of each cluster at ex (boxes: name -> (y, h)), + its names from col. Returns each enlargement's yof and x, for ties.""" + apex = whole(g, wx, wy0, wy1) + out = {} + for name in ["stack", "libraries", "program"]: + lo, hi = CLUSTERS[name] + y, h = boxes[name] + magnify(g, *apex[name], ex, y, h) + runs = (runs_of if says == "all" and name == "program" else files_of)(lo, hi) # the program by segment + yof = enlargement(g, ex, y, ew, h, lo, hi, [("rsp", RSP)] if live and name == "stack" else (), + [(r[0], r[1]) for r in runs if r[2] is None], hot) + lo_b, hi_b = (bounds or {}).get(name, (y - 200, y + h + 200)) + table(g, ex, ew, yof, runs, col, lo_b, hi_b, {0x1001926: "rip"} if live else None, hot) + out[name] = (yof, ex) + return out -def ink_of(lo, perm, name): - return STACK if name == "[stack]" else HEAP if name == "[heap]" else INK[perm] +BOXES = {"stack": (72, 72), "libraries": (196, 368), "program": (612, 252)} +BOUNDS = {"stack": (60, 156), "libraries": (184, 588), "program": (600, 876)} +EIFFEL = [[("If this bar were the Eiffel Tower, all that is mapped would be",)], + [("7 µm thick: a tenth of a hair, split between its two ends.",)]] -def bar(g, x, y, w, h, lo, hi, unit, step, labels=False, label_x=None, hot=()): - """`segments` at one level: the stretch lo..hi to a linear scale, high on top, its ruler on the left in - `unit` every `step`, each mapping where it is, a hairline (1 unit) when that is all it comes to.""" - yof = lambda a: y + h - (a - lo) / (hi - lo) * h - g.rect(x, y, w, h, PAGE) - g.frame(x, y, w, h, DIM, 2) - n = (hi - lo) // step - for k in range(int(n) + 1): - a = lo + k * step - yy = yof(a) - g.rect(x - 8, yy - 1, 8, 2, FAINT) - if k % max(int(n) // 8, 1) == 0 or k == n: - g.t(x - 12, yy + 6, "%d %s" % (k * step // unit, {1 << 40: "TiB", 1 << 30: "GiB", 1 << 20: "MiB", - 1 << 10: "KiB"}[unit]), 18, FAINT, "sans", "end") - for lo_, hi_, perm, name in MAPPINGS: - if hi_ <= lo or lo_ >= hi: - continue - a, z = max(lo_, lo), min(hi_, hi) - y0, y1 = yof(z), yof(a) - ink = ink_of(lo_, perm, name) - g.rect(x + 2, y0, w - 4, max(y1 - y0, 1), ink if y1 - y0 < 3 else mix(ink, PAGE, 0.35)) - if lo_ in hot: - g.frame(x - 2, y0 - 3, w + 4, max(y1 - y0, 1) + 6, READ, 2) - if labels and y1 - y0 >= 6: - g.path(f"M{x + w + 4},{(y0 + y1) / 2} H{label_x - 6}", RULE, 2) - g.t(label_x, (y0 + y1) / 2 + 7, "%s %s" % (perm, name or "anonymous"), 18, VELLUM, "sans") - g.t(label_x + 200, (y0 + y1) / 2 + 7, size(hi_ - lo_), 18, FAINT, "sans") - return yof +def treatment_a(g): + """A: the whole, and each cluster enlarged: the program's segments each named with its two addresses, + permissions and size, under a bracket with the file's name; every other file once, its start, stop, size, + its parts' permissions inside the bands that have room; rsp and rip pinned.""" + segments(g, 472, 72, 864, 972, BOXES, 1216, bounds=BOUNDS) + gen.caption(g, EIFFEL) -def magnify(g, yof, x0, lo, hi, x1, y1, h1): - """The lines of a magnification: from the stretch's two ends on one level to the next level's corners.""" - g.path(f"M{x0},{yof(hi)} L{x1},{y1}", RULE, 2) - g.path(f"M{x0},{yof(lo)} L{x1},{y1 + h1}", RULE, 2) +def treatment_b(g): + """B: the same, saying less: the program too is one file, named once; no pins.""" + segments(g, 472, 72, 864, 972, BOXES, 1216, says="files", live=False, bounds=BOUNDS) + gen.caption(g, EIFFEL) -def treatment_a(g, x, y, H, wide=False, hot=()): - """A: the whole user half to true scale, then two magnifications of its two clusters, then the libraries.""" - yof = bar(g, x, y, 60, H, 0, TOP, 1 << 40, 16 << 40) - g.t(x - 12, y - 30, "0x7fffffffffff · the user half's end", 18, DIM, "sans") - g.t(x - 12, y + H + 26, "0", 18, DIM, "sans") - g.t(x - 12, y - 52, "above: 16 EiB not canonical, then the kernel's half", 18, FAINT, "sans") - # the top cluster: the libraries and the stack, the last 128 MiB of the user half - t_lo, t_hi = TOP - (256 << 20), TOP - X2 = x + 240 - y2 = bar(g, X2, y, 60, H * 0.45, t_lo, t_hi, 1 << 20, 32 << 20, hot=hot) - magnify(g, yof, x + 60, t_lo, t_hi, X2, y, H * 0.45) - g.t(X2, y - 8, "the top 256 MiB · × 500 000", 18, DIM, "sans") - # the libraries: 4 MiB of them, with names - l_lo, l_hi = 0x7ffff7c00000, 0x7ffff8000000 - X3 = X2 + 300 - y3 = bar(g, X3, y, 60, H * 0.45, l_lo, l_hi, 1 << 20, 1 << 20, labels=True, label_x=X3 + 80, hot=hot) - magnify(g, y2, X2 + 60, l_lo, l_hi, X3, y, H * 0.45) - g.t(X3, y - 8, "the libraries, 4 MiB · × 32", 18, DIM, "sans") - # the bottom cluster: the program and its heap, the first 20 MiB - b_lo, b_hi = 0, 20 << 20 - yb = y + H * 0.55 - y4 = bar(g, X2, yb, 60, H * 0.45, b_lo, b_hi, 1 << 20, 4 << 20, labels=True, label_x=X2 + 80, hot=hot) - magnify(g, yof, x + 60, b_lo, b_hi, X2, yb, H * 0.45) - g.t(X2, yb - 8, "the bottom 20 MiB · × 7 000 000", 18, DIM, "sans") - # the stack, its 132 KiB, in the top magnification's level: its label there - sy = y2(0x7ffffffff000) - g.path(f"M{X2 + 64},{sy + 2} L{X2 + 120},{y + H * 0.45 + 30}", RULE, 2) - g.t(X2 + 124, y + H * 0.45 + 36, "rw- [stack] 132 KiB, rsp in it: a hairline even here", 18, VELLUM, "sans") - gen.arrow(g, [(X2 + 30, sy + 4), (X2 + 30, sy + 26)], BRIGHT, 2, hl=8, hw=8) - return yof +def treatment_c(g): + """C: the whole alone. Each end's clusters one label each, where they start and how big; no enlargement.""" + x, y0, y1 = 760, 72, 864 + apex = whole(g, x, y0, y1, 24) + lx = x + 24 + 96 + for (ax, ay), ly, a, text, sz in [(apex["stack"], y0, 0x7ffffffde000, "[stack]", 0x21000), + (apex["libraries"], y0 + 48, 0x7ffff7c00000, "libc.so.6, ld.so", + 0x7ffff7fff000 - 0x7ffff7c00000), + (apex["program"], y1, 0x1000000, "list, [heap]", 0x25000)]: + g.path(f"M{ax + 2},{ay} h12 L{lx - 20},{ly} H{lx - 8}", RULE, 2) + C.addr(g, lx, ly - ROW / 2, a, ink_of(a)) + g.t(lx + 13 * A + 7 * A, b(ly - ROW / 2), size(sz), S, DIM, MONO, "end") + g.t(lx + 21 * A, b(ly - ROW / 2), text, S, VELLUM, MONO) + gen.caption(g, [[("Everything mapped is at the two ends of this bar:",)], + [("128 TiB, and between them nothing.",)]]) def frame_space(name): - """Mock 34: treatment A at full size: the whole user half to scale, and magnified into its clusters.""" + """Mock 34: `segments` at full size, treatment A.""" gen.W, gen.H = 1920, 1080 g = Svg() - treatment_a(g, 200, 100, 760) - gen.caption(g, [[("If this space were as tall as the Eiffel Tower, all the program has mapped would",)], - [("be thinner than a hair: the stack a third of a micron, at the very top.",)]]) + treatment_a(g) g.save(name) def sheet_abc(name): - """Mock 35: three treatments of physicality, on the list recording's mappings.""" - gen.W, gen.H = 2880, 1000 + """Mock 35: three treatments of the address space, each as the frame it would be, saying less and less.""" + gen.W, gen.H = 1920, 3 * 1160 g = Svg() - for k, (t, sub) in enumerate([("A", "true scale, then magnified: the recommended"), - ("B", "one long strip of powers of ten, left to right"), - ("C", "true scale alone, distances written")]): - x0 = 80 + k * 960 - g.t(x0, 50, t, 44, GOLD, "sansb") - g.t(x0 + 50, 50, sub, 22, VELLUM, "sans") - if t == "A": - treatment_a(g, x0 + 120, 130, 760) - elif t == "B": - levels = [(0, TOP, 1 << 40, 32 << 40, "128 TiB"), (TOP - (128 << 20), TOP, 1 << 20, 32 << 20, "× 1 000 000"), - (0x7ffff7c00000, 0x7ffff8000000, 1 << 20, 1 << 20, "× 32"), - (0x7ffffffde000, 0x7ffffffff000, 1 << 10, 32 << 10, "the stack × 30")] - prev = None - for j, (lo, hi, u, st_, lab) in enumerate(levels): - xx = x0 + 70 + j * 210 - f = bar(g, xx, 130, 50, 760, lo, hi, u, st_) - g.t(xx, 120, lab, 18, DIM, "sans") - if prev: - magnify(g, prev[0], prev[1] + 50, lo, hi, xx, 130, 760) - prev = (f, xx) - else: - yof = bar(g, x0 + 150, 130, 80, 760, 0, TOP, 1 << 40, 16 << 40) - for a, txt in [(0x7ffffffde000, "the stack, 132 KiB, a hairline at the top"), - (0x7ffff7c00000, "the libraries, 4 MiB, 127 MiB below it"), - (0x1000000, "the program and its heap, 0.2 MiB, 16 MiB above 0")]: - yy = yof(a) - g.path(f"M{x0 + 236},{yy} H{x0 + 300}", RULE, 2) - g.t(x0 + 306, yy + 6 + (20 if a == 0x7ffff7c00000 else 0), txt, 18, VELLUM, "sans") - g.t(x0 + 306, 500, "127 TiB of nothing between", 22, FAINT, "sans") + for k, (t, sub, draw) in enumerate([ + ("A", "each cluster enlarged; the program's segments each with its start, stop and size; every other " + "file once (recommended)", treatment_a), + ("B", "each cluster enlarged; every file once, the program too: its start, stop and size; no pins", + treatment_b), + ("C", "the whole alone; each cluster one label: where it starts, its size", treatment_c)]): + oy = k * 1160 + g.rect(0, oy, 1920, 80, gen.RAISED) + g.t(64, oy + 54, t, 40, GOLD, "sansb") + g.t(120, oy + 52, sub, 24, VELLUM, "sans") + g.o.append(f'<g transform="translate(0,{oy + 80})">') + draw(g) + g.o.append("</g>") g.save(name) @@ -289,10 +303,10 @@ def raw_dump(g, x, y, rows, per=16, words=False, inked=False): for k, v in enumerate(vals): g.t(bx + k * 30 + (12 if k >= 8 else 0), b(yy), "%02x" % v, S, mix(VELLUM, PAGE, 0.5) if words else VELLUM, MONO) - if words: + if words: # the word, ranged right in its field (its eight pairs as they land): low byte last w_ = D.word(a0) - ink = (D.region(w_) or VELLUM) if inked else VELLUM - g.t(bx + per * 30 + 24, b(yy), "%x" % w_ if w_ else "0", S, ink, MONO) + ink = (ink_of(w_) or VELLUM) if inked else VELLUM + g.t(bx + per * 30 + 24 + 8 * 30, b(yy), "%x" % w_, S, ink, MONO, "end") return ys @@ -310,21 +324,175 @@ def frame_bytes(name): def frame_words(name): - """Eight bytes a row now, each row's word beside it, inked by where it points; the address space beside.""" + """Mock 30: the `ink` beat. The words, eight bytes a row, in the state column where the stack will be; the + address space in the program column; the dump tied to its place at the top of the stack; the return + address, 10018b0, framed with the mapping it points into (an echo).""" gen.W, gen.H = 1920, 1080 g = Svg() - g.o.append('<g transform="translate(64,96) scale(1.35)">') - g.t(0, -10, "eight bytes a row, read right to left, a word", 18, FAINT, "sans") - ys = raw_dump(g, 0, 0, 16, per=8, words=True, inked=True) - k = 1 # 7fffffffece8: the return address, main+80 - g.frame(12 * A + 12 - 4, ys[k] - 1, 8 * 30 + 4, ROW + 2, READ, 2) - g.frame(12 * A + 12 + 8 * 30 + 24 - 6, ys[k] - 1, 7 * A + 12, ROW + 2, READ, 2) - g.o.append("</g>") - g.o.append('<g transform="translate(0,0)">') - treatment_a(g, 1060, 110, 740, hot={0x1001000}) - g.o.append("</g>") - gen.caption(g, [[("Eight bytes make a word, stored low byte first: read right to left,",)], - [("this one is ",), ("10018b0", "mono"), (", an address in the code, so it takes code's ink.",)]]) + top = 80 + boxes = {k: (y + 8, h) for k, (y, h) in BOXES.items()} + bounds = {k: (a + 8, z + 8) for k, (a, z) in BOUNDS.items()} + bounds["stack"] = (top + 24, 172) # the tie comes in over the stack's top: its first row under it + out = segments(g, 224, top, 864, 368, boxes, 540, live=False, bounds=bounds, hot={0x10018b0}, ew=108) + dx = 1048 # the dump, where the stack panel will be + ys = raw_dump(g, dx, top, 16, per=8, words=True, inked=True) + bx = dx + 12 * A + 12 + k = 1 # 7fffffffece8: the return address, main+80 + g.frame(bx - 6, ys[k], 8 * 30, ROW, READ, 2) + g.frame(bx + 8 * 30 + 24 + 8 * 30 - 7 * A - 6, ys[k], 7 * A + 12, ROW, READ, 2) + # the tie: these 128 bytes are at the top of the stack. A bracket on the dump's rows, a `link` in the stack's + # ink from it over the top of the stage and down onto the stack's place, its head on it; standing now (its + # beat was the one before), so quiet. + yof, ex = out["stack"] + hx, ink = ex + 108 - 30, mix(STACK, PAGE, 0.6) + C.bracket(g, dx - 14, top + 3, top + 16 * ROW - 3, ink, 3, 6) + gen.arrow(g, [(dx - 14, top + 3), (dx - 14, 56), (hx, 56), (hx, top - 1)], ink, 3, r=8, hl=10, hw=10) + gen.caption(g, [[("A word takes the ink of the mapping it points into: ",)], + [("10018b0", "mono"), (" is in list's code, ",), ("1001000", "mono"), (" to ",), ("1002000", "mono"), + (".",)]]) + g.save(name) + + +# ---- `regroup`, walked: the return address's eight bytes becoming its word --------------------------------------- +WALK = 0x7fffffffece8 # b0 18 00 01 00 00 00 00: 10018b0, main+80 +BX = 12 * A + 12 # the bytes, 12 after the address +FR = BX + 8 * 30 + 24 + 8 * 30 # the word's field ends here, ranged right: its low byte last + + +def glide(t): + """`glide`, the cubic (0.45, 0, 0.55, 1), by bisection on x.""" + lo, hi = 0.0, 1.0 + for _ in range(40): + m = (lo + hi) / 2 + x = 3 * (1 - m) ** 2 * m * 0.45 + 3 * (1 - m) * m * m * 0.55 + m ** 3 + lo, hi = (m, hi) if x < t else (lo, m) + m = (lo + hi) / 2 + return 3 * (1 - m) * m * m + m ** 3 + + +def flight(k, t, closed=False): + """Where byte k is at t of its flight: straight up off its place, over on an arc a quarter as high as it is long + (34 to 120), straight down onto its pair's place in the word, the pairs 30 apart as they land. Returns the + byte's x and rise, its two ends and the arc's height; arc(k, u) is the path.""" + sx = BX + 30 * k + ex = FR - (24 if closed else 30) * (k + 1) + (0 if closed else 6) + h = min(max((ex - sx) / 4, 34), 120) + x, y = arc(sx, ex, h, glide(t)) + return x, y, sx, ex, h + + +def arc(sx, ex, h, u): + """The cubic from (sx, 0) to (ex, 0) leaving and arriving upright, h high at its middle.""" + H = h / 0.75 + return sx + (ex - sx) * (3 * u * u - 2 * u ** 3), -3 * (1 - u) * u * H + + +def walk_row(g, y, state, f=0): + """The walked row at frame f of the beat: state names the instant (see regroup_strip). The bytes themselves + fly; each one's place in the dump takes a faint copy once it has left (from a third of its flight).""" + vals = [D.MEM[WALK + k] for k in range(8)] + faint = mix(VELLUM, PAGE, 0.5) + C.addr(g, 0, y, WALK, STACK if state == "ink" else FAINT) + t_ = lambda k: (f - 108 - 6 * k) / 36 if state == "flight" else (1 if state != "which" else 0) + for k, v in enumerate(vals): + t = t_(k) + if t <= 0: + g.t(BX + 30 * k, b(y), "%02x" % v, S, VELLUM, MONO) + elif t >= 1 / 3: + g.t(BX + 30 * k, b(y), "%02x" % v, S, faint, MONO) + if state in ("which", "flight"): + for k in range(8): # the offsets: where each byte is, under it + if t_(k) <= 0: + g.t(BX + 30 * k + 12, b(y + ROW), "+%d" % k, S, FAINT, MONO, "middle") + if t_(0) <= 0: + g.t(BX + 12, y + 2 * ROW + 16, "low byte", 18, DIM, "sans", "middle") + if t_(7) <= 0: + g.t(BX + 7 * 30 + 12, y + 2 * ROW + 16, "high byte", 18, DIM, "sans", "middle") + if state == "flight": + for k in range(8): + t = t_(k) + x, yy, sx, ex, h = flight(k, min(max(t, 0), 1)) + if 0 < t < 1: # the path, for the still only, broken round the byte + pts = [(arc(sx, ex, h, u)[0] + 12, y + ROW / 2 + arc(sx, ex, h, u)[1]) for u in [i / 60 for i in range(61)]] + seg = [] + for p in pts + [None]: + if p is not None and p[1] < y - 4 and (abs(p[0] - x - 12) > 26 or abs(p[1] - (y + yy + ROW / 2)) > 16): + seg.append(p) + continue + if len(seg) > 1: + g.path("M" + " L".join("%.1f,%.1f" % q for q in seg), RULE, 2, dash="12 8") + seg = [] + g.t(x, b(y + yy), "%02x" % vals[k], S, BRIGHT, BOLD) + if t >= 1: + g.t(ex, b(y), "%02x" % vals[k], S, VELLUM, MONO) + g.t(ex + 12, b(y + ROW), "+%d" % k, S, FAINT, MONO, "middle") + if t_(0) >= 1: + g.t(FR - 12, y + 2 * ROW + 16, "low byte", 18, DIM, "sans", "middle") + if state == "landed": + for k in range(8): + ex = FR - 30 * (k + 1) + 6 + g.t(ex, b(y), "%02x" % vals[k], S, VELLUM, MONO) + g.t(ex + 12, b(y + ROW), "+%d" % k, S, FAINT, MONO, "middle") + g.t(FR - 12, y + 2 * ROW + 16, "low byte", 18, DIM, "sans", "middle") + g.t(FR - 234 + 12, y + 2 * ROW + 16, "high byte", 18, DIM, "sans", "middle") + if state == "closed": + word = "".join("%02x" % vals[k] for k in reversed(range(8))) + g.t(FR, b(y), word, S, VELLUM, MONO, "end") + lead = len(word) - len(word.lstrip("0")) + x0, x1 = FR - 16 * A, FR - (16 - lead) * A + g.path(f"M{x0 + 1},{y + ROW + 2} v6 H{x1 - 1} v-6", FAINT, 2) + g.t((x0 + x1) / 2, y + ROW + 30, "zeros in front", 18, DIM, "sans", "middle") + if state in ("zeros", "ink"): + ink = CODE if state == "ink" else VELLUM + g.t(FR, b(y), "10018b0", S, ink, MONO, "end") + if state == "ink": + g.frame(BX - 6, y, 8 * 30, ROW, READ, 2) + g.frame(FR - 7 * A - 6, y, 7 * A + 12, ROW, READ, 2) + + +def regroup_strip(name): + """Mock 37: `regroup` walked, on the return address's real bytes, seven instants with their frames (from the + beat's step, 552 in the introduction's chart). Each is the frame's x2 camera on the dump, drawn here at x1.5.""" + gen.W, gen.H = 1920, 1520 + g = Svg() + cells = [("0 to 48", "eight to a row", "the rows make room; each right half goes down a row, then left", "reflow"), + ("66 to 96", "which byte is which", "its offsets under it; the low byte is the first in memory", "which"), + ("138", "in flight, the low byte first", "6 f apart, 36 f each, on nested arcs; a faint copy stays", + "flight"), + ("186", "landed, reversed", "the low byte last; each offset lands under its byte", "landed"), + ("216", "the spaces close", "sixteen digits, the same eight bytes", "closed"), + ("240", "the zeros in front go", "a word is written without them", "zeros"), + ("288 to 386", "the other fifteen at a run", "rows 6 f apart: each word fades in (8 f) as its bytes dim", + "run"), + ("ink beat", "then its ink, and its mapping", "framed with its band in the address space (mock 30)", "ink")] + for i, (fr, title, sub, state) in enumerate(cells): + ox, oy = 40 + (i % 2) * 950, 30 + (i // 2) * 370 + g.t(ox, oy + 26, "%d" % (i + 1), 26, GOLD, "sansb") + g.t(ox + 30, oy + 26, title, 24, VELLUM, "sansm") + g.t(ox + 30 + gen.tw(title, 24, "sansm") + 16, oy + 26, "f " + fr, 20, FAINT, "mono") + g.t(ox + 30, oy + 52, sub, 18, DIM, "sans") + g.o.append(f'<g transform="translate({ox + 10},{oy + 220}) scale(1.25)">') + if state == "reflow": # f 30: the right half on its new row, halfway left + vals = [D.MEM[WALK - 8 + k] for k in range(16)] + C.addr(g, 0, -ROW, WALK - 8, FAINT) + for k in range(8): + g.t(BX + 30 * k, b(-ROW), "%02x" % vals[k], S, VELLUM, MONO) + C.addr(g, 0, 0, WALK, mix(FAINT, PAGE, 0.5)) # f 40: down a row (18 to 30), now halfway left + for k in range(8): + g.t(BX + (30 * 8 + 12) * (1 - glide(10 / 18)) + 30 * k, b(0), "%02x" % vals[8 + k], S, VELLUM, MONO) + elif state == "run": + for r, a in enumerate(range(0x7fffffffed30, 0x7fffffffed50, 8)): + y = (r - 2) * ROW + C.addr(g, 0, y, a, FAINT) + vals = [D.MEM[a + k] for k in range(8)] + fade = [1, 1, 0.5, 0][r] # mid-wave: two rows done, one half in, one to come + for k, v in enumerate(vals): + g.t(BX + 30 * k, b(y), "%02x" % v, S, mix(VELLUM, PAGE, 1 - 0.5 * fade), MONO) + if fade: + g.t(FR, b(y), "%x" % D.word(a), S, mix(VELLUM, PAGE, fade), MONO, "end") + else: + walk_row(g, 0, state, {"which": 96, "flight": 138}.get(state, 0)) + g.o.append("</g>") g.save(name) @@ -405,6 +573,99 @@ def aligned(g, y, first, n, cur, cur_line, bytes_=False, title="reverse"): return yy +def columns(g, y, first, n, cur, cur_line, title="reverse"): + """`aligned` `columns`: one panel, two columns: the source on the left, its instructions on the right, the seam + the frame's gutter. A group is a run of a line's instructions; the line stands level with the run's first row, + its cell as tall as the run, its first row on raised paper; a 2-unit rule over each group across both columns. + Each column's place (the line's number, the instruction's address) ranged right in the same slot at its + column's start, in the same face. A line's second run: the line again, faint, `again`. Rows of no line: the + cell empty but for the function's name, faint. Lines with no code of their own: in source order after the + line before them, faint, their instruction cell empty.""" + rows = [r for r in D.disasm_rows() if r[0] >= first][:n] + groups = [] # (lines, rows): lines (number or None, func); rows in order + for r in rows: + key = (r[2], r[1]) if r[2] is not None else (None, r[1]) + if not groups or groups[-1][0] != [key]: + groups.append([[key], []]) + groups[-1][1].append(r) + out, seen = [], set() # the lines with no code, after the line before them + for (key,), rs in groups: + out.append(([key], rs)) + line = key[0] + if line is not None and line not in seen: + seen.add(line) + nxt = line + 1 + gap = [] + while nxt not in {r[2] for r in D.disasm_rows() if r[1] == key[1]} and nxt < 40 and \ + D.SRC[nxt - 1].strip() and nxt not in seen and any(r[2] and r[2] > nxt for r in rows): + gap.append((nxt, key[1])) + nxt += 1 + if gap and gap[0][0] not in seen: + seen.update(l for l, _ in gap) + out.append((gap, [])) + total = sum(max(len(ls), len(rs)) for ls, rs in out) + C.tag(g, 64, y, 1792, ph(total), "list.c · %s" % title, focus=True, + note="each line level with its instructions") + PL = 132 # a place is ranged right 132 into its column: seven digits + longest = max(len(D.SRC[l - 1].strip()) for ls, _ in out for l, _ in ls if l) + LX, RX = 64, 64 + PL + 24 + (longest + 4) * A # the seam: the longest line and 48 after it + g.rect(RX - 1, y + TAG, 2, ph(total) - TAG - 2, RULE) + yy, done = y + TAG + PAD, set() + for lines, rs in out: + h = max(len(lines), len(rs)) + g.rect(66, yy, 1788, 2, RULE) # where a group starts (and the last one ends) + for i, (line, func) in enumerate(lines): + ly = yy + i * ROW + nocode = not rs + again = line in done and not nocode + if line is None: + g.t(LX + PL + 24, b(ly), "%s · no line" % func, 18, FAINT, "sans") + continue + on = line == cur_line and any(r[0] == cur for r in rs) + if i == 0 and not again and not nocode: # the line's row: raised, as a head; the wash if it is current + g.rect(LX + 2, ly + 2, RX - 1 - LX - 2, ROW - 2, C.WASH if on else RAISED) + if on: + g.rect(LX + PL - 50, ly + 2, 50, ROW - 2, GOLD) + g.t(LX + PL, b(ly), str(line), S, gen.INK if on else (FAINT if again or nocode else DIM), + BOLD if on else MONO, "end") + text = D.SRC[line - 1].strip() + if again: + g.t(LX + PL + 24, b(ly), text, S, FAINT, MONO) + g.t(LX + PL + 24 + (len(text) + 3) * A, b(ly), "again: its second run", 18, FAINT, "sans") + elif nocode: + g.t(LX + PL + 24, b(ly), text, S, FAINT, MONO) + else: + C.c_line(g, LX + PL + 24, ly, text, 56) + done.add(line) + if not rs: + g.t(RX + PL + 24, b(yy), "no code of their own", 18, FAINT, "sans") + for j, (a, f_, ln, mn, ops) in enumerate(rs): + ry = yy + j * ROW + if a == cur: + g.rect(RX + 1, ry, 1854 - RX - 1, ROW, C.WASH) + g.rect(RX + 1, ry, 24, ROW, GOLD) + g.path(f"M{RX + 19},{ry + 12} l-10,-6 v12 Z", fill=gen.INK) + C.addr(g, RX + PL - len("%x" % a) * A, ry, a, CODE, lead=4) + x = RX + PL + 24 + g.t(x, b(ry), mn, S, FAINT if mn in ("nop", "endbr64") else KW, MONO if mn in ("nop", "endbr64") else BOLD) + C.operands(g, x + 7 * A, ry, mn, ops, VELLUM) + if a == cur: + C.chain(g, RX + PL + 24 + 30 * A, ry, [(0x1004098, HEAP), (0x1004070, HEAP)], ("int", 9), None, room=1844) + yy += h * ROW + g.rect(66, yy, 1788, 2, RULE) + return yy + + +def frame_columns(name): + """Mock 36: `aligned` `columns` on mock 33's data: reverse, the pc at the load, and _init after it.""" + gen.W, gen.H = 1920, 1080 + g = Svg() + columns(g, 48, 0x1001910, 18, 0x1001926, 21) + gen.caption(g, [[("The same listing in two columns: each line level with the first",)], + [("of its instructions. Lines 23 to 25 compiled to nothing of their own.",)]]) + g.save(name) + + def frame_code(name): """The code chapter: the source and its instructions side by side, each line level with its code, and the instruction's bytes between the address and the mnemonic: an instruction is bytes too.""" @@ -434,3 +695,5 @@ if __name__ == "__main__": frame_aligned("33-aligned") frame_space("34-segments") sheet_abc("35-segments-abc") + frame_columns("36-aligned-columns") + regroup_strip("37-regroup") |
