1 //===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===// 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 contains code to emit Constant Expr nodes as LLVM code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenFunction.h" 15 #include "CodeGenModule.h" 16 #include "CGCXXABI.h" 17 #include "CGObjCRuntime.h" 18 #include "CGRecordLayout.h" 19 #include "clang/AST/APValue.h" 20 #include "clang/AST/ASTContext.h" 21 #include "clang/AST/RecordLayout.h" 22 #include "clang/AST/StmtVisitor.h" 23 #include "clang/Basic/Builtins.h" 24 #include "llvm/Constants.h" 25 #include "llvm/Function.h" 26 #include "llvm/GlobalVariable.h" 27 #include "llvm/Target/TargetData.h" 28 using namespace clang; 29 using namespace CodeGen; 30 31 //===----------------------------------------------------------------------===// 32 // ConstStructBuilder 33 //===----------------------------------------------------------------------===// 34 35 namespace { 36 class ConstStructBuilder { 37 CodeGenModule &CGM; 38 CodeGenFunction *CGF; 39 40 bool Packed; 41 CharUnits NextFieldOffsetInChars; 42 CharUnits LLVMStructAlignment; 43 SmallVector<llvm::Constant *, 32> Elements; 44 public: 45 static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, 46 InitListExpr *ILE); 47 static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF, 48 const APValue &Value, QualType ValTy); 49 50 private: 51 ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF) 52 : CGM(CGM), CGF(CGF), Packed(false), 53 NextFieldOffsetInChars(CharUnits::Zero()), 54 LLVMStructAlignment(CharUnits::One()) { } 55 56 void AppendVTablePointer(BaseSubobject Base, llvm::Constant *VTable, 57 const CXXRecordDecl *VTableClass); 58 59 void AppendField(const FieldDecl *Field, uint64_t FieldOffset, 60 llvm::Constant *InitExpr); 61 62 void AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst); 63 64 void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset, 65 llvm::ConstantInt *InitExpr); 66 67 void AppendPadding(CharUnits PadSize); 68 69 void AppendTailPadding(CharUnits RecordSize); 70 71 void ConvertStructToPacked(); 72 73 bool Build(InitListExpr *ILE); 74 void Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase, 75 llvm::Constant *VTable, const CXXRecordDecl *VTableClass, 76 CharUnits BaseOffset); 77 llvm::Constant *Finalize(QualType Ty); 78 79 CharUnits getAlignment(const llvm::Constant *C) const { 80 if (Packed) return CharUnits::One(); 81 return CharUnits::fromQuantity( 82 CGM.getTargetData().getABITypeAlignment(C->getType())); 83 } 84 85 CharUnits getSizeInChars(const llvm::Constant *C) const { 86 return CharUnits::fromQuantity( 87 CGM.getTargetData().getTypeAllocSize(C->getType())); 88 } 89 }; 90 91 void ConstStructBuilder::AppendVTablePointer(BaseSubobject Base, 92 llvm::Constant *VTable, 93 const CXXRecordDecl *VTableClass) { 94 // Find the appropriate vtable within the vtable group. 95 uint64_t AddressPoint = 96 CGM.getVTableContext().getVTableLayout(VTableClass).getAddressPoint(Base); 97 llvm::Value *Indices[] = { 98 llvm::ConstantInt::get(CGM.Int64Ty, 0), 99 llvm::ConstantInt::get(CGM.Int64Ty, AddressPoint) 100 }; 101 llvm::Constant *VTableAddressPoint = 102 llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Indices); 103 104 // Add the vtable at the start of the object. 105 AppendBytes(CharUnits::Zero(), VTableAddressPoint); 106 } 107 108 void ConstStructBuilder:: 109 AppendField(const FieldDecl *Field, uint64_t FieldOffset, 110 llvm::Constant *InitCst) { 111 const ASTContext &Context = CGM.getContext(); 112 113 CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset); 114 115 AppendBytes(FieldOffsetInChars, InitCst); 116 } 117 118 void ConstStructBuilder:: 119 AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst) { 120 121 assert(NextFieldOffsetInChars <= FieldOffsetInChars 122 && "Field offset mismatch!"); 123 124 CharUnits FieldAlignment = getAlignment(InitCst); 125 126 // Round up the field offset to the alignment of the field type. 127 CharUnits AlignedNextFieldOffsetInChars = 128 NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment); 129 130 if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) { 131 assert(!Packed && "Alignment is wrong even with a packed struct!"); 132 133 // Convert the struct to a packed struct. 134 ConvertStructToPacked(); 135 136 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars; 137 } 138 139 if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) { 140 // We need to append padding. 141 AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars); 142 143 assert(NextFieldOffsetInChars == FieldOffsetInChars && 144 "Did not add enough padding!"); 145 146 AlignedNextFieldOffsetInChars = NextFieldOffsetInChars; 147 } 148 149 // Add the field. 150 Elements.push_back(InitCst); 151 NextFieldOffsetInChars = AlignedNextFieldOffsetInChars + 152 getSizeInChars(InitCst); 153 154 if (Packed) 155 assert(LLVMStructAlignment == CharUnits::One() && 156 "Packed struct not byte-aligned!"); 157 else 158 LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment); 159 } 160 161 void ConstStructBuilder::AppendBitField(const FieldDecl *Field, 162 uint64_t FieldOffset, 163 llvm::ConstantInt *CI) { 164 const ASTContext &Context = CGM.getContext(); 165 const uint64_t CharWidth = Context.getCharWidth(); 166 uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars); 167 if (FieldOffset > NextFieldOffsetInBits) { 168 // We need to add padding. 169 CharUnits PadSize = Context.toCharUnitsFromBits( 170 llvm::RoundUpToAlignment(FieldOffset - NextFieldOffsetInBits, 171 Context.getTargetInfo().getCharAlign())); 172 173 AppendPadding(PadSize); 174 } 175 176 uint64_t FieldSize = Field->getBitWidthValue(Context); 177 178 llvm::APInt FieldValue = CI->getValue(); 179 180 // Promote the size of FieldValue if necessary 181 // FIXME: This should never occur, but currently it can because initializer 182 // constants are cast to bool, and because clang is not enforcing bitfield 183 // width limits. 184 if (FieldSize > FieldValue.getBitWidth()) 185 FieldValue = FieldValue.zext(FieldSize); 186 187 // Truncate the size of FieldValue to the bit field size. 188 if (FieldSize < FieldValue.getBitWidth()) 189 FieldValue = FieldValue.trunc(FieldSize); 190 191 NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars); 192 if (FieldOffset < NextFieldOffsetInBits) { 193 // Either part of the field or the entire field can go into the previous 194 // byte. 195 assert(!Elements.empty() && "Elements can't be empty!"); 196 197 unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset; 198 199 bool FitsCompletelyInPreviousByte = 200 BitsInPreviousByte >= FieldValue.getBitWidth(); 201 202 llvm::APInt Tmp = FieldValue; 203 204 if (!FitsCompletelyInPreviousByte) { 205 unsigned NewFieldWidth = FieldSize - BitsInPreviousByte; 206 207 if (CGM.getTargetData().isBigEndian()) { 208 Tmp = Tmp.lshr(NewFieldWidth); 209 Tmp = Tmp.trunc(BitsInPreviousByte); 210 211 // We want the remaining high bits. 212 FieldValue = FieldValue.trunc(NewFieldWidth); 213 } else { 214 Tmp = Tmp.trunc(BitsInPreviousByte); 215 216 // We want the remaining low bits. 217 FieldValue = FieldValue.lshr(BitsInPreviousByte); 218 FieldValue = FieldValue.trunc(NewFieldWidth); 219 } 220 } 221 222 Tmp = Tmp.zext(CharWidth); 223 if (CGM.getTargetData().isBigEndian()) { 224 if (FitsCompletelyInPreviousByte) 225 Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth()); 226 } else { 227 Tmp = Tmp.shl(CharWidth - BitsInPreviousByte); 228 } 229 230 // 'or' in the bits that go into the previous byte. 231 llvm::Value *LastElt = Elements.back(); 232 if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt)) 233 Tmp |= Val->getValue(); 234 else { 235 assert(isa<llvm::UndefValue>(LastElt)); 236 // If there is an undef field that we're adding to, it can either be a 237 // scalar undef (in which case, we just replace it with our field) or it 238 // is an array. If it is an array, we have to pull one byte off the 239 // array so that the other undef bytes stay around. 240 if (!isa<llvm::IntegerType>(LastElt->getType())) { 241 // The undef padding will be a multibyte array, create a new smaller 242 // padding and then an hole for our i8 to get plopped into. 243 assert(isa<llvm::ArrayType>(LastElt->getType()) && 244 "Expected array padding of undefs"); 245 llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType()); 246 assert(AT->getElementType()->isIntegerTy(CharWidth) && 247 AT->getNumElements() != 0 && 248 "Expected non-empty array padding of undefs"); 249 250 // Remove the padding array. 251 NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements()); 252 Elements.pop_back(); 253 254 // Add the padding back in two chunks. 255 AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1)); 256 AppendPadding(CharUnits::One()); 257 assert(isa<llvm::UndefValue>(Elements.back()) && 258 Elements.back()->getType()->isIntegerTy(CharWidth) && 259 "Padding addition didn't work right"); 260 } 261 } 262 263 Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp); 264 265 if (FitsCompletelyInPreviousByte) 266 return; 267 } 268 269 while (FieldValue.getBitWidth() > CharWidth) { 270 llvm::APInt Tmp; 271 272 if (CGM.getTargetData().isBigEndian()) { 273 // We want the high bits. 274 Tmp = 275 FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth); 276 } else { 277 // We want the low bits. 278 Tmp = FieldValue.trunc(CharWidth); 279 280 FieldValue = FieldValue.lshr(CharWidth); 281 } 282 283 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp)); 284 ++NextFieldOffsetInChars; 285 286 FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth); 287 } 288 289 assert(FieldValue.getBitWidth() > 0 && 290 "Should have at least one bit left!"); 291 assert(FieldValue.getBitWidth() <= CharWidth && 292 "Should not have more than a byte left!"); 293 294 if (FieldValue.getBitWidth() < CharWidth) { 295 if (CGM.getTargetData().isBigEndian()) { 296 unsigned BitWidth = FieldValue.getBitWidth(); 297 298 FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth); 299 } else 300 FieldValue = FieldValue.zext(CharWidth); 301 } 302 303 // Append the last element. 304 Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), 305 FieldValue)); 306 ++NextFieldOffsetInChars; 307 } 308 309 void ConstStructBuilder::AppendPadding(CharUnits PadSize) { 310 if (PadSize.isZero()) 311 return; 312 313 llvm::Type *Ty = CGM.Int8Ty; 314 if (PadSize > CharUnits::One()) 315 Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity()); 316 317 llvm::Constant *C = llvm::UndefValue::get(Ty); 318 Elements.push_back(C); 319 assert(getAlignment(C) == CharUnits::One() && 320 "Padding must have 1 byte alignment!"); 321 322 NextFieldOffsetInChars += getSizeInChars(C); 323 } 324 325 void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) { 326 assert(NextFieldOffsetInChars <= RecordSize && 327 "Size mismatch!"); 328 329 AppendPadding(RecordSize - NextFieldOffsetInChars); 330 } 331 332 void ConstStructBuilder::ConvertStructToPacked() { 333 SmallVector<llvm::Constant *, 16> PackedElements; 334 CharUnits ElementOffsetInChars = CharUnits::Zero(); 335 336 for (unsigned i = 0, e = Elements.size(); i != e; ++i) { 337 llvm::Constant *C = Elements[i]; 338 339 CharUnits ElementAlign = CharUnits::fromQuantity( 340 CGM.getTargetData().getABITypeAlignment(C->getType())); 341 CharUnits AlignedElementOffsetInChars = 342 ElementOffsetInChars.RoundUpToAlignment(ElementAlign); 343 344 if (AlignedElementOffsetInChars > ElementOffsetInChars) { 345 // We need some padding. 346 CharUnits NumChars = 347 AlignedElementOffsetInChars - ElementOffsetInChars; 348 349 llvm::Type *Ty = CGM.Int8Ty; 350 if (NumChars > CharUnits::One()) 351 Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity()); 352 353 llvm::Constant *Padding = llvm::UndefValue::get(Ty); 354 PackedElements.push_back(Padding); 355 ElementOffsetInChars += getSizeInChars(Padding); 356 } 357 358 PackedElements.push_back(C); 359 ElementOffsetInChars += getSizeInChars(C); 360 } 361 362 assert(ElementOffsetInChars == NextFieldOffsetInChars && 363 "Packing the struct changed its size!"); 364 365 Elements.swap(PackedElements); 366 LLVMStructAlignment = CharUnits::One(); 367 Packed = true; 368 } 369 370 bool ConstStructBuilder::Build(InitListExpr *ILE) { 371 if (ILE->initializesStdInitializerList()) { 372 //CGM.ErrorUnsupported(ILE, "global std::initializer_list"); 373 return false; 374 } 375 376 RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl(); 377 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 378 379 unsigned FieldNo = 0; 380 unsigned ElementNo = 0; 381 const FieldDecl *LastFD = 0; 382 bool IsMsStruct = RD->hasAttr<MsStructAttr>(); 383 384 for (RecordDecl::field_iterator Field = RD->field_begin(), 385 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { 386 if (IsMsStruct) { 387 // Zero-length bitfields following non-bitfield members are 388 // ignored: 389 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) { 390 --FieldNo; 391 continue; 392 } 393 LastFD = (*Field); 394 } 395 396 // If this is a union, skip all the fields that aren't being initialized. 397 if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field) 398 continue; 399 400 // Don't emit anonymous bitfields, they just affect layout. 401 if (Field->isUnnamedBitfield()) { 402 LastFD = (*Field); 403 continue; 404 } 405 406 // Get the initializer. A struct can include fields without initializers, 407 // we just use explicit null values for them. 408 llvm::Constant *EltInit; 409 if (ElementNo < ILE->getNumInits()) 410 EltInit = CGM.EmitConstantExpr(ILE->getInit(ElementNo++), 411 Field->getType(), CGF); 412 else 413 EltInit = CGM.EmitNullConstant(Field->getType()); 414 415 if (!EltInit) 416 return false; 417 418 if (!Field->isBitField()) { 419 // Handle non-bitfield members. 420 AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit); 421 } else { 422 // Otherwise we have a bitfield. 423 AppendBitField(*Field, Layout.getFieldOffset(FieldNo), 424 cast<llvm::ConstantInt>(EltInit)); 425 } 426 } 427 428 return true; 429 } 430 431 namespace { 432 struct BaseInfo { 433 BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index) 434 : Decl(Decl), Offset(Offset), Index(Index) { 435 } 436 437 const CXXRecordDecl *Decl; 438 CharUnits Offset; 439 unsigned Index; 440 441 bool operator<(const BaseInfo &O) const { return Offset < O.Offset; } 442 }; 443 } 444 445 void ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD, 446 bool IsPrimaryBase, llvm::Constant *VTable, 447 const CXXRecordDecl *VTableClass, 448 CharUnits Offset) { 449 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 450 451 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { 452 // Add a vtable pointer, if we need one and it hasn't already been added. 453 if (CD->isDynamicClass() && !IsPrimaryBase) 454 AppendVTablePointer(BaseSubobject(CD, Offset), VTable, VTableClass); 455 456 // Accumulate and sort bases, in order to visit them in address order, which 457 // may not be the same as declaration order. 458 llvm::SmallVector<BaseInfo, 8> Bases; 459 Bases.reserve(CD->getNumBases()); 460 unsigned BaseNo = 0; 461 for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(), 462 BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) { 463 assert(!Base->isVirtual() && "should not have virtual bases here"); 464 const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl(); 465 CharUnits BaseOffset = Layout.getBaseClassOffset(BD); 466 Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo)); 467 } 468 std::stable_sort(Bases.begin(), Bases.end()); 469 470 for (unsigned I = 0, N = Bases.size(); I != N; ++I) { 471 BaseInfo &Base = Bases[I]; 472 // Build the base class subobject at the appropriately-offset location 473 // within this object. 474 NextFieldOffsetInChars -= Base.Offset; 475 476 bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl; 477 Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase, 478 VTable, VTableClass, Offset + Base.Offset); 479 480 NextFieldOffsetInChars += Base.Offset; 481 } 482 } 483 484 unsigned FieldNo = 0; 485 const FieldDecl *LastFD = 0; 486 bool IsMsStruct = RD->hasAttr<MsStructAttr>(); 487 488 for (RecordDecl::field_iterator Field = RD->field_begin(), 489 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { 490 if (IsMsStruct) { 491 // Zero-length bitfields following non-bitfield members are 492 // ignored: 493 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) { 494 --FieldNo; 495 continue; 496 } 497 LastFD = (*Field); 498 } 499 500 // If this is a union, skip all the fields that aren't being initialized. 501 if (RD->isUnion() && Val.getUnionField() != *Field) 502 continue; 503 504 // Don't emit anonymous bitfields, they just affect layout. 505 if (Field->isUnnamedBitfield()) { 506 LastFD = (*Field); 507 continue; 508 } 509 510 // Emit the value of the initializer. 511 const APValue &FieldValue = 512 RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo); 513 llvm::Constant *EltInit = 514 CGM.EmitConstantValue(FieldValue, Field->getType(), CGF); 515 assert(EltInit && "EmitConstantValue can't fail"); 516 517 if (!Field->isBitField()) { 518 // Handle non-bitfield members. 519 AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit); 520 } else { 521 // Otherwise we have a bitfield. 522 AppendBitField(*Field, Layout.getFieldOffset(FieldNo), 523 cast<llvm::ConstantInt>(EltInit)); 524 } 525 } 526 } 527 528 llvm::Constant *ConstStructBuilder::Finalize(QualType Ty) { 529 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); 530 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 531 532 CharUnits LayoutSizeInChars = Layout.getSize(); 533 534 if (NextFieldOffsetInChars > LayoutSizeInChars) { 535 // If the struct is bigger than the size of the record type, 536 // we must have a flexible array member at the end. 537 assert(RD->hasFlexibleArrayMember() && 538 "Must have flexible array member if struct is bigger than type!"); 539 540 // No tail padding is necessary. 541 } else { 542 // Append tail padding if necessary. 543 AppendTailPadding(LayoutSizeInChars); 544 545 CharUnits LLVMSizeInChars = 546 NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment); 547 548 // Check if we need to convert the struct to a packed struct. 549 if (NextFieldOffsetInChars <= LayoutSizeInChars && 550 LLVMSizeInChars > LayoutSizeInChars) { 551 assert(!Packed && "Size mismatch!"); 552 553 ConvertStructToPacked(); 554 assert(NextFieldOffsetInChars <= LayoutSizeInChars && 555 "Converting to packed did not help!"); 556 } 557 558 assert(LayoutSizeInChars == NextFieldOffsetInChars && 559 "Tail padding mismatch!"); 560 } 561 562 // Pick the type to use. If the type is layout identical to the ConvertType 563 // type then use it, otherwise use whatever the builder produced for us. 564 llvm::StructType *STy = 565 llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(), 566 Elements, Packed); 567 llvm::Type *ValTy = CGM.getTypes().ConvertType(Ty); 568 if (llvm::StructType *ValSTy = dyn_cast<llvm::StructType>(ValTy)) { 569 if (ValSTy->isLayoutIdentical(STy)) 570 STy = ValSTy; 571 } 572 573 llvm::Constant *Result = llvm::ConstantStruct::get(STy, Elements); 574 575 assert(NextFieldOffsetInChars.RoundUpToAlignment(getAlignment(Result)) == 576 getSizeInChars(Result) && "Size mismatch!"); 577 578 return Result; 579 } 580 581 llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM, 582 CodeGenFunction *CGF, 583 InitListExpr *ILE) { 584 ConstStructBuilder Builder(CGM, CGF); 585 586 if (!Builder.Build(ILE)) 587 return 0; 588 589 return Builder.Finalize(ILE->getType()); 590 } 591 592 llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM, 593 CodeGenFunction *CGF, 594 const APValue &Val, 595 QualType ValTy) { 596 ConstStructBuilder Builder(CGM, CGF); 597 598 const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl(); 599 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); 600 llvm::Constant *VTable = 0; 601 if (CD && CD->isDynamicClass()) 602 VTable = CGM.getVTables().GetAddrOfVTable(CD); 603 604 Builder.Build(Val, RD, false, VTable, CD, CharUnits::Zero()); 605 606 return Builder.Finalize(ValTy); 607 } 608 609 610 //===----------------------------------------------------------------------===// 611 // ConstExprEmitter 612 //===----------------------------------------------------------------------===// 613 614 /// This class only needs to handle two cases: 615 /// 1) Literals (this is used by APValue emission to emit literals). 616 /// 2) Arrays, structs and unions (outside C++11 mode, we don't currently 617 /// constant fold these types). 618 class ConstExprEmitter : 619 public StmtVisitor<ConstExprEmitter, llvm::Constant*> { 620 CodeGenModule &CGM; 621 CodeGenFunction *CGF; 622 llvm::LLVMContext &VMContext; 623 public: 624 ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf) 625 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) { 626 } 627 628 //===--------------------------------------------------------------------===// 629 // Visitor Methods 630 //===--------------------------------------------------------------------===// 631 632 llvm::Constant *VisitStmt(Stmt *S) { 633 return 0; 634 } 635 636 llvm::Constant *VisitParenExpr(ParenExpr *PE) { 637 return Visit(PE->getSubExpr()); 638 } 639 640 llvm::Constant * 641 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) { 642 return Visit(PE->getReplacement()); 643 } 644 645 llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 646 return Visit(GE->getResultExpr()); 647 } 648 649 llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 650 return Visit(E->getInitializer()); 651 } 652 653 llvm::Constant *VisitCastExpr(CastExpr* E) { 654 Expr *subExpr = E->getSubExpr(); 655 llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF); 656 if (!C) return 0; 657 658 llvm::Type *destType = ConvertType(E->getType()); 659 660 switch (E->getCastKind()) { 661 case CK_ToUnion: { 662 // GCC cast to union extension 663 assert(E->getType()->isUnionType() && 664 "Destination type is not union type!"); 665 666 // Build a struct with the union sub-element as the first member, 667 // and padded to the appropriate size 668 SmallVector<llvm::Constant*, 2> Elts; 669 SmallVector<llvm::Type*, 2> Types; 670 Elts.push_back(C); 671 Types.push_back(C->getType()); 672 unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType()); 673 unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(destType); 674 675 assert(CurSize <= TotalSize && "Union size mismatch!"); 676 if (unsigned NumPadBytes = TotalSize - CurSize) { 677 llvm::Type *Ty = CGM.Int8Ty; 678 if (NumPadBytes > 1) 679 Ty = llvm::ArrayType::get(Ty, NumPadBytes); 680 681 Elts.push_back(llvm::UndefValue::get(Ty)); 682 Types.push_back(Ty); 683 } 684 685 llvm::StructType* STy = 686 llvm::StructType::get(C->getType()->getContext(), Types, false); 687 return llvm::ConstantStruct::get(STy, Elts); 688 } 689 690 case CK_LValueToRValue: 691 case CK_AtomicToNonAtomic: 692 case CK_NonAtomicToAtomic: 693 case CK_NoOp: 694 return C; 695 696 case CK_Dependent: llvm_unreachable("saw dependent cast!"); 697 698 case CK_ReinterpretMemberPointer: 699 case CK_DerivedToBaseMemberPointer: 700 case CK_BaseToDerivedMemberPointer: 701 return CGM.getCXXABI().EmitMemberPointerConversion(E, C); 702 703 // These will never be supported. 704 case CK_ObjCObjectLValueCast: 705 case CK_ARCProduceObject: 706 case CK_ARCConsumeObject: 707 case CK_ARCReclaimReturnedObject: 708 case CK_ARCExtendBlockObject: 709 case CK_CopyAndAutoreleaseBlockObject: 710 return 0; 711 712 // These don't need to be handled here because Evaluate knows how to 713 // evaluate them in the cases where they can be folded. 714 case CK_BitCast: 715 case CK_ToVoid: 716 case CK_Dynamic: 717 case CK_LValueBitCast: 718 case CK_NullToMemberPointer: 719 case CK_UserDefinedConversion: 720 case CK_ConstructorConversion: 721 case CK_CPointerToObjCPointerCast: 722 case CK_BlockPointerToObjCPointerCast: 723 case CK_AnyPointerToBlockPointerCast: 724 case CK_ArrayToPointerDecay: 725 case CK_FunctionToPointerDecay: 726 case CK_BaseToDerived: 727 case CK_DerivedToBase: 728 case CK_UncheckedDerivedToBase: 729 case CK_MemberPointerToBoolean: 730 case CK_VectorSplat: 731 case CK_FloatingRealToComplex: 732 case CK_FloatingComplexToReal: 733 case CK_FloatingComplexToBoolean: 734 case CK_FloatingComplexCast: 735 case CK_FloatingComplexToIntegralComplex: 736 case CK_IntegralRealToComplex: 737 case CK_IntegralComplexToReal: 738 case CK_IntegralComplexToBoolean: 739 case CK_IntegralComplexCast: 740 case CK_IntegralComplexToFloatingComplex: 741 case CK_PointerToIntegral: 742 case CK_PointerToBoolean: 743 case CK_NullToPointer: 744 case CK_IntegralCast: 745 case CK_IntegralToPointer: 746 case CK_IntegralToBoolean: 747 case CK_IntegralToFloating: 748 case CK_FloatingToIntegral: 749 case CK_FloatingToBoolean: 750 case CK_FloatingCast: 751 return 0; 752 } 753 llvm_unreachable("Invalid CastKind"); 754 } 755 756 llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 757 return Visit(DAE->getExpr()); 758 } 759 760 llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { 761 return Visit(E->GetTemporaryExpr()); 762 } 763 764 llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) { 765 unsigned NumInitElements = ILE->getNumInits(); 766 if (NumInitElements == 1 && ILE->getType() == ILE->getInit(0)->getType() && 767 (isa<StringLiteral>(ILE->getInit(0)) || 768 isa<ObjCEncodeExpr>(ILE->getInit(0)))) 769 return Visit(ILE->getInit(0)); 770 771 llvm::ArrayType *AType = 772 cast<llvm::ArrayType>(ConvertType(ILE->getType())); 773 llvm::Type *ElemTy = AType->getElementType(); 774 unsigned NumElements = AType->getNumElements(); 775 776 // Initialising an array requires us to automatically 777 // initialise any elements that have not been initialised explicitly 778 unsigned NumInitableElts = std::min(NumInitElements, NumElements); 779 780 // Copy initializer elements. 781 std::vector<llvm::Constant*> Elts; 782 Elts.reserve(NumInitableElts + NumElements); 783 784 bool RewriteType = false; 785 for (unsigned i = 0; i < NumInitableElts; ++i) { 786 Expr *Init = ILE->getInit(i); 787 llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF); 788 if (!C) 789 return 0; 790 RewriteType |= (C->getType() != ElemTy); 791 Elts.push_back(C); 792 } 793 794 // Initialize remaining array elements. 795 // FIXME: This doesn't handle member pointers correctly! 796 llvm::Constant *fillC; 797 if (Expr *filler = ILE->getArrayFiller()) 798 fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF); 799 else 800 fillC = llvm::Constant::getNullValue(ElemTy); 801 if (!fillC) 802 return 0; 803 RewriteType |= (fillC->getType() != ElemTy); 804 Elts.resize(NumElements, fillC); 805 806 if (RewriteType) { 807 // FIXME: Try to avoid packing the array 808 std::vector<llvm::Type*> Types; 809 Types.reserve(NumInitableElts + NumElements); 810 for (unsigned i = 0, e = Elts.size(); i < e; ++i) 811 Types.push_back(Elts[i]->getType()); 812 llvm::StructType *SType = llvm::StructType::get(AType->getContext(), 813 Types, true); 814 return llvm::ConstantStruct::get(SType, Elts); 815 } 816 817 return llvm::ConstantArray::get(AType, Elts); 818 } 819 820 llvm::Constant *EmitStructInitialization(InitListExpr *ILE) { 821 return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); 822 } 823 824 llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) { 825 return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); 826 } 827 828 llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) { 829 return CGM.EmitNullConstant(E->getType()); 830 } 831 832 llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { 833 if (ILE->getType()->isArrayType()) 834 return EmitArrayInitialization(ILE); 835 836 if (ILE->getType()->isRecordType()) 837 return EmitStructInitialization(ILE); 838 839 if (ILE->getType()->isUnionType()) 840 return EmitUnionInitialization(ILE); 841 842 return 0; 843 } 844 845 llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) { 846 if (!E->getConstructor()->isTrivial()) 847 return 0; 848 849 QualType Ty = E->getType(); 850 851 // FIXME: We should not have to call getBaseElementType here. 852 const RecordType *RT = 853 CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>(); 854 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 855 856 // If the class doesn't have a trivial destructor, we can't emit it as a 857 // constant expr. 858 if (!RD->hasTrivialDestructor()) 859 return 0; 860 861 // Only copy and default constructors can be trivial. 862 863 864 if (E->getNumArgs()) { 865 assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument"); 866 assert(E->getConstructor()->isCopyOrMoveConstructor() && 867 "trivial ctor has argument but isn't a copy/move ctor"); 868 869 Expr *Arg = E->getArg(0); 870 assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) && 871 "argument to copy ctor is of wrong type"); 872 873 return Visit(Arg); 874 } 875 876 return CGM.EmitNullConstant(Ty); 877 } 878 879 llvm::Constant *VisitStringLiteral(StringLiteral *E) { 880 return CGM.GetConstantArrayFromStringLiteral(E); 881 } 882 883 llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) { 884 // This must be an @encode initializing an array in a static initializer. 885 // Don't emit it as the address of the string, emit the string data itself 886 // as an inline array. 887 std::string Str; 888 CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); 889 const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType()); 890 891 // Resize the string to the right size, adding zeros at the end, or 892 // truncating as needed. 893 Str.resize(CAT->getSize().getZExtValue(), '\0'); 894 return llvm::ConstantDataArray::getString(VMContext, Str, false); 895 } 896 897 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { 898 return Visit(E->getSubExpr()); 899 } 900 901 // Utility methods 902 llvm::Type *ConvertType(QualType T) { 903 return CGM.getTypes().ConvertType(T); 904 } 905 906 public: 907 llvm::Constant *EmitLValue(APValue::LValueBase LVBase) { 908 if (const ValueDecl *Decl = LVBase.dyn_cast<const ValueDecl*>()) { 909 if (Decl->hasAttr<WeakRefAttr>()) 910 return CGM.GetWeakRefReference(Decl); 911 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) 912 return CGM.GetAddrOfFunction(FD); 913 if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) { 914 // We can never refer to a variable with local storage. 915 if (!VD->hasLocalStorage()) { 916 if (VD->isFileVarDecl() || VD->hasExternalStorage()) 917 return CGM.GetAddrOfGlobalVar(VD); 918 else if (VD->isLocalVarDecl()) { 919 assert(CGF && "Can't access static local vars without CGF"); 920 return CGF->GetAddrOfStaticLocalVar(VD); 921 } 922 } 923 } 924 return 0; 925 } 926 927 Expr *E = const_cast<Expr*>(LVBase.get<const Expr*>()); 928 switch (E->getStmtClass()) { 929 default: break; 930 case Expr::CompoundLiteralExprClass: { 931 // Note that due to the nature of compound literals, this is guaranteed 932 // to be the only use of the variable, so we just generate it here. 933 CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); 934 llvm::Constant* C = CGM.EmitConstantExpr(CLE->getInitializer(), 935 CLE->getType(), CGF); 936 // FIXME: "Leaked" on failure. 937 if (C) 938 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), 939 E->getType().isConstant(CGM.getContext()), 940 llvm::GlobalValue::InternalLinkage, 941 C, ".compoundliteral", 0, false, 942 CGM.getContext().getTargetAddressSpace(E->getType())); 943 return C; 944 } 945 case Expr::StringLiteralClass: 946 return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E)); 947 case Expr::ObjCEncodeExprClass: 948 return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E)); 949 case Expr::ObjCStringLiteralClass: { 950 ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E); 951 llvm::Constant *C = 952 CGM.getObjCRuntime().GenerateConstantString(SL->getString()); 953 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 954 } 955 case Expr::PredefinedExprClass: { 956 unsigned Type = cast<PredefinedExpr>(E)->getIdentType(); 957 if (CGF) { 958 LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E)); 959 return cast<llvm::Constant>(Res.getAddress()); 960 } else if (Type == PredefinedExpr::PrettyFunction) { 961 return CGM.GetAddrOfConstantCString("top level", ".tmp"); 962 } 963 964 return CGM.GetAddrOfConstantCString("", ".tmp"); 965 } 966 case Expr::AddrLabelExprClass: { 967 assert(CGF && "Invalid address of label expression outside function."); 968 llvm::Constant *Ptr = 969 CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel()); 970 return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType())); 971 } 972 case Expr::CallExprClass: { 973 CallExpr* CE = cast<CallExpr>(E); 974 unsigned builtin = CE->isBuiltinCall(); 975 if (builtin != 976 Builtin::BI__builtin___CFStringMakeConstantString && 977 builtin != 978 Builtin::BI__builtin___NSStringMakeConstantString) 979 break; 980 const Expr *Arg = CE->getArg(0)->IgnoreParenCasts(); 981 const StringLiteral *Literal = cast<StringLiteral>(Arg); 982 if (builtin == 983 Builtin::BI__builtin___NSStringMakeConstantString) { 984 return CGM.getObjCRuntime().GenerateConstantString(Literal); 985 } 986 // FIXME: need to deal with UCN conversion issues. 987 return CGM.GetAddrOfConstantCFString(Literal); 988 } 989 case Expr::BlockExprClass: { 990 std::string FunctionName; 991 if (CGF) 992 FunctionName = CGF->CurFn->getName(); 993 else 994 FunctionName = "global"; 995 996 return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str()); 997 } 998 case Expr::CXXTypeidExprClass: { 999 CXXTypeidExpr *Typeid = cast<CXXTypeidExpr>(E); 1000 QualType T; 1001 if (Typeid->isTypeOperand()) 1002 T = Typeid->getTypeOperand(); 1003 else 1004 T = Typeid->getExprOperand()->getType(); 1005 return CGM.GetAddrOfRTTIDescriptor(T); 1006 } 1007 } 1008 1009 return 0; 1010 } 1011 }; 1012 1013 } // end anonymous namespace. 1014 1015 llvm::Constant *CodeGenModule::EmitConstantInit(const VarDecl &D, 1016 CodeGenFunction *CGF) { 1017 if (const APValue *Value = D.evaluateValue()) 1018 return EmitConstantValue(*Value, D.getType(), CGF); 1019 1020 // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a 1021 // reference is a constant expression, and the reference binds to a temporary, 1022 // then constant initialization is performed. ConstExprEmitter will 1023 // incorrectly emit a prvalue constant in this case, and the calling code 1024 // interprets that as the (pointer) value of the reference, rather than the 1025 // desired value of the referee. 1026 if (D.getType()->isReferenceType()) 1027 return 0; 1028 1029 const Expr *E = D.getInit(); 1030 assert(E && "No initializer to emit"); 1031 1032 llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); 1033 if (C && C->getType()->isIntegerTy(1)) { 1034 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); 1035 C = llvm::ConstantExpr::getZExt(C, BoolTy); 1036 } 1037 return C; 1038 } 1039 1040 llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E, 1041 QualType DestType, 1042 CodeGenFunction *CGF) { 1043 Expr::EvalResult Result; 1044 1045 bool Success = false; 1046 1047 if (DestType->isReferenceType()) 1048 Success = E->EvaluateAsLValue(Result, Context); 1049 else 1050 Success = E->EvaluateAsRValue(Result, Context); 1051 1052 if (Success && !Result.HasSideEffects) 1053 return EmitConstantValue(Result.Val, DestType, CGF); 1054 1055 llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); 1056 if (C && C->getType()->isIntegerTy(1)) { 1057 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); 1058 C = llvm::ConstantExpr::getZExt(C, BoolTy); 1059 } 1060 return C; 1061 } 1062 1063 llvm::Constant *CodeGenModule::EmitConstantValue(const APValue &Value, 1064 QualType DestType, 1065 CodeGenFunction *CGF) { 1066 switch (Value.getKind()) { 1067 case APValue::Uninitialized: 1068 llvm_unreachable("Constant expressions should be initialized."); 1069 case APValue::LValue: { 1070 llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType); 1071 llvm::Constant *Offset = 1072 llvm::ConstantInt::get(Int64Ty, Value.getLValueOffset().getQuantity()); 1073 1074 llvm::Constant *C; 1075 if (APValue::LValueBase LVBase = Value.getLValueBase()) { 1076 // An array can be represented as an lvalue referring to the base. 1077 if (isa<llvm::ArrayType>(DestTy)) { 1078 assert(Offset->isNullValue() && "offset on array initializer"); 1079 return ConstExprEmitter(*this, CGF).Visit( 1080 const_cast<Expr*>(LVBase.get<const Expr*>())); 1081 } 1082 1083 C = ConstExprEmitter(*this, CGF).EmitLValue(LVBase); 1084 1085 // Apply offset if necessary. 1086 if (!Offset->isNullValue()) { 1087 llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Int8PtrTy); 1088 Casted = llvm::ConstantExpr::getGetElementPtr(Casted, Offset); 1089 C = llvm::ConstantExpr::getBitCast(Casted, C->getType()); 1090 } 1091 1092 // Convert to the appropriate type; this could be an lvalue for 1093 // an integer. 1094 if (isa<llvm::PointerType>(DestTy)) 1095 return llvm::ConstantExpr::getBitCast(C, DestTy); 1096 1097 return llvm::ConstantExpr::getPtrToInt(C, DestTy); 1098 } else { 1099 C = Offset; 1100 1101 // Convert to the appropriate type; this could be an lvalue for 1102 // an integer. 1103 if (isa<llvm::PointerType>(DestTy)) 1104 return llvm::ConstantExpr::getIntToPtr(C, DestTy); 1105 1106 // If the types don't match this should only be a truncate. 1107 if (C->getType() != DestTy) 1108 return llvm::ConstantExpr::getTrunc(C, DestTy); 1109 1110 return C; 1111 } 1112 } 1113 case APValue::Int: { 1114 llvm::Constant *C = llvm::ConstantInt::get(VMContext, 1115 Value.getInt()); 1116 1117 if (C->getType()->isIntegerTy(1)) { 1118 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(DestType); 1119 C = llvm::ConstantExpr::getZExt(C, BoolTy); 1120 } 1121 return C; 1122 } 1123 case APValue::ComplexInt: { 1124 llvm::Constant *Complex[2]; 1125 1126 Complex[0] = llvm::ConstantInt::get(VMContext, 1127 Value.getComplexIntReal()); 1128 Complex[1] = llvm::ConstantInt::get(VMContext, 1129 Value.getComplexIntImag()); 1130 1131 // FIXME: the target may want to specify that this is packed. 1132 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), 1133 Complex[1]->getType(), 1134 NULL); 1135 return llvm::ConstantStruct::get(STy, Complex); 1136 } 1137 case APValue::Float: { 1138 const llvm::APFloat &Init = Value.getFloat(); 1139 if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf) 1140 return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt()); 1141 else 1142 return llvm::ConstantFP::get(VMContext, Init); 1143 } 1144 case APValue::ComplexFloat: { 1145 llvm::Constant *Complex[2]; 1146 1147 Complex[0] = llvm::ConstantFP::get(VMContext, 1148 Value.getComplexFloatReal()); 1149 Complex[1] = llvm::ConstantFP::get(VMContext, 1150 Value.getComplexFloatImag()); 1151 1152 // FIXME: the target may want to specify that this is packed. 1153 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), 1154 Complex[1]->getType(), 1155 NULL); 1156 return llvm::ConstantStruct::get(STy, Complex); 1157 } 1158 case APValue::Vector: { 1159 SmallVector<llvm::Constant *, 4> Inits; 1160 unsigned NumElts = Value.getVectorLength(); 1161 1162 for (unsigned i = 0; i != NumElts; ++i) { 1163 const APValue &Elt = Value.getVectorElt(i); 1164 if (Elt.isInt()) 1165 Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt())); 1166 else 1167 Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat())); 1168 } 1169 return llvm::ConstantVector::get(Inits); 1170 } 1171 case APValue::AddrLabelDiff: { 1172 const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS(); 1173 const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS(); 1174 llvm::Constant *LHS = EmitConstantExpr(LHSExpr, LHSExpr->getType(), CGF); 1175 llvm::Constant *RHS = EmitConstantExpr(RHSExpr, RHSExpr->getType(), CGF); 1176 1177 // Compute difference 1178 llvm::Type *ResultType = getTypes().ConvertType(DestType); 1179 LHS = llvm::ConstantExpr::getPtrToInt(LHS, IntPtrTy); 1180 RHS = llvm::ConstantExpr::getPtrToInt(RHS, IntPtrTy); 1181 llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS); 1182 1183 // LLVM is a bit sensitive about the exact format of the 1184 // address-of-label difference; make sure to truncate after 1185 // the subtraction. 1186 return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType); 1187 } 1188 case APValue::Struct: 1189 case APValue::Union: 1190 return ConstStructBuilder::BuildStruct(*this, CGF, Value, DestType); 1191 case APValue::Array: { 1192 const ArrayType *CAT = Context.getAsArrayType(DestType); 1193 unsigned NumElements = Value.getArraySize(); 1194 unsigned NumInitElts = Value.getArrayInitializedElts(); 1195 1196 std::vector<llvm::Constant*> Elts; 1197 Elts.reserve(NumElements); 1198 1199 // Emit array filler, if there is one. 1200 llvm::Constant *Filler = 0; 1201 if (Value.hasArrayFiller()) 1202 Filler = EmitConstantValue(Value.getArrayFiller(), 1203 CAT->getElementType(), CGF); 1204 1205 // Emit initializer elements. 1206 llvm::Type *CommonElementType = 0; 1207 for (unsigned I = 0; I < NumElements; ++I) { 1208 llvm::Constant *C = Filler; 1209 if (I < NumInitElts) 1210 C = EmitConstantValue(Value.getArrayInitializedElt(I), 1211 CAT->getElementType(), CGF); 1212 if (I == 0) 1213 CommonElementType = C->getType(); 1214 else if (C->getType() != CommonElementType) 1215 CommonElementType = 0; 1216 Elts.push_back(C); 1217 } 1218 1219 if (!CommonElementType) { 1220 // FIXME: Try to avoid packing the array 1221 std::vector<llvm::Type*> Types; 1222 Types.reserve(NumElements); 1223 for (unsigned i = 0, e = Elts.size(); i < e; ++i) 1224 Types.push_back(Elts[i]->getType()); 1225 llvm::StructType *SType = llvm::StructType::get(VMContext, Types, true); 1226 return llvm::ConstantStruct::get(SType, Elts); 1227 } 1228 1229 llvm::ArrayType *AType = 1230 llvm::ArrayType::get(CommonElementType, NumElements); 1231 return llvm::ConstantArray::get(AType, Elts); 1232 } 1233 case APValue::MemberPointer: 1234 return getCXXABI().EmitMemberPointer(Value, DestType); 1235 } 1236 llvm_unreachable("Unknown APValue kind"); 1237 } 1238 1239 llvm::Constant * 1240 CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) { 1241 assert(E->isFileScope() && "not a file-scope compound literal expr"); 1242 return ConstExprEmitter(*this, 0).EmitLValue(E); 1243 } 1244 1245 llvm::Constant * 1246 CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) { 1247 // Member pointer constants always have a very particular form. 1248 const MemberPointerType *type = cast<MemberPointerType>(uo->getType()); 1249 const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl(); 1250 1251 // A member function pointer. 1252 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl)) 1253 return getCXXABI().EmitMemberPointer(method); 1254 1255 // Otherwise, a member data pointer. 1256 uint64_t fieldOffset = getContext().getFieldOffset(decl); 1257 CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset); 1258 return getCXXABI().EmitMemberDataPointer(type, chars); 1259 } 1260 1261 static void 1262 FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T, 1263 SmallVectorImpl<llvm::Constant *> &Elements, 1264 uint64_t StartOffset) { 1265 assert(StartOffset % CGM.getContext().getCharWidth() == 0 && 1266 "StartOffset not byte aligned!"); 1267 1268 if (CGM.getTypes().isZeroInitializable(T)) 1269 return; 1270 1271 if (const ConstantArrayType *CAT = 1272 CGM.getContext().getAsConstantArrayType(T)) { 1273 QualType ElementTy = CAT->getElementType(); 1274 uint64_t ElementSize = CGM.getContext().getTypeSize(ElementTy); 1275 1276 for (uint64_t I = 0, E = CAT->getSize().getZExtValue(); I != E; ++I) { 1277 FillInNullDataMemberPointers(CGM, ElementTy, Elements, 1278 StartOffset + I * ElementSize); 1279 } 1280 } else if (const RecordType *RT = T->getAs<RecordType>()) { 1281 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1282 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 1283 1284 // Go through all bases and fill in any null pointer to data members. 1285 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 1286 E = RD->bases_end(); I != E; ++I) { 1287 if (I->isVirtual()) { 1288 // Ignore virtual bases. 1289 continue; 1290 } 1291 1292 const CXXRecordDecl *BaseDecl = 1293 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 1294 1295 // Ignore empty bases. 1296 if (BaseDecl->isEmpty()) 1297 continue; 1298 1299 // Ignore bases that don't have any pointer to data members. 1300 if (CGM.getTypes().isZeroInitializable(BaseDecl)) 1301 continue; 1302 1303 uint64_t BaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl); 1304 FillInNullDataMemberPointers(CGM, I->getType(), 1305 Elements, StartOffset + BaseOffset); 1306 } 1307 1308 // Visit all fields. 1309 unsigned FieldNo = 0; 1310 for (RecordDecl::field_iterator I = RD->field_begin(), 1311 E = RD->field_end(); I != E; ++I, ++FieldNo) { 1312 QualType FieldType = I->getType(); 1313 1314 if (CGM.getTypes().isZeroInitializable(FieldType)) 1315 continue; 1316 1317 uint64_t FieldOffset = StartOffset + Layout.getFieldOffset(FieldNo); 1318 FillInNullDataMemberPointers(CGM, FieldType, Elements, FieldOffset); 1319 } 1320 } else { 1321 assert(T->isMemberPointerType() && "Should only see member pointers here!"); 1322 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && 1323 "Should only see pointers to data members here!"); 1324 1325 CharUnits StartIndex = CGM.getContext().toCharUnitsFromBits(StartOffset); 1326 CharUnits EndIndex = StartIndex + CGM.getContext().getTypeSizeInChars(T); 1327 1328 // FIXME: hardcodes Itanium member pointer representation! 1329 llvm::Constant *NegativeOne = 1330 llvm::ConstantInt::get(CGM.Int8Ty, -1ULL, /*isSigned*/true); 1331 1332 // Fill in the null data member pointer. 1333 for (CharUnits I = StartIndex; I != EndIndex; ++I) 1334 Elements[I.getQuantity()] = NegativeOne; 1335 } 1336 } 1337 1338 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, 1339 llvm::Type *baseType, 1340 const CXXRecordDecl *base); 1341 1342 static llvm::Constant *EmitNullConstant(CodeGenModule &CGM, 1343 const CXXRecordDecl *record, 1344 bool asCompleteObject) { 1345 const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record); 1346 llvm::StructType *structure = 1347 (asCompleteObject ? layout.getLLVMType() 1348 : layout.getBaseSubobjectLLVMType()); 1349 1350 unsigned numElements = structure->getNumElements(); 1351 std::vector<llvm::Constant *> elements(numElements); 1352 1353 // Fill in all the bases. 1354 for (CXXRecordDecl::base_class_const_iterator 1355 I = record->bases_begin(), E = record->bases_end(); I != E; ++I) { 1356 if (I->isVirtual()) { 1357 // Ignore virtual bases; if we're laying out for a complete 1358 // object, we'll lay these out later. 1359 continue; 1360 } 1361 1362 const CXXRecordDecl *base = 1363 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); 1364 1365 // Ignore empty bases. 1366 if (base->isEmpty()) 1367 continue; 1368 1369 unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base); 1370 llvm::Type *baseType = structure->getElementType(fieldIndex); 1371 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); 1372 } 1373 1374 // Fill in all the fields. 1375 for (RecordDecl::field_iterator I = record->field_begin(), 1376 E = record->field_end(); I != E; ++I) { 1377 const FieldDecl *field = *I; 1378 1379 // Fill in non-bitfields. (Bitfields always use a zero pattern, which we 1380 // will fill in later.) 1381 if (!field->isBitField()) { 1382 unsigned fieldIndex = layout.getLLVMFieldNo(field); 1383 elements[fieldIndex] = CGM.EmitNullConstant(field->getType()); 1384 } 1385 1386 // For unions, stop after the first named field. 1387 if (record->isUnion() && field->getDeclName()) 1388 break; 1389 } 1390 1391 // Fill in the virtual bases, if we're working with the complete object. 1392 if (asCompleteObject) { 1393 for (CXXRecordDecl::base_class_const_iterator 1394 I = record->vbases_begin(), E = record->vbases_end(); I != E; ++I) { 1395 const CXXRecordDecl *base = 1396 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); 1397 1398 // Ignore empty bases. 1399 if (base->isEmpty()) 1400 continue; 1401 1402 unsigned fieldIndex = layout.getVirtualBaseIndex(base); 1403 1404 // We might have already laid this field out. 1405 if (elements[fieldIndex]) continue; 1406 1407 llvm::Type *baseType = structure->getElementType(fieldIndex); 1408 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); 1409 } 1410 } 1411 1412 // Now go through all other fields and zero them out. 1413 for (unsigned i = 0; i != numElements; ++i) { 1414 if (!elements[i]) 1415 elements[i] = llvm::Constant::getNullValue(structure->getElementType(i)); 1416 } 1417 1418 return llvm::ConstantStruct::get(structure, elements); 1419 } 1420 1421 /// Emit the null constant for a base subobject. 1422 static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, 1423 llvm::Type *baseType, 1424 const CXXRecordDecl *base) { 1425 const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base); 1426 1427 // Just zero out bases that don't have any pointer to data members. 1428 if (baseLayout.isZeroInitializableAsBase()) 1429 return llvm::Constant::getNullValue(baseType); 1430 1431 // If the base type is a struct, we can just use its null constant. 1432 if (isa<llvm::StructType>(baseType)) { 1433 return EmitNullConstant(CGM, base, /*complete*/ false); 1434 } 1435 1436 // Otherwise, some bases are represented as arrays of i8 if the size 1437 // of the base is smaller than its corresponding LLVM type. Figure 1438 // out how many elements this base array has. 1439 llvm::ArrayType *baseArrayType = cast<llvm::ArrayType>(baseType); 1440 unsigned numBaseElements = baseArrayType->getNumElements(); 1441 1442 // Fill in null data member pointers. 1443 SmallVector<llvm::Constant *, 16> baseElements(numBaseElements); 1444 FillInNullDataMemberPointers(CGM, CGM.getContext().getTypeDeclType(base), 1445 baseElements, 0); 1446 1447 // Now go through all other elements and zero them out. 1448 if (numBaseElements) { 1449 llvm::Constant *i8_zero = llvm::Constant::getNullValue(CGM.Int8Ty); 1450 for (unsigned i = 0; i != numBaseElements; ++i) { 1451 if (!baseElements[i]) 1452 baseElements[i] = i8_zero; 1453 } 1454 } 1455 1456 return llvm::ConstantArray::get(baseArrayType, baseElements); 1457 } 1458 1459 llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) { 1460 if (getTypes().isZeroInitializable(T)) 1461 return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T)); 1462 1463 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) { 1464 llvm::ArrayType *ATy = 1465 cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T)); 1466 1467 QualType ElementTy = CAT->getElementType(); 1468 1469 llvm::Constant *Element = EmitNullConstant(ElementTy); 1470 unsigned NumElements = CAT->getSize().getZExtValue(); 1471 1472 if (Element->isNullValue()) 1473 return llvm::ConstantAggregateZero::get(ATy); 1474 1475 SmallVector<llvm::Constant *, 8> Array(NumElements, Element); 1476 return llvm::ConstantArray::get(ATy, Array); 1477 } 1478 1479 if (const RecordType *RT = T->getAs<RecordType>()) { 1480 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1481 return ::EmitNullConstant(*this, RD, /*complete object*/ true); 1482 } 1483 1484 assert(T->isMemberPointerType() && "Should only see member pointers here!"); 1485 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && 1486 "Should only see pointers to data members here!"); 1487 1488 // Itanium C++ ABI 2.3: 1489 // A NULL pointer is represented as -1. 1490 return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>()); 1491 } 1492 1493 llvm::Constant * 1494 CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) { 1495 return ::EmitNullConstant(*this, Record, false); 1496 } 1497