CodeGenModule.cpp revision 5f2bfd4811996abb783aa6c7254c56baa6930e8c
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), 39d5cab5435371b8cc74a9e05ebd40b5995ebad149Anders Carlsson CFConstantStringClassRef(0), NSConcreteGlobalBlock(0) { 40208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 41208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Features.ObjC1) { 42f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar if (Features.NeXTRuntime) { 4330bc57187be7535c57ef1ca8ff3e765653e94332Fariborz Jahanian Runtime = Features.ObjCNonFragileABI ? CreateMacNonFragileABIObjCRuntime(*this) 44ee0af74d1e0990c7b66d32657f3e4e54b8691552Fariborz Jahanian : CreateMacObjCRuntime(*this); 45208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar } else { 46208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar Runtime = CreateGNUObjCRuntime(*this); 47208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar } 48c17a4d3b16a2624a76de5d7508805534545bd3bfDaniel Dunbar } 49e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta 50e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta // If debug info generation is enabled, create the CGDebugInfo object. 51815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek DebugInfo = GenerateDebugInfo ? new CGDebugInfo(this) : 0; 522b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 532b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner 542b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris LattnerCodeGenModule::~CodeGenModule() { 55815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete Runtime; 56815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek delete DebugInfo; 57815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek} 58815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 59815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenekvoid CodeGenModule::Release() { 6020ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov EmitStatics(); 61219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar EmitAliases(); 62208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (Runtime) 63208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction()) 64208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar AddGlobalCtor(ObjCInitFunction); 656bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalCtors, "llvm.global_ctors"); 666bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar EmitCtorList(GlobalDtors, "llvm.global_dtors"); 67532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman EmitAnnotations(); 68f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar BindRuntimeFunctions(); 692b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner} 705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 71f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarvoid CodeGenModule::BindRuntimeFunctions() { 72f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // Deal with protecting runtime function names. 73f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar for (unsigned i = 0, e = RuntimeFunctions.size(); i < e; ++i) { 74f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = RuntimeFunctions[i].first; 75f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name = RuntimeFunctions[i].second; 76f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 770293d540baafbe070c1035611787a81001a4118eDaniel Dunbar // Discard unused runtime functions. 780293d540baafbe070c1035611787a81001a4118eDaniel Dunbar if (Fn->use_empty()) { 790293d540baafbe070c1035611787a81001a4118eDaniel Dunbar Fn->eraseFromParent(); 800293d540baafbe070c1035611787a81001a4118eDaniel Dunbar continue; 810293d540baafbe070c1035611787a81001a4118eDaniel Dunbar } 820293d540baafbe070c1035611787a81001a4118eDaniel Dunbar 83f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // See if there is a conflict against a function. 84f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Conflict = TheModule.getFunction(Name); 85f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar if (Conflict) { 86f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // Decide which version to take. If the conflict is a definition 87f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // we are forced to take that, otherwise assume the runtime 88f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // knows best. 89f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar if (!Conflict->isDeclaration()) { 90f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 91f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::ConstantExpr::getBitCast(Conflict, Fn->getType()); 92f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->replaceAllUsesWith(Casted); 93f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->eraseFromParent(); 94f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } else { 95f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->takeName(Conflict); 96f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Value *Casted = 97f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::ConstantExpr::getBitCast(Fn, Conflict->getType()); 98f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->replaceAllUsesWith(Casted); 99f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Conflict->eraseFromParent(); 100f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 101f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } else { 102f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // FIXME: There still may be conflicts with aliases and 103f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar // variables. 104f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar Fn->setName(Name); 105f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 106f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar } 107f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 108f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 109488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 1102c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner/// specified stmt yet. 11190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type, 11290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 11390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 11490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 115488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 11656b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 1172c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner std::string Msg = Type; 1180a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(S->getLocStart()), DiagID) 1190a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner << Msg << S->getSourceRange(); 1202c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner} 12158c3f9ec11cbe852a518bf2f83af46f938b7b852Chris Lattner 122488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar/// ErrorUnsupported - Print out an error that codegen doesn't support the 123c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner/// specified decl yet. 12490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbarvoid CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type, 12590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError) { 12690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar if (OmitOnError && getDiags().hasErrorOccurred()) 12790df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar return; 128488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar unsigned DiagID = getDiags().getCustomDiagID(Diagnostic::Error, 12956b8001b42bd603ef593e3cb278d8b9b9ba26ca9Daniel Dunbar "cannot compile this %0 yet"); 130c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner std::string Msg = Type; 1310a14eee528a901c16f0e288fbc10a3abc1660d87Chris Lattner getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 132c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner} 133c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner 13441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// setGlobalVisibility - Set the visibility for the given LLVM 13541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// GlobalValue according to the given clang AST visibility value. 13641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarstatic void setGlobalVisibility(llvm::GlobalValue *GV, 13741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar VisibilityAttr::VisibilityTypes Vis) { 1384f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman switch (Vis) { 1394f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman default: assert(0 && "Unknown visibility!"); 1404f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::DefaultVisibility: 1414f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1424f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1434f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::HiddenVisibility: 1444f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 1454f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1464f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman case VisibilityAttr::ProtectedVisibility: 1474f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); 1484f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman break; 1494f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman } 1504f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman} 1514f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 1525f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// \brief Retrieves the mangled name for the given declaration. 1535f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1545f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// If the given declaration requires a mangled name, returns an 1555f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// IdentifierInfo* containing the mangled name. Otherwise, returns 1565f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// the name of the declaration as an identifier. 1575f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1585f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Returning an IdentifierInfo* here is a total hack. We 1595f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// really need some kind of string abstraction that either stores a 1605f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// mangled name or stores an IdentifierInfo*. This will require 1615f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// changes to the GlobalDeclMap, too. 1625f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// 1635f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// FIXME: Performance here is going to be terribly until we start 1645f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// caching mangled names. However, we should fix the problem above 1655f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor/// first. 1665f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas GregorIdentifierInfo *CodeGenModule::getMangledName(const NamedDecl *ND) const { 1675f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor std::string Name; 1685f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::raw_string_ostream Out(Name); 1695f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor if (!mangleName(ND, Context, Out)) 1705f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor return ND->getIdentifier(); 1715f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1725f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor return &Context.Idents.get(Out.str()); 1735f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor} 1745f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 1756d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// AddGlobalCtor - Add a function to the list that will be called before 1766d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner/// main() runs. 1776bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalCtor(llvm::Function * Ctor, int Priority) { 17849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1796bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalCtors.push_back(std::make_pair(Ctor, Priority)); 1806d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 1816d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 1826bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// AddGlobalDtor - Add a function to the list that will be called 1836bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar/// when the module is unloaded. 1846bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) { 18549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Type coercion of void()* types. 1866bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalDtors.push_back(std::make_pair(Dtor, Priority)); 1876bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar} 1886bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1896bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbarvoid CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) { 1906bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Ctor function type is void()*. 1916bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType* CtorFTy = 1926bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::FunctionType::get(llvm::Type::VoidTy, 1936bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<const llvm::Type*>(), 1946bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar false); 1956bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::Type *CtorPFTy = llvm::PointerType::getUnqual(CtorFTy); 1966bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 1976bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Get the type of a ctor entry, { i32, void ()* }. 198572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::StructType* CtorStructTy = 1996bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::StructType::get(llvm::Type::Int32Ty, 2006bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::PointerType::getUnqual(CtorFTy), NULL); 2016bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2026bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // Construct the constructor and destructor arrays. 2036bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> Ctors; 2046bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar for (CtorList::const_iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { 2056bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar std::vector<llvm::Constant*> S; 2066bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, I->second, false)); 2076bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar S.push_back(llvm::ConstantExpr::getBitCast(I->first, CtorPFTy)); 2086bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar Ctors.push_back(llvm::ConstantStruct::get(CtorStructTy, S)); 2096bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar } 2106bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 2116bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar if (!Ctors.empty()) { 2126bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ArrayType *AT = llvm::ArrayType::get(CtorStructTy, Ctors.size()); 2136bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar new llvm::GlobalVariable(AT, false, 214572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner llvm::GlobalValue::AppendingLinkage, 2156bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar llvm::ConstantArray::get(AT, Ctors), 2166bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar GlobalName, 217572cf09ae8a78af1c56d40b016ec4cf1837163acChris Lattner &TheModule); 2186d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner } 2196d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner} 2206d39760673df2e92d9293f46ff8c66dad6ab5e0aChris Lattner 221532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begemanvoid CodeGenModule::EmitAnnotations() { 222532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman if (Annotations.empty()) 223532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman return; 224532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 225532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman // Create a new global variable for the ConstantStruct in the Module. 226532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::Constant *Array = 227532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::ConstantArray::get(llvm::ArrayType::get(Annotations[0]->getType(), 228532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations.size()), 229532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman Annotations); 230532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue *gv = 231532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman new llvm::GlobalVariable(Array->getType(), false, 232532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman llvm::GlobalValue::AppendingLinkage, Array, 233532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman "llvm.global.annotations", &TheModule); 234532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman gv->setSection("llvm.metadata"); 235532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman} 236532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman 2370dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarstatic void SetGlobalValueAttributes(const Decl *D, 2380dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar bool IsInternal, 2390dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar bool IsInline, 240219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::GlobalValue *GV, 241219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar bool ForDefinition) { 24249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Set up linkage and many other things. Note, this is a simple 243d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // approximation of what we really want. 244219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!ForDefinition) { 245219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Only a few attributes are set on declarations. 2462f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLImportAttr>()) { 2472f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllimport attribute is overridden by a subsequent declaration as 2482f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllexport. 2493ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl if (!D->getAttr<DLLExportAttr>()) { 2502f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // dllimport attribute can be applied only to function decls, not to 2512f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // definitions. 2522f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2532f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (!FD->getBody()) 2542f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2552f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2562f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLImportLinkage); 2573ef5db646a6f66bb23146c3e506c294f31adf018Sebastian Redl } 2582f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } 259219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 260219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (IsInternal) { 261219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::InternalLinkage); 262219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 2632f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (D->getAttr<DLLExportAttr>()) { 2642f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 2652f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov // The dllexport attribute is ignored for undefined symbols. 2662f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov if (FD->getBody()) 2672f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2682f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else 2692f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov GV->setLinkage(llvm::Function::DLLExportLinkage); 2702f402708e62f89fb875442802e3d3f20fc909d33Anton Korobeynikov } else if (D->getAttr<WeakAttr>() || IsInline) 271219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GV->setLinkage(llvm::Function::WeakLinkage); 272219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 2730dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar } 274d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 27549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: Figure out the relative priority of the attribute, 27649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // -fvisibility, and private_extern. 2770dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 27841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 279d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes // FIXME: else handle -fvisibility 280a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 2810dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 282a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // Prefaced with special LLVM marker to indicate that the name 283a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // should not be munged. 284a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 285a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar } 28617f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 28717f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 28817f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 289d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes} 290d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 291761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patelvoid CodeGenModule::SetFunctionAttributes(const Decl *D, 29245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar const CGFunctionInfo &Info, 293b768807c49a1c7085def099b848631856af766faDaniel Dunbar llvm::Function *F) { 294761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType AttributeList; 29588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar ConstructAttributeList(Info, D, AttributeList); 296c134fcb0d7989fe6937e47e6216637647e074aefEli Friedman 297761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel F->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(), 298761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeList.size())); 299ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 300ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman // Set the appropriate calling convention for the Function. 30145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar if (D->getAttr<FastCallAttr>()) 302f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_FastCall); 303f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov 304f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov if (D->getAttr<StdCallAttr>()) 305f1c9c09e2e2220e4bbfb7e9d8adf9bf2c2406b80Anton Korobeynikov F->setCallingConv(llvm::CallingConv::X86_StdCall); 306f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 307f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 308f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// SetFunctionAttributesForDefinition - Set function attributes 309f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar/// specific to a function definition. 310219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::SetFunctionAttributesForDefinition(const Decl *D, 311219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F) { 312219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (isa<ObjCMethodDecl>(D)) { 313219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, true, false, F, true); 314219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else { 315219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *FD = cast<FunctionDecl>(D); 316219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 317219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, true); 318219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 319219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 320f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar if (!Features.Exceptions) 321f93349f3ec4d69eafba42436c33aaa91bfca7e70Daniel Dunbar F->addFnAttr(llvm::Attribute::NoUnwind); 322af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar 323af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar if (D->getAttr<AlwaysInlineAttr>()) 324af668b0e7d3581dea3b4f29a9262686e83887e5bDaniel Dunbar F->addFnAttr(llvm::Attribute::AlwaysInline); 325f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 326f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 327f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetMethodAttributes(const ObjCMethodDecl *MD, 328f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 329541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(MD, getTypes().getFunctionInfo(MD), F); 330f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 331219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(MD, F); 332f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar} 333f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 334f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbarvoid CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, 335f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F) { 336541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar SetFunctionAttributes(FD, getTypes().getFunctionInfo(FD), F); 33745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 338219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(FD, FD->getStorageClass() == FunctionDecl::Static, 339219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar FD->isInline(), F, false); 340219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar} 341219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 34245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 343219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbarvoid CodeGenModule::EmitAliases() { 344219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar for (unsigned i = 0, e = Aliases.size(); i != e; ++i) { 345219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const FunctionDecl *D = Aliases[i]; 346219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const AliasAttr *AA = D->getAttr<AliasAttr>(); 347219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 348219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // This is something of a hack, if the FunctionDecl got overridden 349219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // then its attributes will be moved to the new declaration. In 350219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // this case the current decl has no alias attribute, but we will 351219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // eventually see it. 352219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!AA) 353219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 354219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 355219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const std::string& aliaseeName = AA->getAliasee(); 356219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *aliasee = getModule().getFunction(aliaseeName); 357219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (!aliasee) { 358219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This isn't unsupported, this is just an error, which 359219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // sema should catch, but... 360219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar ErrorUnsupported(D, "alias referencing a missing function"); 361219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar continue; 362219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 363219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 364219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::GlobalValue *GA = 365219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar new llvm::GlobalAlias(aliasee->getType(), 366219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 3675f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor getMangledName(D)->getName(), aliasee, 3685f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule()); 369219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 3705f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 371219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (Entry) { 372219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // If we created a dummy function for this then replace it. 373219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar GA->takeName(Entry); 374219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 375219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Value *Casted = 376219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::ConstantExpr::getBitCast(GA, Entry->getType()); 377219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->replaceAllUsesWith(Casted); 378219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 379ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 380219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry = GA; 381219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 382219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 383219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Alias should never be internal or inline. 384219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetGlobalValueAttributes(D, false, false, GA, true); 385219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 386ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman} 387ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman 3884c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begemanvoid CodeGenModule::EmitStatics() { 3894c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Emit code for each used static decl encountered. Since a previously unused 3904c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // static decl may become used during the generation of code for a static 3914c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // function, iterate until no changes are made. 3924c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman bool Changed; 3934c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman do { 3944c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = false; 395b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 396b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson for (std::list<const ValueDecl*>::iterator i = StaticDecls.begin(), 397b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson e = StaticDecls.end(); i != e; ) { 398b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson const ValueDecl *D = *i; 399b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4006f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // Check if we have used a decl with the same name 4016f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // FIXME: The AST should have some sort of aggregate decls or 4026f7e2eee917a136ffc36834f020782b3f15d8fa6Eli Friedman // global symbol map. 403219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // FIXME: This is missing some important cases. For example, we 404219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // need to check for uses in an alias and in a constructor. 4055f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor if (!GlobalDeclMap.count(getMangledName(D))) { 406b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson i++; 407a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar continue; 408b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson } 409b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 410bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Emit the definition. 411bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(D); 412bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 4134c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Erase the used decl from the list. 414b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson i = StaticDecls.erase(i); 415b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 4164c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman // Remember that we made a change. 4174c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman Changed = true; 4184c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 4194c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } while (Changed); 4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 4228bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// EmitAnnotateAttr - Generate the llvm::ConstantStruct which contains the 4238bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// annotation information for a given GlobalValue. The annotation struct is 4248bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// {i8 *, i8 *, i8 *, i32}. The first field is a constant expression, the 4253c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar/// GlobalValue being annotated. The second field is the constant string 4268bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// created from the AnnotateAttr's annotation. The third field is a constant 4278bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// string containing the name of the translation unit. The fourth field is 4288bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// the line number in the file of the annotated value declaration. 4298bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4308bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// FIXME: this does not unique the annotation string constants, as llvm-gcc 4318bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// appears to. 4328bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman/// 4338bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begemanllvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 4348bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, 4358bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman unsigned LineNo) { 4368bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Module *M = &getModule(); 4378bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4388bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // get [N x i8] constants for the annotation string, and the filename string 4398bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // which are the 2nd and 3rd elements of the global annotation structure. 4408bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const llvm::Type *SBP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 4418bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *anno = llvm::ConstantArray::get(AA->getAnnotation(), true); 4428bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *unit = llvm::ConstantArray::get(M->getModuleIdentifier(), 4438bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman true); 4448bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4458bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Get the two global values corresponding to the ConstantArrays we just 4468bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // created to hold the bytes of the strings. 4478bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *annoGV = 4488bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(anno->getType(), false, 4498bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, anno, 4508bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman GV->getName() + ".str", M); 4518bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // translation unit name string, emitted into the llvm.metadata section. 4528bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue *unitGV = 4538bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman new llvm::GlobalVariable(unit->getType(), false, 4548bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::GlobalValue::InternalLinkage, unit, ".str", M); 4558bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 4568bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman // Create the ConstantStruct that is the global annotion. 4578bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *Fields[4] = { 4588bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(GV, SBP), 4598bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(annoGV, SBP), 4608bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantExpr::getBitCast(unitGV, SBP), 4618bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::ConstantInt::get(llvm::Type::Int32Ty, LineNo) 4628bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman }; 4638bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman return llvm::ConstantStruct::get(Fields, 4, false); 4648bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman} 4658bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 466bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobal(const ValueDecl *Global) { 467bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar bool isDef, isStatic; 468bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 469bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Global)) { 470219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // Aliases are deferred until code for everything else has been 471219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar // emitted. 472219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (FD->getAttr<AliasAttr>()) { 473219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar assert(!FD->isThisDeclarationADefinition() && 474219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar "Function alias cannot have a definition!"); 475219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Aliases.push_back(FD); 476219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return; 477219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } 478219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 479219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar isDef = FD->isThisDeclarationADefinition(); 480bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar isStatic = FD->getStorageClass() == FunctionDecl::Static; 481bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = cast<VarDecl>(Global)) { 482bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 483bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 48449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar isDef = !((VD->getStorageClass() == VarDecl::Extern || 48549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar VD->getStorageClass() == VarDecl::PrivateExtern) && 48649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar VD->getInit() == 0); 487bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar isStatic = VD->getStorageClass() == VarDecl::Static; 488bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 489bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobal"); 4904c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman return; 4914c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman } 4924c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 493bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Forward declarations are emitted lazily on first use. 494bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (!isDef) 49588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner return; 496bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 497bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // If the global is a static, defer code generation until later so 498bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // we can easily omit unused statics. 499d604c40e933c445ff33ac83d62cc6b1adcf7014cDaniel Dunbar if (isStatic && !Features.EmitAllDecls) { 500bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar StaticDecls.push_back(Global); 501bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar return; 502bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 503bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 504bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise emit the definition. 505bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalDefinition(Global); 5064c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman} 5074c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman 508bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { 509bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 510bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalFunctionDefinition(FD); 511bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { 512bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar EmitGlobalVarDefinition(VD); 513bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 514bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar assert(0 && "Invalid argument to EmitGlobalDefinition()"); 515bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 516bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 517bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5189986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D) { 51977ba708819285931932ecd33691a672bb59d221aEli Friedman assert(D->hasGlobalStorage() && "Not a global variable"); 52077ba708819285931932ecd33691a672bb59d221aEli Friedman 521bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar QualType ASTTy = D->getType(); 522bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 5239986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 524bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 5253c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 5265f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 52749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (!Entry) { 52849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalVariable *GV = 52949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar new llvm::GlobalVariable(Ty, false, 53049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar llvm::GlobalValue::ExternalLinkage, 5315f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, getMangledName(D)->getName(), &getModule(), 5325f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, ASTTy.getAddressSpace()); 53349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar Entry = GV; 53449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 53549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // Handle things which are present even on external declarations. 53649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 53749988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: This code is overly simple and should be merged with 53849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // other global handling. 53949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 54049988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setConstant(D->getType().isConstant(Context)); 54149988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 54249988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar if (D->getStorageClass() == VarDecl::PrivateExtern) 54349988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar setGlobalVisibility(GV, VisibilityAttr::HiddenVisibility); 54449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar } 5453c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 5469986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar // Make sure the result is of the correct type. 5479986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 548bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 549bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 550bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { 5518f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner llvm::Constant *Init = 0; 55277ba708819285931932ecd33691a672bb59d221aEli Friedman QualType ASTTy = D->getType(); 55377ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type *VarTy = getTypes().ConvertTypeForMem(ASTTy); 55477ba708819285931932ecd33691a672bb59d221aEli Friedman 5558f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner if (D->getInit() == 0) { 556cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // This is a tentative definition; tentative definitions are 557cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // implicitly initialized with { 0 } 558cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::Type* InitTy; 559cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman if (ASTTy->isIncompleteArrayType()) { 560cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // An incomplete array is normally [ TYPE x 0 ], but we need 561cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // to fix it to [ TYPE x 1 ]. 562cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman const llvm::ArrayType* ATy = cast<llvm::ArrayType>(VarTy); 563cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = llvm::ArrayType::get(ATy->getElementType(), 1); 564cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } else { 565cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman InitTy = VarTy; 566cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman } 567cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Init = llvm::Constant::getNullValue(InitTy); 56877ba708819285931932ecd33691a672bb59d221aEli Friedman } else { 569bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Init = EmitConstantExpr(D->getInit()); 5708f32f7189b12f67aa4a19bc7c3855b599980eca0Chris Lattner } 57177ba708819285931932ecd33691a672bb59d221aEli Friedman const llvm::Type* InitType = Init->getType(); 5728e53e720b3d7c962e91138a130dbd5d6c2def0e5Devang Patel 5735f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 5743c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::GlobalVariable *GV = cast_or_null<llvm::GlobalVariable>(Entry); 5753c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 57677ba708819285931932ecd33691a672bb59d221aEli Friedman if (!GV) { 57777ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 57877ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 5795f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, getMangledName(D)->getName(), 5805f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 5813c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar } else if (GV->getType() != 5823c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::PointerType::get(InitType, ASTTy.getAddressSpace())) { 58377ba708819285931932ecd33691a672bb59d221aEli Friedman // We have a definition after a prototype with the wrong type. 58477ba708819285931932ecd33691a672bb59d221aEli Friedman // We must make a new GlobalVariable* and update everything that used OldGV 58577ba708819285931932ecd33691a672bb59d221aEli Friedman // (a declaration or tentative definition) with the new GlobalVariable* 58677ba708819285931932ecd33691a672bb59d221aEli Friedman // (which will be a definition). 58777ba708819285931932ecd33691a672bb59d221aEli Friedman // 58877ba708819285931932ecd33691a672bb59d221aEli Friedman // This happens if there is a prototype for a global (e.g. "extern int x[];") 58977ba708819285931932ecd33691a672bb59d221aEli Friedman // and then a definition of a different type (e.g. "int x[10];"). This also 59077ba708819285931932ecd33691a672bb59d221aEli Friedman // happens when an initializer has a different type from the type of the 59177ba708819285931932ecd33691a672bb59d221aEli Friedman // global (this happens with unions). 592cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // 593cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // FIXME: This also ends up happening if there's a definition followed by 594cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // a tentative definition! (Although Sema rejects that construct 595cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // at the moment.) 59677ba708819285931932ecd33691a672bb59d221aEli Friedman 59777ba708819285931932ecd33691a672bb59d221aEli Friedman // Save the old global 59877ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalVariable *OldGV = GV; 59977ba708819285931932ecd33691a672bb59d221aEli Friedman 60077ba708819285931932ecd33691a672bb59d221aEli Friedman // Make a new global with the correct type 60177ba708819285931932ecd33691a672bb59d221aEli Friedman GV = new llvm::GlobalVariable(InitType, false, 60277ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::GlobalValue::ExternalLinkage, 6035f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 0, getMangledName(D)->getName(), 6045f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule(), 0, ASTTy.getAddressSpace()); 60577ba708819285931932ecd33691a672bb59d221aEli Friedman // Steal the name of the old global 60677ba708819285931932ecd33691a672bb59d221aEli Friedman GV->takeName(OldGV); 60777ba708819285931932ecd33691a672bb59d221aEli Friedman 60877ba708819285931932ecd33691a672bb59d221aEli Friedman // Replace all uses of the old global with the new global 60977ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::Constant *NewPtrForOldDecl = 61077ba708819285931932ecd33691a672bb59d221aEli Friedman llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 61177ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->replaceAllUsesWith(NewPtrForOldDecl); 61277ba708819285931932ecd33691a672bb59d221aEli Friedman 61377ba708819285931932ecd33691a672bb59d221aEli Friedman // Erase the old global, since it is no longer used. 61477ba708819285931932ecd33691a672bb59d221aEli Friedman OldGV->eraseFromParent(); 61577ba708819285931932ecd33691a672bb59d221aEli Friedman } 61677ba708819285931932ecd33691a672bb59d221aEli Friedman 6173c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry = GV; 6189e32d4b4a7270a9701b7cb454381eeaa4cc42a77Devang Patel 6198bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman if (const AnnotateAttr *AA = D->getAttr<AnnotateAttr>()) { 6208bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman SourceManager &SM = Context.getSourceManager(); 6218bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman AddAnnotation(EmitAnnotateAttr(GV, AA, 622f7cf85b330bedd2877e1371fb0a83e99751ae162Chris Lattner SM.getInstantiationLineNumber(D->getLocation()))); 6238bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman } 6248bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman 62588a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner GV->setInitializer(Init); 626b381aac9bae6d608c72267dd0ed08ec6369e94e4Nuno Lopes GV->setConstant(D->getType().isConstant(Context)); 627ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner 628cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman // FIXME: This is silly; getTypeAlign should just work for incomplete arrays 629cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman unsigned Align; 630c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner if (const IncompleteArrayType* IAT = 631c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner Context.getAsIncompleteArrayType(D->getType())) 632cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Align = Context.getTypeAlign(IAT->getElementType()); 633cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman else 634cd5f4aaf0c219189878126d556f35e38fdb8afa1Eli Friedman Align = Context.getTypeAlign(D->getType()); 63508d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) { 63608d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman Align = std::max(Align, AA->getAlignment()); 63708d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman } 63808d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman GV->setAlignment(Align / 8); 63908d7802a406ee4a7cc18e8fce0c137b8c410ea7cEli Friedman 640ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (const VisibilityAttr *attr = D->getAttr<VisibilityAttr>()) 64141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar setGlobalVisibility(GV, attr->getVisibility()); 642ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: else handle -fvisibility 643a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar 644a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar if (const AsmLabelAttr *ALA = D->getAttr<AsmLabelAttr>()) { 645a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // Prefaced with special LLVM marker to indicate that the name 646a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar // should not be munged. 647a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar GV->setName("\01" + ALA->getLabel()); 648a735ad8be5536a1cd3e9817ec27dfeb2a0c1d5caDaniel Dunbar } 64988a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner 65088a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner // Set the llvm linkage type as appropriate. 6518fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner if (D->getStorageClass() == VarDecl::Static) 6528fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner GV->setLinkage(llvm::Function::InternalLinkage); 6538fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<DLLImportAttr>()) 654ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLImportLinkage); 655ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner else if (D->getAttr<DLLExportAttr>()) 656ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::Function::DLLExportLinkage); 6578fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else if (D->getAttr<WeakAttr>()) 658ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner GV->setLinkage(llvm::GlobalVariable::WeakLinkage); 6598fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner else { 660ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // FIXME: This isn't right. This should handle common linkage and other 661ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner // stuff. 662ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner switch (D->getStorageClass()) { 6638fabd78f1976243cb223fb3e969c6f317d1ae44dChris Lattner case VarDecl::Static: assert(0 && "This case handled above"); 664ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Auto: 665ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Register: 666ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner assert(0 && "Can't have auto or register globals"); 667ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::None: 668ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner if (!D->getInit()) 669a07b76419a03f126c22949dce2e546ba4df0e415Eli Friedman GV->setLinkage(llvm::GlobalVariable::CommonLinkage); 67098883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson else 67198883e1e699457697fb8d5ac6d175dd3ee078774Anders Carlsson GV->setLinkage(llvm::GlobalVariable::ExternalLinkage); 672ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 673ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::Extern: 67449988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 67549988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar break; 67649988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar 677ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner case VarDecl::PrivateExtern: 67849988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 67949988884c1da4b2200bfe2298a1e41b3f044e8d4Daniel Dunbar // FIXME: common 680ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner break; 681ddee4231e9bdfbac1e1f5385ff1a17fd0e0b0e39Chris Lattner } 68288a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner } 683686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta 68417f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 68517f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar GV->setSection(SA->getName()); 68617f194f4393a67fd28ad822c06d32b8cb99bad3fDaniel Dunbar 687686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta // Emit global variable debug information. 688686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta CGDebugInfo *DI = getDebugInfo(); 689686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta if(DI) { 69066031a5594bc9a7dc0dc5137c3e7955f835e4639Daniel Dunbar DI->setLocation(D->getLocation()); 691686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta DI->EmitGlobalVariable(GV, D); 692686226b538e72c5059ab7c9a8f87eb883193b645Sanjiv Gupta } 69388a69ad80e1550e9932666e6efa050a5b1223889Chris Lattner} 6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 695bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::GlobalValue * 696bd012ff1fa088181646a784f385b28867372d434Daniel DunbarCodeGenModule::EmitForwardFunctionDefinition(const FunctionDecl *D) { 697219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(D->getType()); 698219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F = llvm::Function::Create(cast<llvm::FunctionType>(Ty), 699219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function::ExternalLinkage, 7005f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor getMangledName(D)->getName(), 7015f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor &getModule()); 702219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributes(D, F); 703219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar return F; 704bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 705bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 706bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfFunction(const FunctionDecl *D) { 7079986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar QualType ASTTy = D->getType(); 7089986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy); 7099986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar const llvm::Type *PTy = llvm::PointerType::get(Ty, ASTTy.getAddressSpace()); 7103c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 7113c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // Lookup the entry, lazily creating it if necessary. 7125f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 7133c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) 7143c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry = EmitForwardFunctionDefinition(D); 715bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 7169986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar return llvm::ConstantExpr::getBitCast(Entry, PTy); 717bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 718bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 719bd012ff1fa088181646a784f385b28867372d434Daniel Dunbarvoid CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { 7205f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor llvm::GlobalValue *&Entry = GlobalDeclMap[getMangledName(D)]; 7213c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (!Entry) { 722bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Entry = EmitForwardFunctionDefinition(D); 723bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } else { 7243c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar // If the types mismatch then we have to rewrite the definition. 7253c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(D->getType()); 7263c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar if (Entry->getType() != llvm::PointerType::getUnqual(Ty)) { 727bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Otherwise, we have a definition after a prototype with the wrong type. 728bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // F is the Function* for the one with the wrong type, we must make a new 729bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Function* and update everything that used F (a declaration) with the new 730bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Function* (which will be a definition). 731bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // 732bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // This happens if there is a prototype for a function (e.g. "int f()") and 733bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // then a definition of a different type (e.g. "int f(int x)"). Start by 734bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // making a new function of the correct type, RAUW, then steal the name. 7353c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::GlobalValue *NewFn = EmitForwardFunctionDefinition(D); 7363c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar NewFn->takeName(Entry); 737bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 738bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Replace uses of F with the Function we will endow with a body. 739bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar llvm::Constant *NewPtrForOldDecl = 7403c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar llvm::ConstantExpr::getBitCast(NewFn, Entry->getType()); 7413c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar Entry->replaceAllUsesWith(NewPtrForOldDecl); 7423c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar 743bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar // Ok, delete the old function now, which is dead. 7443c827a79cb7d04c255db8080e682ee2c6912373dDaniel Dunbar assert(Entry->isDeclaration() && "Shouldn't replace non-declaration"); 745219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar Entry->eraseFromParent(); 746bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 747bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar Entry = NewFn; 748bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 749bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 750bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 751219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *Fn = cast<llvm::Function>(Entry); 752219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar CodeGenFunction(*this).GenerateCode(D, Fn); 7536379a7a15335e0af543a942efe9cfd514a83dab8Daniel Dunbar 754219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar SetFunctionAttributesForDefinition(D, Fn); 755219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 756219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) { 757219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalCtor(Fn, CA->getPriority()); 758219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar } else if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) { 759219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar AddGlobalDtor(Fn, DA->getPriority()); 760bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar } 761bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar} 762bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 763f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbarllvm::Function * 764f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel DunbarCodeGenModule::CreateRuntimeFunction(const llvm::FunctionType *FTy, 765f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name) { 766f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function *Fn = llvm::Function::Create(FTy, 767f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar llvm::Function::ExternalLinkage, 768f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar "", &TheModule); 769f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar RuntimeFunctions.push_back(std::make_pair(Fn, Name)); 770f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar return Fn; 771f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar} 772f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 773c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattnervoid CodeGenModule::UpdateCompletedType(const TagDecl *TD) { 774c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner // Make sure that this type is translated. 775c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner Types.UpdateCompletedType(TD); 776d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner} 777d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 778d86e6bc7ab4388a578daf46e7c76be9122a25072Chris Lattner 779bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner/// getBuiltinLibFunction 780bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Function *CodeGenModule::getBuiltinLibFunction(unsigned BuiltinID) { 7811426fecdf90dd1986751b9940422e675880ff671Chris Lattner if (BuiltinID > BuiltinFunctions.size()) 7821426fecdf90dd1986751b9940422e675880ff671Chris Lattner BuiltinFunctions.resize(BuiltinID); 783bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 7841426fecdf90dd1986751b9940422e675880ff671Chris Lattner // Cache looked up functions. Since builtin id #0 is invalid we don't reserve 7851426fecdf90dd1986751b9940422e675880ff671Chris Lattner // a slot for it. 7861426fecdf90dd1986751b9940422e675880ff671Chris Lattner assert(BuiltinID && "Invalid Builtin ID"); 7871426fecdf90dd1986751b9940422e675880ff671Chris Lattner llvm::Function *&FunctionSlot = BuiltinFunctions[BuiltinID-1]; 788bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner if (FunctionSlot) 789bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner return FunctionSlot; 790bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 791bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner assert(Context.BuiltinInfo.isLibFunction(BuiltinID) && "isn't a lib fn"); 792bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 793bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the name, skip over the __builtin_ prefix. 794bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const char *Name = Context.BuiltinInfo.GetName(BuiltinID)+10; 795bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 796bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // Get the type for the builtin. 797bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner QualType Type = Context.BuiltinInfo.GetBuiltinType(BuiltinID, Context); 798bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner const llvm::FunctionType *Ty = 799bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner cast<llvm::FunctionType>(getTypes().ConvertType(Type)); 800bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 801bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: This has a serious problem with code like this: 802bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // void abs() {} 803bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // ... __builtin_abs(x); 804bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // The two versions of abs will collide. The fix is for the builtin to win, 805bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // and for the existing one to be turned into a constantexpr cast of the 806bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // builtin. In the case where the existing one is a static function, it 807bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // should just be renamed. 808c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (llvm::Function *Existing = getModule().getFunction(Name)) { 809c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner if (Existing->getFunctionType() == Ty && Existing->hasExternalLinkage()) 810c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner return FunctionSlot = Existing; 811c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner assert(Existing == 0 && "FIXME: Name collision"); 812c5e940fa551840ecd71e8116c316c9131490f5faChris Lattner } 813bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 814bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner // FIXME: param attributes for sext/zext etc. 8154c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman return FunctionSlot = 8164c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman llvm::Function::Create(Ty, llvm::Function::ExternalLinkage, Name, 8174c13b7a3973d2d263d9682d7b68fbfeb76334af5Nate Begeman &getModule()); 818bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner} 819bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 8207acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattnerllvm::Function *CodeGenModule::getIntrinsic(unsigned IID,const llvm::Type **Tys, 8217acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys) { 8227acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner return llvm::Intrinsic::getDeclaration(&getModule(), 8237acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner (llvm::Intrinsic::ID)IID, Tys, NumTys); 8247acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner} 825bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattner 8265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::Function *CodeGenModule::getMemCpyFn() { 8275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (MemCpyFn) return MemCpyFn; 8284e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 8294e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemCpyFn = getIntrinsic(llvm::Intrinsic::memcpy, &IntPtr, 1); 8305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 831c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 8320c99509927a0c7a48490486b9fec287b63e5c09cEli Friedmanllvm::Function *CodeGenModule::getMemMoveFn() { 8330c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman if (MemMoveFn) return MemMoveFn; 8344e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 8354e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemMoveFn = getIntrinsic(llvm::Intrinsic::memmove, &IntPtr, 1); 8360c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman} 8370c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman 83841ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venanciollvm::Function *CodeGenModule::getMemSetFn() { 83941ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio if (MemSetFn) return MemSetFn; 8404e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner const llvm::Type *IntPtr = TheTargetData.getIntPtrType(); 8414e8a9e8640a6717120394ee2ee5f27989757754dChris Lattner return MemSetFn = getIntrinsic(llvm::Intrinsic::memset, &IntPtr, 1); 84241ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio} 8437acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner 844e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlssonstatic void appendFieldAndPadding(CodeGenModule &CGM, 845e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*>& Fields, 84644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *FieldD, FieldDecl *NextFieldD, 84744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::Constant* Field, 848e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl* RD, const llvm::StructType *STy) 849e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson{ 850e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append the field. 851e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(Field); 852e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 85344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor int StructFieldNo = CGM.getTypes().getLLVMFieldNo(FieldD); 854e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 855e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson int NextStructFieldNo; 85644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (!NextFieldD) { 857e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson NextStructFieldNo = STy->getNumElements(); 858e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } else { 85944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextStructFieldNo = CGM.getTypes().getLLVMFieldNo(NextFieldD); 860e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 861e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 862e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson // Append padding 863e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson for (int i = StructFieldNo + 1; i < NextStructFieldNo; i++) { 864e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant *C = 865e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::Constant::getNullValue(STy->getElementType(StructFieldNo + 1)); 866e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 867e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson Fields.push_back(C); 868e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson } 869e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson} 870e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 8713e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// We still need to work out the details of handling UTF-16. 8723e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar// See: <rdr://2996215> 873bef20ac367a09555b30d6eb3847a81ec164caf88Chris Lattnerllvm::Constant *CodeGenModule:: 874bef20ac367a09555b30d6eb3847a81ec164caf88Chris LattnerGetAddrOfConstantCFString(const std::string &str) { 875c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::StringMapEntry<llvm::Constant *> &Entry = 876c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson CFConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 877c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 878c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (Entry.getValue()) 879c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson return Entry.getValue(); 880c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 8813e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); 8823e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *Zeros[] = { Zero, Zero }; 883c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 884c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson if (!CFConstantStringClassRef) { 885c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson const llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 886c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = llvm::ArrayType::get(Ty, 0); 8873e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 8883e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // FIXME: This is fairly broken if 8893e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // __CFConstantStringClassReference is already defined, in that it 8903e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // will get renamed and the user will most likely see an opaque 8913e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // error message. This is a general issue with relying on 8923e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // particular names. 8933e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::GlobalVariable *GV = 894c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson new llvm::GlobalVariable(Ty, false, 895c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalVariable::ExternalLinkage, 0, 896c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson "__CFConstantStringClassReference", 897c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson &getModule()); 8983e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 8993e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar // Decay array -> ptr 9003e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar CFConstantStringClassRef = 9013e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::ConstantExpr::getGetElementPtr(GV, Zeros, 2); 902c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson } 903c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 904e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson QualType CFTy = getContext().getCFConstantStringType(); 905e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson RecordDecl *CFRD = CFTy->getAsRecordType()->getDecl(); 9063e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 907e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson const llvm::StructType *STy = 908e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 909e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson 910e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson std::vector<llvm::Constant*> Fields; 91144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor RecordDecl::field_iterator Field = CFRD->field_begin(); 91244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 913c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Class pointer. 91444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *CurField = *Field++; 91544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor FieldDecl *NextField = *Field++; 91644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 91744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CFConstantStringClassRef, CFRD, STy); 918c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 919c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // Flags. 92044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 92144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 9223e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar const llvm::Type *Ty = getTypes().ConvertType(getContext().UnsignedIntTy); 92344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 92444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, 0x07C8), CFRD, STy); 925c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 926c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String pointer. 92744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 92844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = *Field++; 9293e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str); 930c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson C = new llvm::GlobalVariable(C->getType(), true, 931c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::GlobalValue::InternalLinkage, 932e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C, ".str", &getModule()); 93344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 934e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2), 935e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson CFRD, STy); 936c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 937c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // String length. 93844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor CurField = NextField; 93944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor NextField = 0; 940c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson Ty = getTypes().ConvertType(getContext().LongTy); 94144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor appendFieldAndPadding(*this, Fields, CurField, NextField, 94244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor llvm::ConstantInt::get(Ty, str.length()), CFRD, STy); 943c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson 944c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson // The struct. 945e3daa761061982f2267f7c8fb847ea02abad0aa9Anders Carlsson C = llvm::ConstantStruct::get(STy, Fields); 9460c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable *GV = 9470c67829763b98bc670062b553897a851fab17401Anders Carlsson new llvm::GlobalVariable(C->getType(), true, 9480c67829763b98bc670062b553897a851fab17401Anders Carlsson llvm::GlobalVariable::InternalLinkage, 9490c67829763b98bc670062b553897a851fab17401Anders Carlsson C, "", &getModule()); 9503e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 9510c67829763b98bc670062b553897a851fab17401Anders Carlsson GV->setSection("__DATA,__cfstring"); 9520c67829763b98bc670062b553897a851fab17401Anders Carlsson Entry.setValue(GV); 9533e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 9540c67829763b98bc670062b553897a851fab17401Anders Carlsson return GV; 955c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson} 95645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 9576143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetStringForStringLiteral - Return the appropriate bytes for a 9581e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar/// string literal, properly padded to match the literal type. 9596143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarstd::string CodeGenModule::GetStringForStringLiteral(const StringLiteral *E) { 960662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar if (E->isWide()) { 961662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar ErrorUnsupported(E, "wide string"); 962662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar return "FIXME"; 963662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar } 964662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar 9651e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const char *StrData = E->getStrData(); 9661e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar unsigned Len = E->getByteLength(); 9671e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 9681e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar const ConstantArrayType *CAT = 9691e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar getContext().getAsConstantArrayType(E->getType()); 9701e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar assert(CAT && "String isn't pointer or array!"); 9711e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 9721e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar // Resize the string to the right size 9731e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar // FIXME: What about wchar_t strings? 9741e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar std::string Str(StrData, StrData+Len); 9751e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar uint64_t RealLen = CAT->getSize().getZExtValue(); 9761e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar Str.resize(RealLen, '\0'); 9771e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 9781e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar return Str; 9791e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar} 9801e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 9816143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantStringFromLiteral - Return a pointer to a 9826143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// constant array for the given string literal. 9836143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbarllvm::Constant * 9846143293fa4366ee95d7e47e61bd030a34bf68b55Daniel DunbarCodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { 9856143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // FIXME: This can be more efficient. 9866143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar return GetAddrOfConstantString(GetStringForStringLiteral(S)); 9876143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 9886143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 989a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner/// GenerateWritableString -- Creates storage for a string literal. 99045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattnerstatic llvm::Constant *GenerateStringLiteral(const std::string &str, 99145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner bool constant, 9925fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar CodeGenModule &CGM, 9935fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 9946143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar // Create Constant for this string literal. Don't add a '\0'. 9956143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar llvm::Constant *C = llvm::ConstantArray::get(str, false); 99645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 99745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this string 99845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner C = new llvm::GlobalVariable(C->getType(), constant, 99945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::GlobalValue::InternalLinkage, 10005fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar C, 10015fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar GlobalName ? GlobalName : ".str", 10025fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar &CGM.getModule()); 10036143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 100445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 100545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 100645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 10076143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantString - Returns a pointer to a character array 10086143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// containing the literal. This contents are exactly that of the 10096143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// given string, i.e. it will not be null terminated automatically; 10106143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// see GetAddrOfConstantCString. Note that whether the result is 10116143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// actually a pointer to an LLVM constant depends on 10126143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// Feature.WriteableStrings. 10136143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// 10146143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// The result has pointer to array type. 10155fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, 10165fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName) { 101745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Don't share any string literals if writable-strings is turned on. 101845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Features.WritableStrings) 10195fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar return GenerateStringLiteral(str, false, *this, GlobalName); 102045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 102145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::StringMapEntry<llvm::Constant *> &Entry = 102245e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); 102345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 102445e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner if (Entry.getValue()) 102545e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return Entry.getValue(); 102645e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner 102745e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner // Create a global variable for this. 10285fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *C = GenerateStringLiteral(str, true, *this, GlobalName); 102945e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner Entry.setValue(C); 103045e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner return C; 103145e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner} 10326143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 10336143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// GetAddrOfConstantCString - Returns a pointer to a character 10346143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// array containing the literal and a terminating '\-' 10356143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar/// character. The result has pointer to array type. 10365fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbarllvm::Constant *CodeGenModule::GetAddrOfConstantCString(const std::string &str, 10375fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName){ 1038c9f29c61856ffb5f643cedbe87ac076f21a1381aChris Lattner return GetAddrOfConstantString(str + '\0', GlobalName); 10396143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar} 104041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1041af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// EmitObjCPropertyImplementations - Emit information for synthesized 1042af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar/// properties for an implementation. 1043af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbarvoid CodeGenModule::EmitObjCPropertyImplementations(const 1044af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *D) { 1045af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar for (ObjCImplementationDecl::propimpl_iterator i = D->propimpl_begin(), 1046af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar e = D->propimpl_end(); i != e; ++i) { 1047af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyImplDecl *PID = *i; 1048af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1049af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Dynamic is just for type-checking. 1050af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1051af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1052af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 1053af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Determine which methods need to be implemented, some may have 1054af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // been overridden. Note that ::isSynthesized is not the method 1055af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // we want, that just indicates if the decl came from a 1056af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // property. What we want to know is if the method is defined in 1057af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // this implementation. 1058af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!D->getInstanceMethod(PD->getGetterName())) 1059fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCGetter( 1060fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1061af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar if (!PD->isReadOnly() && 1062af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar !D->getInstanceMethod(PD->getSetterName())) 1063fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian CodeGenFunction(*this).GenerateObjCSetter( 1064fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian const_cast<ObjCImplementationDecl *>(D), PID); 1065af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1066af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 1067af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar} 1068af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 106941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar/// EmitTopLevelDecl - Emit code for a single top level declaration. 107041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbarvoid CodeGenModule::EmitTopLevelDecl(Decl *D) { 107141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If an error has occurred, stop code generation, but continue 107241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // parsing and semantic analysis (to ensure all warnings and errors 107341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // are emitted). 107441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (Diags.hasErrorOccurred()) 107541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar return; 107641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 107741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar switch (D->getKind()) { 107841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Function: 107941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Var: 108041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar EmitGlobal(cast<ValueDecl>(D)); 108141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 108241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 108341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::Namespace: 1084662174c82ef46b19a2329c7d37208e1d12dfb7b3Daniel Dunbar ErrorUnsupported(D, "namespace"); 108541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 108641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 108741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Objective-C Decls 108841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 108941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Forward declarations, no (immediate) code generation. 109041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCClass: 109141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategory: 109241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCForwardProtocol: 109341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCInterface: 109441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 109541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 109641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCProtocol: 109741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateProtocol(cast<ObjCProtocolDecl>(D)); 109841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 109941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 110041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCategoryImpl: 1101af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // Categories have properties but don't support synthesize so we 1102af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar // can ignore them here. 1103af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar 110441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar Runtime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 110541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 110641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 1107af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar case Decl::ObjCImplementation: { 1108af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar ObjCImplementationDecl *OMD = cast<ObjCImplementationDecl>(D); 1109af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar EmitObjCPropertyImplementations(OMD); 1110af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar Runtime->GenerateClass(OMD); 111141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 1112af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar } 111341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCMethod: { 111441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(D); 111541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // If this is not a prototype, emit the body. 111641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (OMD->getBody()) 111741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar CodeGenFunction(*this).GenerateObjCMethod(OMD); 111841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 111941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 112041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::ObjCCompatibleAlias: 1121305c658ebce84bb9833fc0e7675554656453b8e8Fariborz Jahanian // compatibility-alias is a directive and has no code gen. 112241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 112341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 112441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::LinkageSpec: { 112541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar LinkageSpecDecl *LSD = cast<LinkageSpecDecl>(D); 112641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (LSD->getLanguage() == LinkageSpecDecl::lang_cxx) 1127488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar ErrorUnsupported(LSD, "linkage spec"); 112841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // FIXME: implement C++ linkage, C linkage works mostly by C 112941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // language reuse already. 113041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 113141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 113241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 113341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar case Decl::FileScopeAsm: { 113441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar FileScopeAsmDecl *AD = cast<FileScopeAsmDecl>(D); 113541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar std::string AsmString(AD->getAsmString()->getStrData(), 113641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar AD->getAsmString()->getByteLength()); 113741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 113841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar const std::string &S = getModule().getModuleInlineAsm(); 113941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar if (S.empty()) 114041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(AsmString); 114141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar else 114241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar getModule().setModuleInlineAsm(S + '\n' + AsmString); 114341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar break; 114441071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 114541071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 114641071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar default: 114741071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // Make sure we handled everything we should, every other kind is 114841071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // a non-top-level decl. FIXME: Would be nice to have an 114941071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // isTopLevelDeclKind function. Need to recode Decl::Kind to do 115041071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar // that easily. 115141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 115241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar } 115341071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar} 1154