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