@asmlift/core 0.9.0 → 0.10.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.
Files changed (101) hide show
  1. package/README.md +25 -24
  2. package/package.json +1 -1
  3. package/src/aggregate.ts +149 -0
  4. package/src/backend/c.ts +1 -0
  5. package/src/backend/cfamily.ts +165 -50
  6. package/src/backend/cpp.ts +2 -1
  7. package/src/backend/pascal.ts +31 -12
  8. package/src/contracts.ts +173 -16
  9. package/src/declare.ts +69 -20
  10. package/src/frontend/asmdata.ts +7 -1
  11. package/src/frontend/call-plan.ts +964 -60
  12. package/src/frontend/dead-reads.ts +628 -0
  13. package/src/frontend/device-pins.ts +23 -4
  14. package/src/frontend/emit.ts +4 -3
  15. package/src/frontend/fpu.ts +224 -17
  16. package/src/frontend/frame-objects.ts +1232 -192
  17. package/src/frontend/frontend.ts +64 -3
  18. package/src/frontend/high-half.ts +53 -4
  19. package/src/frontend/jump-table.ts +530 -0
  20. package/src/frontend/local-object.ts +158 -32
  21. package/src/frontend/mips.ts +2076 -215
  22. package/src/frontend/own-struct-return.ts +500 -0
  23. package/src/frontend/ppc.ts +2711 -232
  24. package/src/frontend/register-struct-call.ts +484 -0
  25. package/src/frontend/reloc-symbol.ts +22 -7
  26. package/src/frontend/ssa.ts +1460 -44
  27. package/src/frontend/stackargs.ts +106 -57
  28. package/src/frontend/thumb.ts +2044 -318
  29. package/src/ir/alias.ts +42 -0
  30. package/src/ir/bits.ts +681 -1
  31. package/src/ir/core.ts +100 -5
  32. package/src/ir/discipline.ts +86 -21
  33. package/src/ir/entry.ts +159 -0
  34. package/src/{structure → ir}/loops.ts +6 -2
  35. package/src/ir/opcodes.ts +109 -9
  36. package/src/ir/types.ts +86 -2
  37. package/src/ir/verify.ts +80 -11
  38. package/src/l3/ast.ts +290 -18
  39. package/src/l3/basecse.ts +33 -5
  40. package/src/l3/constinit.ts +382 -0
  41. package/src/l3/dce.ts +2 -2
  42. package/src/l3/initfirst.ts +42 -11
  43. package/src/l3/intcell.ts +2 -1
  44. package/src/l3/narrowdecl.ts +77 -36
  45. package/src/l3/pollguard.ts +8 -2
  46. package/src/l3/ptrcell.ts +31 -34
  47. package/src/l3/ptrfield.ts +123 -8
  48. package/src/l3/regspell.ts +9 -4
  49. package/src/l3/reindex.ts +283 -96
  50. package/src/l3/storage.ts +98 -1
  51. package/src/l3/symbol-refs.ts +84 -6
  52. package/src/l3/tailmerge.ts +4 -2
  53. package/src/l3/typing.ts +108 -15
  54. package/src/l3/unreduce.ts +6 -5
  55. package/src/l3/volatileptr.ts +17 -54
  56. package/src/mangle.ts +30 -0
  57. package/src/pattern/engine.ts +361 -73
  58. package/src/pipeline.ts +541 -24
  59. package/src/proto-context.ts +905 -45
  60. package/src/proto.ts +555 -40
  61. package/src/raise/aggmembers.ts +290 -0
  62. package/src/raise/arrays.ts +5 -1
  63. package/src/raise/declared-volatile.ts +207 -31
  64. package/src/raise/floathelpers.ts +254 -33
  65. package/src/raise/gvn.ts +3 -3
  66. package/src/raise/latch.ts +162 -6
  67. package/src/raise/memberarrays.ts +10 -4
  68. package/src/raise/offsetnames.ts +10 -26
  69. package/src/raise/pairparams.ts +33 -0
  70. package/src/raise/paramwidth.ts +196 -6
  71. package/src/raise/pre-recovery.ts +102 -19
  72. package/src/raise/pre-structure.ts +54 -0
  73. package/src/raise/recover.ts +12 -5
  74. package/src/raise/reroll.ts +694 -0
  75. package/src/raise/shortcircuit.ts +26 -12
  76. package/src/raise/softdiv.ts +1 -1
  77. package/src/raise/storesign.ts +153 -0
  78. package/src/raise/struct-arrays.ts +30 -11
  79. package/src/raise/structs.ts +396 -33
  80. package/src/raise/truncload.ts +4 -3
  81. package/src/raise/unthread.ts +400 -0
  82. package/src/rank-declare.ts +118 -12
  83. package/src/rank-variations.ts +89 -31
  84. package/src/rank.ts +307 -54
  85. package/src/runtime-helpers.ts +140 -22
  86. package/src/structure/analysis.ts +165 -2
  87. package/src/structure/arith-ops.ts +23 -0
  88. package/src/structure/globalaccess.ts +111 -9
  89. package/src/structure/hazards.ts +16 -64
  90. package/src/structure/local-statics.ts +145 -9
  91. package/src/structure/namecoalesce.ts +1 -1
  92. package/src/structure/pointer-spelling.ts +1106 -0
  93. package/src/structure/redundant-test.ts +1 -1
  94. package/src/structure/structure.ts +2180 -1054
  95. package/src/structure/switch-recover.ts +244 -100
  96. package/src/symbols.ts +6 -0
  97. package/src/target.ts +765 -52
  98. package/src/trace.ts +26 -8
  99. package/src/variation-definitions.ts +126 -14
  100. package/src/variation-gates.ts +2 -0
  101. package/src/variation-tokens.ts +5 -1
package/src/pipeline.ts CHANGED
@@ -1,22 +1,30 @@
1
1
  // asmlift — the library entry point. `decompile(name, asm, target)` runs the raising tower and
