From 1cc00cc850d4d656dbdbd6cca33790579435f1b1 Mon Sep 17 00:00:00 2001 From: Markus Hauschild Date: Sat, 1 Jun 2013 20:45:22 +0200 Subject: [PATCH] Fix load/store related stuff and verify functions ... --- src/AST/CodeGenVisitor.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/AST/CodeGenVisitor.cpp b/src/AST/CodeGenVisitor.cpp index c1b85e9..893b6b7 100644 --- a/src/AST/CodeGenVisitor.cpp +++ b/src/AST/CodeGenVisitor.cpp @@ -1,4 +1,5 @@ #include "AST/CodeGenVisitor.h" +#include "llvm/Analysis/Verifier.h" #include CodeGenVisitor::CodeGenVisitor(llvm::Module* module, llvm::FunctionPassManager *fpm) { @@ -55,10 +56,10 @@ void CodeGenVisitor::visit(BinOpExpression* e) { value_ = builder_->CreateSDiv(lhs, rhs, "divtmp"); break; case BinOp::EQUALS: - value_ = builder_->CreateICmpEQ(lhs, rhs, "eqtmp"); + value_ = builder_->CreateICmpEQ(lhs, rhs, "cmptmp"); break; case BinOp::LESS: - value_ = builder_->CreateICmpSLT(lhs, rhs, "eqtmp"); + value_ = builder_->CreateICmpSLT(lhs, rhs, "cmptmp"); break; default: // TODO error @@ -84,7 +85,6 @@ void CodeGenVisitor::visit(ForStatement* e) value_ = 0; e->getInit()->accept(this); - e->getCond()->accept(this); llvm::Function* f = builder_->GetInsertBlock()->getParent(); @@ -93,22 +93,18 @@ void CodeGenVisitor::visit(ForStatement* e) builder_->CreateBr(loopBB); builder_->SetInsertPoint(loopBB); - 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) { @@ -204,7 +200,13 @@ void CodeGenVisitor::visit(FunctionDefinition* e) { // build code for the statements e->getSl()->accept(this); - // TODO we might want to call verifyFunction ... + f->dump(); + // validate generated code + llvm::verifyFunction(*f); + + // optimize function + //fpm_->run(*f); + value_ = f; } @@ -291,6 +293,7 @@ void CodeGenVisitor::visit(StatementList* e) { } void CodeGenVisitor::visit(ValueList* e) { + // NOT USED } void CodeGenVisitor::visit(VariableDefinition* e) { @@ -301,7 +304,13 @@ void CodeGenVisitor::visit(VariableDefinition* e) { } void CodeGenVisitor::visit(LoadExpression *e) { - value_ = getNamedValue(e->getId()); + llvm::Value* v = getNamedValue(e->getId()); + + if (!v) { + throw "unknown variable name"; + } + + value_ = builder_->CreateLoad(v, e->getId()); } void CodeGenVisitor::JIT(Expression* e) {