diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index f2d9327..808c9da 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -502,9 +502,23 @@ impl DebugTypeHolder { } DebugTypeData::Struct(st) => { let mut elements = st - .elements + .fields .iter() - .map(|e| *debug.types.get(e).unwrap()) + .map(|field| { + LLVMDIBuilderCreateMemberType( + debug.builder, + *debug.programs.get(&st.scope).unwrap(), + into_cstring(field.name.clone()).as_ptr(), + field.name.len(), + debug.file_ref, + field.location.line, + field.size_bits, + 0, + 1, + field.flags.as_llvm(), + *debug.types.get(&field.ty).unwrap(), + ) + }) .collect::>(); LLVMDIBuilderCreateStructType( debug.builder, diff --git a/reid-llvm-lib/src/debug.rs b/reid-llvm-lib/src/debug.rs index d2d28d7..0066dd1 100644 --- a/reid-llvm-lib/src/debug.rs +++ b/reid-llvm-lib/src/debug.rs @@ -9,9 +9,10 @@ use crate::{ CmpPredicate, Instr, InstructionData, TerminatorKind, builder::*, debug_information::{ - DebugArrayType, DebugBasicType, DebugLocation, DebugLocationValue, DebugMetadataHolder, - DebugMetadataValue, DebugPointerType, DebugProgramValue, DebugScopeValue, DebugStructType, - DebugSubprogramType, DebugTypeData, DebugTypeHolder, DebugTypeValue, + DebugArrayType, DebugBasicType, DebugFieldType, DebugLocation, DebugLocationValue, + DebugMetadataHolder, DebugMetadataValue, DebugPointerType, DebugProgramValue, + DebugScopeValue, DebugStructType, DebugSubprogramType, DebugTypeData, DebugTypeHolder, + DebugTypeValue, }, }; @@ -267,7 +268,19 @@ impl Debug for DebugStructType { .field("location", &self.location) .field("size_bit", &self.size_bits) .field("flags", &self.flags) - .field("elements", &self.elements) + .field("elements", &self.fields) + .finish() + } +} + +impl Debug for DebugFieldType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct(&format!("Field({})", self.name)) + .field("location", &self.location) + .field("size_bits", &self.size_bits) + .field("offset", &self.offset) + .field("flags", &self.flags) + .field("ty", &self.ty) .finish() } } diff --git a/reid-llvm-lib/src/debug_information.rs b/reid-llvm-lib/src/debug_information.rs index 137e429..8a769c2 100644 --- a/reid-llvm-lib/src/debug_information.rs +++ b/reid-llvm-lib/src/debug_information.rs @@ -295,7 +295,17 @@ pub struct DebugStructType { pub location: DebugLocation, pub size_bits: u64, pub flags: DwarfFlags, - pub elements: Vec, + pub fields: Vec, +} + +#[derive(Clone)] +pub struct DebugFieldType { + pub name: String, + pub location: DebugLocation, + pub size_bits: u64, + pub offset: u64, + pub flags: DwarfFlags, + pub ty: DebugTypeValue, } #[derive(Clone)] diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 211d4c4..e46f3f8 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -4,11 +4,11 @@ use reid_lib::{ builder::{InstructionValue, TypeValue}, compile::CompiledModule, debug_information::{ - DebugArrayType, DebugBasicType, DebugFileData, DebugInformation, DebugLocalVariable, - DebugLocation, DebugMetadata, DebugMetadataValue, DebugParamVariable, DebugPointerType, - DebugProgramValue, DebugRecordKind, DebugScopeValue, DebugStructType, DebugSubprogramData, - DebugSubprogramOptionals, DebugSubprogramType, DebugTypeData, DebugTypeValue, - DwarfEncoding, DwarfFlags, InstructionDebugRecordData, + DebugArrayType, DebugBasicType, DebugFieldType, DebugFileData, DebugInformation, + DebugLocalVariable, DebugLocation, DebugMetadata, DebugMetadataValue, DebugParamVariable, + DebugPointerType, DebugProgramValue, DebugRecordKind, DebugScopeValue, DebugStructType, + DebugSubprogramData, DebugSubprogramOptionals, DebugSubprogramType, DebugTypeData, + DebugTypeValue, DwarfEncoding, DwarfFlags, InstructionDebugRecordData, }, Block, CmpPredicate, ConstValue, Context, CustomTypeKind, Function, FunctionFlags, Instr, Module, NamedStruct, TerminatorKind as Term, Type, @@ -1044,24 +1044,26 @@ impl TypeKind { match &typedef.kind { TypeDefinitionKind::Struct(struct_type) => { - let (elements, sizes): (Vec<_>, Vec<_>) = struct_type - .0 - .iter() - .map(|t| { - ( - t.1.clone().get_debug_type_hard( - scope, - debug_info, - debug_types, - type_values, - types, - tokens, - ), - t.1.size_of(), - ) - }) - .unzip(); - let size_bits: u64 = sizes.iter().sum(); + let mut fields = Vec::new(); + let mut size_bits = 0; + for field in &struct_type.0 { + fields.push(DebugFieldType { + name: field.0.clone(), + location: field.2.into_debug(tokens).unwrap(), + size_bits: field.1.size_of(), + offset: size_bits, + flags: DwarfFlags, + ty: field.1.get_debug_type_hard( + scope, + debug_info, + debug_types, + type_values, + types, + tokens, + ), + }); + size_bits += field.1.size_of(); + } { DebugTypeData::Struct(DebugStructType { name: name.clone(), @@ -1069,7 +1071,7 @@ impl TypeKind { location: typedef.meta.into_debug(tokens).unwrap(), size_bits, flags: DwarfFlags, - elements, + fields, }) } } diff --git a/reid_src/array_structs.reid b/reid_src/array_structs.reid index 628c393..e5efdb8 100644 --- a/reid_src/array_structs.reid +++ b/reid_src/array_structs.reid @@ -1,8 +1,7 @@ // Arithmetic, function calls and imports! struct Test { - field: i32, - second: [u32; 4] + field : i32, second : [u32; 4] } fn test() -> [Test; 1] { @@ -14,15 +13,15 @@ fn test() -> [Test; 1] { } fn main() -> u32 { - let mut value = test(); + let mut value = test(); - let val1 = 0; - let val2 = 1; + let val1 = 0; + let val2 = 1; - // value[val1].second[val2 + 1] = 99; + // value[val1].second[val2 + 1] = 99; - let mut b = value[val1]; - b.second[2] = 99; + let mut b = value[val1]; + b.second[2] = 99; - return value[val1].second[2]; + return value[val1].second[2]; }