CGExprConstant.cpp revision cfa88f893915ceb8ae4ce2f17c46c24a4d67502f
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 "CGCXXABI.h" 16#include "CGObjCRuntime.h" 17#include "CGRecordLayout.h" 18#include "CodeGenModule.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/IR/Constants.h" 25#include "llvm/IR/DataLayout.h" 26#include "llvm/IR/Function.h" 27#include "llvm/IR/GlobalVariable.h" 28using namespace clang; 29using namespace CodeGen; 30 31//===----------------------------------------------------------------------===// 32// ConstStructBuilder 33//===----------------------------------------------------------------------===// 34 35namespace { 36class ConstStructBuilder { 37 CodeGenModule &CGM; 38 CodeGenFunction *CGF; 39 40 bool Packed; 41 CharUnits NextFieldOffsetInChars; 42 CharUnits LLVMStructAlignment; 43 SmallVector<llvm::Constant *, 32> Elements; 44public: 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 50private: 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.getDataLayout().getABITypeAlignment(C->getType())); 83 } 84 85 CharUnits getSizeInChars(const llvm::Constant *C) const { 86 return CharUnits::fromQuantity( 87 CGM.getDataLayout().getTypeAllocSize(C->getType())); 88 } 89}; 90 91void 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(Base.getBaseOffset(), VTableAddressPoint); 106} 107 108void ConstStructBuilder:: 109AppendField(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 118void ConstStructBuilder:: 119AppendBytes(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 161void 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.getDataLayout().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.getDataLayout().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.getDataLayout().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.getDataLayout().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 309void 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 325void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) { 326 assert(NextFieldOffsetInChars <= RecordSize && 327 "Size mismatch!"); 328 329 AppendPadding(RecordSize - NextFieldOffsetInChars); 330} 331 332void 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.getDataLayout().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 370bool 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->isMsStruct(CGM.getContext()); 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 431namespace { 432struct 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 445void 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 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 473 bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl; 474 Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase, 475 VTable, VTableClass, Offset + Base.Offset); 476 } 477 } 478 479 unsigned FieldNo = 0; 480 const FieldDecl *LastFD = 0; 481 bool IsMsStruct = RD->isMsStruct(CGM.getContext()); 482 uint64_t OffsetBits = CGM.getContext().toBits(Offset); 483 484 for (RecordDecl::field_iterator Field = RD->field_begin(), 485 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { 486 if (IsMsStruct) { 487 // Zero-length bitfields following non-bitfield members are 488 // ignored: 489 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield(*Field, LastFD)) { 490 --FieldNo; 491 continue; 492 } 493 LastFD = *Field; 494 } 495 496 // If this is a union, skip all the fields that aren't being initialized. 497 if (RD->isUnion() && Val.getUnionField() != *Field) 498 continue; 499 500 // Don't emit anonymous bitfields, they just affect layout. 501 if (Field->isUnnamedBitfield()) { 502 LastFD = *Field; 503 continue; 504 } 505 506 // Emit the value of the initializer. 507 const APValue &FieldValue = 508 RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo); 509 llvm::Constant *EltInit = 510 CGM.EmitConstantValueForMemory(FieldValue, Field->getType(), CGF); 511 assert(EltInit && "EmitConstantValue can't fail"); 512 513 if (!Field->isBitField()) { 514 // Handle non-bitfield members. 515 AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, EltInit); 516 } else { 517 // Otherwise we have a bitfield. 518 AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, 519 cast<llvm::ConstantInt>(EltInit)); 520 } 521 } 522} 523 524llvm::Constant *ConstStructBuilder::Finalize(QualType Ty) { 525 RecordDecl *RD = Ty->getAs<RecordType>()->getDecl(); 526 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 527 528 CharUnits LayoutSizeInChars = Layout.getSize(); 529 530 if (NextFieldOffsetInChars > LayoutSizeInChars) { 531 // If the struct is bigger than the size of the record type, 532 // we must have a flexible array member at the end. 533 assert(RD->hasFlexibleArrayMember() && 534 "Must have flexible array member if struct is bigger than type!"); 535 536 // No tail padding is necessary. 537 } else { 538 // Append tail padding if necessary. 539 AppendTailPadding(LayoutSizeInChars); 540 541 CharUnits LLVMSizeInChars = 542 NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment); 543 544 // Check if we need to convert the struct to a packed struct. 545 if (NextFieldOffsetInChars <= LayoutSizeInChars && 546 LLVMSizeInChars > LayoutSizeInChars) { 547 assert(!Packed && "Size mismatch!"); 548 549 ConvertStructToPacked(); 550 assert(NextFieldOffsetInChars <= LayoutSizeInChars && 551 "Converting to packed did not help!"); 552 } 553 554 assert(LayoutSizeInChars == NextFieldOffsetInChars && 555 "Tail padding mismatch!"); 556 } 557 558 // Pick the type to use. If the type is layout identical to the ConvertType 559 // type then use it, otherwise use whatever the builder produced for us. 560 llvm::StructType *STy = 561 llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(), 562 Elements, Packed); 563 llvm::Type *ValTy = CGM.getTypes().ConvertType(Ty); 564 if (llvm::StructType *ValSTy = dyn_cast<llvm::StructType>(ValTy)) { 565 if (ValSTy->isLayoutIdentical(STy)) 566 STy = ValSTy; 567 } 568 569 llvm::Constant *Result = llvm::ConstantStruct::get(STy, Elements); 570 571 assert(NextFieldOffsetInChars.RoundUpToAlignment(getAlignment(Result)) == 572 getSizeInChars(Result) && "Size mismatch!"); 573 574 return Result; 575} 576 577llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM, 578 CodeGenFunction *CGF, 579 InitListExpr *ILE) { 580 ConstStructBuilder Builder(CGM, CGF); 581 582 if (!Builder.Build(ILE)) 583 return 0; 584 585 return Builder.Finalize(ILE->getType()); 586} 587 588llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM, 589 CodeGenFunction *CGF, 590 const APValue &Val, 591 QualType ValTy) { 592 ConstStructBuilder Builder(CGM, CGF); 593 594 const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl(); 595 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); 596 llvm::Constant *VTable = 0; 597 if (CD && CD->isDynamicClass()) 598 VTable = CGM.getVTables().GetAddrOfVTable(CD); 599 600 Builder.Build(Val, RD, false, VTable, CD, CharUnits::Zero()); 601 602 return Builder.Finalize(ValTy); 603} 604 605 606//===----------------------------------------------------------------------===// 607// ConstExprEmitter 608//===----------------------------------------------------------------------===// 609 610/// This class only needs to handle two cases: 611/// 1) Literals (this is used by APValue emission to emit literals). 612/// 2) Arrays, structs and unions (outside C++11 mode, we don't currently 613/// constant fold these types). 614class ConstExprEmitter : 615 public StmtVisitor<ConstExprEmitter, llvm::Constant*> { 616 CodeGenModule &CGM; 617 CodeGenFunction *CGF; 618 llvm::LLVMContext &VMContext; 619public: 620 ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf) 621 : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) { 622 } 623 624 //===--------------------------------------------------------------------===// 625 // Visitor Methods 626 //===--------------------------------------------------------------------===// 627 628 llvm::Constant *VisitStmt(Stmt *S) { 629 return 0; 630 } 631 632 llvm::Constant *VisitParenExpr(ParenExpr *PE) { 633 return Visit(PE->getSubExpr()); 634 } 635 636 llvm::Constant * 637 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) { 638 return Visit(PE->getReplacement()); 639 } 640 641 llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) { 642 return Visit(GE->getResultExpr()); 643 } 644 645 llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { 646 return Visit(E->getInitializer()); 647 } 648 649 llvm::Constant *VisitCastExpr(CastExpr* E) { 650 Expr *subExpr = E->getSubExpr(); 651 llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF); 652 if (!C) return 0; 653 654 llvm::Type *destType = ConvertType(E->getType()); 655 656 switch (E->getCastKind()) { 657 case CK_ToUnion: { 658 // GCC cast to union extension 659 assert(E->getType()->isUnionType() && 660 "Destination type is not union type!"); 661 662 // Build a struct with the union sub-element as the first member, 663 // and padded to the appropriate size 664 SmallVector<llvm::Constant*, 2> Elts; 665 SmallVector<llvm::Type*, 2> Types; 666 Elts.push_back(C); 667 Types.push_back(C->getType()); 668 unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType()); 669 unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destType); 670 671 assert(CurSize <= TotalSize && "Union size mismatch!"); 672 if (unsigned NumPadBytes = TotalSize - CurSize) { 673 llvm::Type *Ty = CGM.Int8Ty; 674 if (NumPadBytes > 1) 675 Ty = llvm::ArrayType::get(Ty, NumPadBytes); 676 677 Elts.push_back(llvm::UndefValue::get(Ty)); 678 Types.push_back(Ty); 679 } 680 681 llvm::StructType* STy = 682 llvm::StructType::get(C->getType()->getContext(), Types, false); 683 return llvm::ConstantStruct::get(STy, Elts); 684 } 685 686 case CK_LValueToRValue: 687 case CK_AtomicToNonAtomic: 688 case CK_NonAtomicToAtomic: 689 case CK_NoOp: 690 return C; 691 692 case CK_Dependent: llvm_unreachable("saw dependent cast!"); 693 694 case CK_BuiltinFnToFnPtr: 695 llvm_unreachable("builtin functions are handled elsewhere"); 696 697 case CK_ReinterpretMemberPointer: 698 case CK_DerivedToBaseMemberPointer: 699 case CK_BaseToDerivedMemberPointer: 700 return CGM.getCXXABI().EmitMemberPointerConversion(E, C); 701 702 // These will never be supported. 703 case CK_ObjCObjectLValueCast: 704 case CK_ARCProduceObject: 705 case CK_ARCConsumeObject: 706 case CK_ARCReclaimReturnedObject: 707 case CK_ARCExtendBlockObject: 708 case CK_CopyAndAutoreleaseBlockObject: 709 return 0; 710 711 // These don't need to be handled here because Evaluate knows how to 712 // evaluate them in the cases where they can be folded. 713 case CK_BitCast: 714 case CK_ToVoid: 715 case CK_Dynamic: 716 case CK_LValueBitCast: 717 case CK_NullToMemberPointer: 718 case CK_UserDefinedConversion: 719 case CK_ConstructorConversion: 720 case CK_CPointerToObjCPointerCast: 721 case CK_BlockPointerToObjCPointerCast: 722 case CK_AnyPointerToBlockPointerCast: 723 case CK_ArrayToPointerDecay: 724 case CK_FunctionToPointerDecay: 725 case CK_BaseToDerived: 726 case CK_DerivedToBase: 727 case CK_UncheckedDerivedToBase: 728 case CK_MemberPointerToBoolean: 729 case CK_VectorSplat: 730 case CK_FloatingRealToComplex: 731 case CK_FloatingComplexToReal: 732 case CK_FloatingComplexToBoolean: 733 case CK_FloatingComplexCast: 734 case CK_FloatingComplexToIntegralComplex: 735 case CK_IntegralRealToComplex: 736 case CK_IntegralComplexToReal: 737 case CK_IntegralComplexToBoolean: 738 case CK_IntegralComplexCast: 739 case CK_IntegralComplexToFloatingComplex: 740 case CK_PointerToIntegral: 741 case CK_PointerToBoolean: 742 case CK_NullToPointer: 743 case CK_IntegralCast: 744 case CK_IntegralToPointer: 745 case CK_IntegralToBoolean: 746 case CK_IntegralToFloating: 747 case CK_FloatingToIntegral: 748 case CK_FloatingToBoolean: 749 case CK_FloatingCast: 750 return 0; 751 } 752 llvm_unreachable("Invalid CastKind"); 753 } 754 755 llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) { 756 return Visit(DAE->getExpr()); 757 } 758 759 llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) { 760 return Visit(E->GetTemporaryExpr()); 761 } 762 763 llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) { 764 if (ILE->isStringLiteralInit()) 765 return Visit(ILE->getInit(0)); 766 767 llvm::ArrayType *AType = 768 cast<llvm::ArrayType>(ConvertType(ILE->getType())); 769 llvm::Type *ElemTy = AType->getElementType(); 770 unsigned NumInitElements = ILE->getNumInits(); 771 unsigned NumElements = AType->getNumElements(); 772 773 // Initialising an array requires us to automatically 774 // initialise any elements that have not been initialised explicitly 775 unsigned NumInitableElts = std::min(NumInitElements, NumElements); 776 777 // Copy initializer elements. 778 std::vector<llvm::Constant*> Elts; 779 Elts.reserve(NumInitableElts + NumElements); 780 781 bool RewriteType = false; 782 for (unsigned i = 0; i < NumInitableElts; ++i) { 783 Expr *Init = ILE->getInit(i); 784 llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF); 785 if (!C) 786 return 0; 787 RewriteType |= (C->getType() != ElemTy); 788 Elts.push_back(C); 789 } 790 791 // Initialize remaining array elements. 792 // FIXME: This doesn't handle member pointers correctly! 793 llvm::Constant *fillC; 794 if (Expr *filler = ILE->getArrayFiller()) 795 fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF); 796 else 797 fillC = llvm::Constant::getNullValue(ElemTy); 798 if (!fillC) 799 return 0; 800 RewriteType |= (fillC->getType() != ElemTy); 801 Elts.resize(NumElements, fillC); 802 803 if (RewriteType) { 804 // FIXME: Try to avoid packing the array 805 std::vector<llvm::Type*> Types; 806 Types.reserve(NumInitableElts + NumElements); 807 for (unsigned i = 0, e = Elts.size(); i < e; ++i) 808 Types.push_back(Elts[i]->getType()); 809 llvm::StructType *SType = llvm::StructType::get(AType->getContext(), 810 Types, true); 811 return llvm::ConstantStruct::get(SType, Elts); 812 } 813 814 return llvm::ConstantArray::get(AType, Elts); 815 } 816 817 llvm::Constant *EmitRecordInitialization(InitListExpr *ILE) { 818 return ConstStructBuilder::BuildStruct(CGM, CGF, ILE); 819 } 820 821 llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) { 822 return CGM.EmitNullConstant(E->getType()); 823 } 824 825 llvm::Constant *VisitInitListExpr(InitListExpr *ILE) { 826 if (ILE->getType()->isArrayType()) 827 return EmitArrayInitialization(ILE); 828 829 if (ILE->getType()->isRecordType()) 830 return EmitRecordInitialization(ILE); 831 832 return 0; 833 } 834 835 llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) { 836 if (!E->getConstructor()->isTrivial()) 837 return 0; 838 839 QualType Ty = E->getType(); 840 841 // FIXME: We should not have to call getBaseElementType here. 842 const RecordType *RT = 843 CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>(); 844 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 845 846 // If the class doesn't have a trivial destructor, we can't emit it as a 847 // constant expr. 848 if (!RD->hasTrivialDestructor()) 849 return 0; 850 851 // Only copy and default constructors can be trivial. 852 853 854 if (E->getNumArgs()) { 855 assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument"); 856 assert(E->getConstructor()->isCopyOrMoveConstructor() && 857 "trivial ctor has argument but isn't a copy/move ctor"); 858 859 Expr *Arg = E->getArg(0); 860 assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) && 861 "argument to copy ctor is of wrong type"); 862 863 return Visit(Arg); 864 } 865 866 return CGM.EmitNullConstant(Ty); 867 } 868 869 llvm::Constant *VisitStringLiteral(StringLiteral *E) { 870 return CGM.GetConstantArrayFromStringLiteral(E); 871 } 872 873 llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) { 874 // This must be an @encode initializing an array in a static initializer. 875 // Don't emit it as the address of the string, emit the string data itself 876 // as an inline array. 877 std::string Str; 878 CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); 879 const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType()); 880 881 // Resize the string to the right size, adding zeros at the end, or 882 // truncating as needed. 883 Str.resize(CAT->getSize().getZExtValue(), '\0'); 884 return llvm::ConstantDataArray::getString(VMContext, Str, false); 885 } 886 887 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) { 888 return Visit(E->getSubExpr()); 889 } 890 891 // Utility methods 892 llvm::Type *ConvertType(QualType T) { 893 return CGM.getTypes().ConvertType(T); 894 } 895 896public: 897 llvm::Constant *EmitLValue(APValue::LValueBase LVBase) { 898 if (const ValueDecl *Decl = LVBase.dyn_cast<const ValueDecl*>()) { 899 if (Decl->hasAttr<WeakRefAttr>()) 900 return CGM.GetWeakRefReference(Decl); 901 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl)) 902 return CGM.GetAddrOfFunction(FD); 903 if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) { 904 // We can never refer to a variable with local storage. 905 if (!VD->hasLocalStorage()) { 906 if (VD->isFileVarDecl() || VD->hasExternalStorage()) 907 return CGM.GetAddrOfGlobalVar(VD); 908 else if (VD->isLocalVarDecl()) 909 return CGM.getStaticLocalDeclAddress(VD); 910 } 911 } 912 return 0; 913 } 914 915 Expr *E = const_cast<Expr*>(LVBase.get<const Expr*>()); 916 switch (E->getStmtClass()) { 917 default: break; 918 case Expr::CompoundLiteralExprClass: { 919 // Note that due to the nature of compound literals, this is guaranteed 920 // to be the only use of the variable, so we just generate it here. 921 CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); 922 llvm::Constant* C = CGM.EmitConstantExpr(CLE->getInitializer(), 923 CLE->getType(), CGF); 924 // FIXME: "Leaked" on failure. 925 if (C) 926 C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), 927 E->getType().isConstant(CGM.getContext()), 928 llvm::GlobalValue::InternalLinkage, 929 C, ".compoundliteral", 0, 930 llvm::GlobalVariable::NotThreadLocal, 931 CGM.getContext().getTargetAddressSpace(E->getType())); 932 return C; 933 } 934 case Expr::StringLiteralClass: 935 return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E)); 936 case Expr::ObjCEncodeExprClass: 937 return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E)); 938 case Expr::ObjCStringLiteralClass: { 939 ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E); 940 llvm::Constant *C = 941 CGM.getObjCRuntime().GenerateConstantString(SL->getString()); 942 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); 943 } 944 case Expr::PredefinedExprClass: { 945 unsigned Type = cast<PredefinedExpr>(E)->getIdentType(); 946 if (CGF) { 947 LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E)); 948 return cast<llvm::Constant>(Res.getAddress()); 949 } else if (Type == PredefinedExpr::PrettyFunction) { 950 return CGM.GetAddrOfConstantCString("top level", ".tmp"); 951 } 952 953 return CGM.GetAddrOfConstantCString("", ".tmp"); 954 } 955 case Expr::AddrLabelExprClass: { 956 assert(CGF && "Invalid address of label expression outside function."); 957 llvm::Constant *Ptr = 958 CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel()); 959 return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType())); 960 } 961 case Expr::CallExprClass: { 962 CallExpr* CE = cast<CallExpr>(E); 963 unsigned builtin = CE->isBuiltinCall(); 964 if (builtin != 965 Builtin::BI__builtin___CFStringMakeConstantString && 966 builtin != 967 Builtin::BI__builtin___NSStringMakeConstantString) 968 break; 969 const Expr *Arg = CE->getArg(0)->IgnoreParenCasts(); 970 const StringLiteral *Literal = cast<StringLiteral>(Arg); 971 if (builtin == 972 Builtin::BI__builtin___NSStringMakeConstantString) { 973 return CGM.getObjCRuntime().GenerateConstantString(Literal); 974 } 975 // FIXME: need to deal with UCN conversion issues. 976 return CGM.GetAddrOfConstantCFString(Literal); 977 } 978 case Expr::BlockExprClass: { 979 std::string FunctionName; 980 if (CGF) 981 FunctionName = CGF->CurFn->getName(); 982 else 983 FunctionName = "global"; 984 985 return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str()); 986 } 987 case Expr::CXXTypeidExprClass: { 988 CXXTypeidExpr *Typeid = cast<CXXTypeidExpr>(E); 989 QualType T; 990 if (Typeid->isTypeOperand()) 991 T = Typeid->getTypeOperand(); 992 else 993 T = Typeid->getExprOperand()->getType(); 994 return CGM.GetAddrOfRTTIDescriptor(T); 995 } 996 case Expr::CXXUuidofExprClass: { 997 return CGM.GetAddrOfUuidDescriptor(cast<CXXUuidofExpr>(E)); 998 } 999 } 1000 1001 return 0; 1002 } 1003}; 1004 1005} // end anonymous namespace. 1006 1007llvm::Constant *CodeGenModule::EmitConstantInit(const VarDecl &D, 1008 CodeGenFunction *CGF) { 1009 // Make a quick check if variable can be default NULL initialized 1010 // and avoid going through rest of code which may do, for c++11, 1011 // initialization of memory to all NULLs. 1012 if (!D.hasLocalStorage()) { 1013 QualType Ty = D.getType(); 1014 if (Ty->isArrayType()) 1015 Ty = Context.getBaseElementType(Ty); 1016 if (Ty->isRecordType()) 1017 if (const CXXConstructExpr *E = 1018 dyn_cast_or_null<CXXConstructExpr>(D.getInit())) { 1019 const CXXConstructorDecl *CD = E->getConstructor(); 1020 if (CD->isTrivial() && CD->isDefaultConstructor() && 1021 Ty->getAsCXXRecordDecl()->hasTrivialDestructor()) 1022 return EmitNullConstant(D.getType()); 1023 } 1024 } 1025 1026 if (const APValue *Value = D.evaluateValue()) 1027 return EmitConstantValueForMemory(*Value, D.getType(), CGF); 1028 1029 // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a 1030 // reference is a constant expression, and the reference binds to a temporary, 1031 // then constant initialization is performed. ConstExprEmitter will 1032 // incorrectly emit a prvalue constant in this case, and the calling code 1033 // interprets that as the (pointer) value of the reference, rather than the 1034 // desired value of the referee. 1035 if (D.getType()->isReferenceType()) 1036 return 0; 1037 1038 const Expr *E = D.getInit(); 1039 assert(E && "No initializer to emit"); 1040 1041 llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); 1042 if (C && C->getType()->isIntegerTy(1)) { 1043 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); 1044 C = llvm::ConstantExpr::getZExt(C, BoolTy); 1045 } 1046 return C; 1047} 1048 1049llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E, 1050 QualType DestType, 1051 CodeGenFunction *CGF) { 1052 Expr::EvalResult Result; 1053 1054 bool Success = false; 1055 1056 if (DestType->isReferenceType()) 1057 Success = E->EvaluateAsLValue(Result, Context); 1058 else 1059 Success = E->EvaluateAsRValue(Result, Context); 1060 1061 llvm::Constant *C = 0; 1062 if (Success && !Result.HasSideEffects) 1063 C = EmitConstantValue(Result.Val, DestType, CGF); 1064 else 1065 C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E)); 1066 1067 if (C && C->getType()->isIntegerTy(1)) { 1068 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType()); 1069 C = llvm::ConstantExpr::getZExt(C, BoolTy); 1070 } 1071 return C; 1072} 1073 1074llvm::Constant *CodeGenModule::EmitConstantValue(const APValue &Value, 1075 QualType DestType, 1076 CodeGenFunction *CGF) { 1077 switch (Value.getKind()) { 1078 case APValue::Uninitialized: 1079 llvm_unreachable("Constant expressions should be initialized."); 1080 case APValue::LValue: { 1081 llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType); 1082 llvm::Constant *Offset = 1083 llvm::ConstantInt::get(Int64Ty, Value.getLValueOffset().getQuantity()); 1084 1085 llvm::Constant *C; 1086 if (APValue::LValueBase LVBase = Value.getLValueBase()) { 1087 // An array can be represented as an lvalue referring to the base. 1088 if (isa<llvm::ArrayType>(DestTy)) { 1089 assert(Offset->isNullValue() && "offset on array initializer"); 1090 return ConstExprEmitter(*this, CGF).Visit( 1091 const_cast<Expr*>(LVBase.get<const Expr*>())); 1092 } 1093 1094 C = ConstExprEmitter(*this, CGF).EmitLValue(LVBase); 1095 1096 // Apply offset if necessary. 1097 if (!Offset->isNullValue()) { 1098 llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Int8PtrTy); 1099 Casted = llvm::ConstantExpr::getGetElementPtr(Casted, Offset); 1100 C = llvm::ConstantExpr::getBitCast(Casted, C->getType()); 1101 } 1102 1103 // Convert to the appropriate type; this could be an lvalue for 1104 // an integer. 1105 if (isa<llvm::PointerType>(DestTy)) 1106 return llvm::ConstantExpr::getBitCast(C, DestTy); 1107 1108 return llvm::ConstantExpr::getPtrToInt(C, DestTy); 1109 } else { 1110 C = Offset; 1111 1112 // Convert to the appropriate type; this could be an lvalue for 1113 // an integer. 1114 if (isa<llvm::PointerType>(DestTy)) 1115 return llvm::ConstantExpr::getIntToPtr(C, DestTy); 1116 1117 // If the types don't match this should only be a truncate. 1118 if (C->getType() != DestTy) 1119 return llvm::ConstantExpr::getTrunc(C, DestTy); 1120 1121 return C; 1122 } 1123 } 1124 case APValue::Int: 1125 return llvm::ConstantInt::get(VMContext, Value.getInt()); 1126 case APValue::ComplexInt: { 1127 llvm::Constant *Complex[2]; 1128 1129 Complex[0] = llvm::ConstantInt::get(VMContext, 1130 Value.getComplexIntReal()); 1131 Complex[1] = llvm::ConstantInt::get(VMContext, 1132 Value.getComplexIntImag()); 1133 1134 // FIXME: the target may want to specify that this is packed. 1135 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), 1136 Complex[1]->getType(), 1137 NULL); 1138 return llvm::ConstantStruct::get(STy, Complex); 1139 } 1140 case APValue::Float: { 1141 const llvm::APFloat &Init = Value.getFloat(); 1142 if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf) 1143 return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt()); 1144 else 1145 return llvm::ConstantFP::get(VMContext, Init); 1146 } 1147 case APValue::ComplexFloat: { 1148 llvm::Constant *Complex[2]; 1149 1150 Complex[0] = llvm::ConstantFP::get(VMContext, 1151 Value.getComplexFloatReal()); 1152 Complex[1] = llvm::ConstantFP::get(VMContext, 1153 Value.getComplexFloatImag()); 1154 1155 // FIXME: the target may want to specify that this is packed. 1156 llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(), 1157 Complex[1]->getType(), 1158 NULL); 1159 return llvm::ConstantStruct::get(STy, Complex); 1160 } 1161 case APValue::Vector: { 1162 SmallVector<llvm::Constant *, 4> Inits; 1163 unsigned NumElts = Value.getVectorLength(); 1164 1165 for (unsigned i = 0; i != NumElts; ++i) { 1166 const APValue &Elt = Value.getVectorElt(i); 1167 if (Elt.isInt()) 1168 Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt())); 1169 else 1170 Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat())); 1171 } 1172 return llvm::ConstantVector::get(Inits); 1173 } 1174 case APValue::AddrLabelDiff: { 1175 const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS(); 1176 const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS(); 1177 llvm::Constant *LHS = EmitConstantExpr(LHSExpr, LHSExpr->getType(), CGF); 1178 llvm::Constant *RHS = EmitConstantExpr(RHSExpr, RHSExpr->getType(), CGF); 1179 1180 // Compute difference 1181 llvm::Type *ResultType = getTypes().ConvertType(DestType); 1182 LHS = llvm::ConstantExpr::getPtrToInt(LHS, IntPtrTy); 1183 RHS = llvm::ConstantExpr::getPtrToInt(RHS, IntPtrTy); 1184 llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS); 1185 1186 // LLVM is a bit sensitive about the exact format of the 1187 // address-of-label difference; make sure to truncate after 1188 // the subtraction. 1189 return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType); 1190 } 1191 case APValue::Struct: 1192 case APValue::Union: 1193 return ConstStructBuilder::BuildStruct(*this, CGF, Value, DestType); 1194 case APValue::Array: { 1195 const ArrayType *CAT = Context.getAsArrayType(DestType); 1196 unsigned NumElements = Value.getArraySize(); 1197 unsigned NumInitElts = Value.getArrayInitializedElts(); 1198 1199 std::vector<llvm::Constant*> Elts; 1200 Elts.reserve(NumElements); 1201 1202 // Emit array filler, if there is one. 1203 llvm::Constant *Filler = 0; 1204 if (Value.hasArrayFiller()) 1205 Filler = EmitConstantValueForMemory(Value.getArrayFiller(), 1206 CAT->getElementType(), CGF); 1207 1208 // Emit initializer elements. 1209 llvm::Type *CommonElementType = 0; 1210 for (unsigned I = 0; I < NumElements; ++I) { 1211 llvm::Constant *C = Filler; 1212 if (I < NumInitElts) 1213 C = EmitConstantValueForMemory(Value.getArrayInitializedElt(I), 1214 CAT->getElementType(), CGF); 1215 if (I == 0) 1216 CommonElementType = C->getType(); 1217 else if (C->getType() != CommonElementType) 1218 CommonElementType = 0; 1219 Elts.push_back(C); 1220 } 1221 1222 if (!CommonElementType) { 1223 // FIXME: Try to avoid packing the array 1224 std::vector<llvm::Type*> Types; 1225 Types.reserve(NumElements); 1226 for (unsigned i = 0, e = Elts.size(); i < e; ++i) 1227 Types.push_back(Elts[i]->getType()); 1228 llvm::StructType *SType = llvm::StructType::get(VMContext, Types, true); 1229 return llvm::ConstantStruct::get(SType, Elts); 1230 } 1231 1232 llvm::ArrayType *AType = 1233 llvm::ArrayType::get(CommonElementType, NumElements); 1234 return llvm::ConstantArray::get(AType, Elts); 1235 } 1236 case APValue::MemberPointer: 1237 return getCXXABI().EmitMemberPointer(Value, DestType); 1238 } 1239 llvm_unreachable("Unknown APValue kind"); 1240} 1241 1242llvm::Constant * 1243CodeGenModule::EmitConstantValueForMemory(const APValue &Value, 1244 QualType DestType, 1245 CodeGenFunction *CGF) { 1246 llvm::Constant *C = EmitConstantValue(Value, DestType, CGF); 1247 if (C->getType()->isIntegerTy(1)) { 1248 llvm::Type *BoolTy = getTypes().ConvertTypeForMem(DestType); 1249 C = llvm::ConstantExpr::getZExt(C, BoolTy); 1250 } 1251 return C; 1252} 1253 1254llvm::Constant * 1255CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) { 1256 assert(E->isFileScope() && "not a file-scope compound literal expr"); 1257 return ConstExprEmitter(*this, 0).EmitLValue(E); 1258} 1259 1260llvm::Constant * 1261CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) { 1262 // Member pointer constants always have a very particular form. 1263 const MemberPointerType *type = cast<MemberPointerType>(uo->getType()); 1264 const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl(); 1265 1266 // A member function pointer. 1267 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl)) 1268 return getCXXABI().EmitMemberPointer(method); 1269 1270 // Otherwise, a member data pointer. 1271 uint64_t fieldOffset = getContext().getFieldOffset(decl); 1272 CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset); 1273 return getCXXABI().EmitMemberDataPointer(type, chars); 1274} 1275 1276static void 1277FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T, 1278 SmallVectorImpl<llvm::Constant *> &Elements, 1279 uint64_t StartOffset) { 1280 assert(StartOffset % CGM.getContext().getCharWidth() == 0 && 1281 "StartOffset not byte aligned!"); 1282 1283 if (CGM.getTypes().isZeroInitializable(T)) 1284 return; 1285 1286 if (const ConstantArrayType *CAT = 1287 CGM.getContext().getAsConstantArrayType(T)) { 1288 QualType ElementTy = CAT->getElementType(); 1289 uint64_t ElementSize = CGM.getContext().getTypeSize(ElementTy); 1290 1291 for (uint64_t I = 0, E = CAT->getSize().getZExtValue(); I != E; ++I) { 1292 FillInNullDataMemberPointers(CGM, ElementTy, Elements, 1293 StartOffset + I * ElementSize); 1294 } 1295 } else if (const RecordType *RT = T->getAs<RecordType>()) { 1296 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1297 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 1298 1299 // Go through all bases and fill in any null pointer to data members. 1300 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), 1301 E = RD->bases_end(); I != E; ++I) { 1302 if (I->isVirtual()) { 1303 // Ignore virtual bases. 1304 continue; 1305 } 1306 1307 const CXXRecordDecl *BaseDecl = 1308 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 1309 1310 // Ignore empty bases. 1311 if (BaseDecl->isEmpty()) 1312 continue; 1313 1314 // Ignore bases that don't have any pointer to data members. 1315 if (CGM.getTypes().isZeroInitializable(BaseDecl)) 1316 continue; 1317 1318 uint64_t BaseOffset = 1319 CGM.getContext().toBits(Layout.getBaseClassOffset(BaseDecl)); 1320 FillInNullDataMemberPointers(CGM, I->getType(), 1321 Elements, StartOffset + BaseOffset); 1322 } 1323 1324 // Visit all fields. 1325 unsigned FieldNo = 0; 1326 for (RecordDecl::field_iterator I = RD->field_begin(), 1327 E = RD->field_end(); I != E; ++I, ++FieldNo) { 1328 QualType FieldType = I->getType(); 1329 1330 if (CGM.getTypes().isZeroInitializable(FieldType)) 1331 continue; 1332 1333 uint64_t FieldOffset = StartOffset + Layout.getFieldOffset(FieldNo); 1334 FillInNullDataMemberPointers(CGM, FieldType, Elements, FieldOffset); 1335 } 1336 } else { 1337 assert(T->isMemberPointerType() && "Should only see member pointers here!"); 1338 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && 1339 "Should only see pointers to data members here!"); 1340 1341 CharUnits StartIndex = CGM.getContext().toCharUnitsFromBits(StartOffset); 1342 CharUnits EndIndex = StartIndex + CGM.getContext().getTypeSizeInChars(T); 1343 1344 // FIXME: hardcodes Itanium member pointer representation! 1345 llvm::Constant *NegativeOne = 1346 llvm::ConstantInt::get(CGM.Int8Ty, -1ULL, /*isSigned*/true); 1347 1348 // Fill in the null data member pointer. 1349 for (CharUnits I = StartIndex; I != EndIndex; ++I) 1350 Elements[I.getQuantity()] = NegativeOne; 1351 } 1352} 1353 1354static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, 1355 llvm::Type *baseType, 1356 const CXXRecordDecl *base); 1357 1358static llvm::Constant *EmitNullConstant(CodeGenModule &CGM, 1359 const CXXRecordDecl *record, 1360 bool asCompleteObject) { 1361 const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record); 1362 llvm::StructType *structure = 1363 (asCompleteObject ? layout.getLLVMType() 1364 : layout.getBaseSubobjectLLVMType()); 1365 1366 unsigned numElements = structure->getNumElements(); 1367 std::vector<llvm::Constant *> elements(numElements); 1368 1369 // Fill in all the bases. 1370 for (CXXRecordDecl::base_class_const_iterator 1371 I = record->bases_begin(), E = record->bases_end(); I != E; ++I) { 1372 if (I->isVirtual()) { 1373 // Ignore virtual bases; if we're laying out for a complete 1374 // object, we'll lay these out later. 1375 continue; 1376 } 1377 1378 const CXXRecordDecl *base = 1379 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); 1380 1381 // Ignore empty bases. 1382 if (base->isEmpty()) 1383 continue; 1384 1385 unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base); 1386 llvm::Type *baseType = structure->getElementType(fieldIndex); 1387 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); 1388 } 1389 1390 // Fill in all the fields. 1391 for (RecordDecl::field_iterator I = record->field_begin(), 1392 E = record->field_end(); I != E; ++I) { 1393 const FieldDecl *field = *I; 1394 1395 // Fill in non-bitfields. (Bitfields always use a zero pattern, which we 1396 // will fill in later.) 1397 if (!field->isBitField()) { 1398 unsigned fieldIndex = layout.getLLVMFieldNo(field); 1399 elements[fieldIndex] = CGM.EmitNullConstant(field->getType()); 1400 } 1401 1402 // For unions, stop after the first named field. 1403 if (record->isUnion() && field->getDeclName()) 1404 break; 1405 } 1406 1407 // Fill in the virtual bases, if we're working with the complete object. 1408 if (asCompleteObject) { 1409 for (CXXRecordDecl::base_class_const_iterator 1410 I = record->vbases_begin(), E = record->vbases_end(); I != E; ++I) { 1411 const CXXRecordDecl *base = 1412 cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); 1413 1414 // Ignore empty bases. 1415 if (base->isEmpty()) 1416 continue; 1417 1418 unsigned fieldIndex = layout.getVirtualBaseIndex(base); 1419 1420 // We might have already laid this field out. 1421 if (elements[fieldIndex]) continue; 1422 1423 llvm::Type *baseType = structure->getElementType(fieldIndex); 1424 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); 1425 } 1426 } 1427 1428 // Now go through all other fields and zero them out. 1429 for (unsigned i = 0; i != numElements; ++i) { 1430 if (!elements[i]) 1431 elements[i] = llvm::Constant::getNullValue(structure->getElementType(i)); 1432 } 1433 1434 return llvm::ConstantStruct::get(structure, elements); 1435} 1436 1437/// Emit the null constant for a base subobject. 1438static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, 1439 llvm::Type *baseType, 1440 const CXXRecordDecl *base) { 1441 const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base); 1442 1443 // Just zero out bases that don't have any pointer to data members. 1444 if (baseLayout.isZeroInitializableAsBase()) 1445 return llvm::Constant::getNullValue(baseType); 1446 1447 // If the base type is a struct, we can just use its null constant. 1448 if (isa<llvm::StructType>(baseType)) { 1449 return EmitNullConstant(CGM, base, /*complete*/ false); 1450 } 1451 1452 // Otherwise, some bases are represented as arrays of i8 if the size 1453 // of the base is smaller than its corresponding LLVM type. Figure 1454 // out how many elements this base array has. 1455 llvm::ArrayType *baseArrayType = cast<llvm::ArrayType>(baseType); 1456 unsigned numBaseElements = baseArrayType->getNumElements(); 1457 1458 // Fill in null data member pointers. 1459 SmallVector<llvm::Constant *, 16> baseElements(numBaseElements); 1460 FillInNullDataMemberPointers(CGM, CGM.getContext().getTypeDeclType(base), 1461 baseElements, 0); 1462 1463 // Now go through all other elements and zero them out. 1464 if (numBaseElements) { 1465 llvm::Constant *i8_zero = llvm::Constant::getNullValue(CGM.Int8Ty); 1466 for (unsigned i = 0; i != numBaseElements; ++i) { 1467 if (!baseElements[i]) 1468 baseElements[i] = i8_zero; 1469 } 1470 } 1471 1472 return llvm::ConstantArray::get(baseArrayType, baseElements); 1473} 1474 1475llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) { 1476 if (getTypes().isZeroInitializable(T)) 1477 return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T)); 1478 1479 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) { 1480 llvm::ArrayType *ATy = 1481 cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T)); 1482 1483 QualType ElementTy = CAT->getElementType(); 1484 1485 llvm::Constant *Element = EmitNullConstant(ElementTy); 1486 unsigned NumElements = CAT->getSize().getZExtValue(); 1487 1488 if (Element->isNullValue()) 1489 return llvm::ConstantAggregateZero::get(ATy); 1490 1491 SmallVector<llvm::Constant *, 8> Array(NumElements, Element); 1492 return llvm::ConstantArray::get(ATy, Array); 1493 } 1494 1495 if (const RecordType *RT = T->getAs<RecordType>()) { 1496 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 1497 return ::EmitNullConstant(*this, RD, /*complete object*/ true); 1498 } 1499 1500 assert(T->isMemberPointerType() && "Should only see member pointers here!"); 1501 assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() && 1502 "Should only see pointers to data members here!"); 1503 1504 // Itanium C++ ABI 2.3: 1505 // A NULL pointer is represented as -1. 1506 return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>()); 1507} 1508 1509llvm::Constant * 1510CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) { 1511 return ::EmitNullConstant(*this, Record, false); 1512} 1513