Skip to content

Commit cafde8c

Browse files
committed
Split debuginfo/line_info.rs from debuginfo/mod.rs
1 parent 4faafde commit cafde8c

File tree

2 files changed

+153
-134
lines changed

2 files changed

+153
-134
lines changed

src/debuginfo/line_info.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
use crate::prelude::*;
2+
3+
use syntax::source_map::FileName;
4+
5+
use gimli::write::{
6+
Address, AttributeValue, FileId, LineProgram, LineString,
7+
LineStringTable, Range, UnitEntryId,
8+
};
9+
10+
fn line_program_add_file(
11+
line_program: &mut LineProgram,
12+
line_strings: &mut LineStringTable,
13+
file: &FileName,
14+
) -> FileId {
15+
match file {
16+
FileName::Real(path) => {
17+
let dir_name = path.parent().unwrap().to_str().unwrap().as_bytes();
18+
let dir_id = if !dir_name.is_empty() {
19+
let dir_name = LineString::new(dir_name, line_program.encoding(), line_strings);
20+
line_program.add_directory(dir_name)
21+
} else {
22+
line_program.default_directory()
23+
};
24+
let file_name = LineString::new(
25+
path.file_name().unwrap().to_str().unwrap().as_bytes(),
26+
line_program.encoding(),
27+
line_strings,
28+
);
29+
line_program.add_file(file_name, dir_id, None)
30+
}
31+
// FIXME give more appropriate file names
32+
_ => {
33+
let dir_id = line_program.default_directory();
34+
let dummy_file_name = LineString::new(
35+
file.to_string().into_bytes(),
36+
line_program.encoding(),
37+
line_strings,
38+
);
39+
line_program.add_file(dummy_file_name, dir_id, None)
40+
}
41+
}
42+
}
43+
44+
impl<'tcx> DebugContext<'tcx> {
45+
pub(super) fn emit_location(&mut self, entry_id: UnitEntryId, span: Span) {
46+
let loc = self.tcx.sess.source_map().lookup_char_pos(span.lo());
47+
48+
let file_id = line_program_add_file(
49+
&mut self.dwarf.unit.line_program,
50+
&mut self.dwarf.line_strings,
51+
&loc.file.name,
52+
);
53+
54+
let entry = self.dwarf.unit.get_mut(entry_id);
55+
56+
entry.set(
57+
gimli::DW_AT_decl_file,
58+
AttributeValue::FileIndex(Some(file_id)),
59+
);
60+
entry.set(
61+
gimli::DW_AT_decl_line,
62+
AttributeValue::Udata(loc.line as u64),
63+
);
64+
// FIXME: probably omit this
65+
entry.set(
66+
gimli::DW_AT_decl_column,
67+
AttributeValue::Udata(loc.col.to_usize() as u64),
68+
);
69+
}
70+
}
71+
72+
impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
73+
pub(crate) fn create_debug_lines(
74+
&mut self,
75+
context: &Context,
76+
isa: &dyn cranelift::codegen::isa::TargetIsa,
77+
source_info_set: &indexmap::IndexSet<(Span, mir::SourceScope)>,
78+
) {
79+
let tcx = self.debug_context.tcx;
80+
81+
let line_program = &mut self.debug_context.dwarf.unit.line_program;
82+
83+
line_program.begin_sequence(Some(Address::Symbol {
84+
symbol: self.symbol,
85+
addend: 0,
86+
}));
87+
88+
let encinfo = isa.encoding_info();
89+
let func = &context.func;
90+
let mut ebbs = func.layout.ebbs().collect::<Vec<_>>();
91+
ebbs.sort_by_key(|ebb| func.offsets[*ebb]); // Ensure inst offsets always increase
92+
93+
let line_strings = &mut self.debug_context.dwarf.line_strings;
94+
let mut create_row_for_span = |line_program: &mut LineProgram, span: Span| {
95+
let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
96+
let file_id = line_program_add_file(line_program, line_strings, &loc.file.name);
97+
98+
/*println!(
99+
"srcloc {:>04X} {}:{}:{}",
100+
line_program.row().address_offset,
101+
file.display(),
102+
loc.line,
103+
loc.col.to_u32()
104+
);*/
105+
106+
line_program.row().file = file_id;
107+
line_program.row().line = loc.line as u64;
108+
line_program.row().column = loc.col.to_u32() as u64 + 1;
109+
line_program.generate_row();
110+
};
111+
112+
let mut end = 0;
113+
for ebb in ebbs {
114+
for (offset, inst, size) in func.inst_offsets(ebb, &encinfo) {
115+
let srcloc = func.srclocs[inst];
116+
line_program.row().address_offset = offset as u64;
117+
if !srcloc.is_default() {
118+
let source_info = *source_info_set.get_index(srcloc.bits() as usize).unwrap();
119+
create_row_for_span(line_program, source_info.0);
120+
} else {
121+
create_row_for_span(line_program, self.mir.span);
122+
}
123+
end = offset + size;
124+
}
125+
}
126+
127+
line_program.end_sequence(end as u64);
128+
129+
let entry = self.debug_context.dwarf.unit.get_mut(self.entry_id);
130+
entry.set(
131+
gimli::DW_AT_low_pc,
132+
AttributeValue::Address(Address::Symbol { symbol: self.symbol, addend: 0 }),
133+
);
134+
entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(end as u64));
135+
136+
self.debug_context.emit_location(self.entry_id, self.mir.span);
137+
138+
self.debug_context
139+
.unit_range_list
140+
.0
141+
.push(Range::StartLength {
142+
begin: Address::Symbol {
143+
symbol: self.symbol,
144+
addend: 0,
145+
},
146+
length: end as u64,
147+
});
148+
}
149+
}

