blob: f056314221a8d52c9626d5e440d2b947be7da225 [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
Daniel Dunbar270e2032010-03-31 00:11:27 +000096 /// AppendPadding - Appends enough padding bytes so that the total
97 /// struct size is a multiple of the field alignment.
98 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
99
100 /// AppendBytes - Append a given number of bytes to the record.
101 void AppendBytes(uint64_t NumBytes);
102
103 /// AppendTailPadding - Append enough tail padding so that the type will have
104 /// the passed size.
105 void AppendTailPadding(uint64_t RecordSize);
106
107 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000108
109 /// CheckForPointerToDataMember - Check if the given type contains a pointer
110 /// to data member.
111 void CheckForPointerToDataMember(QualType T);
112
113public:
114 CGRecordLayoutBuilder(CodeGenTypes &Types)
115 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
116 Alignment(0), AlignmentAsLLVMStruct(1),
117 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
118
119 /// Layout - Will layout a RecordDecl.
120 void Layout(const RecordDecl *D);
121};
122
123}
124}
125
Anders Carlsson45372a62009-07-23 03:17:50 +0000126void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +0000127 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlssond0eb3b92009-09-02 17:51:33 +0000128 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +0000129
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000130 if (D->isUnion()) {
131 LayoutUnion(D);
132 return;
133 }
Anders Carlssona860e752009-08-08 18:23:56 +0000134
Anders Carlsson45372a62009-07-23 03:17:50 +0000135 if (LayoutFields(D))
136 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Anders Carlsson45372a62009-07-23 03:17:50 +0000138 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000139 Packed = true;
Anders Carlsson45372a62009-07-23 03:17:50 +0000140 AlignmentAsLLVMStruct = 1;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000141 NextFieldOffsetInBytes = 0;
Anders Carlsson45372a62009-07-23 03:17:50 +0000142 FieldTypes.clear();
Anders Carlsson45372a62009-07-23 03:17:50 +0000143 LLVMFields.clear();
144 LLVMBitFields.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Anders Carlsson45372a62009-07-23 03:17:50 +0000146 LayoutFields(D);
147}
148
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000149static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types,
150 const FieldDecl *FD,
151 uint64_t FieldOffset,
152 uint64_t FieldSize) {
153 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarab970f92010-04-13 20:58:55 +0000154 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
155 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000156
157 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000158
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000159 if (FieldSize > TypeSizeInBits) {
Anders Carlsson6ba38152010-04-17 22:54:57 +0000160 // We have a wide bit-field. The extra bits are only used for padding, so
161 // if we have a bitfield of type T, with size N:
162 //
163 // T t : N;
164 //
165 // We can just assume that it's:
166 //
167 // T t : sizeof(T);
168 //
169 FieldSize = TypeSizeInBits;
Anders Carlsson1c7658f2010-04-16 16:23:02 +0000170 }
171
172 unsigned StartBit = FieldOffset % TypeSizeInBits;
173
Daniel Dunbarab970f92010-04-13 20:58:55 +0000174 // The current policy is to always access the bit-field using the source type
175 // of the bit-field. With the C bit-field rules, this implies that we always
176 // use either one or two accesses, and two accesses can only occur with a
177 // packed structure when the bit-field straddles an alignment boundary.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000178 CGBitFieldInfo::AccessInfo Components[2];
179
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000180 unsigned LowBits = std::min(FieldSize, TypeSizeInBits - StartBit);
Daniel Dunbarab970f92010-04-13 20:58:55 +0000181 bool NeedsHighAccess = LowBits != FieldSize;
Daniel Dunbar2df25692010-04-15 05:09:32 +0000182 unsigned NumComponents = 1 + NeedsHighAccess;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000183
184 // FIXME: This access policy is probably wrong on big-endian systems.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000185 CGBitFieldInfo::AccessInfo &LowAccess = Components[0];
Daniel Dunbarab970f92010-04-13 20:58:55 +0000186 LowAccess.FieldIndex = 0;
187 LowAccess.FieldByteOffset =
188 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000189 LowAccess.FieldBitStart = StartBit;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000190 LowAccess.AccessWidth = TypeSizeInBits;
191 // FIXME: This might be wrong!
192 LowAccess.AccessAlignment = 0;
193 LowAccess.TargetBitOffset = 0;
194 LowAccess.TargetBitWidth = LowBits;
195
196 if (NeedsHighAccess) {
Daniel Dunbar2df25692010-04-15 05:09:32 +0000197 CGBitFieldInfo::AccessInfo &HighAccess = Components[1];
Daniel Dunbarab970f92010-04-13 20:58:55 +0000198 HighAccess.FieldIndex = 0;
199 HighAccess.FieldByteOffset = LowAccess.FieldByteOffset + TypeSizeInBytes;
200 HighAccess.FieldBitStart = 0;
201 HighAccess.AccessWidth = TypeSizeInBits;
202 // FIXME: This might be wrong!
203 HighAccess.AccessAlignment = 0;
204 HighAccess.TargetBitOffset = LowBits;
205 HighAccess.TargetBitWidth = FieldSize - LowBits;
206 }
207
Daniel Dunbar2df25692010-04-15 05:09:32 +0000208 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000209}
210
Anders Carlsson45372a62009-07-23 03:17:50 +0000211void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
212 uint64_t FieldOffset) {
Mike Stump1eb44332009-09-09 15:08:12 +0000213 uint64_t FieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000214 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Anders Carlsson45372a62009-07-23 03:17:50 +0000216 if (FieldSize == 0)
217 return;
218
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000219 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000220 unsigned NumBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Anders Carlsson45372a62009-07-23 03:17:50 +0000222 if (FieldOffset < NextFieldOffset) {
223 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000224 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Anders Carlsson45372a62009-07-23 03:17:50 +0000226 // The bitfield begins in the previous bit-field.
Mike Stump1eb44332009-09-09 15:08:12 +0000227 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000228 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
229 } else {
230 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
231
232 // Append padding if necessary.
233 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump1eb44332009-09-09 15:08:12 +0000234
235 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000236 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Anders Carlsson45372a62009-07-23 03:17:50 +0000238 assert(NumBytesToAppend && "No bytes to append!");
239 }
240
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000241 // Add the bit field info.
242 LLVMBitFields.push_back(
243 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Anders Carlsson45372a62009-07-23 03:17:50 +0000245 AppendBytes(NumBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Mike Stump1eb44332009-09-09 15:08:12 +0000247 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000248 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000249}
250
251bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
252 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000253 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000254 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000255 return false;
256
257 if (D->isBitField()) {
258 // We must use packed structs for unnamed bit fields since they
259 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000260 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000261 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Anders Carlsson45372a62009-07-23 03:17:50 +0000263 LayoutBitField(D, FieldOffset);
264 return true;
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Anders Carlsson2c12d032010-02-02 05:17:25 +0000267 // Check if we have a pointer to data member in this field.
268 CheckForPointerToDataMember(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000269
Anders Carlsson45372a62009-07-23 03:17:50 +0000270 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000271 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000273 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
274 unsigned TypeAlignment = getTypeAlignment(Ty);
275
Anders Carlssona5dd7222009-08-08 19:38:24 +0000276 // If the type alignment is larger then the struct alignment, we must use
277 // a packed struct.
278 if (TypeAlignment > Alignment) {
279 assert(!Packed && "Alignment is wrong even with packed struct!");
280 return false;
281 }
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Anders Carlssona5dd7222009-08-08 19:38:24 +0000283 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
284 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
285 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
286 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
287 return false;
288 }
289 }
290
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000291 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000292 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000293 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
294
295 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
296 assert(!Packed && "Could not place field even with packed struct!");
297 return false;
298 }
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000300 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
301 // Even with alignment, the field offset is not at the right place,
302 // insert padding.
303 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000305 AppendBytes(PaddingInBytes);
306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Anders Carlsson45372a62009-07-23 03:17:50 +0000308 // Now append the field.
309 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000310 AppendField(FieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Anders Carlsson45372a62009-07-23 03:17:50 +0000312 return true;
313}
314
Anders Carlsson86664462010-04-17 20:49:27 +0000315const llvm::Type *
316CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
317 const ASTRecordLayout &Layout) {
318 if (Field->isBitField()) {
319 uint64_t FieldSize =
320 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
321
322 // Ignore zero sized bit fields.
323 if (FieldSize == 0)
324 return 0;
325
Anders Carlssond62328e2010-04-17 21:04:52 +0000326 const llvm::Type *FieldTy;
327
328 if (!Field->getDeclName()) {
329 // This is an unnamed bit-field, which shouldn't affect alignment on the
330 // struct so we use an array of bytes for it.
331
332 FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
333
334 unsigned NumBytesToAppend =
335 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
336
337 if (NumBytesToAppend > 1)
338 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
339 } else
340 FieldTy = Types.ConvertTypeForMemRecursive(Field->getType());
341
Anders Carlsson86664462010-04-17 20:49:27 +0000342 // Add the bit field info.
343 LLVMBitFields.push_back(
344 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
Anders Carlssond62328e2010-04-17 21:04:52 +0000345 return FieldTy;
Anders Carlsson86664462010-04-17 20:49:27 +0000346 }
347
348 // This is a regular union field.
349 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
350 return Types.ConvertTypeForMemRecursive(Field->getType());
351}
352
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000353void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
354 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000356 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000358 const llvm::Type *Ty = 0;
359 uint64_t Size = 0;
360 unsigned Align = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000362 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000363
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000364 unsigned FieldNo = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000365 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000366 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump1eb44332009-09-09 15:08:12 +0000367 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000368 "Union field offset did not start at the beginning of record!");
Anders Carlsson86664462010-04-17 20:49:27 +0000369 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000370
Anders Carlsson86664462010-04-17 20:49:27 +0000371 if (!FieldTy)
372 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000374 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000375
Anders Carlsson177d4d82009-07-23 21:52:03 +0000376 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
377 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000379 if (FieldAlign < Align)
380 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000382 if (FieldAlign > Align || FieldSize > Size) {
383 Ty = FieldTy;
384 Align = FieldAlign;
385 Size = FieldSize;
386 }
387 }
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000389 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000390 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000391 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000392
393 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
394 // We need a packed struct.
395 Packed = true;
396 Align = 1;
397 }
398 }
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000399 if (!Align) {
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000400 assert(HasOnlyZeroSizedBitFields &&
401 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000402 Align = 1;
403 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000404
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000405 // Append tail padding.
406 if (Layout.getSize() / 8 > Size)
407 AppendPadding(Layout.getSize() / 8, Align);
408}
409
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000410void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
411 const ASTRecordLayout &Layout) {
412 // Check if we need to add a vtable pointer.
413 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar270e2032010-03-31 00:11:27 +0000414 const llvm::Type *Int8PtrTy =
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000415 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000416
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000417 assert(NextFieldOffsetInBytes == 0 &&
Anders Carlsson046c2942010-04-17 20:15:18 +0000418 "VTable pointer must come first!");
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000419 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
420 }
421}
422
Anders Carlsson45372a62009-07-23 03:17:50 +0000423bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
424 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000425 assert(Alignment && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000427 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000429 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
430 LayoutBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000431
Anders Carlsson45372a62009-07-23 03:17:50 +0000432 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000433
Mike Stump1eb44332009-09-09 15:08:12 +0000434 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000435 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
436 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000437 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000438 "Could not layout fields even with a packed LLVM struct!");
439 return false;
440 }
441 }
442
443 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000444 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Anders Carlsson45372a62009-07-23 03:17:50 +0000446 return true;
447}
448
Anders Carlssonc1efe362009-07-27 14:55:54 +0000449void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
450 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Anders Carlssonc1efe362009-07-27 14:55:54 +0000452 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000453 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Daniel Dunbar270e2032010-03-31 00:11:27 +0000455 uint64_t AlignedNextFieldOffset =
Anders Carlssonc2456822009-12-08 01:24:23 +0000456 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
457
458 if (AlignedNextFieldOffset == RecordSizeInBytes) {
459 // We don't need any padding.
460 return;
461 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000462
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000463 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000464 AppendBytes(NumPadBytes);
465}
466
Mike Stump1eb44332009-09-09 15:08:12 +0000467void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000468 const llvm::Type *FieldTy) {
469 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
470 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000471
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000472 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000473
Anders Carlsson45372a62009-07-23 03:17:50 +0000474 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000475
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000476 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000477 BitsAvailableInLastField = 0;
478}
479
Mike Stump1eb44332009-09-09 15:08:12 +0000480void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000481 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000482 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
483 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Anders Carlsson45372a62009-07-23 03:17:50 +0000485 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000486 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson45372a62009-07-23 03:17:50 +0000487 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
488
489 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
490 // Even with alignment, the field offset is not at the right place,
491 // insert padding.
492 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
493
494 AppendBytes(PaddingInBytes);
495 }
496}
497
498void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
499 if (NumBytes == 0)
500 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Owen Anderson0032b272009-08-13 21:57:51 +0000502 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000503 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000504 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Anders Carlsson45372a62009-07-23 03:17:50 +0000506 // Append the padding field
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000507 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000508}
509
510unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000511 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000512 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Anders Carlsson45372a62009-07-23 03:17:50 +0000514 return Types.getTargetData().getABITypeAlignment(Ty);
515}
516
Anders Carlsson2c12d032010-02-02 05:17:25 +0000517void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000518 // This record already contains a member pointer.
Anders Carlsson2c12d032010-02-02 05:17:25 +0000519 if (ContainsPointerToDataMember)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000520 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000522 // Can only have member pointers if we're compiling C++.
523 if (!Types.getContext().getLangOptions().CPlusPlus)
524 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Anders Carlsson2c12d032010-02-02 05:17:25 +0000526 T = Types.getContext().getBaseElementType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Anders Carlsson2c12d032010-02-02 05:17:25 +0000528 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
529 if (!MPT->getPointeeType()->isFunctionType()) {
530 // We have a pointer to data member.
531 ContainsPointerToDataMember = true;
532 }
533 } else if (const RecordType *RT = T->getAs<RecordType>()) {
534 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000535
Anders Carlsson2c12d032010-02-02 05:17:25 +0000536 // FIXME: It would be better if there was a way to explicitly compute the
537 // record layout instead of converting to a type.
538 Types.ConvertTagDeclType(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000539
Anders Carlsson2c12d032010-02-02 05:17:25 +0000540 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000541
Anders Carlsson2c12d032010-02-02 05:17:25 +0000542 if (Layout.containsPointerToDataMember())
543 ContainsPointerToDataMember = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000544 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000545}
546
Daniel Dunbar270e2032010-03-31 00:11:27 +0000547CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
548 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Anders Carlsson45372a62009-07-23 03:17:50 +0000550 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000551
Daniel Dunbar270e2032010-03-31 00:11:27 +0000552 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000553 Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000554 Builder.Packed);
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000556 CGRecordLayout *RL =
557 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
558
Anders Carlsson45372a62009-07-23 03:17:50 +0000559 // Add all the field numbers.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000560 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
561 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson45372a62009-07-23 03:17:50 +0000562
563 // Add bitfield info.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000564 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
565 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000567 // Dump the layout, if requested.
Daniel Dunbarab970f92010-04-13 20:58:55 +0000568 if (getContext().getLangOptions().DumpRecordLayouts) {
569 llvm::errs() << "\n*** Dumping Record Layout\n";
570 llvm::errs() << "Record: ";
571 D->dump();
572 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000573 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000574 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000575
Daniel Dunbar2e7b7c22010-04-19 20:44:47 +0000576 // Verify that the computed LLVM struct size matches the AST layout size.
577 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
578 getTargetData().getTypeAllocSize(Ty) &&
579 "Type size mismatch!");
580
581 // FIXME: We should verify the individual field offsets here as well.
582
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000583 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000584}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000585
586void CGRecordLayout::print(llvm::raw_ostream &OS) const {
587 OS << "<CGRecordLayout\n";
588 OS << " LLVMType:" << *LLVMType << "\n";
589 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
590 OS << " BitFields:[\n";
591 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
592 it = BitFields.begin(), ie = BitFields.end();
593 it != ie; ++it) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000594 OS.indent(4);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000595 it->second.print(OS);
596 OS << "\n";
597 }
598 OS << "]>\n";
599}
600
601void CGRecordLayout::dump() const {
602 print(llvm::errs());
603}
604
605void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
606 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000607 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000608 OS << " IsSigned:" << IsSigned << "\n";
609
610 OS.indent(4 + strlen("<CGBitFieldInfo"));
611 OS << " NumComponents:" << getNumComponents();
612 OS << " Components: [";
613 if (getNumComponents()) {
614 OS << "\n";
615 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
616 const AccessInfo &AI = getComponent(i);
617 OS.indent(8);
618 OS << "<AccessInfo"
619 << " FieldIndex:" << AI.FieldIndex
620 << " FieldByteOffset:" << AI.FieldByteOffset
621 << " FieldBitStart:" << AI.FieldBitStart
622 << " AccessWidth:" << AI.AccessWidth << "\n";
623 OS.indent(8 + strlen("<AccessInfo"));
624 OS << " AccessAlignment:" << AI.AccessAlignment
625 << " TargetBitOffset:" << AI.TargetBitOffset
626 << " TargetBitWidth:" << AI.TargetBitWidth
627 << ">\n";
628 }
629 OS.indent(4);
630 }
631 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000632}
633
634void CGBitFieldInfo::dump() const {
635 print(llvm::errs());
636}