Get structs to work in debug information

This commit is contained in:
Sofia 2025-07-20 13:52:54 +03:00
parent c0b02f8a45
commit f952651a66
5 changed files with 78 additions and 40 deletions

View File

@ -502,9 +502,23 @@ impl DebugTypeHolder {
} }
DebugTypeData::Struct(st) => { DebugTypeData::Struct(st) => {
let mut elements = st let mut elements = st
.elements .fields
.iter() .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::<Vec<_>>(); .collect::<Vec<_>>();
LLVMDIBuilderCreateStructType( LLVMDIBuilderCreateStructType(
debug.builder, debug.builder,

View File

@ -9,9 +9,10 @@ use crate::{
CmpPredicate, Instr, InstructionData, TerminatorKind, CmpPredicate, Instr, InstructionData, TerminatorKind,
builder::*, builder::*,
debug_information::{ debug_information::{
DebugArrayType, DebugBasicType, DebugLocation, DebugLocationValue, DebugMetadataHolder, DebugArrayType, DebugBasicType, DebugFieldType, DebugLocation, DebugLocationValue,
DebugMetadataValue, DebugPointerType, DebugProgramValue, DebugScopeValue, DebugStructType, DebugMetadataHolder, DebugMetadataValue, DebugPointerType, DebugProgramValue,
DebugSubprogramType, DebugTypeData, DebugTypeHolder, DebugTypeValue, DebugScopeValue, DebugStructType, DebugSubprogramType, DebugTypeData, DebugTypeHolder,
DebugTypeValue,
}, },
}; };
@ -267,7 +268,19 @@ impl Debug for DebugStructType {
.field("location", &self.location) .field("location", &self.location)
.field("size_bit", &self.size_bits) .field("size_bit", &self.size_bits)
.field("flags", &self.flags) .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() .finish()
} }
} }

View File

@ -295,7 +295,17 @@ pub struct DebugStructType {
pub location: DebugLocation, pub location: DebugLocation,
pub size_bits: u64, pub size_bits: u64,
pub flags: DwarfFlags, pub flags: DwarfFlags,
pub elements: Vec<DebugTypeValue>, pub fields: Vec<DebugFieldType>,
}
#[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)] #[derive(Clone)]

View File

@ -4,11 +4,11 @@ use reid_lib::{
builder::{InstructionValue, TypeValue}, builder::{InstructionValue, TypeValue},
compile::CompiledModule, compile::CompiledModule,
debug_information::{ debug_information::{
DebugArrayType, DebugBasicType, DebugFileData, DebugInformation, DebugLocalVariable, DebugArrayType, DebugBasicType, DebugFieldType, DebugFileData, DebugInformation,
DebugLocation, DebugMetadata, DebugMetadataValue, DebugParamVariable, DebugPointerType, DebugLocalVariable, DebugLocation, DebugMetadata, DebugMetadataValue, DebugParamVariable,
DebugProgramValue, DebugRecordKind, DebugScopeValue, DebugStructType, DebugSubprogramData, DebugPointerType, DebugProgramValue, DebugRecordKind, DebugScopeValue, DebugStructType,
DebugSubprogramOptionals, DebugSubprogramType, DebugTypeData, DebugTypeValue, DebugSubprogramData, DebugSubprogramOptionals, DebugSubprogramType, DebugTypeData,
DwarfEncoding, DwarfFlags, InstructionDebugRecordData, DebugTypeValue, DwarfEncoding, DwarfFlags, InstructionDebugRecordData,
}, },
Block, CmpPredicate, ConstValue, Context, CustomTypeKind, Function, FunctionFlags, Instr, Block, CmpPredicate, ConstValue, Context, CustomTypeKind, Function, FunctionFlags, Instr,
Module, NamedStruct, TerminatorKind as Term, Type, Module, NamedStruct, TerminatorKind as Term, Type,
@ -1044,12 +1044,16 @@ impl TypeKind {
match &typedef.kind { match &typedef.kind {
TypeDefinitionKind::Struct(struct_type) => { TypeDefinitionKind::Struct(struct_type) => {
let (elements, sizes): (Vec<_>, Vec<_>) = struct_type let mut fields = Vec::new();
.0 let mut size_bits = 0;
.iter() for field in &struct_type.0 {
.map(|t| { fields.push(DebugFieldType {
( name: field.0.clone(),
t.1.clone().get_debug_type_hard( 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, scope,
debug_info, debug_info,
debug_types, debug_types,
@ -1057,11 +1061,9 @@ impl TypeKind {
types, types,
tokens, tokens,
), ),
t.1.size_of(), });
) size_bits += field.1.size_of();
}) }
.unzip();
let size_bits: u64 = sizes.iter().sum();
{ {
DebugTypeData::Struct(DebugStructType { DebugTypeData::Struct(DebugStructType {
name: name.clone(), name: name.clone(),
@ -1069,7 +1071,7 @@ impl TypeKind {
location: typedef.meta.into_debug(tokens).unwrap(), location: typedef.meta.into_debug(tokens).unwrap(),
size_bits, size_bits,
flags: DwarfFlags, flags: DwarfFlags,
elements, fields,
}) })
} }
} }

View File

@ -1,8 +1,7 @@
// Arithmetic, function calls and imports! // Arithmetic, function calls and imports!
struct Test { struct Test {
field: i32, field : i32, second : [u32; 4]
second: [u32; 4]
} }
fn test() -> [Test; 1] { fn test() -> [Test; 1] {