Files
LithosAnanke/v4/src/effect.c
T
Claude daf614285a
Build / Image RASPI (push) Successful in 4m35s
Build / Image PC (push) Successful in 4m20s
Build / Image MILKV (push) Successful in 4m6s
Build / v4 tests (32- and 64-bit cells) (push) Successful in 15m23s
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
2026-11-19 19:28:33 +00:00

292 lines
9.6 KiB
C

/* 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);
}