@asmlift/core 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,72 @@
1
+ // asmlift — SSA cleanups that belong to the substrate, not to any one pass.
2
+ //
3
+ // Peer of `pattern/engine.ts`'s `dce`: general, opcode-agnostic, and callable by anything that has
4
+ // just changed the CFG.
5
+ import { Block, Fn, Successor, Value, replaceAllUsesWith } from './core';
6
+
7
+ /**
8
+ * Remove block params that are really TRIVIAL PHIS — those whose incoming args, across every
9
+ * predecessor edge and ignoring a self-reference from a back edge, are all one value. Such a param
10
+ * carries no join information, so it is replaced by that value and the arg dropped from each
11
+ * predecessor's terminator. Returns how many were removed.
12
+ *
13
+ * Iterated to a fixpoint: removing one phi can make the next trivial.
14
+ *
15
+ * DOMINANCE is free. If every predecessor passes `v`, then `v` is defined before each of those
16
+ * terminators and every path into the block goes through one of them — so `v` dominates the block
17
+ * and every use the param had. The self-reference waiver preserves that: a class where every edge
18
+ * passes the param itself has no first dynamic entry, so the first real entry always arrives on a
19
+ * `v`-passing edge.
20
+ *
21
+ * THE ENTRY BLOCK IS NEVER TOUCHED. Its params are the function's own parameters. Braun's
22
+ * construction in `frontend/ssa.ts` used to rely on entry having no in-edges to get this for free —
23
+ * which stops being true for an entry block that is ALSO a loop header, a shape this codebase does
24
+ * have (`raise/shortcircuit.ts` and `raise/retsink.ts` both guard it explicitly, the former after a
25
+ * reproduced silent miscompile). The guard is stated rather than inherited from an accident.
26
+ *
27
+ * `onRemoved` lets a caller drop its own bookkeeping for the retired param (the frontend's phi-block
28
+ * map); it is called once per removal, before the param is spliced out.
29
+ */
30
+ export function simplifyTrivialPhis(fn: Fn, onRemoved?: (param: Value) => void): number {
31
+ const edgesTo = (b: Block): Successor[] => {
32
+ const out: Successor[] = [];
33
+ for (const pb of fn.blocks) {
34
+ for (const op of pb.ops) {
35
+ for (const s of op.successors) {
36
+ if (s.block === b) {
37
+ out.push(s);
38
+ }
39
+ }
40
+ }
41
+ }
42
+ return out;
43
+ };
44
+ let removed = 0;
45
+ for (;;) {
46
+ let changed = false;
47
+ for (const b of fn.blocks) {
48
+ if (b === fn.blocks[0]) {
49
+ continue;
50
+ }
51
+ const incoming = edgesTo(b);
52
+ for (let i = b.params.length - 1; i >= 0; i--) {
53
+ const param = b.params[i];
54
+ const distinct = [...new Set(incoming.map((s) => s.args[i]).filter((v) => v !== param))];
55
+ if (distinct.length !== 1) {
56
+ continue; // a genuine join, or unreachable (no in-edges at all)
57
+ }
58
+ replaceAllUsesWith(fn, param, distinct[0]);
59
+ onRemoved?.(param);
60
+ b.params.splice(i, 1);
61
+ for (const s of incoming) {
62
+ s.args.splice(i, 1);
63
+ }
64
+ removed++;
65
+ changed = true;
66
+ }
67
+ }
68
+ if (!changed) {
69
+ return removed;
70
+ }
71
+ }
72
+ }
@@ -0,0 +1,216 @@
1
+ // L3 re-spelling lever: materialize the deref BASES of a call's arguments into locals, before the
2
+ // call.
3
+ //
4
+ // When a call's arguments are each a deref through a different fixed address, the compiler loads
5
+ // BOTH addresses before dereferencing either — it needs two registers live across the argument
6
+ // setup, so it emits the two pool loads first:
7
+ //
8
+ // ldr r0, .L4 <- both addresses
9
+ // ldr r1, .L4+0x4
10
+ // ldrb r0, [r0] <- then both loads
11
+ // ldrb r1, [r1, #0x8]
12
+ //
13
+ // Spelling the derefs INLINE in the argument list (`f(*(u8 *)0x4000006, gEntityArray[8])`) makes
14
+ // agbcc finish argument 0 before starting argument 1 — `ldr; ldrb; ldr; ldrb` — which is the same
15
+ // four instructions in a different order, and a nonmatch. The source that produces the target's
16
+ // order names the bases first (`vu8 *p = &REG_VCOUNT_L; u8 *e = gEntityArray; f(*p, e[8])`), which
17
+ // is what a decomp author writes and what this pass reproduces.
18
+ //
19
+ // A LEVER, not a rewrite: it is emitted as an ADDITIONAL candidate (rank.ts `/argbase`) and the
20
+ // differ referees, so the inline spelling is always still there to win. That is what bounds the
21
+ // risk — a lever that replaced the primary could lose a match, this one cannot.
22
+ //
23
+ // SEMANTICS ARE PRESERVED BY CONSTRUCTION, which matters because on a NONMATCH row the
24
+ // best-scoring candidate is what the user is shown. Only a PURE leaf base is eligible — a global's
25
+ // address (`addr`), a numeric pointer (`const`), or the bare name of a declared global — so
26
+ // evaluating it earlier can be neither observable nor faulting. A local variable is excluded: it
27
+ // may be assigned between the hoist point and the call, which would change what is dereferenced.
28
+ //
29
+ // KNOWN LIMITATION: the hoisted local is a plain `T *` — `IrType` models no cv-qualifier at all,
30
+ // so naming a VOLATILE cell through it drops the qualifier that macros.ts goes out of its way to
31
+ // carry. Pre-existing and not introduced here (every pointer local in the tower has it), but the
32
+ // two features meet on exactly the MMIO shape this lever targets, so it is written down rather
33
+ // than left to be rediscovered.
34
+ //
35
+ // GATE: at least TWO arguments of the same call must qualify, with DISTINCT bases. The reordering
36
+ // this reproduces only exists when two addresses compete for registers during argument setup — with
37
+ // ONE base there is nothing to interleave, so the compiler emits the same sequence either way and
38
+ // naming it is pure churn. (Evidence: on kleod:UpdateFadeEffect, hoisting only the first base
39
+ // leaves the diff at 2; both together take it to 0.)
40
+ import { type IrType, T, scalarTypeForAccess } from '../ir/types';
41
+ import type { Expr, SFn, Stmt } from './ast';
42
+ import { mapExprChildren, stmtExprs } from './ast';
43
+ import { nameAllocator } from './hoist';
44
+
45
+ /** A base this pass may evaluate early: pure, and not something a store can change under us. */
46
+ function eligibleBase(base: Expr, globals: ReadonlySet<string>): boolean {
47
+ return base.k === 'addr' || base.k === 'const' || (base.k === 'var' && globals.has(base.name));
48
+ }
49
+
50
+ /** THE identity of an eligible base — what makes two accesses "the same address".
51
+ *
52
+ * A global reaches this pass under TWO spellings: `addr g` (its address) and, when the symbol map
53
+ * types it as an array of the access width, the bare `var g`. They denote the same cell, so a
54
+ * structural comparison would count them as two addresses and defeat the gate on
55
+ * `f(*(u8 *)&g, g[4])` — the exact churn the gate exists to reject, reached by a different route.
56
+ * Named bases therefore key on the NAME alone, whichever node kind carries it. */
57
+ function baseIdentity(base: Expr): string {
58
+ return base.k === 'const' ? `c:${base.value}` : `n:${(base as Extract<Expr, { k: 'var' | 'addr' }>).name}`;
59
+ }
60
+
61
+ /** The `index` nodes directly under a call's arguments whose base is eligible — the candidates for
62
+ * materialization. Only the argument's OWN top-level deref counts: a base buried inside arbitrary
63
+ * argument arithmetic is not what the compiler is loading up front. */
64
+ function argBases(call: Extract<Expr, { k: 'call' }>, globals: ReadonlySet<string>): Extract<Expr, { k: 'index' }>[] {
65
+ const out: Extract<Expr, { k: 'index' }>[] = [];
66
+ for (const a of call.args) {
67
+ if (a.k === 'index' && !a.lead?.length && eligibleBase(a.base, globals)) {
68
+ out.push(a);
69
+ }
70
+ }
71
+ return out;
72
+ }
73
+
74
+ /** Distinct bases, in first-appearance order — the compiler loads each ADDRESS once.
75
+ *
76
+ * Keyed on the base alone, deliberately, even though the naming below keys on width too: the gate
77
+ * counts how many addresses compete for registers during argument setup, and two accesses of the
78
+ * same address at different widths are still ONE address. Counting them separately would pass the
79
+ * gate on `callee(*(u8 *)&g, *(u16 *)&g)`, where nothing reorders and there is nothing to fix. */
80
+ function distinctBases(nodes: Extract<Expr, { k: 'index' }>[]): Extract<Expr, { k: 'index' }>[] {
81
+ const out: Extract<Expr, { k: 'index' }>[] = [];
82
+ for (const n of nodes) {
83
+ if (!out.some((o) => baseIdentity(o.base) === baseIdentity(n.base))) {
84
+ out.push(n);
85
+ }
86
+ }
87
+ return out;
88
+ }
89
+
90
+ /** The (base, width, signedness) key an `index` shares with every other access through the same
91
+ * base — so ALL of a base's uses in one call rewrite to the same local, not just the first. */
92
+ function baseKey(n: Extract<Expr, { k: 'index' }>): string {
93
+ return `${baseIdentity(n.base)} ${n.width} ${n.signed}`;
94
+ }
95
+
96
+ /**
97
+ * The `/argbase` re-spelling, or null when no statement qualifies (the caller then adds no
98
+ * candidate at all rather than a duplicate of the primary).
99
+ */
100
+ export function materializeArgBases(sfn: SFn): SFn | null {
101
+ const globals = new Set((sfn.globals ?? []).map((g) => g.name));
102
+ const fresh = nameAllocator(sfn);
103
+ const newLocals: { name: string; type: IrType }[] = [];
104
+ let fired = false;
105
+
106
+ // Rewrite ONE statement into the list that replaces it: the naming assignments, then the
107
+ // statement with its qualifying bases pointed at them. The naming goes immediately BEFORE the
108
+ // statement holding the call, not at the function top — the compiler loads these addresses where
109
+ // it needs them, and hoisting further extends live ranges the original never had (the
110
+ // register-pressure failure basecse.ts's loop gate exists for).
111
+ //
112
+ // NOTE the shape: recursion happens per FIELD, through an exhaustive switch, and the `pre`
113
+ // insertion happens INSIDE it. Rebuilding a statement from a flattened `stmtChildren` list cannot
114
+ // work — inserting statements shifts the boundary the rebuild would have to split at, and
115
+ // `stmtChildren('for')` is `[init, inc, ...body]`, which is not a body. Both mistakes produce
116
+ // COMPILING but wrong C (a call migrating across an if/else boundary; a `for` init duplicated
117
+ // into its body), which no boundary contract checks: they check resolution and spellability, not
118
+ // statement placement.
119
+ const rewrite = (s: Stmt): Stmt[] => {
120
+ const pre: Stmt[] = [];
121
+ const localFor = new Map<string, string>();
122
+ // Only the statement's OWN expressions can carry a call this pass names bases for; nested
123
+ // statement lists get their own `pre`, in their own scope, via the recursion below.
124
+ //
125
+ // A LOOP's own expression is its CONDITION, which runs every iteration — but `pre` lands
126
+ // BEFORE the loop, which would make this a loop-invariant hoist to a point the original never
127
+ // had. That is the register-pressure failure basecse.ts's `inLoop` gate exists to refuse, and
128
+ // it would contradict this pass's own placement rule two comments down. So a loop's condition
129
+ // is left alone; only its body (via the recursion) is eligible.
130
+ const ownExprs = s.k === 'while' || s.k === 'dowhile' || s.k === 'for' ? [] : stmtExprs(s);
131
+ for (const e of ownExprs) {
132
+ const scan = (x: Expr): void => {
133
+ if (x.k === 'call') {
134
+ const bases = distinctBases(argBases(x, globals));
135
+ if (bases.length >= 2) {
136
+ for (const b of bases) {
137
+ const key = baseKey(b);
138
+ if (localFor.has(key)) {
139
+ continue;
140
+ }
141
+ const ptrType = T.ptr(scalarTypeForAccess(b.width, b.signed));
142
+ const nm = fresh();
143
+ localFor.set(key, nm);
144
+ newLocals.push({ name: nm, type: ptrType });
145
+ pre.push({ k: 'assign', name: nm, value: { k: 'cast', to: ptrType, e: b.base } });
146
+ }
147
+ fired = true;
148
+ }
149
+ }
150
+ mapExprChildren(x, (c) => {
151
+ scan(c);
152
+ return c;
153
+ });
154
+ };
155
+ scan(e);
156
+ }
157
+ const point = (e: Expr): Expr => {
158
+ if (e.k === 'index' && !e.lead?.length && eligibleBase(e.base, globals)) {
159
+ const nm = localFor.get(baseKey(e));
160
+ if (nm) {
161
+ return { ...e, base: { k: 'var', name: nm }, idx: point(e.idx) };
162
+ }
163
+ }
164
+ return mapExprChildren(e, point);
165
+ };
166
+ const kids = (list: Stmt[]): Stmt[] => list.flatMap(rewrite);
167
+ let out: Stmt;
168
+ switch (s.k) {
169
+ case 'assign':
170
+ out = { ...s, value: point(s.value) };
171
+ break;
172
+ case 'store':
173
+ out = { ...s, lval: point(s.lval), value: point(s.value) };
174
+ break;
175
+ case 'exprstmt':
176
+ out = { ...s, value: point(s.value) };
177
+ break;
178
+ case 'return':
179
+ out = s.value === undefined ? s : { ...s, value: point(s.value) };
180
+ break;
181
+ case 'if':
182
+ out = { ...s, cond: point(s.cond), then: kids(s.then), else: kids(s.else) };
183
+ break;
184
+ case 'while':
185
+ case 'dowhile':
186
+ out = { ...s, cond: point(s.cond), body: kids(s.body) };
187
+ break;
188
+ case 'for': {
189
+ // `init`/`inc` are single statements. A `pre` produced inside either has nowhere legal to
190
+ // go (before the loop changes when it runs; inside the body repeats it), so this pass
191
+ // declines to fire there and leaves them alone.
192
+ out = { ...s, cond: point(s.cond), body: kids(s.body) };
193
+ break;
194
+ }
195
+ case 'switch':
196
+ out = {
197
+ ...s,
198
+ scrutinee: point(s.scrutinee),
199
+ cases: s.cases.map((c) => ({ ...c, body: kids(c.body) })),
200
+ ...(s.default ? { default: kids(s.default) } : {}),
201
+ };
202
+ break;
203
+ case 'break':
204
+ case 'continue':
205
+ out = s;
206
+ break;
207
+ }
208
+ if (pre.length === 0) {
209
+ return [out];
210
+ }
211
+ return [...pre, out];
212
+ };
213
+
214
+ const body = sfn.body.flatMap(rewrite);
215
+ return fired ? { ...sfn, body, locals: [...sfn.locals, ...newLocals] } : null;
216
+ }
package/src/l3/ast.ts CHANGED
@@ -38,7 +38,13 @@ export type Expr =
38
38
  // Variable-index `a[i]` is recovered at the IR level (`aload`/`astore` carry elemSize;
