1f22ef01cSRoman Divacky //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // This coordinates the per-module state used while generating code. 11f22ef01cSRoman Divacky // 12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 13f22ef01cSRoman Divacky 14f22ef01cSRoman Divacky #include "CodeGenModule.h" 15f22ef01cSRoman Divacky #include "CGDebugInfo.h" 16f22ef01cSRoman Divacky #include "CodeGenFunction.h" 172754fe60SDimitry Andric #include "CodeGenTBAA.h" 18f22ef01cSRoman Divacky #include "CGCall.h" 19e580952dSDimitry Andric #include "CGCXXABI.h" 20f22ef01cSRoman Divacky #include "CGObjCRuntime.h" 21f22ef01cSRoman Divacky #include "TargetInfo.h" 22ffd1746dSEd Schouten #include "clang/Frontend/CodeGenOptions.h" 23f22ef01cSRoman Divacky #include "clang/AST/ASTContext.h" 24f22ef01cSRoman Divacky #include "clang/AST/CharUnits.h" 25f22ef01cSRoman Divacky #include "clang/AST/DeclObjC.h" 26f22ef01cSRoman Divacky #include "clang/AST/DeclCXX.h" 27ffd1746dSEd Schouten #include "clang/AST/DeclTemplate.h" 282754fe60SDimitry Andric #include "clang/AST/Mangle.h" 29f22ef01cSRoman Divacky #include "clang/AST/RecordLayout.h" 30f22ef01cSRoman Divacky #include "clang/Basic/Builtins.h" 31f22ef01cSRoman Divacky #include "clang/Basic/Diagnostic.h" 32f22ef01cSRoman Divacky #include "clang/Basic/SourceManager.h" 33f22ef01cSRoman Divacky #include "clang/Basic/TargetInfo.h" 34f22ef01cSRoman Divacky #include "clang/Basic/ConvertUTF.h" 35f22ef01cSRoman Divacky #include "llvm/CallingConv.h" 36f22ef01cSRoman Divacky #include "llvm/Module.h" 37f22ef01cSRoman Divacky #include "llvm/Intrinsics.h" 38f22ef01cSRoman Divacky #include "llvm/LLVMContext.h" 39f22ef01cSRoman Divacky #include "llvm/ADT/Triple.h" 402754fe60SDimitry Andric #include "llvm/Target/Mangler.h" 41f22ef01cSRoman Divacky #include "llvm/Target/TargetData.h" 42f22ef01cSRoman Divacky #include "llvm/Support/CallSite.h" 43f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h" 44f22ef01cSRoman Divacky using namespace clang; 45f22ef01cSRoman Divacky using namespace CodeGen; 46f22ef01cSRoman Divacky 47e580952dSDimitry Andric static CGCXXABI &createCXXABI(CodeGenModule &CGM) { 48e580952dSDimitry Andric switch (CGM.getContext().Target.getCXXABI()) { 49e580952dSDimitry Andric case CXXABI_ARM: return *CreateARMCXXABI(CGM); 50e580952dSDimitry Andric case CXXABI_Itanium: return *CreateItaniumCXXABI(CGM); 51e580952dSDimitry Andric case CXXABI_Microsoft: return *CreateMicrosoftCXXABI(CGM); 52e580952dSDimitry Andric } 53e580952dSDimitry Andric 54e580952dSDimitry Andric llvm_unreachable("invalid C++ ABI kind"); 55e580952dSDimitry Andric return *CreateItaniumCXXABI(CGM); 56e580952dSDimitry Andric } 57e580952dSDimitry Andric 58f22ef01cSRoman Divacky 59f22ef01cSRoman Divacky CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, 60f22ef01cSRoman Divacky llvm::Module &M, const llvm::TargetData &TD, 61f22ef01cSRoman Divacky Diagnostic &diags) 622754fe60SDimitry Andric : Context(C), Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M), 63f22ef01cSRoman Divacky TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags), 64e580952dSDimitry Andric ABI(createCXXABI(*this)), 6517a519f9SDimitry Andric Types(C, M, TD, getTargetCodeGenInfo().getABIInfo(), ABI, CGO), 662754fe60SDimitry Andric TBAA(0), 6717a519f9SDimitry Andric VTables(*this), Runtime(0), DebugInfo(0), ARCData(0), RRData(0), 682754fe60SDimitry Andric CFConstantStringClassRef(0), ConstantStringClassRef(0), 69e580952dSDimitry Andric VMContext(M.getContext()), 70e580952dSDimitry Andric NSConcreteGlobalBlockDecl(0), NSConcreteStackBlockDecl(0), 71e580952dSDimitry Andric NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), 72e580952dSDimitry Andric BlockObjectAssignDecl(0), BlockObjectDisposeDecl(0), 732754fe60SDimitry Andric BlockObjectAssign(0), BlockObjectDispose(0), 742754fe60SDimitry Andric BlockDescriptorType(0), GenericBlockLiteralType(0) { 753b0f4066SDimitry Andric if (Features.ObjC1) 763b0f4066SDimitry Andric createObjCRuntime(); 77f22ef01cSRoman Divacky 782754fe60SDimitry Andric // Enable TBAA unless it's suppressed. 792754fe60SDimitry Andric if (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0) 802754fe60SDimitry Andric TBAA = new CodeGenTBAA(Context, VMContext, getLangOptions(), 812754fe60SDimitry Andric ABI.getMangleContext()); 822754fe60SDimitry Andric 833b0f4066SDimitry Andric // If debug info or coverage generation is enabled, create the CGDebugInfo 843b0f4066SDimitry Andric // object. 853b0f4066SDimitry Andric if (CodeGenOpts.DebugInfo || CodeGenOpts.EmitGcovArcs || 863b0f4066SDimitry Andric CodeGenOpts.EmitGcovNotes) 873b0f4066SDimitry Andric DebugInfo = new CGDebugInfo(*this); 882754fe60SDimitry Andric 892754fe60SDimitry Andric Block.GlobalUniqueCount = 0; 902754fe60SDimitry Andric 9117a519f9SDimitry Andric if (C.getLangOptions().ObjCAutoRefCount) 9217a519f9SDimitry Andric ARCData = new ARCEntrypoints(); 9317a519f9SDimitry Andric RRData = new RREntrypoints(); 9417a519f9SDimitry Andric 952754fe60SDimitry Andric // Initialize the type cache. 962754fe60SDimitry Andric llvm::LLVMContext &LLVMContext = M.getContext(); 97bd5abe19SDimitry Andric VoidTy = llvm::Type::getVoidTy(LLVMContext); 982754fe60SDimitry Andric Int8Ty = llvm::Type::getInt8Ty(LLVMContext); 992754fe60SDimitry Andric Int32Ty = llvm::Type::getInt32Ty(LLVMContext); 1002754fe60SDimitry Andric Int64Ty = llvm::Type::getInt64Ty(LLVMContext); 1012754fe60SDimitry Andric PointerWidthInBits = C.Target.getPointerWidth(0); 102dd6029ffSDimitry Andric PointerAlignInBytes = 103dd6029ffSDimitry Andric C.toCharUnitsFromBits(C.Target.getPointerAlign(0)).getQuantity(); 1042754fe60SDimitry Andric IntTy = llvm::IntegerType::get(LLVMContext, C.Target.getIntWidth()); 1052754fe60SDimitry Andric IntPtrTy = llvm::IntegerType::get(LLVMContext, PointerWidthInBits); 1062754fe60SDimitry Andric Int8PtrTy = Int8Ty->getPointerTo(0); 1072754fe60SDimitry Andric Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); 108f22ef01cSRoman Divacky } 109f22ef01cSRoman Divacky 110f22ef01cSRoman Divacky CodeGenModule::~CodeGenModule() { 111f22ef01cSRoman Divacky delete Runtime; 112e580952dSDimitry Andric delete &ABI; 1132754fe60SDimitry Andric delete TBAA; 114f22ef01cSRoman Divacky delete DebugInfo; 11517a519f9SDimitry Andric delete ARCData; 11617a519f9SDimitry Andric delete RRData; 117f22ef01cSRoman Divacky } 118f22ef01cSRoman Divacky 119f22ef01cSRoman Divacky void CodeGenModule::createObjCRuntime() { 120f22ef01cSRoman Divacky if (!Features.NeXTRuntime) 121f22ef01cSRoman Divacky Runtime = CreateGNUObjCRuntime(*this); 122f22ef01cSRoman Divacky else 123f22ef01cSRoman Divacky Runtime = CreateMacObjCRuntime(*this); 124f22ef01cSRoman Divacky } 125f22ef01cSRoman Divacky 126f22ef01cSRoman Divacky void CodeGenModule::Release() { 127f22ef01cSRoman Divacky EmitDeferred(); 128f22ef01cSRoman Divacky EmitCXXGlobalInitFunc(); 129f22ef01cSRoman Divacky EmitCXXGlobalDtorFunc(); 130f22ef01cSRoman Divacky if (Runtime) 131f22ef01cSRoman Divacky if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 132f22ef01cSRoman Divacky AddGlobalCtor(ObjCInitFunction); 133f22ef01cSRoman Divacky EmitCtorList(GlobalCtors, "llvm.global_ctors"); 134f22ef01cSRoman Divacky EmitCtorList(GlobalDtors, "llvm.global_dtors"); 135f22ef01cSRoman Divacky EmitAnnotations(); 136f22ef01cSRoman Divacky EmitLLVMUsed(); 137ffd1746dSEd Schouten 1382754fe60SDimitry Andric SimplifyPersonality(); 1392754fe60SDimitry Andric 140ffd1746dSEd Schouten if (getCodeGenOpts().EmitDeclMetadata) 141ffd1746dSEd Schouten EmitDeclMetadata(); 142bd5abe19SDimitry Andric 143bd5abe19SDimitry Andric if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes) 144bd5abe19SDimitry Andric EmitCoverageFile(); 145f22ef01cSRoman Divacky } 146f22ef01cSRoman Divacky 1473b0f4066SDimitry Andric void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { 1483b0f4066SDimitry Andric // Make sure that this type is translated. 1493b0f4066SDimitry Andric Types.UpdateCompletedType(TD); 1503b0f4066SDimitry Andric if (DebugInfo) 1513b0f4066SDimitry Andric DebugInfo->UpdateCompletedType(TD); 1523b0f4066SDimitry Andric } 1533b0f4066SDimitry Andric 1542754fe60SDimitry Andric llvm::MDNode *CodeGenModule::getTBAAInfo(QualType QTy) { 1552754fe60SDimitry Andric if (!TBAA) 1562754fe60SDimitry Andric return 0; 1572754fe60SDimitry Andric return TBAA->getTBAAInfo(QTy); 1582754fe60SDimitry Andric } 1592754fe60SDimitry Andric 1602754fe60SDimitry Andric void CodeGenModule::DecorateInstruction(llvm::Instruction *Inst, 1612754fe60SDimitry Andric llvm::MDNode *TBAAInfo) { 1622754fe60SDimitry Andric Inst->setMetadata(llvm::LLVMContext::MD_tbaa, TBAAInfo); 1632754fe60SDimitry Andric } 1642754fe60SDimitry Andric 165f22ef01cSRoman Divacky bool CodeGenModule::isTargetDarwin() const { 1663b0f4066SDimitry Andric return getContext().Target.getTriple().isOSDarwin(); 1673b0f4066SDimitry Andric } 1683b0f4066SDimitry Andric 1693b0f4066SDimitry Andric void CodeGenModule::Error(SourceLocation loc, llvm::StringRef error) { 1703b0f4066SDimitry Andric unsigned diagID = getDiags().getCustomDiagID(Diagnostic::Error, error); 1713b0f4066SDimitry Andric getDiags().Report(Context.getFullLoc(loc), diagID); 172f22ef01cSRoman Divacky } 173f22ef01cSRoman Divacky 174f22ef01cSRoman Divacky /// ErrorUnsupported - Print out an error that codegen doesn't support the 175f22ef01cSRoman Divacky /// specified stmt yet. 176f22ef01cSRoman Divacky void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 177f22ef01cSRoman Divacky bool OmitOnError) { 178f22ef01cSRoman Divacky if (OmitOnError && getDiags().hasErrorOccurred()) 179f22ef01cSRoman Divacky return; 180f22ef01cSRoman Divacky unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 181f22ef01cSRoman Divacky "cannot compile this %0 yet"); 182f22ef01cSRoman Divacky std::string Msg = Type; 183f22ef01cSRoman Divacky getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 184f22ef01cSRoman Divacky << Msg << S->getSourceRange(); 185f22ef01cSRoman Divacky } 186f22ef01cSRoman Divacky 187f22ef01cSRoman Divacky /// ErrorUnsupported - Print out an error that codegen doesn't support the 188f22ef01cSRoman Divacky /// specified decl yet. 189f22ef01cSRoman Divacky void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 190f22ef01cSRoman Divacky bool OmitOnError) { 191f22ef01cSRoman Divacky if (OmitOnError && getDiags().hasErrorOccurred()) 192f22ef01cSRoman Divacky return; 193f22ef01cSRoman Divacky unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 194f22ef01cSRoman Divacky "cannot compile this %0 yet"); 195f22ef01cSRoman Divacky std::string Msg = Type; 196f22ef01cSRoman Divacky getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 197f22ef01cSRoman Divacky } 198f22ef01cSRoman Divacky 19917a519f9SDimitry Andric llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) { 20017a519f9SDimitry Andric return llvm::ConstantInt::get(SizeTy, size.getQuantity()); 20117a519f9SDimitry Andric } 20217a519f9SDimitry Andric 203f22ef01cSRoman Divacky void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, 2042754fe60SDimitry Andric const NamedDecl *D) const { 205f22ef01cSRoman Divacky // Internal definitions always have default visibility. 206f22ef01cSRoman Divacky if (GV->hasLocalLinkage()) { 207f22ef01cSRoman Divacky GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 208f22ef01cSRoman Divacky return; 209f22ef01cSRoman Divacky } 210f22ef01cSRoman Divacky 2112754fe60SDimitry Andric // Set visibility for definitions. 2122754fe60SDimitry Andric NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility(); 2132754fe60SDimitry Andric if (LV.visibilityExplicit() || !GV->hasAvailableExternallyLinkage()) 2142754fe60SDimitry Andric GV->setVisibility(GetLLVMVisibility(LV.visibility())); 215f22ef01cSRoman Divacky } 216f22ef01cSRoman Divacky 217e580952dSDimitry Andric /// Set the symbol visibility of type information (vtable and RTTI) 218e580952dSDimitry Andric /// associated with the given type. 219e580952dSDimitry Andric void CodeGenModule::setTypeVisibility(llvm::GlobalValue *GV, 220e580952dSDimitry Andric const CXXRecordDecl *RD, 2212754fe60SDimitry Andric TypeVisibilityKind TVK) const { 222e580952dSDimitry Andric setGlobalVisibility(GV, RD); 223e580952dSDimitry Andric 224e580952dSDimitry Andric if (!CodeGenOpts.HiddenWeakVTables) 225e580952dSDimitry Andric return; 226e580952dSDimitry Andric 2272754fe60SDimitry Andric // We never want to drop the visibility for RTTI names. 2282754fe60SDimitry Andric if (TVK == TVK_ForRTTIName) 2292754fe60SDimitry Andric return; 2302754fe60SDimitry Andric 231e580952dSDimitry Andric // We want to drop the visibility to hidden for weak type symbols. 232e580952dSDimitry Andric // This isn't possible if there might be unresolved references 233e580952dSDimitry Andric // elsewhere that rely on this symbol being visible. 234e580952dSDimitry Andric 235e580952dSDimitry Andric // This should be kept roughly in sync with setThunkVisibility 236e580952dSDimitry Andric // in CGVTables.cpp. 237e580952dSDimitry Andric 238e580952dSDimitry Andric // Preconditions. 2392754fe60SDimitry Andric if (GV->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage || 240e580952dSDimitry Andric GV->getVisibility() != llvm::GlobalVariable::DefaultVisibility) 241e580952dSDimitry Andric return; 242e580952dSDimitry Andric 243e580952dSDimitry Andric // Don't override an explicit visibility attribute. 2443b0f4066SDimitry Andric if (RD->getExplicitVisibility()) 245e580952dSDimitry Andric return; 246e580952dSDimitry Andric 247e580952dSDimitry Andric switch (RD->getTemplateSpecializationKind()) { 248e580952dSDimitry Andric // We have to disable the optimization if this is an EI definition 249e580952dSDimitry Andric // because there might be EI declarations in other shared objects. 250e580952dSDimitry Andric case TSK_ExplicitInstantiationDefinition: 251e580952dSDimitry Andric case TSK_ExplicitInstantiationDeclaration: 252e580952dSDimitry Andric return; 253e580952dSDimitry Andric 254e580952dSDimitry Andric // Every use of a non-template class's type information has to emit it. 255e580952dSDimitry Andric case TSK_Undeclared: 256e580952dSDimitry Andric break; 257e580952dSDimitry Andric 258e580952dSDimitry Andric // In theory, implicit instantiations can ignore the possibility of 259e580952dSDimitry Andric // an explicit instantiation declaration because there necessarily 260e580952dSDimitry Andric // must be an EI definition somewhere with default visibility. In 261e580952dSDimitry Andric // practice, it's possible to have an explicit instantiation for 262e580952dSDimitry Andric // an arbitrary template class, and linkers aren't necessarily able 263e580952dSDimitry Andric // to deal with mixed-visibility symbols. 264e580952dSDimitry Andric case TSK_ExplicitSpecialization: 265e580952dSDimitry Andric case TSK_ImplicitInstantiation: 266e580952dSDimitry Andric if (!CodeGenOpts.HiddenWeakTemplateVTables) 267e580952dSDimitry Andric return; 268e580952dSDimitry Andric break; 269e580952dSDimitry Andric } 270e580952dSDimitry Andric 271e580952dSDimitry Andric // If there's a key function, there may be translation units 272e580952dSDimitry Andric // that don't have the key function's definition. But ignore 273e580952dSDimitry Andric // this if we're emitting RTTI under -fno-rtti. 2742754fe60SDimitry Andric if (!(TVK != TVK_ForRTTI) || Features.RTTI) { 275e580952dSDimitry Andric if (Context.getKeyFunction(RD)) 276e580952dSDimitry Andric return; 2772754fe60SDimitry Andric } 278e580952dSDimitry Andric 279e580952dSDimitry Andric // Otherwise, drop the visibility to hidden. 280e580952dSDimitry Andric GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 2812754fe60SDimitry Andric GV->setUnnamedAddr(true); 282e580952dSDimitry Andric } 283e580952dSDimitry Andric 284ffd1746dSEd Schouten llvm::StringRef CodeGenModule::getMangledName(GlobalDecl GD) { 285f22ef01cSRoman Divacky const NamedDecl *ND = cast<NamedDecl>(GD.getDecl()); 286f22ef01cSRoman Divacky 287ffd1746dSEd Schouten llvm::StringRef &Str = MangledDeclNames[GD.getCanonicalDecl()]; 288ffd1746dSEd Schouten if (!Str.empty()) 289ffd1746dSEd Schouten return Str; 290f22ef01cSRoman Divacky 291e580952dSDimitry Andric if (!getCXXABI().getMangleContext().shouldMangleDeclName(ND)) { 292ffd1746dSEd Schouten IdentifierInfo *II = ND->getIdentifier(); 293ffd1746dSEd Schouten assert(II && "Attempt to mangle unnamed decl."); 294ffd1746dSEd Schouten 295ffd1746dSEd Schouten Str = II->getName(); 296ffd1746dSEd Schouten return Str; 297f22ef01cSRoman Divacky } 298f22ef01cSRoman Divacky 299ffd1746dSEd Schouten llvm::SmallString<256> Buffer; 3002754fe60SDimitry Andric llvm::raw_svector_ostream Out(Buffer); 301ffd1746dSEd Schouten if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) 3022754fe60SDimitry Andric getCXXABI().getMangleContext().mangleCXXCtor(D, GD.getCtorType(), Out); 303ffd1746dSEd Schouten else if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) 3042754fe60SDimitry Andric getCXXABI().getMangleContext().mangleCXXDtor(D, GD.getDtorType(), Out); 305ffd1746dSEd Schouten else if (const BlockDecl *BD = dyn_cast<BlockDecl>(ND)) 3062754fe60SDimitry Andric getCXXABI().getMangleContext().mangleBlock(BD, Out); 307ffd1746dSEd Schouten else 3082754fe60SDimitry Andric getCXXABI().getMangleContext().mangleName(ND, Out); 309ffd1746dSEd Schouten 310ffd1746dSEd Schouten // Allocate space for the mangled name. 3112754fe60SDimitry Andric Out.flush(); 312ffd1746dSEd Schouten size_t Length = Buffer.size(); 313ffd1746dSEd Schouten char *Name = MangledNamesAllocator.Allocate<char>(Length); 314ffd1746dSEd Schouten std::copy(Buffer.begin(), Buffer.end(), Name); 315ffd1746dSEd Schouten 316ffd1746dSEd Schouten Str = llvm::StringRef(Name, Length); 317ffd1746dSEd Schouten 318ffd1746dSEd Schouten return Str; 319ffd1746dSEd Schouten } 320ffd1746dSEd Schouten 3212754fe60SDimitry Andric void CodeGenModule::getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer, 322ffd1746dSEd Schouten const BlockDecl *BD) { 3232754fe60SDimitry Andric MangleContext &MangleCtx = getCXXABI().getMangleContext(); 3242754fe60SDimitry Andric const Decl *D = GD.getDecl(); 3252754fe60SDimitry Andric llvm::raw_svector_ostream Out(Buffer.getBuffer()); 3262754fe60SDimitry Andric if (D == 0) 3272754fe60SDimitry Andric MangleCtx.mangleGlobalBlock(BD, Out); 3282754fe60SDimitry Andric else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) 3292754fe60SDimitry Andric MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out); 3302754fe60SDimitry Andric else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) 3312754fe60SDimitry Andric MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out); 3322754fe60SDimitry Andric else 3332754fe60SDimitry Andric MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out); 334f22ef01cSRoman Divacky } 335f22ef01cSRoman Divacky 336f22ef01cSRoman Divacky llvm::GlobalValue *CodeGenModule::GetGlobalValue(llvm::StringRef Name) { 337f22ef01cSRoman Divacky return getModule().getNamedValue(Name); 338f22ef01cSRoman Divacky } 339f22ef01cSRoman Divacky 340f22ef01cSRoman Divacky /// AddGlobalCtor - Add a function to the list that will be called before 341f22ef01cSRoman Divacky /// main() runs. 342f22ef01cSRoman Divacky void CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 343f22ef01cSRoman Divacky // FIXME: Type coercion of void()* types. 344f22ef01cSRoman Divacky GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 345f22ef01cSRoman Divacky } 346f22ef01cSRoman Divacky 347f22ef01cSRoman Divacky /// AddGlobalDtor - Add a function to the list that will be called 348f22ef01cSRoman Divacky /// when the module is unloaded. 349f22ef01cSRoman Divacky void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 350f22ef01cSRoman Divacky // FIXME: Type coercion of void()* types. 351f22ef01cSRoman Divacky GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 352f22ef01cSRoman Divacky } 353f22ef01cSRoman Divacky 354f22ef01cSRoman Divacky void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 355f22ef01cSRoman Divacky // Ctor function type is void()*. 356bd5abe19SDimitry Andric llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); 357f22ef01cSRoman Divacky llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 358f22ef01cSRoman Divacky 359f22ef01cSRoman Divacky // Get the type of a ctor entry, { i32, void ()* }. 360f22ef01cSRoman Divacky llvm::StructType *CtorStructTy = 36117a519f9SDimitry Andric llvm::StructType::get(llvm::Type::getInt32Ty(VMContext), 362f22ef01cSRoman Divacky llvm::PointerType::getUnqual(CtorFTy), NULL); 363f22ef01cSRoman Divacky 364f22ef01cSRoman Divacky // Construct the constructor and destructor arrays. 365f22ef01cSRoman Divacky std::vector<llvm::Constant*> Ctors; 366f22ef01cSRoman Divacky for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 367f22ef01cSRoman Divacky std::vector<llvm::Constant*> S; 368f22ef01cSRoman Divacky S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 369f22ef01cSRoman Divacky I->second, false)); 370f22ef01cSRoman Divacky S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 371f22ef01cSRoman Divacky Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 372f22ef01cSRoman Divacky } 373f22ef01cSRoman Divacky 374f22ef01cSRoman Divacky if (!Ctors.empty()) { 375f22ef01cSRoman Divacky llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 376f22ef01cSRoman Divacky new llvm::GlobalVariable(TheModule, AT, false, 377f22ef01cSRoman Divacky llvm::GlobalValue::AppendingLinkage, 378f22ef01cSRoman Divacky llvm::ConstantArray::get(AT, Ctors), 379f22ef01cSRoman Divacky GlobalName); 380f22ef01cSRoman Divacky } 381f22ef01cSRoman Divacky } 382f22ef01cSRoman Divacky 383f22ef01cSRoman Divacky void CodeGenModule::EmitAnnotations() { 384f22ef01cSRoman Divacky if (Annotations.empty()) 385f22ef01cSRoman Divacky return; 386f22ef01cSRoman Divacky 387f22ef01cSRoman Divacky // Create a new global variable for the ConstantStruct in the Module. 388f22ef01cSRoman Divacky llvm::Constant *Array = 389f22ef01cSRoman Divacky llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 390f22ef01cSRoman Divacky Annotations.size()), 391f22ef01cSRoman Divacky Annotations); 392f22ef01cSRoman Divacky llvm::GlobalValue *gv = 393f22ef01cSRoman Divacky new llvm::GlobalVariable(TheModule, Array->getType(), false, 394f22ef01cSRoman Divacky llvm::GlobalValue::AppendingLinkage, Array, 395f22ef01cSRoman Divacky "llvm.global.annotations"); 396f22ef01cSRoman Divacky gv->setSection("llvm.metadata"); 397f22ef01cSRoman Divacky } 398f22ef01cSRoman Divacky 399f22ef01cSRoman Divacky llvm::GlobalValue::LinkageTypes 400f22ef01cSRoman Divacky CodeGenModule::getFunctionLinkage(const FunctionDecl *D) { 401e580952dSDimitry Andric GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); 402f22ef01cSRoman Divacky 403ffd1746dSEd Schouten if (Linkage == GVA_Internal) 404f22ef01cSRoman Divacky return llvm::Function::InternalLinkage; 405ffd1746dSEd Schouten 406ffd1746dSEd Schouten if (D->hasAttr<DLLExportAttr>()) 407f22ef01cSRoman Divacky return llvm::Function::DLLExportLinkage; 408ffd1746dSEd Schouten 409ffd1746dSEd Schouten if (D->hasAttr<WeakAttr>()) 410f22ef01cSRoman Divacky return llvm::Function::WeakAnyLinkage; 411ffd1746dSEd Schouten 412f22ef01cSRoman Divacky // In C99 mode, 'inline' functions are guaranteed to have a strong 413f22ef01cSRoman Divacky // definition somewhere else, so we can use available_externally linkage. 414ffd1746dSEd Schouten if (Linkage == GVA_C99Inline) 415f22ef01cSRoman Divacky return llvm::Function::AvailableExternallyLinkage; 416ffd1746dSEd Schouten 417f22ef01cSRoman Divacky // In C++, the compiler has to emit a definition in every translation unit 418f22ef01cSRoman Divacky // that references the function. We should use linkonce_odr because 419f22ef01cSRoman Divacky // a) if all references in this translation unit are optimized away, we 420f22ef01cSRoman Divacky // don't need to codegen it. b) if the function persists, it needs to be 421f22ef01cSRoman Divacky // merged with other definitions. c) C++ has the ODR, so we know the 422f22ef01cSRoman Divacky // definition is dependable. 423ffd1746dSEd Schouten if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) 4242754fe60SDimitry Andric return !Context.getLangOptions().AppleKext 4252754fe60SDimitry Andric ? llvm::Function::LinkOnceODRLinkage 4262754fe60SDimitry Andric : llvm::Function::InternalLinkage; 427ffd1746dSEd Schouten 428f22ef01cSRoman Divacky // An explicit instantiation of a template has weak linkage, since 429f22ef01cSRoman Divacky // explicit instantiations can occur in multiple translation units 430f22ef01cSRoman Divacky // and must all be equivalent. However, we are not allowed to 431f22ef01cSRoman Divacky // throw away these explicit instantiations. 432ffd1746dSEd Schouten if (Linkage == GVA_ExplicitTemplateInstantiation) 4332754fe60SDimitry Andric return !Context.getLangOptions().AppleKext 4342754fe60SDimitry Andric ? llvm::Function::WeakODRLinkage 4352754fe60SDimitry Andric : llvm::Function::InternalLinkage; 436ffd1746dSEd Schouten 437f22ef01cSRoman Divacky // Otherwise, we have strong external linkage. 438ffd1746dSEd Schouten assert(Linkage == GVA_StrongExternal); 439f22ef01cSRoman Divacky return llvm::Function::ExternalLinkage; 440f22ef01cSRoman Divacky } 441f22ef01cSRoman Divacky 442f22ef01cSRoman Divacky 443f22ef01cSRoman Divacky /// SetFunctionDefinitionAttributes - Set attributes for a global. 444f22ef01cSRoman Divacky /// 445f22ef01cSRoman Divacky /// FIXME: This is currently only done for aliases and functions, but not for 446f22ef01cSRoman Divacky /// variables (these details are set in EmitGlobalVarDefinition for variables). 447f22ef01cSRoman Divacky void CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, 448f22ef01cSRoman Divacky llvm::GlobalValue *GV) { 449f22ef01cSRoman Divacky SetCommonAttributes(D, GV); 450f22ef01cSRoman Divacky } 451f22ef01cSRoman Divacky 452f22ef01cSRoman Divacky void CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, 453f22ef01cSRoman Divacky const CGFunctionInfo &Info, 454f22ef01cSRoman Divacky llvm::Function *F) { 455f22ef01cSRoman Divacky unsigned CallingConv; 456f22ef01cSRoman Divacky AttributeListType AttributeList; 457f22ef01cSRoman Divacky ConstructAttributeList(Info, D, AttributeList, CallingConv); 458f22ef01cSRoman Divacky F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 459f22ef01cSRoman Divacky AttributeList.size())); 460f22ef01cSRoman Divacky F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 461f22ef01cSRoman Divacky } 462f22ef01cSRoman Divacky 463f22ef01cSRoman Divacky void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, 464f22ef01cSRoman Divacky llvm::Function *F) { 465bd5abe19SDimitry Andric if (CodeGenOpts.UnwindTables) 466bd5abe19SDimitry Andric F->setHasUWTable(); 467bd5abe19SDimitry Andric 468f22ef01cSRoman Divacky if (!Features.Exceptions && !Features.ObjCNonFragileABI) 469f22ef01cSRoman Divacky F->addFnAttr(llvm::Attribute::NoUnwind); 470f22ef01cSRoman Divacky 471f22ef01cSRoman Divacky if (D->hasAttr<AlwaysInlineAttr>()) 472f22ef01cSRoman Divacky F->addFnAttr(llvm::Attribute::AlwaysInline); 473f22ef01cSRoman Divacky 4742754fe60SDimitry Andric if (D->hasAttr<NakedAttr>()) 4752754fe60SDimitry Andric F->addFnAttr(llvm::Attribute::Naked); 4762754fe60SDimitry Andric 477f22ef01cSRoman Divacky if (D->hasAttr<NoInlineAttr>()) 478f22ef01cSRoman Divacky F->addFnAttr(llvm::Attribute::NoInline); 479f22ef01cSRoman Divacky 4802754fe60SDimitry Andric if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D)) 4812754fe60SDimitry Andric F->setUnnamedAddr(true); 4822754fe60SDimitry Andric 483f22ef01cSRoman Divacky if (Features.getStackProtectorMode() == LangOptions::SSPOn) 484f22ef01cSRoman Divacky F->addFnAttr(llvm::Attribute::StackProtect); 485f22ef01cSRoman Divacky else if (Features.getStackProtectorMode() == LangOptions::SSPReq) 486f22ef01cSRoman Divacky F->addFnAttr(llvm::Attribute::StackProtectReq); 487f22ef01cSRoman Divacky 488e580952dSDimitry Andric unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); 489e580952dSDimitry Andric if (alignment) 490e580952dSDimitry Andric F->setAlignment(alignment); 491e580952dSDimitry Andric 492f22ef01cSRoman Divacky // C++ ABI requires 2-byte alignment for member functions. 493f22ef01cSRoman Divacky if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) 494f22ef01cSRoman Divacky F->setAlignment(2); 495f22ef01cSRoman Divacky } 496f22ef01cSRoman Divacky 497f22ef01cSRoman Divacky void CodeGenModule::SetCommonAttributes(const Decl *D, 498f22ef01cSRoman Divacky llvm::GlobalValue *GV) { 4992754fe60SDimitry Andric if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 5002754fe60SDimitry Andric setGlobalVisibility(GV, ND); 5012754fe60SDimitry Andric else 5022754fe60SDimitry Andric GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 503f22ef01cSRoman Divacky 504f22ef01cSRoman Divacky if (D->hasAttr<UsedAttr>()) 505f22ef01cSRoman Divacky AddUsedGlobal(GV); 506f22ef01cSRoman Divacky 507f22ef01cSRoman Divacky if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 508f22ef01cSRoman Divacky GV->setSection(SA->getName()); 509f22ef01cSRoman Divacky 510f22ef01cSRoman Divacky getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this); 511f22ef01cSRoman Divacky } 512f22ef01cSRoman Divacky 513f22ef01cSRoman Divacky void CodeGenModule::SetInternalFunctionAttributes(const Decl *D, 514f22ef01cSRoman Divacky llvm::Function *F, 515f22ef01cSRoman Divacky const CGFunctionInfo &FI) { 516f22ef01cSRoman Divacky SetLLVMFunctionAttributes(D, FI, F); 517f22ef01cSRoman Divacky SetLLVMFunctionAttributesForDefinition(D, F); 518f22ef01cSRoman Divacky 519f22ef01cSRoman Divacky F->setLinkage(llvm::Function::InternalLinkage); 520f22ef01cSRoman Divacky 521f22ef01cSRoman Divacky SetCommonAttributes(D, F); 522f22ef01cSRoman Divacky } 523f22ef01cSRoman Divacky 524f22ef01cSRoman Divacky void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, 525f22ef01cSRoman Divacky llvm::Function *F, 526f22ef01cSRoman Divacky bool IsIncompleteFunction) { 5273b0f4066SDimitry Andric if (unsigned IID = F->getIntrinsicID()) { 5283b0f4066SDimitry Andric // If this is an intrinsic function, set the function's attributes 5293b0f4066SDimitry Andric // to the intrinsic's attributes. 5303b0f4066SDimitry Andric F->setAttributes(llvm::Intrinsic::getAttributes((llvm::Intrinsic::ID)IID)); 5313b0f4066SDimitry Andric return; 5323b0f4066SDimitry Andric } 5333b0f4066SDimitry Andric 534f22ef01cSRoman Divacky const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 535f22ef01cSRoman Divacky 536f22ef01cSRoman Divacky if (!IsIncompleteFunction) 537f22ef01cSRoman Divacky SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F); 538f22ef01cSRoman Divacky 539f22ef01cSRoman Divacky // Only a few attributes are set on declarations; these may later be 540f22ef01cSRoman Divacky // overridden by a definition. 541f22ef01cSRoman Divacky 542f22ef01cSRoman Divacky if (FD->hasAttr<DLLImportAttr>()) { 543f22ef01cSRoman Divacky F->setLinkage(llvm::Function::DLLImportLinkage); 544f22ef01cSRoman Divacky } else if (FD->hasAttr<WeakAttr>() || 5453b0f4066SDimitry Andric FD->isWeakImported()) { 546f22ef01cSRoman Divacky // "extern_weak" is overloaded in LLVM; we probably should have 547f22ef01cSRoman Divacky // separate linkage types for this. 548f22ef01cSRoman Divacky F->setLinkage(llvm::Function::ExternalWeakLinkage); 549f22ef01cSRoman Divacky } else { 550f22ef01cSRoman Divacky F->setLinkage(llvm::Function::ExternalLinkage); 5512754fe60SDimitry Andric 5522754fe60SDimitry Andric NamedDecl::LinkageInfo LV = FD->getLinkageAndVisibility(); 5532754fe60SDimitry Andric if (LV.linkage() == ExternalLinkage && LV.visibilityExplicit()) { 5542754fe60SDimitry Andric F->setVisibility(GetLLVMVisibility(LV.visibility())); 5552754fe60SDimitry Andric } 556f22ef01cSRoman Divacky } 557f22ef01cSRoman Divacky 558f22ef01cSRoman Divacky if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) 559f22ef01cSRoman Divacky F->setSection(SA->getName()); 560f22ef01cSRoman Divacky } 561f22ef01cSRoman Divacky 562f22ef01cSRoman Divacky void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 563f22ef01cSRoman Divacky assert(!GV->isDeclaration() && 564f22ef01cSRoman Divacky "Only globals with definition can force usage."); 565f22ef01cSRoman Divacky LLVMUsed.push_back(GV); 566f22ef01cSRoman Divacky } 567f22ef01cSRoman Divacky 568f22ef01cSRoman Divacky void CodeGenModule::EmitLLVMUsed() { 569f22ef01cSRoman Divacky // Don't create llvm.used if there is no need. 570f22ef01cSRoman Divacky if (LLVMUsed.empty()) 571f22ef01cSRoman Divacky return; 572f22ef01cSRoman Divacky 573f22ef01cSRoman Divacky const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); 574f22ef01cSRoman Divacky 575f22ef01cSRoman Divacky // Convert LLVMUsed to what ConstantArray needs. 576f22ef01cSRoman Divacky std::vector<llvm::Constant*> UsedArray; 577f22ef01cSRoman Divacky UsedArray.resize(LLVMUsed.size()); 578f22ef01cSRoman Divacky for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { 579f22ef01cSRoman Divacky UsedArray[i] = 580f22ef01cSRoman Divacky llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), 581f22ef01cSRoman Divacky i8PTy); 582f22ef01cSRoman Divacky } 583f22ef01cSRoman Divacky 584f22ef01cSRoman Divacky if (UsedArray.empty()) 585f22ef01cSRoman Divacky return; 586f22ef01cSRoman Divacky llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); 587f22ef01cSRoman Divacky 588f22ef01cSRoman Divacky llvm::GlobalVariable *GV = 589f22ef01cSRoman Divacky new llvm::GlobalVariable(getModule(), ATy, false, 590f22ef01cSRoman Divacky llvm::GlobalValue::AppendingLinkage, 591f22ef01cSRoman Divacky llvm::ConstantArray::get(ATy, UsedArray), 592f22ef01cSRoman Divacky "llvm.used"); 593f22ef01cSRoman Divacky 594f22ef01cSRoman Divacky GV->setSection("llvm.metadata"); 595f22ef01cSRoman Divacky } 596f22ef01cSRoman Divacky 597f22ef01cSRoman Divacky void CodeGenModule::EmitDeferred() { 598f22ef01cSRoman Divacky // Emit code for any potentially referenced deferred decls. Since a 599f22ef01cSRoman Divacky // previously unused static decl may become used during the generation of code 600f22ef01cSRoman Divacky // for a static function, iterate until no changes are made. 601f22ef01cSRoman Divacky 602f22ef01cSRoman Divacky while (!DeferredDeclsToEmit.empty() || !DeferredVTables.empty()) { 603f22ef01cSRoman Divacky if (!DeferredVTables.empty()) { 604f22ef01cSRoman Divacky const CXXRecordDecl *RD = DeferredVTables.back(); 605f22ef01cSRoman Divacky DeferredVTables.pop_back(); 606f22ef01cSRoman Divacky getVTables().GenerateClassData(getVTableLinkage(RD), RD); 607f22ef01cSRoman Divacky continue; 608f22ef01cSRoman Divacky } 609f22ef01cSRoman Divacky 610f22ef01cSRoman Divacky GlobalDecl D = DeferredDeclsToEmit.back(); 611f22ef01cSRoman Divacky DeferredDeclsToEmit.pop_back(); 612f22ef01cSRoman Divacky 613f22ef01cSRoman Divacky // Check to see if we've already emitted this. This is necessary 614f22ef01cSRoman Divacky // for a couple of reasons: first, decls can end up in the 615f22ef01cSRoman Divacky // deferred-decls queue multiple times, and second, decls can end 616f22ef01cSRoman Divacky // up with definitions in unusual ways (e.g. by an extern inline 617f22ef01cSRoman Divacky // function acquiring a strong function redefinition). Just 618f22ef01cSRoman Divacky // ignore these cases. 619f22ef01cSRoman Divacky // 620f22ef01cSRoman Divacky // TODO: That said, looking this up multiple times is very wasteful. 621ffd1746dSEd Schouten llvm::StringRef Name = getMangledName(D); 622f22ef01cSRoman Divacky llvm::GlobalValue *CGRef = GetGlobalValue(Name); 623f22ef01cSRoman Divacky assert(CGRef && "Deferred decl wasn't referenced?"); 624f22ef01cSRoman Divacky 625f22ef01cSRoman Divacky if (!CGRef->isDeclaration()) 626f22ef01cSRoman Divacky continue; 627f22ef01cSRoman Divacky 628f22ef01cSRoman Divacky // GlobalAlias::isDeclaration() defers to the aliasee, but for our 629f22ef01cSRoman Divacky // purposes an alias counts as a definition. 630f22ef01cSRoman Divacky if (isa<llvm::GlobalAlias>(CGRef)) 631f22ef01cSRoman Divacky continue; 632f22ef01cSRoman Divacky 633f22ef01cSRoman Divacky // Otherwise, emit the definition and move on to the next one. 634f22ef01cSRoman Divacky EmitGlobalDefinition(D); 635f22ef01cSRoman Divacky } 636f22ef01cSRoman Divacky } 637f22ef01cSRoman Divacky 638f22ef01cSRoman Divacky /// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 639f22ef01cSRoman Divacky /// annotation information for a given GlobalValue. The annotation struct is 640f22ef01cSRoman Divacky /// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 641f22ef01cSRoman Divacky /// GlobalValue being annotated. The second field is the constant string 642f22ef01cSRoman Divacky /// created from the AnnotateAttr's annotation. The third field is a constant 643f22ef01cSRoman Divacky /// string containing the name of the translation unit. The fourth field is 644f22ef01cSRoman Divacky /// the line number in the file of the annotated value declaration. 645f22ef01cSRoman Divacky /// 646f22ef01cSRoman Divacky /// FIXME: this does not unique the annotation string constants, as llvm-gcc 647f22ef01cSRoman Divacky /// appears to. 648f22ef01cSRoman Divacky /// 649f22ef01cSRoman Divacky llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 650f22ef01cSRoman Divacky const AnnotateAttr *AA, 651f22ef01cSRoman Divacky unsigned LineNo) { 652f22ef01cSRoman Divacky llvm::Module *M = &getModule(); 653f22ef01cSRoman Divacky 654f22ef01cSRoman Divacky // get [N x i8] constants for the annotation string, and the filename string 655f22ef01cSRoman Divacky // which are the 2nd and 3rd elements of the global annotation structure. 656f22ef01cSRoman Divacky const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext); 657f22ef01cSRoman Divacky llvm::Constant *anno = llvm::ConstantArray::get(VMContext, 658f22ef01cSRoman Divacky AA->getAnnotation(), true); 659f22ef01cSRoman Divacky llvm::Constant *unit = llvm::ConstantArray::get(VMContext, 660f22ef01cSRoman Divacky M->getModuleIdentifier(), 661f22ef01cSRoman Divacky true); 662f22ef01cSRoman Divacky 663f22ef01cSRoman Divacky // Get the two global values corresponding to the ConstantArrays we just 664f22ef01cSRoman Divacky // created to hold the bytes of the strings. 665f22ef01cSRoman Divacky llvm::GlobalValue *annoGV = 666f22ef01cSRoman Divacky new llvm::GlobalVariable(*M, anno->getType(), false, 667f22ef01cSRoman Divacky llvm::GlobalValue::PrivateLinkage, anno, 668f22ef01cSRoman Divacky GV->getName()); 669f22ef01cSRoman Divacky // translation unit name string, emitted into the llvm.metadata section. 670f22ef01cSRoman Divacky llvm::GlobalValue *unitGV = 671f22ef01cSRoman Divacky new llvm::GlobalVariable(*M, unit->getType(), false, 672f22ef01cSRoman Divacky llvm::GlobalValue::PrivateLinkage, unit, 673f22ef01cSRoman Divacky ".str"); 6742754fe60SDimitry Andric unitGV->setUnnamedAddr(true); 675f22ef01cSRoman Divacky 676f22ef01cSRoman Divacky // Create the ConstantStruct for the global annotation. 677f22ef01cSRoman Divacky llvm::Constant *Fields[4] = { 678f22ef01cSRoman Divacky llvm::ConstantExpr::getBitCast(GV, SBP), 679f22ef01cSRoman Divacky llvm::ConstantExpr::getBitCast(annoGV, SBP), 680f22ef01cSRoman Divacky llvm::ConstantExpr::getBitCast(unitGV, SBP), 681f22ef01cSRoman Divacky llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo) 682f22ef01cSRoman Divacky }; 68317a519f9SDimitry Andric return llvm::ConstantStruct::getAnon(Fields); 684f22ef01cSRoman Divacky } 685f22ef01cSRoman Divacky 686f22ef01cSRoman Divacky bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 687e580952dSDimitry Andric // Never defer when EmitAllDecls is specified. 688e580952dSDimitry Andric if (Features.EmitAllDecls) 689f22ef01cSRoman Divacky return false; 690f22ef01cSRoman Divacky 691e580952dSDimitry Andric return !getContext().DeclMustBeEmitted(Global); 692f22ef01cSRoman Divacky } 693f22ef01cSRoman Divacky 694f22ef01cSRoman Divacky llvm::Constant *CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { 695f22ef01cSRoman Divacky const AliasAttr *AA = VD->getAttr<AliasAttr>(); 696f22ef01cSRoman Divacky assert(AA && "No alias?"); 697f22ef01cSRoman Divacky 698f22ef01cSRoman Divacky const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); 699f22ef01cSRoman Divacky 700f22ef01cSRoman Divacky // See if there is already something with the target's name in the module. 701f22ef01cSRoman Divacky llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); 702f22ef01cSRoman Divacky 703f22ef01cSRoman Divacky llvm::Constant *Aliasee; 704f22ef01cSRoman Divacky if (isa<llvm::FunctionType>(DeclTy)) 7052754fe60SDimitry Andric Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl(), 7062754fe60SDimitry Andric /*ForVTable=*/false); 707f22ef01cSRoman Divacky else 708f22ef01cSRoman Divacky Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), 709f22ef01cSRoman Divacky llvm::PointerType::getUnqual(DeclTy), 0); 710f22ef01cSRoman Divacky if (!Entry) { 711f22ef01cSRoman Divacky llvm::GlobalValue* F = cast<llvm::GlobalValue>(Aliasee); 712f22ef01cSRoman Divacky F->setLinkage(llvm::Function::ExternalWeakLinkage); 713f22ef01cSRoman Divacky WeakRefReferences.insert(F); 714f22ef01cSRoman Divacky } 715f22ef01cSRoman Divacky 716f22ef01cSRoman Divacky return Aliasee; 717f22ef01cSRoman Divacky } 718f22ef01cSRoman Divacky 719f22ef01cSRoman Divacky void CodeGenModule::EmitGlobal(GlobalDecl GD) { 720f22ef01cSRoman Divacky const ValueDecl *Global = cast<ValueDecl>(GD.getDecl()); 721f22ef01cSRoman Divacky 722f22ef01cSRoman Divacky // Weak references don't produce any output by themselves. 723f22ef01cSRoman Divacky if (Global->hasAttr<WeakRefAttr>()) 724f22ef01cSRoman Divacky return; 725f22ef01cSRoman Divacky 726f22ef01cSRoman Divacky // If this is an alias definition (which otherwise looks like a declaration) 727f22ef01cSRoman Divacky // emit it now. 728f22ef01cSRoman Divacky if (Global->hasAttr<AliasAttr>()) 729f22ef01cSRoman Divacky return EmitAliasDefinition(GD); 730f22ef01cSRoman Divacky 731f22ef01cSRoman Divacky // Ignore declarations, they will be emitted on their first use. 732f22ef01cSRoman Divacky if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 733e580952dSDimitry Andric if (FD->getIdentifier()) { 734e580952dSDimitry Andric llvm::StringRef Name = FD->getName(); 735e580952dSDimitry Andric if (Name == "_Block_object_assign") { 736e580952dSDimitry Andric BlockObjectAssignDecl = FD; 737e580952dSDimitry Andric } else if (Name == "_Block_object_dispose") { 738e580952dSDimitry Andric BlockObjectDisposeDecl = FD; 739e580952dSDimitry Andric } 740e580952dSDimitry Andric } 741e580952dSDimitry Andric 742f22ef01cSRoman Divacky // Forward declarations are emitted lazily on first use. 743bd5abe19SDimitry Andric if (!FD->doesThisDeclarationHaveABody()) 744f22ef01cSRoman Divacky return; 745f22ef01cSRoman Divacky } else { 746f22ef01cSRoman Divacky const VarDecl *VD = cast<VarDecl>(Global); 747f22ef01cSRoman Divacky assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 748f22ef01cSRoman Divacky 749e580952dSDimitry Andric if (VD->getIdentifier()) { 750e580952dSDimitry Andric llvm::StringRef Name = VD->getName(); 751e580952dSDimitry Andric if (Name == "_NSConcreteGlobalBlock") { 752e580952dSDimitry Andric NSConcreteGlobalBlockDecl = VD; 753e580952dSDimitry Andric } else if (Name == "_NSConcreteStackBlock") { 754e580952dSDimitry Andric NSConcreteStackBlockDecl = VD; 755e580952dSDimitry Andric } 756e580952dSDimitry Andric } 757e580952dSDimitry Andric 758e580952dSDimitry Andric 759f22ef01cSRoman Divacky if (VD->isThisDeclarationADefinition() != VarDecl::Definition) 760f22ef01cSRoman Divacky return; 761f22ef01cSRoman Divacky } 762f22ef01cSRoman Divacky 763f22ef01cSRoman Divacky // Defer code generation when possible if this is a static definition, inline 764f22ef01cSRoman Divacky // function etc. These we only want to emit if they are used. 765f22ef01cSRoman Divacky if (!MayDeferGeneration(Global)) { 766f22ef01cSRoman Divacky // Emit the definition if it can't be deferred. 767f22ef01cSRoman Divacky EmitGlobalDefinition(GD); 768f22ef01cSRoman Divacky return; 769f22ef01cSRoman Divacky } 770f22ef01cSRoman Divacky 771e580952dSDimitry Andric // If we're deferring emission of a C++ variable with an 772e580952dSDimitry Andric // initializer, remember the order in which it appeared in the file. 773e580952dSDimitry Andric if (getLangOptions().CPlusPlus && isa<VarDecl>(Global) && 774e580952dSDimitry Andric cast<VarDecl>(Global)->hasInit()) { 775e580952dSDimitry Andric DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); 776e580952dSDimitry Andric CXXGlobalInits.push_back(0); 777e580952dSDimitry Andric } 778e580952dSDimitry Andric 779f22ef01cSRoman Divacky // If the value has already been used, add it directly to the 780f22ef01cSRoman Divacky // DeferredDeclsToEmit list. 781ffd1746dSEd Schouten llvm::StringRef MangledName = getMangledName(GD); 782f22ef01cSRoman Divacky if (GetGlobalValue(MangledName)) 783f22ef01cSRoman Divacky DeferredDeclsToEmit.push_back(GD); 784f22ef01cSRoman Divacky else { 785f22ef01cSRoman Divacky // Otherwise, remember that we saw a deferred decl with this name. The 786f22ef01cSRoman Divacky // first use of the mangled name will cause it to move into 787f22ef01cSRoman Divacky // DeferredDeclsToEmit. 788f22ef01cSRoman Divacky DeferredDecls[MangledName] = GD; 789f22ef01cSRoman Divacky } 790f22ef01cSRoman Divacky } 791f22ef01cSRoman Divacky 792f22ef01cSRoman Divacky void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { 793f22ef01cSRoman Divacky const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 794f22ef01cSRoman Divacky 795f22ef01cSRoman Divacky PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), 796f22ef01cSRoman Divacky Context.getSourceManager(), 797f22ef01cSRoman Divacky "Generating code for declaration"); 798f22ef01cSRoman Divacky 799ffd1746dSEd Schouten if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 800ffd1746dSEd Schouten // At -O0, don't generate IR for functions with available_externally 801ffd1746dSEd Schouten // linkage. 802ffd1746dSEd Schouten if (CodeGenOpts.OptimizationLevel == 0 && 803e580952dSDimitry Andric !Function->hasAttr<AlwaysInlineAttr>() && 804ffd1746dSEd Schouten getFunctionLinkage(Function) 805ffd1746dSEd Schouten == llvm::Function::AvailableExternallyLinkage) 806ffd1746dSEd Schouten return; 807ffd1746dSEd Schouten 808ffd1746dSEd Schouten if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { 809bd5abe19SDimitry Andric // Make sure to emit the definition(s) before we emit the thunks. 810bd5abe19SDimitry Andric // This is necessary for the generation of certain thunks. 811bd5abe19SDimitry Andric if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Method)) 812bd5abe19SDimitry Andric EmitCXXConstructor(CD, GD.getCtorType()); 813bd5abe19SDimitry Andric else if (const CXXDestructorDecl *DD =dyn_cast<CXXDestructorDecl>(Method)) 814bd5abe19SDimitry Andric EmitCXXDestructor(DD, GD.getDtorType()); 815bd5abe19SDimitry Andric else 816bd5abe19SDimitry Andric EmitGlobalFunctionDefinition(GD); 817bd5abe19SDimitry Andric 818f22ef01cSRoman Divacky if (Method->isVirtual()) 819f22ef01cSRoman Divacky getVTables().EmitThunks(GD); 820f22ef01cSRoman Divacky 821bd5abe19SDimitry Andric return; 822ffd1746dSEd Schouten } 823f22ef01cSRoman Divacky 824f22ef01cSRoman Divacky return EmitGlobalFunctionDefinition(GD); 825ffd1746dSEd Schouten } 826f22ef01cSRoman Divacky 827f22ef01cSRoman Divacky if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 828f22ef01cSRoman Divacky return EmitGlobalVarDefinition(VD); 829f22ef01cSRoman Divacky 830f22ef01cSRoman Divacky assert(0 && "Invalid argument to EmitGlobalDefinition()"); 831f22ef01cSRoman Divacky } 832f22ef01cSRoman Divacky 833f22ef01cSRoman Divacky /// GetOrCreateLLVMFunction - If the specified mangled name is not in the 834f22ef01cSRoman Divacky /// module, create and return an llvm Function with the specified type. If there 835f22ef01cSRoman Divacky /// is something in the module with the specified name, return it potentially 836f22ef01cSRoman Divacky /// bitcasted to the right type. 837f22ef01cSRoman Divacky /// 838f22ef01cSRoman Divacky /// If D is non-null, it specifies a decl that correspond to this. This is used 839f22ef01cSRoman Divacky /// to set the attributes on the function when it is first created. 840f22ef01cSRoman Divacky llvm::Constant * 841f22ef01cSRoman Divacky CodeGenModule::GetOrCreateLLVMFunction(llvm::StringRef MangledName, 842f22ef01cSRoman Divacky const llvm::Type *Ty, 84317a519f9SDimitry Andric GlobalDecl D, bool ForVTable, 84417a519f9SDimitry Andric llvm::Attributes ExtraAttrs) { 845f22ef01cSRoman Divacky // Lookup the entry, lazily creating it if necessary. 846f22ef01cSRoman Divacky llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 847f22ef01cSRoman Divacky if (Entry) { 848f22ef01cSRoman Divacky if (WeakRefReferences.count(Entry)) { 849f22ef01cSRoman Divacky const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl()); 850f22ef01cSRoman Divacky if (FD && !FD->hasAttr<WeakAttr>()) 851f22ef01cSRoman Divacky Entry->setLinkage(llvm::Function::ExternalLinkage); 852f22ef01cSRoman Divacky 853f22ef01cSRoman Divacky WeakRefReferences.erase(Entry); 854f22ef01cSRoman Divacky } 855f22ef01cSRoman Divacky 856f22ef01cSRoman Divacky if (Entry->getType()->getElementType() == Ty) 857f22ef01cSRoman Divacky return Entry; 858f22ef01cSRoman Divacky 859f22ef01cSRoman Divacky // Make sure the result is of the correct type. 86017a519f9SDimitry Andric return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo()); 861f22ef01cSRoman Divacky } 862f22ef01cSRoman Divacky 863f22ef01cSRoman Divacky // This function doesn't have a complete type (for example, the return 864f22ef01cSRoman Divacky // type is an incomplete struct). Use a fake type instead, and make 865f22ef01cSRoman Divacky // sure not to try to set attributes. 866f22ef01cSRoman Divacky bool IsIncompleteFunction = false; 867f22ef01cSRoman Divacky 868f22ef01cSRoman Divacky const llvm::FunctionType *FTy; 869f22ef01cSRoman Divacky if (isa<llvm::FunctionType>(Ty)) { 870f22ef01cSRoman Divacky FTy = cast<llvm::FunctionType>(Ty); 871f22ef01cSRoman Divacky } else { 872bd5abe19SDimitry Andric FTy = llvm::FunctionType::get(VoidTy, false); 873f22ef01cSRoman Divacky IsIncompleteFunction = true; 874f22ef01cSRoman Divacky } 875ffd1746dSEd Schouten 876f22ef01cSRoman Divacky llvm::Function *F = llvm::Function::Create(FTy, 877f22ef01cSRoman Divacky llvm::Function::ExternalLinkage, 878f22ef01cSRoman Divacky MangledName, &getModule()); 879f22ef01cSRoman Divacky assert(F->getName() == MangledName && "name was uniqued!"); 880f22ef01cSRoman Divacky if (D.getDecl()) 881f22ef01cSRoman Divacky SetFunctionAttributes(D, F, IsIncompleteFunction); 88217a519f9SDimitry Andric if (ExtraAttrs != llvm::Attribute::None) 88317a519f9SDimitry Andric F->addFnAttr(ExtraAttrs); 884f22ef01cSRoman Divacky 885f22ef01cSRoman Divacky // This is the first use or definition of a mangled name. If there is a 886f22ef01cSRoman Divacky // deferred decl with this name, remember that we need to emit it at the end 887f22ef01cSRoman Divacky // of the file. 888f22ef01cSRoman Divacky llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); 889f22ef01cSRoman Divacky if (DDI != DeferredDecls.end()) { 890f22ef01cSRoman Divacky // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 891f22ef01cSRoman Divacky // list, and remove it from DeferredDecls (since we don't need it anymore). 892f22ef01cSRoman Divacky DeferredDeclsToEmit.push_back(DDI->second); 893f22ef01cSRoman Divacky DeferredDecls.erase(DDI); 8942754fe60SDimitry Andric 8952754fe60SDimitry Andric // Otherwise, there are cases we have to worry about where we're 8962754fe60SDimitry Andric // using a declaration for which we must emit a definition but where 8972754fe60SDimitry Andric // we might not find a top-level definition: 8982754fe60SDimitry Andric // - member functions defined inline in their classes 8992754fe60SDimitry Andric // - friend functions defined inline in some class 9002754fe60SDimitry Andric // - special member functions with implicit definitions 9012754fe60SDimitry Andric // If we ever change our AST traversal to walk into class methods, 9022754fe60SDimitry Andric // this will be unnecessary. 9032754fe60SDimitry Andric // 9042754fe60SDimitry Andric // We also don't emit a definition for a function if it's going to be an entry 9052754fe60SDimitry Andric // in a vtable, unless it's already marked as used. 9062754fe60SDimitry Andric } else if (getLangOptions().CPlusPlus && D.getDecl()) { 9072754fe60SDimitry Andric // Look for a declaration that's lexically in a record. 9082754fe60SDimitry Andric const FunctionDecl *FD = cast<FunctionDecl>(D.getDecl()); 9092754fe60SDimitry Andric do { 9102754fe60SDimitry Andric if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { 9112754fe60SDimitry Andric if (FD->isImplicit() && !ForVTable) { 9122754fe60SDimitry Andric assert(FD->isUsed() && "Sema didn't mark implicit function as used!"); 9132754fe60SDimitry Andric DeferredDeclsToEmit.push_back(D.getWithDecl(FD)); 9142754fe60SDimitry Andric break; 915bd5abe19SDimitry Andric } else if (FD->doesThisDeclarationHaveABody()) { 9162754fe60SDimitry Andric DeferredDeclsToEmit.push_back(D.getWithDecl(FD)); 9172754fe60SDimitry Andric break; 918f22ef01cSRoman Divacky } 919f22ef01cSRoman Divacky } 9202754fe60SDimitry Andric FD = FD->getPreviousDeclaration(); 9212754fe60SDimitry Andric } while (FD); 922f22ef01cSRoman Divacky } 923f22ef01cSRoman Divacky 924f22ef01cSRoman Divacky // Make sure the result is of the requested type. 925f22ef01cSRoman Divacky if (!IsIncompleteFunction) { 926f22ef01cSRoman Divacky assert(F->getType()->getElementType() == Ty); 927f22ef01cSRoman Divacky return F; 928f22ef01cSRoman Divacky } 929f22ef01cSRoman Divacky 93017a519f9SDimitry Andric llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 931f22ef01cSRoman Divacky return llvm::ConstantExpr::getBitCast(F, PTy); 932f22ef01cSRoman Divacky } 933f22ef01cSRoman Divacky 934f22ef01cSRoman Divacky /// GetAddrOfFunction - Return the address of the given function. If Ty is 935f22ef01cSRoman Divacky /// non-null, then this function will use the specified type if it has to 936f22ef01cSRoman Divacky /// create it (this occurs when we see a definition of the function). 937f22ef01cSRoman Divacky llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, 9382754fe60SDimitry Andric const llvm::Type *Ty, 9392754fe60SDimitry Andric bool ForVTable) { 940f22ef01cSRoman Divacky // If there was no specific requested type, just convert it now. 941f22ef01cSRoman Divacky if (!Ty) 942f22ef01cSRoman Divacky Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); 943ffd1746dSEd Schouten 944ffd1746dSEd Schouten llvm::StringRef MangledName = getMangledName(GD); 9452754fe60SDimitry Andric return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable); 946f22ef01cSRoman Divacky } 947f22ef01cSRoman Divacky 948f22ef01cSRoman Divacky /// CreateRuntimeFunction - Create a new runtime function with the specified 949f22ef01cSRoman Divacky /// type and name. 950f22ef01cSRoman Divacky llvm::Constant * 951f22ef01cSRoman Divacky CodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 95217a519f9SDimitry Andric llvm::StringRef Name, 95317a519f9SDimitry Andric llvm::Attributes ExtraAttrs) { 95417a519f9SDimitry Andric return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, 95517a519f9SDimitry Andric ExtraAttrs); 956f22ef01cSRoman Divacky } 957f22ef01cSRoman Divacky 958bd5abe19SDimitry Andric static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D, 959bd5abe19SDimitry Andric bool ConstantInit) { 960f22ef01cSRoman Divacky if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType()) 961f22ef01cSRoman Divacky return false; 962bd5abe19SDimitry Andric 963bd5abe19SDimitry Andric if (Context.getLangOptions().CPlusPlus) { 964bd5abe19SDimitry Andric if (const RecordType *Record 965bd5abe19SDimitry Andric = Context.getBaseElementType(D->getType())->getAs<RecordType>()) 966bd5abe19SDimitry Andric return ConstantInit && 967bd5abe19SDimitry Andric cast<CXXRecordDecl>(Record->getDecl())->isPOD() && 968bd5abe19SDimitry Andric !cast<CXXRecordDecl>(Record->getDecl())->hasMutableFields(); 969f22ef01cSRoman Divacky } 970bd5abe19SDimitry Andric 971f22ef01cSRoman Divacky return true; 972f22ef01cSRoman Divacky } 973f22ef01cSRoman Divacky 974f22ef01cSRoman Divacky /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 975f22ef01cSRoman Divacky /// create and return an llvm GlobalVariable with the specified type. If there 976f22ef01cSRoman Divacky /// is something in the module with the specified name, return it potentially 977f22ef01cSRoman Divacky /// bitcasted to the right type. 978f22ef01cSRoman Divacky /// 979f22ef01cSRoman Divacky /// If D is non-null, it specifies a decl that correspond to this. This is used 980f22ef01cSRoman Divacky /// to set the attributes on the global when it is first created. 981f22ef01cSRoman Divacky llvm::Constant * 982f22ef01cSRoman Divacky CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName, 983f22ef01cSRoman Divacky const llvm::PointerType *Ty, 9842754fe60SDimitry Andric const VarDecl *D, 9852754fe60SDimitry Andric bool UnnamedAddr) { 986f22ef01cSRoman Divacky // Lookup the entry, lazily creating it if necessary. 987f22ef01cSRoman Divacky llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 988f22ef01cSRoman Divacky if (Entry) { 989f22ef01cSRoman Divacky if (WeakRefReferences.count(Entry)) { 990f22ef01cSRoman Divacky if (D && !D->hasAttr<WeakAttr>()) 991f22ef01cSRoman Divacky Entry->setLinkage(llvm::Function::ExternalLinkage); 992f22ef01cSRoman Divacky 993f22ef01cSRoman Divacky WeakRefReferences.erase(Entry); 994f22ef01cSRoman Divacky } 995f22ef01cSRoman Divacky 9962754fe60SDimitry Andric if (UnnamedAddr) 9972754fe60SDimitry Andric Entry->setUnnamedAddr(true); 9982754fe60SDimitry Andric 999f22ef01cSRoman Divacky if (Entry->getType() == Ty) 1000f22ef01cSRoman Divacky return Entry; 1001f22ef01cSRoman Divacky 1002f22ef01cSRoman Divacky // Make sure the result is of the correct type. 1003f22ef01cSRoman Divacky return llvm::ConstantExpr::getBitCast(Entry, Ty); 1004f22ef01cSRoman Divacky } 1005f22ef01cSRoman Divacky 1006f22ef01cSRoman Divacky // This is the first use or definition of a mangled name. If there is a 1007f22ef01cSRoman Divacky // deferred decl with this name, remember that we need to emit it at the end 1008f22ef01cSRoman Divacky // of the file. 1009f22ef01cSRoman Divacky llvm::StringMap<GlobalDecl>::iterator DDI = DeferredDecls.find(MangledName); 1010f22ef01cSRoman Divacky if (DDI != DeferredDecls.end()) { 1011f22ef01cSRoman Divacky // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 1012f22ef01cSRoman Divacky // list, and remove it from DeferredDecls (since we don't need it anymore). 1013f22ef01cSRoman Divacky DeferredDeclsToEmit.push_back(DDI->second); 1014f22ef01cSRoman Divacky DeferredDecls.erase(DDI); 1015f22ef01cSRoman Divacky } 1016f22ef01cSRoman Divacky 1017f22ef01cSRoman Divacky llvm::GlobalVariable *GV = 1018f22ef01cSRoman Divacky new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, 1019f22ef01cSRoman Divacky llvm::GlobalValue::ExternalLinkage, 1020f22ef01cSRoman Divacky 0, MangledName, 0, 1021f22ef01cSRoman Divacky false, Ty->getAddressSpace()); 1022f22ef01cSRoman Divacky 1023f22ef01cSRoman Divacky // Handle things which are present even on external declarations. 1024f22ef01cSRoman Divacky if (D) { 1025f22ef01cSRoman Divacky // FIXME: This code is overly simple and should be merged with other global 1026f22ef01cSRoman Divacky // handling. 1027bd5abe19SDimitry Andric GV->setConstant(DeclIsConstantGlobal(Context, D, false)); 1028f22ef01cSRoman Divacky 10292754fe60SDimitry Andric // Set linkage and visibility in case we never see a definition. 10302754fe60SDimitry Andric NamedDecl::LinkageInfo LV = D->getLinkageAndVisibility(); 10312754fe60SDimitry Andric if (LV.linkage() != ExternalLinkage) { 10322754fe60SDimitry Andric // Don't set internal linkage on declarations. 10332754fe60SDimitry Andric } else { 10342754fe60SDimitry Andric if (D->hasAttr<DLLImportAttr>()) 10352754fe60SDimitry Andric GV->setLinkage(llvm::GlobalValue::DLLImportLinkage); 10363b0f4066SDimitry Andric else if (D->hasAttr<WeakAttr>() || D->isWeakImported()) 1037f22ef01cSRoman Divacky GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 1038f22ef01cSRoman Divacky 10392754fe60SDimitry Andric // Set visibility on a declaration only if it's explicit. 10402754fe60SDimitry Andric if (LV.visibilityExplicit()) 10412754fe60SDimitry Andric GV->setVisibility(GetLLVMVisibility(LV.visibility())); 10422754fe60SDimitry Andric } 10432754fe60SDimitry Andric 1044f22ef01cSRoman Divacky GV->setThreadLocal(D->isThreadSpecified()); 1045f22ef01cSRoman Divacky } 1046f22ef01cSRoman Divacky 1047f22ef01cSRoman Divacky return GV; 1048f22ef01cSRoman Divacky } 1049f22ef01cSRoman Divacky 1050f22ef01cSRoman Divacky 10512754fe60SDimitry Andric llvm::GlobalVariable * 10522754fe60SDimitry Andric CodeGenModule::CreateOrReplaceCXXRuntimeVariable(llvm::StringRef Name, 10532754fe60SDimitry Andric const llvm::Type *Ty, 10542754fe60SDimitry Andric llvm::GlobalValue::LinkageTypes Linkage) { 10552754fe60SDimitry Andric llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); 10562754fe60SDimitry Andric llvm::GlobalVariable *OldGV = 0; 10572754fe60SDimitry Andric 10582754fe60SDimitry Andric 10592754fe60SDimitry Andric if (GV) { 10602754fe60SDimitry Andric // Check if the variable has the right type. 10612754fe60SDimitry Andric if (GV->getType()->getElementType() == Ty) 10622754fe60SDimitry Andric return GV; 10632754fe60SDimitry Andric 10642754fe60SDimitry Andric // Because C++ name mangling, the only way we can end up with an already 10652754fe60SDimitry Andric // existing global with the same name is if it has been declared extern "C". 10662754fe60SDimitry Andric assert(GV->isDeclaration() && "Declaration has wrong type!"); 10672754fe60SDimitry Andric OldGV = GV; 10682754fe60SDimitry Andric } 10692754fe60SDimitry Andric 10702754fe60SDimitry Andric // Create a new variable. 10712754fe60SDimitry Andric GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, 10722754fe60SDimitry Andric Linkage, 0, Name); 10732754fe60SDimitry Andric 10742754fe60SDimitry Andric if (OldGV) { 10752754fe60SDimitry Andric // Replace occurrences of the old variable if needed. 10762754fe60SDimitry Andric GV->takeName(OldGV); 10772754fe60SDimitry Andric 10782754fe60SDimitry Andric if (!OldGV->use_empty()) { 10792754fe60SDimitry Andric llvm::Constant *NewPtrForOldDecl = 10802754fe60SDimitry Andric llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 10812754fe60SDimitry Andric OldGV->replaceAllUsesWith(NewPtrForOldDecl); 10822754fe60SDimitry Andric } 10832754fe60SDimitry Andric 10842754fe60SDimitry Andric OldGV->eraseFromParent(); 10852754fe60SDimitry Andric } 10862754fe60SDimitry Andric 10872754fe60SDimitry Andric return GV; 10882754fe60SDimitry Andric } 10892754fe60SDimitry Andric 1090f22ef01cSRoman Divacky /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 1091f22ef01cSRoman Divacky /// given global variable. If Ty is non-null and if the global doesn't exist, 1092f22ef01cSRoman Divacky /// then it will be greated with the specified type instead of whatever the 1093f22ef01cSRoman Divacky /// normal requested type would be. 1094f22ef01cSRoman Divacky llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 1095f22ef01cSRoman Divacky const llvm::Type *Ty) { 1096f22ef01cSRoman Divacky assert(D->hasGlobalStorage() && "Not a global variable"); 1097f22ef01cSRoman Divacky QualType ASTTy = D->getType(); 1098f22ef01cSRoman Divacky if (Ty == 0) 1099f22ef01cSRoman Divacky Ty = getTypes().ConvertTypeForMem(ASTTy); 1100f22ef01cSRoman Divacky 1101f22ef01cSRoman Divacky const llvm::PointerType *PTy = 11023b0f4066SDimitry Andric llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy)); 1103f22ef01cSRoman Divacky 1104ffd1746dSEd Schouten llvm::StringRef MangledName = getMangledName(D); 1105f22ef01cSRoman Divacky return GetOrCreateLLVMGlobal(MangledName, PTy, D); 1106f22ef01cSRoman Divacky } 1107f22ef01cSRoman Divacky 1108f22ef01cSRoman Divacky /// CreateRuntimeVariable - Create a new runtime global variable with the 1109f22ef01cSRoman Divacky /// specified type and name. 1110f22ef01cSRoman Divacky llvm::Constant * 1111f22ef01cSRoman Divacky CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 1112f22ef01cSRoman Divacky llvm::StringRef Name) { 11132754fe60SDimitry Andric return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0, 11142754fe60SDimitry Andric true); 1115f22ef01cSRoman Divacky } 1116f22ef01cSRoman Divacky 1117f22ef01cSRoman Divacky void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { 1118f22ef01cSRoman Divacky assert(!D->getInit() && "Cannot emit definite definitions here!"); 1119f22ef01cSRoman Divacky 1120f22ef01cSRoman Divacky if (MayDeferGeneration(D)) { 1121f22ef01cSRoman Divacky // If we have not seen a reference to this variable yet, place it 1122f22ef01cSRoman Divacky // into the deferred declarations table to be emitted if needed 1123f22ef01cSRoman Divacky // later. 1124ffd1746dSEd Schouten llvm::StringRef MangledName = getMangledName(D); 1125f22ef01cSRoman Divacky if (!GetGlobalValue(MangledName)) { 1126f22ef01cSRoman Divacky DeferredDecls[MangledName] = D; 1127f22ef01cSRoman Divacky return; 1128f22ef01cSRoman Divacky } 1129f22ef01cSRoman Divacky } 1130f22ef01cSRoman Divacky 1131f22ef01cSRoman Divacky // The tentative definition is the only definition. 1132f22ef01cSRoman Divacky EmitGlobalVarDefinition(D); 1133f22ef01cSRoman Divacky } 1134f22ef01cSRoman Divacky 1135f22ef01cSRoman Divacky void CodeGenModule::EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired) { 1136f22ef01cSRoman Divacky if (DefinitionRequired) 1137f22ef01cSRoman Divacky getVTables().GenerateClassData(getVTableLinkage(Class), Class); 1138f22ef01cSRoman Divacky } 1139f22ef01cSRoman Divacky 1140f22ef01cSRoman Divacky llvm::GlobalVariable::LinkageTypes 1141f22ef01cSRoman Divacky CodeGenModule::getVTableLinkage(const CXXRecordDecl *RD) { 1142bd5abe19SDimitry Andric if (RD->getLinkage() != ExternalLinkage) 1143f22ef01cSRoman Divacky return llvm::GlobalVariable::InternalLinkage; 1144f22ef01cSRoman Divacky 1145f22ef01cSRoman Divacky if (const CXXMethodDecl *KeyFunction 1146f22ef01cSRoman Divacky = RD->getASTContext().getKeyFunction(RD)) { 1147f22ef01cSRoman Divacky // If this class has a key function, use that to determine the linkage of 1148f22ef01cSRoman Divacky // the vtable. 1149f22ef01cSRoman Divacky const FunctionDecl *Def = 0; 1150ffd1746dSEd Schouten if (KeyFunction->hasBody(Def)) 1151f22ef01cSRoman Divacky KeyFunction = cast<CXXMethodDecl>(Def); 1152f22ef01cSRoman Divacky 1153f22ef01cSRoman Divacky switch (KeyFunction->getTemplateSpecializationKind()) { 1154f22ef01cSRoman Divacky case TSK_Undeclared: 1155f22ef01cSRoman Divacky case TSK_ExplicitSpecialization: 11562754fe60SDimitry Andric // When compiling with optimizations turned on, we emit all vtables, 11572754fe60SDimitry Andric // even if the key function is not defined in the current translation 11582754fe60SDimitry Andric // unit. If this is the case, use available_externally linkage. 11592754fe60SDimitry Andric if (!Def && CodeGenOpts.OptimizationLevel) 11602754fe60SDimitry Andric return llvm::GlobalVariable::AvailableExternallyLinkage; 11612754fe60SDimitry Andric 1162f22ef01cSRoman Divacky if (KeyFunction->isInlined()) 11632754fe60SDimitry Andric return !Context.getLangOptions().AppleKext ? 11642754fe60SDimitry Andric llvm::GlobalVariable::LinkOnceODRLinkage : 11652754fe60SDimitry Andric llvm::Function::InternalLinkage; 1166f22ef01cSRoman Divacky 1167f22ef01cSRoman Divacky return llvm::GlobalVariable::ExternalLinkage; 1168f22ef01cSRoman Divacky 1169f22ef01cSRoman Divacky case TSK_ImplicitInstantiation: 11702754fe60SDimitry Andric return !Context.getLangOptions().AppleKext ? 11712754fe60SDimitry Andric llvm::GlobalVariable::LinkOnceODRLinkage : 11722754fe60SDimitry Andric llvm::Function::InternalLinkage; 11732754fe60SDimitry Andric 1174f22ef01cSRoman Divacky case TSK_ExplicitInstantiationDefinition: 11752754fe60SDimitry Andric return !Context.getLangOptions().AppleKext ? 11762754fe60SDimitry Andric llvm::GlobalVariable::WeakODRLinkage : 11772754fe60SDimitry Andric llvm::Function::InternalLinkage; 1178f22ef01cSRoman Divacky 1179f22ef01cSRoman Divacky case TSK_ExplicitInstantiationDeclaration: 1180f22ef01cSRoman Divacky // FIXME: Use available_externally linkage. However, this currently 1181f22ef01cSRoman Divacky // breaks LLVM's build due to undefined symbols. 1182f22ef01cSRoman Divacky // return llvm::GlobalVariable::AvailableExternallyLinkage; 11832754fe60SDimitry Andric return !Context.getLangOptions().AppleKext ? 11842754fe60SDimitry Andric llvm::GlobalVariable::LinkOnceODRLinkage : 11852754fe60SDimitry Andric llvm::Function::InternalLinkage; 1186f22ef01cSRoman Divacky } 1187f22ef01cSRoman Divacky } 1188f22ef01cSRoman Divacky 11892754fe60SDimitry Andric if (Context.getLangOptions().AppleKext) 11902754fe60SDimitry Andric return llvm::Function::InternalLinkage; 11912754fe60SDimitry Andric 1192f22ef01cSRoman Divacky switch (RD->getTemplateSpecializationKind()) { 1193f22ef01cSRoman Divacky case TSK_Undeclared: 1194f22ef01cSRoman Divacky case TSK_ExplicitSpecialization: 1195f22ef01cSRoman Divacky case TSK_ImplicitInstantiation: 1196f22ef01cSRoman Divacky // FIXME: Use available_externally linkage. However, this currently 1197f22ef01cSRoman Divacky // breaks LLVM's build due to undefined symbols. 1198f22ef01cSRoman Divacky // return llvm::GlobalVariable::AvailableExternallyLinkage; 11992754fe60SDimitry Andric case TSK_ExplicitInstantiationDeclaration: 12002754fe60SDimitry Andric return llvm::GlobalVariable::LinkOnceODRLinkage; 12012754fe60SDimitry Andric 12022754fe60SDimitry Andric case TSK_ExplicitInstantiationDefinition: 1203f22ef01cSRoman Divacky return llvm::GlobalVariable::WeakODRLinkage; 1204f22ef01cSRoman Divacky } 1205f22ef01cSRoman Divacky 1206f22ef01cSRoman Divacky // Silence GCC warning. 12072754fe60SDimitry Andric return llvm::GlobalVariable::LinkOnceODRLinkage; 1208f22ef01cSRoman Divacky } 1209f22ef01cSRoman Divacky 1210f22ef01cSRoman Divacky CharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const { 12112754fe60SDimitry Andric return Context.toCharUnitsFromBits( 12122754fe60SDimitry Andric TheTargetData.getTypeStoreSizeInBits(Ty)); 1213f22ef01cSRoman Divacky } 1214f22ef01cSRoman Divacky 1215f22ef01cSRoman Divacky void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 1216f22ef01cSRoman Divacky llvm::Constant *Init = 0; 1217f22ef01cSRoman Divacky QualType ASTTy = D->getType(); 1218f22ef01cSRoman Divacky bool NonConstInit = false; 1219f22ef01cSRoman Divacky 1220f22ef01cSRoman Divacky const Expr *InitExpr = D->getAnyInitializer(); 1221f22ef01cSRoman Divacky 1222f22ef01cSRoman Divacky if (!InitExpr) { 1223f22ef01cSRoman Divacky // This is a tentative definition; tentative definitions are 1224f22ef01cSRoman Divacky // implicitly initialized with { 0 }. 1225f22ef01cSRoman Divacky // 1226f22ef01cSRoman Divacky // Note that tentative definitions are only emitted at the end of 1227f22ef01cSRoman Divacky // a translation unit, so they should never have incomplete 1228f22ef01cSRoman Divacky // type. In addition, EmitTentativeDefinition makes sure that we 1229f22ef01cSRoman Divacky // never attempt to emit a tentative definition if a real one 1230f22ef01cSRoman Divacky // exists. A use may still exists, however, so we still may need 1231f22ef01cSRoman Divacky // to do a RAUW. 1232f22ef01cSRoman Divacky assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); 1233f22ef01cSRoman Divacky Init = EmitNullConstant(D->getType()); 1234f22ef01cSRoman Divacky } else { 1235f22ef01cSRoman Divacky Init = EmitConstantExpr(InitExpr, D->getType()); 1236f22ef01cSRoman Divacky if (!Init) { 1237f22ef01cSRoman Divacky QualType T = InitExpr->getType(); 1238f22ef01cSRoman Divacky if (D->getType()->isReferenceType()) 1239f22ef01cSRoman Divacky T = D->getType(); 1240f22ef01cSRoman Divacky 1241f22ef01cSRoman Divacky if (getLangOptions().CPlusPlus) { 1242f22ef01cSRoman Divacky Init = EmitNullConstant(T); 1243f22ef01cSRoman Divacky NonConstInit = true; 1244f22ef01cSRoman Divacky } else { 1245f22ef01cSRoman Divacky ErrorUnsupported(D, "static initializer"); 1246f22ef01cSRoman Divacky Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 1247f22ef01cSRoman Divacky } 1248e580952dSDimitry Andric } else { 1249e580952dSDimitry Andric // We don't need an initializer, so remove the entry for the delayed 1250e580952dSDimitry Andric // initializer position (just in case this entry was delayed). 1251e580952dSDimitry Andric if (getLangOptions().CPlusPlus) 1252e580952dSDimitry Andric DelayedCXXInitPosition.erase(D); 1253f22ef01cSRoman Divacky } 1254f22ef01cSRoman Divacky } 1255f22ef01cSRoman Divacky 1256f22ef01cSRoman Divacky const llvm::Type* InitType = Init->getType(); 1257f22ef01cSRoman Divacky llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); 1258f22ef01cSRoman Divacky 1259f22ef01cSRoman Divacky // Strip off a bitcast if we got one back. 1260f22ef01cSRoman Divacky if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 1261f22ef01cSRoman Divacky assert(CE->getOpcode() == llvm::Instruction::BitCast || 1262f22ef01cSRoman Divacky // all zero index gep. 1263f22ef01cSRoman Divacky CE->getOpcode() == llvm::Instruction::GetElementPtr); 1264f22ef01cSRoman Divacky Entry = CE->getOperand(0); 1265f22ef01cSRoman Divacky } 1266f22ef01cSRoman Divacky 1267f22ef01cSRoman Divacky // Entry is now either a Function or GlobalVariable. 1268f22ef01cSRoman Divacky llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); 1269f22ef01cSRoman Divacky 1270f22ef01cSRoman Divacky // We have a definition after a declaration with the wrong type. 1271f22ef01cSRoman Divacky // We must make a new GlobalVariable* and update everything that used OldGV 1272f22ef01cSRoman Divacky // (a declaration or tentative definition) with the new GlobalVariable* 1273f22ef01cSRoman Divacky // (which will be a definition). 1274f22ef01cSRoman Divacky // 1275f22ef01cSRoman Divacky // This happens if there is a prototype for a global (e.g. 1276f22ef01cSRoman Divacky // "extern int x[];") and then a definition of a different type (e.g. 1277f22ef01cSRoman Divacky // "int x[10];"). This also happens when an initializer has a different type 1278f22ef01cSRoman Divacky // from the type of the global (this happens with unions). 1279f22ef01cSRoman Divacky if (GV == 0 || 1280f22ef01cSRoman Divacky GV->getType()->getElementType() != InitType || 12813b0f4066SDimitry Andric GV->getType()->getAddressSpace() != 12823b0f4066SDimitry Andric getContext().getTargetAddressSpace(ASTTy)) { 1283f22ef01cSRoman Divacky 1284f22ef01cSRoman Divacky // Move the old entry aside so that we'll create a new one. 1285f22ef01cSRoman Divacky Entry->setName(llvm::StringRef()); 1286f22ef01cSRoman Divacky 1287f22ef01cSRoman Divacky // Make a new global with the correct type, this is now guaranteed to work. 1288f22ef01cSRoman Divacky GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); 1289f22ef01cSRoman Divacky 1290f22ef01cSRoman Divacky // Replace all uses of the old global with the new global 1291f22ef01cSRoman Divacky llvm::Constant *NewPtrForOldDecl = 1292f22ef01cSRoman Divacky llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 1293f22ef01cSRoman Divacky Entry->replaceAllUsesWith(NewPtrForOldDecl); 1294f22ef01cSRoman Divacky 1295f22ef01cSRoman Divacky // Erase the old global, since it is no longer used. 1296f22ef01cSRoman Divacky cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 1297f22ef01cSRoman Divacky } 1298f22ef01cSRoman Divacky 1299f22ef01cSRoman Divacky if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 1300f22ef01cSRoman Divacky SourceManager &SM = Context.getSourceManager(); 1301f22ef01cSRoman Divacky AddAnnotation(EmitAnnotateAttr(GV, AA, 1302f22ef01cSRoman Divacky SM.getInstantiationLineNumber(D->getLocation()))); 1303f22ef01cSRoman Divacky } 1304f22ef01cSRoman Divacky 1305f22ef01cSRoman Divacky GV->setInitializer(Init); 1306f22ef01cSRoman Divacky 1307f22ef01cSRoman Divacky // If it is safe to mark the global 'constant', do so now. 1308f22ef01cSRoman Divacky GV->setConstant(false); 1309bd5abe19SDimitry Andric if (!NonConstInit && DeclIsConstantGlobal(Context, D, true)) 1310f22ef01cSRoman Divacky GV->setConstant(true); 1311f22ef01cSRoman Divacky 1312f22ef01cSRoman Divacky GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); 1313f22ef01cSRoman Divacky 1314f22ef01cSRoman Divacky // Set the llvm linkage type as appropriate. 13152754fe60SDimitry Andric llvm::GlobalValue::LinkageTypes Linkage = 13162754fe60SDimitry Andric GetLLVMLinkageVarDefinition(D, GV); 13172754fe60SDimitry Andric GV->setLinkage(Linkage); 13182754fe60SDimitry Andric if (Linkage == llvm::GlobalVariable::CommonLinkage) 1319f22ef01cSRoman Divacky // common vars aren't constant even if declared const. 1320f22ef01cSRoman Divacky GV->setConstant(false); 1321f22ef01cSRoman Divacky 1322f22ef01cSRoman Divacky SetCommonAttributes(D, GV); 1323f22ef01cSRoman Divacky 13242754fe60SDimitry Andric // Emit the initializer function if necessary. 13252754fe60SDimitry Andric if (NonConstInit) 13262754fe60SDimitry Andric EmitCXXGlobalVarDeclInitFunc(D, GV); 13272754fe60SDimitry Andric 1328f22ef01cSRoman Divacky // Emit global variable debug information. 13293b0f4066SDimitry Andric if (CGDebugInfo *DI = getModuleDebugInfo()) { 1330f22ef01cSRoman Divacky DI->setLocation(D->getLocation()); 1331f22ef01cSRoman Divacky DI->EmitGlobalVariable(GV, D); 1332f22ef01cSRoman Divacky } 1333f22ef01cSRoman Divacky } 1334f22ef01cSRoman Divacky 13352754fe60SDimitry Andric llvm::GlobalValue::LinkageTypes 13362754fe60SDimitry Andric CodeGenModule::GetLLVMLinkageVarDefinition(const VarDecl *D, 13372754fe60SDimitry Andric llvm::GlobalVariable *GV) { 13382754fe60SDimitry Andric GVALinkage Linkage = getContext().GetGVALinkageForVariable(D); 13392754fe60SDimitry Andric if (Linkage == GVA_Internal) 13402754fe60SDimitry Andric return llvm::Function::InternalLinkage; 13412754fe60SDimitry Andric else if (D->hasAttr<DLLImportAttr>()) 13422754fe60SDimitry Andric return llvm::Function::DLLImportLinkage; 13432754fe60SDimitry Andric else if (D->hasAttr<DLLExportAttr>()) 13442754fe60SDimitry Andric return llvm::Function::DLLExportLinkage; 13452754fe60SDimitry Andric else if (D->hasAttr<WeakAttr>()) { 13462754fe60SDimitry Andric if (GV->isConstant()) 13472754fe60SDimitry Andric return llvm::GlobalVariable::WeakODRLinkage; 13482754fe60SDimitry Andric else 13492754fe60SDimitry Andric return llvm::GlobalVariable::WeakAnyLinkage; 13502754fe60SDimitry Andric } else if (Linkage == GVA_TemplateInstantiation || 13512754fe60SDimitry Andric Linkage == GVA_ExplicitTemplateInstantiation) 13523b0f4066SDimitry Andric return llvm::GlobalVariable::WeakODRLinkage; 13532754fe60SDimitry Andric else if (!getLangOptions().CPlusPlus && 13542754fe60SDimitry Andric ((!CodeGenOpts.NoCommon && !D->getAttr<NoCommonAttr>()) || 13552754fe60SDimitry Andric D->getAttr<CommonAttr>()) && 13562754fe60SDimitry Andric !D->hasExternalStorage() && !D->getInit() && 135717a519f9SDimitry Andric !D->getAttr<SectionAttr>() && !D->isThreadSpecified() && 135817a519f9SDimitry Andric !D->getAttr<WeakImportAttr>()) { 13592754fe60SDimitry Andric // Thread local vars aren't considered common linkage. 13602754fe60SDimitry Andric return llvm::GlobalVariable::CommonLinkage; 13612754fe60SDimitry Andric } 13622754fe60SDimitry Andric return llvm::GlobalVariable::ExternalLinkage; 13632754fe60SDimitry Andric } 13642754fe60SDimitry Andric 1365f22ef01cSRoman Divacky /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we 1366f22ef01cSRoman Divacky /// implement a function with no prototype, e.g. "int foo() {}". If there are 1367f22ef01cSRoman Divacky /// existing call uses of the old function in the module, this adjusts them to 1368f22ef01cSRoman Divacky /// call the new function directly. 1369f22ef01cSRoman Divacky /// 1370f22ef01cSRoman Divacky /// This is not just a cleanup: the always_inline pass requires direct calls to 1371f22ef01cSRoman Divacky /// functions to be able to inline them. If there is a bitcast in the way, it 1372f22ef01cSRoman Divacky /// won't inline them. Instcombine normally deletes these calls, but it isn't 1373f22ef01cSRoman Divacky /// run at -O0. 1374f22ef01cSRoman Divacky static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 1375f22ef01cSRoman Divacky llvm::Function *NewFn) { 1376f22ef01cSRoman Divacky // If we're redefining a global as a function, don't transform it. 1377f22ef01cSRoman Divacky llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); 1378f22ef01cSRoman Divacky if (OldFn == 0) return; 1379f22ef01cSRoman Divacky 1380f22ef01cSRoman Divacky const llvm::Type *NewRetTy = NewFn->getReturnType(); 1381f22ef01cSRoman Divacky llvm::SmallVector<llvm::Value*, 4> ArgList; 1382f22ef01cSRoman Divacky 1383f22ef01cSRoman Divacky for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); 1384f22ef01cSRoman Divacky UI != E; ) { 1385f22ef01cSRoman Divacky // TODO: Do invokes ever occur in C code? If so, we should handle them too. 1386f22ef01cSRoman Divacky llvm::Value::use_iterator I = UI++; // Increment before the CI is erased. 1387f22ef01cSRoman Divacky llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*I); 1388e580952dSDimitry Andric if (!CI) continue; // FIXME: when we allow Invoke, just do CallSite CS(*I) 1389f22ef01cSRoman Divacky llvm::CallSite CS(CI); 1390f22ef01cSRoman Divacky if (!CI || !CS.isCallee(I)) continue; 1391f22ef01cSRoman Divacky 1392f22ef01cSRoman Divacky // If the return types don't match exactly, and if the call isn't dead, then 1393f22ef01cSRoman Divacky // we can't transform this call. 1394f22ef01cSRoman Divacky if (CI->getType() != NewRetTy && !CI->use_empty()) 1395f22ef01cSRoman Divacky continue; 1396f22ef01cSRoman Divacky 1397f22ef01cSRoman Divacky // If the function was passed too few arguments, don't transform. If extra 1398f22ef01cSRoman Divacky // arguments were passed, we silently drop them. If any of the types 1399f22ef01cSRoman Divacky // mismatch, we don't transform. 1400f22ef01cSRoman Divacky unsigned ArgNo = 0; 1401f22ef01cSRoman Divacky bool DontTransform = false; 1402f22ef01cSRoman Divacky for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), 1403f22ef01cSRoman Divacky E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { 1404f22ef01cSRoman Divacky if (CS.arg_size() == ArgNo || 1405f22ef01cSRoman Divacky CS.getArgument(ArgNo)->getType() != AI->getType()) { 1406f22ef01cSRoman Divacky DontTransform = true; 1407f22ef01cSRoman Divacky break; 1408f22ef01cSRoman Divacky } 1409f22ef01cSRoman Divacky } 1410f22ef01cSRoman Divacky if (DontTransform) 1411f22ef01cSRoman Divacky continue; 1412f22ef01cSRoman Divacky 1413f22ef01cSRoman Divacky // Okay, we can transform this. Create the new call instruction and copy 1414f22ef01cSRoman Divacky // over the required information. 1415f22ef01cSRoman Divacky ArgList.append(CS.arg_begin(), CS.arg_begin() + ArgNo); 141617a519f9SDimitry Andric llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList, "", CI); 1417f22ef01cSRoman Divacky ArgList.clear(); 1418f22ef01cSRoman Divacky if (!NewCall->getType()->isVoidTy()) 1419f22ef01cSRoman Divacky NewCall->takeName(CI); 1420f22ef01cSRoman Divacky NewCall->setAttributes(CI->getAttributes()); 1421f22ef01cSRoman Divacky NewCall->setCallingConv(CI->getCallingConv()); 1422f22ef01cSRoman Divacky 1423f22ef01cSRoman Divacky // Finally, remove the old call, replacing any uses with the new one. 1424f22ef01cSRoman Divacky if (!CI->use_empty()) 1425f22ef01cSRoman Divacky CI->replaceAllUsesWith(NewCall); 1426f22ef01cSRoman Divacky 1427f22ef01cSRoman Divacky // Copy debug location attached to CI. 1428f22ef01cSRoman Divacky if (!CI->getDebugLoc().isUnknown()) 1429f22ef01cSRoman Divacky NewCall->setDebugLoc(CI->getDebugLoc()); 1430f22ef01cSRoman Divacky CI->eraseFromParent(); 1431f22ef01cSRoman Divacky } 1432f22ef01cSRoman Divacky } 1433f22ef01cSRoman Divacky 1434f22ef01cSRoman Divacky 1435f22ef01cSRoman Divacky void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { 1436f22ef01cSRoman Divacky const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); 14373b0f4066SDimitry Andric 14383b0f4066SDimitry Andric // Compute the function info and LLVM type. 14393b0f4066SDimitry Andric const CGFunctionInfo &FI = getTypes().getFunctionInfo(GD); 14403b0f4066SDimitry Andric bool variadic = false; 14413b0f4066SDimitry Andric if (const FunctionProtoType *fpt = D->getType()->getAs<FunctionProtoType>()) 14423b0f4066SDimitry Andric variadic = fpt->isVariadic(); 144317a519f9SDimitry Andric const llvm::FunctionType *Ty = getTypes().GetFunctionType(FI, variadic); 14443b0f4066SDimitry Andric 1445f22ef01cSRoman Divacky // Get or create the prototype for the function. 1446f22ef01cSRoman Divacky llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); 1447f22ef01cSRoman Divacky 1448f22ef01cSRoman Divacky // Strip off a bitcast if we got one back. 1449f22ef01cSRoman Divacky if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 1450f22ef01cSRoman Divacky assert(CE->getOpcode() == llvm::Instruction::BitCast); 1451f22ef01cSRoman Divacky Entry = CE->getOperand(0); 1452f22ef01cSRoman Divacky } 1453f22ef01cSRoman Divacky 1454f22ef01cSRoman Divacky 1455f22ef01cSRoman Divacky if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { 1456f22ef01cSRoman Divacky llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); 1457f22ef01cSRoman Divacky 1458f22ef01cSRoman Divacky // If the types mismatch then we have to rewrite the definition. 1459f22ef01cSRoman Divacky assert(OldFn->isDeclaration() && 1460f22ef01cSRoman Divacky "Shouldn't replace non-declaration"); 1461f22ef01cSRoman Divacky 1462f22ef01cSRoman Divacky // F is the Function* for the one with the wrong type, we must make a new 1463f22ef01cSRoman Divacky // Function* and update everything that used F (a declaration) with the new 1464f22ef01cSRoman Divacky // Function* (which will be a definition). 1465f22ef01cSRoman Divacky // 1466f22ef01cSRoman Divacky // This happens if there is a prototype for a function 1467f22ef01cSRoman Divacky // (e.g. "int f()") and then a definition of a different type 1468f22ef01cSRoman Divacky // (e.g. "int f(int x)"). Move the old function aside so that it 1469f22ef01cSRoman Divacky // doesn't interfere with GetAddrOfFunction. 1470f22ef01cSRoman Divacky OldFn->setName(llvm::StringRef()); 1471f22ef01cSRoman Divacky llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); 1472f22ef01cSRoman Divacky 1473f22ef01cSRoman Divacky // If this is an implementation of a function without a prototype, try to 1474f22ef01cSRoman Divacky // replace any existing uses of the function (which may be calls) with uses 1475f22ef01cSRoman Divacky // of the new function 1476f22ef01cSRoman Divacky if (D->getType()->isFunctionNoProtoType()) { 1477f22ef01cSRoman Divacky ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); 1478f22ef01cSRoman Divacky OldFn->removeDeadConstantUsers(); 1479f22ef01cSRoman Divacky } 1480f22ef01cSRoman Divacky 1481f22ef01cSRoman Divacky // Replace uses of F with the Function we will endow with a body. 1482f22ef01cSRoman Divacky if (!Entry->use_empty()) { 1483f22ef01cSRoman Divacky llvm::Constant *NewPtrForOldDecl = 1484f22ef01cSRoman Divacky llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 1485f22ef01cSRoman Divacky Entry->replaceAllUsesWith(NewPtrForOldDecl); 1486f22ef01cSRoman Divacky } 1487f22ef01cSRoman Divacky 1488f22ef01cSRoman Divacky // Ok, delete the old function now, which is dead. 1489f22ef01cSRoman Divacky OldFn->eraseFromParent(); 1490f22ef01cSRoman Divacky 1491f22ef01cSRoman Divacky Entry = NewFn; 1492f22ef01cSRoman Divacky } 1493f22ef01cSRoman Divacky 14942754fe60SDimitry Andric // We need to set linkage and visibility on the function before 14952754fe60SDimitry Andric // generating code for it because various parts of IR generation 14962754fe60SDimitry Andric // want to propagate this information down (e.g. to local static 14972754fe60SDimitry Andric // declarations). 1498f22ef01cSRoman Divacky llvm::Function *Fn = cast<llvm::Function>(Entry); 1499f22ef01cSRoman Divacky setFunctionLinkage(D, Fn); 1500f22ef01cSRoman Divacky 15012754fe60SDimitry Andric // FIXME: this is redundant with part of SetFunctionDefinitionAttributes 15022754fe60SDimitry Andric setGlobalVisibility(Fn, D); 15032754fe60SDimitry Andric 15043b0f4066SDimitry Andric CodeGenFunction(*this).GenerateCode(D, Fn, FI); 1505f22ef01cSRoman Divacky 1506f22ef01cSRoman Divacky SetFunctionDefinitionAttributes(D, Fn); 1507f22ef01cSRoman Divacky SetLLVMFunctionAttributesForDefinition(D, Fn); 1508f22ef01cSRoman Divacky 1509f22ef01cSRoman Divacky if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 1510f22ef01cSRoman Divacky AddGlobalCtor(Fn, CA->getPriority()); 1511f22ef01cSRoman Divacky if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 1512f22ef01cSRoman Divacky AddGlobalDtor(Fn, DA->getPriority()); 1513f22ef01cSRoman Divacky } 1514f22ef01cSRoman Divacky 1515f22ef01cSRoman Divacky void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { 1516f22ef01cSRoman Divacky const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 1517f22ef01cSRoman Divacky const AliasAttr *AA = D->getAttr<AliasAttr>(); 1518f22ef01cSRoman Divacky assert(AA && "Not an alias?"); 1519f22ef01cSRoman Divacky 1520ffd1746dSEd Schouten llvm::StringRef MangledName = getMangledName(GD); 1521f22ef01cSRoman Divacky 1522f22ef01cSRoman Divacky // If there is a definition in the module, then it wins over the alias. 1523f22ef01cSRoman Divacky // This is dubious, but allow it to be safe. Just ignore the alias. 1524f22ef01cSRoman Divacky llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 1525f22ef01cSRoman Divacky if (Entry && !Entry->isDeclaration()) 1526f22ef01cSRoman Divacky return; 1527f22ef01cSRoman Divacky 1528f22ef01cSRoman Divacky const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 1529f22ef01cSRoman Divacky 1530f22ef01cSRoman Divacky // Create a reference to the named value. This ensures that it is emitted 1531f22ef01cSRoman Divacky // if a deferred decl. 1532f22ef01cSRoman Divacky llvm::Constant *Aliasee; 1533f22ef01cSRoman Divacky if (isa<llvm::FunctionType>(DeclTy)) 15342754fe60SDimitry Andric Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GlobalDecl(), 15352754fe60SDimitry Andric /*ForVTable=*/false); 1536f22ef01cSRoman Divacky else 1537f22ef01cSRoman Divacky Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), 1538f22ef01cSRoman Divacky llvm::PointerType::getUnqual(DeclTy), 0); 1539f22ef01cSRoman Divacky 1540f22ef01cSRoman Divacky // Create the new alias itself, but don't set a name yet. 1541f22ef01cSRoman Divacky llvm::GlobalValue *GA = 1542f22ef01cSRoman Divacky new llvm::GlobalAlias(Aliasee->getType(), 1543f22ef01cSRoman Divacky llvm::Function::ExternalLinkage, 1544f22ef01cSRoman Divacky "", Aliasee, &getModule()); 1545f22ef01cSRoman Divacky 1546f22ef01cSRoman Divacky if (Entry) { 1547f22ef01cSRoman Divacky assert(Entry->isDeclaration()); 1548f22ef01cSRoman Divacky 1549f22ef01cSRoman Divacky // If there is a declaration in the module, then we had an extern followed 1550f22ef01cSRoman Divacky // by the alias, as in: 1551f22ef01cSRoman Divacky // extern int test6(); 1552f22ef01cSRoman Divacky // ... 1553f22ef01cSRoman Divacky // int test6() __attribute__((alias("test7"))); 1554f22ef01cSRoman Divacky // 1555f22ef01cSRoman Divacky // Remove it and replace uses of it with the alias. 1556f22ef01cSRoman Divacky GA->takeName(Entry); 1557f22ef01cSRoman Divacky 1558f22ef01cSRoman Divacky Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 1559f22ef01cSRoman Divacky Entry->getType())); 1560f22ef01cSRoman Divacky Entry->eraseFromParent(); 1561f22ef01cSRoman Divacky } else { 1562ffd1746dSEd Schouten GA->setName(MangledName); 1563f22ef01cSRoman Divacky } 1564f22ef01cSRoman Divacky 1565f22ef01cSRoman Divacky // Set attributes which are particular to an alias; this is a 1566f22ef01cSRoman Divacky // specialization of the attributes which may be set on a global 1567f22ef01cSRoman Divacky // variable/function. 1568f22ef01cSRoman Divacky if (D->hasAttr<DLLExportAttr>()) { 1569f22ef01cSRoman Divacky if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 1570f22ef01cSRoman Divacky // The dllexport attribute is ignored for undefined symbols. 1571ffd1746dSEd Schouten if (FD->hasBody()) 1572f22ef01cSRoman Divacky GA->setLinkage(llvm::Function::DLLExportLinkage); 1573f22ef01cSRoman Divacky } else { 1574f22ef01cSRoman Divacky GA->setLinkage(llvm::Function::DLLExportLinkage); 1575f22ef01cSRoman Divacky } 1576f22ef01cSRoman Divacky } else if (D->hasAttr<WeakAttr>() || 1577f22ef01cSRoman Divacky D->hasAttr<WeakRefAttr>() || 15783b0f4066SDimitry Andric D->isWeakImported()) { 1579f22ef01cSRoman Divacky GA->setLinkage(llvm::Function::WeakAnyLinkage); 1580f22ef01cSRoman Divacky } 1581f22ef01cSRoman Divacky 1582f22ef01cSRoman Divacky SetCommonAttributes(D, GA); 1583f22ef01cSRoman Divacky } 1584f22ef01cSRoman Divacky 1585f22ef01cSRoman Divacky /// getBuiltinLibFunction - Given a builtin id for a function like 1586f22ef01cSRoman Divacky /// "__builtin_fabsf", return a Function* for "fabsf". 1587f22ef01cSRoman Divacky llvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, 1588f22ef01cSRoman Divacky unsigned BuiltinID) { 1589f22ef01cSRoman Divacky assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 1590f22ef01cSRoman Divacky Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 1591f22ef01cSRoman Divacky "isn't a lib fn"); 1592f22ef01cSRoman Divacky 1593f22ef01cSRoman Divacky // Get the name, skip over the __builtin_ prefix (if necessary). 1594bd5abe19SDimitry Andric llvm::StringRef Name; 1595bd5abe19SDimitry Andric GlobalDecl D(FD); 1596bd5abe19SDimitry Andric 1597bd5abe19SDimitry Andric // If the builtin has been declared explicitly with an assembler label, 1598bd5abe19SDimitry Andric // use the mangled name. This differs from the plain label on platforms 1599bd5abe19SDimitry Andric // that prefix labels. 1600bd5abe19SDimitry Andric if (FD->hasAttr<AsmLabelAttr>()) 1601bd5abe19SDimitry Andric Name = getMangledName(D); 1602bd5abe19SDimitry Andric else if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 1603bd5abe19SDimitry Andric Name = Context.BuiltinInfo.GetName(BuiltinID) + 10; 1604bd5abe19SDimitry Andric else 1605bd5abe19SDimitry Andric Name = Context.BuiltinInfo.GetName(BuiltinID); 1606bd5abe19SDimitry Andric 1607f22ef01cSRoman Divacky 1608f22ef01cSRoman Divacky const llvm::FunctionType *Ty = 1609f22ef01cSRoman Divacky cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType())); 1610f22ef01cSRoman Divacky 1611bd5abe19SDimitry Andric return GetOrCreateLLVMFunction(Name, Ty, D, /*ForVTable=*/false); 1612f22ef01cSRoman Divacky } 1613f22ef01cSRoman Divacky 161417a519f9SDimitry Andric llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, 161517a519f9SDimitry Andric llvm::ArrayRef<llvm::Type*> Tys) { 161617a519f9SDimitry Andric return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, 161717a519f9SDimitry Andric Tys); 1618f22ef01cSRoman Divacky } 1619f22ef01cSRoman Divacky 1620f22ef01cSRoman Divacky static llvm::StringMapEntry<llvm::Constant*> & 1621f22ef01cSRoman Divacky GetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, 1622f22ef01cSRoman Divacky const StringLiteral *Literal, 1623f22ef01cSRoman Divacky bool TargetIsLSB, 1624f22ef01cSRoman Divacky bool &IsUTF16, 1625f22ef01cSRoman Divacky unsigned &StringLength) { 1626e580952dSDimitry Andric llvm::StringRef String = Literal->getString(); 1627e580952dSDimitry Andric unsigned NumBytes = String.size(); 1628f22ef01cSRoman Divacky 1629f22ef01cSRoman Divacky // Check for simple case. 1630f22ef01cSRoman Divacky if (!Literal->containsNonAsciiOrNull()) { 1631f22ef01cSRoman Divacky StringLength = NumBytes; 1632e580952dSDimitry Andric return Map.GetOrCreateValue(String); 1633f22ef01cSRoman Divacky } 1634f22ef01cSRoman Divacky 1635f22ef01cSRoman Divacky // Otherwise, convert the UTF8 literals into a byte string. 1636f22ef01cSRoman Divacky llvm::SmallVector<UTF16, 128> ToBuf(NumBytes); 1637e580952dSDimitry Andric const UTF8 *FromPtr = (UTF8 *)String.data(); 1638f22ef01cSRoman Divacky UTF16 *ToPtr = &ToBuf[0]; 1639f22ef01cSRoman Divacky 16402754fe60SDimitry Andric (void)ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, 1641f22ef01cSRoman Divacky &ToPtr, ToPtr + NumBytes, 1642f22ef01cSRoman Divacky strictConversion); 1643f22ef01cSRoman Divacky 1644f22ef01cSRoman Divacky // ConvertUTF8toUTF16 returns the length in ToPtr. 1645f22ef01cSRoman Divacky StringLength = ToPtr - &ToBuf[0]; 1646f22ef01cSRoman Divacky 1647f22ef01cSRoman Divacky // Render the UTF-16 string into a byte array and convert to the target byte 1648f22ef01cSRoman Divacky // order. 1649f22ef01cSRoman Divacky // 1650f22ef01cSRoman Divacky // FIXME: This isn't something we should need to do here. 1651f22ef01cSRoman Divacky llvm::SmallString<128> AsBytes; 1652f22ef01cSRoman Divacky AsBytes.reserve(StringLength * 2); 1653f22ef01cSRoman Divacky for (unsigned i = 0; i != StringLength; ++i) { 1654f22ef01cSRoman Divacky unsigned short Val = ToBuf[i]; 1655f22ef01cSRoman Divacky if (TargetIsLSB) { 1656f22ef01cSRoman Divacky AsBytes.push_back(Val & 0xFF); 1657f22ef01cSRoman Divacky AsBytes.push_back(Val >> 8); 1658f22ef01cSRoman Divacky } else { 1659f22ef01cSRoman Divacky AsBytes.push_back(Val >> 8); 1660f22ef01cSRoman Divacky AsBytes.push_back(Val & 0xFF); 1661f22ef01cSRoman Divacky } 1662f22ef01cSRoman Divacky } 1663f22ef01cSRoman Divacky // Append one extra null character, the second is automatically added by our 1664f22ef01cSRoman Divacky // caller. 1665f22ef01cSRoman Divacky AsBytes.push_back(0); 1666f22ef01cSRoman Divacky 1667f22ef01cSRoman Divacky IsUTF16 = true; 1668f22ef01cSRoman Divacky return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size())); 1669f22ef01cSRoman Divacky } 1670f22ef01cSRoman Divacky 1671bd5abe19SDimitry Andric static llvm::StringMapEntry<llvm::Constant*> & 1672bd5abe19SDimitry Andric GetConstantStringEntry(llvm::StringMap<llvm::Constant*> &Map, 1673bd5abe19SDimitry Andric const StringLiteral *Literal, 1674bd5abe19SDimitry Andric unsigned &StringLength) 1675bd5abe19SDimitry Andric { 1676bd5abe19SDimitry Andric llvm::StringRef String = Literal->getString(); 1677bd5abe19SDimitry Andric StringLength = String.size(); 1678bd5abe19SDimitry Andric return Map.GetOrCreateValue(String); 1679bd5abe19SDimitry Andric } 1680bd5abe19SDimitry Andric 1681f22ef01cSRoman Divacky llvm::Constant * 1682f22ef01cSRoman Divacky CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { 1683f22ef01cSRoman Divacky unsigned StringLength = 0; 1684f22ef01cSRoman Divacky bool isUTF16 = false; 1685f22ef01cSRoman Divacky llvm::StringMapEntry<llvm::Constant*> &Entry = 1686f22ef01cSRoman Divacky GetConstantCFStringEntry(CFConstantStringMap, Literal, 1687f22ef01cSRoman Divacky getTargetData().isLittleEndian(), 1688f22ef01cSRoman Divacky isUTF16, StringLength); 1689f22ef01cSRoman Divacky 1690f22ef01cSRoman Divacky if (llvm::Constant *C = Entry.getValue()) 1691f22ef01cSRoman Divacky return C; 1692f22ef01cSRoman Divacky 1693f22ef01cSRoman Divacky llvm::Constant *Zero = 1694f22ef01cSRoman Divacky llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 1695f22ef01cSRoman Divacky llvm::Constant *Zeros[] = { Zero, Zero }; 1696f22ef01cSRoman Divacky 1697f22ef01cSRoman Divacky // If we don't already have it, get __CFConstantStringClassReference. 1698f22ef01cSRoman Divacky if (!CFConstantStringClassRef) { 1699f22ef01cSRoman Divacky const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 1700f22ef01cSRoman Divacky Ty = llvm::ArrayType::get(Ty, 0); 1701f22ef01cSRoman Divacky llvm::Constant *GV = CreateRuntimeVariable(Ty, 1702f22ef01cSRoman Divacky "__CFConstantStringClassReference"); 1703f22ef01cSRoman Divacky // Decay array -> ptr 1704f22ef01cSRoman Divacky CFConstantStringClassRef = 1705f22ef01cSRoman Divacky llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1706f22ef01cSRoman Divacky } 1707f22ef01cSRoman Divacky 1708f22ef01cSRoman Divacky QualType CFTy = getContext().getCFConstantStringType(); 1709f22ef01cSRoman Divacky 1710f22ef01cSRoman Divacky const llvm::StructType *STy = 1711f22ef01cSRoman Divacky cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1712f22ef01cSRoman Divacky 1713f22ef01cSRoman Divacky std::vector<llvm::Constant*> Fields(4); 1714f22ef01cSRoman Divacky 1715f22ef01cSRoman Divacky // Class pointer. 1716f22ef01cSRoman Divacky Fields[0] = CFConstantStringClassRef; 1717f22ef01cSRoman Divacky 1718f22ef01cSRoman Divacky // Flags. 1719f22ef01cSRoman Divacky const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 1720f22ef01cSRoman Divacky Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : 1721f22ef01cSRoman Divacky llvm::ConstantInt::get(Ty, 0x07C8); 1722f22ef01cSRoman Divacky 1723f22ef01cSRoman Divacky // String pointer. 1724f22ef01cSRoman Divacky llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 1725f22ef01cSRoman Divacky 1726f22ef01cSRoman Divacky llvm::GlobalValue::LinkageTypes Linkage; 1727f22ef01cSRoman Divacky bool isConstant; 1728f22ef01cSRoman Divacky if (isUTF16) { 1729f22ef01cSRoman Divacky // FIXME: why do utf strings get "_" labels instead of "L" labels? 1730f22ef01cSRoman Divacky Linkage = llvm::GlobalValue::InternalLinkage; 1731f22ef01cSRoman Divacky // Note: -fwritable-strings doesn't make unicode CFStrings writable, but 1732f22ef01cSRoman Divacky // does make plain ascii ones writable. 1733f22ef01cSRoman Divacky isConstant = true; 1734f22ef01cSRoman Divacky } else { 17353b0f4066SDimitry Andric // FIXME: With OS X ld 123.2 (xcode 4) and LTO we would get a linker error 17363b0f4066SDimitry Andric // when using private linkage. It is not clear if this is a bug in ld 17373b0f4066SDimitry Andric // or a reasonable new restriction. 17383b0f4066SDimitry Andric Linkage = llvm::GlobalValue::LinkerPrivateLinkage; 1739f22ef01cSRoman Divacky isConstant = !Features.WritableStrings; 1740f22ef01cSRoman Divacky } 1741f22ef01cSRoman Divacky 1742f22ef01cSRoman Divacky llvm::GlobalVariable *GV = 1743f22ef01cSRoman Divacky new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 1744f22ef01cSRoman Divacky ".str"); 17452754fe60SDimitry Andric GV->setUnnamedAddr(true); 1746f22ef01cSRoman Divacky if (isUTF16) { 1747f22ef01cSRoman Divacky CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); 1748f22ef01cSRoman Divacky GV->setAlignment(Align.getQuantity()); 17493b0f4066SDimitry Andric } else { 17503b0f4066SDimitry Andric CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy); 17513b0f4066SDimitry Andric GV->setAlignment(Align.getQuantity()); 1752f22ef01cSRoman Divacky } 1753f22ef01cSRoman Divacky Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1754f22ef01cSRoman Divacky 1755f22ef01cSRoman Divacky // String length. 1756f22ef01cSRoman Divacky Ty = getTypes().ConvertType(getContext().LongTy); 1757f22ef01cSRoman Divacky Fields[3] = llvm::ConstantInt::get(Ty, StringLength); 1758f22ef01cSRoman Divacky 1759f22ef01cSRoman Divacky // The struct. 1760f22ef01cSRoman Divacky C = llvm::ConstantStruct::get(STy, Fields); 1761f22ef01cSRoman Divacky GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 1762f22ef01cSRoman Divacky llvm::GlobalVariable::PrivateLinkage, C, 1763f22ef01cSRoman Divacky "_unnamed_cfstring_"); 1764f22ef01cSRoman Divacky if (const char *Sect = getContext().Target.getCFStringSection()) 1765f22ef01cSRoman Divacky GV->setSection(Sect); 1766f22ef01cSRoman Divacky Entry.setValue(GV); 1767f22ef01cSRoman Divacky 1768f22ef01cSRoman Divacky return GV; 1769f22ef01cSRoman Divacky } 1770f22ef01cSRoman Divacky 1771f22ef01cSRoman Divacky llvm::Constant * 17722754fe60SDimitry Andric CodeGenModule::GetAddrOfConstantString(const StringLiteral *Literal) { 1773f22ef01cSRoman Divacky unsigned StringLength = 0; 1774f22ef01cSRoman Divacky llvm::StringMapEntry<llvm::Constant*> &Entry = 1775bd5abe19SDimitry Andric GetConstantStringEntry(CFConstantStringMap, Literal, StringLength); 1776f22ef01cSRoman Divacky 1777f22ef01cSRoman Divacky if (llvm::Constant *C = Entry.getValue()) 1778f22ef01cSRoman Divacky return C; 1779f22ef01cSRoman Divacky 1780f22ef01cSRoman Divacky llvm::Constant *Zero = 1781f22ef01cSRoman Divacky llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 1782f22ef01cSRoman Divacky llvm::Constant *Zeros[] = { Zero, Zero }; 1783f22ef01cSRoman Divacky 1784f22ef01cSRoman Divacky // If we don't already have it, get _NSConstantStringClassReference. 17852754fe60SDimitry Andric if (!ConstantStringClassRef) { 17862754fe60SDimitry Andric std::string StringClass(getLangOptions().ObjCConstantStringClass); 1787f22ef01cSRoman Divacky const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 17882754fe60SDimitry Andric llvm::Constant *GV; 1789bd5abe19SDimitry Andric if (Features.ObjCNonFragileABI) { 1790bd5abe19SDimitry Andric std::string str = 1791bd5abe19SDimitry Andric StringClass.empty() ? "OBJC_CLASS_$_NSConstantString" 1792bd5abe19SDimitry Andric : "OBJC_CLASS_$_" + StringClass; 1793bd5abe19SDimitry Andric GV = getObjCRuntime().GetClassGlobal(str); 1794bd5abe19SDimitry Andric // Make sure the result is of the correct type. 1795bd5abe19SDimitry Andric const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 1796bd5abe19SDimitry Andric ConstantStringClassRef = 1797bd5abe19SDimitry Andric llvm::ConstantExpr::getBitCast(GV, PTy); 1798bd5abe19SDimitry Andric } else { 1799bd5abe19SDimitry Andric std::string str = 1800bd5abe19SDimitry Andric StringClass.empty() ? "_NSConstantStringClassReference" 1801bd5abe19SDimitry Andric : "_" + StringClass + "ClassReference"; 1802bd5abe19SDimitry Andric const llvm::Type *PTy = llvm::ArrayType::get(Ty, 0); 1803bd5abe19SDimitry Andric GV = CreateRuntimeVariable(PTy, str); 1804f22ef01cSRoman Divacky // Decay array -> ptr 18052754fe60SDimitry Andric ConstantStringClassRef = 1806f22ef01cSRoman Divacky llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1807f22ef01cSRoman Divacky } 1808bd5abe19SDimitry Andric } 1809f22ef01cSRoman Divacky 1810f22ef01cSRoman Divacky QualType NSTy = getContext().getNSConstantStringType(); 1811f22ef01cSRoman Divacky 1812f22ef01cSRoman Divacky const llvm::StructType *STy = 1813f22ef01cSRoman Divacky cast<llvm::StructType>(getTypes().ConvertType(NSTy)); 1814f22ef01cSRoman Divacky 1815f22ef01cSRoman Divacky std::vector<llvm::Constant*> Fields(3); 1816f22ef01cSRoman Divacky 1817f22ef01cSRoman Divacky // Class pointer. 18182754fe60SDimitry Andric Fields[0] = ConstantStringClassRef; 1819f22ef01cSRoman Divacky 1820f22ef01cSRoman Divacky // String pointer. 1821f22ef01cSRoman Divacky llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 1822f22ef01cSRoman Divacky 1823f22ef01cSRoman Divacky llvm::GlobalValue::LinkageTypes Linkage; 1824f22ef01cSRoman Divacky bool isConstant; 1825f22ef01cSRoman Divacky Linkage = llvm::GlobalValue::PrivateLinkage; 1826f22ef01cSRoman Divacky isConstant = !Features.WritableStrings; 1827f22ef01cSRoman Divacky 1828f22ef01cSRoman Divacky llvm::GlobalVariable *GV = 1829f22ef01cSRoman Divacky new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 1830f22ef01cSRoman Divacky ".str"); 18312754fe60SDimitry Andric GV->setUnnamedAddr(true); 18323b0f4066SDimitry Andric CharUnits Align = getContext().getTypeAlignInChars(getContext().CharTy); 18333b0f4066SDimitry Andric GV->setAlignment(Align.getQuantity()); 1834f22ef01cSRoman Divacky Fields[1] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1835f22ef01cSRoman Divacky 1836f22ef01cSRoman Divacky // String length. 1837f22ef01cSRoman Divacky const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 1838f22ef01cSRoman Divacky Fields[2] = llvm::ConstantInt::get(Ty, StringLength); 1839f22ef01cSRoman Divacky 1840f22ef01cSRoman Divacky // The struct. 1841f22ef01cSRoman Divacky C = llvm::ConstantStruct::get(STy, Fields); 1842f22ef01cSRoman Divacky GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 1843f22ef01cSRoman Divacky llvm::GlobalVariable::PrivateLinkage, C, 1844f22ef01cSRoman Divacky "_unnamed_nsstring_"); 1845f22ef01cSRoman Divacky // FIXME. Fix section. 1846f22ef01cSRoman Divacky if (const char *Sect = 1847f22ef01cSRoman Divacky Features.ObjCNonFragileABI 1848f22ef01cSRoman Divacky ? getContext().Target.getNSStringNonFragileABISection() 1849f22ef01cSRoman Divacky : getContext().Target.getNSStringSection()) 1850f22ef01cSRoman Divacky GV->setSection(Sect); 1851f22ef01cSRoman Divacky Entry.setValue(GV); 1852f22ef01cSRoman Divacky 1853f22ef01cSRoman Divacky return GV; 1854f22ef01cSRoman Divacky } 1855f22ef01cSRoman Divacky 1856f22ef01cSRoman Divacky /// GetStringForStringLiteral - Return the appropriate bytes for a 1857f22ef01cSRoman Divacky /// string literal, properly padded to match the literal type. 1858f22ef01cSRoman Divacky std::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 18592754fe60SDimitry Andric const ASTContext &Context = getContext(); 1860f22ef01cSRoman Divacky const ConstantArrayType *CAT = 18612754fe60SDimitry Andric Context.getAsConstantArrayType(E->getType()); 1862f22ef01cSRoman Divacky assert(CAT && "String isn't pointer or array!"); 1863f22ef01cSRoman Divacky 1864f22ef01cSRoman Divacky // Resize the string to the right size. 1865f22ef01cSRoman Divacky uint64_t RealLen = CAT->getSize().getZExtValue(); 1866f22ef01cSRoman Divacky 1867f22ef01cSRoman Divacky if (E->isWide()) 18682754fe60SDimitry Andric RealLen *= Context.Target.getWCharWidth() / Context.getCharWidth(); 1869f22ef01cSRoman Divacky 1870e580952dSDimitry Andric std::string Str = E->getString().str(); 1871f22ef01cSRoman Divacky Str.resize(RealLen, '\0'); 1872f22ef01cSRoman Divacky 1873f22ef01cSRoman Divacky return Str; 1874f22ef01cSRoman Divacky } 1875f22ef01cSRoman Divacky 1876f22ef01cSRoman Divacky /// GetAddrOfConstantStringFromLiteral - Return a pointer to a 1877f22ef01cSRoman Divacky /// constant array for the given string literal. 1878f22ef01cSRoman Divacky llvm::Constant * 1879f22ef01cSRoman Divacky CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 1880f22ef01cSRoman Divacky // FIXME: This can be more efficient. 1881f22ef01cSRoman Divacky // FIXME: We shouldn't need to bitcast the constant in the wide string case. 1882f22ef01cSRoman Divacky llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S)); 1883f22ef01cSRoman Divacky if (S->isWide()) { 1884f22ef01cSRoman Divacky llvm::Type *DestTy = 1885f22ef01cSRoman Divacky llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType())); 1886f22ef01cSRoman Divacky C = llvm::ConstantExpr::getBitCast(C, DestTy); 1887f22ef01cSRoman Divacky } 1888f22ef01cSRoman Divacky return C; 1889f22ef01cSRoman Divacky } 1890f22ef01cSRoman Divacky 1891f22ef01cSRoman Divacky /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1892f22ef01cSRoman Divacky /// array for the given ObjCEncodeExpr node. 1893f22ef01cSRoman Divacky llvm::Constant * 1894f22ef01cSRoman Divacky CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1895f22ef01cSRoman Divacky std::string Str; 1896f22ef01cSRoman Divacky getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1897f22ef01cSRoman Divacky 1898f22ef01cSRoman Divacky return GetAddrOfConstantCString(Str); 1899f22ef01cSRoman Divacky } 1900f22ef01cSRoman Divacky 1901f22ef01cSRoman Divacky 1902f22ef01cSRoman Divacky /// GenerateWritableString -- Creates storage for a string literal. 19033b0f4066SDimitry Andric static llvm::Constant *GenerateStringLiteral(llvm::StringRef str, 1904f22ef01cSRoman Divacky bool constant, 1905f22ef01cSRoman Divacky CodeGenModule &CGM, 1906f22ef01cSRoman Divacky const char *GlobalName) { 1907f22ef01cSRoman Divacky // Create Constant for this string literal. Don't add a '\0'. 1908f22ef01cSRoman Divacky llvm::Constant *C = 1909f22ef01cSRoman Divacky llvm::ConstantArray::get(CGM.getLLVMContext(), str, false); 1910f22ef01cSRoman Divacky 1911f22ef01cSRoman Divacky // Create a global variable for this string 19122754fe60SDimitry Andric llvm::GlobalVariable *GV = 19132754fe60SDimitry Andric new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, 1914f22ef01cSRoman Divacky llvm::GlobalValue::PrivateLinkage, 1915f22ef01cSRoman Divacky C, GlobalName); 1916bd5abe19SDimitry Andric GV->setAlignment(1); 19172754fe60SDimitry Andric GV->setUnnamedAddr(true); 19182754fe60SDimitry Andric return GV; 1919f22ef01cSRoman Divacky } 1920f22ef01cSRoman Divacky 1921f22ef01cSRoman Divacky /// GetAddrOfConstantString - Returns a pointer to a character array 1922f22ef01cSRoman Divacky /// containing the literal. This contents are exactly that of the 1923f22ef01cSRoman Divacky /// given string, i.e. it will not be null terminated automatically; 1924f22ef01cSRoman Divacky /// see GetAddrOfConstantCString. Note that whether the result is 1925f22ef01cSRoman Divacky /// actually a pointer to an LLVM constant depends on 1926f22ef01cSRoman Divacky /// Feature.WriteableStrings. 1927f22ef01cSRoman Divacky /// 1928f22ef01cSRoman Divacky /// The result has pointer to array type. 19293b0f4066SDimitry Andric llvm::Constant *CodeGenModule::GetAddrOfConstantString(llvm::StringRef Str, 1930f22ef01cSRoman Divacky const char *GlobalName) { 1931f22ef01cSRoman Divacky bool IsConstant = !Features.WritableStrings; 1932f22ef01cSRoman Divacky 1933f22ef01cSRoman Divacky // Get the default prefix if a name wasn't specified. 1934f22ef01cSRoman Divacky if (!GlobalName) 1935f22ef01cSRoman Divacky GlobalName = ".str"; 1936f22ef01cSRoman Divacky 1937f22ef01cSRoman Divacky // Don't share any string literals if strings aren't constant. 1938f22ef01cSRoman Divacky if (!IsConstant) 19393b0f4066SDimitry Andric return GenerateStringLiteral(Str, false, *this, GlobalName); 1940f22ef01cSRoman Divacky 1941f22ef01cSRoman Divacky llvm::StringMapEntry<llvm::Constant *> &Entry = 19423b0f4066SDimitry Andric ConstantStringMap.GetOrCreateValue(Str); 1943f22ef01cSRoman Divacky 1944f22ef01cSRoman Divacky if (Entry.getValue()) 1945f22ef01cSRoman Divacky return Entry.getValue(); 1946f22ef01cSRoman Divacky 1947f22ef01cSRoman Divacky // Create a global variable for this. 19483b0f4066SDimitry Andric llvm::Constant *C = GenerateStringLiteral(Str, true, *this, GlobalName); 1949f22ef01cSRoman Divacky Entry.setValue(C); 1950f22ef01cSRoman Divacky return C; 1951f22ef01cSRoman Divacky } 1952f22ef01cSRoman Divacky 1953f22ef01cSRoman Divacky /// GetAddrOfConstantCString - Returns a pointer to a character 19543b0f4066SDimitry Andric /// array containing the literal and a terminating '\0' 1955f22ef01cSRoman Divacky /// character. The result has pointer to array type. 19563b0f4066SDimitry Andric llvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &Str, 1957f22ef01cSRoman Divacky const char *GlobalName){ 19583b0f4066SDimitry Andric llvm::StringRef StrWithNull(Str.c_str(), Str.size() + 1); 19593b0f4066SDimitry Andric return GetAddrOfConstantString(StrWithNull, GlobalName); 1960f22ef01cSRoman Divacky } 1961f22ef01cSRoman Divacky 1962f22ef01cSRoman Divacky /// EmitObjCPropertyImplementations - Emit information for synthesized 1963f22ef01cSRoman Divacky /// properties for an implementation. 1964f22ef01cSRoman Divacky void CodeGenModule::EmitObjCPropertyImplementations(const 1965f22ef01cSRoman Divacky ObjCImplementationDecl *D) { 1966f22ef01cSRoman Divacky for (ObjCImplementationDecl::propimpl_iterator 1967f22ef01cSRoman Divacky i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { 1968f22ef01cSRoman Divacky ObjCPropertyImplDecl *PID = *i; 1969f22ef01cSRoman Divacky 1970f22ef01cSRoman Divacky // Dynamic is just for type-checking. 1971f22ef01cSRoman Divacky if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1972f22ef01cSRoman Divacky ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1973f22ef01cSRoman Divacky 1974f22ef01cSRoman Divacky // Determine which methods need to be implemented, some may have 1975f22ef01cSRoman Divacky // been overridden. Note that ::isSynthesized is not the method 1976f22ef01cSRoman Divacky // we want, that just indicates if the decl came from a 1977f22ef01cSRoman Divacky // property. What we want to know is if the method is defined in 1978f22ef01cSRoman Divacky // this implementation. 1979f22ef01cSRoman Divacky if (!D->getInstanceMethod(PD->getGetterName())) 1980f22ef01cSRoman Divacky CodeGenFunction(*this).GenerateObjCGetter( 1981f22ef01cSRoman Divacky const_cast<ObjCImplementationDecl *>(D), PID); 1982f22ef01cSRoman Divacky if (!PD->isReadOnly() && 1983f22ef01cSRoman Divacky !D->getInstanceMethod(PD->getSetterName())) 1984f22ef01cSRoman Divacky CodeGenFunction(*this).GenerateObjCSetter( 1985f22ef01cSRoman Divacky const_cast<ObjCImplementationDecl *>(D), PID); 1986f22ef01cSRoman Divacky } 1987f22ef01cSRoman Divacky } 1988f22ef01cSRoman Divacky } 1989f22ef01cSRoman Divacky 19903b0f4066SDimitry Andric static bool needsDestructMethod(ObjCImplementationDecl *impl) { 19913b0f4066SDimitry Andric ObjCInterfaceDecl *iface 19923b0f4066SDimitry Andric = const_cast<ObjCInterfaceDecl*>(impl->getClassInterface()); 19933b0f4066SDimitry Andric for (ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 19943b0f4066SDimitry Andric ivar; ivar = ivar->getNextIvar()) 19953b0f4066SDimitry Andric if (ivar->getType().isDestructedType()) 19963b0f4066SDimitry Andric return true; 19973b0f4066SDimitry Andric 19983b0f4066SDimitry Andric return false; 19993b0f4066SDimitry Andric } 20003b0f4066SDimitry Andric 2001f22ef01cSRoman Divacky /// EmitObjCIvarInitializations - Emit information for ivar initialization 2002f22ef01cSRoman Divacky /// for an implementation. 2003f22ef01cSRoman Divacky void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { 20043b0f4066SDimitry Andric // We might need a .cxx_destruct even if we don't have any ivar initializers. 20053b0f4066SDimitry Andric if (needsDestructMethod(D)) { 2006f22ef01cSRoman Divacky IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); 2007f22ef01cSRoman Divacky Selector cxxSelector = getContext().Selectors.getSelector(0, &II); 20083b0f4066SDimitry Andric ObjCMethodDecl *DTORMethod = 20093b0f4066SDimitry Andric ObjCMethodDecl::Create(getContext(), D->getLocation(), D->getLocation(), 20103b0f4066SDimitry Andric cxxSelector, getContext().VoidTy, 0, D, true, 20113b0f4066SDimitry Andric false, true, false, ObjCMethodDecl::Required); 2012f22ef01cSRoman Divacky D->addInstanceMethod(DTORMethod); 2013f22ef01cSRoman Divacky CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); 201417a519f9SDimitry Andric D->setHasCXXStructors(true); 20153b0f4066SDimitry Andric } 2016f22ef01cSRoman Divacky 20173b0f4066SDimitry Andric // If the implementation doesn't have any ivar initializers, we don't need 20183b0f4066SDimitry Andric // a .cxx_construct. 20193b0f4066SDimitry Andric if (D->getNumIvarInitializers() == 0) 20203b0f4066SDimitry Andric return; 20213b0f4066SDimitry Andric 20223b0f4066SDimitry Andric IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); 20233b0f4066SDimitry Andric Selector cxxSelector = getContext().Selectors.getSelector(0, &II); 2024f22ef01cSRoman Divacky // The constructor returns 'self'. 2025f22ef01cSRoman Divacky ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create(getContext(), 2026f22ef01cSRoman Divacky D->getLocation(), 2027f22ef01cSRoman Divacky D->getLocation(), cxxSelector, 2028f22ef01cSRoman Divacky getContext().getObjCIdType(), 0, 20293b0f4066SDimitry Andric D, true, false, true, false, 2030f22ef01cSRoman Divacky ObjCMethodDecl::Required); 2031f22ef01cSRoman Divacky D->addInstanceMethod(CTORMethod); 2032f22ef01cSRoman Divacky CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); 203317a519f9SDimitry Andric D->setHasCXXStructors(true); 2034f22ef01cSRoman Divacky } 2035f22ef01cSRoman Divacky 2036f22ef01cSRoman Divacky /// EmitNamespace - Emit all declarations in a namespace. 2037f22ef01cSRoman Divacky void CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { 2038f22ef01cSRoman Divacky for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); 2039f22ef01cSRoman Divacky I != E; ++I) 2040f22ef01cSRoman Divacky EmitTopLevelDecl(*I); 2041f22ef01cSRoman Divacky } 2042f22ef01cSRoman Divacky 2043f22ef01cSRoman Divacky // EmitLinkageSpec - Emit all declarations in a linkage spec. 2044f22ef01cSRoman Divacky void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 2045f22ef01cSRoman Divacky if (LSD->getLanguage() != LinkageSpecDecl::lang_c && 2046f22ef01cSRoman Divacky LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { 2047f22ef01cSRoman Divacky ErrorUnsupported(LSD, "linkage spec"); 2048f22ef01cSRoman Divacky return; 2049f22ef01cSRoman Divacky } 2050f22ef01cSRoman Divacky 2051f22ef01cSRoman Divacky for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); 2052f22ef01cSRoman Divacky I != E; ++I) 2053f22ef01cSRoman Divacky EmitTopLevelDecl(*I); 2054f22ef01cSRoman Divacky } 2055f22ef01cSRoman Divacky 2056f22ef01cSRoman Divacky /// EmitTopLevelDecl - Emit code for a single top level declaration. 2057f22ef01cSRoman Divacky void CodeGenModule::EmitTopLevelDecl(Decl *D) { 2058f22ef01cSRoman Divacky // If an error has occurred, stop code generation, but continue 2059f22ef01cSRoman Divacky // parsing and semantic analysis (to ensure all warnings and errors 2060f22ef01cSRoman Divacky // are emitted). 2061f22ef01cSRoman Divacky if (Diags.hasErrorOccurred()) 2062f22ef01cSRoman Divacky return; 2063f22ef01cSRoman Divacky 2064f22ef01cSRoman Divacky // Ignore dependent declarations. 2065f22ef01cSRoman Divacky if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) 2066f22ef01cSRoman Divacky return; 2067f22ef01cSRoman Divacky 2068f22ef01cSRoman Divacky switch (D->getKind()) { 2069f22ef01cSRoman Divacky case Decl::CXXConversion: 2070f22ef01cSRoman Divacky case Decl::CXXMethod: 2071f22ef01cSRoman Divacky case Decl::Function: 2072f22ef01cSRoman Divacky // Skip function templates 20733b0f4066SDimitry Andric if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || 20743b0f4066SDimitry Andric cast<FunctionDecl>(D)->isLateTemplateParsed()) 2075f22ef01cSRoman Divacky return; 2076f22ef01cSRoman Divacky 2077f22ef01cSRoman Divacky EmitGlobal(cast<FunctionDecl>(D)); 2078f22ef01cSRoman Divacky break; 2079f22ef01cSRoman Divacky 2080f22ef01cSRoman Divacky case Decl::Var: 2081f22ef01cSRoman Divacky EmitGlobal(cast<VarDecl>(D)); 2082f22ef01cSRoman Divacky break; 2083f22ef01cSRoman Divacky 20843b0f4066SDimitry Andric // Indirect fields from global anonymous structs and unions can be 20853b0f4066SDimitry Andric // ignored; only the actual variable requires IR gen support. 20863b0f4066SDimitry Andric case Decl::IndirectField: 20873b0f4066SDimitry Andric break; 20883b0f4066SDimitry Andric 2089f22ef01cSRoman Divacky // C++ Decls 2090f22ef01cSRoman Divacky case Decl::Namespace: 2091f22ef01cSRoman Divacky EmitNamespace(cast<NamespaceDecl>(D)); 2092f22ef01cSRoman Divacky break; 2093f22ef01cSRoman Divacky // No code generation needed. 2094f22ef01cSRoman Divacky case Decl::UsingShadow: 2095f22ef01cSRoman Divacky case Decl::Using: 2096f22ef01cSRoman Divacky case Decl::UsingDirective: 2097f22ef01cSRoman Divacky case Decl::ClassTemplate: 2098f22ef01cSRoman Divacky case Decl::FunctionTemplate: 2099bd5abe19SDimitry Andric case Decl::TypeAliasTemplate: 2100f22ef01cSRoman Divacky case Decl::NamespaceAlias: 2101bd5abe19SDimitry Andric case Decl::Block: 2102f22ef01cSRoman Divacky break; 2103f22ef01cSRoman Divacky case Decl::CXXConstructor: 2104f22ef01cSRoman Divacky // Skip function templates 21053b0f4066SDimitry Andric if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate() || 21063b0f4066SDimitry Andric cast<FunctionDecl>(D)->isLateTemplateParsed()) 2107f22ef01cSRoman Divacky return; 2108f22ef01cSRoman Divacky 2109f22ef01cSRoman Divacky EmitCXXConstructors(cast<CXXConstructorDecl>(D)); 2110f22ef01cSRoman Divacky break; 2111f22ef01cSRoman Divacky case Decl::CXXDestructor: 21123b0f4066SDimitry Andric if (cast<FunctionDecl>(D)->isLateTemplateParsed()) 21133b0f4066SDimitry Andric return; 2114f22ef01cSRoman Divacky EmitCXXDestructors(cast<CXXDestructorDecl>(D)); 2115f22ef01cSRoman Divacky break; 2116f22ef01cSRoman Divacky 2117f22ef01cSRoman Divacky case Decl::StaticAssert: 2118f22ef01cSRoman Divacky // Nothing to do. 2119f22ef01cSRoman Divacky break; 2120f22ef01cSRoman Divacky 2121f22ef01cSRoman Divacky // Objective-C Decls 2122f22ef01cSRoman Divacky 2123f22ef01cSRoman Divacky // Forward declarations, no (immediate) code generation. 2124f22ef01cSRoman Divacky case Decl::ObjCClass: 2125f22ef01cSRoman Divacky case Decl::ObjCForwardProtocol: 2126f22ef01cSRoman Divacky case Decl::ObjCInterface: 2127f22ef01cSRoman Divacky break; 2128f22ef01cSRoman Divacky 2129e580952dSDimitry Andric case Decl::ObjCCategory: { 2130e580952dSDimitry Andric ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); 2131e580952dSDimitry Andric if (CD->IsClassExtension() && CD->hasSynthBitfield()) 2132e580952dSDimitry Andric Context.ResetObjCLayout(CD->getClassInterface()); 2133e580952dSDimitry Andric break; 2134e580952dSDimitry Andric } 2135e580952dSDimitry Andric 2136f22ef01cSRoman Divacky case Decl::ObjCProtocol: 2137f22ef01cSRoman Divacky Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 2138f22ef01cSRoman Divacky break; 2139f22ef01cSRoman Divacky 2140f22ef01cSRoman Divacky case Decl::ObjCCategoryImpl: 2141f22ef01cSRoman Divacky // Categories have properties but don't support synthesize so we 2142f22ef01cSRoman Divacky // can ignore them here. 2143f22ef01cSRoman Divacky Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 2144f22ef01cSRoman Divacky break; 2145f22ef01cSRoman Divacky 2146f22ef01cSRoman Divacky case Decl::ObjCImplementation: { 2147f22ef01cSRoman Divacky ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 2148e580952dSDimitry Andric if (Features.ObjCNonFragileABI2 && OMD->hasSynthBitfield()) 2149e580952dSDimitry Andric Context.ResetObjCLayout(OMD->getClassInterface()); 2150f22ef01cSRoman Divacky EmitObjCPropertyImplementations(OMD); 2151f22ef01cSRoman Divacky EmitObjCIvarInitializations(OMD); 2152f22ef01cSRoman Divacky Runtime->GenerateClass(OMD); 2153f22ef01cSRoman Divacky break; 2154f22ef01cSRoman Divacky } 2155f22ef01cSRoman Divacky case Decl::ObjCMethod: { 2156f22ef01cSRoman Divacky ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 2157f22ef01cSRoman Divacky // If this is not a prototype, emit the body. 2158f22ef01cSRoman Divacky if (OMD->getBody()) 2159f22ef01cSRoman Divacky CodeGenFunction(*this).GenerateObjCMethod(OMD); 2160f22ef01cSRoman Divacky break; 2161f22ef01cSRoman Divacky } 2162f22ef01cSRoman Divacky case Decl::ObjCCompatibleAlias: 2163f22ef01cSRoman Divacky // compatibility-alias is a directive and has no code gen. 2164f22ef01cSRoman Divacky break; 2165f22ef01cSRoman Divacky 2166f22ef01cSRoman Divacky case Decl::LinkageSpec: 2167f22ef01cSRoman Divacky EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 2168f22ef01cSRoman Divacky break; 2169f22ef01cSRoman Divacky 2170f22ef01cSRoman Divacky case Decl::FileScopeAsm: { 2171f22ef01cSRoman Divacky FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 2172f22ef01cSRoman Divacky llvm::StringRef AsmString = AD->getAsmString()->getString(); 2173f22ef01cSRoman Divacky 2174f22ef01cSRoman Divacky const std::string &S = getModule().getModuleInlineAsm(); 2175f22ef01cSRoman Divacky if (S.empty()) 2176f22ef01cSRoman Divacky getModule().setModuleInlineAsm(AsmString); 2177f22ef01cSRoman Divacky else 2178f22ef01cSRoman Divacky getModule().setModuleInlineAsm(S + '\n' + AsmString.str()); 2179f22ef01cSRoman Divacky break; 2180f22ef01cSRoman Divacky } 2181f22ef01cSRoman Divacky 2182f22ef01cSRoman Divacky default: 2183f22ef01cSRoman Divacky // Make sure we handled everything we should, every other kind is a 2184f22ef01cSRoman Divacky // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind 2185f22ef01cSRoman Divacky // function. Need to recode Decl::Kind to do that easily. 2186f22ef01cSRoman Divacky assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 2187f22ef01cSRoman Divacky } 2188f22ef01cSRoman Divacky } 2189ffd1746dSEd Schouten 2190ffd1746dSEd Schouten /// Turns the given pointer into a constant. 2191ffd1746dSEd Schouten static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, 2192ffd1746dSEd Schouten const void *Ptr) { 2193ffd1746dSEd Schouten uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); 2194ffd1746dSEd Schouten const llvm::Type *i64 = llvm::Type::getInt64Ty(Context); 2195ffd1746dSEd Schouten return llvm::ConstantInt::get(i64, PtrInt); 2196ffd1746dSEd Schouten } 2197ffd1746dSEd Schouten 2198ffd1746dSEd Schouten static void EmitGlobalDeclMetadata(CodeGenModule &CGM, 2199ffd1746dSEd Schouten llvm::NamedMDNode *&GlobalMetadata, 2200ffd1746dSEd Schouten GlobalDecl D, 2201ffd1746dSEd Schouten llvm::GlobalValue *Addr) { 2202ffd1746dSEd Schouten if (!GlobalMetadata) 2203ffd1746dSEd Schouten GlobalMetadata = 2204ffd1746dSEd Schouten CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); 2205ffd1746dSEd Schouten 2206ffd1746dSEd Schouten // TODO: should we report variant information for ctors/dtors? 2207ffd1746dSEd Schouten llvm::Value *Ops[] = { 2208ffd1746dSEd Schouten Addr, 2209ffd1746dSEd Schouten GetPointerConstant(CGM.getLLVMContext(), D.getDecl()) 2210ffd1746dSEd Schouten }; 22113b0f4066SDimitry Andric GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); 2212ffd1746dSEd Schouten } 2213ffd1746dSEd Schouten 2214ffd1746dSEd Schouten /// Emits metadata nodes associating all the global values in the 2215ffd1746dSEd Schouten /// current module with the Decls they came from. This is useful for 2216ffd1746dSEd Schouten /// projects using IR gen as a subroutine. 2217ffd1746dSEd Schouten /// 2218ffd1746dSEd Schouten /// Since there's currently no way to associate an MDNode directly 2219ffd1746dSEd Schouten /// with an llvm::GlobalValue, we create a global named metadata 2220ffd1746dSEd Schouten /// with the name 'clang.global.decl.ptrs'. 2221ffd1746dSEd Schouten void CodeGenModule::EmitDeclMetadata() { 2222ffd1746dSEd Schouten llvm::NamedMDNode *GlobalMetadata = 0; 2223ffd1746dSEd Schouten 2224ffd1746dSEd Schouten // StaticLocalDeclMap 2225ffd1746dSEd Schouten for (llvm::DenseMap<GlobalDecl,llvm::StringRef>::iterator 2226ffd1746dSEd Schouten I = MangledDeclNames.begin(), E = MangledDeclNames.end(); 2227ffd1746dSEd Schouten I != E; ++I) { 2228ffd1746dSEd Schouten llvm::GlobalValue *Addr = getModule().getNamedValue(I->second); 2229ffd1746dSEd Schouten EmitGlobalDeclMetadata(*this, GlobalMetadata, I->first, Addr); 2230ffd1746dSEd Schouten } 2231ffd1746dSEd Schouten } 2232ffd1746dSEd Schouten 2233ffd1746dSEd Schouten /// Emits metadata nodes for all the local variables in the current 2234ffd1746dSEd Schouten /// function. 2235ffd1746dSEd Schouten void CodeGenFunction::EmitDeclMetadata() { 2236ffd1746dSEd Schouten if (LocalDeclMap.empty()) return; 2237ffd1746dSEd Schouten 2238ffd1746dSEd Schouten llvm::LLVMContext &Context = getLLVMContext(); 2239ffd1746dSEd Schouten 2240ffd1746dSEd Schouten // Find the unique metadata ID for this name. 2241ffd1746dSEd Schouten unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); 2242ffd1746dSEd Schouten 2243ffd1746dSEd Schouten llvm::NamedMDNode *GlobalMetadata = 0; 2244ffd1746dSEd Schouten 2245ffd1746dSEd Schouten for (llvm::DenseMap<const Decl*, llvm::Value*>::iterator 2246ffd1746dSEd Schouten I = LocalDeclMap.begin(), E = LocalDeclMap.end(); I != E; ++I) { 2247ffd1746dSEd Schouten const Decl *D = I->first; 2248ffd1746dSEd Schouten llvm::Value *Addr = I->second; 2249ffd1746dSEd Schouten 2250ffd1746dSEd Schouten if (llvm::AllocaInst *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { 2251ffd1746dSEd Schouten llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); 22523b0f4066SDimitry Andric Alloca->setMetadata(DeclPtrKind, llvm::MDNode::get(Context, DAddr)); 2253ffd1746dSEd Schouten } else if (llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(Addr)) { 2254ffd1746dSEd Schouten GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); 2255ffd1746dSEd Schouten EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); 2256ffd1746dSEd Schouten } 2257ffd1746dSEd Schouten } 2258ffd1746dSEd Schouten } 2259e580952dSDimitry Andric 2260bd5abe19SDimitry Andric void CodeGenModule::EmitCoverageFile() { 2261bd5abe19SDimitry Andric if (!getCodeGenOpts().CoverageFile.empty()) { 2262bd5abe19SDimitry Andric if (llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu")) { 2263bd5abe19SDimitry Andric llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); 2264bd5abe19SDimitry Andric llvm::LLVMContext &Ctx = TheModule.getContext(); 2265bd5abe19SDimitry Andric llvm::MDString *CoverageFile = 2266bd5abe19SDimitry Andric llvm::MDString::get(Ctx, getCodeGenOpts().CoverageFile); 2267bd5abe19SDimitry Andric for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { 2268bd5abe19SDimitry Andric llvm::MDNode *CU = CUNode->getOperand(i); 2269bd5abe19SDimitry Andric llvm::Value *node[] = { CoverageFile, CU }; 2270bd5abe19SDimitry Andric llvm::MDNode *N = llvm::MDNode::get(Ctx, node); 2271bd5abe19SDimitry Andric GCov->addOperand(N); 2272bd5abe19SDimitry Andric } 2273bd5abe19SDimitry Andric } 2274bd5abe19SDimitry Andric } 2275bd5abe19SDimitry Andric } 2276bd5abe19SDimitry Andric 2277e580952dSDimitry Andric ///@name Custom Runtime Function Interfaces 2278e580952dSDimitry Andric ///@{ 2279e580952dSDimitry Andric // 2280e580952dSDimitry Andric // FIXME: These can be eliminated once we can have clients just get the required 2281e580952dSDimitry Andric // AST nodes from the builtin tables. 2282e580952dSDimitry Andric 2283e580952dSDimitry Andric llvm::Constant *CodeGenModule::getBlockObjectDispose() { 2284e580952dSDimitry Andric if (BlockObjectDispose) 2285e580952dSDimitry Andric return BlockObjectDispose; 2286e580952dSDimitry Andric 2287e580952dSDimitry Andric // If we saw an explicit decl, use that. 2288e580952dSDimitry Andric if (BlockObjectDisposeDecl) { 2289e580952dSDimitry Andric return BlockObjectDispose = GetAddrOfFunction( 2290e580952dSDimitry Andric BlockObjectDisposeDecl, 2291e580952dSDimitry Andric getTypes().GetFunctionType(BlockObjectDisposeDecl)); 2292e580952dSDimitry Andric } 2293e580952dSDimitry Andric 2294e580952dSDimitry Andric // Otherwise construct the function by hand. 229517a519f9SDimitry Andric llvm::Type *args[] = { Int8PtrTy, Int32Ty }; 2296bd5abe19SDimitry Andric const llvm::FunctionType *fty 2297bd5abe19SDimitry Andric = llvm::FunctionType::get(VoidTy, args, false); 2298e580952dSDimitry Andric return BlockObjectDispose = 2299bd5abe19SDimitry Andric CreateRuntimeFunction(fty, "_Block_object_dispose"); 2300e580952dSDimitry Andric } 2301e580952dSDimitry Andric 2302e580952dSDimitry Andric llvm::Constant *CodeGenModule::getBlockObjectAssign() { 2303e580952dSDimitry Andric if (BlockObjectAssign) 2304e580952dSDimitry Andric return BlockObjectAssign; 2305e580952dSDimitry Andric 2306e580952dSDimitry Andric // If we saw an explicit decl, use that. 2307e580952dSDimitry Andric if (BlockObjectAssignDecl) { 2308e580952dSDimitry Andric return BlockObjectAssign = GetAddrOfFunction( 2309e580952dSDimitry Andric BlockObjectAssignDecl, 2310e580952dSDimitry Andric getTypes().GetFunctionType(BlockObjectAssignDecl)); 2311e580952dSDimitry Andric } 2312e580952dSDimitry Andric 2313e580952dSDimitry Andric // Otherwise construct the function by hand. 231417a519f9SDimitry Andric llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, Int32Ty }; 2315bd5abe19SDimitry Andric const llvm::FunctionType *fty 2316bd5abe19SDimitry Andric = llvm::FunctionType::get(VoidTy, args, false); 2317e580952dSDimitry Andric return BlockObjectAssign = 2318bd5abe19SDimitry Andric CreateRuntimeFunction(fty, "_Block_object_assign"); 2319e580952dSDimitry Andric } 2320e580952dSDimitry Andric 2321e580952dSDimitry Andric llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() { 2322e580952dSDimitry Andric if (NSConcreteGlobalBlock) 2323e580952dSDimitry Andric return NSConcreteGlobalBlock; 2324e580952dSDimitry Andric 2325e580952dSDimitry Andric // If we saw an explicit decl, use that. 2326e580952dSDimitry Andric if (NSConcreteGlobalBlockDecl) { 2327e580952dSDimitry Andric return NSConcreteGlobalBlock = GetAddrOfGlobalVar( 2328e580952dSDimitry Andric NSConcreteGlobalBlockDecl, 2329e580952dSDimitry Andric getTypes().ConvertType(NSConcreteGlobalBlockDecl->getType())); 2330e580952dSDimitry Andric } 2331e580952dSDimitry Andric 2332e580952dSDimitry Andric // Otherwise construct the variable by hand. 23332754fe60SDimitry Andric return NSConcreteGlobalBlock = 23342754fe60SDimitry Andric CreateRuntimeVariable(Int8PtrTy, "_NSConcreteGlobalBlock"); 2335e580952dSDimitry Andric } 2336e580952dSDimitry Andric 2337e580952dSDimitry Andric llvm::Constant *CodeGenModule::getNSConcreteStackBlock() { 2338e580952dSDimitry Andric if (NSConcreteStackBlock) 2339e580952dSDimitry Andric return NSConcreteStackBlock; 2340e580952dSDimitry Andric 2341e580952dSDimitry Andric // If we saw an explicit decl, use that. 2342e580952dSDimitry Andric if (NSConcreteStackBlockDecl) { 2343e580952dSDimitry Andric return NSConcreteStackBlock = GetAddrOfGlobalVar( 2344e580952dSDimitry Andric NSConcreteStackBlockDecl, 2345e580952dSDimitry Andric getTypes().ConvertType(NSConcreteStackBlockDecl->getType())); 2346e580952dSDimitry Andric } 2347e580952dSDimitry Andric 2348e580952dSDimitry Andric // Otherwise construct the variable by hand. 23492754fe60SDimitry Andric return NSConcreteStackBlock = 23502754fe60SDimitry Andric CreateRuntimeVariable(Int8PtrTy, "_NSConcreteStackBlock"); 2351e580952dSDimitry Andric } 2352e580952dSDimitry Andric 2353e580952dSDimitry Andric ///@} 2354