miden-objtool 0.10.2

Common utilities for analyzing Miden artifacts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
//! A command to dump debug information from MASP packages
//!
//! Similar to llvm-dwarfdump, this tool parses the `.debug_info` section from compiled MASP
//! packages and displays the debug metadata in a human-readable format.
use std::{collections::BTreeMap, path::PathBuf, sync::Arc};

use clap::Args;
use miden_assembly_syntax::ast::DebugVarLocation;
use miden_core::serde::Deserializable;
use miden_mast_package::{
    MastForest, Package,
    debug_info::{
        DebugFileInfo, DebugFunctionInfo, DebugPrimitiveType, DebugSourceNodeId, DebugSourceVar,
        DebugTypeInfo, PackageDebugInfo,
    },
};
use midenc_hir::dialects::debuginfo::attributes::{Expression, ExpressionOp, ResolvedFrameBase};

use super::{DumpError, Section};

/// Dump debug information encoded in a .masp file
#[derive(Debug, Args)]
pub struct Config {
    /// The input package to dump info from
    #[arg(required = true)]
    input: PathBuf,

    /// Filter output to a specific section
    #[arg(short, long, value_enum)]
    section: Option<Section>,

    /// Show all available information
    #[arg(short, long)]
    verbose: bool,

    /// Show raw indices instead of resolved names
    #[arg(long)]
    raw: bool,

    /// Only show summary statistics
    #[arg(long)]
    summary: bool,
}

pub fn dump(config: &Config) -> Result<(), DumpError> {
    // Read the MASP file
    let bytes = std::fs::read(&config.input)?;

    // Parse the package
    let package: Package =
        Package::read_from_bytes_unchecked(&bytes).map_err(|e| DumpError::Parse(e.to_string()))?;

    // Get the MAST forest for location decorators
    let Some(debug_info) = package.debug_info()? else {
        return Err(DumpError::NoDebugInfo);
    };

    // Print header
    println!("{}", "=".repeat(80));
    println!("Package Info:");
    println!("  | Name:               {}", package.name);
    println!("  | Version:            {}", package.version);
    println!("  | Kind:               {}", package.kind);
    println!("  | Debug Info Version: {}", debug_info.version());
    println!("{}", "=".repeat(80));
    println!();

    if config.summary {
        print_summary(&debug_info);
        return Ok(());
    }

    match config.section {
        Some(Section::Strings) => print_strings(&debug_info),
        Some(Section::Types) => print_types(&debug_info, config.raw),
        Some(Section::Files) => print_files(&debug_info, config.raw),
        Some(Section::Functions) => {
            print_functions(&debug_info, package.mast_forest(), config.raw, config.verbose)
        }
        Some(Section::Variables) => print_variables(&debug_info, package.mast_forest(), config.raw),
        Some(Section::Locations) => print_locations(&debug_info),
        None => {
            // Print everything
            let mast_forest = package.mast_forest();
            print_summary(&debug_info);
            println!();
            print_strings(&debug_info);
            println!();
            print_types(&debug_info, config.raw);
            println!();
            print_files(&debug_info, config.raw);
            println!();
            print_functions(&debug_info, mast_forest, config.raw, config.verbose);
            println!();
            print_locations(&debug_info);
        }
    }

    Ok(())
}

const DEBUG_VAR_KILL_SENTINEL: &[u8] = b"\0miden.debug.kill";

fn format_debug_var_location(location: &DebugVarLocation) -> String {
    if is_debug_var_kill_location(location) {
        "di.debug_kill".to_string()
    } else if let DebugVarLocation::Expression(bytes) = location
        && let Some(formatted) = format_resolved_frame_base(bytes)
    {
        formatted
    } else {
        location.to_string()
    }
}

