-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegenVis.cpp
396 lines (372 loc) · 14.7 KB
/
codegenVis.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
// Copyright (C) 2014, Daniel S. Fava
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
#include "node.h"
#include "codegenVis.h"
#include "parser.hpp"
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/PassManager.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/CallingConv.h>
#include <llvm-c/BitWriter.h>
#include <llvm/IR/Verifier.h>
#include <llvm/IR/IRPrintingPasses.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/Support/raw_ostream.h>
using namespace llvm;
static llvm::IRBuilder<> Builder(getGlobalContext()); // TODO: Get rid of this global
void CodeGenVisitor::init()
{
if (verbose) std::cout << "CodeGenVis::init()" << std::endl;
Module* m = new Module("main", getGlobalContext());
Scope* s = new Scope();
context = new CodeGenContext(s, m);
// Create a global scope
context->scope->InitializeScope("global");
// Create a main function and add the first basic block to it
ArrayRef<llvm::Type *> argTypes;
//FunctionType *ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), argTypes, false);
FunctionType *ftype = FunctionType::get(Type::getInt32Ty(getGlobalContext()), argTypes, false);
mainFunction = Function::Create(ftype, GlobalValue::ExternalLinkage, "main", context->module);
BasicBlock *bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0);
// Set the Builder's basic block to this first block from the main function
Builder.SetInsertPoint(bblock);
}
void CodeGenVisitor::generateCode()
{
assert(vals.size() == 0);
//Builder.CreateRetVoid();
Builder.CreateRet(ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 0, true));
// Cleanup scopes
assert(context->scope->depth() == 1);
context->scope->FinalizeScope();
// Validate the generated code, checking for consistency.
verifyFunction(*mainFunction);
// Dump IR to screen
if (verbose) std::cout << "Code is generated." << std::endl;
if (verbose) context->module->dump();
if (filename != NULL) {
LLVMWriteBitcodeToFile((LLVMOpaqueModule *)context->module, filename);
}
}
/* Executes the AST by running the main function */
GenericValue CodeGenVisitor::runCode()
{
if (verbose) std::cout << "Running code...\n";
InitializeNativeTarget();
ExecutionEngine* ee = EngineBuilder(context->module).create();
assert(ee != 0);
std::vector<GenericValue> noargs;
GenericValue v = ee->runFunction(mainFunction, noargs);
if (verbose) std::cout << "Code was run.\n";
return v;
}
/* Returns an LLVM type based on the identifier */
static const Type *typeOf(const NType& type)
{
if (type.name.compare("int") == 0) {
return Type::getInt64Ty(getGlobalContext());
}
else if (type.name.compare("double") == 0) {
return Type::getDoubleTy(getGlobalContext());
}
else if (type.name.compare("bool") == 0) {
return Type::getInt1Ty(getGlobalContext());
}
return Type::getVoidTy(getGlobalContext());
}
void CodeGenVisitor::visit(NSkip* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
// Generate a nop
new BitCastInst(Constant::getNullValue(
Type::getInt1Ty(getGlobalContext())),
Type::getInt1Ty(getGlobalContext()), "", Builder.GetInsertBlock());
}
void CodeGenVisitor::visit(NInteger* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
vals.push_front(ConstantInt::get(Type::getInt64Ty(getGlobalContext()), element->value, true));
}
void CodeGenVisitor::visit(NBool* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
if (element->value.compare("true") == 0) {
vals.push_front(ConstantInt::getTrue(getGlobalContext()));
return;
}
assert (element->value.compare("false") == 0);
vals.push_front(ConstantInt::getFalse(getGlobalContext()));
}
void CodeGenVisitor::visit(NDouble* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
vals.push_front(ConstantFP::get(Type::getDoubleTy(getGlobalContext()), element->value));
}
void CodeGenVisitor::visit(NType* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
// Do nothing
}
void CodeGenVisitor::visit(NSecurity* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
// Do nothing
}
void CodeGenVisitor::visit(NIdentifier* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
if (context->scope->LookUp(element->name) == NULL) {
assert(0); // Caught by type-checker
}
vals.push_front(new LoadInst(context->scope->LookUp(element->name)->value,
"", false, Builder.GetInsertBlock()));
}
void CodeGenVisitor::visit(NIfExpression* element, uint64_t flag)
{
switch (flag)
{
case V_FLAG_GUARD | V_FLAG_EXIT:
{
if (verbose) std::cout << "CodeGenVisitor if-guard-exit" << typeid(element).name() << std::endl;
Value *CondV = vals.front();
vals.pop_front(); // Pop guard
if (CondV == NULL) {
assert(0);
}
If* myIf = new If();
myIf->function = Builder.GetInsertBlock()->getParent();
// Create blocks for the then and else cases. Insert the 'then' block at the
// end of the function.
myIf->thenBB = BasicBlock::Create(getGlobalContext(), "if.then", myIf->function);
myIf->elseBB = BasicBlock::Create(getGlobalContext(), "if.else");
myIf->mergeBB = BasicBlock::Create(getGlobalContext(), "if.end");
Builder.CreateCondBr(CondV, myIf->thenBB, myIf->elseBB);
ifs.push_front(myIf);
}
break;
case V_FLAG_THEN | V_FLAG_ENTER:
if (verbose) std::cout << "CodeGenVisitor then-enter " << typeid(element).name() << std::endl;
// Emit then block.
Builder.SetInsertPoint(ifs.front()->thenBB);
break;
case V_FLAG_THEN | V_FLAG_EXIT:
if (verbose) std::cout << "CodeGenVisitor then-exit " << typeid(element).name() << std::endl;
Builder.CreateBr(ifs.front()->mergeBB);
break;
case V_FLAG_ELSE | V_FLAG_ENTER:
if (verbose) std::cout << "CodeGenVisitor else-enter " << typeid(element).name() << std::endl;
// Emit else block.
ifs.front()->function->getBasicBlockList().push_back(ifs.front()->elseBB);
Builder.SetInsertPoint(ifs.front()->elseBB);
break;
case V_FLAG_ELSE | V_FLAG_EXIT:
if (verbose) std::cout << "CodeGenVisitor else-exit " << typeid(element).name() << std::endl;
Builder.CreateBr(ifs.front()->mergeBB);
break;
case V_FLAG_EXIT:
if (verbose) std::cout << "CodeGenVisitor exit " << typeid(element).name() << std::endl;
// Emit merge block.
ifs.front()->function->getBasicBlockList().push_back(ifs.front()->mergeBB);
Builder.SetInsertPoint(ifs.front()->mergeBB);
{
If* myIf = ifs.front();
delete myIf;
}
ifs.pop_front();
break;
default:
return;
}
// No need to add anything to vals
}
void CodeGenVisitor::visit(NWhileExpression* element, uint64_t flag)
{
switch (flag)
{
case V_FLAG_GUARD | V_FLAG_ENTER:
{
if (verbose) std::cout << "CodeGenVisitor while-guard-enter" << typeid(element).name() << std::endl;
While* myWhile = new While();
whiles.push_front(myWhile);
whiles.front()->function = Builder.GetInsertBlock()->getParent();
// There will be three basic blocks:
// 1) condBB ("while.cond")
// perform the check for the loop condition. If true, branch to while.body, otherwise branch to while.end
// 2) bodyBB ("while.body")
// run the loop body then unconditionally branch to while.cond
// 3) endBB ("while.end")
// gets branched to from while.cond
//
// Note that initially there will be an unconditional branch to while.cond
// to see if the loop need to be executed at least once,
// or if the loop is to be skipped without executing at all
whiles.front()->condBB = BasicBlock::Create(getGlobalContext(), "while.cond", whiles.front()->function);
whiles.front()->bodyBB = BasicBlock::Create(getGlobalContext(), "while.body");
whiles.front()->endBB = BasicBlock::Create(getGlobalContext(), "while.end");
Builder.CreateBr(whiles.front()->condBB);
Builder.SetInsertPoint(whiles.front()->condBB);
}
break;
case V_FLAG_GUARD | V_FLAG_EXIT:
{
if (verbose) std::cout << "CodeGenVisitor while-guard-exit" << typeid(element).name() << std::endl;
}
break;
case V_FLAG_THEN | V_FLAG_ENTER:
{
if (verbose) std::cout << "CodeGenVisitor body-enter " << typeid(element).name() << std::endl;
Value *CondV = vals.front();
vals.pop_front(); // Pop guard
if (CondV == NULL) {
assert(0);
}
Builder.CreateCondBr(CondV, whiles.front()->bodyBB, whiles.front()->endBB);
whiles.front()->function->getBasicBlockList().push_back(whiles.front()->bodyBB);
Builder.SetInsertPoint(whiles.front()->bodyBB);
}
break;
case V_FLAG_THEN | V_FLAG_EXIT:
{
if (verbose) std::cout << "CodeGenVisitor body-exit " << typeid(element).name() << std::endl;
Builder.CreateBr(whiles.front()->condBB);
}
break;
case V_FLAG_EXIT:
if (verbose) std::cout << "CodeGenVisitor exit " << typeid(element).name() << std::endl;
whiles.front()->function->getBasicBlockList().push_back(whiles.front()->endBB);
Builder.SetInsertPoint(whiles.front()->endBB);
{
While* myWhile = whiles.front();
delete myWhile;
}
whiles.pop_front();
break;
default:
return;
}
// No need to add anything to vals
}
void CodeGenVisitor::visit(NBinaryOperator* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
Instruction::BinaryOps binstr;
Instruction::OtherOps oinstr;
CmpInst::Predicate pred;
Value* rhsv = vals.front();
vals.pop_front();
Value* lhsv = vals.front();
vals.pop_front();
switch (element->op) {
// Arith instructions
case TPLUS: binstr = Instruction::Add; goto math;
case TMINUS: binstr = Instruction::Sub; goto math;
case TMUL: binstr = Instruction::Mul; goto math;
case TDIV: binstr = Instruction::SDiv; goto math;
}
// For now, we assume that if the type checker passed,
// both lhs and rhs have the exact same value.
// So we don't promote the int 1 to a float 1.0,
// though we probably should
if (lhsv->getType() == Type::getDoubleTy(getGlobalContext())) {
// Comparision instructions, doubles
oinstr = Instruction::FCmp;
switch (element->op) {
case TCEQ: pred = CmpInst::FCMP_UEQ; goto comp;
case TCNE: pred = CmpInst::FCMP_UNE; goto comp;
case TCLT: pred = CmpInst::FCMP_ULT; goto comp;
case TCLE: pred = CmpInst::FCMP_ULE; goto comp;
case TCGT: pred = CmpInst::FCMP_UGT; goto comp;
case TCGE: pred = CmpInst::FCMP_UGE; goto comp;
}
} else {
// Comparison instructions, int
oinstr = Instruction::ICmp;
switch (element->op) {
case TCEQ: pred = CmpInst::ICMP_EQ; goto comp;
case TCNE: pred = CmpInst::ICMP_NE; goto comp;
case TCLT: pred = CmpInst::ICMP_SLT; goto comp;
case TCLE: pred = CmpInst::ICMP_SLE; goto comp;
case TCGT: pred = CmpInst::ICMP_SGT; goto comp;
case TCGE: pred = CmpInst::ICMP_SGE; goto comp;
}
}
comp:
vals.push_front(CmpInst::Create(oinstr, pred, lhsv, rhsv, "", Builder.GetInsertBlock()));
return;
math:
vals.push_front(BinaryOperator::Create(binstr, lhsv, rhsv, "", Builder.GetInsertBlock()));
return;
}
void CodeGenVisitor::visit(NAssignment* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
if (context->scope->LookUp(element->lhs.name) == NULL) {
assert(0); // Caught by type-checker
}
Value* rhsv = vals.front();
vals.pop_front();
// No need to add StoreInst to vals
new StoreInst(rhsv,
context->scope->LookUp(element->lhs.name)->value,
false, Builder.GetInsertBlock());
}
void CodeGenVisitor::visit(NBlock* element, uint64_t flag)
{
//static int size_on_entering = 0;
//static int size_on_leaving = 0;
switch (flag)
{
case V_FLAG_ENTER:
if (verbose) std::cout << "CodeGenVisitor entering " << typeid(element).name() << std::endl;
//size_on_entering = vals.size();
//std::cout << "Size on entering: " << size_on_entering << std::endl;;
context->scope->InitializeScope();
break;
case V_FLAG_EXIT:
if (verbose) std::cout << "CodeGenVisitor leaving " << typeid(element).name() << std::endl;
//size_on_leaving = vals.size();
//std::cout << "Size on leaving: " << size_on_leaving << std::endl;;
context->scope->FinalizeScope();
break;
default:
assert(0);
}
}
void CodeGenVisitor::visit(NExpressionStatement* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
}
void CodeGenVisitor::visit(NVariableDeclaration* element, uint64_t flag)
{
if (verbose) std::cout << "CodeGenVisitor " << typeid(element).name() << std::endl;
AllocaInst *alloc = new AllocaInst( (llvm::Type *) typeOf(element->type),
element->id.name.c_str(), Builder.GetInsertBlock());
Symbol* sym = new Symbol(alloc, new SType());
context->scope->Insert(element->id.name, sym);
// No need to add alloc to vals
}