1- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+ // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22// file at the top-level directory of this distribution and at
33// http://rust-lang.org/COPYRIGHT.
44//
@@ -32,9 +32,7 @@ use core::vec;
3232use syntax:: codemap:: span;
3333use syntax:: { ast, codemap, ast_util, ast_map} ;
3434
35- static LLVMDebugVersion : int = ( 12 << 16 ) ;
36-
37- static DW_LANG_RUST : int = 12 ; //0x9000;
35+ static DW_LANG_RUST : int = 0x9000 ;
3836
3937static CompileUnitTag : int = 17 ;
4038static FileDescriptorTag : int = 41 ;
@@ -65,6 +63,7 @@ pub type DebugContext = @mut _DebugContext;
6563struct _DebugContext {
6664 names : namegen ,
6765 crate_file : ~str ,
66+ llcontext : ContextRef ,
6867 builder : DIBuilderRef ,
6968 curr_loc : ( uint , uint ) ,
7069 created_files : HashMap < ~str , DIFile > ,
@@ -73,13 +72,16 @@ struct _DebugContext {
7372 created_types : HashMap < uint , DIType >
7473}
7574
76- /** Create new DebugContext */
75+ /// Create new DebugContext
7776pub fn mk_ctxt ( llmod : ModuleRef , crate : ~str ) -> DebugContext {
7877 debug ! ( "mk_ctxt" ) ;
7978 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) } ;
8081 let dcx = @mut _DebugContext {
8182 names : new_namegen ( ) ,
8283 crate_file: crate ,
84+ llcontext : llcontext,
8385 builder : builder,
8486 curr_loc : ( 0 , 0 ) ,
8587 created_files : HashMap :: new ( ) ,
@@ -91,18 +93,11 @@ pub fn mk_ctxt(llmod: ModuleRef, crate: ~str) -> DebugContext {
9193}
9294
9395#[ inline( always) ]
94- fn dbg_cx ( cx : & CrateContext ) -> DebugContext
95- {
96+ fn dbg_cx ( cx : & CrateContext ) -> DebugContext {
9697 return cx. dbg_cx . get ( ) ;
9798}
9899
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
106101pub fn finalize ( cx : @CrateContext ) {
107102 debug ! ( "finalize" ) ;
108103 create_compile_unit ( cx) ;
@@ -113,6 +108,12 @@ pub fn finalize(cx: @CrateContext) {
113108 } ;
114109}
115110
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+
116117fn create_compile_unit ( cx : @CrateContext ) {
117118 let crate_name: & str = dbg_cx ( cx) . crate_file ;
118119 let work_dir = cx. sess . working_dir . to_str ( ) ;
@@ -158,6 +159,7 @@ fn create_file(cx: @CrateContext, full_path: &str) -> DIFile {
158159 return file_md;
159160}
160161
162+ /// Return codemap::Loc corresponding to the beginning of the span
161163fn span_start ( cx : @CrateContext , span : span ) -> codemap:: Loc {
162164 return cx. sess . codemap . lookup_char_pos ( span. lo ) ;
163165}
@@ -208,7 +210,7 @@ fn size_and_align_of(cx: @CrateContext, t: ty::t) -> (uint, uint) {
208210 ( machine:: llsize_of_real ( cx, llty) , machine:: llalign_of_min ( cx, llty) )
209211}
210212
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 {
212214 let dcx = dbg_cx ( cx) ;
213215 let ty_id = ty:: type_id ( t) ;
214216 match dcx. created_types . find ( & ty_id) {
@@ -255,7 +257,7 @@ fn create_basic_type(cx: @CrateContext, t: ty::t, span: span) -> DIType{
255257 return ty_md;
256258}
257259
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 {
259261 let ( size, align) = size_and_align_of ( cx, t) ;
260262 let name = ty_to_str ( cx. tcx , t) ;
261263 let ptr_md = do as_c_str ( name) |name| { unsafe {
@@ -291,7 +293,8 @@ impl StructContext {
291293 }
292294
293295 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) ;
295298 let offset = roundup ( self . total_size , align) ;
296299 let mem_t = do as_c_str ( name) |name| { unsafe {
297300 llvm:: LLVMDIBuilderCreateMemberType ( dbg_cx ( self . cx ) . builder ,
@@ -306,7 +309,8 @@ impl StructContext {
306309 }
307310
308311 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) ;
310314 let dcx = dbg_cx ( self . cx ) ;
311315 let members_md = create_DIArray ( dcx. builder , self . members ) ;
312316
@@ -323,8 +327,8 @@ impl StructContext {
323327}
324328
325329#[ 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
328332}
329333
330334fn create_struct ( cx : @CrateContext , t : ty:: t , fields : ~[ ty:: field ] , span : span ) -> DICompositeType {
@@ -353,7 +357,7 @@ fn voidptr(cx: @CrateContext) -> (DIDerivedType, uint, uint) {
353357 return ( vp, size, align) ;
354358}
355359
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 {
357361 let dcx = dbg_cx ( cx) ;
358362 let loc = span_start ( cx, span) ;
359363 let file_md = create_file ( cx, loc. file . name ) ;
@@ -391,12 +395,10 @@ fn create_boxed_type(cx: @CrateContext, contents: ty::t,
391395 return scx. finalize ( ) ;
392396}
393397
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 ,
395399 len : uint , span : span ) -> DIType {
396400 let dcx = dbg_cx ( cx) ;
397401 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 ) ;
400402 let ( size, align) = size_and_align_of ( cx, elem_t) ;
401403
402404 let subrange = unsafe {
@@ -465,7 +467,7 @@ fn create_vec_slice(cx: @CrateContext, vec_t: ty::t, elem_t: ty::t, span: span)
465467 return scx. finalize ( ) ;
466468}
467469
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 ,
469471 span : span ) -> DICompositeType {
470472 let dcx = dbg_cx ( cx) ;
471473 let loc = span_start ( cx, span) ;
@@ -487,7 +489,7 @@ fn create_unimpl_ty(cx: @CrateContext, t: ty::t) -> DIType {
487489 let name = ty_to_str ( cx. tcx , t) ;
488490 let md = do as_c_str ( fmt ! ( "NYI<%s>" , name) ) |name| { unsafe {
489491 llvm:: LLVMDIBuilderCreateBasicType (
490- dcx. builder , name,
492+ dcx. builder , name,
491493 0_u64 , 8_u64 , DW_ATE_unsigned as c_uint )
492494 } } ;
493495 return md;
@@ -621,7 +623,7 @@ pub fn create_local_var(bcx: block, local: @ast::local) -> DIVariable {
621623 }
622624 } ;
623625
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 ( ) ) ;
625627 unsafe {
626628 let instr = llvm:: LLVMDIBuilderInsertDeclareAtEnd ( dcx. builder , llptr, var_md, bcx. llbb ) ;
627629 llvm:: LLVMSetInstDebugLocation ( trans:: build:: B ( bcx) , instr) ;
@@ -633,7 +635,7 @@ pub fn create_local_var(bcx: block, local: @ast::local) -> DIVariable {
633635pub fn create_arg ( bcx : block , arg : ast:: arg , span : span ) -> Option < DIVariable > {
634636 debug ! ( "create_arg" ) ;
635637 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
637639 // up: "error: internal compiler error: node_id_to_type: no type for node `arg (id=10)`"
638640 return None ;
639641 }
@@ -661,12 +663,15 @@ pub fn create_arg(bcx: block, arg: ast::arg, span: span) -> Option<DIVariable> {
661663 llvm:: LLVMDIBuilderCreateLocalVariable ( dcx. builder ,
662664 ArgVariableTag as u32 , context, name,
663665 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
665667 } } ;
666668
667669 let llptr = fcx. llargs . get_copy ( & arg. id ) ;
670+ set_debug_location ( cx, create_block ( bcx) , loc. line , loc. col . to_uint ( ) ) ;
668671 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) ;
670675 }
671676 return Some ( mdnode) ;
672677 }
@@ -676,33 +681,31 @@ pub fn create_arg(bcx: block, arg: ast::arg, span: span) -> Option<DIVariable> {
676681 }
677682}
678683
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 ( ) ] ;
682693 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) ;
685698 }
686699}
687700
701+ /// Set current debug location at the beginning of the span
688702pub fn update_source_pos ( bcx : block , span : span ) {
689703 if !bcx. sess ( ) . opts . debuginfo || ( * span. lo == 0 && * span. hi == 0 ) {
690704 return ;
691705 }
692-
693706 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 ( ) )
706709}
707710
708711pub fn create_function ( fcx : fn_ctxt ) -> DISubprogram {
0 commit comments