@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
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 +6563 -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 +16198 -0
- package/dist/index.js.map +62 -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 +1090 -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-enums.ts +196 -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 +1344 -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 +2177 -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 +612 -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 +659 -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,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
coreTables,
|
|
3
|
+
type GeneratedObject,
|
|
4
|
+
type PlcProgram,
|
|
5
|
+
type Task,
|
|
6
|
+
} from "@tsplc/compiler/ir";
|
|
7
|
+
import type { IoChannel } from "./global-lists";
|
|
8
|
+
import type { CodesysPlan } from "./plan";
|
|
9
|
+
import { required } from "./required";
|
|
10
|
+
import { latchScanClock, restoreScanClock, runtimeClock } from "./scan-clock";
|
|
11
|
+
import { noResult } from "./statements";
|
|
12
|
+
import type { EmissionContext } from "./symbols";
|
|
13
|
+
import { accessedStorage } from "./task-access";
|
|
14
|
+
|
|
15
|
+
/** The PROGRAM a task calls for a program: its IO scan around a call of the authored routine. */
|
|
16
|
+
export function emitScanProgram(
|
|
17
|
+
{ plan, symbols, instrumentation }: EmissionContext,
|
|
18
|
+
ioChannels: readonly IoChannel[],
|
|
19
|
+
program: PlcProgram,
|
|
20
|
+
): GeneratedObject {
|
|
21
|
+
const { core } = plan;
|
|
22
|
+
const name =
|
|
23
|
+
plan.programNames.get(program.name) ??
|
|
24
|
+
symbols.names.allocateObject("PRG", program.name);
|
|
25
|
+
const used = accessedStorage(
|
|
26
|
+
coreTables(core).functionOf(program.entry),
|
|
27
|
+
core,
|
|
28
|
+
);
|
|
29
|
+
const channels = ioChannels.filter((channel) => used.has(channel.root));
|
|
30
|
+
const stateful = symbols.programRoutines.has(program.entry);
|
|
31
|
+
const call = `${required(symbols.functionNames.get(program.entry))}();`;
|
|
32
|
+
// A task's first program starts its cycle: it reads the scan's clock before anything else runs,
|
|
33
|
+
// then a task with a ring runs its sender, which counts the scan.
|
|
34
|
+
const task = plan.deployment.tasks.find(
|
|
35
|
+
(item) => item.programs[0] === program.name,
|
|
36
|
+
);
|
|
37
|
+
const ring = task && instrumentation.rings.get(task.name);
|
|
38
|
+
const clock = task && instrumentation.scanClocks.get(task.name);
|
|
39
|
+
// The scan snapshots physical inputs before the authored routine and commits outputs
|
|
40
|
+
// afterwards. RETURN in an authored routine cannot skip that epilogue.
|
|
41
|
+
const lines = [
|
|
42
|
+
...(task && symbols.scanClock
|
|
43
|
+
? latchScanClock(symbols.scanClock, task.name)
|
|
44
|
+
: []),
|
|
45
|
+
...(ring ? [`${instrumentation.global}.${ring}();`] : []),
|
|
46
|
+
...(clock
|
|
47
|
+
? [
|
|
48
|
+
`${instrumentation.global}.${clock} := ${symbols.scanClock?.now ?? runtimeClock};`,
|
|
49
|
+
]
|
|
50
|
+
: []),
|
|
51
|
+
...channels
|
|
52
|
+
.filter((channel) => channel.input)
|
|
53
|
+
.map(
|
|
54
|
+
({ target, wire, inverted }) =>
|
|
55
|
+
`${target} := ${inverted ? "NOT " : ""}${wire};`,
|
|
56
|
+
),
|
|
57
|
+
stateful ? call : `plc_discard := ${call}`,
|
|
58
|
+
...channels
|
|
59
|
+
.filter((channel) => !channel.input)
|
|
60
|
+
.map(({ target, wire }) => `${wire} := ${target};`),
|
|
61
|
+
];
|
|
62
|
+
return {
|
|
63
|
+
id: program.id,
|
|
64
|
+
kind: "program",
|
|
65
|
+
name,
|
|
66
|
+
declaration: stateful
|
|
67
|
+
? `PROGRAM ${name}\n`
|
|
68
|
+
: `PROGRAM ${name}\nVAR\n plc_discard : ${noResult.type};\nEND_VAR\n`,
|
|
69
|
+
implementation: `${lines.join("\n")}\n`,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* How a task's program list splits: the programs its configuration names, which it calls itself,
|
|
75
|
+
* and the ones the compiler adds (detached continuations, background jobs), which it calls through
|
|
76
|
+
* one wrapper. A task's configuration then depends on the project's configuration alone, so adding
|
|
77
|
+
* a continuation or a background job at an existing interval changes code, which an online change
|
|
78
|
+
* applies, never the task configuration, which it refuses.
|
|
79
|
+
*/
|
|
80
|
+
export const taskPrograms = (plan: CodesysPlan, task: Task) => {
|
|
81
|
+
const programs = new Map(
|
|
82
|
+
plan.core.programs.map((program) => [program.name, program]),
|
|
83
|
+
);
|
|
84
|
+
const generated = (name: string) => {
|
|
85
|
+
const program = programs.get(name);
|
|
86
|
+
return Boolean(program?.continuation || program?.background);
|
|
87
|
+
};
|
|
88
|
+
const background = task.programs.every(
|
|
89
|
+
(name) => programs.get(name)?.background,
|
|
90
|
+
);
|
|
91
|
+
return {
|
|
92
|
+
configured: task.programs.filter((name) => !generated(name)),
|
|
93
|
+
wrapped: task.programs.filter(generated),
|
|
94
|
+
wrapper: {
|
|
95
|
+
id: `tsplc:task:${task.name}`,
|
|
96
|
+
label: background ? task.name : `TsplcContinuations_${task.name}`,
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Each task's wrapper PROGRAM, which calls the programs the compiler added to it in their order.
|
|
103
|
+
* It is the last program of the task's cycle, so it ends the scan's clock.
|
|
104
|
+
*/
|
|
105
|
+
export function emitTaskWrappers(
|
|
106
|
+
{ plan, symbols }: EmissionContext,
|
|
107
|
+
objects: readonly GeneratedObject[],
|
|
108
|
+
): GeneratedObject[] {
|
|
109
|
+
const programObjects = new Map(
|
|
110
|
+
plan.core.programs.map((program) => [
|
|
111
|
+
program.name,
|
|
112
|
+
required(
|
|
113
|
+
objects.find(
|
|
114
|
+
(object) => object.id === program.id && object.kind === "program",
|
|
115
|
+
),
|
|
116
|
+
),
|
|
117
|
+
]),
|
|
118
|
+
);
|
|
119
|
+
return plan.deployment.tasks.map((task) => {
|
|
120
|
+
const { wrapped, wrapper } = taskPrograms(plan, task);
|
|
121
|
+
const name = symbols.names.allocateObject("PRG", wrapper.label);
|
|
122
|
+
const calls = [
|
|
123
|
+
...wrapped.map(
|
|
124
|
+
(program) => `${required(programObjects.get(program)).name}();`,
|
|
125
|
+
),
|
|
126
|
+
...(symbols.scanClock
|
|
127
|
+
? restoreScanClock(symbols.scanClock, task.name)
|
|
128
|
+
: []),
|
|
129
|
+
];
|
|
130
|
+
return {
|
|
131
|
+
id: wrapper.id,
|
|
132
|
+
kind: "program",
|
|
133
|
+
name,
|
|
134
|
+
declaration: `PROGRAM ${name}\n`,
|
|
135
|
+
implementation: calls.length ? `${calls.join("\n")}\n` : ";\n",
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { isLogicalIo, type VerifiedCoreModule } from "@tsplc/compiler/ir";
|
|
2
|
+
import type { Publication } from "@tsplc/config";
|
|
3
|
+
import type { ServiceChannel } from "@tsplc/server/wire";
|
|
4
|
+
import { required } from "./required";
|
|
5
|
+
import { moduleFile, moduleName, storagePath } from "./storage";
|
|
6
|
+
import type { EmissionContext } from "./symbols";
|
|
7
|
+
|
|
8
|
+
const globals = (core: VerifiedCoreModule) =>
|
|
9
|
+
core.variables.filter(
|
|
10
|
+
(variable) => variable.storage === "module" && variable.exported,
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
/** Source names are independent of CODESYS name allocation and the active build target. */
|
|
14
|
+
export const publicationSelectors = (core: VerifiedCoreModule): string[] => {
|
|
15
|
+
const variables = globals(core);
|
|
16
|
+
return [
|
|
17
|
+
...new Set(
|
|
18
|
+
variables.flatMap((variable) => [
|
|
19
|
+
moduleName(variable),
|
|
20
|
+
storagePath(variable),
|
|
21
|
+
]),
|
|
22
|
+
),
|
|
23
|
+
].sort();
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const resolvePublication = (
|
|
27
|
+
core: VerifiedCoreModule,
|
|
28
|
+
policy: Publication,
|
|
29
|
+
): { publication: Publication; errors: string[] } => {
|
|
30
|
+
const variables = globals(core);
|
|
31
|
+
const errors: string[] = [];
|
|
32
|
+
const select = (selectors: readonly string[]) => {
|
|
33
|
+
const selected = new Set<string>();
|
|
34
|
+
const seen = new Set<string>();
|
|
35
|
+
for (const selector of selectors) {
|
|
36
|
+
if (seen.has(selector)) {
|
|
37
|
+
errors.push(`Duplicate publication selector ${selector}.`);
|
|
38
|
+
}
|
|
39
|
+
seen.add(selector);
|
|
40
|
+
const candidates = variables.filter(
|
|
41
|
+
(variable) =>
|
|
42
|
+
selector === moduleName(variable) ||
|
|
43
|
+
selector === storagePath(variable),
|
|
44
|
+
);
|
|
45
|
+
if (!candidates.length) {
|
|
46
|
+
errors.push(
|
|
47
|
+
`Unknown publication selector ${selector}; select an exported state root or a field of one, such as counter or counter.total.`,
|
|
48
|
+
);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (new Set(candidates.map(({ id }) => moduleFile(id))).size !== 1) {
|
|
52
|
+
errors.push(`Ambiguous publication selector ${selector}.`);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
for (const variable of candidates) {
|
|
56
|
+
selected.add(variable.id);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return variables
|
|
60
|
+
.filter((variable) => selected.has(variable.id))
|
|
61
|
+
.map((variable) => variable.id);
|
|
62
|
+
};
|
|
63
|
+
const publication = {
|
|
64
|
+
production: select(policy.production),
|
|
65
|
+
development: select(policy.development),
|
|
66
|
+
};
|
|
67
|
+
return { publication, errors };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** The global symbols a build publishes for each target: its selected storage, logical IO and service channels. */
|
|
71
|
+
export const publishedSymbols = (
|
|
72
|
+
{ plan, symbols }: EmissionContext,
|
|
73
|
+
rpcChannels: readonly ServiceChannel[],
|
|
74
|
+
): Publication => {
|
|
75
|
+
const logicalIo = plan.core.variables
|
|
76
|
+
.filter(isLogicalIo)
|
|
77
|
+
.map((variable) => variable.id);
|
|
78
|
+
const rpcSymbols = [
|
|
79
|
+
...new Set(
|
|
80
|
+
rpcChannels.flatMap((channel) =>
|
|
81
|
+
[
|
|
82
|
+
channel.request,
|
|
83
|
+
channel.requestId,
|
|
84
|
+
channel.reply,
|
|
85
|
+
channel.replyId,
|
|
86
|
+
channel.epoch,
|
|
87
|
+
].map((path) => path.replace(/\[\d+\]$/, "")),
|
|
88
|
+
),
|
|
89
|
+
),
|
|
90
|
+
];
|
|
91
|
+
const published = (ids: readonly string[]) =>
|
|
92
|
+
[...ids, ...logicalIo]
|
|
93
|
+
.map((id) => required(symbols.references.get(id)))
|
|
94
|
+
.concat(rpcSymbols);
|
|
95
|
+
return {
|
|
96
|
+
production: published(plan.publication.production),
|
|
97
|
+
development: published(plan.publication.development),
|
|
98
|
+
};
|
|
99
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// All symbol relationships were checked by normalization and planning. Failure
|
|
2
|
+
// here is an emitter invariant violation, rather than a recoverable source error.
|
|
3
|
+
export function required<T>(value: T | undefined): T {
|
|
4
|
+
if (value === undefined) {
|
|
5
|
+
throw new Error("Missing verified CODESYS symbol.");
|
|
6
|
+
}
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CoreFunction,
|
|
3
|
+
type CoreFunctionBlock,
|
|
4
|
+
coreTables,
|
|
5
|
+
type GeneratedObject,
|
|
6
|
+
instancedBlock,
|
|
7
|
+
isBlockRoutine,
|
|
8
|
+
type SourceSpan,
|
|
9
|
+
type Variable,
|
|
10
|
+
} from "@tsplc/compiler/ir";
|
|
11
|
+
import { instrumentationFields } from "@tsplc/debug/manifest";
|
|
12
|
+
import { type StorageScope, storageName } from "./expressions";
|
|
13
|
+
import { jobField, jobTracking, type RoutineProbes } from "./instrumentation";
|
|
14
|
+
import { required } from "./required";
|
|
15
|
+
import type { ObjectMapping } from "./st-files";
|
|
16
|
+
import {
|
|
17
|
+
noResult,
|
|
18
|
+
type RoutineWriter,
|
|
19
|
+
writeAcquire,
|
|
20
|
+
writeRelease,
|
|
21
|
+
writeStatements,
|
|
22
|
+
} from "./statements";
|
|
23
|
+
import {
|
|
24
|
+
type CodesysSymbols,
|
|
25
|
+
type EmissionContext,
|
|
26
|
+
owningBlock,
|
|
27
|
+
} from "./symbols";
|
|
28
|
+
|
|
29
|
+
function routineHeader(
|
|
30
|
+
symbols: CodesysSymbols,
|
|
31
|
+
fn: CoreFunction | CoreFunctionBlock,
|
|
32
|
+
name: string,
|
|
33
|
+
): { kind: GeneratedObject["kind"]; declaration: string } {
|
|
34
|
+
if (isBlockRoutine(fn)) {
|
|
35
|
+
return { kind: "functionBlock", declaration: `FUNCTION_BLOCK ${name}` };
|
|
36
|
+
}
|
|
37
|
+
if (fn.owner) {
|
|
38
|
+
return {
|
|
39
|
+
kind: "method",
|
|
40
|
+
declaration: `METHOD ${fn.owner.access.toUpperCase()} ${name}${fn.result ? ` : ${symbols.targetTypes.name(fn.result)}` : ""}`,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (symbols.programRoutines.has(fn.id)) {
|
|
44
|
+
return { kind: "program", declaration: `PROGRAM ${name}` };
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
kind: "function",
|
|
48
|
+
declaration: `FUNCTION ${name} : ${fn.result ? symbols.targetTypes.name(fn.result) : noResult.type}`,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A function, function block or method: its declaration, its body and, for a tracked job, its tracking method. */
|
|
53
|
+
export function emitRoutine(
|
|
54
|
+
context: EmissionContext,
|
|
55
|
+
fn: CoreFunction | CoreFunctionBlock,
|
|
56
|
+
): { objects: GeneratedObject[]; mappings: ObjectMapping[] } {
|
|
57
|
+
const { plan, symbols, instrumentation } = context;
|
|
58
|
+
const { targetTypes, typeMap } = symbols;
|
|
59
|
+
const mappings: ObjectMapping[] = [];
|
|
60
|
+
const block = isBlockRoutine(fn) ? fn : undefined;
|
|
61
|
+
const method = isBlockRoutine(fn) ? undefined : fn.owner;
|
|
62
|
+
const program = symbols.programRoutines.has(fn.id);
|
|
63
|
+
const owner = owningBlock(symbols, fn);
|
|
64
|
+
const name = block
|
|
65
|
+
? required(symbols.blockNames.get(block.id))
|
|
66
|
+
: required(symbols.functionNames.get(fn.id));
|
|
67
|
+
const inputs = isBlockRoutine(fn) ? fn.inputs : fn.parameters;
|
|
68
|
+
const map = (
|
|
69
|
+
file: ObjectMapping["file"],
|
|
70
|
+
line: number,
|
|
71
|
+
span: SourceSpan,
|
|
72
|
+
): void => {
|
|
73
|
+
mappings.push({ object: fn.id, file, line, span });
|
|
74
|
+
};
|
|
75
|
+
const scope = required(symbols.routineScopes.get(fn.id));
|
|
76
|
+
const storage: StorageScope = {
|
|
77
|
+
symbols,
|
|
78
|
+
names: new Map([...symbols.references, ...scope.references]),
|
|
79
|
+
variableOf: coreTables(plan.core).variableOf,
|
|
80
|
+
};
|
|
81
|
+
const temporaries = fn.locals.map((item) => ({
|
|
82
|
+
declaration: ` ${storageName(storage, item.id)} : ${targetTypes.name(item.type)};`,
|
|
83
|
+
span: item.span,
|
|
84
|
+
}));
|
|
85
|
+
const declare = (declaration: string, span: SourceSpan): void => {
|
|
86
|
+
temporaries.push({ declaration, span });
|
|
87
|
+
};
|
|
88
|
+
const mutex =
|
|
89
|
+
method && owner?.runtime?.kind === "mailbox"
|
|
90
|
+
? required(symbols.mailboxLocks.get(owner.id))
|
|
91
|
+
: undefined;
|
|
92
|
+
const lines: string[] = [];
|
|
93
|
+
const probes: RoutineProbes = {
|
|
94
|
+
context,
|
|
95
|
+
storage,
|
|
96
|
+
task: instrumentation.analysis.tasks.get(fn.id),
|
|
97
|
+
};
|
|
98
|
+
const tracked = block && instrumentation.fields.get(block.id);
|
|
99
|
+
const routine: RoutineWriter = {
|
|
100
|
+
context,
|
|
101
|
+
fn,
|
|
102
|
+
name,
|
|
103
|
+
block,
|
|
104
|
+
owner,
|
|
105
|
+
method: method !== undefined,
|
|
106
|
+
program,
|
|
107
|
+
storage,
|
|
108
|
+
probes,
|
|
109
|
+
locals: scope.names,
|
|
110
|
+
mutex,
|
|
111
|
+
tracked,
|
|
112
|
+
write: (line, depth, source) => {
|
|
113
|
+
lines.push(" ".repeat(depth) + line);
|
|
114
|
+
if (source) {
|
|
115
|
+
map("implementation", lines.length, source);
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
declare,
|
|
119
|
+
};
|
|
120
|
+
const { write } = routine;
|
|
121
|
+
|
|
122
|
+
if (mutex) {
|
|
123
|
+
writeAcquire(routine);
|
|
124
|
+
}
|
|
125
|
+
if (block && tracked?.track) {
|
|
126
|
+
// Before the job's own early return, so a job started since its last call is seen.
|
|
127
|
+
write(`${tracked.track}();`, 0);
|
|
128
|
+
if (tracked.step) {
|
|
129
|
+
write(`${tracked.step} := ${jobField(storage, block, "step")};`, 0);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
writeStatements(routine, fn.body, 0);
|
|
133
|
+
if (tracked?.track) {
|
|
134
|
+
write(`${tracked.track}();`, 0);
|
|
135
|
+
}
|
|
136
|
+
if (mutex) {
|
|
137
|
+
writeRelease(routine, 0);
|
|
138
|
+
}
|
|
139
|
+
if (!block && !method && !program && !fn.result) {
|
|
140
|
+
write(`${name} := ${noResult.value};`, 0);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const header = routineHeader(symbols, fn, name);
|
|
144
|
+
const declaration = [header.declaration];
|
|
145
|
+
map("declaration", 1, fn.span);
|
|
146
|
+
const section = (keyword: string, items: readonly Variable[]): void => {
|
|
147
|
+
if (!items.length) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
declaration.push(keyword);
|
|
151
|
+
for (const item of items) {
|
|
152
|
+
map("declaration", declaration.length + 1, item.span);
|
|
153
|
+
const initial =
|
|
154
|
+
item.initial && !instancedBlock(item.type, typeMap)
|
|
155
|
+
? targetTypes.declarationInitializer(item.initial, item.type)
|
|
156
|
+
: "";
|
|
157
|
+
declaration.push(
|
|
158
|
+
` ${storageName(storage, item.id)} : ${targetTypes.name(item.type)}${initial};`,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
declaration.push("END_VAR");
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
section(
|
|
165
|
+
"VAR_INPUT",
|
|
166
|
+
inputs.filter((item) => item.mode !== "inOut"),
|
|
167
|
+
);
|
|
168
|
+
section(
|
|
169
|
+
"VAR_IN_OUT",
|
|
170
|
+
inputs.filter((item) => item.mode === "inOut"),
|
|
171
|
+
);
|
|
172
|
+
if (tracked?.job) {
|
|
173
|
+
declaration.push(
|
|
174
|
+
"VAR_INPUT",
|
|
175
|
+
` ${tracked.job} : ${instrumentationFields.job};`,
|
|
176
|
+
"END_VAR",
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
if (program && !block && fn.result) {
|
|
180
|
+
declaration.push(
|
|
181
|
+
"VAR_OUTPUT",
|
|
182
|
+
` ${required(symbols.programResults.get(fn.id))} : ${targetTypes.name(fn.result)};`,
|
|
183
|
+
"END_VAR",
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
if (block) {
|
|
187
|
+
if (block.runtime?.kind === "mailbox") {
|
|
188
|
+
declaration.push(
|
|
189
|
+
"VAR",
|
|
190
|
+
` ${required(symbols.mailboxLocks.get(block.id))} : __XWORD;`,
|
|
191
|
+
"END_VAR",
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
section("VAR_OUTPUT", block.outputs);
|
|
195
|
+
section(
|
|
196
|
+
"VAR",
|
|
197
|
+
block.state.filter((item) => !symbols.retainedVariables.has(item.id)),
|
|
198
|
+
);
|
|
199
|
+
section(
|
|
200
|
+
"VAR RETAIN",
|
|
201
|
+
block.state.filter((item) => symbols.retainedVariables.has(item.id)),
|
|
202
|
+
);
|
|
203
|
+
const fields = (["busy", "step", "startedAt"] as const).flatMap((key) => {
|
|
204
|
+
const field = tracked?.[key];
|
|
205
|
+
return field ? [` ${field} : ${instrumentationFields[key]};`] : [];
|
|
206
|
+
});
|
|
207
|
+
if (fields.length) {
|
|
208
|
+
declaration.push("VAR", ...fields, "END_VAR");
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (temporaries.length) {
|
|
212
|
+
declaration.push(block || program ? "VAR_TEMP" : "VAR");
|
|
213
|
+
temporaries.forEach(({ span }, index) => {
|
|
214
|
+
map("declaration", declaration.length + index + 1, span);
|
|
215
|
+
});
|
|
216
|
+
declaration.push(
|
|
217
|
+
...temporaries.map((temporary) => temporary.declaration),
|
|
218
|
+
"END_VAR",
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const routineObject: GeneratedObject = {
|
|
223
|
+
id: fn.id,
|
|
224
|
+
...(method ? { parent: method.block } : {}),
|
|
225
|
+
kind: header.kind,
|
|
226
|
+
name,
|
|
227
|
+
declaration: `${declaration.join("\n")}\n`,
|
|
228
|
+
implementation: `${lines.join("\n")}\n`,
|
|
229
|
+
};
|
|
230
|
+
if (!block || !tracked?.track) {
|
|
231
|
+
return { objects: [routineObject], mappings };
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
objects: [
|
|
235
|
+
routineObject,
|
|
236
|
+
{
|
|
237
|
+
id: `${fn.id}:${tracked.track}`,
|
|
238
|
+
parent: fn.id,
|
|
239
|
+
kind: "method",
|
|
240
|
+
name: tracked.track,
|
|
241
|
+
declaration: `METHOD PRIVATE ${tracked.track}\n`,
|
|
242
|
+
implementation: jobTracking(probes, block, tracked),
|
|
243
|
+
},
|
|
244
|
+
],
|
|
245
|
+
mappings,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CoreExpression,
|
|
3
|
+
CoreStatement,
|
|
4
|
+
GeneratedObject,
|
|
5
|
+
Task,
|
|
6
|
+
VerifiedCoreModule,
|
|
7
|
+
} from "@tsplc/compiler/ir";
|
|
8
|
+
import {
|
|
9
|
+
nestedBodies,
|
|
10
|
+
statementExpressions,
|
|
11
|
+
subexpressions,
|
|
12
|
+
} from "@tsplc/compiler/ir";
|
|
13
|
+
import { scanClockType } from "@tsplc/debug/manifest";
|
|
14
|
+
import { Names } from "./names";
|
|
15
|
+
|
|
16
|
+
/** Milliseconds since the runtime started; DINT wraps after 24.8 days, so subtract to compare. */
|
|
17
|
+
export const runtimeClock = `TO_${scanClockType}(TIME_TO_UDINT(TIME()))`;
|
|
18
|
+
|
|
19
|
+
/** The scan's clock of a program that calls `now()`: where a scan's time is held while the scan runs. */
|
|
20
|
+
export interface ScanClock {
|
|
21
|
+
/** The global list that holds the clock. */
|
|
22
|
+
list: string;
|
|
23
|
+
/** The time the running scan began, which every `now()` reads. */
|
|
24
|
+
now: string;
|
|
25
|
+
/**
|
|
26
|
+
* By task, where its scan keeps the clock of the scan it interrupted. A program with one task
|
|
27
|
+
* has none.
|
|
28
|
+
*/
|
|
29
|
+
interrupted: ReadonlyMap<string, string>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const readsClock = (value: CoreExpression): boolean =>
|
|
33
|
+
(value.kind === "unary" && value.operator === "clock") ||
|
|
34
|
+
subexpressions(value).some(readsClock);
|
|
35
|
+
|
|
36
|
+
const anyReadsClock = (items: readonly CoreStatement[]): boolean =>
|
|
37
|
+
items.some(
|
|
38
|
+
(item) =>
|
|
39
|
+
statementExpressions(item).some(readsClock) ||
|
|
40
|
+
nestedBodies(item).some(anyReadsClock),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
/** The scan clock's names, for a program in which some routine calls `now()`. */
|
|
44
|
+
export function planScanClock(
|
|
45
|
+
core: VerifiedCoreModule,
|
|
46
|
+
tasks: readonly Task[],
|
|
47
|
+
objectNames: Names,
|
|
48
|
+
): ScanClock | undefined {
|
|
49
|
+
if (
|
|
50
|
+
![...core.functions, ...core.blocks].some((routine) =>
|
|
51
|
+
anyReadsClock(routine.body),
|
|
52
|
+
)
|
|
53
|
+
) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const list = objectNames.allocateObject("GVL", "TsplcScan");
|
|
57
|
+
const fields = new Names();
|
|
58
|
+
const now = fields.allocate("clock");
|
|
59
|
+
return {
|
|
60
|
+
list,
|
|
61
|
+
now: `${list}.${now}`,
|
|
62
|
+
interrupted: new Map(
|
|
63
|
+
tasks.length > 1
|
|
64
|
+
? tasks.map((task) => [
|
|
65
|
+
task.name,
|
|
66
|
+
`${list}.${fields.allocate(`interrupted_${task.name}`)}`,
|
|
67
|
+
])
|
|
68
|
+
: [],
|
|
69
|
+
),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const field = (qualified: string) =>
|
|
74
|
+
qualified.slice(qualified.indexOf(".") + 1);
|
|
75
|
+
|
|
76
|
+
export const emitScanClock = (clock: ScanClock): GeneratedObject => ({
|
|
77
|
+
id: "tsplc:scanClock",
|
|
78
|
+
kind: "globalList",
|
|
79
|
+
name: clock.list,
|
|
80
|
+
declaration: `{attribute 'qualified_only'}
|
|
81
|
+
VAR_GLOBAL
|
|
82
|
+
${[clock.now, ...clock.interrupted.values()].map((name) => ` ${field(name)} : ${scanClockType};\n`).join("")}END_VAR
|
|
83
|
+
`,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* What a task's cycle starts with: it reads the runtime's clock once, so every `now()` of the scan
|
|
88
|
+
* returns one time. A task that interrupts another's scan runs to its end before that scan
|
|
89
|
+
* resumes, so it keeps the interrupted scan's time for `restoreScanClock` to put back.
|
|
90
|
+
*/
|
|
91
|
+
export const latchScanClock = (clock: ScanClock, task: string): string[] => {
|
|
92
|
+
const interrupted = clock.interrupted.get(task);
|
|
93
|
+
return [
|
|
94
|
+
...(interrupted ? [`${interrupted} := ${clock.now};`] : []),
|
|
95
|
+
`${clock.now} := ${runtimeClock};`,
|
|
96
|
+
];
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** What a task's cycle ends with when it may have interrupted another task's scan. */
|
|
100
|
+
export const restoreScanClock = (clock: ScanClock, task: string): string[] => {
|
|
101
|
+
const interrupted = clock.interrupted.get(task);
|
|
102
|
+
return interrupted ? [`${clock.now} := ${interrupted};`] : [];
|
|
103
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {
|
|
2
|
+
coreTables,
|
|
3
|
+
type Task,
|
|
4
|
+
type VerifiedCoreModule,
|
|
5
|
+
} from "@tsplc/compiler/ir";
|
|
6
|
+
import { nanosecondsPerMillisecond } from "@tsplc/iec";
|
|
7
|
+
import type { Report } from "./plan";
|
|
8
|
+
import { accessedStorage } from "./task-access";
|
|
9
|
+
|
|
10
|
+
/** The deployment's tasks, then one per background interval and priority, with detached continuations added after their callers. */
|
|
11
|
+
export function schedulePrograms(
|
|
12
|
+
core: VerifiedCoreModule,
|
|
13
|
+
tasks: readonly Task[],
|
|
14
|
+
report: Report,
|
|
15
|
+
): readonly Task[] {
|
|
16
|
+
const background = new Map<string, Task & { programs: string[] }>();
|
|
17
|
+
for (const program of core.programs) {
|
|
18
|
+
if (!program.background) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const { priority, intervalNanoseconds } = program.background;
|
|
22
|
+
const name = `TsplcBackground_${priority}_${BigInt(intervalNanoseconds) / nanosecondsPerMillisecond}`;
|
|
23
|
+
const existing = background.get(name);
|
|
24
|
+
if (existing) {
|
|
25
|
+
existing.programs.push(program.name);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
background.set(name, {
|
|
29
|
+
name,
|
|
30
|
+
priority,
|
|
31
|
+
intervalNanoseconds,
|
|
32
|
+
programs: [program.name],
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return appendContinuations(core, [...tasks, ...background.values()], report);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Detached continuations run after their caller programs in the same cyclic task. */
|
|
39
|
+
function appendContinuations(
|
|
40
|
+
core: VerifiedCoreModule,
|
|
41
|
+
tasks: readonly Task[],
|
|
42
|
+
report: Report,
|
|
43
|
+
): readonly Task[] {
|
|
44
|
+
const continuations = core.programs.flatMap((program) =>
|
|
45
|
+
program.continuation
|
|
46
|
+
? [{ name: program.name, scope: program.continuation }]
|
|
47
|
+
: [],
|
|
48
|
+
);
|
|
49
|
+
if (!continuations.length) {
|
|
50
|
+
return tasks;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const { functionOf } = coreTables(core);
|
|
54
|
+
// The deployment's program names are checked after scheduling, so an unknown one is skipped here.
|
|
55
|
+
const programs = new Map(
|
|
56
|
+
core.programs.map((program) => [program.name, program]),
|
|
57
|
+
);
|
|
58
|
+
const placements = tasks.map(
|
|
59
|
+
(task): { task: Task; roots: Set<string>; additions: string[] } => ({
|
|
60
|
+
task,
|
|
61
|
+
roots: new Set(
|
|
62
|
+
task.programs.flatMap((name) => {
|
|
63
|
+
const program = programs.get(name);
|
|
64
|
+
return !program || program.background
|
|
65
|
+
? []
|
|
66
|
+
: [...accessedStorage(functionOf(program.entry), core)];
|
|
67
|
+
}),
|
|
68
|
+
),
|
|
69
|
+
additions: [],
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
for (const { name, scope } of continuations) {
|
|
73
|
+
const root = core.variables.find(
|
|
74
|
+
(variable) =>
|
|
75
|
+
variable.name === scope.instance && variable.span.file === scope.file,
|
|
76
|
+
);
|
|
77
|
+
if (!root) {
|
|
78
|
+
report(`Missing continuation owner ${scope.instance}.`);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const owners = placements.filter(({ roots }) => roots.has(root.id));
|
|
82
|
+
const [owner] = owners;
|
|
83
|
+
if (!owner || owners.length !== 1) {
|
|
84
|
+
report(
|
|
85
|
+
`Continuation owner ${scope.instance} must belong to exactly one caller task.`,
|
|
86
|
+
root.span,
|
|
87
|
+
);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
owner.additions.push(name);
|
|
91
|
+
}
|
|
92
|
+
return placements.map(({ task, additions }) => ({
|
|
93
|
+
...task,
|
|
94
|
+
programs: task.programs.concat(additions),
|
|
95
|
+
}));
|
|
96
|
+
}
|