1use std::cell::RefCell;
4use std::collections::HashMap;
5use std::panic::RefUnwindSafe;
6use std::rc::Rc;
7
8use dfir_lang::graph::{DfirGraph, FlatGraphBuilder, FlatGraphBuilderOutput};
9use libloading::Library;
10use slotmap::SparseSecondaryMap;
11
12use super::builder::SimBuilder;
13use super::compiled::{CompiledSim, CompiledSimInstance};
14use super::graph::{SimDeploy, SimExternal, SimNode, compile_sim, create_sim_graph_trybuild};
15use crate::compile::ir::HydroRoot;
16use crate::location::LocationKey;
17use crate::location::dynamic::LocationId;
18use crate::prelude::Cluster;
19use crate::sim::graph::SimExternalPortRegistry;
20use crate::staging_util::Invariant;
21
22pub struct SimFlow<'a> {
24 pub(crate) ir: Vec<HydroRoot>,
25
26 pub(crate) processes: SparseSecondaryMap<LocationKey, SimNode>,
28 pub(crate) clusters: SparseSecondaryMap<LocationKey, SimNode>,
30 pub(crate) externals: SparseSecondaryMap<LocationKey, SimExternal>,
32
33 pub(crate) cluster_max_sizes: SparseSecondaryMap<LocationKey, usize>,
35 pub(crate) externals_port_registry: Rc<RefCell<SimExternalPortRegistry>>,
37
38 pub(crate) _phantom: Invariant<'a>,
39}
40
41impl<'a> SimFlow<'a> {
42 pub fn with_cluster_size<C>(mut self, cluster: &Cluster<'a, C>, max_size: usize) -> Self {
44 self.cluster_max_sizes.insert(cluster.key, max_size);
45 self
46 }
47
48 pub fn with_instance<T>(self, thunk: impl FnOnce(CompiledSimInstance) -> T) -> T {
50 self.compiled().with_instance(thunk)
51 }
52
53 pub fn fuzz(self, thunk: impl AsyncFn() + RefUnwindSafe) {
64 self.compiled().fuzz(thunk)
65 }
66
67 pub fn exhaustive(self, thunk: impl AsyncFnMut() + RefUnwindSafe) -> usize {
78 self.compiled().exhaustive(thunk)
79 }
80
81 pub fn compiled(mut self) -> CompiledSim {
83 use std::collections::BTreeMap;
84
85 use dfir_lang::graph::{eliminate_extra_unions_tees, partition_graph};
86
87 let mut sim_emit = SimBuilder {
88 process_graphs: BTreeMap::new(),
89 cluster_graphs: BTreeMap::new(),
90 process_tick_dfirs: BTreeMap::new(),
91 cluster_tick_dfirs: BTreeMap::new(),
92 extra_stmts_global: vec![],
93 extra_stmts_cluster: BTreeMap::new(),
94 next_hoff_id: 0,
95 };
96
97 self.externals.insert(
99 LocationKey::FIRST,
100 SimExternal {
101 shared_inner: self.externals_port_registry.clone(),
102 },
103 );
104
105 let mut seen_tees_instantiate: HashMap<_, _> = HashMap::new();
106 self.ir.iter_mut().for_each(|leaf| {
107 leaf.compile_network::<SimDeploy>(
108 &mut SparseSecondaryMap::new(),
109 &mut seen_tees_instantiate,
110 &self.processes,
111 &self.clusters,
112 &self.externals,
113 );
114 });
115
116 let mut seen_tees = HashMap::new();
117 let mut built_tees = HashMap::new();
118 let mut next_stmt_id = 0;
119 for leaf in &mut self.ir {
120 leaf.emit::<SimDeploy>(
121 &mut sim_emit,
122 &mut seen_tees,
123 &mut built_tees,
124 &mut next_stmt_id,
125 );
126 }
127
128 fn build_graphs(
129 graphs: BTreeMap<LocationId, FlatGraphBuilder>,
130 ) -> BTreeMap<LocationId, DfirGraph> {
131 graphs
132 .into_iter()
133 .map(|(l, g)| {
134 let FlatGraphBuilderOutput { mut flat_graph, .. } =
135 g.build().expect("Failed to build DFIR flat graph.");
136 eliminate_extra_unions_tees(&mut flat_graph);
137 (
138 l,
139 partition_graph(flat_graph).expect("Failed to partition (cycle detected)."),
140 )
141 })
142 .collect()
143 }
144
145 let process_graphs = build_graphs(sim_emit.process_graphs);
146 let cluster_graphs = build_graphs(sim_emit.cluster_graphs);
147 let process_tick_graphs = build_graphs(sim_emit.process_tick_dfirs);
148 let cluster_tick_graphs = build_graphs(sim_emit.cluster_tick_dfirs);
149
150 #[expect(
151 clippy::disallowed_methods,
152 reason = "nondeterministic iteration order, fine for checks"
153 )]
154 for c in self.clusters.keys() {
155 assert!(
156 self.cluster_max_sizes.contains_key(c),
157 "Cluster {:?} missing max size; call with_cluster_size() before compiled()",
158 c
159 );
160 }
161
162 let (bin, trybuild) = create_sim_graph_trybuild(
163 process_graphs,
164 cluster_graphs,
165 self.cluster_max_sizes,
166 process_tick_graphs,
167 cluster_tick_graphs,
168 sim_emit.extra_stmts_global,
169 sim_emit.extra_stmts_cluster,
170 );
171
172 let out = compile_sim(bin, trybuild).unwrap();
173 let lib = unsafe { Library::new(&out).unwrap() };
174
175 CompiledSim {
176 _path: out,
177 lib,
178 externals_port_registry: self.externals_port_registry.take(),
179 }
180 }
181}