Get borrow to work, somewhat

This commit is contained in:
Sofia 2025-07-21 00:53:30 +03:00
parent c8d6566489
commit 7b11951209
2 changed files with 93 additions and 48 deletions

View File

@ -18,7 +18,7 @@ use crate::{
error_raporting::ModuleMap, error_raporting::ModuleMap,
lexer::{FullToken, Position}, lexer::{FullToken, Position},
mir::{ mir::{
self, Metadata, NamedVariableRef, StructField, StructType, TypeDefinition, self, ExprKind, Metadata, NamedVariableRef, StructField, StructType, TypeDefinition,
TypeDefinitionKind, TypeKind, VagueLiteral, TypeDefinitionKind, TypeKind, VagueLiteral,
}, },
}; };
@ -479,33 +479,37 @@ impl mir::Statement {
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 alloca = scope let stack_value = if let mir::Expression(ExprKind::Borrow(_), _) = expression {
.block value
.build( } else {
name, let alloca = scope
Instr::Alloca(ty.get_type(scope.type_values, scope.types)), .block
) .build(
.unwrap() name,
.maybe_location(&mut scope.block, location); Instr::Alloca(ty.get_type(scope.type_values, scope.types)),
)
.unwrap()
.maybe_location(&mut scope.block, location);
let store = scope scope
.block .block
.build( .build(
format!("{}.store", name), format!("{}.store", name),
Instr::Store(alloca, value.instr()), Instr::Store(alloca, value.instr()),
) )
.unwrap() .unwrap()
.maybe_location(&mut scope.block, location); .maybe_location(&mut scope.block, location);
let stack_value = match mutable { StackValue(
true => StackValueKind::Mutable(alloca), match mutable {
false => StackValueKind::Immutable(alloca), true => StackValueKind::Mutable(alloca),
false => StackValueKind::Immutable(alloca),
},
TypeKind::Ptr(Box::new(value.clone().1)),
)
}; };
scope.stack_values.insert( scope.stack_values.insert(name.clone(), stack_value.clone());
name.clone(),
StackValue(stack_value, TypeKind::Ptr(Box::new(value.clone().1))),
);
if let Some(debug) = &scope.debug { if let Some(debug) = &scope.debug {
let location = self.1.into_debug(scope.tokens).unwrap(); let location = self.1.into_debug(scope.tokens).unwrap();
let var = debug.info.metadata( let var = debug.info.metadata(
@ -518,15 +522,15 @@ impl mir::Statement {
flags: DwarfFlags, flags: DwarfFlags,
}), }),
); );
store.add_record( // value.instr().add_record(
&mut scope.block, // &mut scope.block,
InstructionDebugRecordData { // InstructionDebugRecordData {
variable: var, // variable: var,
location, // location,
kind: DebugRecordKind::Declare(value.instr()), // kind: DebugRecordKind::Declare(value.instr()),
scope: debug.scope, // scope: debug.scope,
}, // },
); // );
} }
None None
} }
@ -934,8 +938,47 @@ impl mir::Expression {
TypeKind::CustomType(name.clone()), TypeKind::CustomType(name.clone()),
)) ))
} }
mir::ExprKind::Borrow(named_variable_ref) => todo!(), mir::ExprKind::Borrow(varref) => {
mir::ExprKind::Deref(named_variable_ref) => todo!(), varref.0.known().expect("variable type unknown");
let v = scope
.stack_values
.get(&varref.1)
.expect("Variable reference not found?!");
Some(v.clone())
}
mir::ExprKind::Deref(varref) => {
varref.0.known().expect("variable type unknown");
let v = scope
.stack_values
.get(&varref.1)
.expect("Variable reference not found?!");
Some({
if state.should_load {
if let TypeKind::Ptr(inner) = &v.1 {
StackValue(
v.0.derive(
scope
.block
.build(
format!("{}.load2", varref.1),
Instr::Load(
v.instr(),
inner.get_type(scope.type_values, scope.types),
),
)
.unwrap(),
),
*inner.clone(),
)
} else {
panic!("Variable was not a pointer?!?")
}
} else {
v.clone()
}
})
}
}; };
if let Some(value) = &value { if let Some(value) = &value {
value.instr().maybe_location(&mut scope.block, location); value.instr().maybe_location(&mut scope.block, location);
@ -1159,18 +1202,20 @@ impl TypeKind {
), ),
size_bits: self.size_of(), size_bits: self.size_of(),
}), }),
TypeKind::Ptr(inner) => DebugTypeData::Pointer(DebugPointerType { TypeKind::Ptr(inner) | TypeKind::Borrow(inner) => {
name, DebugTypeData::Pointer(DebugPointerType {
pointee: inner.get_debug_type_hard( name,
scope, pointee: inner.get_debug_type_hard(
debug_info, scope,
debug_types, debug_info,
type_values, debug_types,
types, type_values,
tokens, types,
), tokens,
size_bits: self.size_of(), ),
}), size_bits: self.size_of(),
})
}
TypeKind::Array(elem_ty, len) => { TypeKind::Array(elem_ty, len) => {
let elem_ty = elem_ty.clone().get_debug_type_hard( let elem_ty = elem_ty.clone().get_debug_type_hard(
scope, scope,

View File

@ -7,5 +7,5 @@ fn main() -> u32 {
let mut borrow = &value; let mut borrow = &value;
*borrow = 17; *borrow = 17;
return *borrow; return value;
} }