-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.cpp
466 lines (381 loc) · 12.9 KB
/
codegen.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <fstream>
#include <iostream>
#include <vector>
#include <map>
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
using namespace llvm;
using namespace std;
const unsigned int TAPE_SIZE = 0x4000;
static std::unique_ptr<LLVMContext> TheContext;
static std::unique_ptr<Module> TheModule;
static std::unique_ptr<IRBuilder<>> Builder;
static std::map<std::string, AllocaInst *> NamedValues;
static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
static void llvm_init() {
// Open a new module.
TheContext = std::make_unique<LLVMContext>();
TheModule = std::make_unique<Module>("brainfuck", *TheContext);
// Create a new builder for the module.
Builder = std::make_unique<IRBuilder<>>(*TheContext);
// Create a new pass manager attached to it.
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
// Do simple "peephole" optimizations and bit-twiddling optzns.
TheFPM->add(createInstructionCombiningPass());
// Reassociate expressions.
TheFPM->add(createReassociatePass());
// Eliminate Common SubExpressions.
TheFPM->add(createGVNPass());
// Simplify the control flow graph (deleting unreachable blocks, etc).
TheFPM->add(createCFGSimplificationPass());
TheFPM->doInitialization();
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
}
static Value* get_current_position() {
AllocaInst *position_var = NamedValues["position"];
return Builder->CreateLoad(
position_var->getAllocatedType(),
position_var,
"position"
);
}
static Value* get_current_tape_cell_ptr() {
AllocaInst *tape = NamedValues["tape"];
// Get the address of the cell value at the current positin
return Builder->CreateGEP(
tape->getAllocatedType(),
tape,
{
ConstantInt::get(Type::getInt8Ty(*TheContext), 0),
get_current_position()
},
"tape cell ptr"
);
}
static Value* get_current_tape_value() {
Value* ptr = get_current_tape_cell_ptr();
return Builder->CreateLoad(
Type::getInt8Ty(*TheContext),
ptr
);
}
namespace Ast {
class Node {
public:
static Node* try_parse(std::fstream&);
virtual void debug_print()=0;
virtual void codegen()=0;
};
// +
class IncrementNode: public Node {
void debug_print() override {
std::cout << "+";
}
void codegen() override {
// Load the current value in the cell
Value* tape_cell_ptr = get_current_tape_cell_ptr();
Value* tape_cell = Builder->CreateLoad(
Type::getInt8Ty(*TheContext),
tape_cell_ptr,
"tape cell"
);
// Increment the value
Value *to_add = ConstantInt::get(Type::getInt8Ty(*TheContext), 1);
Value *new_value = Builder->CreateAdd(tape_cell, to_add, "new tape value");
// Write back
Builder->CreateStore(new_value, tape_cell_ptr);
};
};
// -
class DecrementNode: public Node {
void debug_print() override {
std::cout << "-";
}
void codegen() override {
// Load the current value in the cell
Value* tape_cell_ptr = get_current_tape_cell_ptr();
Value* tape_cell = Builder->CreateLoad(
Type::getInt8Ty(*TheContext),
tape_cell_ptr,
"tape cell"
);
// Decrement the value
Value *to_sub = ConstantInt::get(Type::getInt8Ty(*TheContext), 1);
Value *new_value = Builder->CreateSub(tape_cell, to_sub, "new tape value");
// Write back
Builder->CreateStore(new_value, tape_cell_ptr);
};
};
// <
class MoveLeftNode: public Node {
void debug_print() override {
std::cout << "<";
}
void codegen() override {
AllocaInst *var = NamedValues["position"];
Value *to_sub = ConstantInt::get(Type::getInt64Ty(*TheContext), 1);
Value *current_value = Builder->CreateLoad(var->getAllocatedType(), var, "position");
Value *new_value = Builder->CreateSub(current_value, to_sub, "next position");
Builder->CreateStore(new_value, var);
};
};
// >
class MoveRightNode: public Node {
void debug_print() override {
std::cout << ">";
}
void codegen() override {
AllocaInst *var = NamedValues["position"];
Value *to_add = ConstantInt::get(Type::getInt64Ty(*TheContext), 1);
Value *current_value = Builder->CreateLoad(var->getAllocatedType(), var, "position");
Value *new_value = Builder->CreateAdd(current_value, to_add, "next position");
Builder->CreateStore(new_value, var);
};
};
// .
class PutCharNode: public Node {
void debug_print() override {
std::cout << ".";
}
void codegen() override {
// declare putchar() function
FunctionType* putchar_type = FunctionType::get(
Type::getInt32Ty(*TheContext), // returns int
{ Type::getInt8Ty(*TheContext) }, // single character argument
false
);
FunctionCallee putchar = TheModule->getOrInsertFunction("putchar", putchar_type);
// Read the cell value at the current position
Value *tape_cell = get_current_tape_value();
// Call putchar
Builder->CreateCall(
putchar_type,
putchar.getCallee(),
{ tape_cell },
"putchar()"
);
};
};
// ,
class GetCharNode: public Node {
void debug_print() override {
std::cout << ",";
}
void codegen() override {
// declare getchar() function
FunctionType* getchar_type = FunctionType::get(
Type::getInt32Ty(*TheContext),
{},
false
);
FunctionCallee getchar = TheModule->getOrInsertFunction("getchar", getchar_type);
// Call getchar
Value *c = Builder->CreateCall(
getchar_type,
getchar.getCallee(),
{},
"getchar()"
);
// Truncate the value to an i8, so we can store it in the tape cell
Value *truncated_char = Builder->CreateIntCast(c, Type::getInt8Ty(*TheContext), true);
// Read the cell value at the current position
AllocaInst *tape = NamedValues["tape"];
Value *tape_cell_ptr = get_current_tape_cell_ptr();
Builder->CreateStore(truncated_char, tape_cell_ptr);
};
};
class ScopeNode: public Node {
protected:
std::vector<Node *> children;
public:
explicit ScopeNode(std::vector<Node *> children): children(children) {};
};
class ProgramNode: public ScopeNode {
public:
using ScopeNode::ScopeNode;
static Ast::Node* try_parse(std::fstream&);
void debug_print() override {
for (auto child: children) {
child->debug_print();
}
}
void codegen() override {
// Setup main function
FunctionType *main_type = FunctionType::get(Builder->getVoidTy(), false);
Function* main = Function::Create(
main_type,
GlobalValue::ExternalLinkage,
"main",
*TheModule
);
// Point the builder to the start of the main function
BasicBlock *main_block = BasicBlock::Create(*TheContext, "entry", main);
Builder->SetInsertPoint(main_block);
// Allocate the position of the write head
AllocaInst* position = Builder->CreateAlloca(
Type::getInt64Ty(*TheContext),
nullptr,
"position"
);
Value* initial_value = ConstantInt::get(Type::getInt64Ty(*TheContext), 0);
Builder->CreateStore(initial_value, position);
NamedValues["position"] = position;
// Allocate the tape storage
Type* tape_type = ArrayType::get(Type::getInt8Ty(*TheContext), TAPE_SIZE);
AllocaInst* tape = Builder->CreateAlloca(
tape_type,
nullptr,
"tape"
);
Constant* initial_tape = ConstantAggregateZero::get(tape_type);
Builder->CreateStore(initial_tape, tape);
NamedValues["tape"] = tape;
// Emit IR for each child
for (auto child: children) {
child->codegen();
}
Builder->CreateRet(NULL);
verifyFunction(*main);
}
};
// [ ... ]
class ConditionalGroupNode: public ScopeNode {
public:
using ScopeNode::ScopeNode;
static Ast::Node* try_parse(std::fstream&);
void debug_print() override {
std::cout << '[';
for (auto child: children) {
child->debug_print();
}
std::cout << ']';
}
void codegen() override {
AllocaInst *position_var = NamedValues["position"];
AllocaInst *tape = NamedValues["tape"];
BasicBlock *base_block = Builder->GetInsertBlock();
Function *TheFunction = base_block->getParent();
// Entry Condition:
// Check if the current tape cell is zero, if so,
// jump past the end of the group
Value* start_condition = Builder->CreateICmpNE(
get_current_tape_value(),
ConstantInt::get(Type::getInt8Ty(*TheContext), 0)
);
BasicBlock *group_content = BasicBlock::Create(*TheContext, "group content", TheFunction);
BasicBlock *merge = BasicBlock::Create(*TheContext, "merge", TheFunction);
Builder->CreateCondBr(start_condition, group_content, merge);
// If the value is not zero, we simply emit all the child IR
Builder->SetInsertPoint(group_content);
for (auto child: children) {
child->codegen();
}
// At the end of the is not zero block, chekc if the current
// cell is zero, in which case jump back to the start of the group
Value* end_condition = Builder->CreateICmpNE(
get_current_tape_value(),
ConstantInt::get(Type::getInt8Ty(*TheContext), 0)
);
// Explicitly fallthrough out of the if branch
Builder->CreateCondBr(end_condition, group_content, merge);
// The merge block simply falls through back into the base block
Builder->SetInsertPoint(merge);
}
};
}
Ast::Node* Ast::Node::try_parse(fstream &in) {
while(true) {
char c;
if (!(in >> c)) {
return NULL;
}
switch (c) {
case '+':
return new Ast::IncrementNode();
case '-':
return new Ast::DecrementNode();
case '<':
return new Ast::MoveLeftNode();
case '>':
return new Ast::MoveRightNode();
case '.':
return new Ast::PutCharNode();
case ',':
return new Ast::GetCharNode();
case '[':
return Ast::ConditionalGroupNode::try_parse(in);
case ']':
return NULL; // don't continue parsing
default:
// Ignore all unknown characters
break;
}
}
}
Ast::Node* Ast::ProgramNode::try_parse(fstream &in) {
std::vector<Ast::Node *> children = {};
Ast::Node* node;
while ((node = Ast::Node::try_parse(in))) {
children.push_back(node);
}
return new Ast::ProgramNode(std::move(children));
}
Ast::Node* Ast::ConditionalGroupNode::try_parse(fstream &in) {
std::vector<Ast::Node *> children = {};
Ast::Node* node;
while ((node = Ast::Node::try_parse(in))) {
children.push_back(node);
}
return new Ast::ConditionalGroupNode(std::move(children));
}
int main() {
fstream in("program.bf");
if (!in.is_open()) {
std::cout << "Failed to open input file" << std::endl;
return -1;
}
// build the AST
Ast::Node *root = Ast::ProgramNode::try_parse(in);
if (!root) {
std::cout << "Failed to parse AST" << std::endl;
return -1;
}
in.close();
// Setup LLVM data structures
llvm_init();
// Emit LLVM IR code
root->codegen();
Function* main = TheModule->getFunction("main");
if (!main) {
std::cout << "main() was not defined" << std::endl;
return -1;
}
// Optimize the function.
TheFPM->run(*main);
// Dump LLVM IR
TheModule->print(outs(), nullptr);
return 0;
}