blob: 71dca504c59e1dd1225fbca9038371b35836235b [file] [log] [blame]
Daniel Dunbar270e2032010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson45372a62009-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 Dunbar270e2032010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson45372a62009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar2924ade2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson45372a62009-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 Dunbar93c62962010-04-12 18:14:18 +000022#include "llvm/Type.h"
23#include "llvm/Support/raw_ostream.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000024#include "llvm/Target/TargetData.h"
Anders Carlsson45372a62009-07-23 03:17:50 +000025using namespace clang;
26using namespace CodeGen;
27
Daniel Dunbar270e2032010-03-31 00:11:27 +000028namespace clang {
29namespace CodeGen {
30
31class CGRecordLayoutBuilder {
32public:
33 /// FieldTypes - Holds the LLVM types that the struct is created from.
34 std::vector<const llvm::Type *> FieldTypes;
35
36 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
37 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
38 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
39
40 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +000041 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar270e2032010-03-31 00:11:27 +000042 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
43
44 /// ContainsPointerToDataMember - Whether one of the fields in this record
45 /// layout is a pointer to data member, or a struct that contains pointer to
46 /// data member.
47 bool ContainsPointerToDataMember;
48
49 /// Packed - Whether the resulting LLVM struct will be packed or not.
50 bool Packed;
51
52private:
53 CodeGenTypes &Types;
54
55 /// Alignment - Contains the alignment of the RecordDecl.
56 //
57 // FIXME: This is not needed and should be removed.
58 unsigned Alignment;
59
60 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
61 /// LLVM types.
62 unsigned AlignmentAsLLVMStruct;
63
64 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
65 /// this will have the number of bits still available in the field.
66 char BitsAvailableInLastField;
67
68 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
69 uint64_t NextFieldOffsetInBytes;
70
Anders Carlsson86664462010-04-17 20:49:27 +000071 /// LayoutUnionField - Will layout a field in an union and return the type
72 /// that the field will have.
73 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
74 const ASTRecordLayout &Layout);
75
Daniel Dunbar270e2032010-03-31 00:11:27 +000076 /// LayoutUnion - Will layout a union RecordDecl.
77 void LayoutUnion(const RecordDecl *D);
78
79 /// LayoutField - try to layout all fields in the record decl.
80 /// Returns false if the operation failed because the struct is not packed.
81 bool LayoutFields(const RecordDecl *D);
82
83 /// LayoutBases - layout the bases and vtable pointer of a record decl.
84 void LayoutBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout);
85
86 /// LayoutField - layout a single field. Returns false if the operation failed
87 /// because the current struct is not packed.
88 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
89
90 /// LayoutBitField - layout a single bit field.
91 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
92
93 /// AppendField - Appends a field with the given offset and type.
94 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
95
96 /// AppendPadding - Appends enough padding bytes so that the total struct
97 /// size matches the alignment of the passed in type.
98 void AppendPadding(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
99
100 /// AppendPadding - Appends enough padding bytes so that the total
101 /// struct size is a multiple of the field alignment.
102 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
103
104 /// AppendBytes - Append a given number of bytes to the record.
105 void AppendBytes(uint64_t NumBytes);
106
107 /// AppendTailPadding - Append enough tail padding so that the type will have
108 /// the passed size.
109 void AppendTailPadding(uint64_t RecordSize);
110
111 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000112
113 /// CheckForPointerToDataMember - Check if the given type contains a pointer
114 /// to data member.
115 void CheckForPointerToDataMember(QualType T);
116
117public:
118 CGRecordLayoutBuilder(CodeGenTypes &Types)
119 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
120 Alignment(0), AlignmentAsLLVMStruct(1),
121 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
122
123 /// Layout - Will layout a RecordDecl.
124 void Layout(const RecordDecl *D);
125};
126
127}
128}
129
Anders Carlsson45372a62009-07-23 03:17:50 +0000130void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000131 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000132 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000133
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000134 if (D->isUnion()) {
135 LayoutUnion(D);
136 return;
137 }
Anders Carlssona860e752009-08-08 18:23:56 +0000138
Anders Carlsson45372a62009-07-23 03:17:50 +0000139 if (LayoutFields(D))
140 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Anders Carlsson45372a62009-07-23 03:17:50 +0000142 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000143 Packed = true;
Anders Carlsson45372a62009-07-23 03:17:50 +0000144 AlignmentAsLLVMStruct = 1;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000145 NextFieldOffsetInBytes = 0;
Anders Carlsson45372a62009-07-23 03:17:50 +0000146 FieldTypes.clear();
Anders Carlsson45372a62009-07-23 03:17:50 +0000147 LLVMFields.clear();
148 LLVMBitFields.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Anders Carlsson45372a62009-07-23 03:17:50 +0000150 LayoutFields(D);
151}
152
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000153static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types,
154 const FieldDecl *FD,
155 uint64_t FieldOffset,
156 uint64_t FieldSize) {
157 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarab970f92010-04-13 20:58:55 +0000158 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
159 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000160
161 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000162
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000163 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000164 // We have a wide bit-field. The extra bits are only used for padding, so
165 // if we have a bitfield of type T, with size N:
166 //
167 // T t : N;
168 //
169 // We can just assume that it's:
170 //
171 // T t : sizeof(T);
172 //
173 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000174 }
175
176 unsigned StartBit = FieldOffset % TypeSizeInBits;
177
Daniel Dunbarab970f92010-04-13 20:58:55 +0000178 // The current policy is to always access the bit-field using the source type
179 // of the bit-field. With the C bit-field rules, this implies that we always
180 // use either one or two accesses, and two accesses can only occur with a
181 // packed structure when the bit-field straddles an alignment boundary.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000182 CGBitFieldInfo::AccessInfo Components[2];
183
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000184 unsigned LowBits = std::min(FieldSize, TypeSizeInBits - StartBit);
Daniel Dunbarab970f92010-04-13 20:58:55 +0000185 bool NeedsHighAccess = LowBits != FieldSize;
Daniel Dunbar2df25692010-04-15 05:09:32 +0000186 unsigned NumComponents = 1 + NeedsHighAccess;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000187
188 // FIXME: This access policy is probably wrong on big-endian systems.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000189 CGBitFieldInfo::AccessInfo &LowAccess = Components[0];
Daniel Dunbarab970f92010-04-13 20:58:55 +0000190 LowAccess.FieldIndex = 0;
191 LowAccess.FieldByteOffset =
192 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000193 LowAccess.FieldBitStart = StartBit;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000194 LowAccess.AccessWidth = TypeSizeInBits;
195 // FIXME: This might be wrong!
196 LowAccess.AccessAlignment = 0;
197 LowAccess.TargetBitOffset = 0;
198 LowAccess.TargetBitWidth = LowBits;
199
200 if (NeedsHighAccess) {
Daniel Dunbar2df25692010-04-15 05:09:32 +0000201 CGBitFieldInfo::AccessInfo &HighAccess = Components[1];
Daniel Dunbarab970f92010-04-13 20:58:55 +0000202 HighAccess.FieldIndex = 0;
203 HighAccess.FieldByteOffset = LowAccess.FieldByteOffset + TypeSizeInBytes;
204 HighAccess.FieldBitStart = 0;
205 HighAccess.AccessWidth = TypeSizeInBits;
206 // FIXME: This might be wrong!
207 HighAccess.AccessAlignment = 0;
208 HighAccess.TargetBitOffset = LowBits;
209 HighAccess.TargetBitWidth = FieldSize - LowBits;
210 }
211
Daniel Dunbar2df25692010-04-15 05:09:32 +0000212 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000213}
214
Anders Carlsson45372a62009-07-23 03:17:50 +0000215void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
216 uint64_t FieldOffset) {
Mike Stump1eb44332009-09-09 15:08:12 +0000217 uint64_t FieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000218 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Anders Carlsson45372a62009-07-23 03:17:50 +0000220 if (FieldSize == 0)
221 return;
222
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000223 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000224 unsigned NumBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Anders Carlsson45372a62009-07-23 03:17:50 +0000226 if (FieldOffset < NextFieldOffset) {
227 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000228 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Anders Carlsson45372a62009-07-23 03:17:50 +0000230 // The bitfield begins in the previous bit-field.
Mike Stump1eb44332009-09-09 15:08:12 +0000231 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000232 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
233 } else {
234 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
235
236 // Append padding if necessary.
237 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump1eb44332009-09-09 15:08:12 +0000238
239 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000240 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Anders Carlsson45372a62009-07-23 03:17:50 +0000242 assert(NumBytesToAppend && "No bytes to append!");
243 }
244
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000245 // Add the bit field info.
246 LLVMBitFields.push_back(
247 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Anders Carlsson45372a62009-07-23 03:17:50 +0000249 AppendBytes(NumBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Mike Stump1eb44332009-09-09 15:08:12 +0000251 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000252 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000253}
254
255bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
256 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000257 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000258 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000259 return false;
260
261 if (D->isBitField()) {
262 // We must use packed structs for unnamed bit fields since they
263 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000264 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000265 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Anders Carlsson45372a62009-07-23 03:17:50 +0000267 LayoutBitField(D, FieldOffset);
268 return true;
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Anders Carlsson2c12d032010-02-02 05:17:25 +0000271 // Check if we have a pointer to data member in this field.
272 CheckForPointerToDataMember(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000273
Anders Carlsson45372a62009-07-23 03:17:50 +0000274 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000275 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000277 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
278 unsigned TypeAlignment = getTypeAlignment(Ty);
279
Anders Carlssona5dd7222009-08-08 19:38:24 +0000280 // If the type alignment is larger then the struct alignment, we must use
281 // a packed struct.
282 if (TypeAlignment > Alignment) {
283 assert(!Packed && "Alignment is wrong even with packed struct!");
284 return false;
285 }
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Anders Carlssona5dd7222009-08-08 19:38:24 +0000287 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
288 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
289 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
290 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
291 return false;
292 }
293 }
294
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000295 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000296 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000297 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
298
299 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
300 assert(!Packed && "Could not place field even with packed struct!");
301 return false;
302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000304 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
305 // Even with alignment, the field offset is not at the right place,
306 // insert padding.
307 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000309 AppendBytes(PaddingInBytes);
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Anders Carlsson45372a62009-07-23 03:17:50 +0000312 // Now append the field.
313 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000314 AppendField(FieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Anders Carlsson45372a62009-07-23 03:17:50 +0000316 return true;
317}
318
Anders Carlsson86664462010-04-17 20:49:27 +0000319const llvm::Type *
320CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
321 const ASTRecordLayout &Layout) {
322 if (Field->isBitField()) {
323 uint64_t FieldSize =
324 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
325
326 // Ignore zero sized bit fields.
327 if (FieldSize == 0)
328 return 0;
329
Anders Carlssond62328e2010-04-17 21:04:52 +0000330 const llvm::Type *FieldTy;
331
332 if (!Field->getDeclName()) {
333 // This is an unnamed bit-field, which shouldn't affect alignment on the
334 // struct so we use an array of bytes for it.
335
336 FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
337
338 unsigned NumBytesToAppend =
339 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
340
341 if (NumBytesToAppend > 1)
342 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
343 } else
344 FieldTy = Types.ConvertTypeForMemRecursive(Field->getType());
345
Anders Carlsson86664462010-04-17 20:49:27 +0000346 // Add the bit field info.
347 LLVMBitFields.push_back(
348 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000349 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000350 }
351
352 // This is a regular union field.
353 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
354 return Types.ConvertTypeForMemRecursive(Field->getType());
355}
356
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000357void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
358 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000360 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000362 const llvm::Type *Ty = 0;
363 uint64_t Size = 0;
364 unsigned Align = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000366 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000367
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000368 unsigned FieldNo = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000369 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000370 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump1eb44332009-09-09 15:08:12 +0000371 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000372 "Union field offset did not start at the beginning of record!");
Anders Carlsson86664462010-04-17 20:49:27 +0000373 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000374
Anders Carlsson86664462010-04-17 20:49:27 +0000375 if (!FieldTy)
376 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000378 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000379
Anders Carlsson177d4d82009-07-23 21:52:03 +0000380 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
381 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000383 if (FieldAlign < Align)
384 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000386 if (FieldAlign > Align || FieldSize > Size) {
387 Ty = FieldTy;
388 Align = FieldAlign;
389 Size = FieldSize;
390 }
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000393 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000394 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000395 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000396
397 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
398 // We need a packed struct.
399 Packed = true;
400 Align = 1;
401 }
402 }
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000403 if (!Align) {
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000404 assert(HasOnlyZeroSizedBitFields &&
405 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000406 Align = 1;
407 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000408
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000409 // Append tail padding.
410 if (Layout.getSize() / 8 > Size)
411 AppendPadding(Layout.getSize() / 8, Align);
412}
413
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000414void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
415 const ASTRecordLayout &Layout) {
416 // Check if we need to add a vtable pointer.
417 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar270e2032010-03-31 00:11:27 +0000418 const llvm::Type *Int8PtrTy =
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000419 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000420
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000421 assert(NextFieldOffsetInBytes == 0 &&
Anders Carlsson046c2942010-04-17 20:15:18 +0000422 "VTable pointer must come first!");
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000423 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
424 }
425}
426
Anders Carlsson45372a62009-07-23 03:17:50 +0000427bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
428 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000429 assert(Alignment && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000431 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000433 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
434 LayoutBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000435
Anders Carlsson45372a62009-07-23 03:17:50 +0000436 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000437
Mike Stump1eb44332009-09-09 15:08:12 +0000438 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000439 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
440 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000441 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000442 "Could not layout fields even with a packed LLVM struct!");
443 return false;
444 }
445 }
446
447 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000448 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Anders Carlsson45372a62009-07-23 03:17:50 +0000450 return true;
451}
452
Anders Carlssonc1efe362009-07-27 14:55:54 +0000453void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
454 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Anders Carlssonc1efe362009-07-27 14:55:54 +0000456 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000457 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Daniel Dunbar270e2032010-03-31 00:11:27 +0000459 uint64_t AlignedNextFieldOffset =
Anders Carlssonc2456822009-12-08 01:24:23 +0000460 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
461
462 if (AlignedNextFieldOffset == RecordSizeInBytes) {
463 // We don't need any padding.
464 return;
465 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000466
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000467 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000468 AppendBytes(NumPadBytes);
469}
470
Mike Stump1eb44332009-09-09 15:08:12 +0000471void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000472 const llvm::Type *FieldTy) {
473 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
474 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000475
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000476 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000477
Anders Carlsson45372a62009-07-23 03:17:50 +0000478 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000479
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000480 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000481 BitsAvailableInLastField = 0;
482}
483
Mike Stump1eb44332009-09-09 15:08:12 +0000484void
Anders Carlsson45372a62009-07-23 03:17:50 +0000485CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
486 const llvm::Type *FieldTy) {
487 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
488}
489
Mike Stump1eb44332009-09-09 15:08:12 +0000490void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000491 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000492 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
493 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Anders Carlsson45372a62009-07-23 03:17:50 +0000495 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000496 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson45372a62009-07-23 03:17:50 +0000497 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
498
499 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
500 // Even with alignment, the field offset is not at the right place,
501 // insert padding.
502 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
503
504 AppendBytes(PaddingInBytes);
505 }
506}
507
508void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
509 if (NumBytes == 0)
510 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Owen Anderson0032b272009-08-13 21:57:51 +0000512 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000513 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000514 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Anders Carlsson45372a62009-07-23 03:17:50 +0000516 // Append the padding field
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000517 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000518}
519
520unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000521 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000522 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Anders Carlsson45372a62009-07-23 03:17:50 +0000524 return Types.getTargetData().getABITypeAlignment(Ty);
525}
526
Anders Carlsson2c12d032010-02-02 05:17:25 +0000527void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000528 // This record already contains a member pointer.
Anders Carlsson2c12d032010-02-02 05:17:25 +0000529 if (ContainsPointerToDataMember)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000530 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000532 // Can only have member pointers if we're compiling C++.
533 if (!Types.getContext().getLangOptions().CPlusPlus)
534 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Anders Carlsson2c12d032010-02-02 05:17:25 +0000536 T = Types.getContext().getBaseElementType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Anders Carlsson2c12d032010-02-02 05:17:25 +0000538 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
539 if (!MPT->getPointeeType()->isFunctionType()) {
540 // We have a pointer to data member.
541 ContainsPointerToDataMember = true;
542 }
543 } else if (const RecordType *RT = T->getAs<RecordType>()) {
544 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000545
Anders Carlsson2c12d032010-02-02 05:17:25 +0000546 // FIXME: It would be better if there was a way to explicitly compute the
547 // record layout instead of converting to a type.
548 Types.ConvertTagDeclType(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000549
Anders Carlsson2c12d032010-02-02 05:17:25 +0000550 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000551
Anders Carlsson2c12d032010-02-02 05:17:25 +0000552 if (Layout.containsPointerToDataMember())
553 ContainsPointerToDataMember = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000554 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000555}
556
Daniel Dunbar270e2032010-03-31 00:11:27 +0000557CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
558 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Anders Carlsson45372a62009-07-23 03:17:50 +0000560 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000561
Daniel Dunbar270e2032010-03-31 00:11:27 +0000562 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000563 Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000564 Builder.Packed);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000565 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
566 getTargetData().getTypeAllocSize(Ty) &&
Anders Carlssondf31e092009-08-08 18:01:57 +0000567 "Type size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000569 CGRecordLayout *RL =
570 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
571
Anders Carlsson45372a62009-07-23 03:17:50 +0000572 // Add all the field numbers.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000573 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
574 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson45372a62009-07-23 03:17:50 +0000575
576 // Add bitfield info.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000577 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
578 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Daniel Dunbarab970f92010-04-13 20:58:55 +0000580 if (getContext().getLangOptions().DumpRecordLayouts) {
581 llvm::errs() << "\n*** Dumping Record Layout\n";
582 llvm::errs() << "Record: ";
583 D->dump();
584 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000585 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000586 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000587
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000588 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000589}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000590
591void CGRecordLayout::print(llvm::raw_ostream &OS) const {
592 OS << "<CGRecordLayout\n";
593 OS << " LLVMType:" << *LLVMType << "\n";
594 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
595 OS << " BitFields:[\n";
596 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
597 it = BitFields.begin(), ie = BitFields.end();
598 it != ie; ++it) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000599 OS.indent(4);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000600 it->second.print(OS);
601 OS << "\n";
602 }
603 OS << "]>\n";
604}
605
606void CGRecordLayout::dump() const {
607 print(llvm::errs());
608}
609
610void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
611 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000612 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000613 OS << " IsSigned:" << IsSigned << "\n";
614
615 OS.indent(4 + strlen("<CGBitFieldInfo"));
616 OS << " NumComponents:" << getNumComponents();
617 OS << " Components: [";
618 if (getNumComponents()) {
619 OS << "\n";
620 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
621 const AccessInfo &AI = getComponent(i);
622 OS.indent(8);
623 OS << "<AccessInfo"
624 << " FieldIndex:" << AI.FieldIndex
625 << " FieldByteOffset:" << AI.FieldByteOffset
626 << " FieldBitStart:" << AI.FieldBitStart
627 << " AccessWidth:" << AI.AccessWidth << "\n";
628 OS.indent(8 + strlen("<AccessInfo"));
629 OS << " AccessAlignment:" << AI.AccessAlignment
630 << " TargetBitOffset:" << AI.TargetBitOffset
631 << " TargetBitWidth:" << AI.TargetBitWidth
632 << ">\n";
633 }
634 OS.indent(4);
635 }
636 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000637}
638
639void CGBitFieldInfo::dump() const {
640 print(llvm::errs());
641}