From c485352850bcaa86fd41c658361b43c869d52c96 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Sun, 2 Jun 2013 00:01:38 +0200 Subject: [PATCH 1/5] More error handling --- src/AST/CodeGenVisitor.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index 74fcd46..f8488db 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -214,6 +214,9 @@ void CodeGenVisitor::visit(FunctionDefinition* e) { void CodeGenVisitor::visit(IfStatement* e) { value_ = 0; e->getCond()->accept(this); + if (!value_) { + throw "error evaluating expression"; + } llvm::Function* f = builder_->GetInsertBlock()->getParent(); llvm::BasicBlock* thenBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "then", f); @@ -238,17 +241,21 @@ void CodeGenVisitor::visit(IfStatement* e) { } void CodeGenVisitor::visit(ParameterList* e) { + // NOT USED } void CodeGenVisitor::visit(RandomForStatement* e) { - value_ = 0; e->getInit()->accept(this); + value_ = 0; e->getProb()->accept(this); - llvm::Function* cf = module_->getFunction("random_if"); - llvm::Value* prob = builder_->CreateCall(cf,value_,"callTmp"); + if (!value_) { + 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::BasicBlock* loopBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "loop", f); @@ -258,20 +265,14 @@ void CodeGenVisitor::visit(RandomForStatement* e) { value_ = 0; e->getStmt()->accept(this); - if (value_ == 0) { - // throw err - } value_ = 0; e->getStep()->accept(this); - if (value_ == 0) { - // throw err - } value_ = 0; e->getProb()->accept(this); if (value_ == 0) { - // throw err + throw "error evaluating expression"; } llvm::BasicBlock* afterBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterLoop",f); @@ -285,6 +286,10 @@ void CodeGenVisitor::visit(RandomForStatement* e) { void CodeGenVisitor::visit(RandomIfStatement* e) { value_ = 0; e->getProb()->accept(this); + if (!value_) { + throw "error evaluating expression"; + } + llvm::Function* cf = module_->getFunction("random_if"); llvm::Value* cond = builder_->CreateCall(cf,value_,"callTmp"); @@ -312,6 +317,11 @@ void CodeGenVisitor::visit(RandomIfStatement* e) { void CodeGenVisitor::visit(ReturnStatement* e) { e->getExpr()->accept(this); + + if (!value_) { + throw "error evaluating expression"; + } + builder_->CreateRet(value_); } From c4e22b86278f59af25cbb9a6a02b416c7f555f57 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Sun, 2 Jun 2013 00:05:26 +0200 Subject: [PATCH 2/5] More exception handling ... --- src/AST/CodeGenVisitor.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index f8488db..a3eabaf 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -79,7 +79,11 @@ void CodeGenVisitor::visit(ConstantExpression* e) { } void CodeGenVisitor::visit(ExpressionStatement* e) { + value_ = 0; e->getExpr()->accept(this); + if (!value_) { + throw "error evaluating expression"; + } } void CodeGenVisitor::visit(ForStatement* e) @@ -87,7 +91,11 @@ void CodeGenVisitor::visit(ForStatement* e) value_ = 0; e->getInit()->accept(this); + value_ = 0; e->getCond()->accept(this); + if (!value_) { + throw "error evaluating expression"; + } llvm::Function* f = builder_->GetInsertBlock()->getParent(); llvm::BasicBlock* loopBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "loop", f); @@ -97,20 +105,14 @@ void CodeGenVisitor::visit(ForStatement* e) value_ = 0; e->getStmt()->accept(this); - if (value_ == 0) { - // throw err - } value_ = 0; e->getStep()->accept(this); - if (value_ == 0) { - // throw err - } value_ = 0; e->getCond()->accept(this); - if (value_ == 0) { - // throw err + if (!value_) { + throw "error evaluating expression"; } llvm::BasicBlock* afterBB = llvm::BasicBlock::Create(llvm::getGlobalContext(), "afterLoop",f); From c438618c536bb36e9b7c76008845016ed552b65f Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Sun, 2 Jun 2013 00:08:43 +0200 Subject: [PATCH 3/5] More error handling again --- src/AST/CodeGenVisitor.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index a3eabaf..95a8a9d 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -125,13 +125,13 @@ void CodeGenVisitor::visit(ForStatement* e) void CodeGenVisitor::visit(FunctionCallExpression* e) { llvm::Function* cf = module_->getFunction(e->getId()); if (!cf) { - // TODO error + throw "function to call not found"; return; } auto values = e->getValues()->getValues(); if (cf->arg_size() != values.size()) { - // TODO error + throw "argument size mismatch"; return; } @@ -142,7 +142,7 @@ void CodeGenVisitor::visit(FunctionCallExpression* e) { Expression *expr = (*iter); expr->accept(this); if (!value_) { - // TODO error + throw "error evaluating expression"; } args.push_back(value_); } @@ -234,7 +234,7 @@ void CodeGenVisitor::visit(IfStatement* e) { f->getBasicBlockList().push_back(elseBB); builder_->SetInsertPoint(elseBB); - // we cna add an else part here later ... + // we can add an else part here later ... builder_->CreateBr(mergeBB); @@ -319,7 +319,6 @@ void CodeGenVisitor::visit(RandomIfStatement* e) { void CodeGenVisitor::visit(ReturnStatement* e) { e->getExpr()->accept(this); - if (!value_) { throw "error evaluating expression"; } From 868d3b27419a0ee592fe4dfbfeecf5e8042064d7 Mon Sep 17 00:00:00 2001 From: Peter Dahlberg Date: Sun, 2 Jun 2013 00:10:20 +0200 Subject: [PATCH 4/5] binop type check --- src/AST/CodeGenVisitor.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index 74fcd46..8244096 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -43,6 +43,10 @@ void CodeGenVisitor::visit(BinOpExpression* e) { return; } + if (lhs->getType() != rhs->getType()) { + throw "lhs type of binop != rhs type of binop"; + } + switch (e->getOp()) { case BinOp::PLUS: value_ = builder_->CreateAdd(lhs, rhs, "addtmp"); @@ -63,7 +67,7 @@ void CodeGenVisitor::visit(BinOpExpression* e) { value_ = builder_->CreateICmpSLT(lhs, rhs, "cmptmp"); break; default: - // TODO error + throw "Unkown Operator, This is a Bug!"; break; } } From 00035eb0292921028da0810bb7691eca76bf92f1 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Sun, 2 Jun 2013 00:11:07 +0200 Subject: [PATCH 5/5] Guess what ... yes! error handling --- src/AST/CodeGenVisitor.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index ae65bfd..66e4d01 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -24,7 +24,6 @@ CodeGenVisitor::~CodeGenVisitor() { void CodeGenVisitor::visit(AssignmentExpression* e) { value_ = 0; e->getExpr()->accept(this); - if (value_ == 0) { throw "error creating expression"; } @@ -34,14 +33,16 @@ void CodeGenVisitor::visit(AssignmentExpression* e) { void CodeGenVisitor::visit(BinOpExpression* e) { e->getLeftExp()->accept(this); - llvm::Value* lhs = value_; - e->getRightExp()->accept(this); - llvm::Value* rhs = value_; - - if ((!lhs) || (!rhs)) { - // TODO error - return; + if (!value_) { + throw "error evaluating expression (lhs)"; } + llvm::Value* lhs = value_; + + e->getRightExp()->accept(this); + if (!value_) { + throw "error evaluating expression (rhs)"; + } + llvm::Value* rhs = value_; if (lhs->getType() != rhs->getType()) { throw "lhs type of binop != rhs type of binop";