diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index a52b711..5a2539d 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -83,13 +83,13 @@ pub struct Scope<'ctx, 'a> { functions: &'a HashMap>, stack_values: HashMap, debug: Option>, - debug_const_tys: &'a HashMap, } #[derive(Debug, Clone)] pub struct Debug<'ctx> { info: &'ctx DebugInformation, scope: DebugProgramValue, + types: &'ctx HashMap, } pub struct StackFunction<'ctx> { @@ -129,7 +129,6 @@ impl<'ctx, 'a> Scope<'ctx, 'a> { type_values: self.type_values, stack_values: self.stack_values.clone(), debug: self.debug.clone(), - debug_const_tys: self.debug_const_tys, } } @@ -146,6 +145,18 @@ impl<'ctx, 'a> Scope<'ctx, 'a> { } } +impl<'ctx> Debug<'ctx> { + fn get_type(&self, kind: &TypeKind) -> DebugTypeValue { + if let Some(ty) = self.types.get(kind) { + *ty + } else { + let debug_data = kind.debug_type_data(); + let ty = self.info.debug_type(debug_data); + ty + } + } +} + #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] struct State { should_load: bool, @@ -188,17 +199,24 @@ impl mir::Module { let mut types = HashMap::new(); let mut type_values = HashMap::new(); - let mut debug_const_types = HashMap::new(); + let mut debug_types = HashMap::new(); - debug_const_types.insert( - TypeKind::U32, - debug.debug_type(DebugTypeData::Basic(DebugBasicType { - name: String::from("u32"), - size_bits: 32, - encoding: DwarfEncoding::Unsigned, - flags: DwarfFlags, - })), - ); + { + use TypeKind::*; + debug_types.insert(Bool, Bool.get_debug_type(&debug_types, &debug)); + debug_types.insert(U8, U8.get_debug_type(&debug_types, &debug)); + debug_types.insert(U16, U16.get_debug_type(&debug_types, &debug)); + debug_types.insert(U32, U32.get_debug_type(&debug_types, &debug)); + debug_types.insert(U64, U64.get_debug_type(&debug_types, &debug)); + debug_types.insert(U128, U128.get_debug_type(&debug_types, &debug)); + debug_types.insert(I8, I8.get_debug_type(&debug_types, &debug)); + debug_types.insert(I16, I16.get_debug_type(&debug_types, &debug)); + debug_types.insert(I32, I32.get_debug_type(&debug_types, &debug)); + debug_types.insert(I64, I64.get_debug_type(&debug_types, &debug)); + debug_types.insert(I128, I128.get_debug_type(&debug_types, &debug)); + debug_types.insert(Void, Void.get_debug_type(&debug_types, &debug)); + debug_types.insert(StringPtr, StringPtr.get_debug_type(&debug_types, &debug)); + } for typedef in &self.typedefs { let type_value = match &typedef.kind { @@ -264,7 +282,9 @@ impl mir::Module { let debug_scope = if let Some(location) = mir_function.signature().into_debug(tokens) { // let debug_scope = debug.inner_scope(&outer_scope, location); - let fn_param_ty = debug_const_types.get(&TypeKind::U32).unwrap(); + let fn_param_ty = &mir_function + .return_type + .get_debug_type(&debug_types, &debug); let debug_ty = debug.debug_type(DebugTypeData::Subprogram(DebugSubprogramTypeData { @@ -318,7 +338,7 @@ impl mir::Module { name: p_name.clone(), arg_idx: i as u32, location, - ty: *debug_const_types.get(&TypeKind::U32).unwrap(), + ty: p_ty.get_debug_type(&debug_types, &debug), always_preserve: true, flags: DwarfFlags, }), @@ -340,9 +360,9 @@ impl mir::Module { Some(Debug { info: &debug, scope, + types: &debug_types, }) }), - debug_const_tys: &debug_const_types, }; match &mir_function.kind { @@ -452,7 +472,7 @@ impl mir::Statement { DebugMetadata::LocalVar(DebugLocalVariable { name: name.clone(), location, - ty: scope.debug_const_tys.get(&TypeKind::U32).unwrap().clone(), + ty: debug.get_type(ty), always_preserve: true, alignment: 32, flags: DwarfFlags, @@ -875,6 +895,20 @@ impl TypeKind { } } } + + fn get_debug_type( + &self, + types: &HashMap, + debug: &DebugInformation, + ) -> DebugTypeValue { + if let Some(ty) = types.get(self) { + *ty + } else { + let debug_data = self.debug_type_data(); + let ty = debug.debug_type(debug_data); + ty + } + } } impl Metadata { diff --git a/reid/src/mir/impl.rs b/reid/src/mir/impl.rs index b71aeca..7c021af 100644 --- a/reid/src/mir/impl.rs +++ b/reid/src/mir/impl.rs @@ -1,4 +1,6 @@ -use reid_lib::debug_information::DebugLocation; +use reid_lib::debug_information::{ + DebugBasicType, DebugLocation, DebugTypeData, DwarfEncoding, DwarfFlags, +}; use super::{typecheck::ErrorKind, typerefs::TypeRefs, VagueType as Vague, *}; @@ -27,6 +29,53 @@ impl TypeKind { BinaryOperator::Cmp(_) => TypeKind::Bool, } } + + pub fn size_of(&self) -> u64 { + match self { + TypeKind::Bool => 1, + TypeKind::I8 => 8, + TypeKind::U8 => 8, + TypeKind::I16 => 16, + TypeKind::U16 => 16, + TypeKind::I32 => 32, + TypeKind::U32 => 32, + TypeKind::I64 => 64, + TypeKind::U64 => 64, + TypeKind::I128 => 128, + TypeKind::U128 => 128, + TypeKind::Void => 0, + TypeKind::StringPtr => 32, + TypeKind::Array(type_kind, len) => 32, + TypeKind::Borrow(_) => todo!(), + TypeKind::CustomType(_) => 32, + TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), + } + } + + pub fn debug_type_data(&self) -> DebugTypeData { + DebugTypeData::Basic(DebugBasicType { + name: format!("{}", self), + size_bits: self.size_of(), + encoding: match self { + TypeKind::Bool => DwarfEncoding::Boolean, + TypeKind::I8 => DwarfEncoding::SignedChar, + TypeKind::U8 => DwarfEncoding::UnsignedChar, + TypeKind::I16 | TypeKind::I32 | TypeKind::I64 | TypeKind::I128 => { + DwarfEncoding::Signed + } + TypeKind::U16 | TypeKind::U32 | TypeKind::U64 | TypeKind::U128 => { + DwarfEncoding::Unsigned + } + TypeKind::Void => DwarfEncoding::Address, + TypeKind::StringPtr => DwarfEncoding::Address, + TypeKind::Array(_, _) => DwarfEncoding::Address, + TypeKind::Borrow(_) => DwarfEncoding::Address, + TypeKind::CustomType(_) => DwarfEncoding::Address, + TypeKind::Vague(_) => panic!("tried fetching debug-type for vague type!"), + }, + flags: DwarfFlags, + }) + } } impl StructType {