From 993c730912c7b1f17fcec7548ed200e95c4a436d Mon Sep 17 00:00:00 2001 From: Florian Sattler Date: Sat, 1 Jun 2013 18:10:13 +0200 Subject: [PATCH] first stuff of randomif --- CMakeLists.txt | 1 + src/AST/CodeGenVisitor.cpp | 23 +++++++++++++++++++++++ src/libscully.c | 9 +++++++++ 3 files changed, 33 insertions(+) create mode 100644 src/libscully.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ce7658..047e2d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ SET(SCULLY_SOURCE src/test.cpp src/Token.cpp + src/libscully.c src/AST/ASTElement.cpp src/AST/ASTVisitor.cpp diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index 681a694..6b013a0 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -176,6 +176,29 @@ void CodeGenVisitor::visit(RandomForStatement* e) { } void CodeGenVisitor::visit(RandomIfStatement* e) { + value_ = 0; + e->getProb()->accept(this); + + llvm::Function* f = builder_->GetInsertBlock()->getParent(); + llvm::BasicBlock* thenBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "then", f); + llvm::BasicBlock* elseBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "else"); + llvm::BasicBlock* mergeBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "merge"); + + builder_->CreateCondBr(value_, thenBB, elseBB); + + // then + builder_->SetInsertPoint(thenBB);# + e->getStmt()->accept(this); + + builder_->CreateBr(mergeBB); + + f->getBasicBlockList().push_back(elseBB); + builder_->SetInsertPoint(elseBB); + + builder_->CreateBr(mergeBB); + + f->getBasicBlockList().push_back(mergeBB); + builder_->SetInsertPoint(mergeBB); } void CodeGenVisitor::visit(ReturnStatement* e) { diff --git a/src/libscully.c b/src/libscully.c new file mode 100644 index 0000000..dc487d3 --- /dev/null +++ b/src/libscully.c @@ -0,0 +1,9 @@ +#include +#include + +bool random_if(int p) { + srand(time(NULL)); + int r = rand() % 100; + + return (r < p); +}