39
39
  // raise/arrays.ts) but still LOWERS to this one C-shaped `index` node, so it stays C-only
40
40
  // (a Pascal array-access spelling is future work). Treat `index` with idx ≠ 0 as C-shaped.
41
- | { k: 'index'; base: Expr; idx: Expr; width: number; signed: boolean }
41
+ // `lead` prefixes CONSTANT subscripts before `idx` `g[0][i]` rather than `g[i]`. It exists for
42
+ // exactly one inhabitant: the bare-name spelling of a MULTIDIMENSIONAL array global, where one
43
+ // subscript reaches a row and the element needs the leading dimensions pinned first. The node
44
+ // still denotes ONE `width`-byte element, so its type, its legalization and its stride contract
45
+ // are unchanged — this is a spelling of the same address, not a new kind of access. Absent for
46
+ // every rank-1 access, which is why it is optional rather than an empty array.
47
+ | { k: 'index'; base: Expr; idx: Expr; width: number; signed: boolean; lead?: number[] }
42
48
  // A named struct-field access `base->name` (raise/structs.ts recovered `base` as a struct
43
49
  // pointer, so the byte offset resolves to a named field instead of a scaled array index).
44
50
  // Unlike `index`, this carries the field NAME (which encodes the byte offset, `field_<off>`),
