@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.
Files changed (185) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +41 -2
  3. package/dist/cli.js +2325 -0
  4. package/dist/cli.js.map +26 -0
  5. package/dist/clock.js +11 -0
  6. package/dist/clock.js.map +11 -0
  7. package/dist/codesys.js +6563 -0
  8. package/dist/codesys.js.map +51 -0
  9. package/dist/collections.js +399 -0
  10. package/dist/collections.js.map +10 -0
  11. package/dist/index.js +16198 -0
  12. package/dist/index.js.map +62 -0
  13. package/dist/ir.js +3948 -0
  14. package/dist/ir.js.map +35 -0
  15. package/dist/retain.js +10 -0
  16. package/dist/retain.js.map +10 -0
  17. package/dist/signals.js +115 -0
  18. package/dist/signals.js.map +10 -0
  19. package/dist/telemetry.js +121 -0
  20. package/dist/telemetry.js.map +12 -0
  21. package/package.json +122 -4
  22. package/src/codesys/config.ts +50 -0
  23. package/src/codesys/debug.ts +655 -0
  24. package/src/codesys/emit.ts +185 -0
  25. package/src/codesys/endpoints.ts +111 -0
  26. package/src/codesys/engineering.ts +96 -0
  27. package/src/codesys/explain/expressions.ts +445 -0
  28. package/src/codesys/explain/flags.ts +134 -0
  29. package/src/codesys/explain/parameters.ts +53 -0
  30. package/src/codesys/explain/rows.ts +182 -0
  31. package/src/codesys/explain/tables.ts +79 -0
  32. package/src/codesys/explain/tasks.ts +68 -0
  33. package/src/codesys/explain/writes.ts +497 -0
  34. package/src/codesys/explain.ts +149 -0
  35. package/src/codesys/expressions.ts +100 -0
  36. package/src/codesys/global-lists.ts +284 -0
  37. package/src/codesys/global-names.ts +66 -0
  38. package/src/codesys/http.ts +1090 -0
  39. package/src/codesys/index.ts +13 -0
  40. package/src/codesys/instrumentation.ts +574 -0
  41. package/src/codesys/io-paths.ts +149 -0
  42. package/src/codesys/loops.ts +101 -0
  43. package/src/codesys/manifest.ts +684 -0
  44. package/src/codesys/names.ts +58 -0
  45. package/src/codesys/numeric.ts +516 -0
  46. package/src/codesys/plan.ts +688 -0
  47. package/src/codesys/programs.ts +138 -0
  48. package/src/codesys/publication.ts +99 -0
  49. package/src/codesys/required.ts +8 -0
  50. package/src/codesys/routines.ts +247 -0
  51. package/src/codesys/scan-clock.ts +103 -0
  52. package/src/codesys/scheduling.ts +96 -0
  53. package/src/codesys/service-channels.ts +24 -0
  54. package/src/codesys/sources.ts +118 -0
  55. package/src/codesys/st-files.ts +219 -0
  56. package/src/codesys/statements.ts +409 -0
  57. package/src/codesys/storage.ts +150 -0
  58. package/src/codesys/symbols.ts +240 -0
  59. package/src/codesys/task-access.ts +186 -0
  60. package/src/codesys/telemetry-settings.ts +19 -0
  61. package/src/codesys/telemetry.ts +852 -0
  62. package/src/codesys/types.ts +310 -0
  63. package/src/codesys/vendor.ts +113 -0
  64. package/src/frontend/analyzer.ts +779 -0
  65. package/src/frontend/async.ts +530 -0
  66. package/src/frontend/blocks.ts +578 -0
  67. package/src/frontend/configuration.ts +83 -0
  68. package/src/frontend/const-enums.ts +196 -0
  69. package/src/frontend/const-functions.ts +204 -0
  70. package/src/frontend/control-flow.ts +787 -0
  71. package/src/frontend/cooperative.ts +114 -0
  72. package/src/frontend/coroutines.ts +1344 -0
  73. package/src/frontend/dialect-names.ts +104 -0
  74. package/src/frontend/elaborate/array-searches.ts +224 -0
  75. package/src/frontend/elaborate/assignments.ts +343 -0
  76. package/src/frontend/elaborate/calls.ts +1342 -0
  77. package/src/frontend/elaborate/capture-protocol.ts +237 -0
  78. package/src/frontend/elaborate/capture.ts +388 -0
  79. package/src/frontend/elaborate/declarations.ts +124 -0
  80. package/src/frontend/elaborate/dialect.ts +473 -0
  81. package/src/frontend/elaborate/expressions.ts +2177 -0
  82. package/src/frontend/elaborate/filtered-arrays.ts +643 -0
  83. package/src/frontend/elaborate/frame.ts +145 -0
  84. package/src/frontend/elaborate/generics.ts +123 -0
  85. package/src/frontend/elaborate/graph.ts +141 -0
  86. package/src/frontend/elaborate/imports.ts +105 -0
  87. package/src/frontend/elaborate/index.ts +851 -0
  88. package/src/frontend/elaborate/initializers.ts +235 -0
  89. package/src/frontend/elaborate/iterators.ts +1541 -0
  90. package/src/frontend/elaborate/layouts.ts +1025 -0
  91. package/src/frontend/elaborate/model.ts +478 -0
  92. package/src/frontend/elaborate/names.ts +134 -0
  93. package/src/frontend/elaborate/reduce.ts +900 -0
  94. package/src/frontend/elaborate/references.ts +727 -0
  95. package/src/frontend/elaborate/retention.ts +284 -0
  96. package/src/frontend/elaborate/specialize.ts +1137 -0
  97. package/src/frontend/elaborate/statements.ts +1220 -0
  98. package/src/frontend/elaborate/storage.ts +836 -0
  99. package/src/frontend/elaborate/time-constructors.ts +34 -0
  100. package/src/frontend/elaborate/values.ts +200 -0
  101. package/src/frontend/elaborate/writes.ts +415 -0
  102. package/src/frontend/expressions.ts +1382 -0
  103. package/src/frontend/generated-names.ts +95 -0
  104. package/src/frontend/intrinsics.ts +382 -0
  105. package/src/frontend/lowering-key.ts +23 -0
  106. package/src/frontend/module-graph.ts +156 -0
  107. package/src/frontend/positions.ts +359 -0
  108. package/src/frontend/promise-chains.ts +872 -0
  109. package/src/frontend/promises.ts +419 -0
  110. package/src/frontend/routines.ts +239 -0
  111. package/src/frontend/sdk.ts +187 -0
  112. package/src/frontend/services.ts +612 -0
  113. package/src/frontend/statements.ts +871 -0
  114. package/src/frontend/tasks.ts +526 -0
  115. package/src/frontend/telemetry.ts +174 -0
  116. package/src/frontend/types.ts +1073 -0
  117. package/src/frontend/typescript.ts +659 -0
  118. package/src/index.ts +9 -0
  119. package/src/ir/aggregates.ts +291 -0
  120. package/src/ir/allocation.ts +96 -0
  121. package/src/ir/conditions.ts +855 -0
  122. package/src/ir/core-model.ts +289 -0
  123. package/src/ir/declarations.ts +281 -0
  124. package/src/ir/diagnostics.ts +68 -0
  125. package/src/ir/domains.ts +151 -0
  126. package/src/ir/fold.ts +279 -0
  127. package/src/ir/identity.ts +19 -0
  128. package/src/ir/index.ts +137 -0
  129. package/src/ir/job-errors.ts +69 -0
  130. package/src/ir/layouts.ts +211 -0
  131. package/src/ir/link.ts +302 -0
  132. package/src/ir/model.ts +394 -0
  133. package/src/ir/normalization-error.ts +10 -0
  134. package/src/ir/normalize.ts +52 -0
  135. package/src/ir/numeric.ts +163 -0
  136. package/src/ir/operands.ts +160 -0
  137. package/src/ir/ranges.ts +420 -0
  138. package/src/ir/routine.ts +1116 -0
  139. package/src/ir/scalars.ts +453 -0
  140. package/src/ir/share.ts +790 -0
  141. package/src/ir/short-circuit.ts +140 -0
  142. package/src/ir/tables.ts +111 -0
  143. package/src/ir/temporaries.ts +138 -0
  144. package/src/ir/types.ts +151 -0
  145. package/src/project/artifacts.ts +424 -0
  146. package/src/project/cli.ts +211 -0
  147. package/src/project/debugger.ts +111 -0
  148. package/src/project/deployments.ts +387 -0
  149. package/src/project/flags.ts +14 -0
  150. package/src/project/git.ts +35 -0
  151. package/src/project/plant.ts +629 -0
  152. package/src/project/project-commands.ts +507 -0
  153. package/src/project/project.ts +559 -0
  154. package/src/project/release.ts +152 -0
  155. package/src/project/server-check.ts +54 -0
  156. package/src/project/server.ts +165 -0
  157. package/src/project/terminal.ts +45 -0
  158. package/src/runtime/async.ts +207 -0
  159. package/src/runtime/launch.ts +59 -0
  160. package/src/runtime/main.ts +59 -0
  161. package/src/runtime/missing-file.ts +3 -0
  162. package/src/runtime/process.ts +177 -0
  163. package/src/runtime/program.ts +109 -0
  164. package/src/runtime/retain.ts +108 -0
  165. package/src/runtime/runtime.ts +324 -0
  166. package/src/runtime/settings.ts +27 -0
  167. package/src/runtime/storage.ts +456 -0
  168. package/src/runtime/symbols.ts +97 -0
  169. package/src/runtime/telemetry.ts +154 -0
  170. package/src/runtime/transport.ts +168 -0
  171. package/stdlib/clock.ts +8 -0
  172. package/stdlib/collections.ts +534 -0
  173. package/stdlib/plc-time.ts +2 -0
  174. package/stdlib/retain.ts +4 -0
  175. package/stdlib/signals.ts +142 -0
  176. package/stdlib/telemetry.ts +89 -0
  177. package/types/async.d.ts +87 -0
  178. package/types/clock.d.ts +8 -0
  179. package/types/config.d.ts +2 -0
  180. package/types/data-types.d.ts +31 -0
  181. package/types/dialect.d.ts +49 -0
  182. package/types/globals.d.ts +10 -0
  183. package/types/index.d.ts +212 -0
  184. package/types/retain.d.ts +12 -0
  185. package/types/telemetry.d.ts +47 -0