2
2
  // returns structured results: the source, the per-level IR dumps, and diagnostics.
3
+ import { aggregateSize, ownArgumentWordsOn, returnedAggregate, returnsInMemory } from './aggregate';
3
4
  import { cBackend } from './backend/c';
4
5
  import {
5
6
  ContractError,
6
7
  assertDerefsTyped,
7
8
  assertEffectsPreserved,
9
+ assertFrameUnionsViewed,
8
10
  assertLocalsWritten,
9
11
  assertPostIncrUnshared,
10
12
  assertResolved,
11
13
  assertTypesRecovered,
12
14
  } from './contracts';
13
15
  import type { AsmData } from './frontend/asmdata';
16
+ import { returnedStruct, structLocalType } from './frontend/call-plan';
14
17
  import { FrontendUnsupportedError } from './frontend/errors';
18
+ import type { LiftOptions } from './frontend/frontend';
19
+ import { liftOwnRegisterStruct, liftOwnStructReturn } from './frontend/own-struct-return';
20
+ import { liftRegisterStructCalls } from './frontend/register-struct-call';
15
21
  import { frontendFor } from './frontend/registry';
22
+ import { ABSORBED_KEYS, HOMED_UNDEF } from './frontend/ssa';
16
23
  import { type Fn, reachableBlocks } from './ir/core';
24
+ import { deadOps } from './ir/discipline';
17
25
  import { print } from './ir/print';
18
- import { firstTrivialPhi } from './ir/simplify';
19
- import { T } from './ir/types';
26
+ import { firstTrivialPhi, pruneDeadParams } from './ir/simplify';
27
+ import { type IrType, T, intWidth } from './ir/types';
20
28
  import { VerifyError, verify } from './ir/verify';
21
29
  import { LanguageBackend, SFn, gapReasonFor, walkExprs } from './l3/ast';
22
30
  import { BASECSE_GATES, hoistBaseLocals } from './l3/basecse';
@@ -24,13 +32,21 @@ import { eliminateDeadStores } from './l3/dce';
24
32
  import { mergeCommonTails } from './l3/tailmerge';
25
33
  import { dropUnspelledReturns } from './l3/tailret';
26
34
  import { DEFAULT_IDIOM_PATTERNS, RewritePattern, applyPattern, dce, patternApplies } from './pattern/engine';
27
- import { type FnProto, type Prototypes, declaresVoidReturn, prototypesFromSymbols } from './proto';
28
- import { stampDeclaredVolatileUnder } from './raise/declared-volatile';
35
+ import {
36
+ type FnProto,
37
+ type Prototypes,
38
+ declaresAggregateReturn,
39
+ declaresVoidReturn,
40
+ ownReturnWidth,
41
+ prototypesFromSymbols,
42
+ } from './proto';
43
+ import { type ContextGlobals, splitOwnContext } from './proto-context';
44
+ import { type StampBehaviors, stampDeclaredVolatileUnder } from './raise/declared-volatile';
29
45
  import { RaiseUnsupportedError } from './raise/errors';
30
46
  import { assumedShapes, inferGlobalArrays, orderLicensedGlobals } from './raise/globalshape';
31
- import { foldEmptyLatches } from './raise/latch';
32
47
  import { OFFSET_NAME_PASS } from './raise/offsetnames';
33
48
  import { type PreRecoveryOptions, type PreRecoveryPass, runPreRecovery } from './raise/pre-recovery';
49
+ import { PRE_STRUCTURE_FOLDS, type PreStructureFold } from './raise/pre-structure';
34
50
  import { recoverTypes } from './raise/recover';
35
51
  import { sinkReturns } from './raise/retsink';
36
52
  import { StructureError, structure } from './structure/structure';
@@ -73,6 +89,18 @@ export interface DecompileOptions {
73
89
  * declaration shapes (DWARF types-sidecar). Drives the Thumb numeric-pool promotion and the
74
90
  * byte-sensitive global spellings. Absent ⇒ byte-identical to today. */
75
91
  symbols?: SymbolMap;
92
+ /** what the project's declaration context states about its globals (proto-context.ts
93
+ * `globalsFromContext`): no aggregate is spelled as a bare scalar name, and every access to a
94
+ * volatile object the map does not lay out, or through a pointer to one, is stamped
95
+ * (raise/declared-volatile.ts), so the cast it is read through carries the qualifier. */
96
+ contextGlobals?: ContextGlobals;
97
+ /** the declaration context's own entry for `name` (proto-context.ts `ownContextEntry`), which
98
+ * bounds the argument registers SOFTLY where `prototypes` states no entry for it, and whose
99
+ * struct return, where it declares one, is lifted where the machine proves it (`liftStamped`);
100
+ * absent ⇒ nothing bounds them softly. A `prototypes` entry for `name` marked
101
+ * `fromContext` is read as this, and taken out of the table (proto-context.ts
102
+ * `splitOwnContext`). */
103
+ ownContextProto?: FnProto;
76
104
  /** gap policy — see `OnGap`. Default "strict". */
77
105
  onGap?: OnGap;
78
106
  }
@@ -120,6 +148,13 @@ export interface DecompileResult {
120
148
  * `stage:offsetnames`), and it is also the only thing in a result that says a walk spelling
121
149
  * stopped being enumerable here. Empty on every run with no symbol map. */
122
150
  walkedNames: string[];
151
+ /** WHERE THE LIFT WENT PAST ITS CONTEXT DECLARATION (`liftStamped`'s `[own-arity]` notes): each
152
+ * names the declaration and either the argument registers the body reads past it, kept as
153
+ * parameters, or the refusal that made the lift drop the bound. A note is no gap, but the
154
+ * parameter list printed contradicts the header, which every compiler here rejects as
155
+ * conflicting types. Empty where no context entry was handed over, or where the lift stayed
156
+ * within it. */
157
+ ownArityNotes: string[];
123
158
  }