@@ -53,8 +59,45 @@ export type Expr =
53
59
  // default) never produces this node; it keeps the `"?"` sentinel → ContractError behavior.
54
60
  | { k: 'marker'; reason: string; args: Expr[] };
55
61
 
62
+ // `>>` is the ARITHMETIC right shift and `>>>` the LOGICAL one. C spells both `>>` and picks from
63
+ // the left operand's type, so the C backend synthesizes the cast that pins the choice — exactly as
64
+ // it already synthesizes scalar deref casts from an `index` node's width. A backend with no
65
+ // spelling for one of them (IDO Pascal) declines LOUDLY on the operation itself, rather than on
66
+ // whatever artifact another language's spelling happened to leave in the tree.
67
+ //
68
+ // WHY THIS ONE SPLIT AND NOT THE OTHERS. "The machine distinguishes them" is NOT the rule — the
69
+ // machine distinguishes `divu`/`div` and `sltu`/`slt` too, and ARITH_TO_BIN deliberately collapses
70
+ // `udiv`→`/`, `umod`→`%`, `icmp_u*`→`<` etc., noting that "unsignedness is in the operand types".
71
+ // Taking the machine as the rule would license four more splits with no inhabitant, which is what
72
+ // "earn the level" forbids. The rule is the repo's own: the shift split because a real,
73
+ // byte-load-bearing divergence HAD inhabitants (~20 rows, 5 projects, 4 compilers) and no other
74
+ // channel could carry it — the operand type could not, since a promoted narrow value is signed
75
+ // whatever it was loaded as.
76
+ //
77
+ // The collapsed operators lean on exactly that channel, so they carry the same latent hazard:
78
+ // `*(u16 *)p / 3` renders as a signed division of a promoted `int` where the asm did `divu`. It is
79
+ // tolerated because no row has produced such a divergence. When one does, the fix is this same
80
+ // split — not a per-site patch.
56
81
  export type BinOp =
