@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,258 @@
1
+ // logic(): the scheduled SCG as netlist C code, node by node in the executor's order (sim/exec.ts walks the same
2
+ // table, ExecutableChart.nodes). Each node gets a guard, KIELER's `_g<n>`, that is true when control reaches it in
3
+ // this tick: the root entry's is `_GO` (the first tick), a depth's the register `_pg<n>` of its surface (the pause
4
+ // was reached in the tick before), a join's its synchronizer (KIELER's: some thread exits now and every other one
5
+ // has no pause reached now, or resumed now), every other node's the disjunction of what its predecessors pass on
6
+ // (a conditional passes `g && c` to its then branch and `g && !c` to its else branch). Effects run under their
7
+ // node's guard. Every condition is computed in every tick into a `char` guard `_cg<n>`, whether control reaches
8
+ // it or not (basics.md B-22), except a plain read of an input the chart never writes, which KIELER's copy
9
+ // propagation inlines. tick() saves the guards of the pauses into the registers.
10
+ import { droppedIndexRefs } from "../sim/guardIndices.js";
11
+ import { effect, expr } from "./expr.js";
12
+ const isAtomic = (s) => /^!?[A-Za-z_][\w>.\-[\]]*$/.test(s) || s === '0' || s === '1';
13
+ function and(t) {
14
+ if (t.some(x => x === '0'))
15
+ return '0';
16
+ const parts = t.filter(x => x !== '1');
17
+ if (!parts.length)
18
+ return '1';
19
+ return parts.map(p => (grouped(p) ? p : `(${p})`)).join(' && ');
20
+ }
21
+ /** An operand that needs no parentheses: a name, a negated name, `(...)` or `!(...)`. */
22
+ const grouped = (s) => isAtomic(s) || (s.startsWith('(') && balanced(s)) || (s.startsWith('!(') && balanced(s.slice(1)));
23
+ /** `(a) && (b)` is not one parenthesised group: true only when the outer parentheses enclose everything. */
24
+ function balanced(s) {
25
+ let depth = 0;
26
+ for (let i = 0; i < s.length; i++) {
27
+ if (s[i] === '(')
28
+ depth++;
29
+ else if (s[i] === ')') {
30
+ depth--;
31
+ if (depth === 0 && i < s.length - 1)
32
+ return false;
33
+ }
34
+ }
35
+ return true;
36
+ }
37
+ function or(terms) {
38
+ const parts = terms.map(and).filter(x => x !== '0');
39
+ if (parts.includes('1'))
40
+ return '1';
41
+ if (!parts.length)
42
+ return '0';
43
+ if (parts.length === 1)
44
+ return parts[0];
45
+ return parts.map(p => (grouped(p) ? p : `(${p})`)).join(' || ');
46
+ }
47
+ const not = (s) => (s === '0' ? '1' : s === '1' ? '0' : isAtomic(s) ? (s.startsWith('!') ? s.slice(1) : `!${s}`) : `!(${s})`);
48
+ export function generateLogic(input) {
49
+ const { exe, env } = input;
50
+ const nodes = exe.nodes;
51
+ const out = { lines: [], guards: [], conditions: [], registers: [], persistent: [] };
52
+ const state = nodes.map(() => ({ terms: [], after: [] }));
53
+ const D = env.structArg ? `${env.structArg}->` : '';
54
+ const GO = `${D}_GO`;
55
+ // A guard's value as a node reading it now sees it (exec.ts reads act[] whenever it gets there).
56
+ const valueOf = (i) => {
57
+ if (i < 0)
58
+ return '0';
59
+ const s = state[i];
60
+ if (s.value === undefined)
61
+ return or(s.terms);
62
+ return s.after.length ? or([[s.value], ...s.after]) : s.value;
63
+ };
64
+ const pass = (to, term) => {
65
+ if (to < 0)
66
+ return;
67
+ const s = state[to];
68
+ if (and(term) === '0')
69
+ return;
70
+ if (s.value === undefined)
71
+ s.terms.push(term);
72
+ else
73
+ s.after.push(term);
74
+ };
75
+ // The depths start with their surface's register (exec.ts: paused[] of the tick before).
76
+ const registerOf = new Map();
77
+ nodes.forEach((x, i) => {
78
+ if (x.src.kind !== 'surface')
79
+ return;
80
+ const reg = input.name(`_pg${x.src.id}`);
81
+ registerOf.set(i, reg);
82
+ out.registers.push({ register: reg });
83
+ });
84
+ nodes.forEach((x, i) => {
85
+ if (x.src.kind === 'depth' && x.pair >= 0 && registerOf.has(x.pair))
86
+ state[i].terms.push([`${D}${registerOf.get(x.pair)}`]);
87
+ });
88
+ if (exe.rootEntry >= 0)
89
+ state[exe.rootEntry].terms.push([GO]);
90
+ // Effects under one guard are grouped into one `if`.
91
+ let group;
92
+ let lastNote;
93
+ const flush = () => {
94
+ if (group && group.body.length) {
95
+ if (group.guard === '1')
96
+ out.lines.push(...group.body.map(l => ` ${l}`));
97
+ else
98
+ out.lines.push(` if (${group.guard}) {`, ...group.body.map(l => ` ${l}`), ' }');
99
+ }
100
+ group = undefined;
101
+ };
102
+ const emit = (line) => { flush(); out.lines.push(` ${line}`); };
103
+ const guardVar = (x, expression, own) => {
104
+ if (!own && isAtomic(expression))
105
+ return expression;
106
+ const name = input.name(`_g${x.src.id}`);
107
+ out.guards.push(name);
108
+ emit(`${D}${name} = ${expression};`);
109
+ return `${D}${name}`;
110
+ };
111
+ for (let i = 0; i < nodes.length; i++) {
112
+ const x = nodes[i];
113
+ const kind = x.src.kind;
114
+ const s = state[i];
115
+ if (kind === 'join') {
116
+ const fires = x.join ? joinGuard(x, valueOf) : '0';
117
+ s.value = fires === '0' ? '0' : guardVar(x, fires, true);
118
+ pass(x.next, [s.value]);
119
+ continue;
120
+ }
121
+ let condition;
122
+ if (kind === 'cond')
123
+ condition = conditionCode(x, input, out, emit);
124
+ let active = or(s.terms);
125
+ if ((kind === 'entry' || kind === 'depth') && x.preempt?.length && active !== '0') {
126
+ // Abort regions: an earlier thread of the preemptive fork finished in this tick.
127
+ active = and([active, not(or(x.preempt.map(e => [valueOf(e)])))]);
128
+ }
129
+ if (active === '0') {
130
+ s.value = '0';
131
+ continue;
132
+ }
133
+ switch (kind) {
134
+ case 'entry':
135
+ case 'depth':
136
+ s.value = guardVar(x, active, false);
137
+ pass(x.next, [s.value]);
138
+ break;
139
+ case 'exit':
140
+ s.value = guardVar(x, active, false);
141
+ if (x.rootExit)
142
+ emit(`${D}_TERM = ${s.value};`);
143
+ break;
144
+ case 'fork':
145
+ s.value = guardVar(x, active, false);
146
+ for (const e of x.entries)
147
+ pass(e, [s.value]);
148
+ break;
149
+ case 'surface':
150
+ s.value = guardVar(x, active, true);
151
+ out.registers.find(r => r.register === registerOf.get(i)).guard = s.value.slice(D.length);
152
+ break;
153
+ case 'cond': {
154
+ s.value = guardVar(x, active, false);
155
+ if (x.then === x.else)
156
+ pass(x.then, [s.value]);
157
+ else {
158
+ pass(x.then, [s.value, condition]);
159
+ pass(x.else, [s.value, not(condition)]);
160
+ }
161
+ break;
162
+ }
163
+ case 'assign': {
164
+ s.value = guardVar(x, active, false);
165
+ if (!input.skip.has(i)) {
166
+ const f = x.src.effect;
167
+ let body = f.k === 'mark' ? markCode(f.m, input, D) : effect(f, env);
168
+ const note = body.length && input.comment?.(x.src);
169
+ if (group?.guard !== s.value)
170
+ flush();
171
+ if (!group)
172
+ group = { guard: s.value, body: [] };
173
+ // A comment where the source transition or action changes.
174
+ if (note && note !== lastNote)
175
+ body = [note, ...body];
176
+ if (note)
177
+ lastNote = note;
178
+ group.body.push(...body);
179
+ }
180
+ pass(x.next, [s.value]);
181
+ break;
182
+ }
183
+ }
184
+ }
185
+ flush();
186
+ // A par-or join that fired ends its threads: their pauses do not resume (exec.ts kills them after the tick).
187
+ for (const x of nodes) {
188
+ if (x.src.kind !== 'join' || !x.join?.kill)
189
+ continue;
190
+ const j = state[nodes.indexOf(x)].value;
191
+ if (!j || j === '0')
192
+ continue;
193
+ let first = true;
194
+ for (const k of x.join.kill) {
195
+ const reg = out.registers.find(r => `${D}${r.guard}` === state[k].value);
196
+ if (!reg)
197
+ continue;
198
+ if (first && input.comment)
199
+ emit('/* a region of a state with abort regions finished: the other regions stop */');
200
+ first = false;
201
+ emit(`${D}${reg.guard} = ${D}${reg.guard} && ${not(j)};`);
202
+ }
203
+ }
204
+ flush();
205
+ return out;
206
+ }
207
+ /** KIELER's synchronizer: when the join fires (exec.ts joinFires). */
208
+ function joinGuard(x, valueOf) {
209
+ const j = x.join;
210
+ if (j.sync === 'instantaneous')
211
+ return valueOf(j.first);
212
+ const exits = j.threads.map(t => valueOf(t.exit));
213
+ const any = or(exits.map(e => [e]));
214
+ if (j.sync === 'any' || any === '0')
215
+ return any;
216
+ // Every counted thread exits now or is empty: no pause reached now (surface) or resumed now (depth).
217
+ const each = j.threads.map((t, k) => {
218
+ const busy = or(t.watch.map(w => [valueOf(w)]));
219
+ return busy === '0' ? '1' : or([[exits[k]], [not(busy)]]);
220
+ });
221
+ return and([any, ...each]);
222
+ }
223
+ /** `_cg<n> = <condition>;` (every tick), or the inlined read of an input; returns what the branches test. */
224
+ function conditionCode(x, input, out, emit) {
225
+ const { env } = input;
226
+ const n = x.src;
227
+ const dropped = droppedIndexRefs(n.expr);
228
+ env.dropped = dropped.size ? dropped : undefined;
229
+ try {
230
+ if (!x.char)
231
+ return expr(n.expr, env).text;
232
+ const D = env.structArg ? `${env.structArg}->` : '';
233
+ const name = input.name(`_cg${n.id}`);
234
+ out.conditions.push(name);
235
+ if (x.mem !== undefined)
236
+ out.persistent.push(name);
237
+ // A conditional without else keeps the guard's previous value (KIELER's `__CONDSELF__`, basics.md B-24).
238
+ const saved = env.self;
239
+ env.self = `${D}${name}`;
240
+ const text = expr(n.expr, env).text;
241
+ env.self = saved;
242
+ emit(`${D}${name} = ${text};`);
243
+ return `${D}${name}`;
244
+ }
245
+ finally {
246
+ env.dropped = undefined;
247
+ }
248
+ }
249
+ function markCode(m, input, D) {
250
+ if (!input.takenTransitions)
251
+ return [];
252
+ if (m.kind === 'take' && m.index >= 0)
253
+ return [`${D}_taken_transitions[${m.index}] += 1;`];
254
+ if (m.kind === 'ttsReset')
255
+ return Array.from({ length: input.exe.compiled.lowered.takenOrder.length }, (_, i) => `${D}_taken_transitions[${i}] = 0;`);
256
+ return [];
257
+ }
258
+ //# sourceMappingURL=logic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logic.js","sourceRoot":"","sources":["../../src/codegen/logic.ts"],"names":[],"mappings":"AAAA,iHAAiH;AACjH,mHAAmH;AACnH,mHAAmH;AACnH,kHAAkH;AAClH,iHAAiH;AACjH,+GAA+G;AAC/G,gHAAgH;AAChH,0GAA0G;AAC1G,iFAAiF;AAIjF,OAAO,EAAE,gBAAgB,EAAE,+BAA2B;AACtD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAgB,kBAAc;AAqCnD,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,2BAA2B,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,GAAG,CAAA;AAE7F,SAAS,GAAG,CAAC,CAAO;IAChB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,CAAC;AAED,SAAS,QAAQ,CAAC,CAA4D,EAAE,KAAiB,EAAE,CAAS;IACxG,IAAI,CAAC,KAAK,CAAC,gBAAgB;QAAE,OAAO,EAAE,CAAA;IACtC,IAAI,CAAC,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC;QAAE,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC,KAAK,SAAS,CAAC,CAAA;IAC1F,IAAI,CAAC,CAAC,IAAI,KAAK,UAAU;QAAE,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAAA;IACrJ,OAAO,EAAE,CAAA;AACb,CAAC","sourcesContent":["// logic(): the scheduled SCG as netlist C code, node by node in the executor's order (sim/exec.ts walks the same\n// table, ExecutableChart.nodes). Each node gets a guard, KIELER's `_g<n>`, that is true when control reaches it in\n// this tick: the root entry's is `_GO` (the first tick), a depth's the register `_pg<n>` of its surface (the pause\n// was reached in the tick before), a join's its synchronizer (KIELER's: some thread exits now and every other one\n// has no pause reached now, or resumed now), every other node's the disjunction of what its predecessors pass on\n// (a conditional passes `g && c` to its then branch and `g && !c` to its else branch). Effects run under their\n// node's guard. Every condition is computed in every tick into a `char` guard `_cg<n>`, whether control reaches\n// it or not (basics.md B-22), except a plain read of an input the chart never writes, which KIELER's copy\n// propagation inlines. tick() saves the guards of the pauses into the registers.\n\nimport type { ExecutableChart, XNode } from '../sim/exec'\nimport type { SNode } from '../sim/scg'\nimport { droppedIndexRefs } from '../sim/guardIndices'\nimport { effect, expr, type ExprEnv } from './expr'\n\n/** What the netlist needs to know besides the schedule. */\nexport interface LogicInput {\n exe: ExecutableChart\n env: ExprEnv\n /** The struct member of a guard name (unique among the chart's variables). */\n name(base: string): string\n /** Nodes whose effect has no code in logic(): it runs in reset() (KIELER's GO-init, reset.ts), or it initialises a\n * constant (KIELER's Const processor removes them). */\n skip: Set<number>\n takenTransitions: boolean\n /** A comment for the node's source transition or action (undefined: none). */\n comment?(n: SNode): string | undefined\n}\n\nexport interface LogicOutput {\n /** The body of logic(), one statement per line, indented by two spaces per level. */\n lines: string[]\n /** Guard members in KIELER's order: `_g`, then `_GO`, `_cg`, `_TERM`, `_pg`. */\n guards: string[]\n conditions: string[]\n /** Every pause's register and the guard tick() saves into it (none: the pause is never reached). */\n registers: { register: string; guard?: string }[]\n /** Guards that keep their value from tick to tick (conditions with a conditional without else). */\n persistent: string[]\n}\n\ntype Term = string[]\n\ninterface GuardState {\n terms: Term[]\n /** The node's own guard, once its turn came (an atomic C expression). */\n value?: string\n after: Term[]\n}\n\nconst isAtomic = (s: string) => /^!?[A-Za-z_][\\w>.\\-[\\]]*$/.test(s) || s === '0' || s === '1'\n\nfunction and(t: Term): string {\n if (t.some(x => x === '0')) return '0'\n const parts = t.filter(x => x !== '1')\n if (!parts.length) return '1'\n return parts.map(p => (grouped(p) ? p : `(${p})`)).join(' && ')\n}\n\n/** An operand that needs no parentheses: a name, a negated name, `(...)` or `!(...)`. */\nconst grouped = (s: string) => isAtomic(s) || (s.startsWith('(') && balanced(s)) || (s.startsWith('!(') && balanced(s.slice(1)))\n\n/** `(a) && (b)` is not one parenthesised group: true only when the outer parentheses enclose everything. */\nfunction balanced(s: string): boolean {\n let depth = 0\n for (let i = 0; i < s.length; i++) {\n if (s[i] === '(') depth++\n else if (s[i] === ')') { depth--; if (depth === 0 && i < s.length - 1) return false }\n }\n return true\n}\n\nfunction or(terms: Term[]): string {\n const parts = terms.map(and).filter(x => x !== '0')\n if (parts.includes('1')) return '1'\n if (!parts.length) return '0'\n if (parts.length === 1) return parts[0]\n return parts.map(p => (grouped(p) ? p : `(${p})`)).join(' || ')\n}\n\nconst not = (s: string) => (s === '0' ? '1' : s === '1' ? '0' : isAtomic(s) ? (s.startsWith('!') ? s.slice(1) : `!${s}`) : `!(${s})`)\n\nexport function generateLogic(input: LogicInput): LogicOutput {\n const { exe, env } = input\n const nodes = exe.nodes\n const out: LogicOutput = { lines: [], guards: [], conditions: [], registers: [], persistent: [] }\n const state: GuardState[] = nodes.map(() => ({ terms: [], after: [] }))\n const D = env.structArg ? `${env.structArg}->` : ''\n const GO = `${D}_GO`\n\n // A guard's value as a node reading it now sees it (exec.ts reads act[] whenever it gets there).\n const valueOf = (i: number): string => {\n if (i < 0) return '0'\n const s = state[i]\n if (s.value === undefined) return or(s.terms)\n return s.after.length ? or([[s.value], ...s.after]) : s.value\n }\n const pass = (to: number, term: Term) => {\n if (to < 0) return\n const s = state[to]\n if (and(term) === '0') return\n if (s.value === undefined) s.terms.push(term)\n else s.after.push(term)\n }\n // The depths start with their surface's register (exec.ts: paused[] of the tick before).\n const registerOf = new Map<number, string>()\n nodes.forEach((x, i) => {\n if (x.src.kind !== 'surface') return\n const reg = input.name(`_pg${x.src.id}`)\n registerOf.set(i, reg)\n out.registers.push({ register: reg })\n })\n nodes.forEach((x, i) => {\n if (x.src.kind === 'depth' && x.pair >= 0 && registerOf.has(x.pair)) state[i].terms.push([`${D}${registerOf.get(x.pair)}`])\n })\n if (exe.rootEntry >= 0) state[exe.rootEntry].terms.push([GO])\n\n // Effects under one guard are grouped into one `if`.\n let group: { guard: string; body: string[] } | undefined\n let lastNote: string | undefined\n const flush = () => {\n if (group && group.body.length) {\n if (group.guard === '1') out.lines.push(...group.body.map(l => ` ${l}`))\n else out.lines.push(` if (${group.guard}) {`, ...group.body.map(l => ` ${l}`), ' }')\n }\n group = undefined\n }\n const emit = (line: string) => { flush(); out.lines.push(` ${line}`) }\n const guardVar = (x: XNode, expression: string, own: boolean): string => {\n if (!own && isAtomic(expression)) return expression\n const name = input.name(`_g${x.src.id}`)\n out.guards.push(name)\n emit(`${D}${name} = ${expression};`)\n return `${D}${name}`\n }\n\n for (let i = 0; i < nodes.length; i++) {\n const x = nodes[i]\n const kind = x.src.kind\n const s = state[i]\n if (kind === 'join') {\n const fires = x.join ? joinGuard(x, valueOf) : '0'\n s.value = fires === '0' ? '0' : guardVar(x, fires, true)\n pass(x.next, [s.value])\n continue\n }\n let condition: string | undefined\n if (kind === 'cond') condition = conditionCode(x, input, out, emit)\n let active = or(s.terms)\n if ((kind === 'entry' || kind === 'depth') && x.preempt?.length && active !== '0') {\n // Abort regions: an earlier thread of the preemptive fork finished in this tick.\n active = and([active, not(or(x.preempt.map(e => [valueOf(e)])))])\n }\n if (active === '0') {\n s.value = '0'\n continue\n }\n switch (kind) {\n case 'entry': case 'depth':\n s.value = guardVar(x, active, false)\n pass(x.next, [s.value])\n break\n case 'exit':\n s.value = guardVar(x, active, false)\n if (x.rootExit) emit(`${D}_TERM = ${s.value};`)\n break\n case 'fork':\n s.value = guardVar(x, active, false)\n for (const e of x.entries!) pass(e, [s.value])\n break\n case 'surface':\n s.value = guardVar(x, active, true)\n out.registers.find(r => r.register === registerOf.get(i))!.guard = s.value.slice(D.length)\n break\n case 'cond': {\n s.value = guardVar(x, active, false)\n if (x.then === x.else) pass(x.then, [s.value])\n else {\n pass(x.then, [s.value, condition!])\n pass(x.else, [s.value, not(condition!)])\n }\n break\n }\n case 'assign': {\n s.value = guardVar(x, active, false)\n if (!input.skip.has(i)) {\n const f = x.src.effect!\n let body = f.k === 'mark' ? markCode(f.m, input, D) : effect(f, env)\n const note = body.length && input.comment?.(x.src)\n if (group?.guard !== s.value) flush()\n if (!group) group = { guard: s.value, body: [] }\n // A comment where the source transition or action changes.\n if (note && note !== lastNote) body = [note, ...body]\n if (note) lastNote = note\n group.body.push(...body)\n }\n pass(x.next, [s.value])\n break\n }\n }\n }\n flush()\n // A par-or join that fired ends its threads: their pauses do not resume (exec.ts kills them after the tick).\n for (const x of nodes) {\n if (x.src.kind !== 'join' || !x.join?.kill) continue\n const j = state[nodes.indexOf(x)].value\n if (!j || j === '0') continue\n let first = true\n for (const k of x.join.kill) {\n const reg = out.registers.find(r => `${D}${r.guard}` === state[k].value)\n if (!reg) continue\n if (first && input.comment) emit('/* a region of a state with abort regions finished: the other regions stop */')\n first = false\n emit(`${D}${reg.guard} = ${D}${reg.guard} && ${not(j)};`)\n }\n }\n flush()\n return out\n}\n\n/** KIELER's synchronizer: when the join fires (exec.ts joinFires). */\nfunction joinGuard(x: XNode, valueOf: (i: number) => string): string {\n const j = x.join!\n if (j.sync === 'instantaneous') return valueOf(j.first)\n const exits = j.threads.map(t => valueOf(t.exit))\n const any = or(exits.map(e => [e]))\n if (j.sync === 'any' || any === '0') return any\n // Every counted thread exits now or is empty: no pause reached now (surface) or resumed now (depth).\n const each = j.threads.map((t, k) => {\n const busy = or(t.watch.map(w => [valueOf(w)]))\n return busy === '0' ? '1' : or([[exits[k]], [not(busy)]])\n })\n return and([any, ...each])\n}\n\n/** `_cg<n> = <condition>;` (every tick), or the inlined read of an input; returns what the branches test. */\nfunction conditionCode(x: XNode, input: LogicInput, out: LogicOutput, emit: (line: string) => void): string {\n const { env } = input\n const n = x.src\n const dropped = droppedIndexRefs(n.expr!)\n env.dropped = dropped.size ? dropped : undefined\n try {\n if (!x.char) return expr(n.expr!, env).text\n const D = env.structArg ? `${env.structArg}->` : ''\n const name = input.name(`_cg${n.id}`)\n out.conditions.push(name)\n if (x.mem !== undefined) out.persistent.push(name)\n // A conditional without else keeps the guard's previous value (KIELER's `__CONDSELF__`, basics.md B-24).\n const saved = env.self\n env.self = `${D}${name}`\n const text = expr(n.expr!, env).text\n env.self = saved\n emit(`${D}${name} = ${text};`)\n return `${D}${name}`\n } finally {\n env.dropped = undefined\n }\n}\n\nfunction markCode(m: Extract<NonNullable<SNode['effect']>, { k: 'mark' }>['m'], input: LogicInput, D: string): string[] {\n if (!input.takenTransitions) return []\n if (m.kind === 'take' && m.index >= 0) return [`${D}_taken_transitions[${m.index}] += 1;`]\n if (m.kind === 'ttsReset') return Array.from({ length: input.exe.compiled.lowered.takenOrder.length }, (_, i) => `${D}_taken_transitions[${i}] = 0;`)\n return []\n}\n"]}
@@ -0,0 +1,17 @@
1
+ import { type CExpr, type CStmt, type CVar } from "../sim/model.js";
2
+ import { type ExprEnv } from "./expr.js";
3
+ export interface MethodsInput {
4
+ /** The environment for the chart's variables (`d->x`). */
5
+ env: ExprEnv;
6
+ /** The C name of each method. */
7
+ name(v: CVar): string;
8
+ structName: string;
9
+ }
10
+ /** Every method the code calls, directly or through other methods (callees first is not needed: prototypes come first). */
11
+ export declare function calledMethods(exprs: CExpr[]): CVar[];
12
+ /** The expressions of a body (conditions, effects' expressions). */
13
+ export declare function bodyExprs(list: CStmt[]): CExpr[];
14
+ /** The function's signature (`int Chart_m(TickData* d, int x)`). */
15
+ export declare function signature(v: CVar, input: MethodsInput): string;
16
+ /** The function: its locals, then its body. */
17
+ export declare function functionCode(v: CVar, input: MethodsInput): string[];
@@ -0,0 +1,242 @@
1
+ // Methods KIELER does not inline (a loop, a call, 30 nodes or more: sim/structure/methods.ts) stay C functions, as in
2
+ // KIELER's generateMethods: `<type> <Chart>_<method>(TickData* d, <parameters>)` with the method's locals declared at
3
+ // the top. The body is structured code. A `goto` jumps forward to its label; KIELER's structured code has no jumps
4
+ // back, so a `goto` to a label before it (or to one that ends a block) ends the method (as the simulator runs it).
5
+ import { walkExpr } from "../sim/model.js";
6
+ import { effect, expr } from "./expr.js";
7
+ import { ctypeOf, dimsOf } from "./layout.js";
8
+ /** Every method the code calls, directly or through other methods (callees first is not needed: prototypes come first). */
9
+ export function calledMethods(exprs) {
10
+ const out = [];
11
+ const seen = new Set();
12
+ const work = [...exprs];
13
+ const visit = (e) => {
14
+ if (e.k !== 'call' || e.extern)
15
+ return;
16
+ const v = e.target?.v;
17
+ if (!v?.method || v.extern || seen.has(v))
18
+ return;
19
+ seen.add(v);
20
+ out.push(v);
21
+ for (const x of bodyExprs(v.method.body))
22
+ work.push(x);
23
+ };
24
+ while (work.length)
25
+ walkExpr(work.pop(), visit);
26
+ return out;
27
+ }
28
+ /** The expressions of a body (conditions, effects' expressions). */
29
+ export function bodyExprs(list) {
30
+ const out = [];
31
+ const visit = (l) => {
32
+ for (const s of l) {
33
+ switch (s.k) {
34
+ case 'do': {
35
+ const f = s.f;
36
+ if (f.k === 'assign') {
37
+ out.push(f.ref);
38
+ if (f.expr)
39
+ out.push(f.expr);
40
+ }
41
+ else if (f.k === 'print')
42
+ out.push(...f.args);
43
+ else if (f.k === 'call')
44
+ out.push(f.call);
45
+ break;
46
+ }
47
+ case 'if':
48
+ out.push(s.cond);
49
+ visit(s.then);
50
+ if (s.else)
51
+ visit(s.else);
52
+ break;
53
+ case 'while':
54
+ out.push(s.cond);
55
+ visit(s.body);
56
+ break;
57
+ case 'for':
58
+ visit(s.init);
59
+ if (s.cond)
60
+ out.push(s.cond);
61
+ visit(s.update);
62
+ visit(s.body);
63
+ break;
64
+ case 'return':
65
+ if (s.expr)
66
+ out.push(s.expr);
67
+ break;
68
+ case 'expr':
69
+ out.push(s.e);
70
+ break;
71
+ default: break;
72
+ }
73
+ }
74
+ };
75
+ visit(list);
76
+ return out;
77
+ }
78
+ const returnType = (m) => (m.returnType === 'void' ? 'void' : ctypeOf({ type: m.returnType, hostType: undefined }));
79
+ /** The function's signature (`int Chart_m(TickData* d, int x)`). */
80
+ export function signature(v, input) {
81
+ const m = v.method;
82
+ const params = m.params.map(p => `${ctypeOf(p)} ${localName(p)}`);
83
+ return `${returnType(m)} ${input.name(v)}(${[`${input.structName}* d`, ...params].join(', ')})`;
84
+ }
85
+ /** A parameter's or local's C name (`d` is the struct: a for-loop counter `d`, which KIELER declares in the loop's
86
+ * header, becomes `d_`). */
87
+ const localName = (v) => {
88
+ const name = v.name.replace(/[^A-Za-z0-9_]/g, '_');
89
+ return name === 'd' ? 'd_' : name;
90
+ };
91
+ /** The function: its locals, then its body. */
92
+ export function functionCode(v, input) {
93
+ const m = v.method;
94
+ // Every local is declared at the top of the function, so two of one name in different blocks (two loops' `i`) get
95
+ // a name each: the first keeps it, the next is `i_2` (probes tests/engine/kieler/codegen/probe-method-locals-*).
96
+ const locals = new Map();
97
+ const used = new Set();
98
+ const unique = (x) => {
99
+ const base = localName(x);
100
+ let name = base;
101
+ for (let k = 2; used.has(name); k++)
102
+ name = `${base}_${k}`;
103
+ used.add(name);
104
+ return name;
105
+ };
106
+ for (const p of m.params)
107
+ locals.set(p, unique(p));
108
+ for (const l of m.locals)
109
+ locals.set(l, unique(l));
110
+ const env = {
111
+ ...input.env,
112
+ access: x => locals.get(x) ?? input.env.access(x),
113
+ self: undefined,
114
+ dropped: undefined,
115
+ };
116
+ const lines = [`${signature(v, input)} {`];
117
+ for (const l of m.locals)
118
+ lines.push(` ${ctypeOf(l)} ${locals.get(l)}${dimsOf(l).map(n => `[${n}]`).join('')};`);
119
+ const typed = m.returnType !== 'void';
120
+ const end = typed ? 'return 0;' : 'return;';
121
+ block(m.body, 1, [], lines, env, end, forwardTargets(m.body));
122
+ lines.push('}');
123
+ // Keep what the expressions asked for (includes, NULL).
124
+ input.env.usesNull ||= env.usesNull;
125
+ return lines;
126
+ }
127
+ /**
128
+ * A statement list. `outer`: the enclosing lists with the position of the statement that contains this list, to find
129
+ * where a `goto` lands (sim/structure/methods.ts compileBlock: the innermost list with the label decides).
130
+ */
131
+ function block(list, depth, outer, lines, env, end, targets) {
132
+ const pad = ' '.repeat(depth);
133
+ list.forEach((s, at) => {
134
+ const here = [...outer, { list, at }];
135
+ switch (s.k) {
136
+ case 'do':
137
+ for (const l of effect(s.f, env))
138
+ lines.push(pad + l);
139
+ break;
140
+ case 'expr':
141
+ lines.push(`${pad}${expr(s.e, env).text};`);
142
+ break;
143
+ case 'return':
144
+ lines.push(s.expr ? `${pad}return ${expr(s.expr, env).text};` : `${pad}return;`);
145
+ break;
146
+ case 'if':
147
+ lines.push(`${pad}if (${expr(s.cond, env).text}) {`);
148
+ block(s.then, depth + 1, here, lines, env, end, targets);
149
+ if (s.else?.length) {
150
+ lines.push(`${pad}} else {`);
151
+ block(s.else, depth + 1, here, lines, env, end, targets);
152
+ }
153
+ lines.push(`${pad}}`);
154
+ break;
155
+ case 'while':
156
+ lines.push(`${pad}while (${expr(s.cond, env).text}) {`);
157
+ block(s.body, depth + 1, here, lines, env, end, targets);
158
+ lines.push(`${pad}}`);
159
+ break;
160
+ case 'for': {
161
+ const cond = s.cond ? expr(s.cond, env).text : '';
162
+ const init = header(s.init, env);
163
+ const update = header(s.update, env);
164
+ if (init !== undefined && update !== undefined) {
165
+ // KIELER's `for (i = 1; i < n; i++)`.
166
+ lines.push(`${pad}for (${init}; ${cond}; ${update}) {`);
167
+ block(s.body, depth + 1, here, lines, env, end, targets);
168
+ }
169
+ else {
170
+ // init; while (cond) { body; update } (SCL has no `continue`).
171
+ block(s.init, depth, outer, lines, env, end, targets);
172
+ lines.push(`${pad}while (${cond || '1'}) {`);
173
+ block(s.body, depth + 1, here, lines, env, end, targets);
174
+ block(s.update, depth + 1, here, lines, env, end, targets);
175
+ }
176
+ lines.push(`${pad}}`);
177
+ break;
178
+ }
179
+ case 'label':
180
+ // Only the labels C jumps to (the others have no code in KIELER's structured C either).
181
+ if (targets.has(s.name))
182
+ lines.push(`${pad.slice(2)}${s.name}: ;`);
183
+ break;
184
+ case 'goto':
185
+ lines.push(pad + (jumpsForward(s.target, here) ? `goto ${s.target};` : end));
186
+ break;
187
+ case 'unsupported':
188
+ env.fail(s.what);
189
+ break;
190
+ }
191
+ });
192
+ }
193
+ /** A for loop's init or update as one C expression (one simple effect), or undefined. */
194
+ function header(list, env) {
195
+ if (list.length > 1)
196
+ return undefined;
197
+ if (!list.length)
198
+ return '';
199
+ const s = list[0];
200
+ if (s.k !== 'do' || (s.f.k !== 'assign' && s.f.k !== 'call'))
201
+ return undefined;
202
+ const code = effect(s.f, env);
203
+ return code.length === 1 && !code[0].includes(' ? ') ? code[0].replace(/;$/, '') : undefined;
204
+ }
205
+ /** The labels of a body that some `goto` jumps forward to. */
206
+ function forwardTargets(body) {
207
+ const out = new Set();
208
+ const walk = (list, outer) => {
209
+ list.forEach((s, at) => {
210
+ const here = [...outer, { list, at }];
211
+ if (s.k === 'goto' && jumpsForward(s.target, here))
212
+ out.add(s.target);
213
+ else if (s.k === 'if') {
214
+ walk(s.then, here);
215
+ if (s.else)
216
+ walk(s.else, here);
217
+ }
218
+ else if (s.k === 'while')
219
+ walk(s.body, here);
220
+ else if (s.k === 'for') {
221
+ walk(s.init, outer);
222
+ walk(s.body, here);
223
+ walk(s.update, here);
224
+ }
225
+ });
226
+ };
227
+ walk(body, []);
228
+ return out;
229
+ }
230
+ /** A `goto` that C can take: to a label after it in the innermost list that has the label, not at that list's end. */
231
+ function jumpsForward(label, stack) {
232
+ for (let k = stack.length - 1; k >= 0; k--) {
233
+ const { list, at } = stack[k];
234
+ const to = list.findIndex(s => s.k === 'label' && s.name === label);
235
+ if (to < 0)
236
+ continue;
237
+ const trailing = list.slice(to + 1).every(s => s.k === 'label');
238
+ return to > at && !trailing;
239
+ }
240
+ return false;
241
+ }
242
+ //# sourceMappingURL=methods.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"methods.js","sourceRoot":"","sources":["../../src/codegen/methods.ts"],"names":[],"mappings":"AAAA,sHAAsH;AACtH,sHAAsH;AACtH,mHAAmH;AACnH,mHAAmH;AAEnH,OAAO,EAAE,QAAQ,EAAmD,wBAAoB;AACxF,OAAO,EAAE,MAAM,EAAE,IAAI,EAAgB,kBAAc;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,oBAAgB;AAU1C,2HAA2H;AAC3H,MAAM,UAAU,aAAa,CAAC,KAAc;IACxC,MAAM,GAAG,GAAW,EAAE,CAAA;IACtB,MAAM,IAAI,GAAG,IAAI,GAAG,EAAQ,CAAA;IAC5B,MAAM,IAAI,GAAY,CAAC,GAAG,KAAK,CAAC,CAAA;IAChC,MAAM,KAAK,GAAG,CAAC,CAAQ,EAAE,EAAE;QACvB,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM,IAAI,CAAC,CAAC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EAAE,CAAC;QACzC,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;QAC7B,MAAM,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,IAAI,CAAC,CAAC,IAAI,KAAK,KAAK,CAAC,CAAA;QACnE,IAAI,EAAE,GAAG,CAAC;YAAE,SAAQ;QACpB,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAA;QAC/D,OAAO,EAAE,GAAG,EAAE,IAAI,CAAC,QAAQ,CAAA;IAC/B,CAAC;IACD,OAAO,KAAK,CAAA;AAChB,CAAC","sourcesContent":["// Methods KIELER does not inline (a loop, a call, 30 nodes or more: sim/structure/methods.ts) stay C functions, as in\n// KIELER's generateMethods: `<type> <Chart>_<method>(TickData* d, <parameters>)` with the method's locals declared at\n// the top. The body is structured code. A `goto` jumps forward to its label; KIELER's structured code has no jumps\n// back, so a `goto` to a label before it (or to one that ends a block) ends the method (as the simulator runs it).\n\nimport { walkExpr, type CExpr, type CMethod, type CStmt, type CVar } from '../sim/model'\nimport { effect, expr, type ExprEnv } from './expr'\nimport { ctypeOf, dimsOf } from './layout'\n\nexport interface MethodsInput {\n /** The environment for the chart's variables (`d->x`). */\n env: ExprEnv\n /** The C name of each method. */\n name(v: CVar): string\n structName: string\n}\n\n/** Every method the code calls, directly or through other methods (callees first is not needed: prototypes come first). */\nexport function calledMethods(exprs: CExpr[]): CVar[] {\n const out: CVar[] = []\n const seen = new Set<CVar>()\n const work: CExpr[] = [...exprs]\n const visit = (e: CExpr) => {\n if (e.k !== 'call' || e.extern) return\n const v = e.target?.v\n if (!v?.method || v.extern || seen.has(v)) return\n seen.add(v)\n out.push(v)\n for (const x of bodyExprs(v.method.body)) work.push(x)\n }\n while (work.length) walkExpr(work.pop(), visit)\n return out\n}\n\n/** The expressions of a body (conditions, effects' expressions). */\nexport function bodyExprs(list: CStmt[]): CExpr[] {\n const out: CExpr[] = []\n const visit = (l: CStmt[]) => {\n for (const s of l) {\n switch (s.k) {\n case 'do': {\n const f = s.f\n if (f.k === 'assign') { out.push(f.ref); if (f.expr) out.push(f.expr) }\n else if (f.k === 'print') out.push(...f.args)\n else if (f.k === 'call') out.push(f.call)\n break\n }\n case 'if': out.push(s.cond); visit(s.then); if (s.else) visit(s.else); break\n case 'while': out.push(s.cond); visit(s.body); break\n case 'for': visit(s.init); if (s.cond) out.push(s.cond); visit(s.update); visit(s.body); break\n case 'return': if (s.expr) out.push(s.expr); break\n case 'expr': out.push(s.e); break\n default: break\n }\n }\n }\n visit(list)\n return out\n}\n\nconst returnType = (m: CMethod) => (m.returnType === 'void' ? 'void' : ctypeOf({ type: m.returnType, hostType: undefined } as CVar))\n\n/** The function's signature (`int Chart_m(TickData* d, int x)`). */\nexport function signature(v: CVar, input: MethodsInput): string {\n const m = v.method!\n const params = m.params.map(p => `${ctypeOf(p)} ${localName(p)}`)\n return `${returnType(m)} ${input.name(v)}(${[`${input.structName}* d`, ...params].join(', ')})`\n}\n\n/** A parameter's or local's C name (`d` is the struct: a for-loop counter `d`, which KIELER declares in the loop's\n * header, becomes `d_`). */\nconst localName = (v: CVar) => {\n const name = v.name.replace(/[^A-Za-z0-9_]/g, '_')\n return name === 'd' ? 'd_' : name\n}\n\n/** The function: its locals, then its body. */\nexport function functionCode(v: CVar, input: MethodsInput): string[] {\n const m = v.method!\n // Every local is declared at the top of the function, so two of one name in different blocks (two loops' `i`) get\n // a name each: the first keeps it, the next is `i_2` (probes tests/engine/kieler/codegen/probe-method-locals-*).\n const locals = new Map<CVar, string>()\n const used = new Set<string>()\n const unique = (x: CVar) => {\n const base = localName(x)\n let name = base\n for (let k = 2; used.has(name); k++) name = `${base}_${k}`\n used.add(name)\n return name\n }\n for (const p of m.params) locals.set(p, unique(p))\n for (const l of m.locals) locals.set(l, unique(l))\n const env: ExprEnv = {\n ...input.env,\n access: x => locals.get(x) ?? input.env.access(x),\n self: undefined,\n dropped: undefined,\n }\n const lines = [`${signature(v, input)} {`]\n for (const l of m.locals) lines.push(` ${ctypeOf(l)} ${locals.get(l)}${dimsOf(l).map(n => `[${n}]`).join('')};`)\n const typed = m.returnType !== 'void'\n const end = typed ? 'return 0;' : 'return;'\n block(m.body, 1, [], lines, env, end, forwardTargets(m.body))\n lines.push('}')\n // Keep what the expressions asked for (includes, NULL).\n input.env.usesNull ||= env.usesNull\n return lines\n}\n\n/**\n * A statement list. `outer`: the enclosing lists with the position of the statement that contains this list, to find\n * where a `goto` lands (sim/structure/methods.ts compileBlock: the innermost list with the label decides).\n */\nfunction block(list: CStmt[], depth: number, outer: { list: CStmt[]; at: number }[], lines: string[], env: ExprEnv, end: string, targets: Set<string>) {\n const pad = ' '.repeat(depth)\n list.forEach((s, at) => {\n const here = [...outer, { list, at }]\n switch (s.k) {\n case 'do':\n for (const l of effect(s.f, env)) lines.push(pad + l)\n break\n case 'expr':\n lines.push(`${pad}${expr(s.e, env).text};`)\n break\n case 'return':\n lines.push(s.expr ? `${pad}return ${expr(s.expr, env).text};` : `${pad}return;`)\n break\n case 'if':\n lines.push(`${pad}if (${expr(s.cond, env).text}) {`)\n block(s.then, depth + 1, here, lines, env, end, targets)\n if (s.else?.length) {\n lines.push(`${pad}} else {`)\n block(s.else, depth + 1, here, lines, env, end, targets)\n }\n lines.push(`${pad}}`)\n break\n case 'while':\n lines.push(`${pad}while (${expr(s.cond, env).text}) {`)\n block(s.body, depth + 1, here, lines, env, end, targets)\n lines.push(`${pad}}`)\n break\n case 'for': {\n const cond = s.cond ? expr(s.cond, env).text : ''\n const init = header(s.init, env)\n const update = header(s.update, env)\n if (init !== undefined && update !== undefined) {\n // KIELER's `for (i = 1; i < n; i++)`.\n lines.push(`${pad}for (${init}; ${cond}; ${update}) {`)\n block(s.body, depth + 1, here, lines, env, end, targets)\n } else {\n // init; while (cond) { body; update } (SCL has no `continue`).\n block(s.init, depth, outer, lines, env, end, targets)\n lines.push(`${pad}while (${cond || '1'}) {`)\n block(s.body, depth + 1, here, lines, env, end, targets)\n block(s.update, depth + 1, here, lines, env, end, targets)\n }\n lines.push(`${pad}}`)\n break\n }\n case 'label':\n // Only the labels C jumps to (the others have no code in KIELER's structured C either).\n if (targets.has(s.name)) lines.push(`${pad.slice(2)}${s.name}: ;`)\n break\n case 'goto':\n lines.push(pad + (jumpsForward(s.target, here) ? `goto ${s.target};` : end))\n break\n case 'unsupported':\n env.fail(s.what)\n break\n }\n })\n}\n\n/** A for loop's init or update as one C expression (one simple effect), or undefined. */\nfunction header(list: CStmt[], env: ExprEnv): string | undefined {\n if (list.length > 1) return undefined\n if (!list.length) return ''\n const s = list[0]\n if (s.k !== 'do' || (s.f.k !== 'assign' && s.f.k !== 'call')) return undefined\n const code = effect(s.f, env)\n return code.length === 1 && !code[0].includes(' ? ') ? code[0].replace(/;$/, '') : undefined\n}\n\n/** The labels of a body that some `goto` jumps forward to. */\nfunction forwardTargets(body: CStmt[]): Set<string> {\n const out = new Set<string>()\n const walk = (list: CStmt[], outer: { list: CStmt[]; at: number }[]) => {\n list.forEach((s, at) => {\n const here = [...outer, { list, at }]\n if (s.k === 'goto' && jumpsForward(s.target, here)) out.add(s.target)\n else if (s.k === 'if') { walk(s.then, here); if (s.else) walk(s.else, here) }\n else if (s.k === 'while') walk(s.body, here)\n else if (s.k === 'for') { walk(s.init, outer); walk(s.body, here); walk(s.update, here) }\n })\n }\n walk(body, [])\n return out\n}\n\n/** A `goto` that C can take: to a label after it in the innermost list that has the label, not at that list's end. */\nfunction jumpsForward(label: string, stack: { list: CStmt[]; at: number }[]): boolean {\n for (let k = stack.length - 1; k >= 0; k--) {\n const { list, at } = stack[k]\n const to = list.findIndex(s => s.k === 'label' && s.name === label)\n if (to < 0) continue\n const trailing = list.slice(to + 1).every(s => s.k === 'label')\n return to > at && !trailing\n }\n return false\n}\n"]}
@@ -0,0 +1,6 @@
1
+ import type { ExecutableChart } from "../sim/exec.js";
2
+ /**
3
+ * The schedule positions of the effects that run in reset() instead of logic(), in the GO block's order: tick 1's
4
+ * input assignments (exec.ts goInit) and the moved effects that make no difference there.
5
+ */
6
+ export declare function resetNodes(exe: ExecutableChart): number[];