blob: 89b62bfa5a84188f999ca4e967c7af4ebedbd21b [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
Daniel Dunbar23ee4b72010-03-31 00:11:27 +000096 /// AppendPadding - Appends enough padding bytes so that the total
97 /// struct size is a multiple of the field alignment.
98 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
99
100 /// AppendBytes - Append a given number of bytes to the record.
101 void AppendBytes(uint64_t NumBytes);
102
103 /// AppendTailPadding - Append enough tail padding so that the type will have
104 /// the passed size.
105 void AppendTailPadding(uint64_t RecordSize);
106
107 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000108
109 /// CheckForPointerToDataMember - Check if the given type contains a pointer
110 /// to data member.
111 void CheckForPointerToDataMember(QualType T);
112
113public:
114 CGRecordLayoutBuilder(CodeGenTypes &Types)
115 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
116 Alignment(0), AlignmentAsLLVMStruct(1),
117 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
118
119 /// Layout - Will layout a RecordDecl.
120 void Layout(const RecordDecl *D);
121};
122
123}
124}
125
Anders Carlsson307846f2009-07-23 03:17:50 +0000126void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000127 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000128 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000129
Anders Carlsson697f6592009-07-23 03:43:54 +0000130 if (D->isUnion()) {
131 LayoutUnion(D);
132 return;
133 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000134
Anders Carlsson307846f2009-07-23 03:17:50 +0000135 if (LayoutFields(D))
136 return;
Mike Stump11289f42009-09-09 15:08:12 +0000137
Anders Carlsson307846f2009-07-23 03:17:50 +0000138 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000139 Packed = true;
Anders Carlsson307846f2009-07-23 03:17:50 +0000140 AlignmentAsLLVMStruct = 1;
Anders Carlssond5d64132009-07-28 17:56:36 +0000141 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000142 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000143 LLVMFields.clear();
144 LLVMBitFields.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000145
Anders Carlsson307846f2009-07-23 03:17:50 +0000146 LayoutFields(D);
147}
148
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000149static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types,
150 const FieldDecl *FD,
151 uint64_t FieldOffset,
152 uint64_t FieldSize) {
153 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000154 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
155 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000156
157 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000158
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000159 if (FieldSize > TypeSizeInBits) {
Anders Carlssond5f27b02010-04-17 22:54:57 +0000160 // We have a wide bit-field. The extra bits are only used for padding, so
161 // if we have a bitfield of type T, with size N:
162 //
163 // T t : N;
164 //
165 // We can just assume that it's:
166 //
167 // T t : sizeof(T);
168 //
169 FieldSize = TypeSizeInBits;
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000170 }
171
172 unsigned StartBit = FieldOffset % TypeSizeInBits;
173
Daniel Dunbarb935b932010-04-13 20:58:55 +0000174 // The current policy is to always access the bit-field using the source type
175 // of the bit-field. With the C bit-field rules, this implies that we always
176 // use either one or two accesses, and two accesses can only occur with a
177 // packed structure when the bit-field straddles an alignment boundary.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000178 CGBitFieldInfo::AccessInfo Components[2];
179
Daniel Dunbarbb138452010-04-15 05:09:28 +0000180 unsigned LowBits = std::min(FieldSize, TypeSizeInBits - StartBit);
Daniel Dunbarb935b932010-04-13 20:58:55 +0000181 bool NeedsHighAccess = LowBits != FieldSize;
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000182 unsigned NumComponents = 1 + NeedsHighAccess;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000183
184 // FIXME: This access policy is probably wrong on big-endian systems.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000185 CGBitFieldInfo::AccessInfo &LowAccess = Components[0];
Daniel Dunbarb935b932010-04-13 20:58:55 +0000186 LowAccess.FieldIndex = 0;
187 LowAccess.FieldByteOffset =
188 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
Daniel Dunbarbb138452010-04-15 05:09:28 +0000189 LowAccess.FieldBitStart = StartBit;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000190 LowAccess.AccessWidth = TypeSizeInBits;
191 // FIXME: This might be wrong!
192 LowAccess.AccessAlignment = 0;
193 LowAccess.TargetBitOffset = 0;
194 LowAccess.TargetBitWidth = LowBits;
195
196 if (NeedsHighAccess) {
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000197 CGBitFieldInfo::AccessInfo &HighAccess = Components[1];
Daniel Dunbarb935b932010-04-13 20:58:55 +0000198 HighAccess.FieldIndex = 0;
199 HighAccess.FieldByteOffset = LowAccess.FieldByteOffset + TypeSizeInBytes;
200 HighAccess.FieldBitStart = 0;
201 HighAccess.AccessWidth = TypeSizeInBits;
202 // FIXME: This might be wrong!
203 HighAccess.AccessAlignment = 0;
204 HighAccess.TargetBitOffset = LowBits;
205 HighAccess.TargetBitWidth = FieldSize - LowBits;
206 }
207
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000208 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000209}
210
Anders Carlsson307846f2009-07-23 03:17:50 +0000211void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
212 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000213 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000214 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000215
Anders Carlsson307846f2009-07-23 03:17:50 +0000216 if (FieldSize == 0)
217 return;
218
Anders Carlssond5d64132009-07-28 17:56:36 +0000219 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000220 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000221
Anders Carlsson307846f2009-07-23 03:17:50 +0000222 if (FieldOffset < NextFieldOffset) {
223 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000224 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000225
Anders Carlsson307846f2009-07-23 03:17:50 +0000226 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000227 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000228 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
229 } else {
230 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
231
232 // Append padding if necessary.
233 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump11289f42009-09-09 15:08:12 +0000234
235 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000236 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000237
Anders Carlsson307846f2009-07-23 03:17:50 +0000238 assert(NumBytesToAppend && "No bytes to append!");
239 }
240
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000241 // Add the bit field info.
242 LLVMBitFields.push_back(
243 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000244
Anders Carlsson307846f2009-07-23 03:17:50 +0000245 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000246
Mike Stump11289f42009-09-09 15:08:12 +0000247 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000248 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000249}
250
251bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
252 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000253 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000254 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000255 return false;
256
257 if (D->isBitField()) {
258 // We must use packed structs for unnamed bit fields since they
259 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000260 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000261 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000262
Anders Carlsson307846f2009-07-23 03:17:50 +0000263 LayoutBitField(D, FieldOffset);
264 return true;
265 }
Mike Stump11289f42009-09-09 15:08:12 +0000266
Anders Carlssone8bfe412010-02-02 05:17:25 +0000267 // Check if we have a pointer to data member in this field.
268 CheckForPointerToDataMember(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000269
Anders Carlsson307846f2009-07-23 03:17:50 +0000270 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000271 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Anders Carlsson19702bb2009-08-04 16:29:15 +0000273 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
274 unsigned TypeAlignment = getTypeAlignment(Ty);
275
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000276 // If the type alignment is larger then the struct alignment, we must use
277 // a packed struct.
278 if (TypeAlignment > Alignment) {
279 assert(!Packed && "Alignment is wrong even with packed struct!");
280 return false;
281 }
Mike Stump11289f42009-09-09 15:08:12 +0000282
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000283 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
284 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
285 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
286 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
287 return false;
288 }
289 }
290
Anders Carlsson19702bb2009-08-04 16:29:15 +0000291 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000292 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000293 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
294
295 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
296 assert(!Packed && "Could not place field even with packed struct!");
297 return false;
298 }
Mike Stump11289f42009-09-09 15:08:12 +0000299
Anders Carlsson19702bb2009-08-04 16:29:15 +0000300 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
301 // Even with alignment, the field offset is not at the right place,
302 // insert padding.
303 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump11289f42009-09-09 15:08:12 +0000304
Anders Carlsson19702bb2009-08-04 16:29:15 +0000305 AppendBytes(PaddingInBytes);
306 }
Mike Stump11289f42009-09-09 15:08:12 +0000307
Anders Carlsson307846f2009-07-23 03:17:50 +0000308 // Now append the field.
309 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000310 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000311
Anders Carlsson307846f2009-07-23 03:17:50 +0000312 return true;
313}
314
Anders Carlsson1de2f572010-04-17 20:49:27 +0000315const llvm::Type *
316CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field,
317 const ASTRecordLayout &Layout) {
318 if (Field->isBitField()) {
319 uint64_t FieldSize =
320 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
321
322 // Ignore zero sized bit fields.
323 if (FieldSize == 0)
324 return 0;
325
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000326 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
327 unsigned NumBytesToAppend =
328 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Anders Carlsson2295f132010-04-17 21:04:52 +0000329
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000330 if (NumBytesToAppend > 1)
331 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
Anders Carlsson2295f132010-04-17 21:04:52 +0000332
Anders Carlsson1de2f572010-04-17 20:49:27 +0000333 // Add the bit field info.
334 LLVMBitFields.push_back(
335 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
Anders Carlsson2295f132010-04-17 21:04:52 +0000336 return FieldTy;
Anders Carlsson1de2f572010-04-17 20:49:27 +0000337 }
Daniel Dunbar20b551a2010-04-20 17:52:30 +0000338
Anders Carlsson1de2f572010-04-17 20:49:27 +0000339 // This is a regular union field.
340 LLVMFields.push_back(LLVMFieldInfo(Field, 0));
341 return Types.ConvertTypeForMemRecursive(Field->getType());
342}
343
Anders Carlsson697f6592009-07-23 03:43:54 +0000344void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
345 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000346
Anders Carlsson697f6592009-07-23 03:43:54 +0000347 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000348
Anders Carlsson697f6592009-07-23 03:43:54 +0000349 const llvm::Type *Ty = 0;
350 uint64_t Size = 0;
351 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000353 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000354
Anders Carlsson697f6592009-07-23 03:43:54 +0000355 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000356 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000357 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000358 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000359 "Union field offset did not start at the beginning of record!");
Anders Carlsson1de2f572010-04-17 20:49:27 +0000360 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout);
Anders Carlssonf814ee62009-07-23 04:00:39 +0000361
Anders Carlsson1de2f572010-04-17 20:49:27 +0000362 if (!FieldTy)
363 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000364
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000365 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000366
Anders Carlssone2accf42009-07-23 21:52:03 +0000367 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
368 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000369
Anders Carlsson697f6592009-07-23 03:43:54 +0000370 if (FieldAlign < Align)
371 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000372
Anders Carlsson697f6592009-07-23 03:43:54 +0000373 if (FieldAlign > Align || FieldSize > Size) {
374 Ty = FieldTy;
375 Align = FieldAlign;
376 Size = FieldSize;
377 }
378 }
Mike Stump11289f42009-09-09 15:08:12 +0000379
Anders Carlsson697f6592009-07-23 03:43:54 +0000380 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000381 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000382 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000383
384 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
385 // We need a packed struct.
386 Packed = true;
387 Align = 1;
388 }
389 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000390 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000391 assert(HasOnlyZeroSizedBitFields &&
392 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000393 Align = 1;
394 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000395
Anders Carlsson697f6592009-07-23 03:43:54 +0000396 // Append tail padding.
397 if (Layout.getSize() / 8 > Size)
398 AppendPadding(Layout.getSize() / 8, Align);
399}
400
Anders Carlssond681a292009-12-16 17:27:20 +0000401void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
402 const ASTRecordLayout &Layout) {
403 // Check if we need to add a vtable pointer.
404 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000405 const llvm::Type *Int8PtrTy =
Anders Carlssond681a292009-12-16 17:27:20 +0000406 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000407
Anders Carlssond681a292009-12-16 17:27:20 +0000408 assert(NextFieldOffsetInBytes == 0 &&
Anders Carlsson11e51402010-04-17 20:15:18 +0000409 "VTable pointer must come first!");
Anders Carlssond681a292009-12-16 17:27:20 +0000410 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
411 }
412}
413
Anders Carlsson307846f2009-07-23 03:17:50 +0000414bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
415 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000416 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000417
Anders Carlsson697f6592009-07-23 03:43:54 +0000418 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000419
Anders Carlssond681a292009-12-16 17:27:20 +0000420 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
421 LayoutBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000422
Anders Carlsson307846f2009-07-23 03:17:50 +0000423 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000424
Mike Stump11289f42009-09-09 15:08:12 +0000425 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000426 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
427 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000428 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000429 "Could not layout fields even with a packed LLVM struct!");
430 return false;
431 }
432 }
433
434 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000435 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000436
Anders Carlsson307846f2009-07-23 03:17:50 +0000437 return true;
438}
439
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000440void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
441 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000442
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000443 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000444 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000445
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000446 uint64_t AlignedNextFieldOffset =
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000447 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
448
449 if (AlignedNextFieldOffset == RecordSizeInBytes) {
450 // We don't need any padding.
451 return;
452 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000453
Anders Carlssond5d64132009-07-28 17:56:36 +0000454 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000455 AppendBytes(NumPadBytes);
456}
457
Mike Stump11289f42009-09-09 15:08:12 +0000458void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000459 const llvm::Type *FieldTy) {
460 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
461 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000462
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000463 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000464
Anders Carlsson307846f2009-07-23 03:17:50 +0000465 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000466
Anders Carlssond5d64132009-07-28 17:56:36 +0000467 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000468 BitsAvailableInLastField = 0;
469}
470
Mike Stump11289f42009-09-09 15:08:12 +0000471void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000472 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000473 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
474 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000475
Anders Carlsson307846f2009-07-23 03:17:50 +0000476 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000477 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson307846f2009-07-23 03:17:50 +0000478 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
479
480 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
481 // Even with alignment, the field offset is not at the right place,
482 // insert padding.
483 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
484
485 AppendBytes(PaddingInBytes);
486 }
487}
488
489void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
490 if (NumBytes == 0)
491 return;
Mike Stump11289f42009-09-09 15:08:12 +0000492
Owen Anderson41a75022009-08-13 21:57:51 +0000493 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000494 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000495 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000496
Anders Carlsson307846f2009-07-23 03:17:50 +0000497 // Append the padding field
Anders Carlssond5d64132009-07-28 17:56:36 +0000498 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000499}
500
501unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000502 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000503 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000504
Anders Carlsson307846f2009-07-23 03:17:50 +0000505 return Types.getTargetData().getABITypeAlignment(Ty);
506}
507
Anders Carlssone8bfe412010-02-02 05:17:25 +0000508void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000509 // This record already contains a member pointer.
Anders Carlssone8bfe412010-02-02 05:17:25 +0000510 if (ContainsPointerToDataMember)
Anders Carlssond606de72009-08-23 01:25:01 +0000511 return;
Mike Stump11289f42009-09-09 15:08:12 +0000512
Anders Carlssond606de72009-08-23 01:25:01 +0000513 // Can only have member pointers if we're compiling C++.
514 if (!Types.getContext().getLangOptions().CPlusPlus)
515 return;
Mike Stump11289f42009-09-09 15:08:12 +0000516
Anders Carlssone8bfe412010-02-02 05:17:25 +0000517 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000518
Anders Carlssone8bfe412010-02-02 05:17:25 +0000519 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
520 if (!MPT->getPointeeType()->isFunctionType()) {
521 // We have a pointer to data member.
522 ContainsPointerToDataMember = true;
523 }
524 } else if (const RecordType *RT = T->getAs<RecordType>()) {
525 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000526
Anders Carlssone8bfe412010-02-02 05:17:25 +0000527 // FIXME: It would be better if there was a way to explicitly compute the
528 // record layout instead of converting to a type.
529 Types.ConvertTagDeclType(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000530
Anders Carlssone8bfe412010-02-02 05:17:25 +0000531 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000532
Anders Carlssone8bfe412010-02-02 05:17:25 +0000533 if (Layout.containsPointerToDataMember())
534 ContainsPointerToDataMember = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000535 }
Anders Carlssond606de72009-08-23 01:25:01 +0000536}
537
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000538CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
539 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000540
Anders Carlsson307846f2009-07-23 03:17:50 +0000541 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000542
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000543 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson758428f2009-08-05 23:18:46 +0000544 Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000545 Builder.Packed);
Mike Stump11289f42009-09-09 15:08:12 +0000546
Daniel Dunbar034299e2010-03-31 01:09:11 +0000547 CGRecordLayout *RL =
548 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
549
Anders Carlsson307846f2009-07-23 03:17:50 +0000550 // Add all the field numbers.
Daniel Dunbard4549102010-04-06 01:07:41 +0000551 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
552 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson307846f2009-07-23 03:17:50 +0000553
554 // Add bitfield info.
Daniel Dunbard4549102010-04-06 01:07:41 +0000555 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
556 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000557
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000558 // Dump the layout, if requested.
Daniel Dunbarb935b932010-04-13 20:58:55 +0000559 if (getContext().getLangOptions().DumpRecordLayouts) {
Daniel Dunbarccabe482010-04-19 20:44:53 +0000560 llvm::errs() << "\n*** Dumping IRgen Record Layout\n";
Daniel Dunbarb935b932010-04-13 20:58:55 +0000561 llvm::errs() << "Record: ";
562 D->dump();
563 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000564 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000565 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000566
Daniel Dunbar2ea51832010-04-19 20:44:47 +0000567 // Verify that the computed LLVM struct size matches the AST layout size.
568 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
569 getTargetData().getTypeAllocSize(Ty) &&
570 "Type size mismatch!");
571
572 // FIXME: We should verify the individual field offsets here as well.
573
Daniel Dunbar034299e2010-03-31 01:09:11 +0000574 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000575}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000576
577void CGRecordLayout::print(llvm::raw_ostream &OS) const {
578 OS << "<CGRecordLayout\n";
579 OS << " LLVMType:" << *LLVMType << "\n";
580 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
581 OS << " BitFields:[\n";
582 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
583 it = BitFields.begin(), ie = BitFields.end();
584 it != ie; ++it) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000585 OS.indent(4);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000586 it->second.print(OS);
587 OS << "\n";
588 }
589 OS << "]>\n";
590}
591
592void CGRecordLayout::dump() const {
593 print(llvm::errs());
594}
595
596void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
597 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000598 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000599 OS << " IsSigned:" << IsSigned << "\n";
600
601 OS.indent(4 + strlen("<CGBitFieldInfo"));
602 OS << " NumComponents:" << getNumComponents();
603 OS << " Components: [";
604 if (getNumComponents()) {
605 OS << "\n";
606 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
607 const AccessInfo &AI = getComponent(i);
608 OS.indent(8);
609 OS << "<AccessInfo"
610 << " FieldIndex:" << AI.FieldIndex
611 << " FieldByteOffset:" << AI.FieldByteOffset
612 << " FieldBitStart:" << AI.FieldBitStart
613 << " AccessWidth:" << AI.AccessWidth << "\n";
614 OS.indent(8 + strlen("<AccessInfo"));
615 OS << " AccessAlignment:" << AI.AccessAlignment
616 << " TargetBitOffset:" << AI.TargetBitOffset
617 << " TargetBitWidth:" << AI.TargetBitWidth
618 << ">\n";
619 }
620 OS.indent(4);
621 }
622 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000623}
624
625void CGBitFieldInfo::dump() const {
626 print(llvm::errs());
627}