57
- '+' | '-' | '*' | '/' | '%' | '<' | '<=' | '>' | '>=' | '==' | '!=' | '&' | '|' | '^' | '<<' | '>>' | '&&' | '||';
82
+ | '+'
83
+ | '-'
84
+ | '*'
85
+ | '/'
86
+ | '%'
87
+ | '<'
88
+ | '<='
89
+ | '>'
90
+ | '>='
91
+ | '=='
92
+ | '!='
93
+ | '&'
94
+ | '|'
95
+ | '^'
96
+ | '<<'
97
+ | '>>'
98
+ | '>>>'
99
+ | '&&'
100
+ | '||';
58
101
 
59
102
  export type Stmt =
60
103
  | { k: 'assign'; name: string; value: Expr }
@@ -189,11 +232,27 @@ export function exprEquals(a: Expr, b: Expr): boolean {
189
232
  }
190
233
  case 'index': {
191
234
  const bb = b as typeof a;
192
- return a.width === bb.width && a.signed === bb.signed && exprEquals(a.base, bb.base) && exprEquals(a.idx, bb.idx);
235
+ // `lead` is part of the ADDRESS (`g[0][i]` and `g[1][i]` are different elements), so it
236
+ // must be compared — an omission here would let CSE/dedup collapse two distinct accesses.
237
+ const lead = a.lead ?? [];
238
+ const bLead = bb.lead ?? [];
239
+ return (
240
+ a.width === bb.width &&
241
+ a.signed === bb.signed &&
242
+ lead.length === bLead.length &&
243
+ lead.every((v, i) => v === bLead[i]) &&
244
+ exprEquals(a.base, bb.base) &&
245
+ exprEquals(a.idx, bb.idx)
246
+ );
193
247
  }
