CodeGenModule.cpp revision 44b0bc008ee11cdee69ad12210ca7550e4fa426a
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This coordinates the per-module state used while generating code. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h" 15bd3606426d389370616126af969904ec493cb105Chris Lattner#include "CGDebugInfo.h" 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h" 170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h" 18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h" 195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "Mangle.h" 20bd3606426d389370616126af969904ec493cb105Chris Lattner#include "clang/Frontend/CompileOptions.h" 215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h" 22c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2321ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h" 242c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h" 258bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h" 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 27e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff#include "clang/Basic/ConvertUTF.h" 28ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h" 29bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h" 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h" 3120ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h" 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 36bd3606426d389370616126af969904ec493cb105Chris LattnerCodeGenModule::CodeGenModule(ASTContext &C, const CompileOptions &compileOpts, 37fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner llvm::Module &M, const llvm::TargetData &TD, 38bd3606426d389370616126af969904ec493cb105Chris Lattner Diagnostic &diags) 39bd3606426d389370616126af969904ec493cb105Chris Lattner : BlockModule(C, M, TD, Types, *this), Context(C), 40bd3606426d389370616126af969904ec493cb105Chris Lattner Features(C.getLangOptions()), CompileOpts(compileOpts), TheModule(M), 412a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump TheTargetData(TD), Diags(diags), Types(C, M, TD), Runtime(0), 422a998148a6823c44d67da347c95eb2ea21f6b986Mike Stump MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0) { 43208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 443c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner if (!Features.ObjC1) 453c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = 0; 463c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (!Features.NeXTRuntime) 473c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateGNUObjCRuntime(*this); 483c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (Features.ObjCNonFragileABI) 493c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacNonFragileABIObjCRuntime(*this); 503c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else 513c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacObjCRuntime(*this); 52e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 53e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 54bd3606426d389370616126af969904ec493cb105Chris Lattner DebugInfo = CompileOpts.DebugInfo ? new CGDebugInfo(this) : 0; 552b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 562b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 572b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 58815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 59815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 60815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 61815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 62815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 6382227ff4eb665bbf41720ebdc0dc9215a86ba838Chris Lattner EmitDeferred(); 64208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 65208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 66208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 676bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 686bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 69532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 700269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitLLVMUsed(); 71f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 72f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 73488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 742c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 7590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 7690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 7790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 7890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 79488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 8056b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 812c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 820a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 830a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 842c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 8558c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 86488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 87c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 8890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 8990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 9090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 9190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 92488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 9356b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 94c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 950a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 96c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 97c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 9804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarLangOptions::VisibilityMode 9904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarCodeGenModule::getDeclVisibilityMode(const Decl *D) const { 10004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 10104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (VD->getStorageClass() == VarDecl::PrivateExtern) 10204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 10304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 10404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) { 10504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (attr->getVisibility()) { 10604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar default: assert(0 && "Unknown visibility!"); 10704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::DefaultVisibility: 10804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Default; 10904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::HiddenVisibility: 11004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 11104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::ProtectedVisibility: 11204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Protected; 11304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar } 1147e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 1156ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar 11604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return getLangOptions().getVisibilityMode(); 1174f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1184f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 11904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbarvoid CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, 12004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar const Decl *D) const { 12104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar // Internal definitions always have default visibility. 122df102fcb978588d5edbc661fb5da0b6922f9ab1cChris Lattner if (GV->hasLocalLinkage()) { 1237e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1246ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar return; 1257e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 1266ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar 12704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (getDeclVisibilityMode(D)) { 1287cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian default: assert(0 && "Unknown visibility!"); 12904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Default: 13004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 13104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Hidden: 13204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 13304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Protected: 13404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1357cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian } 1367cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian} 1377cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian 1385f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration. 1395f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1405f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an 141c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// const char* containing the mangled name. Otherwise, returns 142c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// the unmangled name. 1435f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1446ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) { 145c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner // In C, functions with no attributes never need to be mangled. Fastpath them. 146c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner if (!getLangOptions().CPlusPlus && !ND->hasAttrs()) { 147c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1483c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return ND->getNameAsCString(); 149c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner } 150c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner 1516ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::SmallString<256> Name; 1526ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::raw_svector_ostream Out(Name); 153fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar if (!mangleName(ND, Context, Out)) { 154fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1553c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return ND->getNameAsCString(); 156fe345572459b69a6b680322fef504b4f8bd98dd7Daniel Dunbar } 1575f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1586ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor Name += '\0'; 1593c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return MangledNames.GetOrCreateValue(Name.begin(), Name.end()).getKeyData(); 1605f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1615f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1626d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1636d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1646bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 16549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1666bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1676d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1686d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1696bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 1706bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 1716bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 17249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1736bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 1746bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 1756bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1766bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 1776bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 1786bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 1796bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType::get(llvm::Type::VoidTy, 1806bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 1816bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 1826bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 1836bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1846bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 185572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::StructType* CtorStructTy = 1866bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::StructType::get(llvm::Type::Int32Ty, 1876bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::PointerType::getUnqual(CtorFTy), NULL); 1886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1896bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 1906bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 1916bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 1926bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); 1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 1956bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1986bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar new llvm::GlobalVariable(AT, false, 201572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 2026bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ConstantArray::get(AT, Ctors), 2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalName, 204572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner &TheModule); 2056d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 2066d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2076d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 208532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 209532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 210532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 211532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 212532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 213532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 214532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 215532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 216532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 217532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue *gv = 218532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman new llvm::GlobalVariable(Array->getType(), false, 219532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue::AppendingLinkage, Array, 220532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman "llvm.global.annotations", &TheModule); 221532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 222532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 223532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 2245c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbarvoid CodeGenModule::SetGlobalValueAttributes(const Decl *D, 22544b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner GVALinkage Linkage, 2265c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar llvm::GlobalValue *GV, 2275c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool ForDefinition) { 22849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Set up linkage and many other things. Note, this is a simple 229d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // approximation of what we really want. 230219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!ForDefinition) { 231219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Only a few attributes are set on declarations. 2329f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner 2339f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner // The dllimport attribute is overridden by a subsequent declaration as 2349f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner // dllexport. 2359f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner if (D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) { 2369f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner // dllimport attribute can be applied only to function decls, not to 2379f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner // definitions. 2389f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2399f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner if (!FD->getBody()) 2402f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2419f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner } else 2429f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 2439f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner } else if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>()) { 2445e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar // "extern_weak" is overloaded in LLVM; we probably should have 2455e27314eb5a9cba7997cf9f6fc82428d99667077Daniel Dunbar // separate linkage types for this. 246e3fedbeee8a014cd4f4e7cad8e7f6059eae12410Duncan Sands GV->setLinkage(llvm::Function::ExternalWeakLinkage); 2479f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner } 24844b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner } else if (Linkage == GVA_Internal) { 2499f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 2509f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner } else if (D->hasAttr<DLLExportAttr>()) { 2519f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2529f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner // The dllexport attribute is ignored for undefined symbols. 2539f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner if (FD->getBody()) 2549f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 255219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 2569f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 257219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 25844b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner } else if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>()) { 25944b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner GV->setLinkage(llvm::Function::WeakAnyLinkage); 26044b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner } else if (Linkage == GVA_Inline || Linkage == GVA_ExternInline) { 2619f9427999cf69b3b89cd0ed3be16ed27a1c282c7Chris Lattner GV->setLinkage(llvm::Function::WeakAnyLinkage); 2620dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 263d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 26403abc9ec05e8edce5e8d0c7f08a05027b9e04170Daniel Dunbar if (ForDefinition) { 26504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar setGlobalVisibility(GV, D); 266a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 26704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar // Only add to llvm.used when we see a definition, otherwise we 26804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar // might add multiple times or risk the value being replaced by a 26904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar // subsequent RAUW. 270b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (D->hasAttr<UsedAttr>()) 2715c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 2725c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar } 27304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 27404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 27504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setSection(SA->getName()); 276d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 277d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 278761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D, 27945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar const CGFunctionInfo &Info, 280b768807c49a1c7085def099b848631856af766faDaniel Dunbar llvm::Function *F) { 281761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 28288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar ConstructAttributeList(Info, D, AttributeList); 283c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman 284761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 285761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeList.size())); 286ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 287ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman // Set the appropriate calling convention for the Function. 288b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (D->hasAttr<FastCallAttr>()) 289f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_FastCall); 290f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov 291b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (D->hasAttr<StdCallAttr>()) 292f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_StdCall); 293f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 294f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 29544b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner 29644b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattnerstatic CodeGenModule::GVALinkage 29744b0bc008ee11cdee69ad12210ca7550e4fa426aChris LattnerGetLinkageForFunctionOrMethodDecl(const Decl *D) { 29844b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 29944b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner if (FD->getStorageClass() == FunctionDecl::Static) 30044b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner return CodeGenModule::GVA_Internal; 30144b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner if (FD->isInline()) { 30244b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner if (FD->getStorageClass() == FunctionDecl::Extern) 30344b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner return CodeGenModule::GVA_ExternInline; 30444b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner return CodeGenModule::GVA_Inline; 30544b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner } 30644b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner } else { 30744b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner assert(isa<ObjCMethodDecl>(D)); 30844b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner return CodeGenModule::GVA_Internal; 30944b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner } 31044b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner return CodeGenModule::GVA_Normal; 31144b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner} 31244b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner 313f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes 314f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition. 315219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, 316219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F) { 31744b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner SetGlobalValueAttributes(D, GetLinkageForFunctionOrMethodDecl(D), F, true); 318219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 31974ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar if (!Features.Exceptions && !Features.ObjCNonFragileABI) 320f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar F->addFnAttr(llvm::Attribute::NoUnwind); 321af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 322b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (D->hasAttr<AlwaysInlineAttr>()) 323af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 32481ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson 325b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (D->hasAttr<NoinlineAttr>()) 32681ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson F->addFnAttr(llvm::Attribute::NoInline); 327f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 328f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 329f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, 330f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 331541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); 332f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 333219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(MD, F); 334f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 335f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 336f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 337f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 338541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 33945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 34044b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner SetGlobalValueAttributes(FD, GetLinkageForFunctionOrMethodDecl(FD), F, false); 341219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 342219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 3430269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 3440269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar assert(!GV->isDeclaration() && 3450269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 34635f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner LLVMUsed.push_back(GV); 3470269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 3480269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 3490269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 3500269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 3510269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar if (LLVMUsed.empty()) 3520269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 3530269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 35435f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 35535f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, LLVMUsed.size()); 35635f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner 35735f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner // Convert LLVMUsed to what ConstantArray needs. 35835f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner std::vector<llvm::Constant*> UsedArray; 35935f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner UsedArray.resize(LLVMUsed.size()); 36035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { 36135f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner UsedArray[i] = 36235f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), i8PTy); 36335f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner } 36435f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner 3650269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalVariable *GV = 3660269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar new llvm::GlobalVariable(ATy, false, 3670269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 36835f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner llvm::ConstantArray::get(ATy, UsedArray), 3690269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "llvm.used", &getModule()); 3700269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 3710269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 3720269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 3730269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 3740269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 37567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Emit code for any potentially referenced deferred decls. Since a 37667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // previously unused static decl may become used during the generation of code 37767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // for a static function, iterate until no changes are made. 37867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner while (!DeferredDeclsToEmit.empty()) { 37967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner const ValueDecl *D = DeferredDeclsToEmit.back(); 38067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.pop_back(); 38167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 38267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // The mangled name for the decl must have been emitted in GlobalDeclMap. 38367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Look it up to see if it was defined with a stronger definition (e.g. an 38467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // extern inline function with a strong function redefinition). If so, 38567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // just ignore the deferred decl. 38667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)]; 38767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner assert(CGRef && "Deferred decl wasn't referenced?"); 388b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 38967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (!CGRef->isDeclaration()) 39067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner continue; 39167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 39267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, emit the definition and move on to the next one. 39367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner EmitGlobalDefinition(D); 39467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 3965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 3988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 3998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 4003c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated. The second field is the constant string 4018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation. The third field is a constant 4028bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 4038bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 4048bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4058bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 4068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 4078bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4088bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 4098bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 4108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 4118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 4128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4138bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 4148bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 4158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4168bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); 4178bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), 4188bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 4198bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4208bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 4218bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 4228e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar const char *StringPrefix = getContext().Target.getStringSymbolPrefix(true); 4238bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *annoGV = 4248bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(anno->getType(), false, 4258bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, anno, 4268e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV->getName() + StringPrefix, M); 4278bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 4288bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 4298bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(unit->getType(), false, 4308e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar llvm::GlobalValue::InternalLinkage, unit, 4318e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar StringPrefix, M); 4328bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4338bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Create the ConstantStruct that is the global annotion. 4348bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 4358bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(GV, SBP), 4368bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(annoGV, SBP), 4378bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(unitGV, SBP), 4388bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) 4398bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 4408bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman return llvm::ConstantStruct::get(Fields, 4, false); 4418bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 4428bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 44373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 4445c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 4455c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 446b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>()) 44773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 448bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 449bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 45073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 451b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) 45273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 45373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 45499b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // FIXME: What about inline, and/or extern inline? 45573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getStorageClass() != FunctionDecl::Static) 45673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 45773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } else { 45873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 45999b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner assert(VD->isFileVarDecl() && "Invalid decl"); 46073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 46173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (VD->getStorageClass() != VarDecl::Static) 46273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 46373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 46473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 46573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return true; 46673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 46773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 46873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) { 469bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If this is an alias definition (which otherwise looks like a declaration) 470bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // emit it now. 471b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (Global->hasAttr<AliasAttr>()) 472bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return EmitAliasDefinition(Global); 473219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 47467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Ignore declarations, they will be emitted on their first use. 4755e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 47673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 47773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 47873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 4790269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 4800269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 481bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 482bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 48373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 4847542bca16b63c84e401b44b586ac3378aed446c5Daniel Dunbar if (!VD->getInit() && VD->hasExternalStorage()) 48573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 4864c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 4874c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 48867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Defer code generation when possible if this is a static definition, inline 48967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // function etc. These we only want to emit if they are used. 49073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (MayDeferGeneration(Global)) { 49167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // If the value has already been used, add it directly to the 49267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit list. 49367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner const char *MangledName = getMangledName(Global); 49467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (GlobalDeclMap.count(MangledName)) 49567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(Global); 49667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner else { 49767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, remember that we saw a deferred decl with this name. The 49867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // first use of the mangled name will cause it to move into 49967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit. 50067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls[MangledName] = Global; 50167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 502bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 503bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 504bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 505bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 506bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(Global); 5074c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 5084c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 509bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { 510bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 511bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalFunctionDefinition(FD); 512bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 513bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 514bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 515bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 516bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 517bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 518bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 51974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMFunction - If the specified mangled name is not in the 52074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// module, create and return an llvm Function with the specified type. If there 52174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 52274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 52374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 52474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 52574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the function when it is first created. 52674391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName, 52774391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty, 52874391b48b4791cded373683a3baf67314f358d50Chris Lattner const FunctionDecl *D) { 5290558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Lookup the entry, lazily creating it if necessary. 5300558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 5310558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry) { 5320558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry->getType()->getElementType() == Ty) 5330558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return Entry; 5340558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 5350558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Make sure the result is of the correct type. 5360558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 5370558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return llvm::ConstantExpr::getBitCast(Entry, PTy); 5380558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 5390558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 54067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 54167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 54267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 54367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI = 54474391b48b4791cded373683a3baf67314f358d50Chris Lattner DeferredDecls.find(MangledName); 54567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 54667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 54767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 54867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 54967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 55067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 55167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 5520558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // This function doesn't have a complete type (for example, the return 5530558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // type is an incomplete struct). Use a fake type instead, and make 5540558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // sure not to try to set attributes. 5550558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner bool ShouldSetAttributes = true; 5560558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (!isa<llvm::FunctionType>(Ty)) { 5570558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Ty = llvm::FunctionType::get(llvm::Type::VoidTy, 5580558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner std::vector<const llvm::Type*>(), false); 5590558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner ShouldSetAttributes = false; 5600558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 5610558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 5620558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function::ExternalLinkage, 563d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner "", &getModule()); 564d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner F->setName(MangledName); 56574391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D && ShouldSetAttributes) 5660558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner SetFunctionAttributes(D, F); 5670558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = F; 5680558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return F; 5690558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner} 5700558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 57174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfFunction - Return the address of the given function. If Ty is 57274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// non-null, then this function will use the specified type if it has to 57374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create it (this occurs when we see a definition of the function). 57474391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D, 57574391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 57674391b48b4791cded373683a3baf67314f358d50Chris Lattner // If there was no specific requested type, just convert it now. 57774391b48b4791cded373683a3baf67314f358d50Chris Lattner if (!Ty) 57874391b48b4791cded373683a3baf67314f358d50Chris Lattner Ty = getTypes().ConvertType(D->getType()); 57974391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMFunction(getMangledName(D), Ty, D); 58074391b48b4791cded373683a3baf67314f358d50Chris Lattner} 58177ba708819285931932ecd33691a672bb59d221aEli Friedman 58274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeFunction - Create a new runtime function with the specified 58374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// type and name. 58474391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 58574391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 58674391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 58774391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 58874391b48b4791cded373683a3baf67314f358d50Chris Lattner Name = getContext().Idents.get(Name).getName(); 58974391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMFunction(Name, FTy, 0); 59074391b48b4791cded373683a3baf67314f358d50Chris Lattner} 591bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 59274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 59374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create and return an llvm GlobalVariable with the specified type. If there 59474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 59574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 59674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 59774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 59874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the global when it is first created. 59974391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName, 60074391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::PointerType*Ty, 60174391b48b4791cded373683a3baf67314f358d50Chris Lattner const VarDecl *D) { 6023c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 6035d4f5c724533b994de05df49ae259120482ec366Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 60499b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner if (Entry) { 60574391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Entry->getType() == Ty) 606570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner return Entry; 607570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 60899b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Make sure the result is of the correct type. 60974391b48b4791cded373683a3baf67314f358d50Chris Lattner return llvm::ConstantExpr::getBitCast(Entry, Ty); 61099b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner } 61167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 612b75863d53b8a2bbf0ece8e6df2b6e5be7f3896c4Chris Lattner // We don't support __thread yet. 613b75863d53b8a2bbf0ece8e6df2b6e5be7f3896c4Chris Lattner if (D && D->isThreadSpecified()) 614b75863d53b8a2bbf0ece8e6df2b6e5be7f3896c4Chris Lattner ErrorUnsupported(D, "thread local ('__thread') variable", true); 615b75863d53b8a2bbf0ece8e6df2b6e5be7f3896c4Chris Lattner 61667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 61767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 61867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 61967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner llvm::DenseMap<const char*, const ValueDecl*>::iterator DDI = 62067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.find(MangledName); 62167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 62267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 62367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 62467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 62567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 62667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 62767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 62899b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner llvm::GlobalVariable *GV = 62974391b48b4791cded373683a3baf67314f358d50Chris Lattner new llvm::GlobalVariable(Ty->getElementType(), false, 63099b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner llvm::GlobalValue::ExternalLinkage, 631d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner 0, "", &getModule(), 63274391b48b4791cded373683a3baf67314f358d50Chris Lattner 0, Ty->getAddressSpace()); 633d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner GV->setName(MangledName); 63449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 63599b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Handle things which are present even on external declarations. 63674391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D) { 63774391b48b4791cded373683a3baf67314f358d50Chris Lattner // FIXME: This code is overly simple and should be merged with 63874391b48b4791cded373683a3baf67314f358d50Chris Lattner // other global handling. 63974391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setConstant(D->getType().isConstant(Context)); 64049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 64174391b48b4791cded373683a3baf67314f358d50Chris Lattner // FIXME: Merge with other attribute handling code. 64274391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D->getStorageClass() == VarDecl::PrivateExtern) 64304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 64449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 645b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>()) 64674391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 64774391b48b4791cded373683a3baf67314f358d50Chris Lattner } 64874391b48b4791cded373683a3baf67314f358d50Chris Lattner 64974391b48b4791cded373683a3baf67314f358d50Chris Lattner return Entry = GV; 65074391b48b4791cded373683a3baf67314f358d50Chris Lattner} 651eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 652eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 65374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 65474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// given global variable. If Ty is non-null and if the global doesn't exist, 65574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// then it will be greated with the specified type instead of whatever the 65674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// normal requested type would be. 65774391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 65874391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 65974391b48b4791cded373683a3baf67314f358d50Chris Lattner assert(D->hasGlobalStorage() && "Not a global variable"); 66074391b48b4791cded373683a3baf67314f358d50Chris Lattner QualType ASTTy = D->getType(); 66174391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Ty == 0) 66274391b48b4791cded373683a3baf67314f358d50Chris Lattner Ty = getTypes().ConvertTypeForMem(ASTTy); 66374391b48b4791cded373683a3baf67314f358d50Chris Lattner 66474391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::PointerType *PTy = 66574391b48b4791cded373683a3baf67314f358d50Chris Lattner llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 66674391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D); 66774391b48b4791cded373683a3baf67314f358d50Chris Lattner} 6683f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar 66974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeVariable - Create a new runtime global variable with the 67074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// specified type and name. 67174391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 67274391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 67374391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 67474391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 67574391b48b4791cded373683a3baf67314f358d50Chris Lattner Name = getContext().Idents.get(Name).getName(); 67674391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); 677bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 678bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 679bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 6808f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 68177ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 682b75863d53b8a2bbf0ece8e6df2b6e5be7f3896c4Chris Lattner 6838f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 684cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 685cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // implicitly initialized with { 0 } 686570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner const llvm::Type *InitTy = getTypes().ConvertTypeForMem(ASTTy); 687cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman if (ASTTy->isIncompleteArrayType()) { 688cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // An incomplete array is normally [ TYPE x 0 ], but we need 689cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // to fix it to [ TYPE x 1 ]. 690570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner const llvm::ArrayType* ATy = cast<llvm::ArrayType>(InitTy); 691cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); 692cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } 693cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Init = llvm::Constant::getNullValue(InitTy); 69477ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 695e9352cc9818ba59e7cf88500ef048991c90f3821Anders Carlsson Init = EmitConstantExpr(D->getInit(), D->getType()); 6966e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman if (!Init) { 697232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar ErrorUnsupported(D, "static initializer"); 6986e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman QualType T = D->getInit()->getType(); 6996e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 7006e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman } 7018f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 7028e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 7032d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner const llvm::Type* InitType = Init->getType(); 704570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); 705570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 706570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Strip off a bitcast if we got one back. 707570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 708570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast); 709570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry = CE->getOperand(0); 710570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner } 7113c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 712570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Entry is now either a Function or GlobalVariable. 713570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); 714570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 715570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // If we already have this global and it has an initializer, then 716570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // we are in the rare situation where we emitted the defining 717570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // declaration of the global and are now being asked to emit a 718570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // definition which would be common. This occurs, for example, in 719570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // the following situation because statics can be emitted out of 720570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // order: 721570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 722570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // static int x; 723570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // static int *y = &x; 724570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // static int x = 10; 725570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // int **z = &y; 726570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 727570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Bail here so we don't blow away the definition. Note that if we 728570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // can't distinguish here if we emitted a definition with a null 729570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // initializer, but this case is safe. 730570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (GV && GV->hasInitializer() && !GV->getInitializer()->isNullValue()) { 731232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar assert(!D->getInit() && "Emitting multiple definitions of a decl!"); 732232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar return; 733570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner } 734570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 735570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We have a definition after a declaration with the wrong type. 736570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We must make a new GlobalVariable* and update everything that used OldGV 737570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (a declaration or tentative definition) with the new GlobalVariable* 738570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (which will be a definition). 739570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 740570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // This happens if there is a prototype for a global (e.g. 741570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "extern int x[];") and then a definition of a different type (e.g. 742570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "int x[10];"). This also happens when an initializer has a different type 743570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // from the type of the global (this happens with unions). 744570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (GV == 0 || 745570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getElementType() != InitType || 746570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { 747570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner 748570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Remove the old entry from GlobalDeclMap so that we'll create a new one. 749570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GlobalDeclMap.erase(getMangledName(D)); 750232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 751570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Make a new global with the correct type, this is now guaranteed to work. 752570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); 7530558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner GV->takeName(cast<llvm::GlobalValue>(Entry)); 7540558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 75577ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 75677ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::Constant *NewPtrForOldDecl = 757570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 758570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 75977ba708819285931932ecd33691a672bb59d221aEli Friedman 76077ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 761570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 76277ba708819285931932ecd33691a672bb59d221aEli Friedman } 76377ba708819285931932ecd33691a672bb59d221aEli Friedman 7648bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 7658bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 7668bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 767f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 7688bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 7698bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 77088a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 771b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes GV->setConstant(D->getType().isConstant(Context)); 7720de40af3a3aa14e3854c0eafeabd08f6762801f9Eli Friedman GV->setAlignment(getContext().getDeclAlignInBytes(D)); 77308d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 77488a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 7758fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner if (D->getStorageClass() == VarDecl::Static) 7768fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 777b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar else if (D->hasAttr<DLLImportAttr>()) 778ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 779b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar else if (D->hasAttr<DLLExportAttr>()) 780ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 781b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar else if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakImportAttr>()) 782286acbdbe0c82e9a6bcad5fca3c4fa582f3f1a2cMike Stump GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 78304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar else if (!CompileOpts.NoCommon && 78404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar (!D->hasExternalStorage() && !D->getInit())) 78504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 7867e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar else 78704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 7887e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar 78904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar setGlobalVisibility(GV, D); 79017f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 791b11fa0d25d86169f0e0a29d5398116c0212bb787Daniel Dunbar if (D->hasAttr<UsedAttr>()) 7925c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 7935c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 79404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 79504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setSection(SA->getName()); 79604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 797686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 7982d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner if (CGDebugInfo *DI = getDebugInfo()) { 79966031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 800686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 801686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 80288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 8035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 804bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 805bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { 8062b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson const llvm::FunctionType *Ty; 8072b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson 8082b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 8092b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson bool isVariadic = D->getType()->getAsFunctionProtoType()->isVariadic(); 8102b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson 8112b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson Ty = getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), isVariadic); 8122b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } else { 8132b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson Ty = cast<llvm::FunctionType>(getTypes().ConvertType(D->getType())); 8142b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson 8152b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // As a special case, make sure that definitions of K&R function 8162b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // "type foo()" aren't declared as varargs (which forces the backend 8172b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // to do unnecessary work). 8182b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson if (D->getType()->isFunctionNoProtoType()) { 8192b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson assert(Ty->isVarArg() && "Didn't lower type as expected"); 8202b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // Due to stret, the lowered function could have arguments. 8212b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // Just create the same type as was lowered by ConvertType 8222b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson // but strip off the varargs bit. 8232b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson std::vector<const llvm::Type*> Args(Ty->param_begin(), Ty->param_end()); 8242b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson Ty = llvm::FunctionType::get(Ty->getReturnType(), Args, false); 8252b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson } 826ff75e1db95a53c7606e0bb114cf9adc59ab3d7f6Chris Lattner } 827d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 82834809507232bc4c3c4840c7d092c7440219fddafChris Lattner // Get or create the prototype for teh function. 8290558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Constant *Entry = GetAddrOfFunction(D, Ty); 83034809507232bc4c3c4840c7d092c7440219fddafChris Lattner 8310558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Strip off a bitcast if we got one back. 8320558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 8330558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast); 8340558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = CE->getOperand(0); 8350558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 8360558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 8370558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 8380558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { 83942745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 8400558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner assert(cast<llvm::GlobalValue>(Entry)->isDeclaration() && 8410558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner "Shouldn't replace non-declaration"); 84234809507232bc4c3c4840c7d092c7440219fddafChris Lattner 84362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // F is the Function* for the one with the wrong type, we must make a new 84462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* and update everything that used F (a declaration) with the new 84562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* (which will be a definition). 84662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // 84762b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // This happens if there is a prototype for a function 84862b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f()") and then a definition of a different type 84962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f(int x)"). Start by making a new function of the 85062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // correct type, RAUW, then steal the name. 85134809507232bc4c3c4840c7d092c7440219fddafChris Lattner GlobalDeclMap.erase(getMangledName(D)); 8520558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(D, Ty)); 8530558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner NewFn->takeName(cast<llvm::GlobalValue>(Entry)); 85462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner 85562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Replace uses of F with the Function we will endow with a body. 85662b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner llvm::Constant *NewPtrForOldDecl = 8570558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 8580558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 85962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner 86062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Ok, delete the old function now, which is dead. 8610558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 86262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner 8630558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = NewFn; 864bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 8650558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 8660558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *Fn = cast<llvm::Function>(Entry); 867bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 868219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 8696379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 870219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(D, Fn); 871219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 87234809507232bc4c3c4840c7d092c7440219fddafChris Lattner if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 873219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 87434809507232bc4c3c4840c7d092c7440219fddafChris Lattner if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 875219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 876bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 877bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 878bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattnervoid CodeGenModule::EmitAliasDefinition(const ValueDecl *D) { 879bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const AliasAttr *AA = D->getAttr<AliasAttr>(); 880bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner assert(AA && "Not an alias?"); 881bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 882bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 883bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 884bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Unique the name through the identifier table. 885bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const char *AliaseeName = AA->getAliasee().c_str(); 886bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner AliaseeName = getContext().Idents.get(AliaseeName).getName(); 887bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 888bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create a reference to the named value. This ensures that it is emitted 889bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // if a deferred decl. 890bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Constant *Aliasee; 891bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (isa<llvm::FunctionType>(DeclTy)) 892bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, 0); 893bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner else 894bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Aliasee = GetOrCreateLLVMGlobal(AliaseeName, 895bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::PointerType::getUnqual(DeclTy), 0); 896bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 897bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create the new alias itself, but don't set a name yet. 898bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::GlobalValue *GA = 899bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner new llvm::GlobalAlias(Aliasee->getType(), 900bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Function::ExternalLinkage, 901bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner "", Aliasee, &getModule()); 902bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 903bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // See if there is already something with the alias' name in the module. 904bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const char *MangledName = getMangledName(D); 905bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 906bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 907bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry && !Entry->isDeclaration()) { 908bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a definition in the module, then it wins over the alias. 909bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // This is dubious, but allow it to be safe. Just ignore the alias. 910bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->eraseFromParent(); 911bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return; 912bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 913bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 914bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry) { 915bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a declaration in the module, then we had an extern followed 916bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // by the alias, as in: 917bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // extern int test6(); 918bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // ... 919bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // int test6() __attribute__((alias("test7"))); 920bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // 921bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Remove it and replace uses of it with the alias. 922bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 923bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 924bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->getType())); 925bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->eraseFromParent(); 926bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 927bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 928bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Now we know that there is no conflict, set the name. 929bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry = GA; 930bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->setName(MangledName); 931bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 932bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Alias should never be internal or inline. 93344b0bc008ee11cdee69ad12210ca7550e4fa426aChris Lattner SetGlobalValueAttributes(D, GVA_Normal, GA, true); 934bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner} 935bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 936b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// getBuiltinLibFunction - Given a builtin id for a function like 937b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// "__builtin_fabsf", return a Function* for "fabsf". 938c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stumpllvm::Value *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { 9393e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 9403e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 9413e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor "isn't a lib fn"); 942bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9433e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor // Get the name, skip over the __builtin_ prefix (if necessary). 9443e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 9453e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 9463e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Name += 10; 947bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 948bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 949370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor Builtin::Context::GetBuiltinTypeError Error; 950370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context, Error); 951370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor assert(Error == Builtin::Context::GE_None && "Can't get builtin type"); 952370ab3f1373841d70582feac9e35c3c6b3489f63Douglas Gregor 953bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const llvm::FunctionType *Ty = 954bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 955bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 956b808c952bbff821dce727dd801a1098d64394f98Chris Lattner // Unique the name through the identifier table. 957b808c952bbff821dce727dd801a1098d64394f98Chris Lattner Name = getContext().Idents.get(Name).getName(); 958bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: param attributes for sext/zext etc. 959b808c952bbff821dce727dd801a1098d64394f98Chris Lattner return GetOrCreateLLVMFunction(Name, Ty, 0); 960bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 961bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9627acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 9637acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 9647acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 9657acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 9667acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 967bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 9685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 9695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 9704e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9714e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 9725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 973c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 9740c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 9750c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 9764e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9774e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 9780c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 9790c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 98041ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 98141ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 9824e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 9834e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 98441ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 9857acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 986e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM, 987e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*>& Fields, 98844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *FieldD, FieldDecl *NextFieldD, 98944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::Constant* Field, 9903c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner RecordDecl* RD, const llvm::StructType *STy) { 991e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append the field. 992e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(Field); 993e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 99444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); 995e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 996e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson int NextStructFieldNo; 99744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (!NextFieldD) { 998e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson NextStructFieldNo = STy->getNumElements(); 999e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } else { 100044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); 1001e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 1002e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1003e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append padding 1004e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { 1005e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant *C = 1006e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); 1007e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1008e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(C); 1009e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 1010e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson} 1011e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1012bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule:: 10138d4141f83d9de379547cf05bd75d4c6cf894b189Steve NaroffGetAddrOfConstantCFString(const StringLiteral *Literal) { 1014b59212a6e494d2c364b54462f545833902c29158Steve Naroff std::string str; 1015b59212a6e494d2c364b54462f545833902c29158Steve Naroff unsigned StringLength; 1016b59212a6e494d2c364b54462f545833902c29158Steve Naroff 1017e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff bool isUTF16 = false; 1018e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff if (Literal->containsNonAsciiOrNull()) { 1019e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff // Convert from UTF-8 to UTF-16. 1020e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff llvm::SmallVector<UTF16, 128> ToBuf(Literal->getByteLength()); 1021e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff const UTF8 *FromPtr = (UTF8 *)Literal->getStrData(); 1022e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff UTF16 *ToPtr = &ToBuf[0]; 1023e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff 1024e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff ConversionResult Result; 1025e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff Result = ConvertUTF8toUTF16(&FromPtr, FromPtr+Literal->getByteLength(), 1026e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff &ToPtr, ToPtr+Literal->getByteLength(), 1027e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff strictConversion); 1028aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff if (Result == conversionOK) { 1029aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff // FIXME: Storing UTF-16 in a C string is a hack to test Unicode strings 1030aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff // without doing more surgery to this routine. Since we aren't explicitly 1031aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff // checking for endianness here, it's also a bug (when generating code for 1032aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff // a target that doesn't match the host endianness). Modeling this as an 1033aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff // i16 array is likely the cleanest solution. 1034aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff StringLength = ToPtr-&ToBuf[0]; 1035aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff str.assign((char *)&ToBuf[0], StringLength*2);// Twice as many UTF8 chars. 1036aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff isUTF16 = true; 1037aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff } else if (Result == sourceIllegal) { 1038fd942628abfe30e30427875db953222ae99b4325Steve Naroff // FIXME: Have Sema::CheckObjCString() validate the UTF-8 string. 1039aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff str.assign(Literal->getStrData(), Literal->getByteLength()); 1040aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff StringLength = str.length(); 1041aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff } else 1042aa4a756185e77755aaa10ae50db08ae5be58e70aSteve Naroff assert(Result == conversionOK && "UTF-8 to UTF-16 conversion failed"); 1043b59212a6e494d2c364b54462f545833902c29158Steve Naroff 1044b59212a6e494d2c364b54462f545833902c29158Steve Naroff } else { 1045b59212a6e494d2c364b54462f545833902c29158Steve Naroff str.assign(Literal->getStrData(), Literal->getByteLength()); 1046b59212a6e494d2c364b54462f545833902c29158Steve Naroff StringLength = str.length(); 1047e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff } 1048c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::StringMapEntry<llvm::Constant *> &Entry = 1049c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 1050c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 10518e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (llvm::Constant *C = Entry.getValue()) 10528e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar return C; 1053c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 10543e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); 10553e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 1056c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1057c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 1058c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 1059c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = llvm::ArrayType::get(Ty, 0); 10603e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10613e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // FIXME: This is fairly broken if 10623e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // __CFConstantStringClassReference is already defined, in that it 10633e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // will get renamed and the user will most likely see an opaque 10643e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // error message. This is a general issue with relying on 10653e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // particular names. 10663e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::GlobalVariable *GV = 1067c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson new llvm::GlobalVariable(Ty, false, 1068c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalVariable::ExternalLinkage, 0, 1069c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson "__CFConstantStringClassReference", 1070c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson &getModule()); 10713e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10723e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 10733e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 10743e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1075c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 1076c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1077e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 1078e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); 10793e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 1080e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson const llvm::StructType *STy = 1081e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1082e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 1083e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*> Fields; 10846ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor RecordDecl::field_iterator Field = CFRD->field_begin(getContext()); 108544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 1086c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 108744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *CurField = *Field++; 108844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *NextField = *Field++; 108944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 109044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringClassRef, CFRD, STy); 1091c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1092c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 109344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 109444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 10953e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 109644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 1097e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) 1098e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff : llvm::ConstantInt::get(Ty, 0x07C8), 1099e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff CFRD, STy); 1100c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1101c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 110244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 110344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 11043e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str); 1105a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar 1106a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar const char *Sect, *Prefix; 1107a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar bool isConstant; 1108a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 1109a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar Prefix = getContext().Target.getUnicodeStringSymbolPrefix(); 1110a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar Sect = getContext().Target.getUnicodeStringSection(); 1111a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar // FIXME: Why does GCC not set constant here? 1112a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar isConstant = false; 1113a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } else { 1114a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar Prefix = getContext().Target.getStringSymbolPrefix(true); 1115a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar Sect = getContext().Target.getCFStringDataSection(); 1116a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar // FIXME: -fwritable-strings should probably affect this, but we 1117a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar // are following gcc here. 1118a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar isConstant = true; 1119a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 11208e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar llvm::GlobalVariable *GV = 1121a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar new llvm::GlobalVariable(C->getType(), isConstant, 11228e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar llvm::GlobalValue::InternalLinkage, 1123a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar C, Prefix, &getModule()); 1124a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (Sect) 11258e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV->setSection(Sect); 1126a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 1127a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar unsigned Align = getContext().getTypeAlign(getContext().ShortTy)/8; 1128a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar GV->setAlignment(Align); 1129a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 113044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 11318e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2), 1132e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson CFRD, STy); 1133c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1134c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 113544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 113644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = 0; 1137c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 113844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 1139b59212a6e494d2c364b54462f545833902c29158Steve Naroff llvm::ConstantInt::get(Ty, StringLength), CFRD, STy); 1140c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 1141c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 1142e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C = llvm::ConstantStruct::get(STy, Fields); 11438e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV = new llvm::GlobalVariable(C->getType(), true, 11448e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar llvm::GlobalVariable::InternalLinkage, C, 11458e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar getContext().Target.getCFStringSymbolPrefix(), 11468e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar &getModule()); 11478e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (const char *Sect = getContext().Target.getCFStringSection()) 11488e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV->setSection(Sect); 11490c67829763b98bc670062b553897a851fab17401Anders Carlsson Entry.setValue(GV); 11503e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 11510c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1152c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 115345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 11546143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 11551e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 11566143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 11571e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 11581e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 11591e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11601e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 11611e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 11621e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 11631e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 1164dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner // Resize the string to the right size. 11651e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 11661e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 1167dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 1168dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner if (E->isWide()) 1169dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner RealLen *= getContext().Target.getWCharWidth()/8; 1170dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner 11711e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 11721e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11731e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 11741e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 11751e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 11766143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 11776143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 11786143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 11796143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 11806143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 11816143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar return GetAddrOfConstantString(GetStringForStringLiteral(S)); 11826143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 11836143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1184eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1185eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node. 1186eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant * 1187eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1188eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner std::string Str; 1189eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1190a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman 1191a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman return GetAddrOfConstantCString(Str); 1192eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner} 1193eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1194eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1195a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 119645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 119745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 11985fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 11995fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 12006143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 12016143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str, false); 120245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 120345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 1204eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return new llvm::GlobalVariable(C->getType(), constant, 1205eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner llvm::GlobalValue::InternalLinkage, 12068e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar C, GlobalName, &CGM.getModule()); 120745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 120845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 12096143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 12106143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 12116143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 12126143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 12136143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 12146143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 12156143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 12166143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 12175fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 12185fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 12198e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar bool IsConstant = !Features.WritableStrings; 12208e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 12218e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Get the default prefix if a name wasn't specified. 12228e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!GlobalName) 12238e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GlobalName = getContext().Target.getStringSymbolPrefix(IsConstant); 12248e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 12258e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Don't share any string literals if strings aren't constant. 12268e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!IsConstant) 12275fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 122845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 122945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::StringMapEntry<llvm::Constant *> &Entry = 123045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 123145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 123245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 1233eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return Entry.getValue(); 123445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 123545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 12365fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 123745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 123845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 123945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 12406143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 12416143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 12426143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 12436143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 12445fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 12455fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1246c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 12476143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 124841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1249af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1250af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 1251af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const 1252af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 1253af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), 1254af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar e = D->propimpl_end(); i != e; ++i) { 1255af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 1256af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1257af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1258af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1259af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1260af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1261af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1262af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1263af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1264af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1265af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 1266af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!D->getInstanceMethod(PD->getGetterName())) 1267fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1268fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1269af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 1270af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar !D->getInstanceMethod(PD->getSetterName())) 1271fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1272fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1273af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1274af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1275af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1276af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 127791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson/// EmitNamespace - Emit all declarations in a namespace. 1278984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlssonvoid CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { 12796ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor for (RecordDecl::decl_iterator I = ND->decls_begin(getContext()), 12806ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor E = ND->decls_end(getContext()); 1281984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson I != E; ++I) 1282984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitTopLevelDecl(*I); 1283984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson} 1284984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson 128591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson// EmitLinkageSpec - Emit all declarations in a linkage spec. 128691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlssonvoid CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 128791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson if (LSD->getLanguage() != LinkageSpecDecl::lang_c) { 128891e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson ErrorUnsupported(LSD, "linkage spec"); 128991e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson return; 129091e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson } 129191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 12926ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor for (RecordDecl::decl_iterator I = LSD->decls_begin(getContext()), 12936ab3524f72a6e64aa04973fa9433b5559abb3525Douglas Gregor E = LSD->decls_end(getContext()); 129491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson I != E; ++I) 129591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitTopLevelDecl(*I); 129691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson} 129791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 129841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 129941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 130041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 130141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 130241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 130341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 130441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 130541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 130641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 13072b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson case Decl::CXXMethod: 130841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 130941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 131041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar EmitGlobal(cast<ValueDecl>(D)); 131141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 131241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 131341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1314984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitNamespace(cast<NamespaceDecl>(D)); 131541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 131641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 131741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Objective-C Decls 131841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 131938e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian // Forward declarations, no (immediate) code generation. 132041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 132141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 1322b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCCategory: 1323285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner break; 1324b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCInterface: 1325285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner // If we already laid out this interface due to an @class, and if we 1326285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner // codegen'd a reference it, update the 'opaque' type to be a real type now. 1327285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner Types.UpdateCompletedType(cast<ObjCInterfaceDecl>(D)); 132841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1329285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner 133041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 1331b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 133241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 133341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 133441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1335af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1336af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 133741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 133841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 133941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1340af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1341af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1342af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1343af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 134441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1345af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 134641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 134741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 134841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 134941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (OMD->getBody()) 135041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 135141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 135241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 135341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCompatibleAlias: 1354305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 135541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 135641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 135791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson case Decl::LinkageSpec: 135891e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 135941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 136041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 136141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 136241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 136341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 136441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 136541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 136641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 136741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 136841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 136941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 137041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 137141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 137241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 137341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 137441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar default: 137541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Make sure we handled everything we should, every other kind is 137641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // a non-top-level decl. FIXME: Would be nice to have an 137741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // isTopLevelDeclKind function. Need to recode Decl::Kind to do 137841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // that easily. 137941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 138041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 138141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1382