MimIR 0.1
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
dot.cpp
Go to the documentation of this file.
1#include <fstream>
2#include <ostream>
3#include <sstream>
4
5#include "mim/def.h"
6#include "mim/nest.h"
7#include "mim/world.h"
8
9#include "mim/util/print.h"
10
11using namespace std::string_literals;
12
13// Do not zonk here!
14// We want to see all Refs in the DOT graph.
15
16namespace mim {
17
18namespace {
19
20template<class T> std::string escape(const T& val) {
21 std::ostringstream oss;
22 oss << val;
23 auto str = oss.str();
24 find_and_replace(str, "<", "&lt;");
25 find_and_replace(str, ">", "&gt;");
26 return str;
27}
28
29class Dot {
30public:
31 Dot(std::ostream& ostream, bool types, const Def* root = nullptr)
32 : os_(ostream)
33 , types_(types)
34 , root_(root) {}
35
36 void prologue() {
37 (tab_++).println(os_, "digraph {{");
38 tab_.println(os_, "ordering=out;");
39 tab_.println(os_, "splines=false;");
40 tab_.println(os_, "node [shape=box,style=filled];");
41 }
42
43 void epilogue() { (--tab_).println(os_, "}}"); }
44
45 void run(const Def* root, uint32_t max) {
46 prologue();
47 recurse(root, max);
48 epilogue();
49 }
50
51 void recurse(const Def* def, uint32_t max) {
52 if (max == 0 || !done_.emplace(def).second) return;
53 tab_.print(os_, "_{}[", def->gid());
54 if (def->isa_mut())
55 if (def == root_)
56 os_ << "style=\"filled,diagonals,bold\"";
57 else
58 os_ << "style=\"filled,diagonals\"";
59 else if (def == root_)
60 os_ << "style=\"filled,bold\"";
61 label(def) << ',';
62 color(def) << ',';
63 if (def->free_vars().empty()) os_ << "rank=min,";
64 tooltip(def) << "];\n";
65
66 if (!def->is_set()) return;
67
68 for (size_t i = 0, e = def->num_ops(); i != e; ++i) {
69 auto op = def->op(i).def();
70 recurse(op, max - 1);
71 tab_.print(os_, "_{} -> _{}[taillabel=\"{}\",", def->gid(), op->gid(), i);
72 if (op->isa<Lit>() || op->isa<Axiom>() || def->isa<Var>() || def->isa<Nat>() || def->isa<Idx>())
73 print(os_, "fontcolor=\"#00000000\",color=\"#00000000\",constraint=false];\n");
74 else
75 print(os_, "];\n");
76 }
77
78 if (auto t = def->type().def(); t && types_) {
79 recurse(t, max - 1);
80 tab_.println(os_, "_{} -> _{}[color=\"#00000000\",constraint=false,style=dashed];", def->gid(), t->gid());
81 }
82 }
83
84 std::ostream& label(const Def* def) {
85 print(os_, "label=<{}<br/>", def->unique_name());
86 if (auto lit = def->isa<Lit>())
87 lit->stream(os_, 0);
88 else
89 os_ << def->node_name();
90 return os_ << '>';
91 }
92
93 std::ostream& color(const Def* def) {
94 return print(os_, "fillcolor=\"{} 0.5 0.75\"", def->node() / (float)Node::Num_Nodes);
95 }
96
97 std::ostream& tooltip(const Def* def) {
98 static constexpr auto NL = "&#13;&#10;";
99 auto loc = escape(def->loc());
100 auto type = escape(def->type().def());
101 escape(loc);
102 print(os_, "tooltip=\"");
103 print(os_, "<b>expr:</b> {}{}", def, NL);
104 print(os_, "<b>type:</b> {}{}", type, NL);
105 print(os_, "<b>name:</b> {}{}", def->sym(), NL);
106 print(os_, "<b>gid:</b> {}{}", def->gid(), NL);
107 print(os_, "<b>flags:</b> 0x{x}{}", def->flags(), NL);
108 print(os_, "<b>free_vars:</b> {{{, }}}{}", def->free_vars(), NL);
109 print(os_, "<b>local_vars:</b> {{{, }}}{}", def->local_vars(), NL);
110 print(os_, "<b>local_muts:</b> {{{, }}}{}", def->local_muts(), 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_, "\"");
114 }
115
116private:
117 std::ostream& os_;
118 bool types_;
119 const Def* root_;
120 Tab tab_;
121 DefSet done_;
122};
123
124} // namespace
125
126void Def::dot(std::ostream& ostream, uint32_t max, bool types) const { Dot(ostream, types, this).run(this, max); }
127
128void Def::dot(const char* file, uint32_t max, bool types) const {
129 if (!file) {
130 dot(std::cout, max, types);
131 } else {
132 auto of = std::ofstream(file);
133 dot(of, max, types);
134 }
135}
136
137void World::dot(const char* file, bool annexes, bool types) const {
138 if (!file) {
139 dot(std::cout, annexes, types);
140 } else {
141 auto of = std::ofstream(file);
142 dot(of, annexes, types);
143 }
144}
145
146void World::dot(std::ostream& os, bool anx, bool types) const {
147 Dot dot(os, types);
148 dot.prologue();
149 for (auto external : externals()) dot.recurse(external, uint32_t(-1));
150 if (anx)
151 for (auto annex : annexes()) dot.recurse(annex, uint32_t(-1));
152 dot.epilogue();
153}
154
155/*
156 * Nest
157 */
158
159void Nest::dot(const char* file) const {
160 if (!file) {
161 dot(std::cout);
162 } else {
163 auto of = std::ofstream(file);
164 dot(of);
165 }
166}
167
168void Nest::dot(std::ostream& os) const {
169 Tab tab;
170 (tab++).println(os, "digraph {{");
171 tab.println(os, "ordering=out;");
172 tab.println(os, "node [shape=box,style=filled];");
173 root()->dot(tab, os);
174 (--tab).println(os, "}}");
175}
176
177void Nest::Node::dot(Tab tab, std::ostream& os) const {
178 std::string s;
179 for (const auto& scc : topo_) {
180 s += '[';
181 for (auto sep = ""s; auto n : *scc) {
182 s += sep + n->name();
183 sep = ", ";
184 }
185 s += "] ";
186 }
187
188 for (auto dep : depends())
189 tab.println(os, "\"{}\":s -> \"{}\":s [style=dashed,constraint=false,splines=true]", this->name(), dep->name());
190
191 auto rec = is_mutually_recursive() ? "rec*" : (is_directly_recursive() ? "rec" : "");
192 tab.println(os, "\"{}\" [label=\"{} {} {}\",tooltip=\"{}\"]", name(), rec, name(), loop_depth(), s);
193 for (auto child : child_nodes()) {
194 tab.println(os, "\"{}\" -> \"{}\" [splines=false]", name(), child->name());
195 child->dot(tab, os);
196 }
197}
198
199} // namespace mim
Ref op(size_t i) const noexcept
Definition def.h:264
void dot(std::ostream &os, uint32_t max=0xFFFFFF, bool types=false) const
Definition dot.cpp:126
constexpr u32 gid() const noexcept
Definition def.h:231
std::string name() const
Definition nest.h:17
const Node * child(Def *mut) const
Definition nest.h:42
auto child_nodes() const
Definition nest.h:33
auto depends() const
Definition nest.h:54
bool is_directly_recursive() const
Definition nest.h:74
bool is_mutually_recursive() const
Definition nest.h:73
uint32_t loop_depth() const
Definition nest.h:27
void dot(std::ostream &os) const
Definition dot.cpp:168
const Node * root() const
Definition nest.h:129
const Def * def() const
Retrieve wrapped Def without Infer::refering.
Definition def.h:97
Keeps track of indentation level.
Definition print.h:195
std::ostream & println(std::ostream &os, const char *s, Args &&... args)
Same as Tab::print but appends a std::endl to os.
Definition print.h:220
auto externals() const
Definition world.h:192
Def * external(Sym name)
Lookup by name.
Definition world.h:191
auto annexes() const
Definition world.h:168
void dot(std::ostream &os, bool annexes=false, bool types=false) const
Dumps DOT to os.
Definition dot.cpp:146
const Def * annex()
Get Axiom from a plugin.
Definition world.h:180
constexpr auto Num_Nodes
Definition def.h:44
Ref op(trait o, Ref type)
Definition core.h:33
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.
Definition sys.cpp:77
std::string escape(const std::filesystem::path &path)
Returns the path as std::string and escapes all whitespaces with backslash.
Definition sys.cpp:86
Definition ast.h:14
std::ostream & print(std::ostream &os, const char *s)
Base case.
Definition print.cpp:5
void find_and_replace(std::string &str, std::string_view what, std::string_view repl)
Replaces all occurrences of what with repl.
Definition util.h:72
GIDSet< const Def * > DefSet
Definition def.h:59
std::ostream & println(std::ostream &os, const char *fmt, Args &&... args)
As above but end with std::endl.
Definition print.h:150