-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (119 loc) · 3.87 KB
/
Copy pathmain.cpp
File metadata and controls
143 lines (119 loc) · 3.87 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
#include <cstdio>
#include <fstream>
#include "lexer/lexer.hpp"
#include "parser/parser.hpp"
#include "genIR/genIR.hpp"
#include "error/error.hpp"
#include "llvm/Support/CommandLine.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Config/llvm-config.h"
llvm::cl::opt<std::string> inputSource(
llvm::cl::Positional,
llvm::cl::desc("<input source>"),
llvm::cl::Required);
llvm::cl::opt<std::string> OutputFile(
"o",
llvm::cl::desc("output file"),
llvm::cl::value_desc("filename"),
llvm::cl::init("out.o"));
llvm::cl::opt<bool> isOutputAST(
"ast",
llvm::cl::desc("Output AST."),
// llvm::cl::value_desc("filename"),
llvm::cl::init(false));
llvm::cl::opt<bool> isOutputIR(
"ir",
llvm::cl::desc("Output LLVM IR."),
// llvm::cl::value_desc("filename"),
llvm::cl::init(false));
int main(int argc, char **argv)
{
llvm::cl::ParseCommandLineOptions(argc, argv, "hinalang compiler");
std::ifstream f(inputSource);
if (!f)
{
Error::err("File '%s' open failed.", inputSource.c_str());
return 1;
}
lexer lex(f);
parser par(lex);
ProgramAST *program = par.parseProgram();
f.close();
if (isOutputAST)
{
FILE *f2 = fopen(OutputFile.c_str(), "w");
if (!f2)
{
Error::err("File '%s' save failed. (err: %s)", OutputFile.c_str(), strerror(errno));
return 1;
}
program->dump(0, f2);
fclose(f2);
return 0;
}
genIR gen;
gen.generate(program);
if (isOutputIR)
{
std::error_code errorCode;
llvm::raw_fd_ostream stream(OutputFile, errorCode);
if (stream.has_error())
{
/* code */
Error::err("File '%s' save failed. (err: %s)", OutputFile.c_str(), errorCode.message().c_str());
}
gen.getModule()->print(stream, nullptr);
return 0;
}
// オブジェクトファイルの生成
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllAsmPrinters();
std::string targetTripleStr = llvm::sys::getDefaultTargetTriple();
#if LLVM_VERSION_MAJOR < 21
gen.getModule()->setTargetTriple(targetTripleStr);
#else
// llvm 21でもコンパイルできるように修正
gen.getModule()->setTargetTriple(llvm::Triple(targetTripleStr));
#endif
std::string error;
const llvm::Target *target = llvm::TargetRegistry::lookupTarget(targetTripleStr, error);
if (!target)
{
llvm::errs() << error;
Error::err("can't lookup target (triple: %s)", targetTripleStr.c_str());
return 1;
}
std::string cpu = "generic";
std::string features = "";
llvm::TargetOptions targetOpt;
#if LLVM_VERSION_MAJOR < 21
auto targetMachine = target->createTargetMachine(
targetTripleStr, cpu, features, targetOpt, llvm::Reloc::PIC_);
#else
// llvm 21でもコンパイルできるように修正
auto targetMachine = target->createTargetMachine(
llvm::Triple(targetTripleStr), cpu, features, targetOpt, llvm::Reloc::PIC_);
#endif
gen.getModule()->setDataLayout(targetMachine->createDataLayout());
std::error_code errorCode;
llvm::raw_fd_ostream stream(OutputFile, errorCode);
if (stream.has_error())
{
/* code */
Error::err("File '%s' save failed. (err: %s)", OutputFile.c_str(), errorCode.message().c_str());
}
llvm::legacy::PassManager pass;
targetMachine->addPassesToEmitFile(pass, stream, nullptr, llvm::CodeGenFileType::ObjectFile);
pass.run(*gen.getModule());
stream.flush();
// gen.getModule()->print(stream, nullptr);
return 0;
}