blob: 215b39e7432f3ae623ef5059781d7faeef46235b [file] [log] [blame]
Daniel Dunbar23ee4b72010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson307846f2009-07-23 03:17:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson307846f2009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
20#include "CodeGenTypes.h"
21#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000022#include "llvm/Type.h"
23#include "llvm/Support/raw_ostream.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000024#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000025using namespace clang;
26using namespace CodeGen;
27
Daniel Dunbar23ee4b72010-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 Dunbard4549102010-04-06 01:07:41 +000041 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar23ee4b72010-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 Carlsson1de2f572010-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 Dunbar23ee4b72010-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 Dunbar23ee4b72010-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 Carlsson307846f2009-07-23 03:17:50 +0000130void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000131 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000132 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000133
Anders Carlsson697f6592009-07-23 03:43:54 +0000134 if (D->isUnion()) {
135 LayoutUnion(D);
136 return;
137 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000138
Anders Carlsson307846f2009-07-23 03:17:50 +0000139 if (LayoutFields(D))
140 return;
Mike Stump11289f42009-09-09 15:08:12 +0000141
Anders Carlsson307846f2009-07-23 03:17:50 +0000142 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000143 Packed = true;
Anders Carlsson307846f2009-07-23 03:17:50 +0000144 AlignmentAsLLVMStruct = 1;
Anders Carlssond5d64132009-07-28 17:56:36 +0000145 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000146 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000147 LLVMFields.clear();
148 LLVMBitFields.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000149
Anders Carlsson307846f2009-07-23 03:17:50 +0000150 LayoutFields(D);
151}
152
Daniel Dunbarf9c24f82010-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 Dunbarb935b932010-04-13 20:58:55 +0000158 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
159 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000160
161 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000162
Anders Carlssonbe6f3182010-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 Dunbarb935b932010-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 Dunbar9c78d632010-04-15 05:09:32 +0000187 CGBitFieldInfo::AccessInfo Components[2];
188
Daniel Dunbarbb138452010-04-15 05:09:28 +0000189 unsigned LowBits = std::min(FieldSize, TypeSizeInBits - StartBit);
Daniel Dunbarb935b932010-04-13 20:58:55 +0000190 bool NeedsHighAccess = LowBits != FieldSize;
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000191 unsigned NumComponents = 1 + NeedsHighAccess;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000192
193 // FIXME: This access policy is probably wrong on big-endian systems.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000194 CGBitFieldInfo::AccessInfo &LowAccess = Components[0];
Daniel Dunbarb935b932010-04-13 20:58:55 +0000195 LowAccess.FieldIndex = 0;
196 LowAccess.FieldByteOffset =
197 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
Daniel Dunbarbb138452010-04-15 05:09:28 +0000198 LowAccess.FieldBitStart = StartBit;
Daniel Dunbarb935b932010-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 Dunbar9c78d632010-04-15 05:09:32 +0000206 CGBitFieldInfo::AccessInfo &HighAccess = Components[1];
Daniel Dunbarb935b932010-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 Dunbar9c78d632010-04-15 05:09:32 +0000217 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000218}
219
Anders Carlsson307846f2009-07-23 03:17:50 +0000220void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
221 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000222 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000223 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000224
Anders Carlsson307846f2009-07-23 03:17:50 +0000225 if (FieldSize == 0)
226 return;
227
Anders Carlssond5d64132009-07-28 17:56:36 +0000228 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000229 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000230
Anders Carlsson307846f2009-07-23 03:17:50 +0000231 if (FieldOffset < NextFieldOffset) {
232 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000233 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000234
Anders Carlsson307846f2009-07-23 03:17:50 +0000235 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000236 NumBytesToAppend =
Anders Carlsson307846f2009-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 Stump11289f42009-09-09 15:08:12 +0000243
244 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000245 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000246
Anders Carlsson307846f2009-07-23 03:17:50 +0000247 assert(NumBytesToAppend && "No bytes to append!");
248 }
249
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000250 // Add the bit field info.
251 LLVMBitFields.push_back(
252 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000253
Anders Carlsson307846f2009-07-23 03:17:50 +0000254 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000255
Mike Stump11289f42009-09-09 15:08:12 +0000256 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000257 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000258}
259
260bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
261 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000262 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000263 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-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 Carlssond78fc892009-07-23 17:24:40 +0000269 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000270 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000271
Anders Carlsson307846f2009-07-23 03:17:50 +0000272 LayoutBitField(D, FieldOffset);
273 return true;
274 }
Mike Stump11289f42009-09-09 15:08:12 +0000275
Anders Carlssone8bfe412010-02-02 05:17:25 +0000276 // Check if we have a pointer to data member in this field.
277 CheckForPointerToDataMember(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000278
Anders Carlsson307846f2009-07-23 03:17:50 +0000279 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000280 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000281
Anders Carlsson19702bb2009-08-04 16:29:15 +0000282 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
283 unsigned TypeAlignment = getTypeAlignment(Ty);
284
Anders Carlsson28a5fa22009-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 Stump11289f42009-09-09 15:08:12 +0000291
Anders Carlsson28a5fa22009-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 Carlsson19702bb2009-08-04 16:29:15 +0000300 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000301 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-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 Stump11289f42009-09-09 15:08:12 +0000308
Anders Carlsson19702bb2009-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 Stump11289f42009-09-09 15:08:12 +0000313
Anders Carlsson19702bb2009-08-04 16:29:15 +0000314 AppendBytes(PaddingInBytes);
315 }
Mike Stump11289f42009-09-09 15:08:12 +0000316
Anders Carlsson307846f2009-07-23 03:17:50 +0000317 // Now append the field.
318 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000319 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000320
Anders Carlsson307846f2009-07-23 03:17:50 +0000321 return true;
322}
323
Anders Carlsson1de2f572010-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
Anders Carlsson2295f132010-04-17 21:04:52 +0000335 const llvm::Type *FieldTy;
336
337 if (!Field->getDeclName()) {
338 // This is an unnamed bit-field, which shouldn't affect alignment on the
339 // struct so we use an array of bytes for it.
340
341 FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
342
343 unsigned NumBytesToAppend =
344 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
345
346 if (NumBytesToAppend > 1)
347 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
348 } else
349 FieldTy = Types.ConvertTypeForMemRecursive(Field->getType());
350
Anders Carlsson1de2f572010-04-17 20:49:27 +0000351 // Add the bit field info.
352 LLVMBitFields.push_back(
353 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000354 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000355 }
356
357 // This is a regular union field.
358 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
359 return Types.ConvertTypeForMemRecursive(Field->getType());
360}
361
Anders Carlsson697f6592009-07-23 03:43:54 +0000362void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
363 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000364
Anders Carlsson697f6592009-07-23 03:43:54 +0000365 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000366
Anders Carlsson697f6592009-07-23 03:43:54 +0000367 const llvm::Type *Ty = 0;
368 uint64_t Size = 0;
369 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000370
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000371 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000372
Anders Carlsson697f6592009-07-23 03:43:54 +0000373 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000374 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000375 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000376 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000377 "Union field offset did not start at the beginning of record!");
Anders Carlsson1de2f572010-04-17 20:49:27 +0000378 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000379
Anders Carlsson1de2f572010-04-17 20:49:27 +0000380 if (!FieldTy)
381 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000382
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000383 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000384
Anders Carlssone2accf42009-07-23 21:52:03 +0000385 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
386 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000387
Anders Carlsson697f6592009-07-23 03:43:54 +0000388 if (FieldAlign < Align)
389 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000390
Anders Carlsson697f6592009-07-23 03:43:54 +0000391 if (FieldAlign > Align || FieldSize > Size) {
392 Ty = FieldTy;
393 Align = FieldAlign;
394 Size = FieldSize;
395 }
396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Anders Carlsson697f6592009-07-23 03:43:54 +0000398 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000399 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000400 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000401
402 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
403 // We need a packed struct.
404 Packed = true;
405 Align = 1;
406 }
407 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000408 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000409 assert(HasOnlyZeroSizedBitFields &&
410 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000411 Align = 1;
412 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000413
Anders Carlsson697f6592009-07-23 03:43:54 +0000414 // Append tail padding.
415 if (Layout.getSize() / 8 > Size)
416 AppendPadding(Layout.getSize() / 8, Align);
417}
418
Anders Carlssond681a292009-12-16 17:27:20 +0000419void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
420 const ASTRecordLayout &Layout) {
421 // Check if we need to add a vtable pointer.
422 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000423 const llvm::Type *Int8PtrTy =
Anders Carlssond681a292009-12-16 17:27:20 +0000424 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000425
Anders Carlssond681a292009-12-16 17:27:20 +0000426 assert(NextFieldOffsetInBytes == 0 &&
Anders Carlsson11e51402010-04-17 20:15:18 +0000427 "VTable pointer must come first!");
Anders Carlssond681a292009-12-16 17:27:20 +0000428 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
429 }
430}
431
Anders Carlsson307846f2009-07-23 03:17:50 +0000432bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
433 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000434 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000435
Anders Carlsson697f6592009-07-23 03:43:54 +0000436 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000437
Anders Carlssond681a292009-12-16 17:27:20 +0000438 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
439 LayoutBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000440
Anders Carlsson307846f2009-07-23 03:17:50 +0000441 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000442
Mike Stump11289f42009-09-09 15:08:12 +0000443 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000444 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
445 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000446 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000447 "Could not layout fields even with a packed LLVM struct!");
448 return false;
449 }
450 }
451
452 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000453 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000454
Anders Carlsson307846f2009-07-23 03:17:50 +0000455 return true;
456}
457
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000458void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
459 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000460
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000461 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000462 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000463
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000464 uint64_t AlignedNextFieldOffset =
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000465 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
466
467 if (AlignedNextFieldOffset == RecordSizeInBytes) {
468 // We don't need any padding.
469 return;
470 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000471
Anders Carlssond5d64132009-07-28 17:56:36 +0000472 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000473 AppendBytes(NumPadBytes);
474}
475
Mike Stump11289f42009-09-09 15:08:12 +0000476void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000477 const llvm::Type *FieldTy) {
478 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
479 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000480
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000481 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000482
Anders Carlsson307846f2009-07-23 03:17:50 +0000483 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000484
Anders Carlssond5d64132009-07-28 17:56:36 +0000485 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000486 BitsAvailableInLastField = 0;
487}
488
Mike Stump11289f42009-09-09 15:08:12 +0000489void
Anders Carlsson307846f2009-07-23 03:17:50 +0000490CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
491 const llvm::Type *FieldTy) {
492 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
493}
494
Mike Stump11289f42009-09-09 15:08:12 +0000495void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000496 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000497 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
498 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000499
Anders Carlsson307846f2009-07-23 03:17:50 +0000500 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000501 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson307846f2009-07-23 03:17:50 +0000502 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
503
504 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
505 // Even with alignment, the field offset is not at the right place,
506 // insert padding.
507 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
508
509 AppendBytes(PaddingInBytes);
510 }
511}
512
513void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
514 if (NumBytes == 0)
515 return;
Mike Stump11289f42009-09-09 15:08:12 +0000516
Owen Anderson41a75022009-08-13 21:57:51 +0000517 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000518 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000519 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000520
Anders Carlsson307846f2009-07-23 03:17:50 +0000521 // Append the padding field
Anders Carlssond5d64132009-07-28 17:56:36 +0000522 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000523}
524
525unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000526 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000527 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000528
Anders Carlsson307846f2009-07-23 03:17:50 +0000529 return Types.getTargetData().getABITypeAlignment(Ty);
530}
531
Anders Carlssone8bfe412010-02-02 05:17:25 +0000532void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000533 // This record already contains a member pointer.
Anders Carlssone8bfe412010-02-02 05:17:25 +0000534 if (ContainsPointerToDataMember)
Anders Carlssond606de72009-08-23 01:25:01 +0000535 return;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Anders Carlssond606de72009-08-23 01:25:01 +0000537 // Can only have member pointers if we're compiling C++.
538 if (!Types.getContext().getLangOptions().CPlusPlus)
539 return;
Mike Stump11289f42009-09-09 15:08:12 +0000540
Anders Carlssone8bfe412010-02-02 05:17:25 +0000541 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000542
Anders Carlssone8bfe412010-02-02 05:17:25 +0000543 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
544 if (!MPT->getPointeeType()->isFunctionType()) {
545 // We have a pointer to data member.
546 ContainsPointerToDataMember = true;
547 }
548 } else if (const RecordType *RT = T->getAs<RecordType>()) {
549 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000550
Anders Carlssone8bfe412010-02-02 05:17:25 +0000551 // FIXME: It would be better if there was a way to explicitly compute the
552 // record layout instead of converting to a type.
553 Types.ConvertTagDeclType(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000554
Anders Carlssone8bfe412010-02-02 05:17:25 +0000555 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000556
Anders Carlssone8bfe412010-02-02 05:17:25 +0000557 if (Layout.containsPointerToDataMember())
558 ContainsPointerToDataMember = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000559 }
Anders Carlssond606de72009-08-23 01:25:01 +0000560}
561
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000562CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
563 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000564
Anders Carlsson307846f2009-07-23 03:17:50 +0000565 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000566
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000567 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson758428f2009-08-05 23:18:46 +0000568 Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000569 Builder.Packed);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000570 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
571 getTargetData().getTypeAllocSize(Ty) &&
Anders Carlsson453878b2009-08-08 18:01:57 +0000572 "Type size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000573
Daniel Dunbar034299e2010-03-31 01:09:11 +0000574 CGRecordLayout *RL =
575 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
576
Anders Carlsson307846f2009-07-23 03:17:50 +0000577 // Add all the field numbers.
Daniel Dunbard4549102010-04-06 01:07:41 +0000578 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
579 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson307846f2009-07-23 03:17:50 +0000580
581 // Add bitfield info.
Daniel Dunbard4549102010-04-06 01:07:41 +0000582 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
583 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000584
Daniel Dunbarb935b932010-04-13 20:58:55 +0000585 if (getContext().getLangOptions().DumpRecordLayouts) {
586 llvm::errs() << "\n*** Dumping Record Layout\n";
587 llvm::errs() << "Record: ";
588 D->dump();
589 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000590 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000591 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000592
Daniel Dunbar034299e2010-03-31 01:09:11 +0000593 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000594}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000595
596void CGRecordLayout::print(llvm::raw_ostream &OS) const {
597 OS << "<CGRecordLayout\n";
598 OS << " LLVMType:" << *LLVMType << "\n";
599 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
600 OS << " BitFields:[\n";
601 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
602 it = BitFields.begin(), ie = BitFields.end();
603 it != ie; ++it) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000604 OS.indent(4);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000605 it->second.print(OS);
606 OS << "\n";
607 }
608 OS << "]>\n";
609}
610
611void CGRecordLayout::dump() const {
612 print(llvm::errs());
613}
614
615void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
616 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000617 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000618 OS << " IsSigned:" << IsSigned << "\n";
619
620 OS.indent(4 + strlen("<CGBitFieldInfo"));
621 OS << " NumComponents:" << getNumComponents();
622 OS << " Components: [";
623 if (getNumComponents()) {
624 OS << "\n";
625 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
626 const AccessInfo &AI = getComponent(i);
627 OS.indent(8);
628 OS << "<AccessInfo"
629 << " FieldIndex:" << AI.FieldIndex
630 << " FieldByteOffset:" << AI.FieldByteOffset
631 << " FieldBitStart:" << AI.FieldBitStart
632 << " AccessWidth:" << AI.AccessWidth << "\n";
633 OS.indent(8 + strlen("<AccessInfo"));
634 OS << " AccessAlignment:" << AI.AccessAlignment
635 << " TargetBitOffset:" << AI.TargetBitOffset
636 << " TargetBitWidth:" << AI.TargetBitWidth
637 << ">\n";
638 }
639 OS.indent(4);
640 }
641 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000642}
643
644void CGBitFieldInfo::dump() const {
645 print(llvm::errs());
646}