-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
353 additions
and
43 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 |
---|---|---|
@@ -1,67 +1,42 @@ | ||
use crate::Function; | ||
use super::printer::{ ToEirTextFun, ToEirTextContext }; | ||
|
||
use petgraph::visit::{ Dfs, IntoNeighbors }; | ||
|
||
const DOT_BREAK: &str = "<br align=\"left\" />"; | ||
use libeir_util_dot_graph::GraphPrinter; | ||
|
||
fn format_label(label: &str) -> String { | ||
label | ||
.replace("{", "\\{") | ||
.replace("}", "\\}") | ||
.replace("|", "\\|") | ||
.replace(">", "<") | ||
.replace("<", ">") | ||
.replace("&", "&") | ||
.replace("\"", """) | ||
.replace("\n", DOT_BREAK) | ||
} | ||
|
||
use std::io::Write; | ||
use petgraph::visit::{ Dfs, IntoNeighbors }; | ||
|
||
#[allow(clippy::write_with_newline)] | ||
pub fn function_to_dot(fun: &Function, w: &mut dyn Write) -> ::std::io::Result<()> { | ||
pub fn function_into_graph_printer<O>(fun: &Function, g: &mut GraphPrinter<O>) | ||
where | ||
O: std::fmt::Write, | ||
{ | ||
let mut to_eir_ctx = ToEirTextContext::new(); | ||
|
||
write!(w, "digraph g {{\n")?; | ||
write!(w, "node [labeljust=\"l\", shape=record, fontname=\"Courier New\"]\n")?; | ||
write!(w, "edge [fontname=\"Courier New\" ]\n\n")?; | ||
|
||
let fun_name = format_label(&format!("{}", fun.ident())); | ||
write!(w, "entry [ label=<entry|fun: {}> ];\n", | ||
fun_name)?; | ||
write!(w, "entry -> blk_{};\n\n", fun.block_entry())?; | ||
|
||
let mut buf = Vec::new(); | ||
|
||
write!(w, "constants [ label=<")?; | ||
super::printer::print_constants(&mut to_eir_ctx, fun, 0, &mut buf)?; | ||
super::printer::print_constants(&mut to_eir_ctx, fun, 0, &mut buf).unwrap(); | ||
let text = std::str::from_utf8(&buf).unwrap(); | ||
let text = format_label(text); | ||
write!(w, "{}", text)?; | ||
write!(w, "> ];\n")?; | ||
g.node("constants", text); | ||
|
||
let block_graph = fun.block_graph(); | ||
let mut block_dfs = Dfs::new(&block_graph, fun.block_entry()); | ||
|
||
while let Some(block) = block_dfs.next(&block_graph) { | ||
write!(w, "blk_{} [ label=<", block)?; | ||
let block_val = fun.block_value(block); | ||
|
||
buf.clear(); | ||
block.to_eir_text_fun(&mut to_eir_ctx, fun, 0, &mut buf).unwrap(); | ||
let text = std::str::from_utf8(&buf).unwrap(); | ||
let text = format_label(text); | ||
write!(w, "{}", text)?; | ||
|
||
write!(w, "> ];\n")?; | ||
g.node(block_val, text); | ||
|
||
for out in block_graph.neighbors(block) { | ||
write!(w, "blk_{} -> blk_{};\n", block, out)?; | ||
let out_val = fun.block_value(out); | ||
g.edge(block_val, out_val, ""); | ||
} | ||
|
||
write!(w, "\n")?; | ||
} | ||
} | ||
|
||
write!(w, "}}\n")?; | ||
Ok(()) | ||
pub fn function_to_dot(fun: &Function) -> String { | ||
let mut g = GraphPrinter::new(); | ||
function_into_graph_printer(fun, &mut g); | ||
g.finish().unwrap() | ||
} |
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,9 @@ | ||
[package] | ||
name = "libeir_util_dot_graph" | ||
version = "0.1.0" | ||
authors = ["Hans Elias B. Josephsen <[email protected]>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
Oops, something went wrong.