11using namespace std::string_literals;
18std::string
escape(
const T& val) {
19 std::ostringstream oss;
29 Dot(std::ostream& ostream,
bool types,
const Def* root =
nullptr)
35 (tab_++).
println(os_,
"digraph {{");
36 tab_.println(os_,
"ordering=out;");
37 tab_.println(os_,
"splines=false;");
38 tab_.println(os_,
"node [shape=box,style=filled];");
41 void epilogue() { (--tab_).
println(os_,
"}}"); }
43 void run(
const Def* root, uint32_t max) {
49 void recurse(
const Def* def, uint32_t max) {
50 if (max == 0 || !done_.emplace(def).second)
return;
51 tab_.print(os_,
"_{}[", def->gid());
54 os_ <<
"style=\"filled,diagonals,bold\"";
56 os_ <<
"style=\"filled,diagonals\"";
57 else if (def == root_)
58 os_ <<
"style=\"filled,bold\"";
61 if (def->free_vars().empty()) os_ <<
"rank=min,";
62 tooltip(def) <<
"];\n";
65 for (
size_t i = 0, e = def->num_ops(); i != e; ++i) {
68 tab_.print(os_,
"_{} -> _{}[taillabel=\"{}\",", def->gid(),
op->
gid(), i);
69 if (
op->isa<
Lit>() ||
op->isa<
Axm>() || def->isa<
Var>() || def->isa<
Nat>() || def->isa<
Idx>())
70 print(os_,
"fontcolor=\"#00000000\",color=\"#00000000\",constraint=false];\n");
76 if (
auto t = def->type(); t && types_) {
78 tab_.println(os_,
"_{} -> _{}[color=\"#00000000\",constraint=false,style=dashed];", def->gid(),
t->gid());
82 std::ostream& label(
const Def* def) {
83 print(os_,
"label=<{}<br/>", def->unique_name());
84 if (
auto lit = def->isa<
Lit>())
87 os_ << def->node_name();
91 std::ostream& color(
const Def* def) {
92 return print(os_,
"fillcolor=\"{} 0.5 0.75\"",
float(def->node()) /
float(
Num_Nodes));
95 std::ostream& tooltip(
const Def* def) {
96 static constexpr auto NL =
" ";
98 auto loc =
escape(def->loc());
101 print(os_,
"tooltip=\"");
102 print(os_,
"<b>expr:</b> {}{}", def, NL);
103 print(os_,
"<b>type:</b> {}{}", type, NL);
104 print(os_,
"<b>name:</b> {}{}", def->sym(), NL);
105 print(os_,
"<b>gid:</b> {}{}", def->gid(), NL);
106 print(os_,
"<b>flags:</b> 0x{x}{}", def->flags(), NL);
107 print(os_,
"<b>mark:</b> 0x{x}{}", def->mark(), NL);
108 print(os_,
"<b>local_muts:</b> {, }{}", def->local_muts(), NL);
109 print(os_,
"<b>local_vars:</b> {, }{}", def->local_vars(), NL);
110 print(os_,
"<b>free_vars:</b> {, }{}", def->free_vars(), NL);
111 if (
auto mut = def->isa_mut())
print(os_,
"<b>users:</b> {{{, }}}{}", mut->users(), NL);
112 print(os_,
"<b>loc:</b> {}", loc);
113 return print(os_,
"\"");
126void Def::dot(std::ostream& ostream, uint32_t max,
bool types)
const { Dot(ostream, types,
this).run(
this, max); }
128void Def::dot(
const char* file, uint32_t max,
bool types)
const {
130 dot(std::cout, max, types);
132 auto of = std::ofstream(file);
141 auto of = std::ofstream(file);
146void World::dot(std::ostream& os,
bool anx,
bool types)
const {
165 auto of = std::ofstream(file);
172 (tab++).
println(os,
"digraph {{");
173 tab.
println(os,
"ordering=out;");
174 tab.
println(os,
"node [shape=box,style=filled];");
175 root()->dot(tab, os);
179void Nest::Node::dot(
Tab tab, std::ostream& os)
const {
181 for (
const auto& scc : topo_) {
183 for (
auto sep =
""s;
auto n : *scc) {
184 s += sep + n->name();
191 tab.
println(os,
"\"{}\":s -> \"{}\":s [style=dashed,constraint=false,splines=true]", this->
name(), dep->name());
void dot(std::ostream &os, uint32_t max=0xFFFFFF, bool types=false) const
const Def * op(size_t i) const noexcept
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
const Node * child(Def *mut) const
bool is_directly_recursive() const
bool is_mutually_recursive() const
uint32_t loop_depth() const
void dot(std::ostream &os) const
const Node * root() const
Keeps track of indentation level.
std::ostream & println(std::ostream &os, const char *s, Args &&... args)
Same as Tab::print but appends a std::endl to os.
const Def * annex(Id id)
Lookup annex by Axm::id.
Def * external(Sym name)
Lookup by name.
void dot(std::ostream &os, bool annexes=false, bool types=false) const
Dumps DOT to os.
const Def * op(trait o, const Def *type)
int run(std::string cmd, std::string args={})
Wraps sys::system and puts .exe at the back (Windows) and ./ at the front (otherwise) of cmd.
std::string escape(const std::filesystem::path &path)
Returns the path as std::string and escapes all whitespaces with backslash.
std::ostream & print(std::ostream &os, const char *s)
Base case.
void find_and_replace(std::string &str, std::string_view what, std::string_view repl)
Replaces all occurrences of what with repl.
GIDSet< const Def * > DefSet
static constexpr size_t Num_Nodes
std::ostream & println(std::ostream &os, const char *fmt, Args &&... args)
As above but end with std::endl.