-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconstructs.cpp
48 lines (45 loc) · 1.8 KB
/
constructs.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
/**
* Implementation of \ref Constructs class
*/
#include "constructs.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
/**
* Constructor for the class
* Initialises the fields
*/
Constructs::Constructs() {
this->Builder = new IRBuilder<>(Context);
this->loops = new std::stack<loopInfo*>();
errors = 0;
this->TheModule = new Module("Decaf compiler", Context);
this->TheFPM = new llvm::legacy::FunctionPassManager(TheModule);
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();
}
/**
* Allocates memory for local variables on the stack of the function by creating an alloca instruction
* @param TheFunction Function whose local variable is to allocated memory
* @param VarName name of the variable
* @param type type of the variable
* @return alloca instruction for creating memory for given variable in the given function
*/
AllocaInst *Constructs::CreateEntryBlockAlloca(Function *TheFunction, std::string VarName, std::string type) {
/* Get the builder for current context */
IRBuilder<> TmpB(&TheFunction->getEntryBlock(), TheFunction->getEntryBlock().begin());
AllocaInst *alloca_instruction = nullptr;
if (type == "int") {
alloca_instruction = TmpB.CreateAlloca(Type::getInt32Ty(this->Context), 0, VarName);
} else if (type == "boolean") {
alloca_instruction = TmpB.CreateAlloca(Type::getInt1Ty(this->Context), 0, VarName);
}
return alloca_instruction;
}