blob: 71d0290548a25626a83c9a4e986909afd13f6127 [file] [log] [blame]
Daniel Dunbar23ee4b72010-03-31 00:11:27 +00001//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- C++ -*-===//
Anders Carlsson307846f2009-07-23 03:17:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000010// Builder implementation for CGRecordLayout objects.
Anders Carlsson307846f2009-07-23 03:17:50 +000011//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar072d0bb2010-03-30 22:26:10 +000014#include "CGRecordLayout.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
20#include "CodeGenTypes.h"
21#include "llvm/DerivedTypes.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000022#include "llvm/Type.h"
Daniel Dunbar2ba67442010-04-21 19:10:49 +000023#include "llvm/Support/Debug.h"
Daniel Dunbarb97bff92010-04-12 18:14:18 +000024#include "llvm/Support/raw_ostream.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000025#include "llvm/Target/TargetData.h"
Anders Carlsson307846f2009-07-23 03:17:50 +000026using namespace clang;
27using namespace CodeGen;
28
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000029namespace clang {
30namespace CodeGen {
31
32class CGRecordLayoutBuilder {
33public:
34 /// FieldTypes - Holds the LLVM types that the struct is created from.
35 std::vector<const llvm::Type *> FieldTypes;
36
37 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number.
38 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo;
39 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields;
40
41 /// LLVMBitFieldInfo - Holds location and size information about a bit field.
Daniel Dunbard4549102010-04-06 01:07:41 +000042 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000043 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields;
44
45 /// ContainsPointerToDataMember - Whether one of the fields in this record
46 /// layout is a pointer to data member, or a struct that contains pointer to
47 /// data member.
48 bool ContainsPointerToDataMember;
49
50 /// Packed - Whether the resulting LLVM struct will be packed or not.
51 bool Packed;
52
53private:
54 CodeGenTypes &Types;
55
56 /// Alignment - Contains the alignment of the RecordDecl.
57 //
58 // FIXME: This is not needed and should be removed.
59 unsigned Alignment;
60
61 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the
62 /// LLVM types.
63 unsigned AlignmentAsLLVMStruct;
64
65 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field,
66 /// this will have the number of bits still available in the field.
67 char BitsAvailableInLastField;
68
69 /// NextFieldOffsetInBytes - Holds the next field offset in bytes.
70 uint64_t NextFieldOffsetInBytes;
71
Anders Carlsson1de2f572010-04-17 20:49:27 +000072 /// LayoutUnionField - Will layout a field in an union and return the type
73 /// that the field will have.
74 const llvm::Type *LayoutUnionField(const FieldDecl *Field,
75 const ASTRecordLayout &Layout);
76
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000077 /// LayoutUnion - Will layout a union RecordDecl.
78 void LayoutUnion(const RecordDecl *D);
79
80 /// LayoutField - try to layout all fields in the record decl.
81 /// Returns false if the operation failed because the struct is not packed.
82 bool LayoutFields(const RecordDecl *D);
83
84 /// LayoutBases - layout the bases and vtable pointer of a record decl.
85 void LayoutBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout);
86
87 /// LayoutField - layout a single field. Returns false if the operation failed
88 /// because the current struct is not packed.
89 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
90
91 /// LayoutBitField - layout a single bit field.
92 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
93
94 /// AppendField - Appends a field with the given offset and type.
95 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
96
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000097 /// AppendPadding - Appends enough padding bytes so that the total
98 /// struct size is a multiple of the field alignment.
99 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
100
101 /// AppendBytes - Append a given number of bytes to the record.
102 void AppendBytes(uint64_t NumBytes);
103
104 /// AppendTailPadding - Append enough tail padding so that the type will have
105 /// the passed size.
106 void AppendTailPadding(uint64_t RecordSize);
107
108 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000109
110 /// CheckForPointerToDataMember - Check if the given type contains a pointer
111 /// to data member.
112 void CheckForPointerToDataMember(QualType T);
113
114public:
115 CGRecordLayoutBuilder(CodeGenTypes &Types)
116 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
117 Alignment(0), AlignmentAsLLVMStruct(1),
118 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
119
120 /// Layout - Will layout a RecordDecl.
121 void Layout(const RecordDecl *D);
122};
123
124}
125}
126
Anders Carlsson307846f2009-07-23 03:17:50 +0000127void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000128 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000129 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000130
Anders Carlsson697f6592009-07-23 03:43:54 +0000131 if (D->isUnion()) {
132 LayoutUnion(D);
133 return;
134 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000135
Anders Carlsson307846f2009-07-23 03:17:50 +0000136 if (LayoutFields(D))
137 return;
Mike Stump11289f42009-09-09 15:08:12 +0000138
Anders Carlsson307846f2009-07-23 03:17:50 +0000139 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000140 Packed = true;
Anders Carlsson307846f2009-07-23 03:17:50 +0000141 AlignmentAsLLVMStruct = 1;
Anders Carlssond5d64132009-07-28 17:56:36 +0000142 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000143 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000144 LLVMFields.clear();
145 LLVMBitFields.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000146
Anders Carlsson307846f2009-07-23 03:17:50 +0000147 LayoutFields(D);
148}
149
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000150static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types,
151 const FieldDecl *FD,
152 uint64_t FieldOffset,
153 uint64_t FieldSize) {
154 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000155 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
156 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000157
158 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000159
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000160 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000161 // We have a wide bit-field. The extra bits are only used for padding, so
162 // if we have a bitfield of type T, with size N:
163 //
164 // T t : N;
165 //
166 // We can just assume that it's:
167 //
168 // T t : sizeof(T);
169 //
170 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000171 }
172
173 unsigned StartBit = FieldOffset % TypeSizeInBits;
174
Daniel Dunbarb935b932010-04-13 20:58:55 +0000175 // The current policy is to always access the bit-field using the source type
176 // of the bit-field. With the C bit-field rules, this implies that we always
177 // use either one or two accesses, and two accesses can only occur with a
178 // packed structure when the bit-field straddles an alignment boundary.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000179 CGBitFieldInfo::AccessInfo Components[2];
180
Daniel Dunbarbb138452010-04-15 05:09:28 +0000181 unsigned LowBits = std::min(FieldSize, TypeSizeInBits - StartBit);
Daniel Dunbarb935b932010-04-13 20:58:55 +0000182 bool NeedsHighAccess = LowBits != FieldSize;
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000183 unsigned NumComponents = 1 + NeedsHighAccess;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000184
185 // FIXME: This access policy is probably wrong on big-endian systems.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000186 CGBitFieldInfo::AccessInfo &LowAccess = Components[0];
Daniel Dunbarb935b932010-04-13 20:58:55 +0000187 LowAccess.FieldIndex = 0;
188 LowAccess.FieldByteOffset =
189 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
Daniel Dunbarbb138452010-04-15 05:09:28 +0000190 LowAccess.FieldBitStart = StartBit;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000191 LowAccess.AccessWidth = TypeSizeInBits;
192 // FIXME: This might be wrong!
193 LowAccess.AccessAlignment = 0;
194 LowAccess.TargetBitOffset = 0;
195 LowAccess.TargetBitWidth = LowBits;
196
197 if (NeedsHighAccess) {
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000198 CGBitFieldInfo::AccessInfo &HighAccess = Components[1];
Daniel Dunbarb935b932010-04-13 20:58:55 +0000199 HighAccess.FieldIndex = 0;
200 HighAccess.FieldByteOffset = LowAccess.FieldByteOffset + TypeSizeInBytes;
201 HighAccess.FieldBitStart = 0;
202 HighAccess.AccessWidth = TypeSizeInBits;
203 // FIXME: This might be wrong!
204 HighAccess.AccessAlignment = 0;
205 HighAccess.TargetBitOffset = LowBits;
206 HighAccess.TargetBitWidth = FieldSize - LowBits;
207 }
208
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000209 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000210}
211
Anders Carlsson307846f2009-07-23 03:17:50 +0000212void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
213 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000214 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000215 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000216
Anders Carlsson307846f2009-07-23 03:17:50 +0000217 if (FieldSize == 0)
218 return;
219
Anders Carlssond5d64132009-07-28 17:56:36 +0000220 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000221 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000222
Anders Carlsson307846f2009-07-23 03:17:50 +0000223 if (FieldOffset < NextFieldOffset) {
224 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000225 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000226
Anders Carlsson307846f2009-07-23 03:17:50 +0000227 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000228 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000229 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
230 } else {
231 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
232
233 // Append padding if necessary.
234 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump11289f42009-09-09 15:08:12 +0000235
236 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000237 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000238
Anders Carlsson307846f2009-07-23 03:17:50 +0000239 assert(NumBytesToAppend && "No bytes to append!");
240 }
241
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000242 // Add the bit field info.
243 LLVMBitFields.push_back(
244 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000245
Anders Carlsson307846f2009-07-23 03:17:50 +0000246 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000247
Mike Stump11289f42009-09-09 15:08:12 +0000248 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000249 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000250}
251
252bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
253 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000254 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000255 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000256 return false;
257
258 if (D->isBitField()) {
259 // We must use packed structs for unnamed bit fields since they
260 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000261 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000262 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000263
Anders Carlsson307846f2009-07-23 03:17:50 +0000264 LayoutBitField(D, FieldOffset);
265 return true;
266 }
Mike Stump11289f42009-09-09 15:08:12 +0000267
Anders Carlssone8bfe412010-02-02 05:17:25 +0000268 // Check if we have a pointer to data member in this field.
269 CheckForPointerToDataMember(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000270
Anders Carlsson307846f2009-07-23 03:17:50 +0000271 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000272 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000273
Anders Carlsson19702bb2009-08-04 16:29:15 +0000274 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
275 unsigned TypeAlignment = getTypeAlignment(Ty);
276
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000277 // If the type alignment is larger then the struct alignment, we must use
278 // a packed struct.
279 if (TypeAlignment > Alignment) {
280 assert(!Packed && "Alignment is wrong even with packed struct!");
281 return false;
282 }
Mike Stump11289f42009-09-09 15:08:12 +0000283
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000284 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
285 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
286 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
287 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
288 return false;
289 }
290 }
291
Anders Carlsson19702bb2009-08-04 16:29:15 +0000292 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000293 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000294 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
295
296 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
297 assert(!Packed && "Could not place field even with packed struct!");
298 return false;
299 }
Mike Stump11289f42009-09-09 15:08:12 +0000300
Anders Carlsson19702bb2009-08-04 16:29:15 +0000301 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
302 // Even with alignment, the field offset is not at the right place,
303 // insert padding.
304 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump11289f42009-09-09 15:08:12 +0000305
Anders Carlsson19702bb2009-08-04 16:29:15 +0000306 AppendBytes(PaddingInBytes);
307 }
Mike Stump11289f42009-09-09 15:08:12 +0000308
Anders Carlsson307846f2009-07-23 03:17:50 +0000309 // Now append the field.
310 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000311 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000312
Anders Carlsson307846f2009-07-23 03:17:50 +0000313 return true;
314}
315
Anders Carlsson1de2f572010-04-17 20:49:27 +0000316const llvm::Type *
317CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
318 const ASTRecordLayout &Layout) {
319 if (Field->isBitField()) {
320 uint64_t FieldSize =
321 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
322
323 // Ignore zero sized bit fields.
324 if (FieldSize == 0)
325 return 0;
326
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000327 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
328 unsigned NumBytesToAppend =
329 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlsson2295f132010-04-17 21:04:52 +0000330
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000331 if (NumBytesToAppend > 1)
332 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlsson2295f132010-04-17 21:04:52 +0000333
Anders Carlsson1de2f572010-04-17 20:49:27 +0000334 // Add the bit field info.
335 LLVMBitFields.push_back(
336 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000337 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000338 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000339
Anders Carlsson1de2f572010-04-17 20:49:27 +0000340 // This is a regular union field.
341 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
342 return Types.ConvertTypeForMemRecursive(Field->getType());
343}
344
Anders Carlsson697f6592009-07-23 03:43:54 +0000345void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
346 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000347
Anders Carlsson697f6592009-07-23 03:43:54 +0000348 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000349
Anders Carlsson697f6592009-07-23 03:43:54 +0000350 const llvm::Type *Ty = 0;
351 uint64_t Size = 0;
352 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000354 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000355
Anders Carlsson697f6592009-07-23 03:43:54 +0000356 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000357 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000358 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000359 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000360 "Union field offset did not start at the beginning of record!");
Anders Carlsson1de2f572010-04-17 20:49:27 +0000361 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000362
Anders Carlsson1de2f572010-04-17 20:49:27 +0000363 if (!FieldTy)
364 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000365
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000366 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000367
Anders Carlssone2accf42009-07-23 21:52:03 +0000368 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
369 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000370
Anders Carlsson697f6592009-07-23 03:43:54 +0000371 if (FieldAlign < Align)
372 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000373
Anders Carlsson697f6592009-07-23 03:43:54 +0000374 if (FieldAlign > Align || FieldSize > Size) {
375 Ty = FieldTy;
376 Align = FieldAlign;
377 Size = FieldSize;
378 }
379 }
Mike Stump11289f42009-09-09 15:08:12 +0000380
Anders Carlsson697f6592009-07-23 03:43:54 +0000381 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000382 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000383 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000384
385 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
386 // We need a packed struct.
387 Packed = true;
388 Align = 1;
389 }
390 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000391 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000392 assert(HasOnlyZeroSizedBitFields &&
393 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000394 Align = 1;
395 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000396
Anders Carlsson697f6592009-07-23 03:43:54 +0000397 // Append tail padding.
398 if (Layout.getSize() / 8 > Size)
399 AppendPadding(Layout.getSize() / 8, Align);
400}
401
Anders Carlssond681a292009-12-16 17:27:20 +0000402void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
403 const ASTRecordLayout &Layout) {
404 // Check if we need to add a vtable pointer.
405 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000406 const llvm::Type *Int8PtrTy =
Anders Carlssond681a292009-12-16 17:27:20 +0000407 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000408
Anders Carlssond681a292009-12-16 17:27:20 +0000409 assert(NextFieldOffsetInBytes == 0 &&
Anders Carlsson11e51402010-04-17 20:15:18 +0000410 "VTable pointer must come first!");
Anders Carlssond681a292009-12-16 17:27:20 +0000411 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
412 }
413}
414
Anders Carlsson307846f2009-07-23 03:17:50 +0000415bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
416 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000417 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000418
Anders Carlsson697f6592009-07-23 03:43:54 +0000419 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000420
Anders Carlssond681a292009-12-16 17:27:20 +0000421 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
422 LayoutBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000423
Anders Carlsson307846f2009-07-23 03:17:50 +0000424 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000425
Mike Stump11289f42009-09-09 15:08:12 +0000426 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000427 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
428 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000429 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000430 "Could not layout fields even with a packed LLVM struct!");
431 return false;
432 }
433 }
434
435 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000436 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000437
Anders Carlsson307846f2009-07-23 03:17:50 +0000438 return true;
439}
440
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000441void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
442 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000443
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000444 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000445 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000446
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000447 uint64_t AlignedNextFieldOffset =
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000448 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
449
450 if (AlignedNextFieldOffset == RecordSizeInBytes) {
451 // We don't need any padding.
452 return;
453 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000454
Anders Carlssond5d64132009-07-28 17:56:36 +0000455 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000456 AppendBytes(NumPadBytes);
457}
458
Mike Stump11289f42009-09-09 15:08:12 +0000459void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000460 const llvm::Type *FieldTy) {
461 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
462 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000463
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000464 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000465
Anders Carlsson307846f2009-07-23 03:17:50 +0000466 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000467
Anders Carlssond5d64132009-07-28 17:56:36 +0000468 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000469 BitsAvailableInLastField = 0;
470}
471
Mike Stump11289f42009-09-09 15:08:12 +0000472void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000473 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000474 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
475 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000476
Anders Carlsson307846f2009-07-23 03:17:50 +0000477 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000478 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson307846f2009-07-23 03:17:50 +0000479 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
480
481 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
482 // Even with alignment, the field offset is not at the right place,
483 // insert padding.
484 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
485
486 AppendBytes(PaddingInBytes);
487 }
488}
489
490void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
491 if (NumBytes == 0)
492 return;
Mike Stump11289f42009-09-09 15:08:12 +0000493
Owen Anderson41a75022009-08-13 21:57:51 +0000494 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000495 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000496 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000497
Anders Carlsson307846f2009-07-23 03:17:50 +0000498 // Append the padding field
Anders Carlssond5d64132009-07-28 17:56:36 +0000499 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000500}
501
502unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000503 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000504 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000505
Anders Carlsson307846f2009-07-23 03:17:50 +0000506 return Types.getTargetData().getABITypeAlignment(Ty);
507}
508
Anders Carlssone8bfe412010-02-02 05:17:25 +0000509void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000510 // This record already contains a member pointer.
Anders Carlssone8bfe412010-02-02 05:17:25 +0000511 if (ContainsPointerToDataMember)
Anders Carlssond606de72009-08-23 01:25:01 +0000512 return;
Mike Stump11289f42009-09-09 15:08:12 +0000513
Anders Carlssond606de72009-08-23 01:25:01 +0000514 // Can only have member pointers if we're compiling C++.
515 if (!Types.getContext().getLangOptions().CPlusPlus)
516 return;
Mike Stump11289f42009-09-09 15:08:12 +0000517
Anders Carlssone8bfe412010-02-02 05:17:25 +0000518 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000519
Anders Carlssone8bfe412010-02-02 05:17:25 +0000520 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
521 if (!MPT->getPointeeType()->isFunctionType()) {
522 // We have a pointer to data member.
523 ContainsPointerToDataMember = true;
524 }
525 } else if (const RecordType *RT = T->getAs<RecordType>()) {
526 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000527
Anders Carlssone8bfe412010-02-02 05:17:25 +0000528 // FIXME: It would be better if there was a way to explicitly compute the
529 // record layout instead of converting to a type.
530 Types.ConvertTagDeclType(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000531
Anders Carlssone8bfe412010-02-02 05:17:25 +0000532 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000533
Anders Carlssone8bfe412010-02-02 05:17:25 +0000534 if (Layout.containsPointerToDataMember())
535 ContainsPointerToDataMember = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000536 }
Anders Carlssond606de72009-08-23 01:25:01 +0000537}
538
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000539CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
540 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000541
Anders Carlsson307846f2009-07-23 03:17:50 +0000542 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000543
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000544 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson758428f2009-08-05 23:18:46 +0000545 Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000546 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000547
Daniel Dunbar034299e2010-03-31 01:09:11 +0000548 CGRecordLayout *RL =
549 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
550
Anders Carlsson307846f2009-07-23 03:17:50 +0000551 // Add all the field numbers.
Daniel Dunbard4549102010-04-06 01:07:41 +0000552 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
553 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson307846f2009-07-23 03:17:50 +0000554
555 // Add bitfield info.
Daniel Dunbard4549102010-04-06 01:07:41 +0000556 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
557 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000558
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000559 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000560 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000561 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000562 llvm::errs() << "Record: ";
563 D->dump();
564 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000565 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000566 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000567
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000568 // Verify that the computed LLVM struct size matches the AST layout size.
569 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
570 getTargetData().getTypeAllocSize(Ty) &&
571 "Type size mismatch!");
572
Daniel Dunbar2ba67442010-04-21 19:10:49 +0000573 // Verify that the LLVM and AST field offsets agree.
574#ifndef NDEBUG
575 const llvm::StructType *ST =
576 dyn_cast<llvm::StructType>(RL->getLLVMType());
577 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST);
578
579 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D);
580 RecordDecl::field_iterator it = D->field_begin();
581 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) {
582 const FieldDecl *FD = *it;
583 if (FD->isBitField()) {
584 // FIXME: Verify assorted things.
585 } else {
586 unsigned FieldNo = RL->getLLVMFieldNo(FD);
587 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) &&
588 "Invalid field offset!");
589 }
590 }
591#endif
Daniel Dunbar2ea51832010-04-19 20:44:47 +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";
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000601
602 // Print bit-field infos in declaration order.
603 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs;
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000604 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
605 it = BitFields.begin(), ie = BitFields.end();
606 it != ie; ++it) {
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000607 const RecordDecl *RD = it->first->getParent();
608 unsigned Index = 0;
609 for (RecordDecl::field_iterator
610 it2 = RD->field_begin(); *it2 != it->first; ++it2)
611 ++Index;
612 BFIs.push_back(std::make_pair(Index, &it->second));
613 }
614 llvm::array_pod_sort(BFIs.begin(), BFIs.end());
615 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000616 OS.indent(4);
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000617 BFIs[i].second->print(OS);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000618 OS << "\n";
619 }
Daniel Dunbarb6f4b052010-04-22 02:35:36 +0000620
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000621 OS << "]>\n";
622}
623
624void CGRecordLayout::dump() const {
625 print(llvm::errs());
626}
627
628void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
629 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000630 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000631 OS << " IsSigned:" << IsSigned << "\n";
632
633 OS.indent(4 + strlen("<CGBitFieldInfo"));
634 OS << " NumComponents:" << getNumComponents();
635 OS << " Components: [";
636 if (getNumComponents()) {
637 OS << "\n";
638 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
639 const AccessInfo &AI = getComponent(i);
640 OS.indent(8);
641 OS << "<AccessInfo"
642 << " FieldIndex:" << AI.FieldIndex
643 << " FieldByteOffset:" << AI.FieldByteOffset
644 << " FieldBitStart:" << AI.FieldBitStart
645 << " AccessWidth:" << AI.AccessWidth << "\n";
646 OS.indent(8 + strlen("<AccessInfo"));
647 OS << " AccessAlignment:" << AI.AccessAlignment
648 << " TargetBitOffset:" << AI.TargetBitOffset
649 << " TargetBitWidth:" << AI.TargetBitWidth
650 << ">\n";
651 }
652 OS.indent(4);
653 }
654 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000655}
656
657void CGBitFieldInfo::dump() const {
658 print(llvm::errs());
659}