From e8616df2031b39535e62dce1142b029e65723c95 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 11 Nov 2026 11:52:21 -0300 Subject: design: the address space as one tall rectangle; aligned as a listing From the user. segments is the textbook picture: one rectangle, high addresses on top, each mapping a band in its ink (heights by the log of their size, holes folded with their size beside them), labels outside on leaders, a file's mappings bracketed as one, the stack's and the heap's growth as arrows, rsp and rip pinned in their bands. aligned is one panel: a line as the head of its instructions, line numbers and addresses in one column since both say where, a rule over every head. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- design/mock/30-intro-words-480.png | Bin 0 -> 43111 bytes design/mock/30-intro-words.png | Bin 224784 -> 230085 bytes design/mock/30-intro-words.svg | 287 ++++++++++++++++++++++++--------- design/mock/32-intro-code.png | Bin 104319 -> 108875 bytes design/mock/32-intro-code.svg | 287 ++++++++++++++++----------------- design/mock/33-aligned-480.png | Bin 18796 -> 15933 bytes design/mock/33-aligned.png | Bin 135867 -> 98391 bytes design/mock/33-aligned.svg | 318 +++++++++++++++---------------------- design/mock/34-segments.png | Bin 0 -> 113202 bytes design/mock/34-segments.svg | 213 +++++++++++++++++++++++++ design/mock/intro.py | 213 ++++++++++++++++++------- 11 files changed, 847 insertions(+), 471 deletions(-) create mode 100644 design/mock/30-intro-words-480.png create mode 100644 design/mock/34-segments.png create mode 100644 design/mock/34-segments.svg (limited to 'design/mock') diff --git a/design/mock/30-intro-words-480.png b/design/mock/30-intro-words-480.png new file mode 100644 index 0000000..4156b0a Binary files /dev/null and b/design/mock/30-intro-words-480.png differ diff --git a/design/mock/30-intro-words.png b/design/mock/30-intro-words.png index faba6f4..463726e 100644 Binary files a/design/mock/30-intro-words.png and b/design/mock/30-intro-words.png differ diff --git a/design/mock/30-intro-words.svg b/design/mock/30-intro-words.svg index fd1502b..267a344 100644 --- a/design/mock/30-intro-words.svg +++ b/design/mock/30-intro-words.svg @@ -165,81 +165,218 @@ 1 - - - -the address space -the recording's mappings, low on top - -1000000 -list -headers, read-only -4 KiB - -1001000 -list -code -4 KiB - - -1002000 -list -read-only data -4 KiB - -1003000 -list -data -4 KiB - -1004000 -heap -malloc's -132 KiB - -nothing mapped · 127 TiB - -7ffff7c00000 -libc.so.6 -read-only -144 KiB - -7ffff7c24000 -libc.so.6 -code -1.5 MiB - -7ffff7d9f000 -libc.so.6 -read-only data -472 KiB - -7ffff7e15000 -libc.so.6 -data -56 KiB - -7ffff7fbc000 -[vdso] -code -8 KiB - -7ffff7fbf000 -ld.so -code -180 KiB - -7ffff7ffb000 -ld.so -data -16 KiB - -nothing mapped · 127 MiB - -7ffffffde000 -[stack] -the stack -132 KiB + + + +ffffffffffffffff + + + +--x +[vsyscall] +4 KiB +ffffffffff600000 + + + + +nothing mapped · 15 EiB + + + +rw- +[stack] +132 KiB +7ffffffde000 + + + + +nothing mapped · 127 MiB + + + +rw- +data (anonymous) +4 KiB +7ffff7ffe000 + + + +rw- +data +4 KiB +7ffff7ffd000 + + + +r-- +read-only +8 KiB +7ffff7ffb000 + + + +r-- +read-only +60 KiB +7ffff7fec000 + + + +r-x +code +180 KiB +7ffff7fbf000 + + + +r-- +read-only +4 KiB +7ffff7fbe000 + + + +r-x +[vdso] +8 KiB +7ffff7fbc000 + + + +r-- +[vvar] +24 KiB +7ffff7fb6000 + + + +r-- +read-only +260 KiB +7ffff7f75000 + + + +rw- +data (anonymous) +20 KiB +7ffff7f70000 + + + + +nothing mapped · 1.3 MiB + + + +rw- +data (anonymous) +32 KiB +7ffff7e1b000 + + + +rw- +data +8 KiB +7ffff7e19000 + + + +r-- +read-only +16 KiB +7ffff7e15000 + + + +r-- +read-only +472 KiB +7ffff7d9f000 + + + +r-x +code +1.5 MiB +7ffff7c24000 + + + +r-- +read-only +144 KiB +7ffff7c00000 + + + + +nothing mapped · 127 TiB + + + +rw- +[heap] +132 KiB +1004000 + + + +rw- +data +4 KiB +1003000 + + + +r-- +read-only +4 KiB +1002000 + + + +r-x +code +4 KiB +1001000 + + + + +r-- +read-only +4 KiB +1000000 + + + +ld.so + + + +ld.so.cache + + + +libc.so.6 + + + +list +0 + + + + +rsp + +the address space +high addresses on top · not to scale: holes 40, bands by the log of their size + Eight bytes make a word, stored low byte first: read right to left, this one is 10018b0, an address in the code, so it takes code's ink. diff --git a/design/mock/32-intro-code.png b/design/mock/32-intro-code.png index eb22ba7..2d9b70b 100644 Binary files a/design/mock/32-intro-code.png and b/design/mock/32-intro-code.png differ diff --git a/design/mock/32-intro-code.svg b/design/mock/32-intro-code.svg index 35597b8..a5eafc0 100644 --- a/design/mock/32-intro-code.svg +++ b/design/mock/32-intro-code.svg @@ -1,155 +1,144 @@ - - - -list.c -each line beside the first of its instructions - - - -disassembly -reverse - - - - - - - - - - - - - - - -20 -while -( -head -) -{ -10019 -10 -48 85 ff -test -rdi, rdi -10019 -13 -74 20 -je -1001935 -10019 -15 -55 -push -rbp -10019 -16 -48 89 e5 -mov -rbp, rsp -10019 -19 -31 c0 -xor -eax, eax -10019 -1b -0f 1f 44 00 00 -nop -[rax + rax] -10019 -20 -48 89 c1 -mov -rcx, rax -10019 -23 -48 89 f8 -mov -rax, rdi - - - - - - -21 -struct -node -* -next -= -head -- -> -next -; -10019 -26 -48 8b 7f 08 -mov -rdi, [rdi + 8] -1004098 - - -1004070 - - -9 -22 -head -- -> -next -= -prev -; -10019 -2a -48 89 48 08 -mov -[rax + 8], rcx -20 -while (head) { -10019 -2e -48 85 ff -test -rdi, rdi -10019 -31 -75 ed -jne -1001920 -10019 -33 -5d -pop -rbp -26 -return -prev -; -10019 -34 -c3 -ret - -10019 -35 -31 c0 -xor -eax, eax -10019 -37 -c3 -ret - + + + +list.c · reverse +each line, then the instructions it compiled to · lines and addresses in one column + + +20 +while +( +head +) +{ +1001 +910 +48 85 ff +test +rdi, rdi +1001 +913 +74 20 +je +1001935 +1001 +915 +55 +push +rbp +1001 +916 +48 89 e5 +mov +rbp, rsp +1001 +919 +31 c0 +xor +eax, eax +1001 +91b +0f 1f 44 00 00 +nop +[rax + rax] +1001 +920 +48 89 c1 +mov +rcx, rax +1001 +923 +48 89 f8 +mov +rax, rdi + + + +21 +struct +node +* +next += +head +- +> +next +; + + + +1001 +926 +48 8b 7f 08 +mov +rdi, [rdi + 8] +1004098 + + +1004070 + + +9 + + +22 +head +- +> +next += +prev +; +1001 +92a +48 89 48 08 +mov +[rax + 8], rcx + + +20 +while (head) { again: its second run +1001 +92e +48 85 ff +test +rdi, rdi +1001 +931 +75 ed +jne +1001920 +1001 +933 +5d +pop +rbp + + +26 +return +prev +; +1001 +934 +c3 +ret + +1001 +935 +31 c0 +xor +eax, eax +1001 +937 +c3 +ret + + 48 8b 7f 08 is one instruction: mov rdi, [rdi + 8], line 21's head->next. The line sits level with its code: they are one row. diff --git a/design/mock/33-aligned-480.png b/design/mock/33-aligned-480.png index a54620d..77ecf25 100644 Binary files a/design/mock/33-aligned-480.png and b/design/mock/33-aligned-480.png differ diff --git a/design/mock/33-aligned.png b/design/mock/33-aligned.png index 5ed74b0..b649754 100644 Binary files a/design/mock/33-aligned.png and b/design/mock/33-aligned.png differ diff --git a/design/mock/33-aligned.svg b/design/mock/33-aligned.svg index 5c46e27..1762f33 100644 --- a/design/mock/33-aligned.svg +++ b/design/mock/33-aligned.svg @@ -1,198 +1,128 @@ - - - -list.c -each line beside the first of its instructions - - - -disassembly -reverse - - - - - - - - - - - - - - - -20 -while -( -head -) -{ -10019 -10 -test -rdi, rdi -10019 -13 -je -1001935 -10019 -15 -push -rbp -10019 -16 -mov -rbp, rsp -10019 -19 -xor -eax, eax -10019 -1b -nop -[rax + rax] -10019 -20 -mov -rcx, rax -10019 -23 -mov -rax, rdi - - - - - - -21 -struct -node -* -next -= -head -- -> -next -; -10019 -26 -mov -rdi, [rdi + 8] -1004098 - - -1004070 - - -9 -22 -head -- -> -next -= -prev -; -10019 -2a -mov -[rax + 8], rcx -20 -while (head) { -10019 -2e -test -rdi, rdi -10019 -31 -jne -1001920 -10019 -33 -pop -rbp -26 -return -prev -; -10019 -34 -ret - -10019 -35 -xor -eax, eax -10019 -37 -ret - -_init · no line -10019 -38 -endbr64 - -10019 -3c -sub -rsp, 8 -10019 -40 -mov -rax, [rip + 0x1231] -10019 -47 -test -rax, rax -10019 -4a -je -100194e -10019 -4c -call -rax -10019 -4e -add -rsp, 8 -10019 -52 -ret - -_fini · no line -10019 -54 -endbr64 - -10019 -58 -sub -rsp, 8 -10019 -5c -add -rsp, 8 -10019 -60 -ret - -strtol@plt-0x10 · no line -10019 -70 -push -[rip + 0x1222] -10019 -76 -jmp -[rip + 0x1224] -Line 20, while (head), has two runs: its test before the body -and again after it. Lines 23 to 25 have no code of their own. + + + +list.c · reverse +each line, then the instructions it compiled to · lines and addresses in one column + + +20 +while +( +head +) +{ +1001 +910 +test +rdi, rdi +1001 +913 +je +1001935 +1001 +915 +push +rbp +1001 +916 +mov +rbp, rsp +1001 +919 +xor +eax, eax +1001 +91b +nop +[rax + rax] +1001 +920 +mov +rcx, rax +1001 +923 +mov +rax, rdi + + + +21 +struct +node +* +next += +head +- +> +next +; + + + +1001 +926 +mov +rdi, [rdi + 8] +1004098 + + +1004070 + + +9 + + +22 +head +- +> +next += +prev +; +1001 +92a +mov +[rax + 8], rcx + + +20 +while (head) { again: its second run +1001 +92e +test +rdi, rdi +1001 +931 +jne +1001920 +1001 +933 +pop +rbp + + +26 +return +prev +; +1001 +934 +ret + +1001 +935 +xor +eax, eax +1001 +937 +ret + + +Line 20, while (head), comes twice: its test before the body and +again after it. Lines 23 to 25 have no code of their own: no group. diff --git a/design/mock/34-segments.png b/design/mock/34-segments.png new file mode 100644 index 0000000..03d26db Binary files /dev/null and b/design/mock/34-segments.png differ diff --git a/design/mock/34-segments.svg b/design/mock/34-segments.svg new file mode 100644 index 0000000..5c5505b --- /dev/null +++ b/design/mock/34-segments.svg @@ -0,0 +1,213 @@ + + + +ffffffffffffffff + + + +--x +[vsyscall] +4 KiB +ffffffffff600000 + + + + +nothing mapped · 15 EiB + + + +rw- +[stack] +132 KiB +7ffffffde000 + + + + +nothing mapped · 127 MiB + + + +rw- +data (anonymous) +4 KiB +7ffff7ffe000 + + + +rw- +data +4 KiB +7ffff7ffd000 + + + +r-- +read-only +8 KiB +7ffff7ffb000 + + + +r-- +read-only +60 KiB +7ffff7fec000 + + + +r-x +code +180 KiB +7ffff7fbf000 + + + +r-- +read-only +4 KiB +7ffff7fbe000 + + + +r-x +[vdso] +8 KiB +7ffff7fbc000 + + + +r-- +[vvar] +24 KiB +7ffff7fb6000 + + + +r-- +read-only +260 KiB +7ffff7f75000 + + + +rw- +data (anonymous) +20 KiB +7ffff7f70000 + + + + +nothing mapped · 1.3 MiB + + + +rw- +data (anonymous) +32 KiB +7ffff7e1b000 + + + +rw- +data +8 KiB +7ffff7e19000 + + + +r-- +read-only +16 KiB +7ffff7e15000 + + + +r-- +read-only +472 KiB +7ffff7d9f000 + + + +r-x +code +1.5 MiB +7ffff7c24000 + + + +r-- +read-only +144 KiB +7ffff7c00000 + + + + +nothing mapped · 127 TiB + + + +rw- +[heap] +132 KiB +1004000 + + + +rw- +data +4 KiB +1003000 + + + +r-- +read-only +4 KiB +1002000 + + + +r-x +code +4 KiB +1001000 + + + +r-- +read-only +4 KiB +1000000 + + + +ld.so + + + +ld.so.cache + + + +libc.so.6 + + + +list +0 + + + + +rsp + +rip + +the address space +high addresses on top · not to scale: holes 40, bands by the log of their size + diff --git a/design/mock/intro.py b/design/mock/intro.py index b9c7d7d..098455c 100644 --- a/design/mock/intro.py +++ b/design/mock/intro.py @@ -26,14 +26,14 @@ CODE_BYTES = [(0x1001920, "48 89 c1"), (0x1001923, "48 89 f8"), (0x1001926, "48 def size(n): - for unit, k in (("TiB", 1 << 40), ("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10)): + 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 B" % n -def segments(g, x, y, w, hot=()): +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).""" @@ -60,6 +60,104 @@ def segments(g, x, y, w, hot=()): return y0 +# 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 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 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 + + +def frame_space(name): + """Mock 34: the rectangle alone, every label reading.""" + gen.W, gen.H = 1920, 1080 + g = Svg() + segments(g, 560, 110, rsp=RSP, rip=0x1001926, label_x=760) + g.save(name) + + def camera(g, k, cx, cy): """The camera at scale k on the point (cx, cy) of the frame: all drawing after this is through it.""" g.o.append(f'') @@ -108,7 +206,11 @@ def frame_words(name): 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) - segments(g, 12 * A + 12 + 8 * 30 + 24 + 13 * A + 36, -24, 560, hot={0x1001000}) + g.o.append("") + g.o.append('') + g.o.append('') # the stage holds it whole above the caption + segments(g, 200, 0, hot={0x1001000}, rsp=RSP, label_x=380) + g.o.append("") g.o.append("") 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.",)]]) @@ -136,56 +238,60 @@ BYTES = {0x1001910: "48 85 ff", 0x1001913: "74 20", 0x1001915: "55", 0x1001916: def aligned(g, y, first, n, cur, cur_line, bytes_=False, title="reverse"): - """`aligned`: the source and the disassembly side by side, a table in address order: each source line level - with the first row of its run of instructions, a line that comes again (a second run) repeated faint at that - run, a line with no code of its own not drawn (its number missing says so), a row of no line (padding) with - nothing beside it. The pc's row and its line are one band across both columns: the alignment is the thread.""" + """`aligned`, one panel: the listing interleaved as objdump -S prints it. Each run of a line's instructions + is a group: a head row (the line on raised paper, its number in the place column) and under it its rows, + their addresses in the same column, indented under the line's text. A 2-unit rule over every head: where a + line starts and ends. A line's second run: its head again, faint; rows of no line: a faint head saying so.""" rows = [r for r in D.disasm_rows() if r[0] >= first][:n] - h = ph(n) - C.tag(g, 64, y, 880, h, "list.c", note="each line beside the first of its instructions") - C.tag(g, 976, y, 880, h, "disassembly", note=title) - y0 = y + TAG + PAD - seen, last_line = set(), None - lead = C.shared([r[0] for r in rows]) - i0 = 0 # each run's extent: a quiet bracket by the gutter, line to its last row - for i in range(1, n + 1): - if i == n or rows[i][2] != rows[i0][2]: - if rows[i0][2] is not None: - C.bracket(g, 932, y + TAG + PAD + i0 * ROW + 3, y + TAG + PAD + i * ROW - 3, RULE, 2, 5, side=-1) - i0 = i - for i, (a, func, line, mn, ops) in enumerate(rows): - yy = y0 + i * ROW - on = a == cur - if on: # one band through the gutter: the line and its instruction are one - g.rect(64 + 2, yy, 880 - 4, ROW, C.WASH) - g.rect(944, yy, 32, ROW, C.WASH) - g.rect(976 + 2, yy, 880 - 4, ROW, C.WASH) - g.rect(976 + 2, yy, 24, ROW, GOLD) - g.path(f"M{976 + 20},{yy + 12} l-10,-6 v12 Z", fill=gen.INK) - if line is not None and line != last_line: - again = line in seen - if line == cur_line and on: - g.rect(64 + 2, yy, 50, ROW, GOLD) - g.t(64 + 46, b(yy), str(line), S, gen.INK, BOLD, "end") + groups, cur_g = [], None + for r in rows: + key = (r[2], r[1]) if r[2] is not None else (None, r[1]) + if cur_g is None or cur_g[0] != key: + cur_g = [key, []] + groups.append(cur_g) + cur_g[1].append(r) + total = len(rows) + len(groups) + C.tag(g, 64, y, 1792, ph(total), "list.c · %s" % title, focus=True, + note="each line, then the instructions it compiled to · lines and addresses in one column") + yy, seen = y + TAG + PAD, set() + PLACE, TEXT = 100 + 7 * A, 220 # the place column ends at 196; the text 24 after it + for (line, func), grp in groups: + g.rect(64 + 2, yy, 1792 - 4, 2, RULE) # where a line's group starts (and the last one ends) + again = line in seen + on_line = line == cur_line and any(r[0] == cur for r in grp) + g.rect(64 + 2, yy + 2, 1792 - 4, ROW - 2, RAISED if not again else PAGE) + if line is None: + g.t(TEXT, b(yy), "%s · no line" % func, 18, FAINT, "sans") + else: + if on_line: + g.rect(PLACE - 50 + 12, yy + 2, 50, ROW - 2, GOLD) + g.t(PLACE + 12, b(yy), str(line), S, gen.INK if on_line else (FAINT if again else DIM), BOLD if on_line else MONO, + "end") + text = D.SRC[line - 1].strip() + if again: + g.t(TEXT, b(yy), text + " again: its second run", S, FAINT, MONO) else: - g.t(64 + 46, b(yy), str(line), S, FAINT, MONO, "end") - text = D.SRC[line - 1] - C.c_line(g, 64 + 64, yy, text, 66) if not again else g.t(64 + 64, b(yy), text.strip(), S, FAINT, MONO) + C.c_line(g, TEXT, yy, text, 80) seen.add(line) - elif line is None and (i == 0 or rows[i - 1][1] != func): - g.t(64 + 64, b(yy), "%s · no line" % func, 18, FAINT, "sans") - last_line = line - C.addr(g, 976 + 40, yy, a, CODE, lead=lead) - x = 976 + 40 + 8 * A + 12 - if bytes_: - g.t(x, b(yy), BYTES.get(a, ""), S, BRIGHT if on else DIM, MONO) - x += 15 * A - g.t(x, b(yy), mn, S, FAINT if mn == "nop" else KW, MONO if mn == "nop" else BOLD) - C.operands(g, x + 7 * A, yy, mn, ops, VELLUM) - if on: - C.chain(g, 976 + 600, yy, [(0x1004098, HEAP), (0x1004070, HEAP)], ("int", 9), {0: "read"} if False else None, - room=976 + 868) - return y0 + yy += ROW + for a, f_, ln, mn, ops in grp: + on = a == cur + if on: + g.rect(64 + 2, yy, 1792 - 4, ROW, C.WASH) + g.rect(64 + 2, yy, 24, ROW, GOLD) + g.path(f"M{64 + 20},{yy + 12} l-10,-6 v12 Z", fill=gen.INK) + C.addr(g, PLACE + 12 - len("%x" % a) * A, yy, a, CODE, lead=4) + x = TEXT + 2 * A # indented under the line it came from + if bytes_: + g.t(x, b(yy), BYTES.get(a, ""), S, BRIGHT if on else DIM, MONO) + x += 15 * A + g.t(x, b(yy), mn, S, FAINT if mn == "nop" else KW, MONO if mn == "nop" else BOLD) + C.operands(g, x + 7 * A, yy, mn, ops, VELLUM) + if on: + C.chain(g, 64 + 900, yy, [(0x1004098, HEAP), (0x1004070, HEAP)], ("int", 9), None, room=64 + 1780) + yy += ROW + g.rect(64 + 2, yy, 1792 - 4, 2, RULE) + return yy def frame_code(name): @@ -203,9 +309,9 @@ def frame_aligned(name): """Mock 33: the `aligned` split, reverse and list_done, the pc at the load.""" gen.W, gen.H = 1920, 1080 g = Svg() - aligned(g, 48, 0x1001910, 30, 0x1001926, 21) - gen.caption(g, [[("Line 20, ",), ("while (head)", "mono"), (", has two runs: its test before the body",)], - [("and again after it. Lines 23 to 25 have no code of their own.",)]]) + aligned(g, 48, 0x1001910, 16, 0x1001926, 21) + gen.caption(g, [[("Line 20, ",), ("while (head)", "mono"), (", comes twice: its test before the body and",)], + [("again after it. Lines 23 to 25 have no code of their own: no group.",)]]) g.save(name) @@ -215,3 +321,4 @@ if __name__ == "__main__": frame_stack("31-intro-stack") frame_code("32-intro-code") frame_aligned("33-aligned") + frame_space("34-segments") -- cgit v1.3