From 4b49bd28e784be2d389aa49915c7f29480fde258 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Sun, 23 Feb 2020 10:52:19 +0100 Subject: [PATCH] Implement "ho visto" statement. --- src/codegen.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/codegen.cpp b/src/codegen.cpp index f47374d..915a21d 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -102,6 +102,7 @@ public: llvm::Value* visitLoopStatement(const LoopStatement* l); llvm::Value* visitInputStatement(const InputStatement* s); llvm::Value* visitPrintStatement(const PrintStatement* p); + llvm::Value* visitAssertStatement(const AssertStatement* a); llvm::Value* visitAbortStatement(const AbortStatement* a); llvm::Value* visitExpressionStatement(const ExpressionStatement* s) { visit(s->getExpression()); @@ -541,6 +542,27 @@ llvm::Value* IRGenerator::visitPrintStatement(const PrintStatement* p) { return nullptr; } +llvm::Value* IRGenerator::visitAssertStatement(const AssertStatement* a) { + llvm::BasicBlock* check_bb = + llvm::BasicBlock::Create(context_, "assert.check", current_function()); + llvm::BasicBlock* fail_bb = llvm::BasicBlock::Create(context_, "assert.fail", current_function()); + llvm::BasicBlock* success_bb = + llvm::BasicBlock::Create(context_, "assert.success", current_function()); + + builder_.CreateBr(check_bb); + + builder_.SetInsertPoint(check_bb); + llvm::Value* condition = evalBooleanCondition(a->getExpression()); + builder_.CreateCondBr(condition, success_bb, fail_bb); + + builder_.SetInsertPoint(fail_bb); + visitAbortStatement(nullptr); // Assert is just a conditional abort. + builder_.CreateBr(success_bb); // We will never get here, but LLVM does not know. + + builder_.SetInsertPoint(success_bb); + return nullptr; +} + llvm::Value* IRGenerator::visitAbortStatement(const AbortStatement*) { auto abort_builtin = module_->getFunction("abort"); assert(abort_builtin && "Builtin abort was not declared");