@@ -303,60 +303,24 @@ impl<'ink> DebugBuilder<'ink> {
303
303
. unwrap_or_else ( || self . compile_unit . get_file ( ) ) ;
304
304
305
305
let mut types = vec ! [ ] ;
306
- let mut last_offset_bits = 0 ;
307
- let mut last_size_bits = 0 ;
308
306
309
307
for ( element_index, ( member_name, dt, location) ) in index_types. iter ( ) . enumerate ( ) {
310
308
let di_type = self . get_or_create_debug_type ( dt, index, types_index) ?;
311
309
312
- // Get the size and alignment
310
+ // Get the size and alignment from LLVM
313
311
let llvm_type = types_index. find_associated_type ( dt. get_name ( ) ) ;
314
312
let align_bits =
315
313
llvm_type. map ( |ty| self . target_data . get_preferred_alignment ( & ty) * 8 ) . unwrap_or ( 0 ) ;
316
314
let size_bits = llvm_type. map ( |ty| self . target_data . get_bit_size ( & ty) ) . unwrap_or ( 0 ) ;
317
315
318
316
// Get LLVM's calculated offset
319
- let llvm_offset_bits = self
317
+ let offset_bits = self
320
318
. target_data
321
319
. offset_of_element ( & struct_type, element_index as u32 )
322
320
. map ( |offset| offset * 8 )
323
321
. unwrap_or ( 0 ) ;
324
322
325
- // Calculate the properly aligned offset based on the previous field
326
- // and this field's alignment requirements
327
- let offset_bits = if size_bits == 0 || ( last_size_bits == 0 && element_index > 0 ) {
328
- // For zero-sized types, always use LLVM's offset directly
329
- // This ensures they don't contribute to the overall layout calculation
330
- // If the previous field was zero-sized, use LLVM's offset
331
- // for proper alignment of fields after zero-sized types
332
- llvm_offset_bits
333
- } else {
334
- let next_offset_bits: u64 = last_offset_bits + last_size_bits;
335
-
336
- // Special handling based on alignment requirements:
337
- // - Fields with alignment of 64 bits need to be explicitly aligned
338
- // to their alignment boundary to prevent misaligned accesses
339
- // - Fields with a lower alignment can use LLVM's natural layout
340
- // This differentiation is crucial for correctly representing complex structures
341
- // where some fields (like LWORD, LINT) need specific alignment while others (like BYTE, BOOL)
342
- // can be packed more efficiently
343
- if align_bits == 64 {
344
- // For fields requiring special alignment (64 bit types),
345
- // round up to the nearest alignment boundary
346
- next_offset_bits. div_ceil ( align_bits as u64 ) * align_bits as u64
347
- } else {
348
- // For smaller fields fields (BYTE, BOOL, DINT), we still ensure we don't
349
- // overlap with previous fields by using the maximum of our calculated offset
350
- // and LLVM's calculated offset
351
- std:: cmp:: max ( next_offset_bits, llvm_offset_bits)
352
- }
353
- } ;
354
-
355
- // Update tracking variables for next field
356
- last_offset_bits = offset_bits;
357
- last_size_bits = size_bits;
358
-
359
- // Create the member type with calculated offset
323
+ // Create the member type with LLVM's calculated offset
360
324
types. push (
361
325
self . debug_info
362
326
. create_member_type (
@@ -374,39 +338,17 @@ impl<'ink> DebugBuilder<'ink> {
374
338
) ;
375
339
}
376
340
377
- // Calculate struct size based on the last field 's offset + size, properly aligned
341
+ // Use LLVM 's calculation for the struct size
378
342
let llvm_size = self . target_data . get_bit_size ( & struct_type) ;
379
- // Calculate our manual size based on adjusted offsets
380
- let calculated_size = {
381
- // Get struct alignment requirement (usually 8 bytes/64 bits for 64-bit architectures)
382
- let struct_align_bits = self . target_data . get_preferred_alignment ( & struct_type) * 8 ;
383
-
384
- // Calculate total size based on last field offset + size, rounded up to alignment
385
- let last_field_end_bits = last_offset_bits + last_size_bits;
386
- let aligned_size_bits =
387
- last_field_end_bits. div_ceil ( struct_align_bits as u64 ) * struct_align_bits as u64 ;
388
-
389
- // If our calculated size is larger than LLVM's, use ours
390
- if aligned_size_bits > llvm_size {
391
- log:: trace!(
392
- "Struct {}: adjusted size from {} to {} bits due to field alignment" ,
393
- name,
394
- llvm_size,
395
- aligned_size_bits
396
- ) ;
397
- aligned_size_bits
398
- } else {
399
- llvm_size
400
- }
401
- } ;
343
+ let struct_align_bits = self . target_data . get_preferred_alignment ( & struct_type) * 8 ;
402
344
403
345
let struct_type = self . debug_info . create_struct_type (
404
346
file. as_debug_info_scope ( ) ,
405
347
name,
406
348
file,
407
349
location. get_line_plus_one ( ) as u32 ,
408
- calculated_size ,
409
- self . target_data . get_preferred_alignment ( & struct_type ) * 8 ,
350
+ llvm_size ,
351
+ struct_align_bits ,
410
352
DIFlags :: PUBLIC ,
411
353
None ,
412
354
types. as_slice ( ) ,
0 commit comments