@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,240 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CoreFunction,
|
|
3
|
+
type CoreFunctionBlock,
|
|
4
|
+
coreTables,
|
|
5
|
+
type GeneratedObject,
|
|
6
|
+
instanceFields,
|
|
7
|
+
isBlockRoutine,
|
|
8
|
+
standardBlocks,
|
|
9
|
+
} from "@tsplc/compiler/ir";
|
|
10
|
+
import { planGlobalNames } from "./global-names";
|
|
11
|
+
import type { BlockInstrumentation, Instrumentation } from "./instrumentation";
|
|
12
|
+
import { Names } from "./names";
|
|
13
|
+
import { NumericLowering } from "./numeric";
|
|
14
|
+
import type { CodesysPlan } from "./plan";
|
|
15
|
+
import { required } from "./required";
|
|
16
|
+
import { planScanClock } from "./scan-clock";
|
|
17
|
+
import { accessedStorage } from "./task-access";
|
|
18
|
+
|
|
19
|
+
/** The names a routine's ST gives its parameters, locals and the block state it reaches. */
|
|
20
|
+
export interface RoutineScope {
|
|
21
|
+
names: Names;
|
|
22
|
+
references: ReadonlyMap<string, string>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The names fixed before emission: objects of types, blocks, storage lists and routines that hold state. */
|
|
26
|
+
export type GlobalSymbols = ReturnType<typeof globalSymbols>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The target names of a program: its objects, storage, routines and block fields. `emitCodesys`
|
|
30
|
+
* allocates them in emission order, each scope reserving the objects emitted before it, and they
|
|
31
|
+
* are complete before any routine body is written.
|
|
32
|
+
*/
|
|
33
|
+
export type CodesysSymbols = GlobalSymbols & {
|
|
34
|
+
/** The global symbol of each module variable and logical IO. */
|
|
35
|
+
references: ReadonlyMap<string, string>;
|
|
36
|
+
functionNames: ReadonlyMap<string, string>;
|
|
37
|
+
programResults: ReadonlyMap<string, string>;
|
|
38
|
+
mailboxLocks: ReadonlyMap<string, string>;
|
|
39
|
+
blockFields: ReadonlyMap<string, ReadonlyMap<string, string>>;
|
|
40
|
+
routineScopes: ReadonlyMap<string, RoutineScope>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** A plan with the target names and instrumentation allocated for it: what each emitter reads. */
|
|
44
|
+
export interface EmissionContext {
|
|
45
|
+
plan: CodesysPlan;
|
|
46
|
+
symbols: CodesysSymbols;
|
|
47
|
+
instrumentation: Instrumentation;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function globalSymbols(plan: CodesysPlan) {
|
|
51
|
+
const { core } = plan;
|
|
52
|
+
const persistent = new Set(
|
|
53
|
+
core.variables
|
|
54
|
+
.filter((item) => item.storage !== "constant")
|
|
55
|
+
.map((item) => item.id),
|
|
56
|
+
);
|
|
57
|
+
const programRoutines: ReadonlySet<string> = new Set(
|
|
58
|
+
core.functions
|
|
59
|
+
.filter(
|
|
60
|
+
(fn) =>
|
|
61
|
+
!fn.owner &&
|
|
62
|
+
[...accessedStorage(fn, core)].some((id) => persistent.has(id)),
|
|
63
|
+
)
|
|
64
|
+
.map((fn) => fn.id),
|
|
65
|
+
);
|
|
66
|
+
const globalNames = planGlobalNames(core, plan.programNames);
|
|
67
|
+
const scanClock = planScanClock(
|
|
68
|
+
core,
|
|
69
|
+
plan.deployment.tasks,
|
|
70
|
+
globalNames.names,
|
|
71
|
+
);
|
|
72
|
+
return {
|
|
73
|
+
...globalNames,
|
|
74
|
+
blocks: coreTables(core).blocks,
|
|
75
|
+
scanClock,
|
|
76
|
+
numeric: new NumericLowering(
|
|
77
|
+
globalNames.typeMap,
|
|
78
|
+
(label) => globalNames.names.allocate(label),
|
|
79
|
+
scanClock?.now,
|
|
80
|
+
),
|
|
81
|
+
programRoutines,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The object name of each function that is not a method. */
|
|
86
|
+
export function allocateFunctionNames(
|
|
87
|
+
{ core }: CodesysPlan,
|
|
88
|
+
symbols: GlobalSymbols,
|
|
89
|
+
): Map<string, string> {
|
|
90
|
+
const entries = new Set(core.programs.map((program) => program.entry));
|
|
91
|
+
return new Map(
|
|
92
|
+
core.functions
|
|
93
|
+
.filter((fn) => !fn.owner)
|
|
94
|
+
.map((fn) => {
|
|
95
|
+
const program = symbols.programRoutines.has(fn.id);
|
|
96
|
+
const label =
|
|
97
|
+
program && entries.has(fn.id) ? `${fn.name}_body` : fn.name;
|
|
98
|
+
return [
|
|
99
|
+
fn.id,
|
|
100
|
+
symbols.names.allocateObject(program ? "PRG" : "F", label),
|
|
101
|
+
];
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The names a routine's or block's own scope must not shadow: the objects emitted so far, the
|
|
108
|
+
* debug global, and every routine and block named so far.
|
|
109
|
+
*/
|
|
110
|
+
const scopeNames = (
|
|
111
|
+
symbols: Pick<CodesysSymbols, "functionNames" | "blockNames">,
|
|
112
|
+
objects: readonly GeneratedObject[],
|
|
113
|
+
global: string,
|
|
114
|
+
): Names =>
|
|
115
|
+
new Names([
|
|
116
|
+
...objects.map((object) => object.name),
|
|
117
|
+
global,
|
|
118
|
+
...symbols.functionNames.values(),
|
|
119
|
+
...symbols.blockNames.values(),
|
|
120
|
+
]);
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Each block's field names, its methods' names and its mailbox lock, and the instrumentation fields
|
|
124
|
+
* of the blocks that have any. A block's scope reserves the names of the functions that are not
|
|
125
|
+
* methods.
|
|
126
|
+
*/
|
|
127
|
+
export function allocateBlockMembers(
|
|
128
|
+
plan: CodesysPlan,
|
|
129
|
+
symbols: GlobalSymbols & Pick<CodesysSymbols, "functionNames">,
|
|
130
|
+
instrumentation: Pick<Instrumentation, "global" | "analysis">,
|
|
131
|
+
objects: readonly GeneratedObject[],
|
|
132
|
+
) {
|
|
133
|
+
const { core } = plan;
|
|
134
|
+
const fields = new Map<string, ReadonlyMap<string, string>>();
|
|
135
|
+
const methods = new Map<string, string>();
|
|
136
|
+
const mailboxLocks = new Map<string, string>();
|
|
137
|
+
const instrumented = new Map<string, BlockInstrumentation>();
|
|
138
|
+
for (const block of core.blocks) {
|
|
139
|
+
const names = scopeNames(symbols, objects, instrumentation.global);
|
|
140
|
+
for (const method of core.functions.filter(
|
|
141
|
+
(fn) => fn.owner?.block === block.id,
|
|
142
|
+
)) {
|
|
143
|
+
methods.set(method.id, names.allocate(method.name));
|
|
144
|
+
}
|
|
145
|
+
const native = block.standard && standardBlocks[block.standard];
|
|
146
|
+
fields.set(
|
|
147
|
+
block.id,
|
|
148
|
+
new Map(
|
|
149
|
+
instanceFields(block).map((item) => [
|
|
150
|
+
item.id,
|
|
151
|
+
native
|
|
152
|
+
? required(
|
|
153
|
+
[...native.inputs, ...native.outputs].find(
|
|
154
|
+
([name]) => name === item.name,
|
|
155
|
+
),
|
|
156
|
+
)[1]
|
|
157
|
+
: names.allocate(item.name),
|
|
158
|
+
]),
|
|
159
|
+
),
|
|
160
|
+
);
|
|
161
|
+
if (block.runtime?.kind === "mailbox") {
|
|
162
|
+
mailboxLocks.set(block.id, names.allocate("plc_mailbox_lock"));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const settings = plan.project.debug;
|
|
166
|
+
// Trace sites and an unhandled failure need the step a job finished at.
|
|
167
|
+
const stepped =
|
|
168
|
+
instrumentation.analysis.jobSites.has(block.id) ||
|
|
169
|
+
plan.telemetry?.failures.has(block.id);
|
|
170
|
+
const tracked = Boolean(block.job && (settings?.jobs || stepped));
|
|
171
|
+
const carries =
|
|
172
|
+
settings?.jobs && instrumentation.analysis.carriers.has(block.id);
|
|
173
|
+
if (tracked || carries) {
|
|
174
|
+
instrumented.set(block.id, {
|
|
175
|
+
...(tracked && {
|
|
176
|
+
busy: names.allocate("tsplcBusy"),
|
|
177
|
+
track: names.allocate("TsplcTrack"),
|
|
178
|
+
}),
|
|
179
|
+
...(tracked &&
|
|
180
|
+
settings?.jobs && { startedAt: names.allocate("startedAt") }),
|
|
181
|
+
...(stepped && { step: names.allocate("tsplcStep") }),
|
|
182
|
+
...(carries && { job: names.allocate("requestJob") }),
|
|
183
|
+
...(carries &&
|
|
184
|
+
block.runtime?.kind === "http" && {
|
|
185
|
+
sequence: names.allocate("sequence"),
|
|
186
|
+
}),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return { fields, methods, mailboxLocks, instrumented };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The names a routine's ST gives its parameters, locals and block state, and a stateful program's result. */
|
|
194
|
+
export function allocateRoutineScope(
|
|
195
|
+
symbols: GlobalSymbols &
|
|
196
|
+
Pick<CodesysSymbols, "functionNames" | "blockFields">,
|
|
197
|
+
objects: readonly GeneratedObject[],
|
|
198
|
+
global: string,
|
|
199
|
+
fn: CoreFunction | CoreFunctionBlock,
|
|
200
|
+
): { scope: RoutineScope; result?: string } {
|
|
201
|
+
const block = isBlockRoutine(fn) ? fn : undefined;
|
|
202
|
+
const owner = owningBlock(symbols, fn);
|
|
203
|
+
const names = scopeNames(symbols, objects, global);
|
|
204
|
+
const references = new Map<string, string>();
|
|
205
|
+
const result =
|
|
206
|
+
!block && symbols.programRoutines.has(fn.id) && fn.result
|
|
207
|
+
? names.allocate("plc_result")
|
|
208
|
+
: undefined;
|
|
209
|
+
if (owner) {
|
|
210
|
+
for (const item of [...owner.outputs, ...owner.state]) {
|
|
211
|
+
const name = required(
|
|
212
|
+
required(symbols.blockFields.get(owner.id)).get(item.id),
|
|
213
|
+
);
|
|
214
|
+
names.reserve(name);
|
|
215
|
+
references.set(item.id, name);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
for (const item of isBlockRoutine(fn) ? fn.inputs : fn.parameters) {
|
|
219
|
+
const name = block
|
|
220
|
+
? required(required(symbols.blockFields.get(block.id)).get(item.id))
|
|
221
|
+
: names.allocate(item.name);
|
|
222
|
+
names.reserve(name);
|
|
223
|
+
references.set(item.id, name);
|
|
224
|
+
}
|
|
225
|
+
for (const item of fn.locals) {
|
|
226
|
+
references.set(item.id, names.allocate(item.name));
|
|
227
|
+
}
|
|
228
|
+
return { scope: { names, references }, ...(result && { result }) };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** The block whose state a routine reaches: the block itself, or a method's block. */
|
|
232
|
+
export const owningBlock = (
|
|
233
|
+
symbols: GlobalSymbols,
|
|
234
|
+
fn: CoreFunction | CoreFunctionBlock,
|
|
235
|
+
): CoreFunctionBlock | undefined => {
|
|
236
|
+
if (isBlockRoutine(fn)) {
|
|
237
|
+
return fn;
|
|
238
|
+
}
|
|
239
|
+
return fn.owner && required(symbols.blocks.get(fn.owner.block));
|
|
240
|
+
};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CoreExpression,
|
|
3
|
+
CoreFunction,
|
|
4
|
+
CoreFunctionBlock,
|
|
5
|
+
CoreStatement,
|
|
6
|
+
Deployment,
|
|
7
|
+
PlcProgram,
|
|
8
|
+
VerifiedCoreModule,
|
|
9
|
+
} from "@tsplc/compiler/ir";
|
|
10
|
+
import {
|
|
11
|
+
coreTables,
|
|
12
|
+
nestedBodies,
|
|
13
|
+
statementExpressions,
|
|
14
|
+
subexpressions,
|
|
15
|
+
} from "@tsplc/compiler/ir";
|
|
16
|
+
import type { Report } from "./plan";
|
|
17
|
+
|
|
18
|
+
/** The task that runs each function and block; undefined for one more than one task runs. */
|
|
19
|
+
export function routineTasks(
|
|
20
|
+
core: VerifiedCoreModule,
|
|
21
|
+
deployment: Deployment,
|
|
22
|
+
): Map<string, string | undefined> {
|
|
23
|
+
const tasks = new Map<string, string | undefined>();
|
|
24
|
+
const { functionOf, calledRoutineOf } = coreTables(core);
|
|
25
|
+
const programs = new Map(core.programs.map((item) => [item.name, item]));
|
|
26
|
+
for (const task of deployment.tasks) {
|
|
27
|
+
for (const name of task.programs) {
|
|
28
|
+
// `validateTasks` reports a program the module lacks.
|
|
29
|
+
const program = programs.get(name);
|
|
30
|
+
if (!program) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const seen = new Set<string>();
|
|
34
|
+
const visit = (routine: CoreFunction | CoreFunctionBlock): void => {
|
|
35
|
+
if (seen.has(routine.id)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
seen.add(routine.id);
|
|
39
|
+
tasks.set(
|
|
40
|
+
routine.id,
|
|
41
|
+
tasks.has(routine.id) && tasks.get(routine.id) !== task.name
|
|
42
|
+
? undefined
|
|
43
|
+
: task.name,
|
|
44
|
+
);
|
|
45
|
+
const inspect = (items: readonly CoreStatement[]): void => {
|
|
46
|
+
for (const item of items) {
|
|
47
|
+
const called = calledRoutineOf(item);
|
|
48
|
+
if (called) {
|
|
49
|
+
visit(called);
|
|
50
|
+
}
|
|
51
|
+
for (const body of nestedBodies(item)) {
|
|
52
|
+
inspect(body);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
inspect(routine.body);
|
|
57
|
+
};
|
|
58
|
+
visit(functionOf(program.entry));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return tasks;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The generated worker may enter its host, and only mailbox roots are shared between tasks. */
|
|
65
|
+
export function taskStorageOwnership(
|
|
66
|
+
core: VerifiedCoreModule,
|
|
67
|
+
report: Report,
|
|
68
|
+
): (task: string, program: PlcProgram) => void {
|
|
69
|
+
const owners = new Map<string, string>();
|
|
70
|
+
const globals = new Map(core.variables.map((item) => [item.id, item]));
|
|
71
|
+
const { types, functionOf, blockOf } = coreTables(core);
|
|
72
|
+
const runtimeOf = (type: string) => {
|
|
73
|
+
const block = types.get(type);
|
|
74
|
+
return block?.kind === "block"
|
|
75
|
+
? blockOf(block.block).runtime?.kind
|
|
76
|
+
: undefined;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return (task, program) => {
|
|
80
|
+
const fn = functionOf(program.entry);
|
|
81
|
+
const worker = program.background?.worker;
|
|
82
|
+
const host = worker
|
|
83
|
+
? core.variables.find(
|
|
84
|
+
(item) =>
|
|
85
|
+
item.name === worker &&
|
|
86
|
+
item.span.file === fn.span.file &&
|
|
87
|
+
runtimeOf(item.type) === "backgroundHost",
|
|
88
|
+
)
|
|
89
|
+
: undefined;
|
|
90
|
+
for (const id of storageAccess(fn, core, host?.id ?? null)) {
|
|
91
|
+
const item = globals.get(id);
|
|
92
|
+
if (
|
|
93
|
+
!item ||
|
|
94
|
+
item.storage === "constant" ||
|
|
95
|
+
runtimeOf(item.type) === "mailbox"
|
|
96
|
+
) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const ownsWorker =
|
|
100
|
+
worker === item.name && item.span.file === fn.span.file;
|
|
101
|
+
if (program.background && (!ownsWorker || item.id === host?.id)) {
|
|
102
|
+
report(
|
|
103
|
+
`Background task ${program.name} accesses scan-owned storage ${item.name}; pass value snapshots through the mailbox.`,
|
|
104
|
+
item.span,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
const owner = owners.get(id);
|
|
108
|
+
if (owner && owner !== task) {
|
|
109
|
+
report(
|
|
110
|
+
`Storage ${item.name} is shared across cyclic tasks ${owner} and ${task}.`,
|
|
111
|
+
item.span,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
owners.set(id, task);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Follow both helper calls and instance updates. Borrow addresses count as storage
|
|
120
|
+
// access, so scheduling cannot accidentally give one mutable record two owners.
|
|
121
|
+
export const accessedStorage = (
|
|
122
|
+
fn: CoreFunction,
|
|
123
|
+
core: VerifiedCoreModule,
|
|
124
|
+
): Set<string> => storageAccess(fn, core, null);
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The storage a function reaches, where `scheduledHost` is the background host whose worker call
|
|
128
|
+
* the generated entry makes: only that call may borrow its host. Ordinary calls reached through
|
|
129
|
+
* the async body still count as access to scan-owned storage.
|
|
130
|
+
*/
|
|
131
|
+
function storageAccess(
|
|
132
|
+
fn: CoreFunction,
|
|
133
|
+
core: VerifiedCoreModule,
|
|
134
|
+
scheduledHost: string | null,
|
|
135
|
+
): Set<string> {
|
|
136
|
+
const scheduledCall =
|
|
137
|
+
scheduledHost === null
|
|
138
|
+
? undefined
|
|
139
|
+
: fn.body.find(
|
|
140
|
+
(item) => item.kind === "invoke" && item.instance === scheduledHost,
|
|
141
|
+
);
|
|
142
|
+
const result = new Set<string>();
|
|
143
|
+
const seen = new Set<string>();
|
|
144
|
+
const { calledRoutineOf } = coreTables(core);
|
|
145
|
+
|
|
146
|
+
const expression = (value: CoreExpression): void => {
|
|
147
|
+
if (value.kind === "load") {
|
|
148
|
+
result.add(value.root);
|
|
149
|
+
}
|
|
150
|
+
if (value.kind === "borrow") {
|
|
151
|
+
result.add(value.place.root);
|
|
152
|
+
}
|
|
153
|
+
subexpressions(value).forEach(expression);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const body = (items: readonly CoreStatement[]): void => {
|
|
157
|
+
for (const item of items) {
|
|
158
|
+
if (item.kind === "assign") {
|
|
159
|
+
result.add(item.root);
|
|
160
|
+
}
|
|
161
|
+
if (item.kind === "invoke" && item.instance && item !== scheduledCall) {
|
|
162
|
+
result.add(item.instance);
|
|
163
|
+
}
|
|
164
|
+
if (item.kind === "blockUpdate") {
|
|
165
|
+
result.add(item.instance);
|
|
166
|
+
}
|
|
167
|
+
statementExpressions(item).forEach(expression);
|
|
168
|
+
if (item.kind === "for") {
|
|
169
|
+
result.add(item.counter);
|
|
170
|
+
}
|
|
171
|
+
if (item.kind === "invoke" && item.destination) {
|
|
172
|
+
result.add(item.destination);
|
|
173
|
+
}
|
|
174
|
+
const called = calledRoutineOf(item);
|
|
175
|
+
if (called && !seen.has(called.id)) {
|
|
176
|
+
seen.add(called.id);
|
|
177
|
+
body(called.body);
|
|
178
|
+
}
|
|
179
|
+
nestedBodies(item).forEach(body);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
seen.add(fn.id);
|
|
184
|
+
body(fn.body);
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type ResolvedTelemetryConfig, telemetrySchema } from "@tsplc/config";
|
|
2
|
+
|
|
3
|
+
/** How long a sender waits for a full batch before it sends what it has, and before it retries. */
|
|
4
|
+
export const telemetrySendIntervalMs = 200;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The configured telemetry settings or their defaults, with rings that also hold `debug.trace`'s
|
|
8
|
+
* records: each task has one ring for both.
|
|
9
|
+
*/
|
|
10
|
+
export const ringSettings = (
|
|
11
|
+
telemetry: ResolvedTelemetryConfig | undefined,
|
|
12
|
+
trace: { capacity: number } | false | undefined,
|
|
13
|
+
): ResolvedTelemetryConfig => {
|
|
14
|
+
const settings = telemetry ?? telemetrySchema.parse({});
|
|
15
|
+
return {
|
|
16
|
+
...settings,
|
|
17
|
+
capacity: Math.max(settings.capacity, trace ? trace.capacity : 0),
|
|
18
|
+
};
|
|
19
|
+
};
|