Verify before printing

This commit is contained in:
Sofia 2024-09-12 20:41:23 +03:00
parent 7f3a3ac1f8
commit a3642f127c
2 changed files with 18 additions and 10 deletions

View File

@ -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);

View File

@ -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)