fn format_resolved_frame_base(bytes: &[u8]) -> Option<String> {
    let expression = Expression::read_from_bytes_with_budget(bytes, bytes.len()).ok()?;
    let [ExpressionOp::ResolvedFrameBase { base, byte_offset }] = expression.operations.as_slice()
    else {
        return None;
    };
    Some(match base {
        ResolvedFrameBase::Local(offset) => {
            format!("frame_base(FMP{offset:+}){byte_offset:+}")
        }
        ResolvedFrameBase::Global(address) => {
            format!("frame_base(global[{address}]){byte_offset:+}")
        }
    })
}

fn is_debug_var_kill_location(location: &DebugVarLocation) -> bool {
    matches!(
        location,
        DebugVarLocation::Expression(expression) if expression == DEBUG_VAR_KILL_SENTINEL
    )
}

fn print_summary(debug_info: &PackageDebugInfo) {
    println!("Summary:");
    println!();

    println!("Strings:");
    println!("  | records: {}", debug_info.strings().len());
    println!();

    println!("Types:");
    println!("  | records: {}", debug_info.types().len());
    println!();

    println!("Functions:");
    println!("  | records:          {}", debug_info.functions().len());
    println!(
        "  | with source info: {}",
        debug_info
            .functions()
            .iter()
            .filter(|f| f.source_node.into_option().is_some())
            .count()
    );
    println!(
        "  | w/o source info:  {}",
        debug_info
            .functions()
            .iter()
            .filter(|f| f.source_node.into_option().is_none())
            .count()
    );
    println!();

    println!("Source Files:");
    println!("  | records: {}", debug_info.files().len());
    println!();

    println!("Locations:");
    println!("  | records: {}", debug_info.locations().len());
    println!();

    let (total_vars, total_inlined) =
        debug_info
            .nodes()
            .iter()
            .fold((0usize, 0usize), |(acc_vars, acc_inlined), node| {
                (acc_vars + node.debug_vars.len(), acc_inlined + node.inline_calls.len())
            });

    println!("Source Nodes:");
    println!("  | records: {}", debug_info.nodes().len());
    println!("  | roots:   {}", debug_info.roots().len());
    println!("  | debug variables (total): {total_vars}");
    println!("  | inline calls (total):    {total_inlined}");
    println!();

    // Count debug vars in MAST
    println!("Found {total_vars} debug variable records");
}

fn print_strings(debug_info: &PackageDebugInfo) {
    println!(".debug_str contents:");
    println!("{:-<80}", "");

    for (idx, s) in debug_info.strings().iter().enumerate() {
        println!("  [{:4}] \"{}\"", idx, s);
    }
}

fn print_types(debug_info: &PackageDebugInfo, raw: bool) {
    println!(".debug_types contents:");
    println!("{:-<80}", "");
    for (idx, ty) in debug_info.types().iter().enumerate() {
        print!("  [{:4}] ", idx);
        print_type(ty, debug_info, raw, 0);
        println!();
    }
}

