// Assembly implementation architecture — 2026-04-18 refresh
// "A diagram is worth 10,000 words." — russell@unturf.com
digraph asm_arch {
    rankdir=TB
    node [shape=box, style=filled, fontname="Helvetica"]
    edge [fontname="Helvetica", fontsize=10]

    subgraph cluster_binary {
        label="Binary: ~22 KB stripped (bump-only) / ~25 KB (GC_NAIVE), zero dependencies"
        style=rounded
        color="#333333"
        fontcolor="#333333"
        start [label="_start\nno libc\nno main()" fillcolor="#2d3436" fontcolor=white]
    }

    subgraph cluster_syscalls {
        label="14 Linux Syscalls — no libc"
        style=rounded
        color="#666666"
        fs_io [label="File I/O\nread(0) write(1)\nopen(2) close(3)\nlseek(8)" fillcolor="#dfe6e9"]
        mem_io [label="Memory\nmmap(9) munmap(11)" fillcolor="#dfe6e9"]
        net_io [label="Sockets\nsocket(41) connect(42)\naccept(43) bind(49)\nlisten(50) setsockopt(54)" fillcolor="#dfe6e9"]
        misc [label="Misc\nclock_gettime(228)\nexit(60)" fillcolor="#dfe6e9"]
    }

    subgraph cluster_memory {
        label="Memory Model — default bump; optional mark-sweep + meta-GC"
        style=rounded
        color="#666666"
        bump [label="Bump Allocator\n%r15 = heap ptr\n%r13 = heap limit\nheap_grow mmaps 64 MB chunks\n(1 MB under GC_NAIVE)" fillcolor="#ffeaa7"]
        snap [label="heap-snapshot / heap-restore\n(portable arena, bump-only)\nper-request loops = O(1)" fillcolor="#ffeaa7"]
        tags [label="Tag-in-Low-3-Bits\n0=int 1=pair 2=sym\n3=closure 4=builtin\n5=special 6=string\n7=vector / hash-table / hash-set\n(disambiguated by negative sentinel)" fillcolor="#ffeaa7"]
        gc [label="GC_NAIVE build (opt-in)\n• 8 B header per block\n• stop-the-world mark-sweep\n• first-fit free list\n• conservative stack scan" fillcolor="#fab1a0"]
        meta [label="Meta-GC (with-arena thunk)\n1. snapshot %r15\n2. run thunk (free list off)\n3. mark phase (+ result root)\n4. NO mark in range → bulk reset O(1)\n5. otherwise → sweep fallback" fillcolor="#fd79a8"]
    }

    subgraph cluster_interp {
        label="Interpreter — 6,645 lines of GNU assembler (GAS, AT&T syntax); as + ld from GNU binutils"
        style=rounded
        color="#666666"
        tokenize [label="Tokenizer\nchar-by-char\n\\n \\r \\t \\0 escapes" fillcolor="#fff3cd"]
        read_fn [label="Reader\nrecursive descent\n→ tagged cons cells\n(load), read-from-string" fillcolor="#fff3cd"]
        eval_fn [label="Evaluator\nTCO via jmp .eval_top\nall special forms\n+ eval as builtin" fillcolor="#d4edda"]
        print_fn [label="Printer\nitoa, list, vector\nhash-table / hash-set\ncustom output_fd" fillcolor="#81ecec"]
        builtins [label="95+ Builtins\narith cons car cdr list ...\nload ports write-file\nportal-save/resume\ntcp-listen/accept/send/recv\nread-from-string eval\nnative hash-table-* / hash-set-*\n(GC build: with-arena, gc-collect,\n gc-stats, arena-stats)" fillcolor="#a29bfe"]
    }

    subgraph cluster_env {
        label="Environment"
        style=rounded
        color="#666666"
        env_chain [label="Linked List\n24 bytes per binding\n[sym | val | next]\n← allocation hot path" fillcolor="#e2d5f1"]
        sym_intern [label="Symbol Table\ndjb2 hash, 1024 buckets\nO(1) intern\n(MOAD-0001 fixed)" fillcolor="#e2d5f1"]
    }

    start -> mem_io [label="allocate heap"]
    start -> fs_io [label="read input"]
    fs_io -> tokenize
    tokenize -> read_fn
    read_fn -> eval_fn
    eval_fn -> eval_fn [label="TCO: jmp" style=bold color=red]
    eval_fn -> builtins [label="apply"]
    eval_fn -> env_chain [label="lookup/define"]
    eval_fn -> print_fn [label="result"]
    print_fn -> fs_io
    builtins -> net_io [label="tcp-*"]
    builtins -> fs_io [label="(load), file I/O"]
    env_chain -> sym_intern
    bump -> tags
    bump -> snap [label="arena pattern" style=dashed]
    bump -> gc [label="GC_NAIVE flag" style=dashed color="#fab1a0"]
    gc -> meta [label="with-arena" color="#fd79a8"]
    eval_fn -> bump [label="cons/closure alloc"]
    meta -> bump [label="reset %r15" style=dashed]

    {rank=same; fs_io; mem_io; net_io; misc}
    {rank=same; tokenize; read_fn}
    {rank=same; bump; tags}
    {rank=same; gc; meta}
}
