Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 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 | |
| 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
| 29 | void CGRecordLayoutBuilder::Layout(const RecordDecl *D) { |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 30 | if (D->isUnion()) { |
| 31 | LayoutUnion(D); |
| 32 | return; |
| 33 | } |
| 34 | |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 35 | if (const PackedAttr* PA = D->getAttr<PackedAttr>()) |
Anders Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 36 | Packed = PA->getAlignment(); |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 37 | |
| 38 | if (LayoutFields(D)) |
| 39 | return; |
| 40 | |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 41 | // We weren't able to layout the struct. Try again with a packed struct |
Anders Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 42 | Packed = true; |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 43 | AlignmentAsLLVMStruct = 1; |
| 44 | FieldTypes.clear(); |
| 45 | FieldInfos.clear(); |
| 46 | LLVMFields.clear(); |
| 47 | LLVMBitFields.clear(); |
| 48 | |
| 49 | LayoutFields(D); |
| 50 | } |
| 51 | |
| 52 | void 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 Carlsson | 8330cee | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 85 | LLVMBitFields.push_back(LLVMBitFieldInfo(D, FieldOffset / TypeSizeInBits, |
| 86 | FieldOffset % TypeSizeInBits, |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 87 | FieldSize)); |
| 88 | |
| 89 | AppendBytes(NumBytesToAppend); |
| 90 | |
Anders Carlsson | faaec22 | 2009-07-23 21:16:33 +0000 | [diff] [blame] | 91 | BitsAvailableInLastField = |
| 92 | getNextFieldOffsetInBytes() * 8 - (FieldOffset + FieldSize); |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D, |
| 96 | uint64_t FieldOffset) { |
Anders Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 97 | bool FieldPacked = Packed; |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 98 | |
| 99 | // FIXME: Should this override struct packing? Probably we want to |
| 100 | // take the minimum? |
| 101 | if (const PackedAttr *PA = D->getAttr<PackedAttr>()) |
Anders Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 102 | FieldPacked = PA->getAlignment(); |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 103 | |
| 104 | // If the field is packed, then we need a packed struct. |
Anders Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 105 | if (!Packed && FieldPacked) |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 106 | 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 Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 111 | if (!Packed && !D->getDeclName()) |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 112 | 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 Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 124 | if (!Packed && getTypeAlignment(Ty) > FieldAlign) |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 125 | 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 | |
| 135 | uint64_t FieldSizeInBytes = getTypeSizeInBytes(Ty); |
| 136 | |
| 137 | // Now append the field. |
| 138 | LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size())); |
| 139 | AppendField(FieldOffsetInBytes, FieldSizeInBytes, Ty); |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 144 | void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) { |
| 145 | assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!"); |
| 146 | |
| 147 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); |
| 148 | |
Anders Carlsson | cfc6758 | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 149 | const FieldDecl *FD = 0; |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 150 | const llvm::Type *Ty = 0; |
| 151 | uint64_t Size = 0; |
| 152 | unsigned Align = 0; |
| 153 | |
| 154 | unsigned FieldNo = 0; |
| 155 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 156 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
| 157 | assert(Layout.getFieldOffset(FieldNo) == 0 && |
| 158 | "Union field offset did not start at the beginning of record!"); |
Anders Carlsson | 2cc8f17 | 2009-07-23 04:00:39 +0000 | [diff] [blame] | 159 | |
| 160 | if (Field->isBitField()) { |
| 161 | uint64_t FieldSize = |
| 162 | Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); |
| 163 | |
| 164 | // Ignore zero sized bit fields. |
| 165 | if (FieldSize == 0) |
| 166 | continue; |
| 167 | } |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 168 | |
| 169 | const llvm::Type *FieldTy = |
| 170 | Types.ConvertTypeForMemRecursive(Field->getType()); |
Anders Carlsson | 177d4d8 | 2009-07-23 21:52:03 +0000 | [diff] [blame] | 171 | unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy); |
| 172 | uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy); |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 173 | |
| 174 | if (FieldAlign < Align) |
| 175 | continue; |
| 176 | |
| 177 | if (FieldAlign > Align || FieldSize > Size) { |
| 178 | Ty = FieldTy; |
| 179 | Align = FieldAlign; |
| 180 | Size = FieldSize; |
Anders Carlsson | cfc6758 | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 181 | FD = *Field; |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | // Now add our field. |
Anders Carlsson | cfc6758 | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 186 | if (FD) { |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 187 | AppendField(0, Size, Ty); |
Anders Carlsson | cfc6758 | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 188 | |
| 189 | if (FD->isBitField()) { |
| 190 | uint64_t FieldSize = |
| 191 | FD->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); |
Anders Carlsson | 8330cee | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 192 | Types.addBitFieldInfo(FD, 0, 0, FieldSize); |
| 193 | } else |
| 194 | Types.addFieldInfo(FD, 0); |
Anders Carlsson | cfc6758 | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 195 | } |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 196 | |
| 197 | // Append tail padding. |
| 198 | if (Layout.getSize() / 8 > Size) |
| 199 | AppendPadding(Layout.getSize() / 8, Align); |
| 200 | } |
| 201 | |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 202 | bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
| 203 | assert(!D->isUnion() && "Can't call LayoutFields on a union!"); |
| 204 | |
Anders Carlsson | 5a6e398 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 205 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 206 | |
| 207 | unsigned FieldNo = 0; |
| 208 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 209 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
| 210 | if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) { |
Anders Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 211 | assert(!Packed && |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 212 | "Could not layout fields even with a packed LLVM struct!"); |
| 213 | return false; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Append tail padding if necessary. |
| 218 | if (Layout.getSize() / 8 > getNextFieldOffsetInBytes()) |
| 219 | AppendPadding(Layout.getSize() / 8, AlignmentAsLLVMStruct); |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes, |
| 225 | uint64_t FieldSizeInBytes, |
| 226 | const llvm::Type *FieldTy) { |
| 227 | AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct, |
| 228 | getTypeAlignment(FieldTy)); |
| 229 | |
| 230 | FieldTypes.push_back(FieldTy); |
| 231 | FieldInfos.push_back(FieldInfo(FieldOffsetInBytes, FieldSizeInBytes)); |
| 232 | |
| 233 | BitsAvailableInLastField = 0; |
| 234 | } |
| 235 | |
| 236 | void |
| 237 | CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes, |
| 238 | const llvm::Type *FieldTy) { |
| 239 | AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy)); |
| 240 | } |
| 241 | |
| 242 | void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes, |
| 243 | unsigned FieldAlignment) { |
| 244 | uint64_t NextFieldOffsetInBytes = getNextFieldOffsetInBytes(); |
| 245 | assert(NextFieldOffsetInBytes <= FieldOffsetInBytes && |
| 246 | "Incorrect field layout!"); |
| 247 | |
| 248 | // Round up the field offset to the alignment of the field type. |
| 249 | uint64_t AlignedNextFieldOffsetInBytes = |
| 250 | llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment); |
| 251 | |
| 252 | if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) { |
| 253 | // Even with alignment, the field offset is not at the right place, |
| 254 | // insert padding. |
| 255 | uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes; |
| 256 | |
| 257 | AppendBytes(PaddingInBytes); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) { |
| 262 | if (NumBytes == 0) |
| 263 | return; |
| 264 | |
| 265 | const llvm::Type *Ty = llvm::Type::Int8Ty; |
| 266 | if (NumBytes > 1) { |
| 267 | // FIXME: Use a VMContext. |
| 268 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 269 | } |
| 270 | |
| 271 | // Append the padding field |
| 272 | AppendField(getNextFieldOffsetInBytes(), NumBytes, Ty); |
| 273 | } |
| 274 | |
| 275 | uint64_t CGRecordLayoutBuilder::getNextFieldOffsetInBytes() const { |
| 276 | if (FieldInfos.empty()) |
| 277 | return 0; |
| 278 | |
| 279 | const FieldInfo &LastInfo = FieldInfos.back(); |
| 280 | return LastInfo.OffsetInBytes + LastInfo.SizeInBytes; |
| 281 | } |
| 282 | |
| 283 | unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const { |
Anders Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 284 | if (Packed) |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 285 | return 1; |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 286 | |
| 287 | return Types.getTargetData().getABITypeAlignment(Ty); |
| 288 | } |
| 289 | |
| 290 | uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const { |
| 291 | return Types.getTargetData().getTypeAllocSize(Ty); |
| 292 | } |
| 293 | |
| 294 | CGRecordLayout * |
| 295 | CGRecordLayoutBuilder::ComputeLayout(CodeGenTypes &Types, |
| 296 | const RecordDecl *D) { |
| 297 | CGRecordLayoutBuilder Builder(Types); |
| 298 | |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 299 | Builder.Layout(D); |
| 300 | |
| 301 | // FIXME: Once this works well enough, enable it. |
| 302 | return 0; |
| 303 | |
| 304 | // FIXME: Use a VMContext. |
| 305 | const llvm::Type *Ty = llvm::StructType::get(Builder.FieldTypes, |
Anders Carlsson | 4b5584b | 2009-07-23 17:24:40 +0000 | [diff] [blame] | 306 | Builder.Packed); |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 307 | |
| 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 Carlsson | 8330cee | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 320 | Types.addBitFieldInfo(Info.FD, Info.FieldNo, Info.Start, Info.Size); |
Anders Carlsson | 45372a6 | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | return new CGRecordLayout(Ty, llvm::SmallSet<unsigned, 8>()); |
| 324 | } |