fn print_type(ty: &DebugTypeInfo, debug_info: &PackageDebugInfo, raw: bool, indent: usize) {
    let pad = "  ".repeat(indent);
    match ty {
        DebugTypeInfo::Primitive(prim) => {
            print!("{}PRIMITIVE: {}", pad, primitive_name(*prim));
            //print!(" (size: {} bytes, {} felts)", prim.size_in_bytes(), prim.size_in_felts());
        }
        DebugTypeInfo::Pointer { pointee_type_idx } => {
            if raw {
                print!("{}POINTER -> type[{pointee_type_idx}]", pad);
            } else {
                print!("{}POINTER -> ", pad);
                if let Some(pointee) = debug_info.get_type(*pointee_type_idx) {
                    print_type_brief(pointee, debug_info);
                } else {
                    print!("<invalid type idx {pointee_type_idx}>");
                }
            }
        }
        DebugTypeInfo::Array {
            element_type_idx,
            count,
        } => {
            if raw {
                print!("{}ARRAY [{element_type_idx}; {:?}]", pad, count);
            } else {
                print!("{}ARRAY [", pad);
                if let Some(elem) = debug_info.get_type(*element_type_idx) {
                    print_type_brief(elem, debug_info);
                } else {
                    print!("<invalid>");
                }
                match count {
                    Some(n) => print!("; {}]", n),
                    None => print!("; ?]"),
                }
            }
        }
        DebugTypeInfo::Struct {
            name_idx,
            size,
            fields,
        } => {
            let name = if raw {
                format!("str[{}]", name_idx).into_boxed_str().into()
            } else {
                debug_info.get_string(*name_idx).unwrap_or_else(|| "<unknown>".into())
            };
            print!("{}STRUCT {} (size: {} bytes)", pad, name, size);
            if !fields.is_empty() {
                println!();
                for field in fields {
                    let field_name = if raw {
                        format!("str[{}]", field.name_idx).into_boxed_str().into()
                    } else {
                        debug_info.get_string(field.name_idx).unwrap_or_else(|| "<unknown>".into())
                    };
                    print!("{}    +{:4}: {} : ", pad, field.offset, field_name);
                    if let Some(fty) = debug_info.get_type(field.type_idx) {
                        print_type_brief(fty, debug_info);
                    } else {
                        print!("<invalid type>");
                    }
                    println!();
                }
            }
        }
        DebugTypeInfo::Enum {
            name_idx,
            size,
            discriminant_type_idx,
            variants,
        } => {
            let name = if raw {
                format!("str[{name_idx}]").into_boxed_str().into()
            } else {
                debug_info.get_string(*name_idx).unwrap_or_else(|| "<unknown>".into())
            };
            print!("{}ENUM {} (discriminant: ", pad, name);
            match debug_info.get_type(*discriminant_type_idx) {
                Some(discrim_ty) => print_type_brief(discrim_ty, debug_info),
                None => print!("unknown"),
            };
            print!(", size: {} bytes)", size);
            if !variants.is_empty() {
                println!();
            }
            for variant in variants.iter() {
                let name = if raw {
                    format!("str[{}]", variant.name_idx).into_boxed_str().into()
                } else {
                    debug_info.get_string(variant.name_idx).unwrap_or_else(|| "<unknown>".into())
                };
                print!("{}    +{:4}: {}", pad, variant.payload_offset.unwrap_or(0), name);
                if let Some(vty) = variant.type_idx {
                    print!(" : ");
                    if let Some(vty) = debug_info.get_type(vty) {
                        print_type_brief(vty, debug_info);
                    } else {
                        print!("<invalid type>");
                    }
                }
                println!(" = {}", variant.discriminant);
            }
        }
        DebugTypeInfo::Function {
            return_type_idx,
            param_type_indices,
        } => {
            print!("{}FUNCTION (", pad);
            for (i, param_idx) in param_type_indices.iter().enumerate() {
                if i > 0 {
                    print!(", ");
                }
                if raw {
                    print!("type[{param_idx}]");
                } else if let Some(pty) = debug_info.get_type(*param_idx) {
                    print_type_brief(pty, debug_info);
                } else {
                    print!("<invalid>");
                }
            }
            print!(") -> ");
            match return_type_idx {
                Some(idx) => {
                    if raw {
                        print!("type[{idx}]");
                    } else if let Some(rty) = debug_info.get_type(*idx) {
                        print_type_brief(rty, debug_info);
                    } else {
                        print!("<invalid>");
                    }
                }
                None => print!("void"),
            }
        }
        DebugTypeInfo::Unknown => {
            print!("{}UNKNOWN", pad);
        }
    }
}

