Skip to content

Commit 44b3ac9

Browse files
committed
Implement line debuginfo
1 parent 3c44289 commit 44b3ac9

File tree

12 files changed

+706
-29
lines changed

12 files changed

+706
-29
lines changed

Cargo.lock

Lines changed: 52 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ target-lexicon = "0.2.0"
2323
#goblin = "0.0.17"
2424
ar = "0.6.1"
2525
bitflags = "1.0.3"
26-
byteorder = "1.2.6"
26+
byteorder = "1.2.7"
2727
libc = "0.2.45"
2828
tempfile = "3.0.4"
2929
env_logger = "0.6"
30+
gimli = { git = "https://github.com/gimli-rs/gimli.git" }
31+
faerie = "0.7.1"
3032

3133
# Uncomment to use local checkout of cranelift
3234
#[patch."https://github.com/CraneStation/cranelift.git"]
@@ -35,5 +37,8 @@ env_logger = "0.6"
3537
#cranelift-simplejit = { path = "../cranelift/lib/simplejit" }
3638
#cranelift-faerie = { path = "../cranelift/lib/faerie" }
3739

40+
[patch."https://github.com/gimli-rs/gimli.git"]
41+
gimli = { git = "https://github.com/bjorn3/gimli.git", branch = "impl_range_write" }
42+
3843
[profile.dev.overrides."*"]
3944
opt-level = 3

abc

9.78 KB
Binary file not shown.

abc.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// compile using g++ -std=c++11 -g -c abc.cpp -o abc.o
2+
3+
struct Opaque;
4+
5+
struct MyType {
6+
unsigned int field_a;
7+
int field_b;
8+
void* field_c;
9+
float field_d;
10+
//double field_e;
11+
//long long field_f;
12+
bool field_g;
13+
char field_h;
14+
Opaque* field_i;
15+
};
16+
17+
MyType bcd(int x, MyType a) {
18+
MyType b = a;
19+
MyType c = b;
20+
MyType d = c;
21+
MyType e = d;
22+
MyType f = e;
23+
MyType g = f;
24+
MyType h = g;
25+
MyType i = h;
26+
MyType j = i;
27+
MyType k = j;
28+
MyType l = k;
29+
MyType m = l;
30+
return b;
31+
}
32+
int main() {
33+
bcd(42, {});
34+
return 0;
35+
}

example/mini_core_hello_world.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,18 @@ struct Unique<T: ?Sized> {
115115

116116
impl<T: ?Sized, U: ?Sized> CoerceUnsized<Unique<U>> for Unique<T> where T: Unsize<U> {}
117117

118+
fn take_f32(f: f32) {}
119+
fn take_unique(u: Unique<()>) {}
120+
118121
fn main() {
122+
take_unique(Unique {
123+
pointer: 0 as *const (),
124+
_marker: PhantomData,
125+
});
126+
take_f32(0.1);
127+
128+
//return;
129+
119130
unsafe {
120131
let hello: &[u8] = b"Hello\0" as &[u8; 6];
121132
let ptr: *const u8 = hello as *const [u8] as *const u8;

mini_core_hello_world

25.1 KB
Binary file not shown.

src/abi.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ fn get_pass_mode<'a, 'tcx: 'a>(
5151
.unwrap();
5252
assert!(!layout.is_unsized());
5353

54+
if ty == tcx.types.f32 {
55+
return PassMode::ByRef; //FIXME test by-ref param debuginfo in a different way
56+
}
57+
5458
if layout.size.bytes() == 0 {
5559
if is_return {
5660
PassMode::NoPass
@@ -338,6 +342,7 @@ fn local_place<'a, 'tcx: 'a>(
338342
) -> CPlace<'tcx> {
339343
let place = if is_ssa {
340344
fx.bcx.declare_var(mir_var(local), fx.clif_type(layout.ty).unwrap());
345+
341346
CPlace::Var(local, layout)
342347
} else {
343348
let stack_slot = fx.bcx.create_stack_slot(StackSlotData {

src/base.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
6969
let func_id = cx.module
7070
.declare_function(&name, linkage, &sig)
7171
.unwrap();
72+
let mut debug_context = cx.debug_context.as_mut().map(|debug_context| FunctionDebugContext::new(
73+
tcx,
74+
debug_context,
75+
mir,
76+
&name,
77+
&sig,
78+
));
7279

7380
// Step 3. Make FunctionBuilder
7481
let mut func = Function::with_name_signature(ExternalName::user(0, 0), sig);
@@ -101,13 +108,15 @@ fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
101108
clif_comments,
102109
constants: &mut cx.ccx,
103110
caches: &mut cx.caches,
111+
spans: Vec::new(),
104112
};
105113

106114
// Step 6. Codegen function
107115
with_unimpl_span(fx.mir.span, || {
108116
crate::abi::codegen_fn_prelude(&mut fx, start_ebb);
109117
codegen_fn_content(&mut fx);
110118
});
119+
let spans = fx.spans.clone();
111120

112121
// Step 7. Write function to file for debugging
113122
#[cfg(debug_assertions)]
@@ -119,8 +128,12 @@ fn trans_fn<'a, 'clif, 'tcx: 'a, B: Backend + 'static>(
119128
// Step 9. Define function
120129
cx.caches.context.func = func;
121130
cx.module
122-
.define_function(func_id, &mut cx.caches.context)
131+
.define_function_peek_compiled(func_id, &mut cx.caches.context, |size, context, isa| {
132+
debug_context.as_mut().map(|x| x.define(tcx, size, context, isa, &spans[..]));
133+
})
123134
.unwrap();
135+
//let module = &mut cx.module;
136+
//let caches = &cx.caches;
124137
cx.caches.context.clear();
125138
}
126139

@@ -154,6 +167,7 @@ fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>)
154167

155168
fx.bcx.ins().nop();
156169
for stmt in &bb_data.statements {
170+
fx.set_debug_loc(stmt.source_info);
157171
trans_stmt(fx, ebb, stmt);
158172
}
159173

@@ -169,6 +183,8 @@ fn codegen_fn_content<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx, impl Backend>)
169183
fx.add_comment(inst, terminator_head);
170184
}
171185

186+
fx.set_debug_loc(bb_data.terminator().source_info);
187+
172188
match &bb_data.terminator().kind {
173189
TerminatorKind::Goto { target } => {
174190
let ebb = fx.get_ebb(*target);
@@ -322,6 +338,8 @@ fn trans_stmt<'a, 'tcx: 'a>(
322338
) {
323339
let _print_guard = PrintOnPanic(|| format!("stmt {:?}", stmt));
324340

341+
fx.set_debug_loc(stmt.source_info);
342+
325343
#[cfg(debug_assertions)]
326344
match &stmt.kind {
327345
StatementKind::StorageLive(..) | StatementKind::StorageDead(..) => {} // Those are not very useful

src/common.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
539539
pub clif_comments: crate::pretty_clif::CommentWriter,
540540
pub constants: &'a mut crate::constant::ConstantCx,
541541
pub caches: &'a mut Caches<'tcx>,
542+
pub spans: Vec<Span>,
542543
}
543544

544545
impl<'a, 'tcx: 'a, B: Backend + 'a> fmt::Debug for FunctionCx<'a, 'tcx, B> {
@@ -617,4 +618,11 @@ impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
617618
pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
618619
*self.local_map.get(&local).unwrap()
619620
}
621+
622+
pub fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
623+
// FIXME: record scope too
624+
let index = self.spans.len() as u32;
625+
self.spans.push(source_info.span);
626+
self.bcx.set_srcloc(SourceLoc::new(index));
627+
}
620628
}

0 commit comments

Comments
 (0)