blob: 141ddd7f2bdfe88cddacef623187fbe9bee6e58b [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
71 /// LayoutUnion - Will layout a union RecordDecl.
72 void LayoutUnion(const RecordDecl *D);
73
74 /// LayoutField - try to layout all fields in the record decl.
75 /// Returns false if the operation failed because the struct is not packed.
76 bool LayoutFields(const RecordDecl *D);
77
78 /// LayoutBases - layout the bases and vtable pointer of a record decl.
79 void LayoutBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout);
80
81 /// LayoutField - layout a single field. Returns false if the operation failed
82 /// because the current struct is not packed.
83 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset);
84
85 /// LayoutBitField - layout a single bit field.
86 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset);
87
88 /// AppendField - Appends a field with the given offset and type.
89 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
90
91 /// AppendPadding - Appends enough padding bytes so that the total struct
92 /// size matches the alignment of the passed in type.
93 void AppendPadding(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy);
94
95 /// AppendPadding - Appends enough padding bytes so that the total
96 /// struct size is a multiple of the field alignment.
97 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment);
98
99 /// AppendBytes - Append a given number of bytes to the record.
100 void AppendBytes(uint64_t NumBytes);
101
102 /// AppendTailPadding - Append enough tail padding so that the type will have
103 /// the passed size.
104 void AppendTailPadding(uint64_t RecordSize);
105
106 unsigned getTypeAlignment(const llvm::Type *Ty) const;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000107
108 /// CheckForPointerToDataMember - Check if the given type contains a pointer
109 /// to data member.
110 void CheckForPointerToDataMember(QualType T);
111
112public:
113 CGRecordLayoutBuilder(CodeGenTypes &Types)
114 : ContainsPointerToDataMember(false), Packed(false), Types(Types),
115 Alignment(0), AlignmentAsLLVMStruct(1),
116 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { }
117
118 /// Layout - Will layout a RecordDecl.
119 void Layout(const RecordDecl *D);
120};
121
122}
123}
124
Anders Carlsson307846f2009-07-23 03:17:50 +0000125void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000126 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlsson09a37742009-09-02 17:51:33 +0000127 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000128
Anders Carlsson697f6592009-07-23 03:43:54 +0000129 if (D->isUnion()) {
130 LayoutUnion(D);
131 return;
132 }
Anders Carlsson68e0b682009-08-08 18:23:56 +0000133
Anders Carlsson307846f2009-07-23 03:17:50 +0000134 if (LayoutFields(D))
135 return;
Mike Stump11289f42009-09-09 15:08:12 +0000136
Anders Carlsson307846f2009-07-23 03:17:50 +0000137 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +0000138 Packed = true;
Anders Carlsson307846f2009-07-23 03:17:50 +0000139 AlignmentAsLLVMStruct = 1;
Anders Carlssond5d64132009-07-28 17:56:36 +0000140 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +0000141 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +0000142 LLVMFields.clear();
143 LLVMBitFields.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000144
Anders Carlsson307846f2009-07-23 03:17:50 +0000145 LayoutFields(D);
146}
147
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000148static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types,
149 const FieldDecl *FD,
150 uint64_t FieldOffset,
151 uint64_t FieldSize) {
152 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType());
Daniel Dunbarb935b932010-04-13 20:58:55 +0000153 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty);
154 uint64_t TypeSizeInBits = TypeSizeInBytes * 8;
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000155
156 bool IsSigned = FD->getType()->isSignedIntegerType();
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000157
Anders Carlssonbe6f3182010-04-16 16:23:02 +0000158 if (FieldSize > TypeSizeInBits) {
159 // We have a wide bit-field.
160
161 CGBitFieldInfo::AccessInfo Component;
162
163 Component.FieldIndex = 0;
164 Component.FieldByteOffset =
165 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
166 Component.FieldBitStart = 0;
167 Component.AccessWidth = TypeSizeInBits;
168 // FIXME: This might be wrong!
169 Component.AccessAlignment = 0;
170 Component.TargetBitOffset = 0;
171 Component.TargetBitWidth = TypeSizeInBits;
172
173 return CGBitFieldInfo(TypeSizeInBits, 1, &Component, IsSigned);
174 }
175
176 unsigned StartBit = FieldOffset % TypeSizeInBits;
177
Daniel Dunbarb935b932010-04-13 20:58:55 +0000178 // The current policy is to always access the bit-field using the source type
179 // of the bit-field. With the C bit-field rules, this implies that we always
180 // use either one or two accesses, and two accesses can only occur with a
181 // packed structure when the bit-field straddles an alignment boundary.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000182 CGBitFieldInfo::AccessInfo Components[2];
183
Daniel Dunbarbb138452010-04-15 05:09:28 +0000184 unsigned LowBits = std::min(FieldSize, TypeSizeInBits - StartBit);
Daniel Dunbarb935b932010-04-13 20:58:55 +0000185 bool NeedsHighAccess = LowBits != FieldSize;
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000186 unsigned NumComponents = 1 + NeedsHighAccess;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000187
188 // FIXME: This access policy is probably wrong on big-endian systems.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000189 CGBitFieldInfo::AccessInfo &LowAccess = Components[0];
Daniel Dunbarb935b932010-04-13 20:58:55 +0000190 LowAccess.FieldIndex = 0;
191 LowAccess.FieldByteOffset =
192 TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
Daniel Dunbarbb138452010-04-15 05:09:28 +0000193 LowAccess.FieldBitStart = StartBit;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000194 LowAccess.AccessWidth = TypeSizeInBits;
195 // FIXME: This might be wrong!
196 LowAccess.AccessAlignment = 0;
197 LowAccess.TargetBitOffset = 0;
198 LowAccess.TargetBitWidth = LowBits;
199
200 if (NeedsHighAccess) {
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000201 CGBitFieldInfo::AccessInfo &HighAccess = Components[1];
Daniel Dunbarb935b932010-04-13 20:58:55 +0000202 HighAccess.FieldIndex = 0;
203 HighAccess.FieldByteOffset = LowAccess.FieldByteOffset + TypeSizeInBytes;
204 HighAccess.FieldBitStart = 0;
205 HighAccess.AccessWidth = TypeSizeInBits;
206 // FIXME: This might be wrong!
207 HighAccess.AccessAlignment = 0;
208 HighAccess.TargetBitOffset = LowBits;
209 HighAccess.TargetBitWidth = FieldSize - LowBits;
210 }
211
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000212 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000213}
214
Anders Carlsson307846f2009-07-23 03:17:50 +0000215void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
216 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000217 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000218 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000219
Anders Carlsson307846f2009-07-23 03:17:50 +0000220 if (FieldSize == 0)
221 return;
222
Anders Carlssond5d64132009-07-28 17:56:36 +0000223 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000224 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000225
Anders Carlsson307846f2009-07-23 03:17:50 +0000226 if (FieldOffset < NextFieldOffset) {
227 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000228 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000229
Anders Carlsson307846f2009-07-23 03:17:50 +0000230 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000231 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000232 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
233 } else {
234 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
235
236 // Append padding if necessary.
237 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump11289f42009-09-09 15:08:12 +0000238
239 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000240 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000241
Anders Carlsson307846f2009-07-23 03:17:50 +0000242 assert(NumBytesToAppend && "No bytes to append!");
243 }
244
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000245 // Add the bit field info.
246 LLVMBitFields.push_back(
247 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000248
Anders Carlsson307846f2009-07-23 03:17:50 +0000249 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000250
Mike Stump11289f42009-09-09 15:08:12 +0000251 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000252 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000253}
254
255bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
256 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000257 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000258 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000259 return false;
260
261 if (D->isBitField()) {
262 // We must use packed structs for unnamed bit fields since they
263 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000264 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000265 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000266
Anders Carlsson307846f2009-07-23 03:17:50 +0000267 LayoutBitField(D, FieldOffset);
268 return true;
269 }
Mike Stump11289f42009-09-09 15:08:12 +0000270
Anders Carlssone8bfe412010-02-02 05:17:25 +0000271 // Check if we have a pointer to data member in this field.
272 CheckForPointerToDataMember(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000273
Anders Carlsson307846f2009-07-23 03:17:50 +0000274 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000275 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000276
Anders Carlsson19702bb2009-08-04 16:29:15 +0000277 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
278 unsigned TypeAlignment = getTypeAlignment(Ty);
279
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000280 // If the type alignment is larger then the struct alignment, we must use
281 // a packed struct.
282 if (TypeAlignment > Alignment) {
283 assert(!Packed && "Alignment is wrong even with packed struct!");
284 return false;
285 }
Mike Stump11289f42009-09-09 15:08:12 +0000286
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000287 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
288 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
289 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
290 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
291 return false;
292 }
293 }
294
Anders Carlsson19702bb2009-08-04 16:29:15 +0000295 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000296 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000297 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
298
299 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
300 assert(!Packed && "Could not place field even with packed struct!");
301 return false;
302 }
Mike Stump11289f42009-09-09 15:08:12 +0000303
Anders Carlsson19702bb2009-08-04 16:29:15 +0000304 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
305 // Even with alignment, the field offset is not at the right place,
306 // insert padding.
307 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump11289f42009-09-09 15:08:12 +0000308
Anders Carlsson19702bb2009-08-04 16:29:15 +0000309 AppendBytes(PaddingInBytes);
310 }
Mike Stump11289f42009-09-09 15:08:12 +0000311
Anders Carlsson307846f2009-07-23 03:17:50 +0000312 // Now append the field.
313 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000314 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000315
Anders Carlsson307846f2009-07-23 03:17:50 +0000316 return true;
317}
318
Anders Carlsson697f6592009-07-23 03:43:54 +0000319void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
320 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000321
Anders Carlsson697f6592009-07-23 03:43:54 +0000322 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000323
Anders Carlsson697f6592009-07-23 03:43:54 +0000324 const llvm::Type *Ty = 0;
325 uint64_t Size = 0;
326 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000327
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000328 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000329
Anders Carlsson697f6592009-07-23 03:43:54 +0000330 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000331 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000332 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000333 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000334 "Union field offset did not start at the beginning of record!");
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000335 const llvm::Type *FieldTy =
336 Types.ConvertTypeForMemRecursive(Field->getType());
Anders Carlssonf814ee62009-07-23 04:00:39 +0000337
338 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +0000339 uint64_t FieldSize =
Anders Carlssonf814ee62009-07-23 04:00:39 +0000340 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000341
Anders Carlssonf814ee62009-07-23 04:00:39 +0000342 // Ignore zero sized bit fields.
343 if (FieldSize == 0)
344 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000345
Anders Carlsson08539542009-07-23 22:52:34 +0000346 // Add the bit field info.
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000347 LLVMBitFields.push_back(
348 LLVMBitFieldInfo(*Field, ComputeBitFieldInfo(Types, *Field,
349 0, FieldSize)));
Daniel Dunbare75a64f2010-03-31 00:55:13 +0000350 } else {
351 LLVMFields.push_back(LLVMFieldInfo(*Field, 0));
352 }
Mike Stump11289f42009-09-09 15:08:12 +0000353
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000354 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000355
Anders Carlssone2accf42009-07-23 21:52:03 +0000356 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
357 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000358
Anders Carlsson697f6592009-07-23 03:43:54 +0000359 if (FieldAlign < Align)
360 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Anders Carlsson697f6592009-07-23 03:43:54 +0000362 if (FieldAlign > Align || FieldSize > Size) {
363 Ty = FieldTy;
364 Align = FieldAlign;
365 Size = FieldSize;
366 }
367 }
Mike Stump11289f42009-09-09 15:08:12 +0000368
Anders Carlsson697f6592009-07-23 03:43:54 +0000369 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000370 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000371 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000372
373 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
374 // We need a packed struct.
375 Packed = true;
376 Align = 1;
377 }
378 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000379 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000380 assert(HasOnlyZeroSizedBitFields &&
381 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000382 Align = 1;
383 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000384
Anders Carlsson697f6592009-07-23 03:43:54 +0000385 // Append tail padding.
386 if (Layout.getSize() / 8 > Size)
387 AppendPadding(Layout.getSize() / 8, Align);
388}
389
Anders Carlssond681a292009-12-16 17:27:20 +0000390void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
391 const ASTRecordLayout &Layout) {
392 // Check if we need to add a vtable pointer.
393 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000394 const llvm::Type *Int8PtrTy =
Anders Carlssond681a292009-12-16 17:27:20 +0000395 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000396
Anders Carlssond681a292009-12-16 17:27:20 +0000397 assert(NextFieldOffsetInBytes == 0 &&
398 "Vtable pointer must come first!");
399 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
400 }
401}
402
Anders Carlsson307846f2009-07-23 03:17:50 +0000403bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
404 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000405 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000406
Anders Carlsson697f6592009-07-23 03:43:54 +0000407 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000408
Anders Carlssond681a292009-12-16 17:27:20 +0000409 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
410 LayoutBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000411
Anders Carlsson307846f2009-07-23 03:17:50 +0000412 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000413
Mike Stump11289f42009-09-09 15:08:12 +0000414 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000415 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
416 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000417 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000418 "Could not layout fields even with a packed LLVM struct!");
419 return false;
420 }
421 }
422
423 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000424 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000425
Anders Carlsson307846f2009-07-23 03:17:50 +0000426 return true;
427}
428
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000429void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
430 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000431
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000432 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000433 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000434
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000435 uint64_t AlignedNextFieldOffset =
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000436 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
437
438 if (AlignedNextFieldOffset == RecordSizeInBytes) {
439 // We don't need any padding.
440 return;
441 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000442
Anders Carlssond5d64132009-07-28 17:56:36 +0000443 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000444 AppendBytes(NumPadBytes);
445}
446
Mike Stump11289f42009-09-09 15:08:12 +0000447void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000448 const llvm::Type *FieldTy) {
449 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
450 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000451
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000452 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000453
Anders Carlsson307846f2009-07-23 03:17:50 +0000454 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000455
Anders Carlssond5d64132009-07-28 17:56:36 +0000456 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000457 BitsAvailableInLastField = 0;
458}
459
Mike Stump11289f42009-09-09 15:08:12 +0000460void
Anders Carlsson307846f2009-07-23 03:17:50 +0000461CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
462 const llvm::Type *FieldTy) {
463 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
464}
465
Mike Stump11289f42009-09-09 15:08:12 +0000466void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000467 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000468 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
469 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000470
Anders Carlsson307846f2009-07-23 03:17:50 +0000471 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000472 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson307846f2009-07-23 03:17:50 +0000473 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
474
475 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
476 // Even with alignment, the field offset is not at the right place,
477 // insert padding.
478 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
479
480 AppendBytes(PaddingInBytes);
481 }
482}
483
484void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
485 if (NumBytes == 0)
486 return;
Mike Stump11289f42009-09-09 15:08:12 +0000487
Owen Anderson41a75022009-08-13 21:57:51 +0000488 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000489 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000490 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000491
Anders Carlsson307846f2009-07-23 03:17:50 +0000492 // Append the padding field
Anders Carlssond5d64132009-07-28 17:56:36 +0000493 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000494}
495
496unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000497 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000498 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000499
Anders Carlsson307846f2009-07-23 03:17:50 +0000500 return Types.getTargetData().getABITypeAlignment(Ty);
501}
502
Anders Carlssone8bfe412010-02-02 05:17:25 +0000503void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000504 // This record already contains a member pointer.
Anders Carlssone8bfe412010-02-02 05:17:25 +0000505 if (ContainsPointerToDataMember)
Anders Carlssond606de72009-08-23 01:25:01 +0000506 return;
Mike Stump11289f42009-09-09 15:08:12 +0000507
Anders Carlssond606de72009-08-23 01:25:01 +0000508 // Can only have member pointers if we're compiling C++.
509 if (!Types.getContext().getLangOptions().CPlusPlus)
510 return;
Mike Stump11289f42009-09-09 15:08:12 +0000511
Anders Carlssone8bfe412010-02-02 05:17:25 +0000512 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000513
Anders Carlssone8bfe412010-02-02 05:17:25 +0000514 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
515 if (!MPT->getPointeeType()->isFunctionType()) {
516 // We have a pointer to data member.
517 ContainsPointerToDataMember = true;
518 }
519 } else if (const RecordType *RT = T->getAs<RecordType>()) {
520 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000521
Anders Carlssone8bfe412010-02-02 05:17:25 +0000522 // FIXME: It would be better if there was a way to explicitly compute the
523 // record layout instead of converting to a type.
524 Types.ConvertTagDeclType(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000525
Anders Carlssone8bfe412010-02-02 05:17:25 +0000526 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000527
Anders Carlssone8bfe412010-02-02 05:17:25 +0000528 if (Layout.containsPointerToDataMember())
529 ContainsPointerToDataMember = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000530 }
Anders Carlssond606de72009-08-23 01:25:01 +0000531}
532
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000533CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
534 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000535
Anders Carlsson307846f2009-07-23 03:17:50 +0000536 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000537
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000538 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson758428f2009-08-05 23:18:46 +0000539 Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000540 Builder.Packed);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000541 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
542 getTargetData().getTypeAllocSize(Ty) &&
Anders Carlsson453878b2009-08-08 18:01:57 +0000543 "Type size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000544
Daniel Dunbar034299e2010-03-31 01:09:11 +0000545 CGRecordLayout *RL =
546 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
547
Anders Carlsson307846f2009-07-23 03:17:50 +0000548 // Add all the field numbers.
Daniel Dunbard4549102010-04-06 01:07:41 +0000549 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
550 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson307846f2009-07-23 03:17:50 +0000551
552 // Add bitfield info.
Daniel Dunbard4549102010-04-06 01:07:41 +0000553 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
554 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000555
Daniel Dunbarb935b932010-04-13 20:58:55 +0000556 if (getContext().getLangOptions().DumpRecordLayouts) {
557 llvm::errs() << "\n*** Dumping Record Layout\n";
558 llvm::errs() << "Record: ";
559 D->dump();
560 llvm::errs() << "\nLayout: ";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000561 RL->dump();
Daniel Dunbarb935b932010-04-13 20:58:55 +0000562 }
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000563
Daniel Dunbar034299e2010-03-31 01:09:11 +0000564 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000565}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000566
567void CGRecordLayout::print(llvm::raw_ostream &OS) const {
568 OS << "<CGRecordLayout\n";
569 OS << " LLVMType:" << *LLVMType << "\n";
570 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
571 OS << " BitFields:[\n";
572 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
573 it = BitFields.begin(), ie = BitFields.end();
574 it != ie; ++it) {
Daniel Dunbarb935b932010-04-13 20:58:55 +0000575 OS.indent(4);
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000576 it->second.print(OS);
577 OS << "\n";
578 }
579 OS << "]>\n";
580}
581
582void CGRecordLayout::dump() const {
583 print(llvm::errs());
584}
585
586void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
587 OS << "<CGBitFieldInfo";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000588 OS << " Size:" << Size;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000589 OS << " IsSigned:" << IsSigned << "\n";
590
591 OS.indent(4 + strlen("<CGBitFieldInfo"));
592 OS << " NumComponents:" << getNumComponents();
593 OS << " Components: [";
594 if (getNumComponents()) {
595 OS << "\n";
596 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
597 const AccessInfo &AI = getComponent(i);
598 OS.indent(8);
599 OS << "<AccessInfo"
600 << " FieldIndex:" << AI.FieldIndex
601 << " FieldByteOffset:" << AI.FieldByteOffset
602 << " FieldBitStart:" << AI.FieldBitStart
603 << " AccessWidth:" << AI.AccessWidth << "\n";
604 OS.indent(8 + strlen("<AccessInfo"));
605 OS << " AccessAlignment:" << AI.AccessAlignment
606 << " TargetBitOffset:" << AI.TargetBitOffset
607 << " TargetBitWidth:" << AI.TargetBitWidth
608 << ">\n";
609 }
610 OS.indent(4);
611 }
612 OS << "]>";
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000613}
614
615void CGBitFieldInfo::dump() const {
616 print(llvm::errs());
617}