blob: 5f6946db6ba9e4c75dc2533a08e705cc37407755 [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>())
36 StructPacking = PA->getAlignment();
37
38 if (LayoutFields(D))
39 return;
40
41 assert(!StructPacking &&
42 "FIXME: Were not able to lay out a struct with #pragma pack!");
43
44 // We weren't able to layout the struct. Try again with a packed struct
45 StructPacking = 1;
46 AlignmentAsLLVMStruct = 1;
47 FieldTypes.clear();
48 FieldInfos.clear();
49 LLVMFields.clear();
50 LLVMBitFields.clear();
51
52 LayoutFields(D);
53}
54
55void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,
56 uint64_t FieldOffset) {
57 uint64_t FieldSize =
58 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
59
60 if (FieldSize == 0)
61 return;
62
63 uint64_t NextFieldOffset = getNextFieldOffsetInBytes() * 8;
64 unsigned NumBytesToAppend;
65
66 if (FieldOffset < NextFieldOffset) {
67 assert(BitsAvailableInLastField && "Bitfield size mismatch!");
68 assert(!FieldInfos.empty() && "Field infos can't be empty!");
69
70 // The bitfield begins in the previous bit-field.
71 NumBytesToAppend =
72 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8;
73 } else {
74 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly");
75
76 // Append padding if necessary.
77 AppendBytes((FieldOffset - NextFieldOffset) / 8);
78
79 NumBytesToAppend =
80 llvm::RoundUpToAlignment(FieldSize, 8) / 8;
81
82 assert(NumBytesToAppend && "No bytes to append!");
83 }
84
85 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
86 uint64_t TypeSizeInBits = getTypeSizeInBytes(Ty) * 8;
87
Anders Carlsson8330cee2009-07-23 17:01:21 +000088 LLVMBitFields.push_back(LLVMBitFieldInfo(D, FieldOffset / TypeSizeInBits,
89 FieldOffset % TypeSizeInBits,
Anders Carlsson45372a62009-07-23 03:17:50 +000090 FieldSize));
91
92 AppendBytes(NumBytesToAppend);
93
94 if (!NumBytesToAppend)
95 BitsAvailableInLastField -= FieldSize;
96 else
97 BitsAvailableInLastField = NumBytesToAppend * 8 - FieldSize;
98}
99
100bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D,
101 uint64_t FieldOffset) {
102 unsigned FieldPacking = StructPacking;
103
104 // FIXME: Should this override struct packing? Probably we want to
105 // take the minimum?
106 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
107 FieldPacking = PA->getAlignment();
108
109 // If the field is packed, then we need a packed struct.
110 if (!StructPacking && FieldPacking)
111 return false;
112
113 if (D->isBitField()) {
114 // We must use packed structs for unnamed bit fields since they
115 // don't affect the struct alignment.
116 if (!StructPacking && !D->getDeclName())
117 return false;
118
119 LayoutBitField(D, FieldOffset);
120 return true;
121 }
122
123 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType());
124
125 // Check if the field is aligned.
126 if (const AlignedAttr *PA = D->getAttr<AlignedAttr>()) {
127 unsigned FieldAlign = PA->getAlignment();
128
129 if (!StructPacking && getTypeAlignment(Ty) > FieldAlign)
130 return false;
131 }
132
133 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!");
134
135 uint64_t FieldOffsetInBytes = FieldOffset / 8;
136
137 // Append padding if necessary.
138 AppendPadding(FieldOffsetInBytes, Ty);
139
140 uint64_t FieldSizeInBytes = getTypeSizeInBytes(Ty);
141
142 // Now append the field.
143 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size()));
144 AppendField(FieldOffsetInBytes, FieldSizeInBytes, Ty);
145
146 return true;
147}
148
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000149void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) {
150 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!");
151
152 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
153
Anders Carlssoncfc67582009-07-23 04:59:05 +0000154 const FieldDecl *FD = 0;
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000155 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;
172 }
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000173
174 const llvm::Type *FieldTy =
175 Types.ConvertTypeForMemRecursive(Field->getType());
176 unsigned FieldAlign = Types.getTargetData().getTypeAllocSize(FieldTy);
177 uint64_t FieldSize = Types.getTargetData().getABITypeAlignment(FieldTy);
178
179 if (FieldAlign < Align)
180 continue;
181
182 if (FieldAlign > Align || FieldSize > Size) {
183 Ty = FieldTy;
184 Align = FieldAlign;
185 Size = FieldSize;
Anders Carlssoncfc67582009-07-23 04:59:05 +0000186 FD = *Field;
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000187 }
188 }
189
190 // Now add our field.
Anders Carlssoncfc67582009-07-23 04:59:05 +0000191 if (FD) {
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000192 AppendField(0, Size, Ty);
Anders Carlssoncfc67582009-07-23 04:59:05 +0000193
194 if (FD->isBitField()) {
195 uint64_t FieldSize =
196 FD->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue();
Anders Carlsson8330cee2009-07-23 17:01:21 +0000197 Types.addBitFieldInfo(FD, 0, 0, FieldSize);
198 } else
199 Types.addFieldInfo(FD, 0);
Anders Carlssoncfc67582009-07-23 04:59:05 +0000200 }
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000201
202 // Append tail padding.
203 if (Layout.getSize() / 8 > Size)
204 AppendPadding(Layout.getSize() / 8, Align);
205}
206
Anders Carlsson45372a62009-07-23 03:17:50 +0000207bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
208 assert(!D->isUnion() && "Can't call LayoutFields on a union!");
209
Anders Carlsson5a6e3982009-07-23 03:43:54 +0000210 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
Anders Carlsson45372a62009-07-23 03:17:50 +0000211
212 unsigned FieldNo = 0;
213 for (RecordDecl::field_iterator Field = D->field_begin(),
214 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
215 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
216 assert(!StructPacking &&
217 "Could not layout fields even with a packed LLVM struct!");
218 return false;
219 }
220 }
221
222 // Append tail padding if necessary.
223 if (Layout.getSize() / 8 > getNextFieldOffsetInBytes())
224 AppendPadding(Layout.getSize() / 8, AlignmentAsLLVMStruct);
225
226 return true;
227}
228
229void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
230 uint64_t FieldSizeInBytes,
231 const llvm::Type *FieldTy) {
232 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
233 getTypeAlignment(FieldTy));
234
235 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;
271 if (NumBytes > 1) {
272 // FIXME: Use a VMContext.
273 Ty = llvm::ArrayType::get(Ty, NumBytes);
274 }
275
276 // Append the padding field
277 AppendField(getNextFieldOffsetInBytes(), NumBytes, Ty);
278}
279
280uint64_t CGRecordLayoutBuilder::getNextFieldOffsetInBytes() const {
281 if (FieldInfos.empty())
282 return 0;
283
284 const FieldInfo &LastInfo = FieldInfos.back();
285 return LastInfo.OffsetInBytes + LastInfo.SizeInBytes;
286}
287
288unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
289 if (StructPacking) {
290 assert(StructPacking == 1 && "FIXME: What if StructPacking is not 1 here");
291 return 1;
292 }
293
294 return Types.getTargetData().getABITypeAlignment(Ty);
295}
296
297uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const {
298 return Types.getTargetData().getTypeAllocSize(Ty);
299}
300
301CGRecordLayout *
302CGRecordLayoutBuilder::ComputeLayout(CodeGenTypes &Types,
303 const RecordDecl *D) {
304 CGRecordLayoutBuilder Builder(Types);
305
Anders Carlsson45372a62009-07-23 03:17:50 +0000306 Builder.Layout(D);
307
308 // FIXME: Once this works well enough, enable it.
309 return 0;
310
311 // FIXME: Use a VMContext.
312 const llvm::Type *Ty = llvm::StructType::get(Builder.FieldTypes,
313 Builder.StructPacking);
314
315 // Add all the field numbers.
316 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) {
317 const FieldDecl *FD = Builder.LLVMFields[i].first;
318 unsigned FieldNo = Builder.LLVMFields[i].second;
319
320 Types.addFieldInfo(FD, FieldNo);
321 }
322
323 // Add bitfield info.
324 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) {
325 const LLVMBitFieldInfo &Info = Builder.LLVMBitFields[i];
326
Anders Carlsson8330cee2009-07-23 17:01:21 +0000327 Types.addBitFieldInfo(Info.FD, Info.FieldNo, Info.Start, Info.Size);
Anders Carlsson45372a62009-07-23 03:17:50 +0000328 }
329
330 return new CGRecordLayout(Ty, llvm::SmallSet<unsigned, 8>());
331}