-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhile.cpp
More file actions
49 lines (39 loc) · 1.54 KB
/
while.cpp
File metadata and controls
49 lines (39 loc) · 1.54 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
#include "while.hpp"
While::While(Condition *c, Stmt *s) : cond(c), stmt(s) {}
While::~While() { delete cond; delete stmt; }
void While::printAST(std::ostream &out) const {
out << "While(" << *cond << "," << *stmt << ")";
}
void While::sem()
{
cond->type_check(typeBoolean);
stmt->sem();
}
llvm::Value* While::compile()
{
/* Grab current function */
llvm::Function *TheFunction = Builder.GetInsertBlock()->getParent();
/* Create basic blocks*/
llvm::BasicBlock *WhileCondBB = llvm::BasicBlock::Create(TheContext, "loop", TheFunction);
llvm::BasicBlock *WhileBodyBB = llvm::BasicBlock::Create(TheContext, "body", TheFunction);
llvm::BasicBlock *AfterWhileBB = llvm::BasicBlock::Create(TheContext, "endwhile", TheFunction);
/* Enter condition basic block */
Builder.CreateBr(WhileCondBB);
/* Compile condition */
Builder.SetInsertPoint(WhileCondBB);
llvm::Value *Cond = cond->compile();
/* Check condition */
Builder.CreateCondBr(Cond, WhileBodyBB, AfterWhileBB);
/* Compile body */
Builder.SetInsertPoint(WhileBodyBB);
stmt->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 "WhileBodyBB" BasicBlock will have two terminators */
if(!stmt->willReturn())
/* Jump to condition in order to re-evaluate it */
Builder.CreateBr(WhileCondBB);
/* Setup compilation for the code that follows after the while-statement*/
Builder.SetInsertPoint(AfterWhileBB);
return nullptr;
}