CGRecordLayoutBuilder.cpp revision 5a6e398eafa48c56e8741ad2cd36630badd98f30
1//===--- 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) {
30  if (D->isUnion()) {
31    LayoutUnion(D);
32    return;
33  }
34
35  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
88  LLVMFields.push_back(LLVMFieldInfo(D, FieldOffset / TypeSizeInBits));
89  LLVMBitFields.push_back(LLVMBitFieldInfo(D, FieldOffset % TypeSizeInBits,
90                                           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
149void 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
154  const llvm::Type *Ty = 0;
155  uint64_t Size = 0;
156  unsigned Align = 0;
157
158  unsigned FieldNo = 0;
159  for (RecordDecl::field_iterator Field = D->field_begin(),
160       FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
161    assert(Layout.getFieldOffset(FieldNo) == 0 &&
162          "Union field offset did not start at the beginning of record!");
163
164    const llvm::Type *FieldTy =
165      Types.ConvertTypeForMemRecursive(Field->getType());
166    unsigned FieldAlign = Types.getTargetData().getTypeAllocSize(FieldTy);
167    uint64_t FieldSize = Types.getTargetData().getABITypeAlignment(FieldTy);
168
169    if (FieldAlign < Align)
170      continue;
171
172    if (FieldAlign > Align || FieldSize > Size) {
173      Ty = FieldTy;
174      Align = FieldAlign;
175      Size = FieldSize;
176    }
177  }
178
179  // Now add our field.
180  if (Ty)
181    AppendField(0, Size, Ty);
182
183  // Append tail padding.
184  if (Layout.getSize() / 8 > Size)
185    AppendPadding(Layout.getSize() / 8, Align);
186}
187
188bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) {
189  assert(!D->isUnion() && "Can't call LayoutFields on a union!");
190
191  const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D);
192
193  unsigned FieldNo = 0;
194  for (RecordDecl::field_iterator Field = D->field_begin(),
195       FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
196    if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) {
197      assert(!StructPacking &&
198             "Could not layout fields even with a packed LLVM struct!");
199      return false;
200    }
201  }
202
203  // Append tail padding if necessary.
204  if (Layout.getSize() / 8 > getNextFieldOffsetInBytes())
205    AppendPadding(Layout.getSize() / 8, AlignmentAsLLVMStruct);
206
207  return true;
208}
209
210void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes,
211                                        uint64_t FieldSizeInBytes,
212                                        const llvm::Type *FieldTy) {
213  AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct,
214                                   getTypeAlignment(FieldTy));
215
216  FieldTypes.push_back(FieldTy);
217  FieldInfos.push_back(FieldInfo(FieldOffsetInBytes, FieldSizeInBytes));
218
219  BitsAvailableInLastField = 0;
220}
221
222void
223CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
224                                     const llvm::Type *FieldTy) {
225  AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy));
226}
227
228void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes,
229                                          unsigned FieldAlignment) {
230  uint64_t NextFieldOffsetInBytes = getNextFieldOffsetInBytes();
231  assert(NextFieldOffsetInBytes <= FieldOffsetInBytes &&
232         "Incorrect field layout!");
233
234  // Round up the field offset to the alignment of the field type.
235  uint64_t AlignedNextFieldOffsetInBytes =
236    llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment);
237
238  if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) {
239    // Even with alignment, the field offset is not at the right place,
240    // insert padding.
241    uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes;
242
243    AppendBytes(PaddingInBytes);
244  }
245}
246
247void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) {
248  if (NumBytes == 0)
249    return;
250
251  const llvm::Type *Ty = llvm::Type::Int8Ty;
252  if (NumBytes > 1) {
253    // FIXME: Use a VMContext.
254    Ty = llvm::ArrayType::get(Ty, NumBytes);
255  }
256
257  // Append the padding field
258  AppendField(getNextFieldOffsetInBytes(), NumBytes, Ty);
259}
260
261uint64_t CGRecordLayoutBuilder::getNextFieldOffsetInBytes() const {
262  if (FieldInfos.empty())
263    return 0;
264
265  const FieldInfo &LastInfo = FieldInfos.back();
266  return LastInfo.OffsetInBytes + LastInfo.SizeInBytes;
267}
268
269unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const {
270  if (StructPacking) {
271    assert(StructPacking == 1 && "FIXME: What if StructPacking is not 1 here");
272    return 1;
273  }
274
275  return Types.getTargetData().getABITypeAlignment(Ty);
276}
277
278uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const {
279  return Types.getTargetData().getTypeAllocSize(Ty);
280}
281
282CGRecordLayout *
283CGRecordLayoutBuilder::ComputeLayout(CodeGenTypes &Types,
284                                     const RecordDecl *D) {
285  CGRecordLayoutBuilder Builder(Types);
286
287  Builder.Layout(D);
288
289  // FIXME: Once this works well enough, enable it.
290  return 0;
291
292  // FIXME: Use a VMContext.
293  const llvm::Type *Ty = llvm::StructType::get(Builder.FieldTypes,
294                                               Builder.StructPacking);
295
296  // Add all the field numbers.
297  for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) {
298    const FieldDecl *FD = Builder.LLVMFields[i].first;
299    unsigned FieldNo = Builder.LLVMFields[i].second;
300
301    Types.addFieldInfo(FD, FieldNo);
302  }
303
304  // Add bitfield info.
305  for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) {
306    const LLVMBitFieldInfo &Info = Builder.LLVMBitFields[i];
307
308    Types.addBitFieldInfo(Info.FD, Info.Start, Info.Size);
309  }
310
311  return new CGRecordLayout(Ty, llvm::SmallSet<unsigned, 8>());
312}
313