@geastack/compiler 1.0.23 → 1.0.25
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.
- package/dist/cli.js +0 -0
- package/dist/ir/dense-loops.d.ts +11 -2
- package/dist/ir/dense-loops.js +58 -9
- package/dist/ir/integer-storage.d.ts +8 -0
- package/dist/ir/integer-storage.js +9 -2
- package/dist/ir/integers.d.ts +26 -0
- package/dist/ir/integers.js +109 -6
- package/dist/ir/numeric-intrinsics.d.ts +1 -1
- package/dist/ir/numeric-intrinsics.js +10 -3
- package/dist/representation/derive.js +30 -1
- package/dist/semantics/frontend.js +5 -1
- package/dist/semantics/normalize/asserted-argument-arms.d.ts +38 -0
- package/dist/semantics/normalize/asserted-argument-arms.js +182 -0
- package/dist/semantics/normalize/structural.d.ts +2 -1
- package/dist/semantics/normalize/structural.js +10 -3
- package/dist/targets/cpp/emit-arrays.js +2 -2
- package/dist/targets/cpp/emit-callable.js +20 -0
- package/dist/targets/cpp/emit-properties.js +15 -2
- package/dist/targets/cpp/emit.d.ts +0 -26
- package/dist/targets/cpp/emit.js +22 -0
- package/dist/targets/cpp/translation-unit.js +15 -3
- package/package.json +1 -1
- package/src/targets/cpp/runtime/gea_runtime.h +38 -11
- package/dist/representation/any-null-record-members.d.ts +0 -41
- package/dist/representation/any-null-record-members.js +0 -115
- package/dist/semantics/any-null-member-declarations.d.ts +0 -32
- package/dist/semantics/any-null-member-declarations.js +0 -90
- package/dist/semantics/normalize/fresh-array-result.d.ts +0 -32
- package/dist/semantics/normalize/fresh-array-result.js +0 -125
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/ir/dense-loops.d.ts
CHANGED
|
@@ -70,8 +70,9 @@ export interface DenseArray {
|
|
|
70
70
|
* rather than the loop's counter -- `ring[i % ring.length]`, the shape every
|
|
71
71
|
* ring buffer and hash bucket is written in.
|
|
72
72
|
*
|
|
73
|
-
* A different proof, and a simpler one: `x % n` lies in `
|
|
74
|
-
* dividend whenever `n > 0
|
|
73
|
+
* A different proof, and a simpler one: `x % n` lies in `(-n, n)` for every
|
|
74
|
+
* dividend whenever `n > 0` -- and in `[0, n)` once the access has tested
|
|
75
|
+
* its sign (`DenseAccess.signed`) -- so the window needs no bound, no base and no
|
|
75
76
|
* counter -- only that the array is non-empty and has no holes, which is what
|
|
76
77
|
* the preheader checks. `bases`, `bound`, `inclusive`, `step` and `widened`
|
|
77
78
|
* carry no meaning for one of these.
|
|
@@ -173,6 +174,14 @@ export interface DenseAccess {
|
|
|
173
174
|
readonly array: number;
|
|
174
175
|
/** The flag it renders behind: the innermost enclosing loop's, which implies the window's own. */
|
|
175
176
|
readonly flag: number;
|
|
177
|
+
/**
|
|
178
|
+
* Whether this access's index is a REMAINDER, which the window's flag does
|
|
179
|
+
* not bound below: `x % n` takes the dividend's sign, so `-3 % 16` is `-3`
|
|
180
|
+
* and no preheader check can see that coming. The emitter tests the index's
|
|
181
|
+
* sign at the access itself -- a register compare, not the size reload the
|
|
182
|
+
* window exists to remove. A mask (`x & M`) is never negative and needs none.
|
|
183
|
+
*/
|
|
184
|
+
readonly signed: boolean;
|
|
176
185
|
}
|
|
177
186
|
export interface DenseLoopPlan {
|
|
178
187
|
readonly arrays: readonly DenseArray[];
|
package/dist/ir/dense-loops.js
CHANGED
|
@@ -252,7 +252,7 @@ export const denseLoopsOf = (body, hoists = loopInvariantHoistsOf(body)) => {
|
|
|
252
252
|
const flag = flagAt(blockOfOperation.get(operation) ?? loop.preheader);
|
|
253
253
|
if (flag === null)
|
|
254
254
|
continue;
|
|
255
|
-
accesses.set(operation, { array: ordinal, flag });
|
|
255
|
+
accesses.set(operation, { array: ordinal, flag, signed: candidate.signed.includes(operation) });
|
|
256
256
|
}
|
|
257
257
|
}
|
|
258
258
|
}
|
|
@@ -504,9 +504,14 @@ const windowsOfLoop = (blocks, body, context) => {
|
|
|
504
504
|
// taken modulo needs no number carried to the preheader, and a program
|
|
505
505
|
// that spells both is better served by the shape that checks less.
|
|
506
506
|
const ownLength = wrapsOwnLength(operation.key, receiver, context);
|
|
507
|
-
const
|
|
507
|
+
const at = block.operations.indexOf(operation);
|
|
508
|
+
const modulus = ownLength ? null : constantModulusOf(operation.key, context, block.operations, at);
|
|
508
509
|
const wrapped = ownLength || modulus !== null;
|
|
509
|
-
|
|
510
|
+
const signed = ownLength || (modulus !== null && !isMaskKey(operation.key, context, block.operations, at));
|
|
511
|
+
// A typed array has no own-length remainder form here (its length is not
|
|
512
|
+
// `cells`), but a constant reach is checked against its `size()` like any
|
|
513
|
+
// other window.
|
|
514
|
+
if (typed !== null && ((wrapped && modulus === null) || held.reference.kind === 'element'))
|
|
510
515
|
continue;
|
|
511
516
|
const base = wrapped ? null : indexBaseOf(operation.key, context, blocks);
|
|
512
517
|
const found = byReference.get(key);
|
|
@@ -514,11 +519,21 @@ const windowsOfLoop = (blocks, body, context) => {
|
|
|
514
519
|
if (found)
|
|
515
520
|
found.sound = false;
|
|
516
521
|
else
|
|
517
|
-
byReference.set(key, { ...held, element, typed, wrapped, modulus, bases: [], operations: [], sound: false });
|
|
522
|
+
byReference.set(key, { ...held, element, typed, wrapped, modulus, bases: [], operations: [], signed: [], sound: false });
|
|
518
523
|
continue;
|
|
519
524
|
}
|
|
520
525
|
if (!found) {
|
|
521
|
-
byReference.set(key, {
|
|
526
|
+
byReference.set(key, {
|
|
527
|
+
...held,
|
|
528
|
+
element,
|
|
529
|
+
typed,
|
|
530
|
+
wrapped,
|
|
531
|
+
modulus,
|
|
532
|
+
bases: [base],
|
|
533
|
+
operations: [operation],
|
|
534
|
+
signed: signed ? [operation] : [],
|
|
535
|
+
sound: true
|
|
536
|
+
});
|
|
522
537
|
continue;
|
|
523
538
|
}
|
|
524
539
|
// Different addends each contribute a range check to the one cumulative
|
|
@@ -539,6 +554,8 @@ const windowsOfLoop = (blocks, body, context) => {
|
|
|
539
554
|
if (!found.bases.some(same))
|
|
540
555
|
found.bases.push(base);
|
|
541
556
|
found.operations.push(operation);
|
|
557
|
+
if (signed)
|
|
558
|
+
found.signed.push(operation);
|
|
542
559
|
}
|
|
543
560
|
}
|
|
544
561
|
}
|
|
@@ -569,7 +586,8 @@ const windowsOfLoop = (blocks, body, context) => {
|
|
|
569
586
|
/**
|
|
570
587
|
* Whether an index is a REMAINDER by this very array's `length`.
|
|
571
588
|
*
|
|
572
|
-
* `x % a.length` is in `
|
|
589
|
+
* `x % a.length` is in `(-a.length, a.length)` for every dividend, and the
|
|
590
|
+
* access tests the sign (`DenseAccess.signed`), so an access keyed
|
|
573
591
|
* by one needs no bound of its own -- which is the whole point, because the
|
|
574
592
|
* bound is what a per-access size read would cost. The receiver of the length
|
|
575
593
|
* read has to be the SAME value the access itself indexes: `a[x % b.length]`
|
|
@@ -604,8 +622,8 @@ const wrapsOwnLength = (key, receiver, context) => {
|
|
|
604
622
|
* The positive integer an index is taken modulo, when the divisor is a
|
|
605
623
|
* CONSTANT -- `table[i % 16]`.
|
|
606
624
|
*
|
|
607
|
-
* The same arithmetic `wrapsOwnLength` relies on: `x % n` lies in `
|
|
608
|
-
* every dividend whenever `n > 0
|
|
625
|
+
* The same arithmetic `wrapsOwnLength` relies on: `x % n` lies in `(-n, n)`
|
|
626
|
+
* for every dividend whenever `n > 0`, and the access tests its sign. What this does NOT prove is that `n` is
|
|
609
627
|
* inside the array, because a constant says nothing about a length; the
|
|
610
628
|
* preheader's `size() >= n` is what closes that, and `DenseArray.modulus`
|
|
611
629
|
* carries the number there.
|
|
@@ -616,7 +634,11 @@ const wrapsOwnLength = (key, receiver, context) => {
|
|
|
616
634
|
*/
|
|
617
635
|
const constantModulusOf = (key, context, operations, at) => {
|
|
618
636
|
const compute = remainderBehind(key, context, operations, at);
|
|
619
|
-
if (compute?.kind !== 'compute' || compute.form !== 'binary'
|
|
637
|
+
if (compute?.kind !== 'compute' || compute.form !== 'binary')
|
|
638
|
+
return null;
|
|
639
|
+
if (compute.operator === '&')
|
|
640
|
+
return constantMaskReachOf(compute.operands, context);
|
|
641
|
+
if (compute.operator !== '%')
|
|
620
642
|
return null;
|
|
621
643
|
const divisor = compute.operands[1];
|
|
622
644
|
if (!divisor)
|
|
@@ -629,6 +651,33 @@ const constantModulusOf = (key, context, operations, at) => {
|
|
|
629
651
|
return null;
|
|
630
652
|
return value;
|
|
631
653
|
};
|
|
654
|
+
/** Whether an index whose reach `constantModulusOf` read is a MASK, which is never negative. */
|
|
655
|
+
const isMaskKey = (key, context, operations, at) => {
|
|
656
|
+
const compute = remainderBehind(key, context, operations, at);
|
|
657
|
+
return compute?.kind === 'compute' && compute.form === 'binary' && compute.operator === '&';
|
|
658
|
+
};
|
|
659
|
+
/**
|
|
660
|
+
* `table[v & 255]` -- the other way every lookup table is indexed, and the
|
|
661
|
+
* one that needs no sign argument at all: `&` works on ToInt32 of both
|
|
662
|
+
* operands, so with a mask `M` in `[0, 2^31)` the result lies in `[0, M]` for
|
|
663
|
+
* EVERY left operand, NaN and negatives included. That is a window over
|
|
664
|
+
* `M + 1` elements, which is exactly what a constant modulus of `M + 1` asks
|
|
665
|
+
* the preheader to check, so it travels as one.
|
|
666
|
+
*
|
|
667
|
+
* Either operand may be the constant; a negative or fractional one bounds
|
|
668
|
+
* nothing and is not one of these.
|
|
669
|
+
*/
|
|
670
|
+
const constantMaskReachOf = (operands, context) => {
|
|
671
|
+
for (const operand of operands) {
|
|
672
|
+
const text = context.constantTexts.get(operand.value);
|
|
673
|
+
if (text === undefined)
|
|
674
|
+
continue;
|
|
675
|
+
const value = Number(text);
|
|
676
|
+
if (Number.isSafeInteger(value) && value >= 0 && value <= 0x7fffffff)
|
|
677
|
+
return value + 1;
|
|
678
|
+
}
|
|
679
|
+
return null;
|
|
680
|
+
};
|
|
632
681
|
/**
|
|
633
682
|
* Where an index's value was computed, seeing through a binding.
|
|
634
683
|
*
|
|
@@ -31,6 +31,8 @@ import type { IrBody } from './model.js';
|
|
|
31
31
|
* lattice, so a field and a local can never disagree about what an integer is.
|
|
32
32
|
*/
|
|
33
33
|
export interface IntegerStorageCensus {
|
|
34
|
+
/** The file-scope cells whose one program-wide write is a bounded integer: declared `long long`. */
|
|
35
|
+
readonly integerCells: ReadonlySet<DeclarationId>;
|
|
34
36
|
/** The slots whose C++ storage may be `long long`. */
|
|
35
37
|
readonly slots: ReadonlySet<string>;
|
|
36
38
|
/** What one body should be censused with, so its reads of those slots narrow too. */
|
|
@@ -89,6 +91,12 @@ export declare const integerParameterSlot: (owner: FunctionId | RegionId, ordina
|
|
|
89
91
|
export declare const integerResultSlot: (owner: FunctionId | RegionId) => string;
|
|
90
92
|
export interface IntegerStorageQuestion {
|
|
91
93
|
readonly bodies: readonly IrBody[];
|
|
94
|
+
/**
|
|
95
|
+
* Whether a cell may be judged by `programCellIntegersOf`: its storage is one
|
|
96
|
+
* the compiled program alone writes, and every body that names it agrees on
|
|
97
|
+
* the `long long` carrier the answer implies.
|
|
98
|
+
*/
|
|
99
|
+
readonly integerCellEligible?: (cell: DeclarationId) => boolean;
|
|
92
100
|
/** The struct a carrier IS, or `null` when this census may not name one (a host struct, a non-record). */
|
|
93
101
|
readonly structNameOf: (representation: Representation) => string | null;
|
|
94
102
|
/** Every native struct whose storage a value exposes, including class bases. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { walkRepresentation } from '../representation/model.js';
|
|
2
|
-
import { integerLinearMagnitude, narrowableIntegersOf, programCellConstantsOf, widenIntegerMagnitude } from './integers.js';
|
|
2
|
+
import { integerLinearMagnitude, narrowableIntegersOf, programCellConstantsOf, programCellIntegersOf, widenIntegerMagnitude } from './integers.js';
|
|
3
3
|
import { callableMemberSlot } from './callable-member-candidates.js';
|
|
4
4
|
import { operandsOfIrOperation, resultOfIrOperation } from './queries.js';
|
|
5
5
|
/**
|
|
@@ -249,6 +249,7 @@ const selfSumOf = (scan, slot, value) => {
|
|
|
249
249
|
};
|
|
250
250
|
const emptyFacts = { reads: new Map(), integral: new Set(), magnitudes: new Map() };
|
|
251
251
|
export const emptyIntegerStorageCensus = {
|
|
252
|
+
integerCells: new Set(),
|
|
252
253
|
slots: new Set(),
|
|
253
254
|
factsOf: () => emptyFacts,
|
|
254
255
|
entryCheckedFactsOf: () => emptyFacts
|
|
@@ -705,7 +706,12 @@ export const integerStorageCensusOf = (question) => {
|
|
|
705
706
|
// Program-wide single-literal cells ride on every facts object handed out,
|
|
706
707
|
// so a body that only READS a module constant still sees its value.
|
|
707
708
|
const cellConstants = programCellConstantsOf(question.bodies);
|
|
708
|
-
const
|
|
709
|
+
const cellIntegers = programCellIntegersOf(question.bodies, question.integerCellEligible ?? (() => false), cellConstants);
|
|
710
|
+
const withCells = (facts) => ({
|
|
711
|
+
...facts,
|
|
712
|
+
...(cellConstants.size > 0 ? { cellConstants } : {}),
|
|
713
|
+
...(cellIntegers.size > 0 ? { cellIntegers } : {})
|
|
714
|
+
});
|
|
709
715
|
// A box whose payload this census cannot name may hold ANY struct -- a
|
|
710
716
|
// member read off a boxed record is a box of a struct no conversion named --
|
|
711
717
|
// so it strikes every field slot, and every method's signature, since a
|
|
@@ -806,6 +812,7 @@ export const integerStorageCensusOf = (question) => {
|
|
|
806
812
|
// version of a body is rendered under -- which a program with no integral
|
|
807
813
|
// slot at all still asks for, since the premise is the version's own.
|
|
808
814
|
const censusFrom = (slots, settledSlots, magnitudes, facts) => ({
|
|
815
|
+
integerCells: new Set(cellIntegers.keys()),
|
|
809
816
|
slots,
|
|
810
817
|
factsOf: (owner) => withCells(facts.get(String(owner)) ?? emptyFacts),
|
|
811
818
|
entryCheckedFactsOf: (owner, ordinals) => {
|
package/dist/ir/integers.d.ts
CHANGED
|
@@ -133,6 +133,14 @@ export interface IntegerStorageFacts {
|
|
|
133
133
|
* reading the cell had none and every index derived from it was a double.
|
|
134
134
|
*/
|
|
135
135
|
readonly cellConstants?: ReadonlyMap<DeclarationId, number>;
|
|
136
|
+
/**
|
|
137
|
+
* Cells the WHOLE program writes exactly once, with a value the writing body's
|
|
138
|
+
* own census proves an integer of bounded magnitude -- `programCellIntegersOf`.
|
|
139
|
+
* The generalization of `cellConstants` from a literal to `(w >> 1) | 0`: a
|
|
140
|
+
* body that only READS the cell has no write to examine, so without this
|
|
141
|
+
* every index built from a computed module constant was a double.
|
|
142
|
+
*/
|
|
143
|
+
readonly cellIntegers?: ReadonlyMap<DeclarationId, IntegerMagnitude>;
|
|
136
144
|
}
|
|
137
145
|
/**
|
|
138
146
|
* Which cells hold one integer literal at every read, program-wide: exactly
|
|
@@ -140,6 +148,24 @@ export interface IntegerStorageFacts {
|
|
|
140
148
|
* that is a safe integer.
|
|
141
149
|
*/
|
|
142
150
|
export declare const programCellConstantsOf: (bodies: readonly IrBody[]) => ReadonlyMap<DeclarationId, number>;
|
|
151
|
+
/**
|
|
152
|
+
* Which cells hold an integer of a known bound at every read, program-wide:
|
|
153
|
+
* exactly one `binding-write` across all `bodies`, and the writing body's
|
|
154
|
+
* census proves its value integral with a BOUNDED magnitude under 2^53.
|
|
155
|
+
*
|
|
156
|
+
* Bounded only, never linear: the write may run many times (a loop, a function
|
|
157
|
+
* called repeatedly), and only a bound holds for every execution. Exactly one
|
|
158
|
+
* static write is what makes the cell's content "that expression" at every read
|
|
159
|
+
* that follows it; a read that precedes it sees the cell's initial content,
|
|
160
|
+
* which `eligible` restricts to cells whose placement is a number scalar
|
|
161
|
+
* (zero-initialized, so 0 as a `long long` or as a double).
|
|
162
|
+
*
|
|
163
|
+
* Settled from below, as a least fixed point: the first round knows no cell, so
|
|
164
|
+
* a writer that reads another such cell (`HALF_W = WIDTH >> 1`) joins only after
|
|
165
|
+
* that cell has joined. Nothing is ever assumed and later struck, so a round
|
|
166
|
+
* that stops early is merely less precise, never unsound.
|
|
167
|
+
*/
|
|
168
|
+
export declare const programCellIntegersOf: (bodies: readonly IrBody[], eligible: (cell: DeclarationId) => boolean, cellConstants: ReadonlyMap<DeclarationId, number>) => ReadonlyMap<DeclarationId, IntegerMagnitude>;
|
|
143
169
|
export type IntegerMagnitude = {
|
|
144
170
|
readonly kind: 'bounded';
|
|
145
171
|
readonly limit: number;
|
package/dist/ir/integers.js
CHANGED
|
@@ -45,6 +45,67 @@ export const programCellConstantsOf = (bodies) => {
|
|
|
45
45
|
}
|
|
46
46
|
return constants;
|
|
47
47
|
};
|
|
48
|
+
/**
|
|
49
|
+
* Which cells hold an integer of a known bound at every read, program-wide:
|
|
50
|
+
* exactly one `binding-write` across all `bodies`, and the writing body's
|
|
51
|
+
* census proves its value integral with a BOUNDED magnitude under 2^53.
|
|
52
|
+
*
|
|
53
|
+
* Bounded only, never linear: the write may run many times (a loop, a function
|
|
54
|
+
* called repeatedly), and only a bound holds for every execution. Exactly one
|
|
55
|
+
* static write is what makes the cell's content "that expression" at every read
|
|
56
|
+
* that follows it; a read that precedes it sees the cell's initial content,
|
|
57
|
+
* which `eligible` restricts to cells whose placement is a number scalar
|
|
58
|
+
* (zero-initialized, so 0 as a `long long` or as a double).
|
|
59
|
+
*
|
|
60
|
+
* Settled from below, as a least fixed point: the first round knows no cell, so
|
|
61
|
+
* a writer that reads another such cell (`HALF_W = WIDTH >> 1`) joins only after
|
|
62
|
+
* that cell has joined. Nothing is ever assumed and later struck, so a round
|
|
63
|
+
* that stops early is merely less precise, never unsound.
|
|
64
|
+
*/
|
|
65
|
+
export const programCellIntegersOf = (bodies, eligible, cellConstants) => {
|
|
66
|
+
const writeCounts = new Map();
|
|
67
|
+
for (const body of bodies)
|
|
68
|
+
for (const blockId of body.blockOrder)
|
|
69
|
+
for (const operation of body.blocks.get(blockId)?.operations ?? [])
|
|
70
|
+
if (operation.kind === 'binding-write')
|
|
71
|
+
writeCounts.set(operation.declaration, (writeCounts.get(operation.declaration) ?? 0) + 1);
|
|
72
|
+
const pending = new Map();
|
|
73
|
+
for (const body of bodies)
|
|
74
|
+
for (const blockId of body.blockOrder)
|
|
75
|
+
for (const operation of body.blocks.get(blockId)?.operations ?? []) {
|
|
76
|
+
if (operation.kind !== 'binding-write' || writeCounts.get(operation.declaration) !== 1)
|
|
77
|
+
continue;
|
|
78
|
+
if (!isNumberScalar(operation.value) || !eligible(operation.declaration))
|
|
79
|
+
continue;
|
|
80
|
+
const writes = pending.get(body) ?? [];
|
|
81
|
+
writes.push({ cell: operation.declaration, value: operation.value });
|
|
82
|
+
pending.set(body, writes);
|
|
83
|
+
}
|
|
84
|
+
const cells = new Map();
|
|
85
|
+
for (let round = 0; round < 16 && pending.size > 0; round++) {
|
|
86
|
+
let changed = false;
|
|
87
|
+
for (const [body, writes] of [...pending]) {
|
|
88
|
+
const narrowed = narrowableIntegersOf(body, { ...noStorageFacts, cellConstants, cellIntegers: cells });
|
|
89
|
+
const unresolved = writes.filter(({ cell, value }) => {
|
|
90
|
+
const magnitude = narrowed.magnitudes.get(value.value);
|
|
91
|
+
if (!narrowed.integral.has(value.value) || magnitude === undefined || magnitude.kind !== 'bounded')
|
|
92
|
+
return true;
|
|
93
|
+
if (magnitude.limit > exactIntegerLimit)
|
|
94
|
+
return true;
|
|
95
|
+
cells.set(cell, magnitude);
|
|
96
|
+
changed = true;
|
|
97
|
+
return false;
|
|
98
|
+
});
|
|
99
|
+
if (unresolved.length === 0)
|
|
100
|
+
pending.delete(body);
|
|
101
|
+
else
|
|
102
|
+
pending.set(body, unresolved);
|
|
103
|
+
}
|
|
104
|
+
if (!changed)
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
return cells;
|
|
108
|
+
};
|
|
48
109
|
const noStorageFacts = { reads: new Map(), integral: new Set(), magnitudes: new Map() };
|
|
49
110
|
/** 2^53: the last integer a double can hold with every integer below it. */
|
|
50
111
|
const exactIntegerLimit = 9007199254740992;
|
|
@@ -141,6 +202,15 @@ const product = (left, right) => {
|
|
|
141
202
|
};
|
|
142
203
|
/** 2^32, the width every ECMA-262 bitwise operator reduces to before it computes. */
|
|
143
204
|
const bitwiseWidth = 4294967296;
|
|
205
|
+
/**
|
|
206
|
+
* 2^31: the magnitude of a signed 32-bit result. `|`, `&`, `^`, `<<`, `>>` and
|
|
207
|
+
* `~` answer ToInt32 of their result, in [-2^31, 2^31); only `>>>` reaches
|
|
208
|
+
* 2^32. The difference matters for products: two `| 0` int32s multiply to
|
|
209
|
+
* under 2^62, which the 64-bit carrier holds, while two 2^32 bounds give 2^64
|
|
210
|
+
* and refuse the whole chain -- `(cell + 1) * pitch` over a runtime grid
|
|
211
|
+
* size then indexes a typed array through the double ToInt32.
|
|
212
|
+
*/
|
|
213
|
+
const int32Magnitude = 2147483648;
|
|
144
214
|
/**
|
|
145
215
|
* The largest `length` (2^40) and `byteLength`/`byteOffset` (2^43) a typed
|
|
146
216
|
* array can have: the runtime's stated implementation limit
|
|
@@ -201,10 +271,26 @@ export const narrowableIntegersOf = (body, storage = noStorageFacts) => {
|
|
|
201
271
|
// was every index derived from it -- soft-float on every pixel of Bloom's
|
|
202
272
|
// upscaler.
|
|
203
273
|
const minMaxResults = new Map();
|
|
274
|
+
// `Math.clz32` counts the leading zeros of ToUint32 of anything, NaN
|
|
275
|
+
// included: always an integer in [0, 32]. `Math.abs` of an integer is an
|
|
276
|
+
// integer no larger than it. Without these a vorticity confinement's
|
|
277
|
+
// `Math.abs(gx)` and `Math.clz32(length)` were doubles, and with them every
|
|
278
|
+
// shift and product downstream -- soft-float per cell on the S31.
|
|
279
|
+
const clz32Results = new Set();
|
|
280
|
+
const absResults = new Map();
|
|
204
281
|
for (const [call, intrinsic] of numericIntrinsicsOf(body).calls) {
|
|
205
|
-
if (
|
|
282
|
+
if (!call.result)
|
|
283
|
+
continue;
|
|
284
|
+
if (intrinsic === 'imul')
|
|
206
285
|
imulResults.add(call.result.id);
|
|
207
|
-
else if (
|
|
286
|
+
else if (intrinsic === 'clz32')
|
|
287
|
+
clz32Results.add(call.result.id);
|
|
288
|
+
else if (intrinsic === 'abs') {
|
|
289
|
+
const argument = call.arguments[0];
|
|
290
|
+
if (argument)
|
|
291
|
+
absResults.set(call.result.id, argument);
|
|
292
|
+
}
|
|
293
|
+
else
|
|
208
294
|
minMaxResults.set(call.result.id, call.arguments);
|
|
209
295
|
}
|
|
210
296
|
const definitions = new Map();
|
|
@@ -332,8 +418,11 @@ export const narrowableIntegersOf = (body, storage = noStorageFacts) => {
|
|
|
332
418
|
if (scalar)
|
|
333
419
|
integralCells.add(cell);
|
|
334
420
|
const producesInteger = (value) => {
|
|
335
|
-
if (imulResults.has(value))
|
|
421
|
+
if (imulResults.has(value) || clz32Results.has(value))
|
|
336
422
|
return true;
|
|
423
|
+
const absolute = absResults.get(value);
|
|
424
|
+
if (absolute !== undefined)
|
|
425
|
+
return integral.has(absolute.value);
|
|
337
426
|
const chosen = minMaxResults.get(value);
|
|
338
427
|
if (chosen !== undefined)
|
|
339
428
|
return chosen.every((argument) => integral.has(argument.value));
|
|
@@ -511,7 +600,9 @@ export const narrowableIntegersOf = (body, storage = noStorageFacts) => {
|
|
|
511
600
|
}
|
|
512
601
|
openCells.add(cell);
|
|
513
602
|
const written = cellWrites.get(cell) ?? [];
|
|
514
|
-
|
|
603
|
+
// A cell this body never writes is whatever the rest of the program wrote
|
|
604
|
+
// into it: nothing, unless the program-wide census proved its one write.
|
|
605
|
+
let seeds = written.length > 0 ? { kind: 'bounded', limit: 0 } : (storage.cellIntegers?.get(cell) ?? null);
|
|
515
606
|
let step = null;
|
|
516
607
|
const stepBlocks = [];
|
|
517
608
|
for (const write of written) {
|
|
@@ -552,6 +643,11 @@ export const narrowableIntegersOf = (body, storage = noStorageFacts) => {
|
|
|
552
643
|
const computeMagnitude = (operand) => {
|
|
553
644
|
if (imulResults.has(operand.value))
|
|
554
645
|
return boundedBy(2 ** 31);
|
|
646
|
+
if (clz32Results.has(operand.value))
|
|
647
|
+
return boundedBy(32);
|
|
648
|
+
const absolute = absResults.get(operand.value);
|
|
649
|
+
if (absolute !== undefined)
|
|
650
|
+
return magnitudeOfValue(absolute);
|
|
555
651
|
const chosen = minMaxResults.get(operand.value);
|
|
556
652
|
if (chosen !== undefined) {
|
|
557
653
|
let answer = { kind: 'bounded', limit: 0 };
|
|
@@ -606,7 +702,7 @@ export const narrowableIntegersOf = (body, storage = noStorageFacts) => {
|
|
|
606
702
|
// `-x` and `+x` keep the magnitude; `~x` is a 32-bit result like every
|
|
607
703
|
// other bitwise operator.
|
|
608
704
|
if (compute.operator === '~')
|
|
609
|
-
return boundedBy(
|
|
705
|
+
return boundedBy(int32Magnitude);
|
|
610
706
|
if (compute.operator !== '-' && compute.operator !== '+')
|
|
611
707
|
return null;
|
|
612
708
|
return magnitudeOfValue(left);
|
|
@@ -618,12 +714,13 @@ export const narrowableIntegersOf = (body, storage = noStorageFacts) => {
|
|
|
618
714
|
// A mask by a non-negative constant bounds the result at the mask; any
|
|
619
715
|
// other AND is still a 32-bit quantity.
|
|
620
716
|
const mask = constants.get(right.value) ?? constants.get(left.value);
|
|
621
|
-
return mask !== undefined && mask >= 0 ? boundedBy(mask) : boundedBy(
|
|
717
|
+
return mask !== undefined && mask >= 0 ? boundedBy(mask) : boundedBy(int32Magnitude);
|
|
622
718
|
}
|
|
623
719
|
case '|':
|
|
624
720
|
case '^':
|
|
625
721
|
case '<<':
|
|
626
722
|
case '>>':
|
|
723
|
+
return boundedBy(int32Magnitude);
|
|
627
724
|
case '>>>':
|
|
628
725
|
return boundedBy(bitwiseWidth);
|
|
629
726
|
case '+':
|
|
@@ -671,6 +768,12 @@ export const narrowableIntegersOf = (body, storage = noStorageFacts) => {
|
|
|
671
768
|
if (magnitudeOfCell(cell) !== null)
|
|
672
769
|
bindings.add(cell);
|
|
673
770
|
}
|
|
771
|
+
// A cell stored as `long long` program-wide (its one write proved integral)
|
|
772
|
+
// is integer storage in every body that reads it, whether or not it writes.
|
|
773
|
+
for (const cell of readsCell.values()) {
|
|
774
|
+
if (storage.cellIntegers?.has(cell) === true && magnitudeOfCell(cell) !== null)
|
|
775
|
+
bindings.add(cell);
|
|
776
|
+
}
|
|
674
777
|
// Only the values that HAVE a magnitude: `valueMagnitudes` records a refusal
|
|
675
778
|
// as `null`, and a consumer reading one back would take "refused" for
|
|
676
779
|
// "unknown" and re-derive it.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { IrValueId } from '../identity/ids.js';
|
|
2
2
|
import type { CallOperation, IrBody } from './model.js';
|
|
3
|
-
export type NumericIntrinsic = 'imul' | 'min' | 'max';
|
|
3
|
+
export type NumericIntrinsic = 'imul' | 'min' | 'max' | 'abs' | 'clz32';
|
|
4
4
|
/**
|
|
5
5
|
* Language builtin calls authenticated by their native protocol, never by a
|
|
6
6
|
* source identifier spelling. A user object named Math has no such protocol.
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { allOperationsOf } from './model.js';
|
|
2
2
|
import { operandsOfIrOperation } from './queries.js';
|
|
3
|
-
|
|
3
|
+
/** How many arguments each intrinsic is recognized at; other arities keep the general callable path. */
|
|
4
|
+
const intrinsicArity = new Map([
|
|
5
|
+
['imul', 2],
|
|
6
|
+
['min', 2],
|
|
7
|
+
['max', 2],
|
|
8
|
+
['abs', 1],
|
|
9
|
+
['clz32', 1]
|
|
10
|
+
]);
|
|
4
11
|
/**
|
|
5
12
|
* Language builtin calls authenticated by their native protocol, never by a
|
|
6
13
|
* source identifier spelling. A user object named Math has no such protocol.
|
|
@@ -26,7 +33,7 @@ export const numericIntrinsicsOf = (body) => {
|
|
|
26
33
|
if (receiver.kind === 'native-handle' &&
|
|
27
34
|
receiver.native === null &&
|
|
28
35
|
receiver.protocol === 'Math' &&
|
|
29
|
-
|
|
36
|
+
intrinsicArity.has(keys.get(operation.key.value) ?? '')) {
|
|
30
37
|
callees.set(operation.result.id, keys.get(operation.key.value));
|
|
31
38
|
}
|
|
32
39
|
}
|
|
@@ -37,7 +44,7 @@ export const numericIntrinsicsOf = (body) => {
|
|
|
37
44
|
continue;
|
|
38
45
|
const intrinsic = callees.get(operation.callee.value);
|
|
39
46
|
// Other arities and coercing ABIs retain the general callable path.
|
|
40
|
-
if (intrinsic === undefined || operation.arguments.length !==
|
|
47
|
+
if (intrinsic === undefined || operation.arguments.length !== intrinsicArity.get(intrinsic))
|
|
41
48
|
continue;
|
|
42
49
|
if (!operation.arguments.every((argument) => argument.representation.kind === 'scalar' && argument.representation.domain === 'number'))
|
|
43
50
|
continue;
|
|
@@ -1209,8 +1209,31 @@ unwrittenMembers = new Map()) => {
|
|
|
1209
1209
|
return null;
|
|
1210
1210
|
return kept.length === 1 ? derive(kept[0]) : deriveUnion({ kind: 'union', members: kept });
|
|
1211
1211
|
};
|
|
1212
|
+
/**
|
|
1213
|
+
* A compiled class instance beside a standard TYPED ARRAY: no value is both.
|
|
1214
|
+
*
|
|
1215
|
+
* `body instanceof Uint8Array` over a declared `string | ReadableStream |
|
|
1216
|
+
* null` (`@hono/node-server`'s `responseViaCache`, listener.ts:186) narrows
|
|
1217
|
+
* the class arm by intersecting rather than discarding it, so the taken
|
|
1218
|
+
* branch reads `ReadableStream & Uint8Array`. A class instance's carrier is
|
|
1219
|
+
* its own struct, and a class-instance shape can name a native `Map`/`Set`,
|
|
1220
|
+
* `Error` or `Promise` base but never typed-array storage, so the instance
|
|
1221
|
+
* cannot be the native view the other member requires. The branch is dead;
|
|
1222
|
+
* left unstated, the nominal-class branch below reads the typed array as
|
|
1223
|
+
* plain structure and answers the class, and the branch then owes a
|
|
1224
|
+
* conversion from that class into whatever the typed array is handed to.
|
|
1225
|
+
*/
|
|
1226
|
+
const isUninhabitedClassTypedArrayIntersection = (substantive) => {
|
|
1227
|
+
const typedArray = substantive.some((member) => {
|
|
1228
|
+
const memberShape = shapeOf(member);
|
|
1229
|
+
return memberShape?.kind === 'declared' && elements.forDeclaration(memberShape.declaration) !== null;
|
|
1230
|
+
});
|
|
1231
|
+
return typedArray && substantive.some((member) => nominalClassDeclarationOf(member) !== null);
|
|
1232
|
+
};
|
|
1212
1233
|
/** ONE answer to "can no value have this intersection's type", for the carrier and the reachability question alike. */
|
|
1213
|
-
const isUninhabitedIntersection = (substantive) => isUninhabitedNominalIntersection(substantive) ||
|
|
1234
|
+
const isUninhabitedIntersection = (substantive) => isUninhabitedNominalIntersection(substantive) ||
|
|
1235
|
+
isUninhabitedPrimitiveIntersection(substantive) ||
|
|
1236
|
+
isUninhabitedClassTypedArrayIntersection(substantive);
|
|
1214
1237
|
const intersectionMemberKind = (member) => intersectionMemberKindOf(shapeOf, member);
|
|
1215
1238
|
const isPrimitiveValueMember = (member) => isPrimitiveValueShape(shapeOf, member);
|
|
1216
1239
|
const flattenedIntersectionMembers = createIntersectionFlattener(shapeOf, (declaration) => Boolean(binding.forDeclaration(declaration)?.native));
|
|
@@ -1439,6 +1462,12 @@ unwrittenMembers = new Map()) => {
|
|
|
1439
1462
|
// object members need no carrier of their own for this decision, so leave
|
|
1440
1463
|
// them unexpanded. Other members still derive normally, preserving the
|
|
1441
1464
|
// refusal for incompatible physical carriers.
|
|
1465
|
+
//
|
|
1466
|
+
// Asked before either the typed-array or the nominal-class reduction:
|
|
1467
|
+
// each would otherwise pick one of the two members of a pair that has no
|
|
1468
|
+
// inhabitant at all (`isUninhabitedClassTypedArrayIntersection`).
|
|
1469
|
+
if (isUninhabitedClassTypedArrayIntersection(substantive))
|
|
1470
|
+
return { kind: 'void' };
|
|
1442
1471
|
const intersectionCarriers = substantive.map((member) => {
|
|
1443
1472
|
const memberShape = shapeOf(member);
|
|
1444
1473
|
if (memberShape?.kind === 'declared') {
|
|
@@ -15,6 +15,7 @@ import { intrinsicPropertyCallOf } from './normalize/intrinsic-property-call.js'
|
|
|
15
15
|
import { createStructuralMapper } from './normalize/structural.js';
|
|
16
16
|
import { createSloppyAbsenceCensus, noSloppyAbsence } from './normalize/sloppy-absence.js';
|
|
17
17
|
import { censusRecordStandInArms } from './normalize/record-stand-in-arms.js';
|
|
18
|
+
import { censusAssertedArgumentArms } from './normalize/asserted-argument-arms.js';
|
|
18
19
|
import { censusSuppressedWriteArms } from './normalize/suppressed-write-arms.js';
|
|
19
20
|
import { withOverrideFieldArms } from './normalize/override-field-arms.js';
|
|
20
21
|
import { censusDeclaredMembers } from './normalize/structural-declarations.js';
|
|
@@ -725,7 +726,10 @@ export const runFrontend = (input) => {
|
|
|
725
726
|
// keeps its own arm -- see `suppressed-write-arms.ts`.
|
|
726
727
|
// A field a subclass redeclares as another record keeps each declaration's
|
|
727
728
|
// record as an arm of its one slot -- see `override-field-arms.ts`.
|
|
728
|
-
withOverrideFieldArms(compiled.checker, compiled.sourceFiles, censusSuppressedWriteArms(compiled.checker, compiled.sourceFiles, reachable, valueFlow))
|
|
729
|
+
withOverrideFieldArms(compiled.checker, compiled.sourceFiles, censusSuppressedWriteArms(compiled.checker, compiled.sourceFiles, reachable, valueFlow)),
|
|
730
|
+
// A record an argument asserts through `unknown` into a union keeps its
|
|
731
|
+
// own arm of that union -- see `asserted-argument-arms.ts`.
|
|
732
|
+
censusAssertedArgumentArms(compiled.checker, compiled.sourceFiles, reachable));
|
|
729
733
|
// The frontend's evidence-policy tables: built from the same
|
|
730
734
|
// `identities`/`valueFlow`/`reachable` the fixpoint above already settled,
|
|
731
735
|
// right after `types` exists so the agreement instrument can ask the
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { type ProgramReachability } from './reachability.js';
|
|
3
|
+
/**
|
|
4
|
+
* A record handed to a union-typed parameter through an explicit
|
|
5
|
+
* `value as unknown as T` assertion, kept as one more native arm of that union.
|
|
6
|
+
*
|
|
7
|
+
* `@hono/node-server`'s websocket upgrade (websocket.ts) builds
|
|
8
|
+
*
|
|
9
|
+
* const env: UpgradeBindings = { incoming: request, outgoing: undefined, wss, [WAIT_FOR_WEBSOCKET_SYMBOL]: waitForWebSocket }
|
|
10
|
+
* await fetchCallback(createUpgradeRequest(request), env as unknown as Parameters<FetchCallback>[1])
|
|
11
|
+
*
|
|
12
|
+
* where the parameter is `HttpBindings | Http2Bindings`. The assertion emits
|
|
13
|
+
* nothing in JavaScript: the callee receives `env` itself, and the program
|
|
14
|
+
* depends on that identity -- hono's helper reads `c.env as UpgradeBindings`,
|
|
15
|
+
* writes `env[CONNECTION_SYMBOL_KEY]`, and the upgrade handler reads it back
|
|
16
|
+
* off the very object it built. A converting copy would lose that write, and
|
|
17
|
+
* a box would be a dynamic carrier for a statically typed record.
|
|
18
|
+
*
|
|
19
|
+
* So the union carries the asserted record's own carrier beside its declared
|
|
20
|
+
* arms. Keyed by the union TYPE, as `record-stand-in-arms.ts` keys its arms,
|
|
21
|
+
* because the parameter here belongs to a callable TYPE (`FetchCallback`):
|
|
22
|
+
* every function value stored into that slot, and the call through it, must
|
|
23
|
+
* agree on one carrier, and the union type is the one thing they share.
|
|
24
|
+
* Adding an arm is a superset -- every value already admitted converts as
|
|
25
|
+
* before -- and a read of a member the record lacks answers through the
|
|
26
|
+
* record's own arm or refuses, never reinterprets one layout as another.
|
|
27
|
+
*
|
|
28
|
+
* Sound only where no narrowing can route the extra arm somewhere the checker
|
|
29
|
+
* says no value goes: any test on a value of the union that can tell its arms
|
|
30
|
+
* apart -- `instanceof`, `in`, `typeof`, a discriminant member, or a member the
|
|
31
|
+
* record lacks -- withdraws the arm, and the certification refusal stands.
|
|
32
|
+
*/
|
|
33
|
+
export interface AssertedArgumentArmCensus {
|
|
34
|
+
/** The asserted records a union type carries beside its declared arms, or `null`. */
|
|
35
|
+
readonly armsOf: (union: ts.Type) => readonly ts.Type[] | null;
|
|
36
|
+
}
|
|
37
|
+
export declare const emptyAssertedArgumentArmCensus: AssertedArgumentArmCensus;
|
|
38
|
+
export declare const censusAssertedArgumentArms: (checker: ts.TypeChecker, files: readonly ts.SourceFile[], reachable: ProgramReachability) => AssertedArgumentArmCensus;
|