blob: 6302cf8d1fc0f670319b67fe8f71d2195e26c882 [file] [log] [blame]
Daniel Dunbar23ee4b72010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson307846f2009-07-23 03:17:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson307846f2009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
20#include "CodeGenTypes.h"
21#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000022#include "llvm/Type.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000023#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000024#include "llvm/Support/raw_ostream.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000025#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000026using namespace clang;
27using namespace CodeGen;
28
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000029namespace clang {
30namespace CodeGen {
31
32class CGRecordLayoutBuilder {
33public:
34 /// FieldTypes - Holds the LLVM types that the struct is created from.
35 std::vector<const llvm::Type *> FieldTypes;
36
37 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
38 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
39 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
40
41 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbard4549102010-04-06 01:07:41 +000042 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000043 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
44
45 /// ContainsPointerToDataMember - Whether one of the fields in this record
46 /// layout is a pointer to data member, or a struct that contains pointer to
47 /// data member.
48 bool ContainsPointerToDataMember;
49
50 /// Packed - Whether the resulting LLVM struct will be packed or not.
51 bool Packed;
52
53private:
54 CodeGenTypes &Types;
55
56 /// Alignment - Contains the alignment of the RecordDecl.
57 //
58 // FIXME: This is not needed and should be removed.
59 unsigned Alignment;
60
61 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
62 /// LLVM types.
63 unsigned AlignmentAsLLVMStruct;
64
65 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
66 /// this will have the number of bits still available in the field.
67 char BitsAvailableInLastField;
68
69 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
70 uint64_t NextFieldOffsetInBytes;
71
Anders Carlsson1de2f572010-04-17 20:49:27 +000072 /// LayoutUnionField - Will layout a field in an union and return the type
73 /// that the field will have.
74 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
75 const ASTRecordLayout &Layout);
76
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000077 /// LayoutUnion - Will layout a union RecordDecl.
78 void LayoutUnion(const RecordDecl *D);
79
80 /// LayoutField - try to layout all fields in the record decl.
81 /// Returns false if the operation failed because the struct is not packed.
82 bool LayoutFields(const RecordDecl *D);
83
84 /// LayoutBases - layout the bases and vtable pointer of a record decl.
85 void LayoutBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout);
86
87 /// LayoutField - layout a single field. Returns false if the operation failed
88 /// because the current struct is not packed.
89 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
90
91 /// LayoutBitField - layout a single bit field.
92 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
93
94 /// AppendField - Appends a field with the given offset and type.
95 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
96
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000097 /// AppendPadding - Appends enough padding bytes so that the total
98 /// struct size is a multiple of the field alignment.
99 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
100
101 /// AppendBytes - Append a given number of bytes to the record.
102 void AppendBytes(uint64_t NumBytes);
103
104 /// AppendTailPadding - Append enough tail padding so that the type will have
105 /// the passed size.
106 void AppendTailPadding(uint64_t RecordSize);
107
108 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000109
110 /// CheckForPointerToDataMember - Check if the given type contains a pointer
111 /// to data member.
112 void CheckForPointerToDataMember(QualType T);
113
114public:
115 CGRecordLayoutBuilder(CodeGenTypes &Types)
116 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
117 Alignment(0), AlignmentAsLLVMStruct(1),
118 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
119
120 /// Layout - Will layout a RecordDecl.
121 void Layout(const RecordDecl *D);
122};
123
124}
125}
126
Anders Carlsson307846f2009-07-23 03:17:50 +0000127void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000128 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000129 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000130
Anders Carlsson697f6592009-07-23 03:43:54 +0000131 if (D->isUnion()) {
132 LayoutUnion(D);
133 return;
134 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000135
Anders Carlsson307846f2009-07-23 03:17:50 +0000136 if (LayoutFields(D))
137 return;
Mike Stump11289f42009-09-09 15:08:12 +0000138
Anders Carlsson307846f2009-07-23 03:17:50 +0000139 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000140 Packed = true;
Anders Carlsson307846f2009-07-23 03:17:50 +0000141 AlignmentAsLLVMStruct = 1;
Anders Carlssond5d64132009-07-28 17:56:36 +0000142 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000143 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000144 LLVMFields.clear();
145 LLVMBitFields.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000146
Anders Carlsson307846f2009-07-23 03:17:50 +0000147 LayoutFields(D);
148}
149
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000150static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types,
151 const FieldDecl *FD,
152 uint64_t FieldOffset,
153 uint64_t FieldSize) {
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000154 const RecordDecl *RD = FD->getParent();
Daniel Dunbarfc66e0e2010-04-22 03:17:04 +0000155 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
156 uint64_t ContainingTypeSizeInBits = RL.getSize();
157 unsigned ContainingTypeAlign = RL.getAlignment();
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000158
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000159 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000160 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
161 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000162
163 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000164
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000165 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000166 // We have a wide bit-field. The extra bits are only used for padding, so
167 // if we have a bitfield of type T, with size N:
168 //
169 // T t : N;
170 //
171 // We can just assume that it's:
172 //
173 // T t : sizeof(T);
174 //
175 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000176 }
177
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000178 // Compute the access components. The policy we use is to start by attempting
179 // to access using the width of the bit-field type itself and to always access
180 // at aligned indices of that type. If such an access would fail because it
181 // extends past the bound of the type, then we reduce size to the next smaller
182 // power of two and retry. The current algorithm assumes pow2 sized types,
183 // although this is easy to fix.
184 //
185 // FIXME: This algorithm is wrong on big-endian systems, I think.
186 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!");
187 CGBitFieldInfo::AccessInfo Components[3];
188 unsigned NumComponents = 0;
189 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed.
190 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt.
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000191
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000192 // Round down from the field offset to find the first access position that is
193 // at an aligned offset of the initial access type.
Daniel Dunbar59813772010-04-22 15:22:33 +0000194 uint64_t AccessStart = FieldOffset - (FieldOffset % AccessWidth);
195
196 // Adjust initial access size to fit within record.
197 while (AccessWidth > 8 &&
198 AccessStart + AccessWidth > ContainingTypeSizeInBits) {
199 AccessWidth >>= 1;
200 AccessStart = FieldOffset - (FieldOffset % AccessWidth);
201 }
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000202
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000203 while (AccessedTargetBits < FieldSize) {
204 // Check that we can access using a type of this size, without reading off
205 // the end of the structure. This can occur with packed structures and
206 // -fno-bitfield-type-align, for example.
207 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) {
208 // If so, reduce access size to the next smaller power-of-two and retry.
209 AccessWidth >>= 1;
210 assert(AccessWidth >= 8 && "Cannot access under byte size!");
211 continue;
212 }
Daniel Dunbarb935b932010-04-13 20:58:55 +0000213
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000214 // Otherwise, add an access component.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000215
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000216 // First, compute the bits inside this access which are part of the
217 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the
218 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits
219 // in the target that we are reading.
Daniel Dunbar59813772010-04-22 15:22:33 +0000220 assert(FieldOffset < AccessStart + AccessWidth && "Invalid access start!");
221 assert(AccessStart < FieldOffset + FieldSize && "Invalid access start!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000222 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset);
223 uint64_t AccessBitsInFieldSize =
Daniel Dunbar59813772010-04-22 15:22:33 +0000224 std::min(AccessWidth + AccessStart,
225 FieldOffset + FieldSize) - AccessBitsInFieldStart;
Daniel Dunbar5d6c07e2010-04-22 14:56:10 +0000226
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000227 assert(NumComponents < 3 && "Unexpected number of components!");
228 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++];
229 AI.FieldIndex = 0;
230 // FIXME: We still follow the old access pattern of only using the field
231 // byte offset. We should switch this once we fix the struct layout to be
232 // pretty.
233 AI.FieldByteOffset = AccessStart / 8;
234 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart;
235 AI.AccessWidth = AccessWidth;
Daniel Dunbarfc66e0e2010-04-22 03:17:04 +0000236 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000237 AI.TargetBitOffset = AccessedTargetBits;
238 AI.TargetBitWidth = AccessBitsInFieldSize;
239
240 AccessStart += AccessWidth;
241 AccessedTargetBits += AI.TargetBitWidth;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000242 }
243
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000244 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!");
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000245 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000246}
247
Anders Carlsson307846f2009-07-23 03:17:50 +0000248void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
249 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000250 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000251 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000252
Anders Carlsson307846f2009-07-23 03:17:50 +0000253 if (FieldSize == 0)
254 return;
255
Anders Carlssond5d64132009-07-28 17:56:36 +0000256 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000257 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000258
Anders Carlsson307846f2009-07-23 03:17:50 +0000259 if (FieldOffset < NextFieldOffset) {
260 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000261 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000262
Anders Carlsson307846f2009-07-23 03:17:50 +0000263 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000264 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000265 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
266 } else {
267 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
268
269 // Append padding if necessary.
270 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump11289f42009-09-09 15:08:12 +0000271
272 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000273 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000274
Anders Carlsson307846f2009-07-23 03:17:50 +0000275 assert(NumBytesToAppend && "No bytes to append!");
276 }
277
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000278 // Add the bit field info.
279 LLVMBitFields.push_back(
280 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000281
Anders Carlsson307846f2009-07-23 03:17:50 +0000282 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000283
Mike Stump11289f42009-09-09 15:08:12 +0000284 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000285 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000286}
287
288bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
289 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000290 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000291 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000292 return false;
293
294 if (D->isBitField()) {
295 // We must use packed structs for unnamed bit fields since they
296 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000297 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000298 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000299
Anders Carlsson307846f2009-07-23 03:17:50 +0000300 LayoutBitField(D, FieldOffset);
301 return true;
302 }
Mike Stump11289f42009-09-09 15:08:12 +0000303
Anders Carlssone8bfe412010-02-02 05:17:25 +0000304 // Check if we have a pointer to data member in this field.
305 CheckForPointerToDataMember(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000306
Anders Carlsson307846f2009-07-23 03:17:50 +0000307 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000308 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000309
Anders Carlsson19702bb2009-08-04 16:29:15 +0000310 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
311 unsigned TypeAlignment = getTypeAlignment(Ty);
312
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000313 // If the type alignment is larger then the struct alignment, we must use
314 // a packed struct.
315 if (TypeAlignment > Alignment) {
316 assert(!Packed && "Alignment is wrong even with packed struct!");
317 return false;
318 }
Mike Stump11289f42009-09-09 15:08:12 +0000319
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000320 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
321 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
322 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
323 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
324 return false;
325 }
326 }
327
Anders Carlsson19702bb2009-08-04 16:29:15 +0000328 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000329 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000330 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
331
332 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
333 assert(!Packed && "Could not place field even with packed struct!");
334 return false;
335 }
Mike Stump11289f42009-09-09 15:08:12 +0000336
Anders Carlsson19702bb2009-08-04 16:29:15 +0000337 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
338 // Even with alignment, the field offset is not at the right place,
339 // insert padding.
340 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump11289f42009-09-09 15:08:12 +0000341
Anders Carlsson19702bb2009-08-04 16:29:15 +0000342 AppendBytes(PaddingInBytes);
343 }
Mike Stump11289f42009-09-09 15:08:12 +0000344
Anders Carlsson307846f2009-07-23 03:17:50 +0000345 // Now append the field.
346 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000347 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000348
Anders Carlsson307846f2009-07-23 03:17:50 +0000349 return true;
350}
351
Anders Carlsson1de2f572010-04-17 20:49:27 +0000352const llvm::Type *
353CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
354 const ASTRecordLayout &Layout) {
355 if (Field->isBitField()) {
356 uint64_t FieldSize =
357 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
358
359 // Ignore zero sized bit fields.
360 if (FieldSize == 0)
361 return 0;
362
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000363 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
364 unsigned NumBytesToAppend =
365 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlsson2295f132010-04-17 21:04:52 +0000366
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000367 if (NumBytesToAppend > 1)
368 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlsson2295f132010-04-17 21:04:52 +0000369
Anders Carlsson1de2f572010-04-17 20:49:27 +0000370 // Add the bit field info.
371 LLVMBitFields.push_back(
372 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000373 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000374 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000375
Anders Carlsson1de2f572010-04-17 20:49:27 +0000376 // This is a regular union field.
377 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
378 return Types.ConvertTypeForMemRecursive(Field->getType());
379}
380
Anders Carlsson697f6592009-07-23 03:43:54 +0000381void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
382 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000383
Anders Carlsson697f6592009-07-23 03:43:54 +0000384 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000385
Anders Carlsson697f6592009-07-23 03:43:54 +0000386 const llvm::Type *Ty = 0;
387 uint64_t Size = 0;
388 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000389
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000390 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000391
Anders Carlsson697f6592009-07-23 03:43:54 +0000392 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000393 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000394 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000395 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000396 "Union field offset did not start at the beginning of record!");
Anders Carlsson1de2f572010-04-17 20:49:27 +0000397 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000398
Anders Carlsson1de2f572010-04-17 20:49:27 +0000399 if (!FieldTy)
400 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000401
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000402 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000403
Anders Carlssone2accf42009-07-23 21:52:03 +0000404 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
405 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000406
Anders Carlsson697f6592009-07-23 03:43:54 +0000407 if (FieldAlign < Align)
408 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000409
Anders Carlsson697f6592009-07-23 03:43:54 +0000410 if (FieldAlign > Align || FieldSize > Size) {
411 Ty = FieldTy;
412 Align = FieldAlign;
413 Size = FieldSize;
414 }
415 }
Mike Stump11289f42009-09-09 15:08:12 +0000416
Anders Carlsson697f6592009-07-23 03:43:54 +0000417 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000418 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000419 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000420
421 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
422 // We need a packed struct.
423 Packed = true;
424 Align = 1;
425 }
426 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000427 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000428 assert(HasOnlyZeroSizedBitFields &&
429 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000430 Align = 1;
431 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000432
Anders Carlsson697f6592009-07-23 03:43:54 +0000433 // Append tail padding.
434 if (Layout.getSize() / 8 > Size)
435 AppendPadding(Layout.getSize() / 8, Align);
436}
437
Anders Carlssond681a292009-12-16 17:27:20 +0000438void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
439 const ASTRecordLayout &Layout) {
440 // Check if we need to add a vtable pointer.
441 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000442 const llvm::Type *Int8PtrTy =
Anders Carlssond681a292009-12-16 17:27:20 +0000443 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000444
Anders Carlssond681a292009-12-16 17:27:20 +0000445 assert(NextFieldOffsetInBytes == 0 &&
Anders Carlsson11e51402010-04-17 20:15:18 +0000446 "VTable pointer must come first!");
Anders Carlssond681a292009-12-16 17:27:20 +0000447 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
448 }
449}
450
Anders Carlsson307846f2009-07-23 03:17:50 +0000451bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
452 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000453 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000454
Anders Carlsson697f6592009-07-23 03:43:54 +0000455 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000456
Anders Carlssond681a292009-12-16 17:27:20 +0000457 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
458 LayoutBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000459
Anders Carlsson307846f2009-07-23 03:17:50 +0000460 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000461
Mike Stump11289f42009-09-09 15:08:12 +0000462 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000463 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
464 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000465 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000466 "Could not layout fields even with a packed LLVM struct!");
467 return false;
468 }
469 }
470
471 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000472 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000473
Anders Carlsson307846f2009-07-23 03:17:50 +0000474 return true;
475}
476
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000477void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
478 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000479
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000480 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000481 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000482
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000483 uint64_t AlignedNextFieldOffset =
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000484 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
485
486 if (AlignedNextFieldOffset == RecordSizeInBytes) {
487 // We don't need any padding.
488 return;
489 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000490
Anders Carlssond5d64132009-07-28 17:56:36 +0000491 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000492 AppendBytes(NumPadBytes);
493}
494
Mike Stump11289f42009-09-09 15:08:12 +0000495void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000496 const llvm::Type *FieldTy) {
497 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
498 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000499
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000500 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000501
Anders Carlsson307846f2009-07-23 03:17:50 +0000502 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000503
Anders Carlssond5d64132009-07-28 17:56:36 +0000504 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000505 BitsAvailableInLastField = 0;
506}
507
Mike Stump11289f42009-09-09 15:08:12 +0000508void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000509 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000510 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
511 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000512
Anders Carlsson307846f2009-07-23 03:17:50 +0000513 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000514 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson307846f2009-07-23 03:17:50 +0000515 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
516
517 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
518 // Even with alignment, the field offset is not at the right place,
519 // insert padding.
520 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
521
522 AppendBytes(PaddingInBytes);
523 }
524}
525
526void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
527 if (NumBytes == 0)
528 return;
Mike Stump11289f42009-09-09 15:08:12 +0000529
Owen Anderson41a75022009-08-13 21:57:51 +0000530 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000531 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000532 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000533
Anders Carlsson307846f2009-07-23 03:17:50 +0000534 // Append the padding field
Anders Carlssond5d64132009-07-28 17:56:36 +0000535 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000536}
537
538unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000539 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000540 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000541
Anders Carlsson307846f2009-07-23 03:17:50 +0000542 return Types.getTargetData().getABITypeAlignment(Ty);
543}
544
Anders Carlssone8bfe412010-02-02 05:17:25 +0000545void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000546 // This record already contains a member pointer.
Anders Carlssone8bfe412010-02-02 05:17:25 +0000547 if (ContainsPointerToDataMember)
Anders Carlssond606de72009-08-23 01:25:01 +0000548 return;
Mike Stump11289f42009-09-09 15:08:12 +0000549
Anders Carlssond606de72009-08-23 01:25:01 +0000550 // Can only have member pointers if we're compiling C++.
551 if (!Types.getContext().getLangOptions().CPlusPlus)
552 return;
Mike Stump11289f42009-09-09 15:08:12 +0000553
Anders Carlssone8bfe412010-02-02 05:17:25 +0000554 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000555
Anders Carlssone8bfe412010-02-02 05:17:25 +0000556 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
557 if (!MPT->getPointeeType()->isFunctionType()) {
558 // We have a pointer to data member.
559 ContainsPointerToDataMember = true;
560 }
561 } else if (const RecordType *RT = T->getAs<RecordType>()) {
562 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000563
Anders Carlssone8bfe412010-02-02 05:17:25 +0000564 // FIXME: It would be better if there was a way to explicitly compute the
565 // record layout instead of converting to a type.
566 Types.ConvertTagDeclType(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000567
Anders Carlssone8bfe412010-02-02 05:17:25 +0000568 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000569
Anders Carlssone8bfe412010-02-02 05:17:25 +0000570 if (Layout.containsPointerToDataMember())
571 ContainsPointerToDataMember = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000572 }
Anders Carlssond606de72009-08-23 01:25:01 +0000573}
574
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000575CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
576 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000577
Anders Carlsson307846f2009-07-23 03:17:50 +0000578 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000579
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000580 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson758428f2009-08-05 23:18:46 +0000581 Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000582 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000583
Daniel Dunbar034299e2010-03-31 01:09:11 +0000584 CGRecordLayout *RL =
585 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
586
Anders Carlsson307846f2009-07-23 03:17:50 +0000587 // Add all the field numbers.
Daniel Dunbard4549102010-04-06 01:07:41 +0000588 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
589 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson307846f2009-07-23 03:17:50 +0000590
591 // Add bitfield info.
Daniel Dunbard4549102010-04-06 01:07:41 +0000592 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
593 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000594
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000595 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000596 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000597 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000598 llvm::errs() << "Record: ";
599 D->dump();
600 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000601 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000602 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000603
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000604#ifndef NDEBUG
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000605 // Verify that the computed LLVM struct size matches the AST layout size.
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000606 uint64_t TypeSizeInBits = getContext().getASTRecordLayout(D).getSize();
607 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000608 "Type size mismatch!");
609
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000610 // Verify that the LLVM and AST field offsets agree.
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000611 const llvm::StructType *ST =
612 dyn_cast<llvm::StructType>(RL->getLLVMType());
613 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
614
615 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
616 RecordDecl::field_iterator it = D->field_begin();
617 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
618 const FieldDecl *FD = *it;
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000619
620 // For non-bit-fields, just check that the LLVM struct offset matches the
621 // AST offset.
622 if (!FD->isBitField()) {
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000623 unsigned FieldNo = RL->getLLVMFieldNo(FD);
624 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
625 "Invalid field offset!");
Daniel Dunbar488f55c2010-04-22 02:35:46 +0000626 continue;
627 }
628
629 // Ignore unnamed bit-fields.
630 if (!FD->getDeclName())
631 continue;
632
633 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD);
634 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) {
635 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i);
636
637 // Verify that every component access is within the structure.
638 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex);
639 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8;
640 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits &&
641 "Invalid bit-field access (out of range)!");
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000642 }
643 }
644#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000645
Daniel Dunbar034299e2010-03-31 01:09:11 +0000646 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000647}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000648
649void CGRecordLayout::print(llvm::raw_ostream &OS) const {
650 OS << "<CGRecordLayout\n";
651 OS << " LLVMType:" << *LLVMType << "\n";
652 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
653 OS << " BitFields:[\n";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000654
655 // Print bit-field infos in declaration order.
656 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000657 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
658 it = BitFields.begin(), ie = BitFields.end();
659 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000660 const RecordDecl *RD = it->first->getParent();
661 unsigned Index = 0;
662 for (RecordDecl::field_iterator
663 it2 = RD->field_begin(); *it2 != it->first; ++it2)
664 ++Index;
665 BFIs.push_back(std::make_pair(Index, &it->second));
666 }
667 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
668 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000669 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000670 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000671 OS << "\n";
672 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000673
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000674 OS << "]>\n";
675}
676
677void CGRecordLayout::dump() const {
678 print(llvm::errs());
679}
680
681void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
682 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000683 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000684 OS << " IsSigned:" << IsSigned << "\n";
685
686 OS.indent(4 + strlen("<CGBitFieldInfo"));
687 OS << " NumComponents:" << getNumComponents();
688 OS << " Components: [";
689 if (getNumComponents()) {
690 OS << "\n";
691 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
692 const AccessInfo &AI = getComponent(i);
693 OS.indent(8);
694 OS << "<AccessInfo"
695 << " FieldIndex:" << AI.FieldIndex
696 << " FieldByteOffset:" << AI.FieldByteOffset
697 << " FieldBitStart:" << AI.FieldBitStart
698 << " AccessWidth:" << AI.AccessWidth << "\n";
699 OS.indent(8 + strlen("<AccessInfo"));
700 OS << " AccessAlignment:" << AI.AccessAlignment
701 << " TargetBitOffset:" << AI.TargetBitOffset
702 << " TargetBitWidth:" << AI.TargetBitWidth
703 << ">\n";
704 }
705 OS.indent(4);
706 }
707 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000708}
709
710void CGBitFieldInfo::dump() const {
711 print(llvm::errs());
712}