blob: d3699bb992d7e80d2d63fd3b700549a2b129792e [file] [log] [blame]
Anders Carlsson307846f2009-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 Carlsson697f6592009-07-23 03:43:54 +000030 if (D->isUnion()) {
31 LayoutUnion(D);
32 return;
33 }
Anders Carlsson68e0b682009-08-08 18:23:56 +000034
35 Packed = D->hasAttr<PackedAttr>();
Anders Carlsson307846f2009-07-23 03:17:50 +000036
37 if (LayoutFields(D))
38 return;
39
Anders Carlsson307846f2009-07-23 03:17:50 +000040 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +000041 Packed = true;
Anders Carlsson307846f2009-07-23 03:17:50 +000042 AlignmentAsLLVMStruct = 1;
Anders Carlssond5d64132009-07-28 17:56:36 +000043 NextFieldOffsetInBytes = 0;
Anders Carlsson307846f2009-07-23 03:17:50 +000044 FieldTypes.clear();
Anders Carlsson307846f2009-07-23 03:17:50 +000045 LLVMFields.clear();
46 LLVMBitFields.clear();
47
48 LayoutFields(D);
49}
50
51void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
52 uint64_t FieldOffset) {
53 uint64_t FieldSize =
54 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
55
56 if (FieldSize == 0)
57 return;
58
Anders Carlssond5d64132009-07-28 17:56:36 +000059 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8;
Anders Carlsson307846f2009-07-23 03:17:50 +000060 unsigned NumBytesToAppend;
61
62 if (FieldOffset < NextFieldOffset) {
63 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
Anders Carlssond5d64132009-07-28 17:56:36 +000064 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!");
Anders Carlsson307846f2009-07-23 03:17:50 +000065
66 // The bitfield begins in the previous bit-field.
67 NumBytesToAppend =
68 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
69 } else {
70 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
71
72 // Append padding if necessary.
73 AppendBytes((FieldOffset - NextFieldOffset) / 8);
74
75 NumBytesToAppend =
76 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
77
78 assert(NumBytesToAppend && "No bytes to append!");
79 }
80
81 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
82 uint64_t TypeSizeInBits = getTypeSizeInBytes(Ty) * 8;
83
Anders Carlsson8af896c2009-07-23 17:01:21 +000084 LLVMBitFields.push_back(LLVMBitFieldInfo(D, FieldOffset / TypeSizeInBits,
85 FieldOffset % TypeSizeInBits,
Anders Carlsson307846f2009-07-23 03:17:50 +000086 FieldSize));
87
88 AppendBytes(NumBytesToAppend);
89
Anders Carlssonb97a3ec2009-07-27 14:55:54 +000090 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct, getTypeAlignment(Ty));
91
Anders Carlsson516e5e72009-07-23 21:16:33 +000092 BitsAvailableInLastField =
Anders Carlssond5d64132009-07-28 17:56:36 +000093 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +000094}
95
96bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
97 uint64_t FieldOffset) {
Anders Carlsson307846f2009-07-23 03:17:50 +000098 // If the field is packed, then we need a packed struct.
Anders Carlsson68e0b682009-08-08 18:23:56 +000099 if (!Packed && D->hasAttr<PackedAttr>())
Anders Carlsson307846f2009-07-23 03:17:50 +0000100 return false;
101
102 if (D->isBitField()) {
103 // We must use packed structs for unnamed bit fields since they
104 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000105 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000106 return false;
107
108 LayoutBitField(D, FieldOffset);
109 return true;
110 }
111
Anders Carlsson307846f2009-07-23 03:17:50 +0000112 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000113 uint64_t FieldOffsetInBytes = FieldOffset / 8;
114
Anders Carlsson19702bb2009-08-04 16:29:15 +0000115 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
116 unsigned TypeAlignment = getTypeAlignment(Ty);
117
118 // Round up the field offset to the alignment of the field type.
119 uint64_t AlignedNextFieldOffsetInBytes =
120 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment);
121
122 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) {
123 assert(!Packed && "Could not place field even with packed struct!");
124 return false;
125 }
126
127 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
128 // Even with alignment, the field offset is not at the right place,
129 // insert padding.
130 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
131
132 AppendBytes(PaddingInBytes);
133 }
Anders Carlsson307846f2009-07-23 03:17:50 +0000134
Anders Carlsson307846f2009-07-23 03:17:50 +0000135 // Now append the field.
136 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000137 AppendField(FieldOffsetInBytes, Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000138
139 return true;
140}
141
Anders Carlsson697f6592009-07-23 03:43:54 +0000142void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
143 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
144
145 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
146
147 const llvm::Type *Ty = 0;
148 uint64_t Size = 0;
149 unsigned Align = 0;
150
151 unsigned FieldNo = 0;
152 for (RecordDecl::field_iterator Field = D->field_begin(),
153 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
154 assert(Layout.getFieldOffset(FieldNo) == 0 &&
155 "Union field offset did not start at the beginning of record!");
Anders Carlssonf814ee62009-07-23 04:00:39 +0000156
157 if (Field->isBitField()) {
158 uint64_t FieldSize =
159 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
160
161 // Ignore zero sized bit fields.
162 if (FieldSize == 0)
163 continue;
Anders Carlsson08539542009-07-23 22:52:34 +0000164
165 // Add the bit field info.
166 Types.addBitFieldInfo(*Field, 0, 0, FieldSize);
167 } else
168 Types.addFieldInfo(*Field, 0);
Anders Carlsson697f6592009-07-23 03:43:54 +0000169
170 const llvm::Type *FieldTy =
171 Types.ConvertTypeForMemRecursive(Field->getType());
Anders Carlssone2accf42009-07-23 21:52:03 +0000172 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy);
173 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy);
Anders Carlsson697f6592009-07-23 03:43:54 +0000174
175 if (FieldAlign < Align)
176 continue;
177
178 if (FieldAlign > Align || FieldSize > Size) {
179 Ty = FieldTy;
180 Align = FieldAlign;
181 Size = FieldSize;
182 }
183 }
184
185 // Now add our field.
Anders Carlsson08539542009-07-23 22:52:34 +0000186 if (Ty)
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000187 AppendField(0, Ty);
Anders Carlsson697f6592009-07-23 03:43:54 +0000188
189 // Append tail padding.
190 if (Layout.getSize() / 8 > Size)
191 AppendPadding(Layout.getSize() / 8, Align);
192}
193
Anders Carlsson307846f2009-07-23 03:17:50 +0000194bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
195 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
196
Anders Carlsson697f6592009-07-23 03:43:54 +0000197 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Anders Carlsson307846f2009-07-23 03:17:50 +0000198
199 unsigned FieldNo = 0;
Fariborz Jahanian7b2b1ec2009-07-27 20:57:45 +0000200
Anders Carlsson307846f2009-07-23 03:17:50 +0000201 for (RecordDecl::field_iterator Field = D->field_begin(),
202 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
203 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Anders Carlssond78fc892009-07-23 17:24:40 +0000204 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000205 "Could not layout fields even with a packed LLVM struct!");
206 return false;
207 }
208 }
209
210 // Append tail padding if necessary.
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000211 AppendTailPadding(Layout.getSize());
Anders Carlsson307846f2009-07-23 03:17:50 +0000212
213 return true;
214}
215
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000216void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) {
217 assert(RecordSize % 8 == 0 && "Invalid record size!");
218
219 uint64_t RecordSizeInBytes = RecordSize / 8;
Anders Carlssond5d64132009-07-28 17:56:36 +0000220 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!");
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000221
Anders Carlssond5d64132009-07-28 17:56:36 +0000222 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000223 AppendBytes(NumPadBytes);
224}
225
Anders Carlsson307846f2009-07-23 03:17:50 +0000226void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000227 const llvm::Type *FieldTy) {
228 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
229 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000230
231 uint64_t FieldSizeInBytes = getTypeSizeInBytes(FieldTy);
232
Anders Carlsson307846f2009-07-23 03:17:50 +0000233 FieldTypes.push_back(FieldTy);
Anders Carlsson307846f2009-07-23 03:17:50 +0000234
Anders Carlssond5d64132009-07-28 17:56:36 +0000235 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes;
Anders Carlsson307846f2009-07-23 03:17:50 +0000236 BitsAvailableInLastField = 0;
237}
238
239void
240CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
241 const llvm::Type *FieldTy) {
242 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
243}
244
245void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
246 unsigned FieldAlignment) {
Anders Carlsson307846f2009-07-23 03:17:50 +0000247 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
248 "Incorrect field layout!");
249
250 // Round up the field offset to the alignment of the field type.
251 uint64_t AlignedNextFieldOffsetInBytes =
252 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
253
254 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
255 // Even with alignment, the field offset is not at the right place,
256 // insert padding.
257 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
258
259 AppendBytes(PaddingInBytes);
260 }
261}
262
263void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
264 if (NumBytes == 0)
265 return;
266
267 const llvm::Type *Ty = llvm::Type::Int8Ty;
Anders Carlssonb97a3ec2009-07-27 14:55:54 +0000268 if (NumBytes > 1)
Anders Carlsson307846f2009-07-23 03:17:50 +0000269 Ty = llvm::ArrayType::get(Ty, NumBytes);
Anders Carlsson307846f2009-07-23 03:17:50 +0000270
271 // Append the padding field
Anders Carlssond5d64132009-07-28 17:56:36 +0000272 AppendField(NextFieldOffsetInBytes, Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000273}
274
275unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000276 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000277 return 1;
Anders Carlsson307846f2009-07-23 03:17:50 +0000278
279 return Types.getTargetData().getABITypeAlignment(Ty);
280}
281
282uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const {
283 return Types.getTargetData().getTypeAllocSize(Ty);
284}
285
286CGRecordLayout *
287CGRecordLayoutBuilder::ComputeLayout(CodeGenTypes &Types,
288 const RecordDecl *D) {
289 CGRecordLayoutBuilder Builder(Types);
290
Anders Carlsson307846f2009-07-23 03:17:50 +0000291 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000292
Owen Anderson758428f2009-08-05 23:18:46 +0000293 const llvm::Type *Ty = llvm::StructType::get(Types.getLLVMContext(),
294 Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000295 Builder.Packed);
Anders Carlsson453878b2009-08-08 18:01:57 +0000296 assert(Types.getContext().getASTRecordLayout(D).getSize() / 8 ==
297 Types.getTargetData().getTypeAllocSize(Ty) &&
298 "Type size mismatch!");
Anders Carlsson307846f2009-07-23 03:17:50 +0000299
300 // Add all the field numbers.
301 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) {
302 const FieldDecl *FD = Builder.LLVMFields[i].first;
303 unsigned FieldNo = Builder.LLVMFields[i].second;
304
305 Types.addFieldInfo(FD, FieldNo);
306 }
307
308 // Add bitfield info.
309 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) {
310 const LLVMBitFieldInfo &Info = Builder.LLVMBitFields[i];
311
Anders Carlsson8af896c2009-07-23 17:01:21 +0000312 Types.addBitFieldInfo(Info.FD, Info.FieldNo, Info.Start, Info.Size);
Anders Carlsson307846f2009-07-23 03:17:50 +0000313 }
314
315 return new CGRecordLayout(Ty, llvm::SmallSet<unsigned, 8>());
316}