@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
@@ -12,26 +12,47 @@
12
12
  // lowers to is built here too (`CallDeclarations.lower`), and asks the frontend (`CallSite`) only
13
13
  // what its ISA alone knows: how it reads and writes a register, where an argument word past the
14
14
  // registers lives, and how it builds a pair.
15
- import { aggregateType, returnedAggregate, returnsInMemory, returnsWithoutHiddenPointer } from '../aggregate';
16
- import { type Value, mkOp, mkValue } from '../ir/core';
17
- import { type IrType, T, typeToString } from '../ir/types';
15
+ import {
16
+ type ByValueArg,
17
+ aggregateType,
18
+ byValueCallArgs,
19
+ byValueRefusal,
20
+ ownArgumentWordsOn,
21
+ ownLeadingWordParamsOn,
22
+ returnedAggregate,
23
+ returnsInMemory,
24
+ returnsWithoutHiddenPointer,
25
+ } from '../aggregate';
26
+ import { type Block, type Op, REGISTER_STRUCT, type Value, calleeName, mkOp, mkValue } from '../ir/core';
27
+ import { qualified } from '../ir/discipline';
28
+ import { OPCODES, type OpSig } from '../ir/opcodes';
29
+ import { type IrType, T, structSpelling, typeToString } from '../ir/types';
18
30
  import {
19
31
  type FnProto,
32
+ type ParamClass,
20
33
  type Prototypes,
21
- STANDARD_SIGNATURES,
34
+ calleeDeclaration,
22
35
  declaredCallArgs,
23
36
  declaredReturnWidth,
24
37
  declaresAggregateReturn,
25
38
  declaresParams,
26
39
  declaresVoidReturn,
40
+ floatingSpelling,
27
41
  isFloatingSpelling,
42
+ ownDeclaresFloatingParam,
43
+ paramClassOf,
28
44
  spellableProto,
45
+ variadicRefusal,
29
46
  wordsOf,
30
47
  } from '../proto';
31
- import { helperPrototypes, isFloatHelper, isWideHelper, lookupHelper } from '../runtime-helpers';
32
- import type { TargetDescription } from '../target';
33
- import type { FrontendRefusal } from './errors';
48
+ import { type ContextGlobals, structNamed } from '../proto-context';
49
+ import { floatSidesOf, helperPrototypes, isFloatHelper, isWideHelper, lookupHelper } from '../runtime-helpers';
50
+ import { type SymbolMap, symbolsByName } from '../symbols';
51
+ import { type TargetDescription, argRegisterHoles } from '../target';
52
+ import { type FrontendRefusal, FrontendUnsupportedError } from './errors';
53
+ import { type OwnDeclaration, ownDeclarationOf } from './frontend';
34
54
  import type { HighHalves } from './high-half';
55
+ import { unspellableReason } from './reloc-symbol';
35
56
  import { type SsaBuilder, clobberedByCall, fallbackArgc } from './ssa';
36
57
 
37
58
  /** What the frontend's own call lowering can carry out. */
@@ -68,6 +89,18 @@ export interface DeclaredCall {
68
89
  /** the declared parameter count, a hidden pointer aside */
69
90
  readonly params: number;
70
91
  readonly returned?: StructReturn | 'register';
92
+ /** each struct passed by value, by its first word in `widths` (a hidden pointer counted) */
93
+ readonly byValue?: readonly ByValueArg[];
94
+ }
95
+
96
+ /** WHAT A DECLARED PARAMETER IS, as far as where the caller places it goes (`paramClassOf`,
97
+ * proto.ts, which `declaredCall`'s widths read too). */
98
+ export type { ParamClass };
99
+
100
+ /** One declared parameter: its class, and the spelling it was read from (empty for a count). */
101
+ export interface DeclaredParam {
102
+ readonly kind: ParamClass;
103
+ readonly spelling: string;
71
104
  }
72
105
 
73
106
  /** What one call is, decided from its callee's declaration before any instruction is read. */
@@ -77,17 +110,27 @@ export interface CallPlan {
77
110
  readonly widths: readonly number[] | null;
78
111
  /** the indices into `widths` its declaration types `double` */
79
112
  readonly doubles: ReadonlySet<number>;
113
+ /** the structs its declaration passes by value, by their words in `widths` */
114
+ readonly byValue?: readonly ByValueArg[];
80
115
  readonly returns:
81
116
  | { readonly kind: 'word' }
82
117
  | { readonly kind: 'void' }
83
118
  | { readonly kind: 'pair' }
84
- | { readonly kind: 'register-struct' }
119
+ | {
120
+ readonly kind: 'register-struct';
121
+ /** the return register holds the struct's image, which a local of it is read off
122
+ * (`registerStructLocal`); false, it holds nothing the caller may read */
123
+ readonly image: boolean;
124
+ }
85
125
  | { readonly kind: 'memory-struct'; readonly type: IrType };
86
126
  /** what the call leaves holding nothing this function can name, for `SsaBuilder.noteCall` */
87
127
  readonly clobbers: readonly string[];
88
128
  /** the callee is declared void, whether or not this lowering writes no value for it (`returns`
89
129
  * is `void` only where `CallLowering.voidReturn` says so) */
90
130
  readonly declaredVoid: boolean;
131
+ /** the floating type an ordinary callee is declared to return where the target has no FPU, so
132
+ * the return register holds its bits (`raise/floathelpers.ts` holds every read of them) */
133
+ readonly floatReturn?: 'float' | 'double' | 'long double';
91
134
  }
92
135
 
93
136
  /** WHAT EACH CALLEE'S DECLARATION SAYS, and what a call leaves holding nothing this function can
@@ -103,11 +146,37 @@ export interface CallDeclarations {
103
146
  /** the callee's declared argument widths, for a frontend that places them before it lowers the
104
147
  * call; null where no declaration sizes them */
105
148
  declaredCall(callee: string): DeclaredCall | null;
149
+ /** what each parameter the callee's declaration spells is (`ParamClass`), read through the tiers
150
+ * `declaredCall` reads; null where no declaration states the parameters */
151
+ declaredParams(callee: string): readonly DeclaredParam[] | null;
106
152
  /** `lower`'s first half, the call decided from the declaration alone: a value its tests compare
107
153
  * whole, where `lower` leaves IR */
108
154
  plan(callee: string): CallPlan;
109
155
  /** the call `site` makes, planned and lowered: its `call` op, its result and its clobbers */
110
156
  lower(site: CallSite): void;
157
+ /** every value an argument register may hold at a call `lower` lowered at an UNDECLARED arity — a
158
+ * guess, or the bound a call through an argument register sets — gathered as each such call is
159
+ * lowered. One the call does not pass is unused by the LIFT, not proved unused by the source: the
160
+ * dead-read pin refuses it (frontend/dead-reads.ts `held-at-undeclared-call`). */
161
+ readonly heldAtUndeclaredCall: ReadonlySet<Value>;
162
+ /** THE #351 GAPLESS GUESS, NOT LET IN THROUGH A JUMP TABLE'S CASES. Once every call is lowered:
163
+ * where the function lifts only because of what an elided or kept dispatch left in its registers
164
+ * — a dispatch's marker or scaled index reaching some guessed call's argument registers
165
+ * (`CallSite.parked`, `CallSite.scaledIndex`; a PowerPC pair's r4 too), which stands where the
166
+ * pre-dispatch value had made a gap refuse, or `caseStateRead`, a read of the scaled index a case
167
+ * target is entered with — refuse the first guessed arity the bytes cannot tell from another:
168
+ * - its last counted register holds a value some path from the dispatch's block to the call
169
+ * did not write (`SsaBuilder.writtenOnEveryPath`; a cross-jumped call's block writes nothing,
170
+ * its cases do), a value passed on unchanged (`g0(a, 9)` lifted `g0(a0, 9, a0)`, agbcc's pre-dispatch copy of
171
+ * a0 still in r2; mwcc `return g()` lifted `return g(a0)`);
172
+ * - a counted register holds a value an effect of its block uses, a store's address or value
173
+ * left in the register (`gc = ga; g()` lifted `g(&gc, v0)`);
174
+ * - a register it leaves out below that marker still holds its incoming value, an argument
175
+ * passed on or not (mwcc `g(a)` with the table base in r4 lifted `g()`).
176
+ * Elsewhere the guess takes these, an open gap (`g()` and `g(a0)` are the same bytes); there the
177
+ * function declined before the dispatch's registers were modelled, and a decline is not turned
178
+ * into that guess. */
179
+ refuseDispatchUnlockedPassThrough(caseStateRead: boolean): void;
111
180
  }
112
181
 
113
182
  /** WHAT ONE ISA ALONE KNOWS ABOUT A CALL: everything `CallDeclarations.lower` asks a frontend. The
@@ -126,15 +195,44 @@ export interface CallSite {
126
195
  stackWord?(k: number): Value;
127
196
  /** how this frontend builds and splits a pair (`CallLowering.pairs`) */
128
197
  readonly pairs?: CallPairs;
