forked from revng/revng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.cpp
159 lines (127 loc) · 4.19 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/// \file dump.cpp
/// \brief Standalone program to extract various information from the LLVM IR
/// generated by revamb
// Standard includes
#include <cstdlib>
#include <iostream>
#include <memory>
// LLVM includes
#include "llvm/IR/Constants.h" // REMOVE ME
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/SourceMgr.h"
// Local includes
#include "argparse.h"
#include "collectcfg.h"
#include "collectfunctionboundaries.h"
#include "collectnoreturn.h"
using namespace llvm;
struct ProgramParameters {
const char *InputPath;
const char *CFGPath;
const char *NoreturnPath;
const char *FunctionBoundariesPath;
};
static const char *const Usage[] = {
"revamb-dump [options] INFILE",
nullptr,
};
static bool parseArgs(int Argc, const char *Argv[], ProgramParameters &Result) {
// Initialize argument parser
struct argparse Arguments;
struct argparse_option Options[] = {
OPT_HELP(),
OPT_STRING('c', "cfg",
&Result.CFGPath,
"path where the CFG should be stored."),
OPT_STRING('n', "noreturn",
&Result.NoreturnPath,
"path where the list of noreturn basic blocks should be "
"stored."),
OPT_STRING('f', "functions-boundaries",
&Result.FunctionBoundariesPath,
"path where the list of function boundaries blocks should be "
"stored."),
OPT_END(),
};
argparse_init(&Arguments, Options, Usage, 0);
argparse_describe(&Arguments, "\nrevamb-dump.",
"\nDump several high-level information from the "
"revamb-generated LLVM IR.\n");
Argc = argparse_parse(&Arguments, Argc, Argv);
// Handle positional arguments
if (Argc != 1) {
fprintf(stderr, "Please specify one and only one input file.\n");
return false;
}
Result.InputPath = Argv[0];
return true;
}
class DumpPass : public FunctionPass {
public:
static char ID;
public:
DumpPass(ProgramParameters &Parameters) : FunctionPass(ID),
Parameters(Parameters) { }
bool runOnFunction(Function &F) override {
std::ofstream Output;
if (Parameters.CFGPath != nullptr) {
auto &Analysis = getAnalysis<CollectCFG>();
Analysis.serialize(pathToStream(Parameters.CFGPath, Output));
}
if (Parameters.NoreturnPath != nullptr) {
auto &Analysis = getAnalysis<CollectNoreturn>();
Analysis.serialize(pathToStream(Parameters.NoreturnPath, Output));
}
if (Parameters.FunctionBoundariesPath != nullptr) {
auto &Analysis = getAnalysis<CollectFunctionBoundaries>();
Analysis.serialize(pathToStream(Parameters.FunctionBoundariesPath,
Output));
}
return false;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
if (Parameters.CFGPath != nullptr)
AU.addRequired<CollectCFG>();
if (Parameters.NoreturnPath != nullptr)
AU.addRequired<CollectNoreturn>();
if (Parameters.FunctionBoundariesPath != nullptr)
AU.addRequired<CollectFunctionBoundaries>();
}
private:
std::ostream &pathToStream(const char *Path, std::ofstream &File) {
if (Path[0] == '-' && Path[1] == '\0') {
return std::cout;
} else {
if (File.is_open())
File.close();
File.open(Path);
return File;
}
}
private:
ProgramParameters &Parameters;
};
char DumpPass::ID = 0;
int main(int argc, const char *argv[]) {
ProgramParameters Parameters = { nullptr, nullptr };
if (!parseArgs(argc, argv, Parameters))
return EXIT_FAILURE;
LLVMContext &Context = getGlobalContext();
SMDiagnostic Err;
std::unique_ptr<Module> TheModule = parseIRFile(Parameters.InputPath,
Err,
Context);
if (!TheModule) {
fprintf(stderr, "Couldn't load the LLVM IR.");
return EXIT_FAILURE;
}
legacy::FunctionPassManager FPM(TheModule.get());
FPM.add(new DumpPass(Parameters));
FPM.run(*TheModule->getFunction("root"));
return EXIT_SUCCESS;
}