Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ node_modules/
*.iml
book/

**/src/bindings.rs
*.lit_test_times.txt*
1 change: 1 addition & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ args = [
"${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/tests/lit/parse",
"${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/tests/lit/wasm-translation",
"${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/tests/lit/source-location",
"${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/tests/lit/debugdump",
]
dependencies = ["litcheck"]

Expand Down
1 change: 1 addition & 0 deletions codegen/masm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn register_dialect_hooks(context: &midenc_hir::Context) {
info.register_operation_trait::<builtin::Ret, dyn HirLowering>();
info.register_operation_trait::<builtin::RetImm, dyn HirLowering>();
info.register_operation_trait::<builtin::GlobalSymbol, dyn HirLowering>();
info.register_operation_trait::<builtin::DbgValue, dyn HirLowering>();
});
context.register_dialect_hook::<arith::ArithDialect, _>(|info, _context| {
info.register_operation_trait::<arith::Constant, dyn HirLowering>();
Expand Down
87 changes: 87 additions & 0 deletions codegen/masm/src/lower/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,93 @@ impl HirLowering for arith::Split {
}
}

impl HirLowering for builtin::DbgValue {
fn emit(&self, emitter: &mut BlockEmitter<'_>) -> Result<(), Report> {
use miden_core::{DebugVarInfo, DebugVarLocation, Felt};
use midenc_hir::DIExpressionOp;

// Get the variable info
let var = self.variable();

// Build the DebugVarLocation from DIExpression
let expr = self.expression();
let value = self.value().as_value_ref();

// If the value is not on the stack and there's no expression info,
// skip emitting this debug info (the value has been optimized away)
let has_location_expr = expr.operations.first().is_some_and(|op| {
matches!(
op,
DIExpressionOp::WasmStack(_)
| DIExpressionOp::WasmLocal(_)
| DIExpressionOp::ConstU64(_)
| DIExpressionOp::ConstS64(_)
)
});
if !has_location_expr && emitter.stack.find(&value).is_none() {
// Value has been dropped and we have no other location info, skip
return Ok(());
}
let value_location = if let Some(first_op) = expr.operations.first() {
match first_op {
DIExpressionOp::WasmStack(offset) => DebugVarLocation::Stack(*offset as u8),
DIExpressionOp::WasmLocal(idx) => DebugVarLocation::Local(*idx as u16),
DIExpressionOp::WasmGlobal(_) | DIExpressionOp::Deref => {
// For global or dereference, check the stack position of the value
if let Some(pos) = emitter.stack.find(&value) {
DebugVarLocation::Stack(pos as u8)
} else {
DebugVarLocation::Expression(vec![])
}
}
DIExpressionOp::ConstU64(val) => DebugVarLocation::Const(Felt::new(*val)),
DIExpressionOp::ConstS64(val) => DebugVarLocation::Const(Felt::new(*val as u64)),
_ => {
// For other operations, try to find the value on the stack
if let Some(pos) = emitter.stack.find(&value) {
DebugVarLocation::Stack(pos as u8)
} else {
DebugVarLocation::Expression(vec![])
}
}
}
} else {
// No expression, try to find the value on the stack
if let Some(pos) = emitter.stack.find(&value) {
DebugVarLocation::Stack(pos as u8)
} else {
// Value not found, use expression
DebugVarLocation::Expression(vec![])
}
};

let mut debug_var = DebugVarInfo::new(var.name.to_string(), value_location);

// Set arg_index if this is a parameter
if let Some(arg_index) = var.arg_index {
debug_var.set_arg_index(arg_index + 1); // Convert to 1-based
}

// Set source location
if let Some(line) = core::num::NonZeroU32::new(var.line) {
use miden_assembly::debuginfo::{ColumnNumber, FileLineCol, LineNumber, Uri};
let uri = Uri::new(var.file.as_str());
let file_line_col = FileLineCol::new(
uri,
LineNumber::new(line.get()).unwrap_or_default(),
var.column.and_then(ColumnNumber::new).unwrap_or_default(),
);
debug_var.set_location(file_line_col);
}

// Emit the instruction
let inst = masm::Instruction::DebugVar(debug_var);
emitter.emit_op(masm::Op::Inst(Span::new(self.span(), inst)));

Ok(())
}
}

impl HirLowering for builtin::GlobalSymbol {
fn emit(&self, emitter: &mut BlockEmitter<'_>) -> Result<(), Report> {
let context = self.as_operation().context();
Expand Down
Loading
Loading