@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,169 @@
|
|
|
1
|
+
// Static checks for KIELER's preemption quirks (preemption.md), run on the chart before the preemption passes
|
|
2
|
+
// rewrite it (from history.ts, the first of them). Each finding is an info diagnostic with code `kieler-quirk`:
|
|
3
|
+
// the guide's simulator reproduces the behaviour (it falls out of KIELER's transformations), and the diagnostic
|
|
4
|
+
// tells the reader where a chart can show it and how to avoid it. Only patterns that can actually make a
|
|
5
|
+
// difference are reported.
|
|
6
|
+
import { containedActions } from "./contained.js";
|
|
7
|
+
import { actionsOf, allStates, CAction, canImmediateTerminate, controlflowRegionsContainStates, isImplicitlyImmediate, walkExpr, } from "../model.js";
|
|
8
|
+
export function preemptionQuirks(ctx) {
|
|
9
|
+
for (const state of allStates(ctx.root)) {
|
|
10
|
+
strongAbortWrittenInside(state, ctx);
|
|
11
|
+
exitRewritesTrigger(state, ctx);
|
|
12
|
+
weakExitOnSuperstate(state, ctx);
|
|
13
|
+
suspendQuirks(state, ctx);
|
|
14
|
+
for (const t of state.out)
|
|
15
|
+
immediateCountDelay(t, ctx);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const name = (v) => v.display ?? v.name;
|
|
19
|
+
function reads(e) {
|
|
20
|
+
const out = new Set();
|
|
21
|
+
walkExpr(e, x => { if (x.k === 'ref')
|
|
22
|
+
out.add(x.v); });
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
function writesOf(effects) {
|
|
26
|
+
const out = new Set();
|
|
27
|
+
for (const f of effects)
|
|
28
|
+
if (f.k === 'assign' || f.k === 'emit')
|
|
29
|
+
out.add(f.ref.v);
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
/** Every variable the inside of a state writes (its regions' transitions and actions, its own during actions). */
|
|
33
|
+
function insideWrites(state) {
|
|
34
|
+
const out = new Set();
|
|
35
|
+
for (const a of containedActions(state)) {
|
|
36
|
+
if (a instanceof CAction) {
|
|
37
|
+
// The state's own entry and exit actions are not inner behaviour.
|
|
38
|
+
if (state.actions.includes(a) && a.kind !== 'during')
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
else if (a.source === state) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
for (const v of writesOf(a.effects))
|
|
45
|
+
out.add(v);
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
function report(ctx, d) {
|
|
50
|
+
ctx.report({ severity: 'info', code: 'kieler-quirk', ...d });
|
|
51
|
+
}
|
|
52
|
+
const ids = (...xs) => xs.filter((x) => !!x);
|
|
53
|
+
/** P-20: a strong abort whose trigger the aborted state writes is checked region by region. */
|
|
54
|
+
function strongAbortWrittenInside(state, ctx) {
|
|
55
|
+
const strong = state.out.filter(t => t.preemption === 'strong');
|
|
56
|
+
if (!strong.length || !controlflowRegionsContainStates(state))
|
|
57
|
+
return;
|
|
58
|
+
// With one inner region this is the normal timing of a strong abort (it reads the value from before the inside
|
|
59
|
+
// reacted, P-19); the quirk needs several concurrent inner regions (during actions are regions too).
|
|
60
|
+
const threads = state.regions.filter(r => !r.dataflow && r.states.length > 0).length + actionsOf(state, 'during').length;
|
|
61
|
+
if (threads < 2)
|
|
62
|
+
return;
|
|
63
|
+
const written = insideWrites(state);
|
|
64
|
+
for (const t of strong) {
|
|
65
|
+
const shared = [...reads(t.trigger)].filter(v => written.has(v));
|
|
66
|
+
if (!shared.length)
|
|
67
|
+
continue;
|
|
68
|
+
const v = name(shared[0]);
|
|
69
|
+
report(ctx, {
|
|
70
|
+
span: t.span, related: ids(t.origin, state.origin),
|
|
71
|
+
message: `This strong abort reads \`${v}\`, which the inside of \`${state.name}\` writes: in a tick in which \`${v}\` changes, some inner regions can be aborted while others still react`,
|
|
72
|
+
hint: `KIELER checks a strong abort separately in each inner region, before that region reacts, so a region that writes \`${v}\` has already read the old value. Use a weak abort (\`go to\`) or a termination (\`join to\`) for conditions computed inside \`${state.name}\`.`,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** P-23: exit actions that change what the state's outgoing triggers read can change the transition taken. */
|
|
77
|
+
function exitRewritesTrigger(state, ctx) {
|
|
78
|
+
if (state.out.length < 2 || state.root)
|
|
79
|
+
return;
|
|
80
|
+
const exits = [state, ...allStates(state, false)].flatMap(s => actionsOf(s, 'exit'));
|
|
81
|
+
if (!exits.length)
|
|
82
|
+
return;
|
|
83
|
+
const written = writesOf(exits.flatMap(a => a.effects));
|
|
84
|
+
for (const t of state.out) {
|
|
85
|
+
const shared = [...reads(t.trigger)].filter(v => written.has(v));
|
|
86
|
+
if (!shared.length)
|
|
87
|
+
continue;
|
|
88
|
+
const v = name(shared[0]);
|
|
89
|
+
const superstate = controlflowRegionsContainStates(state) || actionsOf(state, 'during').length > 0;
|
|
90
|
+
report(ctx, {
|
|
91
|
+
span: t.span, related: ids(t.origin, state.origin, exits[0].origin),
|
|
92
|
+
message: superstate
|
|
93
|
+
? `An exit action writes ${v}, which this transition's trigger reads: KIELER may decide which transition leaves ${state.name} only after the exit actions ran`
|
|
94
|
+
: `An exit action writes ${v}, which this transition's trigger reads`,
|
|
95
|
+
hint: superstate
|
|
96
|
+
? `When a superstate is left, KIELER runs the exit actions first and then picks the transition again: an abort whose trigger the exits made false is skipped, and the last transition is taken without checking its trigger. Let exit actions write only what the outgoing triggers do not read.`
|
|
97
|
+
: `For a simple state KIELER chooses the transition before the exit actions run, so the exit action does not change which transition is taken; it is still clearer to keep them apart.`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** P-12: `weak exit` on a superstate runs only when it terminates (join to), not on aborts. */
|
|
102
|
+
function weakExitOnSuperstate(state, ctx) {
|
|
103
|
+
if (!controlflowRegionsContainStates(state))
|
|
104
|
+
return;
|
|
105
|
+
const weak = actionsOf(state, 'exit').filter(a => a.preemption === 'weak');
|
|
106
|
+
if (!weak.length || !state.out.some(t => t.preemption !== 'termination'))
|
|
107
|
+
return;
|
|
108
|
+
report(ctx, {
|
|
109
|
+
span: weak[0].span, related: ids(weak[0].origin, state.origin),
|
|
110
|
+
message: `This weak exit only runs when \`${state.name}\` finishes through \`join to\`, not when it is left by \`go to\` or \`abort to\``,
|
|
111
|
+
hint: 'On a superstate, KIELER expands weak exit actions before the aborts, so both kinds of abort bypass them. Use a plain exit action to run something however the state is left.',
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/** P-30: an immediate count delay can complete at once on re-entry (pre(counter) is last visit's count). */
|
|
115
|
+
function immediateCountDelay(t, ctx) {
|
|
116
|
+
if (t.triggerDelay === undefined || t.triggerDelay <= 1 || !isImplicitlyImmediate(t))
|
|
117
|
+
return;
|
|
118
|
+
const s = t.source;
|
|
119
|
+
// Only a state that can be entered again.
|
|
120
|
+
if (!s.incoming.length && !(s.initial && !s.parent?.parent.root))
|
|
121
|
+
return;
|
|
122
|
+
report(ctx, {
|
|
123
|
+
span: t.span, related: ids(t.origin, s.origin),
|
|
124
|
+
message: `KIELER's immediate count can complete as soon as \`${s.name}\` is entered again, if \`${s.name}\` was left one count short, however long ago`,
|
|
125
|
+
hint: `KIELER implements \`immediate if ${t.triggerDelay} ...\` with a counter that restarts on entry, but compares the counter's previous value, which is still the old visit's in the tick after a re-entry. A delayed count (without immediate) does not have this problem.`,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/** P-38, P-42, P-43, P-44: what a `suspend` changes beyond freezing the inside. */
|
|
129
|
+
function suspendQuirks(state, ctx) {
|
|
130
|
+
const suspends = state.actions.filter(a => a.kind === 'suspend' && !a.weak);
|
|
131
|
+
if (!suspends.length)
|
|
132
|
+
return;
|
|
133
|
+
const s0 = suspends[0];
|
|
134
|
+
const entries = actionsOf(state, 'entry');
|
|
135
|
+
if (entries.length) {
|
|
136
|
+
report(ctx, {
|
|
137
|
+
span: entries[0].span, related: ids(entries[0].origin, s0.origin, state.origin),
|
|
138
|
+
message: state.root
|
|
139
|
+
? 'Because the chart has a suspend, KIELER never runs its entry actions'
|
|
140
|
+
: `Because \`${state.name}\` has a suspend, KIELER skips its entry actions when \`${state.name}\` is entered for the first time`,
|
|
141
|
+
hint: `KIELER guards every action of a suspendable state with a flag that its during actions compute, and they run after the entry actions; before the first tick the flag is false. Later entries run them only if \`${state.name}\` was not suspended when it was last left. Put the set-up into the transition that enters \`${state.name}\`, or into an inner state.`,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const exits = actionsOf(state, 'exit');
|
|
145
|
+
if (exits.length && !state.root) {
|
|
146
|
+
report(ctx, {
|
|
147
|
+
span: exits[0].span, related: ids(exits[0].origin, s0.origin, state.origin),
|
|
148
|
+
message: `If an enclosing state is left while \`${state.name}\` is suspended, \`${state.name}\`'s exit actions do not run`,
|
|
149
|
+
hint: 'The suspend flag guards the exit actions as well.',
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
const own = state.out.filter(t => t.preemption !== 'termination');
|
|
153
|
+
if (own.length) {
|
|
154
|
+
report(ctx, {
|
|
155
|
+
span: s0.span, related: ids(s0.origin, state.origin, ...own.map(t => t.origin)),
|
|
156
|
+
message: `While \`${state.name}\` is suspended, its own outgoing transitions cannot fire either`,
|
|
157
|
+
hint: `KIELER's suspend freezes everything that belongs to \`${state.name}\`, including the transitions that leave it. To leave a suspended state, put the transition on an enclosing state.`,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
const join = state.out.find(t => t.preemption === 'termination' && t.trigger === undefined && t.delay !== 'delayed');
|
|
161
|
+
if (join && canImmediateTerminate(state)) {
|
|
162
|
+
report(ctx, {
|
|
163
|
+
span: join.span, related: ids(join.origin, s0.origin, state.origin),
|
|
164
|
+
message: `Because \`${state.name}\` has a suspend, this \`join to\` waits one tick longer when the inside finishes in \`${state.name}\`'s first tick`,
|
|
165
|
+
hint: 'The suspend flag becomes part of the termination\'s trigger, and a termination with a trigger is delayed.',
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=quirks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quirks.js","sourceRoot":"","sources":["../../../src/sim/passes/quirks.ts"],"names":[],"mappings":"AAAA,8GAA8G;AAC9G,gHAAgH;AAChH,gHAAgH;AAChH,yGAAyG;AACzG,2BAA2B;AAI3B,OAAO,EAAE,gBAAgB,EAAE,uBAAmB;AAC9C,OAAO,EACH,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,qBAAqB,EAAE,+BAA+B,EAAE,qBAAqB,EAAE,QAAQ,GAEzH,oBAAgB;AAEjB,MAAM,UAAU,gBAAgB,CAAC,GAAgB;IAC7C,KAAK,MAAM,KAAK,IAAI,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QACtC,wBAAwB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QACpC,mBAAmB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QAC/B,oBAAoB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QAChC,aAAa,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QACzB,KAAK,MAAM,CAAC,IAAI,KAAK,CAAC,GAAG;YAAE,mBAAmB,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;IAC1D,CAAC;AACL,CAAC;AAED,MAAM,IAAI,GAAG,CAAC,CAAO,EAA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GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;YAC/E,OAAO,EAAE,WAAW,KAAK,CAAC,IAAI,kEAAkE;YAChG,IAAI,EAAE,yDAAyD,KAAK,CAAC,IAAI,oHAAoH;SAChM,CAAC,CAAA;IACN,CAAC;IACD,MAAM,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,KAAK,aAAa,IAAI,CAAC,CAAC,OAAO,KAAK,SAAS,IAAI,CAAC,CAAC,KAAK,KAAK,SAAS,CAAC,CAAA;IACpH,IAAI,IAAI,IAAI,qBAAqB,CAAC,KAAK,CAAC,EAAE,CAAC;QACvC,MAAM,CAAC,GAAG,EAAE;YACR,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC;YACnE,OAAO,EAAE,aAAa,KAAK,CAAC,IAAI,0FAA0F,KAAK,CAAC,IAAI,iBAAiB;YACrJ,IAAI,EAAE,2GAA2G;SACpH,CAAC,CAAA;IACN,CAAC;AACL,CAAC","sourcesContent":["// Static checks for KIELER's preemption quirks (preemption.md), run on the chart before the preemption passes\n// rewrite it (from history.ts, the first of them). Each finding is an info diagnostic with code `kieler-quirk`:\n// the guide's simulator reproduces the behaviour (it falls out of KIELER's transformations), and the diagnostic\n// tells the reader where a chart can show it and how to avoid it. Only patterns that can actually make a\n// difference are reported.\n\nimport type { Diagnostic } from '../../types'\nimport type { PassContext } from './context'\nimport { containedActions } from './contained'\nimport {\n actionsOf, allStates, CAction, canImmediateTerminate, controlflowRegionsContainStates, isImplicitlyImmediate, walkExpr, type CEffect,\n type CExpr, type CState, type CTransition, type CVar,\n} from '../model'\n\nexport function preemptionQuirks(ctx: PassContext) {\n for (const state of allStates(ctx.root)) {\n strongAbortWrittenInside(state, ctx)\n exitRewritesTrigger(state, ctx)\n weakExitOnSuperstate(state, ctx)\n suspendQuirks(state, ctx)\n for (const t of state.out) immediateCountDelay(t, ctx)\n }\n}\n\nconst name = (v: CVar) => v.display ?? v.name\n\nfunction reads(e: CExpr | undefined): Set<CVar> {\n const out = new Set<CVar>()\n walkExpr(e, x => { if (x.k === 'ref') out.add(x.v) })\n return out\n}\n\nfunction writesOf(effects: CEffect[]): Set<CVar> {\n const out = new Set<CVar>()\n for (const f of effects) if (f.k === 'assign' || f.k === 'emit') out.add(f.ref.v)\n return out\n}\n\n/** Every variable the inside of a state writes (its regions' transitions and actions, its own during actions). */\nfunction insideWrites(state: CState): Set<CVar> {\n const out = new Set<CVar>()\n for (const a of containedActions(state)) {\n if (a instanceof CAction) {\n // The state's own entry and exit actions are not inner behaviour.\n if (state.actions.includes(a) && a.kind !== 'during') continue\n } else if (a.source === state) {\n continue\n }\n for (const v of writesOf(a.effects)) out.add(v)\n }\n return out\n}\n\nfunction report(ctx: PassContext, d: Omit<Diagnostic, 'severity' | 'code'>) {\n ctx.report({ severity: 'info', code: 'kieler-quirk', ...d })\n}\n\nconst ids = (...xs: (string | undefined)[]) => xs.filter((x): x is string => !!x)\n\n/** P-20: a strong abort whose trigger the aborted state writes is checked region by region. */\nfunction strongAbortWrittenInside(state: CState, ctx: PassContext) {\n const strong = state.out.filter(t => t.preemption === 'strong')\n if (!strong.length || !controlflowRegionsContainStates(state)) return\n // With one inner region this is the normal timing of a strong abort (it reads the value from before the inside\n // reacted, P-19); the quirk needs several concurrent inner regions (during actions are regions too).\n const threads = state.regions.filter(r => !r.dataflow && r.states.length > 0).length + actionsOf(state, 'during').length\n if (threads < 2) return\n const written = insideWrites(state)\n for (const t of strong) {\n const shared = [...reads(t.trigger)].filter(v => written.has(v))\n if (!shared.length) continue\n const v = name(shared[0])\n report(ctx, {\n span: t.span, related: ids(t.origin, state.origin),\n message: `This strong abort reads \\`${v}\\`, which the inside of \\`${state.name}\\` writes: in a tick in which \\`${v}\\` changes, some inner regions can be aborted while others still react`,\n hint: `KIELER checks a strong abort separately in each inner region, before that region reacts, so a region that writes \\`${v}\\` has already read the old value. Use a weak abort (\\`go to\\`) or a termination (\\`join to\\`) for conditions computed inside \\`${state.name}\\`.`,\n })\n }\n}\n\n/** P-23: exit actions that change what the state's outgoing triggers read can change the transition taken. */\nfunction exitRewritesTrigger(state: CState, ctx: PassContext) {\n if (state.out.length < 2 || state.root) return\n const exits = [state, ...allStates(state, false)].flatMap(s => actionsOf(s, 'exit'))\n if (!exits.length) return\n const written = writesOf(exits.flatMap(a => a.effects))\n for (const t of state.out) {\n const shared = [...reads(t.trigger)].filter(v => written.has(v))\n if (!shared.length) continue\n const v = name(shared[0])\n const superstate = controlflowRegionsContainStates(state) || actionsOf(state, 'during').length > 0\n report(ctx, {\n span: t.span, related: ids(t.origin, state.origin, exits[0].origin),\n message: superstate\n ? `An exit action writes ${v}, which this transition's trigger reads: KIELER may decide which transition leaves ${state.name} only after the exit actions ran`\n : `An exit action writes ${v}, which this transition's trigger reads`,\n hint: superstate\n ? `When a superstate is left, KIELER runs the exit actions first and then picks the transition again: an abort whose trigger the exits made false is skipped, and the last transition is taken without checking its trigger. Let exit actions write only what the outgoing triggers do not read.`\n : `For a simple state KIELER chooses the transition before the exit actions run, so the exit action does not change which transition is taken; it is still clearer to keep them apart.`,\n })\n }\n}\n\n/** P-12: `weak exit` on a superstate runs only when it terminates (join to), not on aborts. */\nfunction weakExitOnSuperstate(state: CState, ctx: PassContext) {\n if (!controlflowRegionsContainStates(state)) return\n const weak = actionsOf(state, 'exit').filter(a => a.preemption === 'weak')\n if (!weak.length || !state.out.some(t => t.preemption !== 'termination')) return\n report(ctx, {\n span: weak[0].span, related: ids(weak[0].origin, state.origin),\n message: `This weak exit only runs when \\`${state.name}\\` finishes through \\`join to\\`, not when it is left by \\`go to\\` or \\`abort to\\``,\n hint: 'On a superstate, KIELER expands weak exit actions before the aborts, so both kinds of abort bypass them. Use a plain exit action to run something however the state is left.',\n })\n}\n\n/** P-30: an immediate count delay can complete at once on re-entry (pre(counter) is last visit's count). */\nfunction immediateCountDelay(t: CTransition, ctx: PassContext) {\n if (t.triggerDelay === undefined || t.triggerDelay <= 1 || !isImplicitlyImmediate(t)) return\n const s = t.source\n // Only a state that can be entered again.\n if (!s.incoming.length && !(s.initial && !s.parent?.parent.root)) return\n report(ctx, {\n span: t.span, related: ids(t.origin, s.origin),\n message: `KIELER's immediate count can complete as soon as \\`${s.name}\\` is entered again, if \\`${s.name}\\` was left one count short, however long ago`,\n hint: `KIELER implements \\`immediate if ${t.triggerDelay} ...\\` with a counter that restarts on entry, but compares the counter's previous value, which is still the old visit's in the tick after a re-entry. A delayed count (without immediate) does not have this problem.`,\n })\n}\n\n/** P-38, P-42, P-43, P-44: what a `suspend` changes beyond freezing the inside. */\nfunction suspendQuirks(state: CState, ctx: PassContext) {\n const suspends = state.actions.filter(a => a.kind === 'suspend' && !a.weak)\n if (!suspends.length) return\n const s0 = suspends[0]\n const entries = actionsOf(state, 'entry')\n if (entries.length) {\n report(ctx, {\n span: entries[0].span, related: ids(entries[0].origin, s0.origin, state.origin),\n message: state.root\n ? 'Because the chart has a suspend, KIELER never runs its entry actions'\n : `Because \\`${state.name}\\` has a suspend, KIELER skips its entry actions when \\`${state.name}\\` is entered for the first time`,\n hint: `KIELER guards every action of a suspendable state with a flag that its during actions compute, and they run after the entry actions; before the first tick the flag is false. Later entries run them only if \\`${state.name}\\` was not suspended when it was last left. Put the set-up into the transition that enters \\`${state.name}\\`, or into an inner state.`,\n })\n }\n const exits = actionsOf(state, 'exit')\n if (exits.length && !state.root) {\n report(ctx, {\n span: exits[0].span, related: ids(exits[0].origin, s0.origin, state.origin),\n message: `If an enclosing state is left while \\`${state.name}\\` is suspended, \\`${state.name}\\`'s exit actions do not run`,\n hint: 'The suspend flag guards the exit actions as well.',\n })\n }\n const own = state.out.filter(t => t.preemption !== 'termination')\n if (own.length) {\n report(ctx, {\n span: s0.span, related: ids(s0.origin, state.origin, ...own.map(t => t.origin)),\n message: `While \\`${state.name}\\` is suspended, its own outgoing transitions cannot fire either`,\n hint: `KIELER's suspend freezes everything that belongs to \\`${state.name}\\`, including the transitions that leave it. To leave a suspended state, put the transition on an enclosing state.`,\n })\n }\n const join = state.out.find(t => t.preemption === 'termination' && t.trigger === undefined && t.delay !== 'delayed')\n if (join && canImmediateTerminate(state)) {\n report(ctx, {\n span: join.span, related: ids(join.origin, s0.origin, state.origin),\n message: `Because \\`${state.name}\\` has a suspend, this \\`join to\\` waits one tick longer when the inside finishes in \\`${state.name}\\`'s first tick`,\n hint: 'The suspend flag becomes part of the termination\\'s trigger, and a termination with a trigger is delayed.',\n })\n }\n}\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Span, State } from "../../types.js";
|
|
2
|
+
import type { ModelIndex } from "../../parser/resolve.js";
|
|
3
|
+
import type { Pass, PassContext } from "./context.js";
|
|
4
|
+
import { type CArg, type CState, type CVar, type Scope } from "../model.js";
|
|
5
|
+
export declare const reference: Pass;
|
|
6
|
+
/** What the tracker needs to know about an instance (structure/track.ts). */
|
|
7
|
+
export interface InstanceRecord {
|
|
8
|
+
/** Id of the referencing state (a prefixed id inside other instances), or of the state declaring `ref`. */
|
|
9
|
+
state: string;
|
|
10
|
+
prefix: string;
|
|
11
|
+
chart: State;
|
|
12
|
+
/** A `ref` declaration's instance (its regions join the declaring state's), not a referencing state. */
|
|
13
|
+
declaration?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** A reference to expand: a state's `is C(args)`, or one the Dataflow processor made. */
|
|
16
|
+
export interface ReferenceCall {
|
|
17
|
+
target: string;
|
|
18
|
+
args: CArg[];
|
|
19
|
+
span?: Span;
|
|
20
|
+
super?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare class Expansion {
|
|
23
|
+
readonly ctx: PassContext;
|
|
24
|
+
readonly index: ModelIndex | undefined;
|
|
25
|
+
/** Charts being expanded, outermost first (the simulated chart, then each chart a copy is being made of). */
|
|
26
|
+
private readonly stack;
|
|
27
|
+
readonly records: InstanceRecord[];
|
|
28
|
+
/** Formals bound to an expression KIELER cannot substitute (reported where they are used). */
|
|
29
|
+
private readonly badBindings;
|
|
30
|
+
private readonly reportedBad;
|
|
31
|
+
constructor(ctx: PassContext);
|
|
32
|
+
run(): void;
|
|
33
|
+
private expandRoot;
|
|
34
|
+
findChart(name: string): State | undefined;
|
|
35
|
+
/** Is the chart written in the simulated file (are its spans this file's)? */
|
|
36
|
+
private local;
|
|
37
|
+
/** KIELER's valuedObjectNameMap: the innermost declaration of a name, from a scope up to the root. */
|
|
38
|
+
lookup(from: Scope, name: string): CVar | undefined;
|
|
39
|
+
/** Names declared around a state (its region and every enclosing scope): KIELER's getAllReservedVariableNames. */
|
|
40
|
+
private reserved;
|
|
41
|
+
/**
|
|
42
|
+
* A fresh copy of the chart with its bindings worked out: the formals (its inputs and outputs) receive the
|
|
43
|
+
* arguments by KIELER's binding pointer and, if unbound, the variable of the same name visible from `scope`
|
|
44
|
+
* (KIELER's createBindings; the front end reports binding errors).
|
|
45
|
+
*/
|
|
46
|
+
private instantiate;
|
|
47
|
+
/** Substitutes the bound formals everywhere in the copy and removes the interface declarations (S-9). */
|
|
48
|
+
private bind;
|
|
49
|
+
/** Expands `state S is C(args)` (KIELER's expandReferencedScope for states). */
|
|
50
|
+
expandState(s: CState): void;
|
|
51
|
+
/** `ref C g(args)` declarations not used by dataflow (KIELER's handleSCChartsDatatype, S-18). */
|
|
52
|
+
private expandDeclarations;
|
|
53
|
+
/**
|
|
54
|
+
* A chart used as a class (structure.md S-65): `c.step` and `c.increment()` in the chart that declares `ref C c`
|
|
55
|
+
* mean the instance's own field and method (KIELER's handleSCChartsDatatype).
|
|
56
|
+
*/
|
|
57
|
+
private redirectMembers;
|
|
58
|
+
private recursive;
|
|
59
|
+
}
|
|
60
|
+
export declare function isReferencing(s: CState): boolean;
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
// KIELER's Reference processor (sccharts/processors/Reference.xtend), with the Dataflow processor it calls
|
|
2
|
+
// (passes/dataflow.ts). Owner: the structure workstream. structure.md S-4 .. S-18, S-33, S-34.
|
|
3
|
+
//
|
|
4
|
+
// `state S is C(args)` is macro expansion: a fresh copy of the chart C (lowered from its AST with every id
|
|
5
|
+
// prefixed by `S/`, structure/instance.ts) replaces S. Its interface (C's inputs and outputs, in declaration
|
|
6
|
+
// order) is bound to the arguments by KIELER's binding pointer (explicit `x to y` moves it), the rest by name;
|
|
7
|
+
// binding is substitution: inside the copy every formal is the actual (a variable or an array cell), and a literal
|
|
8
|
+
// input becomes a local initialised with the literal. Output initial values become entry actions of S, after C's
|
|
9
|
+
// own root actions. Copied locals whose names are visible around S are renamed `_x_` (`_x_2`, ...). The copy
|
|
10
|
+
// takes S's place at the end of its region (EMF's add, then remove), with S's name, flags and transitions.
|
|
11
|
+
//
|
|
12
|
+
// `ref C g(args)` outside dataflow copies C's regions into the declaring state as `g_<region>`, and its actions
|
|
13
|
+
// after the state's own (handleSCChartsDatatype; its locals become hidden members). In dataflow regions, instances
|
|
14
|
+
// are reference states the Dataflow processor creates.
|
|
15
|
+
//
|
|
16
|
+
// Order (expandRoot): references of states (in each copy: its own references, then its dataflow regions and the
|
|
17
|
+
// references these create), then the chart's dataflow regions, then `ref` declarations, then the references the
|
|
18
|
+
// dataflow regions created.
|
|
19
|
+
import { construct } from "./detect.js";
|
|
20
|
+
import { addVar, allContainedStates, allRegions, allScopes, allStates, copyExpr, createEntryAction, isRegion, ref, sameExpr, setSource, setTarget, } from "../model.js";
|
|
21
|
+
import { lowerInstance, REFERENCE_SEPARATOR } from "../structure/instance.js";
|
|
22
|
+
import { declaredVars, rewriteScope } from "../structure/rewrite.js";
|
|
23
|
+
import { processDataflow } from "./dataflow.js";
|
|
24
|
+
import { inherit } from "./inheritance.js";
|
|
25
|
+
export const reference = {
|
|
26
|
+
id: 'de.cau.cs.kieler.sccharts.processors.reference',
|
|
27
|
+
name: 'Reference',
|
|
28
|
+
owner: 'structure',
|
|
29
|
+
run(ctx) {
|
|
30
|
+
new Expansion(ctx).run();
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
class Recursion extends Error {
|
|
34
|
+
}
|
|
35
|
+
/** KIELER names the expression class it cannot substitute (S-8). */
|
|
36
|
+
const CLASS_NAMES = {
|
|
37
|
+
op: 'de.cau.cs.kieler.kexpressions.impl.OperatorExpressionImpl',
|
|
38
|
+
random: 'de.cau.cs.kieler.kexpressions.impl.RandomCallImpl',
|
|
39
|
+
host: 'de.cau.cs.kieler.kexpressions.impl.TextExpressionImpl',
|
|
40
|
+
call: 'de.cau.cs.kieler.kexpressions.impl.ReferenceCallImpl',
|
|
41
|
+
};
|
|
42
|
+
export class Expansion {
|
|
43
|
+
ctx;
|
|
44
|
+
index;
|
|
45
|
+
/** Charts being expanded, outermost first (the simulated chart, then each chart a copy is being made of). */
|
|
46
|
+
stack = [];
|
|
47
|
+
records = [];
|
|
48
|
+
/** Formals bound to an expression KIELER cannot substitute (reported where they are used). */
|
|
49
|
+
badBindings = new Map();
|
|
50
|
+
reportedBad = new Set();
|
|
51
|
+
constructor(ctx) {
|
|
52
|
+
this.ctx = ctx;
|
|
53
|
+
this.index = ctx.shared.index;
|
|
54
|
+
ctx.shared.instances = this.records;
|
|
55
|
+
}
|
|
56
|
+
run() {
|
|
57
|
+
const root = this.ctx.root;
|
|
58
|
+
this.stack.push(root.name);
|
|
59
|
+
try {
|
|
60
|
+
this.expandRoot(root);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
if (!(e instanceof Recursion))
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
expandRoot(root, chart) {
|
|
68
|
+
// Inheritance first (KIELER's expandRoot: inheritBaseStates), for the chart itself.
|
|
69
|
+
const source = chart ?? (root.origin ? this.index?.states.get(root.origin) : undefined);
|
|
70
|
+
if (root.ext.baseStates && source)
|
|
71
|
+
inherit(this.ctx, root, source, name => this.findChart(name));
|
|
72
|
+
const unsupported = [
|
|
73
|
+
...allStates(root).filter(s => s.ext.baseStates !== undefined).map(s => construct('inheritance (extends) of inner states', undefined, s.origin)),
|
|
74
|
+
...allRegions(root).filter(r => r.ext.reference !== undefined).map(r => construct('region references (is ...) outside an override region', undefined, r.origin)),
|
|
75
|
+
];
|
|
76
|
+
if (unsupported.length) {
|
|
77
|
+
this.ctx.report({
|
|
78
|
+
severity: 'info', code: 'unsupported', related: unsupported.flatMap(u => u.related ?? []),
|
|
79
|
+
message: `The guide's simulator cannot run ${unsupported[0].what} yet`,
|
|
80
|
+
hint: 'This needs more of KIELER\'s Inheritance transformation than the simulator implements. The official KIELER tools can run this chart.',
|
|
81
|
+
});
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
for (const s of allContainedStates(root).filter(isReferencing))
|
|
85
|
+
this.expandState(s);
|
|
86
|
+
processDataflow(root, this);
|
|
87
|
+
this.expandDeclarations(root);
|
|
88
|
+
for (const s of allContainedStates(root).filter(isReferencing))
|
|
89
|
+
this.expandState(s);
|
|
90
|
+
}
|
|
91
|
+
// ----- names and charts -----
|
|
92
|
+
findChart(name) {
|
|
93
|
+
return this.index?.charts.find(c => c.name === name);
|
|
94
|
+
}
|
|
95
|
+
/** Is the chart written in the simulated file (are its spans this file's)? */
|
|
96
|
+
local(chart) {
|
|
97
|
+
return !this.index || !!this.index.programs[0]?.charts.includes(chart);
|
|
98
|
+
}
|
|
99
|
+
/** KIELER's valuedObjectNameMap: the innermost declaration of a name, from a scope up to the root. */
|
|
100
|
+
lookup(from, name) {
|
|
101
|
+
for (let s = from; s; s = isRegion(s) ? s.parent : s.parent) {
|
|
102
|
+
const v = s.decls.find(x => x.name === name);
|
|
103
|
+
if (v)
|
|
104
|
+
return v;
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
/** Names declared around a state (its region and every enclosing scope): KIELER's getAllReservedVariableNames. */
|
|
109
|
+
reserved(s) {
|
|
110
|
+
const out = new Set();
|
|
111
|
+
for (let x = s.parent; x; x = isRegion(x) ? x.parent : x.parent)
|
|
112
|
+
for (const v of x.decls)
|
|
113
|
+
out.add(v.name);
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
// ----- one instance -----
|
|
117
|
+
/**
|
|
118
|
+
* A fresh copy of the chart with its bindings worked out: the formals (its inputs and outputs) receive the
|
|
119
|
+
* arguments by KIELER's binding pointer and, if unbound, the variable of the same name visible from `scope`
|
|
120
|
+
* (KIELER's createBindings; the front end reports binding errors).
|
|
121
|
+
*/
|
|
122
|
+
instantiate(chart, prefix, call, scope, owner, outputInits) {
|
|
123
|
+
if (this.stack.includes(chart.name)) {
|
|
124
|
+
this.recursive(chart.name, owner, call.span);
|
|
125
|
+
throw new Recursion();
|
|
126
|
+
}
|
|
127
|
+
const lowered = lowerInstance(chart, this.index, prefix, this.local(chart) ? undefined : call.span);
|
|
128
|
+
for (const d of lowered.reports)
|
|
129
|
+
this.ctx.report(d);
|
|
130
|
+
const inst = lowered.root;
|
|
131
|
+
// The chart's own inputs and outputs, then the inherited ones (KIELER's binding targets).
|
|
132
|
+
const ownIO = inst.decls.filter(v => (v.input || v.output) && !v.declKind);
|
|
133
|
+
if (inst.ext.baseStates)
|
|
134
|
+
inherit(this.ctx, inst, chart, name => this.findChart(name));
|
|
135
|
+
const formals = [...ownIO, ...inst.decls.filter(v => (v.input || v.output) && !v.declKind && !ownIO.includes(v))];
|
|
136
|
+
const bound = new Map();
|
|
137
|
+
let ptr = 0;
|
|
138
|
+
for (const a of call.args) {
|
|
139
|
+
if (a.binding !== undefined) {
|
|
140
|
+
const i = formals.findIndex(f => f.name === a.binding);
|
|
141
|
+
if (i < 0)
|
|
142
|
+
continue;
|
|
143
|
+
ptr = i;
|
|
144
|
+
}
|
|
145
|
+
if (ptr >= formals.length)
|
|
146
|
+
continue;
|
|
147
|
+
bound.set(formals[ptr], a.expr);
|
|
148
|
+
ptr++;
|
|
149
|
+
}
|
|
150
|
+
for (const f of formals) {
|
|
151
|
+
if (bound.has(f))
|
|
152
|
+
continue;
|
|
153
|
+
const v = this.lookup(scope, f.name);
|
|
154
|
+
if (v)
|
|
155
|
+
bound.set(f, { k: 'ref', v, idx: [] });
|
|
156
|
+
}
|
|
157
|
+
const map = new Map();
|
|
158
|
+
for (const [f, actual] of bound) {
|
|
159
|
+
if (actual.k === 'ref' && !actual.sub)
|
|
160
|
+
map.set(f, actual);
|
|
161
|
+
else if (actual.k === 'lit' || actual.k === 'vector' || actual.k === 'null') {
|
|
162
|
+
// replaceReferenceWithLiteral: the formal becomes a local with the literal as its initial value.
|
|
163
|
+
f.input = false;
|
|
164
|
+
f.output = false;
|
|
165
|
+
f.init = copyExpr(actual);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
this.badBindings.set(f, { actual, state: owner });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Output initial values become entry actions of the referencing state, after the chart's own (S-10).
|
|
172
|
+
for (const f of formals) {
|
|
173
|
+
const actual = map.get(f)?.v;
|
|
174
|
+
// Only when the two initial values differ: with equal ones "not its own" would only be noise.
|
|
175
|
+
if (f.output && f.init && outputInits && actual?.init && !sameExpr(f.init, actual.init)) {
|
|
176
|
+
this.ctx.report({
|
|
177
|
+
severity: 'info', code: 'kieler-quirk', span: call.span, related: [owner],
|
|
178
|
+
message: `\`${actual.display ?? actual.name}\` gets the initial value of \`${chart.name}\`'s output \`${f.name}\` whenever \`${owner.split(/[.:/]/).pop()}\` is entered, not its own`,
|
|
179
|
+
hint: `KIELER turns the referenced chart's output initial values into entry actions of the referencing state, so they win over the initial value declared here and are applied again on every entry.`,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
if (f.output && f.init && outputInits) {
|
|
183
|
+
const entry = createEntryAction(inst);
|
|
184
|
+
entry.about = owner;
|
|
185
|
+
entry.effects.push({ k: 'assign', ref: ref(f), op: '=', expr: f.init, o: { by: f.decl, init: f.decl, span: f.span } });
|
|
186
|
+
}
|
|
187
|
+
// The declarations go, with their initial values (input initial values are dropped).
|
|
188
|
+
if (f.input || f.output)
|
|
189
|
+
f.init = undefined;
|
|
190
|
+
}
|
|
191
|
+
return { inst, formals, map };
|
|
192
|
+
}
|
|
193
|
+
/** Substitutes the bound formals everywhere in the copy and removes the interface declarations (S-9). */
|
|
194
|
+
bind({ inst, formals, map }) {
|
|
195
|
+
const replace = (r) => {
|
|
196
|
+
const actual = map.get(r.v);
|
|
197
|
+
if (actual) {
|
|
198
|
+
// KIELER's replaceReferences: the actual's variable, and its indices if the use has none.
|
|
199
|
+
return { ...r, v: actual.v, idx: r.idx.length ? r.idx : actual.idx.map(i => copyExpr(i)) };
|
|
200
|
+
}
|
|
201
|
+
const bad = this.badBindings.get(r.v);
|
|
202
|
+
if (bad && !this.reportedBad.has(r.v)) {
|
|
203
|
+
this.reportedBad.add(r.v);
|
|
204
|
+
const kind = CLASS_NAMES[bad.actual.k] ?? 'de.cau.cs.kieler.kexpressions.impl.ExpressionImpl';
|
|
205
|
+
this.ctx.report({
|
|
206
|
+
severity: 'error', code: 'binding-expression', span: bad.actual.span, related: [bad.state],
|
|
207
|
+
message: `\`${r.v.name}\` of the referenced chart is bound to an expression: only variables, array cells and values can be passed`,
|
|
208
|
+
kieler: `A binding for the valued object reference "${r.v.name}" exists, but the type "${kind}" is not supported.`,
|
|
209
|
+
hint: `Compute the value into a variable of this chart first (for example \`t = ...\`) and pass that variable as \`${r.v.name}\`.`,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return undefined;
|
|
213
|
+
};
|
|
214
|
+
rewriteScope(inst, { read: replace, target: replace });
|
|
215
|
+
inst.decls = inst.decls.filter(v => !formals.includes(v) || !(v.input || v.output));
|
|
216
|
+
}
|
|
217
|
+
/** Expands `state S is C(args)` (KIELER's expandReferencedScope for states). */
|
|
218
|
+
expandState(s) {
|
|
219
|
+
const call = referenceCall(s);
|
|
220
|
+
if (!call || call.super)
|
|
221
|
+
return;
|
|
222
|
+
if (s.ext.calls) {
|
|
223
|
+
// `calls` is not expanded (isModuleExpansionReference): KIELER's C code calls a function it never defines (S-19).
|
|
224
|
+
delete s.ext.reference;
|
|
225
|
+
this.ctx.report({
|
|
226
|
+
severity: 'error', code: 'module-call', span: call.span, related: s.origin ? [s.origin] : undefined,
|
|
227
|
+
message: `\`calls ${call.target}\` does not work in KIELER's C simulation`,
|
|
228
|
+
kieler: `implicit declaration of function \u2018_r${call.target}\u2019 [-Wimplicit-function-declaration]\nCompiler did not return success (exit value != 0)`,
|
|
229
|
+
hint: `Write \`state ${s.name} is ${call.target}(...)\` instead: is copies the chart into the state, calls generates a C function call that KIELER's netlist code never defines.`,
|
|
230
|
+
});
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const chart = this.findChart(call.target);
|
|
234
|
+
if (!chart)
|
|
235
|
+
return;
|
|
236
|
+
const id = s.origin ?? s.about ?? s.name;
|
|
237
|
+
const prefix = id + REFERENCE_SEPARATOR;
|
|
238
|
+
const instance = this.instantiate(chart, prefix, call, s, id, true);
|
|
239
|
+
const { inst, formals } = instance;
|
|
240
|
+
this.records.push({ state: id, prefix, chart });
|
|
241
|
+
// Name clashes with the referencing state's surroundings (resolveNameClashes, S-11).
|
|
242
|
+
const reserved = this.reserved(s);
|
|
243
|
+
for (const v of declaredVars(inst)) {
|
|
244
|
+
if (formals.includes(v) && (v.input || v.output))
|
|
245
|
+
continue;
|
|
246
|
+
if (!reserved.has(v.name))
|
|
247
|
+
continue;
|
|
248
|
+
const base = `_${v.name}_`;
|
|
249
|
+
let name = base;
|
|
250
|
+
for (let i = 2; reserved.has(name); i++)
|
|
251
|
+
name = base + i;
|
|
252
|
+
v.name = name;
|
|
253
|
+
}
|
|
254
|
+
this.bind(instance);
|
|
255
|
+
// The copy takes the referencing state's place: appended to the region, with its transitions.
|
|
256
|
+
const region = s.parent;
|
|
257
|
+
inst.name = s.name;
|
|
258
|
+
inst.label = s.label;
|
|
259
|
+
inst.initial = s.initial;
|
|
260
|
+
inst.final = s.final;
|
|
261
|
+
inst.origin = s.origin;
|
|
262
|
+
inst.about = s.about;
|
|
263
|
+
for (const [k, v] of s.annotations)
|
|
264
|
+
inst.annotations.set(k, v);
|
|
265
|
+
region.states.splice(region.states.indexOf(s), 1);
|
|
266
|
+
inst.parent = region;
|
|
267
|
+
region.states.push(inst);
|
|
268
|
+
for (const t of [...s.out])
|
|
269
|
+
setSource(t, inst);
|
|
270
|
+
for (const t of [...s.incoming])
|
|
271
|
+
setTarget(t, inst);
|
|
272
|
+
// Inside the copy: its references, its dataflow regions, and the references these create.
|
|
273
|
+
this.stack.push(chart.name);
|
|
274
|
+
try {
|
|
275
|
+
for (const x of allContainedStates(inst).filter(isReferencing))
|
|
276
|
+
this.expandState(x);
|
|
277
|
+
processDataflow(inst, this);
|
|
278
|
+
for (const x of allContainedStates(inst).filter(isReferencing))
|
|
279
|
+
this.expandState(x);
|
|
280
|
+
}
|
|
281
|
+
finally {
|
|
282
|
+
this.stack.pop();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/** `ref C g(args)` declarations not used by dataflow (KIELER's handleSCChartsDatatype, S-18). */
|
|
286
|
+
expandDeclarations(root) {
|
|
287
|
+
for (const scope of allScopes(root)) {
|
|
288
|
+
for (const v of [...scope.decls]) {
|
|
289
|
+
if (v.declKind !== 'reference' || !v.instance || v.input)
|
|
290
|
+
continue;
|
|
291
|
+
const chart = this.findChart(v.instance.target);
|
|
292
|
+
if (!chart)
|
|
293
|
+
continue;
|
|
294
|
+
const id = v.decl ?? v.name;
|
|
295
|
+
const prefix = id + REFERENCE_SEPARATOR;
|
|
296
|
+
const instance = this.instantiate(chart, prefix, { target: v.instance.target, args: v.instance.args, span: v.span }, scope, id, false);
|
|
297
|
+
const { inst } = instance;
|
|
298
|
+
this.stack.push(chart.name);
|
|
299
|
+
try {
|
|
300
|
+
this.expandRoot(inst, chart);
|
|
301
|
+
}
|
|
302
|
+
finally {
|
|
303
|
+
this.stack.pop();
|
|
304
|
+
}
|
|
305
|
+
this.bind(instance);
|
|
306
|
+
this.redirectMembers(v, prefix, inst);
|
|
307
|
+
const target = isRegion(scope) ? scope.parent : scope;
|
|
308
|
+
// The instance's locals become hidden members of the declaring state (in KIELER's data pool: members
|
|
309
|
+
// of an object named after the declaration, `"g": {"k": 0}`).
|
|
310
|
+
// A chart class instance without fields is still an (empty) object in KIELER's data pool (structure.md
|
|
311
|
+
// S-60, conformance structure/131).
|
|
312
|
+
if (target === this.ctx.root && !v.instance.args.length && !inst.decls.some(l => !l.declKind)) {
|
|
313
|
+
(this.ctx.root.ext.emptyInstances ??= []).push(v.name);
|
|
314
|
+
}
|
|
315
|
+
for (const local of [...inst.decls]) {
|
|
316
|
+
if (!local.declKind)
|
|
317
|
+
local.member = { of: v, name: local.name };
|
|
318
|
+
local.name = `_${v.name}_${local.name}`;
|
|
319
|
+
local.generated = true;
|
|
320
|
+
addVar(target, local);
|
|
321
|
+
}
|
|
322
|
+
for (const r of [...inst.regions]) {
|
|
323
|
+
r.name = `${v.name}_${r.name ?? 'default'}`;
|
|
324
|
+
r.parent = target;
|
|
325
|
+
target.regions.push(r);
|
|
326
|
+
}
|
|
327
|
+
target.actions.push(...inst.actions);
|
|
328
|
+
scope.decls.splice(scope.decls.indexOf(v), 1);
|
|
329
|
+
this.records.push({ state: target.origin ?? '', prefix, chart, declaration: true });
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* A chart used as a class (structure.md S-65): `c.step` and `c.increment()` in the chart that declares `ref C c`
|
|
335
|
+
* mean the instance's own field and method (KIELER's handleSCChartsDatatype).
|
|
336
|
+
*/
|
|
337
|
+
redirectMembers(v, prefix, inst) {
|
|
338
|
+
// By the declaration's own id (an inherited one is `<instance>/<chart>/<id>`: the last part).
|
|
339
|
+
const byDecl = new Map();
|
|
340
|
+
for (const local of inst.decls)
|
|
341
|
+
if (local.decl?.startsWith(prefix))
|
|
342
|
+
byDecl.set(local.decl.split(REFERENCE_SEPARATOR).pop(), local);
|
|
343
|
+
if (!byDecl.size)
|
|
344
|
+
return;
|
|
345
|
+
const member = (r) => {
|
|
346
|
+
if (r.v !== v || !r.sub)
|
|
347
|
+
return undefined;
|
|
348
|
+
const m = r.sub.v.decl ? byDecl.get(r.sub.v.decl) : undefined;
|
|
349
|
+
return m ? { ...r.sub, v: m, idx: [...r.idx, ...r.sub.idx] } : undefined;
|
|
350
|
+
};
|
|
351
|
+
rewriteScope(this.ctx.root, {
|
|
352
|
+
read: r => member(r),
|
|
353
|
+
target: r => member(r),
|
|
354
|
+
node: e => {
|
|
355
|
+
if (e.k !== 'call' || !e.target)
|
|
356
|
+
return undefined;
|
|
357
|
+
const t = member(e.target);
|
|
358
|
+
return t ? { ...e, target: t, name: t.v.name } : undefined;
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
recursive(chart, owner, span) {
|
|
363
|
+
const path = [...this.stack];
|
|
364
|
+
const cycle = path.slice(path.lastIndexOf(chart));
|
|
365
|
+
const chain = [...cycle, chart].join(' -> ');
|
|
366
|
+
const user = cycle[cycle.length - 1];
|
|
367
|
+
this.ctx.report({
|
|
368
|
+
severity: 'error', code: 'recursive-reference', span, related: [owner],
|
|
369
|
+
message: cycle.length === 1 ? `\`${chart}\` uses itself, so its copy would contain another copy for ever` : `\`${user}\` uses \`${chart}\`, which leads back to \`${user}\``,
|
|
370
|
+
kieler: `Recursive reference: chart ${cycle.length === 1 ? `${user} uses itself` : `\`${user}\` uses \`${chart}\`, which leads back to \`${user}\``} (${chain}).`,
|
|
371
|
+
hint: `A reference such as \`state S is ${chart}(...)\` inserts a copy of \`${chart}\`, so a chart that uses itself, directly or through other charts, would be copied for ever (official KIELER never finishes compiling it). Express the repetition with states and transitions instead, e.g. a transition back to an earlier state.`,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
export function isReferencing(s) {
|
|
376
|
+
return s.ext.reference !== undefined || s.ext.call !== undefined;
|
|
377
|
+
}
|
|
378
|
+
function referenceCall(s) {
|
|
379
|
+
if (s.ext.call)
|
|
380
|
+
return s.ext.call;
|
|
381
|
+
const call = s.ext.reference;
|
|
382
|
+
if (!call)
|
|
383
|
+
return undefined;
|
|
384
|
+
return { target: call.target, args: s.ext.args ?? [], span: call.targetSpan ?? call.span, super: call.super };
|
|
385
|
+
}
|
|
386
|
+
//# sourceMappingURL=reference.js.map
|