@@ -0,0 +1,453 @@
1
+ import {
2
+ type ElementaryFunction,
3
+ floatMaximum,
4
+ isComparison,
5
+ isElementary,
6
+ roundFloat,
7
+ } from "@tsplc/iec/arithmetic";
8
+ import { fold } from "./fold";
9
+ import type { Constant, Expression, Variable } from "./model";
10
+ import {
11
+ admits,
12
+ canAssign,
13
+ constantFits,
14
+ exactIntegerLimit,
15
+ type IntegerInterval,
16
+ literalOutsideType,
17
+ within,
18
+ wrappingOperators,
19
+ wrapsNatively,
20
+ } from "./numeric";
21
+ import { interval } from "./ranges";
22
+ import { integerRange, integerType, type PlcType } from "./types";
23
+
24
+ /** An operand's constant value and proved integer range, over the storage in scope. */
25
+ interface Proof {
26
+ fold: (value: Expression) => Constant | undefined;
27
+ interval: (value: Expression) => IntegerInterval | undefined;
28
+ }
29
+
30
+ const scalarKinds: ReadonlySet<PlcType["kind"]> = new Set([
31
+ "bool",
32
+ "integer",
33
+ "bits",
34
+ "float",
35
+ ]);
36
+
37
+ const bitwiseOperators: ReadonlySet<string> = new Set([
38
+ "bitAnd",
39
+ "bitOr",
40
+ "bitXor",
41
+ "shiftLeft",
42
+ "shiftRight",
43
+ ]);
44
+
45
+ export const scalarIssue = (
46
+ expression: Expression,
47
+ types: ReadonlyMap<string, PlcType>,
48
+ variables: ReadonlyMap<string, Variable>,
49
+ ranges: ReadonlyMap<string, IntegerInterval>,
50
+ ): string | undefined => {
51
+ const proof: Proof = {
52
+ fold: (value) => fold(value, variables, types),
53
+ interval: (value) => interval(value, types, variables, ranges),
54
+ };
55
+ const type = types.get(expression.type);
56
+ if (!type || !scalarKinds.has(type.kind)) {
57
+ return "Unknown scalar expression type.";
58
+ }
59
+ if (expression.kind === "constant") {
60
+ return constantFits(expression.value, type)
61
+ ? undefined
62
+ : literalOutsideType;
63
+ }
64
+ if (expression.kind === "load" || expression.kind === "call") {
65
+ return undefined;
66
+ }
67
+ if (expression.kind === "convert" || expression.kind === "unary") {
68
+ const source = types.get(expression.operand.type);
69
+ if (!source) {
70
+ return "Unknown operand type.";
71
+ }
72
+ return expression.kind === "convert"
73
+ ? conversionIssue(expression, source, type, proof)
74
+ : unaryIssue(expression, source, type, proof);
75
+ }
76
+ if (expression.kind === "binary") {
77
+ const left = types.get(expression.left.type);
78
+ const right = types.get(expression.right.type);
79
+ if (!left || !right) {
80
+ return "Unknown operand type.";
81
+ }
82
+ return binaryIssue(expression, left, right, type, proof);
83
+ }
84
+ if ("operand" in expression && !types.has(expression.operand.type)) {
85
+ return "Unknown operand type.";
86
+ }
87
+ return "Expression has not been reduced to a scalar operation.";
88
+ };
89
+
90
+ const conversionIssue = (
91
+ expression: Extract<Expression, { kind: "convert" }>,
92
+ source: PlcType,
93
+ type: PlcType,
94
+ proof: Proof,
95
+ ): string | undefined => {
96
+ if (source.kind === "bool" || type.kind === "bool") {
97
+ return "Numeric conversions cannot use BOOL.";
98
+ }
99
+ if (expression.policy === "exact") {
100
+ return exactConversionIssue(expression, source, type, proof);
101
+ }
102
+ // A float saturates to an integer by truncating toward zero: there is no wrap of a float,
103
+ // whose whole part may exceed every integer width.
104
+ if (
105
+ expression.policy === "saturate" &&
106
+ source.kind === "float" &&
107
+ (type.kind === "float" || integerType(type))
108
+ ) {
109
+ return undefined;
110
+ }
111
+ if (integerType(source) && integerType(type)) {
112
+ return undefined;
113
+ }
114
+ return expression.policy === "saturate"
115
+ ? "Saturate conversions go to an integer/bit-string type from an integer, bit-string or floating operand, or between floating types."
116
+ : "Wrap conversions require integer or bit-string operands.";
117
+ };
118
+
119
+ const exactConversionIssue = (
120
+ expression: Extract<Expression, { kind: "convert" }>,
121
+ source: PlcType,
122
+ type: PlcType,
123
+ proof: Proof,
124
+ ): string | undefined => {
125
+ const value = proof.fold(expression.operand);
126
+ if (
127
+ canAssign(source, type, value) ||
128
+ (value &&
129
+ integerType(source) &&
130
+ integerType(type) &&
131
+ constantFits(value, type))
132
+ ) {
133
+ return undefined;
134
+ }
135
+ if (!integerType(source)) {
136
+ return "Exact conversion cannot be proved lossless.";
137
+ }
138
+ if (type.kind === "float" && value?.kind === "integer") {
139
+ const converted = roundFloat(Number(value.value), type);
140
+ if (
141
+ Number.isFinite(converted) &&
142
+ Number.isInteger(converted) &&
143
+ BigInt(converted) === BigInt(value.value)
144
+ ) {
145
+ return undefined;
146
+ }
147
+ }
148
+ const proved = proof.interval(expression.operand) ?? integerRange(source);
149
+ if (type.kind === "float") {
150
+ const limit = exactIntegerLimit(type);
151
+ if (within(proved, [-limit, limit])) {
152
+ return undefined;
153
+ }
154
+ }
155
+ if (integerType(type) && within(proved, integerRange(type))) {
156
+ return undefined;
157
+ }
158
+ return "Exact conversion cannot be proved lossless.";
159
+ };
160
+
161
+ const unaryIssue = (
162
+ expression: Extract<Expression, { kind: "unary" }>,
163
+ source: PlcType,
164
+ type: PlcType,
165
+ proof: Proof,
166
+ ): string | undefined => {
167
+ if (isElementary(expression.operator)) {
168
+ if (source.kind !== "float" || source.id !== type.id) {
169
+ return "Elementary functions take and return one floating type.";
170
+ }
171
+ return operandDomainIssue(
172
+ expression.operator,
173
+ expression.operand,
174
+ type,
175
+ proof,
176
+ );
177
+ }
178
+ switch (expression.operator) {
179
+ case "truncate":
180
+ return source.kind === "float" && source.id === type.id
181
+ ? undefined
182
+ : "trunc takes and returns one floating type.";
183
+ case "clock":
184
+ return type.id === "DINT"
185
+ ? undefined
186
+ : "now() returns DINT milliseconds.";
187
+ case "timeToMilliseconds":
188
+ return source.id === "TIME" &&
189
+ source.kind === "duration" &&
190
+ type.id === "UDINT"
191
+ ? undefined
192
+ : "timeToMilliseconds requires TIME and returns UDINT.";
193
+ case "not":
194
+ return type.kind === "bool" && source.kind === "bool"
195
+ ? undefined
196
+ : "Logical negation requires BOOL.";
197
+ case "bitNot":
198
+ return type.kind === "bits" && source.id === type.id
199
+ ? undefined
200
+ : "Bitwise complement requires a bit string.";
201
+ case "negate":
202
+ if (
203
+ source.id !== type.id ||
204
+ !(type.kind === "integer" || type.kind === "float")
205
+ ) {
206
+ return "Numeric negation requires one arithmetic type.";
207
+ }
208
+ return resultIssue(expression, type, proof);
209
+ }
210
+ };
211
+
212
+ const binaryIssue = (
213
+ expression: Extract<Expression, { kind: "binary" }>,
214
+ left: PlcType,
215
+ right: PlcType,
216
+ type: PlcType,
217
+ proof: Proof,
218
+ ): string | undefined => {
219
+ if (expression.operator === "and" || expression.operator === "or") {
220
+ return type.kind === "bool" && left.kind === "bool" && right.kind === "bool"
221
+ ? undefined
222
+ : "Logical operators require BOOL.";
223
+ }
224
+ if (isComparison(expression.operator)) {
225
+ return type.kind === "bool" &&
226
+ left.id === right.id &&
227
+ scalarKinds.has(left.kind) &&
228
+ (left.kind !== "bool" ||
229
+ ["equal", "notEqual"].includes(expression.operator))
230
+ ? undefined
231
+ : "Comparison operands need the same PLC domain.";
232
+ }
233
+ if (bitwiseOperators.has(expression.operator)) {
234
+ if (type.kind !== "bits" || left.id !== type.id) {
235
+ return "Bitwise operators require bit strings.";
236
+ }
237
+ if (
238
+ expression.operator === "shiftLeft" ||
239
+ expression.operator === "shiftRight"
240
+ ) {
241
+ const count = proof.fold(expression.right);
242
+ return count?.kind === "integer" &&
243
+ BigInt(count.value) >= 0n &&
244
+ BigInt(count.value) < BigInt(type.bits)
245
+ ? undefined
246
+ : "Shift count must be a compile-time integer within the bit width.";
247
+ }
248
+ return right.id === type.id ? undefined : "Bit-string widths differ.";
249
+ }
250
+ if (expression.operator === "power") {
251
+ if (type.kind !== "float" || left.id !== type.id || right.id !== type.id) {
252
+ return "A power takes and returns one floating type.";
253
+ }
254
+ return powerIssue(expression, type, proof);
255
+ }
256
+ if (
257
+ left.id !== type.id ||
258
+ right.id !== type.id ||
259
+ (type.kind !== "integer" && type.kind !== "float")
260
+ ) {
261
+ return "Arithmetic operands need one integer or floating type.";
262
+ }
263
+ if (expression.policy) {
264
+ return type.kind === "integer" && wrappingOperators.has(expression.operator)
265
+ ? undefined
266
+ : "Explicit arithmetic policies require integer addition, subtraction or multiplication.";
267
+ }
268
+ if (
269
+ (expression.operator === "divide" || expression.operator === "remainder") &&
270
+ type.kind === "integer"
271
+ ) {
272
+ const divisor = proof.interval(expression.right);
273
+ if (!divisor || admits(divisor, 0n)) {
274
+ return "Division requires a proved nonzero divisor.";
275
+ }
276
+ const dividend = proof.interval(expression.left);
277
+ const [minimum] = integerRange(type);
278
+ if (
279
+ type.signed &&
280
+ dividend &&
281
+ dividend[0] <= minimum &&
282
+ admits(divisor, -1n)
283
+ ) {
284
+ return "Signed minimum divided by -1 cannot be represented.";
285
+ }
286
+ }
287
+ return resultIssue(expression, type, proof);
288
+ };
289
+
290
+ /**
291
+ * The bounds a floating operand is proved to keep: a constant's value, or the proved range of an
292
+ * integer converted to it exactly. Other floating values have no proved bounds.
293
+ */
294
+ const floatBounds = (
295
+ expression: Expression,
296
+ proof: Proof,
297
+ ): readonly [number, number] | undefined => {
298
+ const value = proof.fold(expression);
299
+ if (value?.kind === "float") {
300
+ return [value.value, value.value];
301
+ }
302
+ if (value?.kind === "integer") {
303
+ return [Number(value.value), Number(value.value)];
304
+ }
305
+ if (expression.kind === "convert" && expression.policy === "exact") {
306
+ const proved = proof.interval(expression.operand);
307
+ return proved && [Number(proved[0]), Number(proved[1])];
308
+ }
309
+ return undefined;
310
+ };
311
+
312
+ /** Where each elementary function's result is finite, and what to prove when its operand may leave there. */
313
+ const domains: Readonly<
314
+ Record<
315
+ ElementaryFunction,
316
+ | {
317
+ holds: (low: number, high: number, type: PlcType) => boolean;
318
+ needs: string;
319
+ }
320
+ | undefined
321
+ >
322
+ > = {
323
+ sqrt: { holds: (low) => low >= 0, needs: "not negative" },
324
+ ln: { holds: (low) => low > 0, needs: "greater than zero" },
325
+ log10: { holds: (low) => low > 0, needs: "greater than zero" },
326
+ exp: {
327
+ holds: (_, high, type) =>
328
+ type.kind === "float" &&
329
+ Number.isFinite(roundFloat(Math.exp(high), type)),
330
+ needs: "small enough for a finite result",
331
+ },
332
+ asin: {
333
+ holds: (low, high) => low >= -1 && high <= 1,
334
+ needs: "between -1 and 1",
335
+ },
336
+ acos: {
337
+ holds: (low, high) => low >= -1 && high <= 1,
338
+ needs: "between -1 and 1",
339
+ },
340
+ sin: undefined,
341
+ cos: undefined,
342
+ tan: undefined,
343
+ atan: undefined,
344
+ };
345
+
346
+ /** A finite float operand keeps sine, cosine, tangent and arctangent finite; the others need their operand's domain proved. */
347
+ const operandDomainIssue = (
348
+ operator: ElementaryFunction,
349
+ operand: Expression,
350
+ type: PlcType,
351
+ proof: Proof,
352
+ ): string | undefined => {
353
+ const domain = domains[operator];
354
+ if (!domain) {
355
+ return undefined;
356
+ }
357
+ const bounds = floatBounds(operand, proof);
358
+ if (bounds && domain.holds(bounds[0], bounds[1], type)) {
359
+ return undefined;
360
+ }
361
+ return `${operator} needs its operand proved ${domain.needs}, as a constant or an integer converted exactly is.`;
362
+ };
363
+
364
+ /** A power needs a constant exponent and a base whose proved bounds keep the result real and finite. */
365
+ const powerIssue = (
366
+ expression: Extract<Expression, { kind: "binary" }>,
367
+ type: PlcType,
368
+ proof: Proof,
369
+ ): string | undefined => {
370
+ const folded = proof.fold(expression);
371
+ if (folded) {
372
+ return constantFits(folded, type)
373
+ ? undefined
374
+ : "Floating result is not finite or representable.";
375
+ }
376
+ const exponent = proof.fold(expression.right);
377
+ const base = floatBounds(expression.left, proof);
378
+ if (exponent?.kind !== "float" || !base || type.kind !== "float") {
379
+ return "A power needs a constant exponent and a base proved within bounds, as a constant or an integer converted exactly is.";
380
+ }
381
+ const [low, high] = base;
382
+ if (!Number.isInteger(exponent.value) && low < 0) {
383
+ return "A fractional power needs its base proved not negative.";
384
+ }
385
+ if (exponent.value < 0 && low <= 0 && high >= 0) {
386
+ return "A negative power needs its base proved not zero.";
387
+ }
388
+ const magnitudes = [Math.abs(low), Math.abs(high)];
389
+ const largest =
390
+ exponent.value >= 0
391
+ ? Math.max(...magnitudes) ** exponent.value
392
+ : Math.min(...magnitudes) ** exponent.value;
393
+ return Number.isFinite(roundFloat(largest, type))
394
+ ? undefined
395
+ : "The power may not be finite.";
396
+ };
397
+
398
+ /** Whether an arithmetic result is proved to stay in its integer type, or finite in its float type. */
399
+ const resultIssue = (
400
+ expression: Extract<Expression, { kind: "unary" | "binary" }>,
401
+ type: PlcType,
402
+ proof: Proof,
403
+ ): string | undefined => {
404
+ if (integerType(type)) {
405
+ const result = proof.interval(expression);
406
+ if (
407
+ !(wrapsNatively(expression) && !proof.fold(expression)) &&
408
+ (!result || !within(result, integerRange(type)))
409
+ ) {
410
+ return "Arithmetic may overflow; widen the result or use an explicit wrap/saturate operation.";
411
+ }
412
+ return undefined;
413
+ }
414
+ if (type.kind !== "float") {
415
+ return undefined;
416
+ }
417
+ const result = proof.fold(expression);
418
+ if (result) {
419
+ return constantFits(result, type)
420
+ ? undefined
421
+ : "Floating result is not finite or representable.";
422
+ }
423
+ if (expression.kind === "unary") {
424
+ return undefined;
425
+ }
426
+ const lhs = proof.fold(expression.left);
427
+ const rhs = proof.fold(expression.right);
428
+ if (
429
+ ["add", "subtract"].includes(expression.operator) &&
430
+ (lhs?.kind === "float" || rhs?.kind === "float")
431
+ ) {
432
+ // Addition/subtraction and nearest rounding are monotone. Checking both
433
+ // finite endpoints proves every finite input without changing its arithmetic.
434
+ const maximum = floatMaximum(type);
435
+ const finite = [-maximum, maximum].every((endpoint) => {
436
+ const a = lhs?.kind === "float" ? lhs.value : endpoint;
437
+ const b = rhs?.kind === "float" ? rhs.value : endpoint;
438
+ const value = expression.operator === "add" ? a + b : a - b;
439
+ return Number.isFinite(roundFloat(value, type));
440
+ });
441
+ if (finite) {
442
+ return undefined;
443
+ }
444
+ }
445
+ if (
446
+ rhs?.kind === "float" &&
447
+ rhs.value === 1 &&
448
+ ["multiply", "divide"].includes(expression.operator)
449
+ ) {
450
+ return undefined;
451
+ }
452
+ return "Floating arithmetic must prove a finite result.";
453
+ };