@sccharts-lab/engine 0.0.0-stage → 0.1.0

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 (279) hide show
  1. package/LICENSE +277 -0
  2. package/README.md +125 -2
  3. package/dist/codegen/expr.d.ts +36 -0
  4. package/dist/codegen/expr.js +252 -0
  5. package/dist/codegen/expr.js.map +1 -0
  6. package/dist/codegen/index.d.ts +4 -0
  7. package/dist/codegen/index.js +238 -0
  8. package/dist/codegen/index.js.map +1 -0
  9. package/dist/codegen/layout.d.ts +57 -0
  10. package/dist/codegen/layout.js +306 -0
  11. package/dist/codegen/layout.js.map +1 -0
  12. package/dist/codegen/logic.d.ts +31 -0
  13. package/dist/codegen/logic.js +258 -0
  14. package/dist/codegen/logic.js.map +1 -0
  15. package/dist/codegen/methods.d.ts +17 -0
  16. package/dist/codegen/methods.js +242 -0
  17. package/dist/codegen/methods.js.map +1 -0
  18. package/dist/codegen/reset.d.ts +6 -0
  19. package/dist/codegen/reset.js +110 -0
  20. package/dist/codegen/reset.js.map +1 -0
  21. package/dist/codegen/types.d.ts +81 -0
  22. package/dist/codegen/types.js +3 -0
  23. package/dist/codegen/types.js.map +1 -0
  24. package/dist/format.d.ts +136 -0
  25. package/dist/format.js +872 -0
  26. package/dist/format.js.map +1 -0
  27. package/dist/index.d.ts +120 -0
  28. package/dist/index.js +507 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/internal.d.ts +1 -0
  31. package/dist/internal.js +4 -0
  32. package/dist/internal.js.map +1 -0
  33. package/dist/parser/ids.d.ts +4 -0
  34. package/dist/parser/ids.js +172 -0
  35. package/dist/parser/ids.js.map +1 -0
  36. package/dist/parser/imports.d.ts +2 -0
  37. package/dist/parser/imports.js +28 -0
  38. package/dist/parser/imports.js.map +1 -0
  39. package/dist/parser/interface.d.ts +10 -0
  40. package/dist/parser/interface.js +93 -0
  41. package/dist/parser/interface.js.map +1 -0
  42. package/dist/parser/kielerWording.d.ts +2 -0
  43. package/dist/parser/kielerWording.js +196 -0
  44. package/dist/parser/kielerWording.js.map +1 -0
  45. package/dist/parser/lexer.d.ts +25 -0
  46. package/dist/parser/lexer.js +334 -0
  47. package/dist/parser/lexer.js.map +1 -0
  48. package/dist/parser/parser.d.ts +29 -0
  49. package/dist/parser/parser.js +3249 -0
  50. package/dist/parser/parser.js.map +1 -0
  51. package/dist/parser/resolve.d.ts +46 -0
  52. package/dist/parser/resolve.js +919 -0
  53. package/dist/parser/resolve.js.map +1 -0
  54. package/dist/parser/source.d.ts +15 -0
  55. package/dist/parser/source.js +42 -0
  56. package/dist/parser/source.js.map +1 -0
  57. package/dist/parser/validate.d.ts +5 -0
  58. package/dist/parser/validate.js +1430 -0
  59. package/dist/parser/validate.js.map +1 -0
  60. package/dist/sim/abortRegions.d.ts +9 -0
  61. package/dist/sim/abortRegions.js +192 -0
  62. package/dist/sim/abortRegions.js.map +1 -0
  63. package/dist/sim/analysis.d.ts +113 -0
  64. package/dist/sim/analysis.js +1104 -0
  65. package/dist/sim/analysis.js.map +1 -0
  66. package/dist/sim/codegen.d.ts +30 -0
  67. package/dist/sim/codegen.js +730 -0
  68. package/dist/sim/codegen.js.map +1 -0
  69. package/dist/sim/compile.d.ts +43 -0
  70. package/dist/sim/compile.js +1062 -0
  71. package/dist/sim/compile.js.map +1 -0
  72. package/dist/sim/deps.d.ts +73 -0
  73. package/dist/sim/deps.js +481 -0
  74. package/dist/sim/deps.js.map +1 -0
  75. package/dist/sim/describe.d.ts +22 -0
  76. package/dist/sim/describe.js +272 -0
  77. package/dist/sim/describe.js.map +1 -0
  78. package/dist/sim/describePreemption.d.ts +22 -0
  79. package/dist/sim/describePreemption.js +90 -0
  80. package/dist/sim/describePreemption.js.map +1 -0
  81. package/dist/sim/describeStructure.d.ts +75 -0
  82. package/dist/sim/describeStructure.js +293 -0
  83. package/dist/sim/describeStructure.js.map +1 -0
  84. package/dist/sim/eval.d.ts +99 -0
  85. package/dist/sim/eval.js +644 -0
  86. package/dist/sim/eval.js.map +1 -0
  87. package/dist/sim/exec.d.ts +212 -0
  88. package/dist/sim/exec.js +874 -0
  89. package/dist/sim/exec.js.map +1 -0
  90. package/dist/sim/fold.d.ts +2 -0
  91. package/dist/sim/fold.js +40 -0
  92. package/dist/sim/fold.js.map +1 -0
  93. package/dist/sim/goblock.d.ts +16 -0
  94. package/dist/sim/goblock.js +171 -0
  95. package/dist/sim/goblock.js.map +1 -0
  96. package/dist/sim/guardIndices.d.ts +13 -0
  97. package/dist/sim/guardIndices.js +137 -0
  98. package/dist/sim/guardIndices.js.map +1 -0
  99. package/dist/sim/index.d.ts +43 -0
  100. package/dist/sim/index.js +138 -0
  101. package/dist/sim/index.js.map +1 -0
  102. package/dist/sim/lower.d.ts +33 -0
  103. package/dist/sim/lower.js +716 -0
  104. package/dist/sim/lower.js.map +1 -0
  105. package/dist/sim/memory.d.ts +47 -0
  106. package/dist/sim/memory.js +328 -0
  107. package/dist/sim/memory.js.map +1 -0
  108. package/dist/sim/model.d.ts +487 -0
  109. package/dist/sim/model.js +772 -0
  110. package/dist/sim/model.js.map +1 -0
  111. package/dist/sim/passes/abort.d.ts +2 -0
  112. package/dist/sim/passes/abort.js +269 -0
  113. package/dist/sim/passes/abort.js.map +1 -0
  114. package/dist/sim/passes/abortRegion.d.ts +10 -0
  115. package/dist/sim/passes/abortRegion.js +62 -0
  116. package/dist/sim/passes/abortRegion.js.map +1 -0
  117. package/dist/sim/passes/arrayAssignment.d.ts +2 -0
  118. package/dist/sim/passes/arrayAssignment.js +147 -0
  119. package/dist/sim/passes/arrayAssignment.js.map +1 -0
  120. package/dist/sim/passes/codeEffects.d.ts +2 -0
  121. package/dist/sim/passes/codeEffects.js +14 -0
  122. package/dist/sim/passes/codeEffects.js.map +1 -0
  123. package/dist/sim/passes/complexFinalState.d.ts +4 -0
  124. package/dist/sim/passes/complexFinalState.js +77 -0
  125. package/dist/sim/passes/complexFinalState.js.map +1 -0
  126. package/dist/sim/passes/connector.d.ts +2 -0
  127. package/dist/sim/passes/connector.js +18 -0
  128. package/dist/sim/passes/connector.js.map +1 -0
  129. package/dist/sim/passes/contained.d.ts +37 -0
  130. package/dist/sim/passes/contained.js +198 -0
  131. package/dist/sim/passes/contained.js.map +1 -0
  132. package/dist/sim/passes/context.d.ts +39 -0
  133. package/dist/sim/passes/context.js +3 -0
  134. package/dist/sim/passes/context.js.map +1 -0
  135. package/dist/sim/passes/countDelay.d.ts +2 -0
  136. package/dist/sim/passes/countDelay.js +51 -0
  137. package/dist/sim/passes/countDelay.js.map +1 -0
  138. package/dist/sim/passes/dataflow.d.ts +11 -0
  139. package/dist/sim/passes/dataflow.js +319 -0
  140. package/dist/sim/passes/dataflow.js.map +1 -0
  141. package/dist/sim/passes/deferred.d.ts +2 -0
  142. package/dist/sim/passes/deferred.js +345 -0
  143. package/dist/sim/passes/deferred.js.map +1 -0
  144. package/dist/sim/passes/detect.d.ts +10 -0
  145. package/dist/sim/passes/detect.js +33 -0
  146. package/dist/sim/passes/detect.js.map +1 -0
  147. package/dist/sim/passes/during.d.ts +2 -0
  148. package/dist/sim/passes/during.js +67 -0
  149. package/dist/sim/passes/during.js.map +1 -0
  150. package/dist/sim/passes/entry.d.ts +5 -0
  151. package/dist/sim/passes/entry.js +93 -0
  152. package/dist/sim/passes/entry.js.map +1 -0
  153. package/dist/sim/passes/enum.d.ts +2 -0
  154. package/dist/sim/passes/enum.js +22 -0
  155. package/dist/sim/passes/enum.js.map +1 -0
  156. package/dist/sim/passes/exit.d.ts +4 -0
  157. package/dist/sim/passes/exit.js +120 -0
  158. package/dist/sim/passes/exit.js.map +1 -0
  159. package/dist/sim/passes/finalRegion.d.ts +2 -0
  160. package/dist/sim/passes/finalRegion.js +21 -0
  161. package/dist/sim/passes/finalRegion.js.map +1 -0
  162. package/dist/sim/passes/followedBy.d.ts +2 -0
  163. package/dist/sim/passes/followedBy.js +78 -0
  164. package/dist/sim/passes/followedBy.js.map +1 -0
  165. package/dist/sim/passes/forRegion.d.ts +9 -0
  166. package/dist/sim/passes/forRegion.js +230 -0
  167. package/dist/sim/passes/forRegion.js.map +1 -0
  168. package/dist/sim/passes/history.d.ts +12 -0
  169. package/dist/sim/passes/history.js +115 -0
  170. package/dist/sim/passes/history.js.map +1 -0
  171. package/dist/sim/passes/hostCode.d.ts +2 -0
  172. package/dist/sim/passes/hostCode.js +152 -0
  173. package/dist/sim/passes/hostCode.js.map +1 -0
  174. package/dist/sim/passes/index.d.ts +4 -0
  175. package/dist/sim/passes/index.js +84 -0
  176. package/dist/sim/passes/index.js.map +1 -0
  177. package/dist/sim/passes/inheritance.d.ts +8 -0
  178. package/dist/sim/passes/inheritance.js +233 -0
  179. package/dist/sim/passes/inheritance.js.map +1 -0
  180. package/dist/sim/passes/initOperator.d.ts +2 -0
  181. package/dist/sim/passes/initOperator.js +68 -0
  182. package/dist/sim/passes/initOperator.js.map +1 -0
  183. package/dist/sim/passes/initialization.d.ts +2 -0
  184. package/dist/sim/passes/initialization.js +69 -0
  185. package/dist/sim/passes/initialization.js.map +1 -0
  186. package/dist/sim/passes/ode.d.ts +2 -0
  187. package/dist/sim/passes/ode.js +14 -0
  188. package/dist/sim/passes/ode.js.map +1 -0
  189. package/dist/sim/passes/period.d.ts +2 -0
  190. package/dist/sim/passes/period.js +106 -0
  191. package/dist/sim/passes/period.js.map +1 -0
  192. package/dist/sim/passes/prTransitions.d.ts +2 -0
  193. package/dist/sim/passes/prTransitions.js +12 -0
  194. package/dist/sim/passes/prTransitions.js.map +1 -0
  195. package/dist/sim/passes/pre.d.ts +4 -0
  196. package/dist/sim/passes/pre.js +326 -0
  197. package/dist/sim/passes/pre.js.map +1 -0
  198. package/dist/sim/passes/quirks.d.ts +2 -0
  199. package/dist/sim/passes/quirks.js +169 -0
  200. package/dist/sim/passes/quirks.js.map +1 -0
  201. package/dist/sim/passes/reference.d.ts +60 -0
  202. package/dist/sim/passes/reference.js +386 -0
  203. package/dist/sim/passes/reference.js.map +1 -0
  204. package/dist/sim/passes/regionActions.d.ts +2 -0
  205. package/dist/sim/passes/regionActions.js +56 -0
  206. package/dist/sim/passes/regionActions.js.map +1 -0
  207. package/dist/sim/passes/sequentialFollowedBy.d.ts +2 -0
  208. package/dist/sim/passes/sequentialFollowedBy.js +72 -0
  209. package/dist/sim/passes/sequentialFollowedBy.js.map +1 -0
  210. package/dist/sim/passes/signal.d.ts +2 -0
  211. package/dist/sim/passes/signal.js +230 -0
  212. package/dist/sim/passes/signal.js.map +1 -0
  213. package/dist/sim/passes/static.d.ts +5 -0
  214. package/dist/sim/passes/static.js +67 -0
  215. package/dist/sim/passes/static.js.map +1 -0
  216. package/dist/sim/passes/suspend.d.ts +10 -0
  217. package/dist/sim/passes/suspend.js +71 -0
  218. package/dist/sim/passes/suspend.js.map +1 -0
  219. package/dist/sim/passes/termination.d.ts +9 -0
  220. package/dist/sim/passes/termination.js +69 -0
  221. package/dist/sim/passes/termination.js.map +1 -0
  222. package/dist/sim/passes/timedAutomata.d.ts +10 -0
  223. package/dist/sim/passes/timedAutomata.js +437 -0
  224. package/dist/sim/passes/timedAutomata.js.map +1 -0
  225. package/dist/sim/passes/unpackClasses.d.ts +2 -0
  226. package/dist/sim/passes/unpackClasses.js +54 -0
  227. package/dist/sim/passes/unpackClasses.js.map +1 -0
  228. package/dist/sim/passes/userSchedule.d.ts +2 -0
  229. package/dist/sim/passes/userSchedule.js +43 -0
  230. package/dist/sim/passes/userSchedule.js.map +1 -0
  231. package/dist/sim/passes/weakSuspend.d.ts +2 -0
  232. package/dist/sim/passes/weakSuspend.js +145 -0
  233. package/dist/sim/passes/weakSuspend.js.map +1 -0
  234. package/dist/sim/preemptionLog.d.ts +41 -0
  235. package/dist/sim/preemptionLog.js +133 -0
  236. package/dist/sim/preemptionLog.js.map +1 -0
  237. package/dist/sim/random.d.ts +12 -0
  238. package/dist/sim/random.js +40 -0
  239. package/dist/sim/random.js.map +1 -0
  240. package/dist/sim/reasons.d.ts +29 -0
  241. package/dist/sim/reasons.js +168 -0
  242. package/dist/sim/reasons.js.map +1 -0
  243. package/dist/sim/scg.d.ts +124 -0
  244. package/dist/sim/scg.js +723 -0
  245. package/dist/sim/scg.js.map +1 -0
  246. package/dist/sim/sfr.d.ts +4 -0
  247. package/dist/sim/sfr.js +264 -0
  248. package/dist/sim/sfr.js.map +1 -0
  249. package/dist/sim/stringConditions.d.ts +3 -0
  250. package/dist/sim/stringConditions.js +42 -0
  251. package/dist/sim/stringConditions.js.map +1 -0
  252. package/dist/sim/structure/copy.d.ts +4 -0
  253. package/dist/sim/structure/copy.js +140 -0
  254. package/dist/sim/structure/copy.js.map +1 -0
  255. package/dist/sim/structure/delays.d.ts +30 -0
  256. package/dist/sim/structure/delays.js +127 -0
  257. package/dist/sim/structure/delays.js.map +1 -0
  258. package/dist/sim/structure/instance.d.ts +15 -0
  259. package/dist/sim/structure/instance.js +69 -0
  260. package/dist/sim/structure/instance.js.map +1 -0
  261. package/dist/sim/structure/methods.d.ts +19 -0
  262. package/dist/sim/structure/methods.js +550 -0
  263. package/dist/sim/structure/methods.js.map +1 -0
  264. package/dist/sim/structure/rewrite.d.ts +20 -0
  265. package/dist/sim/structure/rewrite.js +113 -0
  266. package/dist/sim/structure/rewrite.js.map +1 -0
  267. package/dist/sim/structure/track.d.ts +22 -0
  268. package/dist/sim/structure/track.js +123 -0
  269. package/dist/sim/structure/track.js.map +1 -0
  270. package/dist/sim/tracker.d.ts +77 -0
  271. package/dist/sim/tracker.js +265 -0
  272. package/dist/sim/tracker.js.map +1 -0
  273. package/dist/sim/values.d.ts +31 -0
  274. package/dist/sim/values.js +103 -0
  275. package/dist/sim/values.js.map +1 -0
  276. package/dist/types.d.ts +670 -0
  277. package/dist/types.js +9 -0
  278. package/dist/types.js.map +1 -0
  279. package/package.json +58 -3
