From 92f12e90eba1bdccbb44e081e038d6f60f89d5bd Mon Sep 17 00:00:00 2001 From: sofia Date: Fri, 18 Jul 2025 19:37:32 +0300 Subject: [PATCH] Make debug information work --- reid-llvm-lib/src/compile.rs | 30 +++++++++++++++++++----------- reid/src/codegen.rs | 14 +++++++++----- reid/src/mir/linker.rs | 8 ++++---- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index 79547a8..c932a8a 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -8,7 +8,7 @@ use std::{ }; use llvm_sys::{ - LLVMIntPredicate, LLVMLinkage, + LLVMIntPredicate, LLVMLinkage, LLVMValueKind, analysis::LLVMVerifyModule, core::*, debuginfo::*, @@ -813,16 +813,24 @@ impl InstructionHolder { }; if let Some(location) = &self.data.location { unsafe { - // LLVMInstructionSetDebugLoc( - // val, - // *module - // .debug - // .as_ref() - // .unwrap() - // .locations - // .get(&location) - // .unwrap(), - // ); + match LLVMGetValueKind(val) { + LLVMValueKind::LLVMInstructionValueKind + | LLVMValueKind::LLVMMemoryDefValueKind + | LLVMValueKind::LLVMMemoryUseValueKind + | LLVMValueKind::LLVMMemoryPhiValueKind => { + LLVMInstructionSetDebugLoc( + val, + *module + .debug + .as_ref() + .unwrap() + .locations + .get(&location) + .unwrap(), + ); + } + _ => {} + } } } LLVMValue { diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 7b0a634..c8e988e 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -364,7 +364,13 @@ impl mir::Block { state: &State, ) -> Option { for stmt in &self.statements { - stmt.codegen(&mut scope, state); + stmt.codegen(&mut scope, state).map(|s| { + if let Some(debug) = &scope.debug { + let location = stmt.1.into_debug(scope.tokens).unwrap(); + let loc_val = debug.info.location(&debug.scope, location); + s.with_location(&mut scope.block, loc_val); + } + }); } if let Some((kind, expr)) = &self.return_expression { @@ -484,10 +490,7 @@ impl mir::Expression { _ => panic!("Found an unknown-mutable variable!"), }) } - mir::ExprKind::Literal(lit) => Some( - lit.as_const(&mut scope.block) - .maybe_location(&mut scope.block, location), - ), + mir::ExprKind::Literal(lit) => Some(lit.as_const(&mut scope.block)), mir::ExprKind::BinOp(binop, lhs_exp, rhs_exp) => { lhs_exp .return_type() @@ -667,6 +670,7 @@ impl mir::Expression { Some(struct_ptr) } } + .map(|i| i.maybe_location(&mut scope.block, location)) } } diff --git a/reid/src/mir/linker.rs b/reid/src/mir/linker.rs index cc81277..f9a6d12 100644 --- a/reid/src/mir/linker.rs +++ b/reid/src/mir/linker.rs @@ -95,10 +95,10 @@ impl<'map> Pass for LinkerPass<'map> { modules.insert(module.name.clone(), Rc::new(RefCell::new((module, tokens)))); } - // modules.insert( - // "std".to_owned(), - // Rc::new(RefCell::new(compile_std(&mut self.module_map))), - // ); + modules.insert( + "std".to_owned(), + Rc::new(RefCell::new(compile_std(&mut self.module_map))), + ); let mut modules_to_process: Vec)>>> = modules.values().cloned().collect();