@mark1russell7/vex-compiler 0.17.2 → 0.19.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.
- package/README.md +35 -3
- package/build/facts-3ZtCfd0v.d.ts +37 -0
- package/build/facts-3ZtCfd0v.d.ts.map +1 -0
- package/build/index.d.ts +163 -97
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1168 -4234
- package/build/index.js.map +1 -1
- package/build/memo-J-Km2qc0.js +577 -0
- package/build/memo-J-Km2qc0.js.map +1 -0
- package/build/module-entry.d.ts +105 -0
- package/build/module-entry.d.ts.map +1 -0
- package/build/module-entry.js +7005 -0
- package/build/module-entry.js.map +1 -0
- package/package.json +17 -6
- package/src/facts.ts +52 -41
- package/src/fn-text.ts +58 -0
- package/src/index.ts +2 -3
- package/src/js.ts +59 -33
- package/src/loop.ts +98 -10
- package/src/lower.ts +610 -128
- package/src/memo.ts +215 -94
- package/src/module-entry.ts +7 -0
- package/src/module-tidy.ts +502 -0
- package/src/module.ts +3166 -714
- package/src/runtime.ts +326 -36
- package/src/scope-key.ts +283 -0
- package/src/sheet-order.ts +13 -99
- package/src/tiers.ts +27 -12
- package/src/unshared.ts +125 -0
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import { MAX_NESTING, addrExprs, children, isListOp, isOpName, liftsAt, listItemKind, listOf, nestingOf } from "@mark1russell7/vex";
|
|
2
|
+
// src/facts.ts
|
|
3
|
+
/**
|
|
4
|
+
* The facts of the options that the printer uses (docs/COMPILER.md §13.2). For each op site, they give the domain
|
|
5
|
+
* whose op the interpreter takes first, and the kinds and the lifts of its parameters. The facts come from the op
|
|
6
|
+
* tables, so the same options always give the same facts, and the same source.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* The most bodies of functions that tier 2 puts at the calls of one program. A function that calls another function
|
|
10
|
+
* twice doubles its body, so a chain of such functions grows fast. A module prints each function once.
|
|
11
|
+
*/
|
|
12
|
+
const MAX_CALLS = 1e4;
|
|
13
|
+
const FUSED_OPS = /* @__PURE__ */ new Set([
|
|
14
|
+
"count",
|
|
15
|
+
"sum",
|
|
16
|
+
"min",
|
|
17
|
+
"max",
|
|
18
|
+
"mean",
|
|
19
|
+
"any",
|
|
20
|
+
"all",
|
|
21
|
+
"none",
|
|
22
|
+
"values",
|
|
23
|
+
"keys",
|
|
24
|
+
"reduce",
|
|
25
|
+
"first",
|
|
26
|
+
"errors"
|
|
27
|
+
]);
|
|
28
|
+
/** This function tells if a list op over an `each` is one loop in tier 2. The lowering gives the same answer to the memo. */
|
|
29
|
+
const isFusedOp = (op) => FUSED_OPS.has(op);
|
|
30
|
+
/** This function gives the kind of the values of a domain. */
|
|
31
|
+
const kindOfDomain = (d) => d.primitive ?? `domain:${d.name}`;
|
|
32
|
+
/** This function gives a kind if it is a primitive type, else `undefined`. A kind of a list is not a primitive type. */
|
|
33
|
+
const primitiveOf = (k) => k === "number" || k === "string" || k === "boolean" ? k : void 0;
|
|
34
|
+
const NO_SPEC = Object.freeze({});
|
|
35
|
+
/** This function gives the op of a name in a domain, or `undefined` for a domain without the op. */
|
|
36
|
+
function entryOf(domain, name) {
|
|
37
|
+
if (!isOpName(name)) return void 0;
|
|
38
|
+
const declared = Object.hasOwn(domain.ops, name) ? domain.ops[name] : void 0;
|
|
39
|
+
if (declared === void 0) return domain.methods === "all" ? {
|
|
40
|
+
domain,
|
|
41
|
+
spec: NO_SPEC,
|
|
42
|
+
fn: void 0
|
|
43
|
+
} : void 0;
|
|
44
|
+
return {
|
|
45
|
+
domain,
|
|
46
|
+
spec: declared,
|
|
47
|
+
fn: declared.fn
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const FREE$1 = Object.freeze({ t: "free" });
|
|
51
|
+
const STATIC_FREE = Object.freeze({ t: "free" });
|
|
52
|
+
/** The positions of the lists of a site without lists. */
|
|
53
|
+
const NO_LISTS = /* @__PURE__ */ new Set();
|
|
54
|
+
/** This function gives the kind of the result of an op of a domain, if the op declares it. */
|
|
55
|
+
function returnsOf(domain, kind) {
|
|
56
|
+
if (kind === "domain") return kindOfDomain(domain);
|
|
57
|
+
return kind === "any" || listItemKind(kind) !== void 0 ? void 0 : kind;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* This function tells if an argument of a kind fits a parameter, or `undefined` if the kind of the argument is unknown.
|
|
61
|
+
* A list fits only `any`, and the test `is` of a domain without a primitive type can accept it (COMPILE.MODULE.LIST.ARG).
|
|
62
|
+
*/
|
|
63
|
+
function fits(domain, param, arg, list) {
|
|
64
|
+
if (param === void 0 || param === "any") return true;
|
|
65
|
+
if (list) return param === "domain" && domain.primitive === void 0 ? void 0 : false;
|
|
66
|
+
if (arg === void 0) return void 0;
|
|
67
|
+
return arg === (param === "domain" ? kindOfDomain(domain) : param);
|
|
68
|
+
}
|
|
69
|
+
/** This function tells if two domains declare different field names, so they have no common value (KIND.FIELDS). */
|
|
70
|
+
function disjoint(a, b) {
|
|
71
|
+
const x = a.fields;
|
|
72
|
+
const y = b?.fields;
|
|
73
|
+
return x !== void 0 && y !== void 0 && (x.length !== y.length || x.some((name) => !y.includes(name)));
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* This function tells if a domain accepts a value of a kind. A domain with a primitive type accepts its type, and a
|
|
77
|
+
* domain without one accepts the values of its kind. It does not accept a value of a domain with other fields. For
|
|
78
|
+
* another value, it gives `undefined`: its `is` can accept it.
|
|
79
|
+
*/
|
|
80
|
+
function accepts(domains, d, kind) {
|
|
81
|
+
if (d.primitive !== void 0) return d.primitive === kind;
|
|
82
|
+
if (kind === kindOfDomain(d)) return true;
|
|
83
|
+
return disjoint(d, domains.find((x) => kindOfDomain(x) === kind)) ? false : void 0;
|
|
84
|
+
}
|
|
85
|
+
/** This function gives the facts of a set of options. */
|
|
86
|
+
function factsOf(options) {
|
|
87
|
+
const domains = options.domains ?? [];
|
|
88
|
+
const fns = options.fns;
|
|
89
|
+
const last = domains.findLastIndex((d) => d.valid !== void 0);
|
|
90
|
+
return {
|
|
91
|
+
checks: domains.slice(0, last + 1).map((d) => d.valid !== void 0),
|
|
92
|
+
staticSite(name, kinds, lists = NO_LISTS, lift = false) {
|
|
93
|
+
const [self, ...args] = kinds;
|
|
94
|
+
if (self === void 0) return void 0;
|
|
95
|
+
for (const [i, d] of domains.entries()) {
|
|
96
|
+
const takes = accepts(domains, d, self);
|
|
97
|
+
if (takes === void 0) return void 0;
|
|
98
|
+
const e = takes ? entryOf(d, name) : void 0;
|
|
99
|
+
if (e?.fn === void 0) {
|
|
100
|
+
if (takes && d.primitive === void 0) return void 0;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const lifted = d.fromScalar === void 0 ? [] : args.flatMap((a, j) => (a === void 0 || a === "number") && !lists.has(j + 1) && liftsAt(e.spec.liftScalar, j) ? [j] : []);
|
|
104
|
+
if (lifted.some((j) => !lift || args[j] === void 0)) return void 0;
|
|
105
|
+
const ok = args.map((a, j) => fits(d, e.spec.params?.[j], lifted.includes(j) ? kindOfDomain(d) : a, lists.has(j + 1)));
|
|
106
|
+
if (ok.includes(void 0)) return void 0;
|
|
107
|
+
const item = listItemKind(e.spec.returns);
|
|
108
|
+
return {
|
|
109
|
+
t: "fn",
|
|
110
|
+
domain: i,
|
|
111
|
+
bad: ok.includes(false),
|
|
112
|
+
returns: returnsOf(d, e.spec.returns),
|
|
113
|
+
...item === void 0 ? {} : { item },
|
|
114
|
+
...lifted.length === 0 ? {} : { lifted }
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return fns !== void 0 && Object.hasOwn(fns, name) ? STATIC_FREE : void 0;
|
|
118
|
+
},
|
|
119
|
+
checkOf(kind) {
|
|
120
|
+
for (const [i, d] of domains.entries()) {
|
|
121
|
+
const takes = accepts(domains, d, kind);
|
|
122
|
+
if (takes === void 0) return void 0;
|
|
123
|
+
if (takes) return i;
|
|
124
|
+
}
|
|
125
|
+
return -1;
|
|
126
|
+
},
|
|
127
|
+
own(name) {
|
|
128
|
+
return domains.every((d) => entryOf(d, name) === void 0) && !(fns !== void 0 && Object.hasOwn(fns, name));
|
|
129
|
+
},
|
|
130
|
+
givesList(name) {
|
|
131
|
+
return domains.some((d) => Object.hasOwn(d.ops, name) && listItemKind(d.ops[name].returns) !== void 0);
|
|
132
|
+
},
|
|
133
|
+
identities(name) {
|
|
134
|
+
return domains.filter((d) => Object.hasOwn(d.ops, name) && d.ops[name].identity !== void 0).length;
|
|
135
|
+
},
|
|
136
|
+
site(name, arity) {
|
|
137
|
+
if (arity === 0 || isListOp(name)) return void 0;
|
|
138
|
+
const index = domains.findIndex((d) => entryOf(d, name) !== void 0);
|
|
139
|
+
if (index < 0) return fns !== void 0 && Object.hasOwn(fns, name) ? FREE$1 : void 0;
|
|
140
|
+
const e = entryOf(domains[index], name);
|
|
141
|
+
return {
|
|
142
|
+
t: "op",
|
|
143
|
+
domain: index,
|
|
144
|
+
method: e.fn === void 0,
|
|
145
|
+
params: e.spec.params,
|
|
146
|
+
lift: e.domain.fromScalar === void 0 ? void 0 : e.spec.liftScalar
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// src/unshared.ts
|
|
152
|
+
/**
|
|
153
|
+
* A program can use one object at two places, and a name can have two meanings at those places. The printer and the
|
|
154
|
+
* lowering hold facts of a place by the object of its node. Thus this file first gives each place an object of its own.
|
|
155
|
+
* A program from JSON is a tree, so it stays the same objects. The root and `./module` of this package do not export this file.
|
|
156
|
+
*/
|
|
157
|
+
/** The original of each copy that `unsharedAll` made. */
|
|
158
|
+
const ORIGINALS = /* @__PURE__ */ new WeakMap();
|
|
159
|
+
/**
|
|
160
|
+
* The most places that a program can have for a compile (COMPILE.PLACES). A place is a node at one position of the tree
|
|
161
|
+
* of the program, so a shared object counts once for each place. The compile takes time at each place.
|
|
162
|
+
*
|
|
163
|
+
* The rule: the largest program of each doubling shape under this limit stays under 1,024 MB, half of a heap of 2,048 MB.
|
|
164
|
+
* At 4 times the places, at least one shape goes over that line. The workflow `places-gate.yml` holds this rule.
|
|
165
|
+
*
|
|
166
|
+
* The worst of 5 fresh processes in tier 2 at the limit is 560 MB (plain), 566 MB (constant) and 489 MB (loop).
|
|
167
|
+
* At 4 times the places it is 1,661 MB, 1,699 MB and 1,469 MB. The limit is 15 times the largest module of `ireko`.
|
|
168
|
+
* `docs/COMPILER.md` has the measures.
|
|
169
|
+
*/
|
|
170
|
+
const MAX_PLACES = 32768;
|
|
171
|
+
/** This function gives the parts of a node. A node that is not valid has no parts here, and the check of the shape gives its reason. */
|
|
172
|
+
function partsOf(x) {
|
|
173
|
+
try {
|
|
174
|
+
return children(x).filter((part) => typeof part === "object" && part !== null);
|
|
175
|
+
} catch {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/** This function gives the count of the objects of the expressions when each object has one place. Otherwise it gives `undefined`. */
|
|
180
|
+
function treeSize(exprs) {
|
|
181
|
+
const seen = /* @__PURE__ */ new Set();
|
|
182
|
+
const todo = [...exprs];
|
|
183
|
+
for (let x = todo.pop(); x !== void 0; x = todo.pop()) {
|
|
184
|
+
if (seen.has(x)) return void 0;
|
|
185
|
+
seen.add(x);
|
|
186
|
+
todo.push(...partsOf(x));
|
|
187
|
+
}
|
|
188
|
+
return seen.size;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* This function gives the number of places of expressions with a shared object, with a walk that visits each object once.
|
|
192
|
+
* So it takes the time of the objects, not of the places. An object that holds itself has no end, and its count is infinite.
|
|
193
|
+
*/
|
|
194
|
+
function sharedPlaces(exprs) {
|
|
195
|
+
const counts = /* @__PURE__ */ new Map();
|
|
196
|
+
const open = /* @__PURE__ */ new Map();
|
|
197
|
+
const todo = [...exprs];
|
|
198
|
+
for (let x = todo.at(-1); x !== void 0; x = todo.at(-1)) {
|
|
199
|
+
const parts = open.get(x);
|
|
200
|
+
if (counts.has(x)) todo.pop();
|
|
201
|
+
else if (parts !== void 0) {
|
|
202
|
+
todo.pop();
|
|
203
|
+
counts.set(x, parts.reduce((sum, part) => sum + (counts.get(part) ?? 1e400), 1));
|
|
204
|
+
} else {
|
|
205
|
+
const own = partsOf(x);
|
|
206
|
+
open.set(x, own);
|
|
207
|
+
todo.push(...own.filter((part) => !counts.has(part)));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return exprs.reduce((sum, e) => sum + counts.get(e), 0);
|
|
211
|
+
}
|
|
212
|
+
/** This function gives the result of one walk of the expressions. So a program with no shared object needs no second walk. */
|
|
213
|
+
function placesRead(exprs) {
|
|
214
|
+
const size = treeSize(exprs);
|
|
215
|
+
const places = size ?? sharedPlaces(exprs);
|
|
216
|
+
return {
|
|
217
|
+
error: places > 32768 ? `COMPILE.PLACES: the program has ${String(places)} places, and the limit is ${String(MAX_PLACES)}` : void 0,
|
|
218
|
+
shares: size === void 0
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/** This function gives a copy of a part of an expression, with a new object for each place. A value of a literal and the data of an extension are not nodes, so they stay. */
|
|
222
|
+
function copied(x) {
|
|
223
|
+
if (Array.isArray(x)) return x.map(copied);
|
|
224
|
+
if (typeof x !== "object" || x === null) return x;
|
|
225
|
+
const out = { ...x };
|
|
226
|
+
const tag = out["tag"];
|
|
227
|
+
if (tag === "lit" || tag === "ext") return noted(out, x);
|
|
228
|
+
for (const name of Object.keys(out)) {
|
|
229
|
+
const part = out[name];
|
|
230
|
+
if (typeof part === "object") out[name] = copied(part);
|
|
231
|
+
}
|
|
232
|
+
return typeof tag === "string" ? noted(out, x) : out;
|
|
233
|
+
}
|
|
234
|
+
/** This function notes the original of a copy of a node, and gives the copy. */
|
|
235
|
+
function noted(copy, original) {
|
|
236
|
+
ORIGINALS.set(copy, original);
|
|
237
|
+
return copy;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* This function gives the expressions with one object at each place. Expressions without a shared object stay the same
|
|
241
|
+
* objects. So does an expression that is nested too deep: the printer and the lowering refuse it. A caller with the
|
|
242
|
+
* result of `placesRead` gives its `shares`, and this function makes no walk.
|
|
243
|
+
*/
|
|
244
|
+
function unsharedAll(exprs, shares = treeSize(exprs) === void 0) {
|
|
245
|
+
return !shares || exprs.some((e) => nestingOf(e) > MAX_NESTING) ? exprs : exprs.map(copied);
|
|
246
|
+
}
|
|
247
|
+
/** This function gives the expression with one object at each place, as `unsharedAll` does. */
|
|
248
|
+
const unshared = (e, shares) => unsharedAll([e], shares)[0];
|
|
249
|
+
/** This function gives the original of a copy, and an expression that is not a copy gives itself. */
|
|
250
|
+
const originalOf = (e) => ORIGINALS.get(e) ?? e;
|
|
251
|
+
// src/memo.ts
|
|
252
|
+
/**
|
|
253
|
+
* The memo of a program (docs/COMPILER.md §7.2 and §7.3). The value of some nodes depends only on the space and on a
|
|
254
|
+
* few values that the node reads from outside: its keys. A program keeps the value of such a node for each space and
|
|
255
|
+
* each value of its keys. Thus a run at each key of a space evaluates the node once for each value of its keys.
|
|
256
|
+
*
|
|
257
|
+
* - `AVERAGE(all.Score)` has no keys, so it evaluates once for each space.
|
|
258
|
+
* - "The largest width in the column of the origin" has one key, the column of the origin. It evaluates once for each
|
|
259
|
+
* column, not once for each origin.
|
|
260
|
+
*
|
|
261
|
+
* A key reads the origin or a name from outside the node, and nothing that the node changes. Inside the node, the
|
|
262
|
+
* value of the key replaces the key. Thus the value of the node depends only on the space and the values of the keys.
|
|
263
|
+
*/
|
|
264
|
+
/** A read of the focus of the expression, outside its own loops. */
|
|
265
|
+
const FOCUS = 1;
|
|
266
|
+
/** A move to the origin. */
|
|
267
|
+
const ORIGIN = 2;
|
|
268
|
+
/** A name that the node does not bind: a variable of the run, or a binding outside the node. */
|
|
269
|
+
const FREE = 4;
|
|
270
|
+
/** A name that the node binds, outside the expression. */
|
|
271
|
+
const INTERNAL = 8;
|
|
272
|
+
/** An extension or an axis table. An extension can read each part of the evaluation, and a table is another space. */
|
|
273
|
+
const OPAQUE = 16;
|
|
274
|
+
/** A move to the origin or a free name inside a loop of the expression. Such a loop is a part of a node, not a key. */
|
|
275
|
+
const DEEP = 32;
|
|
276
|
+
/** This function gives the axis inside the `where` axes of an axis, and the tests of the `where` axes. */
|
|
277
|
+
function parts(a) {
|
|
278
|
+
const tests = [];
|
|
279
|
+
let x = a;
|
|
280
|
+
while (x.t === "where") {
|
|
281
|
+
tests.push(x.test);
|
|
282
|
+
x = x.axis;
|
|
283
|
+
}
|
|
284
|
+
return {
|
|
285
|
+
base: x,
|
|
286
|
+
tests
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
/** This function adds `DEEP` to a read of the origin or of a free name inside a loop. */
|
|
290
|
+
const inLoop = (r, top) => top || (r & 6) === 0 ? r : r | DEEP;
|
|
291
|
+
/**
|
|
292
|
+
* This function gives the reads of an address: the focus at the top of the expression, or the origin, or nothing. A
|
|
293
|
+
* move from the focus or the origin can fail, so the address reads its start also before a fixed key (V-052).
|
|
294
|
+
*/
|
|
295
|
+
function addressReads(at, top) {
|
|
296
|
+
let start = top ? FOCUS : 0;
|
|
297
|
+
let found = 0;
|
|
298
|
+
at?.forEach((m) => {
|
|
299
|
+
if (m.t === "key" || m.t === "index") start = 0;
|
|
300
|
+
else if (m.t === "origin") start = ORIGIN;
|
|
301
|
+
else found |= start;
|
|
302
|
+
});
|
|
303
|
+
return inLoop(found | start, top);
|
|
304
|
+
}
|
|
305
|
+
/** The facts of an expression that reads nothing. */
|
|
306
|
+
const NOTHING = {
|
|
307
|
+
top: 0,
|
|
308
|
+
inner: 0,
|
|
309
|
+
near: 0n,
|
|
310
|
+
far: 0n,
|
|
311
|
+
loop: false
|
|
312
|
+
};
|
|
313
|
+
/** The facts of an expression that the analysis does not follow. */
|
|
314
|
+
const OPAQUE_FACTS = {
|
|
315
|
+
...NOTHING,
|
|
316
|
+
top: OPAQUE,
|
|
317
|
+
inner: OPAQUE
|
|
318
|
+
};
|
|
319
|
+
/** This function joins the keys of the parts of an expression. A part that cannot be in the memo makes the expression the same. */
|
|
320
|
+
function all(xs) {
|
|
321
|
+
const known = xs.filter((x) => x !== void 0);
|
|
322
|
+
if (known.length < xs.length) return void 0;
|
|
323
|
+
const some = known.filter((x) => x.length > 0);
|
|
324
|
+
return some.length === 1 ? some[0] : some;
|
|
325
|
+
}
|
|
326
|
+
/** This function adds each key of a list to `out`, in the order of the parts. */
|
|
327
|
+
function flatten(keys, out) {
|
|
328
|
+
for (const x of keys) if (Array.isArray(x)) flatten(x, out);
|
|
329
|
+
else out.push(x);
|
|
330
|
+
return out;
|
|
331
|
+
}
|
|
332
|
+
/**
|
|
333
|
+
* The analysis of one program. Each node has one record of facts, so a read of a part does not walk that part again. A
|
|
334
|
+
* part that cannot be in the memo stays known, so the next node above it does not walk it again. The walk of the keys
|
|
335
|
+
* starts at each node of the memo. Thus the work on the key lists grows with the nodes of the memo times the levels of
|
|
336
|
+
* `origin` around them. The gate COMPILE.GROWTH holds this growth at the largest legal sizes.
|
|
337
|
+
*/
|
|
338
|
+
var Analysis = class {
|
|
339
|
+
#bits = /* @__PURE__ */ new Map();
|
|
340
|
+
#facts = /* @__PURE__ */ new Map();
|
|
341
|
+
#dead = /* @__PURE__ */ new Map();
|
|
342
|
+
/** This method gives the set of some names. */
|
|
343
|
+
namesOf(names) {
|
|
344
|
+
return names.reduce((set, name) => {
|
|
345
|
+
const bit = this.#bits.get(name) ?? 1n << BigInt(this.#bits.size);
|
|
346
|
+
this.#bits.set(name, bit);
|
|
347
|
+
return set | bit;
|
|
348
|
+
}, 0n);
|
|
349
|
+
}
|
|
350
|
+
/** This method gives the facts of an expression. */
|
|
351
|
+
factsOf(e) {
|
|
352
|
+
const known = this.#facts.get(e);
|
|
353
|
+
if (known !== void 0) return known;
|
|
354
|
+
const facts = this.#read(e);
|
|
355
|
+
this.#facts.set(e, facts);
|
|
356
|
+
return facts;
|
|
357
|
+
}
|
|
358
|
+
/** This method joins the facts of some expressions. */
|
|
359
|
+
#joined(xs) {
|
|
360
|
+
return xs.reduce((a, x) => {
|
|
361
|
+
const b = this.factsOf(x);
|
|
362
|
+
return {
|
|
363
|
+
top: a.top | b.top,
|
|
364
|
+
inner: a.inner | b.inner,
|
|
365
|
+
near: a.near | b.near,
|
|
366
|
+
far: a.far | b.far,
|
|
367
|
+
loop: a.loop || b.loop
|
|
368
|
+
};
|
|
369
|
+
}, NOTHING);
|
|
370
|
+
}
|
|
371
|
+
/** This method gives the facts of an expression from the facts of its parts. */
|
|
372
|
+
#read(e) {
|
|
373
|
+
switch (e.tag) {
|
|
374
|
+
case "lit": return NOTHING;
|
|
375
|
+
case "var": return {
|
|
376
|
+
...NOTHING,
|
|
377
|
+
near: this.namesOf([e.name])
|
|
378
|
+
};
|
|
379
|
+
case "fold":
|
|
380
|
+
case "iterate":
|
|
381
|
+
case "fix": return {
|
|
382
|
+
...OPAQUE_FACTS,
|
|
383
|
+
loop: true
|
|
384
|
+
};
|
|
385
|
+
case "ext":
|
|
386
|
+
case "last":
|
|
387
|
+
case "fn":
|
|
388
|
+
case "call": return OPAQUE_FACTS;
|
|
389
|
+
case "ref": {
|
|
390
|
+
const at = this.#joined(addrExprs(e.at));
|
|
391
|
+
return {
|
|
392
|
+
...at,
|
|
393
|
+
top: at.top | addressReads(e.at, true),
|
|
394
|
+
inner: at.inner | addressReads(e.at, false)
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
case "app": return this.#joined(e.args);
|
|
398
|
+
case "rec": return this.#joined(Object.values(e.fields));
|
|
399
|
+
case "let": {
|
|
400
|
+
const binds = this.#joined(Object.values(e.bind));
|
|
401
|
+
const body = this.factsOf(e.body);
|
|
402
|
+
const free = ~this.namesOf(Object.keys(e.bind));
|
|
403
|
+
return {
|
|
404
|
+
top: binds.top | body.top,
|
|
405
|
+
inner: binds.inner | body.inner,
|
|
406
|
+
near: binds.near | body.near & free,
|
|
407
|
+
far: binds.far | body.far & free,
|
|
408
|
+
loop: binds.loop || body.loop
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
case "each": {
|
|
412
|
+
const { base, tests } = parts(e.axis);
|
|
413
|
+
const targets = (top) => base.t === "table" || listOf(e.axis) !== void 0 ? OPAQUE : base.t === "all" && (e.at ?? []).length === 0 ? 0 : addressReads(e.at, top);
|
|
414
|
+
const at = this.#joined(addrExprs(e.at));
|
|
415
|
+
const body = this.#joined([e.body, ...tests]);
|
|
416
|
+
return {
|
|
417
|
+
top: targets(true) | at.top | body.inner,
|
|
418
|
+
inner: targets(false) | at.inner | body.inner,
|
|
419
|
+
near: at.near,
|
|
420
|
+
far: at.far | body.near | body.far,
|
|
421
|
+
loop: true
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* This method gives the reads of `e`. `outer` has the names that the node binds outside `e`. `top` is true outside the
|
|
428
|
+
* loops of the node.
|
|
429
|
+
*/
|
|
430
|
+
#reads(e, outer, top) {
|
|
431
|
+
const { near, far, top: atTop, inner } = this.factsOf(e);
|
|
432
|
+
const internal = ((near | far) & outer) === 0n ? 0 : INTERNAL;
|
|
433
|
+
const free = (near & ~outer) === 0n ? 0 : inLoop(FREE, top);
|
|
434
|
+
const deep = (far & ~outer) === 0n ? 0 : inLoop(FREE, false);
|
|
435
|
+
return (top ? atTop : inner) | internal | free | deep;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* This method gives the keys of the part `e` of a node, or `undefined` if the part cannot be in the memo. `top` is
|
|
439
|
+
* true outside the loops of the node, and `outer` has the names that the node binds above `e`.
|
|
440
|
+
*/
|
|
441
|
+
keysOf(e, top, outer, pure) {
|
|
442
|
+
const mark = outer << 1n | (top ? 1n : 0n);
|
|
443
|
+
const dead = this.#dead.get(e);
|
|
444
|
+
if (dead?.has(mark) === true) return void 0;
|
|
445
|
+
const keys = this.#partsOf(e, top, outer, pure, false);
|
|
446
|
+
if (keys === void 0) this.#dead.set(e, (dead ?? /* @__PURE__ */ new Set()).add(mark));
|
|
447
|
+
return keys;
|
|
448
|
+
}
|
|
449
|
+
/** This method gives the keys of a node of the program. The node itself is not a key of itself. */
|
|
450
|
+
keysOfNode(e, pure) {
|
|
451
|
+
const keys = this.#partsOf(e, true, 0n, pure, true);
|
|
452
|
+
return keys === void 0 ? void 0 : flatten(keys, []);
|
|
453
|
+
}
|
|
454
|
+
/** This method gives the keys of an expression from the keys of its parts. An undefined value means no memo. */
|
|
455
|
+
#partsOf(e, top, outer, pure, root) {
|
|
456
|
+
const r = this.#reads(e, outer, true);
|
|
457
|
+
if ((r & (22 | (top ? FOCUS : 0))) === 0) {
|
|
458
|
+
if (!root) pure.add(e);
|
|
459
|
+
return [];
|
|
460
|
+
}
|
|
461
|
+
if (!root && (r & 57) === 0) return [e];
|
|
462
|
+
switch (e.tag) {
|
|
463
|
+
case "app": return all(e.args.map((x) => this.keysOf(x, top, outer, pure)));
|
|
464
|
+
case "rec": return all(Object.values(e.fields).map((x) => this.keysOf(x, top, outer, pure)));
|
|
465
|
+
case "let": {
|
|
466
|
+
const inner = outer | this.namesOf(Object.keys(e.bind));
|
|
467
|
+
return all([...Object.values(e.bind).map((x) => this.keysOf(x, top, outer, pure)), this.keysOf(e.body, top, inner, pure)]);
|
|
468
|
+
}
|
|
469
|
+
case "each": {
|
|
470
|
+
const { base, tests } = parts(e.axis);
|
|
471
|
+
if (base.t === "table" || listOf(e.axis) !== void 0 || (base.t !== "all" || (e.at ?? []).length > 0) && addressReads(e.at, top) !== 0) return void 0;
|
|
472
|
+
if (addrExprs(e.at).some((x) => this.#reads(x, outer, top) !== 0)) return void 0;
|
|
473
|
+
return all([e.body, ...tests].map((x) => this.keysOf(x, false, outer, pure)));
|
|
474
|
+
}
|
|
475
|
+
case "lit":
|
|
476
|
+
case "ref":
|
|
477
|
+
case "var":
|
|
478
|
+
case "ext":
|
|
479
|
+
case "fold":
|
|
480
|
+
case "iterate":
|
|
481
|
+
case "fix":
|
|
482
|
+
case "last":
|
|
483
|
+
case "fn":
|
|
484
|
+
case "call": return;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
/** This function tells if an expression has a loop outside its keys and outside the parts that have a memo of their own. */
|
|
489
|
+
function loopOutside(e, keys) {
|
|
490
|
+
if (keys.has(e)) return false;
|
|
491
|
+
switch (e.tag) {
|
|
492
|
+
case "each":
|
|
493
|
+
case "fold":
|
|
494
|
+
case "iterate":
|
|
495
|
+
case "fix": return true;
|
|
496
|
+
case "app": return e.args.some((x) => loopOutside(x, keys));
|
|
497
|
+
case "rec": return Object.values(e.fields).some((x) => loopOutside(x, keys));
|
|
498
|
+
case "let": return [...Object.values(e.bind), e.body].some((x) => loopOutside(x, keys));
|
|
499
|
+
case "ref": return addrExprs(e.at).some((x) => loopOutside(x, keys));
|
|
500
|
+
case "last":
|
|
501
|
+
case "lit":
|
|
502
|
+
case "var":
|
|
503
|
+
case "ext":
|
|
504
|
+
case "fn":
|
|
505
|
+
case "call": return false;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
/** This function gives the `let` expressions above an `each`, and the `each`, or `undefined` for another expression. */
|
|
509
|
+
function eachUnderLets(e) {
|
|
510
|
+
const lets = [];
|
|
511
|
+
let x = e;
|
|
512
|
+
while (x?.tag === "let") {
|
|
513
|
+
lets.push(x);
|
|
514
|
+
x = x.body;
|
|
515
|
+
}
|
|
516
|
+
return x?.tag === "each" ? {
|
|
517
|
+
lets,
|
|
518
|
+
each: x
|
|
519
|
+
} : void 0;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* This function gives the nodes of `expr` that a program keeps in its memo, each with its keys. Each such node has a
|
|
523
|
+
* loop outside its keys. A node inside such a node can be in the memo too. Thus the first evaluation of the outer
|
|
524
|
+
* node evaluates the inner node once. The function `loops` tells if a list op is one loop with an `each`. In a module,
|
|
525
|
+
* each list op is one. In tier 2, the ops of `isFusedOp` are.
|
|
526
|
+
*
|
|
527
|
+
* A node at two places of the program has one object in the source, and the compile gives each place a copy. A node of
|
|
528
|
+
* the memo at one place is a node of the memo at each place, so the second pass finds the other copies.
|
|
529
|
+
*/
|
|
530
|
+
function memoNodes(expr, loops = isListOp) {
|
|
531
|
+
const out = /* @__PURE__ */ new Map();
|
|
532
|
+
const analysis = new Analysis();
|
|
533
|
+
const note = (e) => {
|
|
534
|
+
if (!analysis.factsOf(e).loop) return;
|
|
535
|
+
const pure = /* @__PURE__ */ new Set();
|
|
536
|
+
const keys = analysis.keysOfNode(e, pure);
|
|
537
|
+
if (keys !== void 0 && loopOutside(e, /* @__PURE__ */ new Set([...keys, ...pure]))) out.set(e, keys);
|
|
538
|
+
};
|
|
539
|
+
const visit = (e) => {
|
|
540
|
+
note(e);
|
|
541
|
+
if (e.tag === "app") {
|
|
542
|
+
const [first, ...rest] = e.args;
|
|
543
|
+
const fused = loops(e.op) ? eachUnderLets(first) : void 0;
|
|
544
|
+
if (fused === void 0) {
|
|
545
|
+
if (first !== void 0) visit(first);
|
|
546
|
+
} else {
|
|
547
|
+
for (const l of fused.lets) Object.values(l.bind).forEach(visit);
|
|
548
|
+
[
|
|
549
|
+
fused.each.body,
|
|
550
|
+
...parts(fused.each.axis).tests,
|
|
551
|
+
...addrExprs(fused.each.at)
|
|
552
|
+
].forEach(visit);
|
|
553
|
+
}
|
|
554
|
+
rest.forEach(visit);
|
|
555
|
+
} else if (e.tag === "rec") Object.values(e.fields).forEach(visit);
|
|
556
|
+
else if (e.tag === "let") {
|
|
557
|
+
Object.values(e.bind).forEach(visit);
|
|
558
|
+
visit(e.body);
|
|
559
|
+
} else if (e.tag === "each") [
|
|
560
|
+
e.body,
|
|
561
|
+
...parts(e.axis).tests,
|
|
562
|
+
...addrExprs(e.at)
|
|
563
|
+
].forEach(visit);
|
|
564
|
+
else if (e.tag === "ref") addrExprs(e.at).forEach(visit);
|
|
565
|
+
};
|
|
566
|
+
visit(expr);
|
|
567
|
+
const found = new Set([...out.keys()].map(originalOf));
|
|
568
|
+
const copies = (e) => {
|
|
569
|
+
if (!out.has(e) && found.has(originalOf(e))) note(e);
|
|
570
|
+
children(e).forEach(copies);
|
|
571
|
+
};
|
|
572
|
+
copies(expr);
|
|
573
|
+
return out;
|
|
574
|
+
}
|
|
575
|
+
export { unsharedAll as a, factsOf as c, primitiveOf as d, unshared as i, isFusedOp as l, originalOf as n, MAX_CALLS as o, placesRead as r, entryOf as s, memoNodes as t, kindOfDomain as u };
|
|
576
|
+
|
|
577
|
+
//# sourceMappingURL=memo-J-Km2qc0.js.map
|