Skip to content

Commit 2204d14

Browse files
authored
Merge pull request #110 from swurzinger/struct-size-fixes
C backend: struct size fixes (#858)
2 parents 5ee8f12 + 88e0237 commit 2204d14

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Version 1.06.0
8080
- #699: fix new[0] causing infinite loop when calling constructor/destructor list
8181
- #883: when mapping 32 & 64 bit functions for PALETTE [GET] USING, check the data type pointed to and choose one of LONG PTR, LONGINT PTR, INTEGER PTR
8282
- #866: fbc was throwing lexer errors in comments stating with $. Comments are lexed for directives; allow suffixes in comments
83+
- #858: C backend: fix internal structure size mismatch due to wrong padding when nesting packed structures
8384

8485

8586
Version 1.05.0

src/compiler/ir-hlc.bas

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,23 @@ private sub hEmitStructWithFields( byval s as FBSYMBOL ptr )
10381038
ln += " __attribute__((packed, aligned(" + str( align ) + ")))"
10391039
end if
10401040
end if
1041+
1042+
'' The alignment of nested structures which are packed with a
1043+
'' smaller alignment than the natural or specified alignment of the
1044+
'' parent structure has to be specified explicitly,
1045+
'' otherwise the field will be packed too.
1046+
if ( align <= 0 orelse skip ) then
1047+
if( typeGet( dtype ) = FB_DATATYPE_STRUCT ) then
1048+
var effectivealign = typeCalcNaturalAlign( dtype, subtype )
1049+
if ( align > 0 andalso align < effectivealign ) then
1050+
effectivealign = align
1051+
end if
1052+
var fieldudtalign = symbGetUDTAlign( subtype )
1053+
if( fieldudtalign > 0 andalso effectivealign > fieldudtalign ) then
1054+
ln += " __attribute__((aligned(" + str( effectivealign ) + ")))"
1055+
end if
1056+
end if
1057+
end if
10411058

10421059
ln += ";"
10431060
hWriteLine( ln, TRUE )

tests/structs/padding.bas

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,5 +1468,26 @@ SUITE( fbc_tests.structs.padding )
14681468
CU_ASSERT( x2.x1.i = 11 )
14691469
CU_ASSERT( x2.b = 22 )
14701470
END_TEST
1471+
1472+
TEST( genGccUdtPackedFieldInUdt )
1473+
'' Regression test for -gen gcc's struct emitting
1474+
1475+
type UDT1 field=1 '' packed UDT
1476+
a as byte '' 1 byte
1477+
b as short '' 2 bytes
1478+
end type
1479+
#assert sizeof( UDT1 ) = sizeof( byte ) + sizeof ( short )
1480+
#assert sizeof( UDT1 ) = 3
14711481

1482+
type UDT2 field=2 '' packed UDT
1483+
x1 as UDT1 '' UDT field, which is packed with field=1
1484+
end type
1485+
#assert sizeof( UDT2 ) = 4
1486+
#assert offsetof(UDT2, x1.b) = 1
1487+
1488+
dim x2 as UDT2 = ((11,22))
1489+
CU_ASSERT( x2.x1.a = 11 )
1490+
CU_ASSERT( x2.x1.b = 22 )
1491+
END_TEST
1492+
14721493
END_SUITE

0 commit comments

Comments
 (0)