src/debuginfo/mod.rs

Lines changed: 4 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
mod emit;
2+
mod line_info;
23

34
use crate::prelude::*;
45

5-
use syntax::source_map::FileName;
6-
76
use cranelift::codegen::ir::{StackSlots, ValueLoc};
87
use cranelift::codegen::isa::RegUnit;
98

109
use gimli::write::{
11-
self, Address, AttributeValue, DwarfUnit, Expression, FileId, LineProgram, LineString,
12-
LineStringTable, Location, LocationList, Range, RangeList, UnitEntryId, Writer,
10+
self, Address, AttributeValue, DwarfUnit, Expression, LineProgram, LineString,
11+
Location, LocationList, RangeList, UnitEntryId, Writer,
1312
};
1413
use gimli::{Encoding, Format, LineEncoding, Register, RunTimeEndian, X86_64};
1514

@@ -24,40 +23,6 @@ fn target_endian(tcx: TyCtxt) -> RunTimeEndian {
2423
}
2524
}
2625

27-
fn line_program_add_file(
28-
line_program: &mut LineProgram,
29-
line_strings: &mut LineStringTable,
30-
file: &FileName,
31-
) -> FileId {
32-
match file {
33-
FileName::Real(path) => {
34-
let dir_name = path.parent().unwrap().to_str().unwrap().as_bytes();
35-
let dir_id = if !dir_name.is_empty() {
36-
let dir_name = LineString::new(dir_name, line_program.encoding(), line_strings);
37-
line_program.add_directory(dir_name)
38-
} else {
39-
line_program.default_directory()
40-
};
41-
let file_name = LineString::new(
42-
path.file_name().unwrap().to_str().unwrap().as_bytes(),
43-
line_program.encoding(),
44-
line_strings,
45-
);
46-
line_program.add_file(file_name, dir_id, None)
47-
}
48-
// FIXME give more appropriate file names
49-
_ => {
50-
let dir_id = line_program.default_directory();
51-
let dummy_file_name = LineString::new(
52-
file.to_string().into_bytes(),
53-
line_program.encoding(),
54-
line_strings,
55-
);
56-
line_program.add_file(dummy_file_name, dir_id, None)
57-
}
58-
}
59-
}
60-
6126
pub struct DebugContext<'tcx> {
6227
tcx: TyCtxt<'tcx>,
6328

@@ -135,32 +100,6 @@ impl<'tcx> DebugContext<'tcx> {
135100
}
136101
}
137102

