@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,409 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Constant,
|
|
3
|
+
type CoreExpression,
|
|
4
|
+
type CoreStatement,
|
|
5
|
+
type GeneratedObject,
|
|
6
|
+
integerRange,
|
|
7
|
+
floatMaximum,
|
|
8
|
+
integerType,
|
|
9
|
+
nestedBodies,
|
|
10
|
+
type PlcType,
|
|
11
|
+
} from "@tsplc/compiler/ir";
|
|
12
|
+
import {
|
|
13
|
+
pathIndexes,
|
|
14
|
+
statementExpressions,
|
|
15
|
+
subexpressions,
|
|
16
|
+
} from "./operands.ts";
|
|
17
|
+
|
|
18
|
+
const operatorTokens: Readonly<Record<string, string>> = {
|
|
19
|
+
add: "+",
|
|
20
|
+
subtract: "-",
|
|
21
|
+
multiply: "*",
|
|
22
|
+
divide: "/",
|
|
23
|
+
remainder: "MOD",
|
|
24
|
+
equal: "=",
|
|
25
|
+
notEqual: "<>",
|
|
26
|
+
less: "<",
|
|
27
|
+
lessEqual: "<=",
|
|
28
|
+
greater: ">",
|
|
29
|
+
greaterEqual: ">=",
|
|
30
|
+
bitAnd: "AND",
|
|
31
|
+
bitOr: "OR",
|
|
32
|
+
bitXor: "XOR",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function scalarLiteral(value: Constant, type: string): string {
|
|
36
|
+
if (value.kind === "bool") {
|
|
37
|
+
return value.value ? "TRUE" : "FALSE";
|
|
38
|
+
}
|
|
39
|
+
if (value.kind === "integer") {
|
|
40
|
+
return `${type}#${value.value}`;
|
|
41
|
+
}
|
|
42
|
+
if (value.kind === "float") {
|
|
43
|
+
// String(-0) loses the IEEE sign; keep it explicit in target float literals.
|
|
44
|
+
const text = Object.is(value.value, -0)
|
|
45
|
+
? "-0.0"
|
|
46
|
+
: String(value.value).replace(/e/i, "E");
|
|
47
|
+
return `${type}#${text}`;
|
|
48
|
+
}
|
|
49
|
+
throw new Error("Expected verified scalar constant");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class NumericLowering {
|
|
53
|
+
readonly objects: GeneratedObject[] = [];
|
|
54
|
+
private readonly helpers = new Map<string, string>();
|
|
55
|
+
|
|
56
|
+
constructor(
|
|
57
|
+
private readonly types: ReadonlyMap<string, PlcType>,
|
|
58
|
+
private readonly allocate: (label: string) => string,
|
|
59
|
+
) {}
|
|
60
|
+
|
|
61
|
+
private prepare(value: CoreExpression): void {
|
|
62
|
+
for (const operand of subexpressions(value)) {
|
|
63
|
+
this.prepare(operand);
|
|
64
|
+
}
|
|
65
|
+
if (value.kind === "binary" && value.policy) {
|
|
66
|
+
this.arithmetic(value.type, value.operator, value.policy);
|
|
67
|
+
}
|
|
68
|
+
if (value.kind === "convert" && value.policy === "saturate") {
|
|
69
|
+
this.saturation(value.operand.type, value.type);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
prepareStatements(items: readonly CoreStatement[]): void {
|
|
74
|
+
for (const item of items) {
|
|
75
|
+
// Helpers are emitted in the order they are found, and the build id hashes that order,
|
|
76
|
+
// so an assignment's value keeps being prepared before its place's indexes.
|
|
77
|
+
const values =
|
|
78
|
+
item.kind === "assign"
|
|
79
|
+
? [item.value, ...pathIndexes(item.path)]
|
|
80
|
+
: statementExpressions(item);
|
|
81
|
+
for (const value of values) {
|
|
82
|
+
this.prepare(value);
|
|
83
|
+
}
|
|
84
|
+
for (const body of nestedBodies(item)) {
|
|
85
|
+
this.prepareStatements(body);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
render(value: CoreExpression, operands: readonly string[]): string {
|
|
91
|
+
if (value.kind === "convert") {
|
|
92
|
+
return value.policy === "saturate"
|
|
93
|
+
? `${this.saturation(value.operand.type, value.type)}(${operands[0]})`
|
|
94
|
+
: `TO_${value.type}(${operands[0]})`;
|
|
95
|
+
}
|
|
96
|
+
if (value.kind === "unary") {
|
|
97
|
+
// Avoid signed intermediate casts: TIME spans the complete UDINT range.
|
|
98
|
+
if (value.operator === "timeToMilliseconds") {
|
|
99
|
+
return `TIME_TO_UDINT(${operands[0]})`;
|
|
100
|
+
}
|
|
101
|
+
// Milliseconds since the runtime started; DINT wraps after 24.8 days, so subtract to compare.
|
|
102
|
+
if (value.operator === "clock") {
|
|
103
|
+
return "TO_DINT(TIME_TO_UDINT(TIME()))";
|
|
104
|
+
}
|
|
105
|
+
return value.operator === "not"
|
|
106
|
+
? `(NOT ${operands[0]})`
|
|
107
|
+
: `TO_${value.type}(${value.operator === "negate" ? "-" : "NOT "}${operands[0]})`;
|
|
108
|
+
}
|
|
109
|
+
if (value.kind !== "binary") {
|
|
110
|
+
throw new Error("Expected numeric operation");
|
|
111
|
+
}
|
|
112
|
+
if (value.policy) {
|
|
113
|
+
return `${this.arithmetic(value.type, value.operator, value.policy)}(${operands.join(", ")})`;
|
|
114
|
+
}
|
|
115
|
+
if (value.operator === "shiftLeft" || value.operator === "shiftRight") {
|
|
116
|
+
const shift = value.operator === "shiftLeft" ? "SHL" : "SHR";
|
|
117
|
+
return `TO_${value.type}(${shift}(${operands.join(", ")}))`;
|
|
118
|
+
}
|
|
119
|
+
const token = operatorTokens[value.operator];
|
|
120
|
+
if (!token) {
|
|
121
|
+
throw new Error("Boolean short-circuit operation was not normalized");
|
|
122
|
+
}
|
|
123
|
+
const text =
|
|
124
|
+
value.operator === "remainder"
|
|
125
|
+
? `(${operands[0]} - (${operands[0]} / ${operands[1]}) * ${operands[1]})`
|
|
126
|
+
: `(${operands[0]} ${token} ${operands[1]})`;
|
|
127
|
+
return value.type === "BOOL" ? text : `TO_${value.type}(${text})`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private helper(
|
|
131
|
+
key: string,
|
|
132
|
+
type: string,
|
|
133
|
+
inputs: readonly [string, string][],
|
|
134
|
+
body: (name: string) => string,
|
|
135
|
+
locals = "",
|
|
136
|
+
): string {
|
|
137
|
+
const existing = this.helpers.get(key);
|
|
138
|
+
if (existing) {
|
|
139
|
+
return existing;
|
|
140
|
+
}
|
|
141
|
+
const name = this.allocate(`numeric_${key}`);
|
|
142
|
+
this.helpers.set(key, name);
|
|
143
|
+
const parameters = inputs
|
|
144
|
+
.map(([input, inputType]) => ` ${input} : ${inputType};`)
|
|
145
|
+
.join("\n");
|
|
146
|
+
const variables = locals
|
|
147
|
+
? `VAR
|
|
148
|
+
${locals}
|
|
149
|
+
END_VAR
|
|
150
|
+
`
|
|
151
|
+
: "";
|
|
152
|
+
const declaration = `FUNCTION ${name} : ${type}
|
|
153
|
+
VAR_INPUT
|
|
154
|
+
${parameters}
|
|
155
|
+
END_VAR
|
|
156
|
+
${variables}`;
|
|
157
|
+
this.objects.push({
|
|
158
|
+
id: `numeric:${key}`,
|
|
159
|
+
kind: "function",
|
|
160
|
+
name,
|
|
161
|
+
declaration,
|
|
162
|
+
implementation: body(name),
|
|
163
|
+
});
|
|
164
|
+
return name;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private add64(): string {
|
|
168
|
+
// XOR forms the sum bits; each carry moves left until it exits the 64-bit word.
|
|
169
|
+
// This gives modulo 2^64 addition without relying on target integer overflow.
|
|
170
|
+
return this.helper(
|
|
171
|
+
"add64",
|
|
172
|
+
"LWORD",
|
|
173
|
+
[
|
|
174
|
+
["a", "LWORD"],
|
|
175
|
+
["b", "LWORD"],
|
|
176
|
+
],
|
|
177
|
+
(name) => `x := a;
|
|
178
|
+
y := b;
|
|
179
|
+
FOR i := 1 TO 64 DO
|
|
180
|
+
carry := SHL(x AND y, 1);
|
|
181
|
+
x := x XOR y;
|
|
182
|
+
y := carry;
|
|
183
|
+
END_FOR;
|
|
184
|
+
${name} := x;
|
|
185
|
+
`,
|
|
186
|
+
` x, y, carry : LWORD;
|
|
187
|
+
i : UINT;`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private wrap64(operator: string): string {
|
|
192
|
+
const add = this.add64();
|
|
193
|
+
if (operator === "add") {
|
|
194
|
+
return add;
|
|
195
|
+
}
|
|
196
|
+
if (operator === "subtract") {
|
|
197
|
+
// Two's complement negation and addition use the same modulo word operation.
|
|
198
|
+
return this.helper(
|
|
199
|
+
"subtract64",
|
|
200
|
+
"LWORD",
|
|
201
|
+
[
|
|
202
|
+
["a", "LWORD"],
|
|
203
|
+
["b", "LWORD"],
|
|
204
|
+
],
|
|
205
|
+
(name) => `${name} := ${add}(a, ${add}(NOT b, LWORD#1));
|
|
206
|
+
`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
if (operator !== "multiply") {
|
|
210
|
+
throw new Error("Unsupported verified wrapping operation");
|
|
211
|
+
}
|
|
212
|
+
// Shift-and-add retains exactly the low 64 product bits.
|
|
213
|
+
return this.helper(
|
|
214
|
+
"multiply64",
|
|
215
|
+
"LWORD",
|
|
216
|
+
[
|
|
217
|
+
["a", "LWORD"],
|
|
218
|
+
["b", "LWORD"],
|
|
219
|
+
],
|
|
220
|
+
(name) => `x := a;
|
|
221
|
+
y := b;
|
|
222
|
+
result := LWORD#0;
|
|
223
|
+
FOR i := 1 TO 64 DO
|
|
224
|
+
IF (y AND LWORD#1) <> LWORD#0 THEN
|
|
225
|
+
result := ${add}(result, x);
|
|
226
|
+
END_IF;
|
|
227
|
+
x := SHL(x, 1);
|
|
228
|
+
y := SHR(y, 1);
|
|
229
|
+
END_FOR;
|
|
230
|
+
${name} := result;
|
|
231
|
+
`,
|
|
232
|
+
` x, y, result : LWORD;
|
|
233
|
+
i : UINT;`,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private arithmetic(
|
|
238
|
+
typeId: string,
|
|
239
|
+
operator: string,
|
|
240
|
+
policy: "wrap" | "saturate",
|
|
241
|
+
): string {
|
|
242
|
+
const type = this.types.get(typeId);
|
|
243
|
+
if (type?.kind !== "integer") {
|
|
244
|
+
throw new Error("Expected verified integer arithmetic");
|
|
245
|
+
}
|
|
246
|
+
const token = operatorTokens[operator];
|
|
247
|
+
if (!token) {
|
|
248
|
+
throw new Error("Expected verified integer arithmetic");
|
|
249
|
+
}
|
|
250
|
+
const key = `${policy}_${operator}_${typeId}`;
|
|
251
|
+
if (policy === "wrap") {
|
|
252
|
+
if (type.bits === 64) {
|
|
253
|
+
const operation = this.wrap64(operator);
|
|
254
|
+
return this.helper(
|
|
255
|
+
key,
|
|
256
|
+
typeId,
|
|
257
|
+
[
|
|
258
|
+
["a", typeId],
|
|
259
|
+
["b", typeId],
|
|
260
|
+
],
|
|
261
|
+
(
|
|
262
|
+
name,
|
|
263
|
+
) => `${name} := LWORD_TO_${typeId}(${operation}(${typeId}_TO_LWORD(a), ${typeId}_TO_LWORD(b)));
|
|
264
|
+
`,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
const wide = !type.signed && operator !== "subtract" ? "ULINT" : "LINT";
|
|
268
|
+
return this.helper(
|
|
269
|
+
key,
|
|
270
|
+
typeId,
|
|
271
|
+
[
|
|
272
|
+
["a", typeId],
|
|
273
|
+
["b", typeId],
|
|
274
|
+
],
|
|
275
|
+
(
|
|
276
|
+
name,
|
|
277
|
+
) => `${name} := ${wide}_TO_${typeId}(${typeId}_TO_${wide}(a) ${token} ${typeId}_TO_${wide}(b));
|
|
278
|
+
`,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const [minimum, maximum] = integerRange(type);
|
|
283
|
+
const min = `${typeId}#${minimum}`;
|
|
284
|
+
const max = `${typeId}#${maximum}`;
|
|
285
|
+
const zero = `${typeId}#0`;
|
|
286
|
+
return this.helper(
|
|
287
|
+
key,
|
|
288
|
+
typeId,
|
|
289
|
+
[
|
|
290
|
+
["a", typeId],
|
|
291
|
+
["b", typeId],
|
|
292
|
+
],
|
|
293
|
+
(name) => {
|
|
294
|
+
const clamp = (
|
|
295
|
+
condition: string,
|
|
296
|
+
endpoint: string,
|
|
297
|
+
depth = "",
|
|
298
|
+
) => `${depth}IF ${condition} THEN
|
|
299
|
+
${depth} ${name} := ${endpoint};
|
|
300
|
+
${depth} RETURN;
|
|
301
|
+
${depth}END_IF;
|
|
302
|
+
`;
|
|
303
|
+
const guard = (): string => {
|
|
304
|
+
if (!type.signed) {
|
|
305
|
+
// Unsigned subtraction has only underflow; the other operations have only overflow.
|
|
306
|
+
if (operator === "add") {
|
|
307
|
+
return clamp(`a > ${max} - b`, max);
|
|
308
|
+
}
|
|
309
|
+
if (operator === "subtract") {
|
|
310
|
+
return clamp("a < b", zero);
|
|
311
|
+
}
|
|
312
|
+
return `IF b <> ${zero} THEN
|
|
313
|
+
${clamp(`a > ${max} / b`, max, " ")}END_IF;
|
|
314
|
+
`;
|
|
315
|
+
}
|
|
316
|
+
if (operator === "add") {
|
|
317
|
+
return `IF b > ${zero} THEN
|
|
318
|
+
${clamp(`a > ${max} - b`, max, " ")}ELSIF b < ${zero} THEN
|
|
319
|
+
${clamp(`a < ${min} - b`, min, " ")}END_IF;
|
|
320
|
+
`;
|
|
321
|
+
}
|
|
322
|
+
if (operator === "subtract") {
|
|
323
|
+
return `IF b < ${zero} THEN
|
|
324
|
+
${clamp(`a > ${max} + b`, max, " ")}ELSIF b > ${zero} THEN
|
|
325
|
+
${clamp(`a < ${min} + b`, min, " ")}END_IF;
|
|
326
|
+
`;
|
|
327
|
+
}
|
|
328
|
+
// Handle zero first, then divide the endpoint before multiplying. Each sign
|
|
329
|
+
// quadrant tests representability without forming an overflowing product.
|
|
330
|
+
return (
|
|
331
|
+
clamp(`a = ${zero}`, zero) +
|
|
332
|
+
clamp(`b = ${zero}`, zero) +
|
|
333
|
+
`IF a > ${zero} THEN
|
|
334
|
+
IF b > ${zero} THEN
|
|
335
|
+
${clamp(`a > ${max} / b`, max, " ")} ELSE
|
|
336
|
+
${clamp(`b < ${min} / a`, min, " ")} END_IF;
|
|
337
|
+
ELSE
|
|
338
|
+
IF b > ${zero} THEN
|
|
339
|
+
${clamp(`a < ${min} / b`, min, " ")} ELSE
|
|
340
|
+
${clamp(`a < ${max} / b`, max, " ")} END_IF;
|
|
341
|
+
END_IF;
|
|
342
|
+
`
|
|
343
|
+
);
|
|
344
|
+
};
|
|
345
|
+
return `${guard()}${name} := a ${token} b;
|
|
346
|
+
`;
|
|
347
|
+
},
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
private saturation(sourceId: string, targetId: string): string {
|
|
352
|
+
const source = this.types.get(sourceId);
|
|
353
|
+
const target = this.types.get(targetId);
|
|
354
|
+
if (source?.kind === "float" && target?.kind === "float") {
|
|
355
|
+
// Widening cannot exceed the destination's finite range. Identity
|
|
356
|
+
// conversions are removed by normalization; neither needs a helper.
|
|
357
|
+
if (source.bits <= target.bits) {
|
|
358
|
+
return `TO_${targetId}`;
|
|
359
|
+
}
|
|
360
|
+
const maximum = floatMaximum(target);
|
|
361
|
+
const literal = (value: number, type: string): string =>
|
|
362
|
+
scalarLiteral({ kind: "float", value }, type);
|
|
363
|
+
return this.helper(
|
|
364
|
+
`saturate_${sourceId}_${targetId}`,
|
|
365
|
+
targetId,
|
|
366
|
+
[["value", sourceId]],
|
|
367
|
+
(name) => `IF value < ${literal(-maximum, sourceId)} THEN
|
|
368
|
+
${name} := ${literal(-maximum, targetId)};
|
|
369
|
+
RETURN;
|
|
370
|
+
END_IF;
|
|
371
|
+
IF value > ${literal(maximum, sourceId)} THEN
|
|
372
|
+
${name} := ${literal(maximum, targetId)};
|
|
373
|
+
RETURN;
|
|
374
|
+
END_IF;
|
|
375
|
+
${name} := TO_${targetId}(value);
|
|
376
|
+
`,
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
if (!source || !target || !integerType(source) || !integerType(target)) {
|
|
380
|
+
throw new Error("Expected verified integer saturation");
|
|
381
|
+
}
|
|
382
|
+
const [fromMin, fromMax] = integerRange(source);
|
|
383
|
+
const [toMin, toMax] = integerRange(target);
|
|
384
|
+
return this.helper(
|
|
385
|
+
`saturate_${sourceId}_${targetId}`,
|
|
386
|
+
targetId,
|
|
387
|
+
[["value", sourceId]],
|
|
388
|
+
(name) => {
|
|
389
|
+
let body = "";
|
|
390
|
+
if (fromMin < toMin) {
|
|
391
|
+
body += `IF value < ${sourceId}#${toMin} THEN
|
|
392
|
+
${name} := ${targetId}#${toMin};
|
|
393
|
+
RETURN;
|
|
394
|
+
END_IF;
|
|
395
|
+
`;
|
|
396
|
+
}
|
|
397
|
+
if (fromMax > toMax) {
|
|
398
|
+
body += `IF value > ${sourceId}#${toMax} THEN
|
|
399
|
+
${name} := ${targetId}#${toMax};
|
|
400
|
+
RETURN;
|
|
401
|
+
END_IF;
|
|
402
|
+
`;
|
|
403
|
+
}
|
|
404
|
+
return `${body}${name} := TO_${targetId}(value);
|
|
405
|
+
`;
|
|
406
|
+
},
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CoreExpression,
|
|
3
|
+
type CorePath,
|
|
4
|
+
type CoreStatement,
|
|
5
|
+
telemetryOperands,
|
|
6
|
+
} from "@tsplc/compiler/ir";
|
|
7
|
+
|
|
8
|
+
/** The index expressions along a place's path. */
|
|
9
|
+
export const pathIndexes = (path: CorePath = []): CoreExpression[] =>
|
|
10
|
+
path.flatMap((segment) => (segment.kind === "index" ? [segment.value] : []));
|
|
11
|
+
|
|
12
|
+
/** The expressions an expression evaluates directly: its operands, or the indexes of the place it loads or borrows. */
|
|
13
|
+
export const subexpressions = (
|
|
14
|
+
value: CoreExpression,
|
|
15
|
+
): readonly CoreExpression[] => {
|
|
16
|
+
switch (value.kind) {
|
|
17
|
+
case "constant":
|
|
18
|
+
return [];
|
|
19
|
+
case "load":
|
|
20
|
+
return pathIndexes(value.path);
|
|
21
|
+
case "borrow":
|
|
22
|
+
return pathIndexes(value.place.path);
|
|
23
|
+
case "unary":
|
|
24
|
+
case "convert":
|
|
25
|
+
return [value.operand];
|
|
26
|
+
case "binary":
|
|
27
|
+
return [value.left, value.right];
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The expressions a statement evaluates itself, a place's indexes before the value stored or
|
|
33
|
+
* passed there. The statements nested in it are `nestedBodies`.
|
|
34
|
+
*/
|
|
35
|
+
export const statementExpressions = (
|
|
36
|
+
item: CoreStatement,
|
|
37
|
+
): readonly CoreExpression[] => {
|
|
38
|
+
switch (item.kind) {
|
|
39
|
+
case "assign":
|
|
40
|
+
return [...pathIndexes(item.path), item.value];
|
|
41
|
+
case "if":
|
|
42
|
+
return [item.condition];
|
|
43
|
+
case "switch":
|
|
44
|
+
return [item.value];
|
|
45
|
+
case "for":
|
|
46
|
+
return item.dynamic ? [item.dynamic.start, item.dynamic.limit] : [];
|
|
47
|
+
case "invoke":
|
|
48
|
+
return [...(item.index ? [item.index] : []), ...item.arguments];
|
|
49
|
+
case "blockUpdate":
|
|
50
|
+
return [
|
|
51
|
+
...(item.index ? [item.index] : []),
|
|
52
|
+
...Object.values(item.inputs),
|
|
53
|
+
];
|
|
54
|
+
case "return":
|
|
55
|
+
return item.value ? [item.value] : [];
|
|
56
|
+
case "telemetry":
|
|
57
|
+
return telemetryOperands(item);
|
|
58
|
+
case "break":
|
|
59
|
+
case "continue":
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
};
|