fn print_type_brief(ty: &DebugTypeInfo, debug_info: &PackageDebugInfo) {
    match ty {
        DebugTypeInfo::Primitive(prim) => print!("{}", primitive_name(*prim)),
        DebugTypeInfo::Pointer { pointee_type_idx } => {
            print!("*");
            if let Some(p) = debug_info.get_type(*pointee_type_idx) {
                print_type_brief(p, debug_info);
            }
        }
        DebugTypeInfo::Array {
            element_type_idx,
            count,
        } => {
            print!("[");
            if let Some(e) = debug_info.get_type(*element_type_idx) {
                print_type_brief(e, debug_info);
            }
            match count {
                Some(n) => print!("; {}]", n),
                None => print!("]"),
            }
        }
        DebugTypeInfo::Struct { name_idx, .. } => {
            print!("struct {}", debug_info.get_string(*name_idx).unwrap_or_else(|| "?".into()));
        }
        DebugTypeInfo::Enum { name_idx, .. } => {
            print!("enum {}", debug_info.get_string(*name_idx).unwrap_or_else(|| "?".into()));
        }
        DebugTypeInfo::Function { .. } => print!("fn(...)"),
        DebugTypeInfo::Unknown => print!("?"),
    }
}

fn primitive_name(prim: DebugPrimitiveType) -> &'static str {
    match prim {
        DebugPrimitiveType::Void => "void",
        DebugPrimitiveType::Bool => "bool",
        DebugPrimitiveType::I8 => "i8",
        DebugPrimitiveType::U8 => "u8",
        DebugPrimitiveType::I16 => "i16",
        DebugPrimitiveType::U16 => "u16",
        DebugPrimitiveType::I32 => "i32",
        DebugPrimitiveType::U32 => "u32",
        DebugPrimitiveType::I64 => "i64",
        DebugPrimitiveType::U64 => "u64",
        DebugPrimitiveType::I128 => "i128",
        DebugPrimitiveType::U128 => "u128",
        DebugPrimitiveType::U256 => "u256",
        DebugPrimitiveType::F32 => "f32",
        DebugPrimitiveType::F64 => "f64",
        DebugPrimitiveType::Felt => "felt",
        DebugPrimitiveType::Word => "word",
    }
}

fn print_files(debug_info: &PackageDebugInfo, raw: bool) {
    println!(".debug_files contents:");
    println!("{:-<80}", "");
    for (idx, file) in debug_info.files().iter().enumerate() {
        print_file(idx, file, debug_info, raw);
    }
}

fn print_file(idx: usize, file: &DebugFileInfo, debug_info: &PackageDebugInfo, raw: bool) {
    let path = if raw {
        format!("str[{}]", file.path_idx).into_boxed_str().into()
    } else {
        debug_info.get_string(file.path_idx).unwrap_or_else(|| "<unknown>".into())
    };

    print!("  [{idx:4}] {path}");

    if let Some(checksum) = file.checksum() {
        print!(" [checksum: ");
        for byte in &checksum[..4] {
            print!("{:02x}", byte);
        }
        print!("...]");
    }

    println!();
}

fn print_functions(
    debug_info: &PackageDebugInfo,
    mast_forest: &MastForest,
    raw: bool,
    verbose: bool,
) {
    println!(".debug_functions contents:");
    println!("{:-<80}", "");
    for (idx, func) in debug_info.functions().iter().enumerate() {
        print_function(idx, func, debug_info, mast_forest, raw, verbose);
        println!();
    }
}

