-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
193 lines (186 loc) · 6.47 KB
/
main.cc
File metadata and controls
193 lines (186 loc) · 6.47 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <fstream>
#include <iostream>
#ifdef _MSC_VER
#include "getopt.h"
#else
#include <unistd.h>
#endif
#include <llvm/IR/IRPrintingPasses.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Support/TargetRegistry.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Transforms/Scalar.h>
#include "AsciiTraveler.hh"
#include "DotTraveler.hh"
#include "Driver.hh"
#include "Log.hh"
#include "Version.hh"
void compile(llvm::Module* module, llvm::raw_pwrite_stream& output,
llvm::CodeGenFileType type, llvm::TargetMachine* machine);
int main(int argc, char* argv[]) {
int rv;
int level = pcc::ToUnderlying(pcc::PCC_WARNING);
std::string OutputFileName;
enum { IR, ASSEMBLY, OBJECT } CompileType = IR;
int OptLevel = 2;
bool AsciiTravel = false, DotTravel = false;
pcc::SetExecutableName(argv[0]);
while ((rv = getopt(argc, argv, "vVhiScado:O:")) != -1) {
switch (rv) {
case 'v':
level = (level == 0 ? 0 : level - 1);
break;
case 'V':
pcc::ShowVersion();
case 'h':
pcc::ShowHelp();
case 'i':
CompileType = IR;
break;
case 'S':
CompileType = ASSEMBLY;
break;
case 'c':
CompileType = OBJECT;
break;
case 'a':
AsciiTravel = true;
break;
case 'd':
DotTravel = true;
break;
case 'o':
OutputFileName = optarg;
break;
case 'O':
OptLevel = optarg[0] - '0';
if (OptLevel > 3 || OptLevel < 0) {
pcc::Log(pcc::PCC_ERROR, "%c is not a valid optimization level",
optarg[0]);
pcc::ShowHelp();
}
break;
default:
pcc::Log(pcc::PCC_ERROR, "unknown option");
pcc::ShowHelp();
}
}
pcc::SetLogLevel(static_cast<pcc::LogLevel>(level));
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmParser();
llvm::InitializeNativeTargetAsmPrinter();
std::string triple = llvm::sys::getDefaultTargetTriple();
std::string error;
const llvm::Target* target =
llvm::TargetRegistry::lookupTarget(triple, error);
if (!error.empty()) {
pcc::Log(pcc::PCC_ERROR,
"failed to find target with triple %s, reason %s",
triple.c_str(), error.c_str());
return -1;
}
const llvm::CodeGenOpt::Level optimization[] = {
llvm::CodeGenOpt::None, llvm::CodeGenOpt::Less,
llvm::CodeGenOpt::Default, llvm::CodeGenOpt::Aggressive};
std::string cpu = "generic";
std::string features = "";
llvm::TargetOptions options;
llvm::TargetMachine* machine =
target->createTargetMachine(triple, cpu, features, options, llvm::None,
llvm::None, optimization[OptLevel]);
llvm::DataLayout layout = machine->createDataLayout();
if (optind >= argc) {
pcc::Log(pcc::PCC_ERROR, "no input file specified");
return -1;
}
if (!OutputFileName.empty() && optind != argc - 1) {
pcc::Log(pcc::PCC_WARNING,
"more than one source file specified, the output file name "
"will be ignored");
OutputFileName.clear();
}
for (int i = optind; i < argc; i++) {
pcc::Driver driver(argv[i]);
llvm::Module* module = driver.GetContext()->GetModule();
module->setTargetTriple(triple);
module->setDataLayout(layout);
std::string filename(argv[i]);
std::shared_ptr<pcc::ProgramNode> root = driver.Parse(filename);
if (root != nullptr) {
root->CodeGen();
std::string msg;
llvm::raw_string_ostream MsgStream(msg);
llvm::verifyModule(*module, &MsgStream);
MsgStream.flush();
if (!msg.empty()) {
pcc::Log(pcc::PCC_WARNING, "%s", msg.c_str());
}
} else {
continue;
}
if (OutputFileName.empty()) {
const char* postfix[] = {".ll", ".s", ".o"};
size_t PostfixPos = filename.rfind(".pas");
if (PostfixPos == std::string::npos) {
OutputFileName = filename + postfix[CompileType];
} else {
OutputFileName =
filename.substr(0, PostfixPos) + postfix[CompileType];
}
}
std::error_code ErrorCode;
llvm::raw_fd_ostream output(OutputFileName, ErrorCode);
llvm::legacy::PassManager manager;
manager.add(llvm::createCFGSimplificationPass());
manager.add(llvm::createMergedLoadStoreMotionPass());
manager.add(llvm::createReassociatePass());
manager.add(llvm::createLoopSimplifyCFGPass());
switch (CompileType) {
case IR:
manager.add(llvm::createPrintModulePass(output));
break;
case ASSEMBLY:
machine->addPassesToEmitFile(
manager, output, nullptr,
llvm::CodeGenFileType::CGFT_AssemblyFile);
break;
case OBJECT:
machine->addPassesToEmitFile(
manager, output, nullptr,
llvm::CodeGenFileType::CGFT_ObjectFile);
break;
default:
break;
}
manager.run(*module);
pcc::Log(pcc::PCC_INFO, "%s compiled to %s", argv[i],
OutputFileName.c_str());
OutputFileName.clear();
if (AsciiTravel == true) {
pcc::Log(pcc::PCC_INFO, "dumping abstract syntax tree of %s",
argv[i]);
pcc::AsciiTraveler traveler(std::cerr);
traveler.Travel(root);
}
if (DotTravel == true) {
std::string DotOutputName;
size_t PostfixPos = filename.rfind(".pas");
if (PostfixPos == std::string::npos) {
DotOutputName = filename + ".dot";
} else {
DotOutputName = filename.substr(0, PostfixPos) + ".dot";
}
std::ofstream DotOut(DotOutputName);
if (DotOut.is_open() == false) {
pcc::Log(pcc::PCC_ERROR, "failed to open dot outpt file %s",
DotOutputName.c_str());
}
pcc::DotTraveler traveler(DotOut);
traveler.Travel(root);
DotOut.close();
}
}
return 0;
}