From 5f65d3438a8a70486546f5c2394288b5d4fd3da6 Mon Sep 17 00:00:00 2001 From: sofia Date: Sat, 19 Jul 2025 22:08:23 +0300 Subject: [PATCH] Get arrays to work in DI, huzzah --- bps | 1 + reid-llvm-lib/src/compile.rs | 23 +++++--- reid-llvm-lib/src/debug.rs | 3 +- reid-llvm-lib/src/debug_information.rs | 2 +- reid/src/codegen.rs | 78 +++++++++++++++----------- reid/src/mir/impl.rs | 2 + reid/src/mir/mod.rs | 2 + reid_src/array.reid | 13 +---- 8 files changed, 68 insertions(+), 56 deletions(-) create mode 100644 bps diff --git a/bps b/bps new file mode 100644 index 0000000..25a2a43 --- /dev/null +++ b/bps @@ -0,0 +1 @@ +[{"Breakpoint":{"BKPTOptions":{"AutoContinue":false,"ConditionText":"","EnabledState":true,"IgnoreCount":0,"OneShotState":false},"BKPTResolver":{"Options":{"Column":0,"Exact":false,"FileName":"array.reid","Inlines":true,"LineNumber":6,"Offset":0,"SkipPrologue":true},"Type":"FileAndLine"},"Hardware":false,"SearchFilter":{"Options":{},"Type":"Unconstrained"}}},{"Breakpoint":{"BKPTOptions":{"AutoContinue":false,"ConditionText":"","EnabledState":true,"IgnoreCount":0,"OneShotState":true},"BKPTResolver":{"Options":{"Column":0,"Exact":false,"FileName":"/home/furret/dev/reid-llvm/reid_src/array.reid","Inlines":true,"LineNumber":4,"Offset":0,"SkipPrologue":true},"Type":"FileAndLine"},"Hardware":false,"SearchFilter":{"Options":{},"Type":"Unconstrained"}}}] diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index 6ad9988..7aabc78 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -487,14 +487,19 @@ impl DebugTypeHolder { into_cstring(ptr.name.clone()).as_ptr(), ptr.name.len(), ), - DebugTypeData::Array(array) => LLVMDIBuilderCreateArrayType( - debug.builder, - array.size_bits, - array.align_bits, - *debug.types.get(&array.element_type).unwrap(), - Vec::new().as_mut_ptr(), - 0, - ), + DebugTypeData::Array(array) => { + let subrange = + LLVMDIBuilderGetOrCreateSubrange(debug.builder, 0, array.length as i64); + let mut elements = vec![subrange]; + LLVMDIBuilderCreateArrayType( + debug.builder, + array.size_bits, + 0, + *debug.types.get(&array.element_type).unwrap(), + elements.as_mut_ptr(), + elements.len() as u32, + ) + } DebugTypeData::Struct(st) => { let mut elements = st .elements @@ -509,7 +514,7 @@ impl DebugTypeHolder { debug.file_ref, st.location.line, st.size_bits, - st.alignment, + 0, st.flags.as_llvm(), null_mut(), // derived from elements.as_mut_ptr(), diff --git a/reid-llvm-lib/src/debug.rs b/reid-llvm-lib/src/debug.rs index 65134a7..86fd9bd 100644 --- a/reid-llvm-lib/src/debug.rs +++ b/reid-llvm-lib/src/debug.rs @@ -292,10 +292,9 @@ impl Debug for DebugPointerType { impl Debug for DebugArrayType { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct(&format!("Array<{:?}>", self.element_type)) + f.debug_struct(&format!("Array<{:?}>[{}]", self.element_type, self.length)) .field("size_bits", &self.size_bits) .field("align_bits", &self.align_bits) - .field("subscripts", &self.subscripts) .finish() } } diff --git a/reid-llvm-lib/src/debug_information.rs b/reid-llvm-lib/src/debug_information.rs index a3042e8..f286da2 100644 --- a/reid-llvm-lib/src/debug_information.rs +++ b/reid-llvm-lib/src/debug_information.rs @@ -279,7 +279,7 @@ pub struct DebugArrayType { pub size_bits: u64, pub align_bits: u32, pub element_type: DebugTypeValue, - pub subscripts: Vec, + pub length: u64, } #[derive(Clone)] diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index f7b8fb0..0eecdbd 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -494,33 +494,27 @@ impl mir::Statement { .stack_values .insert(name.clone(), StackValue(stack_value, ty.clone())); if let Some(debug) = &scope.debug { - match stack_value { - StackValueKind::Immutable(_) => {} - StackValueKind::Mutable(_) => { - let location = self.1.into_debug(scope.tokens).unwrap(); - let var = debug.info.metadata( - &debug.scope, - DebugMetadata::LocalVar(DebugLocalVariable { - name: name.clone(), - location, - ty: ty.get_debug_type(debug, scope), - always_preserve: true, - alignment: 32, - flags: DwarfFlags, - }), - ); - store.add_record( - &mut scope.block, - InstructionDebugRecordData { - variable: var, - location, - kind: DebugRecordKind::Declare(value.instr()), - scope: debug.scope, - }, - ); - } - StackValueKind::Literal(_) => {} - } + let location = self.1.into_debug(scope.tokens).unwrap(); + let var = debug.info.metadata( + &debug.scope, + DebugMetadata::LocalVar(DebugLocalVariable { + name: name.clone(), + location, + ty: ty.get_debug_type(debug, scope), + always_preserve: true, + alignment: 32, + flags: DwarfFlags, + }), + ); + store.add_record( + &mut scope.block, + InstructionDebugRecordData { + variable: var, + location, + kind: DebugRecordKind::Declare(value.instr()), + scope: debug.scope, + }, + ); } None } @@ -971,6 +965,9 @@ impl TypeKind { TypeDefinitionKind::Struct(_) => Type::Ptr(Box::new(custom_t)), } } + TypeKind::Ptr(type_kind) => { + Type::Ptr(Box::new(type_kind.get_type(type_vals, typedefs))) + } } } } @@ -1015,10 +1012,9 @@ impl TypeKind { ), size_bits: self.size_of(), }), - TypeKind::Array(type_kind, len) => DebugTypeData::Array(DebugArrayType { - size_bits: type_kind.size_of() * len, - align_bits: type_kind.alignment(), - element_type: type_kind.get_debug_type_hard( + TypeKind::Ptr(inner) => DebugTypeData::Pointer(DebugPointerType { + name, + pointee: inner.get_debug_type_hard( scope, debug_info, debug_types, @@ -1026,8 +1022,24 @@ impl TypeKind { types, tokens, ), - subscripts: Vec::new(), + size_bits: self.size_of(), }), + TypeKind::Array(type_kind, len) => { + let elem_ty = type_kind.get_debug_type_hard( + scope, + debug_info, + debug_types, + type_values, + types, + tokens, + ); + DebugTypeData::Array(DebugArrayType { + size_bits: type_kind.size_of() * len, + align_bits: type_kind.alignment(), + element_type: elem_ty, + length: *len, + }) + } TypeKind::CustomType(name) => { let typedef = types.get(type_values.get(name).unwrap()).unwrap(); @@ -1082,7 +1094,7 @@ impl TypeKind { TypeKind::StringPtr => DwarfEncoding::Address, TypeKind::Array(_, _) => DwarfEncoding::Address, TypeKind::CustomType(_) => DwarfEncoding::Address, - TypeKind::Vague(_) => panic!("tried fetching debug-type for vague type!"), + _ => panic!("tried fetching debug-type for non-supported type!"), }, flags: DwarfFlags, }), diff --git a/reid/src/mir/impl.rs b/reid/src/mir/impl.rs index 34d32e7..6a4ef82 100644 --- a/reid/src/mir/impl.rs +++ b/reid/src/mir/impl.rs @@ -47,6 +47,7 @@ impl TypeKind { TypeKind::StringPtr => 32, TypeKind::Array(type_kind, len) => type_kind.size_of() * len, TypeKind::CustomType(_) => 32, + TypeKind::Ptr(inner) => 32, TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), } } @@ -68,6 +69,7 @@ impl TypeKind { TypeKind::StringPtr => 32, TypeKind::Array(type_kind, _) => type_kind.alignment(), TypeKind::CustomType(_) => 32, + TypeKind::Ptr(type_kind) => 32, TypeKind::Vague(_) => panic!("Tried to sizeof a vague type!"), } } diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 11a885f..b69b6b3 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -109,6 +109,8 @@ pub enum TypeKind { Array(Box, u64), #[error("{0}")] CustomType(String), + #[error("Ptr({0})")] + Ptr(Box), #[error(transparent)] Vague(#[from] VagueType), } diff --git a/reid_src/array.reid b/reid_src/array.reid index 0e494bc..e1badac 100644 --- a/reid_src/array.reid +++ b/reid_src/array.reid @@ -1,16 +1,7 @@ // Arithmetic, function calls and imports! -fn array() -> [[[u16; 4];1];1] { - return [[[10, 15, 7, 9]]]; -} - fn main() -> u16 { - let mut list = array(); + let a = [ 5, 3, 2 ]; - let a = [[[5, 3, 2]]]; - - list[0][0][2] = 19; - // let a = 1; - - return a[0][0][1]; + return a[1]; }