fn print_function(
    idx: usize,
    func: &DebugFunctionInfo,
    debug_info: &PackageDebugInfo,
    mast_forest: &MastForest,
    raw: bool,
    verbose: bool,
) {
    let name = if raw {
        format!("str[{}]", func.name_idx).into_boxed_str().into()
    } else {
        debug_info.get_string(func.name_idx).unwrap_or_else(|| "<unknown>".into())
    };

    println!("  [{idx:4}] FUNCTION: {name}");

    // Linkage name
    if let Some(linkage_idx) = func.linkage_name_idx.into_option() {
        let linkage = if raw {
            format!("str[{}]", linkage_idx).into_boxed_str().into()
        } else {
            debug_info.get_string(linkage_idx).unwrap_or_else(|| "<unknown>".into())
        };
        println!("         Linkage name: {linkage}");
    }

    // Location
    let file_path = if raw {
        format!("file[{}]", func.file_idx).into_boxed_str().into()
    } else {
        debug_info
            .get_file(func.file_idx)
            .and_then(|f| debug_info.get_string(f.path_idx))
            .unwrap_or_else(|| "<unknown>".into())
    };
    println!("         Location: {file_path}:{}:{}", func.line, func.column);

    // Type
    if let Some(type_idx) = func.type_idx.into_option() {
        print!("         Type: ");
        if raw {
            println!("type[{type_idx}]");
        } else if let Some(ty) = debug_info.get_type(type_idx) {
            print_type_brief(ty, debug_info);
            println!();
        } else {
            println!("<invalid>");
        }
    }

    // MAST root
    print!("         MAST root: 0x");
    for byte in func.mast_root.as_bytes() {
        print!("{byte:02x}");
    }
    println!();

    // Variables
    let source_node_id = func.source_node.into_option().or_else(|| {
        mast_forest.find_procedure_root(func.mast_root).and_then(|exec_node| {
            debug_info.unique_source_root_for_exec_node(exec_node).ok().flatten()
        })
    });
    if let Some(source_node_id) = source_node_id {
        let source_node = &debug_info[source_node_id];
        if !source_node.debug_vars.is_empty() {
            println!("         Variables ({}):", source_node.debug_vars.len());
            for var in source_node.debug_vars.iter() {
                print_variable(var, debug_info, raw, verbose);
            }
        }
        // Inlined calls
        if !source_node.inline_calls.is_empty() && verbose {
            println!("         Inlined calls ({}):", source_node.inline_calls.len());
            for call in source_node.inline_calls.iter() {
                let callee = if raw {
                    format!("func[{}]", call.callee_idx).into_boxed_str().into()
                } else {
                    debug_info
                        .get_function(call.callee_idx)
                        .and_then(|f| debug_info.get_string(f.name_idx))
                        .unwrap_or_else(|| "<unknown>".into())
                };
                let loc = debug_info.get_location(call.loc_idx);
                let call_file = if raw {
                    format!("loc[{}]", call.loc_idx).into_boxed_str().into()
                } else {
                    loc.clone()
                        .map(|loc| Arc::<str>::from(loc.uri))
                        .unwrap_or_else(|| "<unknown>".into())
                };
                if let Some(loc) = loc {
                    println!(
                        "           - {callee} inlined at {call_file}:{}..{}",
                        loc.start, loc.end
                    );
                } else {
                    println!("           - {callee} inlined at {call_file}:?..?",);
                }
            }
        }
    }
}

fn print_variable(var: &DebugSourceVar, debug_info: &PackageDebugInfo, raw: bool, _verbose: bool) {
    let kind = if let Some(index) = var.arg_idx {
        format!("param #{index}")
    } else {
        "local".to_string()
    };

    print!("           - {} ({}): ", debug_info[var.name_idx].as_ref(), kind);

    if let Some(type_id) = var.type_id {
        if raw {
            print!("type[{type_id}]");
        } else if let Some(ty) = debug_info.get_type(type_id) {
            print_type_brief(ty, debug_info);
        } else {
            print!("<invalid type>");
        }
    } else {
        print!("<invalid type>");
    }

    if let Some(loc) = var.location_idx.and_then(|loc| debug_info.get_location(loc)) {
        print!(" at {}:{}..{}", loc.uri(), loc.start, loc.end);
    }

    // TODO(pauls): Restore once scope info is available again
    /*
    if var.scope_depth > 0 {
        print!(" [scope depth: {}]", var.scope_depth);
    }
    */

    println!();
}

