Skip to main content

fic/ccfg/
graph.rs

1//! A graph and its labels: the object `fidus`'s `Ccfg.lean`
2//! formalises,
3//!
4//! ```text
5//! Edge (ℓ, g, λ, ℓ')   src, guard, at most one label, target
6//! Ccfg (ℓ₀, X, E)      init, exits, edges
7//! ```
8//!
9//! Locations are anonymous numbers. A guard is an edge's field beside its
10//! label; the label is what the transition system observes: one of
11//! `tast`'s events (a function's run, a send, a receive into its binder,
12//! an IRQ) or `Div`'s hang. Guard and label are `tast`'s nodes, their
13//! variables the proc vars of the process the edge was written in. An
14//! event-less edge is a guarded edge to an exit. A graph is one
15//! invocation of its entry point; re-invocation is the scheduler's.
16
17use std::collections::{BTreeMap, BTreeSet};
18
19use serde::Serialize;
20
21use crate::ast::IrqKind;
22use crate::base::ProcId;
23use crate::tast::{Comm, Event, Expr};
24
25/// A control location: a number, with no store valuation.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
27pub struct Location(pub usize);
28
29/// What an edge fires.
30#[derive(Debug, Clone, Serialize)]
31pub enum Label {
32    /// A computation, channel or IRQ event.
33    Event(Event),
34    /// `Div`'s divergence.
35    Hang,
36}
37
38impl Label {
39    /// Whether firing this label parks the invocation until an event is
40    /// delivered: a receive, or an IRQ await.
41    #[must_use]
42    pub fn blocks(&self) -> bool {
43        matches!(
44            self,
45            Label::Event(
46                Event::Comm(Comm::Recv { .. })
47                    | Event::Irq {
48                        kind: IrqKind::Await,
49                        ..
50                    }
51            )
52        )
53    }
54}
55
56/// An edge `(ℓ, g, λ, ℓ')`.
57#[derive(Debug, Clone, Serialize)]
58pub struct Edge {
59    /// Source location.
60    pub src: Location,
61    /// The process the guard and label were written in: their body vars are
62    /// its.
63    pub process: ProcId,
64    /// Pending guard. `None` is "no guard", distinct from `[⊤]`.
65    pub guard: Option<Expr>,
66    /// The label fired. `None` is an event-less edge to an exit.
67    pub label: Option<Label>,
68    /// Target location.
69    pub target: Location,
70}
71
72impl Edge {
73    /// Whether the edge parks the invocation (its label does).
74    #[must_use]
75    pub fn blocks(&self) -> bool {
76        self.label.as_ref().is_some_and(Label::blocks)
77    }
78}
79
80/// One entry point's graph `(ℓ₀, X, E)`: one invocation.
81#[derive(Debug, Clone, Serialize)]
82pub struct Ccfg {
83    /// Where the invocation starts.
84    pub init: Location,
85    /// Where the invocation ends.
86    pub exits: BTreeSet<Location>,
87    /// Every edge, in flattening order.
88    pub edges: Vec<Edge>,
89    /// Which process or step a location came from. An annotation for
90    /// printing and diagnostics, read by nothing semantic.
91    pub names: BTreeMap<Location, String>,
92}
93
94impl Ccfg {
95    /// The edges out of `l`, in flattening order.
96    pub fn out_edges(&self, l: Location) -> impl Iterator<Item = &Edge> {
97        self.edges.iter().filter(move |e| e.src == l)
98    }
99}