-
Notifications
You must be signed in to change notification settings - Fork 445
/
dump.cpp
134 lines (122 loc) · 4.7 KB
/
dump.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "dump.h"
#include <set>
#include "ir/ir.h"
#include "ir/node.h"
#include "ir/visitor.h"
#include "lib/cstring.h"
#include "lib/indent.h"
#include "lib/source_file.h"
namespace {
class IRDumper : public Inspector {
std::ostream &out;
std::set<const IR::Node *> dumped;
unsigned maxdepth;
cstring ignore;
bool source;
bool preorder(const IR::Node *n) override {
if (auto ctxt = getContext()) {
if (unsigned(ctxt->depth) > maxdepth) return false;
if (ctxt->parent && ctxt->parent->child_name && ignore == ctxt->parent->child_name)
return false;
out << indent_t(ctxt->depth);
if (ctxt->child_name) out << ctxt->child_name << ": ";
}
out << "[" << n->id << "] ";
if (source && n->srcInfo) out << "(" << n->srcInfo.toPositionString() << ") ";
out << n->node_type_name();
n->dump_fields(out);
if (dumped.count(n)) {
out << "..." << std::endl;
return false;
}
dumped.insert(n);
out << std::endl;
return true;
}
bool preorder(const IR::Expression *e) override {
if (!preorder(static_cast<const IR::Node *>(e))) return false;
visit(e->type, "type");
return true;
}
bool preorder(const IR::Constant *c) override {
return preorder(static_cast<const IR::Node *>(c));
}
void postorder(const IR::Node *n) override {
if (getChildrenVisited() == 0) dumped.erase(n);
}
public:
IRDumper(std::ostream &o, unsigned m, cstring ign, bool src)
: out(o), maxdepth(m), ignore(ign), source(src) {
visitDagOnce = false;
}
};
} // namespace
void dump(std::ostream &out, const IR::Node *n, unsigned maxdepth) {
n->apply(IRDumper(out, maxdepth, nullptr, false));
}
void dump(std::ostream &out, const IR::Node *n) { dump(out, n, ~0U); }
void dump(const IR::Node *n, unsigned maxdepth) { dump(std::cout, n, maxdepth); }
void dump(const IR::Node *n) { dump(n, ~0U); }
void dump(const IR::INode *n, unsigned maxdepth) { dump(std::cout, n->getNode(), maxdepth); }
void dump(const IR::INode *n) { dump(n, ~0U); }
void dump_notype(const IR::Node *n, unsigned maxdepth) {
n->apply(IRDumper(std::cout, maxdepth, "type", false));
}
void dump_notype(const IR::Node *n) { dump_notype(n, ~0U); }
void dump_notype(const IR::INode *n, unsigned maxdepth) {
n->getNode()->apply(IRDumper(std::cout, maxdepth, "type", false));
}
void dump_notype(const IR::INode *n) { dump_notype(n, ~0U); }
void dump_src(const IR::Node *n, unsigned maxdepth) {
n->apply(IRDumper(std::cout, maxdepth, "type", true));
}
void dump_src(const IR::Node *n) { dump_src(n, ~0U); }
void dump_src(const IR::INode *n, unsigned maxdepth) {
n->getNode()->apply(IRDumper(std::cout, maxdepth, "type", true));
}
void dump_src(const IR::INode *n) { dump_src(n, ~0U); }
void dump(uintptr_t p, unsigned maxdepth) { dump(reinterpret_cast<const IR::Node *>(p), maxdepth); }
void dump(uintptr_t p) { dump(p, ~0U); }
void dump_notype(uintptr_t p, unsigned maxdepth) {
dump_notype(reinterpret_cast<const IR::Node *>(p), maxdepth);
}
void dump_notype(uintptr_t p) { dump_notype(p, ~0U); }
void dump(std::ostream &out, const Visitor::Context *ctxt) {
if (!ctxt) return;
dump(ctxt->parent);
out << indent_t(ctxt->depth - 1);
if (ctxt->parent) {
if (ctxt->parent->child_name)
out << ctxt->parent->child_name << ": ";
else
out << ctxt->parent->child_index << ": ";
}
if (ctxt->original != ctxt->node) {
out << "<" << static_cast<const void *>(ctxt->original) << ":[" << ctxt->original->id
<< "] " << ctxt->original->node_type_name();
ctxt->original->dump_fields(out);
out << std::endl << indent_t(ctxt->depth - 1) << ">";
}
out << static_cast<const void *>(ctxt->node) << ":[" << ctxt->node->id << "] "
<< ctxt->node->node_type_name();
ctxt->node->dump_fields(out);
std::cout << std::endl;
}
void dump(const Visitor::Context *ctxt) { dump(std::cout, ctxt); }
std::string dumpToString(const IR::Node *n) {
std::stringstream str;
dump(str, n);
return str.str();
}