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
170 lines
7.8 KiB
C
170 lines
7.8 KiB
C
/* 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;
|
|
}
|