Anders Carlsson | 307846f | 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 | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 30 | if (D->isUnion()) { |
| 31 | LayoutUnion(D); |
| 32 | return; |
| 33 | } |
| 34 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 35 | if (const PackedAttr* PA = D->getAttr<PackedAttr>()) |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 36 | Packed = PA->getAlignment(); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 37 | |
| 38 | if (LayoutFields(D)) |
| 39 | return; |
| 40 | |
Anders Carlsson | 307846f | 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 | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 42 | Packed = true; |
Anders Carlsson | 307846f | 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 | 8af896c | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 85 | LLVMBitFields.push_back(LLVMBitFieldInfo(D, FieldOffset / TypeSizeInBits, |
| 86 | FieldOffset % TypeSizeInBits, |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 87 | FieldSize)); |
| 88 | |
| 89 | AppendBytes(NumBytesToAppend); |
| 90 | |
| 91 | if (!NumBytesToAppend) |
| 92 | BitsAvailableInLastField -= FieldSize; |
| 93 | else |
| 94 | BitsAvailableInLastField = NumBytesToAppend * 8 - FieldSize; |
| 95 | } |
| 96 | |
| 97 | bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D, |
| 98 | uint64_t FieldOffset) { |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 99 | bool FieldPacked = Packed; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 100 | |
| 101 | // FIXME: Should this override struct packing? Probably we want to |
| 102 | // take the minimum? |
| 103 | if (const PackedAttr *PA = D->getAttr<PackedAttr>()) |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 104 | FieldPacked = PA->getAlignment(); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 105 | |
| 106 | // If the field is packed, then we need a packed struct. |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 107 | if (!Packed && FieldPacked) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 108 | return false; |
| 109 | |
| 110 | if (D->isBitField()) { |
| 111 | // We must use packed structs for unnamed bit fields since they |
| 112 | // don't affect the struct alignment. |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 113 | if (!Packed && !D->getDeclName()) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 114 | return false; |
| 115 | |
| 116 | LayoutBitField(D, FieldOffset); |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType()); |
| 121 | |
| 122 | // Check if the field is aligned. |
| 123 | if (const AlignedAttr *PA = D->getAttr<AlignedAttr>()) { |
| 124 | unsigned FieldAlign = PA->getAlignment(); |
| 125 | |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 126 | if (!Packed && getTypeAlignment(Ty) > FieldAlign) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 127 | return false; |
| 128 | } |
| 129 | |
| 130 | assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!"); |
| 131 | |
| 132 | uint64_t FieldOffsetInBytes = FieldOffset / 8; |
| 133 | |
| 134 | // Append padding if necessary. |
| 135 | AppendPadding(FieldOffsetInBytes, Ty); |
| 136 | |
| 137 | uint64_t FieldSizeInBytes = getTypeSizeInBytes(Ty); |
| 138 | |
| 139 | // Now append the field. |
| 140 | LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size())); |
| 141 | AppendField(FieldOffsetInBytes, FieldSizeInBytes, Ty); |
| 142 | |
| 143 | return true; |
| 144 | } |
| 145 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 146 | void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) { |
| 147 | assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!"); |
| 148 | |
| 149 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); |
| 150 | |
Anders Carlsson | f55922b | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 151 | const FieldDecl *FD = 0; |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 152 | const llvm::Type *Ty = 0; |
| 153 | uint64_t Size = 0; |
| 154 | unsigned Align = 0; |
| 155 | |
| 156 | unsigned FieldNo = 0; |
| 157 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 158 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
| 159 | assert(Layout.getFieldOffset(FieldNo) == 0 && |
| 160 | "Union field offset did not start at the beginning of record!"); |
Anders Carlsson | f814ee6 | 2009-07-23 04:00:39 +0000 | [diff] [blame] | 161 | |
| 162 | if (Field->isBitField()) { |
| 163 | uint64_t FieldSize = |
| 164 | Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); |
| 165 | |
| 166 | // Ignore zero sized bit fields. |
| 167 | if (FieldSize == 0) |
| 168 | continue; |
| 169 | } |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 170 | |
| 171 | const llvm::Type *FieldTy = |
| 172 | Types.ConvertTypeForMemRecursive(Field->getType()); |
| 173 | unsigned FieldAlign = Types.getTargetData().getTypeAllocSize(FieldTy); |
| 174 | uint64_t FieldSize = Types.getTargetData().getABITypeAlignment(FieldTy); |
| 175 | |
| 176 | if (FieldAlign < Align) |
| 177 | continue; |
| 178 | |
| 179 | if (FieldAlign > Align || FieldSize > Size) { |
| 180 | Ty = FieldTy; |
| 181 | Align = FieldAlign; |
| 182 | Size = FieldSize; |
Anders Carlsson | f55922b | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 183 | FD = *Field; |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | // Now add our field. |
Anders Carlsson | f55922b | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 188 | if (FD) { |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 189 | AppendField(0, Size, Ty); |
Anders Carlsson | f55922b | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 190 | |
| 191 | if (FD->isBitField()) { |
| 192 | uint64_t FieldSize = |
| 193 | FD->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); |
Anders Carlsson | 8af896c | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 194 | Types.addBitFieldInfo(FD, 0, 0, FieldSize); |
| 195 | } else |
| 196 | Types.addFieldInfo(FD, 0); |
Anders Carlsson | f55922b | 2009-07-23 04:59:05 +0000 | [diff] [blame] | 197 | } |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 198 | |
| 199 | // Append tail padding. |
| 200 | if (Layout.getSize() / 8 > Size) |
| 201 | AppendPadding(Layout.getSize() / 8, Align); |
| 202 | } |
| 203 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 204 | bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { |
| 205 | assert(!D->isUnion() && "Can't call LayoutFields on a union!"); |
| 206 | |
Anders Carlsson | 697f659 | 2009-07-23 03:43:54 +0000 | [diff] [blame] | 207 | const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 208 | |
| 209 | unsigned FieldNo = 0; |
| 210 | for (RecordDecl::field_iterator Field = D->field_begin(), |
| 211 | FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
| 212 | if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) { |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 213 | assert(!Packed && |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 214 | "Could not layout fields even with a packed LLVM struct!"); |
| 215 | return false; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Append tail padding if necessary. |
| 220 | if (Layout.getSize() / 8 > getNextFieldOffsetInBytes()) |
| 221 | AppendPadding(Layout.getSize() / 8, AlignmentAsLLVMStruct); |
| 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes, |
| 227 | uint64_t FieldSizeInBytes, |
| 228 | const llvm::Type *FieldTy) { |
| 229 | AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct, |
| 230 | getTypeAlignment(FieldTy)); |
| 231 | |
| 232 | FieldTypes.push_back(FieldTy); |
| 233 | FieldInfos.push_back(FieldInfo(FieldOffsetInBytes, FieldSizeInBytes)); |
| 234 | |
| 235 | BitsAvailableInLastField = 0; |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes, |
| 240 | const llvm::Type *FieldTy) { |
| 241 | AppendPadding(FieldOffsetInBytes, getTypeAlignment(FieldTy)); |
| 242 | } |
| 243 | |
| 244 | void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes, |
| 245 | unsigned FieldAlignment) { |
| 246 | uint64_t NextFieldOffsetInBytes = getNextFieldOffsetInBytes(); |
| 247 | 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 | |
| 263 | void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) { |
| 264 | if (NumBytes == 0) |
| 265 | return; |
| 266 | |
| 267 | const llvm::Type *Ty = llvm::Type::Int8Ty; |
| 268 | if (NumBytes > 1) { |
| 269 | // FIXME: Use a VMContext. |
| 270 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 271 | } |
| 272 | |
| 273 | // Append the padding field |
| 274 | AppendField(getNextFieldOffsetInBytes(), NumBytes, Ty); |
| 275 | } |
| 276 | |
| 277 | uint64_t CGRecordLayoutBuilder::getNextFieldOffsetInBytes() const { |
| 278 | if (FieldInfos.empty()) |
| 279 | return 0; |
| 280 | |
| 281 | const FieldInfo &LastInfo = FieldInfos.back(); |
| 282 | return LastInfo.OffsetInBytes + LastInfo.SizeInBytes; |
| 283 | } |
| 284 | |
| 285 | unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const { |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 286 | if (Packed) |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 287 | return 1; |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 288 | |
| 289 | return Types.getTargetData().getABITypeAlignment(Ty); |
| 290 | } |
| 291 | |
| 292 | uint64_t CGRecordLayoutBuilder::getTypeSizeInBytes(const llvm::Type *Ty) const { |
| 293 | return Types.getTargetData().getTypeAllocSize(Ty); |
| 294 | } |
| 295 | |
| 296 | CGRecordLayout * |
| 297 | CGRecordLayoutBuilder::ComputeLayout(CodeGenTypes &Types, |
| 298 | const RecordDecl *D) { |
| 299 | CGRecordLayoutBuilder Builder(Types); |
| 300 | |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 301 | Builder.Layout(D); |
| 302 | |
| 303 | // FIXME: Once this works well enough, enable it. |
| 304 | return 0; |
| 305 | |
| 306 | // FIXME: Use a VMContext. |
| 307 | const llvm::Type *Ty = llvm::StructType::get(Builder.FieldTypes, |
Anders Carlsson | d78fc89 | 2009-07-23 17:24:40 +0000 | [diff] [blame^] | 308 | Builder.Packed); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 309 | |
| 310 | // Add all the field numbers. |
| 311 | for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) { |
| 312 | const FieldDecl *FD = Builder.LLVMFields[i].first; |
| 313 | unsigned FieldNo = Builder.LLVMFields[i].second; |
| 314 | |
| 315 | Types.addFieldInfo(FD, FieldNo); |
| 316 | } |
| 317 | |
| 318 | // Add bitfield info. |
| 319 | for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) { |
| 320 | const LLVMBitFieldInfo &Info = Builder.LLVMBitFields[i]; |
| 321 | |
Anders Carlsson | 8af896c | 2009-07-23 17:01:21 +0000 | [diff] [blame] | 322 | Types.addBitFieldInfo(Info.FD, Info.FieldNo, Info.Start, Info.Size); |
Anders Carlsson | 307846f | 2009-07-23 03:17:50 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return new CGRecordLayout(Ty, llvm::SmallSet<unsigned, 8>()); |
| 326 | } |