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/world.h"
7
8#include "mim/util/print.h"
9
10namespace mim {
11
12namespace {
13
14template<class T> std::string escape(const T& val) {
15 std::ostringstream oss;
16 oss << val;
17 auto str = oss.str();
18 find_and_replace(str, "<", "&lt;");
19 find_and_replace(str, ">", "&gt;");
20 return str;
21}
22
23class Dot {
24public:
25 Dot(std::ostream& ostream, bool types, const Def* root = nullptr)
26 : os_(ostream)
27 , types_(types)
28 , root_(root) {}
29
30 void prologue() {
31 (tab_++).println(os_, "digraph {{");
32 tab_.println(os_, "ordering=out;");
33 tab_.println(os_, "splines=false;");
34 tab_.println(os_, "node [shape=box,style=filled];");
35 }
36
37 void epilogue() { (tab_--).println(os_, "}}"); }
38
39 void run(const Def* root, uint32_t max) {
40 prologue();
41 recurse(root, max);
42 epilogue();
43 }
44
45 void recurse(const Def* def, uint32_t max) {
46 if (max == 0 || !done_.emplace(def).second) return;
47 tab_.print(os_, "_{}[", def->gid());
48 if (def->isa_mut())
49 if (def == root_)
50 os_ << "style=\"filled,diagonals,bold\"";
51 else
52 os_ << "style=\"filled,diagonals\"";
53 else if (def == root_)
54 os_ << "style=\"filled,bold\"";
55 label(def) << ',';
56 color(def) << ',';
57 if (def->free_vars().empty()) os_ << "rank=min,";
58 tooltip(def) << "];\n";
59
60 if (!def->is_set()) return;
61
62 for (size_t i = 0, e = def->num_ops(); i != e; ++i) {
63 auto op = def->op(i);
64 recurse(op, max - 1);
65 tab_.print(os_, "_{} -> _{}[taillabel=\"{}\",", def->gid(), op->gid(), i);
66 if (op->isa<Lit>() || op->isa<Axiom>() || def->isa<Var>() || def->isa<Nat>() || def->isa<Idx>())
67 print(os_, "fontcolor=\"#00000000\",color=\"#00000000\",constraint=false];\n");
68 else
69 print(os_, "];\n");
70 }
71
72 if (auto t = def->type(); t && types_) {
73 recurse(t, max - 1);
74 tab_.println(os_, "_{} -> _{}[color=\"#00000000\",constraint=false,style=dashed];", def->gid(), t->gid());
75 }
76 }
77
78 std::ostream& label(const Def* def) {
79 print(os_, "label=<{}<br/>", def->unique_name());
80 if (auto lit = def->isa<Lit>())
81 lit->stream(os_, 0);
82 else
83 os_ << def->node_name();
84 return os_ << '>';
85 }
86
87 std::ostream& color(const Def* def) {
88 return print(os_, "fillcolor=\"{} 0.5 0.75\"", def->node() / (float)Node::Num_Nodes);
89 }
90
91 std::ostream& tooltip(const Def* def) {
92 static constexpr auto NL = "&#13;&#10;";
93 auto loc = escape(def->loc());
94 auto type = escape(def->type());
95 escape(loc);
96 print(os_, "tooltip=\"");
97 print(os_, "<b>expr:</b> {}{}", def, NL);
98 print(os_, "<b>type:</b> {}{}", type, NL);
99 print(os_, "<b>name:</b> {}{}", def->sym(), NL);
100 print(os_, "<b>gid:</b> {}{}", def->gid(), NL);
101 print(os_, "<b>flags:</b> 0x{x}{}", def->flags(), NL);
102 print(os_, "<b>free_vars:</b> {{{, }}}{}", def->free_vars(), NL);
103 print(os_, "<b>local_vars:</b> {{{, }}}{}", def->local_vars(), NL);
104 print(os_, "<b>local_muts:</b> {{{, }}}{}", def->local_muts(), NL);
105 if (auto mut = def->isa_mut()) print(os_, "<b>fv_consumers:</b> {{{, }}}{}", mut->fv_consumers(), NL);
106 print(os_, "<b>loc:</b> {}", loc);
107 return print(os_, "\"");
108 }
109
110private:
111 std::ostream& os_;
112 bool types_;
113 const Def* root_;
114 Tab tab_;
115 DefSet done_;
116};
117
118} // namespace
119
120void Def::dot(std::ostream& ostream, uint32_t max, bool types) const { Dot(ostream, types, this).run(this, max); }
121
122void Def::dot(const char* file, uint32_t max, bool types) const {
123 if (!file) {
124 dot(std::cout, max, types);
125 } else {
126 auto of = std::ofstream(file);
127 dot(of, max, types);
128 }
129}
130
131void World::dot(const char* file, bool annexes, bool types) const {
132 if (!file) {
133 dot(std::cout, annexes, types);
134 } else {
135 auto of = std::ofstream(file);
136 dot(of, annexes, types);
137 }
138}
139
140void World::dot(std::ostream& os, bool anx, bool types) const {
141 Dot dot(os, types);
142 dot.prologue();
143 for (auto [_, external] : externals()) dot.recurse(external, uint32_t(-1));
144 if (anx)
145 for (auto [_, annex] : annexes()) dot.recurse(annex, uint32_t(-1));
146 dot.epilogue();
147}
148
149} // namespace mim
void dot(std::ostream &os, uint32_t max=0xFFFFFF, bool types=false) const
Definition dot.cpp:120
const Def * op(size_t i) const
Definition def.h:266
u32 gid() const
Definition def.h:236
const auto & annexes() const
Definition world.h:167
const auto & externals() const
Definition world.h:168
Def * external(Sym name)
Lookup by name.
Definition world.h:182
void dot(std::ostream &os, bool annexes=false, bool types=false) const
Definition dot.cpp:140
const Def * annex()
Get Axiom from a plugin.
Definition world.h:194
constexpr auto Num_Nodes
Definition def.h:43
Ref op(trait o, Ref type)
Definition core.h:35
Definition cfg.h:11
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:70
GIDSet< const Def * > DefSet
Definition def.h:58
std::ostream & println(std::ostream &os, const char *fmt, Args &&... args)
As above but end with std::endl.
Definition print.h:150