// JIT compilation pipeline
// "A diagram is worth 10,000 words." — russell@unturf.com
digraph jit_pipeline {
    rankdir=LR
    node [shape=box, style=filled, fontname="Helvetica"]
    edge [fontname="Helvetica", fontsize=10]

    scheme [label="Scheme Source\n(define (ack m n)\n  (cond ...))" fillcolor="#e8f4fd" shape=note]
    ast [label="AST\n(Pair tree)" fillcolor="#fff3cd"]
    analysis [label="can_jit_proc()\ncheck: only ints,\narith, cond, if,\nself-recursion" fillcolor="#ffeaa7"]

    subgraph cluster_codegen {
        label="x86_64 Code Generation"
        style=rounded
        color="#cc0000"
        prologue [label="Prologue\npush rbp\nmov rbp,rsp\npush r12-r15" fillcolor="#ffcccc"]
        body [label="Body Emission\ncmp → je/jne\nadd/sub/imul\ncall self\njmp (TCO)" fillcolor="#ffcccc"]
        epilogue [label="Epilogue\npop r15-r12\npop rbp\nret" fillcolor="#ffcccc"]
    }

    mmap [label="mmap()\nPROT_READ|\nPROT_WRITE|\nPROT_EXEC" fillcolor="#ff6b6b" fontcolor=white]
    native [label="Native Function\nJitFunc ptr\n0.12ms ack(3,4)" fillcolor="#00b894" fontcolor=white shape=doubleoctagon]

    fallback [label="Interpreter\nFallback\n(call/cc, macros)" fillcolor="#dfe6e9" style="filled,dashed"]

    scheme -> ast [label="parse"]
    ast -> analysis [label="walk"]
    analysis -> prologue [label="jittable"]
    analysis -> fallback [label="complex" style=dashed]
    prologue -> body -> epilogue
    epilogue -> mmap [label="raw bytes"]
    mmap -> native [label="cast to\nfunction ptr"]

    {rank=same; analysis; fallback}
}