fn print_variables(debug_info: &PackageDebugInfo, mast_forest: &MastForest, raw: bool) {
    println!(".debug_variables contents (all functions):");
    println!("{:-<80}", "");
    for function in debug_info.functions() {
        let Some(source_node_id) = function.source_node.into_option().or_else(|| {
            mast_forest.find_procedure_root(function.mast_root).and_then(|exec_node| {
                debug_info.unique_source_root_for_exec_node(exec_node).ok().flatten()
            })
        }) else {
            continue;
        };
        let vars = debug_info.debug_vars_for_source_node(source_node_id).collect::<Vec<_>>();
        if vars.is_empty() {
            continue;
        }
        let func_name =
            debug_info.get_string(function.name_idx).unwrap_or_else(|| "<unknown>".into());
        println!("  Function: {func_name}");
        for var in vars {
            print_variable(var, debug_info, raw, /*verbose=*/ false);
        }
        println!();
    }
}

/// Prints the .debug_loc section - variable location entries from MAST
///
/// This is analogous to DWARF's .debug_loc section which contains location
/// lists describing where a variable's value can be found at runtime.
fn print_locations(debug_info: &PackageDebugInfo) {
    println!(".debug_loc contents (DebugLoc entries from MAST):");
    println!("{:-<80}", "");

    // Collect all debug vars from the MastForest

    // Group by variable name for a cleaner view
    let mut by_name: BTreeMap<Arc<str>, BTreeMap<DebugSourceNodeId, Vec<&DebugSourceVar>>> =
        BTreeMap::new();
    let mut total_count = 0usize;
    for (node_id, node) in debug_info.nodes().iter().enumerate() {
        if node.debug_vars.is_empty() {
            continue;
        }
        let node_id = DebugSourceNodeId::from(node_id as u32);

        total_count += node.debug_vars.len();
        for var in node.debug_vars.iter() {
            by_name
                .entry(debug_info[var.name_idx].clone())
                .or_default()
                .entry(node_id)
                .or_default()
                .push(var);
        }
    }

    println!("  Total DebugVar entries: {total_count}");
    println!("  Unique variable names: {}", by_name.len());
    println!();

    for (name, entries_by_node) in &by_name {
        println!("  Variable: \"{}\"", name);
        println!(
            "  {} location entries:",
            entries_by_node.values().map(|entries| entries.len()).sum::<usize>()
        );

        for (node_id, info) in entries_by_node
            .iter()
            .flat_map(|(nid, entries)| entries.iter().map(|e| (*nid, e)))
        {
            print!("    [node#{node_id}] ");

            // Print value location
            print!("{}", format_debug_var_location(&info.value_location));

            // Print argument info if present
            if let Some(arg_idx) = info.arg_idx {
                print!(" (param #{})", arg_idx);
            }

            // Print type info if present and we can resolve it
            if let Some(type_id) = info.type_id {
                if let Some(ty) = debug_info.get_type(type_id) {
                    print!(" : ");
                    print_type_brief(ty, debug_info);
                } else {
                    print!(" : type[{type_id}]");
                }
            }

            // Print source location if present
            if let Some(loc) = info.location_idx.and_then(|idx| debug_info.get_location(idx)) {
                print!(" @ {}:{}..{}", loc.uri, loc.start, loc.end);
            }

            println!();
        }
        println!();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn formats_debug_kill_sentinel() {
        let location = DebugVarLocation::Expression(DEBUG_VAR_KILL_SENTINEL.to_vec());

        assert_eq!(format_debug_var_location(&location), "di.debug_kill");
    }

    #[test]
    fn formats_explicit_local_frame_base() {
        use miden_core::serde::Serializable;

        let expression = Expression::with_ops(vec![ExpressionOp::ResolvedFrameBase {
            base: ResolvedFrameBase::Local(-7),
            byte_offset: 28,
        }]);
        let location = DebugVarLocation::Expression(expression.to_bytes());

        assert_eq!(format_debug_var_location(&location), "frame_base(FMP-7)+28");
    }

    #[test]
    fn does_not_decode_high_bit_globals_as_locals() {
        let location = DebugVarLocation::FrameBase {
            global_index: 1 << 31,
            byte_offset: 0,
        };

        assert_eq!(format_debug_var_location(&location), "global[2147483648]+0");
    }
}