v4 effect.c: stack effects derived from code (ADR step 3, task 2)
Walks a word's code, opcode by opcode with the engine's own counts, and gives what it takes and leaves on both stacks, or why it has none. Called and in-line words; recursion, computed jumps, paths that meet at different depths and loops that leave a count have none. Test first: 23 checks failed against a stub. SWAP', UM/MOD, C@, C! and ROUNDTRIP agree with their stack comments. On the nucleus: 214 of 315 headers fixed. v4 test, test-32 and ASan+UBSan pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
@@ -178,3 +178,48 @@ accepted before the next:
|
||||
one-hour background limit at the 64-bit `test_host_mesh`, so the rest was run again from there.
|
||||
`v4-gates.txt` and `v4-sanitize-rest.txt` hold the two parts.
|
||||
- The boot does not use the record, so the kernel and the protocol are unchanged.
|
||||
|
||||
## 9. Task 2 built (2026-11-19): `effect.c`, the stack effects
|
||||
|
||||
**What it is:** `v4/include/v4/effect.h` and `v4/src/effect.c`, a build-machine analysis, not part of the
|
||||
engine or the kernel. `v4_effect_of(e, addr, how)` walks a word's code from its entry and gives what it
|
||||
takes and leaves on both stacks, or why it has none, and where. The opcode counts are the engine's own
|
||||
table (`exec.c`, D-16). Effects of called words are kept, so each is walked once. A callback gives the
|
||||
effects of code outside the range analysed: the ROM's, or another train's.
|
||||
|
||||
**Two ways a word is reached,** from the header's flag 8:
|
||||
- **Called:** it returns with `;` at its own entry depth on the return stack, so its return-stack effect
|
||||
is always 0 → 0. A `;` with its own value on the return stack is a computed jump (EXECUTE, a DEFERred
|
||||
word), and has no fixed effect. So has touching its caller's return address, and `ex`.
|
||||
- **In line:** the body runs to the first `;` wherever the return stack is. `push` in line is data 1 → 0
|
||||
and return stack 0 → 1.
|
||||
|
||||
No fixed effect: paths that meet with different depths (a loop that leaves a count of values), returns
|
||||
that disagree, recursion, a word that never returns, and a call to a word with none.
|
||||
|
||||
**Test first** (`logs/20261119-effect/`): `test_effect`, 34 checks.
|
||||
- Against a stub that derived nothing, the 23 checks that expect a fixed effect failed. The 10 that
|
||||
expect none passed vacuously.
|
||||
- Three of my expectations were wrong, and the analysis was right. I corrected them:
|
||||
- `a! @ !` is 1 → 0, not 2 → 0: the value fetched is stored back;
|
||||
- `drop jump A`, with A `dup`, is 2 → 2: `dup` needs a second cell;
|
||||
- `3 FOR dup drop NEXT` is 1 → 1: the `dup` needs a cell already there. The fixed-loop check now uses
|
||||
`5 drop`, and the 1 → 1 case is a check of its own.
|
||||
- **Five real words agree with their stack comments:** SWAP', UM/MOD, C@, C! and ROUNDTRIP (2 → 1, through
|
||||
its `jump C@`).
|
||||
|
||||
**On the nucleus** (`nucleus-dictionary.txt`, a one-off measurement): of 315 headers, 65 of them in line,
|
||||
214 have a fixed effect and 101 have none.
|
||||
- 78 call a word with no fixed effect;
|
||||
- 11 have a computed jump;
|
||||
- 8 have returns that disagree;
|
||||
- 4 touch their caller's return address.
|
||||
|
||||
Under ruling §6.4, the 214 would be the nucleus's exports. The nucleus is not an artifact (§6.2a), so
|
||||
this is a measure of the analysis, not an interface.
|
||||
|
||||
**Verified:**
|
||||
- `make -C v4 test` passes, with all three mesh boots at POST 990/990.
|
||||
- `make -C v4 test-32` passes.
|
||||
- `make -C v4 sanitize` passes: every test at both widths, and the mesh boot at POST 990/990, under
|
||||
ASan+UBSan.
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
make: Entering directory '/home/user/LithosAnanke/v4'
|
||||
[V4_CELL_BITS=64 run-64-test_effect.c]
|
||||
v4 stack effects: V4_CELL_BITS=64
|
||||
34 checks, 0 failures
|
||||
[V4_CELL_BITS=32 run-32-test_effect.c]
|
||||
v4 stack effects: V4_CELL_BITS=32
|
||||
34 checks, 0 failures
|
||||
make: Leaving directory '/home/user/LithosAnanke/v4'
|
||||
@@ -0,0 +1,52 @@
|
||||
make: Entering directory '/home/user/LithosAnanke/v4'
|
||||
[V4_CELL_BITS=64 run-64-test_effect.c]
|
||||
v4 stack effects: V4_CELL_BITS=64
|
||||
D: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:119: dup: 1 -> 2
|
||||
X: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:120: drop: 1 -> 0
|
||||
K: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:121: a literal: 0 -> 1
|
||||
P: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:122: +: 2 -> 1
|
||||
O: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:123: over: 2 -> 3
|
||||
V: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:124: 2* inv: 1 -> 1
|
||||
S: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:125: a! @ !: 2 -> 0
|
||||
T: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:126: push and pop of its own: 1 -> 1, return stack 0 -> 0
|
||||
B: no fixed effect at 17: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:129: a call applies the callee's effect
|
||||
J: no fixed effect at 17: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:130: a jump to a word goes on there
|
||||
A: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:131: a word with no ; runs on into the next
|
||||
G: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:136: two paths that agree: if looks at T
|
||||
F: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:139: FOR ... NEXT that keeps the stacks: fixed
|
||||
U: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:141: FOR ... UNEXT: fixed
|
||||
PU: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:149: push in line: data 1 -> 0, return stack 0 -> 1
|
||||
PO: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:150: pop in line: data 0 -> 1, return stack 1 -> 0
|
||||
PL: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:151: + in line: as called
|
||||
XC: no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:155: a call outside the code: the effect it is given
|
||||
SWAP': no fixed effect at 16: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:159: SWAP' ( a b -- b a )
|
||||
UM/MOD: no fixed effect at 18: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:160: UM/MOD ( ulo uhi ud -- urem uquot )
|
||||
C@: no fixed effect at 44: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:161: C@ ( baddr -- c )
|
||||
C!: no fixed effect at 65: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:162: C! ( c baddr -- )
|
||||
ROUNDTRIP: no fixed effect at 92: not derived yet
|
||||
FAIL /home/user/LithosAnanke/v4/tests/test_effect.c:163: ROUNDTRIP ( c baddr -- c' )
|
||||
33 checks, 23 failures
|
||||
make: *** [Makefile:358: run-64-test_effect.c] Error 1
|
||||
make: Leaving directory '/home/user/LithosAnanke/v4'
|
||||
@@ -0,0 +1,7 @@
|
||||
The nucleus's dictionary, analysed by effect.c (a one-off measurement; the program is not kept).
|
||||
Each header's word, in line if its flag 8 is set, else as called; 64-bit cells.
|
||||
headers 315 (in line 65): fixed 214, none 101
|
||||
78 it calls a word with no fixed effect
|
||||
4 it touches its caller's return address
|
||||
8 two returns leave different depths
|
||||
11 ; to an address of its own: a computed jump
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,72 @@
|
||||
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
|
||||
* Copyright (c) 2023-2026 Robert A. James
|
||||
*/
|
||||
/* effect.h -- derive a word's stack effect from its compiled code.
|
||||
*
|
||||
* docs/v4.0.0/plans/2026-11-19-f18-artifact-build.md section 2, task 2; ABI.md section 9, choice 2,
|
||||
* "Derived by the tool", and 2c. A build-machine analysis, not part of the engine: the host tool
|
||||
* (mkf18a) records each exported word's effect in its artifact, and the proofs check it.
|
||||
*
|
||||
* THE WALK. From the word's entry, every path is followed instruction word by instruction word,
|
||||
* slot by slot, counting what each opcode takes from and leaves on the data stack and the return
|
||||
* stack, as the engine's own table has it (v4/src/exec.c, D-16):
|
||||
* - a literal (`@p`) and the fetches push one; the stores, `drop`, `a!`, `b!` pop one;
|
||||
* - `+ and xor` take two and leave one; `+*` looks at two; `2* 2/ inv` and `if -if` look at one;
|
||||
* - `dup` and `over` push one, after looking at one and two;
|
||||
* - `push` moves one to the return stack, `pop` one back;
|
||||
* - `jump` goes on at its target; `if`, `-if` and `next` go on at both their target and the next
|
||||
* word, `next` and `unext` counting the return stack's top down;
|
||||
* - `call` applies the effect of the word it calls, derived first, and goes on.
|
||||
* Where two paths meet at the same instruction word with different depths, the word has no fixed
|
||||
* effect. So a loop that leaves a count of values has none, while a loop that leaves the stacks as
|
||||
* it found them has one, whatever its count.
|
||||
*
|
||||
* HOW A WORD ENDS.
|
||||
* - Called (V4_EFFECT_CALLED): it returns with `;` when the return stack is back where it was on
|
||||
* entry, its caller's return address on top. Every return must leave the same depths. A `;`
|
||||
* with something of the word's own on the return stack goes to a computed address (EXECUTE, a
|
||||
* DEFERred word): no fixed effect. So does touching the caller's return address (`pop` or `ex`
|
||||
* with nothing of the word's own pushed, or `ex` at all). A called word's return-stack effect is
|
||||
* therefore always 0 -> 0.
|
||||
* - In line (V4_EFFECT_INLINE, a header's flag 8): the compiler copies the body to the first `;`,
|
||||
* and that `;` ends it wherever the return stack is. The body may take from the return stack
|
||||
* and leave on it: `push` is 1 -> 0 on the data stack and 0 -> 1 on the return stack.
|
||||
* A word with no way to return has no fixed effect.
|
||||
*
|
||||
* WHAT IS GIVEN. `taken` is the deepest the word reaches below its entry; `left` is what it leaves
|
||||
* counted from there. `dup` takes 1 and leaves 2.
|
||||
*/
|
||||
#ifndef V4_EFFECT_H
|
||||
#define V4_EFFECT_H
|
||||
|
||||
#include "v4/node.h"
|
||||
|
||||
#define V4_EFFECT_CALLED 0
|
||||
#define V4_EFFECT_INLINE 1
|
||||
|
||||
typedef struct {
|
||||
int fixed; /* 1: the effect below is the word's; 0: it has none */
|
||||
unsigned d_taken, d_left; /* the data stack */
|
||||
unsigned r_taken, r_left; /* the return stack */
|
||||
v4_cell at; /* when not fixed: the instruction word where that was found */
|
||||
const char *why; /* when not fixed: why */
|
||||
} v4_effect;
|
||||
|
||||
/* The effect of code outside the range analysed (the ROM's words, another train's): 1 and *out, or
|
||||
* 0 if it is not known. */
|
||||
typedef int (*v4_effect_extern)(void *ctx, v4_cell addr, v4_effect *out);
|
||||
|
||||
typedef struct v4_effects v4_effects;
|
||||
|
||||
/* Analyse the code in [lo, hi) of node `n`. `ext` may be NULL: a call outside the range then has
|
||||
* no fixed effect. NULL if there is no memory for it. */
|
||||
v4_effects *v4_effects_new(const v4_node *n, v4_cell lo, v4_cell hi, v4_effect_extern ext, void *ext_ctx);
|
||||
|
||||
/* The effect of the word whose entry is `addr`, reached as `how` (V4_EFFECT_CALLED or
|
||||
* V4_EFFECT_INLINE). A called word's effect is kept, so a word called from many places is walked
|
||||
* once. */
|
||||
v4_effect v4_effect_of(v4_effects *e, v4_cell addr, int how);
|
||||
|
||||
void v4_effects_free(v4_effects *e);
|
||||
|
||||
#endif /* V4_EFFECT_H */
|
||||
+291
@@ -0,0 +1,291 @@
|
||||
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
|
||||
* Copyright (c) 2023-2026 Robert A. James
|
||||
*/
|
||||
/* effect.c -- derive a word's stack effect from its compiled code (effect.h). */
|
||||
#include "v4/effect.h"
|
||||
#include "v4/opcode.h"
|
||||
#include "v4/iword.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
/* a called word's effect, kept: 0 not yet, 1 being walked, 2 known */
|
||||
struct v4_effects {
|
||||
const v4_node *n;
|
||||
v4_cell lo, hi;
|
||||
v4_effect_extern ext;
|
||||
void *ext_ctx;
|
||||
unsigned char *status;
|
||||
v4_effect *memo;
|
||||
};
|
||||
|
||||
/* One walk's state: where it has been, with what depths, and where it has still to go. */
|
||||
typedef struct { v4_cell w; long d, r; } point;
|
||||
typedef struct {
|
||||
v4_effects *e;
|
||||
int how;
|
||||
point *seen; size_t nseen, capseen;
|
||||
point *todo; size_t ntodo, captodo;
|
||||
long min_d, min_r;
|
||||
int returned;
|
||||
long ret_d, ret_r;
|
||||
v4_effect out; /* .fixed 0 and .why set once something is found */
|
||||
} walk;
|
||||
|
||||
static void lose(walk *k, v4_cell at, const char *why)
|
||||
{
|
||||
if (k->out.why) return; /* the first reason is kept */
|
||||
k->out.fixed = 0;
|
||||
k->out.at = at;
|
||||
k->out.why = why;
|
||||
}
|
||||
|
||||
static int add(point **v, size_t *nv, size_t *cap, point p)
|
||||
{
|
||||
if (*nv == *cap) {
|
||||
size_t c = *cap ? *cap * 2u : 64u;
|
||||
point *g = realloc(*v, c * sizeof *g);
|
||||
if (!g) return 0;
|
||||
*v = g;
|
||||
*cap = c;
|
||||
}
|
||||
(*v)[(*nv)++] = p;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Go on at `w` with depths d and r. */
|
||||
static void go(walk *k, v4_cell w, long d, long r)
|
||||
{
|
||||
point p;
|
||||
p.w = w; p.d = d; p.r = r;
|
||||
if (!add(&k->todo, &k->ntodo, &k->captodo, p)) lose(k, w, "out of memory");
|
||||
}
|
||||
|
||||
/* The data stack must hold `need` of the word's or its caller's; the return stack `rneed`. A
|
||||
* called word may not reach under its own return address. */
|
||||
static int needs(walk *k, v4_cell at, long d, unsigned need, long r, unsigned rneed)
|
||||
{
|
||||
if (d - (long)need < k->min_d) k->min_d = d - (long)need;
|
||||
if (r - (long)rneed < k->min_r) {
|
||||
if (k->how == V4_EFFECT_CALLED) { lose(k, at, "it touches its caller's return address"); return 0; }
|
||||
k->min_r = r - (long)rneed;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void returns(walk *k, v4_cell at, long d, long r)
|
||||
{
|
||||
if (!k->returned) { k->returned = 1; k->ret_d = d; k->ret_r = r; return; }
|
||||
if (d != k->ret_d || r != k->ret_r) lose(k, at, "two returns leave different depths");
|
||||
}
|
||||
|
||||
static v4_effect effect_called(v4_effects *e, v4_cell addr);
|
||||
|
||||
/* The effect of a call to `target`: 1 and *ce, or 0 having lost. */
|
||||
static int callee(walk *k, v4_cell at, v4_cell target, v4_effect *ce)
|
||||
{
|
||||
if (target >= k->e->lo && target < k->e->hi) {
|
||||
*ce = effect_called(k->e, target);
|
||||
if (!ce->fixed) { lose(k, at, "it calls a word with no fixed effect"); return 0; }
|
||||
return 1;
|
||||
}
|
||||
if (k->e->ext && k->e->ext(k->e->ext_ctx, target, ce) && ce->fixed) return 1;
|
||||
lose(k, at, "it calls outside the code, a word whose effect is not known");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* One instruction word, entered at slot 0 with depths d and r. */
|
||||
static void step(walk *k, v4_cell w, long d, long r)
|
||||
{
|
||||
const v4_node *n = k->e->n;
|
||||
v4_iword word;
|
||||
v4_cell p = w + 1;
|
||||
unsigned slot;
|
||||
v4_effect ce;
|
||||
|
||||
if (w < k->e->lo || w >= k->e->hi) { lose(k, w, "it runs outside the code"); return; }
|
||||
word = (v4_iword)n->mem[w];
|
||||
for (slot = 0; slot < V4_SLOT_COUNT && !k->out.why; slot++) {
|
||||
unsigned op = v4_iword_op(word, slot);
|
||||
if (v4_op_is_branch(op) && !v4_iword_branch_legal(slot)) { lose(k, w, "a branch in slot 4 or 5"); return; }
|
||||
switch (op) {
|
||||
case V4_OP_SEMI:
|
||||
if (k->how == V4_EFFECT_CALLED && r > 0) { lose(k, w, "; to an address of its own: a computed jump"); return; }
|
||||
if (!needs(k, w, d, 0, r, k->how == V4_EFFECT_CALLED ? 0u : 0u)) return;
|
||||
returns(k, w, d, r);
|
||||
return;
|
||||
case V4_OP_EX:
|
||||
lose(k, w, "ex: the return address is exchanged");
|
||||
return;
|
||||
case V4_OP_JUMP: {
|
||||
v4_cell t = v4_iword_branch(p, word, slot);
|
||||
if (t >= k->e->lo && t < k->e->hi) { go(k, t, d, r); return; }
|
||||
if (!callee(k, w, t, &ce)) return; /* a tail call outside the code */
|
||||
if (!needs(k, w, d, ce.d_taken, r, ce.r_taken)) return;
|
||||
returns(k, w, d - (long)ce.d_taken + (long)ce.d_left, r - (long)ce.r_taken + (long)ce.r_left);
|
||||
return;
|
||||
}
|
||||
case V4_OP_CALL: {
|
||||
v4_cell t = v4_iword_branch(p, word, slot);
|
||||
if (!callee(k, w, t, &ce)) return;
|
||||
if (!needs(k, w, d, ce.d_taken, r, ce.r_taken)) return;
|
||||
go(k, p, d - (long)ce.d_taken + (long)ce.d_left, r - (long)ce.r_taken + (long)ce.r_left);
|
||||
return;
|
||||
}
|
||||
case V4_OP_IF: case V4_OP_MINUS_IF:
|
||||
if (!needs(k, w, d, 1, r, 0)) return;
|
||||
go(k, v4_iword_branch(p, word, slot), d, r);
|
||||
go(k, p, d, r);
|
||||
return;
|
||||
case V4_OP_NEXT:
|
||||
if (!needs(k, w, d, 0, r, 1)) return;
|
||||
go(k, v4_iword_branch(p, word, slot), d, r);
|
||||
go(k, p, d, r - 1);
|
||||
return;
|
||||
case V4_OP_UNEXT:
|
||||
if (!needs(k, w, d, 0, r, 1)) return;
|
||||
go(k, w, d, r); /* the word again, from slot 0 */
|
||||
r--; /* or on, the count popped */
|
||||
break;
|
||||
case V4_OP_FETCH_P:
|
||||
d++; p++;
|
||||
break;
|
||||
case V4_OP_FETCH_INC: case V4_OP_FETCH_B: case V4_OP_FETCH_A: case V4_OP_PUSH_A:
|
||||
d++;
|
||||
break;
|
||||
case V4_OP_STORE_P:
|
||||
if (!needs(k, w, d, 1, r, 0)) return;
|
||||
d--; p++;
|
||||
break;
|
||||
case V4_OP_STORE_INC: case V4_OP_STORE_B: case V4_OP_STORE_A:
|
||||
case V4_OP_DROP: case V4_OP_BANG_B: case V4_OP_BANG_A:
|
||||
if (!needs(k, w, d, 1, r, 0)) return;
|
||||
d--;
|
||||
break;
|
||||
case V4_OP_MUL_STEP:
|
||||
if (!needs(k, w, d, 2, r, 0)) return;
|
||||
break;
|
||||
case V4_OP_TWO_STAR: case V4_OP_TWO_SLASH: case V4_OP_INV:
|
||||
if (!needs(k, w, d, 1, r, 0)) return;
|
||||
break;
|
||||
case V4_OP_ADD: case V4_OP_AND: case V4_OP_XOR:
|
||||
if (!needs(k, w, d, 2, r, 0)) return;
|
||||
d--;
|
||||
break;
|
||||
case V4_OP_DUP:
|
||||
if (!needs(k, w, d, 1, r, 0)) return;
|
||||
d++;
|
||||
break;
|
||||
case V4_OP_OVER:
|
||||
if (!needs(k, w, d, 2, r, 0)) return;
|
||||
d++;
|
||||
break;
|
||||
case V4_OP_RPOP:
|
||||
if (!needs(k, w, d, 0, r, 1)) return;
|
||||
r--; d++;
|
||||
break;
|
||||
case V4_OP_PUSH:
|
||||
if (!needs(k, w, d, 1, r, 0)) return;
|
||||
d--; r++;
|
||||
break;
|
||||
case V4_OP_NOP:
|
||||
break;
|
||||
default:
|
||||
lose(k, w, "an opcode it does not know");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!k->out.why) go(k, p, d, r); /* on into the next word */
|
||||
}
|
||||
|
||||
static v4_effect walk_from(v4_effects *e, v4_cell entry, int how)
|
||||
{
|
||||
walk k;
|
||||
size_t i;
|
||||
|
||||
k.e = e; k.how = how;
|
||||
k.seen = NULL; k.nseen = k.capseen = 0;
|
||||
k.todo = NULL; k.ntodo = k.captodo = 0;
|
||||
k.min_d = 0; k.min_r = 0;
|
||||
k.returned = 0; k.ret_d = 0; k.ret_r = 0;
|
||||
k.out.fixed = 0; k.out.d_taken = k.out.d_left = k.out.r_taken = k.out.r_left = 0;
|
||||
k.out.at = entry; k.out.why = NULL;
|
||||
|
||||
go(&k, entry, 0, 0);
|
||||
while (k.ntodo && !k.out.why) {
|
||||
point p = k.todo[--k.ntodo];
|
||||
int known = 0;
|
||||
for (i = 0; i < k.nseen; i++)
|
||||
if (k.seen[i].w == p.w) {
|
||||
if (k.seen[i].d != p.d || k.seen[i].r != p.r) lose(&k, p.w, "two paths meet with different depths");
|
||||
known = 1;
|
||||
break;
|
||||
}
|
||||
if (known) continue;
|
||||
if (!add(&k.seen, &k.nseen, &k.capseen, p)) { lose(&k, p.w, "out of memory"); break; }
|
||||
step(&k, p.w, p.d, p.r);
|
||||
}
|
||||
if (!k.out.why && !k.returned) lose(&k, entry, "it never returns");
|
||||
if (!k.out.why) {
|
||||
k.out.fixed = 1;
|
||||
k.out.d_taken = (unsigned)-k.min_d;
|
||||
k.out.d_left = (unsigned)(k.ret_d - k.min_d);
|
||||
k.out.r_taken = (unsigned)-k.min_r;
|
||||
k.out.r_left = (unsigned)(k.ret_r - k.min_r);
|
||||
k.out.at = entry;
|
||||
}
|
||||
free(k.seen);
|
||||
free(k.todo);
|
||||
return k.out;
|
||||
}
|
||||
|
||||
static v4_effect effect_called(v4_effects *e, v4_cell addr)
|
||||
{
|
||||
size_t i = (size_t)(addr - e->lo);
|
||||
if (e->status[i] == 2) return e->memo[i];
|
||||
if (e->status[i] == 1) {
|
||||
v4_effect r = { 0, 0, 0, 0, 0, 0, "it calls itself" };
|
||||
r.at = addr;
|
||||
return r;
|
||||
}
|
||||
e->status[i] = 1;
|
||||
e->memo[i] = walk_from(e, addr, V4_EFFECT_CALLED);
|
||||
e->status[i] = 2;
|
||||
return e->memo[i];
|
||||
}
|
||||
|
||||
v4_effects *v4_effects_new(const v4_node *n, v4_cell lo, v4_cell hi, v4_effect_extern ext, void *ext_ctx)
|
||||
{
|
||||
v4_effects *e;
|
||||
size_t len;
|
||||
if (lo < 0 || hi < lo || (v4_ucell)hi > V4_NODE_WORDS) return NULL;
|
||||
len = (size_t)(hi - lo) + 1u;
|
||||
e = malloc(sizeof *e);
|
||||
if (!e) return NULL;
|
||||
e->n = n;
|
||||
e->lo = lo;
|
||||
e->hi = hi;
|
||||
e->ext = ext;
|
||||
e->ext_ctx = ext_ctx;
|
||||
e->status = calloc(len, 1);
|
||||
e->memo = calloc(len, sizeof *e->memo);
|
||||
if (!e->status || !e->memo) { v4_effects_free(e); return NULL; }
|
||||
return e;
|
||||
}
|
||||
|
||||
v4_effect v4_effect_of(v4_effects *e, v4_cell addr, int how)
|
||||
{
|
||||
if (addr < e->lo || addr >= e->hi) {
|
||||
v4_effect r = { 0, 0, 0, 0, 0, 0, "not a word of the code" };
|
||||
r.at = addr;
|
||||
return r;
|
||||
}
|
||||
if (how == V4_EFFECT_CALLED) return effect_called(e, addr);
|
||||
return walk_from(e, addr, V4_EFFECT_INLINE);
|
||||
}
|
||||
|
||||
void v4_effects_free(v4_effects *e)
|
||||
{
|
||||
if (!e) return;
|
||||
free(e->status);
|
||||
free(e->memo);
|
||||
free(e);
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
|
||||
* Copyright (c) 2023-2026 Robert A. James
|
||||
*/
|
||||
/* test_effect.c -- a word's stack effect, derived from its code (effect.h).
|
||||
*
|
||||
* docs/v4.0.0/plans/2026-11-19-f18-artifact-build.md section 4, test 2. Each text is assembled by
|
||||
* text.c and its words analysed: words of known effect, the loops and branches that keep a fixed
|
||||
* effect and those that lose it, words in line, calls outside the code, and four real words
|
||||
* (test_text.c's, from DECOMPOSITION.md) whose effects are known from their stack comments.
|
||||
*/
|
||||
#include "v4/effect.h"
|
||||
#include "v4/text.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static int failures = 0, checks = 0;
|
||||
#define CHECK(c,...) do{checks++; if(!(c)){failures++; printf("FAIL %s:%d: ",__FILE__,__LINE__); printf(__VA_ARGS__); printf("\n");}}while(0)
|
||||
|
||||
#define ORIGIN 16
|
||||
|
||||
static v4_node n;
|
||||
static v4_text tx;
|
||||
static v4_effects *fx;
|
||||
|
||||
/* test_text.c's four definitions, from DECOMPOSITION.md sections 4 and 5.3, with their stack comments */
|
||||
static const char four[] =
|
||||
"macro SWAP over push push drop pop pop endmacro\n"
|
||||
": SWAP' ( a b -- b a ) SWAP ;\n"
|
||||
": UM/MOD ( ulo uhi ud -- urem uquot )\n"
|
||||
" a! N-1 FOR\n"
|
||||
" -if L0\n"
|
||||
" 2* over -if L1 drop 1 + jump L2\n"
|
||||
" L1: drop L2: push 2* pop\n"
|
||||
" jump SUB\n"
|
||||
" L0:\n"
|
||||
" 2* over -if L3 drop 1 + jump L4\n"
|
||||
" L3: drop L4: push 2* pop\n"
|
||||
" dup a xor -if L5\n"
|
||||
" drop -if NOSUB jump SUB\n"
|
||||
" L5: drop dup inv a + inv -if L6\n"
|
||||
" drop jump NOSUB\n"
|
||||
" L6: push drop pop jump SETBIT\n"
|
||||
" SUB: inv a + inv\n"
|
||||
" SETBIT: push 1 + pop\n"
|
||||
" NOSUB:\n"
|
||||
" NEXT\n"
|
||||
" SWAP ;\n"
|
||||
": C@ ( baddr -- c )\n"
|
||||
" dup 2/ 2/ a! 3 and\n"
|
||||
" if K0 -1 + if K1 -1 + if K2\n"
|
||||
" drop @ 23 FOR 2/ UNEXT 255 and ;\n"
|
||||
" K2: drop @ 15 FOR 2/ UNEXT 255 and ;\n"
|
||||
" K1: drop @ 7 FOR 2/ UNEXT 255 and ;\n"
|
||||
" K0: drop @ 255 and ;\n"
|
||||
": C! ( c baddr -- )\n"
|
||||
" dup 2/ 2/ a! 3 and push 255 and pop\n"
|
||||
" if K0 -1 + if K1 -1 + if K2\n"
|
||||
" drop 23 FOR 2* UNEXT @ 4278190080 inv and + ! ;\n"
|
||||
" K2: drop 15 FOR 2* UNEXT @ -16711681 and + ! ;\n"
|
||||
" K1: drop 7 FOR 2* UNEXT @ -65281 and + ! ;\n"
|
||||
" K0: drop @ -256 and + ! ;\n"
|
||||
": ROUNDTRIP ( c baddr -- c' ) dup push C! pop jump C@\n";
|
||||
|
||||
/* A word outside the code analysed: XW, assembled after the range's end. What the callback says
|
||||
* of it. */
|
||||
static int ext_effect(void *ctx, v4_cell addr, v4_effect *out)
|
||||
{
|
||||
(void)ctx;
|
||||
if (addr != v4_text_word(&tx, "XW")) return 0;
|
||||
out->fixed = 1;
|
||||
out->d_taken = 2; out->d_left = 1;
|
||||
out->r_taken = 0; out->r_left = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Assemble `src` and start analysing its code, up to the word XW if there is one; 1 if it
|
||||
* assembled. */
|
||||
static int build(const char *src, int with_ext)
|
||||
{
|
||||
v4_cell hi;
|
||||
if (fx) v4_effects_free(fx);
|
||||
fx = NULL;
|
||||
v4_node_reset(&n);
|
||||
v4_text_begin(&tx, &n, ORIGIN);
|
||||
v4_text_constant(&tx, "N-1", V4_CELL_BITS - 1);
|
||||
if (!v4_text_assemble(&tx, src) || !v4_text_finish(&tx)) { printf(" %s\n", v4_text_error(&tx)); return 0; }
|
||||
hi = v4_text_word(&tx, "XW") >= 0 ? v4_text_word(&tx, "XW") : v4_text_here(&tx);
|
||||
fx = v4_effects_new(&n, ORIGIN, hi, with_ext ? ext_effect : NULL, NULL);
|
||||
return fx != NULL;
|
||||
}
|
||||
|
||||
/* 1 if NAME, reached as `how`, has the fixed effect given */
|
||||
static int is(const char *name, int how, unsigned dt, unsigned dl, unsigned rt, unsigned rl)
|
||||
{
|
||||
v4_effect e = v4_effect_of(fx, v4_text_word(&tx, name), how);
|
||||
if (e.fixed && e.d_taken == dt && e.d_left == dl && e.r_taken == rt && e.r_left == rl) return 1;
|
||||
if (e.fixed) printf(" %s: d %u -> %u, r %u -> %u\n", name, e.d_taken, e.d_left, e.r_taken, e.r_left);
|
||||
else printf(" %s: no fixed effect at %ld: %s\n", name, (long)e.at, e.why ? e.why : "?");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 1 if NAME, reached as `how`, has no fixed effect */
|
||||
static int none(const char *name, int how)
|
||||
{
|
||||
v4_effect e = v4_effect_of(fx, v4_text_word(&tx, name), how);
|
||||
if (!e.fixed) return 1;
|
||||
printf(" %s: d %u -> %u, r %u -> %u\n", name, e.d_taken, e.d_left, e.r_taken, e.r_left);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define C V4_EFFECT_CALLED
|
||||
#define I V4_EFFECT_INLINE
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("v4 stack effects: V4_CELL_BITS=%d\n", V4_CELL_BITS);
|
||||
|
||||
/* opcodes */
|
||||
CHECK(build(": D dup ;", 0) && is("D", C, 1, 2, 0, 0), "dup: 1 -> 2");
|
||||
CHECK(build(": X drop ;", 0) && is("X", C, 1, 0, 0, 0), "drop: 1 -> 0");
|
||||
CHECK(build(": K 5 ;", 0) && is("K", C, 0, 1, 0, 0), "a literal: 0 -> 1");
|
||||
CHECK(build(": P + ;", 0) && is("P", C, 2, 1, 0, 0), "+: 2 -> 1");
|
||||
CHECK(build(": O over ;", 0) && is("O", C, 2, 3, 0, 0), "over: 2 -> 3");
|
||||
CHECK(build(": V 2* inv ;", 0) && is("V", C, 1, 1, 0, 0), "2* inv: 1 -> 1");
|
||||
CHECK(build(": S a! @ ! ;", 0) && is("S", C, 1, 0, 0, 0), "a! @ !: 1 -> 0, the value fetched stored back");
|
||||
CHECK(build(": T dup push drop pop ;", 0) && is("T", C, 1, 1, 0, 0), "push and pop of its own: 1 -> 1, return stack 0 -> 0");
|
||||
|
||||
/* calls, jumps, running on */
|
||||
CHECK(build(": A dup ; : B A A ;", 0) && is("B", C, 1, 3, 0, 0), "a call applies the callee's effect");
|
||||
CHECK(build(": A dup ; : J drop jump A", 0) && is("J", C, 2, 2, 0, 0), "a jump to a word goes on there: drop, then dup needs a second");
|
||||
CHECK(build(": A dup : B drop ;", 0) && is("A", C, 1, 1, 0, 0), "a word with no ; runs on into the next");
|
||||
CHECK(build(": R R ;", 0) && none("R", C), "a word that calls itself: none");
|
||||
CHECK(build(": W L: jump L", 0) && none("W", C), "a word that never returns: none");
|
||||
|
||||
/* branches and loops */
|
||||
CHECK(build(": G if L dup drop L: ;", 0) && is("G", C, 1, 1, 0, 0), "two paths that agree: if looks at T");
|
||||
CHECK(build(": H if L dup L: ;", 0) && none("H", C), "two paths that disagree: none");
|
||||
CHECK(build(": Z if L drop ; L: ;", 0) && none("Z", C), "two returns that disagree: none");
|
||||
CHECK(build(": F 3 FOR 5 drop NEXT ;", 0) && is("F", C, 0, 0, 0, 0), "FOR ... NEXT that keeps the stacks: fixed");
|
||||
CHECK(build(": F2 3 FOR dup drop NEXT ;", 0) && is("F2", C, 1, 1, 0, 0), "a loop body that looks at a cell below: it is taken");
|
||||
CHECK(build(": N FOR dup NEXT ;", 0) && none("N", C), "a loop that leaves a count of values: none");
|
||||
CHECK(build(": U 3 FOR 2* UNEXT ;", 0) && is("U", C, 1, 1, 0, 0), "FOR ... UNEXT: fixed");
|
||||
|
||||
/* control through the return stack */
|
||||
CHECK(build(": E push ;", 0) && none("E", C), "push ; -- EXECUTE, a computed address: none");
|
||||
CHECK(build(": Q pop drop ;", 0) && none("Q", C), "pop of the caller's return address: none");
|
||||
CHECK(build(": Y ex ;", 0) && none("Y", C), "ex: none");
|
||||
|
||||
/* in line */
|
||||
CHECK(build(": PU push ;", 0) && is("PU", I, 1, 0, 0, 1), "push in line: data 1 -> 0, return stack 0 -> 1");
|
||||
CHECK(build(": PO pop ;", 0) && is("PO", I, 0, 1, 1, 0), "pop in line: data 0 -> 1, return stack 1 -> 0");
|
||||
CHECK(build(": PL + ;", 0) && is("PL", I, 2, 1, 0, 0), "+ in line: as called");
|
||||
|
||||
/* outside the code */
|
||||
CHECK(build(": XC XW ; : XW + ;", 0) && none("XC", C), "a call outside the code, with nothing known of it: none");
|
||||
CHECK(build(": XC XW ; : XW + ;", 1) && is("XC", C, 2, 1, 0, 0), "a call outside the code: the effect it is given");
|
||||
|
||||
/* four real words, against their stack comments */
|
||||
CHECK(build(four, 0), "the four words assemble");
|
||||
CHECK(is("SWAP'", C, 2, 2, 0, 0), "SWAP' ( a b -- b a )");
|
||||
CHECK(is("UM/MOD", C, 3, 2, 0, 0), "UM/MOD ( ulo uhi ud -- urem uquot )");
|
||||
CHECK(is("C@", C, 1, 1, 0, 0), "C@ ( baddr -- c )");
|
||||
CHECK(is("C!", C, 2, 0, 0, 0), "C! ( c baddr -- )");
|
||||
CHECK(is("ROUNDTRIP", C, 2, 1, 0, 0), "ROUNDTRIP ( c baddr -- c' )");
|
||||
|
||||
if (fx) v4_effects_free(fx);
|
||||
printf(" %d checks, %d failures\n", checks, failures);
|
||||
return failures ? 1 : 0;
|
||||
}
|
||||
Reference in New Issue
Block a user