Repository navigation
Expand file tree
/
Copy pathLoopIrGen.cpp
More file actions
38 lines (35 loc) · 1.53 KB
/
Copy pathLoopIrGen.cpp
File metadata and controls
38 lines (35 loc) · 1.53 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
#include "AST.hpp"
optional<Value*> ForExpr::codegen (CompilationContext &ctx) {
ctx.pushFrame();
auto f = ctx.builder->GetInsertBlock()->getParent();
varDecl->codegen(ctx);
BasicBlock *forCond = BasicBlock::Create(*ctx.context, ctx.names.use("for_cond"), f);
BasicBlock *bodyBB = BasicBlock::Create(*ctx.context, ctx.names.use("for"), f);
BasicBlock *endBB = BasicBlock::Create(*ctx.context, ctx.names.use("for_end"), f);
ctx.builder->CreateBr(forCond);
ctx.builder->SetInsertPoint(forCond);
ctx.builder->CreateCondBr(*(condition->codegen(ctx)), bodyBB, endBB);
ctx.builder->SetInsertPoint(bodyBB);
body->codegen(ctx);
operation->codegen(ctx);
ctx.builder->CreateBr(forCond);
ctx.builder->SetInsertPoint(endBB);
ctx.popFrame();
return nullopt;
}
optional<Value*> WhileExpr::codegen(CompilationContext &ctx) {
ctx.pushFrame();
Function *f = ctx.builder->GetInsertBlock()->getParent();
BasicBlock *whileCond = BasicBlock::Create(*ctx.context, ctx.names.use("while_cond"), f);
BasicBlock *bodyBB = BasicBlock::Create(*ctx.context, ctx.names.use("while"), f);
BasicBlock *endBB = BasicBlock::Create(*ctx.context, ctx.names.use("while_end"), f);
ctx.builder->CreateBr(whileCond);
ctx.builder->SetInsertPoint(whileCond);
ctx.builder->CreateCondBr(*(cond->codegen(ctx)), bodyBB, endBB);
ctx.builder->SetInsertPoint(bodyBB);
body->codegen(ctx);
ctx.builder->CreateBr(whileCond);
ctx.builder->SetInsertPoint(endBB);
ctx.popFrame();
return nullopt;
}