1 //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// 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 coordinates the per-module state used while generating code. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "CodeGenModule.h" 15 #include "CGDebugInfo.h" 16 #include "CodeGenFunction.h" 17 #include "CodeGenTBAA.h" 18 #include "CGCall.h" 19 #include "CGCXXABI.h" 20 #include "CGObjCRuntime.h" 21 #include "TargetInfo.h" 22 #include "clang/Frontend/CodeGenOptions.h" 23 #include "clang/AST/ASTContext.h" 24 #include "clang/AST/CharUnits.h" 25 #include "clang/AST/DeclObjC.h" 26 #include "clang/AST/DeclCXX.h" 27 #include "clang/AST/DeclTemplate.h" 28 #include "clang/AST/Mangle.h" 29 #include "clang/AST/RecordLayout.h" 30 #include "clang/Basic/Builtins.h" 31 #include "clang/Basic/Diagnostic.h" 32 #include "clang/Basic/SourceManager.h" 33 #include "clang/Basic/TargetInfo.h" 34 #include "clang/Basic/ConvertUTF.h" 35 #include "llvm/CallingConv.h" 36 #include "llvm/Module.h" 37 #include "llvm/Intrinsics.h" 38 #include "llvm/LLVMContext.h" 39 #include "llvm/ADT/Triple.h" 40 #include "llvm/Target/TargetData.h" 41 #include "llvm/Support/CallSite.h" 42 #include "llvm/Support/ErrorHandling.h" 43 using namespace clang; 44 using namespace CodeGen; 45 46 static CGCXXABI &createCXXABI(CodeGenModule &CGM) { 47 switch (CGM.getContext().Target.getCXXABI()) { 48 case CXXABI_ARM: return *CreateARMCXXABI(CGM); 49 case CXXABI_Itanium: return *CreateItaniumCXXABI(CGM); 50 case CXXABI_Microsoft: return *CreateMicrosoftCXXABI(CGM); 51 } 52 53 llvm_unreachable("invalid C++ ABI kind"); 54 return *CreateItaniumCXXABI(CGM); 55 } 56 57 58 CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, 59 llvm::Module &M, const llvm::TargetData &TD, 60 Diagnostic &diags) 61 : BlockModule(C, M, TD, Types, *this), Context(C), 62 Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M), 63 TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags), 64 ABI(createCXXABI(*this)), 65 Types(C, M, TD, getTargetCodeGenInfo().getABIInfo(), ABI), 66 TBAA(0), 67 VTables(*this), Runtime(0), 68 CFConstantStringClassRef(0), ConstantStringClassRef(0), 69 VMContext(M.getContext()), 70 NSConcreteGlobalBlockDecl(0), NSConcreteStackBlockDecl(0), 71 NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), 72 BlockObjectAssignDecl(0), BlockObjectDisposeDecl(0), 73 BlockObjectAssign(0), BlockObjectDispose(0){ 74 75 if (!Features.ObjC1) 76 Runtime = 0; 77 else if (!Features.NeXTRuntime) 78 Runtime = CreateGNUObjCRuntime(*this); 79 else if (Features.ObjCNonFragileABI) 80 Runtime = CreateMacNonFragileABIObjCRuntime(*this); 81 else 82 Runtime = CreateMacObjCRuntime(*this); 83 84 // Enable TBAA unless it's suppressed. 85 if (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0) 86 TBAA = new CodeGenTBAA(Context, VMContext, getLangOptions(), 87 ABI.getMangleContext()); 88 89 // If debug info generation is enabled, create the CGDebugInfo object. 90 DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0; 91 } 92 93 CodeGenModule::~CodeGenModule() { 94 delete Runtime; 95 delete &ABI; 96 delete TBAA; 97 delete DebugInfo; 98 } 99 100 void CodeGenModule::createObjCRuntime() { 101 if (!Features.NeXTRuntime) 102 Runtime = CreateGNUObjCRuntime(*this); 103 else if (Features.ObjCNonFragileABI) 104 Runtime = CreateMacNonFragileABIObjCRuntime(*this); 105 else 106 Runtime = CreateMacObjCRuntime(*this); 107 } 108 109 void CodeGenModule::Release() { 110 EmitDeferred(); 111 EmitCXXGlobalInitFunc(); 112 EmitCXXGlobalDtorFunc(); 113 if (Runtime) 114 if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 115 AddGlobalCtor(ObjCInitFunction); 116 EmitCtorList(GlobalCtors, "llvm.global_ctors"); 117 EmitCtorList(GlobalDtors, "llvm.global_dtors"); 118 EmitAnnotations(); 119 EmitLLVMUsed(); 120 121 SimplifyPersonality(); 122 123 if (getCodeGenOpts().EmitDeclMetadata) 124 EmitDeclMetadata(); 125 } 126 127 llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) { 128 if (!TBAA) 129 return 0; 130 return TBAA->getTBAAInfo(QTy); 131 } 132 133 void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst, 134 llvm::MDNode *TBAAInfo) { 135 Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo); 136 } 137 138 bool CodeGenModule::isTargetDarwin() const { 139 return getContext().Target.getTriple().getOS() == llvm::Triple::Darwin; 140 } 141 142 /// ErrorUnsupported - Print out an error that codegen doesn't support the 143 /// specified stmt yet. 144 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 145 bool OmitOnError) { 146 if (OmitOnError && getDiags().hasErrorOccurred()) 147 return; 148 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 149 "cannot compile this %0 yet"); 150 std::string Msg = Type; 151 getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 152 << Msg << S->getSourceRange(); 153 } 154 155 /// ErrorUnsupported - Print out an error that codegen doesn't support the 156 /// specified decl yet. 157 void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 158 bool OmitOnError) { 159 if (OmitOnError && getDiags().hasErrorOccurred()) 160 return; 161 unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 162 "cannot compile this %0 yet"); 163 std::string Msg = Type; 164 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 165 } 166 167 static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) { 168 switch (V) { 169 case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility; 170 case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility; 171 case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility; 172 } 173 llvm_unreachable("unknown visibility!"); 174 return llvm::GlobalValue::DefaultVisibility; 175 } 176 177 178 void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, 179 const NamedDecl *D) const { 180 // Internal definitions always have default visibility. 181 if (GV->hasLocalLinkage()) { 182 GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 183 return; 184 } 185 186 // Set visibility for definitions. 187 NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility(); 188 if (LV.visibilityExplicit() || !GV->hasAvailableExternallyLinkage()) 189 GV->setVisibility(GetLLVMVisibility(LV.visibility())); 190 } 191 192 /// Set the symbol visibility of type information (vtable and RTTI) 193 /// associated with the given type. 194 void CodeGenModule::setTypeVisibility(llvm::GlobalValue *GV, 195 const CXXRecordDecl *RD, 196 TypeVisibilityKind TVK) const { 197 setGlobalVisibility(GV, RD); 198 199 if (!CodeGenOpts.HiddenWeakVTables) 200 return; 201 202 // We never want to drop the visibility for RTTI names. 203 if (TVK == TVK_ForRTTIName) 204 return; 205 206 // We want to drop the visibility to hidden for weak type symbols. 207 // This isn't possible if there might be unresolved references 208 // elsewhere that rely on this symbol being visible. 209 210 // This should be kept roughly in sync with setThunkVisibility 211 // in CGVTables.cpp. 212 213 // Preconditions. 214 if (GV->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage || 215 GV->getVisibility() != llvm::GlobalVariable::DefaultVisibility) 216 return; 217 218 // Don't override an explicit visibility attribute. 219 if (RD->hasAttr<VisibilityAttr>()) 220 return; 221 222 switch (RD->getTemplateSpecializationKind()) { 223 // We have to disable the optimization if this is an EI definition 224 // because there might be EI declarations in other shared objects. 225 case TSK_ExplicitInstantiationDefinition: 226 case TSK_ExplicitInstantiationDeclaration: 227 return; 228 229 // Every use of a non-template class's type information has to emit it. 230 case TSK_Undeclared: 231 break; 232 233 // In theory, implicit instantiations can ignore the possibility of 234 // an explicit instantiation declaration because there necessarily 235 // must be an EI definition somewhere with default visibility. In 236 // practice, it's possible to have an explicit instantiation for 237 // an arbitrary template class, and linkers aren't necessarily able 238 // to deal with mixed-visibility symbols. 239 case TSK_ExplicitSpecialization: 240 case TSK_ImplicitInstantiation: 241 if (!CodeGenOpts.HiddenWeakTemplateVTables) 242 return; 243 break; 244 } 245 246 // If there's a key function, there may be translation units 247 // that don't have the key function's definition. But ignore 248 // this if we're emitting RTTI under -fno-rtti. 249 if (!(TVK != TVK_ForRTTI) || Features.RTTI) { 250 if (Context.getKeyFunction(RD)) 251 return; 252 } 253 254 // Otherwise, drop the visibility to hidden. 255 GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 256 GV->setUnnamedAddr(true); 257 } 258 259 llvm::StringRef CodeGenModule::getMangledName(GlobalDecl GD) { 260 const NamedDecl *ND = cast<NamedDecl>(GD.getDecl()); 261 262 llvm::StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()]; 263 if (!Str.empty()) 264 return Str; 265 266 if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) { 267 IdentifierInfo *II = ND->getIdentifier(); 268 assert(II && "Attempt to mangle unnamed decl."); 269 270 Str = II->getName(); 271 return Str; 272 } 273 274 llvm::SmallString<256> Buffer; 275 if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) 276 getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Buffer); 277 else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) 278 getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Buffer); 279 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND)) 280 getCXXABI().getMangleContext().mangleBlock(BD, Buffer); 281 else 282 getCXXABI().getMangleContext().mangleName(ND, Buffer); 283 284 // Allocate space for the mangled name. 285 size_t Length = Buffer.size(); 286 char *Name = MangledNamesAllocator.Allocate<char>(Length); 287 std::copy(Buffer.begin(), Buffer.end(), Name); 288 289 Str = llvm::StringRef(Name, Length); 290 291 return Str; 292 } 293 294 void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer, 295 const BlockDecl *BD) { 296 MangleContext &MangleCtx = getCXXABI().getMangleContext(); 297 const Decl *D = GD.getDecl(); 298 if (D == 0) 299 MangleCtx.mangleGlobalBlock(BD, Buffer.getBuffer()); 300 else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) 301 MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Buffer.getBuffer()); 302 else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) 303 MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Buffer.getBuffer()); 304 else 305 MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Buffer.getBuffer()); 306 } 307 308 llvm::GlobalValue *CodeGenModule::GetGlobalValue(llvm::StringRef Name) { 309 return getModule().getNamedValue(Name); 310 } 311 312 /// AddGlobalCtor - Add a function to the list that will be called before 313 /// main() runs. 314 void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 315 // FIXME: Type coercion of void()* types. 316 GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 317 } 318 319 /// AddGlobalDtor - Add a function to the list that will be called 320 /// when the module is unloaded. 321 void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 322 // FIXME: Type coercion of void()* types. 323 GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 324 } 325 326 void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 327 // Ctor function type is void()*. 328 llvm::FunctionType* CtorFTy = 329 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false); 330 llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 331 332 // Get the type of a ctor entry, { i32, void ()* }. 333 llvm::StructType* CtorStructTy = 334 llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext), 335 llvm::PointerType::getUnqual(CtorFTy), NULL); 336 337 // Construct the constructor and destructor arrays. 338 std::vector<llvm::Constant*> Ctors; 339 for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 340 std::vector<llvm::Constant*> S; 341 S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 342 I->second, false)); 343 S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 344 Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 345 } 346 347 if (!Ctors.empty()) { 348 llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 349 new llvm::GlobalVariable(TheModule, AT, false, 350 llvm::GlobalValue::AppendingLinkage, 351 llvm::ConstantArray::get(AT, Ctors), 352 GlobalName); 353 } 354 } 355 356 void CodeGenModule::EmitAnnotations() { 357 if (Annotations.empty()) 358 return; 359 360 // Create a new global variable for the ConstantStruct in the Module. 361 llvm::Constant *Array = 362 llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 363 Annotations.size()), 364 Annotations); 365 llvm::GlobalValue *gv = 366 new llvm::GlobalVariable(TheModule, Array->getType(), false, 367 llvm::GlobalValue::AppendingLinkage, Array, 368 "llvm.global.annotations"); 369 gv->setSection("llvm.metadata"); 370 } 371 372 llvm::GlobalValue::LinkageTypes 373 CodeGenModule::getFunctionLinkage(const FunctionDecl *D) { 374 GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); 375 376 if (Linkage == GVA_Internal) 377 return llvm::Function::InternalLinkage; 378 379 if (D->hasAttr<DLLExportAttr>()) 380 return llvm::Function::DLLExportLinkage; 381 382 if (D->hasAttr<WeakAttr>()) 383 return llvm::Function::WeakAnyLinkage; 384 385 // In C99 mode, 'inline' functions are guaranteed to have a strong 386 // definition somewhere else, so we can use available_externally linkage. 387 if (Linkage == GVA_C99Inline) 388 return llvm::Function::AvailableExternallyLinkage; 389 390 // In C++, the compiler has to emit a definition in every translation unit 391 // that references the function. We should use linkonce_odr because 392 // a) if all references in this translation unit are optimized away, we 393 // don't need to codegen it. b) if the function persists, it needs to be 394 // merged with other definitions. c) C++ has the ODR, so we know the 395 // definition is dependable. 396 if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) 397 return llvm::Function::LinkOnceODRLinkage; 398 399 // An explicit instantiation of a template has weak linkage, since 400 // explicit instantiations can occur in multiple translation units 401 // and must all be equivalent. However, we are not allowed to 402 // throw away these explicit instantiations. 403 if (Linkage == GVA_ExplicitTemplateInstantiation) 404 return llvm::Function::WeakODRLinkage; 405 406 // Otherwise, we have strong external linkage. 407 assert(Linkage == GVA_StrongExternal); 408 return llvm::Function::ExternalLinkage; 409 } 410 411 412 /// SetFunctionDefinitionAttributes - Set attributes for a global. 413 /// 414 /// FIXME: This is currently only done for aliases and functions, but not for 415 /// variables (these details are set in EmitGlobalVarDefinition for variables). 416 void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, 417 llvm::GlobalValue *GV) { 418 SetCommonAttributes(D, GV); 419 } 420 421 void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, 422 const CGFunctionInfo &Info, 423 llvm::Function *F) { 424 unsigned CallingConv; 425 AttributeListType AttributeList; 426 ConstructAttributeList(Info, D, AttributeList, CallingConv); 427 F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 428 AttributeList.size())); 429 F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 430 } 431 432 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, 433 llvm::Function *F) { 434 if (!Features.Exceptions && !Features.ObjCNonFragileABI) 435 F->addFnAttr(llvm::Attribute::NoUnwind); 436 437 if (D->hasAttr<AlwaysInlineAttr>()) 438 F->addFnAttr(llvm::Attribute::AlwaysInline); 439 440 if (D->hasAttr<NakedAttr>()) 441 F->addFnAttr(llvm::Attribute::Naked); 442 443 if (D->hasAttr<NoInlineAttr>()) 444 F->addFnAttr(llvm::Attribute::NoInline); 445 446 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D)) 447 F->setUnnamedAddr(true); 448 449 if (Features.getStackProtectorMode() == LangOptions::SSPOn) 450 F->addFnAttr(llvm::Attribute::StackProtect); 451 else if (Features.getStackProtectorMode() == LangOptions::SSPReq) 452 F->addFnAttr(llvm::Attribute::StackProtectReq); 453 454 unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); 455 if (alignment) 456 F->setAlignment(alignment); 457 458 // C++ ABI requires 2-byte alignment for member functions. 459 if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) 460 F->setAlignment(2); 461 } 462 463 void CodeGenModule::SetCommonAttributes(const Decl *D, 464 llvm::GlobalValue *GV) { 465 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 466 setGlobalVisibility(GV, ND); 467 else 468 GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 469 470 if (D->hasAttr<UsedAttr>()) 471 AddUsedGlobal(GV); 472 473 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 474 GV->setSection(SA->getName()); 475 476 getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this); 477 } 478 479 void CodeGenModule::SetInternalFunctionAttributes(const Decl *D, 480 llvm::Function *F, 481 const CGFunctionInfo &FI) { 482 SetLLVMFunctionAttributes(D, FI, F); 483 SetLLVMFunctionAttributesForDefinition(D, F); 484 485 F->setLinkage(llvm::Function::InternalLinkage); 486 487 SetCommonAttributes(D, F); 488 } 489 490 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, 491 llvm::Function *F, 492 bool IsIncompleteFunction) { 493 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 494 495 if (!IsIncompleteFunction) 496 SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F); 497 498 // Only a few attributes are set on declarations; these may later be 499 // overridden by a definition. 500 501 if (FD->hasAttr<DLLImportAttr>()) { 502 F->setLinkage(llvm::Function::DLLImportLinkage); 503 } else if (FD->hasAttr<WeakAttr>() || 504 FD->hasAttr<WeakImportAttr>()) { 505 // "extern_weak" is overloaded in LLVM; we probably should have 506 // separate linkage types for this. 507 F->setLinkage(llvm::Function::ExternalWeakLinkage); 508 } else { 509 F->setLinkage(llvm::Function::ExternalLinkage); 510 511 NamedDecl::LinkageInfo LV = FD->getLinkageAndVisibility(); 512 if (LV.linkage() == ExternalLinkage && LV.visibilityExplicit()) { 513 F->setVisibility(GetLLVMVisibility(LV.visibility())); 514 } 515 } 516 517 if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) 518 F->setSection(SA->getName()); 519 } 520 521 void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 522 assert(!GV->isDeclaration() && 523 "Only globals with definition can force usage."); 524 LLVMUsed.push_back(GV); 525 } 526 527 void CodeGenModule::EmitLLVMUsed() { 528 // Don't create llvm.used if there is no need. 529 if (LLVMUsed.empty()) 530 return; 531 532 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); 533 534 // Convert LLVMUsed to what ConstantArray needs. 535 std::vector<llvm::Constant*> UsedArray; 536 UsedArray.resize(LLVMUsed.size()); 537 for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { 538 UsedArray[i] = 539 llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), 540 i8PTy); 541 } 542 543 if (UsedArray.empty()) 544 return; 545 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); 546 547 llvm::GlobalVariable *GV = 548 new llvm::GlobalVariable(getModule(), ATy, false, 549 llvm::GlobalValue::AppendingLinkage, 550 llvm::ConstantArray::get(ATy, UsedArray), 551 "llvm.used"); 552 553 GV->setSection("llvm.metadata"); 554 } 555 556 void CodeGenModule::EmitDeferred() { 557 // Emit code for any potentially referenced deferred decls. Since a 558 // previously unused static decl may become used during the generation of code 559 // for a static function, iterate until no changes are made. 560 561 while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) { 562 if (!DeferredVTables.empty()) { 563 const CXXRecordDecl *RD = DeferredVTables.back(); 564 DeferredVTables.pop_back(); 565 getVTables().GenerateClassData(getVTableLinkage(RD), RD); 566 continue; 567 } 568 569 GlobalDecl D = DeferredDeclsToEmit.back(); 570 DeferredDeclsToEmit.pop_back(); 571 572 // Check to see if we've already emitted this. This is necessary 573 // for a couple of reasons: first, decls can end up in the 574 // deferred-decls queue multiple times, and second, decls can end 575 // up with definitions in unusual ways (e.g. by an extern inline 576 // function acquiring a strong function redefinition). Just 577 // ignore these cases. 578 // 579 // TODO: That said, looking this up multiple times is very wasteful. 580 llvm::StringRef Name = getMangledName(D); 581 llvm::GlobalValue *CGRef = GetGlobalValue(Name); 582 assert(CGRef && "Deferred decl wasn't referenced?"); 583 584 if (!CGRef->isDeclaration()) 585 continue; 586 587 // GlobalAlias::isDeclaration() defers to the aliasee, but for our 588 // purposes an alias counts as a definition. 589 if (isa<llvm::GlobalAlias>(CGRef)) 590 continue; 591 592 // Otherwise, emit the definition and move on to the next one. 593 EmitGlobalDefinition(D); 594 } 595 } 596 597 /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 598 /// annotation information for a given GlobalValue. The annotation struct is 599 /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 600 /// GlobalValue being annotated. The second field is the constant string 601 /// created from the AnnotateAttr's annotation. The third field is a constant 602 /// string containing the name of the translation unit. The fourth field is 603 /// the line number in the file of the annotated value declaration. 604 /// 605 /// FIXME: this does not unique the annotation string constants, as llvm-gcc 606 /// appears to. 607 /// 608 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 609 const AnnotateAttr *AA, 610 unsigned LineNo) { 611 llvm::Module *M = &getModule(); 612 613 // get [N x i8] constants for the annotation string, and the filename string 614 // which are the 2nd and 3rd elements of the global annotation structure. 615 const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext); 616 llvm::Constant *anno = llvm::ConstantArray::get(VMContext, 617 AA->getAnnotation(), true); 618 llvm::Constant *unit = llvm::ConstantArray::get(VMContext, 619 M->getModuleIdentifier(), 620 true); 621 622 // Get the two global values corresponding to the ConstantArrays we just 623 // created to hold the bytes of the strings. 624 llvm::GlobalValue *annoGV = 625 new llvm::GlobalVariable(*M, anno->getType(), false, 626 llvm::GlobalValue::PrivateLinkage, anno, 627 GV->getName()); 628 // translation unit name string, emitted into the llvm.metadata section. 629 llvm::GlobalValue *unitGV = 630 new llvm::GlobalVariable(*M, unit->getType(), false, 631 llvm::GlobalValue::PrivateLinkage, unit, 632 ".str"); 633 unitGV->setUnnamedAddr(true); 634 635 // Create the ConstantStruct for the global annotation. 636 llvm::Constant *Fields[4] = { 637 llvm::ConstantExpr::getBitCast(GV, SBP), 638 llvm::ConstantExpr::getBitCast(annoGV, SBP), 639 llvm::ConstantExpr::getBitCast(unitGV, SBP), 640 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo) 641 }; 642 return llvm::ConstantStruct::get(VMContext, Fields, 4, false); 643 } 644 645 bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 646 // Never defer when EmitAllDecls is specified. 647 if (Features.EmitAllDecls) 648 return false; 649 650 return !getContext().DeclMustBeEmitted(Global); 651 } 652 653 llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { 654 const AliasAttr *AA = VD->getAttr<AliasAttr>(); 655 assert(AA && "No alias?"); 656 657 const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); 658 659 // See if there is already something with the target's name in the module. 660 llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); 661 662 llvm::Constant *Aliasee; 663 if (isa<llvm::FunctionType>(DeclTy)) 664 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl()); 665 else 666 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), 667 llvm::PointerType::getUnqual(DeclTy), 0); 668 if (!Entry) { 669 llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee); 670 F->setLinkage(llvm::Function::ExternalWeakLinkage); 671 WeakRefReferences.insert(F); 672 } 673 674 return Aliasee; 675 } 676 677 void CodeGenModule::EmitGlobal(GlobalDecl GD) { 678 const ValueDecl *Global = cast<ValueDecl>(GD.getDecl()); 679 680 // Weak references don't produce any output by themselves. 681 if (Global->hasAttr<WeakRefAttr>()) 682 return; 683 684 // If this is an alias definition (which otherwise looks like a declaration) 685 // emit it now. 686 if (Global->hasAttr<AliasAttr>()) 687 return EmitAliasDefinition(GD); 688 689 // Ignore declarations, they will be emitted on their first use. 690 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 691 if (FD->getIdentifier()) { 692 llvm::StringRef Name = FD->getName(); 693 if (Name == "_Block_object_assign") { 694 BlockObjectAssignDecl = FD; 695 } else if (Name == "_Block_object_dispose") { 696 BlockObjectDisposeDecl = FD; 697 } 698 } 699 700 // Forward declarations are emitted lazily on first use. 701 if (!FD->isThisDeclarationADefinition()) 702 return; 703 } else { 704 const VarDecl *VD = cast<VarDecl>(Global); 705 assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 706 707 if (VD->getIdentifier()) { 708 llvm::StringRef Name = VD->getName(); 709 if (Name == "_NSConcreteGlobalBlock") { 710 NSConcreteGlobalBlockDecl = VD; 711 } else if (Name == "_NSConcreteStackBlock") { 712 NSConcreteStackBlockDecl = VD; 713 } 714 } 715 716 717 if (VD->isThisDeclarationADefinition() != VarDecl::Definition) 718 return; 719 } 720 721 // Defer code generation when possible if this is a static definition, inline 722 // function etc. These we only want to emit if they are used. 723 if (!MayDeferGeneration(Global)) { 724 // Emit the definition if it can't be deferred. 725 EmitGlobalDefinition(GD); 726 return; 727 } 728 729 // If we're deferring emission of a C++ variable with an 730 // initializer, remember the order in which it appeared in the file. 731 if (getLangOptions().CPlusPlus && isa<VarDecl>(Global) && 732 cast<VarDecl>(Global)->hasInit()) { 733 DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); 734 CXXGlobalInits.push_back(0); 735 } 736 737 // If the value has already been used, add it directly to the 738 // DeferredDeclsToEmit list. 739 llvm::StringRef MangledName = getMangledName(GD); 740 if (GetGlobalValue(MangledName)) 741 DeferredDeclsToEmit.push_back(GD); 742 else { 743 // Otherwise, remember that we saw a deferred decl with this name. The 744 // first use of the mangled name will cause it to move into 745 // DeferredDeclsToEmit. 746 DeferredDecls[MangledName] = GD; 747 } 748 } 749 750 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { 751 const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 752 753 PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), 754 Context.getSourceManager(), 755 "Generating code for declaration"); 756 757 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 758 // At -O0, don't generate IR for functions with available_externally 759 // linkage. 760 if (CodeGenOpts.OptimizationLevel == 0 && 761 !Function->hasAttr<AlwaysInlineAttr>() && 762 getFunctionLinkage(Function) 763 == llvm::Function::AvailableExternallyLinkage) 764 return; 765 766 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 767 if (Method->isVirtual()) 768 getVTables().EmitThunks(GD); 769 770 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method)) 771 return EmitCXXConstructor(CD, GD.getCtorType()); 772 773 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Method)) 774 return EmitCXXDestructor(DD, GD.getDtorType()); 775 } 776 777 return EmitGlobalFunctionDefinition(GD); 778 } 779 780 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 781 return EmitGlobalVarDefinition(VD); 782 783 assert(0 && "Invalid argument to EmitGlobalDefinition()"); 784 } 785 786 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the 787 /// module, create and return an llvm Function with the specified type. If there 788 /// is something in the module with the specified name, return it potentially 789 /// bitcasted to the right type. 790 /// 791 /// If D is non-null, it specifies a decl that correspond to this. This is used 792 /// to set the attributes on the function when it is first created. 793 llvm::Constant * 794 CodeGenModule::GetOrCreateLLVMFunction(llvm::StringRef MangledName, 795 const llvm::Type *Ty, 796 GlobalDecl D) { 797 // Lookup the entry, lazily creating it if necessary. 798 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 799 if (Entry) { 800 if (WeakRefReferences.count(Entry)) { 801 const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl()); 802 if (FD && !FD->hasAttr<WeakAttr>()) 803 Entry->setLinkage(llvm::Function::ExternalLinkage); 804 805 WeakRefReferences.erase(Entry); 806 } 807 808 if (Entry->getType()->getElementType() == Ty) 809 return Entry; 810 811 // Make sure the result is of the correct type. 812 const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 813 return llvm::ConstantExpr::getBitCast(Entry, PTy); 814 } 815 816 // This function doesn't have a complete type (for example, the return 817 // type is an incomplete struct). Use a fake type instead, and make 818 // sure not to try to set attributes. 819 bool IsIncompleteFunction = false; 820 821 const llvm::FunctionType *FTy; 822 if (isa<llvm::FunctionType>(Ty)) { 823 FTy = cast<llvm::FunctionType>(Ty); 824 } else { 825 FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false); 826 IsIncompleteFunction = true; 827 } 828 829 llvm::Function *F = llvm::Function::Create(FTy, 830 llvm::Function::ExternalLinkage, 831 MangledName, &getModule()); 832 assert(F->getName() == MangledName && "name was uniqued!"); 833 if (D.getDecl()) 834 SetFunctionAttributes(D, F, IsIncompleteFunction); 835 836 // This is the first use or definition of a mangled name. If there is a 837 // deferred decl with this name, remember that we need to emit it at the end 838 // of the file. 839 llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); 840 if (DDI != DeferredDecls.end()) { 841 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 842 // list, and remove it from DeferredDecls (since we don't need it anymore). 843 DeferredDeclsToEmit.push_back(DDI->second); 844 DeferredDecls.erase(DDI); 845 846 // Otherwise, there are cases we have to worry about where we're 847 // using a declaration for which we must emit a definition but where 848 // we might not find a top-level definition: 849 // - member functions defined inline in their classes 850 // - friend functions defined inline in some class 851 // - special member functions with implicit definitions 852 // If we ever change our AST traversal to walk into class methods, 853 // this will be unnecessary. 854 } else if (getLangOptions().CPlusPlus && D.getDecl()) { 855 // Look for a declaration that's lexically in a record. 856 const FunctionDecl *FD = cast<FunctionDecl>(D.getDecl()); 857 do { 858 if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { 859 if (FD->isImplicit()) { 860 assert(FD->isUsed() && "Sema didn't mark implicit function as used!"); 861 DeferredDeclsToEmit.push_back(D); 862 break; 863 } else if (FD->isThisDeclarationADefinition()) { 864 DeferredDeclsToEmit.push_back(D); 865 break; 866 } 867 } 868 FD = FD->getPreviousDeclaration(); 869 } while (FD); 870 } 871 872 // Make sure the result is of the requested type. 873 if (!IsIncompleteFunction) { 874 assert(F->getType()->getElementType() == Ty); 875 return F; 876 } 877 878 const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 879 return llvm::ConstantExpr::getBitCast(F, PTy); 880 } 881 882 /// GetAddrOfFunction - Return the address of the given function. If Ty is 883 /// non-null, then this function will use the specified type if it has to 884 /// create it (this occurs when we see a definition of the function). 885 llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, 886 const llvm::Type *Ty) { 887 // If there was no specific requested type, just convert it now. 888 if (!Ty) 889 Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); 890 891 llvm::StringRef MangledName = getMangledName(GD); 892 return GetOrCreateLLVMFunction(MangledName, Ty, GD); 893 } 894 895 /// CreateRuntimeFunction - Create a new runtime function with the specified 896 /// type and name. 897 llvm::Constant * 898 CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 899 llvm::StringRef Name) { 900 return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl()); 901 } 902 903 static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) { 904 if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType()) 905 return false; 906 if (Context.getLangOptions().CPlusPlus && 907 Context.getBaseElementType(D->getType())->getAs<RecordType>()) { 908 // FIXME: We should do something fancier here! 909 return false; 910 } 911 return true; 912 } 913 914 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 915 /// create and return an llvm GlobalVariable with the specified type. If there 916 /// is something in the module with the specified name, return it potentially 917 /// bitcasted to the right type. 918 /// 919 /// If D is non-null, it specifies a decl that correspond to this. This is used 920 /// to set the attributes on the global when it is first created. 921 llvm::Constant * 922 CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName, 923 const llvm::PointerType *Ty, 924 const VarDecl *D, 925 bool UnnamedAddr) { 926 // Lookup the entry, lazily creating it if necessary. 927 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 928 if (Entry) { 929 if (WeakRefReferences.count(Entry)) { 930 if (D && !D->hasAttr<WeakAttr>()) 931 Entry->setLinkage(llvm::Function::ExternalLinkage); 932 933 WeakRefReferences.erase(Entry); 934 } 935 936 if (UnnamedAddr) 937 Entry->setUnnamedAddr(true); 938 939 if (Entry->getType() == Ty) 940 return Entry; 941 942 // Make sure the result is of the correct type. 943 return llvm::ConstantExpr::getBitCast(Entry, Ty); 944 } 945 946 // This is the first use or definition of a mangled name. If there is a 947 // deferred decl with this name, remember that we need to emit it at the end 948 // of the file. 949 llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); 950 if (DDI != DeferredDecls.end()) { 951 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 952 // list, and remove it from DeferredDecls (since we don't need it anymore). 953 DeferredDeclsToEmit.push_back(DDI->second); 954 DeferredDecls.erase(DDI); 955 } 956 957 llvm::GlobalVariable *GV = 958 new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, 959 llvm::GlobalValue::ExternalLinkage, 960 0, MangledName, 0, 961 false, Ty->getAddressSpace()); 962 963 // Handle things which are present even on external declarations. 964 if (D) { 965 // FIXME: This code is overly simple and should be merged with other global 966 // handling. 967 GV->setConstant(DeclIsConstantGlobal(Context, D)); 968 969 // Set linkage and visibility in case we never see a definition. 970 NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility(); 971 if (LV.linkage() != ExternalLinkage) { 972 GV->setLinkage(llvm::GlobalValue::InternalLinkage); 973 } else { 974 if (D->hasAttr<DLLImportAttr>()) 975 GV->setLinkage(llvm::GlobalValue::DLLImportLinkage); 976 else if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>()) 977 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 978 979 // Set visibility on a declaration only if it's explicit. 980 if (LV.visibilityExplicit()) 981 GV->setVisibility(GetLLVMVisibility(LV.visibility())); 982 } 983 984 GV->setThreadLocal(D->isThreadSpecified()); 985 } 986 987 return GV; 988 } 989 990 991 llvm::GlobalVariable * 992 CodeGenModule::CreateOrReplaceCXXRuntimeVariable(llvm::StringRef Name, 993 const llvm::Type *Ty, 994 llvm::GlobalValue::LinkageTypes Linkage) { 995 llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); 996 llvm::GlobalVariable *OldGV = 0; 997 998 999 if (GV) { 1000 // Check if the variable has the right type. 1001 if (GV->getType()->getElementType() == Ty) 1002 return GV; 1003 1004 // Because C++ name mangling, the only way we can end up with an already 1005 // existing global with the same name is if it has been declared extern "C". 1006 assert(GV->isDeclaration() && "Declaration has wrong type!"); 1007 OldGV = GV; 1008 } 1009 1010 // Create a new variable. 1011 GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, 1012 Linkage, 0, Name); 1013 1014 if (OldGV) { 1015 // Replace occurrences of the old variable if needed. 1016 GV->takeName(OldGV); 1017 1018 if (!OldGV->use_empty()) { 1019 llvm::Constant *NewPtrForOldDecl = 1020 llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 1021 OldGV->replaceAllUsesWith(NewPtrForOldDecl); 1022 } 1023 1024 OldGV->eraseFromParent(); 1025 } 1026 1027 return GV; 1028 } 1029 1030 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 1031 /// given global variable. If Ty is non-null and if the global doesn't exist, 1032 /// then it will be greated with the specified type instead of whatever the 1033 /// normal requested type would be. 1034 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 1035 const llvm::Type *Ty) { 1036 assert(D->hasGlobalStorage() && "Not a global variable"); 1037 QualType ASTTy = D->getType(); 1038 if (Ty == 0) 1039 Ty = getTypes().ConvertTypeForMem(ASTTy); 1040 1041 const llvm::PointerType *PTy = 1042 llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 1043 1044 llvm::StringRef MangledName = getMangledName(D); 1045 return GetOrCreateLLVMGlobal(MangledName, PTy, D); 1046 } 1047 1048 /// CreateRuntimeVariable - Create a new runtime global variable with the 1049 /// specified type and name. 1050 llvm::Constant * 1051 CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 1052 llvm::StringRef Name) { 1053 return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0, 1054 true); 1055 } 1056 1057 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { 1058 assert(!D->getInit() && "Cannot emit definite definitions here!"); 1059 1060 if (MayDeferGeneration(D)) { 1061 // If we have not seen a reference to this variable yet, place it 1062 // into the deferred declarations table to be emitted if needed 1063 // later. 1064 llvm::StringRef MangledName = getMangledName(D); 1065 if (!GetGlobalValue(MangledName)) { 1066 DeferredDecls[MangledName] = D; 1067 return; 1068 } 1069 } 1070 1071 // The tentative definition is the only definition. 1072 EmitGlobalVarDefinition(D); 1073 } 1074 1075 void CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) { 1076 if (DefinitionRequired) 1077 getVTables().GenerateClassData(getVTableLinkage(Class), Class); 1078 } 1079 1080 llvm::GlobalVariable::LinkageTypes 1081 CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { 1082 if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) 1083 return llvm::GlobalVariable::InternalLinkage; 1084 1085 if (const CXXMethodDecl *KeyFunction 1086 = RD->getASTContext().getKeyFunction(RD)) { 1087 // If this class has a key function, use that to determine the linkage of 1088 // the vtable. 1089 const FunctionDecl *Def = 0; 1090 if (KeyFunction->hasBody(Def)) 1091 KeyFunction = cast<CXXMethodDecl>(Def); 1092 1093 switch (KeyFunction->getTemplateSpecializationKind()) { 1094 case TSK_Undeclared: 1095 case TSK_ExplicitSpecialization: 1096 if (KeyFunction->isInlined()) 1097 return llvm::GlobalVariable::LinkOnceODRLinkage; 1098 1099 return llvm::GlobalVariable::ExternalLinkage; 1100 1101 case TSK_ImplicitInstantiation: 1102 return llvm::GlobalVariable::LinkOnceODRLinkage; 1103 1104 case TSK_ExplicitInstantiationDefinition: 1105 return llvm::GlobalVariable::WeakODRLinkage; 1106 1107 case TSK_ExplicitInstantiationDeclaration: 1108 // FIXME: Use available_externally linkage. However, this currently 1109 // breaks LLVM's build due to undefined symbols. 1110 // return llvm::GlobalVariable::AvailableExternallyLinkage; 1111 return llvm::GlobalVariable::LinkOnceODRLinkage; 1112 } 1113 } 1114 1115 switch (RD->getTemplateSpecializationKind()) { 1116 case TSK_Undeclared: 1117 case TSK_ExplicitSpecialization: 1118 case TSK_ImplicitInstantiation: 1119 return llvm::GlobalVariable::LinkOnceODRLinkage; 1120 1121 case TSK_ExplicitInstantiationDefinition: 1122 return llvm::GlobalVariable::WeakODRLinkage; 1123 1124 case TSK_ExplicitInstantiationDeclaration: 1125 // FIXME: Use available_externally linkage. However, this currently 1126 // breaks LLVM's build due to undefined symbols. 1127 // return llvm::GlobalVariable::AvailableExternallyLinkage; 1128 return llvm::GlobalVariable::LinkOnceODRLinkage; 1129 } 1130 1131 // Silence GCC warning. 1132 return llvm::GlobalVariable::LinkOnceODRLinkage; 1133 } 1134 1135 CharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const { 1136 return Context.toCharUnitsFromBits( 1137 TheTargetData.getTypeStoreSizeInBits(Ty)); 1138 } 1139 1140 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 1141 llvm::Constant *Init = 0; 1142 QualType ASTTy = D->getType(); 1143 bool NonConstInit = false; 1144 1145 const Expr *InitExpr = D->getAnyInitializer(); 1146 1147 if (!InitExpr) { 1148 // This is a tentative definition; tentative definitions are 1149 // implicitly initialized with { 0 }. 1150 // 1151 // Note that tentative definitions are only emitted at the end of 1152 // a translation unit, so they should never have incomplete 1153 // type. In addition, EmitTentativeDefinition makes sure that we 1154 // never attempt to emit a tentative definition if a real one 1155 // exists. A use may still exists, however, so we still may need 1156 // to do a RAUW. 1157 assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); 1158 Init = EmitNullConstant(D->getType()); 1159 } else { 1160 Init = EmitConstantExpr(InitExpr, D->getType()); 1161 if (!Init) { 1162 QualType T = InitExpr->getType(); 1163 if (D->getType()->isReferenceType()) 1164 T = D->getType(); 1165 1166 if (getLangOptions().CPlusPlus) { 1167 Init = EmitNullConstant(T); 1168 NonConstInit = true; 1169 } else { 1170 ErrorUnsupported(D, "static initializer"); 1171 Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 1172 } 1173 } else { 1174 // We don't need an initializer, so remove the entry for the delayed 1175 // initializer position (just in case this entry was delayed). 1176 if (getLangOptions().CPlusPlus) 1177 DelayedCXXInitPosition.erase(D); 1178 } 1179 } 1180 1181 const llvm::Type* InitType = Init->getType(); 1182 llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); 1183 1184 // Strip off a bitcast if we got one back. 1185 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 1186 assert(CE->getOpcode() == llvm::Instruction::BitCast || 1187 // all zero index gep. 1188 CE->getOpcode() == llvm::Instruction::GetElementPtr); 1189 Entry = CE->getOperand(0); 1190 } 1191 1192 // Entry is now either a Function or GlobalVariable. 1193 llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); 1194 1195 // We have a definition after a declaration with the wrong type. 1196 // We must make a new GlobalVariable* and update everything that used OldGV 1197 // (a declaration or tentative definition) with the new GlobalVariable* 1198 // (which will be a definition). 1199 // 1200 // This happens if there is a prototype for a global (e.g. 1201 // "extern int x[];") and then a definition of a different type (e.g. 1202 // "int x[10];"). This also happens when an initializer has a different type 1203 // from the type of the global (this happens with unions). 1204 if (GV == 0 || 1205 GV->getType()->getElementType() != InitType || 1206 GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { 1207 1208 // Move the old entry aside so that we'll create a new one. 1209 Entry->setName(llvm::StringRef()); 1210 1211 // Make a new global with the correct type, this is now guaranteed to work. 1212 GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); 1213 1214 // Replace all uses of the old global with the new global 1215 llvm::Constant *NewPtrForOldDecl = 1216 llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 1217 Entry->replaceAllUsesWith(NewPtrForOldDecl); 1218 1219 // Erase the old global, since it is no longer used. 1220 cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 1221 } 1222 1223 if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 1224 SourceManager &SM = Context.getSourceManager(); 1225 AddAnnotation(EmitAnnotateAttr(GV, AA, 1226 SM.getInstantiationLineNumber(D->getLocation()))); 1227 } 1228 1229 GV->setInitializer(Init); 1230 1231 // If it is safe to mark the global 'constant', do so now. 1232 GV->setConstant(false); 1233 if (!NonConstInit && DeclIsConstantGlobal(Context, D)) 1234 GV->setConstant(true); 1235 1236 GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); 1237 1238 // Set the llvm linkage type as appropriate. 1239 llvm::GlobalValue::LinkageTypes Linkage = 1240 GetLLVMLinkageVarDefinition(D, GV); 1241 GV->setLinkage(Linkage); 1242 if (Linkage == llvm::GlobalVariable::CommonLinkage) 1243 // common vars aren't constant even if declared const. 1244 GV->setConstant(false); 1245 1246 SetCommonAttributes(D, GV); 1247 1248 // Emit the initializer function if necessary. 1249 if (NonConstInit) 1250 EmitCXXGlobalVarDeclInitFunc(D, GV); 1251 1252 // Emit global variable debug information. 1253 if (CGDebugInfo *DI = getDebugInfo()) { 1254 DI->setLocation(D->getLocation()); 1255 DI->EmitGlobalVariable(GV, D); 1256 } 1257 } 1258 1259 llvm::GlobalValue::LinkageTypes 1260 CodeGenModule::GetLLVMLinkageVarDefinition(const VarDecl *D, 1261 llvm::GlobalVariable *GV) { 1262 GVALinkage Linkage = getContext().GetGVALinkageForVariable(D); 1263 if (Linkage == GVA_Internal) 1264 return llvm::Function::InternalLinkage; 1265 else if (D->hasAttr<DLLImportAttr>()) 1266 return llvm::Function::DLLImportLinkage; 1267 else if (D->hasAttr<DLLExportAttr>()) 1268 return llvm::Function::DLLExportLinkage; 1269 else if (D->hasAttr<WeakAttr>()) { 1270 if (GV->isConstant()) 1271 return llvm::GlobalVariable::WeakODRLinkage; 1272 else 1273 return llvm::GlobalVariable::WeakAnyLinkage; 1274 } else if (Linkage == GVA_TemplateInstantiation || 1275 Linkage == GVA_ExplicitTemplateInstantiation) 1276 // FIXME: It seems like we can provide more specific linkage here 1277 // (LinkOnceODR, WeakODR). 1278 return llvm::GlobalVariable::WeakAnyLinkage; 1279 else if (!getLangOptions().CPlusPlus && 1280 ((!CodeGenOpts.NoCommon && !D->getAttr<NoCommonAttr>()) || 1281 D->getAttr<CommonAttr>()) && 1282 !D->hasExternalStorage() && !D->getInit() && 1283 !D->getAttr<SectionAttr>() && !D->isThreadSpecified()) { 1284 // Thread local vars aren't considered common linkage. 1285 return llvm::GlobalVariable::CommonLinkage; 1286 } 1287 return llvm::GlobalVariable::ExternalLinkage; 1288 } 1289 1290 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we 1291 /// implement a function with no prototype, e.g. "int foo() {}". If there are 1292 /// existing call uses of the old function in the module, this adjusts them to 1293 /// call the new function directly. 1294 /// 1295 /// This is not just a cleanup: the always_inline pass requires direct calls to 1296 /// functions to be able to inline them. If there is a bitcast in the way, it 1297 /// won't inline them. Instcombine normally deletes these calls, but it isn't 1298 /// run at -O0. 1299 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 1300 llvm::Function *NewFn) { 1301 // If we're redefining a global as a function, don't transform it. 1302 llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); 1303 if (OldFn == 0) return; 1304 1305 const llvm::Type *NewRetTy = NewFn->getReturnType(); 1306 llvm::SmallVector<llvm::Value*, 4> ArgList; 1307 1308 for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); 1309 UI != E; ) { 1310 // TODO: Do invokes ever occur in C code? If so, we should handle them too. 1311 llvm::Value::use_iterator I = UI++; // Increment before the CI is erased. 1312 llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*I); 1313 if (!CI) continue; // FIXME: when we allow Invoke, just do CallSite CS(*I) 1314 llvm::CallSite CS(CI); 1315 if (!CI || !CS.isCallee(I)) continue; 1316 1317 // If the return types don't match exactly, and if the call isn't dead, then 1318 // we can't transform this call. 1319 if (CI->getType() != NewRetTy && !CI->use_empty()) 1320 continue; 1321 1322 // If the function was passed too few arguments, don't transform. If extra 1323 // arguments were passed, we silently drop them. If any of the types 1324 // mismatch, we don't transform. 1325 unsigned ArgNo = 0; 1326 bool DontTransform = false; 1327 for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), 1328 E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { 1329 if (CS.arg_size() == ArgNo || 1330 CS.getArgument(ArgNo)->getType() != AI->getType()) { 1331 DontTransform = true; 1332 break; 1333 } 1334 } 1335 if (DontTransform) 1336 continue; 1337 1338 // Okay, we can transform this. Create the new call instruction and copy 1339 // over the required information. 1340 ArgList.append(CS.arg_begin(), CS.arg_begin() + ArgNo); 1341 llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(), 1342 ArgList.end(), "", CI); 1343 ArgList.clear(); 1344 if (!NewCall->getType()->isVoidTy()) 1345 NewCall->takeName(CI); 1346 NewCall->setAttributes(CI->getAttributes()); 1347 NewCall->setCallingConv(CI->getCallingConv()); 1348 1349 // Finally, remove the old call, replacing any uses with the new one. 1350 if (!CI->use_empty()) 1351 CI->replaceAllUsesWith(NewCall); 1352 1353 // Copy debug location attached to CI. 1354 if (!CI->getDebugLoc().isUnknown()) 1355 NewCall->setDebugLoc(CI->getDebugLoc()); 1356 CI->eraseFromParent(); 1357 } 1358 } 1359 1360 1361 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { 1362 const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); 1363 const llvm::FunctionType *Ty = getTypes().GetFunctionType(GD); 1364 // Get or create the prototype for the function. 1365 llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); 1366 1367 // Strip off a bitcast if we got one back. 1368 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 1369 assert(CE->getOpcode() == llvm::Instruction::BitCast); 1370 Entry = CE->getOperand(0); 1371 } 1372 1373 1374 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { 1375 llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); 1376 1377 // If the types mismatch then we have to rewrite the definition. 1378 assert(OldFn->isDeclaration() && 1379 "Shouldn't replace non-declaration"); 1380 1381 // F is the Function* for the one with the wrong type, we must make a new 1382 // Function* and update everything that used F (a declaration) with the new 1383 // Function* (which will be a definition). 1384 // 1385 // This happens if there is a prototype for a function 1386 // (e.g. "int f()") and then a definition of a different type 1387 // (e.g. "int f(int x)"). Move the old function aside so that it 1388 // doesn't interfere with GetAddrOfFunction. 1389 OldFn->setName(llvm::StringRef()); 1390 llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); 1391 1392 // If this is an implementation of a function without a prototype, try to 1393 // replace any existing uses of the function (which may be calls) with uses 1394 // of the new function 1395 if (D->getType()->isFunctionNoProtoType()) { 1396 ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); 1397 OldFn->removeDeadConstantUsers(); 1398 } 1399 1400 // Replace uses of F with the Function we will endow with a body. 1401 if (!Entry->use_empty()) { 1402 llvm::Constant *NewPtrForOldDecl = 1403 llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 1404 Entry->replaceAllUsesWith(NewPtrForOldDecl); 1405 } 1406 1407 // Ok, delete the old function now, which is dead. 1408 OldFn->eraseFromParent(); 1409 1410 Entry = NewFn; 1411 } 1412 1413 // We need to set linkage and visibility on the function before 1414 // generating code for it because various parts of IR generation 1415 // want to propagate this information down (e.g. to local static 1416 // declarations). 1417 llvm::Function *Fn = cast<llvm::Function>(Entry); 1418 setFunctionLinkage(D, Fn); 1419 1420 // FIXME: this is redundant with part of SetFunctionDefinitionAttributes 1421 setGlobalVisibility(Fn, D); 1422 1423 CodeGenFunction(*this).GenerateCode(D, Fn); 1424 1425 SetFunctionDefinitionAttributes(D, Fn); 1426 SetLLVMFunctionAttributesForDefinition(D, Fn); 1427 1428 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 1429 AddGlobalCtor(Fn, CA->getPriority()); 1430 if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 1431 AddGlobalDtor(Fn, DA->getPriority()); 1432 } 1433 1434 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { 1435 const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 1436 const AliasAttr *AA = D->getAttr<AliasAttr>(); 1437 assert(AA && "Not an alias?"); 1438 1439 llvm::StringRef MangledName = getMangledName(GD); 1440 1441 // If there is a definition in the module, then it wins over the alias. 1442 // This is dubious, but allow it to be safe. Just ignore the alias. 1443 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 1444 if (Entry && !Entry->isDeclaration()) 1445 return; 1446 1447 const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 1448 1449 // Create a reference to the named value. This ensures that it is emitted 1450 // if a deferred decl. 1451 llvm::Constant *Aliasee; 1452 if (isa<llvm::FunctionType>(DeclTy)) 1453 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl()); 1454 else 1455 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), 1456 llvm::PointerType::getUnqual(DeclTy), 0); 1457 1458 // Create the new alias itself, but don't set a name yet. 1459 llvm::GlobalValue *GA = 1460 new llvm::GlobalAlias(Aliasee->getType(), 1461 llvm::Function::ExternalLinkage, 1462 "", Aliasee, &getModule()); 1463 1464 if (Entry) { 1465 assert(Entry->isDeclaration()); 1466 1467 // If there is a declaration in the module, then we had an extern followed 1468 // by the alias, as in: 1469 // extern int test6(); 1470 // ... 1471 // int test6() __attribute__((alias("test7"))); 1472 // 1473 // Remove it and replace uses of it with the alias. 1474 GA->takeName(Entry); 1475 1476 Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 1477 Entry->getType())); 1478 Entry->eraseFromParent(); 1479 } else { 1480 GA->setName(MangledName); 1481 } 1482 1483 // Set attributes which are particular to an alias; this is a 1484 // specialization of the attributes which may be set on a global 1485 // variable/function. 1486 if (D->hasAttr<DLLExportAttr>()) { 1487 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1488 // The dllexport attribute is ignored for undefined symbols. 1489 if (FD->hasBody()) 1490 GA->setLinkage(llvm::Function::DLLExportLinkage); 1491 } else { 1492 GA->setLinkage(llvm::Function::DLLExportLinkage); 1493 } 1494 } else if (D->hasAttr<WeakAttr>() || 1495 D->hasAttr<WeakRefAttr>() || 1496 D->hasAttr<WeakImportAttr>()) { 1497 GA->setLinkage(llvm::Function::WeakAnyLinkage); 1498 } 1499 1500 SetCommonAttributes(D, GA); 1501 } 1502 1503 /// getBuiltinLibFunction - Given a builtin id for a function like 1504 /// "__builtin_fabsf", return a Function* for "fabsf". 1505 llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, 1506 unsigned BuiltinID) { 1507 assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 1508 Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 1509 "isn't a lib fn"); 1510 1511 // Get the name, skip over the __builtin_ prefix (if necessary). 1512 const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 1513 if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 1514 Name += 10; 1515 1516 const llvm::FunctionType *Ty = 1517 cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType())); 1518 1519 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl(FD)); 1520 } 1521 1522 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 1523 unsigned NumTys) { 1524 return llvm::Intrinsic::getDeclaration(&getModule(), 1525 (llvm::Intrinsic::ID)IID, Tys, NumTys); 1526 } 1527 1528 static llvm::StringMapEntry<llvm::Constant*> & 1529 GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, 1530 const StringLiteral *Literal, 1531 bool TargetIsLSB, 1532 bool &IsUTF16, 1533 unsigned &StringLength) { 1534 llvm::StringRef String = Literal->getString(); 1535 unsigned NumBytes = String.size(); 1536 1537 // Check for simple case. 1538 if (!Literal->containsNonAsciiOrNull()) { 1539 StringLength = NumBytes; 1540 return Map.GetOrCreateValue(String); 1541 } 1542 1543 // Otherwise, convert the UTF8 literals into a byte string. 1544 llvm::SmallVector<UTF16, 128> ToBuf(NumBytes); 1545 const UTF8 *FromPtr = (UTF8 *)String.data(); 1546 UTF16 *ToPtr = &ToBuf[0]; 1547 1548 (void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, 1549 &ToPtr, ToPtr + NumBytes, 1550 strictConversion); 1551 1552 // ConvertUTF8toUTF16 returns the length in ToPtr. 1553 StringLength = ToPtr - &ToBuf[0]; 1554 1555 // Render the UTF-16 string into a byte array and convert to the target byte 1556 // order. 1557 // 1558 // FIXME: This isn't something we should need to do here. 1559 llvm::SmallString<128> AsBytes; 1560 AsBytes.reserve(StringLength * 2); 1561 for (unsigned i = 0; i != StringLength; ++i) { 1562 unsigned short Val = ToBuf[i]; 1563 if (TargetIsLSB) { 1564 AsBytes.push_back(Val & 0xFF); 1565 AsBytes.push_back(Val >> 8); 1566 } else { 1567 AsBytes.push_back(Val >> 8); 1568 AsBytes.push_back(Val & 0xFF); 1569 } 1570 } 1571 // Append one extra null character, the second is automatically added by our 1572 // caller. 1573 AsBytes.push_back(0); 1574 1575 IsUTF16 = true; 1576 return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size())); 1577 } 1578 1579 llvm::Constant * 1580 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { 1581 unsigned StringLength = 0; 1582 bool isUTF16 = false; 1583 llvm::StringMapEntry<llvm::Constant*> &Entry = 1584 GetConstantCFStringEntry(CFConstantStringMap, Literal, 1585 getTargetData().isLittleEndian(), 1586 isUTF16, StringLength); 1587 1588 if (llvm::Constant *C = Entry.getValue()) 1589 return C; 1590 1591 llvm::Constant *Zero = 1592 llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 1593 llvm::Constant *Zeros[] = { Zero, Zero }; 1594 1595 // If we don't already have it, get __CFConstantStringClassReference. 1596 if (!CFConstantStringClassRef) { 1597 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 1598 Ty = llvm::ArrayType::get(Ty, 0); 1599 llvm::Constant *GV = CreateRuntimeVariable(Ty, 1600 "__CFConstantStringClassReference"); 1601 // Decay array -> ptr 1602 CFConstantStringClassRef = 1603 llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1604 } 1605 1606 QualType CFTy = getContext().getCFConstantStringType(); 1607 1608 const llvm::StructType *STy = 1609 cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1610 1611 std::vector<llvm::Constant*> Fields(4); 1612 1613 // Class pointer. 1614 Fields[0] = CFConstantStringClassRef; 1615 1616 // Flags. 1617 const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 1618 Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : 1619 llvm::ConstantInt::get(Ty, 0x07C8); 1620 1621 // String pointer. 1622 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 1623 1624 llvm::GlobalValue::LinkageTypes Linkage; 1625 bool isConstant; 1626 if (isUTF16) { 1627 // FIXME: why do utf strings get "_" labels instead of "L" labels? 1628 Linkage = llvm::GlobalValue::InternalLinkage; 1629 // Note: -fwritable-strings doesn't make unicode CFStrings writable, but 1630 // does make plain ascii ones writable. 1631 isConstant = true; 1632 } else { 1633 Linkage = llvm::GlobalValue::PrivateLinkage; 1634 isConstant = !Features.WritableStrings; 1635 } 1636 1637 llvm::GlobalVariable *GV = 1638 new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 1639 ".str"); 1640 GV->setUnnamedAddr(true); 1641 if (isUTF16) { 1642 CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); 1643 GV->setAlignment(Align.getQuantity()); 1644 } 1645 Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1646 1647 // String length. 1648 Ty = getTypes().ConvertType(getContext().LongTy); 1649 Fields[3] = llvm::ConstantInt::get(Ty, StringLength); 1650 1651 // The struct. 1652 C = llvm::ConstantStruct::get(STy, Fields); 1653 GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 1654 llvm::GlobalVariable::PrivateLinkage, C, 1655 "_unnamed_cfstring_"); 1656 if (const char *Sect = getContext().Target.getCFStringSection()) 1657 GV->setSection(Sect); 1658 Entry.setValue(GV); 1659 1660 return GV; 1661 } 1662 1663 llvm::Constant * 1664 CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) { 1665 unsigned StringLength = 0; 1666 bool isUTF16 = false; 1667 llvm::StringMapEntry<llvm::Constant*> &Entry = 1668 GetConstantCFStringEntry(CFConstantStringMap, Literal, 1669 getTargetData().isLittleEndian(), 1670 isUTF16, StringLength); 1671 1672 if (llvm::Constant *C = Entry.getValue()) 1673 return C; 1674 1675 llvm::Constant *Zero = 1676 llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 1677 llvm::Constant *Zeros[] = { Zero, Zero }; 1678 1679 // If we don't already have it, get _NSConstantStringClassReference. 1680 if (!ConstantStringClassRef) { 1681 std::string StringClass(getLangOptions().ObjCConstantStringClass); 1682 const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 1683 Ty = llvm::ArrayType::get(Ty, 0); 1684 llvm::Constant *GV; 1685 if (StringClass.empty()) 1686 GV = CreateRuntimeVariable(Ty, 1687 Features.ObjCNonFragileABI ? 1688 "OBJC_CLASS_$_NSConstantString" : 1689 "_NSConstantStringClassReference"); 1690 else { 1691 std::string str; 1692 if (Features.ObjCNonFragileABI) 1693 str = "OBJC_CLASS_$_" + StringClass; 1694 else 1695 str = "_" + StringClass + "ClassReference"; 1696 GV = CreateRuntimeVariable(Ty, str); 1697 } 1698 // Decay array -> ptr 1699 ConstantStringClassRef = 1700 llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1701 } 1702 1703 QualType NSTy = getContext().getNSConstantStringType(); 1704 1705 const llvm::StructType *STy = 1706 cast<llvm::StructType>(getTypes().ConvertType(NSTy)); 1707 1708 std::vector<llvm::Constant*> Fields(3); 1709 1710 // Class pointer. 1711 Fields[0] = ConstantStringClassRef; 1712 1713 // String pointer. 1714 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 1715 1716 llvm::GlobalValue::LinkageTypes Linkage; 1717 bool isConstant; 1718 if (isUTF16) { 1719 // FIXME: why do utf strings get "_" labels instead of "L" labels? 1720 Linkage = llvm::GlobalValue::InternalLinkage; 1721 // Note: -fwritable-strings doesn't make unicode NSStrings writable, but 1722 // does make plain ascii ones writable. 1723 isConstant = true; 1724 } else { 1725 Linkage = llvm::GlobalValue::PrivateLinkage; 1726 isConstant = !Features.WritableStrings; 1727 } 1728 1729 llvm::GlobalVariable *GV = 1730 new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 1731 ".str"); 1732 GV->setUnnamedAddr(true); 1733 if (isUTF16) { 1734 CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); 1735 GV->setAlignment(Align.getQuantity()); 1736 } 1737 Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1738 1739 // String length. 1740 const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 1741 Fields[2] = llvm::ConstantInt::get(Ty, StringLength); 1742 1743 // The struct. 1744 C = llvm::ConstantStruct::get(STy, Fields); 1745 GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 1746 llvm::GlobalVariable::PrivateLinkage, C, 1747 "_unnamed_nsstring_"); 1748 // FIXME. Fix section. 1749 if (const char *Sect = 1750 Features.ObjCNonFragileABI 1751 ? getContext().Target.getNSStringNonFragileABISection() 1752 : getContext().Target.getNSStringSection()) 1753 GV->setSection(Sect); 1754 Entry.setValue(GV); 1755 1756 return GV; 1757 } 1758 1759 /// GetStringForStringLiteral - Return the appropriate bytes for a 1760 /// string literal, properly padded to match the literal type. 1761 std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 1762 const ASTContext &Context = getContext(); 1763 const ConstantArrayType *CAT = 1764 Context.getAsConstantArrayType(E->getType()); 1765 assert(CAT && "String isn't pointer or array!"); 1766 1767 // Resize the string to the right size. 1768 uint64_t RealLen = CAT->getSize().getZExtValue(); 1769 1770 if (E->isWide()) 1771 RealLen *= Context.Target.getWCharWidth() / Context.getCharWidth(); 1772 1773 std::string Str = E->getString().str(); 1774 Str.resize(RealLen, '\0'); 1775 1776 return Str; 1777 } 1778 1779 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a 1780 /// constant array for the given string literal. 1781 llvm::Constant * 1782 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 1783 // FIXME: This can be more efficient. 1784 // FIXME: We shouldn't need to bitcast the constant in the wide string case. 1785 llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S)); 1786 if (S->isWide()) { 1787 llvm::Type *DestTy = 1788 llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType())); 1789 C = llvm::ConstantExpr::getBitCast(C, DestTy); 1790 } 1791 return C; 1792 } 1793 1794 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1795 /// array for the given ObjCEncodeExpr node. 1796 llvm::Constant * 1797 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1798 std::string Str; 1799 getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1800 1801 return GetAddrOfConstantCString(Str); 1802 } 1803 1804 1805 /// GenerateWritableString -- Creates storage for a string literal. 1806 static llvm::Constant *GenerateStringLiteral(const std::string &str, 1807 bool constant, 1808 CodeGenModule &CGM, 1809 const char *GlobalName) { 1810 // Create Constant for this string literal. Don't add a '\0'. 1811 llvm::Constant *C = 1812 llvm::ConstantArray::get(CGM.getLLVMContext(), str, false); 1813 1814 // Create a global variable for this string 1815 llvm::GlobalVariable *GV = 1816 new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, 1817 llvm::GlobalValue::PrivateLinkage, 1818 C, GlobalName); 1819 GV->setUnnamedAddr(true); 1820 return GV; 1821 } 1822 1823 /// GetAddrOfConstantString - Returns a pointer to a character array 1824 /// containing the literal. This contents are exactly that of the 1825 /// given string, i.e. it will not be null terminated automatically; 1826 /// see GetAddrOfConstantCString. Note that whether the result is 1827 /// actually a pointer to an LLVM constant depends on 1828 /// Feature.WriteableStrings. 1829 /// 1830 /// The result has pointer to array type. 1831 llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 1832 const char *GlobalName) { 1833 bool IsConstant = !Features.WritableStrings; 1834 1835 // Get the default prefix if a name wasn't specified. 1836 if (!GlobalName) 1837 GlobalName = ".str"; 1838 1839 // Don't share any string literals if strings aren't constant. 1840 if (!IsConstant) 1841 return GenerateStringLiteral(str, false, *this, GlobalName); 1842 1843 llvm::StringMapEntry<llvm::Constant *> &Entry = 1844 ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 1845 1846 if (Entry.getValue()) 1847 return Entry.getValue(); 1848 1849 // Create a global variable for this. 1850 llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 1851 Entry.setValue(C); 1852 return C; 1853 } 1854 1855 /// GetAddrOfConstantCString - Returns a pointer to a character 1856 /// array containing the literal and a terminating '\-' 1857 /// character. The result has pointer to array type. 1858 llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 1859 const char *GlobalName){ 1860 return GetAddrOfConstantString(str + '\0', GlobalName); 1861 } 1862 1863 /// EmitObjCPropertyImplementations - Emit information for synthesized 1864 /// properties for an implementation. 1865 void CodeGenModule::EmitObjCPropertyImplementations(const 1866 ObjCImplementationDecl *D) { 1867 for (ObjCImplementationDecl::propimpl_iterator 1868 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { 1869 ObjCPropertyImplDecl *PID = *i; 1870 1871 // Dynamic is just for type-checking. 1872 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1873 ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1874 1875 // Determine which methods need to be implemented, some may have 1876 // been overridden. Note that ::isSynthesized is not the method 1877 // we want, that just indicates if the decl came from a 1878 // property. What we want to know is if the method is defined in 1879 // this implementation. 1880 if (!D->getInstanceMethod(PD->getGetterName())) 1881 CodeGenFunction(*this).GenerateObjCGetter( 1882 const_cast<ObjCImplementationDecl *>(D), PID); 1883 if (!PD->isReadOnly() && 1884 !D->getInstanceMethod(PD->getSetterName())) 1885 CodeGenFunction(*this).GenerateObjCSetter( 1886 const_cast<ObjCImplementationDecl *>(D), PID); 1887 } 1888 } 1889 } 1890 1891 /// EmitObjCIvarInitializations - Emit information for ivar initialization 1892 /// for an implementation. 1893 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { 1894 if (!Features.NeXTRuntime || D->getNumIvarInitializers() == 0) 1895 return; 1896 DeclContext* DC = const_cast<DeclContext*>(dyn_cast<DeclContext>(D)); 1897 assert(DC && "EmitObjCIvarInitializations - null DeclContext"); 1898 IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); 1899 Selector cxxSelector = getContext().Selectors.getSelector(0, &II); 1900 ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create(getContext(), 1901 D->getLocation(), 1902 D->getLocation(), cxxSelector, 1903 getContext().VoidTy, 0, 1904 DC, true, false, true, false, 1905 ObjCMethodDecl::Required); 1906 D->addInstanceMethod(DTORMethod); 1907 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); 1908 1909 II = &getContext().Idents.get(".cxx_construct"); 1910 cxxSelector = getContext().Selectors.getSelector(0, &II); 1911 // The constructor returns 'self'. 1912 ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), 1913 D->getLocation(), 1914 D->getLocation(), cxxSelector, 1915 getContext().getObjCIdType(), 0, 1916 DC, true, false, true, false, 1917 ObjCMethodDecl::Required); 1918 D->addInstanceMethod(CTORMethod); 1919 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); 1920 1921 1922 } 1923 1924 /// EmitNamespace - Emit all declarations in a namespace. 1925 void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { 1926 for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); 1927 I != E; ++I) 1928 EmitTopLevelDecl(*I); 1929 } 1930 1931 // EmitLinkageSpec - Emit all declarations in a linkage spec. 1932 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 1933 if (LSD->getLanguage() != LinkageSpecDecl::lang_c && 1934 LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { 1935 ErrorUnsupported(LSD, "linkage spec"); 1936 return; 1937 } 1938 1939 for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); 1940 I != E; ++I) 1941 EmitTopLevelDecl(*I); 1942 } 1943 1944 /// EmitTopLevelDecl - Emit code for a single top level declaration. 1945 void CodeGenModule::EmitTopLevelDecl(Decl *D) { 1946 // If an error has occurred, stop code generation, but continue 1947 // parsing and semantic analysis (to ensure all warnings and errors 1948 // are emitted). 1949 if (Diags.hasErrorOccurred()) 1950 return; 1951 1952 // Ignore dependent declarations. 1953 if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) 1954 return; 1955 1956 switch (D->getKind()) { 1957 case Decl::CXXConversion: 1958 case Decl::CXXMethod: 1959 case Decl::Function: 1960 // Skip function templates 1961 if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 1962 return; 1963 1964 EmitGlobal(cast<FunctionDecl>(D)); 1965 break; 1966 1967 case Decl::Var: 1968 EmitGlobal(cast<VarDecl>(D)); 1969 break; 1970 1971 // C++ Decls 1972 case Decl::Namespace: 1973 EmitNamespace(cast<NamespaceDecl>(D)); 1974 break; 1975 // No code generation needed. 1976 case Decl::UsingShadow: 1977 case Decl::Using: 1978 case Decl::UsingDirective: 1979 case Decl::ClassTemplate: 1980 case Decl::FunctionTemplate: 1981 case Decl::NamespaceAlias: 1982 break; 1983 case Decl::CXXConstructor: 1984 // Skip function templates 1985 if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 1986 return; 1987 1988 EmitCXXConstructors(cast<CXXConstructorDecl>(D)); 1989 break; 1990 case Decl::CXXDestructor: 1991 EmitCXXDestructors(cast<CXXDestructorDecl>(D)); 1992 break; 1993 1994 case Decl::StaticAssert: 1995 // Nothing to do. 1996 break; 1997 1998 // Objective-C Decls 1999 2000 // Forward declarations, no (immediate) code generation. 2001 case Decl::ObjCClass: 2002 case Decl::ObjCForwardProtocol: 2003 case Decl::ObjCInterface: 2004 break; 2005 2006 case Decl::ObjCCategory: { 2007 ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); 2008 if (CD->IsClassExtension() && CD->hasSynthBitfield()) 2009 Context.ResetObjCLayout(CD->getClassInterface()); 2010 break; 2011 } 2012 2013 2014 case Decl::ObjCProtocol: 2015 Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 2016 break; 2017 2018 case Decl::ObjCCategoryImpl: 2019 // Categories have properties but don't support synthesize so we 2020 // can ignore them here. 2021 Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 2022 break; 2023 2024 case Decl::ObjCImplementation: { 2025 ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 2026 if (Features.ObjCNonFragileABI2 && OMD->hasSynthBitfield()) 2027 Context.ResetObjCLayout(OMD->getClassInterface()); 2028 EmitObjCPropertyImplementations(OMD); 2029 EmitObjCIvarInitializations(OMD); 2030 Runtime->GenerateClass(OMD); 2031 break; 2032 } 2033 case Decl::ObjCMethod: { 2034 ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 2035 // If this is not a prototype, emit the body. 2036 if (OMD->getBody()) 2037 CodeGenFunction(*this).GenerateObjCMethod(OMD); 2038 break; 2039 } 2040 case Decl::ObjCCompatibleAlias: 2041 // compatibility-alias is a directive and has no code gen. 2042 break; 2043 2044 case Decl::LinkageSpec: 2045 EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 2046 break; 2047 2048 case Decl::FileScopeAsm: { 2049 FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 2050 llvm::StringRef AsmString = AD->getAsmString()->getString(); 2051 2052 const std::string &S = getModule().getModuleInlineAsm(); 2053 if (S.empty()) 2054 getModule().setModuleInlineAsm(AsmString); 2055 else 2056 getModule().setModuleInlineAsm(S + '\n' + AsmString.str()); 2057 break; 2058 } 2059 2060 default: 2061 // Make sure we handled everything we should, every other kind is a 2062 // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind 2063 // function. Need to recode Decl::Kind to do that easily. 2064 assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 2065 } 2066 } 2067 2068 /// Turns the given pointer into a constant. 2069 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, 2070 const void *Ptr) { 2071 uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); 2072 const llvm::Type *i64 = llvm::Type::getInt64Ty(Context); 2073 return llvm::ConstantInt::get(i64, PtrInt); 2074 } 2075 2076 static void EmitGlobalDeclMetadata(CodeGenModule &CGM, 2077 llvm::NamedMDNode *&GlobalMetadata, 2078 GlobalDecl D, 2079 llvm::GlobalValue *Addr) { 2080 if (!GlobalMetadata) 2081 GlobalMetadata = 2082 CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); 2083 2084 // TODO: should we report variant information for ctors/dtors? 2085 llvm::Value *Ops[] = { 2086 Addr, 2087 GetPointerConstant(CGM.getLLVMContext(), D.getDecl()) 2088 }; 2089 GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops, 2)); 2090 } 2091 2092 /// Emits metadata nodes associating all the global values in the 2093 /// current module with the Decls they came from. This is useful for 2094 /// projects using IR gen as a subroutine. 2095 /// 2096 /// Since there's currently no way to associate an MDNode directly 2097 /// with an llvm::GlobalValue, we create a global named metadata 2098 /// with the name 'clang.global.decl.ptrs'. 2099 void CodeGenModule::EmitDeclMetadata() { 2100 llvm::NamedMDNode *GlobalMetadata = 0; 2101 2102 // StaticLocalDeclMap 2103 for (llvm::DenseMap<GlobalDecl,llvm::StringRef>::iterator 2104 I = MangledDeclNames.begin(), E = MangledDeclNames.end(); 2105 I != E; ++I) { 2106 llvm::GlobalValue *Addr = getModule().getNamedValue(I->second); 2107 EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr); 2108 } 2109 } 2110 2111 /// Emits metadata nodes for all the local variables in the current 2112 /// function. 2113 void CodeGenFunction::EmitDeclMetadata() { 2114 if (LocalDeclMap.empty()) return; 2115 2116 llvm::LLVMContext &Context = getLLVMContext(); 2117 2118 // Find the unique metadata ID for this name. 2119 unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); 2120 2121 llvm::NamedMDNode *GlobalMetadata = 0; 2122 2123 for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator 2124 I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) { 2125 const Decl *D = I->first; 2126 llvm::Value *Addr = I->second; 2127 2128 if (llvm::AllocaInst *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { 2129 llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); 2130 Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, &DAddr, 1)); 2131 } else if (llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Addr)) { 2132 GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); 2133 EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); 2134 } 2135 } 2136 } 2137 2138 ///@name Custom Runtime Function Interfaces 2139 ///@{ 2140 // 2141 // FIXME: These can be eliminated once we can have clients just get the required 2142 // AST nodes from the builtin tables. 2143 2144 llvm::Constant *CodeGenModule::getBlockObjectDispose() { 2145 if (BlockObjectDispose) 2146 return BlockObjectDispose; 2147 2148 // If we saw an explicit decl, use that. 2149 if (BlockObjectDisposeDecl) { 2150 return BlockObjectDispose = GetAddrOfFunction( 2151 BlockObjectDisposeDecl, 2152 getTypes().GetFunctionType(BlockObjectDisposeDecl)); 2153 } 2154 2155 // Otherwise construct the function by hand. 2156 const llvm::FunctionType *FTy; 2157 std::vector<const llvm::Type*> ArgTys; 2158 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 2159 ArgTys.push_back(PtrToInt8Ty); 2160 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 2161 FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 2162 return BlockObjectDispose = 2163 CreateRuntimeFunction(FTy, "_Block_object_dispose"); 2164 } 2165 2166 llvm::Constant *CodeGenModule::getBlockObjectAssign() { 2167 if (BlockObjectAssign) 2168 return BlockObjectAssign; 2169 2170 // If we saw an explicit decl, use that. 2171 if (BlockObjectAssignDecl) { 2172 return BlockObjectAssign = GetAddrOfFunction( 2173 BlockObjectAssignDecl, 2174 getTypes().GetFunctionType(BlockObjectAssignDecl)); 2175 } 2176 2177 // Otherwise construct the function by hand. 2178 const llvm::FunctionType *FTy; 2179 std::vector<const llvm::Type*> ArgTys; 2180 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext); 2181 ArgTys.push_back(PtrToInt8Ty); 2182 ArgTys.push_back(PtrToInt8Ty); 2183 ArgTys.push_back(llvm::Type::getInt32Ty(VMContext)); 2184 FTy = llvm::FunctionType::get(ResultType, ArgTys, false); 2185 return BlockObjectAssign = 2186 CreateRuntimeFunction(FTy, "_Block_object_assign"); 2187 } 2188 2189 llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() { 2190 if (NSConcreteGlobalBlock) 2191 return NSConcreteGlobalBlock; 2192 2193 // If we saw an explicit decl, use that. 2194 if (NSConcreteGlobalBlockDecl) { 2195 return NSConcreteGlobalBlock = GetAddrOfGlobalVar( 2196 NSConcreteGlobalBlockDecl, 2197 getTypes().ConvertType(NSConcreteGlobalBlockDecl->getType())); 2198 } 2199 2200 // Otherwise construct the variable by hand. 2201 return NSConcreteGlobalBlock = CreateRuntimeVariable( 2202 PtrToInt8Ty, "_NSConcreteGlobalBlock"); 2203 } 2204 2205 llvm::Constant *CodeGenModule::getNSConcreteStackBlock() { 2206 if (NSConcreteStackBlock) 2207 return NSConcreteStackBlock; 2208 2209 // If we saw an explicit decl, use that. 2210 if (NSConcreteStackBlockDecl) { 2211 return NSConcreteStackBlock = GetAddrOfGlobalVar( 2212 NSConcreteStackBlockDecl, 2213 getTypes().ConvertType(NSConcreteStackBlockDecl->getType())); 2214 } 2215 2216 // Otherwise construct the variable by hand. 2217 return NSConcreteStackBlock = CreateRuntimeVariable( 2218 PtrToInt8Ty, "_NSConcreteStackBlock"); 2219 } 2220 2221 ///@} 2222