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
|
#include <iostream>
#ifdef _MSC_VER
#include "getopt.h"
#else
#include <unistd.h>
#endif
#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 "AsciiTraveler.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;
pcc::SetExecutableName(argv[0]);
while ((rv = getopt(argc, argv, "vVhiScao: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 '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);
switch (CompileType) {
case IR:
module->print(output, nullptr);
break;
case ASSEMBLY:
compile(module, output, llvm::CGFT_AssemblyFile, machine);
break;
case OBJECT:
compile(module, output, llvm::CGFT_ObjectFile, machine);
break;
default:
break;
}
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);
root->Travel(traveler);
}
}
return 0;
}
void compile(llvm::Module* module, llvm::raw_pwrite_stream& output,
llvm::CodeGenFileType type, llvm::TargetMachine* machine) {
llvm::legacy::PassManager manager;
machine->addPassesToEmitFile(manager, output, nullptr, type);
manager.run(*module);
}
|