-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolidLang.cpp
More file actions
191 lines (154 loc) · 5.41 KB
/
SolidLang.cpp
File metadata and controls
191 lines (154 loc) · 5.41 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
#include "SolidLang.h"
int SolidLang::Start() {
int ExitCode = 0;
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
if (IsRepl()) {
In = stdin;
} else {
In = fopen(InputFile.c_str(), "r");
}
Lexer = std::make_unique<class Lexer>(In);
Parser = std::make_unique<class Parser>(*Lexer);
IfReplPrint("ready> ");
Lexer->GetNextToken();
JIT = OnErrorExit(JIT::Create());
InitLLVM();
ProcessInput();
if (!IsRepl()) {
fclose(In);
}
if (HasOutputFile()) {
ExitCode = WriteObjectFile();
}
if (PrintIR) {
errs() << "\n";
Module->print(errs(), nullptr);
}
return ExitCode;
}
void SolidLang::ProcessInput() {
bool done = false;
while (!done) {
switch (Lexer->GetCurrentToken()) {
case t_eof:
done = true;
break;
case ';':
Lexer->GetNextToken();
break;
case t_func:
case t_operator: {
std::unique_ptr<Expression> Result = Parser->ParseFunctionDefinition();
HandleFunction(Result.get());
break;
}
case t_native: {
std::unique_ptr<FunctionDeclaration> Result = Parser->ParseNative();
HandleNative(std::move(Result));
break;
}
default: {
std::unique_ptr<Expression> Result = Parser->ParseTopLevelExpression();
HandleTopLevelExpression(Result.get());
IfReplPrint("ready> ");
break;
}
}
}
}
void SolidLang::InitLLVM() {
Context = std::make_unique<LLVMContext>();
Module = std::make_unique<class Module>("Solid JIT", *Context);
Module->setDataLayout(JIT->GetDataLayout());
std::unique_ptr<legacy::FunctionPassManager> PassManager = nullptr;
if (!IsRepl()) {
PassManager = std::make_unique<legacy::FunctionPassManager>(Module.get());
PassManager->add(createPromoteMemoryToRegisterPass());
PassManager->add(createInstructionCombiningPass());
PassManager->add(createReassociatePass());
PassManager->add(createGVNPass());
PassManager->add(createCFGSimplificationPass());
PassManager->doInitialization();
}
Builder = std::make_unique<IRBuilder<>>(*Context);
auto IRGenerator = std::make_unique<class IRGenerator>(*Context, *Builder, *Module, std::move(PassManager),
ValuesByName, FunctionDeclarations);
if (PrintIR) {
Visitor = std::make_unique<class IRPrinter>(std::move(IRGenerator));
} else {
Visitor = std::move(IRGenerator);
}
}
int SolidLang::WriteObjectFile() {
auto TargetTriple = sys::getDefaultTargetTriple();
Module->setTargetTriple(TargetTriple);
std::string Error;
auto Target = TargetRegistry::lookupTarget(TargetTriple, Error);
if (!Target) {
errs() << Error;
return 1;
}
auto CPU = "generic";
TargetOptions Options;
auto Machine = Target->createTargetMachine(TargetTriple, CPU, "", Options, std::optional<Reloc::Model>());
Module->setDataLayout(Machine->createDataLayout());
std::error_code ErrorCode;
raw_fd_ostream OutputStream(OutputFile, ErrorCode, sys::fs::OF_None);
if (ErrorCode) {
errs() << "could not open file: " << ErrorCode.message();
return 1;
}
legacy::PassManager OutputPassManager;
if (Machine->addPassesToEmitFile(OutputPassManager, OutputStream, nullptr, CGFT_ObjectFile)) {
errs() << "could not emit file";
return 1;
}
OutputPassManager.run(*Module);
OutputStream.flush();
outs() << "created " << OutputFile << "\n";
return 0;
}
void SolidLang::HandleFunction(Expression *ParsedExpression) {
if (ParsedExpression) {
ParsedExpression->Accept(*Visitor);
if (IsRepl()) {
OnErrorExit(JIT->AddModule(
ThreadSafeModule(std::move(Module), std::move(Context))
));
InitLLVM();
}
} else {
Lexer->GetNextToken();
}
}
void SolidLang::HandleNative(std::unique_ptr<FunctionDeclaration> Declaration) {
if (Declaration) {
Declaration->Accept(*Visitor);
Visitor->Register(std::move(Declaration));
} else {
Lexer->GetNextToken();
}
}
void SolidLang::HandleTopLevelExpression(Expression *ParsedExpression) {
if (ParsedExpression) {
ParsedExpression->Accept(*Visitor);
if (IsRepl()) {
auto ResourceTracker = JIT->GetMain().createResourceTracker();
OnErrorExit(JIT->AddModule(
ThreadSafeModule(std::move(Module), std::move(Context)),
ResourceTracker
));
InitLLVM();
auto TopLevelExprSymbol = OnErrorExit(JIT->Lookup("__anonymous_top_level_expr"));
assert(TopLevelExprSymbol && "Top level expression not found");
// Cast symbol's address to be able to call it as a native function (no arguments, returns double)
auto (*TopLevelExpr)() = (double (*)()) (intptr_t) TopLevelExprSymbol.getAddress();
fprintf(stderr, "Evaluated to %f\n", TopLevelExpr());
OnErrorExit(ResourceTracker->remove());
}
} else {
Lexer->GetNextToken();
}
}