1use crate::ast::IrqKind;
15use crate::base::{ChanId, ProcId};
16use crate::ccfg::graph::{Edge, Label};
17use crate::error::Diagnostic;
18use crate::tast::{Comm, Event};
19
20use super::wellformed::Graph;
21
22const VOIDS: &str =
24 "a program that breaks this rule is outside the deadlock freedom fic guarantees";
25
26#[derive(Clone, Copy, PartialEq)]
28pub(super) enum Receive {
29 Chan(ChanId),
30 Irq(u32),
31}
32
33impl Receive {
34 fn of(edge: &Edge) -> Option<Receive> {
36 match &edge.label {
37 Some(Label::Event(event)) => Receive::of_event(event),
38 _ => None,
39 }
40 }
41
42 pub(super) fn of_event(event: &Event) -> Option<Receive> {
44 match event {
45 Event::Comm(Comm::Recv { chan, .. }) => Some(Receive::Chan(*chan)),
46 Event::Irq {
47 irq,
48 kind: IrqKind::Await,
49 } => Some(Receive::Irq(*irq)),
50 _ => None,
51 }
52 }
53}
54
55impl Graph<'_> {
56 pub(super) fn head(&mut self, notified: ProcId, head: &[&Edge]) {
59 if head.iter().any(|e| !e.blocks() || e.guard.is_some()) {
60 let decl = &self.view.body().processes[notified].sig;
61 self.found.push(Diagnostic::at(
62 format!(
63 "process `{}` bound to `microkit.notified` is not receive-headed: each path at its head must begin with an unguarded receive (`c?x`) or IRQ await (`irq?N`) — the kernel invokes it for a message; {VOIDS}",
64 decl.name
65 ),
66 decl.span.clone(),
67 self.file,
68 ));
69 }
70 for (i, edge) in head.iter().enumerate() {
71 let Some(on) = Receive::of(edge) else {
72 continue;
73 };
74 if head[..i].iter().any(|e| Receive::of(e) == Some(on)) {
75 let name = self.receive_name(on);
76 self.report(
77 edge.process,
78 self.event_span(edge),
79 &format!(
80 "two branches of the notified head receive on `{name}`: which one takes the \
81 message is not decided — receive once, and decide by guards after; {VOIDS}"
82 ),
83 );
84 }
85 }
86 }
87
88 pub(super) fn stray_receive(&mut self, edge: &Edge) {
90 let Some(on) = Receive::of(edge) else {
91 return;
92 };
93 let name = self.receive_name(on);
94 self.report(
95 edge.process,
96 self.event_span(edge),
97 &format!(
98 "a receive (`{name}…`) outside the head of the notified body: a message is taken only \
99 where the kernel's invocation starts — receive there, and decide by guards after; {VOIDS}"
100 ),
101 );
102 }
103
104 fn receive_name(&self, on: Receive) -> String {
106 match on {
107 Receive::Chan(c) => format!("{}?", self.view.chan(c).name),
108 Receive::Irq(n) => format!("irq?{n}"),
109 }
110 }
111}