Files
LithosAnanake/v4/include/v4/asm.h
T
rajamesandClaude Opus 5.5 067f317c47 feat(v4.0.0): hosted golden model, single node
First code for StarForth v4 (JUSTIFICATION.md section 10, step 1): one node
of the 32-instruction core as a C99 model, with cell width as a build
parameter.

- Node: P, A, B, F18 circular stacks (10 and 9 deep, D-2), word-addressed
  memory (D-1), 5% guard bands on every bounded list.
- Instruction word: six 5-bit slots in 32 bits at every cell width.
- Executor: all 32 opcodes of DECOMPOSITION.md 1.3. Cell arithmetic wraps
  explicitly; no signed overflow or implementation-defined shift.
- Heat: per-opcode and per-call-target counters and the anti-clock, driven
  by instruction retirement (1.4, D-6 interim).
- Slot packer and runner for tests, and a reference unsigned multiply in
  plain C99 with no 128-bit type.

Tests run at 32- and 64-bit cells, and under ASan and UBSan. They cover
every opcode and execute the first section 4 definitions (NIP SWAP OR
NEGATE ROT 0< 0= 2DUP - U<) against the C operation each stands for.

UM* as written in section 4 is exact only while u1 <= 2^(n-2). Two known
failing cases are pinned in test_foundation.c until it is rewritten.

DECOMPOSITION.md: record D-9, the instruction word is 32 bits at every
cell width (ruled 2026-10-02).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 14:38:27 -04:00

69 lines
2.6 KiB
C

/* asm.h -- slot packer for hand-assembled test programs.
*
* This is not the compiler capsule (JUSTIFICATION.md section 9). It exists so
* that tests can write a DECOMPOSITION.md definition opcode by opcode and have
* the slots, literals and branch targets laid out the way section 1.2 and
* section 2 require, instead of packing words by hand in every test.
*
* Layout rules it applies:
*
* - Opcodes fill slots 0..5 left to right; a full word is written out and a
* new one started. Unused slots are padded with `nop`.
* - A literal is `@p` in a slot plus its value in the word after the
* instruction word. Several literals in one word follow it in order.
* - `;` and `ex` close the word, since nothing after them executes.
* - A branch closes the word, since the bits to its right are its address.
* If the next free slot is 4 or 5 the word is closed first, because
* branches are legal in slots 0-3 only.
* - A branch target must lie in the same page as P at the time the branch
* executes, for the reach of the slot it landed in. Otherwise the
* assembler sets its error flag rather than emit a branch that goes
* somewhere else.
*/
#ifndef V4_ASM_H
#define V4_ASM_H
#include "v4/iword.h"
#include "v4/node.h"
typedef struct {
v4_node *n;
v4_cell here; /* address of the word being built */
v4_iword cur;
unsigned slot; /* next free slot, 0..V4_SLOT_COUNT */
v4_cell lit[V4_SLOT_COUNT]; /* literals owed after this word */
unsigned nlit;
int error;
} v4_asm;
/* A branch whose target is not known yet. */
typedef struct {
v4_cell addr; /* the word holding the branch */
unsigned slot;
v4_cell p; /* P when the branch executes */
} v4_asm_ref;
void v4_asm_begin(v4_asm *as, v4_node *n, v4_cell origin);
/* Any opcode that is neither a branch nor `@p`. */
void v4_asm_op(v4_asm *as, unsigned op);
/* `@p` and its value. */
void v4_asm_lit(v4_asm *as, v4_cell value);
/* jump, call, next, if, -if to a known address. */
void v4_asm_branch(v4_asm *as, unsigned op, v4_cell target);
/* The same with the target supplied later by v4_asm_resolve. */
v4_asm_ref v4_asm_branch_fwd(v4_asm *as, unsigned op);
void v4_asm_resolve(v4_asm *as, v4_asm_ref ref, v4_cell target);
/* Close the word being built and return the address of the next one. This is
* how a label is taken: branch targets are word addresses. */
v4_cell v4_asm_label(v4_asm *as);
/* 1 if nothing went wrong so far. Closes the word being built. */
int v4_asm_ok(v4_asm *as);
#endif /* V4_ASM_H */