Skip to content

Commit 5a9ae5e

Browse files
committed
Add if codegen
1 parent 131eb34 commit 5a9ae5e

File tree

9 files changed

+108
-4
lines changed

9 files changed

+108
-4
lines changed

AST/StatementAST.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ class IfAST : public StatementAST
187187
condition(condition), thenBlock(thenBlock), elseBlock(elseBlock)
188188
{}
189189

190+
std::shared_ptr<ExpressionAST> getCondition()
191+
{
192+
return condition;
193+
}
194+
195+
std::shared_ptr<BlockAST> getThenBlock()
196+
{
197+
return thenBlock;
198+
}
199+
200+
std::shared_ptr<BlockAST> getElseBlock()
201+
{
202+
return elseBlock;
203+
}
204+
190205
virtual llvm::Value * accept(Demux * demux) override;
191206
};
192207

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ set(SOURCES
2929
TypeSystem/Types.cpp
3030
AST/StructAST.cpp
3131
CodeGen/AssignmentGen.cpp
32-
CodeGen/VarDeclGen.cpp CodeGen/ForGen.cpp CodeGen/ForGen.hpp CodeGen/BuiltInFuncGen.cpp CodeGen/BuiltInFuncGen.hpp)
32+
CodeGen/VarDeclGen.cpp CodeGen/ForGen.cpp CodeGen/ForGen.hpp CodeGen/BuiltInFuncGen.cpp CodeGen/BuiltInFuncGen.hpp CodeGen/IfGen.cpp CodeGen/IfGen.hpp)
3333

3434
add_executable(vflc Main.cpp ${SOURCES})

CodeGen/IfGen.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "IfGen.hpp"
2+
3+
#include "../AST/FunctionAST.hpp"
4+
5+
llvm::Value * IfGen::emit(VflModule & module, IfAST & node)
6+
{
7+
auto condition = module.loadIfPtr(demux, node.getCondition());
8+
9+
auto function = module.getBuilder().GetInsertBlock()->getParent();
10+
11+
auto thenBlock = llvm::BasicBlock::Create(llvm::getGlobalContext(), "then", function);
12+
auto elseBlock = llvm::BasicBlock::Create(llvm::getGlobalContext(), "else");
13+
auto mergeBlock = llvm::BasicBlock::Create(llvm::getGlobalContext(), "ifcont");
14+
15+
if (node.getElseBlock()) {
16+
module.getBuilder().CreateCondBr(condition, thenBlock, elseBlock);
17+
} else {
18+
module.getBuilder().CreateCondBr(condition, thenBlock, mergeBlock);
19+
}
20+
21+
module.getBuilder().SetInsertPoint(thenBlock);
22+
23+
module.createScope();
24+
node.getThenBlock()->accept(demux);
25+
module.popScope();
26+
27+
thenBlock = module.getBuilder().GetInsertBlock();
28+
29+
if (thenBlock->getTerminator() == nullptr) {
30+
module.getBuilder().CreateBr(mergeBlock);
31+
}
32+
33+
if (node.getElseBlock()) {
34+
function->getBasicBlockList().push_back(elseBlock);
35+
module.getBuilder().SetInsertPoint(elseBlock);
36+
37+
module.createScope();
38+
node.getElseBlock()->accept(demux);
39+
module.popScope();
40+
41+
module.getBuilder().CreateBr(mergeBlock);
42+
}
43+
44+
function->getBasicBlockList().push_back(mergeBlock);
45+
module.getBuilder().SetInsertPoint(mergeBlock);
46+
47+
return nullptr;
48+
}

CodeGen/IfGen.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef VFL_IFGEN_HPP
2+
#define VFL_IFGEN_HPP
3+
4+
5+
#include "../Demux/DemuxComponent.hpp"
6+
#include "Generator.hpp"
7+
#include "../AST/StatementAST.hpp"
8+
9+
class IfGen : public DemuxComponent, public Generator<IfAST>
10+
{
11+
public:
12+
virtual llvm::Value * emit(VflModule & module, IfAST & node) override;
13+
};
14+
15+
16+
#endif //VFL_IFGEN_HPP

Demux/Demux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ llvm::Value * Demux::visit(ReturnAST & node)
117117

118118
llvm::Value * Demux::visit(IfAST & node)
119119
{
120-
return nullptr; // ifGen.emit(module, node);
120+
return ifGen.emit(module, node);
121121
}
122122

123123
llvm::Value * Demux::visit(PrintAST & node)

Demux/Demux.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "../CodeGen/VarDeclGen.hpp"
2121
#include "../CodeGen/ForGen.hpp"
2222
#include "../CodeGen/BuiltInFuncGen.hpp"
23+
#include "../CodeGen/IfGen.hpp"
2324

2425
class FunctionAST;
2526
class Program;
@@ -57,6 +58,8 @@ class Demux : private NonCopyable
5758

5859
BuiltInFuncGen builtInFuncGen;
5960

61+
IfGen ifGen;
62+
6063
public:
6164
Demux(VflModule & module) :
6265
module(module)
@@ -73,6 +76,7 @@ class Demux : private NonCopyable
7376
varDeclGen.setDemux(this);
7477
forGen.setDemux(this);
7578
builtInFuncGen.setDemux(this);
79+
ifGen.setDemux(this);
7680
}
7781

7882
void walkAST(Program & program);

examples/demo.vfl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@Main ()
2-
for i = 0, i < 4 {
3-
print i
2+
if true {
3+
4+
} else {
5+
46
}
57
End

tools/vfsc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
s="$1"
4+
s=${s##*/}
5+
s=${s%.*}
6+
7+
/Users/mijara/Projects/vfs/build/vfsc $1 2> "$s.bc"
8+
/usr/local/opt/llvm/bin/llc -filetype=obj "$s.bc"
9+
clang "$s.o" -o "$s" 2> /dev/null
10+
rm -f "$s.bc" "$s.o"

tools/vfsr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
s="$1"
4+
s=${s##*/}
5+
s=${s%.*}
6+
7+
vfsc $1
8+
./$s
9+
rm -f $s

0 commit comments

Comments
 (0)