feat(v4.0.0): the code generator

The third layer of the compiler capsule, v4/capsule/codegen.v4: opcodes,
literals and branches packed into instruction words at HERE, by the
rules of sections 1.2 and 2.  (OP,) (LIT,) (LABEL) (BRANCH,) (JUMP,)
(CALL,) (BRANCH>) (RESOLVE) (FLUSH) (CG-RESET).

Tested by laying the same programmes down with the text assembler on
one node and with the code generator, running, on another, and
comparing memory word for word: every opcode, literals and branches in
every slot position, and 600 random programmes.  What it lays down is
then run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rajames
2026-10-04 09:19:57 -04:00
co-authored by Claude Opus 5.5
parent c10d3a9cca
commit 880560fc00
3 changed files with 496 additions and 0 deletions
+21
View File
@@ -736,6 +736,27 @@ it, `xt + 1`; for any other word the parameter field is the code itself.
| `LIT` | OP | `@p` |
| `does_rt` | RET | Internal helper; `DOES>` is implemented by the compiler capsule. |
**The code generator** (`v4/capsule/codegen.v4`) is what these words are built on. It packs opcodes,
literals and branches into instruction words at `HERE`, by the rules of §1.2 and §2: slots filled left
to right, unused slots `nop`, a literal's value in the cell after its word, `;` `ex` and any branch
closing the word. A branch goes only in a slot whose address field reaches all of the node's memory
(slots 0–2 on a 16,384-word host node, 0–3 on a 1,024-word one); otherwise it starts the next word, so
no reach check is ever needed.
| Word | Does |
| --- | --- |
| `(OP,)` | `( op -- )` any opcode that is neither a branch nor `@p` |
| `(LIT,)` | `( x -- )` `@p` and its value |
| `(LABEL)` | `( -- addr )` closes the word being built; the address of the next: a branch target |
| `(BRANCH,)` | `( target op -- )` a branch to a known address; `(JUMP,)` and `(CALL,)` `( addr -- )` are the two commonest |
| `(BRANCH>)` `(RESOLVE)` | `( op -- ref )` a branch whose address is not known yet, and `( target ref -- )` filling it in |
| `(FLUSH)` `(CG-RESET)` | append the word being built; forget it |
Executed on the golden model's host node (2026-10-04) by laying the same programmes down with the text
assembler on one node and with the code generator, running, on another, and comparing memory word for
word: every opcode, literals and branches in every slot position, and 600 random programmes. What it
lays down was then run.
### 5.18 Control flow
| Word | Fate | v4 definition / notes |