@tsplc/compiler 0.2.17 → 0.2.19
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,603 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Constant,
|
|
3
|
+
type CoreFunction,
|
|
4
|
+
type CoreFunctionBlock,
|
|
5
|
+
type CoreStatement,
|
|
6
|
+
defaultValue,
|
|
7
|
+
type GeneratedObject,
|
|
8
|
+
instancedBlock,
|
|
9
|
+
type SourceSpan,
|
|
10
|
+
type Variable,
|
|
11
|
+
} from "@tsplc/compiler/ir";
|
|
12
|
+
import type { SourceMapping } from "./emit.ts";
|
|
13
|
+
import {
|
|
14
|
+
expressionText,
|
|
15
|
+
placeText,
|
|
16
|
+
type StorageScope,
|
|
17
|
+
storageName,
|
|
18
|
+
} from "./expressions.ts";
|
|
19
|
+
import {
|
|
20
|
+
jobField,
|
|
21
|
+
jobTracking,
|
|
22
|
+
ringTarget,
|
|
23
|
+
type RoutineProbes,
|
|
24
|
+
writeProbes,
|
|
25
|
+
} from "./instrumentation.ts";
|
|
26
|
+
import {
|
|
27
|
+
type CodesysSymbols,
|
|
28
|
+
type EmissionContext,
|
|
29
|
+
instrumentationFieldTypes,
|
|
30
|
+
required,
|
|
31
|
+
} from "./symbols.ts";
|
|
32
|
+
import { callParameters } from "./telemetry.ts";
|
|
33
|
+
|
|
34
|
+
function routineHeader(
|
|
35
|
+
symbols: CodesysSymbols,
|
|
36
|
+
fn: CoreFunction | CoreFunctionBlock,
|
|
37
|
+
name: string,
|
|
38
|
+
): { kind: GeneratedObject["kind"]; declaration: string } {
|
|
39
|
+
if ("inputs" in fn) {
|
|
40
|
+
return { kind: "functionBlock", declaration: `FUNCTION_BLOCK ${name}` };
|
|
41
|
+
}
|
|
42
|
+
if (fn.owner) {
|
|
43
|
+
return {
|
|
44
|
+
kind: "method",
|
|
45
|
+
declaration: `METHOD ${fn.owner.access.toUpperCase()} ${name}${fn.result ? ` : ${symbols.targetTypes.name(fn.result)}` : ""}`,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (symbols.programRoutines.has(fn.id)) {
|
|
49
|
+
return { kind: "program", declaration: `PROGRAM ${name}` };
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
kind: "function",
|
|
53
|
+
declaration: `FUNCTION ${name} : ${fn.result ? symbols.targetTypes.name(fn.result) : "BOOL"}`,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A function, function block or method: its declaration, its body and, for a tracked job, its tracking method. */
|
|
58
|
+
export function emitRoutine(
|
|
59
|
+
context: EmissionContext,
|
|
60
|
+
fn: CoreFunction | CoreFunctionBlock,
|
|
61
|
+
): { objects: GeneratedObject[]; mappings: SourceMapping[] } {
|
|
62
|
+
const { plan, symbols, instrumentation } = context;
|
|
63
|
+
const { core } = plan;
|
|
64
|
+
const {
|
|
65
|
+
blockNames,
|
|
66
|
+
functionNames,
|
|
67
|
+
references,
|
|
68
|
+
targetTypes,
|
|
69
|
+
typeMap,
|
|
70
|
+
blocks,
|
|
71
|
+
blockFields,
|
|
72
|
+
retainedVariables,
|
|
73
|
+
} = symbols;
|
|
74
|
+
const objects: GeneratedObject[] = [];
|
|
75
|
+
const mappings: SourceMapping[] = [];
|
|
76
|
+
const block = "inputs" in fn ? fn : undefined;
|
|
77
|
+
const method = "owner" in fn ? fn.owner : undefined;
|
|
78
|
+
const program = symbols.programRoutines.has(fn.id);
|
|
79
|
+
const owner = method ? required(blocks.get(method.block)) : block;
|
|
80
|
+
const name = block
|
|
81
|
+
? required(blockNames.get(block.id))
|
|
82
|
+
: required(functionNames.get(fn.id));
|
|
83
|
+
const inputs = "parameters" in fn ? fn.parameters : fn.inputs;
|
|
84
|
+
const state = owner ? [...owner.outputs, ...owner.state] : [];
|
|
85
|
+
const mappingName = method ? fn.id : name;
|
|
86
|
+
const routineScope = required(symbols.routineScopes.get(fn.id));
|
|
87
|
+
const scope = new Map([...references, ...routineScope.references]);
|
|
88
|
+
const locals = routineScope.names;
|
|
89
|
+
const declarations = fn.locals.map(
|
|
90
|
+
(item) =>
|
|
91
|
+
` ${required(scope.get(item.id))} : ${targetTypes.name(item.type)};`,
|
|
92
|
+
);
|
|
93
|
+
const declarationSpans = fn.locals.map((item) => item.span);
|
|
94
|
+
const mutex =
|
|
95
|
+
method && owner?.runtime?.kind === "mailbox"
|
|
96
|
+
? required(symbols.mailboxLocks.get(owner.id))
|
|
97
|
+
: undefined;
|
|
98
|
+
const acquired = mutex ? locals.allocate("plc_acquired") : undefined;
|
|
99
|
+
const unavailable =
|
|
100
|
+
mutex && fn.result ? locals.allocate("plc_unavailable") : undefined;
|
|
101
|
+
if (acquired) {
|
|
102
|
+
declarations.push(` ${acquired} : BOOL;`);
|
|
103
|
+
declarationSpans.push(fn.span);
|
|
104
|
+
}
|
|
105
|
+
if (unavailable && fn.result) {
|
|
106
|
+
declarations.push(
|
|
107
|
+
` ${unavailable} : ${targetTypes.name(fn.result)} := ${targetTypes.literal(defaultValue(fn.result, typeMap), fn.result)};`,
|
|
108
|
+
);
|
|
109
|
+
declarationSpans.push(fn.span);
|
|
110
|
+
}
|
|
111
|
+
// Count target lines as they are appended; diagnostics use these exact offsets.
|
|
112
|
+
const lines: string[] = [];
|
|
113
|
+
const write = (line: string, depth: number, source?: SourceSpan): void => {
|
|
114
|
+
lines.push(" ".repeat(depth) + line);
|
|
115
|
+
if (source) {
|
|
116
|
+
mappings.push({
|
|
117
|
+
path: `${mappingName}/implementation.st`,
|
|
118
|
+
line: lines.length,
|
|
119
|
+
span: source,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// Only the holder releases, so a failed swap is spurious (see the acquire above); retry it.
|
|
125
|
+
const release = (lock: string, depth: number, source?: SourceSpan) => {
|
|
126
|
+
write(`WHILE NOT __COMPARE_AND_SWAP(ADR(${lock}), 1, 0) DO`, depth, source);
|
|
127
|
+
write(";", depth + 1);
|
|
128
|
+
write("END_WHILE;", depth);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const storage: StorageScope = {
|
|
132
|
+
symbols,
|
|
133
|
+
names: scope,
|
|
134
|
+
types: new Map(
|
|
135
|
+
[...core.variables, ...inputs, ...state, ...fn.locals].map((variable) => [
|
|
136
|
+
variable.id,
|
|
137
|
+
variable.type,
|
|
138
|
+
]),
|
|
139
|
+
),
|
|
140
|
+
};
|
|
141
|
+
const probes: RoutineProbes = {
|
|
142
|
+
context,
|
|
143
|
+
storage,
|
|
144
|
+
task: instrumentation.analysis.tasks.get(fn.id),
|
|
145
|
+
};
|
|
146
|
+
// Aggregate constants are expanded into legal leaf assignments for ST.
|
|
147
|
+
const assignConstant = (
|
|
148
|
+
destination: string,
|
|
149
|
+
value: Constant,
|
|
150
|
+
id: string,
|
|
151
|
+
depth: number,
|
|
152
|
+
span: SourceSpan,
|
|
153
|
+
): void => {
|
|
154
|
+
const type = required(typeMap.get(id));
|
|
155
|
+
if (type.kind === "record" && value.kind === "record") {
|
|
156
|
+
type.fields.forEach((field) => {
|
|
157
|
+
assignConstant(
|
|
158
|
+
`${destination}.${targetTypes.field(id, field.name)}`,
|
|
159
|
+
required(value.fields[field.name]),
|
|
160
|
+
field.type,
|
|
161
|
+
depth,
|
|
162
|
+
span,
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
} else if (type.kind === "array" && value.kind === "array") {
|
|
166
|
+
value.items.forEach((item, index) => {
|
|
167
|
+
assignConstant(
|
|
168
|
+
`${destination}[${type.lowerBound + index}]`,
|
|
169
|
+
item,
|
|
170
|
+
type.element,
|
|
171
|
+
depth,
|
|
172
|
+
span,
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
} else {
|
|
176
|
+
write(
|
|
177
|
+
`${destination} := ${targetTypes.literal(value, id)};`,
|
|
178
|
+
depth,
|
|
179
|
+
span,
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const tracked = block && instrumentation.fields.get(block.id);
|
|
185
|
+
const body = (items: readonly CoreStatement[], depth: number): void => {
|
|
186
|
+
for (const item of items) {
|
|
187
|
+
switch (item.kind) {
|
|
188
|
+
case "assign":
|
|
189
|
+
writeProbes(probes, write, item, depth);
|
|
190
|
+
if (
|
|
191
|
+
tracked?.step &&
|
|
192
|
+
block?.job &&
|
|
193
|
+
item.root === block.state.find((field) => field.name === "step")?.id
|
|
194
|
+
) {
|
|
195
|
+
write(
|
|
196
|
+
`${tracked.step} := ${jobField(storage, block, "step")};`,
|
|
197
|
+
depth,
|
|
198
|
+
item.span,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
if (item.value.kind === "constant") {
|
|
202
|
+
assignConstant(
|
|
203
|
+
placeText(storage, item.root, item.path),
|
|
204
|
+
item.value.value,
|
|
205
|
+
item.value.type,
|
|
206
|
+
depth,
|
|
207
|
+
item.span,
|
|
208
|
+
);
|
|
209
|
+
} else {
|
|
210
|
+
write(
|
|
211
|
+
`${placeText(storage, item.root, item.path)} := ${expressionText(storage, item.value)};`,
|
|
212
|
+
depth,
|
|
213
|
+
item.span,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
case "blockUpdate": {
|
|
218
|
+
const instance = instancedBlock(
|
|
219
|
+
required(storage.types.get(item.instance)),
|
|
220
|
+
typeMap,
|
|
221
|
+
);
|
|
222
|
+
if (!instance) {
|
|
223
|
+
throw new Error("Invalid verified block receiver");
|
|
224
|
+
}
|
|
225
|
+
const target = required(blocks.get(instance.block));
|
|
226
|
+
const arguments_ = Object.entries(item.inputs).map(
|
|
227
|
+
([input, value]) => {
|
|
228
|
+
const formal = required(
|
|
229
|
+
target.inputs.find((field) => field.name === input),
|
|
230
|
+
);
|
|
231
|
+
return `${required(required(blockFields.get(target.id)).get(formal.id))} := ${expressionText(storage, value)}`;
|
|
232
|
+
},
|
|
233
|
+
);
|
|
234
|
+
// Service calls report the job that made them: a single-instance job passes its own id,
|
|
235
|
+
// and everything between it and the call passes the id it was given.
|
|
236
|
+
const job = instrumentation.fields.get(target.id)?.job;
|
|
237
|
+
const id = instrumentation.jobIds.get(item.instance);
|
|
238
|
+
const given = owner && instrumentation.fields.get(owner.id)?.job;
|
|
239
|
+
if (job && (id || given)) {
|
|
240
|
+
arguments_.push(`${job} := ${id ? `UDINT#${id}` : given}`);
|
|
241
|
+
}
|
|
242
|
+
const receiver = item.index
|
|
243
|
+
? `${storageName(storage, item.instance)}[${expressionText(storage, item.index)}]`
|
|
244
|
+
: storageName(storage, item.instance);
|
|
245
|
+
write(`${receiver}(${arguments_.join(", ")});`, depth, item.span);
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
case "invoke": {
|
|
249
|
+
const receiver = item.instance
|
|
250
|
+
? `${storageName(storage, item.instance)}${item.index ? `[${expressionText(storage, item.index)}]` : ""}.`
|
|
251
|
+
: "";
|
|
252
|
+
const called = required(
|
|
253
|
+
core.functions.find((fn) => fn.id === item.function),
|
|
254
|
+
);
|
|
255
|
+
const programCall = symbols.programRoutines.has(called.id);
|
|
256
|
+
const arguments_ = item.arguments.map((value, index) => {
|
|
257
|
+
if (
|
|
258
|
+
!called.owner &&
|
|
259
|
+
!programCall &&
|
|
260
|
+
!called.parameters.some((parameter) => parameter.mode === "inOut")
|
|
261
|
+
) {
|
|
262
|
+
return expressionText(storage, value);
|
|
263
|
+
}
|
|
264
|
+
const parameter = required(called.parameters[index]);
|
|
265
|
+
const formal = required(
|
|
266
|
+
required(symbols.routineScopes.get(called.id)).references.get(
|
|
267
|
+
parameter.id,
|
|
268
|
+
),
|
|
269
|
+
);
|
|
270
|
+
return `${formal} := ${expressionText(storage, value)}`;
|
|
271
|
+
});
|
|
272
|
+
const call = `${receiver}${required(functionNames.get(item.function))}(${arguments_.join(", ")})`;
|
|
273
|
+
if (programCall) {
|
|
274
|
+
write(`${call};`, depth, item.span);
|
|
275
|
+
if (item.destination) {
|
|
276
|
+
write(
|
|
277
|
+
`${storageName(storage, item.destination)} := ${required(functionNames.get(called.id))}.${required(symbols.programResults.get(called.id))};`,
|
|
278
|
+
depth,
|
|
279
|
+
item.span,
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
write(
|
|
285
|
+
item.destination
|
|
286
|
+
? `${storageName(storage, item.destination)} := ${call};`
|
|
287
|
+
: `${call};`,
|
|
288
|
+
depth,
|
|
289
|
+
item.span,
|
|
290
|
+
);
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
case "if":
|
|
294
|
+
write(
|
|
295
|
+
`IF ${expressionText(storage, item.condition)} THEN`,
|
|
296
|
+
depth,
|
|
297
|
+
item.span,
|
|
298
|
+
);
|
|
299
|
+
body(item.then, depth + 1);
|
|
300
|
+
if (item.else.length) {
|
|
301
|
+
write("ELSE", depth, item.span);
|
|
302
|
+
body(item.else, depth + 1);
|
|
303
|
+
}
|
|
304
|
+
write("END_IF;", depth, item.span);
|
|
305
|
+
break;
|
|
306
|
+
case "switch": {
|
|
307
|
+
for (const [index, branch] of item.cases.entries()) {
|
|
308
|
+
const conditions = branch.values.map(
|
|
309
|
+
(value) =>
|
|
310
|
+
`(${expressionText(storage, item.value)} = ${targetTypes.literal(value, item.value.type)})`,
|
|
311
|
+
);
|
|
312
|
+
write(
|
|
313
|
+
`${index ? "ELSIF" : "IF"} ${conditions.join(" OR ")} THEN`,
|
|
314
|
+
depth,
|
|
315
|
+
item.span,
|
|
316
|
+
);
|
|
317
|
+
body(branch.body, depth + 1);
|
|
318
|
+
}
|
|
319
|
+
if (item.cases.length) {
|
|
320
|
+
if (item.fallback.length) {
|
|
321
|
+
write("ELSE", depth, item.span);
|
|
322
|
+
}
|
|
323
|
+
body(item.fallback, depth + 1);
|
|
324
|
+
write("END_IF;", depth, item.span);
|
|
325
|
+
} else {
|
|
326
|
+
body(item.fallback, depth);
|
|
327
|
+
}
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
case "for": {
|
|
331
|
+
// Iterate with a DINT ordinal rather than overflowing the authored
|
|
332
|
+
// counter at a width boundary. Reconstruct its typed value each scan.
|
|
333
|
+
const counterType = storage.types.get(item.counter);
|
|
334
|
+
if (!counterType || item.start.kind !== "integer") {
|
|
335
|
+
throw new Error("Invalid verified loop");
|
|
336
|
+
}
|
|
337
|
+
if (!item.maxIterations) {
|
|
338
|
+
if (item.counterScope === "existing") {
|
|
339
|
+
write(
|
|
340
|
+
`${storageName(storage, item.counter)} := ${item.dynamic ? expressionText(storage, item.dynamic.start) : targetTypes.literal(item.start, counterType)};`,
|
|
341
|
+
depth,
|
|
342
|
+
item.span,
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
const ordinal = locals.allocate("iteration");
|
|
348
|
+
declarations.push(` ${ordinal} : DINT;`);
|
|
349
|
+
declarationSpans.push(item.span);
|
|
350
|
+
const wide = counterType === "ULINT" ? "ULINT" : "LINT";
|
|
351
|
+
const ascending = BigInt(item.step) > 0n;
|
|
352
|
+
const step =
|
|
353
|
+
BigInt(item.step) < 0n ? -BigInt(item.step) : BigInt(item.step);
|
|
354
|
+
const { dynamic } = item;
|
|
355
|
+
const first = dynamic
|
|
356
|
+
? `TO_${wide}(${expressionText(storage, dynamic.start)})`
|
|
357
|
+
: `${wide}#${item.start.value}`;
|
|
358
|
+
// A dynamic range's iteration count is computed once, before the loop.
|
|
359
|
+
const countIterations = ({
|
|
360
|
+
limit,
|
|
361
|
+
exclusive,
|
|
362
|
+
}: NonNullable<typeof dynamic>): string => {
|
|
363
|
+
const last = `TO_${wide}(${expressionText(storage, limit)})`;
|
|
364
|
+
const count = locals.allocate("iterationCount");
|
|
365
|
+
declarations.push(` ${count} : DINT;`);
|
|
366
|
+
declarationSpans.push(item.span);
|
|
367
|
+
const comparison = ascending ? "<" : ">";
|
|
368
|
+
const inclusive = exclusive ? "" : "=";
|
|
369
|
+
const distance = ascending
|
|
370
|
+
? `${last} - ${first}`
|
|
371
|
+
: `${first} - ${last}`;
|
|
372
|
+
const excluded = exclusive ? ` - ${wide}#1` : "";
|
|
373
|
+
write(`${count} := DINT#0;`, depth, item.span);
|
|
374
|
+
write(
|
|
375
|
+
`IF ${first} ${comparison}${inclusive} ${last} THEN`,
|
|
376
|
+
depth,
|
|
377
|
+
item.span,
|
|
378
|
+
);
|
|
379
|
+
write(
|
|
380
|
+
`${count} := ${wide}_TO_DINT(((${distance}${excluded}) / ${wide}#${step}) + ${wide}#1);`,
|
|
381
|
+
depth + 1,
|
|
382
|
+
item.span,
|
|
383
|
+
);
|
|
384
|
+
write("END_IF;", depth, item.span);
|
|
385
|
+
return count;
|
|
386
|
+
};
|
|
387
|
+
const lastOrdinal = dynamic
|
|
388
|
+
? `${countIterations(dynamic)} - DINT#1`
|
|
389
|
+
: `DINT#${item.maxIterations - 1}`;
|
|
390
|
+
write(
|
|
391
|
+
`FOR ${ordinal} := DINT#0 TO ${lastOrdinal} DO`,
|
|
392
|
+
depth,
|
|
393
|
+
item.span,
|
|
394
|
+
);
|
|
395
|
+
const counterValue = `${storageName(storage, item.counter)} := TO_${counterType}(${first} ${ascending ? "+" : "-"} (${wide}#${step} * DINT_TO_${wide}(${ordinal})));`;
|
|
396
|
+
write(counterValue, depth + 1, item.span);
|
|
397
|
+
body(item.body, depth + 1);
|
|
398
|
+
write("END_FOR;", depth, item.span);
|
|
399
|
+
// CODESYS keeps the ordinal at EXIT and advances it on normal
|
|
400
|
+
// completion/CONTINUE. Reconstructing here preserves that distinction.
|
|
401
|
+
if (item.counterScope === "existing") {
|
|
402
|
+
write(counterValue, depth, item.span);
|
|
403
|
+
}
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
case "return":
|
|
407
|
+
writeProbes(probes, write, item, depth);
|
|
408
|
+
if (block) {
|
|
409
|
+
if (item.value) {
|
|
410
|
+
const returned = expressionText(storage, item.value);
|
|
411
|
+
for (const output of block.outputs) {
|
|
412
|
+
write(
|
|
413
|
+
`${storageName(storage, output.id)} := ${returned}.${targetTypes.field(required(block.result), output.name)};`,
|
|
414
|
+
depth,
|
|
415
|
+
item.span,
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
} else if (program) {
|
|
420
|
+
if (item.value) {
|
|
421
|
+
write(
|
|
422
|
+
`${required(symbols.programResults.get(fn.id))} := ${expressionText(storage, item.value)};`,
|
|
423
|
+
depth,
|
|
424
|
+
item.span,
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
} else if (!method || fn.result) {
|
|
428
|
+
write(
|
|
429
|
+
`${name} := ${item.value ? expressionText(storage, item.value) : "TRUE"};`,
|
|
430
|
+
depth,
|
|
431
|
+
item.span,
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
if (mutex) {
|
|
435
|
+
release(mutex, depth, item.span);
|
|
436
|
+
}
|
|
437
|
+
if (tracked?.track) {
|
|
438
|
+
write(`${tracked.track}();`, depth);
|
|
439
|
+
}
|
|
440
|
+
write("RETURN;", depth, item.span);
|
|
441
|
+
break;
|
|
442
|
+
case "break":
|
|
443
|
+
write("EXIT;", depth, item.span);
|
|
444
|
+
break;
|
|
445
|
+
case "continue":
|
|
446
|
+
write("CONTINUE;", depth, item.span);
|
|
447
|
+
break;
|
|
448
|
+
case "telemetry": {
|
|
449
|
+
const site = required(plan.telemetry?.calls.get(item));
|
|
450
|
+
const arguments_ = callParameters(item).map(
|
|
451
|
+
({ name, value }) => `${name} := ${expressionText(storage, value)}`,
|
|
452
|
+
);
|
|
453
|
+
write(
|
|
454
|
+
`${ringTarget(instrumentation, site.task)}.Record${site.id}(${arguments_.join(", ")});`,
|
|
455
|
+
depth,
|
|
456
|
+
item.span,
|
|
457
|
+
);
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
if (mutex && acquired) {
|
|
465
|
+
// ARM64 implements __COMPARE_AND_SWAP with load-linked/store-conditional, which can fail
|
|
466
|
+
// even when the word holds the expected value. Retry while the lock reads free.
|
|
467
|
+
write(`${acquired} := FALSE;`, 0);
|
|
468
|
+
write(`WHILE NOT ${acquired} AND ${mutex} = 0 DO`, 0);
|
|
469
|
+
write(`${acquired} := __COMPARE_AND_SWAP(ADR(${mutex}), 0, 1);`, 1);
|
|
470
|
+
write("END_WHILE;", 0);
|
|
471
|
+
write(`IF NOT ${acquired} THEN`, 0);
|
|
472
|
+
if (unavailable) {
|
|
473
|
+
write(`${name} := ${unavailable};`, 1);
|
|
474
|
+
}
|
|
475
|
+
write("RETURN;", 1);
|
|
476
|
+
write("END_IF;", 0);
|
|
477
|
+
}
|
|
478
|
+
if (block && tracked?.track) {
|
|
479
|
+
// Before the job's own early return, so a job started since its last call is seen.
|
|
480
|
+
write(`${tracked.track}();`, 0);
|
|
481
|
+
// The step it was in when it finished.
|
|
482
|
+
if (tracked.step) {
|
|
483
|
+
write(`${tracked.step} := ${jobField(storage, block, "step")};`, 0);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
body(fn.body, 0);
|
|
487
|
+
if (tracked?.track) {
|
|
488
|
+
write(`${tracked.track}();`, 0);
|
|
489
|
+
}
|
|
490
|
+
if (mutex) {
|
|
491
|
+
release(mutex, 0);
|
|
492
|
+
}
|
|
493
|
+
if (!block && !method && !program && !fn.result) {
|
|
494
|
+
write(`${name} := TRUE;`, 0);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const header = routineHeader(symbols, fn, name);
|
|
498
|
+
const declaration = [header.declaration];
|
|
499
|
+
mappings.push({
|
|
500
|
+
path: `${mappingName}/declaration.st`,
|
|
501
|
+
line: 1,
|
|
502
|
+
span: fn.span,
|
|
503
|
+
});
|
|
504
|
+
const section = (keyword: string, items: readonly Variable[]): void => {
|
|
505
|
+
if (!items.length) {
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
declaration.push(keyword);
|
|
509
|
+
for (const item of items) {
|
|
510
|
+
mappings.push({
|
|
511
|
+
path: `${mappingName}/declaration.st`,
|
|
512
|
+
line: declaration.length + 1,
|
|
513
|
+
span: item.span,
|
|
514
|
+
});
|
|
515
|
+
const initial =
|
|
516
|
+
item.initial && !instancedBlock(item.type, typeMap)
|
|
517
|
+
? targetTypes.declarationInitializer(item.initial, item.type)
|
|
518
|
+
: "";
|
|
519
|
+
declaration.push(
|
|
520
|
+
` ${storageName(storage, item.id)} : ${targetTypes.name(item.type)}${initial};`,
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
declaration.push("END_VAR");
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
section(
|
|
527
|
+
"VAR_INPUT",
|
|
528
|
+
inputs.filter((item) => item.mode !== "inOut"),
|
|
529
|
+
);
|
|
530
|
+
section(
|
|
531
|
+
"VAR_IN_OUT",
|
|
532
|
+
inputs.filter((item) => item.mode === "inOut"),
|
|
533
|
+
);
|
|
534
|
+
if (tracked && tracked.job) {
|
|
535
|
+
declaration.push(
|
|
536
|
+
"VAR_INPUT",
|
|
537
|
+
` ${tracked.job} : ${instrumentationFieldTypes.job};`,
|
|
538
|
+
"END_VAR",
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
if (program && "parameters" in fn && fn.result) {
|
|
542
|
+
declaration.push(
|
|
543
|
+
"VAR_OUTPUT",
|
|
544
|
+
` ${required(symbols.programResults.get(fn.id))} : ${targetTypes.name(fn.result)};`,
|
|
545
|
+
"END_VAR",
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
if (block) {
|
|
549
|
+
if (block.runtime?.kind === "mailbox") {
|
|
550
|
+
declaration.push(
|
|
551
|
+
"VAR",
|
|
552
|
+
` ${required(symbols.mailboxLocks.get(block.id))} : __XWORD;`,
|
|
553
|
+
"END_VAR",
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
section("VAR_OUTPUT", block.outputs);
|
|
557
|
+
section(
|
|
558
|
+
"VAR",
|
|
559
|
+
block.state.filter((item) => !retainedVariables.has(item.id)),
|
|
560
|
+
);
|
|
561
|
+
section(
|
|
562
|
+
"VAR RETAIN",
|
|
563
|
+
block.state.filter((item) => retainedVariables.has(item.id)),
|
|
564
|
+
);
|
|
565
|
+
const fields = (["busy", "step", "startedAt"] as const).flatMap((key) => {
|
|
566
|
+
const field = tracked?.[key];
|
|
567
|
+
return field ? [` ${field} : ${instrumentationFieldTypes[key]};`] : [];
|
|
568
|
+
});
|
|
569
|
+
if (fields.length) {
|
|
570
|
+
declaration.push("VAR", ...fields, "END_VAR");
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
if (declarations.length) {
|
|
574
|
+
declaration.push(block || program ? "VAR_TEMP" : "VAR");
|
|
575
|
+
declarationSpans.forEach((span, index) => {
|
|
576
|
+
mappings.push({
|
|
577
|
+
path: `${mappingName}/declaration.st`,
|
|
578
|
+
line: declaration.length + index + 1,
|
|
579
|
+
span,
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
declaration.push(...declarations, "END_VAR");
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
objects.push({
|
|
586
|
+
id: fn.id,
|
|
587
|
+
kind: header.kind,
|
|
588
|
+
name,
|
|
589
|
+
declaration: `${declaration.join("\n")}\n`,
|
|
590
|
+
implementation: `${lines.join("\n")}\n`,
|
|
591
|
+
});
|
|
592
|
+
if (block && tracked?.track) {
|
|
593
|
+
objects.push({
|
|
594
|
+
id: `${fn.id}:${tracked.track}`,
|
|
595
|
+
parent: fn.id,
|
|
596
|
+
kind: "method",
|
|
597
|
+
name: tracked.track,
|
|
598
|
+
declaration: `METHOD PRIVATE ${tracked.track}\n`,
|
|
599
|
+
implementation: jobTracking(probes, block, tracked),
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
return { objects, mappings };
|
|
603
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { isAbsolute, relative } from "node:path";
|
|
3
|
+
import type { SourceSpan } from "@tsplc/compiler/ir";
|
|
4
|
+
import type { SourceRange } from "@tsplc/debug/manifest";
|
|
5
|
+
|
|
6
|
+
/** Project files by index. Spans outside the project, such as virtual modules and packages, have no range. */
|
|
7
|
+
export class Sources {
|
|
8
|
+
readonly files: string[] = [];
|
|
9
|
+
private readonly loaded = new Map<
|
|
10
|
+
string,
|
|
11
|
+
{ text: string; lines: number[] } | undefined
|
|
12
|
+
>();
|
|
13
|
+
|
|
14
|
+
constructor(private readonly root: string) {}
|
|
15
|
+
|
|
16
|
+
private load(file: string) {
|
|
17
|
+
if (this.loaded.has(file)) {
|
|
18
|
+
return this.loaded.get(file);
|
|
19
|
+
}
|
|
20
|
+
const path = relative(this.root, file);
|
|
21
|
+
const inside =
|
|
22
|
+
!path.startsWith("..") &&
|
|
23
|
+
!isAbsolute(path) &&
|
|
24
|
+
!path.split(/[\\/]/).includes("node_modules");
|
|
25
|
+
let source: { text: string; lines: number[] } | undefined;
|
|
26
|
+
if (inside && existsSync(file)) {
|
|
27
|
+
const text = readFileSync(file, "utf8");
|
|
28
|
+
const lines = [0];
|
|
29
|
+
for (let index = 0; index < text.length; index++) {
|
|
30
|
+
if (text[index] === "\n") {
|
|
31
|
+
lines.push(index + 1);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
source = { text, lines };
|
|
35
|
+
}
|
|
36
|
+
this.loaded.set(file, source);
|
|
37
|
+
return source;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
range(span: SourceSpan): SourceRange | undefined {
|
|
41
|
+
const source = this.load(span.file);
|
|
42
|
+
if (!source || span.end > source.text.length) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
const position = (offset: number) => {
|
|
46
|
+
let low = 0;
|
|
47
|
+
let high = source.lines.length - 1;
|
|
48
|
+
while (low < high) {
|
|
49
|
+
const middle = (low + high + 1) >> 1;
|
|
50
|
+
if (source.lines[middle]! <= offset) {
|
|
51
|
+
low = middle;
|
|
52
|
+
} else {
|
|
53
|
+
high = middle - 1;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return { line: low + 1, column: offset - source.lines[low]! + 1 };
|
|
57
|
+
};
|
|
58
|
+
const path = relative(this.root, span.file).replaceAll("\\", "/");
|
|
59
|
+
let file = this.files.indexOf(path);
|
|
60
|
+
if (file < 0) {
|
|
61
|
+
file = this.files.push(path) - 1;
|
|
62
|
+
}
|
|
63
|
+
const start = position(span.start);
|
|
64
|
+
const end = position(span.end);
|
|
65
|
+
return { file, ...start, endLine: end.line, endColumn: end.column };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
text(span: SourceSpan): string | undefined {
|
|
69
|
+
return this.load(span.file)?.text.slice(span.start, span.end);
|
|
70
|
+
}
|
|
71
|
+
}
|