194
248
  case 'field': {
195
249
  const bb = b as typeof a;
196
- return a.name === bb.name && exprEquals(a.base, bb.base);
250
+ // `dot` is part of the SPELLING, and for the same reason `lead` is compared above: a CSE or
251
+ // dedup that treats these as equal keeps one node and discards the other, silently respelling
252
+ // `p->field_4` as `p.field_4` (or the reverse). Both compile only for the base type each
253
+ // belongs to, so collapsing them is how a valid access becomes an invalid one — or worse, a
254
+ // valid one against a different object.
255
+ return a.name === bb.name && (a.dot ?? false) === (bb.dot ?? false) && exprEquals(a.base, bb.base);
197
256
  }
198
257
  case 'marker': {
199
258
  const bb = b as typeof a;
@@ -305,3 +364,58 @@ export function stmtChildren(s: Stmt): Stmt[] {
305
364
  return [...s.cases.flatMap((c) => c.body), ...(s.default ?? [])];
306
365
  }
307
366
  }
367
+
368
+ // THE negation of a CONDITION — the one implementation, shared by every L3 pass that flips one.
369
+ //
370
+ // There were two, and they drifted: structure.ts's empty-then peephole learned to distribute over
371
+ // the short-circuit connectives while l3/dce.ts's copy kept wrapping in `!`, and because
372
+ // `eliminateDeadStores` runs AFTER structuring it re-introduced the very spelling the other one had
373
+ // just removed. That is the l3/hoist.ts failure mode verbatim — a copied helper silently losing the
374
+ // newer rule — so this lives with the AST vocabulary and the passes call it.
375
+ //
376
+ // Three rules, in order:
377
+ // 1. a relational operator flips directly (`!=` → `==`, `<` → `>=`, …), exact over C's total
378
+ // integer order;
379
+ // 2. DE MORGAN — `!(a && b)` becomes `!a || !b`. Sound including EVALUATION ORDER: `a && b` runs
380
+ // `b` only when `a` holds, and `!a || !b` runs `!b` only when `!a` is false, i.e. when `a`
381
+ // holds. Same operands, same inputs — which is what makes it safe over a `b` that loads. It
382
+ // matters because a source `&&` and its dual `||` compile to the SAME branch graph, so the
383
+ // recognizers in raise/shortcircuit.ts can only pick whichever the asm's branch senses spell;
384
+ // distributing is what lets the `/flip-branch` candidate reach the other one;
385
+ // 3. `!!x` collapses to `x`, reachable only from a double flip that rule 2 now produces.
386
+ //
387
+ // CONTEXT REQUIREMENT, and it is the reason this is `negateCond` and not `negate`: rule 3 is valid
388
+ // only in a TRUTH-VALUE context, where `x` and `!!x` are interchangeable. `!!5` is 1 and `5` is 5,
389
+ // so this must never be used to negate a general integer expression — only an `if`/loop test or an
390
+ // operand of one of the connectives above.
391
+ //
392
+ // SCOPE of rule 2: it only gives the differ a second spelling where a candidate lever already flips
393
+ // the condition, and `preserveDivergentBranchSense` covers divergent `if`s ONLY. A connective that
394
+ // ended up as a LOOP test therefore has no dual candidate at all — the differ never sees the other
395
+ // form, so on such a row this rule changes how the code READS and nothing else. Widening the
396
+ // branch-sense lever to loop tests is what would make it a matching lever there, and that is a
397
+ // separate change.
398
+ const NEGATE_REL: Partial<Record<BinOp, BinOp>> = {
399
+ '<': '>=',
400
+ '>=': '<',
401
+ '>': '<=',
402
+ '<=': '>',
403
+ '==': '!=',
404
+ '!=': '==',
405
+ };
406
+
407
+ export function negateCond(e: Expr): Expr {
408
+ if (e.k === 'bin') {
409
+ const flipped = NEGATE_REL[e.op];
410
+ if (flipped) {
411
+ return { ...e, op: flipped };
412
+ }
413
+ if (e.op === '&&' || e.op === '||') {
414
+ return { ...e, op: e.op === '&&' ? '||' : '&&', l: negateCond(e.l), r: negateCond(e.r) };
415
+ }
416
+ }
417
+ if (e.k === 'un' && e.op === '!') {
418
+ return e.e;
419
+ }
420
+ return { k: 'un', op: '!', e };
421
+ }
package/src/l3/basecse.ts CHANGED
@@ -21,6 +21,7 @@
21
21
  import { type IrType, T, scalarTypeForAccess } from '../ir/types';
