@@ -24,6 +24,7 @@ use util::ppaux::ty_to_str;
2424use core:: hashmap:: HashMap ;
2525use core:: libc;
2626use core:: libc:: c_uint;
27+ use core:: cmp;
2728use core:: ptr;
2829use core:: str:: as_c_str;
2930use core:: sys;
@@ -204,8 +205,7 @@ fn create_block(bcx: block) -> DILexicalBlock {
204205
205206fn size_and_align_of ( cx : @CrateContext , t : ty:: t ) -> ( uint , uint ) {
206207 let llty = type_of:: type_of ( cx, t) ;
207- ( machine:: llsize_of_real ( cx, llty) ,
208- machine:: llalign_of_pref ( cx, llty) )
208+ ( machine:: llsize_of_real ( cx, llty) , machine:: llalign_of_min ( cx, llty) )
209209}
210210
211211fn create_basic_type ( cx : @CrateContext , t : ty:: t , span : span ) -> DIType {
@@ -277,46 +277,56 @@ struct StructContext {
277277
278278impl StructContext {
279279 fn create ( cx : @CrateContext , file : DIFile , name : ~str , line : uint ) -> ~StructContext {
280+ debug ! ( "StructContext::create: %s" , name) ;
280281 let scx = ~StructContext {
281282 cx : cx,
282283 file : file,
283284 name : name,
284285 line : line,
285286 members : ~[ ] ,
286287 total_size : 0 ,
287- align : 64 //XXX different alignment per arch?
288+ align : 1
288289 } ;
289290 return scx;
290291 }
291292
292293 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) ;
295+ let offset = roundup ( self . total_size , align) ;
293296 let mem_t = do as_c_str ( name) |name| { unsafe {
294297 llvm:: LLVMDIBuilderCreateMemberType ( dbg_cx ( self . cx ) . builder ,
295298 ptr:: null ( ) , name, self . file , line as c_uint ,
296- size * 8 as u64 , align * 8 as u64 , self . total_size as u64 ,
299+ size * 8 as u64 , align * 8 as u64 , offset * 8 as u64 ,
297300 0 , ty)
298301 } } ;
299- // XXX What about member alignment???
300302 self . members . push ( mem_t) ;
301- self . total_size += size * 8 ;
303+ self . total_size = offset + size;
304+ // struct alignment is the max alignment of its' members
305+ self . align = cmp:: max ( self . align , align) ;
302306 }
303307
304308 fn finalize ( & self ) -> DICompositeType {
309+ debug ! ( "StructContext(%s)::finalize: total_size=%u, align=%u" , self . name, self . total_size, self . align) ;
305310 let dcx = dbg_cx ( self . cx ) ;
306311 let members_md = create_DIArray ( dcx. builder , self . members ) ;
307312
308313 let struct_md =
309314 do as_c_str ( self . name ) |name| { unsafe {
310315 llvm:: LLVMDIBuilderCreateStructType (
311- dcx. builder , ptr :: null ( ) , name,
316+ dcx. builder , self . file , name,
312317 self . file , self . line as c_uint ,
313- self . total_size as u64 , self . align as u64 , 0 , ptr:: null ( ) ,
318+ self . total_size * 8 as u64 , self . align * 8 as u64 , 0 , ptr:: null ( ) ,
314319 members_md, 0 , ptr:: null ( ) )
315320 } } ;
316321 return struct_md;
317322 }
318323}
319324
325+ #[ inline( always) ]
326+ fn roundup ( x : uint , a : uint ) -> uint {
327+ ( ( x + ( a - 1 ) ) / a) * a
328+ }
329+
320330fn create_struct ( cx : @CrateContext , t : ty:: t , fields : ~[ ty:: field ] , span : span ) -> DICompositeType {
321331 let loc = span_start ( cx, span) ;
322332 let file_md = create_file ( cx, loc. file . name ) ;
@@ -390,12 +400,12 @@ fn create_fixed_vec(cx: @CrateContext, vec_t: ty::t, elem_t: ty::t,
390400 let ( size, align) = size_and_align_of ( cx, elem_t) ;
391401
392402 let subrange = unsafe {
393- llvm:: LLVMDIBuilderGetOrCreateSubrange ( dcx. builder , 0_i64 , ( len- 1 ) as i64 ) } ;
403+ llvm:: LLVMDIBuilderGetOrCreateSubrange ( dcx. builder , 0_i64 , len as i64 ) } ;
394404
395405 let subscripts = create_DIArray ( dcx. builder , [ subrange] ) ;
396406 return unsafe {
397- llvm:: LLVMDIBuilderCreateVectorType ( dcx. builder ,
398- size * len as u64 , align as u64 , elem_ty_md, subscripts)
407+ llvm:: LLVMDIBuilderCreateArrayType ( dcx. builder ,
408+ size * len * 8 as u64 , align * 8 as u64 , elem_ty_md, subscripts)
399409 } ;
400410}
401411
@@ -418,8 +428,8 @@ fn create_boxed_vec(cx: @CrateContext, vec_t: ty::t, elem_t: ty::t,
418428 let name = fmt ! ( "[%s]" , ty_to_str( cx. tcx, elem_t) ) ;
419429
420430 let subscripts = create_DIArray ( dcx. builder , [ subrange] ) ;
421- let data_ptr = unsafe { llvm:: LLVMDIBuilderCreateVectorType ( dcx. builder ,
422- arr_size as u64 , arr_align as u64 , elem_ty_md, subscripts) } ;
431+ let data_ptr = unsafe { llvm:: LLVMDIBuilderCreateArrayType ( dcx. builder ,
432+ arr_size * 8 as u64 , arr_align * 8 as u64 , elem_ty_md, subscripts) } ;
423433 vec_scx. add_member ( "data" , 0 , 0 , // clang says the size should be 0
424434 sys:: min_align_of :: < u8 > ( ) , data_ptr) ;
425435 let vec_md = vec_scx. finalize ( ) ;
0 commit comments