Store all variables in pointers

This commit is contained in:
Sofia 2025-07-19 13:54:54 +03:00
parent 94fbd51d35
commit 59ce454f91

View File

@ -97,7 +97,7 @@ pub struct StackFunction<'ctx> {
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct StackValue(StackValueKind, Type); pub struct StackValue(StackValueKind, TypeKind);
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StackValueKind { pub enum StackValueKind {
@ -295,12 +295,17 @@ impl mir::Module {
let mut stack_values = HashMap::new(); let mut stack_values = HashMap::new();
for (i, (p_name, p_ty)) in mir_function.parameters.iter().enumerate() { for (i, (p_name, p_ty)) in mir_function.parameters.iter().enumerate() {
// Codegen actual parameters // Codegen actual parameters
let param = entry.build(Instr::Param(i)).unwrap();
let alloca = entry
.build(Instr::Alloca(
p_name.clone(),
p_ty.get_type(&type_values, &types),
))
.unwrap();
entry.build(Instr::Store(alloca, param)).unwrap();
stack_values.insert( stack_values.insert(
p_name.clone(), p_name.clone(),
StackValue( StackValue(StackValueKind::Immutable(alloca), p_ty.clone()),
StackValueKind::Immutable(entry.build(Instr::Param(i)).unwrap()),
p_ty.get_type(&type_values, &types),
),
); );
// Generate debug info // Generate debug info
@ -413,20 +418,7 @@ impl mir::Statement {
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).unwrap(); let value = expression.codegen(scope, &state).unwrap();
let (stack_value, _) = match mutable {
false => (StackValueKind::Immutable(value), value),
true => match ty {
// Struct is already allocated at initialization
TypeKind::Array(_, _) => (StackValueKind::Mutable(value), value),
TypeKind::CustomType(n) => {
match scope.types.get(scope.type_values.get(n).unwrap()).unwrap() {
// Struct also is allocated at initialization
TypeDefinitionKind::Struct(_) => {
(StackValueKind::Mutable(value), value)
}
}
}
_ => {
let alloca = scope let alloca = scope
.block .block
.build(Instr::Alloca( .build(Instr::Alloca(
@ -435,25 +427,27 @@ impl mir::Statement {
)) ))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
let store = scope
scope
.block .block
.build(Instr::Store(alloca, value)) .build(Instr::Store(alloca, value))
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
(StackValueKind::Mutable(alloca), store)
} let stack_value = match mutable {
}, true => StackValueKind::Mutable(alloca),
false => StackValueKind::Immutable(alloca),
}; };
scope.stack_values.insert(
name.clone(), scope
StackValue(stack_value, ty.get_type(scope.type_values, scope.types)), .stack_values
); .insert(name.clone(), StackValue(stack_value, ty.clone()));
if let Some(debug) = &scope.debug { if let Some(debug) = &scope.debug {
match stack_value { match stack_value {
StackValueKind::Immutable(_) => {} StackValueKind::Immutable(_) => {}
StackValueKind::Mutable(_) => { StackValueKind::Mutable(_) => {
let location = self.1.into_debug(scope.tokens).unwrap(); let location = self.1.into_debug(scope.tokens).unwrap();
let var = debug.info.metadata( debug.info.metadata(
&debug.scope, &debug.scope,
DebugMetadata::LocalVar(DebugLocalVariable { DebugMetadata::LocalVar(DebugLocalVariable {
name: name.clone(), name: name.clone(),
@ -524,19 +518,15 @@ impl mir::Expression {
.stack_values .stack_values
.get(&varref.1) .get(&varref.1)
.expect("Variable reference not found?!"); .expect("Variable reference not found?!");
dbg!(varref);
Some(match v.0 { Some(match v.0 {
StackValueKind::Immutable(val) => val.clone(), StackValueKind::Immutable(val) | StackValueKind::Mutable(val) => scope
StackValueKind::Mutable(val) => { .block
if state.should_load { .build(Instr::Load(
match v.1 { val,
// TODO probably wrong ..? v.1.get_type(scope.type_values, scope.types),
Type::Ptr(_) => val, ))
_ => scope.block.build(Instr::Load(val, v.1.clone())).unwrap(), .unwrap(),
}
} else {
val
}
}
_ => panic!("Found an unknown-mutable variable!"), _ => panic!("Found an unknown-mutable variable!"),
}) })
} }
@ -687,8 +677,6 @@ impl mir::Expression {
let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name).unwrap(); let TypeDefinitionKind::Struct(struct_ty) = scope.get_typedef(&name).unwrap();
let idx = struct_ty.find_index(field).unwrap(); let idx = struct_ty.find_index(field).unwrap();
dbg!(&scope.context);
dbg!(&struct_val);
let mut value = scope let mut value = scope
.block .block
.build(Instr::GetStructElemPtr(struct_val, idx as u32)) .build(Instr::GetStructElemPtr(struct_val, idx as u32))