◄ WORLD V · SONNY 5DART 148 · a helldive at the net

THREADED CODE the four-step loop that is the whole processor

The compiled program is just a list of addresses (or opcodes) of primitive routines, run by a tiny dispatch loop: fetch the next token, jump to its routine, which does its work and returns to the loop. It is Forth’s inner interpreter, and the beating heart of every bytecode VM — the “thread” is the single strand of control woven through a fixed set of primitives (nothing to do with concurrency).

THE TECHNIQUE fetch, dispatch, execute, repeat

A tiny bytecode — PUSH 2, PUSH 3, ADD, PUSH 4, MUL, HALT — over an operand stack. The dispatch loop reads each opcode and jumps to its handler; PUSH grows the stack, ADD/MUL pop two and push one. This is the inner loop every VM runs, including the 8/19 benchmark’s Cortex VM. Watch it compute (2+3)*4. live demo


  

HISTORY & CREDIT named 1973, running ~1970

“Bell invented threaded code” — he gave the first published definition (1973); DEC’s PDP-11 FORTRAN already generated it, and Moore ran it in Forth ~1970. cited

pre-1970 · assembly folklore — subroutine-threaded code (a bare list of CALLs) is the honest predecessor, no single inventor.
~1970 · Charles Moore (NRAO) — builds (indirect) threaded code into the first Forth, independently and contemporaneously with Bell’s submission.
1973 · James Bell (DEC) — “Threaded Code” (CACM 16(6)): the first published definition, name, and the four-step dispatch analysis; Dewar (1975) names indirect threading.
1990s–2009 · GCC labels-as-values → computed-goto; CPython 3.1 (2009) adds it to the bytecode loop (~15–20% faster) — though the folklore “always 2× faster” is now false on modern branch predictors (Rohou et al. 2015).

The “thread” is a data structure (a strand of control through primitives), not a unit of parallelism. Bell, 1973

RECOMMEND FOR I-13 a program array + a dispatch recursion

The bytecode is an f64 array, the operand stack is an array, and the dispatch loop is recursion over the program counter:

$ i13 run thread.i13 # [PUSH 2, PUSH 3, ADD, PUSH 4, MUL, HALT] dispatch: PUSH 2 -> [2] ; PUSH 3 -> [2,3] ; ADD -> [5] ; PUSH 4 -> [5,4] ; MUL -> [20] ; HALT (2+3)*4 = 20
Recommend: nothing new — the program is an f64 opcode array; the operand stack is an array with a top pointer; the dispatch loop is a recursion that reads prog[pc] and branches on the opcode (verified (2+3)*4 = 20). This is a defunctionalized interpreter (dart 147): opcodes are tags, the switch is the apply.
Note: it is literally what the 8/19 benchmark’s Cortex WASM VM does at its core — the AST→IVM lowering produces a bytecode a dispatch loop runs. The full VM (branches, calls) wants more opcodes but the same array + pointer machinery. Direct-threading (computed-goto) is a dispatch optimisation, not a new model.