blob: 60ef7fe409340aeead4e7ef4af3f1f916edfea0e [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) {
164 // We have a wide bit-field.
165
166 CGBitFieldInfo::AccessInfo Component;
167
168 Component.FieldIndex = 0;
169 Component.FieldByteOffset =
170 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
171 Component.FieldBitStart = 0;
172 Component.AccessWidth = TypeSizeInBits;
173 // FIXME: This might be wrong!
174 Component.AccessAlignment = 0;
175 Component.TargetBitOffset = 0;
176 Component.TargetBitWidth = TypeSizeInBits;
177
178 return CGBitFieldInfo(TypeSizeInBits, 1, &Component, IsSigned);
179 }
180
181 unsigned StartBit = FieldOffset % TypeSizeInBits;
182
Daniel Dunbarab970f92010-04-13 20:58:55 +0000183 // The current policy is to always access the bit-field using the source type
184 // of the bit-field. With the C bit-field rules, this implies that we always
185 // use either one or two accesses, and two accesses can only occur with a
186 // packed structure when the bit-field straddles an alignment boundary.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000187 CGBitFieldInfo::AccessInfo Components[2];
188
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000189 unsigned LowBits = std::min(FieldSize, TypeSizeInBits - StartBit);
Daniel Dunbarab970f92010-04-13 20:58:55 +0000190 bool NeedsHighAccess = LowBits != FieldSize;
Daniel Dunbar2df25692010-04-15 05:09:32 +0000191 unsigned NumComponents = 1 + NeedsHighAccess;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000192
193 // FIXME: This access policy is probably wrong on big-endian systems.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000194 CGBitFieldInfo::AccessInfo &LowAccess = Components[0];
Daniel Dunbarab970f92010-04-13 20:58:55 +0000195 LowAccess.FieldIndex = 0;
196 LowAccess.FieldByteOffset =
197 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
Daniel Dunbar7fb61952010-04-15 05:09:28 +0000198 LowAccess.FieldBitStart = StartBit;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000199 LowAccess.AccessWidth = TypeSizeInBits;
200 // FIXME: This might be wrong!
201 LowAccess.AccessAlignment = 0;
202 LowAccess.TargetBitOffset = 0;
203 LowAccess.TargetBitWidth = LowBits;
204
205 if (NeedsHighAccess) {
Daniel Dunbar2df25692010-04-15 05:09:32 +0000206 CGBitFieldInfo::AccessInfo &HighAccess = Components[1];
Daniel Dunbarab970f92010-04-13 20:58:55 +0000207 HighAccess.FieldIndex = 0;
208 HighAccess.FieldByteOffset = LowAccess.FieldByteOffset + TypeSizeInBytes;
209 HighAccess.FieldBitStart = 0;
210 HighAccess.AccessWidth = TypeSizeInBits;
211 // FIXME: This might be wrong!
212 HighAccess.AccessAlignment = 0;
213 HighAccess.TargetBitOffset = LowBits;
214 HighAccess.TargetBitWidth = FieldSize - LowBits;
215 }
216
Daniel Dunbar2df25692010-04-15 05:09:32 +0000217 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000218}
219
Anders Carlsson45372a62009-07-23 03:17:50 +0000220void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
221 uint64_t FieldOffset) {
Mike Stump1eb44332009-09-09 15:08:12 +0000222 uint64_t FieldSize =
Anders Carlsson45372a62009-07-23 03:17:50 +0000223 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Anders Carlsson45372a62009-07-23 03:17:50 +0000225 if (FieldSize == 0)
226 return;
227
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000228 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +0000229 unsigned NumBytesToAppend;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Anders Carlsson45372a62009-07-23 03:17:50 +0000231 if (FieldOffset < NextFieldOffset) {
232 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000233 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Anders Carlsson45372a62009-07-23 03:17:50 +0000235 // The bitfield begins in the previous bit-field.
Mike Stump1eb44332009-09-09 15:08:12 +0000236 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000237 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
238 } else {
239 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
240
241 // Append padding if necessary.
242 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
244 NumBytesToAppend =
Anders Carlsson45372a62009-07-23 03:17:50 +0000245 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Anders Carlsson45372a62009-07-23 03:17:50 +0000247 assert(NumBytesToAppend && "No bytes to append!");
248 }
249
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000250 // Add the bit field info.
251 LLVMBitFields.push_back(
252 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Anders Carlsson45372a62009-07-23 03:17:50 +0000254 AppendBytes(NumBytesToAppend);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Mike Stump1eb44332009-09-09 15:08:12 +0000256 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000257 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +0000258}
259
260bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
261 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000262 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000263 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000264 return false;
265
266 if (D->isBitField()) {
267 // We must use packed structs for unnamed bit fields since they
268 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000269 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000270 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Anders Carlsson45372a62009-07-23 03:17:50 +0000272 LayoutBitField(D, FieldOffset);
273 return true;
274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Anders Carlsson2c12d032010-02-02 05:17:25 +0000276 // Check if we have a pointer to data member in this field.
277 CheckForPointerToDataMember(D->getType());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000278
Anders Carlsson45372a62009-07-23 03:17:50 +0000279 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000280 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000282 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
283 unsigned TypeAlignment = getTypeAlignment(Ty);
284
Anders Carlssona5dd7222009-08-08 19:38:24 +0000285 // If the type alignment is larger then the struct alignment, we must use
286 // a packed struct.
287 if (TypeAlignment > Alignment) {
288 assert(!Packed && "Alignment is wrong even with packed struct!");
289 return false;
290 }
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Anders Carlssona5dd7222009-08-08 19:38:24 +0000292 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
293 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
294 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
295 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
296 return false;
297 }
298 }
299
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000300 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000301 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000302 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
303
304 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
305 assert(!Packed && "Could not place field even with packed struct!");
306 return false;
307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000309 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
310 // Even with alignment, the field offset is not at the right place,
311 // insert padding.
312 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000314 AppendBytes(PaddingInBytes);
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Anders Carlsson45372a62009-07-23 03:17:50 +0000317 // Now append the field.
318 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000319 AppendField(FieldOffsetInBytes, Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Anders Carlsson45372a62009-07-23 03:17:50 +0000321 return true;
322}
323
Anders Carlsson86664462010-04-17 20:49:27 +0000324const llvm::Type *
325CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
326 const ASTRecordLayout &Layout) {
327 if (Field->isBitField()) {
328 uint64_t FieldSize =
329 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
330
331 // Ignore zero sized bit fields.
332 if (FieldSize == 0)
333 return 0;
334
335 // Add the bit field info.
336 LLVMBitFields.push_back(
337 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
338 return Types.ConvertTypeForMemRecursive(Field->getType());
339 }
340
341 // This is a regular union field.
342 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
343 return Types.ConvertTypeForMemRecursive(Field->getType());
344}
345
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000346void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
347 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000349 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000351 const llvm::Type *Ty = 0;
352 uint64_t Size = 0;
353 unsigned Align = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000355 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000356
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000357 unsigned FieldNo = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000358 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000359 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump1eb44332009-09-09 15:08:12 +0000360 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000361 "Union field offset did not start at the beginning of record!");
Anders Carlsson86664462010-04-17 20:49:27 +0000362 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000363
Anders Carlsson86664462010-04-17 20:49:27 +0000364 if (!FieldTy)
365 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000367 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000368
Anders Carlsson177d4d82009-07-23 21:52:03 +0000369 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
370 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000372 if (FieldAlign < Align)
373 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000375 if (FieldAlign > Align || FieldSize > Size) {
376 Ty = FieldTy;
377 Align = FieldAlign;
378 Size = FieldSize;
379 }
380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000382 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000383 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000384 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000385
386 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
387 // We need a packed struct.
388 Packed = true;
389 Align = 1;
390 }
391 }
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000392 if (!Align) {
Anders Carlsson21fd7d72010-01-28 18:22:03 +0000393 assert(HasOnlyZeroSizedBitFields &&
394 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane5041702009-11-06 20:47:40 +0000395 Align = 1;
396 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000397
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000398 // Append tail padding.
399 if (Layout.getSize() / 8 > Size)
400 AppendPadding(Layout.getSize() / 8, Align);
401}
402
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000403void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
404 const ASTRecordLayout &Layout) {
405 // Check if we need to add a vtable pointer.
406 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar270e2032010-03-31 00:11:27 +0000407 const llvm::Type *Int8PtrTy =
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000408 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000409
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000410 assert(NextFieldOffsetInBytes == 0 &&
Anders Carlsson046c2942010-04-17 20:15:18 +0000411 "VTable pointer must come first!");
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000412 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
413 }
414}
415
Anders Carlsson45372a62009-07-23 03:17:50 +0000416bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
417 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000418 assert(Alignment && "Did not set alignment!");
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000420 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Anders Carlsson4b3e5be2009-12-16 17:27:20 +0000422 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
423 LayoutBases(RD, Layout);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000424
Anders Carlsson45372a62009-07-23 03:17:50 +0000425 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000426
Mike Stump1eb44332009-09-09 15:08:12 +0000427 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson45372a62009-07-23 03:17:50 +0000428 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
429 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000430 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000431 "Could not layout fields even with a packed LLVM struct!");
432 return false;
433 }
434 }
435
436 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000437 AppendTailPadding(Layout.getSize());
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Anders Carlsson45372a62009-07-23 03:17:50 +0000439 return true;
440}
441
Anders Carlssonc1efe362009-07-27 14:55:54 +0000442void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
443 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Anders Carlssonc1efe362009-07-27 14:55:54 +0000445 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000446 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Daniel Dunbar270e2032010-03-31 00:11:27 +0000448 uint64_t AlignedNextFieldOffset =
Anders Carlssonc2456822009-12-08 01:24:23 +0000449 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
450
451 if (AlignedNextFieldOffset == RecordSizeInBytes) {
452 // We don't need any padding.
453 return;
454 }
Daniel Dunbar270e2032010-03-31 00:11:27 +0000455
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000456 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000457 AppendBytes(NumPadBytes);
458}
459
Mike Stump1eb44332009-09-09 15:08:12 +0000460void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000461 const llvm::Type *FieldTy) {
462 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
463 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000464
Daniel Dunbar9b28daf2010-04-12 21:01:28 +0000465 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000466
Anders Carlsson45372a62009-07-23 03:17:50 +0000467 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000468
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000469 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000470 BitsAvailableInLastField = 0;
471}
472
Mike Stump1eb44332009-09-09 15:08:12 +0000473void
Anders Carlsson45372a62009-07-23 03:17:50 +0000474CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
475 const llvm::Type *FieldTy) {
476 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
477}
478
Mike Stump1eb44332009-09-09 15:08:12 +0000479void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000480 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000481 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
482 "Incorrect field layout!");
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Anders Carlsson45372a62009-07-23 03:17:50 +0000484 // Round up the field offset to the alignment of the field type.
Mike Stump1eb44332009-09-09 15:08:12 +0000485 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson45372a62009-07-23 03:17:50 +0000486 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
487
488 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
489 // Even with alignment, the field offset is not at the right place,
490 // insert padding.
491 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
492
493 AppendBytes(PaddingInBytes);
494 }
495}
496
497void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
498 if (NumBytes == 0)
499 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Owen Anderson0032b272009-08-13 21:57:51 +0000501 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000502 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000503 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Anders Carlsson45372a62009-07-23 03:17:50 +0000505 // Append the padding field
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000506 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000507}
508
509unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000510 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000511 return 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Anders Carlsson45372a62009-07-23 03:17:50 +0000513 return Types.getTargetData().getABITypeAlignment(Ty);
514}
515
Anders Carlsson2c12d032010-02-02 05:17:25 +0000516void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000517 // This record already contains a member pointer.
Anders Carlsson2c12d032010-02-02 05:17:25 +0000518 if (ContainsPointerToDataMember)
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000519 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000521 // Can only have member pointers if we're compiling C++.
522 if (!Types.getContext().getLangOptions().CPlusPlus)
523 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Anders Carlsson2c12d032010-02-02 05:17:25 +0000525 T = Types.getContext().getBaseElementType(T);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Anders Carlsson2c12d032010-02-02 05:17:25 +0000527 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
528 if (!MPT->getPointeeType()->isFunctionType()) {
529 // We have a pointer to data member.
530 ContainsPointerToDataMember = true;
531 }
532 } else if (const RecordType *RT = T->getAs<RecordType>()) {
533 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar270e2032010-03-31 00:11:27 +0000534
Anders Carlsson2c12d032010-02-02 05:17:25 +0000535 // FIXME: It would be better if there was a way to explicitly compute the
536 // record layout instead of converting to a type.
537 Types.ConvertTagDeclType(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000538
Anders Carlsson2c12d032010-02-02 05:17:25 +0000539 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000540
Anders Carlsson2c12d032010-02-02 05:17:25 +0000541 if (Layout.containsPointerToDataMember())
542 ContainsPointerToDataMember = true;
Daniel Dunbar270e2032010-03-31 00:11:27 +0000543 }
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000544}
545
Daniel Dunbar270e2032010-03-31 00:11:27 +0000546CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
547 CGRecordLayoutBuilder Builder(*this);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Anders Carlsson45372a62009-07-23 03:17:50 +0000549 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000550
Daniel Dunbar270e2032010-03-31 00:11:27 +0000551 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson47a434f2009-08-05 23:18:46 +0000552 Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000553 Builder.Packed);
Daniel Dunbar270e2032010-03-31 00:11:27 +0000554 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
555 getTargetData().getTypeAllocSize(Ty) &&
Anders Carlssondf31e092009-08-08 18:01:57 +0000556 "Type size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000558 CGRecordLayout *RL =
559 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
560
Anders Carlsson45372a62009-07-23 03:17:50 +0000561 // Add all the field numbers.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000562 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
563 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson45372a62009-07-23 03:17:50 +0000564
565 // Add bitfield info.
Daniel Dunbarc7a984a2010-04-06 01:07:41 +0000566 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
567 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Daniel Dunbarab970f92010-04-13 20:58:55 +0000569 if (getContext().getLangOptions().DumpRecordLayouts) {
570 llvm::errs() << "\n*** Dumping Record Layout\n";
571 llvm::errs() << "Record: ";
572 D->dump();
573 llvm::errs() << "\nLayout: ";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000574 RL->dump();
Daniel Dunbarab970f92010-04-13 20:58:55 +0000575 }
Daniel Dunbar93c62962010-04-12 18:14:18 +0000576
Daniel Dunbar198bcb42010-03-31 01:09:11 +0000577 return RL;
Anders Carlsson45372a62009-07-23 03:17:50 +0000578}
Daniel Dunbar93c62962010-04-12 18:14:18 +0000579
580void CGRecordLayout::print(llvm::raw_ostream &OS) const {
581 OS << "<CGRecordLayout\n";
582 OS << " LLVMType:" << *LLVMType << "\n";
583 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
584 OS << " BitFields:[\n";
585 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
586 it = BitFields.begin(), ie = BitFields.end();
587 it != ie; ++it) {
Daniel Dunbarab970f92010-04-13 20:58:55 +0000588 OS.indent(4);
Daniel Dunbar93c62962010-04-12 18:14:18 +0000589 it->second.print(OS);
590 OS << "\n";
591 }
592 OS << "]>\n";
593}
594
595void CGRecordLayout::dump() const {
596 print(llvm::errs());
597}
598
599void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
600 OS << "<CGBitFieldInfo";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000601 OS << " Size:" << Size;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000602 OS << " IsSigned:" << IsSigned << "\n";
603
604 OS.indent(4 + strlen("<CGBitFieldInfo"));
605 OS << " NumComponents:" << getNumComponents();
606 OS << " Components: [";
607 if (getNumComponents()) {
608 OS << "\n";
609 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
610 const AccessInfo &AI = getComponent(i);
611 OS.indent(8);
612 OS << "<AccessInfo"
613 << " FieldIndex:" << AI.FieldIndex
614 << " FieldByteOffset:" << AI.FieldByteOffset
615 << " FieldBitStart:" << AI.FieldBitStart
616 << " AccessWidth:" << AI.AccessWidth << "\n";
617 OS.indent(8 + strlen("<AccessInfo"));
618 OS << " AccessAlignment:" << AI.AccessAlignment
619 << " TargetBitOffset:" << AI.TargetBitOffset
620 << " TargetBitWidth:" << AI.TargetBitWidth
621 << ">\n";
622 }
623 OS.indent(4);
624 }
625 OS << "]>";
Daniel Dunbar93c62962010-04-12 18:14:18 +0000626}
627
628void CGBitFieldInfo::dump() const {
629 print(llvm::errs());
630}