diff options
author | Daniel Dunbar <daniel@zuster.org> | 2008-10-16 02:34:03 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2008-10-16 02:34:03 +0000 |
commit | 3b0db908ebd07eaa26bc90deba5e826de00fe515 (patch) | |
tree | 2316573acc7083d4f2a9a4aecdf8beb50bab7713 /lib/AST/ASTContext.cpp | |
parent | dc914c876cd2a4308c81a9bb0ac07033f2117c23 (diff) |
Implement #pragma pack use in structure packing. The general approach
is to encode the state of the #pragma pack stack as an attribute when
the structure is declared.
- Extend PackedAttr to take an alignment (in bits), and reuse for
both __attribute__((packed)) (which takes no argument, instead
packing tightly (to "minimize the memory required") and for #pragma
pack (which allows specification of the maximum alignment in
bytes). __attribute__((packed)) is just encoded as Alignment=1.
This conflates two related but different mechanisms, but it didn't
seem worth another attribute.
- I have attempted to follow the MSVC semantics as opposed to the gcc
ones, since if I understand correctly #pragma pack originated with
MSVC. The semantics are generally equivalent except when the stack
is altered during the definition of a structure; its not clear if
anyone does this in practice. See testcase if curious.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57623 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/AST/ASTContext.cpp')
-rw-r--r-- | lib/AST/ASTContext.cpp | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 572e003dd7..e8d7978bca 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -371,12 +371,17 @@ ASTContext::getTypeInfo(QualType T) { /// LayoutField - Field layout. void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, - bool IsUnion, bool StructIsPacked, + bool IsUnion, unsigned StructPacking, ASTContext &Context) { - bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>(); + unsigned FieldPacking = StructPacking; uint64_t FieldOffset = IsUnion ? 0 : Size; uint64_t FieldSize; unsigned FieldAlign; + + // FIXME: Should this override struct packing? Probably we want to + // take the minimum? + if (const PackedAttr *PA = FD->getAttr<PackedAttr>()) + FieldPacking = PA->getAlignment(); if (const Expr *BitWidthExpr = FD->getBitWidth()) { // TODO: Need to check this algorithm on other targets! @@ -388,9 +393,14 @@ void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, Context.getTypeInfo(FD->getType()); uint64_t TypeSize = FieldInfo.first; + // Determine the alignment of this bitfield. The packing + // attributes define a maximum and the alignment attribute defines + // a minimum. + // FIXME: What is the right behavior when the specified alignment + // is smaller than the specified packing? FieldAlign = FieldInfo.second; - if (FieldIsPacked) - FieldAlign = 1; + if (FieldPacking) + FieldAlign = std::min(FieldAlign, FieldPacking); if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) FieldAlign = std::max(FieldAlign, AA->getAlignment()); @@ -418,8 +428,15 @@ void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, FieldAlign = FieldInfo.second; } - if (FieldIsPacked) - FieldAlign = 8; + // Determine the alignment of this bitfield. The packing + // attributes define a maximum and the alignment attribute defines + // a minimum. Additionally, the packing alignment must be at least + // a byte for non-bitfields. + // + // FIXME: What is the right behavior when the specified alignment + // is smaller than the specified packing? + if (FieldPacking) + FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking)); if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) FieldAlign = std::max(FieldAlign, AA->getAlignment()); @@ -470,7 +487,9 @@ ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { } Entry = NewEntry; - bool IsPacked = D->getAttr<PackedAttr>(); + unsigned StructPacking = 0; + if (const PackedAttr *PA = D->getAttr<PackedAttr>()) + StructPacking = PA->getAlignment(); if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), @@ -481,7 +500,7 @@ ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(), IVE = D->ivar_end(); IVI != IVE; ++IVI) { const ObjCIvarDecl* Ivar = (*IVI); - NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this); + NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this); } // Finally, round the size of the total struct up to the alignment of the @@ -507,9 +526,12 @@ const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { Entry = NewEntry; NewEntry->InitializeLayout(D->getNumMembers()); - bool StructIsPacked = D->getAttr<PackedAttr>(); bool IsUnion = D->isUnion(); + unsigned StructPacking = 0; + if (const PackedAttr *PA = D->getAttr<PackedAttr>()) + StructPacking = PA->getAlignment(); + if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), AA->getAlignment())); @@ -518,7 +540,7 @@ const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { // the future, this will need to be tweakable by targets. for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) { const FieldDecl *FD = D->getMember(i); - NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this); + NewEntry->LayoutField(FD, i, IsUnion, StructPacking, *this); } // Finally, round the size of the total struct up to the alignment of the |