Merge branch 'master' of git.tuxzone.org:woc2013

This commit is contained in:
Florian Sattler
2013-06-02 00:13:13 +02:00

View File

@@ -24,7 +24,6 @@ CodeGenVisitor::~CodeGenVisitor() {
void CodeGenVisitor::visit(AssignmentExpression* e) { void CodeGenVisitor::visit(AssignmentExpression* e) {
value_ = 0; value_ = 0;
e->getExpr()->accept(this); e->getExpr()->accept(this);
if (value_ == 0) { if (value_ == 0) {
throw "error creating expression"; throw "error creating expression";
} }
@@ -34,13 +33,19 @@ void CodeGenVisitor::visit(AssignmentExpression* e) {
void CodeGenVisitor::visit(BinOpExpression* e) { void CodeGenVisitor::visit(BinOpExpression* e) {
e->getLeftExp()->accept(this); e->getLeftExp()->accept(this);
if (!value_) {
throw "error evaluating expression (lhs)";
}
llvm::Value* lhs = value_; llvm::Value* lhs = value_;
e->getRightExp()->accept(this); e->getRightExp()->accept(this);
if (!value_) {
throw "error evaluating expression (rhs)";
}
llvm::Value* rhs = value_; llvm::Value* rhs = value_;
if ((!lhs) || (!rhs)) { if (lhs->getType() != rhs->getType()) {
// TODO error throw "lhs type of binop != rhs type of binop";
return;
} }
switch (e->getOp()) { switch (e->getOp()) {
@@ -63,7 +68,7 @@ void CodeGenVisitor::visit(BinOpExpression* e) {
value_ = builder_->CreateICmpSLT(lhs, rhs, "cmptmp"); value_ = builder_->CreateICmpSLT(lhs, rhs, "cmptmp");
break; break;
default: default:
// TODO error throw "Unkown Operator, This is a Bug!";
break; break;
} }
} }
@@ -79,7 +84,11 @@ void CodeGenVisitor::visit(ConstantExpression* e) {
} }
void CodeGenVisitor::visit(ExpressionStatement* e) { void CodeGenVisitor::visit(ExpressionStatement* e) {
value_ = 0;
e->getExpr()->accept(this); e->getExpr()->accept(this);
if (!value_) {
throw "error evaluating expression";
}
} }
void CodeGenVisitor::visit(ForStatement* e) void CodeGenVisitor::visit(ForStatement* e)
@@ -87,7 +96,11 @@ void CodeGenVisitor::visit(ForStatement* e)
value_ = 0; value_ = 0;
e->getInit()->accept(this); e->getInit()->accept(this);
value_ = 0;
e->getCond()->accept(this); e->getCond()->accept(this);
if (!value_) {
throw "error evaluating expression";
}
llvm::Function* f = builder_->GetInsertBlock()->getParent(); llvm::Function* f = builder_->GetInsertBlock()->getParent();
llvm::BasicBlock* loopBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "loop", f); llvm::BasicBlock* loopBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "loop", f);
@@ -97,20 +110,14 @@ void CodeGenVisitor::visit(ForStatement* e)
value_ = 0; value_ = 0;
e->getStmt()->accept(this); e->getStmt()->accept(this);
if (value_ == 0) {
// throw err
}
value_ = 0; value_ = 0;
e->getStep()->accept(this); e->getStep()->accept(this);
if (value_ == 0) {
// throw err
}
value_ = 0; value_ = 0;
e->getCond()->accept(this); e->getCond()->accept(this);
if (value_ == 0) { if (!value_) {
// throw err throw "error evaluating expression";
} }
llvm::BasicBlock* afterBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterLoop",f); llvm::BasicBlock* afterBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterLoop",f);
@@ -123,13 +130,13 @@ void CodeGenVisitor::visit(ForStatement* e)
void CodeGenVisitor::visit(FunctionCallExpression* e) { void CodeGenVisitor::visit(FunctionCallExpression* e) {
llvm::Function* cf = module_->getFunction(e->getId()); llvm::Function* cf = module_->getFunction(e->getId());
if (!cf) { if (!cf) {
// TODO error throw "function to call not found";
return; return;
} }
auto values = e->getValues()->getValues(); auto values = e->getValues()->getValues();
if (cf->arg_size() != values.size()) { if (cf->arg_size() != values.size()) {
// TODO error throw "argument size mismatch";
return; return;
} }
@@ -140,7 +147,7 @@ void CodeGenVisitor::visit(FunctionCallExpression* e) {
Expression *expr = (*iter); Expression *expr = (*iter);
expr->accept(this); expr->accept(this);
if (!value_) { if (!value_) {
// TODO error throw "error evaluating expression";
} }
args.push_back(value_); args.push_back(value_);
} }
@@ -214,6 +221,9 @@ void CodeGenVisitor::visit(FunctionDefinition* e) {
void CodeGenVisitor::visit(IfStatement* e) { void CodeGenVisitor::visit(IfStatement* e) {
value_ = 0; value_ = 0;
e->getCond()->accept(this); e->getCond()->accept(this);
if (!value_) {
throw "error evaluating expression";
}
llvm::Function* f = builder_->GetInsertBlock()->getParent(); llvm::Function* f = builder_->GetInsertBlock()->getParent();
llvm::BasicBlock* thenBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "then", f); llvm::BasicBlock* thenBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "then", f);
@@ -229,7 +239,7 @@ void CodeGenVisitor::visit(IfStatement* e) {
f->getBasicBlockList().push_back(elseBB); f->getBasicBlockList().push_back(elseBB);
builder_->SetInsertPoint(elseBB); builder_->SetInsertPoint(elseBB);
// we cna add an else part here later ... // we can add an else part here later ...
builder_->CreateBr(mergeBB); builder_->CreateBr(mergeBB);
@@ -238,17 +248,21 @@ void CodeGenVisitor::visit(IfStatement* e) {
} }
void CodeGenVisitor::visit(ParameterList* e) { void CodeGenVisitor::visit(ParameterList* e) {
// NOT USED
} }
void CodeGenVisitor::visit(RandomForStatement* e) { void CodeGenVisitor::visit(RandomForStatement* e) {
value_ = 0; value_ = 0;
e->getInit()->accept(this); e->getInit()->accept(this);
value_ = 0;
e->getProb()->accept(this); e->getProb()->accept(this);
llvm::Function* cf = module_->getFunction("random_if"); if (!value_) {
llvm::Value* prob = builder_->CreateCall(cf,value_,"callTmp"); throw "error evaluating expression";
}
llvm::Function* cf = module_->getFunction("random_if");
llvm::Value* prob = builder_->CreateCall(cf, value_, "callTmp");
llvm::Function* f = builder_->GetInsertBlock()->getParent(); llvm::Function* f = builder_->GetInsertBlock()->getParent();
llvm::BasicBlock* loopBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "loop", f); llvm::BasicBlock* loopBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "loop", f);
@@ -258,20 +272,14 @@ void CodeGenVisitor::visit(RandomForStatement* e) {
value_ = 0; value_ = 0;
e->getStmt()->accept(this); e->getStmt()->accept(this);
if (value_ == 0) {
// throw err
}
value_ = 0; value_ = 0;
e->getStep()->accept(this); e->getStep()->accept(this);
if (value_ == 0) {
// throw err
}
value_ = 0; value_ = 0;
e->getProb()->accept(this); e->getProb()->accept(this);
if (value_ == 0) { if (value_ == 0) {
// throw err throw "error evaluating expression";
} }
llvm::BasicBlock* afterBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterLoop",f); llvm::BasicBlock* afterBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterLoop",f);
@@ -285,6 +293,10 @@ void CodeGenVisitor::visit(RandomForStatement* e) {
void CodeGenVisitor::visit(RandomIfStatement* e) { void CodeGenVisitor::visit(RandomIfStatement* e) {
value_ = 0; value_ = 0;
e->getProb()->accept(this); e->getProb()->accept(this);
if (!value_) {
throw "error evaluating expression";
}
llvm::Function* cf = module_->getFunction("random_if"); llvm::Function* cf = module_->getFunction("random_if");
llvm::Value* cond = builder_->CreateCall(cf,value_,"callTmp"); llvm::Value* cond = builder_->CreateCall(cf,value_,"callTmp");
@@ -312,6 +324,10 @@ void CodeGenVisitor::visit(RandomIfStatement* e) {
void CodeGenVisitor::visit(ReturnStatement* e) { void CodeGenVisitor::visit(ReturnStatement* e) {
e->getExpr()->accept(this); e->getExpr()->accept(this);
if (!value_) {
throw "error evaluating expression";
}
builder_->CreateRet(value_); builder_->CreateRet(value_);
} }