CGRecordLayoutBuilder.cpp revision 89da874f8ecfebabdac2c6e9b7930ebe179ccf81
1//===--- CGRecordLayoutBuilder.cpp - CGRecordLayout builder ----*- 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// Builder implementation for CGRecordLayout objects. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CGRecordLayout.h" 15#include "clang/AST/ASTContext.h" 16#include "clang/AST/Attr.h" 17#include "clang/AST/DeclCXX.h" 18#include "clang/AST/Expr.h" 19#include "clang/AST/RecordLayout.h" 20#include "CodeGenTypes.h" 21#include "llvm/DerivedTypes.h" 22#include "llvm/Type.h" 23#include "llvm/Support/Debug.h" 24#include "llvm/Support/raw_ostream.h" 25#include "llvm/Target/TargetData.h" 26using namespace clang; 27using namespace CodeGen; 28 29namespace clang { 30namespace CodeGen { 31 32class CGRecordLayoutBuilder { 33public: 34 /// FieldTypes - Holds the LLVM types that the struct is created from. 35 std::vector<const llvm::Type *> FieldTypes; 36 37 /// LLVMFieldInfo - Holds a field and its corresponding LLVM field number. 38 typedef std::pair<const FieldDecl *, unsigned> LLVMFieldInfo; 39 llvm::SmallVector<LLVMFieldInfo, 16> LLVMFields; 40 41 /// LLVMBitFieldInfo - Holds location and size information about a bit field. 42 typedef std::pair<const FieldDecl *, CGBitFieldInfo> LLVMBitFieldInfo; 43 llvm::SmallVector<LLVMBitFieldInfo, 16> LLVMBitFields; 44 45 /// ContainsPointerToDataMember - Whether one of the fields in this record 46 /// layout is a pointer to data member, or a struct that contains pointer to 47 /// data member. 48 bool ContainsPointerToDataMember; 49 50 /// Packed - Whether the resulting LLVM struct will be packed or not. 51 bool Packed; 52 53private: 54 CodeGenTypes &Types; 55 56 /// Alignment - Contains the alignment of the RecordDecl. 57 // 58 // FIXME: This is not needed and should be removed. 59 unsigned Alignment; 60 61 /// AlignmentAsLLVMStruct - Will contain the maximum alignment of all the 62 /// LLVM types. 63 unsigned AlignmentAsLLVMStruct; 64 65 /// BitsAvailableInLastField - If a bit field spans only part of a LLVM field, 66 /// this will have the number of bits still available in the field. 67 char BitsAvailableInLastField; 68 69 /// NextFieldOffsetInBytes - Holds the next field offset in bytes. 70 uint64_t NextFieldOffsetInBytes; 71 72 /// LayoutUnionField - Will layout a field in an union and return the type 73 /// that the field will have. 74 const llvm::Type *LayoutUnionField(const FieldDecl *Field, 75 const ASTRecordLayout &Layout); 76 77 /// LayoutUnion - Will layout a union RecordDecl. 78 void LayoutUnion(const RecordDecl *D); 79 80 /// LayoutField - try to layout all fields in the record decl. 81 /// Returns false if the operation failed because the struct is not packed. 82 bool LayoutFields(const RecordDecl *D); 83 84 /// LayoutBases - layout the bases and vtable pointer of a record decl. 85 void LayoutBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout); 86 87 /// LayoutField - layout a single field. Returns false if the operation failed 88 /// because the current struct is not packed. 89 bool LayoutField(const FieldDecl *D, uint64_t FieldOffset); 90 91 /// LayoutBitField - layout a single bit field. 92 void LayoutBitField(const FieldDecl *D, uint64_t FieldOffset); 93 94 /// AppendField - Appends a field with the given offset and type. 95 void AppendField(uint64_t FieldOffsetInBytes, const llvm::Type *FieldTy); 96 97 /// AppendPadding - Appends enough padding bytes so that the total 98 /// struct size is a multiple of the field alignment. 99 void AppendPadding(uint64_t FieldOffsetInBytes, unsigned FieldAlignment); 100 101 /// AppendBytes - Append a given number of bytes to the record. 102 void AppendBytes(uint64_t NumBytes); 103 104 /// AppendTailPadding - Append enough tail padding so that the type will have 105 /// the passed size. 106 void AppendTailPadding(uint64_t RecordSize); 107 108 unsigned getTypeAlignment(const llvm::Type *Ty) const; 109 110 /// CheckForPointerToDataMember - Check if the given type contains a pointer 111 /// to data member. 112 void CheckForPointerToDataMember(QualType T); 113 114public: 115 CGRecordLayoutBuilder(CodeGenTypes &Types) 116 : ContainsPointerToDataMember(false), Packed(false), Types(Types), 117 Alignment(0), AlignmentAsLLVMStruct(1), 118 BitsAvailableInLastField(0), NextFieldOffsetInBytes(0) { } 119 120 /// Layout - Will layout a RecordDecl. 121 void Layout(const RecordDecl *D); 122}; 123 124} 125} 126 127void CGRecordLayoutBuilder::Layout(const RecordDecl *D) { 128 Alignment = Types.getContext().getASTRecordLayout(D).getAlignment() / 8; 129 Packed = D->hasAttr<PackedAttr>(); 130 131 if (D->isUnion()) { 132 LayoutUnion(D); 133 return; 134 } 135 136 if (LayoutFields(D)) 137 return; 138 139 // We weren't able to layout the struct. Try again with a packed struct 140 Packed = true; 141 AlignmentAsLLVMStruct = 1; 142 NextFieldOffsetInBytes = 0; 143 FieldTypes.clear(); 144 LLVMFields.clear(); 145 LLVMBitFields.clear(); 146 147 LayoutFields(D); 148} 149 150static CGBitFieldInfo ComputeBitFieldInfo(CodeGenTypes &Types, 151 const FieldDecl *FD, 152 uint64_t FieldOffset, 153 uint64_t FieldSize) { 154 const RecordDecl *RD = FD->getParent(); 155 const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD); 156 uint64_t ContainingTypeSizeInBits = RL.getSize(); 157 unsigned ContainingTypeAlign = RL.getAlignment(); 158 159 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(FD->getType()); 160 uint64_t TypeSizeInBytes = Types.getTargetData().getTypeAllocSize(Ty); 161 uint64_t TypeSizeInBits = TypeSizeInBytes * 8; 162 163 bool IsSigned = FD->getType()->isSignedIntegerType(); 164 165 if (FieldSize > TypeSizeInBits) { 166 // We have a wide bit-field. The extra bits are only used for padding, so 167 // if we have a bitfield of type T, with size N: 168 // 169 // T t : N; 170 // 171 // We can just assume that it's: 172 // 173 // T t : sizeof(T); 174 // 175 FieldSize = TypeSizeInBits; 176 } 177 178 // Compute the access components. The policy we use is to start by attempting 179 // to access using the width of the bit-field type itself and to always access 180 // at aligned indices of that type. If such an access would fail because it 181 // extends past the bound of the type, then we reduce size to the next smaller 182 // power of two and retry. The current algorithm assumes pow2 sized types, 183 // although this is easy to fix. 184 // 185 // FIXME: This algorithm is wrong on big-endian systems, I think. 186 assert(llvm::isPowerOf2_32(TypeSizeInBits) && "Unexpected type size!"); 187 CGBitFieldInfo::AccessInfo Components[3]; 188 unsigned NumComponents = 0; 189 unsigned AccessedTargetBits = 0; // The tumber of target bits accessed. 190 unsigned AccessWidth = TypeSizeInBits; // The current access width to attempt. 191 192 // Round down from the field offset to find the first access position that is 193 // at an aligned offset of the initial access type. 194 uint64_t AccessStart = FieldOffset - (FieldOffset % TypeSizeInBits); 195 196 while (AccessedTargetBits < FieldSize) { 197 // Check that we can access using a type of this size, without reading off 198 // the end of the structure. This can occur with packed structures and 199 // -fno-bitfield-type-align, for example. 200 if (AccessStart + AccessWidth > ContainingTypeSizeInBits) { 201 // If so, reduce access size to the next smaller power-of-two and retry. 202 AccessWidth >>= 1; 203 assert(AccessWidth >= 8 && "Cannot access under byte size!"); 204 continue; 205 } 206 207 // Otherwise, add an access component. 208 209 // First, compute the bits inside this access which are part of the 210 // target. We are reading bits [AccessStart, AccessStart + AccessWidth); the 211 // intersection with [FieldOffset, FieldOffset + FieldSize) gives the bits 212 // in the target that we are reading. 213 uint64_t AccessBitsInFieldStart = std::max(AccessStart, FieldOffset); 214 uint64_t AccessBitsInFieldSize = 215 std::min(AccessWidth - (AccessBitsInFieldStart - AccessStart), 216 FieldSize - (AccessBitsInFieldStart-FieldOffset)); 217 218 assert(NumComponents < 3 && "Unexpected number of components!"); 219 CGBitFieldInfo::AccessInfo &AI = Components[NumComponents++]; 220 AI.FieldIndex = 0; 221 // FIXME: We still follow the old access pattern of only using the field 222 // byte offset. We should switch this once we fix the struct layout to be 223 // pretty. 224 AI.FieldByteOffset = AccessStart / 8; 225 AI.FieldBitStart = AccessBitsInFieldStart - AccessStart; 226 AI.AccessWidth = AccessWidth; 227 AI.AccessAlignment = llvm::MinAlign(ContainingTypeAlign, AccessStart) / 8; 228 AI.TargetBitOffset = AccessedTargetBits; 229 AI.TargetBitWidth = AccessBitsInFieldSize; 230 231 AccessStart += AccessWidth; 232 AccessedTargetBits += AI.TargetBitWidth; 233 } 234 235 assert(AccessedTargetBits == FieldSize && "Invalid bit-field access!"); 236 return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned); 237} 238 239void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D, 240 uint64_t FieldOffset) { 241 uint64_t FieldSize = 242 D->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); 243 244 if (FieldSize == 0) 245 return; 246 247 uint64_t NextFieldOffset = NextFieldOffsetInBytes * 8; 248 unsigned NumBytesToAppend; 249 250 if (FieldOffset < NextFieldOffset) { 251 assert(BitsAvailableInLastField && "Bitfield size mismatch!"); 252 assert(NextFieldOffsetInBytes && "Must have laid out at least one byte!"); 253 254 // The bitfield begins in the previous bit-field. 255 NumBytesToAppend = 256 llvm::RoundUpToAlignment(FieldSize - BitsAvailableInLastField, 8) / 8; 257 } else { 258 assert(FieldOffset % 8 == 0 && "Field offset not aligned correctly"); 259 260 // Append padding if necessary. 261 AppendBytes((FieldOffset - NextFieldOffset) / 8); 262 263 NumBytesToAppend = 264 llvm::RoundUpToAlignment(FieldSize, 8) / 8; 265 266 assert(NumBytesToAppend && "No bytes to append!"); 267 } 268 269 // Add the bit field info. 270 LLVMBitFields.push_back( 271 LLVMBitFieldInfo(D, ComputeBitFieldInfo(Types, D, FieldOffset, FieldSize))); 272 273 AppendBytes(NumBytesToAppend); 274 275 BitsAvailableInLastField = 276 NextFieldOffsetInBytes * 8 - (FieldOffset + FieldSize); 277} 278 279bool CGRecordLayoutBuilder::LayoutField(const FieldDecl *D, 280 uint64_t FieldOffset) { 281 // If the field is packed, then we need a packed struct. 282 if (!Packed && D->hasAttr<PackedAttr>()) 283 return false; 284 285 if (D->isBitField()) { 286 // We must use packed structs for unnamed bit fields since they 287 // don't affect the struct alignment. 288 if (!Packed && !D->getDeclName()) 289 return false; 290 291 LayoutBitField(D, FieldOffset); 292 return true; 293 } 294 295 // Check if we have a pointer to data member in this field. 296 CheckForPointerToDataMember(D->getType()); 297 298 assert(FieldOffset % 8 == 0 && "FieldOffset is not on a byte boundary!"); 299 uint64_t FieldOffsetInBytes = FieldOffset / 8; 300 301 const llvm::Type *Ty = Types.ConvertTypeForMemRecursive(D->getType()); 302 unsigned TypeAlignment = getTypeAlignment(Ty); 303 304 // If the type alignment is larger then the struct alignment, we must use 305 // a packed struct. 306 if (TypeAlignment > Alignment) { 307 assert(!Packed && "Alignment is wrong even with packed struct!"); 308 return false; 309 } 310 311 if (const RecordType *RT = D->getType()->getAs<RecordType>()) { 312 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl()); 313 if (const PragmaPackAttr *PPA = RD->getAttr<PragmaPackAttr>()) { 314 if (PPA->getAlignment() != TypeAlignment * 8 && !Packed) 315 return false; 316 } 317 } 318 319 // Round up the field offset to the alignment of the field type. 320 uint64_t AlignedNextFieldOffsetInBytes = 321 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, TypeAlignment); 322 323 if (FieldOffsetInBytes < AlignedNextFieldOffsetInBytes) { 324 assert(!Packed && "Could not place field even with packed struct!"); 325 return false; 326 } 327 328 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) { 329 // Even with alignment, the field offset is not at the right place, 330 // insert padding. 331 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes; 332 333 AppendBytes(PaddingInBytes); 334 } 335 336 // Now append the field. 337 LLVMFields.push_back(LLVMFieldInfo(D, FieldTypes.size())); 338 AppendField(FieldOffsetInBytes, Ty); 339 340 return true; 341} 342 343const llvm::Type * 344CGRecordLayoutBuilder::LayoutUnionField(const FieldDecl *Field, 345 const ASTRecordLayout &Layout) { 346 if (Field->isBitField()) { 347 uint64_t FieldSize = 348 Field->getBitWidth()->EvaluateAsInt(Types.getContext()).getZExtValue(); 349 350 // Ignore zero sized bit fields. 351 if (FieldSize == 0) 352 return 0; 353 354 const llvm::Type *FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext()); 355 unsigned NumBytesToAppend = 356 llvm::RoundUpToAlignment(FieldSize, 8) / 8; 357 358 if (NumBytesToAppend > 1) 359 FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend); 360 361 // Add the bit field info. 362 LLVMBitFields.push_back( 363 LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize))); 364 return FieldTy; 365 } 366 367 // This is a regular union field. 368 LLVMFields.push_back(LLVMFieldInfo(Field, 0)); 369 return Types.ConvertTypeForMemRecursive(Field->getType()); 370} 371 372void CGRecordLayoutBuilder::LayoutUnion(const RecordDecl *D) { 373 assert(D->isUnion() && "Can't call LayoutUnion on a non-union record!"); 374 375 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); 376 377 const llvm::Type *Ty = 0; 378 uint64_t Size = 0; 379 unsigned Align = 0; 380 381 bool HasOnlyZeroSizedBitFields = true; 382 383 unsigned FieldNo = 0; 384 for (RecordDecl::field_iterator Field = D->field_begin(), 385 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { 386 assert(Layout.getFieldOffset(FieldNo) == 0 && 387 "Union field offset did not start at the beginning of record!"); 388 const llvm::Type *FieldTy = LayoutUnionField(*Field, Layout); 389 390 if (!FieldTy) 391 continue; 392 393 HasOnlyZeroSizedBitFields = false; 394 395 unsigned FieldAlign = Types.getTargetData().getABITypeAlignment(FieldTy); 396 uint64_t FieldSize = Types.getTargetData().getTypeAllocSize(FieldTy); 397 398 if (FieldAlign < Align) 399 continue; 400 401 if (FieldAlign > Align || FieldSize > Size) { 402 Ty = FieldTy; 403 Align = FieldAlign; 404 Size = FieldSize; 405 } 406 } 407 408 // Now add our field. 409 if (Ty) { 410 AppendField(0, Ty); 411 412 if (getTypeAlignment(Ty) > Layout.getAlignment() / 8) { 413 // We need a packed struct. 414 Packed = true; 415 Align = 1; 416 } 417 } 418 if (!Align) { 419 assert(HasOnlyZeroSizedBitFields && 420 "0-align record did not have all zero-sized bit-fields!"); 421 Align = 1; 422 } 423 424 // Append tail padding. 425 if (Layout.getSize() / 8 > Size) 426 AppendPadding(Layout.getSize() / 8, Align); 427} 428 429void CGRecordLayoutBuilder::LayoutBases(const CXXRecordDecl *RD, 430 const ASTRecordLayout &Layout) { 431 // Check if we need to add a vtable pointer. 432 if (RD->isDynamicClass() && !Layout.getPrimaryBase()) { 433 const llvm::Type *Int8PtrTy = 434 llvm::Type::getInt8PtrTy(Types.getLLVMContext()); 435 436 assert(NextFieldOffsetInBytes == 0 && 437 "VTable pointer must come first!"); 438 AppendField(NextFieldOffsetInBytes, Int8PtrTy->getPointerTo()); 439 } 440} 441 442bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { 443 assert(!D->isUnion() && "Can't call LayoutFields on a union!"); 444 assert(Alignment && "Did not set alignment!"); 445 446 const ASTRecordLayout &Layout = Types.getContext().getASTRecordLayout(D); 447 448 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) 449 LayoutBases(RD, Layout); 450 451 unsigned FieldNo = 0; 452 453 for (RecordDecl::field_iterator Field = D->field_begin(), 454 FieldEnd = D->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { 455 if (!LayoutField(*Field, Layout.getFieldOffset(FieldNo))) { 456 assert(!Packed && 457 "Could not layout fields even with a packed LLVM struct!"); 458 return false; 459 } 460 } 461 462 // Append tail padding if necessary. 463 AppendTailPadding(Layout.getSize()); 464 465 return true; 466} 467 468void CGRecordLayoutBuilder::AppendTailPadding(uint64_t RecordSize) { 469 assert(RecordSize % 8 == 0 && "Invalid record size!"); 470 471 uint64_t RecordSizeInBytes = RecordSize / 8; 472 assert(NextFieldOffsetInBytes <= RecordSizeInBytes && "Size mismatch!"); 473 474 uint64_t AlignedNextFieldOffset = 475 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, AlignmentAsLLVMStruct); 476 477 if (AlignedNextFieldOffset == RecordSizeInBytes) { 478 // We don't need any padding. 479 return; 480 } 481 482 unsigned NumPadBytes = RecordSizeInBytes - NextFieldOffsetInBytes; 483 AppendBytes(NumPadBytes); 484} 485 486void CGRecordLayoutBuilder::AppendField(uint64_t FieldOffsetInBytes, 487 const llvm::Type *FieldTy) { 488 AlignmentAsLLVMStruct = std::max(AlignmentAsLLVMStruct, 489 getTypeAlignment(FieldTy)); 490 491 uint64_t FieldSizeInBytes = Types.getTargetData().getTypeAllocSize(FieldTy); 492 493 FieldTypes.push_back(FieldTy); 494 495 NextFieldOffsetInBytes = FieldOffsetInBytes + FieldSizeInBytes; 496 BitsAvailableInLastField = 0; 497} 498 499void CGRecordLayoutBuilder::AppendPadding(uint64_t FieldOffsetInBytes, 500 unsigned FieldAlignment) { 501 assert(NextFieldOffsetInBytes <= FieldOffsetInBytes && 502 "Incorrect field layout!"); 503 504 // Round up the field offset to the alignment of the field type. 505 uint64_t AlignedNextFieldOffsetInBytes = 506 llvm::RoundUpToAlignment(NextFieldOffsetInBytes, FieldAlignment); 507 508 if (AlignedNextFieldOffsetInBytes < FieldOffsetInBytes) { 509 // Even with alignment, the field offset is not at the right place, 510 // insert padding. 511 uint64_t PaddingInBytes = FieldOffsetInBytes - NextFieldOffsetInBytes; 512 513 AppendBytes(PaddingInBytes); 514 } 515} 516 517void CGRecordLayoutBuilder::AppendBytes(uint64_t NumBytes) { 518 if (NumBytes == 0) 519 return; 520 521 const llvm::Type *Ty = llvm::Type::getInt8Ty(Types.getLLVMContext()); 522 if (NumBytes > 1) 523 Ty = llvm::ArrayType::get(Ty, NumBytes); 524 525 // Append the padding field 526 AppendField(NextFieldOffsetInBytes, Ty); 527} 528 529unsigned CGRecordLayoutBuilder::getTypeAlignment(const llvm::Type *Ty) const { 530 if (Packed) 531 return 1; 532 533 return Types.getTargetData().getABITypeAlignment(Ty); 534} 535 536void CGRecordLayoutBuilder::CheckForPointerToDataMember(QualType T) { 537 // This record already contains a member pointer. 538 if (ContainsPointerToDataMember) 539 return; 540 541 // Can only have member pointers if we're compiling C++. 542 if (!Types.getContext().getLangOptions().CPlusPlus) 543 return; 544 545 T = Types.getContext().getBaseElementType(T); 546 547 if (const MemberPointerType *MPT = T->getAs<MemberPointerType>()) { 548 if (!MPT->getPointeeType()->isFunctionType()) { 549 // We have a pointer to data member. 550 ContainsPointerToDataMember = true; 551 } 552 } else if (const RecordType *RT = T->getAs<RecordType>()) { 553 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 554 555 // FIXME: It would be better if there was a way to explicitly compute the 556 // record layout instead of converting to a type. 557 Types.ConvertTagDeclType(RD); 558 559 const CGRecordLayout &Layout = Types.getCGRecordLayout(RD); 560 561 if (Layout.containsPointerToDataMember()) 562 ContainsPointerToDataMember = true; 563 } 564} 565 566CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) { 567 CGRecordLayoutBuilder Builder(*this); 568 569 Builder.Layout(D); 570 571 const llvm::Type *Ty = llvm::StructType::get(getLLVMContext(), 572 Builder.FieldTypes, 573 Builder.Packed); 574 575 CGRecordLayout *RL = 576 new CGRecordLayout(Ty, Builder.ContainsPointerToDataMember); 577 578 // Add all the field numbers. 579 for (unsigned i = 0, e = Builder.LLVMFields.size(); i != e; ++i) 580 RL->FieldInfo.insert(Builder.LLVMFields[i]); 581 582 // Add bitfield info. 583 for (unsigned i = 0, e = Builder.LLVMBitFields.size(); i != e; ++i) 584 RL->BitFields.insert(Builder.LLVMBitFields[i]); 585 586 // Dump the layout, if requested. 587 if (getContext().getLangOptions().DumpRecordLayouts) { 588 llvm::errs() << "\n*** Dumping IRgen Record Layout\n"; 589 llvm::errs() << "Record: "; 590 D->dump(); 591 llvm::errs() << "\nLayout: "; 592 RL->dump(); 593 } 594 595#ifndef NDEBUG 596 // Verify that the computed LLVM struct size matches the AST layout size. 597 uint64_t TypeSizeInBits = getContext().getASTRecordLayout(D).getSize(); 598 assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) && 599 "Type size mismatch!"); 600 601 // Verify that the LLVM and AST field offsets agree. 602 const llvm::StructType *ST = 603 dyn_cast<llvm::StructType>(RL->getLLVMType()); 604 const llvm::StructLayout *SL = getTargetData().getStructLayout(ST); 605 606 const ASTRecordLayout &AST_RL = getContext().getASTRecordLayout(D); 607 RecordDecl::field_iterator it = D->field_begin(); 608 for (unsigned i = 0, e = AST_RL.getFieldCount(); i != e; ++i, ++it) { 609 const FieldDecl *FD = *it; 610 611 // For non-bit-fields, just check that the LLVM struct offset matches the 612 // AST offset. 613 if (!FD->isBitField()) { 614 unsigned FieldNo = RL->getLLVMFieldNo(FD); 615 assert(AST_RL.getFieldOffset(i) == SL->getElementOffsetInBits(FieldNo) && 616 "Invalid field offset!"); 617 continue; 618 } 619 620 // Ignore unnamed bit-fields. 621 if (!FD->getDeclName()) 622 continue; 623 624 const CGBitFieldInfo &Info = RL->getBitFieldInfo(FD); 625 for (unsigned i = 0, e = Info.getNumComponents(); i != e; ++i) { 626 const CGBitFieldInfo::AccessInfo &AI = Info.getComponent(i); 627 628 // Verify that every component access is within the structure. 629 uint64_t FieldOffset = SL->getElementOffsetInBits(AI.FieldIndex); 630 uint64_t AccessBitOffset = FieldOffset + AI.FieldByteOffset * 8; 631 assert(AccessBitOffset + AI.AccessWidth <= TypeSizeInBits && 632 "Invalid bit-field access (out of range)!"); 633 } 634 } 635#endif 636 637 return RL; 638} 639 640void CGRecordLayout::print(llvm::raw_ostream &OS) const { 641 OS << "<CGRecordLayout\n"; 642 OS << " LLVMType:" << *LLVMType << "\n"; 643 OS << " ContainsPointerToDataMember:" << ContainsPointerToDataMember << "\n"; 644 OS << " BitFields:[\n"; 645 646 // Print bit-field infos in declaration order. 647 std::vector<std::pair<unsigned, const CGBitFieldInfo*> > BFIs; 648 for (llvm::DenseMap<const FieldDecl*, CGBitFieldInfo>::const_iterator 649 it = BitFields.begin(), ie = BitFields.end(); 650 it != ie; ++it) { 651 const RecordDecl *RD = it->first->getParent(); 652 unsigned Index = 0; 653 for (RecordDecl::field_iterator 654 it2 = RD->field_begin(); *it2 != it->first; ++it2) 655 ++Index; 656 BFIs.push_back(std::make_pair(Index, &it->second)); 657 } 658 llvm::array_pod_sort(BFIs.begin(), BFIs.end()); 659 for (unsigned i = 0, e = BFIs.size(); i != e; ++i) { 660 OS.indent(4); 661 BFIs[i].second->print(OS); 662 OS << "\n"; 663 } 664 665 OS << "]>\n"; 666} 667 668void CGRecordLayout::dump() const { 669 print(llvm::errs()); 670} 671 672void CGBitFieldInfo::print(llvm::raw_ostream &OS) const { 673 OS << "<CGBitFieldInfo"; 674 OS << " Size:" << Size; 675 OS << " IsSigned:" << IsSigned << "\n"; 676 677 OS.indent(4 + strlen("<CGBitFieldInfo")); 678 OS << " NumComponents:" << getNumComponents(); 679 OS << " Components: ["; 680 if (getNumComponents()) { 681 OS << "\n"; 682 for (unsigned i = 0, e = getNumComponents(); i != e; ++i) { 683 const AccessInfo &AI = getComponent(i); 684 OS.indent(8); 685 OS << "<AccessInfo" 686 << " FieldIndex:" << AI.FieldIndex 687 << " FieldByteOffset:" << AI.FieldByteOffset 688 << " FieldBitStart:" << AI.FieldBitStart 689 << " AccessWidth:" << AI.AccessWidth << "\n"; 690 OS.indent(8 + strlen("<AccessInfo")); 691 OS << " AccessAlignment:" << AI.AccessAlignment 692 << " TargetBitOffset:" << AI.TargetBitOffset 693 << " TargetBitWidth:" << AI.TargetBitWidth 694 << ">\n"; 695 } 696 OS.indent(4); 697 } 698 OS << "]>"; 699} 700 701void CGBitFieldInfo::dump() const { 702 print(llvm::errs()); 703} 704