@tsplc/tmp-compiler 0.0.0-stage → 0.6.2
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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6558 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16076 -0
- package/dist/index.js.map +61 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1078 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1336 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2171 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +609 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +651 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,1137 @@
|
|
|
1
|
+
import { identityDigest } from "@tsplc/compiler/ir";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { from, joined, type Mapped, mapped } from "../positions";
|
|
4
|
+
import { sdkModules, valueSymbol } from "../sdk";
|
|
5
|
+
import {
|
|
6
|
+
namedElements,
|
|
7
|
+
namedFields,
|
|
8
|
+
parameterDefault,
|
|
9
|
+
type RunTimeOptional,
|
|
10
|
+
runTimeOptional,
|
|
11
|
+
symbolOf,
|
|
12
|
+
} from "./declarations";
|
|
13
|
+
import { DialectText } from "./dialect";
|
|
14
|
+
import {
|
|
15
|
+
type Closure,
|
|
16
|
+
ExpressionSpecializer,
|
|
17
|
+
type RoutineCall,
|
|
18
|
+
type RoutineTable,
|
|
19
|
+
type SpecializeContext,
|
|
20
|
+
type Typing,
|
|
21
|
+
} from "./expressions";
|
|
22
|
+
import { Emitter, type Frame, type Referents } from "./frame";
|
|
23
|
+
import type { GenericTypes } from "./generics";
|
|
24
|
+
import { EntryImports } from "./imports";
|
|
25
|
+
import { Initializers } from "./initializers";
|
|
26
|
+
import { type ArrayLayout, arrayLayout, Layouts } from "./layouts";
|
|
27
|
+
import { type Model, type ModelNode, type Root, tsPath } from "./model";
|
|
28
|
+
import { Names, routineNames, unusedName } from "./names";
|
|
29
|
+
import { referenceLayout, referenceType } from "./references";
|
|
30
|
+
import { StatementSpecializer } from "./statements";
|
|
31
|
+
import { Storage } from "./storage";
|
|
32
|
+
import {
|
|
33
|
+
type Counted,
|
|
34
|
+
constant,
|
|
35
|
+
type FunctionLike,
|
|
36
|
+
holdsSetupObjects,
|
|
37
|
+
restoring,
|
|
38
|
+
SpecializeError,
|
|
39
|
+
type Value,
|
|
40
|
+
} from "./values";
|
|
41
|
+
import { assignsSymbol } from "./writes";
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Evaluates the program's scan code against the setup graph. Everything setup decided (which
|
|
45
|
+
* instance `this` is, what a field refers to, array lengths, config never written in scan code)
|
|
46
|
+
* resolves at compile time; what is left becomes tsPLC dialect code over the stored state.
|
|
47
|
+
*/
|
|
48
|
+
export class Specializer implements RoutineTable {
|
|
49
|
+
readonly layouts: Layouts;
|
|
50
|
+
readonly storage: Storage;
|
|
51
|
+
readonly initializers: Initializers;
|
|
52
|
+
private readonly dialect: DialectText;
|
|
53
|
+
readonly expressions: ExpressionSpecializer;
|
|
54
|
+
private readonly statements: StatementSpecializer;
|
|
55
|
+
readonly imports: EntryImports;
|
|
56
|
+
/** The names the generated modules use, which routines, types and imports share. */
|
|
57
|
+
private readonly names: Names;
|
|
58
|
+
/** The source of each routine specialized so far, in the order each was finished. */
|
|
59
|
+
private readonly finished: Mapped[] = [];
|
|
60
|
+
/** Each async routine specialized for an instance, by routine name, which `cancelAsync(this)` cancels. */
|
|
61
|
+
private readonly asyncOwners = new Map<string, ModelNode>();
|
|
62
|
+
/** The routine that cancels an instance's async calls, by the instance, for each `cancelAsync(this)`. */
|
|
63
|
+
private readonly cancellers = new Map<ModelNode, string>();
|
|
64
|
+
/** Routines specialized from a class method, by routine name: `Class.method` and the TypeScript path of `this`. */
|
|
65
|
+
readonly origins = new Map<string, { name: string; instance?: string }>();
|
|
66
|
+
/** The routine each specialization is, by what tells it apart. */
|
|
67
|
+
private readonly specialized = new Map<string, Specialized>();
|
|
68
|
+
/** The routines whose bodies are being specialized, which a call to one of them would recurse into. */
|
|
69
|
+
private readonly unfinished = new Set<string>();
|
|
70
|
+
/** What each routine gives its callers, by routine name; empty until its body is specialized. */
|
|
71
|
+
private readonly results = new Map<string, RoutineResult>();
|
|
72
|
+
readonly checker: ts.TypeChecker;
|
|
73
|
+
|
|
74
|
+
constructor(
|
|
75
|
+
readonly model: Model,
|
|
76
|
+
private readonly generics: GenericTypes,
|
|
77
|
+
sdkFile: string,
|
|
78
|
+
sources: readonly ts.SourceFile[],
|
|
79
|
+
{
|
|
80
|
+
kept,
|
|
81
|
+
named,
|
|
82
|
+
enumRefusal,
|
|
83
|
+
}: {
|
|
84
|
+
/** Modules the compiled program keeps as written. */
|
|
85
|
+
kept: (file: string) => boolean;
|
|
86
|
+
/** Modules whose exported types the compiled program imports by name rather than copies. */
|
|
87
|
+
named: (source: ts.SourceFile) => boolean;
|
|
88
|
+
/** Why the generated modules can't import an enum from the module that declares it; undefined when they can. */
|
|
89
|
+
enumRefusal: (declaration: ts.EnumDeclaration) => string | undefined;
|
|
90
|
+
},
|
|
91
|
+
rootModule: (root: Root) => string,
|
|
92
|
+
) {
|
|
93
|
+
const checker = generics.checker;
|
|
94
|
+
this.checker = checker;
|
|
95
|
+
this.names = new Names(
|
|
96
|
+
routineNames(sources.filter((source) => !kept(source.fileName))),
|
|
97
|
+
);
|
|
98
|
+
this.imports = new EntryImports(this.names);
|
|
99
|
+
this.layouts = new Layouts(
|
|
100
|
+
model,
|
|
101
|
+
checker,
|
|
102
|
+
kept,
|
|
103
|
+
sources.filter(named),
|
|
104
|
+
(node) => this.storage.storageType(node),
|
|
105
|
+
this.names,
|
|
106
|
+
enumRefusal,
|
|
107
|
+
);
|
|
108
|
+
this.storage = new Storage(
|
|
109
|
+
model,
|
|
110
|
+
checker,
|
|
111
|
+
this.layouts,
|
|
112
|
+
this.imports,
|
|
113
|
+
sources,
|
|
114
|
+
kept,
|
|
115
|
+
rootModule,
|
|
116
|
+
);
|
|
117
|
+
this.initializers = new Initializers(
|
|
118
|
+
model,
|
|
119
|
+
checker,
|
|
120
|
+
this.layouts,
|
|
121
|
+
this.storage,
|
|
122
|
+
);
|
|
123
|
+
this.dialect = new DialectText(
|
|
124
|
+
model,
|
|
125
|
+
checker,
|
|
126
|
+
this.layouts,
|
|
127
|
+
this.storage,
|
|
128
|
+
this.initializers,
|
|
129
|
+
this.imports,
|
|
130
|
+
);
|
|
131
|
+
const context: SpecializeContext = {
|
|
132
|
+
model,
|
|
133
|
+
checker,
|
|
134
|
+
layouts: this.layouts,
|
|
135
|
+
storage: this.storage,
|
|
136
|
+
initializers: this.initializers,
|
|
137
|
+
imports: this.imports,
|
|
138
|
+
dialect: this.dialect,
|
|
139
|
+
sdkFile,
|
|
140
|
+
sources,
|
|
141
|
+
isKept: kept,
|
|
142
|
+
};
|
|
143
|
+
this.expressions = new ExpressionSpecializer(context, this);
|
|
144
|
+
this.statements = new StatementSpecializer(context, this.expressions);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Statements that couldn't be specialized, for the build to report together. */
|
|
148
|
+
get failures(): readonly SpecializeError[] {
|
|
149
|
+
return this.statements.failures;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Specializes the entry function, returning its body. */
|
|
153
|
+
entry(declaration: ts.FunctionDeclaration | ts.ArrowFunction): Mapped[] {
|
|
154
|
+
if (!declaration.body) {
|
|
155
|
+
throw new SpecializeError(declaration, "The entry function has no body.");
|
|
156
|
+
}
|
|
157
|
+
const frame: Frame = {
|
|
158
|
+
emitter: new Emitter(declaration),
|
|
159
|
+
env: new Map(),
|
|
160
|
+
loops: [],
|
|
161
|
+
returns: undefined,
|
|
162
|
+
async: false,
|
|
163
|
+
};
|
|
164
|
+
this.statements.body(declaration.body, frame);
|
|
165
|
+
return frame.emitter.lines;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The routines specialized, then each instance's canceller, written last: it names every async
|
|
170
|
+
* routine of its instance, which may be specialized after the call that cancels them.
|
|
171
|
+
*/
|
|
172
|
+
get routines(): Mapped[] {
|
|
173
|
+
return [
|
|
174
|
+
...this.finished,
|
|
175
|
+
...[...this.cancellers].map(([self, name]) => {
|
|
176
|
+
const owned = [...this.asyncOwners]
|
|
177
|
+
.filter(([, owner]) => owner === self)
|
|
178
|
+
.map(([routine]) => routine);
|
|
179
|
+
const body = owned.length ? ` cancelAsync(${owned.join(", ")});` : "";
|
|
180
|
+
return mapped`function ${name}(): void {${body} }`;
|
|
181
|
+
}),
|
|
182
|
+
];
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The routine that cancels the pending async calls of `self`'s methods, as `cancelAsync(this)` does. */
|
|
186
|
+
canceller(self: ModelNode): string {
|
|
187
|
+
const known = this.cancellers.get(self);
|
|
188
|
+
if (known) {
|
|
189
|
+
return known;
|
|
190
|
+
}
|
|
191
|
+
const name = unusedName(
|
|
192
|
+
`tsplcCancel_${identityDigest(this.model.identity(self) ?? String(self.id))}`,
|
|
193
|
+
(candidate) => this.routineNamed(candidate),
|
|
194
|
+
);
|
|
195
|
+
this.results.set(name, {});
|
|
196
|
+
this.names.declare(name);
|
|
197
|
+
this.cancellers.set(self, name);
|
|
198
|
+
this.imports.addSdk(sdkModules.async, "cancelAsync");
|
|
199
|
+
return name;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
speculatively<T>(run: () => T): T {
|
|
203
|
+
const routines = this.finished.length;
|
|
204
|
+
const restored = [
|
|
205
|
+
restoring(this.asyncOwners),
|
|
206
|
+
restoring(this.cancellers),
|
|
207
|
+
restoring(this.origins),
|
|
208
|
+
restoring(this.specialized),
|
|
209
|
+
restoring(this.results),
|
|
210
|
+
];
|
|
211
|
+
try {
|
|
212
|
+
return run();
|
|
213
|
+
} finally {
|
|
214
|
+
this.finished.splice(routines);
|
|
215
|
+
for (const restore of restored) {
|
|
216
|
+
restore();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** The name of `declaration` specialized for `self` and these known arguments. */
|
|
222
|
+
routine(
|
|
223
|
+
declaration: FunctionLike,
|
|
224
|
+
self: ModelNode | undefined,
|
|
225
|
+
args: Value[],
|
|
226
|
+
closure?: Closure,
|
|
227
|
+
): RoutineCall {
|
|
228
|
+
// A generic class's method specializes with its instance's type arguments in scope.
|
|
229
|
+
const owner =
|
|
230
|
+
self?.captured.kind === "instance" ? this.model.classOf(self) : undefined;
|
|
231
|
+
return this.generics.within(owner, self?.type, () =>
|
|
232
|
+
this.specializeRoutine(declaration, self, args, closure),
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private specializeRoutine(
|
|
237
|
+
declaration: FunctionLike,
|
|
238
|
+
self: ModelNode | undefined,
|
|
239
|
+
args: Value[],
|
|
240
|
+
closure?: Closure,
|
|
241
|
+
): RoutineCall {
|
|
242
|
+
const signature = this.checker.getSignatureFromDeclaration(declaration);
|
|
243
|
+
const returnType =
|
|
244
|
+
signature && this.checker.getReturnTypeOfSignature(signature);
|
|
245
|
+
const isAsync = Boolean(
|
|
246
|
+
ts.canHaveModifiers(declaration) &&
|
|
247
|
+
ts
|
|
248
|
+
.getModifiers(declaration)
|
|
249
|
+
?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword),
|
|
250
|
+
);
|
|
251
|
+
const frame: Frame = {
|
|
252
|
+
emitter: new Emitter(declaration),
|
|
253
|
+
env: new Map(closure?.env),
|
|
254
|
+
loops: [],
|
|
255
|
+
self,
|
|
256
|
+
returns:
|
|
257
|
+
returnType &&
|
|
258
|
+
(isAsync ? this.checker.getAwaitedType(returnType) : returnType),
|
|
259
|
+
async: isAsync,
|
|
260
|
+
...(returnType &&
|
|
261
|
+
referenceType(returnType) && {
|
|
262
|
+
referents: { options: new Set<ModelNode>(), optional: false },
|
|
263
|
+
}),
|
|
264
|
+
};
|
|
265
|
+
const parameters: RoutineParameter[] = [];
|
|
266
|
+
const keys: Specialization[] = [];
|
|
267
|
+
const parameterList = ts.isGetAccessorDeclaration(declaration)
|
|
268
|
+
? []
|
|
269
|
+
: declaration.parameters;
|
|
270
|
+
parameterList.forEach((parameter, index) => {
|
|
271
|
+
const declared = this.checker.getTypeAtLocation(parameter);
|
|
272
|
+
if (parameter.dotDotDotToken) {
|
|
273
|
+
this.bindRest(
|
|
274
|
+
parameter,
|
|
275
|
+
args.slice(index),
|
|
276
|
+
declared,
|
|
277
|
+
frame,
|
|
278
|
+
parameters,
|
|
279
|
+
keys,
|
|
280
|
+
);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const arg = args[index] ?? constant(undefined);
|
|
284
|
+
const fallback = parameterDefault(this.checker, arg, parameter);
|
|
285
|
+
if (fallback) {
|
|
286
|
+
this.bindDefault(
|
|
287
|
+
parameter,
|
|
288
|
+
fallback,
|
|
289
|
+
declared,
|
|
290
|
+
frame,
|
|
291
|
+
parameters,
|
|
292
|
+
keys,
|
|
293
|
+
);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
this.bindParameter(
|
|
297
|
+
parameter.name,
|
|
298
|
+
arg,
|
|
299
|
+
declared,
|
|
300
|
+
frame,
|
|
301
|
+
parameters,
|
|
302
|
+
keys,
|
|
303
|
+
);
|
|
304
|
+
});
|
|
305
|
+
// Captured runtime locals the routine reads become its parameters; one it writes is borrowed.
|
|
306
|
+
const read = readSymbols(declaration, this.checker);
|
|
307
|
+
const captured = [...(closure?.env ?? [])].filter(([symbol]) =>
|
|
308
|
+
read.has(symbol),
|
|
309
|
+
);
|
|
310
|
+
const locals = new Map<string, Value>();
|
|
311
|
+
const passed = new Map<ts.Symbol, Value | string>();
|
|
312
|
+
for (const [symbol, value] of captured) {
|
|
313
|
+
if (
|
|
314
|
+
value.kind === "code" &&
|
|
315
|
+
value.place &&
|
|
316
|
+
declaration.body !== undefined &&
|
|
317
|
+
assignsSymbol(declaration.body, new Set([symbol]), this.checker)
|
|
318
|
+
) {
|
|
319
|
+
const name = frame.emitter.local(symbol.name);
|
|
320
|
+
const type = this.expressions.heldType(value, parameterTyping);
|
|
321
|
+
this.imports.addSdk(sdkModules.types, "ref");
|
|
322
|
+
parameters.push({
|
|
323
|
+
name,
|
|
324
|
+
type: `InOut<${type}>`,
|
|
325
|
+
argument: mapped`ref(${value.text})`,
|
|
326
|
+
});
|
|
327
|
+
const local: Value = {
|
|
328
|
+
kind: "code",
|
|
329
|
+
text: name,
|
|
330
|
+
declared: value.declared,
|
|
331
|
+
place: true,
|
|
332
|
+
iec: value.iec,
|
|
333
|
+
};
|
|
334
|
+
locals.set(String(value.text), local);
|
|
335
|
+
frame.env.set(symbol, local);
|
|
336
|
+
passed.set(symbol, "ref");
|
|
337
|
+
} else if (
|
|
338
|
+
value.kind === "code" ||
|
|
339
|
+
value.kind === "choice" ||
|
|
340
|
+
value.kind === "masked" ||
|
|
341
|
+
value.kind === "counted"
|
|
342
|
+
) {
|
|
343
|
+
// A captured reference is the caller's too: its run-time id is passed as a local's value is.
|
|
344
|
+
const lifted = this.lifted(
|
|
345
|
+
value,
|
|
346
|
+
symbol.name,
|
|
347
|
+
frame,
|
|
348
|
+
everyValue(frame, parameters),
|
|
349
|
+
);
|
|
350
|
+
if (value.kind === "code") {
|
|
351
|
+
locals.set(String(value.text), lifted);
|
|
352
|
+
}
|
|
353
|
+
frame.env.set(symbol, lifted);
|
|
354
|
+
passed.set(symbol, lifted);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
for (const [symbol, value] of captured) {
|
|
358
|
+
keys.push({ name: symbol.name, value: passed.get(symbol) ?? value });
|
|
359
|
+
}
|
|
360
|
+
// A captured arrow or literal is lifted after the locals, so a local it holds is the routine's parameter.
|
|
361
|
+
const lift = (parameter: Passing["parameter"]) => {
|
|
362
|
+
for (const [symbol, value] of captured) {
|
|
363
|
+
if (!passed.has(symbol)) {
|
|
364
|
+
frame.env.set(
|
|
365
|
+
symbol,
|
|
366
|
+
this.lifted(value, symbol.name, frame, {
|
|
367
|
+
parameter,
|
|
368
|
+
local: (place) => locals.get(String(place)),
|
|
369
|
+
}),
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
// A run-time part that reads the caller's locals is passed in; any other is read where it is, so the key holds its text.
|
|
375
|
+
const parts: { base: string; text: string; caller: boolean }[] = [];
|
|
376
|
+
lift((text, base) => {
|
|
377
|
+
parts.push({
|
|
378
|
+
base,
|
|
379
|
+
text: String(text),
|
|
380
|
+
caller: Boolean(closure?.caller.names(text)),
|
|
381
|
+
});
|
|
382
|
+
return undefined;
|
|
383
|
+
});
|
|
384
|
+
const key = `${declarationKey(declaration)}|${self?.id ?? ""}|${[
|
|
385
|
+
...keys.map((entry) => specializationKey(entry, { stable: false })),
|
|
386
|
+
...parts.map(({ base, text, caller }) =>
|
|
387
|
+
caller ? `${base}<-` : `${base}@${text}`,
|
|
388
|
+
),
|
|
389
|
+
].join(",")}`;
|
|
390
|
+
const pass = everyValue(frame, parameters).parameter;
|
|
391
|
+
const known = this.specialized.get(key);
|
|
392
|
+
if (known && this.unfinished.has(known.name)) {
|
|
393
|
+
const named = declaration.name?.getText() ?? "This function";
|
|
394
|
+
throw new SpecializeError(
|
|
395
|
+
declaration.name ?? declaration,
|
|
396
|
+
`${named} calls itself, and a PLC routine can't: each call's state is allocated when the program compiles. Write the repetition as a loop.`,
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
if (known) {
|
|
400
|
+
lift((text, base, type) =>
|
|
401
|
+
known.parts.has(base) ? pass(text, base, type) : undefined,
|
|
402
|
+
);
|
|
403
|
+
return this.call(
|
|
404
|
+
known.name,
|
|
405
|
+
declaration,
|
|
406
|
+
parameters.map(({ argument }) => argument),
|
|
407
|
+
frame.returns,
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
const name = this.routineName(declaration, self, keys);
|
|
411
|
+
this.specialized.set(key, { name, parts: new Set() });
|
|
412
|
+
this.unfinished.add(name);
|
|
413
|
+
const callerParts = new Set(
|
|
414
|
+
parts.filter(({ caller }) => caller).map(({ base }) => base),
|
|
415
|
+
);
|
|
416
|
+
const passedParts: { base: string; local: string }[] = [];
|
|
417
|
+
lift((text, base, type) => {
|
|
418
|
+
if (!callerParts.has(base)) {
|
|
419
|
+
return undefined;
|
|
420
|
+
}
|
|
421
|
+
const local = pass(text, base, type);
|
|
422
|
+
passedParts.push({ base, local });
|
|
423
|
+
return local;
|
|
424
|
+
});
|
|
425
|
+
try {
|
|
426
|
+
this.specializeBody(declaration, frame);
|
|
427
|
+
} finally {
|
|
428
|
+
this.unfinished.delete(name);
|
|
429
|
+
}
|
|
430
|
+
const unread = new Set(
|
|
431
|
+
passedParts
|
|
432
|
+
.map(({ local }) => local)
|
|
433
|
+
.filter((local) => !frame.emitter.reads(local)),
|
|
434
|
+
);
|
|
435
|
+
const kept = parameters.filter(({ name }) => !unread.has(name));
|
|
436
|
+
this.specialized.set(key, {
|
|
437
|
+
name,
|
|
438
|
+
parts: new Set(
|
|
439
|
+
passedParts
|
|
440
|
+
.filter(({ local }) => !unread.has(local))
|
|
441
|
+
.map(({ base }) => base),
|
|
442
|
+
),
|
|
443
|
+
});
|
|
444
|
+
const resultText = this.resultText(declaration, frame);
|
|
445
|
+
const head = `${frame.async ? "async " : ""}function ${name}(${kept.map((parameter) => `${parameter.name}: ${parameter.type}`).join(", ")}): ${
|
|
446
|
+
frame.async ? `Promise<${resultText}>` : resultText
|
|
447
|
+
} {`;
|
|
448
|
+
const instance = self?.owner && tsPath(self.owner);
|
|
449
|
+
if (
|
|
450
|
+
self?.captured.kind === "instance" &&
|
|
451
|
+
(ts.isMethodDeclaration(declaration) ||
|
|
452
|
+
ts.isGetAccessorDeclaration(declaration))
|
|
453
|
+
) {
|
|
454
|
+
this.origins.set(name, {
|
|
455
|
+
name: `${self.captured.name}.${declaration.name.getText()}`,
|
|
456
|
+
...(instance && { instance }),
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
if (frame.async && self?.captured.kind === "instance") {
|
|
460
|
+
this.asyncOwners.set(name, self);
|
|
461
|
+
}
|
|
462
|
+
this.finished.push(
|
|
463
|
+
joined(
|
|
464
|
+
[
|
|
465
|
+
from(declaration, `${head}${instance ? ` /* ${instance} */` : ""}`),
|
|
466
|
+
...frame.emitter.lines,
|
|
467
|
+
from(closingBrace(declaration), "}"),
|
|
468
|
+
],
|
|
469
|
+
"\n",
|
|
470
|
+
),
|
|
471
|
+
);
|
|
472
|
+
this.results.set(name, {
|
|
473
|
+
layout: frame.resultLayout,
|
|
474
|
+
length: frame.resultLength,
|
|
475
|
+
returnsStored: frame.returnsStored,
|
|
476
|
+
referents: frame.referents,
|
|
477
|
+
});
|
|
478
|
+
return this.call(
|
|
479
|
+
name,
|
|
480
|
+
declaration,
|
|
481
|
+
kept.map(({ argument }) => argument),
|
|
482
|
+
frame.returns,
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** A routine's body: its statements, or a one-expression arrow's result. */
|
|
487
|
+
private specializeBody(declaration: FunctionLike, frame: Frame): void {
|
|
488
|
+
if (ts.isArrowFunction(declaration) && !ts.isBlock(declaration.body)) {
|
|
489
|
+
frame.emitter.origin = declaration.body;
|
|
490
|
+
const result = this.expressions.expression(
|
|
491
|
+
declaration.body,
|
|
492
|
+
frame,
|
|
493
|
+
frame.returns,
|
|
494
|
+
);
|
|
495
|
+
this.expressions.references.returned(result, declaration.body, frame);
|
|
496
|
+
if (!frame.returns || frame.returns.flags & ts.TypeFlags.Void) {
|
|
497
|
+
if (result.kind === "code") {
|
|
498
|
+
frame.emitter.line(mapped`${result.text};`);
|
|
499
|
+
}
|
|
500
|
+
} else {
|
|
501
|
+
frame.emitter.line(
|
|
502
|
+
mapped`return ${this.dialect.code(result, frame.returns)};`,
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
} else if (declaration.body) {
|
|
506
|
+
this.statements.body(declaration.body, frame);
|
|
507
|
+
} else {
|
|
508
|
+
throw new SpecializeError(declaration, "This function has no body.");
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/** A call to a routine with these runtime arguments: what its callers read from its result. */
|
|
513
|
+
private call(
|
|
514
|
+
name: string,
|
|
515
|
+
declaration: FunctionLike,
|
|
516
|
+
args: (string | Mapped)[],
|
|
517
|
+
returns: ts.Type | undefined,
|
|
518
|
+
): RoutineCall {
|
|
519
|
+
const result = this.results.get(name);
|
|
520
|
+
const referents = result?.referents;
|
|
521
|
+
return {
|
|
522
|
+
name,
|
|
523
|
+
args,
|
|
524
|
+
returns,
|
|
525
|
+
iec: this.layouts.iecName(declaration),
|
|
526
|
+
layout: result?.layout,
|
|
527
|
+
length: result?.length,
|
|
528
|
+
returnsStored: result?.returnsStored,
|
|
529
|
+
referents: referents && {
|
|
530
|
+
options: [...referents.options],
|
|
531
|
+
optional: referents.optional,
|
|
532
|
+
},
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/** The result type a routine's head declares: an IEC width the declaration names, as `(): UINT` does, wins. */
|
|
537
|
+
private resultText(declaration: FunctionLike, frame: Frame): string {
|
|
538
|
+
if (!frame.returns || frame.returns.flags & ts.TypeFlags.Void) {
|
|
539
|
+
return "void";
|
|
540
|
+
}
|
|
541
|
+
return (
|
|
542
|
+
frame.resultLayout?.text ??
|
|
543
|
+
this.layouts.iecName(declaration) ??
|
|
544
|
+
(frame.referents ? referenceLayout : this.layouts.text(frame.returns))
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* A specialized routine's name: its class and method, and for an instance or a known argument a
|
|
550
|
+
* digest of which. Numbering specializations in the order they were made renamed them when an
|
|
551
|
+
* instance was added before, which renamed the routines a job calls, and so its code.
|
|
552
|
+
*/
|
|
553
|
+
private routineName(
|
|
554
|
+
declaration: FunctionLike,
|
|
555
|
+
self: ModelNode | undefined,
|
|
556
|
+
keys: readonly Specialization[],
|
|
557
|
+
): string {
|
|
558
|
+
const own =
|
|
559
|
+
declaration.name && ts.isIdentifier(declaration.name)
|
|
560
|
+
? declaration.name.text
|
|
561
|
+
: undefined;
|
|
562
|
+
const variable =
|
|
563
|
+
!own &&
|
|
564
|
+
declaration.parent &&
|
|
565
|
+
ts.isVariableDeclaration(declaration.parent) &&
|
|
566
|
+
ts.isIdentifier(declaration.parent.name)
|
|
567
|
+
? declaration.parent.name.text
|
|
568
|
+
: undefined;
|
|
569
|
+
const owner =
|
|
570
|
+
self?.captured.kind === "instance" ? self.captured.name : undefined;
|
|
571
|
+
const named = [owner, own ?? variable ?? "callback"]
|
|
572
|
+
.filter(Boolean)
|
|
573
|
+
.join("_");
|
|
574
|
+
const instance = self && this.model.identity(self);
|
|
575
|
+
const specialized = Boolean(instance) || keys.length > 0;
|
|
576
|
+
const stable = keys.map((entry) =>
|
|
577
|
+
specializationKey(entry, { stable: true }),
|
|
578
|
+
);
|
|
579
|
+
const base = specialized
|
|
580
|
+
? `${named}_${identityDigest([instance ?? "", ...stable].join("\0"))}`
|
|
581
|
+
: named;
|
|
582
|
+
const name = unusedName(base, (candidate) => this.routineNamed(candidate));
|
|
583
|
+
this.results.set(name, {});
|
|
584
|
+
this.names.declare(name);
|
|
585
|
+
return name;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** Whether the generated entry module already uses `name`, for a routine or an import. */
|
|
589
|
+
named(name: string): boolean {
|
|
590
|
+
return this.routineNamed(name);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/** Whether a routine can't take `name`: another has it, or the entry module imports something as it. */
|
|
594
|
+
private routineNamed(name: string): boolean {
|
|
595
|
+
return this.results.has(name) || this.names.importedAs(name);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
private bindParameter(
|
|
599
|
+
name: ts.BindingName,
|
|
600
|
+
arg: Value,
|
|
601
|
+
declared: ts.Type,
|
|
602
|
+
frame: Frame,
|
|
603
|
+
parameters: RoutineParameter[],
|
|
604
|
+
keys: Specialization[],
|
|
605
|
+
): void {
|
|
606
|
+
if (ts.isObjectBindingPattern(name)) {
|
|
607
|
+
for (const { element, field } of namedFields(name, "parameter")) {
|
|
608
|
+
this.bindElement(
|
|
609
|
+
element,
|
|
610
|
+
this.expressions.member(arg, field, element, frame),
|
|
611
|
+
frame,
|
|
612
|
+
parameters,
|
|
613
|
+
keys,
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (ts.isArrayBindingPattern(name)) {
|
|
619
|
+
for (const { element, index } of namedElements(name, "parameter")) {
|
|
620
|
+
this.bindElement(
|
|
621
|
+
element,
|
|
622
|
+
this.expressions.element(arg, constant(index), element, frame),
|
|
623
|
+
frame,
|
|
624
|
+
parameters,
|
|
625
|
+
keys,
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
const symbol = symbolOf(this.checker, name);
|
|
631
|
+
// A known primitive stays a parameter too, so one routine serves every value.
|
|
632
|
+
if (
|
|
633
|
+
arg.kind === "code" ||
|
|
634
|
+
(arg.kind === "virtual" && !this.isKnownShape(arg)) ||
|
|
635
|
+
(arg.kind === "const" &&
|
|
636
|
+
!(declared.flags & ts.TypeFlags.Undefined) &&
|
|
637
|
+
arg.value !== undefined)
|
|
638
|
+
) {
|
|
639
|
+
const local = frame.emitter.local(name.text);
|
|
640
|
+
// A name destructured from a field keeps that field's width.
|
|
641
|
+
const iec =
|
|
642
|
+
this.layouts.iecName(name.parent) ??
|
|
643
|
+
(arg.kind === "code" ? arg.iec : undefined);
|
|
644
|
+
// An array parameter takes the length of the storage or literal passed to it, which its type doesn't name.
|
|
645
|
+
const element = this.checker.getIndexTypeOfType(
|
|
646
|
+
declared,
|
|
647
|
+
ts.IndexKind.Number,
|
|
648
|
+
);
|
|
649
|
+
const literal =
|
|
650
|
+
arg.kind === "virtual" && arg.items && element
|
|
651
|
+
? arrayLayout(this.layouts.text(element), arg.items.length)
|
|
652
|
+
: undefined;
|
|
653
|
+
const layout = arg.kind === "code" ? arg.layout : literal;
|
|
654
|
+
const type = iec ?? layout?.text ?? this.layouts.text(declared);
|
|
655
|
+
parameters.push({
|
|
656
|
+
name: local,
|
|
657
|
+
type,
|
|
658
|
+
argument: this.dialect.assignmentText(arg, declared, iec),
|
|
659
|
+
});
|
|
660
|
+
// A PLC parameter is read-only; one the routine assigns, as JavaScript allows, is a local copy.
|
|
661
|
+
const routine = ts.findAncestor(name, ts.isFunctionLike);
|
|
662
|
+
const assigned =
|
|
663
|
+
routine !== undefined &&
|
|
664
|
+
"body" in routine &&
|
|
665
|
+
routine.body !== undefined &&
|
|
666
|
+
assignsSymbol(routine.body, new Set([symbol]), this.checker);
|
|
667
|
+
const held = assigned ? frame.emitter.local(name.text) : local;
|
|
668
|
+
if (assigned) {
|
|
669
|
+
frame.emitter.line(`let ${held}: ${type} = ${local};`);
|
|
670
|
+
}
|
|
671
|
+
frame.env.set(symbol, {
|
|
672
|
+
kind: "code",
|
|
673
|
+
text: held,
|
|
674
|
+
declared,
|
|
675
|
+
place: true,
|
|
676
|
+
iec,
|
|
677
|
+
layout,
|
|
678
|
+
// A copy of stored state stays one in the routine it is passed to.
|
|
679
|
+
copyOf: arg.kind === "code" ? arg.copyOf : undefined,
|
|
680
|
+
});
|
|
681
|
+
keys.push({
|
|
682
|
+
name: name.text,
|
|
683
|
+
value: `code${layout ? `:${layout.text}` : ""}`,
|
|
684
|
+
width: iec,
|
|
685
|
+
});
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
const lifted = this.lifted(
|
|
689
|
+
arg,
|
|
690
|
+
name.text,
|
|
691
|
+
frame,
|
|
692
|
+
everyValue(frame, parameters),
|
|
693
|
+
);
|
|
694
|
+
frame.env.set(symbol, lifted);
|
|
695
|
+
keys.push({ name: name.text, value: lifted });
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* A pattern element's default is worked out in the routine, as a parameter's is: it may read an
|
|
700
|
+
* earlier parameter, which the caller passing the part cannot name.
|
|
701
|
+
*/
|
|
702
|
+
private bindElement(
|
|
703
|
+
element: ts.BindingElement,
|
|
704
|
+
part: Value,
|
|
705
|
+
frame: Frame,
|
|
706
|
+
parameters: RoutineParameter[],
|
|
707
|
+
keys: Specialization[],
|
|
708
|
+
): void {
|
|
709
|
+
const declared = this.checker.getTypeAtLocation(element);
|
|
710
|
+
if (!element.initializer) {
|
|
711
|
+
this.bindParameter(element.name, part, declared, frame, parameters, keys);
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
if (part.kind === "const" && part.value === undefined) {
|
|
715
|
+
this.bindDefault(
|
|
716
|
+
element,
|
|
717
|
+
element.initializer,
|
|
718
|
+
declared,
|
|
719
|
+
frame,
|
|
720
|
+
parameters,
|
|
721
|
+
keys,
|
|
722
|
+
);
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
const optional = runTimeOptional(this.checker, part);
|
|
726
|
+
if (!optional) {
|
|
727
|
+
this.bindParameter(element.name, part, declared, frame, parameters, keys);
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
this.expressions.bind(
|
|
731
|
+
element.name,
|
|
732
|
+
this.expressions.defaulted(
|
|
733
|
+
this.passedPart(element, optional, frame, parameters, keys),
|
|
734
|
+
element.initializer,
|
|
735
|
+
frame,
|
|
736
|
+
),
|
|
737
|
+
frame,
|
|
738
|
+
declared,
|
|
739
|
+
{ mutable: true, iec: this.layouts.iecName(element) },
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/** A pattern part that may be undefined at run time, passed to the routine whole, as the routine reads it. */
|
|
744
|
+
private passedPart(
|
|
745
|
+
element: ts.BindingElement,
|
|
746
|
+
part: RunTimeOptional,
|
|
747
|
+
frame: Frame,
|
|
748
|
+
parameters: RoutineParameter[],
|
|
749
|
+
keys: Specialization[],
|
|
750
|
+
): Value {
|
|
751
|
+
const name = element.name.getText();
|
|
752
|
+
const base = ts.isIdentifier(element.name) ? element.name.text : "part";
|
|
753
|
+
if (part.kind === "choice") {
|
|
754
|
+
const lifted = this.lifted(
|
|
755
|
+
part,
|
|
756
|
+
base,
|
|
757
|
+
frame,
|
|
758
|
+
everyValue(frame, parameters),
|
|
759
|
+
);
|
|
760
|
+
// A narrowed choice passed whole has the same key, but its routine has no fallback.
|
|
761
|
+
keys.push({ name, value: lifted }, { name, value: "default?" });
|
|
762
|
+
return lifted;
|
|
763
|
+
}
|
|
764
|
+
const local = frame.emitter.local(base);
|
|
765
|
+
parameters.push({
|
|
766
|
+
name: local,
|
|
767
|
+
type: this.layouts.text(part.declared),
|
|
768
|
+
argument: this.dialect.assignmentText(part, part.declared, undefined),
|
|
769
|
+
});
|
|
770
|
+
// Unlike a default bound in the routine, the part is passed, so the routine has one more parameter.
|
|
771
|
+
keys.push({ name, value: "code?" });
|
|
772
|
+
return { kind: "code", text: local, declared: part.declared, place: true };
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* A literal argument that holds objects setup built is known to the routine, but a runtime value
|
|
777
|
+
* inside it is the caller's: each becomes a parameter of its own, which the caller passes.
|
|
778
|
+
*/
|
|
779
|
+
private lifted(
|
|
780
|
+
value: Value,
|
|
781
|
+
base: string,
|
|
782
|
+
frame: Frame,
|
|
783
|
+
passing: Passing,
|
|
784
|
+
): Value {
|
|
785
|
+
const lift = (part: Value, name: string) =>
|
|
786
|
+
this.lifted(part, name, frame, passing);
|
|
787
|
+
// A counted array passes its local array and each bound decided at run time.
|
|
788
|
+
if (value.kind === "counted") {
|
|
789
|
+
const { source } = value;
|
|
790
|
+
const start = lift(value.start, `${base}_start`);
|
|
791
|
+
const end = lift(value.end, `${base}_end`);
|
|
792
|
+
if (source.kind === "items") {
|
|
793
|
+
return {
|
|
794
|
+
kind: "counted",
|
|
795
|
+
source: {
|
|
796
|
+
kind: "items",
|
|
797
|
+
items: source.items.map((item, index) =>
|
|
798
|
+
lift(item, `${base}_${index}`),
|
|
799
|
+
),
|
|
800
|
+
},
|
|
801
|
+
start,
|
|
802
|
+
end,
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
const local = passing.parameter(source.text, base, () => source.layout);
|
|
806
|
+
if (local === undefined) {
|
|
807
|
+
return { ...value, start, end };
|
|
808
|
+
}
|
|
809
|
+
return {
|
|
810
|
+
kind: "counted",
|
|
811
|
+
source: {
|
|
812
|
+
...source,
|
|
813
|
+
text: local,
|
|
814
|
+
element: (index) => reread(source.element(index), local, index),
|
|
815
|
+
// The routine is passed the end as it is now, which a count it never saw can't be compared with.
|
|
816
|
+
filled: undefined,
|
|
817
|
+
},
|
|
818
|
+
start,
|
|
819
|
+
end,
|
|
820
|
+
};
|
|
821
|
+
}
|
|
822
|
+
// A reference chosen at run time passes its id; the routine specializes on what it can be.
|
|
823
|
+
if (value.kind === "choice") {
|
|
824
|
+
const local = passing.parameter(value.index, base, () => referenceLayout);
|
|
825
|
+
if (local === undefined) {
|
|
826
|
+
return value;
|
|
827
|
+
}
|
|
828
|
+
return {
|
|
829
|
+
kind: "choice",
|
|
830
|
+
options: value.options,
|
|
831
|
+
index: local,
|
|
832
|
+
optional: value.optional,
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
if (value.kind === "masked") {
|
|
836
|
+
return {
|
|
837
|
+
...value,
|
|
838
|
+
entries: value.entries.map((entry, index) => ({
|
|
839
|
+
item: lift(entry.item, `${base}_${index}`),
|
|
840
|
+
present: lift(entry.present, `${base}_${index}_present`),
|
|
841
|
+
})),
|
|
842
|
+
order: value.order && lift(value.order, `${base}_order`),
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
if (value.kind === "code") {
|
|
846
|
+
const own = value.place ? passing.local(value.text) : undefined;
|
|
847
|
+
if (own) {
|
|
848
|
+
return own;
|
|
849
|
+
}
|
|
850
|
+
const local = passing.parameter(value.text, base, () =>
|
|
851
|
+
this.expressions.heldType(value, parameterTyping),
|
|
852
|
+
);
|
|
853
|
+
if (local === undefined) {
|
|
854
|
+
return value;
|
|
855
|
+
}
|
|
856
|
+
return {
|
|
857
|
+
kind: "code",
|
|
858
|
+
text: local,
|
|
859
|
+
declared: value.declared,
|
|
860
|
+
place: true,
|
|
861
|
+
iec: value.iec,
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
// A callback passed in carries what it captured from the caller: runtime values among them are passed too.
|
|
865
|
+
if (value.kind === "function" && value.env) {
|
|
866
|
+
const read = readSymbols(value.declaration, this.checker);
|
|
867
|
+
return {
|
|
868
|
+
...value,
|
|
869
|
+
env: new Map(
|
|
870
|
+
[...value.env]
|
|
871
|
+
.filter(([symbol]) => read.has(symbol))
|
|
872
|
+
.map(([symbol, captured]) => [
|
|
873
|
+
symbol,
|
|
874
|
+
lift(captured, `${base}_${symbol.name}`),
|
|
875
|
+
]),
|
|
876
|
+
),
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
if (value.kind !== "virtual") {
|
|
880
|
+
return value;
|
|
881
|
+
}
|
|
882
|
+
return {
|
|
883
|
+
...value,
|
|
884
|
+
...(value.items && {
|
|
885
|
+
items: value.items.map((item, index) => lift(item, `${base}_${index}`)),
|
|
886
|
+
}),
|
|
887
|
+
...(value.fields && {
|
|
888
|
+
fields: new Map(
|
|
889
|
+
[...value.fields].map(([field, item]) => [
|
|
890
|
+
field,
|
|
891
|
+
lift(item, `${base}_${field}`),
|
|
892
|
+
]),
|
|
893
|
+
),
|
|
894
|
+
}),
|
|
895
|
+
};
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
/**
|
|
899
|
+
* A default setup fixes is a known argument like any other; one read from scan state or an earlier
|
|
900
|
+
* parameter is worked out when the routine starts, as JavaScript does.
|
|
901
|
+
*/
|
|
902
|
+
private bindDefault(
|
|
903
|
+
parameter: ts.ParameterDeclaration | ts.BindingElement,
|
|
904
|
+
initializer: ts.Expression,
|
|
905
|
+
declared: ts.Type,
|
|
906
|
+
frame: Frame,
|
|
907
|
+
parameters: RoutineParameter[],
|
|
908
|
+
keys: Specialization[],
|
|
909
|
+
): void {
|
|
910
|
+
const value = this.expressions.expression(initializer, frame, declared);
|
|
911
|
+
if (value.kind !== "const") {
|
|
912
|
+
this.expressions.bind(parameter.name, value, frame, declared, {
|
|
913
|
+
mutable: value.kind === "code",
|
|
914
|
+
iec: this.layouts.iecName(parameter),
|
|
915
|
+
});
|
|
916
|
+
keys.push({ name: parameter.name.getText(), value: "default" });
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
this.bindParameter(
|
|
920
|
+
parameter.name,
|
|
921
|
+
value,
|
|
922
|
+
declared,
|
|
923
|
+
frame,
|
|
924
|
+
parameters,
|
|
925
|
+
keys,
|
|
926
|
+
);
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
/** A rest parameter is the call's remaining arguments, each a parameter of its own when it is a runtime value. */
|
|
930
|
+
private bindRest(
|
|
931
|
+
parameter: ts.ParameterDeclaration,
|
|
932
|
+
args: readonly Value[],
|
|
933
|
+
declared: ts.Type,
|
|
934
|
+
frame: Frame,
|
|
935
|
+
parameters: RoutineParameter[],
|
|
936
|
+
keys: Specialization[],
|
|
937
|
+
): void {
|
|
938
|
+
if (!ts.isIdentifier(parameter.name)) {
|
|
939
|
+
throw new SpecializeError(
|
|
940
|
+
parameter.name,
|
|
941
|
+
"Name the rest parameter; destructure it in the body.",
|
|
942
|
+
);
|
|
943
|
+
}
|
|
944
|
+
const element =
|
|
945
|
+
this.checker.getIndexTypeOfType(declared, ts.IndexKind.Number) ??
|
|
946
|
+
this.checker.getAnyType();
|
|
947
|
+
const name = parameter.name.text;
|
|
948
|
+
const items = args.map((arg, index): Value => {
|
|
949
|
+
if (arg.kind !== "code" && arg.kind !== "const") {
|
|
950
|
+
keys.push({ name: `${name}[${index}]`, value: arg });
|
|
951
|
+
return arg;
|
|
952
|
+
}
|
|
953
|
+
const local = frame.emitter.local(`${name}_${index}`);
|
|
954
|
+
const iec = arg.kind === "code" ? arg.iec : undefined;
|
|
955
|
+
parameters.push({
|
|
956
|
+
name: local,
|
|
957
|
+
type: iec ?? this.layouts.text(element),
|
|
958
|
+
argument: this.dialect.assignmentText(arg, element, iec),
|
|
959
|
+
});
|
|
960
|
+
keys.push({ name: `${name}[${index}]`, value: "code" });
|
|
961
|
+
return { kind: "code", text: local, declared: element, place: true, iec };
|
|
962
|
+
});
|
|
963
|
+
frame.env.set(symbolOf(this.checker, parameter.name), {
|
|
964
|
+
kind: "virtual",
|
|
965
|
+
items,
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/** A literal holding objects setup built, or only values known now: an argument the routine specializes on. */
|
|
970
|
+
private isKnownShape(value: Value): boolean {
|
|
971
|
+
if (value.kind !== "virtual") {
|
|
972
|
+
return false;
|
|
973
|
+
}
|
|
974
|
+
const inner = [...(value.fields?.values() ?? []), ...(value.items ?? [])];
|
|
975
|
+
return (
|
|
976
|
+
holdsSetupObjects(value) ||
|
|
977
|
+
inner.some((item) => this.isKnownShape(item)) ||
|
|
978
|
+
(value.items !== undefined &&
|
|
979
|
+
inner.every((item) => item.kind === "const"))
|
|
980
|
+
);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/** Where a routine ends: the closing brace of its body, or the whole of a one-expression arrow. */
|
|
985
|
+
export const closingBrace = (declaration: FunctionLike): ts.Node =>
|
|
986
|
+
(declaration.body &&
|
|
987
|
+
ts.isBlock(declaration.body) &&
|
|
988
|
+
declaration.body.getLastToken()) ||
|
|
989
|
+
declaration;
|
|
990
|
+
|
|
991
|
+
/** A specialized routine's result as its callers read it, once its body is specialized. */
|
|
992
|
+
interface RoutineResult {
|
|
993
|
+
layout?: ArrayLayout;
|
|
994
|
+
length?: number;
|
|
995
|
+
returnsStored?: boolean;
|
|
996
|
+
referents?: Referents;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
/** What one parameter or captured name was specialized for: a known value, or how a runtime one is passed. */
|
|
1000
|
+
interface Specialization {
|
|
1001
|
+
name: string;
|
|
1002
|
+
value: Value | string;
|
|
1003
|
+
/**
|
|
1004
|
+
* The IEC width a runtime parameter takes from its argument. Calls at different widths need a
|
|
1005
|
+
* routine each, but the routine's name leaves it out, so names stay as they were.
|
|
1006
|
+
*/
|
|
1007
|
+
width?: string;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/** One parameter of a routine, and what its call passes for it. */
|
|
1011
|
+
interface RoutineParameter {
|
|
1012
|
+
name: string;
|
|
1013
|
+
type: string;
|
|
1014
|
+
argument: string | Mapped;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
/** A routine specialized for one key, and the run-time parts of what it captured that it is passed. */
|
|
1018
|
+
interface Specialized {
|
|
1019
|
+
name: string;
|
|
1020
|
+
parts: ReadonlySet<string>;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/** How a routine is given the runtime values lifted into it. */
|
|
1024
|
+
interface Passing {
|
|
1025
|
+
/** The parameter named from `base` that passes `text`; undefined when the routine reads `text` as it is. */
|
|
1026
|
+
parameter: (
|
|
1027
|
+
text: string | Mapped,
|
|
1028
|
+
base: string,
|
|
1029
|
+
type: () => string,
|
|
1030
|
+
) => string | undefined;
|
|
1031
|
+
/** A caller's local the routine already has a parameter for. */
|
|
1032
|
+
local: (place: string | Mapped) => Value | undefined;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
/** Passes each runtime value lifted into a routine as a parameter of its own, which the caller passes. */
|
|
1036
|
+
const everyValue = (frame: Frame, parameters: RoutineParameter[]) =>
|
|
1037
|
+
({
|
|
1038
|
+
parameter: (text: string | Mapped, base: string, type: () => string) => {
|
|
1039
|
+
const local = frame.emitter.local(base);
|
|
1040
|
+
parameters.push({ name: local, type: type(), argument: text });
|
|
1041
|
+
return local;
|
|
1042
|
+
},
|
|
1043
|
+
local: () => undefined,
|
|
1044
|
+
}) satisfies Passing;
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* A routine parameter is typed as the value passed to it says: the IEC width it carries, else its
|
|
1048
|
+
* declared type as written.
|
|
1049
|
+
*/
|
|
1050
|
+
const parameterTyping: Typing = {
|
|
1051
|
+
width: true,
|
|
1052
|
+
widen: false,
|
|
1053
|
+
untyped: "number",
|
|
1054
|
+
};
|
|
1055
|
+
|
|
1056
|
+
const declarationKey = (declaration: ts.Node): string =>
|
|
1057
|
+
`${declaration.getSourceFile().fileName}:${declaration.pos}`;
|
|
1058
|
+
|
|
1059
|
+
const specializationKey = (
|
|
1060
|
+
{ name, value, width }: Specialization,
|
|
1061
|
+
options: { stable: boolean },
|
|
1062
|
+
): string =>
|
|
1063
|
+
`${name}=${typeof value === "string" ? value : valueKey(value, options)}${!options.stable && width ? `:${width}` : ""}`;
|
|
1064
|
+
|
|
1065
|
+
/**
|
|
1066
|
+
* What tells a value apart to the routine specialized for it. The stable key, which names the
|
|
1067
|
+
* routine, leaves out a callback's source position, so a name doesn't move with an edit above it.
|
|
1068
|
+
*/
|
|
1069
|
+
const valueKey = (value: Value, options: { stable: boolean }): string => {
|
|
1070
|
+
const key = (inner: Value) => valueKey(inner, options);
|
|
1071
|
+
switch (value.kind) {
|
|
1072
|
+
case "static":
|
|
1073
|
+
return `#${value.node.reference}`;
|
|
1074
|
+
case "const":
|
|
1075
|
+
return `=${String(value.value)}`;
|
|
1076
|
+
case "function":
|
|
1077
|
+
return options.stable
|
|
1078
|
+
? "fn"
|
|
1079
|
+
: `fn:${declarationKey(value.declaration)}:${value.self?.id ?? ""}`;
|
|
1080
|
+
case "virtual":
|
|
1081
|
+
return value.items
|
|
1082
|
+
? `[${value.items.map(key).join(",")}]`
|
|
1083
|
+
: `{${[...(value.fields ?? [])].map(([name, inner]) => `${name}:${key(inner)}`).join(",")}}`;
|
|
1084
|
+
case "code":
|
|
1085
|
+
return "code";
|
|
1086
|
+
case "choice":
|
|
1087
|
+
return `ref(${value.options.map((option) => option.reference).join(",")})${value.optional ? "?" : ""}`;
|
|
1088
|
+
case "masked":
|
|
1089
|
+
return `some[${value.entries.map((entry) => `${key(entry.item)}:${key(entry.present)}`).join(",")}]${value.order ? `by ${key(value.order)}` : ""}`;
|
|
1090
|
+
case "counted":
|
|
1091
|
+
return countedKey(value, key);
|
|
1092
|
+
}
|
|
1093
|
+
};
|
|
1094
|
+
|
|
1095
|
+
/** What tells two counted arguments apart to the routine specialized for them. */
|
|
1096
|
+
const countedKey = (
|
|
1097
|
+
counted: Counted,
|
|
1098
|
+
key: (value: Value) => string,
|
|
1099
|
+
): string => {
|
|
1100
|
+
const { source } = counted;
|
|
1101
|
+
const bounds = `${key(counted.start)}..${key(counted.end)}`;
|
|
1102
|
+
return source.kind === "items"
|
|
1103
|
+
? `counted[${source.items.map(key).join(",")}]${bounds}`
|
|
1104
|
+
: `counted:${source.layout}:${key(source.element("0"))}:${bounds}`;
|
|
1105
|
+
};
|
|
1106
|
+
|
|
1107
|
+
/** An element as a routine reads it from the array it was passed: the same slot of its own parameter. */
|
|
1108
|
+
const reread = (
|
|
1109
|
+
sample: Value,
|
|
1110
|
+
array: string,
|
|
1111
|
+
index: string | Mapped,
|
|
1112
|
+
): Value => {
|
|
1113
|
+
const place = mapped`${array}[${index}]`;
|
|
1114
|
+
if (sample.kind === "choice") {
|
|
1115
|
+
return { ...sample, index: place };
|
|
1116
|
+
}
|
|
1117
|
+
return sample.kind === "code" ? { ...sample, text: place } : sample;
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
/** The symbols a function's body reads, which are all of its closure it needs. */
|
|
1121
|
+
const readSymbols = (
|
|
1122
|
+
declaration: ts.Node,
|
|
1123
|
+
checker: ts.TypeChecker,
|
|
1124
|
+
): ReadonlySet<ts.Symbol> => {
|
|
1125
|
+
const read = new Set<ts.Symbol>();
|
|
1126
|
+
const visit = (node: ts.Node): void => {
|
|
1127
|
+
if (ts.isIdentifier(node)) {
|
|
1128
|
+
const symbol = valueSymbol(checker, node);
|
|
1129
|
+
if (symbol) {
|
|
1130
|
+
read.add(symbol);
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
ts.forEachChild(node, visit);
|
|
1134
|
+
};
|
|
1135
|
+
visit(declaration);
|
|
1136
|
+
return read;
|
|
1137
|
+
};
|