Add RetVoid just in case

This commit is contained in:
Sofia 2025-07-09 22:01:32 +03:00
parent 94c4ec0613
commit 1aa9b3e76c
5 changed files with 15 additions and 4 deletions

View File

@ -269,6 +269,7 @@ impl Builder {
if let Some(term) = &other.data.terminator { if let Some(term) = &other.data.terminator {
match term { match term {
TerminatorKind::Ret(_) => {} TerminatorKind::Ret(_) => {}
TerminatorKind::RetVoid => {}
TerminatorKind::Br(other_val) => { TerminatorKind::Br(other_val) => {
if other_val == &block_v { if other_val == &block_v {
return true; return true;
@ -372,6 +373,7 @@ impl TerminatorKind {
use TerminatorKind::*; use TerminatorKind::*;
match self { match self {
Ret(instr_val) => instr_val.get_type(builder), Ret(instr_val) => instr_val.get_type(builder),
RetVoid => Ok(Type::Void),
Br(_) => Ok(Type::Void), Br(_) => Ok(Type::Void),
CondBr(_, _, _) => Ok(Type::Void), CondBr(_, _, _) => Ok(Type::Void),
} }

View File

@ -356,6 +356,7 @@ impl TerminatorKind {
let value = module.values.get(val).unwrap(); let value = module.values.get(val).unwrap();
LLVMBuildRet(module.builder_ref, value.value_ref) LLVMBuildRet(module.builder_ref, value.value_ref)
} }
TerminatorKind::RetVoid => LLVMBuildRetVoid(module.builder_ref),
TerminatorKind::Br(block_value) => { TerminatorKind::Br(block_value) => {
let dest = *module.blocks.get(block_value).unwrap(); let dest = *module.blocks.get(block_value).unwrap();
LLVMBuildBr(module.builder_ref, dest) LLVMBuildBr(module.builder_ref, dest)

View File

@ -145,6 +145,7 @@ impl Debug for TerminatorKind {
write!(f, "Ret ")?; write!(f, "Ret ")?;
val.fmt(f) val.fmt(f)
} }
Self::RetVoid => write!(f, "Void Ret"),
Self::Br(val) => { Self::Br(val) => {
write!(f, "Br ")?; write!(f, "Br ")?;
val.fmt(f) val.fmt(f)

View File

@ -132,12 +132,14 @@ impl<'builder> Block<'builder> {
unsafe { self.builder.terminate(&self.value, instruction) } unsafe { self.builder.terminate(&self.value, instruction) }
} }
pub fn delete_if_unused(&mut self) -> Result<(), ()> { /// Delete block if it is unused. Return true if deleted, false if not.
pub fn delete_if_unused(&mut self) -> Result<bool, ()> {
unsafe { unsafe {
if !self.builder.is_block_used(self.value()) { if !self.builder.is_block_used(self.value()) {
self.builder.delete_block(&self.value) self.builder.delete_block(&self.value)?;
Ok(true)
} else { } else {
Ok(()) Ok(false)
} }
} }
} }
@ -212,6 +214,7 @@ pub enum ConstValue {
#[derive(Clone, Hash)] #[derive(Clone, Hash)]
pub enum TerminatorKind { pub enum TerminatorKind {
Ret(InstructionValue), Ret(InstructionValue),
RetVoid,
Br(BlockValue), Br(BlockValue),
CondBr(InstructionValue, BlockValue, BlockValue), CondBr(InstructionValue, BlockValue, BlockValue),
} }

View File

@ -90,7 +90,11 @@ impl mir::Module {
if let Some(ret) = block.codegen(&mut scope) { if let Some(ret) = block.codegen(&mut scope) {
scope.block.terminate(Term::Ret(ret)).unwrap(); scope.block.terminate(Term::Ret(ret)).unwrap();
} else { } else {
scope.block.delete_if_unused().unwrap(); if !scope.block.delete_if_unused().unwrap() {
// Add a void return just in case if the block
// wasn't unused but didn't have a terminator yet
scope.block.terminate(Term::RetVoid).unwrap();
}
} }
} }
mir::FunctionDefinitionKind::Extern => {} mir::FunctionDefinitionKind::Extern => {}