Files
LithosAnanke/v4/src/exec.c
T
Claude e1b731f444 feat(v4): Phase 7 Task 3b -- compudynamics on the 32 F18 opcodes as on the words, behind OPCODE_PHYSICS (#189)
Captain Bob, 2026-11-14: "we're supposed to be working on f18 opcode as well as our composed f18
dictionary"; ruled every loop on both, "and an easy revert if opcodes are too granular".

- The engine: a hook at retire (exec.h on_retire), as Part A's at call; NULL unless set. test_exec.c.
- The mesh: 32 opcode records per node, an opcode window, opcode transitions under L4; at every retired
  instruction decay at the opcodes' own slope, heat, temperature, the window, the transition; in each cycle
  all 32 cooled, the opcode window's service, inference on its heat. L8 scores from the words and its gates
  govern both; the tick stays paced by word calls. The window is now one type for words and opcodes.
- The revert: OPCODE_PHYSICS (Kconfig.physics, default y; the kernel's flags) -- 0 and the opcodes are
  counted and charged to the Stadium as in Phase 1. make -C v4 mesh-boot-noop builds and tests it.
- Tests first: test_mesh_boot.c 77 checks as built, 86 with pipelining on, 74 with the revert.
- Measured on amd64, back to back: 219 s on, 196 s off (+12% under TCG). Both boots' logs labelled.
Not pushed (held to the end of Phase 7).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
2026-11-14 00:29:08 +00:00

359 lines
13 KiB
C

/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* exec.c -- instruction execution. See exec.h.
*
* All cell arithmetic is done in v4_ucell and converted back. Signed overflow
* is undefined behaviour in C99 and a right shift of a negative value is
* implementation-defined, and a golden model cannot have either: the hardware
* wraps, so the model wraps, on every host and at every optimisation level.
*/
#include "v4/exec.h"
static v4_cell add_wrap(v4_cell x, v4_cell y)
{
return (v4_cell)((v4_ucell)x + (v4_ucell)y);
}
/* Arithmetic shift right by one: the sign bit is kept. */
static v4_cell asr1(v4_cell x)
{
v4_ucell u = (v4_ucell)x;
return (v4_cell)((u >> 1) | (u & V4_MSB));
}
/* An instruction word occupies the low 32 bits of a cell at every cell width
* (DECOMPOSITION.md D-9). */
static v4_iword fetch_iword(const v4_node *n, v4_cell addr)
{
return (v4_iword)((v4_ucell)v4_node_load(n, addr) & 0xFFFFFFFFu);
}
/* Opcodes after which no further slot of the word executes. The five branch
* opcodes end it because everything to their right is address, taken or not.
* `;` and `ex` end it because P has been replaced, as on the F18. `unext` is
* not in this set: when its loop finishes it falls through to the next slot. */
static int ends_word(unsigned op)
{
return v4_op_is_branch(op) || op == V4_OP_SEMI || op == V4_OP_EX;
}
/* D-14: an address outside memory is a fault, and the opcode does nothing. */
static int faulted(v4_node *n, v4_cell addr)
{
if (v4_node_addr_ok(addr)) return 0;
v4_node_fault(n, V4_FAULT_ADDRESS, addr);
return 1;
}
/* The same for a fetch, which may also read the ROM (node.h). */
static int faulted_read(v4_node *n, v4_cell addr)
{
if (v4_node_addr_readable(n, addr)) return 0;
v4_node_fault(n, V4_FAULT_ADDRESS, addr);
return 1;
}
/* D-16: what each opcode takes from the stacks and leaves on them. If the
* data stack holds fewer than `dneed`, or the return stack fewer than
* `rneed`, or a stack the opcode leaves one more on is full, it is a fault
* and the opcode does nothing. An opcode that only looks at T (`if`) or at
* T and S (`+*`) needs them to be there. */
static int stack_faulted(v4_node *n, unsigned op)
{
unsigned dneed = 0, rneed = 0, dgrow = 0, rgrow = 0;
switch (op) {
case V4_OP_SEMI: case V4_OP_EX: case V4_OP_UNEXT: case V4_OP_NEXT:
rneed = 1; break;
case V4_OP_CALL:
rgrow = 1; break;
case V4_OP_FETCH_P: case V4_OP_FETCH_INC: case V4_OP_FETCH_B: case V4_OP_FETCH_A: case V4_OP_PUSH_A:
dgrow = 1; break;
case V4_OP_IF: case V4_OP_MINUS_IF:
case V4_OP_STORE_P: case V4_OP_STORE_INC: case V4_OP_STORE_B: case V4_OP_STORE_A:
case V4_OP_TWO_STAR: case V4_OP_TWO_SLASH: case V4_OP_INV: case V4_OP_DROP:
case V4_OP_BANG_B: case V4_OP_BANG_A:
dneed = 1; break;
case V4_OP_MUL_STEP: case V4_OP_ADD: case V4_OP_AND: case V4_OP_XOR:
dneed = 2; break;
case V4_OP_DUP:
dneed = 1; dgrow = 1; break;
case V4_OP_OVER:
dneed = 2; dgrow = 1; break;
case V4_OP_RPOP:
rneed = 1; dgrow = 1; break;
case V4_OP_PUSH:
dneed = 1; rgrow = 1; break;
default:
break; /* jump, nop */
}
if (n->ds.depth < dneed) { v4_node_fault(n, V4_FAULT_DATA_UNDER, 0); return 1; }
if (n->rs.depth < rneed) { v4_node_fault(n, V4_FAULT_RET_UNDER, 0); return 1; }
if (dgrow && n->ds.depth >= V4_DATA_DEPTH) { v4_node_fault(n, V4_FAULT_DATA_OVER, 0); return 1; }
if (rgrow && n->rs.depth >= V4_RET_DEPTH) { v4_node_fault(n, V4_FAULT_RET_OVER, 0); return 1; }
return 0;
}
void v4_exec_reset(v4_exec_state *es)
{
es->anticlock = 0;
es->on_call = 0;
es->on_call_ctx = 0;
es->on_retire = 0;
es->on_retire_ctx = 0;
es->resume = 0;
es->resume_iw = 0;
es->resume_slot = 0;
es->resume_faults = 0;
}
void v4_exec_op(v4_node *n, v4_exec_state *es, v4_heat *h,
unsigned op, v4_iword w, unsigned slot)
{
v4_dstack *ds = &n->ds;
v4_rstack *rs = &n->rs;
v4_cell x;
/* A store can be refused by the node itself -- to "any port", "which
* port", How or Have (node.c) -- or raise an error (D-18). Refused, the
* opcode did nothing more (node.h, FAULTS); raised, the store was made,
* but P is the handler's. */
unsigned faults = n->faults;
if (op <= V4_OP_BANG_A && stack_faulted(n, op)) return;
switch (op) {
case V4_OP_SEMI:
n->p = v4_rstack_pop(rs);
break;
case V4_OP_EX:
x = rs->r;
rs->r = n->p;
n->p = x;
break;
case V4_OP_JUMP:
n->p = v4_iword_branch(n->p, w, slot);
break;
case V4_OP_CALL:
x = v4_iword_branch(n->p, w, slot);
v4_rstack_push(rs, n->p);
if (es->on_call) es->on_call(es->on_call_ctx, n, x);
n->p = x;
break;
case V4_OP_UNEXT:
/* The restart at slot 0 is v4_exec_step_word's business; this is only
* the effect on R. */
if (rs->r != 0) rs->r = add_wrap(rs->r, -1);
else (void)v4_rstack_pop(rs);
break;
case V4_OP_NEXT:
if (rs->r != 0) {
rs->r = add_wrap(rs->r, -1);
n->p = v4_iword_branch(n->p, w, slot);
} else {
(void)v4_rstack_pop(rs);
}
break;
case V4_OP_IF: /* does not pop T */
if (ds->t == 0) n->p = v4_iword_branch(n->p, w, slot);
break;
case V4_OP_MINUS_IF: /* does not pop T */
if (((v4_ucell)ds->t & V4_MSB) == 0) n->p = v4_iword_branch(n->p, w, slot);
break;
case V4_OP_FETCH_P:
if (faulted_read(n, n->p)) return;
if (v4_node_port_index(n, n->p) >= 0) {
/* P is a port: the literal is the next word its neighbour
* writes, and P stays where it is */
if (!v4_node_read_ready(n, n->p)) return;
v4_dstack_push(ds, v4_node_fetch(n, n->p));
break;
}
v4_dstack_push(ds, v4_node_load(n, n->p));
n->p = add_wrap(n->p, 1);
break;
case V4_OP_FETCH_INC:
if (faulted_read(n, n->a)) return;
if (!v4_node_read_ready(n, n->a)) return; /* a port with nothing written to it yet */
v4_dstack_push(ds, v4_node_fetch(n, n->a));
n->a = add_wrap(n->a, 1);
break;
case V4_OP_FETCH_B:
if (faulted_read(n, n->b)) return;
if (!v4_node_read_ready(n, n->b)) return; /* a port with nothing written to it yet */
v4_dstack_push(ds, v4_node_fetch(n, n->b));
break;
case V4_OP_FETCH_A:
if (faulted_read(n, n->a)) return;
if (!v4_node_read_ready(n, n->a)) return; /* a port with nothing written to it yet */
v4_dstack_push(ds, v4_node_fetch(n, n->a));
break;
case V4_OP_STORE_P:
if (faulted(n, n->p)) return;
v4_node_store(n, n->p, v4_dstack_pop(ds));
if (n->faults != faults) { /* P is the handler now: it is not advanced */
if (n->fault_kind != V4_FAULT_RAISED) return;
break;
}
n->p = add_wrap(n->p, 1);
break;
case V4_OP_STORE_INC:
if (faulted(n, n->a)) return;
v4_node_store(n, n->a, v4_dstack_pop(ds));
if (n->faults != faults && n->fault_kind != V4_FAULT_RAISED) return; /* refused: A is untouched */
n->a = add_wrap(n->a, 1);
break;
case V4_OP_STORE_B:
if (faulted(n, n->b)) return;
v4_node_store(n, n->b, v4_dstack_pop(ds));
if (n->faults != faults && n->fault_kind != V4_FAULT_RAISED) return;
break;
case V4_OP_STORE_A:
if (faulted(n, n->a)) return;
v4_node_store(n, n->a, v4_dstack_pop(ds));
if (n->faults != faults && n->fault_kind != V4_FAULT_RAISED) return;
break;
case V4_OP_MUL_STEP:
/* D-3, plain F18: the carry out of T+S is not kept. T:A then shifts
* right one bit, T keeping its sign bit and T's bit 0 entering the top
* of A. S is not consumed. */
x = ds->t;
if ((v4_ucell)n->a & 1u) x = add_wrap(x, ds->s);
n->a = (v4_cell)(((v4_ucell)n->a >> 1)
| (((v4_ucell)x & 1u) << (V4_CELL_BITS - 1)));
ds->t = asr1(x);
break;
case V4_OP_TWO_STAR:
ds->t = (v4_cell)((v4_ucell)ds->t << 1);
break;
case V4_OP_TWO_SLASH:
ds->t = asr1(ds->t);
break;
case V4_OP_INV:
ds->t = (v4_cell)~(v4_ucell)ds->t;
break;
case V4_OP_ADD:
x = v4_dstack_pop(ds); /* old T; T is now old S */
ds->t = add_wrap(ds->t, x);
break;
case V4_OP_AND:
x = v4_dstack_pop(ds);
ds->t = (v4_cell)((v4_ucell)ds->t & (v4_ucell)x);
break;
case V4_OP_XOR:
x = v4_dstack_pop(ds);
ds->t = (v4_cell)((v4_ucell)ds->t ^ (v4_ucell)x);
break;
case V4_OP_DROP:
(void)v4_dstack_pop(ds);
break;
case V4_OP_DUP:
v4_dstack_push(ds, ds->t);
break;
case V4_OP_RPOP:
v4_dstack_push(ds, v4_rstack_pop(rs));
break;
case V4_OP_OVER:
v4_dstack_push(ds, ds->s);
break;
case V4_OP_PUSH_A:
v4_dstack_push(ds, n->a);
break;
case V4_OP_NOP:
break;
case V4_OP_PUSH:
v4_rstack_push(rs, v4_dstack_pop(ds));
break;
case V4_OP_BANG_B:
n->b = v4_dstack_pop(ds);
break;
case V4_OP_BANG_A:
n->a = v4_dstack_pop(ds);
break;
default:
return; /* not an opcode: nothing retires */
}
v4_heat_on_retire(h, op, &es->anticlock);
if (es->on_retire) es->on_retire(es->on_retire_ctx, n, op);
}
unsigned v4_exec_step_word(v4_node *n, v4_exec_state *es, v4_heat *h)
{
v4_iword iw;
unsigned executed = 0;
unsigned slot = 0;
/* D-14: a node that faulted with no handler has stopped. A node that
* has written to its port is blocked until it has been served. */
if (n->stopped || n->asking || n->reading || n->doing) return 0;
if (es->resume && es->resume_faults != n->faults) es->resume = 0; /* it faulted while blocked: P is the handler */
if (es->resume) {
/* unblocked: the rest of the word it was in */
es->resume = 0;
iw = es->resume_iw;
slot = es->resume_slot;
} else {
/* P itself may be what is outside memory */
if (faulted_read(n, n->p)) return 0;
if (v4_node_port_index(n, n->p) >= 0) {
/* P is a port: the node executes what its neighbour writes
* there, and P stays where it is */
if (!v4_node_read_ready(n, n->p)) return 0;
iw = (v4_iword)((v4_ucell)v4_node_fetch(n, n->p) & 0xFFFFFFFFu);
} else {
iw = fetch_iword(n, n->p);
n->p = add_wrap(n->p, 1);
}
}
while (slot < V4_SLOT_COUNT) {
unsigned op = v4_iword_op(iw, slot);
/* Decided before the opcode runs, because running it changes R. */
int again = (op == V4_OP_UNEXT) && (n->rs.r != 0);
unsigned faults = n->faults;
v4_exec_op(n, es, h, op, iw, slot);
if (n->reading) {
/* The fetch was from a port with nothing written to it yet. The
* opcode has not executed: the node waits here, and this slot
* is where it goes on from. */
es->resume = 1;
es->resume_iw = iw;
es->resume_slot = slot;
es->resume_faults = n->faults;
break;
}
if (n->faults != faults) { /* P is the handler now, or the node has stopped */
/* a store that raised an error (D-18) did execute; any other fault means the opcode did not */
if (n->fault_kind == V4_FAULT_RAISED) executed++;
break;
}
executed++;
if (n->asking || n->doing) {
/* The store was to the port, or to Do. The node is blocked here; what is
* left of the word waits until it has been served. */
if (slot + 1 < V4_SLOT_COUNT) {
es->resume = 1;
es->resume_iw = iw;
es->resume_slot = slot + 1;
es->resume_faults = n->faults;
}
break;
}
if (again) {
/* Same word, slot 0, no refetch and P untouched: an `@p` or `!p`
* inside a unext loop therefore walks the words that follow, as
* it does on the F18. */
slot = 0;
continue;
}
if (ends_word(op)) break;
slot++;
}
return executed;
}