]> granicus.if.org Git - clang/commitdiff
Add TargetInfo::useBitfieldTypeAlignment().
authorDaniel Dunbar <daniel@zuster.org>
Thu, 15 Apr 2010 06:18:39 +0000 (06:18 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Thu, 15 Apr 2010 06:18:39 +0000 (06:18 +0000)
 - Used to determine whether the alignment of the type in a bit-field is
   respected when laying out structures. The default is true, targets can
   override this as needed.

 - This is designed to correspond to the PCC_BITFIELD_TYPE_MATTERS macro in
   gcc. The AST/Sema implementation only affects one line, unless I have
   forgotten something. I'd appreciate further review.

 - IRgen still needs to be updated to fully support this (which is effectively
   PR5591).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101356 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/TargetInfo.h
lib/AST/RecordLayoutBuilder.cpp
lib/Basic/TargetInfo.cpp

index bc2cf198c3f555de0f441ee8e08aabbb0bd12148..55669faa45a91244b808f2afdff81c693eef3a33 100644 (file)
@@ -85,6 +85,14 @@ public:
 protected:
   IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType,
           WIntType, Char16Type, Char32Type, Int64Type, SigAtomicType;
+
+  /// Control whether the alignment of bit-field types is respected when laying
+  /// out structures. If true, then the alignment of the bit-field type will be
+  /// used to (a) impact the alignment of the containing structure, and (b)
+  /// ensure that the individual bit-field will not straddle an alignment
+  /// boundary.
+  unsigned UseBitfieldTypeAlignment : 1;
+
 public:
   IntType getSizeType() const { return SizeType; }
   IntType getIntMaxType() const { return IntMaxType; }
@@ -197,6 +205,10 @@ public:
     return UserLabelPrefix;
   }
 
+  bool useBitfieldTypeAlignment() const {
+    return UseBitfieldTypeAlignment;
+  }
+
   /// getTypeName - Return the user string for the specified integer type enum.
   /// For example, SignedShort -> "short".
   static const char *getTypeName(IntType T);
index bb73e7ea11eb46f1520d5486821f425869dbae7a..d312ee13fec617be40eae04b5b7cc41ee321ebd2 100644 (file)
@@ -585,7 +585,7 @@ void ASTRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
   uint64_t TypeSize = FieldInfo.first;
   unsigned FieldAlign = FieldInfo.second;
 
-  if (FieldPacked)
+  if (FieldPacked || !Ctx.Target.useBitfieldTypeAlignment())
     FieldAlign = 1;
   if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
     FieldAlign = std::max(FieldAlign, AA->getMaxAlignment());
@@ -594,12 +594,11 @@ void ASTRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
   if (MaxFieldAlignment)
     FieldAlign = std::min(FieldAlign, MaxFieldAlignment);
 
-  // Check if we need to add padding to give the field the correct
-  // alignment.
+  // Check if we need to add padding to give the field the correct alignment.
   if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
     FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
 
-  // Padding members don't affect overall alignment
+  // Padding members don't affect overall alignment.
   if (!D->getIdentifier())
     FieldAlign = 1;
 
index 136089fe90c20b7b1186d4c07a953d1dd0a46992..8b4fdee47c2152110cfb39c6369dcad2e83c480f 100644 (file)
@@ -20,9 +20,8 @@ using namespace clang;
 
 // TargetInfo Constructor.
 TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
-  // Set defaults.  Defaults are set for a 32-bit RISC platform,
-  // like PPC or SPARC.
-  // These should be overridden by concrete targets as needed.
+  // Set defaults.  Defaults are set for a 32-bit RISC platform, like PPC or
+  // SPARC.  These should be overridden by concrete targets as needed.
   TLSSupported = true;
   PointerWidth = PointerAlign = 32;
   IntWidth = IntAlign = 32;
@@ -45,6 +44,7 @@ TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
   Char32Type = UnsignedInt;
   Int64Type = SignedLongLong;
   SigAtomicType = SignedInt;
+  UseBitfieldTypeAlignment = true;
   FloatFormat = &llvm::APFloat::IEEEsingle;
   DoubleFormat = &llvm::APFloat::IEEEdouble;
   LongDoubleFormat = &llvm::APFloat::IEEEdouble;