-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_llvmir.cpp
152 lines (132 loc) · 5.46 KB
/
tree_llvmir.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "aspartame/optional.hpp"
#include "aspartame/view.hpp"
#include "fmt/core.h"
#include "tree_llvmir.h"
#include "tree_utils.h"
using namespace aspartame;
static std::string print(llvm::Type *t) {
std::string s;
llvm::raw_string_ostream rso(s);
t->print(rso);
return rso.str();
}
[[nodiscard]] static sv::Location resolveFnLocation(const llvm::Function &fn) {
if (auto prog = fn.getSubprogram()) {
return sv::Location{.path = std::filesystem::path(prog->getFilename().str()),
.line = prog->getLine(),
.col = 0};
}
return {};
}
sv::LLVMIRTreeVisitor::LLVMIRTreeVisitor(NTree<SNode> *root, const llvm::Module &module,
bool normaliseName)
: NTreeVisitor(root), normaliseName(normaliseName) {
module.globals() | for_each([&](const llvm::GlobalVariable &var) {
Location loc{};
if (auto md = var.getMetadata(llvm::LLVMContext::MD_dbg)) {
if (auto expr = llvm::dyn_cast<llvm::DIGlobalVariableExpression>(md)) {
if (auto diVar = expr->getVariable()) {
loc.path = std::filesystem::path(diVar->getFilename().str());
loc.line = diVar->getLine();
}
}
}
auto name = named("Global", var.getName().str() + ": " + print(var.getType()));
if (var.hasInitializer()) {
scoped([&]() { walk(var.getInitializer()); }, SNode{name, loc});
} else {
single(SNode{name, loc});
}
});
module.functions() | for_each([&](const llvm::Function &fn) {
auto args =
fn.args() | mk_string(",", [&](const llvm::Argument &arg) { return print(arg.getType()); });
auto sig = fmt::format("{}({}): {}", (!normaliseName ? fn.getName().str() : ""), args,
print(fn.getReturnType()));
scoped(
[&]() {
fn | for_each([&](const llvm::BasicBlock &bb) {
scoped(
[&]() {
bb | for_each([&](const llvm::Instruction &ins) { walk(&ins); });
return true;
},
SNode{named("BB", bb.getName().str()), Location{}});
});
},
SNode{fmt::format("Fn: {}", sig), resolveFnLocation(fn)});
});
}
std::string sv::LLVMIRTreeVisitor::named(const std::string &kind, const std::string &name) const {
return normaliseName ? kind : (kind + ": " + name);
}
void sv::LLVMIRTreeVisitor::walk(const llvm::Value *value) {
if (!value) {
single(SNode{"<<<NUll>>>", Location{}});
return;
}
visitDyn0( //
value, //
[&](const llvm::Argument *arg) {
return single(SNode{fmt::format("Arg: {}", print(arg->getType())), Location{}});
},
[&](const llvm::Function *fn) {
single(SNode{named("Fn", fmt::format("{}: {}", fn->getName().str(), print(fn->getType()))),
resolveFnLocation(*fn)});
},
[&](const llvm::Instruction *ins) {
if (ins->isDebugOrPseudoInst()) { return; }
// if (auto call = llvm::dyn_cast<llvm::CallInst>(ins)) {
// if (auto func = call->getCalledFunction(); func && func->isIntrinsic()) {
// switch (func->getIntrinsicID()) {
// case llvm::Intrinsic::dbg_assign: [[fallthrough]];
// case llvm::Intrinsic::dbg_declare: [[fallthrough]];
// case llvm::Intrinsic::dbg_label: [[fallthrough]];
// case llvm::Intrinsic::dbg_value: return; // found a debug intrinsic, stop
// default: break; // keep going otherwise
// }
// }
// }
std::string s;
llvm::raw_string_ostream rso(s);
ins->print(rso);
Location loc{};
if (auto &dloc = ins->getDebugLoc(); dloc) {
auto actualDI = (iterate(dloc.get(), [](auto l) { return l->getInlinedAt(); }) //
| take_while([](auto l) { return l != nullptr; }) //
| last_maybe()) ^
get_or_else(dloc.get());
loc.path = std::filesystem::path(actualDI->getFilename().str());
loc.line = actualDI->getLine();
loc.col = actualDI->getColumn();
}
auto args = ins->operands() |
mk_string(",", [](const llvm::Use &u) { return print(u->getType()); });
auto sig = fmt::format("{}({}):{}", ins->getOpcodeName(), args, print(ins->getType()));
// scoped( //
// [&]() {
// ins->operand_values() | zip_with_index() |
// for_each([&](const llvm::Value *v, auto idx) {
// if (idx == 0) return; // arg 0 should be the ins itself
// if (v == value) single("(identity)");
// else walk(v);
// });
// },
// sig);
single(SNode{sig, loc});
},
[&](const llvm::Constant *c) {
std::string s;
llvm::raw_string_ostream rso(s);
c->print(rso);
single(SNode{named("Const", fmt::format("{}: {}", rso.str(), print(c->getType()))),
Location{}});
},
[&](const llvm::Value *c) {
single(SNode{named("Val", fmt::format("{}: {}", c->getName().str(), print(c->getType()))),
Location{}});
});
}