22
22
  import type { Expr, SFn, Stmt } from './ast';
23
23
  import { mapExprChildren, stmtChildren, stmtExprs } from './ast';
24
+ import { nameAllocator } from './hoist';
24
25
 
25
26
  // A HOISTABLE base is a bare `addr` (a global address) or a bare `const` (a numeric pointer
26
27
  // address). Both are relocation-invariant leaves whose value the compiler keeps in one register
@@ -135,17 +136,6 @@ function rewriteStmt(s: Stmt, localFor: Map<string, string>): Stmt {
135
136
  }
136
137
  }
137
138
 
138
- /** A name not already used by a param/local/global in `sfn`, of the form `p<n>`. */
139
- function freshName(taken: Set<string>): string {
140
- let n = 0;
141
- while (taken.has(`p${n}`)) {
142
- n++;
143
- }
144
- const nm = `p${n}`;
145
- taken.add(nm);
146
- return nm;
147
- }
148
-
149
139
  export function hoistReusedGlobalBases(sfn: SFn): SFn {
150
140
  const c: Collected = { count: new Map(), order: [], meta: new Map(), inLoop: new Set(), constOffCount: new Map() };
151
141
  collect(sfn.body, c, false);
@@ -171,9 +161,7 @@ export function hoistReusedGlobalBases(sfn: SFn): SFn {
171
161
  return sfn;
172
162
  }
173
163
 
174
- const taken = new Set<string>([...sfn.params.map((p) => p.name), ...sfn.locals.map((l) => l.name)]);
175
- // globals are referenced by name; a hoist name must not shadow one that appears in the body.
176
- collectNames(sfn.body, taken);
164
+ const fresh = nameAllocator(sfn);
177
165
 
178
166
  const localFor = new Map<string, string>();
179
167
  const newLocals: { name: string; type: IrType }[] = [];
@@ -181,7 +169,7 @@ export function hoistReusedGlobalBases(sfn: SFn): SFn {
181
169
  for (const k of hoisted) {
182
170
  const m = meta.get(k)!;
183
171
  const ptrType = T.ptr(scalarTypeForAccess(m.width, m.signed));
184
- const nm = freshName(taken);
172
+ const nm = fresh();
185
173
  localFor.set(k, nm);
186
174
  newLocals.push({ name: nm, type: ptrType });
187
175
  // `p = (T *)base` — the cast makes the local the access's pointer type so each `p[i]` strides it.
@@ -191,28 +179,3 @@ export function hoistReusedGlobalBases(sfn: SFn): SFn {
191
179
  const body = [...hoistStmts, ...sfn.body.map((s) => rewriteStmt(s, localFor))];
192
180
  return { ...sfn, body, locals: [...sfn.locals, ...newLocals] };
193
181
  }
194
-
195
- /** Every `var`/`addr`/called-function name mentioned anywhere in `stmts` (so a hoist name collides
196
- * with none — a global via `addr`, a local via `var`, OR a callee via `call.fn`). */
197
- function collectNames(stmts: Stmt[], out: Set<string>): void {
198
- const walk = (e: Expr): void => {
199
- if (e.k === 'var' || e.k === 'addr') {
200
- out.add(e.name);
201
- }
202
- if (e.k === 'call') {
203
- out.add(e.fn); // a hoist local must not shadow a called function symbol
204
- }
205
- for (const c of exprChildrenOf(e)) {
206
- walk(c);
207
- }
208
- };
209
- for (const s of stmts) {
210
- if (s.k === 'assign') {
211
- out.add(s.name);
212
- }
213
- for (const e of stmtExprs(s)) {
214
- walk(e);
215
- }
216
- collectNames(stmtChildren(s), out);
217
- }
218
- }
@@ -0,0 +1,146 @@
1
+ import { typeToString } from '../ir/types';
2
+ import type { Expr, SFn, Stmt } from './ast';
3
+ import { exprChildren, mapExprChildren, stmtChildren, stmtExprs } from './ast';
4
+
5
+ function namesIn(e: Expr, out: Set<string>): void {
6
+ // `addr` names a GLOBAL, never a local — collected anyway. A name reaching BOTH forms would
7
+ // otherwise get a span that ignores its `addr` mentions, and a SHORT span is a clobber while a
8
+ // long one is only a missed merge. `structure.ts` keeps locals to /^[vt]\d+$/ and excludes global
9
+ // names, so this cannot fire today; collecting is the direction that stays safe if that changes.
10
+ if (e.k === 'var' || e.k === 'addr') out.add(e.name);
11
+ for (const c of exprChildren(e)) namesIn(c, out);
12
+ }
13
+
14
+ /** Does `e` mention `n` anywhere? */
15
+ function mentions(e: Expr, n: string): boolean {
16
+ const seen = new Set<string>();
17
+ namesIn(e, seen);
18
+ return seen.has(n);
19
+ }
20
+ interface Span {
21
+ first: number;
22
+ last: number;
23
+ inLoop: boolean;
24
+ constFed: boolean;
25
+ /** the local's FIRST mention is a write, not a read */
26
+ firstIsWrite: boolean;
27
+ }
28
+ function spans(body: Stmt[]): Map<string, Span> {
29
+ const out = new Map<string, Span>();
30
+ let at = 0;
31
+ const walk = (list: Stmt[], inLoop: boolean): void => {
32
+ for (const s of list) {
33
+ at++;
34
+ const here = new Set<string>();
35
+ if (s.k === 'assign') here.add(s.name);
36
+ for (const e of stmtExprs(s)) namesIn(e, here);
37
+ for (const n of here) {
38
+ const sp = out.get(n) ?? {
39
+ first: at,
40
+ last: at,
41
+ inLoop,
42
+ constFed: true,
43
+ // an assign that ALSO READS the name (`b = g(b)`) is not a pure write; treating it as one
44
+ // let `g` receive the absorbed value
45
+ firstIsWrite: s.k === 'assign' && s.name === n && !stmtExprs(s).some((e) => mentions(e, n)),
46
+ };
47
+ sp.last = at;
48
+ sp.inLoop ||= inLoop;
49
+ if (s.k === 'assign' && s.name === n && s.value.k !== 'const') sp.constFed = false;
50
+ out.set(n, sp);
51
+ }
52
+ walk(stmtChildren(s), inLoop || s.k === 'while' || s.k === 'dowhile' || s.k === 'for');
53
+ }
54
+ };
55
+ walk(body, false);
56
+ return out;
57
+ }
58
+ function rename(body: Stmt[], from: string, to: string): Stmt[] {
59
+ const inExpr = (e: Expr): Expr =>
60
+ e.k === 'var' && e.name === from ? { ...e, name: to } : mapExprChildren(e, inExpr);
61
+ const inStmt = (s: Stmt): Stmt => {
62
+ const r = { ...s } as Record<string, unknown>;
63
+ if (s.k === 'assign' && s.name === from) r.name = to;
64
+ for (const key of ['value', 'lval', 'cond', 'scrutinee'] as const) {
65
+ const v = (s as Record<string, unknown>)[key];
66
+ if (v !== undefined) r[key] = inExpr(v as Expr);
67
+ }
68
+ for (const key of ['then', 'else', 'body', 'default'] as const) {
69
+ const v = (s as Record<string, unknown>)[key];
70
+ if (Array.isArray(v)) r[key] = (v as Stmt[]).map(inStmt);
71
+ }
72
+ if (s.k === 'for') {
73
+ r.init = inStmt(s.init);
74
+ r.inc = inStmt(s.inc);
75
+ }
76
+ if (s.k === 'switch') r.cases = s.cases.map((c) => ({ ...c, body: c.body.map(inStmt) }));
77
+ return r as Stmt;
78
+ };
79
+ return body.map(inStmt);
80
+ }
81
+ /** Every legal single merge, each as its own tree — NOT one committed choice.
82
+ *
83
+ * Which pair a register allocator coalesced is not derivable from the L3 tree, and first-fit gets
84
+ * it wrong: on kleod:UpdateHUDCounterDisplay the two legal merges score 18 and 40 against a
85
+ * no-merge baseline of 21, and declaration order picks the 40. `rank.ts` already has the idiom for
86
+ * exactly this — `/regcopy`'s "the tail choice is allocator-ambiguous, so both are ranked" — so
87
+ * every candidate is emitted and the differ referees.
88
+ *
89
+ * GATES:
90
+ * - a local mentioned inside a loop BODY is excluded. SOUND-critical: it is what makes preorder
91
+ * statement order a sufficient approximation of liveness. Preorder is a topological order of the
92
+ * CFG except where a later-indexed statement can run before an earlier one, and the positions
93
+ * that do that — a `for`'s `init`/`inc`, and everything in any loop body — are inside a loop, so
94
+ * the gate covers them. A loop's own CONDITION is NOT covered: it is visited at the loop
95
+ * statement's own index with the ENCLOSING loop flag. That is safe only because a condition
96
+ * cannot WRITE, so it can extend a read range but never reorder a definition — an earlier
97
+ * version of this comment claimed the gate covered conditions too, which it does not.
98
+ * Differential fuzzing supports this: removing the gate produces clobbers immediately, leaving
99
+ * it on produces none. No such harness is committed, so nothing here re-checks it.
100
+ * - both must be CONSTANT-fed. A codegen heuristic, not soundness — removing it stayed
101
+ * clobber-free under the same (uncommitted) fuzz and simply scored worse, because a load-fed
102
+ * local is one the compiler had a reason to keep where it was. It is also what currently BOUNDS
103
+ * candidate growth: merges are `L(L-1)/2` in the local count, each a distinct source and so a
104
+ * distinct compile, and nothing else caps that. Corpus-wide today: 2 rows, 13 kept sources.
105
+ * - the survivor's first mention must be an ASSIGN THAT DOES NOT ALSO READ IT. `b = g(b)` is a
106
+ * write and a read in one statement; counting it as a pure write let `g` receive the absorbed
107
+ * value. These two gates are NOT independent: `constFed` also rejects a self-reading assign
108
+ * (its value is not a literal), so it masks this one. No committed test isolates it — this is
109
+ * defence-in-depth for the day `constFed` is relaxed, which the note above makes plausible.
110
+ *
111
+ * ACCEPTED, NOT FIXED: a survivor assigned only on SOME paths still absorbs the other's value on
112
+ * the paths that skip it. The original read an uninitialized local there, so both spellings are
113
+ * ill-defined rather than one being wrong — but this is a real difference and the differ, not this
114
+ * gate, is what keeps it from faking a match. */
115
+ export function coalesceCandidates(sfn: SFn): { merged: string; sfn: SFn }[] {
116
+ if (sfn.locals.length < 2) {
117
+ return [];
118
+ }
119
+ const params = new Set(sfn.params.map((p) => p.name));
120
+ const typeOf = new Map(sfn.locals.map((l) => [l.name, typeToString(l.type)]));
121
+ const sp = spans(sfn.body);
122
+ const out: { merged: string; sfn: SFn }[] = [];
123
+ for (const a of sfn.locals.map((l) => l.name)) {
124
+ for (const b of sfn.locals.map((l) => l.name)) {
125
+ const x = sp.get(a);
126
+ const y = sp.get(b);
127
+ if (a === b || !x || !y || params.has(a) || params.has(b)) {
128
+ continue;
129
+ }
130
+ if (typeOf.get(a) !== typeOf.get(b) || x.inLoop || y.inLoop || !x.constFed || !y.constFed) {
131
+ continue;
132
+ }
133
+ if (x.last >= y.first || !y.firstIsWrite) {
134
+ continue;
135
+ }
136
+ // Labelled by the PAIR, not by an index into enumeration order: an index silently re-points
137
+ // at a different merge if `sfn.locals` ordering ever changes, leaving a recorded provenance
138
+ // that is wrong but plausible.
139
+ out.push({
140
+ merged: `${a}-${b}`,
141
+ sfn: { ...sfn, body: rename(sfn.body, a, b), locals: sfn.locals.filter((l) => l.name !== a) },
142
+ });
143
+ }
144
+ }
145
+ return out;
146
+ }