@@ -0,0 +1,644 @@
1
+ // Expression and effect evaluation with the C semantics of KIELER's netlist C simulation. Expressions are
2
+ // compiled once into closures over a Runtime (the variable store), so a tick is a plain array walk.
3
+ //
4
+ // Types follow C: bool variables are `char` and promote to int; comparisons and logical operators give int
5
+ // 0/1; int arithmetic wraps at 32 bits; any double operand makes the operation double; random() is a C
6
+ // `float` (single precision) and so is arithmetic between it and ints. Assignments convert to the target's
7
+ // type as C does, and KIELER's code generator casts a float right-hand side of an int target to int before a
8
+ // compound operator is applied (basics.md B-27).
9
+ import { newRandState, random } from "./random.js";
10
+ import { compileMethodCall } from "./structure/methods.js";
11
+ import { convert, doubleToInt, INT_MIN, isCString, literalString, printfValue, toChar, } from "./values.js";
12
+ /** A failure while running a tick (division by zero, index out of range, ...). */
13
+ export class SimError extends Error {
14
+ span;
15
+ code;
16
+ hint;
17
+ constructor(message, span, code = 'runtime-error', hint) {
18
+ super(message);
19
+ this.span = span;
20
+ this.code = code;
21
+ this.hint = hint;
22
+ }
23
+ }
24
+ /** An index outside its array, as the flat (row-major) element number C computes from it: C reads or writes whatever
25
+ * is stored there (memory.ts). */
26
+ export class Far {
27
+ flat;
28
+ constructor(flat) {
29
+ this.flat = flat;
30
+ }
31
+ }
32
+ const truthy = (v) => (typeof v === 'number' ? v !== 0 : v !== null);
33
+ const num = (v) => (typeof v === 'number' ? v : v === null ? 0 : isCString(v) ? v.id : 0);
34
+ /** KIELER's isFloatExpression (KExpressionsTypeExtensions.inferType == FLOAT): decides the (int) cast. */
35
+ export function isFloatExpression(e) {
36
+ const has = (x, t) => {
37
+ switch (x.k) {
38
+ case 'lit': return x.t === t;
39
+ case 'ref': return lowerType(x) === t;
40
+ case 'op': return x.args.some((a, i) => !(x.op === '?:' && i === 0) && has(a, t));
41
+ default: return false;
42
+ }
43
+ };
44
+ const all = (x, t) => {
45
+ switch (x.k) {
46
+ case 'lit': return x.t === t;
47
+ case 'ref': return lowerType(x) === t;
48
+ case 'op': return x.args.every((a, i) => (x.op === '?:' && i === 0) || all(a, t));
49
+ default: return false;
50
+ }
51
+ };
52
+ const arithmetic = !has(e, 'bool') && !has(e, 'string') && (has(e, 'int') || has(e, 'float'));
53
+ if (arithmetic && has(e, 'float'))
54
+ return true;
55
+ if (all(e, 'bool') || all(e, 'int'))
56
+ return false;
57
+ return all(e, 'float');
58
+ }
59
+ function lowerType(r) {
60
+ let x = r;
61
+ while (x.sub)
62
+ x = x.sub;
63
+ return x.v.type === 'pure' ? 'bool' : x.v.type;
64
+ }
65
+ /** The operands of a comparison as C compares them: converted to their common type (a C float rounds an int). */
66
+ function operands(a, b) {
67
+ if (binaryType(a.type, b.type) !== 'f32')
68
+ return [rt => num(a.fn(rt)), rt => num(b.fn(rt))];
69
+ return [rt => Math.fround(num(a.fn(rt))), rt => Math.fround(num(b.fn(rt)))];
70
+ }
71
+ function binaryType(a, b) {
72
+ if (a === 'float' || b === 'float')
73
+ return 'float';
74
+ if (a === 'f32' || b === 'f32')
75
+ return 'f32';
76
+ return 'int';
77
+ }
78
+ const isNumeric = (t) => t === 'int' || t === 'bool' || t === 'float' || t === 'f32';
79
+ export class ExprCompiler {
80
+ ctx;
81
+ constructor(ctx) {
82
+ this.ctx = ctx;
83
+ }
84
+ /**
85
+ * What a conditional without an else part (`c ? a`) gives when `c` is false. KIELER's C code generator writes it
86
+ * as `c ? a : __CONDSELF__` and replaces the placeholder by the target of the assignment it is in (`x = 1 +
87
+ * (c ? 2)` is `x = 1 + (c ? 2 : x)`, `x += c ? 1` doubles x) or, for a condition, by the guard variable that holds
88
+ * the condition's value (the value it had when last computed, exec.ts). Elsewhere gcc rejects the placeholder
89
+ * (codegen.ts); the innermost enclosing one counts.
90
+ */
91
+ selves = [];
92
+ /** Compiles with `self` as the value of a conditional without an else (see `selves`). */
93
+ withSelf(self, compile) {
94
+ this.selves.push(self);
95
+ try {
96
+ return compile();
97
+ }
98
+ finally {
99
+ this.selves.pop();
100
+ }
101
+ }
102
+ compile(e) {
103
+ switch (e.k) {
104
+ case 'lit': {
105
+ if (e.t === 'string') {
106
+ const s = literalString(e.v);
107
+ return { fn: () => s, type: 'string' };
108
+ }
109
+ const v = e.t === 'bool' ? (e.v ? 1 : 0) : e.v;
110
+ return { fn: () => v, type: e.t === 'float' ? 'float' : 'int' };
111
+ }
112
+ case 'null': return { fn: () => null, type: 'null' };
113
+ case 'ref': return this.read(e);
114
+ case 'op': return this.op(e.op, e.args, e.span);
115
+ case 'random': return { fn: rt => random(rt.rand), type: 'f32' };
116
+ case 'vector': {
117
+ const parts = e.values.map(v => this.compile(v));
118
+ return { fn: rt => parts.map(p => p.fn(rt)), type: parts[0]?.type ?? 'int' };
119
+ }
120
+ case 'call': {
121
+ // Methods (and code effects) run their SCL body (structure/methods.ts); host functions cannot run here.
122
+ const method = e.extern ? undefined : e.target?.v.method;
123
+ if (method)
124
+ return compileMethodCall(this, e, method);
125
+ const span = e.span;
126
+ return { fn: () => { throw new SimError(`The simulator cannot call ${e.extern ? 'the host function ' : ''}${e.name}() in the browser`, span, 'unsupported'); }, type: 'int' };
127
+ }
128
+ case 'host': {
129
+ const span = e.span;
130
+ return { fn: () => { throw new SimError('The simulator cannot run host code', span, 'unsupported'); }, type: 'int' };
131
+ }
132
+ case 'ignore': return { fn: () => 0, type: 'int' };
133
+ case 'unsupported': {
134
+ const span = e.span;
135
+ return { fn: () => { throw new SimError(`The simulator cannot evaluate ${e.what}`, span, 'unsupported'); }, type: 'int' };
136
+ }
137
+ }
138
+ }
139
+ /** Reads a variable or an array element. */
140
+ read(r) {
141
+ if (r.sub) {
142
+ const span = r.span;
143
+ return { fn: () => { throw new SimError(`The simulator cannot read members (${r.v.name}.${r.sub.v.name}) yet`, span, 'unsupported'); }, type: 'int' };
144
+ }
145
+ const info = this.ctx.info(r.v);
146
+ const slot = info.slot;
147
+ const type = info.type;
148
+ if (!r.idx.length)
149
+ return { fn: rt => rt.vals[slot], type };
150
+ const index = this.indexer(r, info);
151
+ const memory = this.ctx.memory;
152
+ const name = this.ctx.label(r.v);
153
+ const span = r.span;
154
+ return {
155
+ fn: rt => {
156
+ const path = index(rt);
157
+ if (path instanceof Far)
158
+ return memory.read(rt, r.v, path.flat, name, span);
159
+ let v = rt.vals[slot];
160
+ for (const i of path)
161
+ v = v[i];
162
+ return v;
163
+ },
164
+ type,
165
+ };
166
+ }
167
+ /**
168
+ * Evaluates the indices of a reference. C only computes an address from them: indices of a multi-dimensional
169
+ * array that are out of their range but land inside the array are that element (`m[0][3]` of `int m[2][3]` is
170
+ * `m[1][0]`); outside the array the access goes to whatever KIELER stores there (`Far`, memory.ts), when the guide
171
+ * knows it, and is a runtime error otherwise.
172
+ */
173
+ indexer(r, info) {
174
+ const parts = r.idx.map(i => this.compile(i));
175
+ const name = this.ctx.label(r.v);
176
+ const span = r.span;
177
+ const dims = info.dims;
178
+ const full = parts.length === dims.length;
179
+ const count = dims.reduce((a, b) => a * b, 1);
180
+ const memory = this.ctx.memory?.has(r.v) ? this.ctx.memory : undefined;
181
+ return rt => {
182
+ const out = new Array(parts.length);
183
+ let flat = 0;
184
+ let inside = true;
185
+ for (let k = 0; k < parts.length; k++) {
186
+ const p = parts[k];
187
+ let i = p.fn(rt);
188
+ if (p.type === 'float' || p.type === 'f32')
189
+ i = doubleToInt(i);
190
+ if (k >= dims.length || ((i < 0 || i >= dims[k]) && !full)) {
191
+ throw new SimError(`Index ${i} is outside the array ${name}${dims.length ? ` (size ${dims.join(' × ')})` : ''}`, span, 'index-out-of-range', 'KIELER does not check array indices: its C code would read or write unrelated memory here. Keep indices between 0 and the size minus 1.');
192
+ }
193
+ if (i < 0 || i >= dims[k])
194
+ inside = false;
195
+ flat = flat * dims[k] + i;
196
+ out[k] = i;
197
+ }
198
+ if (inside)
199
+ return out;
200
+ if (flat >= 0 && flat < count) {
201
+ for (let k = dims.length - 1, f = flat; k >= 0; k--) {
202
+ out[k] = f % dims[k];
203
+ f = Math.floor(f / dims[k]);
204
+ }
205
+ return out;
206
+ }
207
+ if (memory)
208
+ return new Far(flat);
209
+ throw new SimError(`Index ${flat} is outside the array ${name} (size ${dims.join(' × ')})`, span, 'index-out-of-range', `KIELER does not check array indices: its C code would read or write memory it uses for something else here, and the guide cannot tell what. Keep indices between 0 and ${count - 1}.`);
210
+ };
211
+ }
212
+ op(o, args, span) {
213
+ const c = args.map(a => this.compile(a));
214
+ // KIELER's operator expressions are n-ary (`a + b + c`, `a && b && c`): fold them from the left.
215
+ if (c.length > 2 && o !== '?:') {
216
+ let acc = this.binary(o, c[0], c[1], span, this.divisorName(args[1]), constantInt(args[1]));
217
+ for (let i = 2; i < c.length; i++)
218
+ acc = this.binary(o, acc, c[i], span, this.divisorName(args[i]), constantInt(args[i]));
219
+ return acc;
220
+ }
221
+ if (c.length === 2 && o !== '?:')
222
+ return this.binary(o, c[0], c[1], span, this.divisorName(args[1]), constantInt(args[1]));
223
+ return this.unary(o, c, span);
224
+ }
225
+ /** The divisor as the chart writes it (`b`, `b - 1`, `pre(n)`), for a division's hint; undefined when the guide
226
+ * cannot tell (a value KIELER computes in a variable of its own). */
227
+ divisorName(e) {
228
+ return e ? sourceText(e, v => this.ctx.label(v)) : undefined;
229
+ }
230
+ /** `known`: the right operand's value when gcc knows it at compile time (literals only), where gcc folds the
231
+ * operation instead of leaving it to the machine (probes tests/engine/kieler/codegen/probe-literal-*). */
232
+ binary(o, a, b, span, divisor, known) {
233
+ switch (o) {
234
+ case '&&': return { fn: rt => (truthy(a.fn(rt)) && truthy(b.fn(rt)) ? 1 : 0), type: 'int' };
235
+ case '||': return { fn: rt => (truthy(a.fn(rt)) || truthy(b.fn(rt)) ? 1 : 0), type: 'int' };
236
+ case '==':
237
+ case '!=': {
238
+ const eq = o === '==';
239
+ if (!isNumeric(a.type) || !isNumeric(b.type)) {
240
+ // Strings and NULL compare as pointers (B-26).
241
+ return { fn: rt => (sameValue(a.fn(rt), b.fn(rt)) === eq ? 1 : 0), type: 'int' };
242
+ }
243
+ const [x, y] = operands(a, b);
244
+ return { fn: rt => ((x(rt) === y(rt)) === eq ? 1 : 0), type: 'int' };
245
+ }
246
+ case '<':
247
+ case '<=':
248
+ case '>':
249
+ case '>=': {
250
+ const [x, y] = operands(a, b);
251
+ if (o === '<')
252
+ return { fn: rt => (x(rt) < y(rt) ? 1 : 0), type: 'int' };
253
+ if (o === '<=')
254
+ return { fn: rt => (x(rt) <= y(rt) ? 1 : 0), type: 'int' };
255
+ if (o === '>')
256
+ return { fn: rt => (x(rt) > y(rt) ? 1 : 0), type: 'int' };
257
+ return { fn: rt => (x(rt) >= y(rt) ? 1 : 0), type: 'int' };
258
+ }
259
+ case '&': return { fn: rt => num(a.fn(rt)) & num(b.fn(rt)), type: 'int' };
260
+ case '|': return { fn: rt => num(a.fn(rt)) | num(b.fn(rt)), type: 'int' };
261
+ case '^': return { fn: rt => num(a.fn(rt)) ^ num(b.fn(rt)), type: 'int' };
262
+ // A literal count of 32 or more: gcc folds the left shift to 0 (a count only known at run time, and every
263
+ // right shift, the machine masks to its low five bits, as JavaScript does).
264
+ case '<<': return known !== undefined && known >= 32 ? { fn: rt => (a.fn(rt), 0), type: 'int' } : { fn: rt => num(a.fn(rt)) << num(b.fn(rt)), type: 'int' };
265
+ case '>>': return { fn: rt => num(a.fn(rt)) >> num(b.fn(rt)), type: 'int' };
266
+ case '>>>': return { fn: rt => (num(a.fn(rt)) >>> num(b.fn(rt))) | 0, type: 'int' };
267
+ case '+':
268
+ case '-':
269
+ case '*':
270
+ case '/':
271
+ case '%': return this.arithmetic(o, a, b, span, divisor, known);
272
+ default: return this.unary(o, [a, b], span);
273
+ }
274
+ }
275
+ unary(o, c, span) {
276
+ const [a] = c;
277
+ switch (o) {
278
+ case '!': return { fn: rt => (truthy(a.fn(rt)) ? 0 : 1), type: 'int' };
279
+ case '~': return { fn: rt => ~num(a.fn(rt)), type: 'int' };
280
+ case 'neg': {
281
+ if (a.type === 'float')
282
+ return { fn: rt => -num(a.fn(rt)), type: 'float' };
283
+ if (a.type === 'f32')
284
+ return { fn: rt => -num(a.fn(rt)), type: 'f32' };
285
+ return { fn: rt => (-num(a.fn(rt))) | 0, type: 'int' };
286
+ }
287
+ case '?:': {
288
+ const [cond, then, other] = c;
289
+ if (!other) {
290
+ const self = this.selves[this.selves.length - 1];
291
+ if (self)
292
+ return { fn: rt => (truthy(cond.fn(rt)) ? then.fn(rt) : self.fn(rt)), type: then.type };
293
+ return { fn: rt => (truthy(cond.fn(rt)) ? then.fn(rt) : 0), type: then.type };
294
+ }
295
+ const type = then.type === 'string' || then.type === 'null' ? then.type : binaryType(then.type, other.type);
296
+ // C converts the branch taken to the conditional's type: an int branch beside a C float one is rounded.
297
+ if (type === 'f32')
298
+ return { fn: rt => Math.fround(num(truthy(cond.fn(rt)) ? then.fn(rt) : other.fn(rt))), type };
299
+ return { fn: rt => (truthy(cond.fn(rt)) ? then.fn(rt) : other.fn(rt)), type };
300
+ }
301
+ case 'pre':
302
+ case 'val': {
303
+ return { fn: () => { throw new SimError(`${o}() was not translated`, span, 'internal'); }, type: a?.type ?? 'int' };
304
+ }
305
+ default:
306
+ return { fn: () => { throw new SimError(`The simulator cannot evaluate the operator ${o}`, span, 'unsupported'); }, type: 'int' };
307
+ }
308
+ }
309
+ arithmetic(o, a, b, span, divisor, known) {
310
+ const type = binaryType(a.type, b.type);
311
+ if (type === 'float' || type === 'f32') {
312
+ const r = type === 'f32' ? Math.fround : (x) => x;
313
+ switch (o) {
314
+ case '+': return { fn: rt => r(num(a.fn(rt)) + num(b.fn(rt))), type };
315
+ case '-': return { fn: rt => r(num(a.fn(rt)) - num(b.fn(rt))), type };
316
+ case '*': return { fn: rt => r(num(a.fn(rt)) * num(b.fn(rt))), type };
317
+ case '/': return { fn: rt => r(num(a.fn(rt)) / num(b.fn(rt))), type };
318
+ default: return {
319
+ fn: () => { throw new SimError('The % operator needs whole numbers (C has no % for floats)', span, 'float-modulo'); }, type: 'int',
320
+ };
321
+ }
322
+ }
323
+ switch (o) {
324
+ case '+': return { fn: rt => (num(a.fn(rt)) + num(b.fn(rt))) | 0, type: 'int' };
325
+ case '-': return { fn: rt => (num(a.fn(rt)) - num(b.fn(rt))) | 0, type: 'int' };
326
+ case '*': return { fn: rt => Math.imul(num(a.fn(rt)), num(b.fn(rt))), type: 'int' };
327
+ // By a literal -1, gcc negates instead of dividing (no trap: -2147483648 stays -2147483648), and `% -1` is 0.
328
+ case '/': return known === -1 ? { fn: rt => -num(a.fn(rt)) | 0, type: 'int' } : {
329
+ fn: rt => {
330
+ const x = num(a.fn(rt)), y = num(b.fn(rt));
331
+ if (y === 0)
332
+ throw divisionByZero(span, divisor);
333
+ if (x === INT_MIN && y === -1)
334
+ throw overflowTrap(span);
335
+ return (x / y) | 0;
336
+ },
337
+ type: 'int',
338
+ };
339
+ default: return known === -1 ? { fn: rt => (a.fn(rt), 0), type: 'int' } : {
340
+ fn: rt => {
341
+ const x = num(a.fn(rt)), y = num(b.fn(rt));
342
+ if (y === 0)
343
+ throw divisionByZero(span, divisor);
344
+ if (x === INT_MIN && y === -1)
345
+ throw overflowTrap(span);
346
+ return (x % y) | 0;
347
+ },
348
+ type: 'int',
349
+ };
350
+ }
351
+ }
352
+ }
353
+ function sameValue(a, b) {
354
+ if (a === null || b === null)
355
+ return a === b || (a === 0 && b === null) || (a === null && b === 0);
356
+ if (isCString(a) && isCString(b))
357
+ return a.id === b.id;
358
+ return a === b;
359
+ }
360
+ function divisionByZero(span, divisor) {
361
+ const test = divisor ? (/^[\w.[\]]+$/.test(divisor) ? `${divisor} != 0` : `(${divisor}) != 0`) : undefined;
362
+ return new SimError(divisor ? `Division by zero: \`${divisor}\` is 0` : 'Division by zero', span, 'division-by-zero', `KIELER's C simulation crashes here: from this tick on it only shows the values of the tick before, and so the guide's simulation stops too. ${divisor ? `Test that \`${divisor}\` is not 0 first, for example with \`if ${test}\` on the transition or around the division.` : 'Make sure the divisor is never 0, for example with a test before the division.'}`);
363
+ }
364
+ /** Binding strengths for sourceText (KIELER's grammar, from loosest). */
365
+ const STRENGTH = {
366
+ '?:': 1, '||': 2, '&&': 3, '|': 4, '^': 5, '&': 6, '==': 7, '!=': 7, '<': 8, '<=': 8, '>': 8, '>=': 8,
367
+ '<<': 9, '>>': 9, '>>>': 9, '+': 10, '-': 10, '*': 11, '/': 11, '%': 11,
368
+ };
369
+ /**
370
+ * An expression of the working model as the chart writes it, with the reader's names (`label`): `b - 1`, `a[i]`,
371
+ * `pre(n)`, `val(S)`. Undefined when it has parts the reader did not write (KIELER's own variables, host code).
372
+ */
373
+ export function sourceText(e, label) {
374
+ const atom = (x) => {
375
+ const t = text(x);
376
+ return t === undefined ? undefined : x.k === 'op' && x.op !== 'pre' && x.op !== 'val' && x.op !== '!' && x.op !== '~' && x.op !== 'neg' ? `(${t})` : t;
377
+ };
378
+ const text = (x) => {
379
+ switch (x.k) {
380
+ case 'lit': return x.t === 'string' ? JSON.stringify(x.v) : x.t === 'float' && Number.isInteger(x.v) ? `${x.v}.0` : String(x.v);
381
+ case 'ref': {
382
+ let r = x;
383
+ const parts = [];
384
+ while (r) {
385
+ const v = r.v;
386
+ let name = label(v);
387
+ if (v.generated) {
388
+ if (v.role === 'pre' && v.of && !v.of.generated)
389
+ name = `pre(${label(v.of)})`;
390
+ else if ((v.role === 'value' || v.role === 'curval') && v.of)
391
+ name = `val(${label(v.of)})`;
392
+ else
393
+ return undefined;
394
+ }
395
+ const idx = r.idx.map(i => text(i));
396
+ if (idx.some(i => i === undefined))
397
+ return undefined;
398
+ parts.push(name + idx.map(i => `[${i}]`).join(''));
399
+ r = r.sub;
400
+ }
401
+ return parts.join('.');
402
+ }
403
+ case 'op': {
404
+ if (x.op === 'neg' || x.op === '!' || x.op === '~') {
405
+ const a = atom(x.args[0]);
406
+ return a === undefined ? undefined : `${x.op === 'neg' ? '-' : x.op}${a}`;
407
+ }
408
+ if (x.op === 'pre' || x.op === 'val') {
409
+ const a = text(x.args[0]);
410
+ return a === undefined ? undefined : `${x.op}(${a})`;
411
+ }
412
+ const strength = STRENGTH[x.op];
413
+ if (strength === undefined)
414
+ return undefined;
415
+ const parts = x.args.map(a => (a.k === 'op' && (STRENGTH[a.op] ?? 99) <= strength ? atom(a) : text(a)));
416
+ if (parts.some(p => p === undefined))
417
+ return undefined;
418
+ if (x.op === '?:')
419
+ return parts.length === 3 ? `${parts[0]} ? ${parts[1]} : ${parts[2]}` : `${parts[0]} ? ${parts[1]}`;
420
+ return parts.join(` ${x.op} `);
421
+ }
422
+ case 'call': {
423
+ const args = x.args.map(a => text(a));
424
+ return args.some(a => a === undefined) ? undefined : `${x.name}(${args.join(', ')})`;
425
+ }
426
+ default: return undefined;
427
+ }
428
+ };
429
+ return text(e);
430
+ }
431
+ function overflowTrap(span) {
432
+ return new SimError('-2147483648 / -1 does not fit in an int', span, 'division-overflow', 'In C this division overflows and the processor stops the program, as for a division by zero.');
433
+ }
434
+ export function compileEffect(f, ec) {
435
+ switch (f.k) {
436
+ case 'assign': return compileAssign(f, ec);
437
+ case 'print': {
438
+ const parts = f.args.map(a => ({ c: ec.compile(a), format: printFormat(a) }));
439
+ return (rt, printed) => {
440
+ printed.push(parts.map(p => printfValue(p.c.fn(rt), p.format)).join(' '));
441
+ return undefined;
442
+ };
443
+ }
444
+ case 'mark': return () => undefined;
445
+ case 'emit': {
446
+ const span = f.o.span;
447
+ return () => { throw new SimError(`Internal: emission of ${f.ref.v.name} was not translated`, span, 'internal'); };
448
+ }
449
+ case 'call': {
450
+ const c = ec.compile(f.call);
451
+ return rt => { c.fn(rt); return undefined; };
452
+ }
453
+ case 'randomize': return () => undefined;
454
+ case 'host': {
455
+ const span = f.o.span;
456
+ return () => { throw new SimError('The simulator cannot run host code', span, 'unsupported'); };
457
+ }
458
+ case 'unsupported': {
459
+ const span = f.o.span;
460
+ return () => { throw new SimError(`The simulator cannot run ${f.what} yet`, span, 'unsupported'); };
461
+ }
462
+ }
463
+ }
464
+ /**
465
+ * printf conversions (B-30): string literal %s, int literal %d, float literal %f, bool literal %x, a variable or a
466
+ * method call by its declared type; anything else (a calculation, a host type) %X, upper-case hex (KIELER's
467
+ * CCodeSerializeHRExtensions: `printf("%X", (d->n + 1))`).
468
+ */
469
+ function printFormat(e) {
470
+ if (e.k === 'lit')
471
+ return e.t === 'string' ? 'string' : e.t === 'float' ? 'float' : e.t === 'bool' ? 'bool' : 'int';
472
+ let r = e.k === 'ref' ? e : e.k === 'call' && !e.extern ? e.target : undefined;
473
+ if (!r)
474
+ return 'hex';
475
+ while (r.sub)
476
+ r = r.sub;
477
+ const type = r.v.method ? r.v.method.returnType : r.v.type;
478
+ switch (type) {
479
+ case 'bool':
480
+ case 'pure': return 'bool';
481
+ case 'int': return 'int';
482
+ case 'float': return 'float';
483
+ case 'string': return 'string';
484
+ default: return 'hex';
485
+ }
486
+ }
487
+ function compileAssign(f, ec) {
488
+ const target = f.ref;
489
+ if (target.sub) {
490
+ const span = f.o.span;
491
+ return () => { throw new SimError(`The simulator cannot assign members (${target.v.name}.${target.sub.v.name}) yet`, span, 'unsupported'); };
492
+ }
493
+ const info = ec.ctx.info(target.v);
494
+ const slot = info.slot;
495
+ const ttype = info.type;
496
+ const index = target.idx.length ? ec.indexer(target, info) : undefined;
497
+ const vref = target.v;
498
+ const elementCount = target.idx.length;
499
+ const name = ec.ctx.label(vref);
500
+ const memory = ec.ctx.memory;
501
+ const get = (rt, path) => {
502
+ if (!path)
503
+ return rt.vals[slot];
504
+ if (path instanceof Far)
505
+ return memory.read(rt, vref, path.flat, name, target.span);
506
+ let v = rt.vals[slot];
507
+ for (const i of path)
508
+ v = v[i];
509
+ return v;
510
+ };
511
+ const set = (rt, path, value) => {
512
+ if (!path) {
513
+ rt.vals[slot] = value;
514
+ return;
515
+ }
516
+ if (path instanceof Far) {
517
+ // C writes whatever is stored there (memory.ts): say what, in the log.
518
+ const { names } = memory.write(rt, vref, path.flat, value, name, target.span);
519
+ const shownValue = typeof value !== 'number' ? undefined : ttype === 'bool' ? String(value !== 0) : String(value);
520
+ const say = (c) => {
521
+ const what = names.length ? `${names.map(c).join(' and ')}, which ${names.length > 1 ? 'are' : 'is'} stored there` : `unused bytes next to ${c(name)}`;
522
+ return `${c(`${name}[${path.flat}]`)} is outside ${c(name)}, which has ${info.dims.reduce((a, b) => a * b, 1)} elements: KIELER's C code does not check, and writes ${shownValue === undefined ? 'the value' : c(shownValue)} into ${what}.`;
523
+ };
524
+ (rt.notes ??= []).push({ text: say(x => x), markdown: say(x => '`' + x + '`') });
525
+ return;
526
+ }
527
+ let v = rt.vals[slot];
528
+ for (let k = 0; k < path.length - 1; k++)
529
+ v = v[path[k]];
530
+ v[path[path.length - 1]] = value;
531
+ };
532
+ const shown = (path) => (path instanceof Far ? [path.flat] : path);
533
+ const isArrayTarget = info.dims.length > elementCount;
534
+ if (f.op === '++' || f.op === '--') {
535
+ const d = f.op === '++' ? 1 : -1;
536
+ return rt => {
537
+ const path = index?.(rt);
538
+ const old = get(rt, path);
539
+ const value = ttype === 'float' ? old + d : ttype === 'bool' ? toChar(old + d) : (old + d) | 0;
540
+ set(rt, path, value);
541
+ return { v: vref, value, index: shown(path) };
542
+ };
543
+ }
544
+ if (!f.expr)
545
+ return () => undefined;
546
+ const rhsExpr = f.expr;
547
+ if (isArrayTarget) {
548
+ const span = f.o.span;
549
+ return () => { throw new SimError(`${ec.ctx.label(vref)} is an array: assign its elements (KIELER's C code cannot assign whole arrays)`, span, 'array-assignment'); };
550
+ }
551
+ // A conditional without an else part keeps the target: KIELER generates `x = c ? e : x` (ExprCompiler.selves).
552
+ const rhs = ec.withSelf({ fn: rt => get(rt, index?.(rt)), type: ttype }, () => ec.compile(rhsExpr));
553
+ const rtype = rhs.type === 'f32' ? 'float' : rhs.type;
554
+ const castRhs = ttype === 'int' && isFloatExpression(rhsExpr);
555
+ const rhsFn = rhs.fn;
556
+ const rhsInt = (rt) => {
557
+ const v = rhsFn(rt);
558
+ return castRhs || rtype === 'float' ? doubleToInt(num(v)) : (num(v) | 0);
559
+ };
560
+ if (f.op === '=') {
561
+ return rt => {
562
+ const path = index?.(rt);
563
+ const v = rhsFn(rt);
564
+ const value = castRhs ? doubleToInt(num(v)) : convert(v, rtype, ttype);
565
+ set(rt, path, value);
566
+ return { v: vref, value, index: shown(path) };
567
+ };
568
+ }
569
+ // Without KIELER's cast C applies the usual arithmetic conversions: `x += random()` on an int adds in single
570
+ // precision and truncates the sum (KIELER's simulation: -5 + 0.84 gives -4), not the operand.
571
+ const single = rhs.type === 'f32' && !castRhs && ttype !== 'float';
572
+ const compute = compoundOperator(f.op, ttype, castRhs || rtype === 'int' || rtype === 'bool' ? 'int' : rtype, f.o.span, ec.divisorName(rhsExpr), single);
573
+ const read = (rt) => (castRhs || (ttype === 'int' && rtype !== 'float') ? rhsInt(rt) : num(rhsFn(rt)));
574
+ // KIELER writes `x min= e` as `x = (x < e) ? x : e`: e is evaluated for the comparison and, when it wins, once
575
+ // more for the value (a random() in it is drawn twice; probes tests/engine/kieler/codegen/probe-minmax-twice*).
576
+ const minmax = f.op === 'min=' || f.op === 'max=';
577
+ return rt => {
578
+ const path = index?.(rt);
579
+ const old = get(rt, path);
580
+ const operand = read(rt);
581
+ let value;
582
+ if (!minmax)
583
+ value = compute(old, operand);
584
+ else if (f.op === 'min=' ? old < operand : old > operand)
585
+ value = compute(old, old);
586
+ else {
587
+ const again = read(rt);
588
+ value = compute(again, again);
589
+ }
590
+ set(rt, path, value);
591
+ return { v: vref, value, index: shown(path) };
592
+ };
593
+ }
594
+ /** x op= e for numeric targets; `rtype` is the (possibly cast) operand's type. */
595
+ function compoundOperator(o, ttype, rtype, span, divisor, single = false) {
596
+ const floatOp = ttype === 'float' || rtype === 'float';
597
+ // `single`: a C float operand (random()) with an int or char target: the operation is in single precision.
598
+ const f = single ? Math.fround : (v) => v;
599
+ const store = (v) => (ttype === 'float' ? v : ttype === 'bool' ? toChar(f(v), floatOp) : floatOp ? doubleToInt(f(v)) : v | 0);
600
+ switch (o) {
601
+ case '+=': return (x, y) => store(floatOp ? f(x) + y : (x + y) | 0);
602
+ case '-=': return (x, y) => store(floatOp ? f(x) - y : (x - y) | 0);
603
+ case '*=': return (x, y) => store(floatOp ? f(x) * y : Math.imul(x, y));
604
+ case '/=': return (x, y) => {
605
+ if (floatOp)
606
+ return store(f(x) / y);
607
+ if (y === 0)
608
+ throw divisionByZero(span, divisor);
609
+ if (x === INT_MIN && y === -1)
610
+ throw overflowTrap(span);
611
+ return store((x / y) | 0);
612
+ };
613
+ case '%=': return (x, y) => {
614
+ if (y === 0)
615
+ throw divisionByZero(span, divisor);
616
+ if (x === INT_MIN && y === -1)
617
+ throw overflowTrap(span);
618
+ return store((x % y) | 0);
619
+ };
620
+ case '&=': return (x, y) => store(x & y);
621
+ case '|=': return (x, y) => store(x | y);
622
+ case '^=': return (x, y) => store(x ^ y);
623
+ case '<<=': return (x, y) => store(x << y);
624
+ case '>>=': return (x, y) => store(x >> y);
625
+ case '>>>=': return (x, y) => store((x >>> y) | 0);
626
+ case 'min=': return (x, y) => store(x < y ? x : y);
627
+ case 'max=': return (x, y) => store(x > y ? x : y);
628
+ default: return () => { throw new SimError(`Unknown assignment operator ${o}`, span, 'internal'); };
629
+ }
630
+ }
631
+ /** The value of an int expression of literals only (gcc folds it when it compiles KIELER's C), else undefined. */
632
+ function constantInt(e) {
633
+ const literal = (x) => (x.k === 'lit' ? x.t === 'int' : x.k === 'op' && x.op !== 'pre' && x.op !== 'val' && x.args.every(literal));
634
+ if (!e || !literal(e))
635
+ return undefined;
636
+ try {
637
+ const v = new ExprCompiler({ info: () => { throw new Error('no variables in a literal expression'); }, label: v => v.name }).compile(e).fn({ vals: [], rand: newRandState() });
638
+ return typeof v === 'number' && Number.isInteger(v) ? v : undefined;
639
+ }
640
+ catch {
641
+ return undefined;
642
+ }
643
+ }
644
+ //# sourceMappingURL=eval.js.map