blob: f2fd885ee4d9a1cf17a388ee4727315529d0ee1f [file] [log] [blame]
Anders Carlsson45372a62009-07-23 03:17:50 +00001//===--- CGRecordLayoutBuilder.cpp - Record builder helper ------*- C++ -*-===//
2//
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//
10// This is a helper class used to build CGRecordLayout objects and LLVM types.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGRecordLayoutBuilder.h"
15
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/Attr.h"
18#include "clang/AST/DeclCXX.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/RecordLayout.h"
21#include "CodeGenTypes.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/Target/TargetData.h"
24
25
26using namespace clang;
27using namespace CodeGen;
28
29void CGRecordLayoutBuilder::Layout(const RecordDecl *D) {
Anders Carlssona5dd7222009-08-08 19:38:24 +000030 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8;
Anders Carlssond0eb3b92009-09-02 17:51:33 +000031 Packed = D->hasAttr<PackedAttr>();
Anders Carlssona5dd7222009-08-08 19:38:24 +000032
Anders Carlsson5a6e3982009-07-23 03:43:54 +000033 if (D->isUnion()) {
34 LayoutUnion(D);
35 return;
36 }
Anders Carlssona860e752009-08-08 18:23:56 +000037
Anders Carlsson45372a62009-07-23 03:17:50 +000038 if (LayoutFields(D))
39 return;
40
Anders Carlsson45372a62009-07-23 03:17:50 +000041 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlsson4b5584b2009-07-23 17:24:40 +000042 Packed = true;
Anders Carlsson45372a62009-07-23 03:17:50 +000043 AlignmentAsLLVMStruct = 1;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +000044 NextFieldOffsetInBytes = 0;
Anders Carlsson45372a62009-07-23 03:17:50 +000045 FieldTypes.clear();
Anders Carlsson45372a62009-07-23 03:17:50 +000046 LLVMFields.clear();
47 LLVMBitFields.clear();
48
49 LayoutFields(D);
50}
51
52void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
53 uint64_t FieldOffset) {
54 uint64_t FieldSize =
55 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
56
57 if (FieldSize == 0)
58 return;
59
Anders Carlssonc2cc1d52009-07-28 17:56:36 +000060 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson45372a62009-07-23 03:17:50 +000061 unsigned NumBytesToAppend;
62
63 if (FieldOffset < NextFieldOffset) {
64 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssonc2cc1d52009-07-28 17:56:36 +000065 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Anders Carlsson45372a62009-07-23 03:17:50 +000066
67 // The bitfield begins in the previous bit-field.
68 NumBytesToAppend =
69 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
70 } else {
71 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
72
73 // Append padding if necessary.
74 AppendBytes((FieldOffset - NextFieldOffset) / 8);
75
76 NumBytesToAppend =
77 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
78
79 assert(NumBytesToAppend && "No bytes to append!");
80 }
81
82 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
83 uint64_t TypeSizeInBits = getTypeSizeInBytes(Ty) * 8;
84
Anders Carlsson8330cee2009-07-23 17:01:21 +000085 LLVMBitFields.push_back(LLVMBitFieldInfo(D, FieldOffset / TypeSizeInBits,
86 FieldOffset % TypeSizeInBits,
Anders Carlsson45372a62009-07-23 03:17:50 +000087 FieldSize));
88
89 AppendBytes(NumBytesToAppend);
90
Anders Carlssonc1efe362009-07-27 14:55:54 +000091 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct, getTypeAlignment(Ty));
92
Anders Carlssonfaaec222009-07-23 21:16:33 +000093 BitsAvailableInLastField =
Anders Carlssonc2cc1d52009-07-28 17:56:36 +000094 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +000095}
96
97bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
98 uint64_t FieldOffset) {
Anders Carlsson45372a62009-07-23 03:17:50 +000099 // If the field is packed, then we need a packed struct.
Anders Carlssona860e752009-08-08 18:23:56 +0000100 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson45372a62009-07-23 03:17:50 +0000101 return false;
102
103 if (D->isBitField()) {
104 // We must use packed structs for unnamed bit fields since they
105 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000106 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000107 return false;
108
109 LayoutBitField(D, FieldOffset);
110 return true;
111 }
112
Anders Carlsson45372a62009-07-23 03:17:50 +0000113 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000114 uint64_t FieldOffsetInBytes = FieldOffset / 8;
115
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000116 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
117 unsigned TypeAlignment = getTypeAlignment(Ty);
118
Anders Carlssona5dd7222009-08-08 19:38:24 +0000119 // If the type alignment is larger then the struct alignment, we must use
120 // a packed struct.
121 if (TypeAlignment > Alignment) {
122 assert(!Packed && "Alignment is wrong even with packed struct!");
123 return false;
124 }
125
126 if (const RecordType *RT = D->getType()->getAs<RecordType>()) {
127 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
128 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) {
129 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed)
130 return false;
131 }
132 }
133
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000134 // Round up the field offset to the alignment of the field type.
135 uint64_t AlignedNextFieldOffsetInBytes =
136 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
137
138 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
139 assert(!Packed && "Could not place field even with packed struct!");
140 return false;
141 }
142
143 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
144 // Even with alignment, the field offset is not at the right place,
145 // insert padding.
146 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
147
148 AppendBytes(PaddingInBytes);
149 }
Anders Carlsson45372a62009-07-23 03:17:50 +0000150
Anders Carlsson45372a62009-07-23 03:17:50 +0000151 // Now append the field.
152 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000153 AppendField(FieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000154
155 return true;
156}
157
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000158void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
159 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
160
161 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
162
163 const llvm::Type *Ty = 0;
164 uint64_t Size = 0;
165 unsigned Align = 0;
166
167 unsigned FieldNo = 0;
168 for (RecordDecl::field_iterator Field = D->field_begin(),
169 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
170 assert(Layout.getFieldOffset(FieldNo) == 0 &&
171 "Union field offset did not start at the beginning of record!");
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000172
173 if (Field->isBitField()) {
174 uint64_t FieldSize =
175 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
176
177 // Ignore zero sized bit fields.
178 if (FieldSize == 0)
179 continue;
Anders Carlsson94ae95f2009-07-23 22:52:34 +0000180
181 // Add the bit field info.
182 Types.addBitFieldInfo(*Field, 0, 0, FieldSize);
183 } else
184 Types.addFieldInfo(*Field, 0);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000185
186 const llvm::Type *FieldTy =
187 Types.ConvertTypeForMemRecursive(Field->getType());
Anders Carlsson177d4d82009-07-23 21:52:03 +0000188 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
189 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000190
191 if (FieldAlign < Align)
192 continue;
193
194 if (FieldAlign > Align || FieldSize > Size) {
195 Ty = FieldTy;
196 Align = FieldAlign;
197 Size = FieldSize;
198 }
199 }
200
201 // Now add our field.
Anders Carlsson36620002009-09-03 22:56:02 +0000202 if (Ty) {
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000203 AppendField(0, Ty);
Anders Carlsson36620002009-09-03 22:56:02 +0000204
205 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) {
206 // We need a packed struct.
207 Packed = true;
208 Align = 1;
209 }
210 }
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000211
212 // Append tail padding.
213 if (Layout.getSize() / 8 > Size)
214 AppendPadding(Layout.getSize() / 8, Align);
215}
216
Anders Carlsson45372a62009-07-23 03:17:50 +0000217bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
218 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
Anders Carlssona5dd7222009-08-08 19:38:24 +0000219 assert(Alignment && "Did not set alignment!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000220
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000221 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Anders Carlsson45372a62009-07-23 03:17:50 +0000222
223 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000224
Anders Carlsson45372a62009-07-23 03:17:50 +0000225 for (RecordDecl::field_iterator Field = D->field_begin(),
226 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
227 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000228 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000229 "Could not layout fields even with a packed LLVM struct!");
230 return false;
231 }
232 }
233
234 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000235 AppendTailPadding(Layout.getSize());
Anders Carlsson45372a62009-07-23 03:17:50 +0000236
237 return true;
238}
239
Anders Carlssonc1efe362009-07-27 14:55:54 +0000240void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
241 assert(RecordSize % 8 == 0 && "Invalid record size!");
242
243 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000244 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Anders Carlssonc1efe362009-07-27 14:55:54 +0000245
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000246 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000247 AppendBytes(NumPadBytes);
248}
249
Anders Carlsson45372a62009-07-23 03:17:50 +0000250void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000251 const llvm::Type *FieldTy) {
252 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
253 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000254
255 uint64_t FieldSizeInBytes = getTypeSizeInBytes(FieldTy);
256
Anders Carlsson45372a62009-07-23 03:17:50 +0000257 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000258
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000259 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000260 BitsAvailableInLastField = 0;
261}
262
263void
264CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
265 const llvm::Type *FieldTy) {
266 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
267}
268
269void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
270 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000271 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
272 "Incorrect field layout!");
273
274 // Round up the field offset to the alignment of the field type.
275 uint64_t AlignedNextFieldOffsetInBytes =
276 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
277
278 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
279 // Even with alignment, the field offset is not at the right place,
280 // insert padding.
281 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
282
283 AppendBytes(PaddingInBytes);
284 }
285}
286
287void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
288 if (NumBytes == 0)
289 return;
290
Owen Anderson0032b272009-08-13 21:57:51 +0000291 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext());
Anders Carlssonc1efe362009-07-27 14:55:54 +0000292 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000293 Ty = llvm::ArrayType::get(Ty, NumBytes);
Anders Carlsson45372a62009-07-23 03:17:50 +0000294
295 // Append the padding field
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000296 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000297}
298
299unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000300 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000301 return 1;
Anders Carlsson45372a62009-07-23 03:17:50 +0000302
303 return Types.getTargetData().getABITypeAlignment(Ty);
304}
305
306uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const {
307 return Types.getTargetData().getTypeAllocSize(Ty);
308}
309
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000310void CGRecordLayoutBuilder::CheckForMemberPointer(const FieldDecl *FD) {
311 // This record already contains a member pointer.
312 if (ContainsMemberPointer)
313 return;
314
315 // Can only have member pointers if we're compiling C++.
316 if (!Types.getContext().getLangOptions().CPlusPlus)
317 return;
318
319 QualType Ty = FD->getType();
320
321 if (Ty->isMemberPointerType()) {
322 // We have a member pointer!
323 ContainsMemberPointer = true;
324 return;
325 }
326
327}
328
Anders Carlsson45372a62009-07-23 03:17:50 +0000329CGRecordLayout *
330CGRecordLayoutBuilder::ComputeLayout(CodeGenTypes &Types,
331 const RecordDecl *D) {
332 CGRecordLayoutBuilder Builder(Types);
333
Anders Carlsson45372a62009-07-23 03:17:50 +0000334 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000335
Owen Anderson47a434f2009-08-05 23:18:46 +0000336 const llvm::Type *Ty = llvm::StructType::get(Types.getLLVMContext(),
337 Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000338 Builder.Packed);
Anders Carlssondf31e092009-08-08 18:01:57 +0000339 assert(Types.getContext().getASTRecordLayout(D).getSize() / 8 ==
340 Types.getTargetData().getTypeAllocSize(Ty) &&
341 "Type size mismatch!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000342
343 // Add all the field numbers.
344 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) {
345 const FieldDecl *FD = Builder.LLVMFields[i].first;
346 unsigned FieldNo = Builder.LLVMFields[i].second;
347
348 Types.addFieldInfo(FD, FieldNo);
349 }
350
351 // Add bitfield info.
352 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) {
353 const LLVMBitFieldInfo &Info = Builder.LLVMBitFields[i];
354
Anders Carlsson8330cee2009-07-23 17:01:21 +0000355 Types.addBitFieldInfo(Info.FD, Info.FieldNo, Info.Start, Info.Size);
Anders Carlsson45372a62009-07-23 03:17:50 +0000356 }
357
Anders Carlssonfc3eaa42009-08-23 01:25:01 +0000358 return new CGRecordLayout(Ty, Builder.ContainsMemberPointer);
Anders Carlsson45372a62009-07-23 03:17:50 +0000359}