CodeGenModule.h revision 43af76e845c95be160c088ec11ba3c43e2527fa8
1bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===// 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// 10ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump// This is the internal per-translation-unit state used for llvm translation. 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 14ef52a2fb2ace36c92f88c6e125bd7defa17dafa5Chris Lattner#ifndef CLANG_CODEGEN_CODEGENMODULE_H 15ef52a2fb2ace36c92f88c6e125bd7defa17dafa5Chris Lattner#define CLANG_CODEGEN_CODEGENMODULE_H 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "CodeGenTypes.h" 184f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman#include "clang/AST/Attr.h" 195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/DenseMap.h" 20c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson#include "llvm/ADT/StringMap.h" 216ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor#include "llvm/ADT/StringSet.h" 225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 2396bd13ae1ab89f5a04b3ad3ec8ec1864cbba3482Mike Stump#include "CGBlocks.h" 24b768807c49a1c7085def099b848631856af766faDaniel Dunbar#include "CGCall.h" 25b768807c49a1c7085def099b848631856af766faDaniel Dunbar 26b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson#include <list> 27b723f7520bcce5f13ccaae557c16a1e7133b6908Anders Carlsson 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace llvm { 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer class Module; 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer class Constant; 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer class Function; 328bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman class GlobalValue; 337a4718e813e5e99d478567a482217c7eef8572c5Devang Patel class TargetData; 34ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman class FunctionType; 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang { 385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer class ASTContext; 395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer class FunctionDecl; 4090db88249ac92e6ed515065048a4ead4467e6639Daniel Dunbar class IdentifierInfo; 41391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner class ObjCMethodDecl; 4220ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov class ObjCImplementationDecl; 4320ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov class ObjCCategoryImplDecl; 4420ff3108fcd2c3bd734dc79efc22ebaa090abd41Anton Korobeynikov class ObjCProtocolDecl; 45eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner class ObjCEncodeExpr; 46d5cab5435371b8cc74a9e05ebd40b5995ebad149Anders Carlsson class BlockExpr; 475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer class Decl; 482824723d6d181d2dfa56e62caabd68b0b18f0b9dOliver Hunt class Expr; 492c8569d5f43f7de1fb993e84c95ea571dd9ac55eChris Lattner class Stmt; 501e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar class StringLiteral; 511a1d92ac5e1354634af08c62a1d72e263d13be0cNate Begeman class NamedDecl; 52bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar class ValueDecl; 532b9d2ca4ce53fffbe8a77c7db2fe4df5ccfb9fd4Chris Lattner class VarDecl; 5443af76e845c95be160c088ec11ba3c43e2527fa8Sebastian Redl class LangOptions; 55fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner class Diagnostic; 568bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman class AnnotateAttr; 57ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace CodeGen { 595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 60813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio class CodeGenFunction; 61e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta class CGDebugInfo; 62af2f62ce32e462f256855cd24b06dec4755d2827Daniel Dunbar class CGObjCRuntime; 63ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 64ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump/// CodeGenModule - This class organizes the cross-function state that is used 65ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump/// while generating LLVM code. 662a998148a6823c44d67da347c95eb2ea21f6b986Mike Stumpclass CodeGenModule : public BlockModule { 678a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson CodeGenModule(const CodeGenModule&); // DO NOT IMPLEMENT 688a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson void operator=(const CodeGenModule&); // DO NOT IMPLEMENT 698a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson 706bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar typedef std::vector< std::pair<llvm::Constant*, int> > CtorList; 716bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ASTContext &Context; 7345e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner const LangOptions &Features; 745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::Module &TheModule; 757a4718e813e5e99d478567a482217c7eef8572c5Devang Patel const llvm::TargetData &TheTargetData; 76fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner Diagnostic &Diags; 775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CodeGenTypes Types; 78815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek CGObjCRuntime* Runtime; 79815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek CGDebugInfo* DebugInfo; 805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::Function *MemCpyFn; 820c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman llvm::Function *MemMoveFn; 8341ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio llvm::Function *MemSetFn; 849986eabd4423d867262c358ca62f94a60ac58412Daniel Dunbar 85b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar /// RuntimeGlobal - List of runtime globals whose names must be 86b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar /// protected from introducing conflicts. These globals should be 87b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar /// created unnamed, we will name them and patch up conflicts when 88b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar /// we release the module. 89b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar std::vector< std::pair<llvm::GlobalValue*, std::string> > RuntimeGlobals; 90f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 916ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// GlobalDeclMap - Mapping of decl names (represented as unique 926ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// character pointers from either the identifier table or the set 936ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// of mangled names) to global variables we have already 946ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// emitted. Note that the entries in this map are the actual 956ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// globals and therefore may not be of the same type as the decl, 966ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// they should be bitcasted on retrieval. Also note that the 976ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// globals are keyed on their source name, not the global name 986ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// (which may change with attributes such as asm-labels). This key 996ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// to this map should be generated using getMangledName(). 1006ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::DenseMap<const char*, llvm::GlobalValue*> GlobalDeclMap; 1016ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor 1026ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// \brief Contains the strings used for mangled names. 1036ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// 1046ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// FIXME: Eventually, this should map from the semantic/canonical 1056ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// declaration for each global entity to its mangled name (if it 1066ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor /// has one). 1076ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor llvm::StringSet<> MangledNames; 108bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 109ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// Aliases - List of aliases in module. These cannot be emitted until all the 110ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// code has been seen, as they reference things by name instead of directly 111ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// and may reference forward. 112219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar std::vector<const FunctionDecl*> Aliases; 113219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar 1140269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// DeferredDecls - List of decls for which code generation has been 1150269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// deferred. When the translation unit has been fully processed we 1160269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// will lazily emit definitions for only the decls that were 1170269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// actually used. This should contain only Function and Var decls, 1180269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// and only those which actually define something. 1190269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar std::list<const ValueDecl*> DeferredDecls; 1200269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 1210269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// LLVMUsed - List of global values which are required to be 1220269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// present in the object file; bitcast to i8*. This is used for 1230269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// forcing visibility of symbols which may otherwise be optimized 1240269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// out. 1250269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar std::vector<llvm::Constant*> LLVMUsed; 126ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 127ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// GlobalCtors - Store the list of global constructors and their respective 128ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// priorities to be emitted when the translation unit is complete. 1296bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar CtorList GlobalCtors; 1306bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 131ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// GlobalDtors - Store the list of global destructors and their respective 132ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// priorities to be emitted when the translation unit is complete. 1336bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar CtorList GlobalDtors; 1346bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 135532485cc6c078d9e51b517c6bbd8984deb17f0feNate Begeman std::vector<llvm::Constant*> Annotations; 136ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 137c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::StringMap<llvm::Constant*> CFConstantStringMap; 13845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner llvm::StringMap<llvm::Constant*> ConstantStringMap; 1393e9df9920db8de8ec93a424b0c1784f9bff301eaDaniel Dunbar 140ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// CFConstantStringClassRef - Cached reference to the class for constant 141ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// strings. This value has type int * but is actually an Obj-C class pointer. 142c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::Constant *CFConstantStringClassRef; 143ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 144c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump std::vector<llvm::Value *> BuiltinFunctions; 1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic: 146ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M, 147e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta const llvm::TargetData &TD, Diagnostic &Diags, 148f77ac86f4eca528a04b817d7ad7f045a47d52712Daniel Dunbar bool GenerateDebugInfo); 149815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek 1502b94fe35edf951a14ecd32b21f7ebcc2e3754c67Chris Lattner ~CodeGenModule(); 151ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 152815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek /// Release - Finalize LLVM code generation. 153815c78fd9ab8bd5dfe8e8a91b8c6a413e2b8c889Ted Kremenek void Release(); 154208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 155208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar /// getObjCRuntime() - Return a reference to the configured 156208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar /// Objective-C runtime. 157ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump CGObjCRuntime &getObjCRuntime() { 158208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar assert(Runtime && "No Objective-C runtime has been configured."); 159ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump return *Runtime; 160208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar } 161ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 162208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar /// hasObjCRuntime() - Return true iff an Objective-C runtime has 163208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar /// been configured. 164208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar bool hasObjCRuntime() { return !!Runtime; } 165208ff5e8a073de2a5d15cbe03cab8a4c0d935e28Daniel Dunbar 166e8b9f5b8ea60983c4a74cb8b63879616b914b65aSanjiv Gupta CGDebugInfo *getDebugInfo() { return DebugInfo; } 1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ASTContext &getContext() const { return Context; } 16845e8cbdce25c2e16c7aac2036a591f6190097ae6Chris Lattner const LangOptions &getLangOptions() const { return Features; } 1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::Module &getModule() const { return TheModule; } 1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer CodeGenTypes &getTypes() { return Types; } 171fb97b03e42d397405f617be0252be83e77a66f6eChris Lattner Diagnostic &getDiags() const { return Diags; } 1728f925280abe3795ae0fb47f2ba90faea62e26af4Chris Lattner const llvm::TargetData &getTargetData() const { return TheTargetData; } 173bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 174ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 175ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// given global variable. 176bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D); 177bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 178ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// GetAddrOfFunction - Return the llvm::Constant for the address of the given 179ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// function. 180ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump llvm::Constant *GetAddrOfFunction(const FunctionDecl *D); 1816143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 182ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// GetStringForStringLiteral - Return the appropriate bytes for a string 183ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// literal, properly padded to match the literal type. If only the address of 184ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// a constant is needed consider using GetAddrOfConstantStringLiteral. 1856143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar std::string GetStringForStringLiteral(const StringLiteral *E); 1866143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 187ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// GetAddrOfConstantCFString - Return a pointer to a constant CFString object 188ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// for the given string. 189c9e2091efcb535110474434dd12015afdc3b1637Anders Carlsson llvm::Constant *GetAddrOfConstantCFString(const std::string& str); 190a7ad98ff0919d6a24ea7c46634ea29bea551c1a0Chris Lattner 191ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// GetAddrOfConstantStringFromLiteral - Return a pointer to a constant array 192ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// for the given string literal. 1936143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S); 1941e04976fc2611d8cc06986a81deed4c42183b870Daniel Dunbar 195eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 196eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner /// array for the given ObjCEncodeExpr node. 197eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *); 198eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner 1996143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar /// GetAddrOfConstantString - Returns a pointer to a character array 200ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// containing the literal. This contents are exactly that of the given 201ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// string, i.e. it will not be null terminated automatically; see 202ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// GetAddrOfConstantCString. Note that whether the result is actually a 203ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// pointer to an LLVM constant depends on Feature.WriteableStrings. 2046143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar /// 2056143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar /// The result has pointer to array type. 2065fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar /// 2075fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar /// \param GlobalName If provided, the name to use for the global 2085fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar /// (if one is created). 2095fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *GetAddrOfConstantString(const std::string& str, 2105fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName=0); 2116143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 212ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// GetAddrOfConstantCString - Returns a pointer to a character array 213ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// containing the literal and a terminating '\0' character. The result has 214ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// pointer to array type. 2155fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar /// 216ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// \param GlobalName If provided, the name to use for the global (if one is 217ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// created). 2185fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar llvm::Constant *GetAddrOfConstantCString(const std::string &str, 2195fabf9dbee29464bcd06cd09f8e569d1b850f948Daniel Dunbar const char *GlobalName=0); 220ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 22141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar /// getBuiltinLibFunction - Given a builtin id for a function like 22241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar /// "__builtin_fabsf", return a Function* for "fabsf". 223c136e6cf237711f9f1324637a0b2cdf6ae8e79e4Mike Stump llvm::Value *getBuiltinLibFunction(unsigned BuiltinID); 2246143293fa4366ee95d7e47e61bd030a34bf68b55Daniel Dunbar 2255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer llvm::Function *getMemCpyFn(); 2260c99509927a0c7a48490486b9fec287b63e5c09cEli Friedman llvm::Function *getMemMoveFn(); 22741ef30e869d3f4940437503eb6a2cf70819fdb08Lauro Ramos Venancio llvm::Function *getMemSetFn(); 228ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0, 2297acda7c4a0e4aec6c003b3169ca45a5f3bc7e033Chris Lattner unsigned NumTys = 0); 230bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 23141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar /// EmitTopLevelDecl - Emit code for a single top level declaration. 23241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar void EmitTopLevelDecl(Decl *D); 233bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 2340269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// AddUsedGlobal - Add a global which should be forced to be 2350269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// present in the object file; these are emitted to the llvm.used 2360269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// metadata global. 2370269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar void AddUsedGlobal(llvm::GlobalValue *GV); 2380269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 239bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); } 240bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 241ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// CreateRuntimeFunction - Create a new runtime function whose name must be 242ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// protected from collisions. 243ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump llvm::Function *CreateRuntimeFunction(const llvm::FunctionType *Ty, 244f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar const std::string &Name); 245b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar /// CreateRuntimeVariable - Create a new runtime global variable 246b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar /// whose name must be protected from collisions. 247b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar llvm::GlobalVariable *CreateRuntimeVariable(const llvm::Type *Ty, 248b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar const std::string &Name); 249f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 250c5b8806cda286cf41866176ef98011fdaa68da01Chris Lattner void UpdateCompletedType(const TagDecl *D); 251d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar 252d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar /// EmitConstantExpr - Try to emit the given expression as a 253d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar /// constant; returns 0 if the expression cannot be emitted as a 254d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar /// constant. 255813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0); 256d60f2fbf69df08e952377b498c036f269395024dDaniel Dunbar 2578bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV, 2588bd4afeb876fd0015bb808eb2f3de1fe709658a7Nate Begeman const AnnotateAttr *AA, unsigned LineNo); 259ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 260488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar /// ErrorUnsupported - Print out an error that codegen doesn't support the 26190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar /// specified stmt yet. 262ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// \param OmitOnError - If true, then this error should only be emitted if no 263ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// other errors have been reported. 264ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump void ErrorUnsupported(const Stmt *S, const char *Type, 26590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError=false); 266ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 267488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar /// ErrorUnsupported - Print out an error that codegen doesn't support the 268c6fdc34ac0183bfa03d65f317c78b7bdac52897eChris Lattner /// specified decl yet. 269ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// \param OmitOnError - If true, then this error should only be emitted if no 270ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// other errors have been reported. 27190df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar void ErrorUnsupported(const Decl *D, const char *Type, 27290df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar bool OmitOnError=false); 2734f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman 274f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar void SetMethodAttributes(const ObjCMethodDecl *MD, 275f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F); 276f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 277761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel void SetFunctionAttributes(const Decl *D, 278ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump const CGFunctionInfo &Info, 279b768807c49a1c7085def099b848631856af766faDaniel Dunbar llvm::Function *F); 280b768807c49a1c7085def099b848631856af766faDaniel Dunbar 281ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// ReturnTypeUsesSret - Return true iff the given type uses 'sret' when used 282ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// as a return type. 28388b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar bool ReturnTypeUsesSret(const CGFunctionInfo &FI); 284b768807c49a1c7085def099b848631856af766faDaniel Dunbar 28588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar void ConstructAttributeList(const CGFunctionInfo &Info, 28688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar const Decl *TargetDecl, 287761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel AttributeListType &PAL); 288b768807c49a1c7085def099b848631856af766faDaniel Dunbar 2896ec3668a2608b63473207319f5ceff9bbd22ea51Douglas Gregor const char *getMangledName(const NamedDecl *ND); 2905f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 2915f2bfd4811996abb783aa6c7254c56baa6930e8cDouglas Gregor 2929cd4fe4af6d3e8b6dcfce9c2cdefcaafca7eed7cChris Lattnerprivate: 2935c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar /// SetGlobalValueAttributes - Set attributes for a global decl. 2945c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar void SetGlobalValueAttributes(const Decl *D, 2955c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInternal, 2965c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool IsInline, 2975c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar llvm::GlobalValue *GV, 2985c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar bool ForDefinition); 2995c61d97ad442b2c0bbecb617c8f21857ce1fff6dDaniel Dunbar 300ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// SetFunctionAttributesForDefinition - Set function attributes specific to a 301ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// function definition. 302219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar /// \param D - The ObjCMethodDecl or FunctionDecl defining \arg F. 303219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar void SetFunctionAttributesForDefinition(const Decl *D, 304219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar llvm::Function *F); 305f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar 306ff4a2d9e2c1ddbf87e00e2a36ae341faf03eafb3Eli Friedman void SetFunctionAttributes(const FunctionDecl *FD, 307f80519b919a348db004fba18530706314d1ebfb5Daniel Dunbar llvm::Function *F); 308d4cbda6292b321c2e7dce7f039d92918fee99b3aNuno Lopes 309ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// EmitGlobal - Emit code for a singal global function or var decl. Forward 310ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// declarations are emitted lazily. 31141071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar void EmitGlobal(const ValueDecl *D); 31241071debe9b8887449c3f2ee0dd7124ed47bdda8Daniel Dunbar 313bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar void EmitGlobalDefinition(const ValueDecl *D); 314d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 315d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar /// EmitForwardFunctionDefinition - Create a new function for the 316d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar /// given decl and set attributes as appropriate. 317d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar /// 318d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar /// \arg Ty - If non-null the LLVM function type to use for the 319d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar /// decl; it is the callers responsibility to make sure this is 320d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar /// compatible with the correct type. 321d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D, 322d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar const llvm::Type *Ty); 323d5d31801fc87239436fa349c89dce7797cf13537Daniel Dunbar 324bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar void EmitGlobalFunctionDefinition(const FunctionDecl *D); 325bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar void EmitGlobalVarDefinition(const VarDecl *D); 326af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D); 327ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump 3286bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar // FIXME: Hardcoding priority here is gross. 3296bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535); 3306bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535); 3316bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar 332ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// EmitCtorList - Generates a global array of functions and priorities using 333ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// the given list and name. This array will have appending linkage and is 334ecc90e9512b2ddaced6cb02a08f933fc7afa8e3fMike Stump /// suitable for use as a LLVM constructor or destructor array. 3356bfed7e411adc46eaf616371f85f68305c6e6257Daniel Dunbar void EmitCtorList(const CtorList &Fns, const char *GlobalName); 336bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar 337219df6644e2338ff067471ab0d85f27b88544ac2Daniel Dunbar void EmitAliases(void); 338bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar void EmitAnnotations(void); 3390269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 3400269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// EmitDeferred - Emit any needed decls for which code generation 3410269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// was deferred. 3420269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar void EmitDeferred(void); 3430269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar 3440269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// EmitLLVMUsed - Emit the llvm.used metadata used to force 3450269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar /// references to global which may otherwise be optimized out. 3460269871c9cba493f76237175ab60313406f3bafaDaniel Dunbar void EmitLLVMUsed(void); 347f1968f28869f4e0675450ae39c478a37c5b9abd6Daniel Dunbar 348b681b8ffab2aa016b3897916d5110927c34a584bDaniel Dunbar void BindRuntimeGlobals(); 34973241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar 35073241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar /// MayDeferGeneration - Determine if the given decl can be emitted 35173241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar /// lazily; this is only relevant for definitions. The given decl 35273241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar /// must be either a function or var decl. 35373241dfeb5c498255b662984cca369fd28ec3147Daniel Dunbar bool MayDeferGeneration(const ValueDecl *D); 3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}; 3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} // end namespace CodeGen 3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} // end namespace clang 3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif 359