CodeGenModule.cpp revision 7542bca16b63c84e401b44b586ac3378aed446c5
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 14e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta#include "CGDebugInfo.h" 155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenModule.h" 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenFunction.h" 170dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h" 18af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar#include "CGObjCRuntime.h" 195f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor#include "Mangle.h" 205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h" 21c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar#include "clang/AST/DeclObjC.h" 2221ef7ae45c8b91f23cf5eab2263421bb398a644bChris Lattner#include "clang/AST/DeclCXX.h" 232c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner#include "clang/Basic/Diagnostic.h" 248bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman#include "clang/Basic/SourceManager.h" 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h" 26ec9426ca6039279bcc99bc2c625bb2abe4f0353dNate Begeman#include "llvm/CallingConv.h" 27bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner#include "llvm/Module.h" 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Intrinsics.h" 2920ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov#include "llvm/Target/TargetData.h" 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang; 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace CodeGen; 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris LattnerCodeGenModule::CodeGenModule(ASTContext &C, const LangOptions &LO, 35fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner llvm::Module &M, const llvm::TargetData &TD, 36f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar Diagnostic &diags, bool GenerateDebugInfo) 37fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner : Context(C), Features(LO), TheModule(M), TheTargetData(TD), Diags(diags), 38208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar Types(C, M, TD), Runtime(0), MemCpyFn(0), MemMoveFn(0), MemSetFn(0), 39ab695143861b520f5c9f8f982534a71d355396f1Mike Stump CFConstantStringClassRef(0), NSConcreteGlobalBlock(0), 409b8a7977109604d573b49d517e98badbbb9d5ac7Mike Stump BlockDescriptorType(0), GenericBlockLiteralType(0) { 41208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 42208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Features.ObjC1) { 43f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar if (Features.NeXTRuntime) { 4430bc57187be7535c57ef1ca8ff3e765653e94332Fariborz Jahanian Runtime = Features.ObjCNonFragileABI ? CreateMacNonFragileABIObjCRuntime(*this) 45ee0af74d1e0990c7b66d32657f3e4e54b8691552Fariborz Jahanian : CreateMacObjCRuntime(*this); 46208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar } else { 47208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar Runtime = CreateGNUObjCRuntime(*this); 48208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar } 49c17a4d3b16a2624a76de5d7508805534545bd3bfDaniel Dunbar } 50e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 51e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 5226efc3388adb010984da2f70e1f24e8286e6476dMike Stump DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0; 5326efc3388adb010984da2f70e1f24e8286e6476dMike Stump 5426efc3388adb010984da2f70e1f24e8286e6476dMike Stump Block.GlobalUniqueCount = 0; 552b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 562b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 572b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 58815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 59815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 60815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 61815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 62815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 630269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitDeferred(); 64219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar EmitAliases(); 65208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 66208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 67208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 686bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 696bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 70532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 710269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar EmitLLVMUsed(); 72f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar BindRuntimeFunctions(); 732b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 75f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarvoid CodeGenModule::BindRuntimeFunctions() { 76f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // Deal with protecting runtime function names. 77f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar for (unsigned i = 0, e = RuntimeFunctions.size(); i < e; ++i) { 78f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = RuntimeFunctions[i].first; 79f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name = RuntimeFunctions[i].second; 80f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 810293d540baafbe070c1035611787a81001a4118eDaniel Dunbar // Discard unused runtime functions. 820293d540baafbe070c1035611787a81001a4118eDaniel Dunbar if (Fn->use_empty()) { 830293d540baafbe070c1035611787a81001a4118eDaniel Dunbar Fn->eraseFromParent(); 840293d540baafbe070c1035611787a81001a4118eDaniel Dunbar continue; 850293d540baafbe070c1035611787a81001a4118eDaniel Dunbar } 860293d540baafbe070c1035611787a81001a4118eDaniel Dunbar 87f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // See if there is a conflict against a function. 88f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Conflict = TheModule.getFunction(Name); 89f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar if (Conflict) { 90f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // Decide which version to take. If the conflict is a definition 91f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // we are forced to take that, otherwise assume the runtime 92f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // knows best. 93f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar if (!Conflict->isDeclaration()) { 94f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 95f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::ConstantExpr::getBitCast(Conflict, Fn->getType()); 96f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->replaceAllUsesWith(Casted); 97f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->eraseFromParent(); 98f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } else { 99f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->takeName(Conflict); 100f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 101f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::ConstantExpr::getBitCast(Fn, Conflict->getType()); 102f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->replaceAllUsesWith(Casted); 103f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->eraseFromParent(); 104f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 105f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } else { 106f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // FIXME: There still may be conflicts with aliases and 107f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // variables. 108f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->setName(Name); 109f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 110f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 111f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 112f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 113488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 1142c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 11590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 11690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 11790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 11890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 119488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 12056b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 1212c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 1220a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 1230a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 1242c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 12558c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 126488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 127c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 12890df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 12990df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 13090df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 13190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 132488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 13356b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 134c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 1350a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 136c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 137c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 13841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// setGlobalVisibility - Set the visibility for the given LLVM 13941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// GlobalValue according to the given clang AST visibility value. 14041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarstatic void setGlobalVisibility(llvm::GlobalValue *GV, 14141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar VisibilityAttr::VisibilityTypes Vis) { 1424f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman switch (Vis) { 1434f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman default: assert(0 && "Unknown visibility!"); 1444f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::DefaultVisibility: 1454f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1464f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1474f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::HiddenVisibility: 1484f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 1494f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1504f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::ProtectedVisibility: 1514f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1524f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1534f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman } 1544f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1554f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 1565f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration. 1575f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1585f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an 1595f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// IdentifierInfo* containing the mangled name. Otherwise, returns 1605f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// the name of the declaration as an identifier. 1615f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1625f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Returning an IdentifierInfo* here is a total hack. We 1635f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// really need some kind of string abstraction that either stores a 1645f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// mangled name or stores an IdentifierInfo*. This will require 1655f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// changes to the GlobalDeclMap, too. 1665f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1675f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Performance here is going to be terribly until we start 1685f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// caching mangled names. However, we should fix the problem above 1695f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// first. 1705f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas GregorIdentifierInfo *CodeGenModule::getMangledName(const NamedDecl *ND) const { 1715f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor std::string Name; 1725f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::raw_string_ostream Out(Name); 1735f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor if (!mangleName(ND, Context, Out)) 1745f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor return ND->getIdentifier(); 1755f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1765f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor return &Context.Idents.get(Out.str()); 1775f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1785f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1796d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1806d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1816bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 18249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1836bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1846d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1856d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1866bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 1876bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 1886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 18949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1906bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 1916bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 1926bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 1956bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType::get(llvm::Type::VoidTy, 1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 1986bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 202572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::StructType* CtorStructTy = 2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::StructType::get(llvm::Type::Int32Ty, 2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::PointerType::getUnqual(CtorFTy), NULL); 2056bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 2086bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 2096bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); 2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 2146bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2156bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 2166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2176bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar new llvm::GlobalVariable(AT, false, 218572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 2196bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ConstantArray::get(AT, Ctors), 2206bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalName, 221572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner &TheModule); 2226d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 2236d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2246d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 225532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 226532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 227532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 228532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 229532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 230532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 231532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 232532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 233532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 234532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue *gv = 235532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman new llvm::GlobalVariable(Array->getType(), false, 236532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue::AppendingLinkage, Array, 237532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman "llvm.global.annotations", &TheModule); 238532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 239532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 240532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 2415c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbarvoid CodeGenModule::SetGlobalValueAttributes(const Decl *D, 2425c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInternal, 2435c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInline, 2445c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar llvm::GlobalValue *GV, 2455c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool ForDefinition) { 24649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Set up linkage and many other things. Note, this is a simple 247d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // approximation of what we really want. 248219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!ForDefinition) { 249219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Only a few attributes are set on declarations. 2502f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLImportAttr>()) { 2512f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllimport attribute is overridden by a subsequent declaration as 2522f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllexport. 2533ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl if (!D->getAttr<DLLExportAttr>()) { 2542f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllimport attribute can be applied only to function decls, not to 2552f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // definitions. 2562f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2572f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (!FD->getBody()) 2582f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2592f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2602f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2613ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl } 2622f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } 263219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 264219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (IsInternal) { 265219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::InternalLinkage); 266219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 2672f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLExportAttr>()) { 2682f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2692f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllexport attribute is ignored for undefined symbols. 2702f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (FD->getBody()) 2712f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2722f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2732f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2742f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else if (D->getAttr<WeakAttr>() || IsInline) 275219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::WeakLinkage); 276219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 2770dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 278d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 27949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Figure out the relative priority of the attribute, 28049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // -fvisibility, and private_extern. 2810dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 28241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 283d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // FIXME: else handle -fvisibility 284a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 2850dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 286a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // Prefaced with special LLVM marker to indicate that the name 287a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // should not be munged. 288a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 289a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar } 29017f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 29117f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 29217f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 2935c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 2945c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Only add to llvm.used when we see a definition, otherwise we 2955c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // might add multiple times or risk the value being replaced by a 2965c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // subsequent RAUW. 2975c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (ForDefinition) { 2985c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 2995c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 3005c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar } 301d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 302d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 303761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D, 30445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar const CGFunctionInfo &Info, 305b768807c49a1c7085def099b848631856af766faDaniel Dunbar llvm::Function *F) { 306761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 30788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar ConstructAttributeList(Info, D, AttributeList); 308c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman 309761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 310761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeList.size())); 311ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 312ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman // Set the appropriate calling convention for the Function. 31345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar if (D->getAttr<FastCallAttr>()) 314f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_FastCall); 315f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov 316f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov if (D->getAttr<StdCallAttr>()) 317f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_StdCall); 318f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 319f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 320f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes 321f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition. 322219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, 323219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F) { 324219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (isa<ObjCMethodDecl>(D)) { 325219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, true, false, F, true); 326219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 327219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *FD = cast<FunctionDecl>(D); 328219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 329219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, true); 330219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 331219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 332f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar if (!Features.Exceptions) 333f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar F->addFnAttr(llvm::Attribute::NoUnwind); 334af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 335af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar if (D->getAttr<AlwaysInlineAttr>()) 336af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 337f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 338f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 339f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, 340f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 341541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); 342f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 343219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(MD, F); 344f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 345f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 346f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 347f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 348541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 34945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 350219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 351219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, false); 352219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 353219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 35445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 355219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::EmitAliases() { 356219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { 357219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *D = Aliases[i]; 358219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const AliasAttr *AA = D->getAttr<AliasAttr>(); 359219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 360219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // This is something of a hack, if the FunctionDecl got overridden 361219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // then its attributes will be moved to the new declaration. In 362219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // this case the current decl has no alias attribute, but we will 363219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // eventually see it. 364219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!AA) 365219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 366219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 367219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const std::string& aliaseeName = AA->getAliasee(); 368219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *aliasee = getModule().getFunction(aliaseeName); 369219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!aliasee) { 370219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This isn't unsupported, this is just an error, which 371219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // sema should catch, but... 372219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar ErrorUnsupported(D, "alias referencing a missing function"); 373219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 374219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 375219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 376219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::GlobalValue *GA = 377219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar new llvm::GlobalAlias(aliasee->getType(), 378219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 3795f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor getMangledName(D)->getName(), aliasee, 3805f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule()); 381219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 3825f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 383219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (Entry) { 384219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // If we created a dummy function for this then replace it. 385219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GA->takeName(Entry); 386219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 387219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Value *Casted = 388219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::ConstantExpr::getBitCast(GA, Entry->getType()); 389219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->replaceAllUsesWith(Casted); 390219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 391ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 392219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry = GA; 393219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 394219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 395219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Alias should never be internal or inline. 396219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, false, false, GA, true); 397219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 398ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman} 399ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 4000269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { 4010269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar assert(!GV->isDeclaration() && 4020269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "Only globals with definition can force usage."); 4030269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4040269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy)); 4050269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4060269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4070269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitLLVMUsed() { 4080269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Don't create llvm.used if there is no need. 4090269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar if (LLVMUsed.empty()) 4100269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar return; 4110269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4120269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(), 4130269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar LLVMUsed.size()); 4140269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalVariable *GV = 4150269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar new llvm::GlobalVariable(ATy, false, 4160269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::GlobalValue::AppendingLinkage, 4170269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar llvm::ConstantArray::get(ATy, LLVMUsed), 4180269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar "llvm.used", &getModule()); 4190269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar GV->setSection("llvm.metadata"); 4210269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar} 4220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 4230269871c9cba493f76237175ab60313406f3bafaDaniel Dunbarvoid CodeGenModule::EmitDeferred() { 4240269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // Emit code for any deferred decl which was used. Since a 4250269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // previously unused static decl may become used during the 4260269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // generation of code for a static function, iterate until no 4270269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar // changes are made. 4284c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman bool Changed; 4294c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman do { 4304c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = false; 431b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4320269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar for (std::list<const ValueDecl*>::iterator i = DeferredDecls.begin(), 4330269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar e = DeferredDecls.end(); i != e; ) { 434b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson const ValueDecl *D = *i; 435b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4366f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // Check if we have used a decl with the same name 4376f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // FIXME: The AST should have some sort of aggregate decls or 4386f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // global symbol map. 439219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This is missing some important cases. For example, we 44073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // need to check for uses in an alias. 4415f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor if (!GlobalDeclMap.count(getMangledName(D))) { 442b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson i++; 443a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar continue; 444b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson } 445b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 446bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Emit the definition. 447bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(D); 448bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 4494c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Erase the used decl from the list. 4500269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar i = DeferredDecls.erase(i); 451b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4524c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Remember that we made a change. 4534c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = true; 4544c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 4554c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } while (Changed); 4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4588bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 4598bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 4608bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 4613c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated. The second field is the constant string 4628bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation. The third field is a constant 4638bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 4648bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 4658bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4668bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 4678bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 4688bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4698bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 4708bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 4718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 4728bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 4738bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 4758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 4768bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4778bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); 4788bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), 4798bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 4808bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4818bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 4828bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 4838bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *annoGV = 4848bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(anno->getType(), false, 4858bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, anno, 4868bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman GV->getName() + ".str", M); 4878bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 4888bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 4898bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(unit->getType(), false, 4908bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, unit, ".str", M); 4918bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4928bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Create the ConstantStruct that is the global annotion. 4938bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 4948bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(GV, SBP), 4958bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(annoGV, SBP), 4968bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(unitGV, SBP), 4978bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) 4988bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 4998bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman return llvm::ConstantStruct::get(Fields, 4, false); 5008bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 5018bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 50273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarbool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { 5035c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // Never defer when EmitAllDecls is specified or the decl has 5045c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar // attribute used. 5055c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (Features.EmitAllDecls || Global->getAttr<UsedAttr>()) 50673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 507bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 508bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 50973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Constructors and destructors should never be deferred. 51073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getAttr<ConstructorAttr>() || FD->getAttr<DestructorAttr>()) 51173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 51273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 51373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (FD->getStorageClass() != FunctionDecl::Static) 51473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 51573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } else { 51673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 51773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar assert(VD->isFileVarDecl() && "Invalid decl."); 51873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 51973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (VD->getStorageClass() != VarDecl::Static) 52073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return false; 52173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar } 52273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 52373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return true; 52473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar} 52573241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 52673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) { 52773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 528219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Aliases are deferred until code for everything else has been 529219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // emitted. 530219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (FD->getAttr<AliasAttr>()) { 531219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar assert(!FD->isThisDeclarationADefinition() && 532219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar "Function alias cannot have a definition!"); 533219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Aliases.push_back(FD); 534219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return; 535219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 536219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 53773241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 53873241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (!FD->isThisDeclarationADefinition()) 53973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5400269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar } else { 5410269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar const VarDecl *VD = cast<VarDecl>(Global); 542bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 543bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 54473241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Forward declarations are emitted lazily on first use. 5457542bca16b63c84e401b44b586ac3378aed446c5Daniel Dunbar if (!VD->getInit() && VD->hasExternalStorage()) 54673241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar return; 5474c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 5484c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 54973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar // Defer code generation when possible. 55073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar if (MayDeferGeneration(Global)) { 5510269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar DeferredDecls.push_back(Global); 552bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 553bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 554bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 555bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 556bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(Global); 5574c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 5584c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 559bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { 560bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 561bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalFunctionDefinition(FD); 562bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 563bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 564bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 565bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 566bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 567bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 568bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5699986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { 57077ba708819285931932ecd33691a672bb59d221aEli Friedman assert(D->hasGlobalStorage() && "Not a global variable"); 57177ba708819285931932ecd33691a672bb59d221aEli Friedman 572bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar QualType ASTTy = D->getType(); 573bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 5749986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 575bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5763c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 5775f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 57849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (!Entry) { 57949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalVariable *GV = 58049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar new llvm::GlobalVariable(Ty, false, 58149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalValue::ExternalLinkage, 5825f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, getMangledName(D)->getName(), &getModule(), 5835f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, ASTTy.getAddressSpace()); 58449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar Entry = GV; 58549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 58649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // Handle things which are present even on external declarations. 58749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 58849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: This code is overly simple and should be merged with 58949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // other global handling. 59049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 59149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setConstant(D->getType().isConstant(Context)); 59249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 59349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (D->getStorageClass() == VarDecl::PrivateExtern) 59449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); 59549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar } 5963c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 5979986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar // Make sure the result is of the correct type. 5989986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 599bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 600bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 601bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 6028f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 60377ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 60477ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); 60577ba708819285931932ecd33691a672bb59d221aEli Friedman 6068f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 607cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 608cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // implicitly initialized with { 0 } 609cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::Type* InitTy; 610cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman if (ASTTy->isIncompleteArrayType()) { 611cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // An incomplete array is normally [ TYPE x 0 ], but we need 612cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // to fix it to [ TYPE x 1 ]. 613cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); 614cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); 615cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } else { 616cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = VarTy; 617cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } 618cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Init = llvm::Constant::getNullValue(InitTy); 61977ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 620bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Init = EmitConstantExpr(D->getInit()); 6218f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 62277ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type* InitType = Init->getType(); 6238e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 6245f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 6253c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); 6263c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 62777ba708819285931932ecd33691a672bb59d221aEli Friedman if (!GV) { 62877ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 62977ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 6305f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, getMangledName(D)->getName(), 6315f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 6323c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar } else if (GV->getType() != 6333c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { 63477ba708819285931932ecd33691a672bb59d221aEli Friedman // We have a definition after a prototype with the wrong type. 63577ba708819285931932ecd33691a672bb59d221aEli Friedman // We must make a new GlobalVariable* and update everything that used OldGV 63677ba708819285931932ecd33691a672bb59d221aEli Friedman // (a declaration or tentative definition) with the new GlobalVariable* 63777ba708819285931932ecd33691a672bb59d221aEli Friedman // (which will be a definition). 63877ba708819285931932ecd33691a672bb59d221aEli Friedman // 63977ba708819285931932ecd33691a672bb59d221aEli Friedman // This happens if there is a prototype for a global (e.g. "extern int x[];") 64077ba708819285931932ecd33691a672bb59d221aEli Friedman // and then a definition of a different type (e.g. "int x[10];"). This also 64177ba708819285931932ecd33691a672bb59d221aEli Friedman // happens when an initializer has a different type from the type of the 64277ba708819285931932ecd33691a672bb59d221aEli Friedman // global (this happens with unions). 643cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // 644cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // FIXME: This also ends up happening if there's a definition followed by 645cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // a tentative definition! (Although Sema rejects that construct 646cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // at the moment.) 64777ba708819285931932ecd33691a672bb59d221aEli Friedman 64877ba708819285931932ecd33691a672bb59d221aEli Friedman // Save the old global 64977ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalVariable *OldGV = GV; 65077ba708819285931932ecd33691a672bb59d221aEli Friedman 65177ba708819285931932ecd33691a672bb59d221aEli Friedman // Make a new global with the correct type 65277ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 65377ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 6545f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, getMangledName(D)->getName(), 6555f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 65677ba708819285931932ecd33691a672bb59d221aEli Friedman // Steal the name of the old global 65777ba708819285931932ecd33691a672bb59d221aEli Friedman GV->takeName(OldGV); 65877ba708819285931932ecd33691a672bb59d221aEli Friedman 65977ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 66077ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::Constant *NewPtrForOldDecl = 66177ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 66277ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->replaceAllUsesWith(NewPtrForOldDecl); 66377ba708819285931932ecd33691a672bb59d221aEli Friedman 66477ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 66577ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->eraseFromParent(); 66677ba708819285931932ecd33691a672bb59d221aEli Friedman } 66777ba708819285931932ecd33691a672bb59d221aEli Friedman 6683c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry = GV; 6699e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel 6708bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 6718bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 6728bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 673f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 6748bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 6758bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 67688a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 677b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes GV->setConstant(D->getType().isConstant(Context)); 678ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner 679cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // FIXME: This is silly; getTypeAlign should just work for incomplete arrays 680cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman unsigned Align; 681c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const IncompleteArrayType* IAT = 682c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner Context.getAsIncompleteArrayType(D->getType())) 683cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Align = Context.getTypeAlign(IAT->getElementType()); 684cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman else 685cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Align = Context.getTypeAlign(D->getType()); 68608d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) { 68708d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman Align = std::max(Align, AA->getAlignment()); 68808d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman } 68908d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman GV->setAlignment(Align / 8); 69008d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 691ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 69241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 693ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: else handle -fvisibility 694a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 695a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 696a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // Prefaced with special LLVM marker to indicate that the name 697a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // should not be munged. 698a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 699a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar } 70088a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner 70188a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 7028fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner if (D->getStorageClass() == VarDecl::Static) 7038fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 7048fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<DLLImportAttr>()) 705ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 706ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner else if (D->getAttr<DLLExportAttr>()) 707ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 7088fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<WeakAttr>()) 709ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakLinkage); 7108fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else { 711ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: This isn't right. This should handle common linkage and other 712ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // stuff. 713ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner switch (D->getStorageClass()) { 7148fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner case VarDecl::Static: assert(0 && "This case handled above"); 715ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Auto: 716ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Register: 717ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner assert(0 && "Can't have auto or register globals"); 718ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::None: 719ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (!D->getInit()) 720a07b76419a03f126c22949dce2e546ba4df0e415Eli Friedman GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 72198883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson else 72298883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 723ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 724ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Extern: 72549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 72649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar break; 72749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 728ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::PrivateExtern: 72949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 73049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 731ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 732ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner } 73388a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner } 734686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta 73517f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 73617f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 73717f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 7385c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar if (D->getAttr<UsedAttr>()) 7395c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar AddUsedGlobal(GV); 7405c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 741686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 742686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta CGDebugInfo *DI = getDebugInfo(); 743686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta if(DI) { 74466031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 745686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 746686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 74788a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 749bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::GlobalValue * 750bd012ff1fa088181646a784f385b28867372d434Daniel DunbarCodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D) { 751219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(D->getType()); 752219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 753219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 7545f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor getMangledName(D)->getName(), 7555f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule()); 756219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributes(D, F); 757219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return F; 758bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 759bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 760bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { 7619986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar QualType ASTTy = D->getType(); 7629986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 7639986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 7643c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 7653c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 7665f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 7673c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) 7683c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry = EmitForwardFunctionDefinition(D); 769bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 7709986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 771bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 772bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 773bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { 7745f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 7753c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) { 776bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Entry = EmitForwardFunctionDefinition(D); 777bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 7783c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 7793c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(D->getType()); 7803c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { 781bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise, we have a definition after a prototype with the wrong type. 782bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // F is the Function* for the one with the wrong type, we must make a new 783bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Function* and update everything that used F (a declaration) with the new 784bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Function* (which will be a definition). 785bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // 786bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // This happens if there is a prototype for a function (e.g. "int f()") and 787bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // then a definition of a different type (e.g. "int f(int x)"). Start by 788bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // making a new function of the correct type, RAUW, then steal the name. 7893c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D); 7903c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar NewFn->takeName(Entry); 791bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 792bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Replace uses of F with the Function we will endow with a body. 793bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar llvm::Constant *NewPtrForOldDecl = 7943c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 7953c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry->replaceAllUsesWith(NewPtrForOldDecl); 7963c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 797bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Ok, delete the old function now, which is dead. 7983c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); 799219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 800bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 801bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Entry = NewFn; 802bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 803bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 804bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 805219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *Fn = cast<llvm::Function>(Entry); 806219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 8076379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 808219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(D, Fn); 809219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 810219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { 811219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 812219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { 813219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 814bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 815bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 816bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 817f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarllvm::Function * 818f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel DunbarCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 819f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name) { 820f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = llvm::Function::Create(FTy, 821f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function::ExternalLinkage, 822f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar "", &TheModule); 823f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar RuntimeFunctions.push_back(std::make_pair(Fn, Name)); 824f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar return Fn; 825f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 826f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 827c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) { 828c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner // Make sure that this type is translated. 829c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner Types.UpdateCompletedType(TD); 830d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner} 831d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 832d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 833bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// getBuiltinLibFunction 834bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { 8351426fecdf90dd1986751b9940422e675880ff671Chris Lattner if (BuiltinID > BuiltinFunctions.size()) 8361426fecdf90dd1986751b9940422e675880ff671Chris Lattner BuiltinFunctions.resize(BuiltinID); 837bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 8381426fecdf90dd1986751b9940422e675880ff671Chris Lattner // Cache looked up functions. Since builtin id #0 is invalid we don't reserve 8391426fecdf90dd1986751b9940422e675880ff671Chris Lattner // a slot for it. 8401426fecdf90dd1986751b9940422e675880ff671Chris Lattner assert(BuiltinID && "Invalid Builtin ID"); 8411426fecdf90dd1986751b9940422e675880ff671Chris Lattner llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; 842bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner if (FunctionSlot) 843bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner return FunctionSlot; 844bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 845bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); 846bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 847bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the name, skip over the __builtin_ prefix. 848bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; 849bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 850bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 851bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); 852bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const llvm::FunctionType *Ty = 853bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 854bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 855bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: This has a serious problem with code like this: 856bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // void abs() {} 857bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // ... __builtin_abs(x); 858bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // The two versions of abs will collide. The fix is for the builtin to win, 859bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // and for the existing one to be turned into a constantexpr cast of the 860bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // builtin. In the case where the existing one is a static function, it 861bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // should just be renamed. 862c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (llvm::Function *Existing = getModule().getFunction(Name)) { 863c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) 864c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner return FunctionSlot = Existing; 865c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner assert(Existing == 0 && "FIXME: Name collision"); 866c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner } 867bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 868bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: param attributes for sext/zext etc. 8694c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman return FunctionSlot = 8704c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, 8714c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman &getModule()); 872bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 873bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 8747acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 8757acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 8767acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 8777acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 8787acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 879bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 8805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 8815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 8824e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 8834e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 885c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 8860c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 8870c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 8884e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 8894e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 8900c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 8910c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 89241ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 89341ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 8944e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 8954e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 89641ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 8977acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 898e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM, 899e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*>& Fields, 90044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *FieldD, FieldDecl *NextFieldD, 90144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::Constant* Field, 902e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl* RD, const llvm::StructType *STy) 903e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson{ 904e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append the field. 905e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(Field); 906e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 90744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); 908e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 909e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson int NextStructFieldNo; 91044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (!NextFieldD) { 911e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson NextStructFieldNo = STy->getNumElements(); 912e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } else { 91344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); 914e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 915e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 916e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append padding 917e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { 918e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant *C = 919e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); 920e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 921e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(C); 922e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 923e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson} 924e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 9253e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// We still need to work out the details of handling UTF-16. 9263e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// See: <rdr://2996215> 927bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule:: 928bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) { 929c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::StringMapEntry<llvm::Constant *> &Entry = 930c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 931c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 932c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (Entry.getValue()) 933c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson return Entry.getValue(); 934c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 9353e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); 9363e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 937c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 938c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 939c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 940c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = llvm::ArrayType::get(Ty, 0); 9413e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 9423e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // FIXME: This is fairly broken if 9433e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // __CFConstantStringClassReference is already defined, in that it 9443e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // will get renamed and the user will most likely see an opaque 9453e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // error message. This is a general issue with relying on 9463e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // particular names. 9473e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::GlobalVariable *GV = 948c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson new llvm::GlobalVariable(Ty, false, 949c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalVariable::ExternalLinkage, 0, 950c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson "__CFConstantStringClassReference", 951c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson &getModule()); 9523e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 9533e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 9543e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 9553e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 956c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 957c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 958e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 959e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); 9603e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 961e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson const llvm::StructType *STy = 962e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 963e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 964e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*> Fields; 96544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor RecordDecl::field_iterator Field = CFRD->field_begin(); 96644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 967c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 96844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *CurField = *Field++; 96944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *NextField = *Field++; 97044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 97144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringClassRef, CFRD, STy); 972c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 973c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 97444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 97544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 9763e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 97744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 97844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); 979c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 980c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 98144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 98244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 9833e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str); 984c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson C = new llvm::GlobalVariable(C->getType(), true, 985c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalValue::InternalLinkage, 986e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C, ".str", &getModule()); 98744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 988e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), 989e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson CFRD, STy); 990c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 991c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 99244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 99344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = 0; 994c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 99544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 99644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); 997c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 998c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 999e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C = llvm::ConstantStruct::get(STy, Fields); 10000c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable *GV = 10010c67829763b98bc670062b553897a851fab17401Anders Carlsson new llvm::GlobalVariable(C->getType(), true, 10020c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable::InternalLinkage, 10030c67829763b98bc670062b553897a851fab17401Anders Carlsson C, "", &getModule()); 10043e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10050c67829763b98bc670062b553897a851fab17401Anders Carlsson GV->setSection("__DATA,__cfstring"); 10060c67829763b98bc670062b553897a851fab17401Anders Carlsson Entry.setValue(GV); 10073e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 10080c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 1009c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 101045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 10116143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 10121e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 10136143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 1014662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar if (E->isWide()) { 1015662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar ErrorUnsupported(E, "wide string"); 1016662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar return "FIXME"; 1017662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar } 1018662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar 10191e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 10201e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 10211e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 10221e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 10231e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 10241e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 10251e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 10261e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar // Resize the string to the right size 10271e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar // FIXME: What about wchar_t strings? 10281e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 10291e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 10301e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 10311e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 10321e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 10331e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 10341e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 10356143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 10366143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 10376143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 10386143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 10396143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 10406143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar return GetAddrOfConstantString(GetStringForStringLiteral(S)); 10416143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 10426143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 1043a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 104445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 104545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 10465fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 10475fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 10486143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 10496143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str, false); 105045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 105145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 105245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner C = new llvm::GlobalVariable(C->getType(), constant, 105345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::GlobalValue::InternalLinkage, 10545fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar C, 10555fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar GlobalName ? GlobalName : ".str", 10565fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar &CGM.getModule()); 10576143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 105845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 105945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 106045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 10616143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 10626143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 10636143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 10646143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 10656143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 10666143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 10676143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 10686143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 10695fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 10705fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 107145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Don't share any string literals if writable-strings is turned on. 107245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Features.WritableStrings) 10735fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 107445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 107545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::StringMapEntry<llvm::Constant *> &Entry = 107645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 107745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 107845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 107945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return Entry.getValue(); 108045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 108145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 10825fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 108345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 108445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 108545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 10866143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 10876143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 10886143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 10896143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 10905fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 10915fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1092c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 10936143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 109441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1095af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1096af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 1097af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const 1098af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 1099af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), 1100af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar e = D->propimpl_end(); i != e; ++i) { 1101af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 1102af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1103af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1104af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1105af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1106af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1107af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1108af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1109af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1110af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1111af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 1112af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!D->getInstanceMethod(PD->getGetterName())) 1113fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1114fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1115af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 1116af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar !D->getInstanceMethod(PD->getSetterName())) 1117fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1118fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1119af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1120af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1121af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1122af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 112341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 112441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 112541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 112641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 112741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 112841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 112941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 113041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 113141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 113241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 113341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 113441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar EmitGlobal(cast<ValueDecl>(D)); 113541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 113641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 113741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1138662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar ErrorUnsupported(D, "namespace"); 113941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 114041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 114141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Objective-C Decls 114241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 114341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Forward declarations, no (immediate) code generation. 114441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 114541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategory: 114641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 114741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCInterface: 114841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 114941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 115041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 115141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 115241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 115341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 115441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1155af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1156af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 1157af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 115841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 115941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 116041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1161af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1162af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1163af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1164af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 116541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1166af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 116741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 116841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 116941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 117041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (OMD->getBody()) 117141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 117241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 117341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 117441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCompatibleAlias: 1175305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 117641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 117741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 117841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::LinkageSpec: { 117941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); 118041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) 1181488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar ErrorUnsupported(LSD, "linkage spec"); 118241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // FIXME: implement C++ linkage, C linkage works mostly by C 118341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // language reuse already. 118441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 118541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 118641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 118741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 118841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 118941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 119041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 119141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 119241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 119341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 119441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 119541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 119641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 119741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 119841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 119941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 120041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar default: 120141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Make sure we handled everything we should, every other kind is 120241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // a non-top-level decl. FIXME: Would be nice to have an 120341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // isTopLevelDeclKind function. Need to recode Decl::Kind to do 120441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // that easily. 120541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 120641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 120741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1208