129
- /** the high halves this frontend defines registers with, which are no argument */
130
- readonly highHalves?: HighHalves;
131
- /** what a guessed arity refuses on; without it a gap in the argument registers ends the count */
132
- readonly guess?: {
133
- /** the call's address, for the gap refusal */
134
- readonly at: number;
135
- /** refuses a guess of `argc` words this ABI's outgoing area could extend */
136
- refuse(argc: number): void;
198
+ /** what this frontend defines registers with that is no value and so no argument — a pending
199
+ * high half (frontend/high-half.ts), a jump table's base (frontend/jump-table.ts) */
200
+ readonly placeholders?: Pick<HighHalves, 'has' | 'guardRead'>;
201
+ /** whether `v`, in argument register `reg`, is what a jump-table dispatch left in that very
202
+ * register (frontend/jump-table.ts `parkedIn`): no argument, as a table base is none, so a guessed
203
+ * arity stops below the lowest register only it reaches (`fallbackArgc`'s `parked`), except one
204
+ * this ABI may skip (an O32 double's a1, a PowerPC pair's r4, a leading float's a0), which stays a
205
+ * gap. A copy of one below that ceiling is counted, and its read declines; a copy above it is
206
+ * dropped with everything else there (`mov r2,r1` ahead of `bl g` lifts `g(…)` without it), which
207
+ * only hand asm meets: no compiler copies a marker. */
208
+ readonly parked?: (v: Value, reg: string) => boolean;
209
+ /** whether `v`, in argument register `reg`, is the scaled index a jump-table dispatch left in that
210
+ * very register (frontend/jump-table.ts `scaledIndexIn`): a call handed it and one not compile
211
+ * alike, so a guessed arity that counts it refuses */
212
+ readonly scaledIndex?: (v: Value, reg: string) => boolean;
213
+ /** what a guessed arity refuses on. Required, as the gap refusal it feeds (`fallbackArgc`) has
214
+ * no opt-out: a count that stopped at the first register nothing reaches would drop every
215
+ * argument past an incoming one the call is handed unchanged (`logv(s, 1)` as `logv()`). */
216
+ readonly guess: {
217
+ /** where the call is, for the gap refusal: its address, or — for an ISA whose instructions
218
+ * carry none (Thumb's `.s` text) — a phrase that names it, printed as written */
219
+ readonly at: number | string;
220
+ /** refuses a guess of `argc` words this ABI's outgoing area could extend; absent where the
221
+ * frontend's frame model already refuses every word such a guess could leave out */
222
+ refuse?(argc: number): void;
137
223
  };
224
+ /** handed the argument registers an UNDECLARED arity leaves out although a value reaches them —
225
+ * past a guessed count, or past the bound of a call through an argument register — and the ones
226
+ * it passes. The call drops what the left-out ones hold, so a frontend that knows one to be an
227
+ * argument all the same refuses here. */
228
+ readonly leftOut?: (regs: readonly string[], passed: readonly string[]) => void;
229
+ /** whether the frontend withholds the shorter `/setup-args` reading from THIS CALL whatever it
230
+ * cuts; the frontend says why where it sets it. A thunk: it is asked once every block is lowered. */
231
+ readonly withholdsSetupArgs?: () => boolean;
232
+ /** whether the shorter `/setup-args` reading of this call asks `leftOut` of EVERY register it
233
+ * cuts, not only of those the call's own block wrote (`GuessedCallSite.asksEveryCut`); the
234
+ * frontend says why where it sets it. A thunk: it is asked once every block is lowered. */
235
+ readonly asksEveryCut?: () => boolean;
138
236
  }
139
237
 
140
238
  /** A call through a register. It names no callee, so no declaration plans it: an argument
@@ -160,9 +258,16 @@ export function callDeclarations(
160
258
  prototypes: Prototypes,
161
259
  lowering: CallLowering,
162
260
  fail: FrontendRefusal,
261
+ /** the function's own declaration, where one binds its body — asked as a call is lowered */
262
+ ownDeclaration: () => OwnDeclaration | undefined = () => ownDeclarationOf(name, prototypes, undefined),
163
263
  ): CallDeclarations {
164
264
  // What a call leaves holding nothing this function can name — checked against `argRegs` there.
165
265
  const callClobbers = clobberedByCall(target);
266
+ // `refuseDispatchUnlockedPassThrough`'s two facts, gathered as each call is lowered: whether a
267
+ // dispatch's marker reached a guessed call's argument registers, and the first guessed count
268
+ // the bytes cannot tell from another, as its refusal
269
+ let parkedInArgs = false;
270
+ let passThrough: { call: string; reg: string; why: string } | undefined;
166
271
  // …and what a call to a PAIR-RETURNING helper leaves: the same set minus the high half, because
167
272
  // the low half is the return register (already excluded) and `lower` writes the high one itself
168
273
  // from the callee's own result. The two arms of one rule: where the callee hands a register back,
@@ -180,9 +285,10 @@ export function callDeclarations(
180
285
  );
181
286
  }
182
287
  const pairReturnClobbers = callClobbers.filter((r) => r !== target.argRegs[1]);
288
+ const heldAtUndeclaredCall = new Set<Value>();
183
289
  const isFloatHelperName = (callee: string): boolean => {
184
290
  const h = lookupHelper(target.runtimeHelpers, callee);
185
- return h !== undefined && isFloatHelper(h);
291
+ return h !== undefined && isFloatHelper(h) && floatSidesOf(h).result;
186
292
  };
187
293
  /** the callee that handed back each pair a call returned, by the pair's value */
188
294
  const pairCallee = new Map<Value, string>();
