@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6563 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16198 -0
- package/dist/index.js.map +62 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1090 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1344 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2177 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +612 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +659 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { generatedNamePattern, isReservedName } from "@tsplc/iec/st";
|
|
2
|
+
|
|
3
|
+
export function isIecIdentifier(name: string): boolean {
|
|
4
|
+
return (
|
|
5
|
+
generatedNamePattern.test(name) &&
|
|
6
|
+
!name.includes("__") &&
|
|
7
|
+
!isReservedName(name)
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const identifierBody = (label: string): string =>
|
|
12
|
+
label
|
|
13
|
+
.replace(/[^A-Za-z0-9_]/g, "_")
|
|
14
|
+
.replace(/_+/g, "_")
|
|
15
|
+
.replace(/^_+|_+$/g, "") || "name";
|
|
16
|
+
|
|
17
|
+
export class Names {
|
|
18
|
+
private readonly used = new Set<string>();
|
|
19
|
+
|
|
20
|
+
constructor(reserved: Iterable<string> = []) {
|
|
21
|
+
for (const name of reserved) {
|
|
22
|
+
this.reserve(name);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
allocateObject(
|
|
27
|
+
prefix: "FB" | "F" | "PRG" | "ST" | "E" | "GVL",
|
|
28
|
+
label: string,
|
|
29
|
+
): string {
|
|
30
|
+
const source = prefix === "F" ? label.replace(/^PRG_/, "") : label;
|
|
31
|
+
const kind = prefix === "F" && source.startsWith("FUN_") ? "FUN" : prefix;
|
|
32
|
+
const body = source.startsWith(`${kind}_`)
|
|
33
|
+
? source.slice(kind.length + 1)
|
|
34
|
+
: source;
|
|
35
|
+
return this.allocate(`${kind}_${identifierBody(body)}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
reserve(name: string): void {
|
|
39
|
+
this.used.add(name.toUpperCase());
|
|
40
|
+
// Qualified native types must keep their library prefix resolvable in the
|
|
41
|
+
// declaration scope, not merely reserve the complete dotted spelling.
|
|
42
|
+
const prefix = name.split(".")[0];
|
|
43
|
+
if (prefix) {
|
|
44
|
+
this.used.add(prefix.toUpperCase());
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
allocate(label: string): string {
|
|
48
|
+
const base = isIecIdentifier(label)
|
|
49
|
+
? label
|
|
50
|
+
: `plc_${identifierBody(label)}`;
|
|
51
|
+
let name = base;
|
|
52
|
+
for (let suffix = 2; this.used.has(name.toUpperCase()); suffix++) {
|
|
53
|
+
name = `${base}_${suffix}`;
|
|
54
|
+
}
|
|
55
|
+
this.reserve(name);
|
|
56
|
+
return name;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Constant,
|
|
3
|
+
type CoreExpression,
|
|
4
|
+
type CoreStatement,
|
|
5
|
+
exactIntegerLimit,
|
|
6
|
+
type GeneratedObject,
|
|
7
|
+
integerRange,
|
|
8
|
+
integerType,
|
|
9
|
+
nestedBodies,
|
|
10
|
+
type PlcType,
|
|
11
|
+
pathIndexes,
|
|
12
|
+
statementExpressions,
|
|
13
|
+
subexpressions,
|
|
14
|
+
} from "@tsplc/compiler/ir";
|
|
15
|
+
import {
|
|
16
|
+
type BinaryOperator,
|
|
17
|
+
type ElementaryFunction,
|
|
18
|
+
floatMaximum,
|
|
19
|
+
isElementary,
|
|
20
|
+
} from "@tsplc/iec/arithmetic";
|
|
21
|
+
import { required } from "./required";
|
|
22
|
+
|
|
23
|
+
/** The Structured Text function each elementary function is. */
|
|
24
|
+
const elementaryNames: Readonly<Record<ElementaryFunction, string>> = {
|
|
25
|
+
sqrt: "SQRT",
|
|
26
|
+
ln: "LN",
|
|
27
|
+
log10: "LOG",
|
|
28
|
+
exp: "EXP",
|
|
29
|
+
sin: "SIN",
|
|
30
|
+
cos: "COS",
|
|
31
|
+
tan: "TAN",
|
|
32
|
+
asin: "ASIN",
|
|
33
|
+
acos: "ACOS",
|
|
34
|
+
atan: "ATAN",
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** The operators written as an ST operator between their operands, and their tokens. */
|
|
38
|
+
type InfixOperator = Exclude<
|
|
39
|
+
BinaryOperator,
|
|
40
|
+
"and" | "or" | "power" | "shiftLeft" | "shiftRight"
|
|
41
|
+
>;
|
|
42
|
+
const operatorTokens: Readonly<Record<InfixOperator, string>> = {
|
|
43
|
+
add: "+",
|
|
44
|
+
subtract: "-",
|
|
45
|
+
multiply: "*",
|
|
46
|
+
divide: "/",
|
|
47
|
+
remainder: "MOD",
|
|
48
|
+
equal: "=",
|
|
49
|
+
notEqual: "<>",
|
|
50
|
+
less: "<",
|
|
51
|
+
lessEqual: "<=",
|
|
52
|
+
greater: ">",
|
|
53
|
+
greaterEqual: ">=",
|
|
54
|
+
bitAnd: "AND",
|
|
55
|
+
bitOr: "OR",
|
|
56
|
+
bitXor: "XOR",
|
|
57
|
+
};
|
|
58
|
+
const isInfix = (operator: BinaryOperator): operator is InfixOperator =>
|
|
59
|
+
Object.hasOwn(operatorTokens, operator);
|
|
60
|
+
|
|
61
|
+
export function scalarLiteral(value: Constant, type: string): string {
|
|
62
|
+
if (value.kind === "bool") {
|
|
63
|
+
return value.value ? "TRUE" : "FALSE";
|
|
64
|
+
}
|
|
65
|
+
if (value.kind === "integer") {
|
|
66
|
+
return `${type}#${value.value}`;
|
|
67
|
+
}
|
|
68
|
+
if (value.kind === "float") {
|
|
69
|
+
// String(-0) loses the IEEE sign; keep it explicit in target float literals.
|
|
70
|
+
const text = Object.is(value.value, -0)
|
|
71
|
+
? "-0.0"
|
|
72
|
+
: String(value.value).replace(/e/i, "E");
|
|
73
|
+
return `${type}#${text}`;
|
|
74
|
+
}
|
|
75
|
+
throw new Error("Expected verified scalar constant");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class NumericLowering {
|
|
79
|
+
readonly objects: GeneratedObject[] = [];
|
|
80
|
+
private readonly helpers = new Map<string, string>();
|
|
81
|
+
|
|
82
|
+
constructor(
|
|
83
|
+
private readonly types: ReadonlyMap<string, PlcType>,
|
|
84
|
+
private readonly allocate: (label: string) => string,
|
|
85
|
+
/** What a `now()` reads; undefined for a program that never calls it. */
|
|
86
|
+
private readonly scanClock: string | undefined,
|
|
87
|
+
) {}
|
|
88
|
+
|
|
89
|
+
private prepare(value: CoreExpression): void {
|
|
90
|
+
for (const operand of subexpressions(value)) {
|
|
91
|
+
this.prepare(operand);
|
|
92
|
+
}
|
|
93
|
+
if (value.kind === "binary" && value.policy) {
|
|
94
|
+
this.arithmetic(value.type, value.operator, value.policy);
|
|
95
|
+
}
|
|
96
|
+
if (value.kind === "convert" && value.policy === "saturate") {
|
|
97
|
+
this.saturation(value.operand.type, value.type);
|
|
98
|
+
}
|
|
99
|
+
if (value.kind === "unary" && value.operator === "truncate") {
|
|
100
|
+
this.truncation(value.type);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
prepareStatements(items: readonly CoreStatement[]): void {
|
|
105
|
+
for (const item of items) {
|
|
106
|
+
// Helpers are emitted in the order they are found, and the build id hashes that order,
|
|
107
|
+
// so an assignment's value keeps being prepared before its place's indexes.
|
|
108
|
+
const values =
|
|
109
|
+
item.kind === "assign"
|
|
110
|
+
? [item.value, ...pathIndexes(item.path)]
|
|
111
|
+
: statementExpressions(item);
|
|
112
|
+
for (const value of values) {
|
|
113
|
+
this.prepare(value);
|
|
114
|
+
}
|
|
115
|
+
for (const body of nestedBodies(item)) {
|
|
116
|
+
this.prepareStatements(body);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
render(value: CoreExpression, operands: readonly string[]): string {
|
|
122
|
+
if (value.kind === "convert") {
|
|
123
|
+
return value.policy === "saturate"
|
|
124
|
+
? `${this.saturation(value.operand.type, value.type)}(${operands[0]})`
|
|
125
|
+
: `TO_${value.type}(${operands[0]})`;
|
|
126
|
+
}
|
|
127
|
+
if (value.kind === "unary") {
|
|
128
|
+
// Avoid signed intermediate casts: TIME spans the complete UDINT range.
|
|
129
|
+
if (value.operator === "timeToMilliseconds") {
|
|
130
|
+
return `TIME_TO_UDINT(${operands[0]})`;
|
|
131
|
+
}
|
|
132
|
+
if (value.operator === "clock") {
|
|
133
|
+
return required(this.scanClock);
|
|
134
|
+
}
|
|
135
|
+
if (isElementary(value.operator)) {
|
|
136
|
+
return `${elementaryNames[value.operator]}(${operands[0]})`;
|
|
137
|
+
}
|
|
138
|
+
if (value.operator === "truncate") {
|
|
139
|
+
return `${this.truncation(value.type)}(${operands[0]})`;
|
|
140
|
+
}
|
|
141
|
+
if (value.operator === "not") {
|
|
142
|
+
return `(NOT ${operands[0]})`;
|
|
143
|
+
}
|
|
144
|
+
const sign = value.operator === "negate" ? "-" : "NOT ";
|
|
145
|
+
return `TO_${value.type}(${sign}${operands[0]})`;
|
|
146
|
+
}
|
|
147
|
+
if (value.kind !== "binary") {
|
|
148
|
+
throw new Error("Expected numeric operation");
|
|
149
|
+
}
|
|
150
|
+
if (value.policy) {
|
|
151
|
+
return `${this.arithmetic(value.type, value.operator, value.policy)}(${operands.join(", ")})`;
|
|
152
|
+
}
|
|
153
|
+
switch (value.operator) {
|
|
154
|
+
case "power":
|
|
155
|
+
return `EXPT(${operands.join(", ")})`;
|
|
156
|
+
case "shiftLeft":
|
|
157
|
+
case "shiftRight": {
|
|
158
|
+
const shift = value.operator === "shiftLeft" ? "SHL" : "SHR";
|
|
159
|
+
return `TO_${value.type}(${shift}(${operands.join(", ")}))`;
|
|
160
|
+
}
|
|
161
|
+
case "and":
|
|
162
|
+
case "or":
|
|
163
|
+
throw new Error("Boolean short-circuit operation was not normalized");
|
|
164
|
+
default: {
|
|
165
|
+
const text =
|
|
166
|
+
value.operator === "remainder"
|
|
167
|
+
? `(${operands[0]} - (${operands[0]} / ${operands[1]}) * ${operands[1]})`
|
|
168
|
+
: `(${operands[0]} ${operatorTokens[value.operator]} ${operands[1]})`;
|
|
169
|
+
return value.type === "BOOL" ? text : `TO_${value.type}(${text})`;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private helper(
|
|
175
|
+
key: string,
|
|
176
|
+
type: string,
|
|
177
|
+
inputs: readonly [string, string][],
|
|
178
|
+
body: (name: string) => string,
|
|
179
|
+
locals = "",
|
|
180
|
+
): string {
|
|
181
|
+
const existing = this.helpers.get(key);
|
|
182
|
+
if (existing) {
|
|
183
|
+
return existing;
|
|
184
|
+
}
|
|
185
|
+
const name = this.allocate(`numeric_${key}`);
|
|
186
|
+
this.helpers.set(key, name);
|
|
187
|
+
const parameters = inputs
|
|
188
|
+
.map(([input, inputType]) => ` ${input} : ${inputType};`)
|
|
189
|
+
.join("\n");
|
|
190
|
+
const variables = locals
|
|
191
|
+
? `VAR
|
|
192
|
+
${locals}
|
|
193
|
+
END_VAR
|
|
194
|
+
`
|
|
195
|
+
: "";
|
|
196
|
+
const declaration = `FUNCTION ${name} : ${type}
|
|
197
|
+
VAR_INPUT
|
|
198
|
+
${parameters}
|
|
199
|
+
END_VAR
|
|
200
|
+
${variables}`;
|
|
201
|
+
this.objects.push({
|
|
202
|
+
id: `numeric:${key}`,
|
|
203
|
+
kind: "function",
|
|
204
|
+
name,
|
|
205
|
+
declaration,
|
|
206
|
+
implementation: body(name),
|
|
207
|
+
});
|
|
208
|
+
return name;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Modulo 2^64 addition that does not rely on target integer overflow. */
|
|
212
|
+
private add64(): string {
|
|
213
|
+
return this.helper(
|
|
214
|
+
"add64",
|
|
215
|
+
"LWORD",
|
|
216
|
+
[
|
|
217
|
+
["a", "LWORD"],
|
|
218
|
+
["b", "LWORD"],
|
|
219
|
+
],
|
|
220
|
+
(name) => `x := a;
|
|
221
|
+
y := b;
|
|
222
|
+
FOR i := 1 TO 64 DO
|
|
223
|
+
carry := SHL(x AND y, 1);
|
|
224
|
+
x := x XOR y;
|
|
225
|
+
y := carry;
|
|
226
|
+
END_FOR;
|
|
227
|
+
${name} := x;
|
|
228
|
+
`,
|
|
229
|
+
` x, y, carry : LWORD;
|
|
230
|
+
i : UINT;`,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private wrap64(operator: InfixOperator): string {
|
|
235
|
+
const add = this.add64();
|
|
236
|
+
if (operator === "add") {
|
|
237
|
+
return add;
|
|
238
|
+
}
|
|
239
|
+
if (operator === "subtract") {
|
|
240
|
+
return this.helper(
|
|
241
|
+
"subtract64",
|
|
242
|
+
"LWORD",
|
|
243
|
+
[
|
|
244
|
+
["a", "LWORD"],
|
|
245
|
+
["b", "LWORD"],
|
|
246
|
+
],
|
|
247
|
+
(name) => `${name} := ${add}(a, ${add}(NOT b, LWORD#1));
|
|
248
|
+
`,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
if (operator !== "multiply") {
|
|
252
|
+
throw new Error("Unsupported verified wrapping operation");
|
|
253
|
+
}
|
|
254
|
+
return this.helper(
|
|
255
|
+
"multiply64",
|
|
256
|
+
"LWORD",
|
|
257
|
+
[
|
|
258
|
+
["a", "LWORD"],
|
|
259
|
+
["b", "LWORD"],
|
|
260
|
+
],
|
|
261
|
+
(name) => `x := a;
|
|
262
|
+
y := b;
|
|
263
|
+
result := LWORD#0;
|
|
264
|
+
FOR i := 1 TO 64 DO
|
|
265
|
+
IF (y AND LWORD#1) <> LWORD#0 THEN
|
|
266
|
+
result := ${add}(result, x);
|
|
267
|
+
END_IF;
|
|
268
|
+
x := SHL(x, 1);
|
|
269
|
+
y := SHR(y, 1);
|
|
270
|
+
END_FOR;
|
|
271
|
+
${name} := result;
|
|
272
|
+
`,
|
|
273
|
+
` x, y, result : LWORD;
|
|
274
|
+
i : UINT;`,
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private arithmetic(
|
|
279
|
+
typeId: string,
|
|
280
|
+
operator: BinaryOperator,
|
|
281
|
+
policy: "wrap" | "saturate",
|
|
282
|
+
): string {
|
|
283
|
+
const type = this.types.get(typeId);
|
|
284
|
+
if (type?.kind !== "integer" || !isInfix(operator)) {
|
|
285
|
+
throw new Error("Expected verified integer arithmetic");
|
|
286
|
+
}
|
|
287
|
+
const token = operatorTokens[operator];
|
|
288
|
+
const key = `${policy}_${operator}_${typeId}`;
|
|
289
|
+
if (policy === "wrap") {
|
|
290
|
+
if (type.bits === 64) {
|
|
291
|
+
const operation = this.wrap64(operator);
|
|
292
|
+
return this.helper(
|
|
293
|
+
key,
|
|
294
|
+
typeId,
|
|
295
|
+
[
|
|
296
|
+
["a", typeId],
|
|
297
|
+
["b", typeId],
|
|
298
|
+
],
|
|
299
|
+
(
|
|
300
|
+
name,
|
|
301
|
+
) => `${name} := LWORD_TO_${typeId}(${operation}(${typeId}_TO_LWORD(a), ${typeId}_TO_LWORD(b)));
|
|
302
|
+
`,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
const wide = !type.signed && operator !== "subtract" ? "ULINT" : "LINT";
|
|
306
|
+
return this.helper(
|
|
307
|
+
key,
|
|
308
|
+
typeId,
|
|
309
|
+
[
|
|
310
|
+
["a", typeId],
|
|
311
|
+
["b", typeId],
|
|
312
|
+
],
|
|
313
|
+
(
|
|
314
|
+
name,
|
|
315
|
+
) => `${name} := ${wide}_TO_${typeId}(${typeId}_TO_${wide}(a) ${token} ${typeId}_TO_${wide}(b));
|
|
316
|
+
`,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const [minimum, maximum] = integerRange(type);
|
|
321
|
+
const min = `${typeId}#${minimum}`;
|
|
322
|
+
const max = `${typeId}#${maximum}`;
|
|
323
|
+
const zero = `${typeId}#0`;
|
|
324
|
+
return this.helper(
|
|
325
|
+
key,
|
|
326
|
+
typeId,
|
|
327
|
+
[
|
|
328
|
+
["a", typeId],
|
|
329
|
+
["b", typeId],
|
|
330
|
+
],
|
|
331
|
+
(name) => {
|
|
332
|
+
const clamp = (
|
|
333
|
+
condition: string,
|
|
334
|
+
endpoint: string,
|
|
335
|
+
depth = "",
|
|
336
|
+
) => `${depth}IF ${condition} THEN
|
|
337
|
+
${depth} ${name} := ${endpoint};
|
|
338
|
+
${depth} RETURN;
|
|
339
|
+
${depth}END_IF;
|
|
340
|
+
`;
|
|
341
|
+
const guard = (): string => {
|
|
342
|
+
if (!type.signed) {
|
|
343
|
+
// Unsigned subtraction has only underflow; the other operations have only overflow.
|
|
344
|
+
if (operator === "add") {
|
|
345
|
+
return clamp(`a > ${max} - b`, max);
|
|
346
|
+
}
|
|
347
|
+
if (operator === "subtract") {
|
|
348
|
+
return clamp("a < b", zero);
|
|
349
|
+
}
|
|
350
|
+
return `IF b <> ${zero} THEN
|
|
351
|
+
${clamp(`a > ${max} / b`, max, " ")}END_IF;
|
|
352
|
+
`;
|
|
353
|
+
}
|
|
354
|
+
if (operator === "add") {
|
|
355
|
+
return `IF b > ${zero} THEN
|
|
356
|
+
${clamp(`a > ${max} - b`, max, " ")}ELSIF b < ${zero} THEN
|
|
357
|
+
${clamp(`a < ${min} - b`, min, " ")}END_IF;
|
|
358
|
+
`;
|
|
359
|
+
}
|
|
360
|
+
if (operator === "subtract") {
|
|
361
|
+
return `IF b < ${zero} THEN
|
|
362
|
+
${clamp(`a > ${max} + b`, max, " ")}ELSIF b > ${zero} THEN
|
|
363
|
+
${clamp(`a < ${min} + b`, min, " ")}END_IF;
|
|
364
|
+
`;
|
|
365
|
+
}
|
|
366
|
+
// Handle zero first, then divide the endpoint before multiplying. Each sign
|
|
367
|
+
// quadrant tests representability without forming an overflowing product.
|
|
368
|
+
return (
|
|
369
|
+
clamp(`a = ${zero}`, zero) +
|
|
370
|
+
clamp(`b = ${zero}`, zero) +
|
|
371
|
+
`IF a > ${zero} THEN
|
|
372
|
+
IF b > ${zero} THEN
|
|
373
|
+
${clamp(`a > ${max} / b`, max, " ")} ELSE
|
|
374
|
+
${clamp(`b < ${min} / a`, min, " ")} END_IF;
|
|
375
|
+
ELSE
|
|
376
|
+
IF b > ${zero} THEN
|
|
377
|
+
${clamp(`a < ${min} / b`, min, " ")} ELSE
|
|
378
|
+
${clamp(`a < ${max} / b`, max, " ")} END_IF;
|
|
379
|
+
END_IF;
|
|
380
|
+
`
|
|
381
|
+
);
|
|
382
|
+
};
|
|
383
|
+
return `${guard()}${name} := a ${token} b;
|
|
384
|
+
`;
|
|
385
|
+
},
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* A float toward zero, as a float. TRUNC returns DINT, so the whole part goes through LINT, which
|
|
391
|
+
* holds every float below the size where each float is already whole; those stay as they are.
|
|
392
|
+
*/
|
|
393
|
+
private truncation(typeId: string): string {
|
|
394
|
+
const type = this.types.get(typeId);
|
|
395
|
+
if (type?.kind !== "float") {
|
|
396
|
+
throw new Error("Expected verified float truncation");
|
|
397
|
+
}
|
|
398
|
+
const whole = scalarLiteral(
|
|
399
|
+
{ kind: "float", value: Number(exactIntegerLimit(type) / 2n) },
|
|
400
|
+
typeId,
|
|
401
|
+
);
|
|
402
|
+
return this.helper(
|
|
403
|
+
`truncate_${typeId}`,
|
|
404
|
+
typeId,
|
|
405
|
+
[["value", typeId]],
|
|
406
|
+
(name) => `IF ABS(value) >= ${whole} THEN
|
|
407
|
+
${name} := value;
|
|
408
|
+
RETURN;
|
|
409
|
+
END_IF;
|
|
410
|
+
nearest := TO_LINT(value);
|
|
411
|
+
IF value >= 0 AND TO_${typeId}(nearest) > value THEN
|
|
412
|
+
nearest := nearest - 1;
|
|
413
|
+
ELSIF value < 0 AND TO_${typeId}(nearest) < value THEN
|
|
414
|
+
nearest := nearest + 1;
|
|
415
|
+
END_IF;
|
|
416
|
+
${name} := TO_${typeId}(nearest);
|
|
417
|
+
`,
|
|
418
|
+
" nearest : LINT;",
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* A float toward zero, clamped to an integer type. The upper test is against the first float
|
|
424
|
+
* past the maximum, a power of two each float type holds, since the maximum itself may round
|
|
425
|
+
* up. NaN fails every comparison and so lands on zero, as `saturateFloatToInteger` gives.
|
|
426
|
+
*/
|
|
427
|
+
private floatToInteger(
|
|
428
|
+
source: Extract<PlcType, { kind: "float" }>,
|
|
429
|
+
target: Extract<PlcType, { kind: "integer" | "bits" }>,
|
|
430
|
+
): string {
|
|
431
|
+
const [minimum, maximum] = integerRange(target);
|
|
432
|
+
const literal = (value: bigint): string =>
|
|
433
|
+
scalarLiteral({ kind: "float", value: Number(value) }, source.id);
|
|
434
|
+
const truncate = this.truncation(source.id);
|
|
435
|
+
return this.helper(
|
|
436
|
+
`saturate_${source.id}_${target.id}`,
|
|
437
|
+
target.id,
|
|
438
|
+
[["value", source.id]],
|
|
439
|
+
(
|
|
440
|
+
name,
|
|
441
|
+
) => `IF (value > ${literal(minimum)}) AND (value < ${literal(maximum + 1n)}) THEN
|
|
442
|
+
${name} := TO_${target.id}(${truncate}(value));
|
|
443
|
+
ELSIF value >= ${literal(maximum + 1n)} THEN
|
|
444
|
+
${name} := ${target.id}#${maximum};
|
|
445
|
+
ELSIF value <= ${literal(minimum)} THEN
|
|
446
|
+
${name} := ${target.id}#${minimum};
|
|
447
|
+
ELSE
|
|
448
|
+
${name} := ${target.id}#0;
|
|
449
|
+
END_IF;
|
|
450
|
+
`,
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
private saturation(sourceId: string, targetId: string): string {
|
|
455
|
+
const source = this.types.get(sourceId);
|
|
456
|
+
const target = this.types.get(targetId);
|
|
457
|
+
if (source?.kind === "float" && target?.kind === "float") {
|
|
458
|
+
// Widening cannot exceed the destination's finite range. Identity
|
|
459
|
+
// conversions are removed by normalization; neither needs a helper.
|
|
460
|
+
if (source.bits <= target.bits) {
|
|
461
|
+
return `TO_${targetId}`;
|
|
462
|
+
}
|
|
463
|
+
const maximum = floatMaximum(target);
|
|
464
|
+
const literal = (value: number, type: string): string =>
|
|
465
|
+
scalarLiteral({ kind: "float", value }, type);
|
|
466
|
+
return this.helper(
|
|
467
|
+
`saturate_${sourceId}_${targetId}`,
|
|
468
|
+
targetId,
|
|
469
|
+
[["value", sourceId]],
|
|
470
|
+
(name) => `IF value < ${literal(-maximum, sourceId)} THEN
|
|
471
|
+
${name} := ${literal(-maximum, targetId)};
|
|
472
|
+
RETURN;
|
|
473
|
+
END_IF;
|
|
474
|
+
IF value > ${literal(maximum, sourceId)} THEN
|
|
475
|
+
${name} := ${literal(maximum, targetId)};
|
|
476
|
+
RETURN;
|
|
477
|
+
END_IF;
|
|
478
|
+
${name} := TO_${targetId}(value);
|
|
479
|
+
`,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
if (source?.kind === "float" && target && integerType(target)) {
|
|
483
|
+
return this.floatToInteger(source, target);
|
|
484
|
+
}
|
|
485
|
+
if (!source || !target || !integerType(source) || !integerType(target)) {
|
|
486
|
+
throw new Error("Expected verified integer saturation");
|
|
487
|
+
}
|
|
488
|
+
const [fromMin, fromMax] = integerRange(source);
|
|
489
|
+
const [toMin, toMax] = integerRange(target);
|
|
490
|
+
return this.helper(
|
|
491
|
+
`saturate_${sourceId}_${targetId}`,
|
|
492
|
+
targetId,
|
|
493
|
+
[["value", sourceId]],
|
|
494
|
+
(name) => {
|
|
495
|
+
const below =
|
|
496
|
+
fromMin < toMin
|
|
497
|
+
? `IF value < ${sourceId}#${toMin} THEN
|
|
498
|
+
${name} := ${targetId}#${toMin};
|
|
499
|
+
RETURN;
|
|
500
|
+
END_IF;
|
|
501
|
+
`
|
|
502
|
+
: "";
|
|
503
|
+
const above =
|
|
504
|
+
fromMax > toMax
|
|
505
|
+
? `IF value > ${sourceId}#${toMax} THEN
|
|
506
|
+
${name} := ${targetId}#${toMax};
|
|
507
|
+
RETURN;
|
|
508
|
+
END_IF;
|
|
509
|
+
`
|
|
510
|
+
: "";
|
|
511
|
+
return `${below}${above}${name} := TO_${targetId}(value);
|
|
512
|
+
`;
|
|
513
|
+
},
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
}
|