@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,1062 @@
|
|
|
1
|
+
// From a resolved, validated chart to an executable model: lowering (lower.ts), KIELER's desugaring passes
|
|
2
|
+
// (passes/), the SCG (scg.ts), the dependency analysis (deps.ts) and KIELER's acceptance test and schedule
|
|
3
|
+
// (analysis.ts). Everything KIELER rejects at this stage becomes an error diagnostic in source terms.
|
|
4
|
+
import { lowerChart } from "./lower.js";
|
|
5
|
+
import { PIPELINE } from "./passes/index.js";
|
|
6
|
+
import { buildSCG, cloneSCG, successors } from "./scg.js";
|
|
7
|
+
import { abortRegionsSCG } from "./abortRegions.js";
|
|
8
|
+
import { concurrentThreads, dependencies, dependencyReach } from "./deps.js";
|
|
9
|
+
import { analyse, liveNodes } from "./analysis.js";
|
|
10
|
+
import { conditionalWithoutElse, checkCodegen } from "./codegen.js";
|
|
11
|
+
import { removeSuperfluousForks } from "./sfr.js";
|
|
12
|
+
import { constantIndexWarnings } from "./memory.js";
|
|
13
|
+
import { droppedIndexDiagnostics, droppedIndexRefs } from "./guardIndices.js";
|
|
14
|
+
import { inlineMethods, isInlined } from "./structure/methods.js";
|
|
15
|
+
import { goBlock, goBlockDiagnostics } from "./goblock.js";
|
|
16
|
+
import { stringConditions } from "./stringConditions.js";
|
|
17
|
+
import { formatExpression } from "../format.js";
|
|
18
|
+
import { allActionSites, allScopes, allStates, effectExprs, readableName, walkExpr } from "./model.js";
|
|
19
|
+
// No DOM types in the engine: performance is global in browsers and Node, and optional here.
|
|
20
|
+
const now = () => globalThis.performance?.now() ?? Date.now();
|
|
21
|
+
/**
|
|
22
|
+
* `hostCode`: compile charts with host code, extern calls and recursive methods on instead of stopping at the simulator's
|
|
23
|
+
* `unsupported` info (the C code generator, ../codegen, carries them into C). Such a result is not for the simulator.
|
|
24
|
+
*/
|
|
25
|
+
export function compileChart(chart, index, options = {}) {
|
|
26
|
+
const diagnostics = [];
|
|
27
|
+
const t0 = now();
|
|
28
|
+
const lowered = lowerChart(chart, index);
|
|
29
|
+
const t1 = now();
|
|
30
|
+
for (const p of lowered.problems) {
|
|
31
|
+
diagnostics.push({ severity: 'error', code: 'internal', message: p.message, span: p.span });
|
|
32
|
+
}
|
|
33
|
+
diagnostics.push(...lowered.reports);
|
|
34
|
+
const ctx = {
|
|
35
|
+
root: lowered.root,
|
|
36
|
+
config: { finalRegions: false, hostCode: options.hostCode },
|
|
37
|
+
vars: lowered.vars,
|
|
38
|
+
report: d => diagnostics.push(d),
|
|
39
|
+
// The resolved model of every file (the Reference pass instantiates other charts).
|
|
40
|
+
shared: { index },
|
|
41
|
+
};
|
|
42
|
+
for (const pass of PIPELINE) {
|
|
43
|
+
if (pass.run) {
|
|
44
|
+
pass.run(ctx);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const uses = pass.uses?.(lowered.root) ?? [];
|
|
48
|
+
if (uses.length) {
|
|
49
|
+
const first = uses[0];
|
|
50
|
+
diagnostics.push({
|
|
51
|
+
// Not a rejection: KIELER runs this chart, only the guide's simulator cannot (yet).
|
|
52
|
+
severity: 'info', code: 'unsupported',
|
|
53
|
+
message: `The guide's simulator cannot run ${first.what} yet`,
|
|
54
|
+
span: first.span,
|
|
55
|
+
related: uses.flatMap(u => u.related ?? []),
|
|
56
|
+
hint: `This needs KIELER's ${pass.name} transformation, which the simulator does not implement yet. The official KIELER tools can run this chart.`,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// An expression the lowering could not represent would only fail when a tick evaluates it: say so now.
|
|
61
|
+
if (!diagnostics.some(d => d.severity === 'error' || d.code === 'unsupported'))
|
|
62
|
+
diagnostics.push(...leftovers(lowered.root));
|
|
63
|
+
const t2 = now();
|
|
64
|
+
// Constructs the passes could not remove make everything after this unreliable: KIELER would analyse a different
|
|
65
|
+
// graph. Report only that the chart cannot be simulated yet.
|
|
66
|
+
const unsupported = diagnostics.some(d => d.code === 'unsupported');
|
|
67
|
+
if (unsupported) {
|
|
68
|
+
// What gcc is known to reject still rejects the chart, host code or not (S-72).
|
|
69
|
+
diagnostics.push(...checkCodegen(lowered.root));
|
|
70
|
+
const scg = { nodes: [], threads: [], root: undefined, stateNodes: new Map(), problems: [], seq: 0 };
|
|
71
|
+
const analysis = { pathTypes: new Map(), loops: [], cured: [], joins: new Map(), cycles: [], order: [], deps: [], lostTerminations: [], ambiguous: false, decisions: [], curedAt: [], preemption: new Map() };
|
|
72
|
+
if (index)
|
|
73
|
+
for (const d of diagnostics)
|
|
74
|
+
if (!d.span)
|
|
75
|
+
d.span = locate(d.related, index);
|
|
76
|
+
return {
|
|
77
|
+
ok: !diagnostics.some(d => d.severity === 'error'), runnable: false, diagnostics, chart, lowered, core: lowered.root,
|
|
78
|
+
scg, deps: [], accesses: new Map(), analysis, goBlock: { nodes: [], moved: new Set() },
|
|
79
|
+
timing: { lower: t1 - t0, passes: t2 - t1, scg: 0, analysis: 0 }, shared: ctx.shared,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
diagnostics.push(...checkCodegen(lowered.root));
|
|
83
|
+
diagnostics.push(...randomizeInclude(chart, index));
|
|
84
|
+
const clashes = labelClashes(chart);
|
|
85
|
+
diagnostics.push(...constantIndexWarnings(lowered.root, chart, lowered.takenOrder.length));
|
|
86
|
+
const scg = buildSCG(lowered.root, lowered.takenOrder.length);
|
|
87
|
+
const sameBranches = scg.problems.find(p => p.message === 'same-branches');
|
|
88
|
+
if (sameBranches) {
|
|
89
|
+
const st = sameBranches.states[0];
|
|
90
|
+
const id = st?.origin ?? st?.about;
|
|
91
|
+
const name = (id ? index?.states.get(id)?.name : undefined) ?? readableName(st);
|
|
92
|
+
diagnostics.push({
|
|
93
|
+
severity: 'warning', code: 'kieler-quirk', related: id ? [id] : undefined,
|
|
94
|
+
message: `KIELER's compiled code for ${name ? `\`${name}\`` : 'this state'} only follows a transition without trigger when the transition before it (to the same place) holds, so that default path is never taken alone`,
|
|
95
|
+
hint: 'This is a defect of KIELER\'s netlist compiler: a condition whose two branches lead to the same place keeps the condition on both. It shows up when a superstate has several delayed strong aborts and the last has no trigger. The guide shows what KIELER does; giving every abort a trigger avoids it.',
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
for (const p of scg.problems) {
|
|
99
|
+
if (p.message !== 'conditional-pattern')
|
|
100
|
+
continue;
|
|
101
|
+
const state = p.states[0];
|
|
102
|
+
const id = state.origin ?? state.about;
|
|
103
|
+
diagnostics.push({
|
|
104
|
+
severity: 'error', code: 'kieler-crash', related: id ? [id] : undefined,
|
|
105
|
+
message: 'KIELER\'s compiler crashes on this chart: after its rewriting, a state has four or more outgoing transitions starting with an immediate triggered one followed by an immediate default',
|
|
106
|
+
kieler: p.kieler,
|
|
107
|
+
hint: 'This comes from KIELER\'s Surface/Depth transformation. Reordering the transitions of the superstate (or of the state) usually avoids it.',
|
|
108
|
+
});
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
// KIELER's SCGAbortRegionProcessor, then its SCG clean-ups (superfluous threads and forks), before everything else
|
|
112
|
+
// that looks at the SCG.
|
|
113
|
+
const abortRegionProblems = abortRegionsSCG(scg, id => index?.states.get(id)?.name);
|
|
114
|
+
diagnostics.push(...abortRegionProblems);
|
|
115
|
+
diagnostics.push(...removeSuperfluousForks(scg, lowered.root));
|
|
116
|
+
// KIELER's Methods processor (scg/processors/MethodProcessor.xtend) inlines simple methods into the SCG.
|
|
117
|
+
diagnostics.push(...inlineMethods(scg));
|
|
118
|
+
diagnostics.push(...crashingClashes(clashes, scg, lowered.root));
|
|
119
|
+
diagnostics.push(...conditionalWithoutElse(scg.nodes));
|
|
120
|
+
diagnostics.push(...conditionKeepsResult(scg));
|
|
121
|
+
diagnostics.push(...stringConditions(scg));
|
|
122
|
+
diagnostics.push(...droppedIndices(scg));
|
|
123
|
+
diagnostics.push(...orderDependentUpdates(scg));
|
|
124
|
+
const go = goBlock(scg);
|
|
125
|
+
diagnostics.push(...goBlockDiagnostics(go));
|
|
126
|
+
// KIELER's search for accesses ends at a flow into a state without nodes (deps.ts dependencyReach).
|
|
127
|
+
const reach = dependencyReach(scg);
|
|
128
|
+
const { deps, accesses } = dependencies(scg.nodes, reach?.stop ? reach.reached : undefined, isInlined);
|
|
129
|
+
const unordered = reach ? lostDependencies(reach, deps, scg, index) : [];
|
|
130
|
+
if (reach?.crash)
|
|
131
|
+
diagnostics.push(...unordered);
|
|
132
|
+
// The curing changes the graph; keep a copy in case the order of cures needs a second look (nondeterminism).
|
|
133
|
+
const pristine = scg.nodes.filter(n => n.kind === 'fork').length > 1 ? snapshot(scg, deps, accesses) : undefined;
|
|
134
|
+
const t3 = now();
|
|
135
|
+
const analysis = analyse({ scg, deps });
|
|
136
|
+
const t4 = now();
|
|
137
|
+
// SimpleGuardTransformation orders the threads of a sequential fork (a state with abort regions) through their
|
|
138
|
+
// guard nodes; a fork that can never be reached (dead: its state is entered only by a join that never fires) has
|
|
139
|
+
// none, and KIELER throws (probes preemption/probe-abort-region-dead-*).
|
|
140
|
+
const alive = liveNodes(scg);
|
|
141
|
+
const deadSequential = scg.nodes.find(n => n.kind === 'fork' && n.preemptive && (n.threads?.length ?? 0) > 1 && !alive.has(n));
|
|
142
|
+
if (deadSequential && !analysis.failure && !diagnostics.some(d => d.code === 'kieler-crash')) {
|
|
143
|
+
const id = sourceState(deadSequential.state);
|
|
144
|
+
const name = (id && index?.states.get(id)?.name) ?? readableName(deadSequential.state) ?? 'A state';
|
|
145
|
+
diagnostics.push({
|
|
146
|
+
severity: 'error', code: 'kieler-crash', related: id ? [id] : undefined,
|
|
147
|
+
message: `KIELER's compiler crashes on this chart: \`${name}\` has abort regions but can never be entered`,
|
|
148
|
+
kieler: 'java.lang.NullPointerException: Cannot invoke "de.cau.cs.kieler.kexpressions.keffects.Linkable.getOutgoingLinks()" because "linkable" is null (SimpleGuardTransformation)',
|
|
149
|
+
hint: `KIELER orders the regions of a state with abort regions in its compiled code, but \`${name}\` has no code: the only way into it is a \`join to\` that can never fire (a region without a final state) or a state that is never entered. Make \`${name}\` reachable, or remove its abort regions.`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
// LoopAnalyzerV2: a sequential (preemptive) fork, a state with abort regions, in an instantaneous loop.
|
|
153
|
+
const sequentialInLoop = analysis.loops.flat().find(n => n.kind === 'fork' && n.preemptive);
|
|
154
|
+
if (sequentialInLoop) {
|
|
155
|
+
const id = sequentialInLoop.state?.origin ?? sequentialInLoop.state?.about;
|
|
156
|
+
const name = (id && index?.states.get(id)?.name) ?? sequentialInLoop.state?.name ?? 'A state';
|
|
157
|
+
diagnostics.push({
|
|
158
|
+
severity: 'error', code: 'abort-region-loop', related: id ? [id] : undefined,
|
|
159
|
+
message: `\`${name}\` has abort regions and can be left and entered again in the same tick: KIELER cannot compile that`,
|
|
160
|
+
kieler: 'Sequential forks must not be schizophrenic!',
|
|
161
|
+
hint: `KIELER runs the regions of a state with abort regions one after the other and cannot untangle such a state when it restarts within a tick. Make the transition that re-enters \`${name}\` delayed, or make the abort regions ordinary regions.`,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
diagnostics.push(...loopDiagnostics(analysis, accesses, lowered, index));
|
|
165
|
+
for (const lost of dedupeLost(analysis.lostTerminations)) {
|
|
166
|
+
const t = lost.node.transition;
|
|
167
|
+
// The superstate the transition finishes: the source state's parent, in the chart as written.
|
|
168
|
+
const source = index?.states.get(t.origin.replace(/->\d+$/, ''));
|
|
169
|
+
const region = source ? index?.parentRegion.get(source) : undefined;
|
|
170
|
+
const superstate = region ? index?.parentState.get(region) : undefined;
|
|
171
|
+
const name = superstate?.name ? `\`${superstate.name}\`` : 'the superstate';
|
|
172
|
+
diagnostics.push({
|
|
173
|
+
severity: 'warning', code: 'kieler-quirk', span: t.span,
|
|
174
|
+
related: [t.origin, ...(superstate ? [superstate.id] : [])],
|
|
175
|
+
message: `If ${name} is entered again in the tick it was left (for example by a reset), KIELER does not let it finish in that tick through this transition`,
|
|
176
|
+
hint: 'KIELER copies the superstate\'s first-tick behaviour to break the instantaneous loop, and the copy cannot reach the final state. The guide shows what KIELER does. Make the transition delayed, or reset with a delayed transition, to avoid it.',
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
if (analysis.failure) {
|
|
180
|
+
diagnostics.push({
|
|
181
|
+
severity: 'error', code: 'kieler-crash', message: 'KIELER\'s compiler fails on this chart (it cannot untangle a superstate that is left and re-entered in the same tick)',
|
|
182
|
+
kieler: analysis.failure,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
diagnostics.push(...nondeterminism(analysis, pristine, lowered, index));
|
|
186
|
+
// (Not worth a warning when the chart is rejected anyway.)
|
|
187
|
+
if (reach && !reach.crash && !diagnostics.some(d => d.severity === 'error'))
|
|
188
|
+
diagnostics.push(...unordered);
|
|
189
|
+
// P-20's note explains how KIELER runs a strong abort whose trigger the inside writes; when KIELER rejects the
|
|
190
|
+
// chart for its trigger (abort-trigger-cycle), the note would contradict the error.
|
|
191
|
+
if (diagnostics.some(d => d.code === 'abort-trigger-cycle')) {
|
|
192
|
+
for (let i = diagnostics.length - 1; i >= 0; i--)
|
|
193
|
+
if (diagnostics[i].code === 'kieler-quirk' && /^This strong abort reads /.test(diagnostics[i].message))
|
|
194
|
+
diagnostics.splice(i, 1);
|
|
195
|
+
}
|
|
196
|
+
if (index)
|
|
197
|
+
for (const d of diagnostics)
|
|
198
|
+
if (!d.span)
|
|
199
|
+
d.span = locate(d.related, index);
|
|
200
|
+
const ok = !diagnostics.some(d => d.severity === 'error');
|
|
201
|
+
return {
|
|
202
|
+
ok,
|
|
203
|
+
runnable: ok && !diagnostics.some(d => d.code === 'unsupported'),
|
|
204
|
+
diagnostics,
|
|
205
|
+
chart,
|
|
206
|
+
lowered,
|
|
207
|
+
core: lowered.root,
|
|
208
|
+
scg,
|
|
209
|
+
deps,
|
|
210
|
+
accesses,
|
|
211
|
+
analysis,
|
|
212
|
+
goBlock: go,
|
|
213
|
+
timing: { lower: t1 - t0, passes: t2 - t1, scg: t3 - t2, analysis: t4 - t3 },
|
|
214
|
+
shared: ctx.shared,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
// ---------------------------------------------------------------------------------------------------
|
|
218
|
+
// Diagnostics
|
|
219
|
+
// ---------------------------------------------------------------------------------------------------
|
|
220
|
+
/**
|
|
221
|
+
* KIELER looks up the label of a `goto` by name among the labels whose innermost named container (a state, a named
|
|
222
|
+
* region, a class) is the goto's, in eAllContents order, and takes the first (Xtext's scoping, with qualified names
|
|
223
|
+
* from SCTXQualifiedNameProvider; methods and code blocks have no name): two code blocks or methods of one state (its
|
|
224
|
+
* methods, actions and outgoing transitions; the chart's for the chart's methods) with a label of the same name make a
|
|
225
|
+
* later block's `goto` jump into the earlier block, whose label the later block's graph does not have, and KIELER's
|
|
226
|
+
* compiler crashes on the flow without target (lab: "The compiler failed during compilation setup.", official 0.3.0
|
|
227
|
+
* never answers). Probes kieler/rare/probe-label-*.
|
|
228
|
+
*/
|
|
229
|
+
function labelClashes(chart) {
|
|
230
|
+
const out = [];
|
|
231
|
+
const check = (bodies, container) => {
|
|
232
|
+
const labels = [];
|
|
233
|
+
const gotos = [];
|
|
234
|
+
const walk = (list, body) => {
|
|
235
|
+
for (const st of list) {
|
|
236
|
+
switch (st.kind) {
|
|
237
|
+
case 'label':
|
|
238
|
+
labels.push({ name: st.name, body });
|
|
239
|
+
break;
|
|
240
|
+
case 'goto':
|
|
241
|
+
gotos.push({ target: st.target, body, span: st.span });
|
|
242
|
+
break;
|
|
243
|
+
case 'block':
|
|
244
|
+
walk(st.statements, body);
|
|
245
|
+
break;
|
|
246
|
+
case 'if':
|
|
247
|
+
walk(st.statements, body);
|
|
248
|
+
if (st.else)
|
|
249
|
+
walk(st.else.statements, body);
|
|
250
|
+
break;
|
|
251
|
+
case 'while':
|
|
252
|
+
case 'for':
|
|
253
|
+
walk(st.statements, body);
|
|
254
|
+
break;
|
|
255
|
+
default: break;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
bodies.forEach((b, i) => walk(b.statements, i));
|
|
260
|
+
for (const g of gotos) {
|
|
261
|
+
const first = labels.find(l => l.name === g.target);
|
|
262
|
+
if (!first || first.body === g.body)
|
|
263
|
+
continue;
|
|
264
|
+
out.push({
|
|
265
|
+
body: bodies[g.body].span,
|
|
266
|
+
diagnostic: {
|
|
267
|
+
severity: 'error', code: 'kieler-crash', span: g.span, related: [bodies[g.body].owner],
|
|
268
|
+
message: `KIELER's compiler crashes on this chart: \`goto ${g.target}\` finds the label \`${g.target}\` of another code block or method of \`${container}\``,
|
|
269
|
+
kieler: 'The compiler failed during compilation setup.',
|
|
270
|
+
hint: `KIELER looks a goto's label up among all labels of the state the block belongs to (its methods, actions and transitions) and takes the first one, which is in an earlier block. Give the labels of each block names of their own (\`${g.target}1\`, \`${g.target}2\`).`,
|
|
271
|
+
},
|
|
272
|
+
});
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const methods = (decls, owner, into) => {
|
|
277
|
+
for (const d of decls) {
|
|
278
|
+
if (d.declKind === 'method' && d.body)
|
|
279
|
+
into.push({ statements: d.body.statements, owner, span: d.span });
|
|
280
|
+
// A class is a named container of its own.
|
|
281
|
+
if ((d.declKind === 'class' || d.declKind === 'struct') && d.members) {
|
|
282
|
+
const own = [];
|
|
283
|
+
methods(d.members, owner, own);
|
|
284
|
+
check(own, d.className ?? d.valuedObjects[0]?.name ?? owner);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
const codes = (effects, owner, into) => {
|
|
289
|
+
for (const f of effects)
|
|
290
|
+
if (f.kind === 'code' && f.statements)
|
|
291
|
+
into.push({ statements: f.statements, owner, span: f.span });
|
|
292
|
+
};
|
|
293
|
+
const visit = (st) => {
|
|
294
|
+
const bodies = [];
|
|
295
|
+
methods(st.declarations, st.id, bodies);
|
|
296
|
+
for (const a of st.actions)
|
|
297
|
+
codes(a.effects, a.id, bodies);
|
|
298
|
+
for (const r of st.regions) {
|
|
299
|
+
// An unnamed region is no container: its methods and actions are the state's.
|
|
300
|
+
const rb = r.name ? [] : bodies;
|
|
301
|
+
methods(r.declarations, r.id, rb);
|
|
302
|
+
for (const a of r.actions)
|
|
303
|
+
codes(a.effects, a.id, rb);
|
|
304
|
+
if (r.name)
|
|
305
|
+
check(rb, r.name);
|
|
306
|
+
r.states.forEach(visit);
|
|
307
|
+
}
|
|
308
|
+
for (const t of st.transitions)
|
|
309
|
+
codes(t.effects, t.id, bodies);
|
|
310
|
+
check(bodies, st.name);
|
|
311
|
+
};
|
|
312
|
+
visit(chart);
|
|
313
|
+
return out;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* The label clashes KIELER crashes on: its ThreadAnalyzer meets the flow without target when it walks the graph from
|
|
317
|
+
* a thread's entry, so a body copied into the graph crashes only where control can reach it (a block of a state that
|
|
318
|
+
* is never entered does not: fuzz campaign 10); a method compiled as a C function has a graph of its own and always
|
|
319
|
+
* does.
|
|
320
|
+
*/
|
|
321
|
+
function crashingClashes(clashes, scg, root) {
|
|
322
|
+
if (!clashes.length)
|
|
323
|
+
return [];
|
|
324
|
+
const methods = new Map();
|
|
325
|
+
for (const scope of allScopes(root))
|
|
326
|
+
for (const v of scope.decls)
|
|
327
|
+
if (v.method?.span)
|
|
328
|
+
methods.set(v.method.span.start, v);
|
|
329
|
+
// Control flow from the root's entry (pauses included, as getAllThreadNodesAndThreads walks them).
|
|
330
|
+
const reached = new Set();
|
|
331
|
+
const work = [scg.root.entry];
|
|
332
|
+
while (work.length) {
|
|
333
|
+
const n = work.pop();
|
|
334
|
+
if (reached.has(n))
|
|
335
|
+
continue;
|
|
336
|
+
reached.add(n);
|
|
337
|
+
for (const m of [n.next, n.then, n.else, n.join, n.depth, ...(n.threads ?? []).map(t => t.entry)])
|
|
338
|
+
if (m && !reached.has(m))
|
|
339
|
+
work.push(m);
|
|
340
|
+
}
|
|
341
|
+
const out = [];
|
|
342
|
+
for (const c of clashes) {
|
|
343
|
+
const v = methods.get(c.body.start);
|
|
344
|
+
if (!v?.method)
|
|
345
|
+
continue;
|
|
346
|
+
if (!isInlined(v.method) || scg.nodes.some(n => n.method?.name === v.name && reached.has(n)))
|
|
347
|
+
out.push(c.diagnostic);
|
|
348
|
+
}
|
|
349
|
+
return out.slice(0, 1);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* `x += (c) ? e` is `x += c ? e : x` in KIELER's C code, which reads x; KIELER's model has no such read, so it treats
|
|
353
|
+
* the update as commuting with a concurrent update of x and orders the two as its scheduler happens to, and the
|
|
354
|
+
* result depends on that order (fuzz campaign 11, polish-111-22: the guide's schedule and KIELER's differ there). A
|
|
355
|
+
* kieler-quirk warning, since the guide cannot promise KIELER's order.
|
|
356
|
+
*/
|
|
357
|
+
function orderDependentUpdates(scg) {
|
|
358
|
+
const out = [];
|
|
359
|
+
const writes = scg.nodes.filter(n => n.kind === 'assign' && n.effect?.k === 'assign');
|
|
360
|
+
for (const n of writes) {
|
|
361
|
+
const f = n.effect;
|
|
362
|
+
if (f.op === '=' || !f.expr)
|
|
363
|
+
continue;
|
|
364
|
+
let selfRead = false;
|
|
365
|
+
walkExpr(f.expr, x => { if (x.k === 'op' && x.op === '?:' && x.args.length === 2)
|
|
366
|
+
selfRead = true; });
|
|
367
|
+
if (!selfRead)
|
|
368
|
+
continue;
|
|
369
|
+
const other = writes.find(m => m !== n && m.effect.ref.v === f.ref.v && concurrentThreads(n.thread, m.thread));
|
|
370
|
+
if (!other)
|
|
371
|
+
continue;
|
|
372
|
+
const name = say(f.ref.v);
|
|
373
|
+
const plain = varName(f.ref.v);
|
|
374
|
+
const line = other.effect.o.span?.line;
|
|
375
|
+
const own = !f.ref.v.generated || /^[\w$.'^[\]]+$/.test(plain);
|
|
376
|
+
out.push({
|
|
377
|
+
severity: 'warning', code: 'kieler-quirk', span: f.o.span, related: f.o.by ? [f.o.by] : undefined,
|
|
378
|
+
message: `KIELER's C code for this update reads ${name} (the missing else part is ${name}), but KIELER does not see the read: it orders it freely against the concurrent update of ${name}${line ? ` (line ${line})` : ''}, and the result depends on that order`,
|
|
379
|
+
hint: `The guide's simulation runs the two in the order it computes for KIELER's scheduler, which can differ from KIELER's in such a case. ${own ? `Write the else part (\`${plain} ${f.op} (c) ? e : 0\`) so that the update does not read ${name}.` : `Write the else part of the conditional so that KIELER's C code does not read ${name}.`}`,
|
|
380
|
+
});
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
return out;
|
|
384
|
+
}
|
|
385
|
+
/** Conditions whose array indices KIELER's C code drops (guardIndices.ts): one diagnostic per source condition. */
|
|
386
|
+
function droppedIndices(scg) {
|
|
387
|
+
const out = [];
|
|
388
|
+
const seen = new Set();
|
|
389
|
+
const alive = liveNodes(scg);
|
|
390
|
+
for (const n of scg.nodes) {
|
|
391
|
+
if (n.kind !== 'cond' || !n.expr || !alive.has(n))
|
|
392
|
+
continue;
|
|
393
|
+
const dropped = droppedIndexRefs(n.expr);
|
|
394
|
+
if (!dropped.size)
|
|
395
|
+
continue;
|
|
396
|
+
const first = [...dropped][0];
|
|
397
|
+
const key = `${first.span?.start ?? n.id}`;
|
|
398
|
+
if (seen.has(key))
|
|
399
|
+
continue;
|
|
400
|
+
seen.add(key);
|
|
401
|
+
const by = n.method ? undefined : n.transition?.origin ?? n.transition?.about;
|
|
402
|
+
out.push(...droppedIndexDiagnostics(n.expr, dropped, v => v.display ?? v.name, by ? [by] : undefined));
|
|
403
|
+
}
|
|
404
|
+
return out;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* A conditional without an else part in a condition (a trigger, an `if` of a copied method body): KIELER keeps the
|
|
408
|
+
* condition's previous result when the conditional's condition is false (exec.ts, ExprCompiler.selves; probes
|
|
409
|
+
* kieler/rare/probe-cond-noelse-*). A kieler-quirk info per conditional.
|
|
410
|
+
*/
|
|
411
|
+
function conditionKeepsResult(scg) {
|
|
412
|
+
const out = [];
|
|
413
|
+
const seen = new Set();
|
|
414
|
+
const name = (e) => (e.k === 'ref' && !e.idx.length && !e.sub ? e.v.display ?? e.v.name : e.k === 'lit' ? String(e.v) : undefined);
|
|
415
|
+
for (const n of scg.nodes) {
|
|
416
|
+
if (n.kind !== 'cond' || !n.expr)
|
|
417
|
+
continue;
|
|
418
|
+
walkExpr(n.expr, x => {
|
|
419
|
+
if (x.k !== 'op' || x.op !== '?:' || x.args.length !== 2)
|
|
420
|
+
return;
|
|
421
|
+
const key = x.span ? `${x.span.start}` : `${n.id}`;
|
|
422
|
+
if (seen.has(key))
|
|
423
|
+
return;
|
|
424
|
+
seen.add(key);
|
|
425
|
+
const [c, a] = [name(x.args[0]), name(x.args[1])];
|
|
426
|
+
const text = c && a ? `\`(${c}) ? ${a}\`` : 'This conditional without an else part';
|
|
427
|
+
const cond = c ? `\`${c}\`` : 'its condition';
|
|
428
|
+
const by = n.method ? undefined : n.transition?.origin ?? n.transition?.about;
|
|
429
|
+
out.push({
|
|
430
|
+
severity: 'info', code: 'kieler-quirk', span: x.span, related: by ? [by] : undefined,
|
|
431
|
+
message: `${text} has no else part, so when ${cond} is false KIELER keeps the result this ${n.method ? 'condition' : 'trigger'} had when it was last computed`,
|
|
432
|
+
hint: `KIELER computes every trigger in every tick, also while its state is not active, and keeps each result in a variable of its own: without an else part, a false ${cond} leaves that variable as it was (false until ${cond} first holds). The guide's simulation does the same. Write the else part to say what you mean${c && a ? `, such as \`(${c}) ? ${a} : false\`` : ''}.`,
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
return out;
|
|
437
|
+
}
|
|
438
|
+
/** A copy of the graph and its dependencies before the analysis changes them. */
|
|
439
|
+
function snapshot(scg, deps, accesses) {
|
|
440
|
+
const { scg: copy, map } = cloneSCG(scg);
|
|
441
|
+
const m = (n) => map.get(n) ?? n;
|
|
442
|
+
const accessCopy = new Map();
|
|
443
|
+
const acc = (a) => {
|
|
444
|
+
let c = accessCopy.get(a);
|
|
445
|
+
if (!c)
|
|
446
|
+
accessCopy.set(a, (c = { ...a, node: m(a.node) }));
|
|
447
|
+
return c;
|
|
448
|
+
};
|
|
449
|
+
return {
|
|
450
|
+
scg: copy,
|
|
451
|
+
deps: deps.map(d => ({ ...d, from: m(d.from), to: m(d.to), a: acc(d.a), b: acc(d.b) })),
|
|
452
|
+
accesses: new Map([...accesses].map(([n, list]) => [m(n), list.map(acc)])),
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* KIELER cures one schizophrenic superstate at a time; when several qualify, it takes the first in a hash map of EMF
|
|
457
|
+
* objects (StructuralDepthJoinProcessor, ThreadData.forkMap), whose order is fixed within a run and changes from run
|
|
458
|
+
* to run. The analysis follows node order; when it had such a choice, the other orders are tried too (analysis.ts
|
|
459
|
+
* AnalysisInput.script: first each single change of one choice, then the paths through the choices depth first, up to
|
|
460
|
+
* MAX_ORDERS analyses). If KIELER accepts the chart with
|
|
461
|
+
* some orders and rejects it with others, the guide rejects it (it may fail in KIELER). If every order is accepted
|
|
462
|
+
* but they untangle different superstates, KIELER's behaviour can change from run to run: an info says so.
|
|
463
|
+
*/
|
|
464
|
+
const MAX_ORDERS = 24;
|
|
465
|
+
function nondeterminism(analysis, pristine, lowered, index) {
|
|
466
|
+
if (!analysis.decisions.length || !pristine)
|
|
467
|
+
return [];
|
|
468
|
+
const accepts = (a) => !a.cycles.length && !a.failure;
|
|
469
|
+
const signature = (a) => `${[...a.curedAt].sort((x, y) => x - y).join(',')}|${a.lostTerminations.length}`;
|
|
470
|
+
let other;
|
|
471
|
+
let otherAccesses = pristine.accesses;
|
|
472
|
+
let differs;
|
|
473
|
+
const tried = new Set(['']);
|
|
474
|
+
const tryScript = (script) => {
|
|
475
|
+
const key = script.join(',');
|
|
476
|
+
if (tried.has(key))
|
|
477
|
+
return undefined;
|
|
478
|
+
tried.add(key);
|
|
479
|
+
const run = snapshot(pristine.scg, pristine.deps, pristine.accesses);
|
|
480
|
+
const result = analyse({ scg: run.scg, deps: run.deps, script });
|
|
481
|
+
if (accepts(result) !== accepts(analysis)) {
|
|
482
|
+
other = result;
|
|
483
|
+
otherAccesses = run.accesses;
|
|
484
|
+
}
|
|
485
|
+
else if (!differs && accepts(result) && signature(result) !== signature(analysis)) {
|
|
486
|
+
differs = result;
|
|
487
|
+
}
|
|
488
|
+
return result;
|
|
489
|
+
};
|
|
490
|
+
// First every single change of one choice, earliest first (which superstate is cured first matters most; with many
|
|
491
|
+
// candidates the depth-first walk below would only reach the last choices), then the paths depth first.
|
|
492
|
+
const first = analysis.decisions;
|
|
493
|
+
for (let k = 0; k < first.length && tried.size < MAX_ORDERS && !other; k++) {
|
|
494
|
+
for (let alt = 1; alt < first[k] && tried.size < MAX_ORDERS && !other; alt++)
|
|
495
|
+
tryScript([...Array.from({ length: k }, () => 0), alt]);
|
|
496
|
+
}
|
|
497
|
+
let script = [];
|
|
498
|
+
let counts = analysis.decisions;
|
|
499
|
+
for (let guard = 0; tried.size < MAX_ORDERS && !other && guard < 10_000; guard++) {
|
|
500
|
+
// The next path through the choices (depth first, in node order).
|
|
501
|
+
let k = counts.length - 1;
|
|
502
|
+
while (k >= 0 && (script[k] ?? 0) + 1 >= counts[k])
|
|
503
|
+
k--;
|
|
504
|
+
if (k < 0)
|
|
505
|
+
break;
|
|
506
|
+
script = [...Array.from({ length: k }, (_, i) => script[i] ?? 0), (script[k] ?? 0) + 1];
|
|
507
|
+
const result = tryScript(script);
|
|
508
|
+
if (result)
|
|
509
|
+
counts = result.decisions;
|
|
510
|
+
}
|
|
511
|
+
const sourceIds = (list) => [...new Set(list.flatMap(a => a.cured).map(f => sourceState(f.state)).filter((x) => !!x))];
|
|
512
|
+
if (!other) {
|
|
513
|
+
if (!differs)
|
|
514
|
+
return [];
|
|
515
|
+
const ids = sourceIds([analysis, differs]);
|
|
516
|
+
const names = ids.map(id => index?.states.get(id)?.name).filter((x) => !!x).map(n => `\`${n}\``);
|
|
517
|
+
return [{
|
|
518
|
+
severity: 'info', code: 'kieler-quirk', related: ids.length ? ids : undefined,
|
|
519
|
+
message: 'KIELER may run this chart differently from one compilation to the next',
|
|
520
|
+
hint: `${names.length > 1 ? `${names.join(' and ')} are` : 'Several superstates are'} left and entered again within one tick. KIELER untangles them one at a time, in an order that changes from run to run, and the orders give the chart different behaviour (for example an immediate transition that fires in some runs and not in others). The guide shows one of them. Making the transitions that re-enter these states delayed makes the chart reliable.`,
|
|
521
|
+
}];
|
|
522
|
+
}
|
|
523
|
+
const ids = sourceIds([analysis, other]);
|
|
524
|
+
const names = ids.map(id => index?.states.get(id)?.name).filter((x) => !!x).map(n => `\`${n}\``);
|
|
525
|
+
const out = [{
|
|
526
|
+
severity: 'error', code: 'kieler-nondeterministic', related: ids.length ? ids : undefined,
|
|
527
|
+
message: 'KIELER\'s compiler accepts this chart in some runs and rejects it in others',
|
|
528
|
+
kieler: (accepts(analysis) ? other : analysis).failure ?? 'The SCG is NOT asc-schedulable! (in some runs)',
|
|
529
|
+
hint: `${names.length > 1 ? `${names.join(' and ')} are` : 'Several superstates are'} left and entered again within one tick. KIELER untangles them one at a time, in an order that changes from run to run, and only some orders work. Making the transitions that re-enter them delayed (or giving them a trigger) makes the chart reliable.`,
|
|
530
|
+
}];
|
|
531
|
+
// The reason it fails in the runs that reject it.
|
|
532
|
+
if (accepts(analysis))
|
|
533
|
+
out.push(...loopDiagnostics(other, otherAccesses, lowered, index).filter(d => d.severity === 'error'));
|
|
534
|
+
return out;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* randomize() is srand(time(NULL)), and current KIELER includes <stdlib.h> in its C code only for random() (basics.md
|
|
538
|
+
* B-29, structure.md S-50): a chart that seeds without calling random() anywhere fails in gcc, unless a `#hostcode`
|
|
539
|
+
* pragma includes the header.
|
|
540
|
+
*/
|
|
541
|
+
function randomizeInclude(chart, index) {
|
|
542
|
+
const programs = index?.programs ?? [];
|
|
543
|
+
let seed;
|
|
544
|
+
let random = false;
|
|
545
|
+
const visit = (value) => {
|
|
546
|
+
if (random)
|
|
547
|
+
return;
|
|
548
|
+
if (Array.isArray(value)) {
|
|
549
|
+
value.forEach(visit);
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
if (!value || typeof value !== 'object')
|
|
553
|
+
return;
|
|
554
|
+
const obj = value;
|
|
555
|
+
if (obj.kind === 'random') {
|
|
556
|
+
random = true;
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
if (obj.kind === 'randomize' && !seed)
|
|
560
|
+
seed = obj.span;
|
|
561
|
+
for (const [k, v] of Object.entries(obj))
|
|
562
|
+
if (k !== 'span' && v && typeof v === 'object')
|
|
563
|
+
visit(v);
|
|
564
|
+
};
|
|
565
|
+
visit(programs.length ? programs.flatMap(p => p.charts) : [chart]);
|
|
566
|
+
if (!seed || random)
|
|
567
|
+
return [];
|
|
568
|
+
const included = programs.some(p => p.pragmas.some(pr => pr.name.toLowerCase().startsWith('hostcode') && pr.values.some(v => v.includes('stdlib.h'))));
|
|
569
|
+
if (included)
|
|
570
|
+
return [];
|
|
571
|
+
return [{
|
|
572
|
+
severity: 'error', code: 'randomize-stdlib', span: seed,
|
|
573
|
+
message: 'randomize() does not compile in KIELER here: its C code includes <stdlib.h> only for charts that also call random()',
|
|
574
|
+
kieler: 'implicit declaration of function \u2018srand\u2019 [-Wimplicit-function-declaration]\nCompiler did not return success (exit value != 0)',
|
|
575
|
+
hint: 'Add `#hostcode "#include <stdlib.h>"` at the top of the file, or call `random()` somewhere in the chart.',
|
|
576
|
+
}];
|
|
577
|
+
}
|
|
578
|
+
/** Where to point a diagnostic that has no span of its own: the first related state (its name), region or transition. */
|
|
579
|
+
function locate(related, index) {
|
|
580
|
+
for (const id of related ?? []) {
|
|
581
|
+
const state = index.states.get(id);
|
|
582
|
+
if (state)
|
|
583
|
+
return state.nameSpan ?? state.span;
|
|
584
|
+
const region = index.regions.get(id);
|
|
585
|
+
if (region)
|
|
586
|
+
return region.nameSpan ?? region.span;
|
|
587
|
+
const m = /^(.*)->(\d+)$/.exec(id);
|
|
588
|
+
const t = m ? index.states.get(m[1])?.transitions[Number(m[2])] : undefined;
|
|
589
|
+
if (t)
|
|
590
|
+
return t.span;
|
|
591
|
+
}
|
|
592
|
+
// Inside a referenced chart's copy (`<reference state>/<id>`, structure/instance.ts): the reference state.
|
|
593
|
+
const outer = (related ?? []).map(id => id.split('/')[0]).filter(id => !(related ?? []).includes(id));
|
|
594
|
+
return outer.length ? locate(outer, index) : undefined;
|
|
595
|
+
}
|
|
596
|
+
/** The source element a node implements: a source transition or action, or the state it belongs to. */
|
|
597
|
+
function nodeElement(n) {
|
|
598
|
+
const t = n.transition;
|
|
599
|
+
if (t?.origin)
|
|
600
|
+
return t.origin;
|
|
601
|
+
if (t?.about)
|
|
602
|
+
return t.about;
|
|
603
|
+
if (n.effect?.o.by)
|
|
604
|
+
return n.effect.o.by;
|
|
605
|
+
return sourceState(n.state);
|
|
606
|
+
}
|
|
607
|
+
function sourceState(s) {
|
|
608
|
+
return s?.origin ?? s?.about;
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* KIELER's search for accesses meets a flow into a state without nodes (deps.ts dependencyReach): it throws, or it
|
|
612
|
+
* stops there and the variables whose concurrent accesses are left unordered get a warning (if there are any).
|
|
613
|
+
*/
|
|
614
|
+
function lostDependencies(reach, deps, scg, index) {
|
|
615
|
+
const id = sourceState(reach.into);
|
|
616
|
+
const name = (id && index?.states.get(id)?.name) ?? reach.into?.name;
|
|
617
|
+
const state = name ? `the referenced chart's state \`${name}\`` : 'a state of a referenced chart';
|
|
618
|
+
const dropped = 'In a referenced chart, KIELER drops a state whose first immediate transition goes back to it without a trigger (the transition has no take counter there), so that state has no code.';
|
|
619
|
+
if (reach.crash) {
|
|
620
|
+
return [{
|
|
621
|
+
severity: 'error', code: 'kieler-crash', related: id ? [id] : undefined,
|
|
622
|
+
message: `KIELER's compiler crashes on this chart: a transition leads into ${state}, which has no code`,
|
|
623
|
+
kieler: 'java.lang.NullPointerException: Cannot invoke "de.cau.cs.kieler.kexpressions.keffects.Link.getTarget()" because "cf" is null (DependencyTransformationV2)',
|
|
624
|
+
hint: `${dropped} Its dependency analysis then fails on the transition into it. Such a loop would not let the tick end anyway: give the transition a trigger, or make it delayed.`,
|
|
625
|
+
}];
|
|
626
|
+
}
|
|
627
|
+
const key = (d) => `${d.from.id}>${d.to.id}:${d.v.uid}:${d.kind}`;
|
|
628
|
+
const kept = new Set(deps.map(key));
|
|
629
|
+
const lost = dependencies(scg.nodes, undefined, isInlined).deps.filter(d => !kept.has(key(d)));
|
|
630
|
+
if (!lost.length)
|
|
631
|
+
return [];
|
|
632
|
+
const vars = [...new Set(lost.map(d => say(d.v)))];
|
|
633
|
+
const shown = vars.length > 3 ? `${vars.slice(0, 3).join(', ')} and others` : vars.length > 1 ? `${vars.slice(0, -1).join(', ')} and ${vars[vars.length - 1]}` : vars[0];
|
|
634
|
+
return [{
|
|
635
|
+
severity: 'warning', code: 'kieler-quirk', related: id ? [id] : undefined,
|
|
636
|
+
message: `KIELER's dependency analysis stops at ${state}, so its compiled code leaves the accesses to ${shown} in concurrent regions unordered: a read may come before a write in another region and see the value from before it`,
|
|
637
|
+
hint: `${dropped} Its dependency analysis walks the compiled graph and stops at the first region that starts in such a state; everything it has not reached by then is scheduled without dependencies. The guide shows what KIELER does. Giving that transition a trigger, or making it delayed, avoids it.`,
|
|
638
|
+
}];
|
|
639
|
+
}
|
|
640
|
+
function varName(v) {
|
|
641
|
+
return v.display ?? v.name;
|
|
642
|
+
}
|
|
643
|
+
/** A variable in a message or hint (inline Markdown): its name in code font, or what a compiler helper is for. */
|
|
644
|
+
function say(v) {
|
|
645
|
+
const shown = varName(v);
|
|
646
|
+
return !v.generated || /^[\w$.'^[\]]+$/.test(shown) ? `\`${shown}\`` : shown;
|
|
647
|
+
}
|
|
648
|
+
/** A list in a sentence: "a", "a and b", "a, b and c". */
|
|
649
|
+
function quote(lines) {
|
|
650
|
+
return lines.length < 2 ? lines.join('') : `${lines.slice(0, -1).join(', ')} and ${lines[lines.length - 1]}`;
|
|
651
|
+
}
|
|
652
|
+
function dedupeLost(list) {
|
|
653
|
+
const seen = new Set();
|
|
654
|
+
return list.filter(l => {
|
|
655
|
+
const key = `${l.thread.id}:${l.node.transition?.origin}`;
|
|
656
|
+
if (seen.has(key))
|
|
657
|
+
return false;
|
|
658
|
+
seen.add(key);
|
|
659
|
+
return true;
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
function loopDiagnostics(analysis, accesses, lowered, index) {
|
|
663
|
+
const out = [];
|
|
664
|
+
void lowered;
|
|
665
|
+
void accesses;
|
|
666
|
+
if (analysis.loops.length && !analysis.cycles.length) {
|
|
667
|
+
const lost = analysis.lostTerminations.length > 0;
|
|
668
|
+
// The superstates that are left and entered again (the cured forks), else the states on the loop.
|
|
669
|
+
const forks = analysis.cured.length ? analysis.cured : analysis.loops.flat().filter(n => n.kind === 'fork');
|
|
670
|
+
const related = [...new Set((forks.length ? forks : analysis.loops.flat()).map(n => sourceState(n.state)).filter((x) => !!x))];
|
|
671
|
+
out.push({
|
|
672
|
+
severity: 'info', code: 'kieler-loop-warning', related: related.length ? related : undefined,
|
|
673
|
+
message: lost
|
|
674
|
+
? 'KIELER warns about a potential instantaneous loop here: a superstate can be left and entered again in the same tick'
|
|
675
|
+
: 'KIELER warns about a potential instantaneous loop here, but compiles and runs the chart correctly',
|
|
676
|
+
kieler: 'Instantaneous loop detected!',
|
|
677
|
+
hint: lost
|
|
678
|
+
? 'KIELER compiles the chart by copying the superstate\'s first-tick behaviour; see the notes about the termination it loses.'
|
|
679
|
+
: `${loopWarningReason(analysis, index)} The chart works as written. See [Instantaneous loop detected!](/reference/errors/#instantaneous-loop-detected).`,
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
for (const cycle of analysis.cycles)
|
|
683
|
+
out.push(cycleDiagnostic(cycle, analysis, index));
|
|
684
|
+
return out;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Why KIELER's loop analysis warns about a chart that works: it looks at the compiled graph, in which count delays,
|
|
688
|
+
* entry and exit actions, aborts of superstates and clocks are extra regions and transitions, and reports any
|
|
689
|
+
* circle of control flow that one tick could go round, without looking at the triggers (or at the pauses that
|
|
690
|
+
* delayed transitions add in the original chart). Names what the chart's loop goes through.
|
|
691
|
+
*/
|
|
692
|
+
function loopWarningReason(analysis, index) {
|
|
693
|
+
const nodes = analysis.loops.flat();
|
|
694
|
+
const ids = new Set();
|
|
695
|
+
for (const n of nodes) {
|
|
696
|
+
for (const id of [n.transition?.origin, n.transition?.about, n.transition?.checks, n.effect?.o.by, sourceState(n.state)])
|
|
697
|
+
if (id)
|
|
698
|
+
ids.add(id);
|
|
699
|
+
}
|
|
700
|
+
const transitionOf = (id) => {
|
|
701
|
+
const m = /^(.*)->(\d+)$/.exec(id);
|
|
702
|
+
return m ? index?.states.get(m[1])?.transitions[Number(m[2]) - 1] : undefined;
|
|
703
|
+
};
|
|
704
|
+
const kinds = [];
|
|
705
|
+
const add = (k) => { if (!kinds.includes(k))
|
|
706
|
+
kinds.push(k); };
|
|
707
|
+
const sourceTransitions = [...ids].map(transitionOf).filter((t) => !!t);
|
|
708
|
+
if (sourceTransitions.some(t => (t.triggerDelay ?? 1) > 1))
|
|
709
|
+
add('count delays');
|
|
710
|
+
const actionKinds = new Set([...ids].map(id => /!(entry|exit|during)\d+$/.exec(id)?.[1]).filter((k) => !!k));
|
|
711
|
+
if (actionKinds.size)
|
|
712
|
+
add(`${[...actionKinds].sort((a, b) => ['entry', 'during', 'exit'].indexOf(a) - ['entry', 'during', 'exit'].indexOf(b)).join(' and ')} actions`);
|
|
713
|
+
const vars = nodes.flatMap(n => (n.kind === 'assign' && n.effect?.k === 'assign' ? [n.effect.ref.v] : []));
|
|
714
|
+
const timed = vars.some(v => v.name === 'sleepT' || v.type === 'clock' || v.display === 'the period\'s clock');
|
|
715
|
+
if (vars.some(v => v.display === 'the period\'s clock'))
|
|
716
|
+
add('periods');
|
|
717
|
+
else if (timed)
|
|
718
|
+
add('clocks (their timeouts and `sleepT`)');
|
|
719
|
+
if (sourceTransitions.some(t => t.preemption === 'strong'))
|
|
720
|
+
add('strong aborts');
|
|
721
|
+
const name = (id) => (id ? index?.states.get(id) : undefined);
|
|
722
|
+
const cured = [...new Set(analysis.cured.map(f => name(sourceState(f.state))).filter((s) => !!s && !s.isRoot).map(s => `\`${s.name}\``))];
|
|
723
|
+
const states = [...new Set([...ids].map(name).filter((s) => !!s && !s.isRoot && !s.connector).map(s => `\`${s.name}\``))];
|
|
724
|
+
if (cured.length && !kinds.length) {
|
|
725
|
+
return `KIELER's loop check also warns about a superstate that a transition leaves and enters again in one tick, here ${quote(cured)}: in the compiled chart, leaving it and starting it again look like a circle, although the new start only enters its initial states and then waits for the next tick.`;
|
|
726
|
+
}
|
|
727
|
+
const where = states.length && states.length <= 4 ? `: here the circle runs through ${quote(states)}` : '';
|
|
728
|
+
const what = kinds.length ? `, in which ${quote(kinds)} are extra regions and transitions,` : '';
|
|
729
|
+
return `KIELER's loop check works on the compiled chart${what} and warns whenever control could go round a circle there within one tick, without checking whether the triggers prevent it${where}.`;
|
|
730
|
+
}
|
|
731
|
+
function cycleDiagnostic(cycle, analysis, index) {
|
|
732
|
+
const inCycle = new Set(cycle);
|
|
733
|
+
const deps = analysisDeps(analysis).filter(d => inCycle.has(d.from) && inCycle.has(d.to));
|
|
734
|
+
const related = [...new Set(cycle.map(nodeElement).filter((x) => !!x))];
|
|
735
|
+
const kieler = 'The SCG is NOT asc-schedulable!';
|
|
736
|
+
const visible = deps.filter(d => !d.v.generated || d.v.of);
|
|
737
|
+
// A backward `goto` in a body KIELER copied into the graph (structure/methods.ts inline): the jump is the circle.
|
|
738
|
+
const back = cycle.find(n => n.method?.jumpsBack)?.method;
|
|
739
|
+
if (back?.jumpsBack) {
|
|
740
|
+
const label = back.jumpsBack.label;
|
|
741
|
+
const block = back.name.startsWith('_effect') ? 'the code block' : `\`${back.name}\``;
|
|
742
|
+
return {
|
|
743
|
+
severity: 'error', code: 'instantaneous-loop', span: back.jumpsBack.span, kieler, related,
|
|
744
|
+
message: `\`goto ${label}\` jumps back to \`${label}\`, a loop within the tick that KIELER cannot schedule`,
|
|
745
|
+
hint: `KIELER copies a short ${back.name.startsWith('_effect') ? 'code block' : 'method'} (no loop, no call, fewer than 28 statements) into the tick's code, and there a jump back is an instantaneous loop, which it rejects even when a condition ends it. To repeat something within a tick, write a \`for\` or \`while\` loop in ${block}: KIELER then compiles it as a C function, which may loop. To repeat it over several ticks, use a transition.`,
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
if (!deps.length) {
|
|
749
|
+
// Control flow only: a chain of immediate behaviour that can come back to where it started.
|
|
750
|
+
const transitions = [...new Set(cycle.flatMap(n => (n.transition?.origin ? [n.transition.origin] : [])))];
|
|
751
|
+
const states = [...new Set(cycle.map(n => sourceState(n.state)).filter((x) => !!x))];
|
|
752
|
+
const first = cycle.find(n => n.transition?.origin && n.transition.span)?.transition?.span ?? cycle.find(n => n.transition?.span)?.transition?.span ?? cycle.find(n => n.effect?.o.span)?.effect?.o.span;
|
|
753
|
+
const path = loopPath(transitions);
|
|
754
|
+
return {
|
|
755
|
+
severity: 'error', code: 'instantaneous-loop',
|
|
756
|
+
message: path
|
|
757
|
+
? `The immediate transitions ${path} form a loop that could run for ever within one tick`
|
|
758
|
+
: 'These immediate transitions form a loop that could run for ever within one tick',
|
|
759
|
+
span: first,
|
|
760
|
+
kieler,
|
|
761
|
+
hint: 'Make one transition on the loop delayed (remove `immediate`): a tick must always end. KIELER rejects such a loop even if the triggers would stop it.',
|
|
762
|
+
related: [...transitions, ...states].length ? [...transitions, ...states] : related,
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
const conflict = visible.find(d => d.kind === 'write-write') ?? deps.find(d => d.kind === 'write-write');
|
|
766
|
+
if (conflict) {
|
|
767
|
+
const v = conflict.v.of ?? conflict.v;
|
|
768
|
+
const name = say(v);
|
|
769
|
+
const plain = varName(v);
|
|
770
|
+
const isSignal = conflict.v.role === 'curval' || conflict.v.role === 'value';
|
|
771
|
+
const rel = [nodeElement(conflict.from), nodeElement(conflict.to)].filter((x) => !!x);
|
|
772
|
+
const spanA = conflict.a.span;
|
|
773
|
+
const spanB = conflict.b.span;
|
|
774
|
+
const relative = conflict.a.cls === 'relative' && conflict.b.cls === 'relative';
|
|
775
|
+
// A declaration of a for-region: KIELER moves it out of the region, so every copy writes the same variable.
|
|
776
|
+
const owner = v.decl ? index?.vos.get(v.decl)?.owner : undefined;
|
|
777
|
+
const forRegion = owner?.kind === 'region' && owner.forLoop ? owner : undefined;
|
|
778
|
+
if (forRegion && !relative && !isSignal) {
|
|
779
|
+
const label = forRegion.name ? `the for-region \`${forRegion.name}\`` : 'the for-region';
|
|
780
|
+
return {
|
|
781
|
+
severity: 'error', code: 'write-conflict', span: spanA ?? spanB, kieler, related: [...new Set([...rel, ...related])],
|
|
782
|
+
message: `The copies of ${label} assign different values to ${name}, which they share`,
|
|
783
|
+
hint: `KIELER makes one copy of ${label} for each value of \`${forRegion.forLoop.variable}\`, but a variable declared in the region is declared once, outside the copies: they all write the same ${name} in the same tick. Declare ${name} in a state inside the region (\`initial state W { int ${plain} = 0 ... }\`), or use an update such as \`${plain} += 1\`.`,
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
// One side is inside a code block or method that KIELER calls as a C function (deps.ts innerAccesses).
|
|
787
|
+
const inner = conflict.a.via !== undefined ? conflict.a : conflict.b.via !== undefined ? conflict.b : undefined;
|
|
788
|
+
if (inner && relative) {
|
|
789
|
+
const block = inner.via.startsWith('_effect') ? 'the code block' : `the method \`${inner.via}\``;
|
|
790
|
+
const other = inner === conflict.a ? conflict.b : conflict.a;
|
|
791
|
+
return {
|
|
792
|
+
severity: 'error', code: 'update-conflict', span: inner.span ?? other.span, kieler, related: [...new Set([...rel, ...related])],
|
|
793
|
+
message: `${name} is updated inside ${block}${inner.span ? ` (line ${inner.span.line})` : ''} and in a concurrent region${other.span ? ` (line ${other.span.line})` : ''}, and KIELER cannot combine the two`,
|
|
794
|
+
hint: `KIELER does not copy ${block} into place (it has a loop, a call or too many statements) but calls it as a C function, and counts the call as one plain assignment of ${name}: that does not combine with \`${plain} ${other.op ?? '+='} ...\` in the other region. Write the update outside ${block === 'the code block' ? 'the block' : 'the method'}, on the transition itself, or let one region own ${name}.`,
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
return {
|
|
798
|
+
severity: 'error', code: relative ? 'update-conflict' : isSignal ? 'emission-conflict' : 'write-conflict',
|
|
799
|
+
message: relative
|
|
800
|
+
? `${name} is updated in two concurrent regions with operators that do not combine (\`${conflict.a.op}\` and \`${conflict.b.op}\`)`
|
|
801
|
+
: isSignal
|
|
802
|
+
? `Two concurrent regions emit the valued signal ${name} with different values`
|
|
803
|
+
: `Two concurrent regions assign different values to ${name}`,
|
|
804
|
+
span: spanA ?? spanB,
|
|
805
|
+
kieler,
|
|
806
|
+
hint: relative
|
|
807
|
+
? `Concurrent updates of one variable must commute: use only \`+=\` and \`-=\` (or only one operator) for ${name}.`
|
|
808
|
+
: isSignal
|
|
809
|
+
? `KIELER does not check whether the two emissions can happen in the same tick. Give ${name} a combine function (\`signal int ${plain} combine +\`), or emit it from one region only.`
|
|
810
|
+
: `KIELER does not check whether the two assignments can happen in the same tick: two regions that run side by side must not both assign ${name}. ${v.type === 'int' || v.type === 'float'
|
|
811
|
+
// Updates only cure a variable that adds up what the regions contribute (a count), not one that names an owner or a mode.
|
|
812
|
+
? `Let one region own ${name}. If the regions add up contributions to ${name}, use an update such as \`${plain} += 1\` in both, or a valued signal with a combine function.`
|
|
813
|
+
: `Let one region own ${name}, and let the other regions send it requests with signals.`}`,
|
|
814
|
+
related: [...new Set([...rel, ...related])],
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
// A read/write cycle: each side waits for the other.
|
|
818
|
+
const reads = deps.filter(d => d.kind === 'write-read' || d.kind === 'relative-read');
|
|
819
|
+
const resetLoop = countDelayResetLoop(deps, index);
|
|
820
|
+
if (resetLoop)
|
|
821
|
+
return { severity: 'error', kieler, ...resetLoop, related: [...new Set([...(resetLoop.related ?? []), ...related])] };
|
|
822
|
+
const span = reads.find(d => d.b.span)?.b.span ?? deps.find(d => d.b.span)?.b.span ?? deps[0]?.a.span;
|
|
823
|
+
const valued = deps.find(d => d.v.role === 'value');
|
|
824
|
+
if (valued && valued.v.of) {
|
|
825
|
+
const name = varName(valued.v.of);
|
|
826
|
+
return {
|
|
827
|
+
severity: 'error', code: 'val-cycle', span: valued.b.span ?? span, kieler, related,
|
|
828
|
+
message: `\`val(${name})\` is read to emit \`${name}\` in the same tick, but \`val(${name})\` has to wait for every emission of \`${name}\``,
|
|
829
|
+
hint: `To build on the value of \`${name}\`, use last tick's value: \`${name}(pre(val(${name})) + 1)\`.`,
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
const preemption = preemptionCycle(cycle, deps, index);
|
|
833
|
+
if (preemption)
|
|
834
|
+
return { severity: 'error', kieler, related, ...preemption, span: preemption.span ?? span };
|
|
835
|
+
const detail = describeAccesses(reads);
|
|
836
|
+
// pre() only helps with a variable the reader wrote; a hidden one ("Cooking's suspend condition") has no pre.
|
|
837
|
+
const first = deps.map(d => d.v.of ?? d.v).find(v => !v.generated);
|
|
838
|
+
const breakIt = first ? `Break the cycle with \`pre(${varName(first)})\` (last tick's value), by making one side wait a tick,` : 'Break the cycle by making one side wait a tick,';
|
|
839
|
+
const visibleNames = [...new Set(deps.map(d => d.v.of ?? d.v).filter(v => !v.generated).map(say))];
|
|
840
|
+
const hiddenNames = [...new Set(deps.map(d => d.v.of ?? d.v).filter(v => v.generated).map(hiddenName))];
|
|
841
|
+
// Name what the reader wrote; the compiler's helpers only when there is nothing else (at most two of them).
|
|
842
|
+
const names = visibleNames.length ? visibleNames.slice(0, 4) : hiddenNames.slice(0, 2);
|
|
843
|
+
const more = visibleNames.length ? (visibleNames.length > 4 ? ' and others' : '') : (hiddenNames.length > 2 ? ` (and ${hiddenNames.length - 2} more of the compiler's helpers)` : '');
|
|
844
|
+
return {
|
|
845
|
+
severity: 'error', code: 'causality-cycle',
|
|
846
|
+
message: names.length === 1 && !more
|
|
847
|
+
? `${names[0]} is read before it can be written in the same tick (the writer depends on the reader)`
|
|
848
|
+
: `${quote(names)}${more} depend on ${names.length > 2 || more ? 'one another' : 'each other'} within one tick: each waits for the other`,
|
|
849
|
+
span,
|
|
850
|
+
kieler,
|
|
851
|
+
hint: `${detail ? detail + '. ' : ''}Within a tick every write of a variable, and every emission of a signal, comes before it is read. ${breakIt} or by moving the logic into one region.`,
|
|
852
|
+
related,
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* A count delay inside a state with a self-loop (preemption.md P-36a): KIELER keeps the count in a variable of the
|
|
857
|
+
* source's parent, and when the self-loop leaves and enters the state around it in one tick, resetting the count,
|
|
858
|
+
* counting and its `pre` register wait for each other. KIELER rejects every such chart, however deep the count sits.
|
|
859
|
+
*/
|
|
860
|
+
function countDelayResetLoop(deps, index) {
|
|
861
|
+
if (!index)
|
|
862
|
+
return undefined;
|
|
863
|
+
const counter = deps.map(d => d.v.of ?? d.v).find(v => v.role === 'counter');
|
|
864
|
+
const by = counter ? [...counter.tags].find(t => t.startsWith('#count-delay:'))?.slice('#count-delay:'.length) : undefined;
|
|
865
|
+
const m = by ? /^(.*)->(\d+)$/.exec(by) : null;
|
|
866
|
+
const source = m ? index.states.get(m[1]) : undefined;
|
|
867
|
+
const t = source?.transitions[Number(m?.[2]) - 1];
|
|
868
|
+
if (!source || !t)
|
|
869
|
+
return undefined;
|
|
870
|
+
// The nearest state around the count with a transition back to itself.
|
|
871
|
+
let loop;
|
|
872
|
+
for (let r = index.parentRegion.get(source); r && !loop;) {
|
|
873
|
+
const p = index.parentState.get(r);
|
|
874
|
+
if (!p)
|
|
875
|
+
break;
|
|
876
|
+
const self = p.transitions.find(x => x.target === p.id);
|
|
877
|
+
if (self)
|
|
878
|
+
loop = { state: p, t: self };
|
|
879
|
+
r = index.parentRegion.get(p);
|
|
880
|
+
}
|
|
881
|
+
if (!loop)
|
|
882
|
+
return undefined;
|
|
883
|
+
const P = loop.state.name;
|
|
884
|
+
const kind = loop.t.preemption === 'strong' ? 'abort to' : loop.t.preemption === 'termination' ? 'join to' : 'go to';
|
|
885
|
+
const loopText = `${loop.t.trigger ? `if ${formatExpression(loop.t.trigger)} ` : ''}${kind} ${P}`;
|
|
886
|
+
const count = `if ${t.triggerDelay} ${t.trigger ? formatExpression(t.trigger) : ''}`.trim();
|
|
887
|
+
const target = t.target ? index.states.get(t.target)?.name ?? t.targetName : t.targetName;
|
|
888
|
+
return {
|
|
889
|
+
code: 'count-delay-reset',
|
|
890
|
+
span: t.trigger?.span ?? t.span,
|
|
891
|
+
related: [by, loop.t.id, loop.state.id],
|
|
892
|
+
message: `KIELER cannot compile the count delay \`${count}\` on \`${source.name}\`'s transition to \`${target}\`: \`${P}\`, around it, has the self-loop \`${loopText}\``,
|
|
893
|
+
hint: `KIELER keeps the count in a variable of the state around \`${source.name}\`, and in a tick in which \`${loopText}\` leaves \`${P}\` and enters it again, restarting the count and counting wait for each other. It rejects every count delay inside a state with a self-loop. Count by hand, with a variable declared outside \`${P}\`: \`entry do count = 0\` and \`during if ${t.trigger ? formatExpression(t.trigger) : 'x'} do count++\` on \`${source.name}\`, and \`if count == ${t.triggerDelay}\` on its transition.`,
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
/** A generated variable as the reader can understand it (its display name; the compiler's names never leak). */
|
|
897
|
+
function hiddenName(v) {
|
|
898
|
+
return v.display ?? 'a variable the compiler adds';
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Cycles that come from preemption: a weak abort out of a state with a `period` (structure.md S-42a), a strong
|
|
902
|
+
* abort out of a state with its own suspend (the suspend freezes the state's own transitions, so the abort waits for
|
|
903
|
+
* the suspend condition, which the state's inside works out), and an abort whose trigger reads what the state's
|
|
904
|
+
* inside writes in the same tick (which KIELER accepts in simple cases only: tests/engine/kieler/preemption/
|
|
905
|
+
* probe-abort-trigger-inside-*).
|
|
906
|
+
*/
|
|
907
|
+
function preemptionCycle(cycle, deps, index) {
|
|
908
|
+
if (!index)
|
|
909
|
+
return undefined;
|
|
910
|
+
// A period guards the transitions out of its own state with its tick, which the state's inside works out
|
|
911
|
+
// (structure.md S-42a): a weak abort out of such a state waits for the tick, and the tick for the abort.
|
|
912
|
+
const tick = deps.map(d => d.v).find(v => v.role === 'tick');
|
|
913
|
+
if (tick) {
|
|
914
|
+
const state = tick.scope;
|
|
915
|
+
const plain = readableName(state) ?? 'S';
|
|
916
|
+
const name = readableName(state) ? `\`${readableName(state)}\`` : 'the state';
|
|
917
|
+
const source = index.states.get(sourceState(state) ?? '');
|
|
918
|
+
return {
|
|
919
|
+
code: 'period-abort-cycle',
|
|
920
|
+
span: source?.actions.find(a => a.kind === 'period')?.span ?? source?.transitions[0]?.span,
|
|
921
|
+
message: `${name}'s period and the transitions out of ${name} depend on each other within the tick`,
|
|
922
|
+
hint: `KIELER guards every transition inside a state with a \`period\` with the period's tick, the transitions out of ${name} included, and the tick is worked out inside ${name}. Put the period into a region inside ${name} (\`initial state ${plain} { region { period ... } }\`) and leave ${name} with a normal transition.`,
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
const transitionOf = (id) => {
|
|
926
|
+
const m = id ? /^(.*)->(\d+)$/.exec(id) : null;
|
|
927
|
+
const state = m ? index.states.get(m[1]) : undefined;
|
|
928
|
+
return state ? { state, t: state.transitions[Number(m[2]) - 1] } : undefined;
|
|
929
|
+
};
|
|
930
|
+
const aborts = cycle.map(n => transitionOf(n.transition?.checks ?? n.transition?.origin)).filter(x => x?.t && x.t.preemption !== 'termination');
|
|
931
|
+
const strong = aborts.find(x => x.t.preemption === 'strong');
|
|
932
|
+
// A strong abort out of a state with its own suspend: the flag of that state is in the cycle.
|
|
933
|
+
if (strong && deps.some(d => d.v.role === 'enabled' && sourceState(d.v.scope) === strong.state.id)) {
|
|
934
|
+
const name = `\`${strong.state.name}\``;
|
|
935
|
+
return {
|
|
936
|
+
code: 'suspend-abort-cycle',
|
|
937
|
+
message: `The strong abort out of ${name} and ${name}'s own suspend depend on each other within the tick`,
|
|
938
|
+
hint: `A strong abort is decided before anything inside ${name} runs, but ${name}'s \`suspend\` also freezes ${name}'s own transitions, so the abort waits for ${name}'s suspend condition, which is worked out inside ${name}. Move the abort to a state around ${name}: KIELER rejects every \`abort to\` out of a state that has its own \`suspend\`.`,
|
|
939
|
+
span: strong.t.span,
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
// An abort (weak or strong) whose trigger reads what the state's own inside writes in the same tick.
|
|
943
|
+
for (const abort of aborts) {
|
|
944
|
+
const S = abort.state;
|
|
945
|
+
const inside = (id) => !!id && (id.startsWith(`${S.id}.`) || id.startsWith(`${S.id}!`) || id.startsWith(`${S.id}:`));
|
|
946
|
+
const written = deps.filter(d => d.kind !== 'write-write' && inside(nodeElement(d.from)) && !(d.v.of ?? d.v).generated);
|
|
947
|
+
const vars = [...new Set(written.map(d => say(d.v.of ?? d.v)))];
|
|
948
|
+
if (!vars.length)
|
|
949
|
+
continue;
|
|
950
|
+
const first = varName(written[0].v.of ?? written[0].v);
|
|
951
|
+
const signal = written.some(d => d.v.role === 'presence' || d.v.role === 'curval' || (d.v.of ?? d.v).signal);
|
|
952
|
+
const what = quote(vars);
|
|
953
|
+
const verb = signal ? 'emitted' : 'set';
|
|
954
|
+
const kind = abort.t.preemption === 'strong' ? 'strong abort' : 'transition';
|
|
955
|
+
const target = abort.t.target ? index.states.get(abort.t.target)?.name ?? abort.t.targetName : abort.t.targetName;
|
|
956
|
+
return {
|
|
957
|
+
code: 'abort-trigger-cycle',
|
|
958
|
+
message: `The ${kind} from \`${S.name}\` to \`${target}\` reads ${what}, which ${vars.length > 1 ? 'are' : 'is'} ${verb} inside \`${S.name}\` in the same tick, and KIELER cannot put the two in an order`,
|
|
959
|
+
hint: `In each tick KIELER has to check this ${kind === 'transition' ? 'transition' : 'abort'} and run the inside of \`${S.name}\` in some order, and here each waits for the other. (KIELER accepts some charts whose transitions out of a superstate read a value written inside it, but not this one.) What always works: ${verb === 'emitted' ? 'emit' : 'set'} ${what} in a region next to \`${S.name}\`, outside it, or read last tick's value with \`pre(${first})\`.`,
|
|
960
|
+
span: abort.t.trigger?.span ?? abort.t.span,
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
return undefined;
|
|
964
|
+
}
|
|
965
|
+
/** `A -> B -> A` from transition ids `X.A->1`, `X.B->1` (when they chain). */
|
|
966
|
+
function loopPath(transitions) {
|
|
967
|
+
const short = (id) => id.replace(/->\d+$/, '').split(/[.:]/).pop();
|
|
968
|
+
const sources = transitions.map(short);
|
|
969
|
+
if (!sources.length || sources.length > 6)
|
|
970
|
+
return undefined;
|
|
971
|
+
return [...sources.reverse(), sources[0]].map(s => `\`${s}\``).join(' → ');
|
|
972
|
+
}
|
|
973
|
+
/** "x is written on line 7 and read on line 12; ..." for the read dependencies of a cycle. */
|
|
974
|
+
function describeAccesses(reads) {
|
|
975
|
+
const parts = [];
|
|
976
|
+
const seen = new Set();
|
|
977
|
+
for (const d of reads) {
|
|
978
|
+
const name = say(d.v.of ?? d.v);
|
|
979
|
+
const w = d.a.span?.line;
|
|
980
|
+
const r = d.b.span?.line;
|
|
981
|
+
if (w === undefined || r === undefined)
|
|
982
|
+
continue;
|
|
983
|
+
const key = `${name}:${w}:${r}`;
|
|
984
|
+
if (seen.has(key))
|
|
985
|
+
continue;
|
|
986
|
+
seen.add(key);
|
|
987
|
+
parts.push(`${name} is ${d.v.role === 'presence' || d.v.signal ? 'emitted' : 'set'} on line ${w} and read on line ${r}`);
|
|
988
|
+
if (parts.length >= 3)
|
|
989
|
+
break;
|
|
990
|
+
}
|
|
991
|
+
return parts.join('; ');
|
|
992
|
+
}
|
|
993
|
+
/** The dependencies the final schedule used. */
|
|
994
|
+
function analysisDeps(analysis) {
|
|
995
|
+
return analysis.deps;
|
|
996
|
+
}
|
|
997
|
+
export { allStates };
|
|
998
|
+
/** The first expression, effect or statement the lowering left as `unsupported`, as an `unsupported` info. */
|
|
999
|
+
function leftovers(root) {
|
|
1000
|
+
let found;
|
|
1001
|
+
const expr = (e) => walkExpr(e, x => { if (!found && x.k === 'unsupported')
|
|
1002
|
+
found = { what: x.what, span: x.span }; });
|
|
1003
|
+
const stmts = (list) => {
|
|
1004
|
+
for (const st of list) {
|
|
1005
|
+
if (found)
|
|
1006
|
+
return;
|
|
1007
|
+
if (st.k === 'do') {
|
|
1008
|
+
if (st.f.k === 'unsupported')
|
|
1009
|
+
found = { what: st.f.what, span: st.f.o.span };
|
|
1010
|
+
else
|
|
1011
|
+
effectExprs(st.f).forEach(expr);
|
|
1012
|
+
}
|
|
1013
|
+
else if (st.k === 'if') {
|
|
1014
|
+
expr(st.cond);
|
|
1015
|
+
stmts(st.then);
|
|
1016
|
+
if (st.else)
|
|
1017
|
+
stmts(st.else);
|
|
1018
|
+
}
|
|
1019
|
+
else if (st.k === 'while') {
|
|
1020
|
+
expr(st.cond);
|
|
1021
|
+
stmts(st.body);
|
|
1022
|
+
}
|
|
1023
|
+
else if (st.k === 'for') {
|
|
1024
|
+
stmts(st.init);
|
|
1025
|
+
expr(st.cond);
|
|
1026
|
+
stmts(st.update);
|
|
1027
|
+
stmts(st.body);
|
|
1028
|
+
}
|
|
1029
|
+
else if (st.k === 'return')
|
|
1030
|
+
expr(st.expr);
|
|
1031
|
+
else if (st.k === 'expr')
|
|
1032
|
+
expr(st.e);
|
|
1033
|
+
else if (st.k === 'unsupported')
|
|
1034
|
+
found = { what: st.what, span: st.span };
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
for (const site of allActionSites(root)) {
|
|
1038
|
+
const holder = site.transition ?? site.action;
|
|
1039
|
+
expr(holder.trigger);
|
|
1040
|
+
for (const f of holder.effects) {
|
|
1041
|
+
if (f.k === 'unsupported' && !found)
|
|
1042
|
+
found = { what: f.what, span: f.o.span };
|
|
1043
|
+
effectExprs(f).forEach(expr);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
for (const scope of allScopes(root)) {
|
|
1047
|
+
for (const v of scope.decls) {
|
|
1048
|
+
expr(v.init);
|
|
1049
|
+
v.dims.forEach(expr);
|
|
1050
|
+
if (v.method)
|
|
1051
|
+
stmts(v.method.body);
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
if (!found)
|
|
1055
|
+
return [];
|
|
1056
|
+
return [{
|
|
1057
|
+
severity: 'info', code: 'unsupported', span: found.span,
|
|
1058
|
+
message: `The guide's simulator cannot evaluate \`${found.what}\``,
|
|
1059
|
+
hint: 'This is a limit of the guide\'s simulator, not a mistake in the chart. The official KIELER tools may run it.',
|
|
1060
|
+
}];
|
|
1061
|
+
}
|
|
1062
|
+
//# sourceMappingURL=compile.js.map
|