@@ -191,7 +297,7 @@ export function callDeclarations(
191
297
  // The compiler's own runtime, off the TARGET (runtime-helpers.ts): which helpers a compiler
192
298
  // emits is a compiler fact, and reading one table for every ISA is how a scan for `__*di3`
193
299
  // reports zero on a compiler whose runtime spells them `__ll_*`.
194
- const helperProtos = helperPrototypes(target.runtimeHelpers);
300
+ const helperProtos = helperPrototypes(target);
195
301
  const wideHelper = (callee: string) => {
196
302
  // Through the table's one reader (`lookupHelper`): a bare index answers with a member of
197
303
  // `Object.prototype` for a callee named `toString`, which is truthy and has no `params` for
@@ -270,9 +376,11 @@ export function callDeclarations(
270
376
  // WHAT THE CONVERSION CANNOT DO ON ITS OWN. `declaredWidth` answers for every type asmlift can
271
377
  // spell, `long long` included, and `undefined` for a project typedef, a by-value struct or a
272
378
  // floating type. `declaredCallArgs` sizes a `double` from the target
273
- // (`TargetDescription.doubleArgWords`); one other such spelling and it states no layout at all,
274
- // because the question here is not how wide that parameter is but whether it occupies one
275
- // argument register or two, and the choice moves every later argument's home. So the declaration licenses no outgoing block and this returns `null`:
379
+ // (`TargetDescription.doubleArgWords`) and a typedef the declaration lists in `wordParams` (an
380
+ // enum, an array or a function type: `proto.ts` `paramClassOf`) at one word; one other such
381
+ // spelling and it states no layout at all, because the question here is not how wide that
382
+ // parameter is but whether it occupies one argument register or two, and the choice moves every
383
+ // later argument's home. So the declaration licenses no outgoing block and this returns `null`:
276
384
  // the call is lifted at the arg-register guess, exactly as a callee the project never declared
277
385
  // is, and the guess retracts the registers a call destroyed where a stated width would assert
278
386
  // them.
@@ -286,6 +394,79 @@ export function callDeclarations(
286
394
  // A frontend that places the widths before it lowers the call runs this over every call, so a
287
395
  // refusal thrown here reaches the caller ahead of every other slot-model refusal, which is right
288
396
  // because it is the most specific thing that was seen.
397
+ const known = (t: Prototypes, callee: string) => (Object.hasOwn(t, callee) ? t[callee] : undefined);
398
+ /** the declaration a callee is read at: the project's, then the runtime table's, then the C
399
+ * standard's (`proto.ts` `calleeDeclaration`; `declaredCall` gives the reasons for the order) */
400
+ const declarationOf = (callee: string): FnProto | undefined => calleeDeclaration(callee, prototypes, helperProtos);
401
+ const declaredParams = (callee: string): readonly DeclaredParam[] | null => {
402
+ const params = declarationOf(callee)?.params;
403
+ if (typeof params === 'number') {
404
+ return Array.from({ length: params }, () => ({ kind: 'word', spelling: '' }));
405
+ }
406
+ if (!Array.isArray(params)) {
407
+ return null;
408
+ }
409
+ const declared = declarationOf(callee)!;
410
+ return params.map((spelling, k) => ({ kind: paramClassOf(declared, params, k), spelling }));
411
+ };
412
+ // A STRUCT OR UNION PASSED BY VALUE THAT NO RULE PLACES REFUSES; IT IS NOT GUESSED. The arity
413
+ // guess counts argument registers, and a struct is not one: it takes as many words as its size
414
+ // (agbcc, O32), starts at an even word when it is 8-aligned (O32), or travels as the address of
415
+ // a copy (mwcc). A guess over it drops the struct (agbcc `takeQ(k, gQ)` with no layout for
416
+ // `struct Q` guesses `takeQ();`) or reads its words as separate arguments (O32 `takeW(gW)` as
417
+ // `takeW(*(s32 *)&gW)`), and either is C that compiles. This is
418
+ // not the abstention `declaredCallArgs` documents for an unsized typedef, which may still be one
419
+ // word: a parameter the declaration names a struct or union is known not to be.
420
+ //
421
+ // A VARIADIC DECLARATION REFUSES THE SAME GUESS over a fixed parameter that is not one word: it
422
+ // states a layout only where each fixed parameter is one word (`variadicFloor`), and otherwise its
423
+ // call is placed by the guess (`FnProto.variadicParams`), which splits a struct, a 64-bit value or
424
+ // a `double` into words. Its parameters are the project's fact, read off its own entry,
425
+ // which `declarationOf` reads past because it states no `params`.
426
+ const refuseUnplacedByValue = (callee: string, hidden: number): void => {
427
+ const own = known(prototypes, callee);
428
+ const fixed = variadicRefusal(own);
429
+ if (fixed !== undefined) {
430
+ const spelling = own!.variadicParams![fixed.param];
431
+ const byValue = paramClassOf(own!, own!.variadicParams!, fixed.param) === 'by-value';
432
+ fail(
433
+ `cannot lift '${name}': \`${callee}\` takes '${spelling}'${byValue ? ' by value' : ''} as its parameter ` +
434
+ `${fixed.param + 1}, and ${fixed.refused} — a variadic call is lifted only where each fixed parameter is one argument word`,
435
+ );
436
+ }
437
+ const p = declarationOf(callee);
438
+ const why = byValueRefusal(p, target, hidden);
439
+ if (why !== undefined) {
440
+ fail(
441
+ `cannot lift '${name}': \`${callee}\` takes '${(p!.params as readonly string[])[why.param]}' by value as its ` +
442
+ `parameter ${why.param + 1}, and ${why.refused} — a struct passed by value is lifted only where its argument words are known`,
443
+ );
444
+ }
445
+ };
446
+ // A FLOATING PARAMETER THE DECLARATION STATES BUT NOTHING LAYS OUT REFUSES; IT IS NOT GUESSED.
447
+ // Where the target has an FPU, `plan` refuses every floating parameter first (it travels in the
448
+ // FPU's registers). Where it has none — agbcc — a `double` is laid out in general words
449
+ // (`TargetDescription.doubleArgWords`) and a `float` or `long double` is not (`proto.ts`
450
+ // `declaredCallArgs`), so the call would fall to the arity guess, which is wrong both ways: a
451
+ // trailing argument passed through unchanged is dropped (`int g(int, float)`: `g(a + 1, b)` as
452
+ // `g(a0 + 1)`), and a register left over from the caller is counted (`float h(float)`: `h(v)` as
453
+ // `h(a1, a1)`). Neither compiles against the header. The unsized-typedef abstention does not
454
+ // apply: a floating spelling is known, and what is missing is a layout, not a width.
455
+ const refuseUnplacedFloat = (callee: string): void => {
456
+ const p = declarationOf(callee);
457
+ const params = p?.params;
458
+ if (target.fpu !== undefined || !Array.isArray(params)) {
459
+ return;
460
+ }
461
+ const at = params.findIndex((_, k) => paramClassOf(p!, params, k) === 'float');
462
+ if (at >= 0) {
463
+ fail(
464
+ `cannot lift '${name}': \`${callee}\` is declared to take '${params[at]}' as its parameter ${at + 1}, ` +
465
+ 'which this target passes in general argument words that are not laid out here — the arity guess ' +
466
+ 'in its place drops an argument passed through unchanged, or counts a leftover register as one',
467
+ );
468
+ }
469
+ };
289
470
  const declaredCall = (callee: string): DeclaredCall | null => {
290
471
  // Three tiers, narrowing: the project's own headers, then the compiler's runtime helpers,
291
472
  // then the signatures the C standard fixes (proto.ts). A project that re-declares one of the
@@ -299,12 +480,17 @@ export function callDeclarations(
299
480
  //
300
481
  // `Object.hasOwn` on all three tables: a callee named `toString` or `valueOf` would otherwise
301
482
  // read a `Function` off `Object.prototype` as its prototype entry.
302
- const known = (t: Prototypes) => (Object.hasOwn(t, callee) ? t[callee] : undefined);
303
- const own = known(prototypes);
483
+ const own = known(prototypes, callee);
304
484
  const returned = declaresAggregateReturn(own) ? structReturnOf(name, target, callee, own!, fail) : undefined;
305
- const proto = declaresParams(own) ? own : (known(helperProtos) ?? known(STANDARD_SIGNATURES));
306
- const declared = declaredCallArgs(proto, target);
485
+ const hidden = returned === undefined || returned === 'register' ? 0 : 1;
486
+ const declared:
487
+ { widths: readonly number[]; doubles: ReadonlySet<number>; byValue?: readonly ByValueArg[] } | undefined =
488
+ declaredCallArgs(declarationOf(callee), target) ?? byValueCallArgs(declarationOf(callee), target, hidden);
307
489
  const params = declared?.widths;
490
+ if (params === undefined) {
491
+ refuseUnplacedByValue(callee, hidden);
492
+ refuseUnplacedFloat(callee);
493
+ }
308
494
  if (params === undefined && returned !== undefined && returned !== 'register') {
309
495
  // a guessed arity reads argument registers from the first, which holds the hidden pointer
310
496
  fail(
@@ -320,7 +506,12 @@ export function callDeclarations(
320
506
  // (`double *`, `size_t`, or a bare parameter count, which states no type) lifts to a candidate
321
507
  // that does not compile. Asked of the printer's own predicate, so the two cannot disagree; a
322
508
  // headers world, which would declare the callee itself, loses the lift with it.
323
- if (returned !== undefined && returned !== 'register' && spellableProto(own, target, returned.type) === undefined) {
509
+ const byValueParams = new Set((declared?.byValue ?? []).map((b) => b.param));
510
+ if (
511
+ returned !== undefined &&
512
+ returned !== 'register' &&
513
+ spellableProto(own, target, returned.type, byValueParams) === undefined
514
+ ) {
324
515
  const via = `\`${callee}\` returns ${returned.type.kind === 'struct' ? returned.type.declared : typeToString(returned.type)} through a hidden pointer in ${target.argRegs[0]}`;
325
516
  if (typeof own!.params === 'number') {
326
517
  fail(
@@ -331,14 +522,27 @@ export function callDeclarations(
331
522
  `cannot lift '${name}': ${via}, and a parameter type of its declaration has no spelling the lifted source can declare it with`,
332
523
  );
333
524
  }
334
- const hidden = returned === undefined || returned === 'register' ? 0 : 1;
335
525
  const widths = hidden === 0 ? params : [32, ...params];
336
526
  const doubles = new Set([...declared!.doubles].map((i) => i + hidden));
527
+ const byValue = (declared!.byValue ?? []).map((b) => ({
528
+ ...b,
529
+ word: b.word + hidden,
530
+ }));
531
+ // a struct partly in the registers and partly on the stack (FUNCTION_ARG_PARTIAL_NREGS) is
532
+ // one value read from two places, which no lowering here builds
533
+ const past = byValue.find((b) => wordsOf(widths.slice(0, b.word), target) + b.words > target.argRegs.length);
534
+ if (past !== undefined) {
535
+ fail(
536
+ `cannot lift '${name}': \`${callee}\` takes ${structSpelling(past.type)} by value as its parameter ${past.param + 1}, ` +
537
+ 'and it does not fit in the argument registers — a struct passed on the stack is not modelled',
538
+ );
539
+ }
337
540
  return {
338
541
  widths,
339
542
  doubles,
340
- params: params.length,
543
+ params: declared!.widths.length - byValue.reduce((n, b) => n + b.words - 1, 0),
341
544
  ...(returned === undefined ? {} : { returned }),
545
+ ...(byValue.length === 0 ? {} : { byValue }),
342
546
  };
343
547
  };
344
548
  // ONE CALL, IN THE ORDER ITS QUESTIONS REFUSE: a struct the lowering cannot receive and a float,
@@ -388,14 +592,21 @@ export function callDeclarations(
388
592
  // register as its own value would honour the declaration by handing the callee one half.
389
593
  const wideAt = declared.widths.findIndex((w) => w > 32);
390
594
  if (wideAt >= 0) {
595
+ // `widths` holds argument words — a hidden struct-return pointer ahead of the declared
596
+ // list, and each word of a struct passed by value — and the text names the parameter
597
+ const hidden = declared.returned !== undefined && declared.returned !== 'register' ? 1 : 0;
598
+ const param =
599
+ wideAt -
600
+ hidden -
601
+ (declared.byValue ?? []).filter((b) => b.word < wideAt).reduce((n, b) => n + b.words - 1, 0);
391
602
  fail(
392
603
  `cannot lift '${name}': one half of a 64-bit value would be handed to '${callee}' — its parameter ` +
393
- `${wideAt + 1} is declared wider than a register, and this frontend passes each argument ` +
604
+ `${param + 1} is declared wider than a register, and this frontend passes each argument ` +
394
605
  'register as its own value rather than building the pair the ABI passes it in',
395
606
  );
396
607
  }
397
608
  }
398
- if (!lowering.stackArgs && declared !== null && wordsOf(declared.widths) > target.argRegs.length) {
609
+ if (!lowering.stackArgs && declared !== null && wordsOf(declared.widths, target) > target.argRegs.length) {
399
610
  fail(
400
611
  `cannot lift '${name}': outgoing stack arguments not modelled — '${callee}' is declared with ` +
401
612
  `${declared.widths.length} parameters and the argument registers carry ${target.argRegs.length}, so the rest ` +
@@ -424,9 +635,22 @@ export function callDeclarations(
424
635
  // a runtime helper's return is its table's, whatever a project declares on its name
425
636
  const declaredVoid = !isRuntimeHelperName(callee) && declaresVoidReturn(own);
426
637
  const voided = lowering.voidReturn && declaredVoid;
638
+ const image = returned === 'register' && registerStructLocal(known(prototypes, callee), target) !== undefined;
639
+ // A FLOAT RETURNED WITHOUT AN FPU IS BITS IN THE RETURN REGISTER. The candidate prints the call
640
+ // against the declaration, so `v = f()` into anything but a float converts the value (agbcc:
641
+ // `bl f; bl __fixsfsi`), where the bytes moved the bits. The call names the type, and
642
+ // `raise/floathelpers.ts` reads the result as that float or refuses. A target with an FPU
643
+ // refused the call above; a runtime helper's return is its table's.
644
+ const returnSpelling = own?.returns ?? declarationOf(callee)?.returns;
645
+ const floatReturn =
646
+ target.fpu === undefined && !isRuntimeHelperName(callee) && !voided && typeof returnSpelling === 'string'
647
+ ? floatingSpelling(returnSpelling)
648
+ : undefined;
427
649
  return {
650
+ ...(floatReturn === undefined ? {} : { floatReturn }),
428
651
  widths: wide?.params ?? declared?.widths ?? null,
429
652
  doubles: declared?.doubles ?? new Set(),
653
+ ...(declared?.byValue === undefined ? {} : { byValue: declared.byValue }),
430
654
  returns: voided
431
655
  ? { kind: 'void' }
432
656
  : pair
@@ -434,22 +658,226 @@ export function callDeclarations(
434
658
  : returned === undefined
435
659
  ? { kind: 'word' }
436
660
  : returned === 'register'
437
- ? { kind: 'register-struct' }
661
+ ? { kind: 'register-struct', image }
438
662
  : { kind: 'memory-struct', type: returned.type },
439
- // a struct leaves the return register holding nothing the caller may read as a value, and a
440
- // void callee leaves it holding whatever the callee did
663
+ // a struct leaves the return register holding nothing the caller may read as a value, save
664
+ // the image of one a local can be read off, and a void callee leaves it holding whatever the
665
+ // callee did
441
666
  clobbers: pair
442
667
  ? pairReturnClobbers
443
- : returned === undefined && !voided
668
+ : (returned === undefined && !voided) || image
444
669
  ? callClobbers
445
670
  : [...callClobbers, target.returnReg],
446
671
  declaredVoid,
447
672
  };
448
673
  };
674
+ // A VARIADIC DECLARATION WHOSE FIXED PARAMETERS ARE EACH ONE WORD STATES ITS FIRST ARGUMENTS:
675
+ // they are the first that many argument words, exactly, so they are read as stated words and only
676
+ // what the `...` takes is guessed, above them (`fallbackArgc`'s and the trim's `floor`). Read the
677
+ // way a call through argument register rN reads the registers below N (`lower`): one this
678
+ // function never wrote is its own argument passed on, and one a call destroyed names nothing and
679
+ // refuses (`SsaBuilder.finish`). So `int logv(const char *, int, ...)` lifts `logv(s + 1, c)` as
680
+ // `logv(a0 + 1, a1)`, where a guess from r0 sees r1 untouched and refuses the gap. A fixed list
681
+ // holding an UNSIZED spelling states no such words — the one may be two — and gets no floor; a
682
+ // fixed parameter known not to be one word refuses (`refuseUnplacedByValue`, in `declaredCall`).
683
+ const variadicFloor = (callee: string | undefined): number => {
684
+ const own = callee === undefined ? undefined : known(prototypes, callee);
685
+ const fixed = own?.params === undefined ? own?.variadicParams : undefined;
686
+ return fixed !== undefined && fixed.every((_, k) => paramClassOf(own!, fixed, k) === 'word') ? fixed.length : 0;
687
+ };
688
+ // A GUESS SHORTER THAN THE DECLARATION REFUSES. A declaration the lowering cannot lay out (an
689
+ // UNSIZED spelling: `proto.ts` `declaredCallArgs`), or a variadic one whose fixed parameters
690
+ // include one, still states how many parameters it has — fixed ones, for a variadic — and every
691
+ // one of them is at least one argument word. A guess that counts fewer registers has dropped
692
+ // arguments: the last one passed through unchanged leaves its register with no definition, so
693
+ // `void h(int, Foo)` with `Foo` unresolved lifts `h(n + 1, v)` as `h(a0 + 1)`. The count bounds
694
+ // the guess from below only — an unsized parameter may be two words, or a struct of several. A
695
+ // variadic call with a `variadicFloor` never reaches the bound: its guess starts there.
696
+ //
697
+ // KNOWN GAP: this bounds the count `fallbackArgc` takes, not what `SsaBuilder.finish` later
698
+ // retracts from it (a register a call between the setup and this one destroyed).
699
+ const refuseShortOfDeclared = (callee: string | undefined, argc: number): void => {
700
+ if (callee === undefined) {
701
+ return;
702
+ }
703
+ const p = declarationOf(callee);
704
+ const own = known(prototypes, callee);
705
+ const stated = Array.isArray(p?.params)
706
+ ? { n: p.params.length, what: 'parameters' }
707
+ : own?.variadicParams !== undefined && own.params === undefined
708
+ ? { n: own.variadicParams.length, what: 'fixed parameters before its `...`' }
709
+ : undefined;
710
+ if (stated !== undefined && argc < stated.n) {
711
+ fail(
712
+ `cannot lift '${name}': \`${callee}\` is declared with ${stated.n} ${stated.what}, which no layout here ` +
713
+ `places, and the arity guess in its place finds ${argc} argument register${argc === 1 ? '' : 's'} set up — ` +
714
+ 'an argument passed through unchanged leaves its register with nothing the guess can see',
715
+ );
716
+ }
717
+ };
718
+ // A GAP IN A GUESSED ARITY IS DECIDED WHERE THE REGISTER ABOVE IT IS SURELY AN ARGUMENT
719
+ // (`fallbackArgc`'s `decide`). The gap refuses because an argument register left at its incoming
720
+ // value and one the call does not pass look the same — but only while nothing says the call takes
721
+ // an argument above it. A register the call's own block wrote with a value that no memory read
722
+ // produced and that nothing but the call reads says exactly that: an optimising compiler deletes a
723
+ // dead computation, so the value exists only to be passed. And where argument words fill in order,
724
+ // every word below an argument is an argument too, so the gap's registers are passed on unchanged.
725
+ // Measured on 2026-10-18 at every toolchain's canonical flags: `logv(s, 1)` sets r1/a1/r4 alone,
726
+ // and at -O0 no gap arises at all (every argument is reloaded from its home first).
727
+ //
728
+ // EACH CLAUSE IS A MEASURED COUNTEREXAMPLE, so each refuses:
729
+ // - the value must be the call's own block's. Where a discarded read may land in an argument
730
+ // register (`compilerBehaviors.discardedReadMayFillArgRegister`), it must also be something no
731
+ // memory read produced: agbcc puts a DISCARDED volatile read in the lowest free argument
732
+ // register (`*(volatile int *)K; g(s)` is `ldr r1,[r1]; bl g`, at -O0 too), the passgap shape
733
+ // exactly. gcc2.7.2kmc, gcc2.7.2 and the three mwcc builds do the same once their usual
734
+ // discard register (v0/v1, r0) is busy: `int a = ga * 3; *(volatile int *)&gx; ...; logv(s)`
735
+ // is `lw a1,0(v0); ... jal logv` on KMC and `lwz r4,gx` on mwcc, the bytes of the twin that
736
+ // passes the read. Only ido7.1 discards into $zero every time, so only there a load only the
737
+ // call reads is an argument: `logv(s, gx)` lifts. Nothing else may read the value either
738
+ // (`SsaBuilder.requireSoleReader`, asked of the finished function): `gx = 1; logv(s)` and
739
+ // `gx = 1; logv(s, 1)` are one agbcc object, the stored constant left in r1.
740
+ // - the value must be no instruction's side effect (`SsaBuilder.noteIncidentalWrite`): a base
741
+ // writeback is left in its register whether anything wants it or not. agbcc copies a struct of
742
+ // 3+ words as `ldmia r3!, {r4, r5, r6}; stmia r2!, {r4, r5, r6}` and leaves both writebacks
743
+ // dead, so `*p = *q; return logv(s, n) + 1;` would lift `logv(a0, a1, a2 + 3, a3 + 3)`.
744
+ // - where a caller forwards its own destination as a callee's hidden pointer
745
+ // (`compilerBehaviors.forwardsStructReturnDestination`, ido7.1), a gap at the first argument
746
+ // register is decided only where the call's result is read (`SsaBuilder.requireReader`,
747
+ // owed by `lower`), by anything but a `ret`. `int b3(struct S *p){ *p = sg(1); return 0; }` is
748
+ // `jal sg; li a1,1` with a0 untouched, and would lift `sg(a0, 1)` even with `int b3(struct S *)`
749
+ // declared: the hidden pointer stated as a C argument, and the `*p =` store dropped. IDO reads
750
+ // no forwarded struct return's result register (five shapes, at the target.ts field), but a
751
+ // `ret` reads it in a function ending in the call, `void b7(struct S *p){ *p = sg(1); }`.
752
+ // - each gap register must hold the function's own incoming value on every path
753
+ // (`holdsEntryValue`): one a call destroyed is the trim's, and holds nothing.
754
+ // - where the ABI can leave a register empty below a filled one (target.ts `argRegisterHoles`)
755
+ // the gap may be that hole. An argument aligned to 8 skips an odd word (O32 a1, PowerPC
756
+ // r4/r6/r8), and the hole is ruled out only where that argument's second word holds nothing:
757
+ // no value reaches it and the own declaration fills fewer words. A leading float shadows the
758
+ // first general words on O32. It is ruled out only where the own declaration surely puts an
759
+ // integer word in each gap register. No register value can rule it out: the MIPS frontend
760
+ // refuses every instruction naming the FPU's file in a function that calls
761
+ // (frontend/mips.ts), so a float here is one passed on untouched in $f12, which nothing
762
+ // reads. A gap that starts at a1 under an a0 that only a read set up (`gx = a; gp(d, 5)`) is
763
+ // the 8-byte skip's case and refuses through it: a1 is odd, so it needs the own declaration's
764
+ // word bound, and `ownLeadingWordParamsOn` stops at the double.
765
+ // - where the own declaration binds the body, each gap register must surely hold one of its
766
+ // integer word parameters (`ownLeadingWordParamsOn`): no hidden struct pointer, no float.
767
+ // - at flags that keep a read nothing uses (`compilerBehaviors.keepsUnusedReads`, the -O0
768
+ // levels) the premise is suspended, as frontend/ppc.ts `computedPairHalf` suspends it. mwcc -O0
769
+ // keeps a dead `li r31,5`. The gap was measured never to arise there (mwcc -O0,p on all three
770
+ // builds, IDO -O1 and agbcc -O0 reload every argument from its home first), so this costs no
771
+ // lift.
772
+ // What the decision leaves open above the proven register is the call's own check
773
+ // (`decidedCallRefusal`). The proven register's value is returned, for the sole-reader
774
+ // obligation the call then takes on.
775
+ const decideGap = (
776
+ ssa: SsaBuilder,
777
+ bi: number,
778
+ gap: readonly string[],
779
+ set: string,
780
+ accept: (v: Value) => boolean,
781
+ ): Value | undefined => {
782
+ const regs = target.argRegs;
783
+ if (target.compilerBehaviors.keepsUnusedReads === true || !ssa.writtenSinceCall(set, bi)) {
784
+ return undefined;
785
+ }
786
+ const v = ssa.readVar(set, bi);
787
+ const def = ssa.irBlocks[bi].ops.find((op) => op.results.includes(v));
788
+ const sig = def === undefined ? undefined : (OPCODES[def.opcode as keyof typeof OPCODES] as OpSig | undefined);
789
+ const readsRefuse = target.compilerBehaviors.discardedReadMayFillArgRegister !== false;
790
+ if (
791
+ !accept(v) ||
792
+ ssa.isIncidentalWrite(v) ||
793
+ sig === undefined ||
794
+ sig.effects ||
795
+ (sig.reads && readsRefuse) ||
796
+ sig.terminator ||
797
+ def!.opcode === 'undef'
798
+ ) {
799
+ return undefined;
800
+ }
801
+ if (gap.some((g) => ssa.writtenSinceCall(g, bi) || !ssa.holdsEntryValue(g, bi))) {
802
+ return undefined;
803
+ }
804
+ const own = ownDeclaration();
805
+ const surelyOwn = own === undefined ? undefined : ownLeadingWordParamsOn(name, own.proto, target);
806
+ if (surelyOwn !== undefined && gap.some((g) => regs.indexOf(g) >= surelyOwn)) {
807
+ return undefined;
808
+ }
809
+ const holes = argRegisterHoles(target);
810
+ if (holes.leadingFloatShadow && regs.indexOf(gap[0]) === 0 && own?.proves !== true) {
811
+ return undefined;
812
+ }
813
+ const ownWords = own?.proves !== true ? undefined : ownArgumentWordsOn(name, own.proto, target);
814
+ for (const g of gap.filter((r) => holes.skippable.has(r))) {
815
+ const j = regs.indexOf(g);
816
+ const second = regs[j + 2];
817
+ if (
818
+ regs[j + 1] !== set ||
819
+ second === undefined ||
820
+ ssa.hasReachingDef(second, bi, accept) ||
821
+ ownWords === undefined ||
822
+ j + 2 < ownWords
823
+ ) {
824
+ return undefined;
825
+ }
826
+ }
827
+ return v;
828
+ };
829
+ // WHAT A DECIDED GAP LEAVES OPEN ABOVE THE COUNT: whether the call takes more than the `argc`
830
+ // words counted. The decision proves that the gap registers are this function's own arguments
831
+ // passed on. Past the count, the guess reads an unwritten register as no argument, as it does at
832
+ // every gapless call, and three things say otherwise. Each refuses (returns true):
833
+ // - an own parameter that may sit past the count (`ownArgumentWordsOn`, an upper bound). It is
834
+ // just as likely passed on as the gap below it was. On gcc2.7.2kmc and ido7.1, `int ll6(int a,
835
+ // int b, int c, int d){ return g(a, 2, c, d, 5) + 1; }` leaves c and d in a2/a3 and would lift
836
+ // `g(a0, 2)`.
837
+ // - a word of the outgoing area that reaches the call (`guess.refuse`, asked as if the count
838
+ // filled every argument register). It is a fifth argument, so every register below it is an
839
+ // argument too: the same `ll6`'s `sw v0,16(sp)`.
840
+ // - a floating argument this ABI passes apart from the general words (`fpu.slots: 'separate'`,
841
+ // PowerPC), which no guess counts. `int fl2(int b, float x){ return g(b, x, 3) + 1; }` leaves
842
+ // x in f1 on all three mwcc builds and would lift `g(a0, 3)`. A float in f1 that nothing reads is
843
+ // invisible, so on such an ABI only an own declaration whose parameters are all integers rules
844
+ // it out. Without a declaration this refuses. A value reaching an FPU argument register refuses
845
+ // on every ABI.
846
+ const decidedCallRefusal = (ssa: SsaBuilder, bi: number, argc: number, site: CallSite): boolean => {
847
+ const own = ownDeclaration();
848
+ if (own !== undefined) {
849
+ const words = ownArgumentWordsOn(name, own.proto, target);
850
+ if (words === undefined || words > argc) {
851
+ return true;
852
+ }
853
+ }
854
+ const fpu = target.fpu;
855
+ if (fpu !== undefined) {
856
+ if (fpu.argRegs.some((r) => ssa.hasReachingDef(r, bi, (v) => !site.placeholders?.has(v)))) {
857
+ return true;
858
+ }
859
+ const floats = own === undefined ? undefined : ownDeclaresFloatingParam(name, own.proto);
860
+ if (floats === true || (fpu.slots === 'separate' && !(own?.proves === true && floats === false))) {
861
+ return true;
862
+ }
863
+ }
864
+ try {
865
+ site.guess.refuse?.(target.argRegs.length);
866
+ } catch (e) {
867
+ if (e instanceof FrontendUnsupportedError) {
868
+ return true;
869
+ }
870
+ throw e;
871
+ }
872
+ return false;
873
+ };
449
874
  // THE CALL A PLAN LOWERS TO, in one order on every ISA: the arity, each argument word, the
450
875
  // `call` op, the guessed arity's record, the result and the clobbers.
451
876
  const lower = (site: CallSite): void => {
452
- const { ssa, bi, pairs, guess, highHalves } = site;
877
+ const { ssa, bi, pairs, guess, placeholders } = site;
878
+ // whether `v`, reaching argument register `r`, may be an argument the caller set up
879
+ const mayPass = (v: Value, r: string) => !placeholders?.has(v) && site.parked?.(v, r) !== true;
880
+ const holes = argRegisterHoles(target);
453
881
  if (lowering.pairs !== (pairs !== undefined) || lowering.stackArgs !== (site.stackWord !== undefined)) {
454
882
  throw new Error(`target '${target.id}': a call site's pairs and stack words must be what its lowering states`);
455
883
  }
@@ -487,15 +915,139 @@ export function callDeclarations(
487
915
  // `declaredCallArgs`), so `widths` is null for it and this falls to the guess below —
488
916
  // the same answer the callee would get with no prototype at all.
489
917
  const { widths, returns } = p;
918
+ // the leading arguments a variadic declaration states, where the rest is guessed
919
+ const floor = widths === null ? variadicFloor(callee) : 0;
920
+ // the gaps `decideGap` decided: the value that proved each, and the gap's own refusal, owed if
921
+ // that proof fails once the function is lifted
922
+ const decided: Array<{ value: Value; refuse: () => void }> = [];
923
+ // the refusals of the decided gaps at the first argument register that the call's result must
924
+ // be read to keep (`compilerBehaviors.forwardsStructReturnDestination`, at `decideGap`)
925
+ const resultOwed: Array<() => void> = [];
926
+ // how many argument words the decided gaps put beyond doubt
927
+ let decidedWords = 0;
490
928
  const argc =
491
929
  widths === null
492
930
  ? fallbackArgc(ssa, target.argRegs, bi, {
493
- accept: (v) => !highHalves?.has(v),
494
- gap: guess && { name, at: guess.at, fail },
931
+ accept: mayPass,
932
+ gap: { name, at: guess.at, fail },
933
+ floor,
934
+ parked: (r) =>
935
+ !holes.skippable.has(r) &&
936
+ !(holes.leadingFloatShadow && r === target.argRegs[0]) &&
937
+ (ssa.holdsValue(r, bi, (v) => site.parked?.(v, r) === true) ||
938
+ // a call in the case destroyed it: the hole is that call's, and refuses only where a
939
+ // register above holds a value written since (`fallbackArgc`'s `parked`)
940
+ (ssa.hasReachingDef(r, bi, (v) => site.parked?.(v, r) === true) &&
941
+ !target.argRegs
942
+ .slice(target.argRegs.indexOf(r) + 1)
943
+ .some((a) => ssa.holdsValue(a, bi, (v) => mayPass(v, a))))),
944
+ decide: (gap, set, refuse) => {
945
+ const value = decideGap(ssa, bi, gap, set, (v) => mayPass(v, set));
946
+ if (value === undefined) {
947
+ return false;
948
+ }
949
+ decided.push({ value, refuse });
950
+ if (gap[0] === target.argRegs[0] && target.compilerBehaviors.forwardsStructReturnDestination !== false) {
951
+ resultOwed.push(refuse);
952
+ }
953
+ decidedWords = target.argRegs.indexOf(set) + 1;
954
+ return true;
955
+ },
956
+ entry: (reg) =>
957
+ !ssa.writtenSinceCall(reg, bi) &&
958
+ ssa.holdsValue(
959
+ reg,
960
+ bi,
961
+ (v) => mayPass(v, reg) && ssa.paramReg.get(v) === reg && ssa.irBlocks[0].params.includes(v),
962
+ ),
495
963
  })
496
- : wordsOf(widths);
964
+ : wordsOf(widths, target);
497
965
  if (widths === null) {
498
- guess?.refuse(argc);
966
+ const index = target.argRegs
967
+ .slice(floor, argc)
968
+ .find((r) => ssa.hasReachingDef(r, bi, (v) => site.scaledIndex?.(v, r) === true));
969
+ if (index !== undefined) {
970
+ fail(
971
+ `cannot lift '${name}': the call ${typeof guess.at === 'number' ? `at 0x${guess.at.toString(16)}` : guess.at} ` +
972
+ `has no prototype, and ${index}, which the arity guessed without one counts, still holds the index the ` +
973
+ "jump table's dispatch scaled — a call handed it and one that is not compile to the same bytes, so the " +
974
+ 'argument count is not decidable',
975
+ );
976
+ }
977
+ // the lowest argument register past the stated ones that a marker or the scaled index a
978
+ // dispatch left there may reach, parked or not (a PowerPC pair's r4 never parks): where the
979
+ // pre-dispatch value stood
980
+ const markedAt = target.argRegs.findIndex(
981
+ (r, k) =>
982
+ k >= floor &&
983
+ ssa.hasReachingDef(r, bi, (v) => site.parked?.(v, r) === true || site.scaledIndex?.(v, r) === true),
984
+ );
985
+ parkedInArgs ||= markedAt >= 0;
986
+ if (passThrough === undefined) {
987
+ const call = typeof guess.at === 'number' ? `at 0x${guess.at.toString(16)}` : guess.at;
988
+ const last = target.argRegs[argc - 1];
989
+ // the blocks since the dispatch on the way to the call: a path stops at a `switch_br`'s block
990
+ const since = new Set<number>([bi]);
991
+ const dispatch = (at: number) => ssa.irBlocks[at].ops.some((op) => op.opcode === 'switch_br');
992
+ const lastWritten = argc <= floor || ssa.writtenOnEveryPath(last, bi, dispatch, (at) => since.add(at));
993
+ // what an effect those blocks made so far reads
994
+ const effectReads = new Set(
995
+ [...since].flatMap((at) =>
996
+ ssa.irBlocks[at].ops.flatMap((op) =>
997
+ (OPCODES[op.opcode as keyof typeof OPCODES] as OpSig | undefined)?.effects ? op.operands : [],
998
+ ),
999
+ ),
1000
+ );
1001
+ const stored = target.argRegs
1002
+ .slice(floor, argc)
1003
+ .find((r) => ssa.holdsValue(r, bi, (v) => mayPass(v, r) && effectReads.has(v)));
1004
+ const leftIncoming =
1005
+ markedAt < 0 ? undefined : target.argRegs.slice(argc, markedAt).find((r) => ssa.holdsEntryValue(r, bi));
1006
+ if (!lastWritten) {
1007
+ passThrough = {
1008
+ call,
1009
+ reg: last,
1010
+ why: 'the last register the count takes, holds a value some path since the dispatch did not write',
1011
+ };
1012
+ } else if (stored !== undefined) {
1013
+ passThrough = { call, reg: stored, why: 'which the count takes, holds a value an effect of its block uses' };
1014
+ } else if (leftIncoming !== undefined) {
1015
+ passThrough = {
1016
+ call,
1017
+ reg: leftIncoming,
1018
+ why: "which the count leaves out below a dispatch's register, holds its incoming value",
1019
+ };
1020
+ }
1021
+ }
1022
+ guess.refuse?.(argc);
1023
+ refuseShortOfDeclared(callee, argc);
1024
+ if (decided.length > 0 && decidedCallRefusal(ssa, bi, argc, site)) {
1025
+ decided[0].refuse();
1026
+ }
1027
+ }
1028
+ // WHAT EVERY ARGUMENT REGISTER HOLDS HERE, where no declaration stated the arity: a value the
1029
+ // count leaves out (or a later trim cuts) may be an argument the source passed, so the dead-read
1030
+ // pin may not call it unused. Asked through `holdsValue`, which reads no register.
1031
+ if (widths === null || bound >= 0) {
1032
+ for (const r of target.argRegs) {
1033
+ ssa.holdsValue(r, bi, (v) => {
1034
+ heldAtUndeclaredCall.add(v);
1035
+ return false; // keep walking: every path's value, not the first
1036
+ });
1037
+ }
1038
+ }
1039
+ // WHAT THE ARITY LEFT OUT, where no declaration stated it: an argument register past a guessed
1040
+ // count, or past the bound a call through an argument register sets, that a value reaches.
1041
+ // The gap refusal has already fired on any such register past a guess; past the bound, the
1042
+ // register leaves the call silently, and the frontend says whether it knows that one to be an
1043
+ // argument all the same.
1044
+ if (site.leftOut !== undefined && (widths === null || bound >= 0)) {
1045
+ site.leftOut(
1046
+ target.argRegs
1047
+ .slice(argc)
1048
+ .filter((r) => r !== indirect?.reg && ssa.hasReachingDef(r, bi, (v) => mayPass(v, r))),
1049
+ target.argRegs.slice(0, argc),
1050
+ );
499
1051
  }
500
1052
  // ARGUMENT WORD `k`: a register, or past the registers the word the frontend's ABI places
501
1053
  // there. A guess never exceeds `argRegs.length`, so a stack word can only come from a stated
@@ -508,7 +1060,7 @@ export function callDeclarations(
508
1060
  const guessed = (k: number): Value => {
509
1061
  const r = target.argRegs[k];
510
1062
  const v = ssa.readGuessedArg(r, bi);
511
- return highHalves ? highHalves.guardRead(name, r, v) : v;
1063
+ return placeholders ? placeholders.guardRead(name, r, v) : v;
512
1064
  };
513
1065
  const args: Value[] = [];
514
1066
  let k = 0;
@@ -517,7 +1069,7 @@ export function callDeclarations(
517
1069
  // rather than recovered from two 32-bit arguments later — `contracts.ts` would fire on
518
1070
  // the second reading anyway, since the structurer materialises an effectful call once
519
1071
  // per result. Its words are wherever the frontend's ABI placed them.
520
- args.push(w > 32 ? pairs!.fuse(word(k), word(k + 1)) : widths === null ? guessed(k) : word(k));
1072
+ args.push(w > 32 ? pairs!.fuse(word(k), word(k + 1)) : widths === null && k >= floor ? guessed(k) : word(k));
521
1073
  k += w > 32 ? 2 : 1;
522
1074
  }
523
1075
  // A 64-BIT VALUE MAY NOT LEAVE AS A WORD WHERE NOTHING SAYS HOW WIDE THE PARAMETER IS,
@@ -543,8 +1095,9 @@ export function callDeclarations(
543
1095
  // no prototype keys a call through a register, so the hint names one only for a named callee
544
1096
  const theCall = indirect === undefined ? `the call to '${callee}'` : `the call through ${indirect.reg}`;
545
1097
  const itsParams = indirect === undefined ? `'${callee}'s parameters` : "the function pointer's parameters";
1098
+ // a stated fixed word (`variadicFloor`) takes a half as the narrowing its declaration authorises
546
1099
  for (const [j, v] of args.entries()) {
547
- const half = pairs.halfOf.get(v);
1100
+ const half = j < floor ? undefined : pairs.halfOf.get(v);
548
1101
  // A DOUBLE THE RUNTIME RETURNED IS NO long long, so the hint below would send the
549
1102
  // reader to the wrong declaration: a double leaves a soft-float helper only into
550
1103
  // another one, the return, or a parameter declared `double` (`raise/floathelpers.ts`),
@@ -583,16 +1136,33 @@ export function callDeclarations(
583
1136
  // spells a different number — `g(1.5)` as `g(1073217536, 0)`. The call names the operands
584
1137
  // that are doubles, and `raise/floathelpers.ts` retypes each or refuses it.
585
1138
  const doubles = p.doubles.size ? [...p.doubles] : undefined;
1139
+ // A STRUCT PASSED BY VALUE IS ITS WORDS HERE, one operand each, and the call names them as
1140
+ // (first operand, words) pairs: `foldByValueArgs` folds them into the one object passed whole
1141
+ // once the function is lifted, or refuses.
1142
+ const byValue = p.byValue?.flatMap((b) => [b.word, b.words]);
586
1143
  const call = mkOp('call', {
587
1144
  operands: indirect === undefined ? args : [...args, indirect.address],
588
1145
  results: [res],
589
1146
  attrs: {
590
1147
  ...(callee !== undefined ? { target: callee } : { indirect: true }),
591
1148
  ...(sret === undefined ? {} : { sret: true }),
1149
+ ...(returns.kind === 'register-struct' && returns.image ? { [REGISTER_STRUCT]: true } : {}),
592
1150
  ...(doubles === undefined ? {} : { doubles }),
1151
+ ...(p.floatReturn === undefined ? {} : { floatReturn: p.floatReturn }),
1152
+ ...(byValue === undefined ? {} : { byValueWords: byValue }),
593
1153
  },
594
1154
  });
595
1155
  ssa.irBlocks[bi].ops.push(call);
1156
+ for (const d of decided) {
1157
+ ssa.requireSoleReader(d.value, call, d.refuse);
1158
+ }
1159
+ for (const refuse of resultOwed) {
1160
+ if (returns.kind === 'word') {
1161
+ ssa.requireReader(res, refuse);
1162
+ } else {
1163
+ refuse();
1164
+ }
1165
+ }
596
1166
  // A GUESSED arity is revisited in `finish()`: only once the whole function is lifted is it
597
1167
  // known whether every path to here passes through another call, which would have clobbered
598
1168
  // the argument registers this guess just read.
@@ -602,8 +1172,30 @@ export function callDeclarations(
602
1172
  // to an unprototyped type, so a dropped argument would never be refused, and `p(g())` would lift
603
1173
  // as `g(); p();`, which compiles to the same bytes. Kept, it lifts as `p(g())`, which passes what
604
1174
  // the machine passes — unless `g` is declared void, and its result names nothing.
1175
+ //
1176
+ // …AND WHAT THE TRIM CUTS IS LEFT OUT TOO, so the frontend is asked the `leftOut` question of it
1177
+ // as well: a guess of four the check above passed can still lose a frame address in r2 when
1178
+ // the trim cuts it back to the run below an unfresh r1 (`v = f64(); g(v, &s)` with neither
1179
+ // declared, which otherwise lifts as `g(v)`).
605
1180
  if (widths === null) {
606
- ssa.recordGuessedCall(call, bi, target, indirect === undefined ? undefined : (r) => !voidResults.has(r));
1181
+ const leftOut = site.leftOut;
1182
+ ssa.recordGuessedCall(
1183
+ call,
1184
+ bi,
1185
+ target,
1186
+ indirect === undefined ? undefined : (r) => !voidResults.has(r),
1187
+ leftOut === undefined
1188
+ ? undefined
1189
+ : (cut, kept) =>
1190
+ leftOut(
1191
+ cut.filter((r) => r !== indirect?.reg),
1192
+ kept,
1193
+ ),
1194
+ site.withholdsSetupArgs,
1195
+ site.asksEveryCut,
1196
+ // a decided gap puts its words beyond doubt, so neither reading cuts below them
1197
+ Math.max(floor, decidedWords),
1198
+ );
607
1199
  }
608
1200
  if (p.declaredVoid) {
609
1201
  voidResults.add(res);
@@ -617,20 +1209,36 @@ export function callDeclarations(
617
1209
  if (callee !== undefined) {
618
1210
  pairCallee.set(res, callee);
619
1211
  }
620
- } else if (returns.kind === 'word') {
1212
+ } else if (returns.kind === 'word' || (returns.kind === 'register-struct' && returns.image)) {
621
1213
  site.write(target.returnReg, res); // the callee defines the return register …
622
1214
  }
623
1215
  // … and the clobber is recorded after it, so that def is the CALLEE's; a struct, the memory at
624
1216
  // argument 0 or the return register's bytes, leaves that register holding nothing the caller
625
- // may read as a value
1217
+ // may read as a value, save the image of one a local is declared for, which a guessed arity
1218
+ // still does not count (`SsaBuilder.noteStructImage`)
626
1219
  ssa.noteCall(bi, p.clobbers);
1220
+ if (returns.kind === 'register-struct' && returns.image) {
1221
+ ssa.noteStructImage(bi, target.returnReg, res);
1222
+ }
627
1223
  };
628
1224
  return {
629
1225
  callClobbers,
630
1226
  returnsWithoutHiddenPointer: (callee) => returnsWithoutHiddenPointer(callee, prototypes, target),
631
1227
  declaredCall,
1228
+ declaredParams,
632
1229
  plan,
633
1230
  lower,
1231
+ heldAtUndeclaredCall,
1232
+ refuseDispatchUnlockedPassThrough: (caseStateRead) => {
1233
+ if ((parkedInArgs || caseStateRead) && passThrough !== undefined) {
1234
+ fail(
1235
+ `cannot lift '${name}': the call ${passThrough.call} has no prototype, and ${passThrough.reg}, ` +
1236
+ `${passThrough.why}, in a function that lifts only through what a jump-table dispatch left in its ` +
1237
+ 'registers — the arity guessed without a prototype and another compile to the same bytes, so the ' +
1238
+ 'argument count is not decidable',
1239
+ );
1240
+ }
1241
+ },
634
1242
  };
635
1243
  }
636
1244
 
@@ -639,9 +1247,9 @@ export function callDeclarations(
639
1247
  // up (on agbcc, thumb.h:644-645, 672), so the call's first word is that pointer. What it returns in
640
1248
  // the return register is not a value the caller reads (calls.c: the struct is the memory at the
641
1249
  // address). One the target returns in registers takes its arguments where they are declared, and
642
- // its registers hold the struct's bytes, which nothing here reads as a struct: the call is
643
- // `'register'` and a read of the return register after it refuses. Every other struct-returning
644
- // call refuses, naming why.
1250
+ // its registers hold the struct's bytes: the call is `'register'`, and the return register is the
1251
+ // struct's image where a local of it can be declared (`registerStructLocal`) and holds nothing the
1252
+ // caller may read otherwise. Every other struct-returning call refuses, naming why.
645
1253
  function structReturnOf(
646
1254
  name: string,
647
1255
  target: TargetDescription,
@@ -649,22 +1257,46 @@ function structReturnOf(
649
1257
  own: FnProto,
650
1258
  fail: FrontendRefusal,
651
1259
  ): StructReturn | 'register' {
652
- const refuse = (why: string): never =>
1260
+ const read = returnedStruct(own, target);
1261
+ if (typeof read === 'object' && 'refused' in read) {
653
1262
  fail(
654
- `cannot lift '${name}': \`${callee}\` is declared to return ${own.returns ?? 'a struct or union'} by value, and ${why}`,
1263
+ `cannot lift '${name}': \`${callee}\` is declared to return ${own.returns ?? 'a struct or union'} by value, and ${read.refused}`,
655
1264
  );
1265
+ }
1266
+ return read as StructReturn | 'register';
1267
+ }
1268
+
1269
+ /** HOW A FUNCTION DECLARED TO RETURN A STRUCT OR UNION BY VALUE HANDS IT BACK on this target: the
1270
+ * struct laid out as a local of it is declared (`type`) where the target returns it through a
1271
+ * hidden pointer in argument 0, `'register'` where it returns it in registers, and the reason
1272
+ * otherwise. Both halves of the return read this: a call to such a callee (`structReturnOf`) and a
1273
+ * function that itself returns one (pipeline.ts `ownStructReturnThroughMemory`). */
1274
+ export function returnedStruct(
1275
+ own: FnProto,
1276
+ target: TargetDescription,
1277
+ ): { type: Extract<IrType, { kind: 'struct' }> } | 'register' | { refused: string } {
656
1278
  const layout = returnedAggregate(own);
657
1279
  const inMemory = returnsInMemory(layout, target);
658
1280
  if (inMemory === undefined) {
659
- refuse(
660
- 'nothing here says whether it comes back through a hidden pointer — its members are not all known, this target does not ' +
1281
+ return {
1282
+ refused:
1283
+ 'nothing here says whether it comes back through a hidden pointer — its members are not all known, this target does not ' +
661
1284
  'say how it lays one of them out (an enum, a bitfield), or it states no rule ' +
662
1285
  '(a prototype states them as `returnLayout`, a context by defining the struct)',
663
- );
1286
+ };
664
1287
  }
665
1288
  if (inMemory === false) {
666
1289
  return 'register';
667
1290
  }
1291
+ const type = structLocalType(own, target);
1292
+ return 'refused' in type ? type : { type };
1293
+ }
1294
+
1295
+ /** The struct a declaration returns by value as a local of it is declared, or why it has none. */
1296
+ export function structLocalType(
1297
+ own: FnProto,
1298
+ target: TargetDescription,
1299
+ ): Extract<IrType, { kind: 'struct' }> | { refused: string } {
668
1300
  // the local it lands in is declared as the header spells the type, qualifiers aside; the tag
669
1301
  // is what the declarations block defines it by, and a typedef name is that tag too
670
1302
  const spelling = (own.returns ?? '')
@@ -673,14 +1305,286 @@ function structReturnOf(
673
1305
  .replace(/\s+/g, ' ');
674
1306
  const tag = /^(?:(?:struct|union)\s+)?([A-Za-z_]\w*)$/.exec(spelling)?.[1];
675
1307
  if (tag === undefined) {
676
- refuse('it names no type a local of it could be declared with');
1308
+ return { refused: 'it names no type a local of it could be declared with' };
677
1309
  }
678
- const type = aggregateType(tag!, spelling, layout, target);
679
- if (type === undefined) {
680
- refuse(
681
- 'it is a union, or its members are not all known or this target does not say how it lays one of them out ' +
1310
+ const type = aggregateType(tag, spelling, returnedAggregate(own), target);
1311
+ if (type?.kind !== 'struct') {
1312
+ return {
1313
+ refused:
1314
+ 'it is a union, or its members are not all known or this target does not say how it lays one of them out ' +
682
1315
  '(an enum, a bitfield) — the local it lands in has no type here',
683
- );
1316
+ };
1317
+ }
1318
+ return type;
1319
+ }
1320
+
1321
+ // A STRUCT RETURNED IN THE RETURN REGISTER IS READ OFF A LOCAL OF IT (frontend/register-struct-call.ts).
1322
+ // The register holds the struct's bytes as the target lays them in a word: from bit 0 up where
1323
+ // the target is little-endian (agbcc's `{u16}` is `strh r0` off the call), from bit 31 down where it
1324
+ // is big-endian (mwcc's is `srwi r0,r3,16`). So the register is the struct's image, which the call
1325
+ // writes and `liftRegisterStructCalls` re-reads as a load of the local the call assigns, and the
1326
+ // member a later read takes out of it is that local's member: `s = mk(x); gV = s.h;`, which is
1327
+ // what every such compiler compiles to these bytes. Only the declaration says the call returns a
1328
+ // struct at all, since the bytes are a scalar callee's, so only a callee the project declares
1329
+ // gets one, and only where the local can be:
1330
+ // - declared with a type (`structLocalType`): a union or a bitfield has none here;
1331
+ // - one, two or four bytes, the widths one load reads whole — three bytes, or the second
1332
+ // register of an eight-byte struct, is an image no load here builds;
1333
+ // - printed beside its callee's prototype, which the declarations block defines it by;
1334
+ // - all integers: a floating member has no spelling here that reads its bits as the float they
1335
+ // are (structure.ts `frameStructMember` spells an integer field), and `*(s32 *)&s` stored to a
1336
+ // `float` converts the value (agbcc: `bl __floatsisf`).
1337
+ // Otherwise the register still holds nothing the caller may read. The ONE derivation of that type:
1338
+ // the call plan marks the call by it, and `liftRegisterStructCalls` declares the local by it.
1339
+ export function registerStructLocal(
1340
+ own: FnProto | undefined,
1341
+ target: TargetDescription,
1342
+ ): Extract<IrType, { kind: 'struct' }> | undefined {
1343
+ if (own === undefined) {
1344
+ return undefined;
1345
+ }
1346
+ const type = structLocalType(own, target);
1347
+ if ('refused' in type || ![1, 2, 4].includes(type.size ?? 0) || hasFloatingMember(type)) {
1348
+ return undefined;
1349
+ }
1350
+ return spellableProto(own, target, type) === undefined ? undefined : type;
1351
+ }
1352
+
1353
+ function hasFloatingMember(t: IrType): boolean {
1354
+ switch (t.kind) {
1355
+ case 'float':
1356
+ return true;
1357
+ case 'struct':
1358
+ return t.fields.some((f) => hasFloatingMember(f.type));
1359
+ case 'union':
1360
+ return t.members.some((f) => hasFloatingMember(f.type));
1361
+ case 'array':
1362
+ return hasFloatingMember(t.elem);
1363
+ default:
1364
+ return false;
1365
+ }
1366
+ }
1367
+
1368
+ /** A STRUCT PASSED BY VALUE IS THE ONE OBJECT ITS ARGUMENT WORDS HOLD AT THE CALL. The lowering
1369
+ * hands its words over one operand each (`byValueWords`, pairs of first operand and word count);
1370
+ * C passes the struct, so the words must be one object's, whole, and that object is what the call
1371
+ * is handed. Two objects are:
1372
+ *
1373
+ * - A FRAME OBJECT the words are loaded off — word loads at consecutive words from one `laddr`
1374
+ * offset. The operand becomes that offset's address, and the frame-object audit judges the
1375
+ * object as the struct (frame-objects.ts `objectShapes`).
1376
+ * - A GLOBAL the words are loaded off, or were just stored to — word accesses at consecutive words
1377
+ * from the symbol's own address (`gaddr`, one or one per word of the same symbol), loaded off it
1378
+ * or stored there as the very values passed. The operand becomes the symbol's address, spelled
1379
+ * bare (`take(gS)`), which compiles only where the declarations type the global as the struct.
1380
+ * So a symbol C cannot name (a section symbol, `.bss`; a function-scope static's `sQ.3`)
1381
+ * refuses, and so does one the declaration context or the symbol map types as anything else:
1382
+ * the struct at offset 0 of `gQs[4]` or of `struct O2 { struct Q q; … } gO2` sits at the
1383
+ * symbol's own address too, and `take(gQs)` is a type error against the header that declares
1384
+ * it (`bareStructRefusal`).
1385
+ *
1386
+ * Either way every access is unqualified and in the call's own block, and no store or call lies
1387
+ * between the earliest of them and the call but those accesses themselves, so the object holds at
1388
+ * the call what the words carry. Every other source — registers computed in place, words off two
1389
+ * objects, off a pointer or into a global's interior, a store the call may not see last — refuses.
1390
+ *
1391
+ * The operand's value is typed a pointer to the struct, and a global's `gaddr` is a fresh one that
1392
+ * names the struct too (`passedAs`), so value numbering never merges it with a plain address of
1393
+ * the symbol. The call then names which operands it passes by value (`byValue`, pairs of operand
1394
+ * and declared parameter, read by ir/core.ts `byValueOperands`). */
1395
+ export function foldByValueArgs(
1396
+ name: string,
1397
+ irBlocks: Block[],
1398
+ calls: Pick<CallDeclarations, 'declaredCall'>,
1399
+ declarations: { symbols: SymbolMap | undefined; context: ContextGlobals | undefined },
1400
+ ): void {
1401
+ const defs = new Map<Value, { op: Op; blk: Block }>();
1402
+ for (const blk of irBlocks) {
1403
+ for (const op of blk.ops) {
1404
+ op.results.forEach((r) => defs.set(r, { op, blk }));
1405
+ }
1406
+ }
1407
+ for (const blk of irBlocks) {
1408
+ for (const call of [...blk.ops]) {
1409
+ const words = call.attrs.byValueWords as number[] | undefined;
1410
+ if (call.opcode !== 'call' || words === undefined) {
1411
+ continue;
1412
+ }
1413
+ const callee = calleeName(call);
1414
+ const declared = callee === undefined ? undefined : calls.declaredCall(callee)?.byValue;
1415
+ const pairs = Array.from({ length: words.length / 2 }, (_, i) => {
1416
+ const arg = declared?.find((b) => b.word === words[2 * i]);
1417
+ const type = arg?.type;
1418
+ if (type?.kind !== 'struct' || type.size === undefined) {
1419
+ throw new Error(`internal: a by-value operand of a call to '${callee}' has no declared struct`);
1420
+ }
1421
+ return { first: words[2 * i], n: words[2 * i + 1], type, param: arg!.param };
1422
+ });
1423
+ // highest first, so folding one leaves the operand places of those below it as they were
1424
+ for (const { first, n, type } of [...pairs].sort((x, y) => y.first - x.first)) {
1425
+ const at = blk.ops.indexOf(call);
1426
+ const passed = call.operands.slice(first, first + n);
1427
+ // the earliest of `accesses` and every op after it up to the call writes nothing else
1428
+ const settled = (accesses: readonly Op[]): boolean => {
1429
+ const earliest = Math.min(...accesses.map((a) => blk.ops.indexOf(a)));
1430
+ return blk.ops
1431
+ .slice(earliest, at)
1432
+ .every((op) => (op.opcode !== 'store' && op.opcode !== 'call') || accesses.includes(op));
1433
+ };
1434
+ // a word access at byte `start + 4 * i` from one base, unqualified, in the call's block
1435
+ // before the call
1436
+ const wordAt = (op: Op | undefined, i: number, base: Value | undefined, start = 0): op is Op =>
1437
+ op !== undefined &&
1438
+ blk.ops.indexOf(op) >= 0 &&
1439
+ blk.ops.indexOf(op) < at &&
1440
+ (op.attrs.width as number) === 4 &&
1441
+ !qualified(op) &&
1442
+ op.operands[0] === base &&
1443
+ (op.attrs.off as number) === start + 4 * i;
1444
+ const loads = passed.map((v) => defs.get(v)?.op);
1445
+ const loaded = loads.every((d) => d?.opcode === 'load');
1446
+ const base0 = loaded ? loads[0]!.operands[0] : undefined;
1447
+ const baseDef = base0 === undefined ? undefined : defs.get(base0)?.op;
1448
+ let object: Op | undefined;
1449
+ if (baseDef?.opcode === 'laddr') {
1450
+ // off the frame, through one `laddr` or several of the same offset (the slot model mints
1451
+ // one per access): the displacement is past the `laddr`'s own offset
1452
+ const frameAt = loads.map((d) => {
1453
+ const b = defs.get(d!.operands[0])?.op;
1454
+ return b?.opcode === 'laddr' && wordAt(d, 0, d!.operands[0], d!.attrs.off as number)
1455
+ ? (b.attrs.off as number) + (d!.attrs.off as number)
1456
+ : undefined;
1457
+ });
1458
+ const from = frameAt[0];
1459
+ if (from !== undefined && frameAt.every((o, i) => o === from + 4 * i) && settled(loads as Op[])) {
1460
+ object = mkOp('laddr', { results: [mkValue(T.ptr(type))], attrs: { off: from } });
1461
+ }
1462
+ } else {
1463
+ const symbol = (v: Value | undefined): string | undefined => {
1464
+ const d = v === undefined ? undefined : defs.get(v)?.op;
1465
+ return d?.opcode === 'gaddr' && d.attrs.code !== true ? (d.attrs.sym as string) : undefined;
1466
+ };
1467
+ // ONE SYMBOL, NOT ONE VALUE: kmc addresses each word through a `lui`/`lw` pair of its own,
1468
+ // so the words come off several `gaddr`s of the same symbol rather than one base. Each
1469
+ // access is still at its word's offset from the symbol's own address.
1470
+ const ofSymbol = (op: Op | undefined, i: number, sym0: string): op is Op =>
1471
+ op !== undefined && symbol(op.operands[0]) === sym0 && wordAt(op, i, op.operands[0]);
1472
+ const sym0 = symbol(base0);
1473
+ let sym: string | undefined;
1474
+ if (loaded && sym0 !== undefined && loads.every((d, i) => ofSymbol(d, i, sym0)) && settled(loads as Op[])) {
1475
+ sym = sym0;
1476
+ } else {
1477
+ // the words stored to a global just before the call: the last store of each, in order
1478
+ const stores = passed.map((v) =>
1479
+ blk.ops
1480
+ .slice(0, at)
1481
+ .filter((op) => op.opcode === 'store' && op.operands[1] === v)
1482
+ .at(-1),
1483
+ );
1484
+ const sink = symbol(stores[0]?.operands[0]);
1485
+ if (sink !== undefined && stores.every((st, i) => ofSymbol(st, i, sink)) && settled(stores as Op[])) {
1486
+ sym = sink;
1487
+ }
1488
+ }
1489
+ const misnamed = sym === undefined ? undefined : bareStructRefusal(sym, type, declarations);
1490
+ if (misnamed !== undefined) {
1491
+ throw new FrontendUnsupportedError(
1492
+ `cannot lift '${name}': the call to \`${callee ?? 'a function pointer'}\` passes ${structSpelling(type)} ` +
1493
+ `by value off '${sym}', and ${misnamed} — the struct would be spelled as that symbol bare`,
1494
+ );
1495
+ }
1496
+ if (sym !== undefined) {
1497
+ object = mkOp('gaddr', {
1498
+ results: [mkValue(T.ptr(type))],
1499
+ attrs: { sym, passedAs: structSpelling(type) },
1500
+ });
1501
+ }
1502
+ }
1503
+ if (object === undefined) {
1504
+ throw new FrontendUnsupportedError(
1505
+ `cannot lift '${name}': the call to \`${callee ?? 'a function pointer'}\` passes ${structSpelling(type)} ` +
1506
+ 'by value, and its words are not one frame object or one global, read whole just before the call — ' +
1507
+ 'a struct passed from anywhere else is not modelled',
1508
+ );
1509
+ }
1510
+ blk.ops.splice(at, 0, object);
1511
+ call.operands.splice(first, n, object.results[0]);
1512
+ // the word loads, and a frame address only they read, are dead now, and would otherwise be
1513
+ // judged as accesses of their own
1514
+ for (const d of loads) {
1515
+ if (d?.opcode === 'load') {
1516
+ dropIfUnread(irBlocks, d);
1517
+ const b = defs.get(d.operands[0])?.op;
1518
+ if (b?.opcode === 'laddr') {
1519
+ dropIfUnread(irBlocks, b);
1520
+ }
1521
+ }
1522
+ }
1523
+ }
1524
+ // each folded struct is one operand now: its place is its first word less the words folded
1525
+ // below it
1526
+ const sorted = [...pairs].sort((x, y) => x.first - y.first);
1527
+ const { byValueWords: _, ...attrs } = call.attrs;
1528
+ call.attrs = {
1529
+ ...attrs,
1530
+ byValue: sorted.flatMap(({ first, param }) => [
1531
+ first - pairs.filter((q) => q.first < first).reduce((m, q) => m + q.n - 1, 0),
1532
+ param,
1533
+ ]),
1534
+ };
1535
+ }
1536
+ }
1537
+ }
1538
+
1539
+ /** Why the global `sym` cannot be spelled bare as the struct `type` a call passes by value, or
1540
+ * undefined where it can: a C name, declared as exactly that struct where a declaration context or
1541
+ * a symbol map types it at all. A symbol neither types is declared from the call (declare.ts
1542
+ * `passedAs`). */
1543
+ function bareStructRefusal(
1544
+ sym: string,
1545
+ type: IrType,
1546
+ { symbols, context }: { symbols: SymbolMap | undefined; context: ContextGlobals | undefined },
1547
+ ): string | undefined {
1548
+ // the linker's name for the object, where it is one no C source spells: a static at its
1549
+ // section's start relocated against the section (`.bss`), or a function-scope static's name and
1550
+ // counter (`sQ.3`), which only the static's definition spells (frontend/reloc-symbol.ts)
1551
+ const unspellable = unspellableReason(sym);
1552
+ if (unspellable !== null) {
1553
+ return `that is no name C can spell: it ${unspellable}`;
1554
+ }
1555
+ // ONE STRUCT, MANY SPELLINGS: `typedef struct Q {…} Q_t;` spells it `Q_t` and `struct Q`, and a
1556
+ // header passes one and declares the other. Each side is resolved to the struct it names through
1557
+ // the context's typedefs (`structNamed`), and a spelling that resolves to none matches only as
1558
+ // written.
1559
+ const spelling = structSpelling(type);
1560
+ const passed = structNamed(spelling, context);
1561
+ const isPassed = (other: string): boolean =>
1562
+ other === spelling || (passed !== undefined && structNamed(other, context) === passed);
1563
+ if (context?.declared.has(sym) && !isPassed(context.objectTypes.get(sym) ?? '')) {
1564
+ return `the declaration context does not declare it as ${spelling} (an array, an enclosing struct, another type)`;
1565
+ }
1566
+ const info = symbols === undefined ? undefined : symbolsByName(symbols, new Set([sym])).get(sym);
1567
+ // the map names a struct by its tag, or by the typedef name that binds an untagged one
1568
+ // (symbols.ts `structName`)
1569
+ const mapped = info?.structName;
1570
+ if (
1571
+ info?.shape !== undefined &&
1572
+ !(info.shape === 'struct' && mapped !== undefined && (isPassed(`struct ${mapped}`) || passed === mapped))
1573
+ ) {
1574
+ return `the symbol map types it as ${info.shape === 'struct' ? `struct ${info.structName ?? '(untagged)'}` : `${info.shape === 'array' ? 'an' : 'a'} ${info.shape}`}`;
1575
+ }
1576
+ return undefined;
1577
+ }
1578
+
1579
+ /** Removes `op` where nothing reads its result. */
1580
+ function dropIfUnread(irBlocks: Block[], op: Op): void {
1581
+ const r = op.results[0];
1582
+ const read = irBlocks.some((b) =>
1583
+ b.ops.some((x) => x.operands.includes(r) || x.successors.some((sc) => sc.args.includes(r))),
1584
+ );
1585
+ if (!read) {
1586
+ for (const b of irBlocks) {
1587
+ b.ops = b.ops.filter((x) => x !== op);
1588
+ }
684
1589
  }
685
- return { type: type! };
686
1590
  }