Skip to content

Commit a5e7643

Browse files
committed
Improve it a bit
1 parent 69dda4e commit a5e7643

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

src/debuginfo.rs

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::prelude::*;
44

55
use std::marker::PhantomData;
66

7-
use gimli::{Endianity, Format, RunTimeEndian};
87
use gimli::write::{
98
Address, AttributeValue, CompilationUnit, DebugAbbrev, DebugInfo, DebugLine, DebugRanges,
109
DebugRngLists, DebugStr, EndianVec, LineProgram, LineProgramId, LineProgramTable, Range,
11-
RangeList, RangeListTable, Result, SectionId, StringTable, UnitEntryId, UnitId, UnitTable, Writer,
10+
RangeList, RangeListTable, Result, SectionId, StringTable, UnitEntryId, UnitId, UnitTable,
11+
Writer,
1212
};
13-
13+
use gimli::{Endianity, Format, RunTimeEndian};
1414

1515
use faerie::*;
1616

@@ -71,7 +71,7 @@ impl<'a, 'tcx: 'a> DebugContext<'tcx> {
7171
let range_lists = RangeListTable::default();
7272

7373
let global_line_program = line_programs.add(LineProgram::new(
74-
version,
74+
3, // FIXME https://github.com/gimli-rs/gimli/issues/363
7575
address_size,
7676
format,
7777
1,
@@ -383,7 +383,7 @@ impl<'a, 'b, 'tcx: 'b> FunctionDebugContext<'a, 'tcx> {
383383
&mut self,
384384
tcx: TyCtxt,
385385
//module: &mut Module<impl Backend>,
386-
size: u32,
386+
code_size: u32,
387387
context: &Context,
388388
isa: &cranelift::codegen::isa::TargetIsa,
389389
source_info_set: &indexmap::IndexSet<SourceInfo>,
@@ -392,7 +392,7 @@ impl<'a, 'b, 'tcx: 'b> FunctionDebugContext<'a, 'tcx> {
392392
// FIXME: add to appropriate scope intead of root
393393
let entry = unit.get_mut(self.entry_id);
394394
let mut size_array = [0; 8];
395-
target_endian(tcx).write_u64(&mut size_array, size as u64);
395+
target_endian(tcx).write_u64(&mut size_array, code_size as u64);
396396
entry.set(gimli::DW_AT_high_pc, AttributeValue::Data8(size_array));
397397

398398
self.debug_context.unit_range_list.0.push(Range {
@@ -402,7 +402,7 @@ impl<'a, 'b, 'tcx: 'b> FunctionDebugContext<'a, 'tcx> {
402402
},
403403
end: Address::Relative {
404404
symbol: self.symbol,
405-
addend: size as i64,
405+
addend: code_size as i64,
406406
},
407407
});
408408

@@ -420,39 +420,54 @@ impl<'a, 'b, 'tcx: 'b> FunctionDebugContext<'a, 'tcx> {
420420
let func = &context.func;
421421
let mut ebbs = func.layout.ebbs().collect::<Vec<_>>();
422422
ebbs.sort_by_key(|ebb| func.offsets[*ebb]); // Ensure inst offsets always increase
423-
for ebb in ebbs {
424-
for (offset, inst, _size) in func.inst_offsets(ebb, &encinfo) {
425-
fn create_row_for_span(tcx: TyCtxt, line_program: &mut LineProgram, offset: u64, span: Span) {
426-
let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
427-
let file = loc.file.name.to_string();
428-
let file = ::std::path::Path::new(&file);
429-
let dir_id = line_program
430-
.add_directory(file.parent().unwrap().to_str().unwrap().as_bytes());
431-
let file_id = line_program.add_file(
432-
file.file_name().unwrap().to_str().unwrap().as_bytes(),
433-
dir_id,
434-
None,
435-
);
436-
line_program.row().file = file_id;
437-
//tcx.sess
438-
// .warn(&format!("srcloc {} {}:{}:{}", offset, file, loc.line, loc.col.to_usize()));
439-
line_program.row().address_offset = offset;
440-
line_program.row().line = loc.line as u64;
441-
line_program.generate_row();
442-
}
443423

424+
let create_row_for_span = |line_program: &mut LineProgram, span: Span| {
425+
let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
426+
let file = loc.file.name.to_string();
427+
let file = ::std::path::Path::new(&file);
428+
let dir_id =
429+
line_program.add_directory(file.parent().unwrap().to_str().unwrap().as_bytes());
430+
let file_id = line_program.add_file(
431+
file.file_name().unwrap().to_str().unwrap().as_bytes(),
432+
dir_id,
433+
None,
434+
);
435+
436+
/*println!(
437+
"srcloc {:>04X} {}:{}:{}",
438+
line_program.row().address_offset,
439+
file.display(),
440+
loc.line,
441+
loc.col.to_u32()
442+
);*/
443+
444+
line_program.row().file = file_id;
445+
line_program.row().line = loc.line as u64;
446+
line_program.row().column = loc.col.to_u32() as u64 + 1;
447+
line_program.generate_row();
448+
};
449+
450+
let mut end = 0;
451+
for ebb in ebbs {
452+
for (offset, inst, size) in func.inst_offsets(ebb, &encinfo) {
444453
let srcloc = func.srclocs[inst];
454+
line_program.row().address_offset = offset as u64;
445455
if !srcloc.is_default() {
446456
let source_info = *source_info_set.get_index(srcloc.bits() as usize).unwrap();
447-
create_row_for_span(tcx, line_program, offset as u64, source_info.span);
457+
create_row_for_span(line_program, source_info.span);
448458
} else {
449-
create_row_for_span(tcx, line_program, offset as u64, self.mir_span);
459+
create_row_for_span(line_program, self.mir_span);
450460
}
461+
end = offset + size;
451462
}
452463
}
453464

454-
let address_offset = line_program.row().address_offset;
455-
line_program.end_sequence(address_offset);
465+
if code_size != end {
466+
line_program.row().address_offset = end as u64;
467+
create_row_for_span(line_program, self.mir_span);
468+
}
469+
470+
line_program.end_sequence(code_size as u64);
456471
}
457472
}
458473

0 commit comments

Comments
 (0)