124
159
 
125
160
  export function decompile(
@@ -153,9 +188,22 @@ function runTower(
153
188
  const backend = opts.backend ?? cBackend;
154
189
  // The project's own DWARF signatures fill in what the caller did not state — in practice the
155
190
  // CALLEES (a function still in assembly has none), which is what makes this transferable.
156
- const prototypes = prototypesFromSymbols(opts.symbols, opts.prototypes ?? {});
191
+ const own = splitOwnContext(opts.prototypes, name, opts.ownContextProto);
192
+ const prototypes = prototypesFromSymbols(opts.symbols, own.prototypes ?? {});
157
193
  // (1) lift: ISA frontend (resolved by target) → L1 with block-argument SSA
158
- const fn = liftStamped(name, asm, target, prototypes, opts.asmData, opts.symbols);
194
+ const ownArityNotes: string[] = [];
195
+ const fn = liftStamped(
196
+ name,
197
+ asm,
198
+ target,
199
+ prototypes,
200
+ opts.asmData,
201
+ opts.symbols,
202
+ opts.contextGlobals,
203
+ undefined,
204
+ own.ownContextProto,
205
+ ownArityNotes,
206
+ );
159
207
  // Every dump carries the write-order record (ir/print.ts `PrintOptions`): it decides the
160
208
  // edge-copy order and the raising folds mutate it, so two dumps compare whole program states.
161
209
  const raw = print(fn, { writeOrder: true });
@@ -178,7 +226,9 @@ function runTower(
178
226
  // `inferredSymbols`, and the ORDER licence only enables a cast spelling that is byte-correct
179
227
  // under any declaration. Measured: with this call moved ABOVE both readings, the three corpus
180
228
  // rows the pass fires on keep their fan size, their winner and their score exactly.
181
- const walkedNames = opts.symbols ? OFFSET_NAME_PASS.run(fn, opts.symbols, target.compilerBehaviors) : [];
229
+ const walkedNames = opts.symbols
230
+ ? OFFSET_NAME_PASS.run(fn, opts.symbols, stampBehaviorsFor(target, opts.contextGlobals))
231
+ : [];
182
232
 
183
233
  // (2) idiom fold: apply serializable patterns on the IR (the AI-improvement surface),
184
234
  // gated generically by the Target's capabilities (not an `arch ==` branch).
@@ -199,8 +249,10 @@ function runTower(
199
249
  // shape are behaviourally identical and only one of them is printable (switch fall-through
200
250
  // vs plain if-nesting): recovery must not mint a tree this backend would refuse.
201
251
  spellSwitchFallthrough: backend.spellsSwitchFallthrough,
252
+ spellSwitchBreak: backend.spellsSwitchBreak,
202
253
  onGap,
203
254
  ...(mapSymbols ? { symbols: mapSymbols } : {}),
255
+ ...(opts.contextGlobals ? { contextGlobals: opts.contextGlobals } : {}),
204
256
  ...(inferredSymbols.size ? { inferredSymbols } : {}),
205
257
  ...(orderLicensed.size ? { orderLicensedGlobals: orderLicensed } : {}),
206
258
  });
@@ -219,13 +271,61 @@ function runTower(
219
271
  // does not have (raise/globalshape.ts `assumedShapes`).
220
272
  assumedSymbols: assumedShapes(inferredSymbols, sfn, mapSymbols),
221
273
  walkedNames,
274
+ ownArityNotes,
275
+ };
276
+ }
277
+
278
+ /** The `[own-arity]` notes (`DecompileResult.ownArityNotes`) of one lift under `opts`, for a caller
279
+ * whose result carries none — the ranked path, whose every candidate lifts as this one does. Empty
280
+ * where the lift fails: the run reports that failure itself. */
281
+ export function ownArityNotesOf(
282
+ name: string,
283
+ asm: string,
284
+ target: TargetDescription,
285
+ opts: Pick<DecompileOptions, 'prototypes' | 'asmData' | 'symbols' | 'contextGlobals' | 'ownContextProto'>,
286
+ ): string[] {
287
+ const own = splitOwnContext(opts.prototypes, name, opts.ownContextProto);
288
+ if (own.ownContextProto === undefined) {
289
+ return [];
290
+ }
291
+ const notes: string[] = [];
292
+ try {
293
+ liftStamped(
294
+ name,
295
+ asm,
296
+ target,
297
+ prototypesFromSymbols(opts.symbols, own.prototypes ?? {}),
298
+ opts.asmData,
299
+ opts.symbols,
300
+ opts.contextGlobals,
301
+ undefined,
302
+ own.ownContextProto,
303
+ notes,
304
+ );
305
+ } catch {
306
+ return [];
307
+ }
308
+ return notes;
309
+ }
310
+
311
+ /** What the declared-volatile stamp reads besides the IR and the map: the compiler's fact and the
312
+ * declaration context's globals. Every stamp a driver puts reads the same two. */
313
+ export function stampBehaviorsFor(
314
+ target: TargetDescription,
315
+ contextGlobals: ContextGlobals | undefined,
316
+ ): StampBehaviors {
317
+ return {
318
+ volatileReadsExtendInRegister: target.compilerBehaviors.volatileReadsExtendInRegister,
319
+ contextGlobals,
222
320
  };
223
321
  }
224
322
 
225
323
  /** Stage 1 — lift: the target's ISA frontend → L1, verified, with every read of an object
226
- * `symbols` declares volatile stamped `declared` (raise/declared-volatile.ts) before any pass asks
227
- * whether it may be deleted. Every entry path lifts through here, each lift under the map its
228
- * structuring will be handed. */
324
+ * `symbols` or `contextGlobals` declares volatile stamped `declared` (raise/declared-volatile.ts)
325
+ * before any pass asks whether it may be deleted. Every entry path lifts through here, each lift
326
+ * under the map its structuring will be handed, and each bounded by the context's own entry
327
+ * (`ownContext`) the same way (`liftWithinContextBound`); `notes` collects that bound's
328
+ * `[own-arity]` notes. */
229
329
  export function liftStamped(
230
330
  name: string,
231
331
  asm: string,
@@ -233,13 +333,396 @@ export function liftStamped(
233
333
  prototypes: Prototypes,
234
334
  asmData: AsmData | undefined,
235
335
  symbols: SymbolMap | undefined,
336
+ contextGlobals: ContextGlobals | undefined,
337
+ liftOptions?: LiftOptions,
338
+ ownContext?: FnProto,
339
+ notes?: string[],
236
340
  ): Fn {
237
- const fn = frontendFor(target).lift(name, asm, target, prototypes, asmData, symbols);
341
+ // Its callers split the table already; split again here, where every lift passes, so that no
342
+ // frontend is handed a marked context entry to read as a stated, hard bound.
343
+ const split = splitOwnContext(prototypes, name, ownContext);
344
+ const table = split.prototypes ?? {};
345
+ const own = Object.hasOwn(table, name) ? table[name] : undefined;
346
+ const ownReturn = ownStructReturnThroughMemory(name, target, own, split.ownContextProto, notes);
347
+ // a refusal about the return's width that the declared struct explains waits for the proof
348
+ let deferred: ((unproved: string) => never) | undefined;
349
+ const bytes = ownReturn?.register === true ? undefined : ownReturn?.type.size;
350
+ const fn = liftWithinContextBound(
351
+ name,
352
+ asm,
353
+ target,
354
+ table,
355
+ asmData,
356
+ symbols,
357
+ contextGlobals,
358
+ bytes === undefined
359
+ ? liftOptions
360
+ : { ...liftOptions, ownMemoryReturn: { bytes, defer: (refuse) => void (deferred ??= refuse) } },
361
+ split.ownContextProto,
362
+ notes,
363
+ );
364
+ // a struct a callee returns in a register is the local the call assigns, or the function's own
365
+ // return where every return hands that struct back whole
366
+ const calls = liftRegisterStructCalls(
367
+ fn,
368
+ table,
369
+ target,
370
+ own ?? split.ownContextProto,
371
+ ownReturn?.register === true ? ownReturn.type : undefined,
372
+ );
238
373
  verify(fn);
239
- stampDeclaredVolatileUnder(fn, symbols, target.compilerBehaviors);
374
+ if (ownReturn !== undefined && !calls.handedBack) {
375
+ returnOwnStruct(fn, name, target, ownReturn, notes, deferred, calls.readsCallStruct);
376
+ verify(fn);
377
+ }
378
+ refuseNarrowReturnsOfWideDeclaration(fn, name, own);
379
+ stampDeclaredVolatileUnder(fn, symbols, stampBehaviorsFor(target, contextGlobals));
380
+ return fn;
381
+ }
382
+
383
+ /** THE CONTEXT'S OWN ENTRY BOUNDS THE ARGUMENT REGISTERS, SOFTLY: an argument register past the
384
+ * words it fills (`ownArgumentWordsOn`, the layout a stated entry gets) is lifted as an uninitialised
385
+ * local's home, and where the finished lift still READS one, the function is lifted again with
386
+ * the bound raised past it, so that register stays the parameter a context-less lift makes it,
387
+ * with an `[own-arity]` note naming the declaration and the register.
388
+ *
389
+ * WHY SOFT, where a stated entry is hard. A header entry for a function still in assembly was
390
+ * written with no compiler checking it against a definition, and the two directions of its error
391
+ * cost differently. Too long, it only keeps a parameter. Too short, a hard bound turns a real
392
+ * argument into `uninit_<reg>`, which compiles silently: sbk2's task callbacks are declared
393
+ * `void f(void);` ahead of a definition that reads `a0`, 407 of 525 such headers were short when
394
+ * their functions were decompiled, and a hard bound lifted 16 of 33 sampled ones as `s32
395
+ * uninit_a0; … memcpy(uninit_a0 + 28, …)`. Kept as a parameter instead, the lift is the right
396
+ * function and contradicts the header, which every compiler here rejects as conflicting types —
397
+ * loud — and the note says which register. What the bound buys is the other direction: a
398
+ * register nothing in the lift reads is no parameter (sa3's `void Task_802F698(void)` lifted
399
+ * `(s32 a0, s32 a1, s32 a2, s32 a3)`).
400
+ *
401
+ * "READS" MEANS THE FINISHED LIFT, not the SSA builder. A register is read as the argument of a
402
+ * call whose arity was guessed, and the guess then trims it (`k4(a, b, 2, 0); zq(a); sl();` reads
403
+ * a2/a3 as `zq`'s and `sl`'s possible arguments); so the test is that an undefined value of a
404
+ * dropped register survives into the lift — an `undef` keyed by it, or by a join that took it,
405
+ * that `dce` would not delete, or one the machine homes to a slot nothing reads
406
+ * (`readDroppedArgRegs`). A read the raising tower would delete later still counts: that only
407
+ * keeps the parameter.
408
+ *
409
+ * A LIFT THAT REFUSES WITHIN THE BOUND IS LIFTED WITHOUT IT, with a note, so the bound turns no
410
+ * lift into a decline. The one refusal it adds is a stated entry's (frontend/ssa.ts
411
+ * `LiveInModel.ownArgWords`): an incoming stack argument past it, which no local can be, and which,
412
+ * named by position past the registers the bound dropped, would be one slot low. A stated entry for
413
+ * `name` — `prototypes`, which includes what the symbol map derives — wins, and the context's is
414
+ * not read. */
415
+ function liftWithinContextBound(
416
+ name: string,
417
+ asm: string,
418
+ target: TargetDescription,
419
+ prototypes: Prototypes,
420
+ asmData: AsmData | undefined,
421
+ symbols: SymbolMap | undefined,
422
+ contextGlobals: ContextGlobals | undefined,
423
+ liftOptions: LiftOptions | undefined,
424
+ ownContext: FnProto | undefined,
425
+ notes: string[] | undefined,
426
+ ): Fn {
427
+ const frontend = frontendFor(target);
428
+ // THE CONTEXT'S OWN ENTRY RIDES ON EVERY LIFT IT MAY BIND, the stated entry's absence permitting.
429
+ // A lift bounded by exactly its words, or one it fills every register of, may take it as proof
430
+ // (`LiftOptions.contextOwnProto.proves`). Any other lift may only refuse on it: the unbounded
431
+ // relift below, and a bound raised past a register the body reads. Otherwise a decided arity gap
432
+ // (frontend/call-plan.ts `decideGap`) that the entry refuses would come back from the relift as a
433
+ // lift without it. With `int pr(void)` in the context, `logv(s, 1)` would lift `logv(a0, 1)`, the
434
+ // same as with no declaration.
435
+ const context = (proves: boolean): LiftOptions =>
436
+ ownContext === undefined || Object.hasOwn(prototypes, name)
437
+ ? {}
438
+ : { contextOwnProto: { proto: ownContext, proves } };
439
+ const unbounded = () =>
440
+ forgetAbsorbedKeys(
441
+ frontend.lift(name, asm, target, prototypes, asmData, symbols, contextGlobals, {
442
+ ...liftOptions,
443
+ ...context(false),
444
+ }),
445
+ );
446
+ let words =
447
+ ownContext === undefined || Object.hasOwn(prototypes, name)
448
+ ? undefined
449
+ : ownArgumentWordsOn(name, ownContext, target);
450
+ if (words === undefined || words >= target.argRegs.length) {
451
+ return words === undefined
452
+ ? unbounded()
453
+ : forgetAbsorbedKeys(
454
+ frontend.lift(name, asm, target, prototypes, asmData, symbols, contextGlobals, {
455
+ ...liftOptions,
456
+ ...context(true),
457
+ }),
458
+ );
459
+ }
460
+ const declared = spelledDeclaration(name, ownContext!);
461
+ const declaredWords = words;
462
+ for (;;) {
463
+ let fn: Fn;
464
+ try {
465
+ fn = frontend.lift(name, asm, target, prototypes, asmData, symbols, contextGlobals, {
466
+ ...liftOptions,
467
+ contextOwnWords: words,
468
+ ...context(words === declaredWords),
469
+ });
470
+ } catch (e) {
471
+ if (!(e instanceof FrontendUnsupportedError)) {
472
+ throw e;
473
+ }
474
+ const lifted = unbounded();
475
+ notes?.push(
476
+ `${name}: lifted without its context declaration \`${declared}\`, which it could not be lifted within ` +
477
+ `(${e.message.split('\n')[0]})`,
478
+ );
479
+ return lifted;
480
+ }
481
+ const read = readDroppedArgRegs(fn, target.argRegs.slice(words));
482
+ if (read.length === 0) {
483
+ return forgetAbsorbedKeys(fn);
484
+ }
485
+ notes?.push(
486
+ `${name}: its context declaration \`${declared}\` fills ${words} argument word${words === 1 ? '' : 's'}, ` +
487
+ `and the body reads ${read.join(', ')} past ${words === 1 ? 'it' : 'them'} — kept as ` +
488
+ `${read.length === 1 ? 'a parameter' : 'parameters'}, which the header contradicts`,
489
+ );
490
+ words = Math.max(...read.map((r) => target.argRegs.indexOf(r))) + 1;
491
+ if (words >= target.argRegs.length) {
492
+ return unbounded();
493
+ }
494
+ }
495
+ }
496
+
497
+ /** The dropped argument registers whose undefined value the lift still reads: an `undef` keyed by
498
+ * one, or that absorbed one when its join collapsed (frontend/ssa.ts `ABSORBED_KEYS`), which is
499
+ * not dead (`deadOps`, the definition `dce` deletes by) or which the machine stored to a slot
500
+ * nothing reads back (`HOMED_UNDEF`). That store reaches no op, and it is still a read: IDO -g3 and
501
+ * -O0 home every argument register the definition declares, so a header naming fewer contradicts
502
+ * the asm, and the main lift's `(a0, a1)` is the one that recompiles to the same words. */
503
+ function readDroppedArgRegs(fn: Fn, dropped: readonly string[]): string[] {
504
+ const dead = deadOps(fn.blocks);
505
+ const read = new Set<string>();
506
+ for (const b of fn.blocks) {
507
+ for (const op of b.ops) {
508
+ if (op.opcode === 'undef' && (!dead.has(op) || op.attrs[HOMED_UNDEF] === true)) {
509
+ for (const k of [String(op.attrs.key), ...String(op.attrs[ABSORBED_KEYS] ?? '').split(',')]) {
510
+ read.add(k);
511
+ }
512
+ }
513
+ }
514
+ }
515
+ return dropped.filter((r) => read.has(r));
516
+ }
517
+
518
+ /** `fn` with no `undef` carrying `ABSORBED_KEYS` or `HOMED_UNDEF`, which nothing past the own-entry
519
+ * bound reads. */
520
+ function forgetAbsorbedKeys(fn: Fn): Fn {
521
+ for (const b of fn.blocks) {
522
+ for (const op of b.ops) {
523
+ if (op.opcode === 'undef') {
524
+ delete op.attrs[ABSORBED_KEYS];
525
+ delete op.attrs[HOMED_UNDEF];
526
+ }
527
+ }
528
+ }
240
529
  return fn;
241
530
  }
242
531
 
532
+ /** A declaration as a note spells it: `int f(short, int)`, `void f(void)`, `int f(int, ...)`, and
533
+ * `int f()` for one that states no parameters. */
534
+ function spelledDeclaration(name: string, p: FnProto): string {
535
+ const ret = p.returns ?? (p.returnsVoid === true ? 'void' : 'int');
536
+ const params =
537
+ p.variadicParams !== undefined
538
+ ? [...p.variadicParams, '...'].join(', ')
539
+ : Array.isArray(p.params)
540
+ ? p.params.length > 0
541
+ ? p.params.join(', ')
542
+ : 'void'
543
+ : '';
544
+ return `${ret} ${name}(${params})`;
545
+ }
546
+
547
+ /** A FUNCTION THAT ITSELF RETURNS A STRUCT BY VALUE, as its declaration says: a stated entry for
548
+ * `name` (`own`), or else the context's own entry (`ownContext`, proto-context.ts
549
+ * `ownContextEntry`). Where its target's rule (`returnsInMemory`) returns the struct through
550
+ * memory, its caller hands it the storage in the first argument register, so its declared
551
+ * parameters sit one register up, and the lift is the pointer function `s32 *f(s32 *a0, s32 a1)`
552
+ * until `returnOwnStruct` proves that pointer is the storage and lifts the struct return the
553
+ * declaration names. This answers what that proof is handed — the struct laid out, and which
554
+ * declaration said so — and refuses, before any frontend runs, where no lift can be the declared
555
+ * function:
556
+ * - a struct the rule hands back in two registers (mwcc's svr4 eight-byte struct in r3:r4): the
557
+ * lift returns one word, and would drop the second register — on both declarations, since a
558
+ * context's header that says so is the only thing that says the second register is live;
559
+ * - a stated declaration whose struct the rule cannot place, because the target states no rule
560
+ * or nothing lays the struct out, or that names no type a local can be declared with. A context
561
+ * entry that cannot be placed keeps the pointer lift, with a note in `notes`: what it costs is
562
+ * a signature the header contradicts, which the compiler rejects.
563
+ * A struct the rule hands back in one register (agbcc's and mwcc's one-word struct in r0/r3) is
564
+ * that register's word: the plan says so (`register`), and a struct that is all one word-wide
565
+ * integer member is spelled as that member (frontend/own-struct-return.ts
566
+ * `liftOwnRegisterStruct`), where any other one stays the word the lift returns, with a note in
567
+ * `notes` saying why (`returnOwnStruct`). */
568
+ function ownStructReturnThroughMemory(
569
+ name: string,
570
+ target: TargetDescription,
571
+ own: FnProto | undefined,
572
+ ownContext: FnProto | undefined,
573
+ notes: string[] | undefined,
574
+ ): OwnStructReturn | undefined {
575
+ const stated = own !== undefined;
576
+ const declared = stated ? own : ownContext;
577
+ if (!declaresAggregateReturn(declared)) {
578
+ return undefined;
579
+ }
580
+ const spelled = declared!.returns ?? 'a struct or union';
581
+ const read = returnedStruct(declared!, target);
582
+ if (read === 'register') {
583
+ // A struct wider than a word that comes back in registers is a pair the lift returns one word
584
+ // of, dropping the second register; one of a word or less is that word
585
+ const size = aggregateSize(returnedAggregate(declared!), target)?.size;
586
+ if (size !== undefined && size <= 4) {
587
+ const type = structLocalType(declared!, target);
588
+ return 'refused' in type
589
+ ? undefined
590
+ : { type, stated, declaration: spelledDeclaration(name, declared!), register: true };
591
+ }
592
+ if (size !== undefined) {
593
+ const pair =
594
+ `this target hands its ${size} bytes back in two registers — a function that itself returns a struct ` +
595
+ 'in a register pair is not modelled';
596
+ if (stated) {
597
+ throw new FrontendUnsupportedError(
598
+ `cannot lift '${name}': it is declared to return ${spelled} by value, and ${pair}`,
599
+ );
600
+ }
601
+ throw new FrontendUnsupportedError(
602
+ `cannot lift '${name}': its context declaration says it returns ${spelled} by value, and ${pair} ` +
603
+ `(\`${spelledDeclaration(name, declared!)}\`)`,
604
+ );
605
+ }
606
+ return undefined;
607
+ }
608
+ if ('refused' in read) {
609
+ const how =
610
+ returnsInMemory(returnedAggregate(declared!), target) === true
611
+ ? `this target returns that through a hidden pointer in ${target.argRegs[0]}, and ${read.refused}`
612
+ : target.compilerBehaviors.aggregateReturn === undefined
613
+ ? `nothing here says whether this target returns it through a hidden pointer in ${target.argRegs[0]}`
614
+ : `nothing here lays it out, and this target's rule needs its layout to say whether it comes back through a ` +
615
+ `hidden pointer in ${target.argRegs[0]}`;
616
+ if (stated) {
617
+ throw new FrontendUnsupportedError(
618
+ `cannot lift '${name}': it is declared to return ${spelled} by value, and ${how}`,
619
+ );
620
+ }
621
+ notes?.push(
622
+ `[own-return] ${name}: its context declaration \`${spelledDeclaration(name, declared!)}\` returns a struct by value, ` +
623
+ `and ${how} — lifted as the machine code's function, which the header contradicts`,
624
+ );
625
+ return undefined;
626
+ }
627
+ return { type: read.type, stated, declaration: spelledDeclaration(name, declared!) };
628
+ }
629
+
630
+ /** The struct a function's own declaration returns by value, and which declaration said so. */
631
+ interface OwnStructReturn {
632
+ type: Extract<IrType, { kind: 'struct' }>;
633
+ stated: boolean;
634
+ declaration: string;
635
+ /** the target hands it back in the return register, not through a hidden pointer */
636
+ register?: true;
637
+ }
638
+
639
+ /** Lifts `fn`'s struct return (frontend/own-struct-return.ts `liftOwnStructReturn`), where the
640
+ * lift proves the hidden pointer is the struct's storage. Where it does not, a STATED declaration
641
+ * refuses — the pointer function contradicts it — and a context's keeps the pointer lift, as the
642
+ * machine code's function, with a note in `notes`: the context's own entry is a header nothing
643
+ * checked against a definition (`liftWithinContextBound` says why that makes it soft). Either way, a
644
+ * refusal the frontend `deferred` because this struct would explain it (agbcc's 8-byte epilogue,
645
+ * `LiftOptions.ownMemoryReturn`) is thrown where the proof fails, since nothing else explains it, and
646
+ * says why the proof failed. */
647
+ function returnOwnStruct(
648
+ fn: Fn,
649
+ name: string,
650
+ target: TargetDescription,
651
+ own: OwnStructReturn,
652
+ notes: string[] | undefined,
653
+ deferred: ((unproved: string) => never) | undefined,
654
+ readsCallStruct: boolean,
655
+ ): void {
656
+ if (own.register) {
657
+ // a word the struct may not be all of stays the lift's own return, as the machine has it, and
658
+ // the note says why the declared struct is not what it returns
659
+ const kept = liftOwnRegisterStruct(fn, own.type);
660
+ // … save in a function that reads a struct a call returned (frontend/register-struct-call.ts):
661
+ // the word it returns may be computed from that struct — through a frame slot as well as an
662
+ // op — and the word return would print it as the scalar the declaration contradicts
663
+ if (kept !== undefined && readsCallStruct) {
664
+ throw new FrontendUnsupportedError(
665
+ `cannot lift '${name}': it is declared to return ${own.type.declared ?? 'a struct'} by value, which comes back in ` +
666
+ `${target.returnReg} — ${kept}, and it reads a struct a call returned there, which what it returns may be computed from`,
667
+ );
668
+ }
669
+ if (kept !== undefined) {
670
+ notes?.push(
671
+ `[own-return] ${name}: its ${own.stated ? '' : 'context '}declaration \`${own.declaration}\` returns ` +
672
+ `${own.type.declared ?? 'a struct'} by value in ${target.returnReg}, and ${kept} — lifted as the word ` +
673
+ 'the machine code returns, which the header contradicts',
674
+ );
675
+ }
676
+ return;
677
+ }
678
+ const why = liftOwnStructReturn(fn, own.type, target);
679
+ if (why === undefined) {
680
+ return;
681
+ }
682
+ // a refusal the frontend deferred because this struct would explain it stands where it does not
683
+ deferred?.(why);
684
+ const declared = `${own.type.declared ?? 'a struct'} by value, which this target returns through a hidden pointer in ${target.argRegs[0]}`;
685
+ if (own.stated) {
686
+ throw new FrontendUnsupportedError(
687
+ `cannot lift '${name}': it is declared to return ${own.type.declared ?? 'a struct'} by value, which this target returns ` +
688
+ `through a hidden pointer in ${target.argRegs[0]}, and the lift cannot show that pointer is only its storage: ${why}`,
689
+ );
690
+ }
691
+ notes?.push(
692
+ `[own-return] ${name}: its context declaration \`${own.declaration}\` returns ${declared}, and the lift cannot show ` +
693
+ `that pointer is only its storage (${why}) — lifted as the machine code's pointer function, which the header contradicts`,
694
+ );
695
+ }
696
+
697
+ /** A FUNCTION DECLARED TO RETURN AN INTEGER WIDER THAN A REGISTER, whose lift returns one word or
698
+ * none, refuses — on every frontend, since this reads only the lifted `ret`s.
699
+ *
700
+ * Only the declaration can decide this. A `long long` comes back in a register pair (Thumb r0:r1,
701
+ * O32 v0:v1, PowerPC r3:r4), and where the body never reads the pair's second register back, the
702
+ * asm of `long long f(long long *p, long long *q) { long long x = *p; *q = x; return x; }` is
703
+ * byte-identical to that of the `s32` function storing the same two words (kmc-gcc and gcc 2.7.2
704
+ * compile both to one object). A frontend that builds the pair returns one 64-bit value and
705
+ * passes; one that lowered a word would print `s32 f(...)` against the project's own `long long
706
+ * f(...)` — a candidate the compiler rejects as conflicting types. The frontends' asm rules
707
+ * (Thumb's `refuseWordReturns`, O32's `v1MayHoldLowWord`) stay as the backstop for a function
708
+ * nothing declares, and that byte-identical shape is their blind spot. */
709
+ function refuseNarrowReturnsOfWideDeclaration(fn: Fn, name: string, own: FnProto | undefined): void {
710
+ const width = ownReturnWidth(own);
711
+ if (width === undefined || width <= 32) {
712
+ return;
713
+ }
714
+ for (const blk of fn.blocks) {
715
+ for (const op of blk.ops) {
716
+ if (op.opcode === 'ret' && !op.operands.some((v) => (intWidth(v.type) ?? 0) > 32)) {
717
+ throw new FrontendUnsupportedError(
718
+ `cannot lift '${name}': its declaration returns a ${width}-bit integer, which comes back as the halves of a ` +
719
+ '64-bit pair, and this lowering returns one word',
720
+ );
721
+ }
722
+ }
723
+ }
724
+ }
725
+
243
726
  // ── the shared raising tower ────────────────────────────────────────────────────────────────
244
727
  // decompile(), decompileTraced (trace.ts), rank.ts's decompileRanked and the cli's
245
728
  // decompileWithReport + its score probe (report.ts) all raise a lifted fn through the SAME stage
@@ -272,19 +755,20 @@ export interface RaiseHooks {
272
755
  afterRecover?: () => void;
273
756
  /** after return-sinking, only when it changed the fn (fires after its verify) */
274
757
  afterRetsink?: () => void;
275
- /** after empty-latch folding, only when it removed a block (fires after its verify) */
276
- afterLatchFold?: () => void;
758
+ /** after each pre-structure fold (raise/pre-structure.ts) that changed the fn (fires after its verify) */
759
+ afterFold?: (fold: PreStructureFold) => void;
277
760
  }
278
761
 
279
762
  /** Stages 2.35–3.5 — pre-recovery recognizers (the shared ordered list in raise/pre-recovery.ts)
280
763
  * → type recovery (boundary contract: no `unknown` survives) → return-sinking (tail-duplicate a
281
- * return-only merge so short-circuits emit early returns) → empty-latch folding (splice out a
282
- * back-edge block SSA construction emptied). `verify` after every pass that changed the IR.
764
+ * return-only merge so short-circuits emit early returns) → the pre-structure folds
765
+ * (raise/pre-structure.ts: empty latches, exit trampolines, peeled loop tests). `verify` after
766
+ * every pass that changed the IR.
283
767
  *
284
- * The two CFG passes look ordered and are not: running latch folding FIRST instead changes
285
- * nothing across a 3337-function agbcc corpus. Worth saying, because folding an empty block ahead
768
+ * Return-sinking and latch folding look ordered and are not: running latch folding FIRST instead
769
+ * changes nothing across a 3337-function agbcc corpus. Worth saying, because folding an empty block ahead
286
770
  * of return-sinking does take away `br` predecessors it needs — the dominance gate is what makes
287
- * that unreachable, since the blocks retsink wants are never back-edge sources. It goes last
771
+ * that unreachable, since the blocks retsink wants are never back-edge sources. The folds go last
288
772
  * because that is where the CFG stops moving.
289
773
  *
290
774
  * The tail is three separate parameters rather than an options bag on purpose: this is the
@@ -314,6 +798,9 @@ export function raiseRecovered(
314
798
  pre,
315
799
  );
316
800
  hooks.beforeRecover?.();
801
+ if (declaresVoidReturn(self)) {
802
+ dropVoidReturnOperands(fn);
803
+ }
317
804
  recoverTypes(fn);
318
805
  verify(fn);
319
806
  assertTypesRecovered(fn);
@@ -330,9 +817,11 @@ export function raiseRecovered(
330
817
  verify(fn);
331
818
  hooks.afterRetsink?.();
332
819
  }
333
- if (foldEmptyLatches(fn)) {
334
- verify(fn);
335
- hooks.afterLatchFold?.();
820
+ for (const fold of PRE_STRUCTURE_FOLDS) {
821
+ if (fold.run(fn) > 0) {
822
+ verify(fn);
823
+ hooks.afterFold?.(fold);
824
+ }
336
825
  }
337
826
  // THE BOUNDARY POSTCONDITION. Above this line passes move the CFG; below it nothing does, and the
338
827
  // structurer reads a block parameter as a JOIN — a name it must give a local of its own. A param
@@ -352,6 +841,31 @@ export function raiseRecovered(
352
841
  }
353
842
  }
354
843
 
844
+ /** A FUNCTION ITS DECLARATION SAYS RETURNS NOTHING RETURNS NOTHING: each `ret` drops the return
845
+ * register the frontend hands it (frontend/ssa.ts returns that register at every `ret`, whatever
846
+ * the declared type), and the joins only that operand read go with it. Before type recovery, since
847
+ * a join read by nothing else is no value of the program, and recovery would otherwise unify what
848
+ * two paths leave in that register: mwcc's `if (*p == 7) { gHit += 1; break; }` leaves r3 holding
849
+ * gHit's value on one exit and the walked pointer on the other, and the one join typed both as
850
+ * `u8 *` (`gHit = (void *)(v0 + 1)`, which mwcc rejects). The structurer already reads no `ret`
851
+ * operand under `returnsVoid`, so this changes no `return` it writes. */
852
+ function dropVoidReturnOperands(fn: Fn): void {
853
+ let dropped = false;
854
+ for (const blk of fn.blocks) {
855
+ for (const op of blk.ops) {
856
+ if (op.opcode === 'ret' && op.operands.length > 0) {
857
+ op.operands = [];
858
+ dropped = true;
859
+ }
860
+ }
861
+ }
862
+ if (dropped) {
863
+ pruneDeadParams(fn);
864
+ dce(fn);
865
+ verify(fn);
866
+ }
867
+ }
868
+
355
869
  /** Run `body`; if it declines, name the unmodelled instructions the function carries.
356
870
  *
357
871
  * An `opaque` degrades its own value AND makes its block impure, so a shape recognizer refuses:
@@ -413,8 +927,9 @@ export function structureChecked(
413
927
  fn: Fn,
414
928
  opts: Parameters<typeof structure>[1],
415
929
  hooks?: Parameters<typeof structure>[2],
930
+ memo?: Parameters<typeof structure>[3],
416
931
  ): SFn {
417
- const raw = attributeOpaques(fn, () => structure(fn, opts, hooks));
932
+ const raw = attributeOpaques(fn, () => structure(fn, opts, hooks, memo));
418
933
  // The boundary contracts run on the pre-DCE tree: the readability pass must never be able to
419
934
  // hide a structuring defect by dropping the dead statement that carries it. assertResolved
420
935
  // catches an unresolved `?` value; assertDerefsTyped catches an ill-typed deref (e.g. a pointer
@@ -430,6 +945,7 @@ export function structureChecked(
430
945
  // re-validate deref typing on the rewritten tree.
431
946
  const sfn = readabilityRewrites(raw);
432
947
  assertDerefsTyped(sfn);
948
+ assertFrameUnionsViewed(sfn);
433
949
  assertPostIncrUnshared(sfn);
434
950
  // Re-checked after the readability rewrites for the same reason deref typing is: a pass that
435
951
  // merges arms or drops statements must not be able to lose or duplicate a call.
@@ -492,6 +1008,7 @@ export function stubResult(name: string, asm: string, backend: LanguageBackend,
492
1008
  // A stub spells no global, so it assumes nothing about one and walked to none.
493
1009
  assumedSymbols: [],
494
1010
  walkedNames: [],
1011
+ ownArityNotes: [],
495
1012
  };
496
1013
  }
497
1014