blob: a119d4f3a40cc43946b030ffe42c5d012ed86fa2 [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;
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 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
Anders Carlsson45372a62009-07-23 03:17:50 +0000120 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000121 uint64_t FieldOffsetInBytes = FieldOffset / 8;
122
Anders Carlssonde9f2c92009-08-04 16:29:15 +0000123 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
124 unsigned TypeAlignment = getTypeAlignment(Ty);
125
126 // Round up the field offset to the alignment of the field type.
127 uint64_t AlignedNextFieldOffsetInBytes =
128 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
129
130 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
131 assert(!Packed && "Could not place field even with packed struct!");
132 return false;
133 }
134
135 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
136 // Even with alignment, the field offset is not at the right place,
137 // insert padding.
138 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
139
140 AppendBytes(PaddingInBytes);
141 }
Anders Carlsson45372a62009-07-23 03:17:50 +0000142
Anders Carlsson45372a62009-07-23 03:17:50 +0000143 // Now append the field.
144 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000145 AppendField(FieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000146
147 return true;
148}
149
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000150void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
151 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
152
153 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
154
155 const llvm::Type *Ty = 0;
156 uint64_t Size = 0;
157 unsigned Align = 0;
158
159 unsigned FieldNo = 0;
160 for (RecordDecl::field_iterator Field = D->field_begin(),
161 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
162 assert(Layout.getFieldOffset(FieldNo) == 0 &&
163 "Union field offset did not start at the beginning of record!");
Anders Carlsson2cc8f172009-07-23 04:00:39 +0000164
165 if (Field->isBitField()) {
166 uint64_t FieldSize =
167 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
168
169 // Ignore zero sized bit fields.
170 if (FieldSize == 0)
171 continue;
Anders Carlsson94ae95f2009-07-23 22:52:34 +0000172
173 // Add the bit field info.
174 Types.addBitFieldInfo(*Field, 0, 0, FieldSize);
175 } else
176 Types.addFieldInfo(*Field, 0);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000177
178 const llvm::Type *FieldTy =
179 Types.ConvertTypeForMemRecursive(Field->getType());
Anders Carlsson177d4d82009-07-23 21:52:03 +0000180 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
181 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000182
183 if (FieldAlign < Align)
184 continue;
185
186 if (FieldAlign > Align || FieldSize > Size) {
187 Ty = FieldTy;
188 Align = FieldAlign;
189 Size = FieldSize;
190 }
191 }
192
193 // Now add our field.
Anders Carlsson94ae95f2009-07-23 22:52:34 +0000194 if (Ty)
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000195 AppendField(0, Ty);
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000196
197 // Append tail padding.
198 if (Layout.getSize() / 8 > Size)
199 AppendPadding(Layout.getSize() / 8, Align);
200}
201
Anders Carlsson45372a62009-07-23 03:17:50 +0000202bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
203 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
204
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000205 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Anders Carlsson45372a62009-07-23 03:17:50 +0000206
207 unsigned FieldNo = 0;
Fariborz Jahaniancad86652009-07-27 20:57:45 +0000208
Anders Carlsson45372a62009-07-23 03:17:50 +0000209 for (RecordDecl::field_iterator Field = D->field_begin(),
210 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
211 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000212 assert(!Packed &&
Anders Carlsson45372a62009-07-23 03:17:50 +0000213 "Could not layout fields even with a packed LLVM struct!");
214 return false;
215 }
216 }
217
218 // Append tail padding if necessary.
Anders Carlssonc1efe362009-07-27 14:55:54 +0000219 AppendTailPadding(Layout.getSize());
Anders Carlsson45372a62009-07-23 03:17:50 +0000220
221 return true;
222}
223
Anders Carlssonc1efe362009-07-27 14:55:54 +0000224void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
225 assert(RecordSize % 8 == 0 && "Invalid record size!");
226
227 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000228 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Anders Carlssonc1efe362009-07-27 14:55:54 +0000229
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000230 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000231 AppendBytes(NumPadBytes);
232}
233
Anders Carlsson45372a62009-07-23 03:17:50 +0000234void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson45372a62009-07-23 03:17:50 +0000235 const llvm::Type *FieldTy) {
236 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
237 getTypeAlignment(FieldTy));
Anders Carlsson728d7cd2009-07-24 02:45:50 +0000238
239 uint64_t FieldSizeInBytes = getTypeSizeInBytes(FieldTy);
240
Anders Carlsson45372a62009-07-23 03:17:50 +0000241 FieldTypes.push_back(FieldTy);
Anders Carlsson45372a62009-07-23 03:17:50 +0000242
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000243 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson45372a62009-07-23 03:17:50 +0000244 BitsAvailableInLastField = 0;
245}
246
247void
248CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
249 const llvm::Type *FieldTy) {
250 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
251}
252
253void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
254 unsigned FieldAlignment) {
Anders Carlsson45372a62009-07-23 03:17:50 +0000255 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
256 "Incorrect field layout!");
257
258 // Round up the field offset to the alignment of the field type.
259 uint64_t AlignedNextFieldOffsetInBytes =
260 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
261
262 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
263 // Even with alignment, the field offset is not at the right place,
264 // insert padding.
265 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
266
267 AppendBytes(PaddingInBytes);
268 }
269}
270
271void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
272 if (NumBytes == 0)
273 return;
274
275 const llvm::Type *Ty = llvm::Type::Int8Ty;
Anders Carlssonc1efe362009-07-27 14:55:54 +0000276 if (NumBytes > 1)
Anders Carlsson45372a62009-07-23 03:17:50 +0000277 Ty = llvm::ArrayType::get(Ty, NumBytes);
Anders Carlsson45372a62009-07-23 03:17:50 +0000278
279 // Append the padding field
Anders Carlssonc2cc1d52009-07-28 17:56:36 +0000280 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson45372a62009-07-23 03:17:50 +0000281}
282
283unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000284 if (Packed)
Anders Carlsson45372a62009-07-23 03:17:50 +0000285 return 1;
Anders Carlsson45372a62009-07-23 03:17:50 +0000286
287 return Types.getTargetData().getABITypeAlignment(Ty);
288}
289
290uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const {
291 return Types.getTargetData().getTypeAllocSize(Ty);
292}
293
294CGRecordLayout *
295CGRecordLayoutBuilder::ComputeLayout(CodeGenTypes &Types,
296 const RecordDecl *D) {
297 CGRecordLayoutBuilder Builder(Types);
298
Anders Carlsson45372a62009-07-23 03:17:50 +0000299 Builder.Layout(D);
Anders Carlsson4c98efd2009-07-24 15:20:52 +0000300
Owen Anderson47a434f2009-08-05 23:18:46 +0000301 const llvm::Type *Ty = llvm::StructType::get(Types.getLLVMContext(),
302 Builder.FieldTypes,
Anders Carlsson4b5584b2009-07-23 17:24:40 +0000303 Builder.Packed);
Anders Carlssondf31e092009-08-08 18:01:57 +0000304 assert(Types.getContext().getASTRecordLayout(D).getSize() / 8 ==
305 Types.getTargetData().getTypeAllocSize(Ty) &&
306 "Type size mismatch!");
Anders Carlsson45372a62009-07-23 03:17:50 +0000307
308 // Add all the field numbers.
309 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) {
310 const FieldDecl *FD = Builder.LLVMFields[i].first;
311 unsigned FieldNo = Builder.LLVMFields[i].second;
312
313 Types.addFieldInfo(FD, FieldNo);
314 }
315
316 // Add bitfield info.
317 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) {
318 const LLVMBitFieldInfo &Info = Builder.LLVMBitFields[i];
319
Anders Carlsson8330cee2009-07-23 17:01:21 +0000320 Types.addBitFieldInfo(Info.FD, Info.FieldNo, Info.Start, Info.Size);
Anders Carlsson45372a62009-07-23 03:17:50 +0000321 }
322
323 return new CGRecordLayout(Ty, llvm::SmallSet<unsigned, 8>());
324}