138-
fn emit_location(&mut self, entry_id: UnitEntryId, span: Span) {
139-
let loc = self.tcx.sess.source_map().lookup_char_pos(span.lo());
140-
141-
let file_id = line_program_add_file(
142-
&mut self.dwarf.unit.line_program,
143-
&mut self.dwarf.line_strings,
144-
&loc.file.name,
145-
);
146-
147-
let entry = self.dwarf.unit.get_mut(entry_id);
148-
149-
entry.set(
150-
gimli::DW_AT_decl_file,
151-
AttributeValue::FileIndex(Some(file_id)),
152-
);
153-
entry.set(
154-
gimli::DW_AT_decl_line,
155-
AttributeValue::Udata(loc.line as u64),
156-
);
157-
// FIXME: probably omit this
158-
entry.set(
159-
gimli::DW_AT_decl_column,
160-
AttributeValue::Udata(loc.col.to_usize() as u64),
161-
);
162-
}
163-
164103
fn dwarf_ty(&mut self, ty: Ty<'tcx>) -> UnitEntryId {
165104
if let Some(type_id) = self.types.get(ty) {
166105
return *type_id;
@@ -248,13 +187,6 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
248187
AttributeValue::StringRef(name_id),
249188
);
250189

251-
entry.set(
252-
gimli::DW_AT_low_pc,
253-
AttributeValue::Address(Address::Symbol { symbol, addend: 0 }),
254-
);
255-
256-
debug_context.emit_location(entry_id, mir.span);
257-
258190
FunctionDebugContext {
259191
debug_context,
260192
entry_id,
@@ -305,58 +237,7 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
305237
isa: &dyn cranelift::codegen::isa::TargetIsa,
306238
source_info_set: &indexmap::IndexSet<(Span, mir::SourceScope)>,
307239
) {
308-
let tcx = self.debug_context.tcx;
309-
310-
let line_program = &mut self.debug_context.dwarf.unit.line_program;
311-
312-
line_program.begin_sequence(Some(Address::Symbol {
313-
symbol: self.symbol,
314-
addend: 0,
315-
}));
316-
317-
let encinfo = isa.encoding_info();
318-
let func = &context.func;
319-
let mut ebbs = func.layout.ebbs().collect::<Vec<_>>();
320-
ebbs.sort_by_key(|ebb| func.offsets[*ebb]); // Ensure inst offsets always increase
321-
322-
let line_strings = &mut self.debug_context.dwarf.line_strings;
323-
let mut create_row_for_span = |line_program: &mut LineProgram, span: Span| {
324-
let loc = tcx.sess.source_map().lookup_char_pos(span.lo());
325-
let file_id = line_program_add_file(line_program, line_strings, &loc.file.name);
326-
327-
/*println!(
328-
"srcloc {:>04X} {}:{}:{}",
329-
line_program.row().address_offset,
330-
file.display(),
331-
loc.line,
332-
loc.col.to_u32()
333-
);*/
334-
335-
line_program.row().file = file_id;
336-
line_program.row().line = loc.line as u64;
337-
line_program.row().column = loc.col.to_u32() as u64 + 1;
338-
line_program.generate_row();
339-
};
340-
341-
let mut end = 0;
342-
for ebb in ebbs {
343-
for (offset, inst, size) in func.inst_offsets(ebb, &encinfo) {
344-
let srcloc = func.srclocs[inst];
345-
line_program.row().address_offset = offset as u64;
346-
if !srcloc.is_default() {
347-
let source_info = *source_info_set.get_index(srcloc.bits() as usize).unwrap();
348-
create_row_for_span(line_program, source_info.0);
349-
} else {
350-
create_row_for_span(line_program, self.mir.span);
351-
}
352-
end = offset + size;
353-
}
354-
}
355-
356-
line_program.end_sequence(end as u64);
357-
358-
let entry = self.debug_context.dwarf.unit.get_mut(self.entry_id);
359-
entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(end as u64));
240+
self.create_debug_lines(context, isa, source_info_set);
360241

361242
{
362243
let value_labels_ranges = context.build_value_labels_ranges(isa).unwrap();
@@ -391,17 +272,6 @@ impl<'a, 'tcx> FunctionDebugContext<'a, 'tcx> {
391272
);
392273
}
393274
}
394-
395-
self.debug_context
396-
.unit_range_list
397-
.0
398-
.push(Range::StartLength {
399-
begin: Address::Symbol {
400-
symbol: self.symbol,
401-
addend: 0,
402-
},
403-
length: end as u64,
404-
});
405275
}
406276
}
407277

0 commit comments

Comments
 (0)