@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.
- package/LICENSE +277 -0
- package/README.md +125 -2
- package/dist/codegen/expr.d.ts +36 -0
- package/dist/codegen/expr.js +252 -0
- package/dist/codegen/expr.js.map +1 -0
- package/dist/codegen/index.d.ts +4 -0
- package/dist/codegen/index.js +238 -0
- package/dist/codegen/index.js.map +1 -0
- package/dist/codegen/layout.d.ts +57 -0
- package/dist/codegen/layout.js +306 -0
- package/dist/codegen/layout.js.map +1 -0
- package/dist/codegen/logic.d.ts +31 -0
- package/dist/codegen/logic.js +258 -0
- package/dist/codegen/logic.js.map +1 -0
- package/dist/codegen/methods.d.ts +17 -0
- package/dist/codegen/methods.js +242 -0
- package/dist/codegen/methods.js.map +1 -0
- package/dist/codegen/reset.d.ts +6 -0
- package/dist/codegen/reset.js +110 -0
- package/dist/codegen/reset.js.map +1 -0
- package/dist/codegen/types.d.ts +81 -0
- package/dist/codegen/types.js +3 -0
- package/dist/codegen/types.js.map +1 -0
- package/dist/format.d.ts +136 -0
- package/dist/format.js +872 -0
- package/dist/format.js.map +1 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.js +507 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +1 -0
- package/dist/internal.js +4 -0
- package/dist/internal.js.map +1 -0
- package/dist/parser/ids.d.ts +4 -0
- package/dist/parser/ids.js +172 -0
- package/dist/parser/ids.js.map +1 -0
- package/dist/parser/imports.d.ts +2 -0
- package/dist/parser/imports.js +28 -0
- package/dist/parser/imports.js.map +1 -0
- package/dist/parser/interface.d.ts +10 -0
- package/dist/parser/interface.js +93 -0
- package/dist/parser/interface.js.map +1 -0
- package/dist/parser/kielerWording.d.ts +2 -0
- package/dist/parser/kielerWording.js +196 -0
- package/dist/parser/kielerWording.js.map +1 -0
- package/dist/parser/lexer.d.ts +25 -0
- package/dist/parser/lexer.js +334 -0
- package/dist/parser/lexer.js.map +1 -0
- package/dist/parser/parser.d.ts +29 -0
- package/dist/parser/parser.js +3249 -0
- package/dist/parser/parser.js.map +1 -0
- package/dist/parser/resolve.d.ts +46 -0
- package/dist/parser/resolve.js +919 -0
- package/dist/parser/resolve.js.map +1 -0
- package/dist/parser/source.d.ts +15 -0
- package/dist/parser/source.js +42 -0
- package/dist/parser/source.js.map +1 -0
- package/dist/parser/validate.d.ts +5 -0
- package/dist/parser/validate.js +1430 -0
- package/dist/parser/validate.js.map +1 -0
- package/dist/sim/abortRegions.d.ts +9 -0
- package/dist/sim/abortRegions.js +192 -0
- package/dist/sim/abortRegions.js.map +1 -0
- package/dist/sim/analysis.d.ts +113 -0
- package/dist/sim/analysis.js +1104 -0
- package/dist/sim/analysis.js.map +1 -0
- package/dist/sim/codegen.d.ts +30 -0
- package/dist/sim/codegen.js +730 -0
- package/dist/sim/codegen.js.map +1 -0
- package/dist/sim/compile.d.ts +43 -0
- package/dist/sim/compile.js +1062 -0
- package/dist/sim/compile.js.map +1 -0
- package/dist/sim/deps.d.ts +73 -0
- package/dist/sim/deps.js +481 -0
- package/dist/sim/deps.js.map +1 -0
- package/dist/sim/describe.d.ts +22 -0
- package/dist/sim/describe.js +272 -0
- package/dist/sim/describe.js.map +1 -0
- package/dist/sim/describePreemption.d.ts +22 -0
- package/dist/sim/describePreemption.js +90 -0
- package/dist/sim/describePreemption.js.map +1 -0
- package/dist/sim/describeStructure.d.ts +75 -0
- package/dist/sim/describeStructure.js +293 -0
- package/dist/sim/describeStructure.js.map +1 -0
- package/dist/sim/eval.d.ts +99 -0
- package/dist/sim/eval.js +644 -0
- package/dist/sim/eval.js.map +1 -0
- package/dist/sim/exec.d.ts +212 -0
- package/dist/sim/exec.js +874 -0
- package/dist/sim/exec.js.map +1 -0
- package/dist/sim/fold.d.ts +2 -0
- package/dist/sim/fold.js +40 -0
- package/dist/sim/fold.js.map +1 -0
- package/dist/sim/goblock.d.ts +16 -0
- package/dist/sim/goblock.js +171 -0
- package/dist/sim/goblock.js.map +1 -0
- package/dist/sim/guardIndices.d.ts +13 -0
- package/dist/sim/guardIndices.js +137 -0
- package/dist/sim/guardIndices.js.map +1 -0
- package/dist/sim/index.d.ts +43 -0
- package/dist/sim/index.js +138 -0
- package/dist/sim/index.js.map +1 -0
- package/dist/sim/lower.d.ts +33 -0
- package/dist/sim/lower.js +716 -0
- package/dist/sim/lower.js.map +1 -0
- package/dist/sim/memory.d.ts +47 -0
- package/dist/sim/memory.js +328 -0
- package/dist/sim/memory.js.map +1 -0
- package/dist/sim/model.d.ts +487 -0
- package/dist/sim/model.js +772 -0
- package/dist/sim/model.js.map +1 -0
- package/dist/sim/passes/abort.d.ts +2 -0
- package/dist/sim/passes/abort.js +269 -0
- package/dist/sim/passes/abort.js.map +1 -0
- package/dist/sim/passes/abortRegion.d.ts +10 -0
- package/dist/sim/passes/abortRegion.js +62 -0
- package/dist/sim/passes/abortRegion.js.map +1 -0
- package/dist/sim/passes/arrayAssignment.d.ts +2 -0
- package/dist/sim/passes/arrayAssignment.js +147 -0
- package/dist/sim/passes/arrayAssignment.js.map +1 -0
- package/dist/sim/passes/codeEffects.d.ts +2 -0
- package/dist/sim/passes/codeEffects.js +14 -0
- package/dist/sim/passes/codeEffects.js.map +1 -0
- package/dist/sim/passes/complexFinalState.d.ts +4 -0
- package/dist/sim/passes/complexFinalState.js +77 -0
- package/dist/sim/passes/complexFinalState.js.map +1 -0
- package/dist/sim/passes/connector.d.ts +2 -0
- package/dist/sim/passes/connector.js +18 -0
- package/dist/sim/passes/connector.js.map +1 -0
- package/dist/sim/passes/contained.d.ts +37 -0
- package/dist/sim/passes/contained.js +198 -0
- package/dist/sim/passes/contained.js.map +1 -0
- package/dist/sim/passes/context.d.ts +39 -0
- package/dist/sim/passes/context.js +3 -0
- package/dist/sim/passes/context.js.map +1 -0
- package/dist/sim/passes/countDelay.d.ts +2 -0
- package/dist/sim/passes/countDelay.js +51 -0
- package/dist/sim/passes/countDelay.js.map +1 -0
- package/dist/sim/passes/dataflow.d.ts +11 -0
- package/dist/sim/passes/dataflow.js +319 -0
- package/dist/sim/passes/dataflow.js.map +1 -0
- package/dist/sim/passes/deferred.d.ts +2 -0
- package/dist/sim/passes/deferred.js +345 -0
- package/dist/sim/passes/deferred.js.map +1 -0
- package/dist/sim/passes/detect.d.ts +10 -0
- package/dist/sim/passes/detect.js +33 -0
- package/dist/sim/passes/detect.js.map +1 -0
- package/dist/sim/passes/during.d.ts +2 -0
- package/dist/sim/passes/during.js +67 -0
- package/dist/sim/passes/during.js.map +1 -0
- package/dist/sim/passes/entry.d.ts +5 -0
- package/dist/sim/passes/entry.js +93 -0
- package/dist/sim/passes/entry.js.map +1 -0
- package/dist/sim/passes/enum.d.ts +2 -0
- package/dist/sim/passes/enum.js +22 -0
- package/dist/sim/passes/enum.js.map +1 -0
- package/dist/sim/passes/exit.d.ts +4 -0
- package/dist/sim/passes/exit.js +120 -0
- package/dist/sim/passes/exit.js.map +1 -0
- package/dist/sim/passes/finalRegion.d.ts +2 -0
- package/dist/sim/passes/finalRegion.js +21 -0
- package/dist/sim/passes/finalRegion.js.map +1 -0
- package/dist/sim/passes/followedBy.d.ts +2 -0
- package/dist/sim/passes/followedBy.js +78 -0
- package/dist/sim/passes/followedBy.js.map +1 -0
- package/dist/sim/passes/forRegion.d.ts +9 -0
- package/dist/sim/passes/forRegion.js +230 -0
- package/dist/sim/passes/forRegion.js.map +1 -0
- package/dist/sim/passes/history.d.ts +12 -0
- package/dist/sim/passes/history.js +115 -0
- package/dist/sim/passes/history.js.map +1 -0
- package/dist/sim/passes/hostCode.d.ts +2 -0
- package/dist/sim/passes/hostCode.js +152 -0
- package/dist/sim/passes/hostCode.js.map +1 -0
- package/dist/sim/passes/index.d.ts +4 -0
- package/dist/sim/passes/index.js +84 -0
- package/dist/sim/passes/index.js.map +1 -0
- package/dist/sim/passes/inheritance.d.ts +8 -0
- package/dist/sim/passes/inheritance.js +233 -0
- package/dist/sim/passes/inheritance.js.map +1 -0
- package/dist/sim/passes/initOperator.d.ts +2 -0
- package/dist/sim/passes/initOperator.js +68 -0
- package/dist/sim/passes/initOperator.js.map +1 -0
- package/dist/sim/passes/initialization.d.ts +2 -0
- package/dist/sim/passes/initialization.js +69 -0
- package/dist/sim/passes/initialization.js.map +1 -0
- package/dist/sim/passes/ode.d.ts +2 -0
- package/dist/sim/passes/ode.js +14 -0
- package/dist/sim/passes/ode.js.map +1 -0
- package/dist/sim/passes/period.d.ts +2 -0
- package/dist/sim/passes/period.js +106 -0
- package/dist/sim/passes/period.js.map +1 -0
- package/dist/sim/passes/prTransitions.d.ts +2 -0
- package/dist/sim/passes/prTransitions.js +12 -0
- package/dist/sim/passes/prTransitions.js.map +1 -0
- package/dist/sim/passes/pre.d.ts +4 -0
- package/dist/sim/passes/pre.js +326 -0
- package/dist/sim/passes/pre.js.map +1 -0
- package/dist/sim/passes/quirks.d.ts +2 -0
- package/dist/sim/passes/quirks.js +169 -0
- package/dist/sim/passes/quirks.js.map +1 -0
- package/dist/sim/passes/reference.d.ts +60 -0
- package/dist/sim/passes/reference.js +386 -0
- package/dist/sim/passes/reference.js.map +1 -0
- package/dist/sim/passes/regionActions.d.ts +2 -0
- package/dist/sim/passes/regionActions.js +56 -0
- package/dist/sim/passes/regionActions.js.map +1 -0
- package/dist/sim/passes/sequentialFollowedBy.d.ts +2 -0
- package/dist/sim/passes/sequentialFollowedBy.js +72 -0
- package/dist/sim/passes/sequentialFollowedBy.js.map +1 -0
- package/dist/sim/passes/signal.d.ts +2 -0
- package/dist/sim/passes/signal.js +230 -0
- package/dist/sim/passes/signal.js.map +1 -0
- package/dist/sim/passes/static.d.ts +5 -0
- package/dist/sim/passes/static.js +67 -0
- package/dist/sim/passes/static.js.map +1 -0
- package/dist/sim/passes/suspend.d.ts +10 -0
- package/dist/sim/passes/suspend.js +71 -0
- package/dist/sim/passes/suspend.js.map +1 -0
- package/dist/sim/passes/termination.d.ts +9 -0
- package/dist/sim/passes/termination.js +69 -0
- package/dist/sim/passes/termination.js.map +1 -0
- package/dist/sim/passes/timedAutomata.d.ts +10 -0
- package/dist/sim/passes/timedAutomata.js +437 -0
- package/dist/sim/passes/timedAutomata.js.map +1 -0
- package/dist/sim/passes/unpackClasses.d.ts +2 -0
- package/dist/sim/passes/unpackClasses.js +54 -0
- package/dist/sim/passes/unpackClasses.js.map +1 -0
- package/dist/sim/passes/userSchedule.d.ts +2 -0
- package/dist/sim/passes/userSchedule.js +43 -0
- package/dist/sim/passes/userSchedule.js.map +1 -0
- package/dist/sim/passes/weakSuspend.d.ts +2 -0
- package/dist/sim/passes/weakSuspend.js +145 -0
- package/dist/sim/passes/weakSuspend.js.map +1 -0
- package/dist/sim/preemptionLog.d.ts +41 -0
- package/dist/sim/preemptionLog.js +133 -0
- package/dist/sim/preemptionLog.js.map +1 -0
- package/dist/sim/random.d.ts +12 -0
- package/dist/sim/random.js +40 -0
- package/dist/sim/random.js.map +1 -0
- package/dist/sim/reasons.d.ts +29 -0
- package/dist/sim/reasons.js +168 -0
- package/dist/sim/reasons.js.map +1 -0
- package/dist/sim/scg.d.ts +124 -0
- package/dist/sim/scg.js +723 -0
- package/dist/sim/scg.js.map +1 -0
- package/dist/sim/sfr.d.ts +4 -0
- package/dist/sim/sfr.js +264 -0
- package/dist/sim/sfr.js.map +1 -0
- package/dist/sim/stringConditions.d.ts +3 -0
- package/dist/sim/stringConditions.js +42 -0
- package/dist/sim/stringConditions.js.map +1 -0
- package/dist/sim/structure/copy.d.ts +4 -0
- package/dist/sim/structure/copy.js +140 -0
- package/dist/sim/structure/copy.js.map +1 -0
- package/dist/sim/structure/delays.d.ts +30 -0
- package/dist/sim/structure/delays.js +127 -0
- package/dist/sim/structure/delays.js.map +1 -0
- package/dist/sim/structure/instance.d.ts +15 -0
- package/dist/sim/structure/instance.js +69 -0
- package/dist/sim/structure/instance.js.map +1 -0
- package/dist/sim/structure/methods.d.ts +19 -0
- package/dist/sim/structure/methods.js +550 -0
- package/dist/sim/structure/methods.js.map +1 -0
- package/dist/sim/structure/rewrite.d.ts +20 -0
- package/dist/sim/structure/rewrite.js +113 -0
- package/dist/sim/structure/rewrite.js.map +1 -0
- package/dist/sim/structure/track.d.ts +22 -0
- package/dist/sim/structure/track.js +123 -0
- package/dist/sim/structure/track.js.map +1 -0
- package/dist/sim/tracker.d.ts +77 -0
- package/dist/sim/tracker.js +265 -0
- package/dist/sim/tracker.js.map +1 -0
- package/dist/sim/values.d.ts +31 -0
- package/dist/sim/values.js +103 -0
- package/dist/sim/values.js.map +1 -0
- package/dist/types.d.ts +670 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/package.json +58 -3
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// A defect of KIELER's netlist code generator (probes kieler/rare/probe-index-dropped-*): when it writes a
|
|
2
|
+
// condition's guard (`_c<g> = <condition>`, SimpleGuardSequentializer's copy of the guard), an array element whose
|
|
3
|
+
// index contains a variable loses its index if no plain variable comes before it in the condition and one comes
|
|
4
|
+
// after it (pre-order: `ov[n] > 0 && a` becomes `ov > 0 && a`, `a && ov[n] > 0` keeps it; a literal index is kept;
|
|
5
|
+
// a variable inside an index does not count as plain). The C code then uses the array itself, its address: never 0,
|
|
6
|
+
// so `ov > 0` and `!ok == false` hold, `ov == 0` and `ov < 3` do not; gcc rejects arithmetic other than + and - with
|
|
7
|
+
// it and comparisons with floats (`invalid operands to binary *`). Official KIELER 0.3.0 does the same.
|
|
8
|
+
//
|
|
9
|
+
// exec.ts evaluates such a condition with the address in place of the element (a large positive number, different
|
|
10
|
+
// for each array), and compile.ts reports it (`kieler-quirk` warning) or, where gcc fails, rejects the chart.
|
|
11
|
+
import { sourceText } from "./eval.js";
|
|
12
|
+
import { walkExpr } from "./model.js";
|
|
13
|
+
/** The array references of a condition whose index KIELER's C code drops (see above). */
|
|
14
|
+
export function droppedIndexRefs(e) {
|
|
15
|
+
const out = new Set();
|
|
16
|
+
const order = [];
|
|
17
|
+
const visit = (x, inIndex) => {
|
|
18
|
+
switch (x.k) {
|
|
19
|
+
case 'ref': {
|
|
20
|
+
const variableIndex = x.idx.some(i => { let found = false; walkExpr(i, y => { if (y.k === 'ref')
|
|
21
|
+
found = true; }); return found; });
|
|
22
|
+
if (!inIndex)
|
|
23
|
+
order.push({ r: x, plain: !x.idx.length && !x.sub, variableIndex });
|
|
24
|
+
for (const i of x.idx)
|
|
25
|
+
visit(i, true);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
case 'op':
|
|
29
|
+
for (const a of x.args)
|
|
30
|
+
visit(a, inIndex);
|
|
31
|
+
break;
|
|
32
|
+
case 'vector':
|
|
33
|
+
for (const a of x.values)
|
|
34
|
+
visit(a, inIndex);
|
|
35
|
+
break;
|
|
36
|
+
case 'call':
|
|
37
|
+
for (const a of x.args)
|
|
38
|
+
visit(a, inIndex);
|
|
39
|
+
break;
|
|
40
|
+
default: break;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
visit(e, false);
|
|
44
|
+
let plainBefore = false;
|
|
45
|
+
for (let i = 0; i < order.length; i++) {
|
|
46
|
+
const o = order[i];
|
|
47
|
+
if (o.plain) {
|
|
48
|
+
plainBefore = true;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (plainBefore || !o.variableIndex)
|
|
52
|
+
continue;
|
|
53
|
+
if (order.slice(i + 1).some(x => x.plain))
|
|
54
|
+
out.add(o.r);
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
/** A stand-in for the address of an array in KIELER's C code: large and positive, one per array. */
|
|
59
|
+
export function addressOf(v, slot) {
|
|
60
|
+
void v;
|
|
61
|
+
return 0x40000000 + slot * 64;
|
|
62
|
+
}
|
|
63
|
+
/** The condition with the dropped elements replaced by their array's address (for exec.ts). */
|
|
64
|
+
export function withAddresses(e, dropped, address) {
|
|
65
|
+
const map = (x) => {
|
|
66
|
+
switch (x.k) {
|
|
67
|
+
case 'ref': return dropped.has(x) ? { k: 'lit', t: 'int', v: address(x.v), span: x.span } : x;
|
|
68
|
+
case 'op': return { ...x, args: x.args.map(map) };
|
|
69
|
+
case 'call': return { ...x, args: x.args.map(map) };
|
|
70
|
+
default: return x;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
return map(e);
|
|
74
|
+
}
|
|
75
|
+
const cType = (v) => (v.type === 'float' ? 'double' : v.type === 'bool' || v.type === 'pure' ? 'char' : v.type === 'string' ? 'char*' : 'int');
|
|
76
|
+
/**
|
|
77
|
+
* The diagnostics for a condition with dropped indices: what gcc rejects (arithmetic other than + and - with the
|
|
78
|
+
* array, a comparison with a float), else a kieler-quirk warning. `label` names variables as the reader does.
|
|
79
|
+
*/
|
|
80
|
+
export function droppedIndexDiagnostics(e, dropped, label, related) {
|
|
81
|
+
const text = (x) => sourceText(x, label) ?? '...';
|
|
82
|
+
const type = (x) => {
|
|
83
|
+
if (x.k === 'ref')
|
|
84
|
+
return dropped.has(x) ? `${cType(x.v)} *` : x.idx.length || !x.v.dims.length ? cType(x.v) : `${cType(x.v)} *`;
|
|
85
|
+
if (x.k === 'lit')
|
|
86
|
+
return x.t === 'float' ? 'double' : x.t === 'string' ? 'char *' : 'int';
|
|
87
|
+
if (x.k === 'op' && (x.op === '+' || x.op === '-') && x.args.some(a => type(a)?.endsWith('*')))
|
|
88
|
+
return x.args.map(type).find(t => t?.endsWith('*'));
|
|
89
|
+
if (x.k === 'op' && x.args.some(a => type(a) === 'double') && !['&&', '||', '!', '==', '!=', '<', '<=', '>', '>='].includes(x.op))
|
|
90
|
+
return 'double';
|
|
91
|
+
return 'int';
|
|
92
|
+
};
|
|
93
|
+
let gcc;
|
|
94
|
+
walkExpr(e, x => {
|
|
95
|
+
if (gcc || x.k !== 'op')
|
|
96
|
+
return;
|
|
97
|
+
const types = x.args.map(type);
|
|
98
|
+
const pointer = types.findIndex(t => t?.endsWith('*'));
|
|
99
|
+
if (pointer < 0)
|
|
100
|
+
return;
|
|
101
|
+
const other = types.find((t, i) => i !== pointer) ?? 'int';
|
|
102
|
+
const symbol = x.op === 'neg' ? '-' : x.op;
|
|
103
|
+
const unary = x.op === 'neg' || x.op === '~';
|
|
104
|
+
const bad = unary || ['*', '/', '%', '&', '|', '^', '<<', '>>', '>>>'].includes(x.op)
|
|
105
|
+
|| (['+', '-', '<', '<=', '>', '>=', '==', '!='].includes(x.op) && other === 'double')
|
|
106
|
+
|| (x.op === '+' && types.filter(t => t?.endsWith('*')).length > 1);
|
|
107
|
+
if (bad)
|
|
108
|
+
gcc = { op: unary ? `unary ${symbol}` : `binary ${symbol}`, a: types[0] ?? 'int', b: types[1] ?? other, at: x };
|
|
109
|
+
});
|
|
110
|
+
const first = [...dropped][0];
|
|
111
|
+
const element = text(first);
|
|
112
|
+
const array = label(first.v);
|
|
113
|
+
if (gcc) {
|
|
114
|
+
return [{
|
|
115
|
+
severity: 'error', code: 'c-array-address', span: first.span, related,
|
|
116
|
+
message: `KIELER's C code drops the index of \`${element}\` here and computes with the array \`${array}\` itself, which gcc rejects`,
|
|
117
|
+
kieler: gcc.op.startsWith('unary') ? `wrong type argument to ${gcc.op}\nCompiler did not return success (exit value != 0)` : `invalid operands to ${gcc.op} (have ‘${gcc.a}’ and ‘${gcc.b}’)\nCompiler did not return success (exit value != 0)`,
|
|
118
|
+
hint: `A defect of KIELER's code generator: in a condition, an array element with a variable index loses the index when a plain variable comes after it and none before it. Put a plain variable first (\`${plainFirst(e, label) ?? 'a && ...'}\`), or copy the element into a variable of its own before the test.`,
|
|
119
|
+
}];
|
|
120
|
+
}
|
|
121
|
+
return [{
|
|
122
|
+
severity: 'warning', code: 'kieler-quirk', span: first.span, related,
|
|
123
|
+
message: `KIELER's compiled code drops the index of \`${element}\` in this condition: it tests the address of the array \`${array}\`, which is never 0, instead of the element`,
|
|
124
|
+
hint: `A defect of KIELER's code generator: in a condition, an array element with a variable index loses the index when a plain variable comes after it and none before it (\`${text(e)}\`). The guide's simulation does the same. Put a plain variable first (\`${plainFirst(e, label) ?? 'a && ...'}\`), or copy the element into a variable of its own before the test.`,
|
|
125
|
+
}];
|
|
126
|
+
}
|
|
127
|
+
/** `a && ov[n] > 0` for `ov[n] > 0 && a`: the operands of a top-level && or || reordered, plain ones first. */
|
|
128
|
+
function plainFirst(e, label) {
|
|
129
|
+
if (e.k !== 'op' || (e.op !== '&&' && e.op !== '||'))
|
|
130
|
+
return undefined;
|
|
131
|
+
const plain = (x) => x.k === 'ref' && !x.idx.length;
|
|
132
|
+
const ordered = [...e.args.filter(plain), ...e.args.filter(x => !plain(x))];
|
|
133
|
+
if (ordered.every((x, i) => x === e.args[i]))
|
|
134
|
+
return undefined;
|
|
135
|
+
return sourceText({ ...e, args: ordered }, label);
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=guardIndices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guardIndices.js","sourceRoot":"","sources":["../../src/sim/guardIndices.ts"],"names":[],"mappings":"AAAA,2GAA2G;AAC3G,mHAAmH;AACnH,gHAAgH;AAChH,mHAAmH;AACnH,oHAAoH;AACpH,qHAAqH;AACrH,wGAAwG;AACxG,EAAE;AACF,kHAAkH;AAClH,8GAA8G;AAG9G,OAAO,EAAE,UAAU,EAAE,kBAAc;AACnC,OAAO,EAAE,QAAQ,EAAoC,mBAAe;AAEpE,yFAAyF;AACzF,MAAM,UAAU,gBAAgB,CAAC,CAAQ;IACrC,MAAM,GAAG,GAAG,IAAI,GAAG,EAAQ,CAAA;IAC3B,MAAM,KAAK,GAA0D,EAAE,CAAA;IACvE,MAAM,KAAK,GAAG,CAAC,CAAQ,EAAE,OAAgB,EAAE,EAAE;QACzC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;YACV,KAAK,KAAK,CAAC,CAAC,CAAC;gBACT,MAAM,aAAa,GAAG,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,KAAK,GAAG,KAAK,CAAC,CAAC,QAAQ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CAAA;IACtE,MAAM,KAAK,GAAG,CAAC,CAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAA;IAC1D,MAAM,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC3E,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAAE,OAAO,SAAS,CAAA;IAC9D,OAAO,UAAU,CAAC,EAAE,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,KAAK,CAAC,CAAA;AACrD,CAAC","sourcesContent":["// A defect of KIELER's netlist code generator (probes kieler/rare/probe-index-dropped-*): when it writes a\n// condition's guard (`_c<g> = <condition>`, SimpleGuardSequentializer's copy of the guard), an array element whose\n// index contains a variable loses its index if no plain variable comes before it in the condition and one comes\n// after it (pre-order: `ov[n] > 0 && a` becomes `ov > 0 && a`, `a && ov[n] > 0` keeps it; a literal index is kept;\n// a variable inside an index does not count as plain). The C code then uses the array itself, its address: never 0,\n// so `ov > 0` and `!ok == false` hold, `ov == 0` and `ov < 3` do not; gcc rejects arithmetic other than + and - with\n// it and comparisons with floats (`invalid operands to binary *`). Official KIELER 0.3.0 does the same.\n//\n// exec.ts evaluates such a condition with the address in place of the element (a large positive number, different\n// for each array), and compile.ts reports it (`kieler-quirk` warning) or, where gcc fails, rejects the chart.\n\nimport type { Diagnostic } from '../types'\nimport { sourceText } from './eval'\nimport { walkExpr, type CExpr, type CRef, type CVar } from './model'\n\n/** The array references of a condition whose index KIELER's C code drops (see above). */\nexport function droppedIndexRefs(e: CExpr): Set<CRef> {\n const out = new Set<CRef>()\n const order: { r: CRef; plain: boolean; variableIndex: boolean }[] = []\n const visit = (x: CExpr, inIndex: boolean) => {\n switch (x.k) {\n case 'ref': {\n const variableIndex = x.idx.some(i => { let found = false; walkExpr(i, y => { if (y.k === 'ref') found = true }); return found })\n if (!inIndex) order.push({ r: x, plain: !x.idx.length && !x.sub, variableIndex })\n for (const i of x.idx) visit(i, true)\n break\n }\n case 'op': for (const a of x.args) visit(a, inIndex); break\n case 'vector': for (const a of x.values) visit(a, inIndex); break\n case 'call': for (const a of x.args) visit(a, inIndex); break\n default: break\n }\n }\n visit(e, false)\n let plainBefore = false\n for (let i = 0; i < order.length; i++) {\n const o = order[i]\n if (o.plain) { plainBefore = true; continue }\n if (plainBefore || !o.variableIndex) continue\n if (order.slice(i + 1).some(x => x.plain)) out.add(o.r)\n }\n return out\n}\n\n/** A stand-in for the address of an array in KIELER's C code: large and positive, one per array. */\nexport function addressOf(v: CVar, slot: number): number {\n void v\n return 0x40000000 + slot * 64\n}\n\n/** The condition with the dropped elements replaced by their array's address (for exec.ts). */\nexport function withAddresses(e: CExpr, dropped: Set<CRef>, address: (v: CVar) => number): CExpr {\n const map = (x: CExpr): CExpr => {\n switch (x.k) {\n case 'ref': return dropped.has(x) ? { k: 'lit', t: 'int', v: address(x.v), span: x.span } : x\n case 'op': return { ...x, args: x.args.map(map) }\n case 'call': return { ...x, args: x.args.map(map) }\n default: return x\n }\n }\n return map(e)\n}\n\nconst cType = (v: CVar): string => (v.type === 'float' ? 'double' : v.type === 'bool' || v.type === 'pure' ? 'char' : v.type === 'string' ? 'char*' : 'int')\n\n/**\n * The diagnostics for a condition with dropped indices: what gcc rejects (arithmetic other than + and - with the\n * array, a comparison with a float), else a kieler-quirk warning. `label` names variables as the reader does.\n */\nexport function droppedIndexDiagnostics(e: CExpr, dropped: Set<CRef>, label: (v: CVar) => string, related: string[] | undefined): Diagnostic[] {\n const text = (x: CExpr) => sourceText(x, label) ?? '...'\n const type = (x: CExpr): string | undefined => {\n if (x.k === 'ref') return dropped.has(x) ? `${cType(x.v)} *` : x.idx.length || !x.v.dims.length ? cType(x.v) : `${cType(x.v)} *`\n if (x.k === 'lit') return x.t === 'float' ? 'double' : x.t === 'string' ? 'char *' : 'int'\n if (x.k === 'op' && (x.op === '+' || x.op === '-') && x.args.some(a => type(a)?.endsWith('*'))) return x.args.map(type).find(t => t?.endsWith('*'))\n if (x.k === 'op' && x.args.some(a => type(a) === 'double') && !['&&', '||', '!', '==', '!=', '<', '<=', '>', '>='].includes(x.op)) return 'double'\n return 'int'\n }\n let gcc: { op: string; a: string; b: string; at?: CExpr } | undefined\n walkExpr(e, x => {\n if (gcc || x.k !== 'op') return\n const types = x.args.map(type)\n const pointer = types.findIndex(t => t?.endsWith('*'))\n if (pointer < 0) return\n const other = types.find((t, i) => i !== pointer) ?? 'int'\n const symbol = x.op === 'neg' ? '-' : x.op\n const unary = x.op === 'neg' || x.op === '~'\n const bad = unary || ['*', '/', '%', '&', '|', '^', '<<', '>>', '>>>'].includes(x.op)\n || (['+', '-', '<', '<=', '>', '>=', '==', '!='].includes(x.op) && other === 'double')\n || (x.op === '+' && types.filter(t => t?.endsWith('*')).length > 1)\n if (bad) gcc = { op: unary ? `unary ${symbol}` : `binary ${symbol}`, a: types[0] ?? 'int', b: types[1] ?? other, at: x }\n })\n const first = [...dropped][0]\n const element = text(first)\n const array = label(first.v)\n if (gcc) {\n return [{\n severity: 'error', code: 'c-array-address', span: first.span, related,\n message: `KIELER's C code drops the index of \\`${element}\\` here and computes with the array \\`${array}\\` itself, which gcc rejects`,\n kieler: gcc.op.startsWith('unary') ? `wrong type argument to ${gcc.op}\\nCompiler did not return success (exit value != 0)` : `invalid operands to ${gcc.op} (have ‘${gcc.a}’ and ‘${gcc.b}’)\\nCompiler did not return success (exit value != 0)`,\n hint: `A defect of KIELER's code generator: in a condition, an array element with a variable index loses the index when a plain variable comes after it and none before it. Put a plain variable first (\\`${plainFirst(e, label) ?? 'a && ...'}\\`), or copy the element into a variable of its own before the test.`,\n }]\n }\n return [{\n severity: 'warning', code: 'kieler-quirk', span: first.span, related,\n message: `KIELER's compiled code drops the index of \\`${element}\\` in this condition: it tests the address of the array \\`${array}\\`, which is never 0, instead of the element`,\n hint: `A defect of KIELER's code generator: in a condition, an array element with a variable index loses the index when a plain variable comes after it and none before it (\\`${text(e)}\\`). The guide's simulation does the same. Put a plain variable first (\\`${plainFirst(e, label) ?? 'a && ...'}\\`), or copy the element into a variable of its own before the test.`,\n }]\n}\n\n/** `a && ov[n] > 0` for `ov[n] > 0 && a`: the operands of a top-level && or || reordered, plain ones first. */\nfunction plainFirst(e: CExpr, label: (v: CVar) => string): string | undefined {\n if (e.k !== 'op' || (e.op !== '&&' && e.op !== '||')) return undefined\n const plain = (x: CExpr) => x.k === 'ref' && !x.idx.length\n const ordered = [...e.args.filter(plain), ...e.args.filter(x => !plain(x))]\n if (ordered.every((x, i) => x === e.args[i])) return undefined\n return sourceText({ ...e, args: ordered }, label)\n}\n"]}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Diagnostic, State, TickResult, Value, VariableInfo } from "../types.js";
|
|
2
|
+
import type { ModelIndex } from "../parser/resolve.js";
|
|
3
|
+
import { type CompiledChart } from "./compile.js";
|
|
4
|
+
import { type SimTickResult } from "./exec.js";
|
|
5
|
+
export { compileChart, type CompiledChart } from "./compile.js";
|
|
6
|
+
export type { PoolValue, SimTickResult } from "./exec.js";
|
|
7
|
+
export { describeTick, elementName, rememberImports, type DescribeOptions } from "./describe.js";
|
|
8
|
+
/**
|
|
9
|
+
* Codes of the diagnostics the simulator's compilation adds to compile() (KIELER's compiler and code generator
|
|
10
|
+
* stage, after the front end). Tests that compare the front end with KIELER's validator can filter them out.
|
|
11
|
+
*/
|
|
12
|
+
export declare const SIMULATOR_CODES: ReadonlySet<string>;
|
|
13
|
+
/** The engine's Simulation (docs-internal/ARCHITECTURE.md). */
|
|
14
|
+
export interface Simulation {
|
|
15
|
+
readonly interface: VariableInfo[];
|
|
16
|
+
/** Completed ticks. */
|
|
17
|
+
readonly tick: number;
|
|
18
|
+
readonly history: SimTickResult[];
|
|
19
|
+
/** One tick. `inputs` by name; one left out, or `undefined`, keeps its value (as in KIELER). */
|
|
20
|
+
step(inputs?: Readonly<Record<string, Value | undefined>>): SimTickResult;
|
|
21
|
+
reset(): void;
|
|
22
|
+
/** Goes back to the state after tick `toTick` (0: before the first tick); later history is dropped. */
|
|
23
|
+
rewind(toTick: number): void;
|
|
24
|
+
/** KIELER's taken-transition counters of a tick, in Taken Transition Signaling order (the oracle's `taken`). */
|
|
25
|
+
takenCounts(result: TickResult): number[];
|
|
26
|
+
}
|
|
27
|
+
/** What createSimulation needs from compile(). */
|
|
28
|
+
export interface SimulationInput {
|
|
29
|
+
ok: boolean;
|
|
30
|
+
chart?: State;
|
|
31
|
+
diagnostics: Diagnostic[];
|
|
32
|
+
interface: VariableInfo[];
|
|
33
|
+
index?: ModelIndex;
|
|
34
|
+
/** Set by compile() when the chart passed the simulator's static checks. */
|
|
35
|
+
sim?: CompiledChart;
|
|
36
|
+
}
|
|
37
|
+
export interface SimulationOptions {
|
|
38
|
+
/** step() throws on a name that is no input of the chart (a typo: `buton` for `button`), with the input it most
|
|
39
|
+
* likely meant. Off by default: KIELER's simulation takes any name from its environment into the data pool (the
|
|
40
|
+
* pool then has it; replaying a trace file with extra columns relies on that). */
|
|
41
|
+
strict?: boolean;
|
|
42
|
+
}
|
|
43
|
+
export declare function createSimulation(result: SimulationInput, options?: SimulationOptions): Simulation;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// The simulator's public face: compile a resolved chart into an executable model (compileChart in compile.ts,
|
|
2
|
+
// called from compile() in ../index.ts) and run it (createSimulation). See README.md for the architecture.
|
|
3
|
+
import { compileChart } from "./compile.js";
|
|
4
|
+
import { ExecutableChart, SimRunner } from "./exec.js";
|
|
5
|
+
export { compileChart } from "./compile.js";
|
|
6
|
+
export { describeTick, elementName, rememberImports } from "./describe.js";
|
|
7
|
+
/**
|
|
8
|
+
* Codes of the diagnostics the simulator's compilation adds to compile() (KIELER's compiler and code generator
|
|
9
|
+
* stage, after the front end). Tests that compare the front end with KIELER's validator can filter them out.
|
|
10
|
+
*/
|
|
11
|
+
export const SIMULATOR_CODES = new Set([
|
|
12
|
+
'unsupported', 'causality-cycle', 'instantaneous-loop', 'kieler-crash', 'cardinality', 'unsigned-shift',
|
|
13
|
+
'array-assignment', 'vector-ignore', 'string-arithmetic', 'empty-method', 'for-outer-counter', 'val-not-signal', 'duplicate-declaration', 'c-keyword', 'c-reserved-name', 'method-array-parameter', 'c-naming', 'c-name', 'c-string', 'c-float-modulo', 'extern-not-called', 'static-access', 'count-delay-reset',
|
|
14
|
+
'unsupported-combine', 'randomize-tick1', 'randomize-stdlib', 'region-action-final', 'kieler-quirk', 'kieler-bug', 'kieler-loop-warning',
|
|
15
|
+
'kieler-nondeterministic', 'kieler-divergence', 'pre-placement',
|
|
16
|
+
// The kinds of scheduling cycle (all "The SCG is NOT asc-schedulable!" in KIELER).
|
|
17
|
+
'write-conflict', 'update-conflict', 'emission-conflict', 'val-cycle', 'abort-trigger-cycle', 'suspend-abort-cycle', 'period-abort-cycle',
|
|
18
|
+
// Structure passes (references, for-regions, dataflow, clocks, methods).
|
|
19
|
+
'binding-expression', 'recursive-reference', 'for-counter-assignment', 'dataflow-for', 'unknown-type', 'shared-clock',
|
|
20
|
+
'clock-variable', 'clock-array', 'timing-constraint', 'default-sleep', 'method-without-value', 'method-without-return', 'for-counter-method',
|
|
21
|
+
'inheritance-conflict', 'module-call', 'method-twice', 'inline-loop', 'signal-member', 'unpack-instances',
|
|
22
|
+
// Preemption passes (suspend, weak suspend, abort regions).
|
|
23
|
+
'suspend-without-trigger', 'abort-region-order', 'abort-region-alone', 'abort-region-loop',
|
|
24
|
+
// Final polish (engine-5).
|
|
25
|
+
'conditional-without-else', 'index-out-of-range', 'c-array-address',
|
|
26
|
+
// Found by the C code generator's checks (tools/cgen).
|
|
27
|
+
'c-string-condition',
|
|
28
|
+
]);
|
|
29
|
+
export function createSimulation(result, options = {}) {
|
|
30
|
+
if (!result.ok || !result.chart)
|
|
31
|
+
return rejectedSimulation(result, result.sim);
|
|
32
|
+
const compiled = result.sim ?? compileChart(result.chart, result.index);
|
|
33
|
+
if (!result.ok || !compiled || !compiled.runnable || !result.chart)
|
|
34
|
+
return rejectedSimulation(result, compiled);
|
|
35
|
+
const exe = new ExecutableChart(compiled, result.interface);
|
|
36
|
+
const runner = new SimRunner(exe, result.chart);
|
|
37
|
+
const history = [];
|
|
38
|
+
const inputsLog = [];
|
|
39
|
+
const snapshots = [runner.snapshot()];
|
|
40
|
+
return {
|
|
41
|
+
interface: result.interface,
|
|
42
|
+
get tick() { return history.length; },
|
|
43
|
+
get history() { return history; },
|
|
44
|
+
step(given = {}) {
|
|
45
|
+
const inputs = defined(given);
|
|
46
|
+
if (options.strict)
|
|
47
|
+
checkInputNames(inputs, result.interface);
|
|
48
|
+
const r = runner.step(history.length + 1, inputs);
|
|
49
|
+
history.push(r);
|
|
50
|
+
inputsLog.push({ ...inputs });
|
|
51
|
+
snapshots.push(runner.snapshot());
|
|
52
|
+
return r;
|
|
53
|
+
},
|
|
54
|
+
reset() {
|
|
55
|
+
runner.reset();
|
|
56
|
+
history.length = 0;
|
|
57
|
+
inputsLog.length = 0;
|
|
58
|
+
snapshots.length = 0;
|
|
59
|
+
snapshots.push(runner.snapshot());
|
|
60
|
+
},
|
|
61
|
+
rewind(toTick) {
|
|
62
|
+
const n = Math.max(0, Math.min(Math.floor(toTick), history.length));
|
|
63
|
+
runner.restore(snapshots[n]);
|
|
64
|
+
history.length = n;
|
|
65
|
+
inputsLog.length = n;
|
|
66
|
+
snapshots.length = n + 1;
|
|
67
|
+
},
|
|
68
|
+
takenCounts(r) {
|
|
69
|
+
return runner.takenCounts(r);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** A chart that cannot run: every step reports the first error. */
|
|
74
|
+
function rejectedSimulation(result, compiled) {
|
|
75
|
+
const history = [];
|
|
76
|
+
const unsupported = compiled?.diagnostics.find(d => d.code === 'unsupported');
|
|
77
|
+
const error = result.diagnostics.find(d => d.severity === 'error') ??
|
|
78
|
+
compiled?.diagnostics.find(d => d.severity === 'error') ??
|
|
79
|
+
(unsupported ? { ...unsupported, severity: 'error' } : undefined) ??
|
|
80
|
+
{ severity: 'error', code: 'no-chart', message: 'There is no chart to run' };
|
|
81
|
+
return {
|
|
82
|
+
interface: result.interface,
|
|
83
|
+
get tick() { return history.length; },
|
|
84
|
+
get history() { return history; },
|
|
85
|
+
step(given = {}) {
|
|
86
|
+
const inputs = defined(given);
|
|
87
|
+
const r = {
|
|
88
|
+
tick: history.length + 1, inputs, values: {}, present: {}, active: [], entered: [], exited: [], taken: [], actions: [],
|
|
89
|
+
terminated: false, terminatedNow: false, log: [], printed: [], error, pool: {}, trace: {},
|
|
90
|
+
};
|
|
91
|
+
history.push(r);
|
|
92
|
+
return r;
|
|
93
|
+
},
|
|
94
|
+
reset() { history.length = 0; },
|
|
95
|
+
rewind(toTick) { history.length = Math.max(0, Math.min(toTick, history.length)); },
|
|
96
|
+
takenCounts() { return []; },
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** The names step() takes in strict mode: the chart's inputs, and `V_val` for a valued input signal `V` (KIELER's). */
|
|
100
|
+
function checkInputNames(inputs, iface) {
|
|
101
|
+
const known = new Set();
|
|
102
|
+
for (const v of iface) {
|
|
103
|
+
if (!v.input)
|
|
104
|
+
continue;
|
|
105
|
+
known.add(v.name);
|
|
106
|
+
if (v.valued)
|
|
107
|
+
known.add(`${v.name}_val`);
|
|
108
|
+
}
|
|
109
|
+
for (const name of Object.keys(inputs)) {
|
|
110
|
+
if (known.has(name))
|
|
111
|
+
continue;
|
|
112
|
+
const close = [...known].map(k => ({ k, d: editDistance(name.toLowerCase(), k.toLowerCase()) })).sort((a, b) => a.d - b.d)[0];
|
|
113
|
+
const hint = close && close.d <= Math.max(2, Math.floor(name.length / 3)) ? ` (did you mean \`${close.k}\`?)` : '';
|
|
114
|
+
throw new Error(`\`${name}\` is not an input of the chart${hint}. Its inputs: ${[...known].map(k => `\`${k}\``).join(', ') || 'none'}.`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function editDistance(a, b) {
|
|
118
|
+
const row = Array.from({ length: b.length + 1 }, (_, j) => j);
|
|
119
|
+
for (let i = 1; i <= a.length; i++) {
|
|
120
|
+
let prev = row[0];
|
|
121
|
+
row[0] = i;
|
|
122
|
+
for (let j = 1; j <= b.length; j++) {
|
|
123
|
+
const keep = row[j];
|
|
124
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
125
|
+
prev = keep;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return row[b.length];
|
|
129
|
+
}
|
|
130
|
+
/** The inputs given a value (`undefined` is one not given: it keeps its value). */
|
|
131
|
+
function defined(given) {
|
|
132
|
+
const out = {};
|
|
133
|
+
for (const [k, v] of Object.entries(given))
|
|
134
|
+
if (v !== undefined)
|
|
135
|
+
out[k] = v;
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/sim/index.ts"],"names":[],"mappings":"AAAA,8GAA8G;AAC9G,2GAA2G;AAI3G,OAAO,EAAE,YAAY,EAAsB,qBAAiB;AAC5D,OAAO,EAAE,eAAe,EAAE,SAAS,EAAsB,kBAAc;AAEvE,OAAO,EAAE,YAAY,EAAsB,qBAAiB;AAE5D,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,eAAe,EAAwB,sBAAkB;AAE7F;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAwB,IAAI,GAAG,CAAC;IACxD,aAAa,EAAE,iBAAi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,EAAE,CAAA;IACrC,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,IAAI,CAAC,KAAK,SAAS;YAAE,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;IAC3E,OAAO,GAAG,CAAA;AACd,CAAC","sourcesContent":["// The simulator's public face: compile a resolved chart into an executable model (compileChart in compile.ts,\n// called from compile() in ../index.ts) and run it (createSimulation). See README.md for the architecture.\n\nimport type { Diagnostic, State, TickResult, Value, VariableInfo } from '../types'\nimport type { ModelIndex } from '../parser/resolve'\nimport { compileChart, type CompiledChart } from './compile'\nimport { ExecutableChart, SimRunner, type SimTickResult } from './exec'\n\nexport { compileChart, type CompiledChart } from './compile'\nexport type { PoolValue, SimTickResult } from './exec'\nexport { describeTick, elementName, rememberImports, type DescribeOptions } from './describe'\n\n/**\n * Codes of the diagnostics the simulator's compilation adds to compile() (KIELER's compiler and code generator\n * stage, after the front end). Tests that compare the front end with KIELER's validator can filter them out.\n */\nexport const SIMULATOR_CODES: ReadonlySet<string> = new Set([\n 'unsupported', 'causality-cycle', 'instantaneous-loop', 'kieler-crash', 'cardinality', 'unsigned-shift',\n 'array-assignment', 'vector-ignore', 'string-arithmetic', 'empty-method', 'for-outer-counter', 'val-not-signal', 'duplicate-declaration', 'c-keyword', 'c-reserved-name', 'method-array-parameter', 'c-naming', 'c-name', 'c-string', 'c-float-modulo', 'extern-not-called', 'static-access', 'count-delay-reset',\n 'unsupported-combine', 'randomize-tick1', 'randomize-stdlib', 'region-action-final', 'kieler-quirk', 'kieler-bug', 'kieler-loop-warning',\n 'kieler-nondeterministic', 'kieler-divergence', 'pre-placement',\n // The kinds of scheduling cycle (all \"The SCG is NOT asc-schedulable!\" in KIELER).\n 'write-conflict', 'update-conflict', 'emission-conflict', 'val-cycle', 'abort-trigger-cycle', 'suspend-abort-cycle', 'period-abort-cycle',\n // Structure passes (references, for-regions, dataflow, clocks, methods).\n 'binding-expression', 'recursive-reference', 'for-counter-assignment', 'dataflow-for', 'unknown-type', 'shared-clock',\n 'clock-variable', 'clock-array', 'timing-constraint', 'default-sleep', 'method-without-value', 'method-without-return', 'for-counter-method',\n 'inheritance-conflict', 'module-call', 'method-twice', 'inline-loop', 'signal-member', 'unpack-instances',\n // Preemption passes (suspend, weak suspend, abort regions).\n 'suspend-without-trigger', 'abort-region-order', 'abort-region-alone', 'abort-region-loop',\n // Final polish (engine-5).\n 'conditional-without-else', 'index-out-of-range', 'c-array-address',\n // Found by the C code generator's checks (tools/cgen).\n 'c-string-condition',\n])\n\n/** The engine's Simulation (docs-internal/ARCHITECTURE.md). */\nexport interface Simulation {\n readonly interface: VariableInfo[]\n /** Completed ticks. */\n readonly tick: number\n readonly history: SimTickResult[]\n /** One tick. `inputs` by name; one left out, or `undefined`, keeps its value (as in KIELER). */\n step(inputs?: Readonly<Record<string, Value | undefined>>): SimTickResult\n reset(): void\n /** Goes back to the state after tick `toTick` (0: before the first tick); later history is dropped. */\n rewind(toTick: number): void\n /** KIELER's taken-transition counters of a tick, in Taken Transition Signaling order (the oracle's `taken`). */\n takenCounts(result: TickResult): number[]\n}\n\n/** What createSimulation needs from compile(). */\nexport interface SimulationInput {\n ok: boolean\n chart?: State\n diagnostics: Diagnostic[]\n interface: VariableInfo[]\n index?: ModelIndex\n /** Set by compile() when the chart passed the simulator's static checks. */\n sim?: CompiledChart\n}\n\nexport interface SimulationOptions {\n /** step() throws on a name that is no input of the chart (a typo: `buton` for `button`), with the input it most\n * likely meant. Off by default: KIELER's simulation takes any name from its environment into the data pool (the\n * pool then has it; replaying a trace file with extra columns relies on that). */\n strict?: boolean\n}\n\nexport function createSimulation(result: SimulationInput, options: SimulationOptions = {}): Simulation {\n if (!result.ok || !result.chart) return rejectedSimulation(result, result.sim)\n const compiled = result.sim ?? compileChart(result.chart, result.index)\n if (!result.ok || !compiled || !compiled.runnable || !result.chart) return rejectedSimulation(result, compiled)\n const exe = new ExecutableChart(compiled, result.interface)\n const runner = new SimRunner(exe, result.chart)\n const history: SimTickResult[] = []\n const inputsLog: Record<string, Value>[] = []\n const snapshots: ReturnType<SimRunner['snapshot']>[] = [runner.snapshot()]\n return {\n interface: result.interface,\n get tick() { return history.length },\n get history() { return history },\n step(given: Readonly<Record<string, Value | undefined>> = {}) {\n const inputs = defined(given)\n if (options.strict) checkInputNames(inputs, result.interface)\n const r = runner.step(history.length + 1, inputs)\n history.push(r)\n inputsLog.push({ ...inputs })\n snapshots.push(runner.snapshot())\n return r\n },\n reset() {\n runner.reset()\n history.length = 0\n inputsLog.length = 0\n snapshots.length = 0\n snapshots.push(runner.snapshot())\n },\n rewind(toTick: number) {\n const n = Math.max(0, Math.min(Math.floor(toTick), history.length))\n runner.restore(snapshots[n])\n history.length = n\n inputsLog.length = n\n snapshots.length = n + 1\n },\n takenCounts(r: TickResult) {\n return runner.takenCounts(r)\n },\n }\n}\n\n/** A chart that cannot run: every step reports the first error. */\nfunction rejectedSimulation(result: SimulationInput, compiled: CompiledChart | undefined): Simulation {\n const history: SimTickResult[] = []\n const unsupported = compiled?.diagnostics.find(d => d.code === 'unsupported')\n const error: Diagnostic = result.diagnostics.find(d => d.severity === 'error') ??\n compiled?.diagnostics.find(d => d.severity === 'error') ??\n (unsupported ? { ...unsupported, severity: 'error' } : undefined) ??\n { severity: 'error', code: 'no-chart', message: 'There is no chart to run' }\n return {\n interface: result.interface,\n get tick() { return history.length },\n get history() { return history },\n step(given: Readonly<Record<string, Value | undefined>> = {}) {\n const inputs = defined(given)\n const r: SimTickResult = {\n tick: history.length + 1, inputs, values: {}, present: {}, active: [], entered: [], exited: [], taken: [], actions: [],\n terminated: false, terminatedNow: false, log: [], printed: [], error, pool: {}, trace: {},\n }\n history.push(r)\n return r\n },\n reset() { history.length = 0 },\n rewind(toTick: number) { history.length = Math.max(0, Math.min(toTick, history.length)) },\n takenCounts() { return [] },\n }\n}\n\n/** The names step() takes in strict mode: the chart's inputs, and `V_val` for a valued input signal `V` (KIELER's). */\nfunction checkInputNames(inputs: Record<string, Value>, iface: VariableInfo[]) {\n const known = new Set<string>()\n for (const v of iface) {\n if (!v.input) continue\n known.add(v.name)\n if (v.valued) known.add(`${v.name}_val`)\n }\n for (const name of Object.keys(inputs)) {\n if (known.has(name)) continue\n const close = [...known].map(k => ({ k, d: editDistance(name.toLowerCase(), k.toLowerCase()) })).sort((a, b) => a.d - b.d)[0]\n const hint = close && close.d <= Math.max(2, Math.floor(name.length / 3)) ? ` (did you mean \\`${close.k}\\`?)` : ''\n throw new Error(`\\`${name}\\` is not an input of the chart${hint}. Its inputs: ${[...known].map(k => `\\`${k}\\``).join(', ') || 'none'}.`)\n }\n}\n\nfunction editDistance(a: string, b: string): number {\n const row = Array.from({ length: b.length + 1 }, (_, j) => j)\n for (let i = 1; i <= a.length; i++) {\n let prev = row[0]\n row[0] = i\n for (let j = 1; j <= b.length; j++) {\n const keep = row[j]\n row[j] = Math.min(row[j] + 1, row[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1))\n prev = keep\n }\n }\n return row[b.length]\n}\n\n/** The inputs given a value (`undefined` is one not given: it keeps its value). */\nfunction defined(given: Readonly<Record<string, Value | undefined>>): Record<string, Value> {\n const out: Record<string, Value> = {}\n for (const [k, v] of Object.entries(given)) if (v !== undefined) out[k] = v\n return out\n}\n"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { AssignOperator, Diagnostic, Span, State } from "../types.js";
|
|
2
|
+
import type { ModelIndex } from "../parser/resolve.js";
|
|
3
|
+
import { CRegion, CState, CVar, type CExpr, type CRef } from "./model.js";
|
|
4
|
+
/** A dataflow equation (`x = e`, `x += e`, `g.A = e`, `g = {e1, e2}`), lowered; `sequential`: ends with `;`/`seq`. */
|
|
5
|
+
export interface CEquation {
|
|
6
|
+
target: CRef;
|
|
7
|
+
op: AssignOperator;
|
|
8
|
+
expr: CExpr;
|
|
9
|
+
sequential: boolean;
|
|
10
|
+
/** A dataflow reference call `A(x, y)` (not supported by the simulator). */
|
|
11
|
+
call: boolean;
|
|
12
|
+
span?: Span;
|
|
13
|
+
}
|
|
14
|
+
export interface LowerResult {
|
|
15
|
+
root: CState;
|
|
16
|
+
/** Source valued object id -> working variable (consts included, although they are inlined). */
|
|
17
|
+
vars: Map<string, CVar>;
|
|
18
|
+
/** Source state id -> working state, as lowered (passes may later restructure). */
|
|
19
|
+
states: Map<string, CState>;
|
|
20
|
+
regions: Map<string, CRegion>;
|
|
21
|
+
/** KIELER's Taken Transition Signaling order: index -> source transition id. */
|
|
22
|
+
takenOrder: string[];
|
|
23
|
+
/** Problems found while lowering (constructs the simulator cannot represent). */
|
|
24
|
+
problems: {
|
|
25
|
+
message: string;
|
|
26
|
+
span?: Span;
|
|
27
|
+
what: string;
|
|
28
|
+
}[];
|
|
29
|
+
/** Errors KIELER reports for what the lowering sees (compile.ts reports them). */
|
|
30
|
+
reports: Diagnostic[];
|
|
31
|
+
}
|
|
32
|
+
/** Lowers the scchart `chart` (with the resolved index from compile()). */
|
|
33
|
+
export declare function lowerChart(chart: State, index: ModelIndex | undefined): LowerResult;
|