@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
package/src/ir/ranges.ts
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BinaryOperator,
|
|
3
|
+
Expression,
|
|
4
|
+
MathHelper,
|
|
5
|
+
Variable,
|
|
6
|
+
} from "./model.ts";
|
|
7
|
+
import { fold } from "./fold.ts";
|
|
8
|
+
import {
|
|
9
|
+
admits,
|
|
10
|
+
highest,
|
|
11
|
+
integerType,
|
|
12
|
+
lowest,
|
|
13
|
+
within,
|
|
14
|
+
type IntegerInterval,
|
|
15
|
+
} from "./numeric.ts";
|
|
16
|
+
import { integerRange, type PlcType } from "./types.ts";
|
|
17
|
+
|
|
18
|
+
export const interval = (
|
|
19
|
+
expression: Expression,
|
|
20
|
+
types: ReadonlyMap<string, PlcType>,
|
|
21
|
+
variables: ReadonlyMap<string, Variable>,
|
|
22
|
+
ranges: ReadonlyMap<string, IntegerInterval> = new Map(),
|
|
23
|
+
): IntegerInterval | undefined => {
|
|
24
|
+
if (expression.kind === "length") {
|
|
25
|
+
const array = types.get(expression.operand.type);
|
|
26
|
+
if (array?.kind === "array") {
|
|
27
|
+
return [BigInt(array.length), BigInt(array.length)];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const value = fold(expression, variables, types);
|
|
31
|
+
if (value?.kind === "integer") {
|
|
32
|
+
return [BigInt(value.value), BigInt(value.value)];
|
|
33
|
+
}
|
|
34
|
+
const type = types.get(expression.type);
|
|
35
|
+
if (!type || !integerType(type)) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
if (expression.kind === "load") {
|
|
39
|
+
return ranges.get(expression.place.root) ?? integerRange(type);
|
|
40
|
+
}
|
|
41
|
+
if (expression.kind === "convert") {
|
|
42
|
+
return expression.policy === "exact"
|
|
43
|
+
? interval(expression.operand, types, variables, ranges)
|
|
44
|
+
: integerRange(type);
|
|
45
|
+
}
|
|
46
|
+
if (expression.kind === "unary" && expression.operator === "negate") {
|
|
47
|
+
const operand = interval(expression.operand, types, variables, ranges);
|
|
48
|
+
return operand && [-operand[1], -operand[0]];
|
|
49
|
+
}
|
|
50
|
+
if (expression.kind === "binary" && !expression.policy) {
|
|
51
|
+
const left = interval(expression.left, types, variables, ranges);
|
|
52
|
+
const right = interval(expression.right, types, variables, ranges);
|
|
53
|
+
return (
|
|
54
|
+
left &&
|
|
55
|
+
right &&
|
|
56
|
+
arithmeticInterval(expression.operator, left, right, integerRange(type))
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return integerRange(type);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** The range of `left operator right`; a result that may wrap spans its whole type. */
|
|
63
|
+
const arithmeticInterval = (
|
|
64
|
+
operator: BinaryOperator,
|
|
65
|
+
left: IntegerInterval,
|
|
66
|
+
right: IntegerInterval,
|
|
67
|
+
type: IntegerInterval,
|
|
68
|
+
): IntegerInterval | undefined => {
|
|
69
|
+
const unwrapped = (result: IntegerInterval): IntegerInterval =>
|
|
70
|
+
within(result, type) ? result : type;
|
|
71
|
+
const corners = (combine: (a: bigint, b: bigint) => bigint) => [
|
|
72
|
+
combine(left[0], right[0]),
|
|
73
|
+
combine(left[0], right[1]),
|
|
74
|
+
combine(left[1], right[0]),
|
|
75
|
+
combine(left[1], right[1]),
|
|
76
|
+
];
|
|
77
|
+
switch (operator) {
|
|
78
|
+
case "add":
|
|
79
|
+
return unwrapped([left[0] + right[0], left[1] + right[1]]);
|
|
80
|
+
case "subtract":
|
|
81
|
+
return unwrapped([left[0] - right[1], left[1] - right[0]]);
|
|
82
|
+
case "multiply": {
|
|
83
|
+
const products = corners((a, b) => a * b);
|
|
84
|
+
return unwrapped([products.reduce(lowest), products.reduce(highest)]);
|
|
85
|
+
}
|
|
86
|
+
case "divide": {
|
|
87
|
+
if (admits(right, 0n)) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
const quotients = corners((a, b) => a / b);
|
|
91
|
+
return [quotients.reduce(lowest), quotients.reduce(highest)];
|
|
92
|
+
}
|
|
93
|
+
case "remainder": {
|
|
94
|
+
if (admits(right, 0n)) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
const magnitude = (value: bigint) => (value < 0n ? -value : value);
|
|
98
|
+
const largest = highest(magnitude(right[0]), magnitude(right[1])) - 1n;
|
|
99
|
+
return [left[0] < 0n ? -largest : 0n, left[1] > 0n ? largest : 0n];
|
|
100
|
+
}
|
|
101
|
+
default:
|
|
102
|
+
return type;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** The most iterations a bounded loop may prove. */
|
|
107
|
+
export const maxLoopIterations = 1_000_000n;
|
|
108
|
+
|
|
109
|
+
// FOR iterations from first to last inclusive; step is nonzero.
|
|
110
|
+
export const loopIterations = (
|
|
111
|
+
first: bigint,
|
|
112
|
+
last: bigint,
|
|
113
|
+
step: bigint,
|
|
114
|
+
): bigint => {
|
|
115
|
+
if (step > 0n) {
|
|
116
|
+
return last >= first ? (last - first) / step + 1n : 0n;
|
|
117
|
+
}
|
|
118
|
+
return last <= first ? (first - last) / -step + 1n : 0n;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const loopBounds = (
|
|
122
|
+
start: IntegerInterval,
|
|
123
|
+
limit: IntegerInterval,
|
|
124
|
+
step: bigint,
|
|
125
|
+
exclusive = false,
|
|
126
|
+
) => {
|
|
127
|
+
const excluded = exclusive ? 1n : 0n;
|
|
128
|
+
const first = step > 0n ? start[0] : start[1];
|
|
129
|
+
const last = step > 0n ? limit[1] - excluded : limit[0] + excluded;
|
|
130
|
+
const count = loopIterations(first, last, step);
|
|
131
|
+
const range: IntegerInterval = first <= last ? [first, last] : [last, first];
|
|
132
|
+
return { first, last, count, range };
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const negatedComparisons: Partial<Record<BinaryOperator, BinaryOperator>> = {
|
|
136
|
+
less: "greaterEqual",
|
|
137
|
+
lessEqual: "greater",
|
|
138
|
+
greater: "lessEqual",
|
|
139
|
+
greaterEqual: "less",
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const bounded = (
|
|
143
|
+
current: IntegerInterval,
|
|
144
|
+
operator: BinaryOperator | undefined,
|
|
145
|
+
value: bigint,
|
|
146
|
+
): IntegerInterval => {
|
|
147
|
+
const [lower, upper] = current;
|
|
148
|
+
switch (operator) {
|
|
149
|
+
case "less":
|
|
150
|
+
return [lower, lowest(upper, value - 1n)];
|
|
151
|
+
case "lessEqual":
|
|
152
|
+
return [lower, lowest(upper, value)];
|
|
153
|
+
case "greater":
|
|
154
|
+
return [highest(lower, value + 1n), upper];
|
|
155
|
+
case "greaterEqual":
|
|
156
|
+
return [highest(lower, value), upper];
|
|
157
|
+
default:
|
|
158
|
+
return current;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// Only storage no statement can change carries branch refinements: loop counters,
|
|
163
|
+
// by-value parameters and const locals, so the proof survives through the branch.
|
|
164
|
+
export const narrowRanges = (
|
|
165
|
+
condition: Expression,
|
|
166
|
+
truth: boolean,
|
|
167
|
+
types: ReadonlyMap<string, PlcType>,
|
|
168
|
+
variables: ReadonlyMap<string, Variable>,
|
|
169
|
+
ranges: ReadonlyMap<string, IntegerInterval>,
|
|
170
|
+
): Map<string, IntegerInterval> => {
|
|
171
|
+
const narrowed = new Map(ranges);
|
|
172
|
+
if (condition.kind !== "binary") {
|
|
173
|
+
return narrowed;
|
|
174
|
+
}
|
|
175
|
+
if (condition.operator === "and") {
|
|
176
|
+
return truth
|
|
177
|
+
? narrowRanges(
|
|
178
|
+
condition.right,
|
|
179
|
+
truth,
|
|
180
|
+
types,
|
|
181
|
+
variables,
|
|
182
|
+
narrowRanges(condition.left, truth, types, variables, ranges),
|
|
183
|
+
)
|
|
184
|
+
: narrowed;
|
|
185
|
+
}
|
|
186
|
+
if (condition.left.kind !== "load" || condition.left.place.path.length) {
|
|
187
|
+
return narrowed;
|
|
188
|
+
}
|
|
189
|
+
const root = condition.left.place.root;
|
|
190
|
+
const variable = variables.get(root);
|
|
191
|
+
const type = types.get(condition.left.type);
|
|
192
|
+
const fixed =
|
|
193
|
+
variable &&
|
|
194
|
+
((variable.storage === "parameter" && !variable.mode) ||
|
|
195
|
+
(variable.storage === "local" && !variable.mutable));
|
|
196
|
+
const current =
|
|
197
|
+
ranges.get(root) ??
|
|
198
|
+
(fixed && type && integerType(type) ? integerRange(type) : undefined);
|
|
199
|
+
const bound = interval(condition.right, types, variables, ranges);
|
|
200
|
+
if (!current || !bound || bound[0] !== bound[1]) {
|
|
201
|
+
return narrowed;
|
|
202
|
+
}
|
|
203
|
+
const [lower, upper] = bounded(
|
|
204
|
+
current,
|
|
205
|
+
truth ? condition.operator : negatedComparisons[condition.operator],
|
|
206
|
+
bound[0],
|
|
207
|
+
);
|
|
208
|
+
if (lower <= upper) {
|
|
209
|
+
narrowed.set(root, [lower, upper]);
|
|
210
|
+
}
|
|
211
|
+
return narrowed;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/** A value's proved integer range; a math helper's result follows from its arguments. */
|
|
215
|
+
export const valueRange = (
|
|
216
|
+
expression: Expression,
|
|
217
|
+
types: ReadonlyMap<string, PlcType>,
|
|
218
|
+
variables: ReadonlyMap<string, Variable>,
|
|
219
|
+
ranges: ReadonlyMap<string, IntegerInterval>,
|
|
220
|
+
functions: ReadonlyMap<string, { bounds?: MathHelper }>,
|
|
221
|
+
): IntegerInterval | undefined => {
|
|
222
|
+
if (expression.kind !== "call") {
|
|
223
|
+
return interval(expression, types, variables, ranges);
|
|
224
|
+
}
|
|
225
|
+
const helper = functions.get(expression.function)?.bounds;
|
|
226
|
+
return (
|
|
227
|
+
helper &&
|
|
228
|
+
mathHelperRange(
|
|
229
|
+
helper,
|
|
230
|
+
expression.arguments.map((argument) =>
|
|
231
|
+
valueRange(argument, types, variables, ranges, functions),
|
|
232
|
+
),
|
|
233
|
+
)
|
|
234
|
+
);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const mathHelperRange = (
|
|
238
|
+
helper: MathHelper,
|
|
239
|
+
[first, second, third]: readonly (IntegerInterval | undefined)[],
|
|
240
|
+
): IntegerInterval | undefined => {
|
|
241
|
+
if (!first || !second) {
|
|
242
|
+
return undefined;
|
|
243
|
+
}
|
|
244
|
+
if (helper === "min") {
|
|
245
|
+
return [lowest(first[0], second[0]), lowest(first[1], second[1])];
|
|
246
|
+
}
|
|
247
|
+
if (helper === "max") {
|
|
248
|
+
return [highest(first[0], second[0]), highest(first[1], second[1])];
|
|
249
|
+
}
|
|
250
|
+
// clamp(value, lower, upper) returns one of its three arguments, never outside lower..upper,
|
|
251
|
+
// and only lower or upper when the bounds are reversed.
|
|
252
|
+
if (!third) {
|
|
253
|
+
return undefined;
|
|
254
|
+
}
|
|
255
|
+
return [lowest(second[0], third[0]), highest(second[1], third[1])];
|
|
256
|
+
};
|