@tsplc/compiler 0.2.17 → 0.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -0,0 +1,1163 @@
|
|
|
1
|
+
import type { SourceSpan } from "./diagnostics.ts";
|
|
2
|
+
import type {
|
|
3
|
+
Variable,
|
|
4
|
+
Expression,
|
|
5
|
+
Statement,
|
|
6
|
+
Place,
|
|
7
|
+
PlcFunction,
|
|
8
|
+
FunctionBlock,
|
|
9
|
+
} from "./model.ts";
|
|
10
|
+
import type {
|
|
11
|
+
CoreExpression,
|
|
12
|
+
CoreStatement,
|
|
13
|
+
CorePath,
|
|
14
|
+
CoreFunction,
|
|
15
|
+
CoreFunctionBlock,
|
|
16
|
+
} from "./core-model.ts";
|
|
17
|
+
import { fold } from "./fold.ts";
|
|
18
|
+
import { admits, canonicalInteger, type IntegerInterval } from "./numeric.ts";
|
|
19
|
+
import {
|
|
20
|
+
interval,
|
|
21
|
+
loopBounds,
|
|
22
|
+
maxLoopIterations,
|
|
23
|
+
narrowRanges,
|
|
24
|
+
valueRange,
|
|
25
|
+
} from "./ranges.ts";
|
|
26
|
+
import { instancedBlock, integerRange } from "./types.ts";
|
|
27
|
+
import {
|
|
28
|
+
aggregate,
|
|
29
|
+
expressionIssue,
|
|
30
|
+
placeType,
|
|
31
|
+
valueFits,
|
|
32
|
+
} from "./aggregates.ts";
|
|
33
|
+
import { reject } from "./normalization-error.ts";
|
|
34
|
+
import type { ModuleDeclarations } from "./declarations.ts";
|
|
35
|
+
|
|
36
|
+
type Flow = "fallthrough" | "return" | "loop" | "mixed";
|
|
37
|
+
interface FlowState {
|
|
38
|
+
flow: Flow;
|
|
39
|
+
initialized: Set<string>;
|
|
40
|
+
}
|
|
41
|
+
interface BodyResult extends FlowState {
|
|
42
|
+
body: CoreStatement[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Only paths that reach the next statement participate in a join. A local is
|
|
46
|
+
// definitely initialized when every such path initialized it; branches that
|
|
47
|
+
// return or leave the loop do not constrain it.
|
|
48
|
+
const join = (
|
|
49
|
+
branches: readonly [BodyResult, ...BodyResult[]],
|
|
50
|
+
initialized: Set<string>,
|
|
51
|
+
): FlowState => {
|
|
52
|
+
const [falling, ...others] = branches.filter(
|
|
53
|
+
(branch) => branch.flow === "fallthrough",
|
|
54
|
+
);
|
|
55
|
+
if (falling) {
|
|
56
|
+
return {
|
|
57
|
+
flow: "fallthrough",
|
|
58
|
+
initialized: new Set(
|
|
59
|
+
[...falling.initialized].filter((id) =>
|
|
60
|
+
others.every((branch) => branch.initialized.has(id)),
|
|
61
|
+
),
|
|
62
|
+
),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const [{ flow }] = branches;
|
|
66
|
+
return {
|
|
67
|
+
flow: branches.every((branch) => branch.flow === flow) ? flow : "mixed",
|
|
68
|
+
initialized,
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// All mutable lowering state belongs to one invocation of one authored routine.
|
|
73
|
+
// sourceScope admits authored places; scope additionally contains captured temps.
|
|
74
|
+
export class RoutineNormalizer {
|
|
75
|
+
private readonly fn: PlcFunction;
|
|
76
|
+
|
|
77
|
+
private readonly block: FunctionBlock | undefined;
|
|
78
|
+
private readonly owner: FunctionBlock | undefined;
|
|
79
|
+
|
|
80
|
+
private readonly scope: Map<string, Variable>;
|
|
81
|
+
|
|
82
|
+
private readonly sourceScope: Map<string, Variable>;
|
|
83
|
+
|
|
84
|
+
private readonly locals: Variable[];
|
|
85
|
+
|
|
86
|
+
private readonly ranges = new Map<string, IntegerInterval>();
|
|
87
|
+
|
|
88
|
+
private readonly counters = new Set<string>();
|
|
89
|
+
|
|
90
|
+
private readonly callees = new Set<string>();
|
|
91
|
+
|
|
92
|
+
private temporaryCount = 0;
|
|
93
|
+
|
|
94
|
+
constructor(
|
|
95
|
+
routine: PlcFunction | FunctionBlock,
|
|
96
|
+
private readonly declarations: ModuleDeclarations,
|
|
97
|
+
) {
|
|
98
|
+
this.block = "inputs" in routine ? routine : undefined;
|
|
99
|
+
this.owner =
|
|
100
|
+
this.block ??
|
|
101
|
+
("owner" in routine && routine.owner
|
|
102
|
+
? declarations.blocks.get(routine.owner.block)
|
|
103
|
+
: undefined);
|
|
104
|
+
const parameters =
|
|
105
|
+
"parameters" in routine ? routine.parameters : routine.inputs;
|
|
106
|
+
this.fn = {
|
|
107
|
+
id: routine.id,
|
|
108
|
+
name: routine.name,
|
|
109
|
+
...("owner" in routine && routine.owner && { owner: routine.owner }),
|
|
110
|
+
...("origin" in routine && routine.origin && { origin: routine.origin }),
|
|
111
|
+
parameters,
|
|
112
|
+
locals: routine.locals,
|
|
113
|
+
body: routine.body,
|
|
114
|
+
span: routine.span,
|
|
115
|
+
...(routine.result && { result: routine.result }),
|
|
116
|
+
};
|
|
117
|
+
this.scope = new Map(declarations.globals);
|
|
118
|
+
for (const item of [
|
|
119
|
+
...parameters,
|
|
120
|
+
...(this.owner ? [...this.owner.state, ...this.owner.outputs] : []),
|
|
121
|
+
...routine.locals,
|
|
122
|
+
]) {
|
|
123
|
+
this.scope.set(item.id, item);
|
|
124
|
+
}
|
|
125
|
+
this.sourceScope = new Map(this.scope);
|
|
126
|
+
this.locals = [...routine.locals];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
normalize(): {
|
|
130
|
+
routine: CoreFunction | CoreFunctionBlock;
|
|
131
|
+
callees: ReadonlySet<string>;
|
|
132
|
+
} {
|
|
133
|
+
if (this.block?.standard) {
|
|
134
|
+
return { routine: { ...this.block, body: [] }, callees: this.callees };
|
|
135
|
+
}
|
|
136
|
+
const normalized = this.body(
|
|
137
|
+
this.fn.body,
|
|
138
|
+
new Set(this.fn.parameters.map((item) => item.id)),
|
|
139
|
+
);
|
|
140
|
+
if (this.fn.result && normalized.flow !== "return") {
|
|
141
|
+
reject("A typed helper must return on every path.", this.fn.span);
|
|
142
|
+
}
|
|
143
|
+
const routine = this.block
|
|
144
|
+
? { ...this.block, locals: this.locals, body: normalized.body }
|
|
145
|
+
: { ...this.fn, locals: this.locals, body: normalized.body };
|
|
146
|
+
return { routine, callees: this.callees };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private temporary(type: string, span: SourceSpan): Variable {
|
|
150
|
+
let id: string;
|
|
151
|
+
do {
|
|
152
|
+
id = `${this.fn.id}:temporary:${this.temporaryCount++}`;
|
|
153
|
+
} while (this.declarations.hasId(id));
|
|
154
|
+
this.declarations.claimId(id);
|
|
155
|
+
const item: Variable = {
|
|
156
|
+
id,
|
|
157
|
+
name: `temporary_${this.temporaryCount}`,
|
|
158
|
+
type,
|
|
159
|
+
storage: "local",
|
|
160
|
+
mutable: true,
|
|
161
|
+
retain: false,
|
|
162
|
+
span,
|
|
163
|
+
};
|
|
164
|
+
this.scope.set(id, item);
|
|
165
|
+
this.locals.push(item);
|
|
166
|
+
return item;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private load(variable: Variable, span: SourceSpan): CoreExpression {
|
|
170
|
+
return { kind: "load", root: variable.id, type: variable.type, span };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The block a method or update receiver instantiates: a static instance, or an InOut reference to one. */
|
|
174
|
+
private receiverBlock(id: string): string | undefined {
|
|
175
|
+
const variable = this.sourceScope.get(id);
|
|
176
|
+
if (variable?.storage !== "instance" && variable?.mode !== "inOut") {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
return instancedBlock(variable.type, this.declarations.types)?.block;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private verifyCall(
|
|
183
|
+
id: string,
|
|
184
|
+
arguments_: readonly Expression[],
|
|
185
|
+
span: SourceSpan,
|
|
186
|
+
instance: string | undefined,
|
|
187
|
+
): PlcFunction {
|
|
188
|
+
const called = this.declarations.signatures.get(id);
|
|
189
|
+
if (!called || called.parameters.length !== arguments_.length) {
|
|
190
|
+
reject("Unknown helper or wrong argument count.", span);
|
|
191
|
+
}
|
|
192
|
+
if (called.owner) {
|
|
193
|
+
if (instance) {
|
|
194
|
+
if (this.receiverBlock(instance) !== called.owner.block) {
|
|
195
|
+
reject(
|
|
196
|
+
"Method calls require a matching static block instance.",
|
|
197
|
+
span,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
} else if (this.owner?.id !== called.owner.block) {
|
|
201
|
+
reject(
|
|
202
|
+
"An implicit method call requires its owning block scope.",
|
|
203
|
+
span,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
if (
|
|
207
|
+
called.owner.access !== "public" &&
|
|
208
|
+
this.owner?.id !== called.owner.block
|
|
209
|
+
) {
|
|
210
|
+
reject(
|
|
211
|
+
"Private and protected methods require their owning block scope.",
|
|
212
|
+
span,
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
} else if (instance) {
|
|
216
|
+
reject("Ordinary functions cannot have an instance receiver.", span);
|
|
217
|
+
}
|
|
218
|
+
this.callees.add(id);
|
|
219
|
+
arguments_.forEach((argument, index) => {
|
|
220
|
+
if (argument.type !== called.parameters[index]?.type) {
|
|
221
|
+
reject("Helper argument has the wrong PLC type.", argument.span);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
return called;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private expression(
|
|
228
|
+
value: Expression,
|
|
229
|
+
initialized: Set<string>,
|
|
230
|
+
output: CoreStatement[],
|
|
231
|
+
allowAggregateLoad = false,
|
|
232
|
+
): CoreExpression {
|
|
233
|
+
const type = this.declarations.types.get(value.type);
|
|
234
|
+
if (!type || !this.declarations.isValueType(value.type)) {
|
|
235
|
+
reject("Unknown or non-value expression type.", value.span);
|
|
236
|
+
}
|
|
237
|
+
if (value.kind === "borrow") {
|
|
238
|
+
reject(
|
|
239
|
+
"Borrowed places are only valid in block update arguments.",
|
|
240
|
+
value.span,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
const common = { type: value.type, span: value.span };
|
|
244
|
+
if (value.kind === "length") {
|
|
245
|
+
const array = this.declarations.types.get(value.operand.type);
|
|
246
|
+
if (array?.kind !== "array" || value.type !== "DINT") {
|
|
247
|
+
reject("Length requires a fixed array and DINT result.", value.span);
|
|
248
|
+
}
|
|
249
|
+
if (value.operand.kind === "load") {
|
|
250
|
+
const source = this.sourceScope.get(value.operand.place.root);
|
|
251
|
+
const checked = placeType(
|
|
252
|
+
value.operand.place,
|
|
253
|
+
this.declarations.types,
|
|
254
|
+
this.sourceScope,
|
|
255
|
+
this.ranges,
|
|
256
|
+
);
|
|
257
|
+
if (
|
|
258
|
+
!source ||
|
|
259
|
+
checked.issue ||
|
|
260
|
+
checked.type !== value.operand.type ||
|
|
261
|
+
(source.storage === "local" && !initialized.has(source.id))
|
|
262
|
+
) {
|
|
263
|
+
reject(checked.issue ?? "Invalid array receiver.", value.span);
|
|
264
|
+
}
|
|
265
|
+
this.normalizePath(value.operand.place, initialized, output);
|
|
266
|
+
} else {
|
|
267
|
+
this.expression(value.operand, initialized, output, true);
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
...common,
|
|
271
|
+
kind: "constant",
|
|
272
|
+
value: { kind: "integer", value: String(array.length) },
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
if (value.kind === "conditional") {
|
|
276
|
+
if (
|
|
277
|
+
value.condition.type !== "BOOL" ||
|
|
278
|
+
value.then.type !== value.type ||
|
|
279
|
+
value.else.type !== value.type
|
|
280
|
+
) {
|
|
281
|
+
reject("Invalid conditional types.", value.span);
|
|
282
|
+
}
|
|
283
|
+
const condition = this.expression(value.condition, initialized, output);
|
|
284
|
+
const result = this.temporary(value.type, value.span);
|
|
285
|
+
const branch = (operand: Expression): CoreStatement[] => {
|
|
286
|
+
const statements: CoreStatement[] = [];
|
|
287
|
+
const evaluated = this.expression(
|
|
288
|
+
operand,
|
|
289
|
+
new Set(initialized),
|
|
290
|
+
statements,
|
|
291
|
+
);
|
|
292
|
+
statements.push({
|
|
293
|
+
kind: "assign",
|
|
294
|
+
root: result.id,
|
|
295
|
+
value: evaluated,
|
|
296
|
+
span: value.span,
|
|
297
|
+
});
|
|
298
|
+
return statements;
|
|
299
|
+
};
|
|
300
|
+
output.push({
|
|
301
|
+
kind: "if",
|
|
302
|
+
condition,
|
|
303
|
+
// biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
|
|
304
|
+
then: branch(value.then),
|
|
305
|
+
else: branch(value.else),
|
|
306
|
+
span: value.span,
|
|
307
|
+
});
|
|
308
|
+
initialized.add(result.id);
|
|
309
|
+
return this.load(result, value.span);
|
|
310
|
+
}
|
|
311
|
+
if (value.kind === "call") {
|
|
312
|
+
const called = this.verifyCall(
|
|
313
|
+
value.function,
|
|
314
|
+
value.arguments,
|
|
315
|
+
value.span,
|
|
316
|
+
value.instance,
|
|
317
|
+
);
|
|
318
|
+
if (called.result !== value.type) {
|
|
319
|
+
reject("Expression call requires a matching result type.", value.span);
|
|
320
|
+
}
|
|
321
|
+
const index =
|
|
322
|
+
value.instance &&
|
|
323
|
+
this.instanceIndex(
|
|
324
|
+
value.instance,
|
|
325
|
+
value.index,
|
|
326
|
+
initialized,
|
|
327
|
+
output,
|
|
328
|
+
value.span,
|
|
329
|
+
);
|
|
330
|
+
const arguments_ = this.arguments(
|
|
331
|
+
called.parameters,
|
|
332
|
+
value.arguments,
|
|
333
|
+
initialized,
|
|
334
|
+
output,
|
|
335
|
+
);
|
|
336
|
+
const result = this.temporary(value.type, value.span);
|
|
337
|
+
output.push({
|
|
338
|
+
kind: "invoke",
|
|
339
|
+
function: value.function,
|
|
340
|
+
...(value.instance && { instance: value.instance }),
|
|
341
|
+
...(index && { index }),
|
|
342
|
+
arguments: arguments_,
|
|
343
|
+
destination: result.id,
|
|
344
|
+
span: value.span,
|
|
345
|
+
});
|
|
346
|
+
initialized.add(result.id);
|
|
347
|
+
return this.load(result, value.span);
|
|
348
|
+
}
|
|
349
|
+
const issue = expressionIssue(
|
|
350
|
+
value,
|
|
351
|
+
this.declarations.types,
|
|
352
|
+
this.scope,
|
|
353
|
+
this.ranges,
|
|
354
|
+
);
|
|
355
|
+
if (issue) {
|
|
356
|
+
reject(issue, value.span);
|
|
357
|
+
}
|
|
358
|
+
switch (value.kind) {
|
|
359
|
+
case "copy":
|
|
360
|
+
return this.expression(value.operand, initialized, output, true);
|
|
361
|
+
case "record":
|
|
362
|
+
case "array": {
|
|
363
|
+
const result = this.temporary(value.type, value.span);
|
|
364
|
+
if (value.kind === "record") {
|
|
365
|
+
for (const [name, operand] of Object.entries(value.fields)) {
|
|
366
|
+
output.push({
|
|
367
|
+
kind: "assign",
|
|
368
|
+
root: result.id,
|
|
369
|
+
path: [{ kind: "field", name }],
|
|
370
|
+
value: this.expression(operand, initialized, output),
|
|
371
|
+
span: operand.span,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
} else {
|
|
375
|
+
if (type.kind !== "array") {
|
|
376
|
+
reject("Invalid array construction.", value.span);
|
|
377
|
+
}
|
|
378
|
+
value.items.forEach((operand, index) => {
|
|
379
|
+
output.push({
|
|
380
|
+
kind: "assign",
|
|
381
|
+
root: result.id,
|
|
382
|
+
path: [
|
|
383
|
+
{
|
|
384
|
+
kind: "index",
|
|
385
|
+
value: {
|
|
386
|
+
kind: "constant",
|
|
387
|
+
type: "DINT",
|
|
388
|
+
value: {
|
|
389
|
+
kind: "integer",
|
|
390
|
+
value: String(type.lowerBound + index),
|
|
391
|
+
},
|
|
392
|
+
span: operand.span,
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
],
|
|
396
|
+
value: this.expression(operand, initialized, output),
|
|
397
|
+
span: operand.span,
|
|
398
|
+
});
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
initialized.add(result.id);
|
|
402
|
+
return this.load(result, value.span);
|
|
403
|
+
}
|
|
404
|
+
case "constant": {
|
|
405
|
+
if (!aggregate(type)) {
|
|
406
|
+
return { ...common, kind: "constant", value: value.value };
|
|
407
|
+
}
|
|
408
|
+
const result = this.temporary(value.type, value.span);
|
|
409
|
+
output.push({
|
|
410
|
+
kind: "assign",
|
|
411
|
+
root: result.id,
|
|
412
|
+
value: { ...common, kind: "constant", value: value.value },
|
|
413
|
+
span: value.span,
|
|
414
|
+
});
|
|
415
|
+
initialized.add(result.id);
|
|
416
|
+
return this.load(result, value.span);
|
|
417
|
+
}
|
|
418
|
+
case "load": {
|
|
419
|
+
const source = this.sourceScope.get(value.place.root);
|
|
420
|
+
const checked = placeType(
|
|
421
|
+
value.place,
|
|
422
|
+
this.declarations.types,
|
|
423
|
+
this.sourceScope,
|
|
424
|
+
this.ranges,
|
|
425
|
+
);
|
|
426
|
+
if (!source || checked.issue || checked.type !== value.type) {
|
|
427
|
+
reject(checked.issue ?? "Invalid PLC load.", value.span);
|
|
428
|
+
}
|
|
429
|
+
if (aggregate(type) && !allowAggregateLoad) {
|
|
430
|
+
reject(
|
|
431
|
+
"Aggregate loads require explicit copy or a readonly value argument.",
|
|
432
|
+
value.span,
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
if (source.storage === "local" && !initialized.has(source.id)) {
|
|
436
|
+
reject("Local read before initialization.", value.span);
|
|
437
|
+
}
|
|
438
|
+
// The virtual .out view is not a vendor record value. Snapshot its
|
|
439
|
+
// declared fields, rather than copying the whole FB instance.
|
|
440
|
+
const first = value.place.path[0];
|
|
441
|
+
if (
|
|
442
|
+
source.storage === "instance" &&
|
|
443
|
+
value.place.path.length === 1 &&
|
|
444
|
+
first?.kind === "field" &&
|
|
445
|
+
first.name === "out" &&
|
|
446
|
+
type.kind === "record"
|
|
447
|
+
) {
|
|
448
|
+
const result = this.temporary(value.type, value.span);
|
|
449
|
+
for (const field of type.fields) {
|
|
450
|
+
const selected: Expression = {
|
|
451
|
+
kind: "load",
|
|
452
|
+
type: field.type,
|
|
453
|
+
place: {
|
|
454
|
+
root: source.id,
|
|
455
|
+
path: [
|
|
456
|
+
...value.place.path,
|
|
457
|
+
{ kind: "field", name: field.name },
|
|
458
|
+
],
|
|
459
|
+
},
|
|
460
|
+
span: value.span,
|
|
461
|
+
};
|
|
462
|
+
const captured = this.expression(
|
|
463
|
+
selected,
|
|
464
|
+
initialized,
|
|
465
|
+
output,
|
|
466
|
+
true,
|
|
467
|
+
);
|
|
468
|
+
output.push({
|
|
469
|
+
kind: "assign",
|
|
470
|
+
root: result.id,
|
|
471
|
+
path: [{ kind: "field", name: field.name }],
|
|
472
|
+
value: captured,
|
|
473
|
+
span: value.span,
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
initialized.add(result.id);
|
|
477
|
+
return this.load(result, value.span);
|
|
478
|
+
}
|
|
479
|
+
const path = this.normalizePath(value.place, initialized, output);
|
|
480
|
+
if (source.storage === "constant" && source.initial && !path.length) {
|
|
481
|
+
return { ...common, kind: "constant", value: source.initial };
|
|
482
|
+
}
|
|
483
|
+
// Capture now, before a later operand can invoke an effectful helper.
|
|
484
|
+
const result = this.temporary(value.type, value.span);
|
|
485
|
+
output.push({
|
|
486
|
+
kind: "assign",
|
|
487
|
+
root: result.id,
|
|
488
|
+
value: {
|
|
489
|
+
...common,
|
|
490
|
+
kind: "load",
|
|
491
|
+
root: source.id,
|
|
492
|
+
...(path.length && { path }),
|
|
493
|
+
},
|
|
494
|
+
span: value.span,
|
|
495
|
+
});
|
|
496
|
+
const bound = interval(
|
|
497
|
+
value,
|
|
498
|
+
this.declarations.types,
|
|
499
|
+
this.scope,
|
|
500
|
+
this.ranges,
|
|
501
|
+
);
|
|
502
|
+
if (bound) {
|
|
503
|
+
this.ranges.set(result.id, bound);
|
|
504
|
+
}
|
|
505
|
+
initialized.add(result.id);
|
|
506
|
+
return this.load(result, value.span);
|
|
507
|
+
}
|
|
508
|
+
case "unary": {
|
|
509
|
+
const operand = this.expression(value.operand, initialized, output);
|
|
510
|
+
const constant = fold(value, this.scope, this.declarations.types);
|
|
511
|
+
return constant
|
|
512
|
+
? { ...common, kind: "constant", value: constant }
|
|
513
|
+
: { ...common, kind: "unary", operator: value.operator, operand };
|
|
514
|
+
}
|
|
515
|
+
case "convert": {
|
|
516
|
+
const operand = this.expression(value.operand, initialized, output);
|
|
517
|
+
const constant = fold(value, this.scope, this.declarations.types);
|
|
518
|
+
if (value.type === value.operand.type) {
|
|
519
|
+
return operand;
|
|
520
|
+
}
|
|
521
|
+
return constant
|
|
522
|
+
? { ...common, kind: "constant", value: constant }
|
|
523
|
+
: { ...common, kind: "convert", operand, policy: value.policy };
|
|
524
|
+
}
|
|
525
|
+
case "binary": {
|
|
526
|
+
const left = this.expression(value.left, initialized, output);
|
|
527
|
+
if (value.operator === "and" || value.operator === "or") {
|
|
528
|
+
const result = this.temporary("BOOL", value.span);
|
|
529
|
+
const branch: CoreStatement[] = [];
|
|
530
|
+
output.push({
|
|
531
|
+
kind: "assign",
|
|
532
|
+
root: result.id,
|
|
533
|
+
value: left,
|
|
534
|
+
span: value.span,
|
|
535
|
+
});
|
|
536
|
+
const right = this.expression(
|
|
537
|
+
value.right,
|
|
538
|
+
new Set(initialized),
|
|
539
|
+
branch,
|
|
540
|
+
);
|
|
541
|
+
branch.push({
|
|
542
|
+
kind: "assign",
|
|
543
|
+
root: result.id,
|
|
544
|
+
value: right,
|
|
545
|
+
span: value.span,
|
|
546
|
+
});
|
|
547
|
+
const condition: CoreExpression =
|
|
548
|
+
value.operator === "and"
|
|
549
|
+
? this.load(result, value.span)
|
|
550
|
+
: {
|
|
551
|
+
kind: "unary",
|
|
552
|
+
operator: "not",
|
|
553
|
+
operand: this.load(result, value.span),
|
|
554
|
+
type: "BOOL",
|
|
555
|
+
span: value.span,
|
|
556
|
+
};
|
|
557
|
+
output.push({
|
|
558
|
+
kind: "if",
|
|
559
|
+
condition,
|
|
560
|
+
// biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
|
|
561
|
+
then: branch,
|
|
562
|
+
else: [],
|
|
563
|
+
span: value.span,
|
|
564
|
+
});
|
|
565
|
+
initialized.add(result.id);
|
|
566
|
+
return this.load(result, value.span);
|
|
567
|
+
}
|
|
568
|
+
const right = this.expression(value.right, initialized, output);
|
|
569
|
+
const constant = fold(value, this.scope, this.declarations.types);
|
|
570
|
+
if (constant) {
|
|
571
|
+
return { ...common, kind: "constant", value: constant };
|
|
572
|
+
}
|
|
573
|
+
return {
|
|
574
|
+
...common,
|
|
575
|
+
kind: "binary",
|
|
576
|
+
operator: value.operator,
|
|
577
|
+
left,
|
|
578
|
+
right,
|
|
579
|
+
...(value.policy && { policy: value.policy }),
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
private normalizePath(
|
|
586
|
+
place: Place,
|
|
587
|
+
initialized: Set<string>,
|
|
588
|
+
output: CoreStatement[],
|
|
589
|
+
): CorePath {
|
|
590
|
+
// An index is evaluated and captured once, before later arguments can change
|
|
591
|
+
// its source storage. The resulting path denotes that selected cell.
|
|
592
|
+
return place.path.map((segment) =>
|
|
593
|
+
segment.kind === "field"
|
|
594
|
+
? segment
|
|
595
|
+
: {
|
|
596
|
+
kind: "index",
|
|
597
|
+
value: this.expression(segment.value, initialized, output),
|
|
598
|
+
},
|
|
599
|
+
);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
private destination(
|
|
603
|
+
place: Place,
|
|
604
|
+
type: string,
|
|
605
|
+
initialized: Set<string>,
|
|
606
|
+
span: SourceSpan,
|
|
607
|
+
): Variable {
|
|
608
|
+
const target = this.sourceScope.get(place.root);
|
|
609
|
+
const initializing =
|
|
610
|
+
target?.storage === "local" &&
|
|
611
|
+
!initialized.has(place.root) &&
|
|
612
|
+
!place.path.length;
|
|
613
|
+
const checked = placeType(
|
|
614
|
+
place,
|
|
615
|
+
this.declarations.types,
|
|
616
|
+
this.sourceScope,
|
|
617
|
+
this.ranges,
|
|
618
|
+
!initializing,
|
|
619
|
+
);
|
|
620
|
+
if (
|
|
621
|
+
!target ||
|
|
622
|
+
checked.issue ||
|
|
623
|
+
checked.type !== type ||
|
|
624
|
+
this.counters.has(place.root)
|
|
625
|
+
) {
|
|
626
|
+
reject(checked.issue ?? "Invalid assignment destination.", span);
|
|
627
|
+
}
|
|
628
|
+
if (
|
|
629
|
+
place.path.length &&
|
|
630
|
+
target.storage === "local" &&
|
|
631
|
+
!initialized.has(target.id)
|
|
632
|
+
) {
|
|
633
|
+
reject("Initialize the whole aggregate before assigning fields.", span);
|
|
634
|
+
}
|
|
635
|
+
return target;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
private instanceIndex(
|
|
639
|
+
instance: string,
|
|
640
|
+
index: Expression | undefined,
|
|
641
|
+
initialized: Set<string>,
|
|
642
|
+
output: CoreStatement[],
|
|
643
|
+
span: SourceSpan,
|
|
644
|
+
): CoreExpression | undefined {
|
|
645
|
+
if (!index) {
|
|
646
|
+
return undefined;
|
|
647
|
+
}
|
|
648
|
+
const checked = placeType(
|
|
649
|
+
{ root: instance, path: [{ kind: "index", value: index }] },
|
|
650
|
+
this.declarations.types,
|
|
651
|
+
this.sourceScope,
|
|
652
|
+
this.ranges,
|
|
653
|
+
);
|
|
654
|
+
if (checked.issue) {
|
|
655
|
+
reject(checked.issue, span);
|
|
656
|
+
}
|
|
657
|
+
return this.expression(index, initialized, output);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
private blockUpdate(
|
|
661
|
+
item: Extract<Statement, { kind: "blockUpdate" }>,
|
|
662
|
+
initialized: Set<string>,
|
|
663
|
+
output: CoreStatement[],
|
|
664
|
+
): void {
|
|
665
|
+
const block = this.receiverBlock(item.instance);
|
|
666
|
+
const called = block && this.declarations.blocks.get(block);
|
|
667
|
+
if (!called) {
|
|
668
|
+
reject(
|
|
669
|
+
"Updates require a statically allocated block instance.",
|
|
670
|
+
item.span,
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
const index = this.instanceIndex(
|
|
674
|
+
item.instance,
|
|
675
|
+
item.index,
|
|
676
|
+
initialized,
|
|
677
|
+
output,
|
|
678
|
+
item.span,
|
|
679
|
+
);
|
|
680
|
+
// Input names are unique, so a matching count means every input appears exactly once.
|
|
681
|
+
const supplied = Object.entries(item.inputs).flatMap(([name, value]) => {
|
|
682
|
+
const input = called.inputs.find((port) => port.name === name);
|
|
683
|
+
return input ? [{ input, value }] : [];
|
|
684
|
+
});
|
|
685
|
+
if (
|
|
686
|
+
Object.keys(item.inputs).length !== called.inputs.length ||
|
|
687
|
+
supplied.length !== called.inputs.length
|
|
688
|
+
) {
|
|
689
|
+
reject(
|
|
690
|
+
"Block update must supply every named input exactly once.",
|
|
691
|
+
item.span,
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
this.callees.add(called.id);
|
|
695
|
+
// Inputs evaluate in source property order.
|
|
696
|
+
const inputs = Object.fromEntries(
|
|
697
|
+
supplied.map(({ input, value }) => [
|
|
698
|
+
input.name,
|
|
699
|
+
this.argument(input, value, initialized, output),
|
|
700
|
+
]),
|
|
701
|
+
);
|
|
702
|
+
output.push({
|
|
703
|
+
kind: "blockUpdate",
|
|
704
|
+
instance: item.instance,
|
|
705
|
+
...(index && { index }),
|
|
706
|
+
inputs,
|
|
707
|
+
span: item.span,
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// verifyCall has matched the argument count to the parameters.
|
|
712
|
+
private arguments(
|
|
713
|
+
parameters: readonly Variable[],
|
|
714
|
+
values: readonly Expression[],
|
|
715
|
+
initialized: Set<string>,
|
|
716
|
+
output: CoreStatement[],
|
|
717
|
+
): CoreExpression[] {
|
|
718
|
+
return values.map((value, index) =>
|
|
719
|
+
this.argument(parameters[index]!, value, initialized, output),
|
|
720
|
+
);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
private argument(
|
|
724
|
+
input: Variable,
|
|
725
|
+
value: Expression,
|
|
726
|
+
initialized: Set<string>,
|
|
727
|
+
output: CoreStatement[],
|
|
728
|
+
): CoreExpression {
|
|
729
|
+
if (value.type !== input.type) {
|
|
730
|
+
reject("Block input differs from its concrete PLC type.", value.span);
|
|
731
|
+
}
|
|
732
|
+
if (input.mode === "inOut" && value.kind === "borrow") {
|
|
733
|
+
return this.borrow(value, initialized, output);
|
|
734
|
+
}
|
|
735
|
+
if (input.mode === "inOut") {
|
|
736
|
+
// A value with no writable address reaches the callee through a temporary.
|
|
737
|
+
if (this.declarations.types.get(value.type)?.kind === "block") {
|
|
738
|
+
reject("Pass a block instance by name.", value.span);
|
|
739
|
+
}
|
|
740
|
+
const temporary = this.temporary(value.type, value.span);
|
|
741
|
+
output.push({
|
|
742
|
+
kind: "assign",
|
|
743
|
+
root: temporary.id,
|
|
744
|
+
value: this.expression(value, initialized, output),
|
|
745
|
+
span: value.span,
|
|
746
|
+
});
|
|
747
|
+
initialized.add(temporary.id);
|
|
748
|
+
return {
|
|
749
|
+
kind: "borrow",
|
|
750
|
+
type: value.type,
|
|
751
|
+
place: { root: temporary.id, path: [] },
|
|
752
|
+
span: value.span,
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
if (value.kind === "borrow") {
|
|
756
|
+
reject(
|
|
757
|
+
"Readonly value inputs cannot receive borrowed references.",
|
|
758
|
+
value.span,
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
return this.expression(value, initialized, output, true);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
private borrow(
|
|
765
|
+
value: Extract<Expression, { kind: "borrow" }>,
|
|
766
|
+
initialized: Set<string>,
|
|
767
|
+
output: CoreStatement[],
|
|
768
|
+
): CoreExpression {
|
|
769
|
+
const block = this.declarations.types.get(value.type)?.kind === "block";
|
|
770
|
+
const root = this.sourceScope.get(value.place.root);
|
|
771
|
+
const checked = placeType(
|
|
772
|
+
value.place,
|
|
773
|
+
this.declarations.types,
|
|
774
|
+
this.sourceScope,
|
|
775
|
+
this.ranges,
|
|
776
|
+
!block,
|
|
777
|
+
);
|
|
778
|
+
if (
|
|
779
|
+
!root ||
|
|
780
|
+
checked.issue ||
|
|
781
|
+
checked.type !== value.type ||
|
|
782
|
+
(!block && !this.declarations.isValueType(value.type)) ||
|
|
783
|
+
this.counters.has(root.id) ||
|
|
784
|
+
(root.storage === "local" && !initialized.has(root.id))
|
|
785
|
+
) {
|
|
786
|
+
reject(
|
|
787
|
+
checked.issue ??
|
|
788
|
+
"Borrow requires initialized mutable value storage of the exact type.",
|
|
789
|
+
value.span,
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
return {
|
|
793
|
+
kind: "borrow",
|
|
794
|
+
type: value.type,
|
|
795
|
+
place: {
|
|
796
|
+
root: value.place.root,
|
|
797
|
+
path: this.normalizePath(value.place, initialized, output),
|
|
798
|
+
},
|
|
799
|
+
span: value.span,
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
private body(
|
|
804
|
+
statements: readonly Statement[],
|
|
805
|
+
initialized: Set<string>,
|
|
806
|
+
): BodyResult {
|
|
807
|
+
const output: CoreStatement[] = [];
|
|
808
|
+
let state: FlowState = { flow: "fallthrough", initialized };
|
|
809
|
+
for (const item of statements) {
|
|
810
|
+
if (state.flow !== "fallthrough") {
|
|
811
|
+
break;
|
|
812
|
+
}
|
|
813
|
+
state = this.statement(item, state.initialized, output);
|
|
814
|
+
}
|
|
815
|
+
return { body: output, ...state };
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
private statement(
|
|
819
|
+
item: Statement,
|
|
820
|
+
initialized: Set<string>,
|
|
821
|
+
output: CoreStatement[],
|
|
822
|
+
): FlowState {
|
|
823
|
+
switch (item.kind) {
|
|
824
|
+
case "assign":
|
|
825
|
+
this.assign(item, initialized, output);
|
|
826
|
+
return { flow: "fallthrough", initialized };
|
|
827
|
+
case "blockUpdate":
|
|
828
|
+
this.blockUpdate(item, initialized, output);
|
|
829
|
+
return { flow: "fallthrough", initialized };
|
|
830
|
+
case "invoke":
|
|
831
|
+
this.invoke(item, initialized, output);
|
|
832
|
+
return { flow: "fallthrough", initialized };
|
|
833
|
+
case "if":
|
|
834
|
+
return this.ifStatement(item, initialized, output);
|
|
835
|
+
case "switch":
|
|
836
|
+
return this.switchStatement(item, initialized, output);
|
|
837
|
+
case "for":
|
|
838
|
+
this.forStatement(item, initialized, output);
|
|
839
|
+
return { flow: "fallthrough", initialized };
|
|
840
|
+
case "return": {
|
|
841
|
+
if (item.value?.type !== this.fn.result) {
|
|
842
|
+
reject("Return differs from the function signature.", item.span);
|
|
843
|
+
}
|
|
844
|
+
const value =
|
|
845
|
+
item.value && this.expression(item.value, initialized, output);
|
|
846
|
+
output.push({
|
|
847
|
+
kind: "return",
|
|
848
|
+
span: item.span,
|
|
849
|
+
...(value && { value }),
|
|
850
|
+
});
|
|
851
|
+
return { flow: "return", initialized };
|
|
852
|
+
}
|
|
853
|
+
case "telemetry":
|
|
854
|
+
this.telemetry(item, initialized, output);
|
|
855
|
+
return { flow: "fallthrough", initialized };
|
|
856
|
+
case "break":
|
|
857
|
+
case "continue":
|
|
858
|
+
// Every active bounded loop holds its counter.
|
|
859
|
+
if (!this.counters.size) {
|
|
860
|
+
reject("Loop control appears outside a bounded loop.", item.span);
|
|
861
|
+
}
|
|
862
|
+
output.push({ kind: item.kind, span: item.span });
|
|
863
|
+
return { flow: "loop", initialized };
|
|
864
|
+
default:
|
|
865
|
+
reject("Unknown PLC statement.");
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
private assign(
|
|
870
|
+
item: Extract<Statement, { kind: "assign" }>,
|
|
871
|
+
initialized: Set<string>,
|
|
872
|
+
output: CoreStatement[],
|
|
873
|
+
): void {
|
|
874
|
+
this.destination(item.destination, item.value.type, initialized, item.span);
|
|
875
|
+
const path = this.normalizePath(item.destination, initialized, output);
|
|
876
|
+
// A const is assigned once, so its initializer's range holds wherever it is read.
|
|
877
|
+
const bound =
|
|
878
|
+
!path.length &&
|
|
879
|
+
this.scope.get(item.destination.root)?.mutable === false &&
|
|
880
|
+
valueRange(
|
|
881
|
+
item.value,
|
|
882
|
+
this.declarations.types,
|
|
883
|
+
this.scope,
|
|
884
|
+
this.ranges,
|
|
885
|
+
this.declarations.signatures,
|
|
886
|
+
);
|
|
887
|
+
const value = this.expression(item.value, initialized, output);
|
|
888
|
+
if (bound) {
|
|
889
|
+
this.ranges.set(item.destination.root, bound);
|
|
890
|
+
}
|
|
891
|
+
if (!path.length) {
|
|
892
|
+
initialized.add(item.destination.root);
|
|
893
|
+
}
|
|
894
|
+
output.push({
|
|
895
|
+
kind: "assign",
|
|
896
|
+
root: item.destination.root,
|
|
897
|
+
...(path.length && { path }),
|
|
898
|
+
value,
|
|
899
|
+
span: item.span,
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
private invoke(
|
|
904
|
+
item: Extract<Statement, { kind: "invoke" }>,
|
|
905
|
+
initialized: Set<string>,
|
|
906
|
+
output: CoreStatement[],
|
|
907
|
+
): void {
|
|
908
|
+
const called = this.verifyCall(
|
|
909
|
+
item.function,
|
|
910
|
+
item.arguments,
|
|
911
|
+
item.span,
|
|
912
|
+
item.instance,
|
|
913
|
+
);
|
|
914
|
+
if (item.destination) {
|
|
915
|
+
if (item.destination.path.length || !called.result) {
|
|
916
|
+
reject("Invalid helper destination.", item.span);
|
|
917
|
+
}
|
|
918
|
+
this.destination(item.destination, called.result, initialized, item.span);
|
|
919
|
+
}
|
|
920
|
+
const index =
|
|
921
|
+
item.instance &&
|
|
922
|
+
this.instanceIndex(
|
|
923
|
+
item.instance,
|
|
924
|
+
item.index,
|
|
925
|
+
initialized,
|
|
926
|
+
output,
|
|
927
|
+
item.span,
|
|
928
|
+
);
|
|
929
|
+
// Each argument snapshots its reads before the next argument can invoke
|
|
930
|
+
// a helper that mutates shared state.
|
|
931
|
+
const arguments_ = this.arguments(
|
|
932
|
+
called.parameters,
|
|
933
|
+
item.arguments,
|
|
934
|
+
initialized,
|
|
935
|
+
output,
|
|
936
|
+
);
|
|
937
|
+
output.push({
|
|
938
|
+
kind: "invoke",
|
|
939
|
+
function: item.function,
|
|
940
|
+
...(item.instance && { instance: item.instance }),
|
|
941
|
+
...(index && { index }),
|
|
942
|
+
arguments: arguments_,
|
|
943
|
+
span: item.span,
|
|
944
|
+
...(item.destination && { destination: item.destination.root }),
|
|
945
|
+
});
|
|
946
|
+
if (item.destination) {
|
|
947
|
+
initialized.add(item.destination.root);
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
private ifStatement(
|
|
952
|
+
item: Extract<Statement, { kind: "if" }>,
|
|
953
|
+
initialized: Set<string>,
|
|
954
|
+
output: CoreStatement[],
|
|
955
|
+
): FlowState {
|
|
956
|
+
if (item.condition.type !== "BOOL") {
|
|
957
|
+
reject("Conditions require BOOL.", item.span);
|
|
958
|
+
}
|
|
959
|
+
const condition = this.expression(item.condition, initialized, output);
|
|
960
|
+
const originalRanges = new Map(this.ranges);
|
|
961
|
+
const branch = (statements: readonly Statement[], truth: boolean) => {
|
|
962
|
+
this.ranges.clear();
|
|
963
|
+
for (const [root, range] of narrowRanges(
|
|
964
|
+
item.condition,
|
|
965
|
+
truth,
|
|
966
|
+
this.declarations.types,
|
|
967
|
+
this.sourceScope,
|
|
968
|
+
originalRanges,
|
|
969
|
+
)) {
|
|
970
|
+
this.ranges.set(root, range);
|
|
971
|
+
}
|
|
972
|
+
try {
|
|
973
|
+
return this.body(statements, new Set(initialized));
|
|
974
|
+
} finally {
|
|
975
|
+
this.ranges.clear();
|
|
976
|
+
for (const [root, range] of originalRanges) {
|
|
977
|
+
this.ranges.set(root, range);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
};
|
|
981
|
+
const yes = branch(item.then, true);
|
|
982
|
+
const no = branch(item.else, false);
|
|
983
|
+
output.push({
|
|
984
|
+
kind: "if",
|
|
985
|
+
condition,
|
|
986
|
+
// biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
|
|
987
|
+
then: yes.body,
|
|
988
|
+
else: no.body,
|
|
989
|
+
span: item.span,
|
|
990
|
+
});
|
|
991
|
+
return join([yes, no], initialized);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
private switchStatement(
|
|
995
|
+
item: Extract<Statement, { kind: "switch" }>,
|
|
996
|
+
initialized: Set<string>,
|
|
997
|
+
output: CoreStatement[],
|
|
998
|
+
): FlowState {
|
|
999
|
+
const value = this.expression(item.value, initialized, output);
|
|
1000
|
+
// The expression above admitted the value's type.
|
|
1001
|
+
const type = this.declarations.types.get(item.value.type)!;
|
|
1002
|
+
const seen = new Set<string>();
|
|
1003
|
+
const cases = item.cases.map((branch) => {
|
|
1004
|
+
for (const constant of branch.values) {
|
|
1005
|
+
if (!valueFits(constant, type, this.declarations.types)) {
|
|
1006
|
+
reject("Invalid case constant.", item.span);
|
|
1007
|
+
}
|
|
1008
|
+
const key = JSON.stringify(constant);
|
|
1009
|
+
if (seen.has(key)) {
|
|
1010
|
+
reject("Duplicate case constant.", item.span);
|
|
1011
|
+
}
|
|
1012
|
+
seen.add(key);
|
|
1013
|
+
}
|
|
1014
|
+
return {
|
|
1015
|
+
values: branch.values,
|
|
1016
|
+
result: this.body(branch.body, new Set(initialized)),
|
|
1017
|
+
};
|
|
1018
|
+
});
|
|
1019
|
+
const fallback = this.body(item.fallback, new Set(initialized));
|
|
1020
|
+
output.push({
|
|
1021
|
+
kind: "switch",
|
|
1022
|
+
value,
|
|
1023
|
+
cases: cases.map((branch) => ({
|
|
1024
|
+
values: branch.values,
|
|
1025
|
+
body: branch.result.body,
|
|
1026
|
+
})),
|
|
1027
|
+
fallback: fallback.body,
|
|
1028
|
+
span: item.span,
|
|
1029
|
+
});
|
|
1030
|
+
return join(
|
|
1031
|
+
[fallback, ...cases.map((branch) => branch.result)],
|
|
1032
|
+
initialized,
|
|
1033
|
+
);
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
private forStatement(
|
|
1037
|
+
item: Extract<Statement, { kind: "for" }>,
|
|
1038
|
+
initialized: Set<string>,
|
|
1039
|
+
output: CoreStatement[],
|
|
1040
|
+
): void {
|
|
1041
|
+
const counter = this.sourceScope.get(item.counter);
|
|
1042
|
+
const counterType = counter && this.declarations.types.get(counter.type);
|
|
1043
|
+
const storage =
|
|
1044
|
+
item.counterScope === "existing"
|
|
1045
|
+
? ["local", "module", "field"]
|
|
1046
|
+
: ["local"];
|
|
1047
|
+
if (
|
|
1048
|
+
!counter ||
|
|
1049
|
+
!storage.includes(counter.storage) ||
|
|
1050
|
+
!counter.mutable ||
|
|
1051
|
+
counterType?.kind !== "integer" ||
|
|
1052
|
+
this.counters.has(counter.id) ||
|
|
1053
|
+
item.start.type !== counter.type ||
|
|
1054
|
+
item.limit.type !== counter.type ||
|
|
1055
|
+
!canonicalInteger.test(item.step) ||
|
|
1056
|
+
item.step === "0"
|
|
1057
|
+
) {
|
|
1058
|
+
reject("Invalid bounded loop.", item.span);
|
|
1059
|
+
}
|
|
1060
|
+
const start = fold(item.start, this.scope, this.declarations.types);
|
|
1061
|
+
const limit = fold(item.limit, this.scope, this.declarations.types);
|
|
1062
|
+
const startRange = interval(
|
|
1063
|
+
item.start,
|
|
1064
|
+
this.declarations.types,
|
|
1065
|
+
this.sourceScope,
|
|
1066
|
+
this.ranges,
|
|
1067
|
+
);
|
|
1068
|
+
const limitRange = interval(
|
|
1069
|
+
item.limit,
|
|
1070
|
+
this.declarations.types,
|
|
1071
|
+
this.sourceScope,
|
|
1072
|
+
this.ranges,
|
|
1073
|
+
);
|
|
1074
|
+
if (!startRange || !limitRange) {
|
|
1075
|
+
reject("Loop bounds require finite integer ranges.", item.span);
|
|
1076
|
+
}
|
|
1077
|
+
const step = BigInt(item.step);
|
|
1078
|
+
const { first, last, count, range } = loopBounds(
|
|
1079
|
+
startRange,
|
|
1080
|
+
limitRange,
|
|
1081
|
+
step,
|
|
1082
|
+
item.exclusive,
|
|
1083
|
+
);
|
|
1084
|
+
const dynamic = start?.kind !== "integer" || limit?.kind !== "integer";
|
|
1085
|
+
if (
|
|
1086
|
+
count > maxLoopIterations ||
|
|
1087
|
+
Number(count) !== item.maxIterations ||
|
|
1088
|
+
!Number.isSafeInteger(item.maxIterations)
|
|
1089
|
+
) {
|
|
1090
|
+
reject("Loop iteration proof differs from its bounds.", item.span);
|
|
1091
|
+
}
|
|
1092
|
+
if (item.counterScope === "existing") {
|
|
1093
|
+
const lastValue = step > 0n ? range[1] : range[0];
|
|
1094
|
+
const terminal = dynamic ? lastValue + step : first + count * step;
|
|
1095
|
+
if (count > 0n && !admits(integerRange(counterType), terminal)) {
|
|
1096
|
+
reject(
|
|
1097
|
+
"The existing loop counter overflows on termination.",
|
|
1098
|
+
item.span,
|
|
1099
|
+
);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
const evaluatedStart = this.expression(item.start, initialized, output);
|
|
1103
|
+
const evaluatedLimit = this.expression(item.limit, initialized, output);
|
|
1104
|
+
initialized.add(counter.id);
|
|
1105
|
+
this.counters.add(counter.id);
|
|
1106
|
+
this.ranges.set(counter.id, range);
|
|
1107
|
+
const nested = this.body(item.body, new Set(initialized));
|
|
1108
|
+
this.counters.delete(counter.id);
|
|
1109
|
+
this.ranges.delete(counter.id);
|
|
1110
|
+
if (item.counterScope !== "existing") {
|
|
1111
|
+
initialized.delete(counter.id);
|
|
1112
|
+
}
|
|
1113
|
+
output.push({
|
|
1114
|
+
kind: "for",
|
|
1115
|
+
counter: counter.id,
|
|
1116
|
+
...(item.counterScope && { counterScope: item.counterScope }),
|
|
1117
|
+
start: { kind: "integer", value: String(first) },
|
|
1118
|
+
limit: { kind: "integer", value: String(last) },
|
|
1119
|
+
...(dynamic && {
|
|
1120
|
+
dynamic: {
|
|
1121
|
+
start: evaluatedStart,
|
|
1122
|
+
limit: evaluatedLimit,
|
|
1123
|
+
...(item.exclusive && { exclusive: item.exclusive }),
|
|
1124
|
+
},
|
|
1125
|
+
}),
|
|
1126
|
+
step: item.step,
|
|
1127
|
+
maxIterations: item.maxIterations,
|
|
1128
|
+
body: nested.body,
|
|
1129
|
+
span: item.span,
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
private telemetry(
|
|
1134
|
+
item: Extract<Statement, { kind: "telemetry" }>,
|
|
1135
|
+
initialized: Set<string>,
|
|
1136
|
+
output: CoreStatement[],
|
|
1137
|
+
): void {
|
|
1138
|
+
const evaluate = (value: Expression) =>
|
|
1139
|
+
this.expression(value, initialized, output);
|
|
1140
|
+
const { record } = item;
|
|
1141
|
+
output.push({
|
|
1142
|
+
kind: "telemetry",
|
|
1143
|
+
record:
|
|
1144
|
+
record.kind === "exception"
|
|
1145
|
+
? { kind: "exception", code: evaluate(record.code) }
|
|
1146
|
+
: record,
|
|
1147
|
+
properties: item.properties.map(({ name, value, length }) => {
|
|
1148
|
+
if (!length) {
|
|
1149
|
+
return { name, value: evaluate(value) };
|
|
1150
|
+
}
|
|
1151
|
+
if (value.kind !== "borrow") {
|
|
1152
|
+
reject("Telemetry text reads a byte array in place.", value.span);
|
|
1153
|
+
}
|
|
1154
|
+
return {
|
|
1155
|
+
name,
|
|
1156
|
+
value: this.borrow(value, initialized, output),
|
|
1157
|
+
length: evaluate(length),
|
|
1158
|
+
};
|
|
1159
|
+
}),
|
|
1160
|
+
span: item.span,
|
|
1161
|
+
});
|
|
1162
|
+
}
|
|
1163
|
+
}
|