-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
325 lines (276 loc) · 8.54 KB
/
main.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <cstdio>
#include <fstream>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
struct Stack {
int stack[100];
int strcnt;
std::map<int, std::string> stringtable;
int sp;
void push(int operand) {
stack[sp++] = operand;
}
void pushs(const std::string &s) {
int cnt = strcnt++;
stringtable[cnt] = s;
push(cnt);
}
int pop() {
return stack[--sp];
}
std::string pops() {
return stringtable[stack[--sp]];
}
};
enum class OPType {
NOP = 0,
PUSHI,
PUSHS,
INTRINSIC
};
enum class IntrinsicType {
EXIT = 0,
PUTS
};
struct OP {
OPType type;
int operand;
std::string soperand;
};
#define ENUMI(enum) static_cast<int>(enum)
#define OP_PLUS_I \
OP { OPType::PLUSI, 0 }
#define OP_PUSH_I(op) \
OP { OPType::PUSHI, op }
#define OP_PUSH_S(sop) \
OP { OPType::PUSHS, 0, sop }
#define OP_INTRINSIC(intrinsic) \
OP { OPType::INTRINSIC, ENUMI(intrinsic) }
#define INTRINSIC_EXIT OP_INTRINSIC(IntrinsicType::EXIT)
#define INTRINSIC_PUTS OP_INTRINSIC(IntrinsicType::PUTS)
std::string intrinsicToName(const IntrinsicType &type) {
switch (type) {
case IntrinsicType::EXIT:
return "IntrinsicType::EXIT";
case IntrinsicType::PUTS:
return "IntrinsicType::PUTS";
default:
return "Unknown intrinsic.";
}
}
std::string opTypeToName(const OP &op) {
switch (op.type) {
case OPType::PUSHI:
return "OPType::PUSHI";
case OPType::PUSHS:
return "OPType::PUSHS";
case OPType::INTRINSIC:
return intrinsicToName(static_cast<IntrinsicType>(op.operand));
default:
return "";
}
}
std::vector<OP> program = {};
void parseProgram(const std::string &source) {
OP current = {};
int cursor = 0;
auto parseStr = [&]() {
int start = ++cursor;
int end = start;
std::string str = {};
while (source[end] != '"') {
if (source[end] != '\\') {
str += source[end];
end++;
} else {
switch (source[end + 1]) {
case 'n':
str += 0xA;
break;
case 't':
str += 0x09;
break;
default:
break;
}
end += 2;
}
}
return make_pair(str, end + 1);
};
auto tryParseIdent = [&]() {
int start = cursor;
int end = start;
while (std::isalpha(source[end]))
end++;
auto str = source.substr(start, end - start);
return std::make_pair(str, end);
};
auto tryParseNumber = [&]() {
int start = cursor;
int end = start;
while (std::isalnum(source[end]))
end++;
auto str = source.substr(start, end - start);
return std::make_pair(str, end);
};
auto push_op = [&]() {
program.emplace_back(current);
printf("> Pushing op:\n");
printf(" - Type: %s\n", opTypeToName(current).c_str());
printf(" - Operand: %d\n", current.operand);
printf(" - Str Operand: %s\n", current.soperand.c_str());
current = {};
cursor++;
};
while (cursor < source.length()) {
switch (source[cursor]) {
case ';':
push_op();
break;
case '\n':
case ' ':
cursor++;
break;
case '"': {
auto a = parseStr();
current.soperand = a.first;
cursor = a.second;
} break;
default:
if (isalpha(source[cursor])) {
auto res = tryParseIdent();
if (res.first == "pushs") {
current.type = OPType::PUSHS;
} else if (res.first == "puts") {
current = INTRINSIC_PUTS;
} else if (res.first == "pushi") {
current.type = OPType::PUSHI;
} else if (res.first == "exit") {
current = INTRINSIC_EXIT;
}
cursor = res.second;
} else if (isalnum(source[cursor])) {
auto res = tryParseNumber();
current.operand = std::stoi(res.first);
cursor = res.second;
}
break;
}
}
}
std::unique_ptr<llvm::LLVMContext> llvmContext;
std::unique_ptr<llvm::IRBuilder<>> irBuilder;
std::unique_ptr<llvm::Module> llvmModule;
void compileToLLVMIR() {
llvmModule->getOrInsertFunction(
"exit",
llvm::FunctionType::get(irBuilder->getVoidTy(), {irBuilder->getInt32Ty()}, false));
llvmModule->getOrInsertFunction(
"printf",
llvm::FunctionType::get(irBuilder->getVoidTy(), {irBuilder->getInt8PtrTy()}, false));
auto main = llvm::Function::Create(
llvm::FunctionType::get(llvm::Type::getInt32Ty(*llvmContext), {}, false),
llvm::Function::ExternalLinkage, "main", llvmModule.get());
auto bb = llvm::BasicBlock::Create(*llvmContext, "entry", main);
irBuilder->SetInsertPoint(bb);
auto stack = Stack{};
for (size_t i = 0; i < program.size(); i++) {
switch (program[i].type) {
case OPType::PUSHI:
stack.push(program[i].operand);
break;
case OPType::PUSHS:
stack.pushs(program[i].soperand);
break;
case OPType::INTRINSIC: {
switch (program[i].operand) {
case ENUMI(IntrinsicType::EXIT): {
int pop = stack.pop();
irBuilder->CreateCall(
llvmModule->getFunction("exit"),
{llvm::ConstantInt::get(*llvmContext, llvm::APInt(32, pop, false))});
} break;
case ENUMI(IntrinsicType::PUTS): {
std::string pop = stack.pops();
auto str = irBuilder->CreateGlobalString(pop.c_str(), "");
irBuilder->CreateCall(
llvmModule->getFunction("printf"),
{str});
} break;
}
}
default:
break;
}
}
irBuilder->CreateRet(llvm::ConstantInt::get(*llvmContext, llvm::APInt(32, 0, false)));
verifyFunction(*main);
}
int main(int argc, char **argv) {
(void)*argv++;
if (argc > 1) {
auto path = *argv++;
std::ifstream source;
source.open(path);
std::stringstream sourcestream;
sourcestream << source.rdbuf();
source.close();
parseProgram(sourcestream.str());
}
llvmContext = std::make_unique<llvm::LLVMContext>();
llvmModule = std::make_unique<llvm::Module>("stacked", *llvmContext);
irBuilder = std::make_unique<llvm::IRBuilder<>>(*llvmContext);
llvmModule->print(llvm::errs(), nullptr);
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllAsmPrinters();
auto triple = llvm::sys::getDefaultTargetTriple();
llvmModule->setTargetTriple(triple);
std::string err;
auto target = llvm::TargetRegistry::lookupTarget(triple, err);
if (!target) {
llvm::errs() << err;
return 1;
}
auto CPU = "generic";
llvm::TargetOptions opt;
auto targetMachine = target->createTargetMachine(triple, CPU, "", opt, llvm::Reloc::PIC_);
llvmModule->setDataLayout(targetMachine->createDataLayout());
std::error_code errorcode;
llvm::raw_fd_ostream dest("output.o", errorcode, llvm::sys::fs::OF_None);
if (errorcode) {
llvm::errs() << "Could not open file: " << errorcode.message();
return 1;
}
llvm::legacy::PassManager pass;
auto fileType = llvm::CodeGenFileType::CGFT_ObjectFile;
if (targetMachine->addPassesToEmitFile(pass, dest, nullptr, fileType)) {
llvm::errs() << "Target machine can't emit a file of this type";
return 1;
}
pass.run(*llvmModule);
dest.flush();
return 0;
}