blob: ac6aae8bdca1788e55e0eea12ae05e667548de5d [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());
153 uint64_t TypeSizeInBits = Types.getTargetData().getTypeAllocSize(Ty) * 8;
154
155 bool IsSigned = FD->getType()->isSignedIntegerType();
156 CGBitFieldInfo BFI(Ty, FieldOffset / TypeSizeInBits,
157 FieldOffset % TypeSizeInBits, FieldSize, IsSigned);
158
159 return BFI;
160}
161
Anders Carlsson307846f2009-07-23 03:17:50 +0000162void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
163 uint64_t FieldOffset) {
Mike Stump11289f42009-09-09 15:08:12 +0000164 uint64_t FieldSize =
Anders Carlsson307846f2009-07-23 03:17:50 +0000165 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000166
Anders Carlsson307846f2009-07-23 03:17:50 +0000167 if (FieldSize == 0)
168 return;
169
Anders Carlssond5d64132009-07-28 17:56:36 +0000170 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +0000171 unsigned NumBytesToAppend;
Mike Stump11289f42009-09-09 15:08:12 +0000172
Anders Carlsson307846f2009-07-23 03:17:50 +0000173 if (FieldOffset < NextFieldOffset) {
174 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +0000175 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Mike Stump11289f42009-09-09 15:08:12 +0000176
Anders Carlsson307846f2009-07-23 03:17:50 +0000177 // The bitfield begins in the previous bit-field.
Mike Stump11289f42009-09-09 15:08:12 +0000178 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000179 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
180 } else {
181 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
182
183 // Append padding if necessary.
184 AppendBytes((FieldOffset - NextFieldOffset) / 8);
Mike Stump11289f42009-09-09 15:08:12 +0000185
186 NumBytesToAppend =
Anders Carlsson307846f2009-07-23 03:17:50 +0000187 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000188
Anders Carlsson307846f2009-07-23 03:17:50 +0000189 assert(NumBytesToAppend && "No bytes to append!");
190 }
191
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000192 // Add the bit field info.
193 LLVMBitFields.push_back(
194 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize)));
Mike Stump11289f42009-09-09 15:08:12 +0000195
Anders Carlsson307846f2009-07-23 03:17:50 +0000196 AppendBytes(NumBytesToAppend);
Mike Stump11289f42009-09-09 15:08:12 +0000197
Mike Stump11289f42009-09-09 15:08:12 +0000198 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +0000199 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +0000200}
201
202bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
203 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000204 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +0000205 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000206 return false;
207
208 if (D->isBitField()) {
209 // We must use packed structs for unnamed bit fields since they
210 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000211 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000212 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000213
Anders Carlsson307846f2009-07-23 03:17:50 +0000214 LayoutBitField(D, FieldOffset);
215 return true;
216 }
Mike Stump11289f42009-09-09 15:08:12 +0000217
Anders Carlssone8bfe412010-02-02 05:17:25 +0000218 // Check if we have a pointer to data member in this field.
219 CheckForPointerToDataMember(D->getType());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000220
Anders Carlsson307846f2009-07-23 03:17:50 +0000221 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000222 uint64_t FieldOffsetInBytes = FieldOffset / 8;
Mike Stump11289f42009-09-09 15:08:12 +0000223
Anders Carlsson19702bb2009-08-04 16:29:15 +0000224 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
225 unsigned TypeAlignment = getTypeAlignment(Ty);
226
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000227 // If the type alignment is larger then the struct alignment, we must use
228 // a packed struct.
229 if (TypeAlignment > Alignment) {
230 assert(!Packed && "Alignment is wrong even with packed struct!");
231 return false;
232 }
Mike Stump11289f42009-09-09 15:08:12 +0000233
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000234 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
235 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
236 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
237 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
238 return false;
239 }
240 }
241
Anders Carlsson19702bb2009-08-04 16:29:15 +0000242 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000243 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson19702bb2009-08-04 16:29:15 +0000244 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
245
246 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
247 assert(!Packed && "Could not place field even with packed struct!");
248 return false;
249 }
Mike Stump11289f42009-09-09 15:08:12 +0000250
Anders Carlsson19702bb2009-08-04 16:29:15 +0000251 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
252 // Even with alignment, the field offset is not at the right place,
253 // insert padding.
254 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
Mike Stump11289f42009-09-09 15:08:12 +0000255
Anders Carlsson19702bb2009-08-04 16:29:15 +0000256 AppendBytes(PaddingInBytes);
257 }
Mike Stump11289f42009-09-09 15:08:12 +0000258
Anders Carlsson307846f2009-07-23 03:17:50 +0000259 // Now append the field.
260 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000261 AppendField(FieldOffsetInBytes, Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000262
Anders Carlsson307846f2009-07-23 03:17:50 +0000263 return true;
264}
265
Anders Carlsson697f6592009-07-23 03:43:54 +0000266void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
267 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
Mike Stump11289f42009-09-09 15:08:12 +0000268
Anders Carlsson697f6592009-07-23 03:43:54 +0000269 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000270
Anders Carlsson697f6592009-07-23 03:43:54 +0000271 const llvm::Type *Ty = 0;
272 uint64_t Size = 0;
273 unsigned Align = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000274
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000275 bool HasOnlyZeroSizedBitFields = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000276
Anders Carlsson697f6592009-07-23 03:43:54 +0000277 unsigned FieldNo = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000278 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson697f6592009-07-23 03:43:54 +0000279 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
Mike Stump11289f42009-09-09 15:08:12 +0000280 assert(Layout.getFieldOffset(FieldNo) == 0 &&
Anders Carlsson697f6592009-07-23 03:43:54 +0000281 "Union field offset did not start at the beginning of record!");
Daniel Dunbarc75c8bd2010-04-08 02:59:45 +0000282 const llvm::Type *FieldTy =
283 Types.ConvertTypeForMemRecursive(Field->getType());
Anders Carlssonf814ee62009-07-23 04:00:39 +0000284
285 if (Field->isBitField()) {
Mike Stump11289f42009-09-09 15:08:12 +0000286 uint64_t FieldSize =
Anders Carlssonf814ee62009-07-23 04:00:39 +0000287 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Mike Stump11289f42009-09-09 15:08:12 +0000288
Anders Carlssonf814ee62009-07-23 04:00:39 +0000289 // Ignore zero sized bit fields.
290 if (FieldSize == 0)
291 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000292
Anders Carlsson08539542009-07-23 22:52:34 +0000293 // Add the bit field info.
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000294 LLVMBitFields.push_back(
295 LLVMBitFieldInfo(*Field, ComputeBitFieldInfo(Types, *Field,
296 0, FieldSize)));
Daniel Dunbare75a64f2010-03-31 00:55:13 +0000297 } else {
298 LLVMFields.push_back(LLVMFieldInfo(*Field, 0));
299 }
Mike Stump11289f42009-09-09 15:08:12 +0000300
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000301 HasOnlyZeroSizedBitFields = false;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000302
Anders Carlssone2accf42009-07-23 21:52:03 +0000303 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
304 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Mike Stump11289f42009-09-09 15:08:12 +0000305
Anders Carlsson697f6592009-07-23 03:43:54 +0000306 if (FieldAlign < Align)
307 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000308
Anders Carlsson697f6592009-07-23 03:43:54 +0000309 if (FieldAlign > Align || FieldSize > Size) {
310 Ty = FieldTy;
311 Align = FieldAlign;
312 Size = FieldSize;
313 }
314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
Anders Carlsson697f6592009-07-23 03:43:54 +0000316 // Now add our field.
Anders Carlsson0e912752009-09-03 22:56:02 +0000317 if (Ty) {
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000318 AppendField(0, Ty);
Anders Carlsson0e912752009-09-03 22:56:02 +0000319
320 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
321 // We need a packed struct.
322 Packed = true;
323 Align = 1;
324 }
325 }
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000326 if (!Align) {
Anders Carlssonb1ef9912010-01-28 18:22:03 +0000327 assert(HasOnlyZeroSizedBitFields &&
328 "0-align record did not have all zero-sized bit-fields!");
Fariborz Jahaniane8e631c2009-11-06 20:47:40 +0000329 Align = 1;
330 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000331
Anders Carlsson697f6592009-07-23 03:43:54 +0000332 // Append tail padding.
333 if (Layout.getSize() / 8 > Size)
334 AppendPadding(Layout.getSize() / 8, Align);
335}
336
Anders Carlssond681a292009-12-16 17:27:20 +0000337void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD,
338 const ASTRecordLayout &Layout) {
339 // Check if we need to add a vtable pointer.
340 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) {
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000341 const llvm::Type *Int8PtrTy =
Anders Carlssond681a292009-12-16 17:27:20 +0000342 llvm::Type::getInt8PtrTy(Types.getLLVMContext());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000343
Anders Carlssond681a292009-12-16 17:27:20 +0000344 assert(NextFieldOffsetInBytes == 0 &&
345 "Vtable pointer must come first!");
346 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo());
347 }
348}
349
Anders Carlsson307846f2009-07-23 03:17:50 +0000350bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
351 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlsson28a5fa22009-08-08 19:38:24 +0000352 assert(Alignment && "Did not set alignment!");
Mike Stump11289f42009-09-09 15:08:12 +0000353
Anders Carlsson697f6592009-07-23 03:43:54 +0000354 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Mike Stump11289f42009-09-09 15:08:12 +0000355
Anders Carlssond681a292009-12-16 17:27:20 +0000356 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D))
357 LayoutBases(RD, Layout);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000358
Anders Carlsson307846f2009-07-23 03:17:50 +0000359 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000360
Mike Stump11289f42009-09-09 15:08:12 +0000361 for (RecordDecl::field_iterator Field = D->field_begin(),
Anders Carlsson307846f2009-07-23 03:17:50 +0000362 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
363 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Mike Stump11289f42009-09-09 15:08:12 +0000364 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000365 "Could not layout fields even with a packed LLVM struct!");
366 return false;
367 }
368 }
369
370 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000371 AppendTailPadding(Layout.getSize());
Mike Stump11289f42009-09-09 15:08:12 +0000372
Anders Carlsson307846f2009-07-23 03:17:50 +0000373 return true;
374}
375
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000376void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
377 assert(RecordSize % 8 == 0 && "Invalid record size!");
Mike Stump11289f42009-09-09 15:08:12 +0000378
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000379 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000380 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000381
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000382 uint64_t AlignedNextFieldOffset =
Anders Carlsson220bf4f2009-12-08 01:24:23 +0000383 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct);
384
385 if (AlignedNextFieldOffset == RecordSizeInBytes) {
386 // We don't need any padding.
387 return;
388 }
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000389
Anders Carlssond5d64132009-07-28 17:56:36 +0000390 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000391 AppendBytes(NumPadBytes);
392}
393
Mike Stump11289f42009-09-09 15:08:12 +0000394void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000395 const llvm::Type *FieldTy) {
396 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
397 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000398
Daniel Dunbarf9c24f82010-04-12 21:01:28 +0000399 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000400
Anders Carlsson307846f2009-07-23 03:17:50 +0000401 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000402
Anders Carlssond5d64132009-07-28 17:56:36 +0000403 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000404 BitsAvailableInLastField = 0;
405}
406
Mike Stump11289f42009-09-09 15:08:12 +0000407void
Anders Carlsson307846f2009-07-23 03:17:50 +0000408CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
409 const llvm::Type *FieldTy) {
410 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
411}
412
Mike Stump11289f42009-09-09 15:08:12 +0000413void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000414 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000415 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
416 "Incorrect field layout!");
Mike Stump11289f42009-09-09 15:08:12 +0000417
Anders Carlsson307846f2009-07-23 03:17:50 +0000418 // Round up the field offset to the alignment of the field type.
Mike Stump11289f42009-09-09 15:08:12 +0000419 uint64_t AlignedNextFieldOffsetInBytes =
Anders Carlsson307846f2009-07-23 03:17:50 +0000420 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
421
422 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
423 // Even with alignment, the field offset is not at the right place,
424 // insert padding.
425 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
426
427 AppendBytes(PaddingInBytes);
428 }
429}
430
431void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
432 if (NumBytes == 0)
433 return;
Mike Stump11289f42009-09-09 15:08:12 +0000434
Owen Anderson41a75022009-08-13 21:57:51 +0000435 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000436 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000437 Ty = llvm::ArrayType::get(Ty, NumBytes);
Mike Stump11289f42009-09-09 15:08:12 +0000438
Anders Carlsson307846f2009-07-23 03:17:50 +0000439 // Append the padding field
Anders Carlssond5d64132009-07-28 17:56:36 +0000440 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000441}
442
443unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000444 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000445 return 1;
Mike Stump11289f42009-09-09 15:08:12 +0000446
Anders Carlsson307846f2009-07-23 03:17:50 +0000447 return Types.getTargetData().getABITypeAlignment(Ty);
448}
449
Anders Carlssone8bfe412010-02-02 05:17:25 +0000450void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) {
Anders Carlssond606de72009-08-23 01:25:01 +0000451 // This record already contains a member pointer.
Anders Carlssone8bfe412010-02-02 05:17:25 +0000452 if (ContainsPointerToDataMember)
Anders Carlssond606de72009-08-23 01:25:01 +0000453 return;
Mike Stump11289f42009-09-09 15:08:12 +0000454
Anders Carlssond606de72009-08-23 01:25:01 +0000455 // Can only have member pointers if we're compiling C++.
456 if (!Types.getContext().getLangOptions().CPlusPlus)
457 return;
Mike Stump11289f42009-09-09 15:08:12 +0000458
Anders Carlssone8bfe412010-02-02 05:17:25 +0000459 T = Types.getContext().getBaseElementType(T);
Mike Stump11289f42009-09-09 15:08:12 +0000460
Anders Carlssone8bfe412010-02-02 05:17:25 +0000461 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) {
462 if (!MPT->getPointeeType()->isFunctionType()) {
463 // We have a pointer to data member.
464 ContainsPointerToDataMember = true;
465 }
466 } else if (const RecordType *RT = T->getAs<RecordType>()) {
467 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000468
Anders Carlssone8bfe412010-02-02 05:17:25 +0000469 // FIXME: It would be better if there was a way to explicitly compute the
470 // record layout instead of converting to a type.
471 Types.ConvertTagDeclType(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000472
Anders Carlssone8bfe412010-02-02 05:17:25 +0000473 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000474
Anders Carlssone8bfe412010-02-02 05:17:25 +0000475 if (Layout.containsPointerToDataMember())
476 ContainsPointerToDataMember = true;
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000477 }
Anders Carlssond606de72009-08-23 01:25:01 +0000478}
479
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000480CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) {
481 CGRecordLayoutBuilder Builder(*this);
Mike Stump11289f42009-09-09 15:08:12 +0000482
Anders Carlsson307846f2009-07-23 03:17:50 +0000483 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000484
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000485 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(),
Owen Anderson758428f2009-08-05 23:18:46 +0000486 Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000487 Builder.Packed);
Daniel Dunbar23ee4b72010-03-31 00:11:27 +0000488 assert(getContext().getASTRecordLayout(D).getSize() / 8 ==
489 getTargetData().getTypeAllocSize(Ty) &&
Anders Carlsson453878b2009-08-08 18:01:57 +0000490 "Type size mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +0000491
Daniel Dunbar034299e2010-03-31 01:09:11 +0000492 CGRecordLayout *RL =
493 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember);
494
Anders Carlsson307846f2009-07-23 03:17:50 +0000495 // Add all the field numbers.
Daniel Dunbard4549102010-04-06 01:07:41 +0000496 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i)
497 RL->FieldInfo.insert(Builder.LLVMFields[i]);
Anders Carlsson307846f2009-07-23 03:17:50 +0000498
499 // Add bitfield info.
Daniel Dunbard4549102010-04-06 01:07:41 +0000500 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i)
501 RL->BitFields.insert(Builder.LLVMBitFields[i]);
Mike Stump11289f42009-09-09 15:08:12 +0000502
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000503 if (getContext().getLangOptions().DumpRecordLayouts)
504 RL->dump();
505
Daniel Dunbar034299e2010-03-31 01:09:11 +0000506 return RL;
Anders Carlsson307846f2009-07-23 03:17:50 +0000507}
Daniel Dunbarb97bff92010-04-12 18:14:18 +0000508
509void CGRecordLayout::print(llvm::raw_ostream &OS) const {
510 OS << "<CGRecordLayout\n";
511 OS << " LLVMType:" << *LLVMType << "\n";
512 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n";
513 OS << " BitFields:[\n";
514 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator
515 it = BitFields.begin(), ie = BitFields.end();
516 it != ie; ++it) {
517 OS << " ";
518 it->second.print(OS);
519 OS << "\n";
520 }
521 OS << "]>\n";
522}
523
524void CGRecordLayout::dump() const {
525 print(llvm::errs());
526}
527
528void CGBitFieldInfo::print(llvm::raw_ostream &OS) const {
529 OS << "<CGBitFieldInfo";
530 OS << " FieldTy:" << *FieldTy;
531 OS << " FieldNo:" << FieldNo;
532 OS << " Start:" << Start;
533 OS << " Size:" << Size;
534 OS << " IsSigned:" << IsSigned;
535 OS << ">";
536}
537
538void CGBitFieldInfo::dump() const {
539 print(llvm::errs());
540}