[PATCH] D21489: [codeview] Added support for bitfield type

David Majnemer via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 18 10:44:43 PDT 2016


majnemer added inline comments.

================
Comment at: lib/CodeGen/AsmPrinter/CodeViewDebug.cpp:1449-1462
@@ -1419,5 +1448,16 @@
 
+    uint64_t Size = Member->getSizeInBits();
+    uint64_t FieldSize = getBaseTypeSize(Member);
     uint64_t OffsetInBytes = MemberInfo.BaseOffset;
 
-    // FIXME: Handle bitfield type memeber.
-    OffsetInBytes += Member->getOffsetInBits() / 8;
+    if (Size != FieldSize) {
+      uint64_t Offset = Member->getOffsetInBits();
+      uint64_t AlignMask = ~(Member->getAlignInBits() - 1);
+      uint64_t HiMark = (Offset + FieldSize) & AlignMask;
+      uint64_t FieldOffset = (HiMark - FieldSize);
+      Offset -= FieldOffset;
+
+      MemberBaseType = TypeTable.writeBitField(
+          BitFieldRecord(MemberBaseType, (uint8_t)Size, (uint8_t)Offset));
+      OffsetInBytes += FieldOffset / 8;
+    } else
----------------
Inferring the properties of the bitfield is problematic, consider:

```
#pragma pack(1)
struct S {
  char x;
  int y : 2;
  int z : 2;
};
S s;
```

You will give back:
>   BitField (0x1001) {
>     TypeLeafKind: LF_BITFIELD (0x1205)
>     Type: int (0x74)
>     BitSize: 2
>     BitOffset: 8
>   }
>   BitField (0x1002) {
>     TypeLeafKind: LF_BITFIELD (0x1205)
>     Type: int (0x74)
>     BitSize: 2
>     BitOffset: 10
>   }
>   UnknownLeaf (0x1003) {
>     TypeLeafKind: LF_FIELDLIST (0x1203)
>     DataMember {
>       AccessSpecifier: Public (0x3)
>       Type: char (0x70)
>       FieldOffset: 0x0
>       Name: x
>     }
>     DataMember {
>       AccessSpecifier: Public (0x3)
>       Type: 0x1001
>       FieldOffset: 0x0
>       Name: y
>     }
>     DataMember {
>       AccessSpecifier: Public (0x3)
>       Type: 0x1002
>       FieldOffset: 0x0
>       Name: z
>     }
>   }



I would think the correct output to be:
>   BitField (0x1001) {
>     TypeLeafKind: LF_BITFIELD (0x1205)
>     Type: int (0x74)
>     BitSize: 2
>     BitOffset: 0
>   }
>   BitField (0x1002) {
>     TypeLeafKind: LF_BITFIELD (0x1205)
>     Type: int (0x74)
>     BitSize: 2
>     BitOffset: 2
>   }
>   UnknownLeaf (0x1003) {
>     TypeLeafKind: LF_FIELDLIST (0x1203)
>     DataMember {
>       AccessSpecifier: Public (0x3)
>       Type: char (0x70)
>       FieldOffset: 0x0
>       Name: x
>     }
>     DataMember {
>       AccessSpecifier: Public (0x3)
>       Type: 0x1001
>       FieldOffset: 0x4
>       Name: y
>     }
>     DataMember {
>       AccessSpecifier: Public (0x3)
>       Type: 0x1002
>       FieldOffset: 0x4
>       Name: z
>     }
>   }

Perhaps we need to encode the storage offset for the bitfield in the metadata?


http://reviews.llvm.org/D21489





More information about the llvm-commits mailing list