Update state.load a bit to make a better default for it

This commit is contained in:
Sofia 2025-07-17 12:27:37 +03:00
parent 8ffb3baa8d
commit c07e488f48

View File

@ -113,7 +113,7 @@ impl<'ctx, 'a> Scope<'ctx, 'a> {
} }
} }
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, Clone, Copy)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
struct State { struct State {
should_load: bool, should_load: bool,
} }
@ -127,6 +127,12 @@ impl State {
} }
} }
impl Default for State {
fn default() -> Self {
Self { should_load: true }
}
}
impl mir::Module { impl mir::Module {
fn codegen<'ctx>(&self, context: &'ctx Context) -> ModuleCodegen<'ctx> { fn codegen<'ctx>(&self, context: &'ctx Context) -> ModuleCodegen<'ctx> {
let mut module = context.module(&self.name, self.is_main); let mut module = context.module(&self.name, self.is_main);
@ -216,8 +222,8 @@ impl mir::Module {
}; };
match &mir_function.kind { match &mir_function.kind {
mir::FunctionDefinitionKind::Local(block, _) => { mir::FunctionDefinitionKind::Local(block, _) => {
let mut state = State::default(); let state = State::default();
if let Some(ret) = block.codegen(&mut scope, &mut state) { if let Some(ret) = block.codegen(&mut scope, &state) {
scope.block.terminate(Term::Ret(ret)).unwrap(); scope.block.terminate(Term::Ret(ret)).unwrap();
} else { } else {
if !scope.block.delete_if_unused().unwrap() { if !scope.block.delete_if_unused().unwrap() {
@ -248,7 +254,7 @@ impl mir::Block {
if let Some((kind, expr)) = &self.return_expression { if let Some((kind, expr)) = &self.return_expression {
match kind { match kind {
mir::ReturnKind::Hard => { mir::ReturnKind::Hard => {
let ret = expr.codegen(&mut scope, &mut state.load(true))?; let ret = expr.codegen(&mut scope, &state)?;
scope.block.terminate(Term::Ret(ret)).unwrap(); scope.block.terminate(Term::Ret(ret)).unwrap();
None None
} }
@ -268,7 +274,7 @@ impl mir::Statement {
) -> Option<InstructionValue> { ) -> Option<InstructionValue> {
match &self.0 { match &self.0 {
mir::StmtKind::Let(NamedVariableRef(ty, name, _), mutable, expression) => { mir::StmtKind::Let(NamedVariableRef(ty, name, _), mutable, expression) => {
let value = expression.codegen(scope, &state.load(true)).unwrap(); let value = expression.codegen(scope, &state).unwrap();
scope.stack_values.insert( scope.stack_values.insert(
name.clone(), name.clone(),
StackValue( StackValue(
@ -305,7 +311,7 @@ impl mir::Statement {
} }
mir::StmtKind::Set(lhs, rhs) => { mir::StmtKind::Set(lhs, rhs) => {
let lhs_value = lhs let lhs_value = lhs
.codegen(scope, &mut state.load(false)) .codegen(scope, &state.load(false))
.expect("non-returning LHS snuck into codegen!"); .expect("non-returning LHS snuck into codegen!");
let rhs_value = rhs.codegen(scope, state)?; let rhs_value = rhs.codegen(scope, state)?;