blob: 74051383f1dd30221544cfa1c2c39255f1bccff0 [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 }
34
Anders Carlsson307846f2009-07-23 03:17:50 +000035 if (const PackedAttr* PA = D->getAttr<PackedAttr>())
Anders Carlssond78fc892009-07-23 17:24:40 +000036 Packed = PA->getAlignment();
Anders Carlsson307846f2009-07-23 03:17:50 +000037
38 if (LayoutFields(D))
39 return;
40
Anders Carlsson307846f2009-07-23 03:17:50 +000041 // We weren't able to layout the struct. Try again with a packed struct
Anders Carlssond78fc892009-07-23 17:24:40 +000042 Packed = true;
Anders Carlsson307846f2009-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 Carlsson8af896c2009-07-23 17:01:21 +000085 LLVMBitFields.push_back(LLVMBitFieldInfo(D, FieldOffset / TypeSizeInBits,
86 FieldOffset % TypeSizeInBits,
Anders Carlsson307846f2009-07-23 03:17:50 +000087 FieldSize));
88
89 AppendBytes(NumBytesToAppend);
90
Anders Carlsson516e5e72009-07-23 21:16:33 +000091 BitsAvailableInLastField =
92 getNextFieldOffsetInBytes() * 8 - (FieldOffset + FieldSize);
Anders Carlsson307846f2009-07-23 03:17:50 +000093}
94
95bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
96 uint64_t FieldOffset) {
Anders Carlssond78fc892009-07-23 17:24:40 +000097 bool FieldPacked = Packed;
Anders Carlsson307846f2009-07-23 03:17:50 +000098
99 // FIXME: Should this override struct packing? Probably we want to
100 // take the minimum?
101 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
Anders Carlssond78fc892009-07-23 17:24:40 +0000102 FieldPacked = PA->getAlignment();
Anders Carlsson307846f2009-07-23 03:17:50 +0000103
104 // If the field is packed, then we need a packed struct.
Anders Carlssond78fc892009-07-23 17:24:40 +0000105 if (!Packed && FieldPacked)
Anders Carlsson307846f2009-07-23 03:17:50 +0000106 return false;
107
108 if (D->isBitField()) {
109 // We must use packed structs for unnamed bit fields since they
110 // don't affect the struct alignment.
Anders Carlssond78fc892009-07-23 17:24:40 +0000111 if (!Packed && !D->getDeclName())
Anders Carlsson307846f2009-07-23 03:17:50 +0000112 return false;
113
114 LayoutBitField(D, FieldOffset);
115 return true;
116 }
117
118 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
119
120 // Check if the field is aligned.
121 if (const AlignedAttr *PA = D->getAttr<AlignedAttr>()) {
122 unsigned FieldAlign = PA->getAlignment();
123
Anders Carlssond78fc892009-07-23 17:24:40 +0000124 if (!Packed && getTypeAlignment(Ty) > FieldAlign)
Anders Carlsson307846f2009-07-23 03:17:50 +0000125 return false;
126 }
127
128 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
129
130 uint64_t FieldOffsetInBytes = FieldOffset / 8;
131
132 // Append padding if necessary.
133 AppendPadding(FieldOffsetInBytes, Ty);
134
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;
200 for (RecordDecl::field_iterator Field = D->field_begin(),
201 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
202 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
Anders Carlssond78fc892009-07-23 17:24:40 +0000203 assert(!Packed &&
Anders Carlsson307846f2009-07-23 03:17:50 +0000204 "Could not layout fields even with a packed LLVM struct!");
205 return false;
206 }
207 }
208
209 // Append tail padding if necessary.
210 if (Layout.getSize() / 8 > getNextFieldOffsetInBytes())
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000211 AppendPadding(getNextFieldOffsetInBytes(), AlignmentAsLLVMStruct);
Anders Carlsson307846f2009-07-23 03:17:50 +0000212
213 return true;
214}
215
216void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
Anders Carlsson307846f2009-07-23 03:17:50 +0000217 const llvm::Type *FieldTy) {
218 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
219 getTypeAlignment(FieldTy));
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000220
221 uint64_t FieldSizeInBytes = getTypeSizeInBytes(FieldTy);
222
Anders Carlsson307846f2009-07-23 03:17:50 +0000223 FieldTypes.push_back(FieldTy);
224 FieldInfos.push_back(FieldInfo(FieldOffsetInBytes, FieldSizeInBytes));
225
226 BitsAvailableInLastField = 0;
227}
228
229void
230CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
231 const llvm::Type *FieldTy) {
232 AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
233}
234
235void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
236 unsigned FieldAlignment) {
237 uint64_t NextFieldOffsetInBytes = getNextFieldOffsetInBytes();
238 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
239 "Incorrect field layout!");
240
241 // Round up the field offset to the alignment of the field type.
242 uint64_t AlignedNextFieldOffsetInBytes =
243 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
244
245 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
246 // Even with alignment, the field offset is not at the right place,
247 // insert padding.
248 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
249
250 AppendBytes(PaddingInBytes);
251 }
252}
253
254void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
255 if (NumBytes == 0)
256 return;
257
258 const llvm::Type *Ty = llvm::Type::Int8Ty;
259 if (NumBytes > 1) {
260 // FIXME: Use a VMContext.
261 Ty = llvm::ArrayType::get(Ty, NumBytes);
262 }
263
264 // Append the padding field
Anders Carlsson6e853bf2009-07-24 02:45:50 +0000265 AppendField(getNextFieldOffsetInBytes(), Ty);
Anders Carlsson307846f2009-07-23 03:17:50 +0000266}
267
268uint64_t CGRecordLayoutBuilder::getNextFieldOffsetInBytes() const {
269 if (FieldInfos.empty())
270 return 0;
271
272 const FieldInfo &LastInfo = FieldInfos.back();
273 return LastInfo.OffsetInBytes + LastInfo.SizeInBytes;
274}
275
276unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
Anders Carlssond78fc892009-07-23 17:24:40 +0000277 if (Packed)
Anders Carlsson307846f2009-07-23 03:17:50 +0000278 return 1;
Anders Carlsson307846f2009-07-23 03:17:50 +0000279
280 return Types.getTargetData().getABITypeAlignment(Ty);
281}
282
283uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const {
284 return Types.getTargetData().getTypeAllocSize(Ty);
285}
286
287CGRecordLayout *
288CGRecordLayoutBuilder::ComputeLayout(CodeGenTypes &Types,
289 const RecordDecl *D) {
290 CGRecordLayoutBuilder Builder(Types);
291
Anders Carlsson307846f2009-07-23 03:17:50 +0000292 Builder.Layout(D);
Anders Carlssone1d5ca52009-07-24 15:20:52 +0000293
Anders Carlsson307846f2009-07-23 03:17:50 +0000294 // FIXME: Once this works well enough, enable it.
295 return 0;
296
297 // FIXME: Use a VMContext.
298 const llvm::Type *Ty = llvm::StructType::get(Builder.FieldTypes,
Anders Carlssond78fc892009-07-23 17:24:40 +0000299 Builder.Packed);
Anders Carlsson307846f2009-07-23 03:17:50 +0000300
301 // Add all the field numbers.
302 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) {
303 const FieldDecl *FD = Builder.LLVMFields[i].first;
304 unsigned FieldNo = Builder.LLVMFields[i].second;
305
306 Types.addFieldInfo(FD, FieldNo);
307 }
308
309 // Add bitfield info.
310 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) {
311 const LLVMBitFieldInfo &Info = Builder.LLVMBitFields[i];
312
Anders Carlsson8af896c2009-07-23 17:01:21 +0000313 Types.addBitFieldInfo(Info.FD, Info.FieldNo, Info.Start, Info.Size);
Anders Carlsson307846f2009-07-23 03:17:50 +0000314 }
315
316 return new CGRecordLayout(Ty, llvm::SmallSet<unsigned, 8>());
317}