@tsplc/compiler 0.2.16 → 0.2.18
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 +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
import { instancedBlock, nestedBodies, telemetryOperands } from "@tsplc/compiler/ir";
|
|
2
|
-
import type {
|
|
3
|
-
CoreExpression,
|
|
4
|
-
CoreFunction,
|
|
5
|
-
CoreFunctionBlock,
|
|
6
|
-
CorePath,
|
|
7
|
-
CoreStatement,
|
|
8
|
-
Deployment,
|
|
9
|
-
PlcProgram,
|
|
10
|
-
SourceSpan,
|
|
11
|
-
Task,
|
|
12
|
-
VerifiedCoreModule,
|
|
13
|
-
} from "@tsplc/compiler/ir";
|
|
14
|
-
|
|
15
|
-
type Report = (message: string, span?: SourceSpan) => void;
|
|
16
|
-
|
|
17
|
-
/** The task that runs each function and block; undefined for one more than one task runs. */
|
|
18
|
-
export function routineTasks(
|
|
19
|
-
core: VerifiedCoreModule,
|
|
20
|
-
deployment: Deployment,
|
|
21
|
-
): Map<string, string | undefined> {
|
|
22
|
-
const tasks = new Map<string, string | undefined>();
|
|
23
|
-
const types = new Map(core.types.map((type) => [type.id, type]));
|
|
24
|
-
const routines = new Map<string, CoreFunction | CoreFunctionBlock>(
|
|
25
|
-
[...core.functions, ...core.blocks].map((routine) => [routine.id, routine]),
|
|
26
|
-
);
|
|
27
|
-
const variables = new Map(
|
|
28
|
-
[
|
|
29
|
-
...core.variables,
|
|
30
|
-
...core.functions.flatMap((fn) => [...fn.parameters, ...fn.locals]),
|
|
31
|
-
...core.blocks.flatMap((block) => [
|
|
32
|
-
...block.inputs,
|
|
33
|
-
...block.outputs,
|
|
34
|
-
...block.state,
|
|
35
|
-
...block.locals,
|
|
36
|
-
]),
|
|
37
|
-
].map((variable) => [variable.id, variable]),
|
|
38
|
-
);
|
|
39
|
-
const programs = new Map(core.programs.map((program) => [program.name, program]));
|
|
40
|
-
for (const task of deployment.tasks)
|
|
41
|
-
for (const name of task.programs) {
|
|
42
|
-
const seen = new Set<string>();
|
|
43
|
-
const visit = (id: string): void => {
|
|
44
|
-
if (seen.has(id)) return;
|
|
45
|
-
seen.add(id);
|
|
46
|
-
tasks.set(id, tasks.has(id) && tasks.get(id) !== task.name ? undefined : task.name);
|
|
47
|
-
const inspect = (items: readonly CoreStatement[]): void => {
|
|
48
|
-
for (const item of items) {
|
|
49
|
-
if (item.kind === "invoke") visit(item.function);
|
|
50
|
-
if (item.kind === "blockUpdate") {
|
|
51
|
-
const block = instancedBlock(variables.get(item.instance)!.type, types);
|
|
52
|
-
if (block) visit(block.block);
|
|
53
|
-
}
|
|
54
|
-
for (const body of nestedBodies(item)) inspect(body);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
inspect(routines.get(id)?.body ?? []);
|
|
58
|
-
};
|
|
59
|
-
visit(programs.get(name)!.entry);
|
|
60
|
-
}
|
|
61
|
-
return tasks;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function schedulePrograms(
|
|
65
|
-
core: VerifiedCoreModule,
|
|
66
|
-
tasks: readonly Task[],
|
|
67
|
-
report: Report,
|
|
68
|
-
): readonly Task[] {
|
|
69
|
-
const background = new Map<string, Task & { programs: string[] }>();
|
|
70
|
-
for (const program of core.programs) {
|
|
71
|
-
if (!program.background) continue;
|
|
72
|
-
const { priority, intervalNanoseconds } = program.background;
|
|
73
|
-
const name = `TsplcBackground_${priority}_${BigInt(intervalNanoseconds) / 1_000_000n}`;
|
|
74
|
-
const existing = background.get(name);
|
|
75
|
-
if (existing) {
|
|
76
|
-
existing.programs.push(program.name);
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
background.set(name, {
|
|
80
|
-
name,
|
|
81
|
-
priority,
|
|
82
|
-
intervalNanoseconds,
|
|
83
|
-
programs: [program.name],
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
return appendContinuations(core, [...tasks, ...background.values()], report);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** The generated worker may enter its host, and only mailbox roots are shared between tasks. */
|
|
90
|
-
export function taskStorageOwnership(
|
|
91
|
-
core: VerifiedCoreModule,
|
|
92
|
-
report: Report,
|
|
93
|
-
): (task: string, program: PlcProgram) => void {
|
|
94
|
-
const owners = new Map<string, string>();
|
|
95
|
-
const globals = new Map(core.variables.map((item) => [item.id, item]));
|
|
96
|
-
const functions = new Map(core.functions.map((item) => [item.id, item]));
|
|
97
|
-
const blocks = new Map(core.blocks.map((block) => [block.id, block.runtime?.kind]));
|
|
98
|
-
const runtimes = new Map(
|
|
99
|
-
core.types.flatMap((type) =>
|
|
100
|
-
type.kind === "block" ? [[type.id, blocks.get(type.block)] as const] : [],
|
|
101
|
-
),
|
|
102
|
-
);
|
|
103
|
-
|
|
104
|
-
return (task, program) => {
|
|
105
|
-
const fn = functions.get(program.entry);
|
|
106
|
-
if (!fn) throw new Error("Verified core has a missing entry.");
|
|
107
|
-
const worker = program.background?.worker;
|
|
108
|
-
const host =
|
|
109
|
-
worker &&
|
|
110
|
-
core.variables.find(
|
|
111
|
-
(item) =>
|
|
112
|
-
item.name === worker &&
|
|
113
|
-
item.span.file === fn.span.file &&
|
|
114
|
-
runtimes.get(item.type) === "backgroundHost",
|
|
115
|
-
);
|
|
116
|
-
for (const id of accessedStorage(fn, core, host ? host.id : undefined)) {
|
|
117
|
-
const item = globals.get(id);
|
|
118
|
-
if (!item || item.storage === "constant" || runtimes.get(item.type) === "mailbox") continue;
|
|
119
|
-
const ownsWorker = worker === item.name && item.span.file === fn.span.file;
|
|
120
|
-
if (program.background && (!ownsWorker || (host && item.id === host.id)))
|
|
121
|
-
report(
|
|
122
|
-
`Background task ${program.name} accesses scan-owned storage ${item.name}; pass value snapshots through the mailbox.`,
|
|
123
|
-
item.span,
|
|
124
|
-
);
|
|
125
|
-
const owner = owners.get(id);
|
|
126
|
-
if (owner && owner !== task)
|
|
127
|
-
report(
|
|
128
|
-
`Storage ${item.name} is shared across cyclic tasks ${owner} and ${task}.`,
|
|
129
|
-
item.span,
|
|
130
|
-
);
|
|
131
|
-
owners.set(id, task);
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// Follow both helper calls and instance updates. Borrow addresses count as storage
|
|
137
|
-
// access, so scheduling cannot accidentally give one mutable record two owners.
|
|
138
|
-
export function accessedStorage(
|
|
139
|
-
fn: CoreFunction,
|
|
140
|
-
core: VerifiedCoreModule,
|
|
141
|
-
scheduledHost?: string,
|
|
142
|
-
): Set<string> {
|
|
143
|
-
// Only the generated entry's worker call may borrow its host. Ordinary calls
|
|
144
|
-
// reached through the async body still count as access to scan-owned storage.
|
|
145
|
-
const scheduledCall = fn.body.find(
|
|
146
|
-
(item) => item.kind === "invoke" && item.instance === scheduledHost,
|
|
147
|
-
);
|
|
148
|
-
const result = new Set<string>(),
|
|
149
|
-
seen = new Set<string>();
|
|
150
|
-
const functions = new Map(core.functions.map((item) => [item.id, item]));
|
|
151
|
-
const blocks = new Map(core.blocks.map((item) => [item.id, item]));
|
|
152
|
-
const types = new Map(core.types.map((item) => [item.id, item]));
|
|
153
|
-
const variables = new Map(
|
|
154
|
-
[
|
|
155
|
-
...core.variables,
|
|
156
|
-
...core.functions.flatMap((item) => [...item.parameters, ...item.locals]),
|
|
157
|
-
...core.blocks.flatMap((item) => [
|
|
158
|
-
...item.inputs,
|
|
159
|
-
...item.outputs,
|
|
160
|
-
...item.state,
|
|
161
|
-
...item.locals,
|
|
162
|
-
]),
|
|
163
|
-
].map((item) => [item.id, item]),
|
|
164
|
-
);
|
|
165
|
-
|
|
166
|
-
const path = (segments: CorePath = []): void => {
|
|
167
|
-
segments.forEach((segment) => {
|
|
168
|
-
if (segment.kind === "index") expression(segment.value);
|
|
169
|
-
});
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
const expression = (value: CoreExpression): void => {
|
|
173
|
-
if (value.kind === "load") {
|
|
174
|
-
result.add(value.root);
|
|
175
|
-
path(value.path);
|
|
176
|
-
}
|
|
177
|
-
if (value.kind === "borrow") {
|
|
178
|
-
result.add(value.place.root);
|
|
179
|
-
path(value.place.path);
|
|
180
|
-
}
|
|
181
|
-
if (value.kind === "unary" || value.kind === "convert") expression(value.operand);
|
|
182
|
-
if (value.kind === "binary") {
|
|
183
|
-
expression(value.left);
|
|
184
|
-
expression(value.right);
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
const body = (items: readonly CoreStatement[]): void => {
|
|
189
|
-
for (const item of items) {
|
|
190
|
-
if (item.kind === "assign") {
|
|
191
|
-
result.add(item.root);
|
|
192
|
-
path(item.path);
|
|
193
|
-
expression(item.value);
|
|
194
|
-
}
|
|
195
|
-
if (item.kind === "if") {
|
|
196
|
-
expression(item.condition);
|
|
197
|
-
body(item.then);
|
|
198
|
-
body(item.else);
|
|
199
|
-
}
|
|
200
|
-
if (item.kind === "switch") {
|
|
201
|
-
expression(item.value);
|
|
202
|
-
for (const branch of item.cases) body(branch.body);
|
|
203
|
-
body(item.fallback);
|
|
204
|
-
}
|
|
205
|
-
if (item.kind === "for") {
|
|
206
|
-
if (item.dynamic) {
|
|
207
|
-
expression(item.dynamic.start);
|
|
208
|
-
expression(item.dynamic.limit);
|
|
209
|
-
}
|
|
210
|
-
result.add(item.counter);
|
|
211
|
-
body(item.body);
|
|
212
|
-
}
|
|
213
|
-
if (item.kind === "return" && item.value) expression(item.value);
|
|
214
|
-
if (item.kind === "telemetry") telemetryOperands(item).forEach(expression);
|
|
215
|
-
if (item.kind === "invoke") {
|
|
216
|
-
if (item.instance && item !== scheduledCall) result.add(item.instance);
|
|
217
|
-
if (item.index) expression(item.index);
|
|
218
|
-
item.arguments.forEach(expression);
|
|
219
|
-
if (item.destination) result.add(item.destination);
|
|
220
|
-
const called = functions.get(item.function);
|
|
221
|
-
if (called && !seen.has(called.id)) {
|
|
222
|
-
seen.add(called.id);
|
|
223
|
-
body(called.body);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
if (item.kind === "blockUpdate") {
|
|
227
|
-
result.add(item.instance);
|
|
228
|
-
if (item.index) expression(item.index);
|
|
229
|
-
Object.values(item.inputs).forEach(expression);
|
|
230
|
-
const variable = variables.get(item.instance);
|
|
231
|
-
if (!variable) throw new Error("Missing verified instance during task planning.");
|
|
232
|
-
const type = instancedBlock(variable.type, types);
|
|
233
|
-
const block = type && blocks.get(type.block);
|
|
234
|
-
if (block && !seen.has(block.id)) {
|
|
235
|
-
seen.add(block.id);
|
|
236
|
-
body(block.body);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
seen.add(fn.id);
|
|
243
|
-
body(fn.body);
|
|
244
|
-
return result;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/** Detached continuations run after their caller programs in the same cyclic task. */
|
|
248
|
-
function appendContinuations(
|
|
249
|
-
core: VerifiedCoreModule,
|
|
250
|
-
tasks: readonly Task[],
|
|
251
|
-
report: Report,
|
|
252
|
-
): readonly Task[] {
|
|
253
|
-
const continuations = core.programs.flatMap((program) =>
|
|
254
|
-
program.continuation ? [{ name: program.name, scope: program.continuation }] : [],
|
|
255
|
-
);
|
|
256
|
-
if (!continuations.length) return tasks;
|
|
257
|
-
|
|
258
|
-
const programs = new Map(core.programs.map((program) => [program.name, program]));
|
|
259
|
-
const functions = new Map(core.functions.map((routine) => [routine.id, routine]));
|
|
260
|
-
const placements = tasks.map((task): { task: Task; roots: Set<string>; additions: string[] } => {
|
|
261
|
-
const roots = new Set<string>();
|
|
262
|
-
for (const name of task.programs) {
|
|
263
|
-
const program = programs.get(name);
|
|
264
|
-
if (!program || program.background) continue;
|
|
265
|
-
const entry = functions.get(program.entry);
|
|
266
|
-
if (entry) for (const id of accessedStorage(entry, core)) roots.add(id);
|
|
267
|
-
}
|
|
268
|
-
return { task, roots, additions: [] };
|
|
269
|
-
});
|
|
270
|
-
for (const { name, scope } of continuations) {
|
|
271
|
-
const root = core.variables.find(
|
|
272
|
-
(variable) => variable.name === scope.instance && variable.span.file === scope.file,
|
|
273
|
-
);
|
|
274
|
-
if (!root) {
|
|
275
|
-
report(`Missing continuation owner ${scope.instance}.`);
|
|
276
|
-
continue;
|
|
277
|
-
}
|
|
278
|
-
const owners = placements.filter(({ roots }) => roots.has(root.id));
|
|
279
|
-
const [owner] = owners;
|
|
280
|
-
if (!owner || owners.length !== 1) {
|
|
281
|
-
report(
|
|
282
|
-
`Continuation owner ${scope.instance} must belong to exactly one caller task.`,
|
|
283
|
-
root.span,
|
|
284
|
-
);
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
owner.additions.push(name);
|
|
288
|
-
}
|
|
289
|
-
return placements.map(({ task, additions }) => ({
|
|
290
|
-
...task,
|
|
291
|
-
programs: task.programs.concat(additions),
|
|
292
|
-
}));
|
|
293
|
-
}
|