CodeGenModule.cpp revision c0bf462cf35fe050bddbd8bff967298e4a67e79d
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" 2082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetInfo.h" 212811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth#include "clang/CodeGen/CodeGenOptions.h" 225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h" 23687cc4a850b59116efee061018f0d8df50728b82Ken Dyck#include "clang/AST/CharUnits.h" 24c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2521ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h" 261a5e0d7f18485e4fb958f96dcddff3e4486a4069Anders Carlsson#include "clang/AST/RecordLayout.h" 271b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h" 282c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h" 298bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h" 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 31e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff#include "clang/Basic/ConvertUTF.h" 32ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h" 33bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h" 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h" 35d5b8902c0329d80a216803b58dc3b689349a0c11Chris Lattner#include "llvm/LLVMContext.h" 3620ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h" 3778f7ece00e2ddfb64d4ed72a7be770b5b9b805e3Chris Lattner#include "llvm/Support/ErrorHandling.h" 385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 422811ccf48d6d898c42cc4cfad37abedb36236d20Chandler CarruthCodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO, 43fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner llvm::Module &M, const llvm::TargetData &TD, 44bd3606426d389370616126af969904ec493cb105Chris Lattner Diagnostic &diags) 45bd3606426d389370616126af969904ec493cb105Chris Lattner : BlockModule(C, M, TD, Types, *this), Context(C), 462811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth Features(C.getLangOptions()), CodeGenOpts(CGO), TheModule(M), 4782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov TheTargetData(TD), TheTargetCodeGenInfo(0), Diags(diags), 4882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov Types(C, M, TD, getTargetCodeGenInfo().getABIInfo()), 4982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov MangleCtx(C), VtableInfo(*this), Runtime(0), 50a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson MemCpyFn(0), MemMoveFn(0), MemSetFn(0), CFConstantStringClassRef(0), 51a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson VMContext(M.getContext()) { 52208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 533c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner if (!Features.ObjC1) 543c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = 0; 553c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (!Features.NeXTRuntime) 563c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateGNUObjCRuntime(*this); 573c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else if (Features.ObjCNonFragileABI) 583c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacNonFragileABIObjCRuntime(*this); 593c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner else 603c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner Runtime = CreateMacObjCRuntime(*this); 61e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 62e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 6320f12a20ba9cfa6f8d53c8304e24f50903c45184Anders Carlsson DebugInfo = CodeGenOpts.DebugInfo ? new CGDebugInfo(*this) : 0; 642b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 652b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 662b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 67815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 68815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 69815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 70815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 710d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnallvoid CodeGenModule::createObjCRuntime() { 720d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall if (!Features.NeXTRuntime) 730d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall Runtime = CreateGNUObjCRuntime(*this); 740d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall else if (Features.ObjCNonFragileABI) 750d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall Runtime = CreateMacNonFragileABIObjCRuntime(*this); 760d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall else 770d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall Runtime = CreateMacObjCRuntime(*this); 780d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall} 790d13f6fdbdd6f06e2449b8834dda53334abd399aDavid Chisnall 80815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 8182227ff4eb665bbf41720ebdc0dc9215a86ba838Chris Lattner EmitDeferred(); 826c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman EmitCXXGlobalInitFunc(); 83208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 84208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 85208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 866bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 876bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 88532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 890269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitLLVMUsed(); 90f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 91f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 92488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 932c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 9490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 9590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 9690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 9790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 9956b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 1002c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 1010a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 1020a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 1032c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 10458c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 105488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 106c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 10790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 10890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 10990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 11090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 1111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 11256b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 113c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 1140a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 115c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 116c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 1171eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpLangOptions::VisibilityMode 11804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel DunbarCodeGenModule::getDeclVisibilityMode(const Decl *D) const { 11904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 12004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar if (VD->getStorageClass() == VarDecl::PrivateExtern) 12104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 12204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 12340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) { 12404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (attr->getVisibility()) { 12504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar default: assert(0 && "Unknown visibility!"); 1261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case VisibilityAttr::DefaultVisibility: 12704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Default; 12804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::HiddenVisibility: 12904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Hidden; 13004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case VisibilityAttr::ProtectedVisibility: 13104d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return LangOptions::Protected; 13204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar } 1337e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 1346ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar 13539de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson // This decl should have the same visibility as its parent. 13639de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson if (const DeclContext *DC = D->getDeclContext()) 13739de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson return getDeclVisibilityMode(cast<Decl>(DC)); 13839de84d671bac60662251e98f20a5a6039795dfcAnders Carlsson 13904d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return getLangOptions().getVisibilityMode(); 1404f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1414f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, 14304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar const Decl *D) const { 14404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar // Internal definitions always have default visibility. 145df102fcb978588d5edbc661fb5da0b6922f9ab1cChris Lattner if (GV->hasLocalLinkage()) { 1467e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1476ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar return; 1487e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar } 1496ab187a49a42de6d351248d8a6e0206e39743a0cDaniel Dunbar 15004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar switch (getDeclVisibilityMode(D)) { 1517cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian default: assert(0 && "Unknown visibility!"); 15204d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Default: 15304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 15404d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Hidden: 15504d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 15604d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar case LangOptions::Protected: 15704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar return GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1587cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian } 1597cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian} 1607cd2e93125e2f3b6ca01b24ed0c3fd7e94683fd9Fariborz Jahanian 1612a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlssonconst char *CodeGenModule::getMangledName(const GlobalDecl &GD) { 1624a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson const NamedDecl *ND = cast<NamedDecl>(GD.getDecl()); 1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1642a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXConstructorDecl *D = dyn_cast<CXXConstructorDecl>(ND)) 1652a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson return getMangledCXXCtorName(D, GD.getCtorType()); 1662a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(ND)) 1672a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson return getMangledCXXDtorName(D, GD.getDtorType()); 1681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1692a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson return getMangledName(ND); 1702a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson} 1712a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson 1725f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration. 1735f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1745f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an 175c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// const char* containing the mangled name. Otherwise, returns 176c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner/// the unmangled name. 1775f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1786ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregorconst char *CodeGenModule::getMangledName(const NamedDecl *ND) { 179f981bf84ad7745861bc9c971f46417527e1e5db5Daniel Dunbar if (!getMangleContext().shouldMangleDeclName(ND)) { 180c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner assert(ND->getIdentifier() && "Attempt to mangle unnamed decl."); 1813c8f153ae767fa55b2da74438c7f8ec370a06d6fChris Lattner return ND->getNameAsCString(); 182c50689bd1e8788a7fc8f19070b7505ff95034979Chris Lattner } 1831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1846ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::SmallString<256> Name; 185f981bf84ad7745861bc9c971f46417527e1e5db5Daniel Dunbar getMangleContext().mangleName(ND, Name); 1866ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor Name += '\0'; 18795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson return UniqueMangledName(Name.begin(), Name.end()); 18895d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson} 18995d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson 19095d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlssonconst char *CodeGenModule::UniqueMangledName(const char *NameStart, 19195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson const char *NameEnd) { 19295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson assert(*(NameEnd - 1) == '\0' && "Mangled name must be null terminated!"); 1931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19495d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson return MangledNames.GetOrCreateValue(NameStart, NameEnd).getKeyData(); 1955f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1965f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1976d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1986d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 20049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 2026d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2036d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 2056bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 20749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 2086bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 2096bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 2141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 2156bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 2166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 21796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 2186bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2196bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::StructType* CtorStructTy = 2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::StructType::get(VMContext, llvm::Type::getInt32Ty(VMContext), 22296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::getUnqual(CtorFTy), NULL); 2236bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2246bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 2256bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 2266bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 2276bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 2281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump S.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2290032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson I->second, false)); 2303c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 23108e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 2326bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 2336bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2346bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 23596e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2361c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson new llvm::GlobalVariable(TheModule, AT, false, 237572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 2387db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::ConstantArray::get(AT, Ctors), 2391c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson GlobalName); 2406d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 2416d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2426d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 243532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 244532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 245532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 246532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 247532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 248532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 24996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 250532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 251532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 2521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue *gv = 2531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump new llvm::GlobalVariable(TheModule, Array->getType(), false, 2541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue::AppendingLinkage, Array, 2551c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson "llvm.global.annotations"); 256532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 257532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 258532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 25986daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattnerstatic CodeGenModule::GVALinkage 2601eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpGetLinkageForFunction(ASTContext &Context, const FunctionDecl *FD, 26168584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor const LangOptions &Features) { 2620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor CodeGenModule::GVALinkage External = CodeGenModule::GVA_StrongExternal; 2639aeed32282fe8a775c24c01c923717ca86695685John McCall 2640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor Linkage L = FD->getLinkage(); 2650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor if (L == ExternalLinkage && Context.getLangOptions().CPlusPlus && 2660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor FD->getType()->getLinkage() == UniqueExternalLinkage) 2670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor L = UniqueExternalLinkage; 2687d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 2690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor switch (L) { 2700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case NoLinkage: 2710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case InternalLinkage: 2720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case UniqueExternalLinkage: 2730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor return CodeGenModule::GVA_Internal; 274548e60efea9a1d2c31679e11e9ff0d2f19b96694Anders Carlsson 2750b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case ExternalLinkage: 2760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor switch (FD->getTemplateSpecializationKind()) { 2770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_Undeclared: 2780b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_ExplicitSpecialization: 2790b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor External = CodeGenModule::GVA_StrongExternal; 2800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 2810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 2820b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_ExplicitInstantiationDefinition: 2830b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor // FIXME: explicit instantiation definitions should use weak linkage 284548e60efea9a1d2c31679e11e9ff0d2f19b96694Anders Carlsson return CodeGenModule::GVA_StrongExternal; 2850b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 2860b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_ExplicitInstantiationDeclaration: 2870b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case TSK_ImplicitInstantiation: 288548e60efea9a1d2c31679e11e9ff0d2f19b96694Anders Carlsson External = CodeGenModule::GVA_TemplateInstantiation; 2890b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 2900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor } 291548e60efea9a1d2c31679e11e9ff0d2f19b96694Anders Carlsson } 2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2937ced9c8529b734e313f62a3b81189d6f402f6713Douglas Gregor if (!FD->isInlined()) 2941fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return External; 2950b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 2961fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor if (!Features.CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { 2971fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // GNU or C99 inline semantics. Determine whether this symbol should be 2981fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // externally visible. 2991fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor if (FD->isInlineDefinitionExternallyVisible()) 3001fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor return External; 301cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner 3021fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor // C99 inline semantics, where the symbol is not externally visible. 303b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor return CodeGenModule::GVA_C99Inline; 3041fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor } 305b3efa98e320590e8be9d62818e89e599303e65b4Douglas Gregor 3067d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // C++0x [temp.explicit]p9: 3077d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // [ Note: The intent is that an inline function that is the subject of 3087d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // an explicit instantiation declaration will still be implicitly 3097d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // instantiated when used so that the body can be considered for 3107d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // inlining, but that no out-of-line copy of the inline function would be 3117d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor // generated in the translation unit. -- end note ] 3127d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor if (FD->getTemplateSpecializationKind() 3137d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor == TSK_ExplicitInstantiationDeclaration) 3147d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor return CodeGenModule::GVA_C99Inline; 3157d9c3c92c90ae36d58ec21bc53c4c08e02ac3555Douglas Gregor 3161fc09a92d0bffda20e06fa882388c01e192e2069Douglas Gregor return CodeGenModule::GVA_CXXInline; 3177c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar} 3187c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 319d46f98573ba104eda102dd3224b2dca69f1c6336John McCallllvm::GlobalValue::LinkageTypes 320d46f98573ba104eda102dd3224b2dca69f1c6336John McCallCodeGenModule::getFunctionLinkage(const FunctionDecl *D) { 32168584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor GVALinkage Linkage = GetLinkageForFunction(getContext(), D, Features); 3227c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 32355d6f50b23a1fd0a04b568787a25beb7537e6c9bDaniel Dunbar if (Linkage == GVA_Internal) { 324d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::InternalLinkage; 32540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis } else if (D->hasAttr<DLLExportAttr>()) { 326d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::DLLExportLinkage; 32740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis } else if (D->hasAttr<WeakAttr>()) { 328d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::WeakAnyLinkage; 329cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner } else if (Linkage == GVA_C99Inline) { 330cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner // In C99 mode, 'inline' functions are guaranteed to have a strong 331cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner // definition somewhere else, so we can use available_externally linkage. 332d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::AvailableExternallyLinkage; 3331fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor } else if (Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) { 33486daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // In C++, the compiler has to emit a definition in every translation unit 33586daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // that references the function. We should use linkonce_odr because 33686daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // a) if all references in this translation unit are optimized away, we 33786daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // don't need to codegen it. b) if the function persists, it needs to be 33886daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // merged with other definitions. c) C++ has the ODR, so we know the 33986daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // definition is dependable. 340d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::LinkOnceODRLinkage; 3417c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 342cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner assert(Linkage == GVA_StrongExternal); 343cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner // Otherwise, we have strong external linkage. 344d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return llvm::Function::ExternalLinkage; 3450dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 346d46f98573ba104eda102dd3224b2dca69f1c6336John McCall} 347d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 348d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 349d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// SetFunctionDefinitionAttributes - Set attributes for a global. 350d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// 351d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// FIXME: This is currently only done for aliases and functions, but not for 352d46f98573ba104eda102dd3224b2dca69f1c6336John McCall/// variables (these details are set in EmitGlobalVarDefinition for variables). 353d46f98573ba104eda102dd3224b2dca69f1c6336John McCallvoid CodeGenModule::SetFunctionDefinitionAttributes(const FunctionDecl *D, 354d46f98573ba104eda102dd3224b2dca69f1c6336John McCall llvm::GlobalValue *GV) { 355d46f98573ba104eda102dd3224b2dca69f1c6336John McCall GV->setLinkage(getFunctionLinkage(D)); 3567c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GV); 357d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 358d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 3597dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributes(const Decl *D, 3601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const CGFunctionInfo &Info, 3617dbd8197040313d796282d4af06eccdf8a17319cDaniel Dunbar llvm::Function *F) { 362ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar unsigned CallingConv; 363761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 364ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar ConstructAttributeList(Info, D, AttributeList, CallingConv); 365761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 366ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar AttributeList.size())); 367ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 368f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 369f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 3707c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbarvoid CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, 3717c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::Function *F) { 37274ac74ae244c501027924c99f2a33559a1e23b53Daniel Dunbar if (!Features.Exceptions && !Features.ObjCNonFragileABI) 3731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump F->addFnAttr(llvm::Attribute::NoUnwind); 374af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 37540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<AlwaysInlineAttr>()) 376af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 3771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3781feade8e520be483293dbf55eb57a51720899589Mike Stump if (D->hasAttr<NoInlineAttr>()) 37981ebbde0fb30a40df0f5e913d8a1f71c383d271aAnders Carlsson F->addFnAttr(llvm::Attribute::NoInline); 380f55314dce992fd60816ba337ad151a2fb7c42239Mike Stump 381fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson if (Features.getStackProtectorMode() == LangOptions::SSPOn) 382fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson F->addFnAttr(llvm::Attribute::StackProtect); 383fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson else if (Features.getStackProtectorMode() == LangOptions::SSPReq) 384fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson F->addFnAttr(llvm::Attribute::StackProtectReq); 385fd015353a3c4f528216276f25df5b4d464d7a0cdAnders Carlsson 386bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) { 387bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt unsigned width = Context.Target.getCharWidth(); 388bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt F->setAlignment(AA->getAlignment() / width); 389bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt while ((AA = AA->getNext<AlignedAttr>())) 390bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt F->setAlignment(std::max(F->getAlignment(), AA->getAlignment() / width)); 391bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt } 392fb51ddfafcd5f8536d0312b3daa3c0b74b90ab5bMike Stump // C++ ABI requires 2-byte alignment for member functions. 393bd6dbd19781cefd5b5ff9750c8bf86e6c341a68cMike Stump if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) 394bd6dbd19781cefd5b5ff9750c8bf86e6c341a68cMike Stump F->setAlignment(2); 395f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 396f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::SetCommonAttributes(const Decl *D, 3987c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar llvm::GlobalValue *GV) { 3997c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar setGlobalVisibility(GV, D); 4007c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 40140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<UsedAttr>()) 4027c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar AddUsedGlobal(GV); 4037c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 40440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 4057c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GV->setSection(SA->getName()); 40682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov 40782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov getTargetCodeGenInfo().SetTargetAttributes(D, GV, *this); 4087c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar} 4097c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 4100e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbarvoid CodeGenModule::SetInternalFunctionAttributes(const Decl *D, 4110e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar llvm::Function *F, 4120e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar const CGFunctionInfo &FI) { 4130e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetLLVMFunctionAttributes(D, FI, F); 4140e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetLLVMFunctionAttributesForDefinition(D, F); 4157c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 4167c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::InternalLinkage); 4177c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 4180e4f40e1bbc4dce16bbb9870300a435419f1b3d5Daniel Dunbar SetCommonAttributes(D, F); 419f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 420f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 421b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlssonvoid CodeGenModule::SetFunctionAttributes(GlobalDecl GD, 422c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman llvm::Function *F, 423c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman bool IsIncompleteFunction) { 424b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 425b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson 426c6c14d1cd68afcd90d097715296377f15be45210Eli Friedman if (!IsIncompleteFunction) 427b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson SetLLVMFunctionAttributes(FD, getTypes().getFunctionInfo(GD), F); 4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4297c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // Only a few attributes are set on declarations; these may later be 4307c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // overridden by a definition. 4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 43240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (FD->hasAttr<DLLImportAttr>()) { 4337c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::DLLImportLinkage); 4341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (FD->hasAttr<WeakAttr>() || 43540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis FD->hasAttr<WeakImportAttr>()) { 4367c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // "extern_weak" is overloaded in LLVM; we probably should have 4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // separate linkage types for this. 4387c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setLinkage(llvm::Function::ExternalWeakLinkage); 4397c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump F->setLinkage(llvm::Function::ExternalLinkage); 4417c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 4427c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 44340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const SectionAttr *SA = FD->getAttr<SectionAttr>()) 4447c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar F->setSection(SA->getName()); 445219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 446219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 4470269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(!GV->isDeclaration() && 4490269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 45035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner LLVMUsed.push_back(GV); 4510269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4520269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4530269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 4540269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 455ad64e024bd18cf25dcfa44e049004371838decd8Chris Lattner if (LLVMUsed.empty()) 4560269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 4570269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4583c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext); 4591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 46035f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner // Convert LLVMUsed to what ConstantArray needs. 46135f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner std::vector<llvm::Constant*> UsedArray; 46235f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner UsedArray.resize(LLVMUsed.size()); 46335f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { 4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump UsedArray[i] = 4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), 466a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson i8PTy); 46735f38a2c22d68c22e2dbe8e9ee84c120c8f327bbChris Lattner } 4681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 469c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian if (UsedArray.empty()) 470c38e9affd4519ea199af22419c8c794973cc4b23Fariborz Jahanian return; 47196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedArray.size()); 4721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable *GV = 4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump new llvm::GlobalVariable(getModule(), ATy, false, 4750269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 4767db6d838aad4083fe86d7bf703a75fe6e8a17856Owen Anderson llvm::ConstantArray::get(ATy, UsedArray), 4771c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson "llvm.used"); 4780269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4790269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 4800269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4810269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4820269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 48367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Emit code for any potentially referenced deferred decls. Since a 48467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // previously unused static decl may become used during the generation of code 48567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // for a static function, iterate until no changes are made. 48667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner while (!DeferredDeclsToEmit.empty()) { 4872a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson GlobalDecl D = DeferredDeclsToEmit.back(); 48867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.pop_back(); 48967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner 49067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // The mangled name for the decl must have been emitted in GlobalDeclMap. 49167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Look it up to see if it was defined with a stronger definition (e.g. an 49267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // extern inline function with a strong function redefinition). If so, 49367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // just ignore the deferred decl. 49467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner llvm::GlobalValue *CGRef = GlobalDeclMap[getMangledName(D)]; 49567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner assert(CGRef && "Deferred decl wasn't referenced?"); 4961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 49767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (!CGRef->isDeclaration()) 49867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner continue; 4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 50067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, emit the definition and move on to the next one. 50167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner EmitGlobalDefinition(D); 50267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 5035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 5045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 5068bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 5071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 5081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// GlobalValue being annotated. The second field is the constant string 5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// created from the AnnotateAttr's annotation. The third field is a constant 5108bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 5118bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 5128bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 5138bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 5148bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 5158bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 5161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 5178bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 5188bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 5198bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 5208bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 5218bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 5228bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 5233c0ef8cc0dc246bd3083e8cdd63005e8873d36d2Benjamin Kramer const llvm::Type *SBP = llvm::Type::getInt8PtrTy(VMContext); 5241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *anno = llvm::ConstantArray::get(VMContext, 5250032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson AA->getAnnotation(), true); 5260032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *unit = llvm::ConstantArray::get(VMContext, 5270032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson M->getModuleIdentifier(), 5288bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 5298bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 5308bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 5318bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 5321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue *annoGV = 53395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner new llvm::GlobalVariable(*M, anno->getType(), false, 53495b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::PrivateLinkage, anno, 53595b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner GV->getName()); 5368bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 5378bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 53895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner new llvm::GlobalVariable(*M, unit->getType(), false, 5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue::PrivateLinkage, unit, 54095b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner ".str"); 5418bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 54257d5cee133495bc21d1abdbce45ab05a79274a23Daniel Dunbar // Create the ConstantStruct for the global annotation. 5438bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 5443c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(GV, SBP), 5453c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(annoGV, SBP), 5463c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(unitGV, SBP), 5470032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo) 5488bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 54947a434ff3d49e7906eda88e8e8242e4297725b32Owen Anderson return llvm::ConstantStruct::get(VMContext, Fields, 4, false); 5508bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 5518bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 55273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 5535c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 5545c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 55540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (Features.EmitAllDecls || Global->hasAttr<UsedAttr>()) 55673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 557bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 558bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 55973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 5601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (FD->hasAttr<ConstructorAttr>() || 56140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis FD->hasAttr<DestructorAttr>()) 56273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 56373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 56461eab8872168af6eb1e0047a82901096cf145e27Eli Friedman // The key function for a class must never be deferred. 56561eab8872168af6eb1e0047a82901096cf145e27Eli Friedman if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Global)) { 56661eab8872168af6eb1e0047a82901096cf145e27Eli Friedman const CXXRecordDecl *RD = MD->getParent(); 56761eab8872168af6eb1e0047a82901096cf145e27Eli Friedman if (MD->isOutOfLine() && RD->isDynamicClass()) { 56861eab8872168af6eb1e0047a82901096cf145e27Eli Friedman const CXXMethodDecl *KeyFunction = getContext().getKeyFunction(RD); 569bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor if (KeyFunction && 570bd6d6197fcfc98356ea60e816365eb0648b69556Douglas Gregor KeyFunction->getCanonicalDecl() == MD->getCanonicalDecl()) 57161eab8872168af6eb1e0047a82901096cf145e27Eli Friedman return false; 57261eab8872168af6eb1e0047a82901096cf145e27Eli Friedman } 57361eab8872168af6eb1e0047a82901096cf145e27Eli Friedman } 57461eab8872168af6eb1e0047a82901096cf145e27Eli Friedman 57568584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor GVALinkage Linkage = GetLinkageForFunction(getContext(), FD, Features); 5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 577dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner // static, static inline, always_inline, and extern inline functions can 57886daeee2d4aa6523679f07f27a826bf4c42ca95dChris Lattner // always be deferred. Normal inline functions can be deferred in C99/C++. 579cbb8fc18d86a886856f5b852a6a3ead71fec17f9Chris Lattner if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || 580ab189950908fc08dd2533692f0e8253e807c73acEli Friedman Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) 581dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner return true; 582dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner return false; 58373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 585dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner const VarDecl *VD = cast<VarDecl>(Global); 586dbb5a376c8b9272813a30c5519031e9ea2fb071fChris Lattner assert(VD->isFileVarDecl() && "Invalid decl"); 587b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor 58874d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson // We never want to defer structs that have non-trivial constructors or 58974d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson // destructors. 59074d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson 59174d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson // FIXME: Handle references. 59274d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { 59374d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { 59474d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson if (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor()) 59574d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson return false; 59674d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson } 59774d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson } 59874d644abe56809d9bcea7311f37aa9063ab9e064Anders Carlsson 5991028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // Static data may be deferred, but out-of-line static data members 6001028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // cannot be. 6010b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor Linkage L = VD->getLinkage(); 6020b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor if (L == ExternalLinkage && getContext().getLangOptions().CPlusPlus && 6030b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor VD->getType()->getLinkage() == UniqueExternalLinkage) 6040b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor L = UniqueExternalLinkage; 6050b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 6060b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor switch (L) { 6070b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case NoLinkage: 6080b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case InternalLinkage: 6090b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case UniqueExternalLinkage: 610393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian // Initializer has side effects? 611393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian if (VD->getInit() && VD->getInit()->HasSideEffects(Context)) 612393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian return false; 613393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian return !(VD->isStaticDataMember() && VD->isOutOfLine()); 6140b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 6150b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case ExternalLinkage: 6160b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor break; 617393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian } 6180b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 619393c247fe025ccb5f914e37e948192ea86faef8cFariborz Jahanian return false; 62073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 62173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 622b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobal(GlobalDecl GD) { 6234a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson const ValueDecl *Global = cast<ValueDecl>(GD.getDecl()); 6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 625bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If this is an alias definition (which otherwise looks like a declaration) 626bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // emit it now. 62740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (Global->hasAttr<AliasAttr>()) 628bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return EmitAliasDefinition(Global); 629219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 63067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Ignore declarations, they will be emitted on their first use. 6315e1e1f95c98b1add70c238093bbd5dc8d4f9c4e9Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 63273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 63373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 63473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 6350269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 6360269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 637bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 638bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 639a9a55c063e9e59c6ab0a6d7a21302660f7bde9f9Douglas Gregor if (VD->isThisDeclarationADefinition() != VarDecl::Definition) 64073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 6414c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 6424c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 64367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Defer code generation when possible if this is a static definition, inline 64467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // function etc. These we only want to emit if they are used. 64573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (MayDeferGeneration(Global)) { 64667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // If the value has already been used, add it directly to the 64767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit list. 6482a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson const char *MangledName = getMangledName(GD); 64967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (GlobalDeclMap.count(MangledName)) 6502a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson DeferredDeclsToEmit.push_back(GD); 65167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner else { 65267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Otherwise, remember that we saw a deferred decl with this name. The 65367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // first use of the mangled name will cause it to move into 65467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // DeferredDeclsToEmit. 6552a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson DeferredDecls[MangledName] = GD; 65667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 657bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 658bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 659bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 660bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 6612a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson EmitGlobalDefinition(GD); 6624c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 6634c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 664b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalDefinition(GlobalDecl GD) { 6654a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson const ValueDecl *D = cast<ValueDecl>(GD.getDecl()); 6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6678e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson PrettyStackTraceDecl CrashInfo((ValueDecl *)D, D->getLocation(), 6688e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson Context.getSourceManager(), 6698e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson "Generating code for declaration"); 6708e2efcc267ed12dc435782288b7f9a4a1bc56c72Anders Carlsson 67172649ed78a25365d003b5218cae7b332c418800eEli Friedman if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 6721a5e0d7f18485e4fb958f96dcddff3e4486a4069Anders Carlsson getVtableInfo().MaybeEmitVtable(GD); 67372649ed78a25365d003b5218cae7b332c418800eEli Friedman if (MD->isVirtual() && MD->isOutOfLine() && 67472649ed78a25365d003b5218cae7b332c418800eEli Friedman (!isa<CXXDestructorDecl>(D) || GD.getDtorType() != Dtor_Base)) { 675b455f0e74be0144ab9738ef574d07bd661959525Eli Friedman if (isa<CXXDestructorDecl>(D)) { 676b455f0e74be0144ab9738ef574d07bd661959525Eli Friedman GlobalDecl CanonGD(cast<CXXDestructorDecl>(D->getCanonicalDecl()), 677b455f0e74be0144ab9738ef574d07bd661959525Eli Friedman GD.getDtorType()); 678b455f0e74be0144ab9738ef574d07bd661959525Eli Friedman BuildThunksForVirtual(CanonGD); 679b455f0e74be0144ab9738ef574d07bd661959525Eli Friedman } else { 680b455f0e74be0144ab9738ef574d07bd661959525Eli Friedman BuildThunksForVirtual(MD->getCanonicalDecl()); 681b455f0e74be0144ab9738ef574d07bd661959525Eli Friedman } 68272649ed78a25365d003b5218cae7b332c418800eEli Friedman } 68372649ed78a25365d003b5218cae7b332c418800eEli Friedman } 6841a5e0d7f18485e4fb958f96dcddff3e4486a4069Anders Carlsson 6852a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D)) 6862a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson EmitCXXConstructor(CD, GD.getCtorType()); 6877267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) 6887267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson EmitCXXDestructor(DD, GD.getDtorType()); 689b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner else if (isa<FunctionDecl>(D)) 690b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner EmitGlobalFunctionDefinition(GD); 6912a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) 692bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 6932a131fbca2a51085dc083b8c56a2d4ced3cf1413Anders Carlsson else { 694bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 695bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 696bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 697bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 69874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMFunction - If the specified mangled name is not in the 69974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// module, create and return an llvm Function with the specified type. If there 70074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 70174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 70274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 70374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 70474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the function when it is first created. 70574391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName, 70674391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty, 707b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner GlobalDecl D) { 7080558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Lookup the entry, lazily creating it if necessary. 7090558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 7100558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry) { 7110558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (Entry->getType()->getElementType() == Ty) 7120558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return Entry; 7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7140558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Make sure the result is of the correct type. 71596e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson const llvm::Type *PTy = llvm::PointerType::getUnqual(Ty); 7163c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(Entry, PTy); 7170558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 719654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman // This function doesn't have a complete type (for example, the return 720654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman // type is an incomplete struct). Use a fake type instead, and make 721654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman // sure not to try to set attributes. 722654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman bool IsIncompleteFunction = false; 723654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman if (!isa<llvm::FunctionType>(Ty)) { 724654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman Ty = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 725654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman std::vector<const llvm::Type*>(), false); 726654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman IsIncompleteFunction = true; 727654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman } 728654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 729654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman llvm::Function::ExternalLinkage, 730654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman "", &getModule()); 731654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman F->setName(MangledName); 732654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman if (D.getDecl()) 733b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson SetFunctionAttributes(D, F, IsIncompleteFunction); 734654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman Entry = F; 735654ad40f27d684e8f3eddbc990247a6dbea5ddedEli Friedman 73667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 73767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 73867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 7391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::DenseMap<const char*, GlobalDecl>::iterator DDI = 7409fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner DeferredDecls.find(MangledName); 74167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 74267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 74367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 74467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 74567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 746b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner } else if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D.getDecl())) { 7470c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // If this the first reference to a C++ inline function in a class, queue up 7480c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // the deferred function body for emission. These are not seen as 7490c337ed63ff0f04fd8315afabb2d7a51969fdc97Chris Lattner // top-level declarations. 750b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner if (FD->isThisDeclarationADefinition() && MayDeferGeneration(FD)) 751b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner DeferredDeclsToEmit.push_back(D); 752c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian // A called constructor which has no definition or declaration need be 753c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian // synthesized. 754c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian else if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { 75515233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman if (CD->isImplicit()) 75615233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman DeferredDeclsToEmit.push_back(D); 75715233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) { 75815233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman if (DD->isImplicit()) 75915233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman DeferredDeclsToEmit.push_back(D); 76015233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { 76115233e5a4d98b66b3c6cfcc4e6413ad776a79481Eli Friedman if (MD->isCopyAssignment() && MD->isImplicit()) 762c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian DeferredDeclsToEmit.push_back(D); 763c7ff8e19081c2e974f05f66c4fa9b40750fc655fFariborz Jahanian } 76467b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7660558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner return F; 7670558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner} 7680558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 76974391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfFunction - Return the address of the given function. If Ty is 77074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// non-null, then this function will use the specified type if it has to 77174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create it (this occurs when we see a definition of the function). 772b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, 77374391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 77474391b48b4791cded373683a3baf67314f358d50Chris Lattner // If there was no specific requested type, just convert it now. 77574391b48b4791cded373683a3baf67314f358d50Chris Lattner if (!Ty) 7764a6835e650ff24e19ce08a3bd347c0ad186777fdAnders Carlsson Ty = getTypes().ConvertType(cast<ValueDecl>(GD.getDecl())->getType()); 7774fe95f99a2693f1145785ea5835ba6937e49c730Douglas Gregor return GetOrCreateLLVMFunction(getMangledName(GD), Ty, GD); 77874391b48b4791cded373683a3baf67314f358d50Chris Lattner} 77977ba708819285931932ecd33691a672bb59d221aEli Friedman 78074391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeFunction - Create a new runtime function with the specified 78174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// type and name. 78274391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 78374391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 78474391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 78574391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 786e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar Name = getContext().Idents.get(Name).getNameStart(); 787b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner return GetOrCreateLLVMFunction(Name, FTy, GlobalDecl()); 78874391b48b4791cded373683a3baf67314f358d50Chris Lattner} 789bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 79020e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedmanstatic bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) { 79188786862424cb2d478516d911709dc19c962af9cJohn McCall if (!D->getType().isConstant(Context) && !D->getType()->isReferenceType()) 79220e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman return false; 79320e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman if (Context.getLangOptions().CPlusPlus && 79420e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman Context.getBaseElementType(D->getType())->getAs<RecordType>()) { 79520e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman // FIXME: We should do something fancier here! 79620e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman return false; 79720e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman } 79820e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman return true; 79920e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman} 80020e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman 80174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 80274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// create and return an llvm GlobalVariable with the specified type. If there 80374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// is something in the module with the specified name, return it potentially 80474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// bitcasted to the right type. 80574391b48b4791cded373683a3baf67314f358d50Chris Lattner/// 80674391b48b4791cded373683a3baf67314f358d50Chris Lattner/// If D is non-null, it specifies a decl that correspond to this. This is used 80774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// to set the attributes on the global when it is first created. 80874391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetOrCreateLLVMGlobal(const char *MangledName, 80974391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::PointerType*Ty, 81074391b48b4791cded373683a3baf67314f358d50Chris Lattner const VarDecl *D) { 8113c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 8125d4f5c724533b994de05df49ae259120482ec366Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 81399b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner if (Entry) { 81474391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Entry->getType() == Ty) 815570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner return Entry; 8161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 81799b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Make sure the result is of the correct type. 8183c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson return llvm::ConstantExpr::getBitCast(Entry, Ty); 81999b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner } 8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 82167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // This is the first use or definition of a mangled name. If there is a 82267b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // deferred decl with this name, remember that we need to emit it at the end 82367b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // of the file. 8241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::DenseMap<const char*, GlobalDecl>::iterator DDI = 82567b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.find(MangledName); 82667b00520c8f5b48fad722b790d87fea6be764efeChris Lattner if (DDI != DeferredDecls.end()) { 82767b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 82867b00520c8f5b48fad722b790d87fea6be764efeChris Lattner // list, and remove it from DeferredDecls (since we don't need it anymore). 82967b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDeclsToEmit.push_back(DDI->second); 83067b00520c8f5b48fad722b790d87fea6be764efeChris Lattner DeferredDecls.erase(DDI); 83167b00520c8f5b48fad722b790d87fea6be764efeChris Lattner } 8321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable *GV = 8341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump new llvm::GlobalVariable(getModule(), Ty->getElementType(), false, 83599b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner llvm::GlobalValue::ExternalLinkage, 8361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 0, "", 0, 83756ebe5082da7411fb37479e230b52735f77cff35Eli Friedman false, Ty->getAddressSpace()); 838d972678a053d4785772b75cf6c8d4ab74ac2c7f6Chris Lattner GV->setName(MangledName); 83949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 84099b53613ebe2c59d41030e987962c1ed101b2efeChris Lattner // Handle things which are present even on external declarations. 84174391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D) { 842f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: This code is overly simple and should be merged with other global 843f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // handling. 84420e098b7e7fda6bed1d67441b56cce77cd3aa918Eli Friedman GV->setConstant(DeclIsConstantGlobal(Context, D)); 84549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 84674391b48b4791cded373683a3baf67314f358d50Chris Lattner // FIXME: Merge with other attribute handling code. 84774391b48b4791cded373683a3baf67314f358d50Chris Lattner if (D->getStorageClass() == VarDecl::PrivateExtern) 84804d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 84949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (D->hasAttr<WeakAttr>() || 85140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis D->hasAttr<WeakImportAttr>()) 85274391b48b4791cded373683a3baf67314f358d50Chris Lattner GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 85356ebe5082da7411fb37479e230b52735f77cff35Eli Friedman 85456ebe5082da7411fb37479e230b52735f77cff35Eli Friedman GV->setThreadLocal(D->isThreadSpecified()); 85574391b48b4791cded373683a3baf67314f358d50Chris Lattner } 8561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 85774391b48b4791cded373683a3baf67314f358d50Chris Lattner return Entry = GV; 85874391b48b4791cded373683a3baf67314f358d50Chris Lattner} 859eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 860eda9a5ec380f172f4e0063744eb796144a125480Daniel Dunbar 86174391b48b4791cded373683a3baf67314f358d50Chris Lattner/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 86274391b48b4791cded373683a3baf67314f358d50Chris Lattner/// given global variable. If Ty is non-null and if the global doesn't exist, 86374391b48b4791cded373683a3baf67314f358d50Chris Lattner/// then it will be greated with the specified type instead of whatever the 86474391b48b4791cded373683a3baf67314f358d50Chris Lattner/// normal requested type would be. 86574391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 86674391b48b4791cded373683a3baf67314f358d50Chris Lattner const llvm::Type *Ty) { 86774391b48b4791cded373683a3baf67314f358d50Chris Lattner assert(D->hasGlobalStorage() && "Not a global variable"); 86874391b48b4791cded373683a3baf67314f358d50Chris Lattner QualType ASTTy = D->getType(); 86974391b48b4791cded373683a3baf67314f358d50Chris Lattner if (Ty == 0) 87074391b48b4791cded373683a3baf67314f358d50Chris Lattner Ty = getTypes().ConvertTypeForMem(ASTTy); 8711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::PointerType *PTy = 87396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 87474391b48b4791cded373683a3baf67314f358d50Chris Lattner return GetOrCreateLLVMGlobal(getMangledName(D), PTy, D); 87574391b48b4791cded373683a3baf67314f358d50Chris Lattner} 8763f75c43bd77e063342bc888ac276daf64ba0ce07Daniel Dunbar 87774391b48b4791cded373683a3baf67314f358d50Chris Lattner/// CreateRuntimeVariable - Create a new runtime global variable with the 87874391b48b4791cded373683a3baf67314f358d50Chris Lattner/// specified type and name. 87974391b48b4791cded373683a3baf67314f358d50Chris Lattnerllvm::Constant * 88074391b48b4791cded373683a3baf67314f358d50Chris LattnerCodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, 88174391b48b4791cded373683a3baf67314f358d50Chris Lattner const char *Name) { 88274391b48b4791cded373683a3baf67314f358d50Chris Lattner // Convert Name to be a uniqued string from the IdentifierInfo table. 883e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar Name = getContext().Idents.get(Name).getNameStart(); 88496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); 885bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 886bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 88703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbarvoid CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { 88803f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar assert(!D->getInit() && "Cannot emit definite definitions here!"); 88903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar 8907520bd1de12af10ea08c662440565adbdf589317Douglas Gregor if (MayDeferGeneration(D)) { 8917520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // If we have not seen a reference to this variable yet, place it 8927520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // into the deferred declarations table to be emitted if needed 8937520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // later. 8947520bd1de12af10ea08c662440565adbdf589317Douglas Gregor const char *MangledName = getMangledName(D); 8957520bd1de12af10ea08c662440565adbdf589317Douglas Gregor if (GlobalDeclMap.count(MangledName) == 0) { 896555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson DeferredDecls[MangledName] = D; 89703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar return; 8987520bd1de12af10ea08c662440565adbdf589317Douglas Gregor } 8997520bd1de12af10ea08c662440565adbdf589317Douglas Gregor } 9007520bd1de12af10ea08c662440565adbdf589317Douglas Gregor 9017520bd1de12af10ea08c662440565adbdf589317Douglas Gregor // The tentative definition is the only definition. 90203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar EmitGlobalVarDefinition(D); 90303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar} 90403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar 9054b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregorllvm::GlobalVariable::LinkageTypes 9064b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas GregorCodeGenModule::getVtableLinkage(const CXXRecordDecl *RD) { 907dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) 908dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor return llvm::GlobalVariable::InternalLinkage; 909dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor 910dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor if (const CXXMethodDecl *KeyFunction 911dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor = RD->getASTContext().getKeyFunction(RD)) { 912dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // If this class has a key function, use that to determine the linkage of 913dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // the vtable. 9144b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor const FunctionDecl *Def = 0; 9154b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor if (KeyFunction->getBody(Def)) 9164b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor KeyFunction = cast<CXXMethodDecl>(Def); 917dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor 9184b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor switch (KeyFunction->getTemplateSpecializationKind()) { 9194b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_Undeclared: 9204b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_ExplicitSpecialization: 9214b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor if (KeyFunction->isInlined()) 9224b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 9234b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 9244b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::ExternalLinkage; 9254b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 9264b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_ImplicitInstantiation: 9274b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_ExplicitInstantiationDefinition: 9284b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 9294b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 9304b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor case TSK_ExplicitInstantiationDeclaration: 9314b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor // FIXME: Use available_externally linkage. However, this currently 9324b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor // breaks LLVM's build due to undefined symbols. 9334b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor // return llvm::GlobalVariable::AvailableExternallyLinkage; 9344b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 9354b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor } 936dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor } 937dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor 938dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor switch (RD->getTemplateSpecializationKind()) { 939dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_Undeclared: 940dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_ExplicitSpecialization: 941dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_ImplicitInstantiation: 942dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_ExplicitInstantiationDefinition: 943dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 944dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor 945dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor case TSK_ExplicitInstantiationDeclaration: 946dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // FIXME: Use available_externally linkage. However, this currently 947dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // breaks LLVM's build due to undefined symbols. 948dffb8010a130733f1b55acf0af01deb0a2e083d3Douglas Gregor // return llvm::GlobalVariable::AvailableExternallyLinkage; 9494b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 9504b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor } 9514b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 9524b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor // Silence GCC warning. 9534b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor return llvm::GlobalVariable::WeakODRLinkage; 9544b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor} 9554b0f21c0f8bed0e2a7dc62d73be64e7e277d6c9aDouglas Gregor 9561028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregorstatic CodeGenModule::GVALinkage 9571028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas GregorGetLinkageForVariable(ASTContext &Context, const VarDecl *VD) { 9580b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor // If this is a static data member, compute the kind of template 9590b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor // specialization. Otherwise, this variable is not part of a 9600b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor // template. 9610b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor TemplateSpecializationKind TSK = TSK_Undeclared; 9620b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor if (VD->isStaticDataMember()) 9630b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor TSK = VD->getTemplateSpecializationKind(); 9640b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 9650b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor Linkage L = VD->getLinkage(); 9660b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor if (L == ExternalLinkage && Context.getLangOptions().CPlusPlus && 9670b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor VD->getType()->getLinkage() == UniqueExternalLinkage) 9680b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor L = UniqueExternalLinkage; 9690b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 9700b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor switch (L) { 9710b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case NoLinkage: 9720b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case InternalLinkage: 9730b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case UniqueExternalLinkage: 9741028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return CodeGenModule::GVA_Internal; 9751028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 9760b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor case ExternalLinkage: 9770b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor switch (TSK) { 9781028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_Undeclared: 9791028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_ExplicitSpecialization: 9800b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor 9810b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor // FIXME: ExplicitInstantiationDefinition should be weak! 9821028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_ExplicitInstantiationDefinition: 9831028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return CodeGenModule::GVA_StrongExternal; 9841028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 9851028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_ExplicitInstantiationDeclaration: 9869f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin llvm_unreachable("Variable should not be instantiated"); 9871028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor // Fall through to treat this like any other instantiation. 9881028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 9891028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor case TSK_ImplicitInstantiation: 9900b6bc8bd7a1d2a7d7478d13d78cff94cacad61fcDouglas Gregor return CodeGenModule::GVA_TemplateInstantiation; 9911028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor } 9921028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor } 993bd94ab9a19cb4bed55ecae9558533da9606bedcfEli Friedman 9941028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor return CodeGenModule::GVA_StrongExternal; 9951028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor} 9961028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor 997687cc4a850b59116efee061018f0d8df50728b82Ken DyckCharUnits CodeGenModule::GetTargetTypeStoreSize(const llvm::Type *Ty) const { 998687cc4a850b59116efee061018f0d8df50728b82Ken Dyck return CharUnits::fromQuantity( 999687cc4a850b59116efee061018f0d8df50728b82Ken Dyck TheTargetData.getTypeStoreSizeInBits(Ty) / Context.getCharWidth()); 1000687cc4a850b59116efee061018f0d8df50728b82Ken Dyck} 1001687cc4a850b59116efee061018f0d8df50728b82Ken Dyck 1002bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 10038f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 100477ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 10056c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman bool NonConstInit = false; 10061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 100731310a21fb2a9f13950f864f681c86080b05d5b2Sebastian Redl const Expr *InitExpr = D->getAnyInitializer(); 10083bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson 10093bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson if (!InitExpr) { 1010cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 101103f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // implicitly initialized with { 0 }. 101203f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // 101303f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // Note that tentative definitions are only emitted at the end of 101403f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // a translation unit, so they should never have incomplete 101503f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // type. In addition, EmitTentativeDefinition makes sure that we 101603f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // never attempt to emit a tentative definition if a real one 101703f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // exists. A use may still exists, however, so we still may need 101803f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar // to do a RAUW. 101903f5ad9a7707e098f601921fcec17ed65eb355a7Daniel Dunbar assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); 1020b0d0ea042116c1f451d3db8ceff9f1dd92bc36d2Anders Carlsson Init = EmitNullConstant(D->getType()); 102177ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 10223bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson Init = EmitConstantExpr(InitExpr, D->getType()); 10231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10246e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman if (!Init) { 10253bb92693c3332c1e99870a4e45afff3892e1c6aeAnders Carlsson QualType T = InitExpr->getType(); 102689ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson if (getLangOptions().CPlusPlus) { 10276c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman EmitCXXGlobalVarDeclInitFunc(D); 102889ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson Init = EmitNullConstant(T); 10296c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman NonConstInit = true; 103089ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson } else { 103189ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson ErrorUnsupported(D, "static initializer"); 103289ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 103389ed31d3f9eeb8ec77c284a5cf404a74bf5e7acfAnders Carlsson } 10346e656f45ae04b415ba7a4c0c25e55633e2d0ecd0Eli Friedman } 10358f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 10368e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 10372d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner const llvm::Type* InitType = Init->getType(); 1038570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::Constant *Entry = GetAddrOfGlobalVar(D, InitType); 10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1040570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Strip off a bitcast if we got one back. 1041570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 10429d4a15fd3b85434c43ea27562793de63a793321aChris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast || 10439d4a15fd3b85434c43ea27562793de63a793321aChris Lattner // all zero index gep. 10449d4a15fd3b85434c43ea27562793de63a793321aChris Lattner CE->getOpcode() == llvm::Instruction::GetElementPtr); 1045570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry = CE->getOperand(0); 1046570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner } 10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1048570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Entry is now either a Function or GlobalVariable. 1049570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Entry); 10501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1051570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We have a definition after a declaration with the wrong type. 1052570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // We must make a new GlobalVariable* and update everything that used OldGV 1053570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (a declaration or tentative definition) with the new GlobalVariable* 1054570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // (which will be a definition). 1055570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // 1056570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // This happens if there is a prototype for a global (e.g. 1057570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "extern int x[];") and then a definition of a different type (e.g. 1058570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // "int x[10];"). This also happens when an initializer has a different type 1059570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // from the type of the global (this happens with unions). 1060570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner if (GV == 0 || 1061570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getElementType() != InitType || 1062570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV->getType()->getAddressSpace() != ASTTy.getAddressSpace()) { 10631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1064570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Remove the old entry from GlobalDeclMap so that we'll create a new one. 1065570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GlobalDeclMap.erase(getMangledName(D)); 1066232350d4faf46ec38d5ff60e11505f9c4fa9535bDaniel Dunbar 1067570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner // Make a new global with the correct type, this is now guaranteed to work. 1068570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner GV = cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, InitType)); 10690558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner GV->takeName(cast<llvm::GlobalValue>(Entry)); 10700558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner 107177ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 10721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *NewPtrForOldDecl = 10733c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 1074570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 107577ba708819285931932ecd33691a672bb59d221aEli Friedman 107677ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 1077570585c91dee98d7ba8ccf1198c03208ba17966bChris Lattner cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 107877ba708819285931932ecd33691a672bb59d221aEli Friedman } 107977ba708819285931932ecd33691a672bb59d221aEli Friedman 108040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 10818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 10828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 1083f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 10848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 10858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 108688a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 1087e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner 1088e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // If it is safe to mark the global 'constant', do so now. 1089e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(false); 10906c6bda3b0b1d8adaac2ba3f4da7056e9f1eef52eEli Friedman if (!NonConstInit && DeclIsConstantGlobal(Context, D)) 1091e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(true); 10921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10938b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck GV->setAlignment(getContext().getDeclAlign(D).getQuantity()); 109408d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 109588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 10961028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor GVALinkage Linkage = GetLinkageForVariable(getContext(), D); 1097a0f00a71fcb0f98298709d5e5318339acf7958acDouglas Gregor if (Linkage == GVA_Internal) 10988fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 109940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis else if (D->hasAttr<DLLImportAttr>()) 1100ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 110140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis else if (D->hasAttr<DLLExportAttr>()) 1102ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 1103e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner else if (D->hasAttr<WeakAttr>()) { 1104e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner if (GV->isConstant()) 1105e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakODRLinkage); 1106e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner else 1107e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 11081028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor } else if (Linkage == GVA_TemplateInstantiation) 11091028c9f0afc1cc5f4951b39b7067fa57c1fea07bDouglas Gregor GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 11101bc5c03a7f08b04f4ac26abd705418aaf4109062Daniel Dunbar else if (!getLangOptions().CPlusPlus && !CodeGenOpts.NoCommon && 1111309457d0f1b416c1b379c9f3e172848adffedb23Chris Lattner !D->hasExternalStorage() && !D->getInit() && 1112e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner !D->getAttr<SectionAttr>()) { 111304d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 1114e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner // common vars aren't constant even if declared const. 1115e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner GV->setConstant(false); 1116e78b86f3201ae5f09e2cf85bd6558f1d9b34de26Chris Lattner } else 111704d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 11187e714cd931fa3a90bfd728318a92485aa3e95748Daniel Dunbar 11197c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GV); 112004d4078425614bf9fd58d606335c1f5f74ee7fa4Daniel Dunbar 1121686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 11222d58406872e5af0c924623d9f7c194c4f09936d3Chris Lattner if (CGDebugInfo *DI = getDebugInfo()) { 112366031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 1124686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 1125686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 112688a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 11275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1128bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we 1129bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// implement a function with no prototype, e.g. "int foo() {}". If there are 1130bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// existing call uses of the old function in the module, this adjusts them to 1131bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// call the new function directly. 1132bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// 1133bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// This is not just a cleanup: the always_inline pass requires direct calls to 1134bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// functions to be able to inline them. If there is a bitcast in the way, it 1135bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// won't inline them. Instcombine normally deletes these calls, but it isn't 1136bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner/// run at -O0. 1137bdb0132722082886558f31eccdba06ae1852c0eeChris Lattnerstatic void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 1138bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::Function *NewFn) { 1139bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If we're redefining a global as a function, don't transform it. 1140bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::Function *OldFn = dyn_cast<llvm::Function>(Old); 1141bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (OldFn == 0) return; 11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1143bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner const llvm::Type *NewRetTy = NewFn->getReturnType(); 1144bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::SmallVector<llvm::Value*, 4> ArgList; 1145bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1146bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner for (llvm::Value::use_iterator UI = OldFn->use_begin(), E = OldFn->use_end(); 1147bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner UI != E; ) { 1148bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // TODO: Do invokes ever occur in C code? If so, we should handle them too. 114908c93a7f2210b464e5abe298a5474b99414615f6Chris Lattner unsigned OpNo = UI.getOperandNo(); 1150bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::CallInst *CI = dyn_cast<llvm::CallInst>(*UI++); 115108c93a7f2210b464e5abe298a5474b99414615f6Chris Lattner if (!CI || OpNo != 0) continue; 11521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1153bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If the return types don't match exactly, and if the call isn't dead, then 1154bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // we can't transform this call. 1155bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (CI->getType() != NewRetTy && !CI->use_empty()) 1156bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner continue; 1157bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1158bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If the function was passed too few arguments, don't transform. If extra 1159bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // arguments were passed, we silently drop them. If any of the types 1160bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // mismatch, we don't transform. 1161bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner unsigned ArgNo = 0; 1162bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner bool DontTransform = false; 1163bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner for (llvm::Function::arg_iterator AI = NewFn->arg_begin(), 1164bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner E = NewFn->arg_end(); AI != E; ++AI, ++ArgNo) { 1165bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (CI->getNumOperands()-1 == ArgNo || 1166bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner CI->getOperand(ArgNo+1)->getType() != AI->getType()) { 1167bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner DontTransform = true; 1168bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner break; 1169bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1170bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1171bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (DontTransform) 1172bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner continue; 11731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1174bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // Okay, we can transform this. Create the new call instruction and copy 1175bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // over the required information. 1176bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.append(CI->op_begin()+1, CI->op_begin()+1+ArgNo); 1177bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::CallInst *NewCall = llvm::CallInst::Create(NewFn, ArgList.begin(), 1178bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.end(), "", CI); 1179bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ArgList.clear(); 1180ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer if (!NewCall->getType()->isVoidTy()) 1181bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewCall->takeName(CI); 1182bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewCall->setAttributes(CI->getAttributes()); 1183ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar NewCall->setCallingConv(CI->getCallingConv()); 1184bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1185bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // Finally, remove the old call, replacing any uses with the new one. 118600549fcec0490b2daf27543e532f94adbb186063Chris Lattner if (!CI->use_empty()) 118700549fcec0490b2daf27543e532f94adbb186063Chris Lattner CI->replaceAllUsesWith(NewCall); 11883b122bc5f1203615e2128e0c1a63da438865b1ccDevang Patel 1189aa40454a14837c573f323e29269cc0b53f495a13Devang Patel // Copy any custom metadata attached with CI. 11901280708c17d373762ea4110b4f989d6c7239f6fbChris Lattner if (llvm::MDNode *DbgNode = CI->getMetadata("dbg")) 11911280708c17d373762ea4110b4f989d6c7239f6fbChris Lattner NewCall->setMetadata("dbg", DbgNode); 1192bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner CI->eraseFromParent(); 1193bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 1194bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner} 1195bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner 1196bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1197b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattnervoid CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD) { 1198b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner const FunctionDecl *D = cast<FunctionDecl>(GD.getDecl()); 1199c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall const llvm::FunctionType *Ty = getTypes().GetFunctionType(GD); 1200d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 12019fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner // Get or create the prototype for the function. 1202b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner llvm::Constant *Entry = GetAddrOfFunction(GD, Ty); 12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12040558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner // Strip off a bitcast if we got one back. 12050558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) { 12060558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner assert(CE->getOpcode() == llvm::Instruction::BitCast); 12070558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = CE->getOperand(0); 12080558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner } 12091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12110558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() != Ty) { 1212bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner llvm::GlobalValue *OldFn = cast<llvm::GlobalValue>(Entry); 12131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 121442745815fa4e90bfb07e581d2e5152b2c2db08ffDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 1215bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner assert(OldFn->isDeclaration() && 12160558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner "Shouldn't replace non-declaration"); 121734809507232bc4c3c4840c7d092c7440219fddafChris Lattner 121862b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // F is the Function* for the one with the wrong type, we must make a new 121962b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* and update everything that used F (a declaration) with the new 122062b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Function* (which will be a definition). 122162b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // 122262b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // This happens if there is a prototype for a function 122362b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f()") and then a definition of a different type 122462b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // (e.g. "int f(int x)"). Start by making a new function of the 122562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // correct type, RAUW, then steal the name. 122634809507232bc4c3c4840c7d092c7440219fddafChris Lattner GlobalDeclMap.erase(getMangledName(D)); 1227b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner llvm::Function *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty)); 1228bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner NewFn->takeName(OldFn); 12291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1230bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // If this is an implementation of a function without a prototype, try to 1231bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // replace any existing uses of the function (which may be calls) with uses 1232bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner // of the new function 12339fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner if (D->getType()->isFunctionNoProtoType()) { 1234bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner ReplaceUsesOfNonProtoTypeWithRealFunction(OldFn, NewFn); 12359fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner OldFn->removeDeadConstantUsers(); 12369fa959d5bfbbb17d7c6ba71252219201fc8dc971Chris Lattner } 12371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 123862b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Replace uses of F with the Function we will endow with a body. 1239bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner if (!Entry->use_empty()) { 12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *NewPtrForOldDecl = 12413c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 1242bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner Entry->replaceAllUsesWith(NewPtrForOldDecl); 1243bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner } 12441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 124562b33ea51adbd0e7f2f05983e9e4a3a2b2ed26deChris Lattner // Ok, delete the old function now, which is dead. 1246bdb0132722082886558f31eccdba06ae1852c0eeChris Lattner OldFn->eraseFromParent(); 12471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12480558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner Entry = NewFn; 1249bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 12501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12510558e79840bfdbbd38c6e2b4f6765bf0158e85f4Chris Lattner llvm::Function *Fn = cast<llvm::Function>(Entry); 1252bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1253219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 12546379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 12557c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetFunctionDefinitionAttributes(D, Fn); 12567c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetLLVMFunctionAttributesForDefinition(D, Fn); 12571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 125840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 1259219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 126040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 1261219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 1262bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 1263bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 1264bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattnervoid CodeGenModule::EmitAliasDefinition(const ValueDecl *D) { 126540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis const AliasAttr *AA = D->getAttr<AliasAttr>(); 1266bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner assert(AA && "Not an alias?"); 1267bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1268bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 12691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1270bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Unique the name through the identifier table. 12713d2c43e9a7ca55f5ddc1f0c77d8f5e5ea7c1b573Ted Kremenek const char *AliaseeName = 12723d2c43e9a7ca55f5ddc1f0c77d8f5e5ea7c1b573Ted Kremenek getContext().Idents.get(AA->getAliasee()).getNameStart(); 1273bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1274bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create a reference to the named value. This ensures that it is emitted 1275bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // if a deferred decl. 1276bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Constant *Aliasee; 1277bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (isa<llvm::FunctionType>(DeclTy)) 1278b4880bab7fc1b61267cfd9a0ad52188e7a828cb3Chris Lattner Aliasee = GetOrCreateLLVMFunction(AliaseeName, DeclTy, GlobalDecl()); 1279bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner else 1280bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Aliasee = GetOrCreateLLVMGlobal(AliaseeName, 128196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson llvm::PointerType::getUnqual(DeclTy), 0); 1282bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1283bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Create the new alias itself, but don't set a name yet. 12841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalValue *GA = 1285bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner new llvm::GlobalAlias(Aliasee->getType(), 1286bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::Function::ExternalLinkage, 1287bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner "", Aliasee, &getModule()); 12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1289bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // See if there is already something with the alias' name in the module. 1290bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner const char *MangledName = getMangledName(D); 1291bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 12921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1293bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry && !Entry->isDeclaration()) { 1294bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a definition in the module, then it wins over the alias. 1295bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // This is dubious, but allow it to be safe. Just ignore the alias. 1296bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->eraseFromParent(); 1297bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner return; 1298bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1300bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner if (Entry) { 1301bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // If there is a declaration in the module, then we had an extern followed 1302bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // by the alias, as in: 1303bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // extern int test6(); 1304bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // ... 1305bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // int test6() __attribute__((alias("test7"))); 1306bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // 1307bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Remove it and replace uses of it with the alias. 13081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13093c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 1310bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->getType())); 1311bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry->eraseFromParent(); 1312bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner } 13131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1314bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner // Now we know that there is no conflict, set the name. 1315bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner Entry = GA; 1316bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner GA->setName(MangledName); 1317bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 13187c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // Set attributes which are particular to an alias; this is a 13197c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // specialization of the attributes which may be set on a global 13207c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // variable/function. 132140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (D->hasAttr<DLLExportAttr>()) { 13227c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 13237c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar // The dllexport attribute is ignored for undefined symbols. 13246fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (FD->getBody()) 13257c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::DLLExportLinkage); 13267c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } else { 13277c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::DLLExportLinkage); 13287c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 13291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else if (D->hasAttr<WeakAttr>() || 133040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis D->hasAttr<WeakImportAttr>()) { 13317c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar GA->setLinkage(llvm::Function::WeakAnyLinkage); 13327c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar } 13337c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar 13347c65e990e9f0dafaf9adbc59b766dcc23eaee845Daniel Dunbar SetCommonAttributes(D, GA); 1335bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner} 1336bd53271dc7570b54f7b7cab7b09bcf04c6e927f6Chris Lattner 1337b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// getBuiltinLibFunction - Given a builtin id for a function like 1338b808c952bbff821dce727dd801a1098d64394f98Chris Lattner/// "__builtin_fabsf", return a Function* for "fabsf". 133934771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbarllvm::Value *CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD, 134034771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbar unsigned BuiltinID) { 13413e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor assert((Context.BuiltinInfo.isLibFunction(BuiltinID) || 13421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) && 13433e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor "isn't a lib fn"); 13441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13453e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor // Get the name, skip over the __builtin_ prefix (if necessary). 13463e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const char *Name = Context.BuiltinInfo.GetName(BuiltinID); 13473e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (Context.BuiltinInfo.isLibFunction(BuiltinID)) 13483e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Name += 10; 13491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::FunctionType *Ty = 1351386ca78d0b03b1fb519e60d1a14cd12a220364a6Eli Friedman cast<llvm::FunctionType>(getTypes().ConvertType(FD->getType())); 1352bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 1353b808c952bbff821dce727dd801a1098d64394f98Chris Lattner // Unique the name through the identifier table. 1354e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar Name = getContext().Idents.get(Name).getNameStart(); 135534771b594ca8cdf8cd2e40b27170efa4ed2833c5Daniel Dunbar return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl(FD)); 1356bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 1357bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 13587acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 13597acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 13607acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 13617acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 13627acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 1363bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 13645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 13655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 13660032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext); 13674e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 13685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 1369c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 13700c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 13710c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 13720032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext); 13734e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 13740c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 13750c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 137641ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 137741ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 13780032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson const llvm::Type *IntPtr = TheTargetData.getIntPtrType(VMContext); 13794e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 138041ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 13817acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 13821d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarstatic llvm::StringMapEntry<llvm::Constant*> & 13831d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarGetConstantCFStringEntry(llvm::StringMap<llvm::Constant*> &Map, 13841d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar const StringLiteral *Literal, 138570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar bool TargetIsLSB, 13861d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar bool &IsUTF16, 13871d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar unsigned &StringLength) { 1388df4eee9b035cc4b95b1ea72635e7429d06b0ecc8Daniel Dunbar unsigned NumBytes = Literal->getByteLength(); 13891d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 1390f015b034159d40e7033309e50036804eb1971787Daniel Dunbar // Check for simple case. 1391f015b034159d40e7033309e50036804eb1971787Daniel Dunbar if (!Literal->containsNonAsciiOrNull()) { 1392f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength = NumBytes; 1393f015b034159d40e7033309e50036804eb1971787Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 1394f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength)); 1395f015b034159d40e7033309e50036804eb1971787Daniel Dunbar } 1396f015b034159d40e7033309e50036804eb1971787Daniel Dunbar 13971d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // Otherwise, convert the UTF8 literals into a byte string. 13981d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar llvm::SmallVector<UTF16, 128> ToBuf(NumBytes); 13991d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar const UTF8 *FromPtr = (UTF8 *)Literal->getStrData(); 14001d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar UTF16 *ToPtr = &ToBuf[0]; 14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ConversionResult Result = ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, 14031d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar &ToPtr, ToPtr + NumBytes, 14041d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar strictConversion); 14051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14061d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // Check for conversion failure. 14071d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar if (Result != conversionOK) { 14081d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // FIXME: Have Sema::CheckObjCString() validate the UTF-8 string and remove 14091d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar // this duplicate code. 14101d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar assert(Result == sourceIllegal && "UTF-8 to UTF-16 conversion failed"); 1411f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength = NumBytes; 1412f015b034159d40e7033309e50036804eb1971787Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(Literal->getStrData(), 1413f015b034159d40e7033309e50036804eb1971787Daniel Dunbar StringLength)); 1414e9b7d8ace8674585818990cff585daae7745bd88Steve Naroff } 14151d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 141670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // ConvertUTF8toUTF16 returns the length in ToPtr. 14171d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar StringLength = ToPtr - &ToBuf[0]; 141870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar 141970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // Render the UTF-16 string into a byte array and convert to the target byte 142070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // order. 142170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // 142270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar // FIXME: This isn't something we should need to do here. 142370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar llvm::SmallString<128> AsBytes; 142470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.reserve(StringLength * 2); 142570ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar for (unsigned i = 0; i != StringLength; ++i) { 142670ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar unsigned short Val = ToBuf[i]; 142770ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar if (TargetIsLSB) { 142870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val & 0xFF); 142970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val >> 8); 143070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } else { 143170ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val >> 8); 143270ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar AsBytes.push_back(Val & 0xFF); 143370ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } 143470ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar } 1435434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar // Append one extra null character, the second is automatically added by our 1436434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar // caller. 1437434da48d0e35764f18b3fc96c75504746050b046Daniel Dunbar AsBytes.push_back(0); 143870ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar 14391d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar IsUTF16 = true; 144070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar return Map.GetOrCreateValue(llvm::StringRef(AsBytes.data(), AsBytes.size())); 14411d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar} 14421d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar 14431d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbarllvm::Constant * 14441d5529132e4620562cab931c1f84c24e42f02741Daniel DunbarCodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { 14451d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar unsigned StringLength = 0; 14461d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar bool isUTF16 = false; 14471d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar llvm::StringMapEntry<llvm::Constant*> &Entry = 14481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GetConstantCFStringEntry(CFConstantStringMap, Literal, 144970ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar getTargetData().isLittleEndian(), 145070ee975fad4653fa09f8e77f9a46a7b1f592ef59Daniel Dunbar isUTF16, StringLength); 14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14521d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar if (llvm::Constant *C = Entry.getValue()) 14531d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar return C; 14541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14550032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *Zero = 14560032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant::getNullValue(llvm::Type::getInt32Ty(VMContext)); 14573e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14599d4a15fd3b85434c43ea27562793de63a793321aChris Lattner // If we don't already have it, get __CFConstantStringClassReference. 1460c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 1461c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 146296e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson Ty = llvm::ArrayType::get(Ty, 0); 14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Constant *GV = CreateRuntimeVariable(Ty, 14649d4a15fd3b85434c43ea27562793de63a793321aChris Lattner "__CFConstantStringClassReference"); 14653e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 14663e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 14673c4972def972f8ca44dcd0561779a12aaa6fec97Owen Anderson llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 1468c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1470e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 14713e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::StructType *STy = 1473e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 1474e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 14755add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson std::vector<llvm::Constant*> Fields(4); 147644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 1477c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 14785add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson Fields[0] = CFConstantStringClassRef; 14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1480c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 14813e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Fields[1] = isUTF16 ? llvm::ConstantInt::get(Ty, 0x07d0) : 14835add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson llvm::ConstantInt::get(Ty, 0x07C8); 14845add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson 1485c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 14860032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *C = llvm::ConstantArray::get(VMContext, Entry.getKey().str()); 1487a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar 148895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::LinkageTypes Linkage; 1489278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner bool isConstant; 1490a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 1491278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner // FIXME: why do utf strings get "_" labels instead of "L" labels? 149295b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Linkage = llvm::GlobalValue::InternalLinkage; 1493278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner // Note: -fwritable-strings doesn't make unicode CFStrings writable, but 1494278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner // does make plain ascii ones writable. 1495278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner isConstant = true; 1496a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } else { 149795b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner Linkage = llvm::GlobalValue::PrivateLinkage; 1498278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner isConstant = !Features.WritableStrings; 1499a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 1500278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner 15011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable *GV = 1502278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner new llvm::GlobalVariable(getModule(), C->getType(), isConstant, Linkage, C, 1503278b9f06933c385ffbccc15f8491787470cb4a1bChris Lattner ".str"); 1504a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar if (isUTF16) { 15054da244c23d6093adbbbc41654aa5c5111b38f431Ken Dyck CharUnits Align = getContext().getTypeAlignInChars(getContext().ShortTy); 15064da244c23d6093adbbbc41654aa5c5111b38f431Ken Dyck GV->setAlignment(Align.getQuantity()); 1507a9668e0b4c451a1021fe650c451b54dc98c2d18dDaniel Dunbar } 15085add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson Fields[2] = llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 15095add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson 1510c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 1511c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 15125add6835b0c6b0f67e19fd5366825d3e41eb0dcfAnders Carlsson Fields[3] = llvm::ConstantInt::get(Ty, StringLength); 15131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1514c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 151508e252425ca2cbdc44ba65d9a657ed5398014e36Owen Anderson C = llvm::ConstantStruct::get(STy, Fields); 15161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump GV = new llvm::GlobalVariable(getModule(), C->getType(), true, 15171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::GlobalVariable::PrivateLinkage, C, 151895b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner "_unnamed_cfstring_"); 15198e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (const char *Sect = getContext().Target.getCFStringSection()) 15208e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar GV->setSection(Sect); 15211d5529132e4620562cab931c1f84c24e42f02741Daniel Dunbar Entry.setValue(GV); 15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15230c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1524c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 152545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 15266143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 15271e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 15286143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 15291e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 15301e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 15311e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 15321e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 15331e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 15341e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 15351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1536dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner // Resize the string to the right size. 15371e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 15381e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 15391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1540dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner if (E->isWide()) 1541dbb1ecc32ca122b07b7c98fd0a8f6f53985adaccChris Lattner RealLen *= getContext().Target.getWCharWidth()/8; 15421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15431e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15451e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 15461e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 15471e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 15486143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 15496143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 15506143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 15516143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 15526143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 15537eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman // FIXME: We shouldn't need to bitcast the constant in the wide string case. 15547eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman llvm::Constant *C = GetAddrOfConstantString(GetStringForStringLiteral(S)); 15557eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman if (S->isWide()) { 15567eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman llvm::Type *DestTy = 15577eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman llvm::PointerType::getUnqual(getTypes().ConvertType(S->getType())); 15587eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman C = llvm::ConstantExpr::getBitCast(C, DestTy); 15597eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman } 15607eb79c1010c8d30b852768bec96e81cd3e6def2eEli Friedman return C; 15616143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 15626143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1563eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 1564eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner/// array for the given ObjCEncodeExpr node. 1565eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattnerllvm::Constant * 1566eaf2bb89eb2aad3b80673de30febe52df43c10ecChris LattnerCodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 1567eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner std::string Str; 1568eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner getContext().getObjCEncodingForType(E->getEncodedType(), Str); 1569a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman 1570a210f350fa78c263caa26e0f999cce85bb235309Eli Friedman return GetAddrOfConstantCString(Str); 1571eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner} 1572eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1573eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1574a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 15751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 157645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 15775fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 15785fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 15796143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 15800032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::Constant *C = 15810032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson llvm::ConstantArray::get(CGM.getLLVMContext(), str, false); 15821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 158345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 15841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, 158595b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner llvm::GlobalValue::PrivateLinkage, 15861c431b323d776362490bbf7cc796b74fedaf19f2Owen Anderson C, GlobalName); 158745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 158845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 15896143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 15906143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 15916143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 15926143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 15936143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 15946143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 15956143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 15966143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 15975fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 15985fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 15998e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar bool IsConstant = !Features.WritableStrings; 16008e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 16018e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Get the default prefix if a name wasn't specified. 16028e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!GlobalName) 160395b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner GlobalName = ".str"; 16048e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar 16058e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar // Don't share any string literals if strings aren't constant. 16068e5c2b8072f4409c7c0004331d1db9652d5209c0Daniel Dunbar if (!IsConstant) 16075fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 16081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::StringMapEntry<llvm::Constant *> &Entry = 161095b851e55c328af4b69da7bfc1124bf258c0ffe5Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 161145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 161245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 1613eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner return Entry.getValue(); 161445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 161545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 16165fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 161745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 161845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 161945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 16206143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 16216143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 16226143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 16236143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 16245fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 16255fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1626c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 16276143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 162841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1629af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1630af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 16311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenModule::EmitObjCPropertyImplementations(const 1632af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 16331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (ObjCImplementationDecl::propimpl_iterator 163417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { 1635af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 16361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1637af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1638af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1639af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1640af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1641af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1642af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1643af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1644af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1645af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 164617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis if (!D->getInstanceMethod(PD->getGetterName())) 1647fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1648fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1649af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 165017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis !D->getInstanceMethod(PD->getSetterName())) 1651fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1652fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1653af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1654af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1655af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1656af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 165791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson/// EmitNamespace - Emit all declarations in a namespace. 1658984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlssonvoid CodeGenModule::EmitNamespace(const NamespaceDecl *ND) { 165917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::decl_iterator I = ND->decls_begin(), E = ND->decls_end(); 1660984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson I != E; ++I) 1661984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitTopLevelDecl(*I); 1662984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson} 1663984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson 166491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson// EmitLinkageSpec - Emit all declarations in a linkage spec. 166591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlssonvoid CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 1666f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman if (LSD->getLanguage() != LinkageSpecDecl::lang_c && 1667f976be8a63f9e0d3bd50e33dadef02c3c9921747Eli Friedman LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { 166891e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson ErrorUnsupported(LSD, "linkage spec"); 166991e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson return; 167091e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson } 167191e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 167217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::decl_iterator I = LSD->decls_begin(), E = LSD->decls_end(); 167391e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson I != E; ++I) 167491e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitTopLevelDecl(*I); 167591e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson} 167691e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson 167741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 167841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 167941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 168041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 168141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 168241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 168341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 168441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 168516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Ignore dependent declarations. 168616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (D->getDeclContext() && D->getDeclContext()->isDependentContext()) 168716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return; 16881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 168941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 1690293361afd4199c92aabff9267fddea890943c586Anders Carlsson case Decl::CXXConversion: 16912b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson case Decl::CXXMethod: 169241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 169316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Skip function templates 169416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 169516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return; 16961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1697555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson EmitGlobal(cast<FunctionDecl>(D)); 1698555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson break; 1699555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson 170041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 1701555b4bb2749aea2ec8e2adc351a71ec1cb9bdc33Anders Carlsson EmitGlobal(cast<VarDecl>(D)); 170241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 170341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 170495d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson // C++ Decls 170541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1706984e06874685396ca2cb51f0008cfff7c9b3d9c6Anders Carlsson EmitNamespace(cast<NamespaceDecl>(D)); 170741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 17089cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor // No code generation needed. 17099d0c6613ec040ad8d952556be909232a7f25dedeJohn McCall case Decl::UsingShadow: 17109cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor case Decl::Using: 1711dd9967a6374c9a44be4af02aaeee340ffb82848fDouglas Gregor case Decl::UsingDirective: 1712127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor case Decl::ClassTemplate: 1713127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor case Decl::FunctionTemplate: 1714018837b081bd1a725bae1c020eb1649975b04d67Anders Carlsson case Decl::NamespaceAlias: 17159cfbe48a7a20a217fdb2920b29b67ae7941cb116Douglas Gregor break; 171695d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson case Decl::CXXConstructor: 17171fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson // Skip function templates 17181fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson if (cast<FunctionDecl>(D)->getDescribedFunctionTemplate()) 17191fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson return; 17201fe598c026dc03fddbd65efb8119eb1afd9b1520Anders Carlsson 172195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson EmitCXXConstructors(cast<CXXConstructorDecl>(D)); 172295d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson break; 172327ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson case Decl::CXXDestructor: 172427ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson EmitCXXDestructors(cast<CXXDestructorDecl>(D)); 172527ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson break; 172636674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson 172736674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson case Decl::StaticAssert: 172836674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson // Nothing to do. 172936674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson break; 173036674d2978eb53962218ac67bb4d352cc287ea05Anders Carlsson 173195d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson // Objective-C Decls 17321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 173338e24c782c17b6058bf61d635747bbde19fb1bc7Fariborz Jahanian // Forward declarations, no (immediate) code generation. 173441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 173541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 1736b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCCategory: 1737b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian case Decl::ObjCInterface: 173841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1739285d0dba947b7c9960eaa88e8c4fced0398d4319Chris Lattner 174041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 1741b31cb7f1752ea011fd06ac9574ce24667d11cbdbFariborz Jahanian Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 174241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 174341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 174441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1745af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1746af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 174741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 174841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 174941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1750af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1751af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1752af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1753af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 175441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 17551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 175641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 175741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 175841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 17596fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis if (OMD->getBody()) 176041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 176141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 176241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 17631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case Decl::ObjCCompatibleAlias: 1764305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 176541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 176641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 176791e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson case Decl::LinkageSpec: 176891e20dd8bf1bc8980ee93839383d2bd170bce050Anders Carlsson EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 176941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 177041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 177141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 177241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 17738d04258483be6583f0865464234d014807a3e1ccBenjamin Kramer llvm::StringRef AsmString = AD->getAsmString()->getString(); 17741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 177541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 177641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 177741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 177841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 17798d04258483be6583f0865464234d014807a3e1ccBenjamin Kramer getModule().setModuleInlineAsm(S + '\n' + AsmString.str()); 178041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 178141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 17821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 17831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump default: 1784f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // Make sure we handled everything we should, every other kind is a 1785f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind 1786f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // function. Need to recode Decl::Kind to do that easily. 178741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 178841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 178941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1790