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