diff --git a/src/codegen/llvm.rs b/src/codegen/llvm.rs index 7b76565..de9528b 100644 --- a/src/codegen/llvm.rs +++ b/src/codegen/llvm.rs @@ -1,7 +1,9 @@ use std::borrow::BorrowMut; use std::ffi::{CStr, CString}; use std::mem; +use std::ptr::null_mut; +use llvm_sys::analysis::LLVMVerifyModule; use llvm_sys::{ core::*, prelude::*, LLVMBasicBlock, LLVMBuilder, LLVMContext, LLVMModule, LLVMType, LLVMValue, }; @@ -105,7 +107,14 @@ impl<'a> IRModule<'a> { } pub fn print_to_string(&mut self) -> Result<&str, std::str::Utf8Error> { - unsafe { CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str() } + unsafe { + LLVMVerifyModule( + self.module, + llvm_sys::analysis::LLVMVerifierFailureAction::LLVMPrintMessageAction, + null_mut(), + ); + CStr::from_ptr(LLVMPrintModuleToString(self.module)).to_str() + } } } @@ -130,7 +139,6 @@ impl<'a, 'b> IRFunction<'a, 'b> { // TODO, fix later! let return_type = LLVMInt32TypeInContext(module.context.context); - let mut argts = []; let func_type = LLVMFunctionType(return_type, argts.as_mut_ptr(), argts.len() as u32, 0); diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 0069fb2..e13c66f 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -109,16 +109,16 @@ impl Expression { let IfExpression(expr, block, _) = ifx.as_ref(); let condition = expr.codegen(scope); - let mut then = IRBlock::new(scope.block.function, c"then"); - let mut after = IRBlock::new(scope.block.function, c"merge"); + let mut thenb = IRBlock::new(scope.block.function, c"then"); + let mut afterb = IRBlock::new(scope.block.function, c"merge"); - scope.block.branch(condition, &mut then, &mut after); - scope.block = after; + scope.block.branch(condition, &mut thenb, &mut afterb); + scope.block = afterb; - let mut inner = scope.inner(then); - match block.codegen(&mut inner) { - Some((ReturnType::Hard, v)) => inner.block.add_return(Some(v)), - _ => inner.block.move_into(&mut scope.block), + let mut then = scope.inner(thenb); + match block.codegen(&mut then) { + Some((ReturnType::Hard, v)) => then.block.add_return(Some(v)), + _ => then.block.move_into(&mut scope.block), } IRValue::from_literal(&crate::ast::Literal::I32(1), scope.block.function.module)