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
13namespace mim {
14
15namespace {
16
17template<class T>
18std::string escape(const T& val) {
19 std::ostringstream oss;
20 oss << val;
21 auto str = oss.str();
22 find_and_replace(str, "<", "&lt;");
23 find_and_replace(str, ">", "&gt;");
24 return str;
25}
26
27class Dot {
28public:
29 Dot(std::ostream& ostream, bool types, const Def* root = nullptr)
30 : os_(ostream)
31 , types_(types)
32 , root_(root) {}
33
34 void prologue() {
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];");
39 }
40
41 void epilogue() { (--tab_).println(os_, "}}"); }
42
43 void run(const Def* root, uint32_t max) {
44 prologue();
45 recurse(root, max);
46 epilogue();
47 }
48
49 void recurse(const Def* def, uint32_t max) {
50 if (max == 0 || !done_.emplace(def).second) return;
51 tab_.print(os_, "_{}[", def->gid());
52 if (def->isa_mut())
53 if (def == root_)
54 os_ << "style=\"filled,diagonals,bold\"";
55 else
56 os_ << "style=\"filled,diagonals\"";
57 else if (def == root_)
58 os_ << "style=\"filled,bold\"";
59 label(def) << ',';
60 color(def) << ',';
61 if (def->free_vars().empty()) os_ << "rank=min,";
62 tooltip(def) << "];\n";
63
64 if (def->is_set()) {
65 for (size_t i = 0, e = def->num_ops(); i != e; ++i) {
66 auto op = def->op(i);
67 recurse(op, max - 1);
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");
71 else
72 print(os_, "];\n");
73 }
74 }
75
76 if (auto t = def->type(); t && types_) {
77 recurse(t, max - 1);
78 tab_.println(os_, "_{} -> _{}[color=\"#00000000\",constraint=false,style=dashed];", def->gid(), t->gid());
79 }
80 }
81
82 std::ostream& label(const Def* def) {
83 print(os_, "label=<{}<br/>", def->unique_name());
84 if (auto lit = def->isa<Lit>())
85 lit->stream(os_, 0);
86 else
87 os_ << def->node_name();
88 return os_ << '>';
89 }
90
91 std::ostream& color(const Def* def) {
92 return print(os_, "fillcolor=\"{} 0.5 0.75\"", float(def->node()) / float(Num_Nodes));
93 }
94
95 std::ostream& tooltip(const Def* def) {
96 static constexpr auto NL = "&#13;&#10;";
97
98 auto loc = escape(def->loc());
99 auto type = escape(def->type());
100 escape(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_, "\"");
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())
150 dot.recurse(external, uint32_t(-1));
151 if (anx)
152 for (auto annex : annexes())
153 dot.recurse(annex, uint32_t(-1));
154 dot.epilogue();
155}
156
157/*
158 * Nest
159 */
160
161void Nest::dot(const char* file) const {
162 if (!file) {
163 dot(std::cout);
164 } else {
165 auto of = std::ofstream(file);
166 dot(of);
167 }
168}
169
170void Nest::dot(std::ostream& os) const {
171 Tab tab;
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);
176 (--tab).println(os, "}}");
177}
178
179void Nest::Node::dot(Tab tab, std::ostream& os) const {
180 std::string s;
181 for (const auto& scc : topo_) {
182 s += '[';
183 for (auto sep = ""s; auto n : *scc) {
184 s += sep + n->name();
185 sep = ", ";
186 }
187 s += "] ";
188 }
189
190 for (auto dep : depends())
191 tab.println(os, "\"{}\":s -> \"{}\":s [style=dashed,constraint=false,splines=true]", this->name(), dep->name());
192
193 auto rec = is_mutually_recursive() ? "rec*" : (is_directly_recursive() ? "rec" : "");
194 tab.println(os, "\"{}\" [label=\"{} {} {}\",tooltip=\"{}\"]", name(), rec, name(), loop_depth(), s);
195 for (auto child : child_nodes()) {
196 tab.println(os, "\"{}\" -> \"{}\" [splines=false]", name(), child->name());
197 child->dot(tab, os);
198 }
199}
200
201} // namespace mim
void dot(std::ostream &os, uint32_t max=0xFFFFFF, bool types=false) const
Definition dot.cpp:126
const Def * op(size_t i) const noexcept
Definition def.h:308
constexpr u32 gid() const noexcept
Global id - unique number for this Def.
Definition def.h:270
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:170
const Node * root() const
Definition nest.h:129
Keeps track of indentation level.
Definition print.h:206
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:235
const Def * annex(Id id)
Lookup annex by Axm::id.
Definition world.h:181
auto externals() const
Definition world.h:205
Def * external(Sym name)
Lookup by name.
Definition world.h:204
auto annexes() const
Definition world.h:177
void dot(std::ostream &os, bool annexes=false, bool types=false) const
Dumps DOT to os.
Definition dot.cpp:146
const Def * op(trait o, const Def *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:74
GIDSet< const Def * > DefSet
Definition def.h:74
static constexpr size_t Num_Nodes
Definition def.h:119
std::ostream & println(std::ostream &os, const char *fmt, Args &&... args)
As above but end with std::endl.
Definition print.h:157
@ Nat
Definition def.h:114
@ Idx
Definition def.h:114
@ Var
Definition def.h:114
@ Axm
Definition def.h:114
@ Lit
Definition def.h:114