blob: 546c5440957c9667154f080df1cac70358dbf791 [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 Carlsson5a6e3982009-07-23 03:43:54 +000030 if (D->isUnion()) {
31 LayoutUnion(D);
32 return;
33 }
34
Anders Carlsson45372a62009-07-23 03:17:50 +000035 if (const PackedAttr* PA = D->getAttr<PackedAttr>())
Anders Carlsson4b5584b2009-07-23 17:24:40 +000036 Packed = PA->getAlignment();
Anders Carlsson45372a62009-07-23 03:17:50 +000037
38 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;
44 FieldTypes.clear();
45 FieldInfos.clear();
46 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
60 uint64_t NextFieldOffset = getNextFieldOffsetInBytes() * 8;
61 unsigned NumBytesToAppend;
62
63 if (FieldOffset < NextFieldOffset) {
64 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
65 assert(!FieldInfos.empty() && "Field infos can't be empty!");
66
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 =
94 getNextFieldOffsetInBytes() * 8 - (FieldOffset + FieldSize);
Anders Carlsson45372a62009-07-23 03:17:50 +000095}
96
97bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
98 uint64_t FieldOffset) {
Anders Carlsson4b5584b2009-07-23 17:24:40 +000099 bool FieldPacked = Packed;
Anders Carlsson45372a62009-07-23 03:17:50 +0000100
101 // FIXME: Should this override struct packing? Probably we want to
102 // take the minimum?
103 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000104 FieldPacked = PA->getAlignment();
Anders Carlsson45372a62009-07-23 03:17:50 +0000105
106 // If the field is packed, then we need a packed struct.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000107 if (!Packed && FieldPacked)
Anders Carlsson45372a62009-07-23 03:17:50 +0000108 return false;
109
110 if (D->isBitField()) {
111 // We must use packed structs for unnamed bit fields since they
112 // don't affect the struct alignment.
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000113 if (!Packed && !D->getDeclName())
Anders Carlsson45372a62009-07-23 03:17:50 +0000114 return false;
115
116 LayoutBitField(D, FieldOffset);
117 return true;
118 }
119
120 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
121
122 // Check if the field is aligned.
123 if (const AlignedAttr *PA = D->getAttr<AlignedAttr>()) {
124 unsigned FieldAlign = PA->getAlignment();
125
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000126 if (!Packed && getTypeAlignment(Ty) > FieldAlign)
Anders Carlsson45372a62009-07-23 03:17:50 +0000127 return false;
128 }
129
130 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
131
132 uint64_t FieldOffsetInBytes = FieldOffset / 8;
133
134 // Append padding if necessary.
135 AppendPadding(FieldOffsetInBytes, Ty);
136
Anders Carlsson45372a62009-07-23 03:17:50 +0000137 // Now append the field.
138 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000139 AppendField(FieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000140
141 return true;
142}
143
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000144void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
145 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
146
147 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
148
149 const llvm::Type *Ty = 0;
150 uint64_t Size = 0;
151 unsigned Align = 0;
152
153 unsigned FieldNo = 0;
154 for (RecordDecl::field_iterator Field = D->field_begin(),
155 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
156 assert(Layout.getFieldOffset(FieldNo) == 0 &&
157 "Union field offset did not start at the beginning of record!");
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000158
159 if (Field->isBitField()) {
160 uint64_t FieldSize =
161 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
162
163 // Ignore zero sized bit fields.
164 if (FieldSize == 0)
165 continue;
Anders Carlsson94ae95f2009-07-23 22:52:34 +0000166
167 // Add the bit field info.
168 Types.addBitFieldInfo(*Field, 0, 0, FieldSize);
169 } else
170 Types.addFieldInfo(*Field, 0);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000171
172 const llvm::Type *FieldTy =
173 Types.ConvertTypeForMemRecursive(Field->getType());
Anders Carlsson177d4d82009-07-23 21:52:03 +0000174 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
175 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000176
177 if (FieldAlign < Align)
178 continue;
179
180 if (FieldAlign > Align || FieldSize > Size) {
181 Ty = FieldTy;
182 Align = FieldAlign;
183 Size = FieldSize;
184 }
185 }
186
187 // Now add our field.
Anders Carlsson94ae95f2009-07-23 22:52:34 +0000188 if (Ty)
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000189 AppendField(0, Ty);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000190
191 // Append tail padding.
192 if (Layout.getSize() / 8 > Size)
193 AppendPadding(Layout.getSize() / 8, Align);
194}
195
Anders Carlsson45372a62009-07-23 03:17:50 +0000196bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
197 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
198
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000199 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Anders Carlsson45372a62009-07-23 03:17:50 +0000200
201 unsigned FieldNo = 0;
202 for (RecordDecl::field_iterator Field = D->field_begin(),
203 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
204 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000205 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000206 "Could not layout fields even with a packed LLVM struct!");
207 return false;
208 }
209 }
210
211 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000212 AppendTailPadding(Layout.getSize());
Anders Carlsson45372a62009-07-23 03:17:50 +0000213
214 return true;
215}
216
Anders Carlssonc1efe362009-07-27 14:55:54 +0000217void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
218 assert(RecordSize % 8 == 0 && "Invalid record size!");
219
220 uint64_t RecordSizeInBytes = RecordSize / 8;
221 assert(getNextFieldOffsetInBytes() <= RecordSizeInBytes && "Size mismatch!");
222
223 unsigned NumPadBytes = RecordSizeInBytes - getNextFieldOffsetInBytes();
224 AppendBytes(NumPadBytes);
225}
226
227
Anders Carlsson45372a62009-07-23 03:17:50 +0000228void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000229 const llvm::Type *FieldTy) {
230 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
231 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000232
233 uint64_t FieldSizeInBytes = getTypeSizeInBytes(FieldTy);
234
Anders Carlsson45372a62009-07-23 03:17:50 +0000235 FieldTypes.push_back(FieldTy);
236 FieldInfos.push_back(FieldInfo(FieldOffsetInBytes, FieldSizeInBytes));
237
238 BitsAvailableInLastField = 0;
239}
240
241void
242CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
243 const llvm::Type *FieldTy) {
244 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
245}
246
247void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
248 unsigned FieldAlignment) {
249 uint64_t NextFieldOffsetInBytes = getNextFieldOffsetInBytes();
250 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
251 "Incorrect field layout!");
252
253 // Round up the field offset to the alignment of the field type.
254 uint64_t AlignedNextFieldOffsetInBytes =
255 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
256
257 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
258 // Even with alignment, the field offset is not at the right place,
259 // insert padding.
260 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
261
262 AppendBytes(PaddingInBytes);
263 }
264}
265
266void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
267 if (NumBytes == 0)
268 return;
269
270 const llvm::Type *Ty = llvm::Type::Int8Ty;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000271 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000272 Ty = llvm::ArrayType::get(Ty, NumBytes);
Anders Carlsson45372a62009-07-23 03:17:50 +0000273
274 // Append the padding field
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000275 AppendField(getNextFieldOffsetInBytes(), Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000276}
277
278uint64_t CGRecordLayoutBuilder::getNextFieldOffsetInBytes() const {
279 if (FieldInfos.empty())
280 return 0;
281
282 const FieldInfo &LastInfo = FieldInfos.back();
283 return LastInfo.OffsetInBytes + LastInfo.SizeInBytes;
284}
285
286unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000287 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000288 return 1;
Anders Carlsson45372a62009-07-23 03:17:50 +0000289
290 return Types.getTargetData().getABITypeAlignment(Ty);
291}
292
293uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const {
294 return Types.getTargetData().getTypeAllocSize(Ty);
295}
296
297CGRecordLayout *
298CGRecordLayoutBuilder::ComputeLayout(CodeGenTypes &Types,
299 const RecordDecl *D) {
300 CGRecordLayoutBuilder Builder(Types);
301
Anders Carlsson45372a62009-07-23 03:17:50 +0000302 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000303
Anders Carlsson45372a62009-07-23 03:17:50 +0000304 const llvm::Type *Ty = llvm::StructType::get(Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000305 Builder.Packed);
Anders Carlsson45372a62009-07-23 03:17:50 +0000306
307 // Add all the field numbers.
308 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) {
309 const FieldDecl *FD = Builder.LLVMFields[i].first;
310 unsigned FieldNo = Builder.LLVMFields[i].second;
311
312 Types.addFieldInfo(FD, FieldNo);
313 }
314
315 // Add bitfield info.
316 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) {
317 const LLVMBitFieldInfo &Info = Builder.LLVMBitFields[i];
318
Anders Carlsson8330cee2009-07-23 17:01:21 +0000319 Types.addBitFieldInfo(Info.FD, Info.FieldNo, Info.Start, Info.Size);
Anders Carlsson45372a62009-07-23 03:17:50 +0000320 }
321
322 return new CGRecordLayout(Ty, llvm::SmallSet<unsigned, 8>());
323}