1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -32,9 +32,7 @@ use core::vec;
32
32
use syntax:: codemap:: span;
33
33
use syntax:: { ast, codemap, ast_util, ast_map} ;
34
34
35
- static LLVMDebugVersion : int = ( 12 << 16 ) ;
36
-
37
- static DW_LANG_RUST : int = 12 ; //0x9000;
35
+ static DW_LANG_RUST : int = 0x9000 ;
38
36
39
37
static CompileUnitTag : int = 17 ;
40
38
static FileDescriptorTag : int = 41 ;
@@ -65,6 +63,7 @@ pub type DebugContext = @mut _DebugContext;
65
63
struct _DebugContext {
66
64
names : namegen ,
67
65
crate_file : ~str ,
66
+ llcontext : ContextRef ,
68
67
builder : DIBuilderRef ,
69
68
curr_loc : ( uint , uint ) ,
70
69
created_files : HashMap < ~str , DIFile > ,
@@ -73,13 +72,16 @@ struct _DebugContext {
73
72
created_types : HashMap < uint , DIType >
74
73
}
75
74
76
- /** Create new DebugContext */
75
+ /// Create new DebugContext
77
76
pub fn mk_ctxt ( llmod : ModuleRef , crate : ~str ) -> DebugContext {
78
77
debug ! ( "mk_ctxt" ) ;
79
78
let builder = unsafe { llvm:: LLVMDIBuilderCreate ( llmod) } ;
79
+ // DIBuilder inherits context from the module, so we'd better use the same one
80
+ let llcontext = unsafe { llvm:: LLVMGetModuleContext ( llmod) } ;
80
81
let dcx = @mut _DebugContext {
81
82
names : new_namegen ( ) ,
82
83
crate_file: crate ,
84
+ llcontext : llcontext,
83
85
builder : builder,
84
86
curr_loc : ( 0 , 0 ) ,
85
87
created_files : HashMap :: new ( ) ,
@@ -91,18 +93,11 @@ pub fn mk_ctxt(llmod: ModuleRef, crate: ~str) -> DebugContext {
91
93
}
92
94
93
95
#[ inline( always) ]
94
- fn dbg_cx ( cx : & CrateContext ) -> DebugContext
95
- {
96
+ fn dbg_cx ( cx : & CrateContext ) -> DebugContext {
96
97
return cx. dbg_cx . get ( ) ;
97
98
}
98
99
99
- fn create_DIArray ( builder : DIBuilderRef , arr : & [ DIDescriptor ] ) -> DIArray {
100
- return unsafe {
101
- llvm:: LLVMDIBuilderGetOrCreateArray ( builder, vec:: raw:: to_ptr ( arr) , arr. len ( ) as u32 )
102
- } ;
103
- }
104
-
105
- /** Create any deferred debug metadata nodes */
100
+ /// Create any deferred debug metadata nodes
106
101
pub fn finalize ( cx : @CrateContext ) {
107
102
debug ! ( "finalize" ) ;
108
103
create_compile_unit ( cx) ;
@@ -113,6 +108,12 @@ pub fn finalize(cx: @CrateContext) {
113
108
} ;
114
109
}
115
110
111
+ fn create_DIArray ( builder : DIBuilderRef , arr : & [ DIDescriptor ] ) -> DIArray {
112
+ return unsafe {
113
+ llvm:: LLVMDIBuilderGetOrCreateArray ( builder, vec:: raw:: to_ptr ( arr) , arr. len ( ) as u32 )
114
+ } ;
115
+ }
116
+
116
117
fn create_compile_unit ( cx : @CrateContext ) {
117
118
let crate_name: & str = dbg_cx ( cx) . crate_file ;
118
119
let work_dir = cx. sess . working_dir . to_str ( ) ;
@@ -158,6 +159,7 @@ fn create_file(cx: @CrateContext, full_path: &str) -> DIFile {
158
159
return file_md;
159
160
}
160
161
162
+ /// Return codemap::Loc corresponding to the beginning of the span
161
163
fn span_start ( cx : @CrateContext , span : span ) -> codemap:: Loc {
162
164
return cx. sess . codemap . lookup_char_pos ( span. lo ) ;
163
165
}
@@ -208,7 +210,7 @@ fn size_and_align_of(cx: @CrateContext, t: ty::t) -> (uint, uint) {
208
210
( machine:: llsize_of_real ( cx, llty) , machine:: llalign_of_min ( cx, llty) )
209
211
}
210
212
211
- fn create_basic_type ( cx : @CrateContext , t : ty:: t , span : span ) -> DIType {
213
+ fn create_basic_type ( cx : @CrateContext , t : ty:: t , _span : span ) -> DIType {
212
214
let dcx = dbg_cx ( cx) ;
213
215
let ty_id = ty:: type_id ( t) ;
214
216
match dcx. created_types . find ( & ty_id) {
@@ -255,7 +257,7 @@ fn create_basic_type(cx: @CrateContext, t: ty::t, span: span) -> DIType{
255
257
return ty_md;
256
258
}
257
259
258
- fn create_pointer_type ( cx : @CrateContext , t : ty:: t , span : span , pointee : DIType ) -> DIType {
260
+ fn create_pointer_type ( cx : @CrateContext , t : ty:: t , _span : span , pointee : DIType ) -> DIType {
259
261
let ( size, align) = size_and_align_of ( cx, t) ;
260
262
let name = ty_to_str ( cx. tcx , t) ;
261
263
let ptr_md = do as_c_str ( name) |name| { unsafe {
@@ -291,7 +293,8 @@ impl StructContext {
291
293
}
292
294
293
295
fn add_member ( & mut self , name : & str , line : uint , size : uint , align : uint , ty : DIType ) {
294
- debug ! ( "StructContext(%s)::add_member: %s, size=%u, align=%u" , self . name, name, size, align) ;
296
+ debug ! ( "StructContext(%s)::add_member: %s, size=%u, align=%u" ,
297
+ self . name, name, size, align) ;
295
298
let offset = roundup ( self . total_size , align) ;
296
299
let mem_t = do as_c_str ( name) |name| { unsafe {
297
300
llvm:: LLVMDIBuilderCreateMemberType ( dbg_cx ( self . cx ) . builder ,
@@ -306,7 +309,8 @@ impl StructContext {
306
309
}
307
310
308
311
fn finalize ( & self ) -> DICompositeType {
309
- debug ! ( "StructContext(%s)::finalize: total_size=%u, align=%u" , self . name, self . total_size, self . align) ;
312
+ debug ! ( "StructContext(%s)::finalize: total_size=%u, align=%u" ,
313
+ self . name, self . total_size, self . align) ;
310
314
let dcx = dbg_cx ( self . cx ) ;
311
315
let members_md = create_DIArray ( dcx. builder , self . members ) ;
312
316
@@ -323,8 +327,8 @@ impl StructContext {
323
327
}
324
328
325
329
#[ inline( always) ]
326
- fn roundup ( x : uint , a : uint ) -> uint {
327
- ( ( x + ( a - 1 ) ) / a) * a
330
+ fn roundup ( x : uint , a : uint ) -> uint {
331
+ ( ( x + ( a - 1 ) ) / a) * a
328
332
}
329
333
330
334
fn create_struct ( cx : @CrateContext , t : ty:: t , fields : ~[ ty:: field ] , span : span ) -> DICompositeType {
@@ -353,7 +357,7 @@ fn voidptr(cx: @CrateContext) -> (DIDerivedType, uint, uint) {
353
357
return ( vp, size, align) ;
354
358
}
355
359
356
- fn create_tuple ( cx : @CrateContext , t : ty:: t , elements : & [ ty:: t ] , span : span ) -> DICompositeType {
360
+ fn create_tuple ( cx : @CrateContext , _t : ty:: t , elements : & [ ty:: t ] , span : span ) -> DICompositeType {
357
361
let dcx = dbg_cx ( cx) ;
358
362
let loc = span_start ( cx, span) ;
359
363
let file_md = create_file ( cx, loc. file . name ) ;
@@ -391,12 +395,10 @@ fn create_boxed_type(cx: @CrateContext, contents: ty::t,
391
395
return scx. finalize ( ) ;
392
396
}
393
397
394
- fn create_fixed_vec ( cx : @CrateContext , vec_t : ty:: t , elem_t : ty:: t ,
398
+ fn create_fixed_vec ( cx : @CrateContext , _vec_t : ty:: t , elem_t : ty:: t ,
395
399
len : uint , span : span ) -> DIType {
396
400
let dcx = dbg_cx ( cx) ;
397
401
let elem_ty_md = create_ty ( cx, elem_t, span) ;
398
- let loc = span_start ( cx, span) ;
399
- let file_md = create_file ( cx, loc. file . name ) ;
400
402
let ( size, align) = size_and_align_of ( cx, elem_t) ;
401
403
402
404
let subrange = unsafe {
@@ -465,7 +467,7 @@ fn create_vec_slice(cx: @CrateContext, vec_t: ty::t, elem_t: ty::t, span: span)
465
467
return scx. finalize ( ) ;
466
468
}
467
469
468
- fn create_fn_ty ( cx : @CrateContext , fn_ty : ty:: t , inputs : ~[ ty:: t ] , output : ty:: t ,
470
+ fn create_fn_ty ( cx : @CrateContext , _fn_ty : ty:: t , inputs : ~[ ty:: t ] , output : ty:: t ,
469
471
span : span ) -> DICompositeType {
470
472
let dcx = dbg_cx ( cx) ;
471
473
let loc = span_start ( cx, span) ;
@@ -487,7 +489,7 @@ fn create_unimpl_ty(cx: @CrateContext, t: ty::t) -> DIType {
487
489
let name = ty_to_str ( cx. tcx , t) ;
488
490
let md = do as_c_str ( fmt ! ( "NYI<%s>" , name) ) |name| { unsafe {
489
491
llvm:: LLVMDIBuilderCreateBasicType (
490
- dcx. builder , name,
492
+ dcx. builder , name,
491
493
0_u64 , 8_u64 , DW_ATE_unsigned as c_uint )
492
494
} } ;
493
495
return md;
@@ -621,7 +623,7 @@ pub fn create_local_var(bcx: block, local: @ast::local) -> DIVariable {
621
623
}
622
624
} ;
623
625
624
- set_debug_location ( bcx, loc. line , loc. col . to_uint ( ) ) ;
626
+ set_debug_location ( cx , create_block ( bcx) , loc. line , loc. col . to_uint ( ) ) ;
625
627
unsafe {
626
628
let instr = llvm:: LLVMDIBuilderInsertDeclareAtEnd ( dcx. builder , llptr, var_md, bcx. llbb ) ;
627
629
llvm:: LLVMSetInstDebugLocation ( trans:: build:: B ( bcx) , instr) ;
@@ -633,7 +635,7 @@ pub fn create_local_var(bcx: block, local: @ast::local) -> DIVariable {
633
635
pub fn create_arg ( bcx : block , arg : ast:: arg , span : span ) -> Option < DIVariable > {
634
636
debug ! ( "create_arg" ) ;
635
637
if true {
636
- // FIXME(5848) create_arg disabled for now because "node_id_type(bcx, arg.id)" below blows
638
+ // XXX create_arg disabled for now because "node_id_type(bcx, arg.id)" below blows
637
639
// up: "error: internal compiler error: node_id_to_type: no type for node `arg (id=10)`"
638
640
return None ;
639
641
}
@@ -661,12 +663,15 @@ pub fn create_arg(bcx: block, arg: ast::arg, span: span) -> Option<DIVariable> {
661
663
llvm:: LLVMDIBuilderCreateLocalVariable ( dcx. builder ,
662
664
ArgVariableTag as u32 , context, name,
663
665
filemd, loc. line as c_uint , tymd, false , 0 , 0 )
664
- // FIXME need to pass a real argument number
666
+ // XXX need to pass in a real argument number
665
667
} } ;
666
668
667
669
let llptr = fcx. llargs . get_copy ( & arg. id ) ;
670
+ set_debug_location ( cx, create_block ( bcx) , loc. line , loc. col . to_uint ( ) ) ;
668
671
unsafe {
669
- llvm:: LLVMDIBuilderInsertDeclareAtEnd ( dcx. builder , llptr, mdnode, bcx. llbb ) ;
672
+ let instr = llvm:: LLVMDIBuilderInsertDeclareAtEnd (
673
+ dcx. builder , llptr, mdnode, bcx. llbb ) ;
674
+ llvm:: LLVMSetInstDebugLocation ( trans:: build:: B ( bcx) , instr) ;
670
675
}
671
676
return Some ( mdnode) ;
672
677
}
@@ -676,33 +681,31 @@ pub fn create_arg(bcx: block, arg: ast::arg, span: span) -> Option<DIVariable> {
676
681
}
677
682
}
678
683
679
- fn set_debug_location ( bcx : block , line : uint , col : uint ) {
680
- let blockmd = create_block ( bcx) ;
681
- let elems = ~[ C_i32 ( line as i32 ) , C_i32 ( col as i32 ) , blockmd, ptr:: null ( ) ] ;
684
+ fn set_debug_location ( cx : @CrateContext , scope : DIScope , line : uint , col : uint ) {
685
+ let dcx = dbg_cx ( cx) ;
686
+ if dcx. curr_loc == ( line, col) {
687
+ return ;
688
+ }
689
+ debug ! ( "setting debug location to %u %u" , line, col) ;
690
+ dcx. curr_loc = ( line, col) ;
691
+
692
+ let elems = ~[ C_i32 ( line as i32 ) , C_i32 ( col as i32 ) , scope, ptr:: null ( ) ] ;
682
693
unsafe {
683
- let dbg_loc = llvm:: LLVMMDNode ( vec:: raw:: to_ptr ( elems) , elems. len ( ) as libc:: c_uint ) ;
684
- llvm:: LLVMSetCurrentDebugLocation ( trans:: build:: B ( bcx) , dbg_loc) ;
694
+ let dbg_loc = llvm:: LLVMMDNodeInContext (
695
+ dcx. llcontext , vec:: raw:: to_ptr ( elems) ,
696
+ elems. len ( ) as libc:: c_uint ) ;
697
+ llvm:: LLVMSetCurrentDebugLocation ( cx. builder . B , dbg_loc) ;
685
698
}
686
699
}
687
700
701
+ /// Set current debug location at the beginning of the span
688
702
pub fn update_source_pos ( bcx : block , span : span ) {
689
703
if !bcx. sess ( ) . opts . debuginfo || ( * span. lo == 0 && * span. hi == 0 ) {
690
704
return ;
691
705
}
692
-
693
706
debug ! ( "update_source_pos: %s" , bcx. sess( ) . codemap. span_to_str( span) ) ;
694
-
695
- let cx = bcx. ccx ( ) ;
696
- let loc = span_start ( cx, span) ;
697
- let dcx = dbg_cx ( cx) ;
698
-
699
- let loc = ( loc. line , loc. col . to_uint ( ) ) ;
700
- if loc == dcx. curr_loc {
701
- return ;
702
- }
703
- debug ! ( "setting_location to %u %u" , loc. first( ) , loc. second( ) ) ;
704
- dcx. curr_loc = loc;
705
- set_debug_location ( bcx, loc. first ( ) , loc. second ( ) ) ;
707
+ let loc = span_start ( bcx. ccx ( ) , span) ;
708
+ set_debug_location ( bcx. ccx ( ) , create_block ( bcx) , loc. line , loc. col . to_uint ( ) )
706
709
}
707
710
708
711
pub fn create_function ( fcx : fn_ctxt ) -> DISubprogram {
0 commit comments