-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[c_cfg] Implement importing
CCFG
to ctrl_flow_struct
- Loading branch information
Showing
4 changed files
with
179 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use super::{ActionEdge, ActionNode, CCFGEdge, CCFGNode, CCFG}; | ||
use backend::ctrl_flow_struct as flstr; | ||
use backend::ctrl_flow_struct::ast_context::{AstContext, AstContextMut}; | ||
use backend::lang_c::c_ast; | ||
|
||
use petgraph::prelude::*; | ||
use petgraph::visit::{Dfs, EdgeFiltered, IntoNodeReferences, VisitMap}; | ||
|
||
pub fn structure_and_convert(ccfg: CCFG) -> Option<c_ast::CAST> { | ||
let cstore = flstr::condition::Storage::new(); | ||
let flstr_cfg = import(cstore.cctx(), ccfg)?; | ||
unimplemented!() | ||
} | ||
|
||
fn import<'cd>( | ||
cctx: flstr::condition::Context<'cd, NodeIndex>, | ||
ccfg: CCFG, | ||
) -> Option<flstr::ControlFlowGraph<'cd, CCFG>> { | ||
let mut new_graph = { | ||
let ef = EdgeFiltered::from_fn(&ccfg.g, |e| { | ||
// ignore `Normal` edges from `Goto` nodes | ||
match (&ccfg.g[e.source()], e.weight()) { | ||
(CCFGNode::Action(ActionNode::Goto), CCFGEdge::Action(ActionEdge::Normal)) => false, | ||
(_, CCFGEdge::Action(_)) => true, | ||
_ => false, | ||
} | ||
}); | ||
let reachable_actions = { | ||
let mut dfs = Dfs::new(&ef, ccfg.entry); | ||
while let Some(_) = dfs.next(&ef) {} | ||
dfs.discovered | ||
}; | ||
|
||
if ccfg | ||
.g | ||
.node_references() | ||
.filter(|(n, _)| reachable_actions.is_visited(n)) | ||
.any(|(_, nw)| !is_action_node(nw)) | ||
{ | ||
return None; | ||
} | ||
|
||
try_filter_map_to_stable( | ||
&ccfg.g, | ||
|n, nw| { | ||
Some(if reachable_actions.is_visited(&n) { | ||
if let CCFGNode::Action(ActionNode::If) = nw { | ||
Some(flstr::mk_cond_node(cctx, ccfg.branch_condition(n)?)) | ||
} else { | ||
Some(flstr::mk_code_node(n)) | ||
} | ||
} else { | ||
None | ||
}) | ||
}, | ||
|e, ew| { | ||
// `Some(_)` <=> keep edge | ||
// `None` <=> ignore edge | ||
// `return None` <=> error | ||
Some(match (&ccfg.g[ccfg.g.edge_endpoints(e).unwrap().0], ew) { | ||
(CCFGNode::Action(ActionNode::If), CCFGEdge::Action(ActionEdge::IfThen)) => { | ||
Some(flstr::CfgEdge::True) | ||
} | ||
(CCFGNode::Action(ActionNode::If), CCFGEdge::Action(ActionEdge::IfElse)) => { | ||
return None | ||
} | ||
(CCFGNode::Action(ActionNode::If), CCFGEdge::Action(ActionEdge::Normal)) => { | ||
Some(flstr::CfgEdge::False) | ||
} | ||
(CCFGNode::Action(ActionNode::Goto), CCFGEdge::Action(ActionEdge::GotoDst)) => { | ||
Some(flstr::CfgEdge::True) | ||
} | ||
(CCFGNode::Action(ActionNode::Goto), CCFGEdge::Action(ActionEdge::Normal)) => { | ||
None | ||
} | ||
(_, CCFGEdge::Action(ActionEdge::Normal)) => Some(flstr::CfgEdge::True), | ||
(_, CCFGEdge::Value(_)) => None, | ||
(_, CCFGEdge::Action(_)) => return None, | ||
}) | ||
}, | ||
)? | ||
}; | ||
|
||
let exit = new_graph.add_node(flstr::empty_node()); | ||
Some(flstr::ControlFlowGraph { | ||
graph: new_graph, | ||
entry: ccfg.entry, | ||
exit, | ||
cctx, | ||
actx: ccfg, | ||
}) | ||
} | ||
|
||
impl AstContext for CCFG { | ||
type Block = NodeIndex; | ||
type Variable = String; | ||
type BoolVariable = String; | ||
type Condition = NodeIndex; | ||
} | ||
|
||
/// based on https://docs.rs/petgraph/0.4.12/src/petgraph/graph_impl/mod.rs.html#1293-1317 | ||
fn try_filter_map_to_stable<'a, N, E, F, G, N2, E2>( | ||
graph: &'a Graph<N, E>, | ||
mut node_map: F, | ||
mut edge_map: G, | ||
) -> Option<StableGraph<N2, E2>> | ||
where | ||
F: FnMut(NodeIndex, &'a N) -> Option<Option<N2>>, | ||
G: FnMut(EdgeIndex, &'a E) -> Option<Option<E2>>, | ||
{ | ||
let mut g = StableGraph::new(); | ||
// mapping from old node index to new node index, end represents removed. | ||
let mut node_index_map = vec![NodeIndex::end(); graph.node_count()]; | ||
for (i, node) in graph.node_references() { | ||
if let Some(nw) = node_map(i, node)? { | ||
node_index_map[i.index()] = g.add_node(nw); | ||
} | ||
} | ||
for edge in graph.edge_references() { | ||
// skip edge if any endpoint was removed | ||
let source = node_index_map[edge.source().index()]; | ||
let target = node_index_map[edge.target().index()]; | ||
if source != NodeIndex::end() && target != NodeIndex::end() { | ||
if let Some(ew) = edge_map(edge.id(), edge.weight())? { | ||
g.add_edge(source, target, ew); | ||
} | ||
} | ||
} | ||
Some(g) | ||
} | ||
|
||
fn is_action_node(e: &CCFGNode) -> bool { | ||
if let CCFGNode::Action(_) = e { | ||
true | ||
} else { | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters