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