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