@tsplc/compiler 0.5.13 → 0.5.15
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 +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- 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 +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Constant,
|
|
3
|
+
type CoreExpression,
|
|
4
|
+
type CoreFunction,
|
|
5
|
+
type CoreFunctionBlock,
|
|
6
|
+
type CoreStatement,
|
|
7
|
+
coreTables,
|
|
8
|
+
defaultValue,
|
|
9
|
+
type SourceSpan,
|
|
10
|
+
} from "@tsplc/compiler/ir";
|
|
11
|
+
import { instrumentationFields } from "@tsplc/debug/manifest";
|
|
12
|
+
import {
|
|
13
|
+
expressionText,
|
|
14
|
+
placeText,
|
|
15
|
+
type StorageScope,
|
|
16
|
+
storageName,
|
|
17
|
+
} from "./expressions";
|
|
18
|
+
import {
|
|
19
|
+
type BlockInstrumentation,
|
|
20
|
+
jobField,
|
|
21
|
+
type RoutineProbes,
|
|
22
|
+
ringTarget,
|
|
23
|
+
writeProbes,
|
|
24
|
+
} from "./instrumentation";
|
|
25
|
+
import { writeLoop } from "./loops";
|
|
26
|
+
import type { Names } from "./names";
|
|
27
|
+
import { required } from "./required";
|
|
28
|
+
import type { EmissionContext } from "./symbols";
|
|
29
|
+
import { callParameters } from "./telemetry";
|
|
30
|
+
|
|
31
|
+
/** What a function without a result returns: a CODESYS function returns a value, which its callers discard. */
|
|
32
|
+
export const noResult = { type: "BOOL", value: "TRUE" } as const;
|
|
33
|
+
|
|
34
|
+
/** A routine being written: its lines, the names its ST uses, and what its statements read. */
|
|
35
|
+
export interface RoutineWriter {
|
|
36
|
+
context: EmissionContext;
|
|
37
|
+
fn: CoreFunction | CoreFunctionBlock;
|
|
38
|
+
/** The routine's object name, which a function also assigns its result to. */
|
|
39
|
+
name: string;
|
|
40
|
+
/** The routine itself when it is a block. */
|
|
41
|
+
block: CoreFunctionBlock | undefined;
|
|
42
|
+
owner: CoreFunctionBlock | undefined;
|
|
43
|
+
method: boolean;
|
|
44
|
+
program: boolean;
|
|
45
|
+
storage: StorageScope;
|
|
46
|
+
probes: RoutineProbes;
|
|
47
|
+
/** The routine's own scope, which names the variables its lowering adds. */
|
|
48
|
+
locals: Names;
|
|
49
|
+
/** A mailbox method's lock. */
|
|
50
|
+
mutex: string | undefined;
|
|
51
|
+
tracked: BlockInstrumentation | undefined;
|
|
52
|
+
/** Appends an implementation line, mapped to `source` when given. */
|
|
53
|
+
write: (line: string, depth: number, source?: SourceSpan) => void;
|
|
54
|
+
/** Declares a temporary variable the lowering adds, mapped to `span`. */
|
|
55
|
+
declare: (declaration: string, span: SourceSpan) => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Takes the routine's mailbox lock, or returns a default result when another caller holds it.
|
|
60
|
+
* ARM64 implements __COMPARE_AND_SWAP with load-linked/store-conditional, which can fail even when
|
|
61
|
+
* the word holds the expected value, so the swap is retried while the lock reads free.
|
|
62
|
+
*/
|
|
63
|
+
export const writeAcquire = ({
|
|
64
|
+
context,
|
|
65
|
+
fn,
|
|
66
|
+
name,
|
|
67
|
+
locals,
|
|
68
|
+
mutex,
|
|
69
|
+
write,
|
|
70
|
+
declare,
|
|
71
|
+
}: RoutineWriter) => {
|
|
72
|
+
const { targetTypes, typeMap } = context.symbols;
|
|
73
|
+
const lock = required(mutex);
|
|
74
|
+
const acquired = locals.allocate("plc_acquired");
|
|
75
|
+
declare(` ${acquired} : BOOL;`, fn.span);
|
|
76
|
+
write(`${acquired} := FALSE;`, 0);
|
|
77
|
+
write(`WHILE NOT ${acquired} AND ${lock} = 0 DO`, 0);
|
|
78
|
+
write(`${acquired} := __COMPARE_AND_SWAP(ADR(${lock}), 0, 1);`, 1);
|
|
79
|
+
write("END_WHILE;", 0);
|
|
80
|
+
write(`IF NOT ${acquired} THEN`, 0);
|
|
81
|
+
if (fn.result) {
|
|
82
|
+
const unavailable = locals.allocate("plc_unavailable");
|
|
83
|
+
declare(
|
|
84
|
+
` ${unavailable} : ${targetTypes.name(fn.result)} := ${targetTypes.literal(defaultValue(fn.result, typeMap), fn.result)};`,
|
|
85
|
+
fn.span,
|
|
86
|
+
);
|
|
87
|
+
write(`${name} := ${unavailable};`, 1);
|
|
88
|
+
}
|
|
89
|
+
write("RETURN;", 1);
|
|
90
|
+
write("END_IF;", 0);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** Releases the routine's mailbox lock. Only the holder releases, so a failed swap is spurious, as in the acquire; retry it. */
|
|
94
|
+
export const writeRelease = (
|
|
95
|
+
{ mutex, write }: RoutineWriter,
|
|
96
|
+
depth: number,
|
|
97
|
+
source?: SourceSpan,
|
|
98
|
+
) => {
|
|
99
|
+
const lock = required(mutex);
|
|
100
|
+
write(`WHILE NOT __COMPARE_AND_SWAP(ADR(${lock}), 1, 0) DO`, depth, source);
|
|
101
|
+
write(";", depth + 1);
|
|
102
|
+
write("END_WHILE;", depth);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** Assigns a constant; ST allows aggregate literals only in declarations, so aggregates go leaf by leaf. */
|
|
106
|
+
const assignConstant = (
|
|
107
|
+
routine: RoutineWriter,
|
|
108
|
+
destination: string,
|
|
109
|
+
value: Constant,
|
|
110
|
+
id: string,
|
|
111
|
+
depth: number,
|
|
112
|
+
span: SourceSpan,
|
|
113
|
+
): void => {
|
|
114
|
+
const { typeMap, targetTypes } = routine.context.symbols;
|
|
115
|
+
const type = required(typeMap.get(id));
|
|
116
|
+
if (type.kind === "record" && value.kind === "record") {
|
|
117
|
+
type.fields.forEach((field) => {
|
|
118
|
+
assignConstant(
|
|
119
|
+
routine,
|
|
120
|
+
`${destination}.${targetTypes.field(id, field.name)}`,
|
|
121
|
+
required(value.fields[field.name]),
|
|
122
|
+
field.type,
|
|
123
|
+
depth,
|
|
124
|
+
span,
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
} else if (type.kind === "array" && value.kind === "array") {
|
|
128
|
+
value.items.forEach((item, index) => {
|
|
129
|
+
assignConstant(
|
|
130
|
+
routine,
|
|
131
|
+
`${destination}[${type.lowerBound + index}]`,
|
|
132
|
+
item,
|
|
133
|
+
type.element,
|
|
134
|
+
depth,
|
|
135
|
+
span,
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
} else {
|
|
139
|
+
routine.write(
|
|
140
|
+
`${destination} := ${targetTypes.literal(value, id)};`,
|
|
141
|
+
depth,
|
|
142
|
+
span,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const assign = (
|
|
148
|
+
routine: RoutineWriter,
|
|
149
|
+
destination: string,
|
|
150
|
+
value: CoreExpression,
|
|
151
|
+
depth: number,
|
|
152
|
+
span: SourceSpan,
|
|
153
|
+
): void => {
|
|
154
|
+
if (value.kind === "constant") {
|
|
155
|
+
assignConstant(routine, destination, value.value, value.type, depth, span);
|
|
156
|
+
} else {
|
|
157
|
+
routine.write(
|
|
158
|
+
`${destination} := ${expressionText(routine.storage, value)};`,
|
|
159
|
+
depth,
|
|
160
|
+
span,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/** A routine's statements as ST, at `depth`. */
|
|
166
|
+
export function writeStatements(
|
|
167
|
+
routine: RoutineWriter,
|
|
168
|
+
items: readonly CoreStatement[],
|
|
169
|
+
depth: number,
|
|
170
|
+
): void {
|
|
171
|
+
for (const item of items) {
|
|
172
|
+
writeStatement(routine, item, depth);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function writeStatement(
|
|
177
|
+
routine: RoutineWriter,
|
|
178
|
+
item: CoreStatement,
|
|
179
|
+
depth: number,
|
|
180
|
+
): void {
|
|
181
|
+
const { context, storage, probes, block, tracked, write } = routine;
|
|
182
|
+
const { plan, symbols, instrumentation } = context;
|
|
183
|
+
switch (item.kind) {
|
|
184
|
+
case "assign":
|
|
185
|
+
writeProbes(probes, write, item, depth);
|
|
186
|
+
if (
|
|
187
|
+
tracked?.step &&
|
|
188
|
+
block?.job &&
|
|
189
|
+
item.root === block.state.find((field) => field.name === "step")?.id
|
|
190
|
+
) {
|
|
191
|
+
write(
|
|
192
|
+
`${tracked.step} := ${jobField(storage, block, "step")};`,
|
|
193
|
+
depth,
|
|
194
|
+
item.span,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
assign(
|
|
198
|
+
routine,
|
|
199
|
+
placeText(storage, item.root, item.path),
|
|
200
|
+
item.value,
|
|
201
|
+
depth,
|
|
202
|
+
item.span,
|
|
203
|
+
);
|
|
204
|
+
return;
|
|
205
|
+
case "blockUpdate": {
|
|
206
|
+
const target = coreTables(plan.core).updatedBlock(item);
|
|
207
|
+
const arguments_ = Object.entries(item.inputs).map(([input, value]) => {
|
|
208
|
+
const formal = required(
|
|
209
|
+
target.inputs.find((field) => field.name === input),
|
|
210
|
+
);
|
|
211
|
+
return `${required(required(symbols.blockFields.get(target.id)).get(formal.id))} := ${expressionText(storage, value)}`;
|
|
212
|
+
});
|
|
213
|
+
// Service calls report the job that made them: a single-instance job passes its own id,
|
|
214
|
+
// and everything between it and the call passes the id it was given.
|
|
215
|
+
const job = instrumentation.fields.get(target.id)?.job;
|
|
216
|
+
const id = instrumentation.jobIds.get(item.instance);
|
|
217
|
+
const given =
|
|
218
|
+
routine.owner && instrumentation.fields.get(routine.owner.id)?.job;
|
|
219
|
+
if (job && (id || given)) {
|
|
220
|
+
arguments_.push(
|
|
221
|
+
`${job} := ${id ? `${instrumentationFields.job}#${id}` : given}`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
const receiver = item.index
|
|
225
|
+
? `${storageName(storage, item.instance)}[${expressionText(storage, item.index)}]`
|
|
226
|
+
: storageName(storage, item.instance);
|
|
227
|
+
write(`${receiver}(${arguments_.join(", ")});`, depth, item.span);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
case "invoke":
|
|
231
|
+
writeInvoke(routine, item, depth);
|
|
232
|
+
return;
|
|
233
|
+
case "if":
|
|
234
|
+
write(
|
|
235
|
+
`IF ${expressionText(storage, item.condition)} THEN`,
|
|
236
|
+
depth,
|
|
237
|
+
item.span,
|
|
238
|
+
);
|
|
239
|
+
writeStatements(routine, item.then, depth + 1);
|
|
240
|
+
if (item.else.length) {
|
|
241
|
+
write("ELSE", depth, item.span);
|
|
242
|
+
writeStatements(routine, item.else, depth + 1);
|
|
243
|
+
}
|
|
244
|
+
write("END_IF;", depth, item.span);
|
|
245
|
+
return;
|
|
246
|
+
case "switch": {
|
|
247
|
+
for (const [index, branch] of item.cases.entries()) {
|
|
248
|
+
const conditions = branch.values.map(
|
|
249
|
+
(value) =>
|
|
250
|
+
`(${expressionText(storage, item.value)} = ${symbols.targetTypes.literal(value, item.value.type)})`,
|
|
251
|
+
);
|
|
252
|
+
write(
|
|
253
|
+
`${index ? "ELSIF" : "IF"} ${conditions.join(" OR ")} THEN`,
|
|
254
|
+
depth,
|
|
255
|
+
item.span,
|
|
256
|
+
);
|
|
257
|
+
writeStatements(routine, branch.body, depth + 1);
|
|
258
|
+
}
|
|
259
|
+
if (!item.cases.length) {
|
|
260
|
+
writeStatements(routine, item.fallback, depth);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (item.fallback.length) {
|
|
264
|
+
write("ELSE", depth, item.span);
|
|
265
|
+
}
|
|
266
|
+
writeStatements(routine, item.fallback, depth + 1);
|
|
267
|
+
write("END_IF;", depth, item.span);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
case "for":
|
|
271
|
+
writeLoop(routine, item, depth, (body, inner) => {
|
|
272
|
+
writeStatements(routine, body, inner);
|
|
273
|
+
});
|
|
274
|
+
return;
|
|
275
|
+
case "return":
|
|
276
|
+
writeProbes(probes, write, item, depth);
|
|
277
|
+
writeReturn(routine, item, depth);
|
|
278
|
+
if (routine.mutex) {
|
|
279
|
+
writeRelease(routine, depth, item.span);
|
|
280
|
+
}
|
|
281
|
+
if (tracked?.track) {
|
|
282
|
+
write(`${tracked.track}();`, depth);
|
|
283
|
+
}
|
|
284
|
+
write("RETURN;", depth, item.span);
|
|
285
|
+
return;
|
|
286
|
+
case "break":
|
|
287
|
+
write("EXIT;", depth, item.span);
|
|
288
|
+
return;
|
|
289
|
+
case "continue":
|
|
290
|
+
write("CONTINUE;", depth, item.span);
|
|
291
|
+
return;
|
|
292
|
+
case "telemetry": {
|
|
293
|
+
const site = required(plan.telemetry?.calls.get(item));
|
|
294
|
+
const arguments_ = callParameters(item).map(
|
|
295
|
+
({ name, value }) => `${name} := ${expressionText(storage, value)}`,
|
|
296
|
+
);
|
|
297
|
+
write(
|
|
298
|
+
`${ringTarget(instrumentation, site.task)}.Record${site.id}(${arguments_.join(", ")});`,
|
|
299
|
+
depth,
|
|
300
|
+
item.span,
|
|
301
|
+
);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function writeInvoke(
|
|
308
|
+
routine: RoutineWriter,
|
|
309
|
+
item: Extract<CoreStatement, { kind: "invoke" }>,
|
|
310
|
+
depth: number,
|
|
311
|
+
): void {
|
|
312
|
+
const { storage, write } = routine;
|
|
313
|
+
const { plan, symbols } = routine.context;
|
|
314
|
+
const receiver = item.instance
|
|
315
|
+
? `${storageName(storage, item.instance)}${item.index ? `[${expressionText(storage, item.index)}]` : ""}.`
|
|
316
|
+
: "";
|
|
317
|
+
const called = coreTables(plan.core).functionOf(item.function);
|
|
318
|
+
const programCall = symbols.programRoutines.has(called.id);
|
|
319
|
+
const arguments_ = item.arguments.map((value, index) => {
|
|
320
|
+
if (
|
|
321
|
+
!called.owner &&
|
|
322
|
+
!programCall &&
|
|
323
|
+
!called.parameters.some((parameter) => parameter.mode === "inOut")
|
|
324
|
+
) {
|
|
325
|
+
return expressionText(storage, value);
|
|
326
|
+
}
|
|
327
|
+
const parameter = required(called.parameters[index]);
|
|
328
|
+
const formal = required(
|
|
329
|
+
required(symbols.routineScopes.get(called.id)).references.get(
|
|
330
|
+
parameter.id,
|
|
331
|
+
),
|
|
332
|
+
);
|
|
333
|
+
return `${formal} := ${expressionText(storage, value)}`;
|
|
334
|
+
});
|
|
335
|
+
const call = `${receiver}${required(symbols.functionNames.get(item.function))}(${arguments_.join(", ")})`;
|
|
336
|
+
if (programCall) {
|
|
337
|
+
write(`${call};`, depth, item.span);
|
|
338
|
+
if (item.destination) {
|
|
339
|
+
write(
|
|
340
|
+
`${storageName(storage, item.destination)} := ${required(symbols.functionNames.get(called.id))}.${required(symbols.programResults.get(called.id))};`,
|
|
341
|
+
depth,
|
|
342
|
+
item.span,
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
write(
|
|
348
|
+
item.destination
|
|
349
|
+
? `${storageName(storage, item.destination)} := ${call};`
|
|
350
|
+
: `${call};`,
|
|
351
|
+
depth,
|
|
352
|
+
item.span,
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** What a `return` hands back: a block's outputs, a stateful program's result, or a function's value. */
|
|
357
|
+
function writeReturn(
|
|
358
|
+
routine: RoutineWriter,
|
|
359
|
+
item: Extract<CoreStatement, { kind: "return" }>,
|
|
360
|
+
depth: number,
|
|
361
|
+
): void {
|
|
362
|
+
const { block, storage, write, name, fn } = routine;
|
|
363
|
+
const { symbols } = routine.context;
|
|
364
|
+
const { value } = item;
|
|
365
|
+
if (block) {
|
|
366
|
+
if (value?.kind === "constant" && value.value.kind === "record") {
|
|
367
|
+
for (const output of block.outputs) {
|
|
368
|
+
assignConstant(
|
|
369
|
+
routine,
|
|
370
|
+
storageName(storage, output.id),
|
|
371
|
+
required(value.value.fields[output.name]),
|
|
372
|
+
output.type,
|
|
373
|
+
depth,
|
|
374
|
+
item.span,
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
} else if (value) {
|
|
378
|
+
const returned = expressionText(storage, value);
|
|
379
|
+
for (const output of block.outputs) {
|
|
380
|
+
write(
|
|
381
|
+
`${storageName(storage, output.id)} := ${returned}.${symbols.targetTypes.field(required(block.result), output.name)};`,
|
|
382
|
+
depth,
|
|
383
|
+
item.span,
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
if (routine.program) {
|
|
390
|
+
if (value) {
|
|
391
|
+
assign(
|
|
392
|
+
routine,
|
|
393
|
+
required(symbols.programResults.get(fn.id)),
|
|
394
|
+
value,
|
|
395
|
+
depth,
|
|
396
|
+
item.span,
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if (routine.method && !fn.result) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (value) {
|
|
405
|
+
assign(routine, name, value, depth, item.span);
|
|
406
|
+
} else {
|
|
407
|
+
write(`${name} := ${noResult.value};`, depth, item.span);
|
|
408
|
+
}
|
|
409
|
+
}
|
package/src/codesys/storage.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { basename } from "node:path";
|
|
2
2
|
import {
|
|
3
|
+
coreTables,
|
|
3
4
|
instancedBlock,
|
|
5
|
+
isLogicalIo,
|
|
4
6
|
type Variable,
|
|
5
7
|
type VerifiedCoreModule,
|
|
6
8
|
} from "@tsplc/compiler/ir";
|
|
7
9
|
|
|
8
|
-
/** Which
|
|
9
|
-
export type GlobalStorage = "constants" | "retained" | "state";
|
|
10
|
+
/** Which section of its module's global lists a variable goes in. */
|
|
11
|
+
export type GlobalStorage = "constants" | "persistent" | "retained" | "state";
|
|
10
12
|
|
|
11
13
|
/** A section of a global list: the variables one `VAR_GLOBAL` qualifier declares. */
|
|
12
14
|
interface GlobalSection {
|
|
@@ -15,32 +17,42 @@ interface GlobalSection {
|
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
/**
|
|
18
|
-
* A module's constants, or its
|
|
19
|
-
* separate sections, so retaining a variable never changes the path it is
|
|
20
|
+
* A module's constants, its mutable storage, or its persistent storage. Retained and ordinary state
|
|
21
|
+
* share one list in separate sections, so retaining a variable never changes the path it is
|
|
22
|
+
* published at. Persistent storage has a list of its own: CODESYS keeps everything a persistent
|
|
23
|
+
* list declares through a download, ordinary state included.
|
|
20
24
|
*/
|
|
21
25
|
interface GlobalList {
|
|
22
26
|
id: string;
|
|
23
27
|
module: string;
|
|
24
|
-
kind:
|
|
28
|
+
kind: GlobalListKind;
|
|
25
29
|
sections: GlobalSection[];
|
|
26
30
|
variables: Variable[];
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
/**
|
|
30
|
-
* The module that declares a module-level variable
|
|
31
|
-
* wrote, which for elaborated storage is not the module the elaborator
|
|
34
|
+
* The module a symbol id names, such as the module that declares a module-level variable. Spans
|
|
35
|
+
* point at what the user wrote, which for elaborated storage is not the module the elaborator
|
|
36
|
+
* made for it.
|
|
32
37
|
*/
|
|
33
|
-
export const moduleFile = (
|
|
34
|
-
variable.id.slice(0, variable.id.indexOf(":"));
|
|
38
|
+
export const moduleFile = (id: string): string => id.split(":", 1)[0] ?? id;
|
|
35
39
|
|
|
36
40
|
/** The module's name: its file name without extension. */
|
|
37
41
|
export const moduleName = (variable: Variable): string =>
|
|
38
|
-
basename(moduleFile(variable)).replace(/\.[^.]+$/, "");
|
|
42
|
+
basename(moduleFile(variable.id)).replace(/\.[^.]+$/, "");
|
|
43
|
+
|
|
44
|
+
/** A module-level variable as TypeScript names it: `Module.variable`. */
|
|
45
|
+
export const storagePath = (variable: Variable): string =>
|
|
46
|
+
`${moduleName(variable)}.${variable.name}`;
|
|
47
|
+
|
|
48
|
+
/** The kinds of global list: each holds the sections of one or more `GlobalStorage`. */
|
|
49
|
+
export type GlobalListKind = Exclude<GlobalStorage, "retained">;
|
|
39
50
|
|
|
40
51
|
const sectionOrder: readonly GlobalStorage[] = [
|
|
41
52
|
"constants",
|
|
42
53
|
"state",
|
|
43
54
|
"retained",
|
|
55
|
+
"persistent",
|
|
44
56
|
];
|
|
45
57
|
|
|
46
58
|
const globalStorage = (
|
|
@@ -50,26 +62,30 @@ const globalStorage = (
|
|
|
50
62
|
if (variable.storage === "constant") {
|
|
51
63
|
return "constants";
|
|
52
64
|
}
|
|
65
|
+
if (variable.retention === "persist") {
|
|
66
|
+
return "persistent";
|
|
67
|
+
}
|
|
53
68
|
return retainedVariables.has(variable.id) ? "retained" : "state";
|
|
54
69
|
};
|
|
55
70
|
|
|
71
|
+
const listKind = (storage: GlobalStorage): GlobalListKind =>
|
|
72
|
+
storage === "retained" ? "state" : storage;
|
|
73
|
+
|
|
56
74
|
/** CODESYS promotes an enclosing instance when private child state is retained. */
|
|
57
75
|
export function storageLayout(core: VerifiedCoreModule) {
|
|
58
|
-
const types =
|
|
59
|
-
const blocks = new Map(core.blocks.map((block) => [block.id, block]));
|
|
76
|
+
const { types, blockOf } = coreTables(core);
|
|
60
77
|
const retention = new Map<string, boolean>();
|
|
61
78
|
const blockRetained = (id: string): boolean => {
|
|
62
79
|
const cached = retention.get(id);
|
|
63
80
|
if (cached !== undefined) {
|
|
64
81
|
return cached;
|
|
65
82
|
}
|
|
66
|
-
const
|
|
67
|
-
if (!block) {
|
|
68
|
-
throw new Error("Missing verified block during retention planning.");
|
|
69
|
-
}
|
|
70
|
-
const retained = block.state.some((field) => {
|
|
83
|
+
const retained = blockOf(id).state.some((field) => {
|
|
71
84
|
const type = instancedBlock(field.type, types);
|
|
72
|
-
return
|
|
85
|
+
return (
|
|
86
|
+
field.retention === "retain" ||
|
|
87
|
+
(type !== undefined && blockRetained(type.block))
|
|
88
|
+
);
|
|
73
89
|
});
|
|
74
90
|
retention.set(id, retained);
|
|
75
91
|
return retained;
|
|
@@ -85,8 +101,10 @@ export function storageLayout(core: VerifiedCoreModule) {
|
|
|
85
101
|
.filter((variable) => {
|
|
86
102
|
const type = instancedBlock(variable.type, types);
|
|
87
103
|
return (
|
|
88
|
-
variable.retain ||
|
|
89
|
-
(
|
|
104
|
+
variable.retention === "retain" ||
|
|
105
|
+
(variable.retention === "none" &&
|
|
106
|
+
type !== undefined &&
|
|
107
|
+
retainedBlocks.has(type.block))
|
|
90
108
|
);
|
|
91
109
|
})
|
|
92
110
|
.map((variable) => variable.id),
|
|
@@ -94,12 +112,12 @@ export function storageLayout(core: VerifiedCoreModule) {
|
|
|
94
112
|
|
|
95
113
|
const groups = new Map<string, Omit<GlobalList, "variables">>();
|
|
96
114
|
for (const variable of core.variables) {
|
|
97
|
-
if (variable
|
|
115
|
+
if (isLogicalIo(variable)) {
|
|
98
116
|
continue;
|
|
99
117
|
}
|
|
100
118
|
const storage = globalStorage(variable, retainedVariables);
|
|
101
|
-
const kind = storage
|
|
102
|
-
const id = `${moduleFile(variable)}:${kind}`;
|
|
119
|
+
const kind = listKind(storage);
|
|
120
|
+
const id = `${moduleFile(variable.id)}:${kind}`;
|
|
103
121
|
const group = groups.get(id) ?? {
|
|
104
122
|
id,
|
|
105
123
|
module: moduleName(variable),
|