-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathif.cpp
More file actions
59 lines (47 loc) · 1.86 KB
/
if.cpp
File metadata and controls
59 lines (47 loc) · 1.86 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
#include "if.hpp"
If::If(Condition *c, Stmt *s1, Stmt *s2) : cond(c), stmt1(s1), stmt2(s2) {}
If::~If() { delete cond; delete stmt1; delete stmt2; }
void If::printAST(std::ostream &out) const {
out << "If(" << *cond << ", " << *stmt1;
if (stmt2 != nullptr) out << ", " << *stmt2;
out << ")";
}
void If::sem()
{
cond->type_check(typeBoolean);
stmt1->sem();
if (stmt2 != nullptr) stmt2->sem();
}
llvm::Value* If::compile()
{
/* Compile condition */
llvm::Value *v = cond->compile();
/* Grab current function */
llvm::Function *TheFunction = Builder.GetInsertBlock()->getParent();
/* Create basic blocks*/
llvm::BasicBlock *ThenBB = llvm::BasicBlock::Create(TheContext, "then", TheFunction);
llvm::BasicBlock *ElseBB = llvm::BasicBlock::Create(TheContext, "else", TheFunction);
llvm::BasicBlock *AfterBB = llvm::BasicBlock::Create(TheContext, "endif", TheFunction);
/* Check condition and branch to "then" or to "else" */
Builder.CreateCondBr(v, ThenBB, ElseBB);
/* Compile "then" */
Builder.SetInsertPoint(ThenBB);
stmt1->compile();
/* If the compiled stmt is a return statement or a block that contains a return statement,
* then appending a "br" is useless and will lead to invalid LLVM IR
* because the "ThenBB" BasicBlock will have two terminators */
if(!stmt1->willReturn())
Builder.CreateBr(AfterBB);
/* Compile "else" */
Builder.SetInsertPoint(ElseBB);
if (stmt2 != nullptr)
stmt2->compile();
/* If the compiled stmt is a return statement or a block that contains a return statement,
* then appending a "br" is useless and will lead to invalid LLVM IR
* because the "ElseBB" BasicBlock will have two terminators */
if (!stmt2 || !stmt2->willReturn())
Builder.CreateBr(AfterBB);
/* Setup compilation for the code that follows after the if-statement */
Builder.SetInsertPoint(AfterBB);
return nullptr;
}