Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is the internal per-translation-unit state used for llvm translation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | ef52a2f | 2008-02-29 17:10:38 +0000 | [diff] [blame] | 14 | #ifndef CLANG_CODEGEN_CODEGENMODULE_H |
| 15 | #define CLANG_CODEGEN_CODEGENMODULE_H |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | |
| 17 | #include "CodeGenTypes.h" |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/Attr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringMap.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
| 23 | class Module; |
| 24 | class Constant; |
| 25 | class Function; |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 26 | class GlobalValue; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 27 | class TargetData; |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 28 | class FunctionType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | namespace clang { |
| 32 | class ASTContext; |
| 33 | class FunctionDecl; |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 34 | class ObjCMethodDecl; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 35 | class ObjCImplementationDecl; |
| 36 | class ObjCCategoryImplDecl; |
| 37 | class ObjCProtocolDecl; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | class Decl; |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 39 | class Expr; |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 40 | class Stmt; |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 41 | class StringLiteral; |
Nate Begeman | 1a1d92a | 2008-04-20 20:38:08 +0000 | [diff] [blame] | 42 | class NamedDecl; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 43 | class ValueDecl; |
Chris Lattner | 2b9d2ca | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 44 | class VarDecl; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 45 | struct LangOptions; |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 46 | class Diagnostic; |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 47 | class AnnotateAttr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | |
| 49 | namespace CodeGen { |
| 50 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 51 | class CodeGenFunction; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 52 | class CGDebugInfo; |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 53 | class CGObjCRuntime; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 54 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | /// CodeGenModule - This class organizes the cross-module state that is used |
| 56 | /// while generating LLVM code. |
| 57 | class CodeGenModule { |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 58 | typedef std::vector< std::pair<llvm::Constant*, int> > CtorList; |
| 59 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | ASTContext &Context; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 61 | const LangOptions &Features; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 62 | llvm::Module &TheModule; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 63 | const llvm::TargetData &TheTargetData; |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 64 | Diagnostic &Diags; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | CodeGenTypes Types; |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 66 | CGObjCRuntime* Runtime; |
| 67 | CGDebugInfo* DebugInfo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | |
| 69 | llvm::Function *MemCpyFn; |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 70 | llvm::Function *MemMoveFn; |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 71 | llvm::Function *MemSetFn; |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 72 | |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 73 | /// GlobalDeclMap - Mapping of decl names global variables we have |
Daniel Dunbar | 9986eab | 2008-07-30 16:32:24 +0000 | [diff] [blame] | 74 | /// already emitted. Note that the entries in this map are the |
Daniel Dunbar | 3c827a7 | 2008-08-05 23:31:02 +0000 | [diff] [blame] | 75 | /// actual globals and therefore may not be of the same type as the |
| 76 | /// decl, they should be bitcasted on retrieval. Also note that the |
| 77 | /// globals are keyed on their source name, not the global name |
| 78 | /// (which may change with attributes such as asm-labels). |
| 79 | llvm::StringMap<llvm::GlobalValue*> GlobalDeclMap; |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 80 | |
| 81 | /// List of static global for which code generation is delayed. When |
| 82 | /// the translation unit has been fully processed we will lazily |
| 83 | /// emit definitions for only the decls that were actually used. |
| 84 | /// This should contain only Function and Var decls, and only those |
| 85 | /// which actually define something. |
| 86 | std::vector<const ValueDecl*> StaticDecls; |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 87 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 88 | /// GlobalCtors - Store the list of global constructors and their |
| 89 | /// respective priorities to be emitted when the translation unit is |
| 90 | /// complete. |
| 91 | CtorList GlobalCtors; |
| 92 | |
| 93 | /// GlobalDtors - Store the list of global destructors and their |
| 94 | /// respective priorities to be emitted when the translation unit is |
| 95 | /// complete. |
| 96 | CtorList GlobalDtors; |
| 97 | |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 98 | std::vector<llvm::Constant*> Annotations; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 99 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 100 | llvm::StringMap<llvm::Constant*> CFConstantStringMap; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 101 | llvm::StringMap<llvm::Constant*> ConstantStringMap; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 102 | llvm::Constant *CFConstantStringClassRef; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 103 | |
| 104 | std::vector<llvm::Function *> BuiltinFunctions; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | public: |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 106 | CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M, |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 107 | const llvm::TargetData &TD, Diagnostic &Diags, |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 108 | bool GenerateDebugInfo); |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 110 | ~CodeGenModule(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 112 | /// Release - Finalize LLVM code generation. |
| 113 | void Release(); |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 114 | |
| 115 | /// getObjCRuntime() - Return a reference to the configured |
| 116 | /// Objective-C runtime. |
| 117 | CGObjCRuntime &getObjCRuntime() { |
| 118 | assert(Runtime && "No Objective-C runtime has been configured."); |
| 119 | return *Runtime; |
| 120 | } |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 122 | /// hasObjCRuntime() - Return true iff an Objective-C runtime has |
| 123 | /// been configured. |
| 124 | bool hasObjCRuntime() { return !!Runtime; } |
| 125 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 126 | CGDebugInfo *getDebugInfo() { return DebugInfo; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | ASTContext &getContext() const { return Context; } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 128 | const LangOptions &getLangOptions() const { return Features; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | llvm::Module &getModule() const { return TheModule; } |
| 130 | CodeGenTypes &getTypes() { return Types; } |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 131 | Diagnostic &getDiags() const { return Diags; } |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 132 | const llvm::TargetData &getTargetData() const { return TheTargetData; } |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 133 | |
| 134 | /// GetAddrOfGlobalVar - Return the llvm::Constant for the address |
| 135 | /// of the given global variable. |
| 136 | llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D); |
| 137 | |
| 138 | /// GetAddrOfFunction - Return the llvm::Constant for the address |
| 139 | /// of the given function. |
| 140 | llvm::Constant *GetAddrOfFunction(const FunctionDecl *D); |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 141 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 142 | /// getBuiltinLibFunction - Given a builtin id for a function like |
| 143 | /// "__builtin_fabsf", return a Function* for "fabsf". |
| 144 | /// |
| 145 | llvm::Function *getBuiltinLibFunction(unsigned BuiltinID); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame^] | 146 | |
| 147 | /// GetStringForStringLiteral - Return the appropriate bytes for a |
| 148 | /// string literal, properly padded to match the literal type. If |
| 149 | /// only the address of a constant is needed consider using |
| 150 | /// GetAddrOfConstantStringLiteral. |
| 151 | std::string GetStringForStringLiteral(const StringLiteral *E); |
| 152 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 153 | llvm::Constant *GetAddrOfConstantCFString(const std::string& str); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 154 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame^] | 155 | /// GetAddrOfConstantStringFromLiteral - Return a pointer to a |
| 156 | /// constant array for the given string literal. |
| 157 | llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S); |
Daniel Dunbar | 1e04976 | 2008-08-10 20:25:57 +0000 | [diff] [blame] | 158 | |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame^] | 159 | /// GetAddrOfConstantString - Returns a pointer to a character array |
| 160 | /// containing the literal. This contents are exactly that of the |
| 161 | /// given string, i.e. it will not be null terminated automatically; |
| 162 | /// see GetAddrOfConstantCString. Note that whether the result is |
| 163 | /// actually a pointer to an LLVM constant depends on |
| 164 | /// Feature.WriteableStrings. |
| 165 | /// |
| 166 | /// The result has pointer to array type. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 167 | llvm::Constant *GetAddrOfConstantString(const std::string& str); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame^] | 168 | |
| 169 | /// GetAddrOfConstantCString - Returns a pointer to a character |
| 170 | /// array containing the literal and a terminating '\-' |
| 171 | /// character. The result has pointer to array type. |
| 172 | llvm::Constant *GetAddrOfConstantCString(const std::string &str); |
| 173 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | llvm::Function *getMemCpyFn(); |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame] | 175 | llvm::Function *getMemMoveFn(); |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 176 | llvm::Function *getMemSetFn(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 177 | llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0, |
| 178 | unsigned NumTys = 0); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 180 | void EmitObjCMethod(const ObjCMethodDecl *OMD); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 181 | void EmitObjCCategoryImpl(const ObjCCategoryImplDecl *OCD); |
| 182 | void EmitObjCClassImplementation(const ObjCImplementationDecl *OID); |
| 183 | void EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 184 | |
| 185 | /// EmitGlobal - Emit code for a singal global function or var |
| 186 | /// decl. Forward declarations are emitted lazily. |
| 187 | void EmitGlobal(const ValueDecl *D); |
| 188 | |
| 189 | void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); } |
| 190 | |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 191 | void UpdateCompletedType(const TagDecl *D); |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 192 | llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 193 | llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 194 | const AnnotateAttr *AA, unsigned LineNo); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 196 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 197 | /// specified stmt yet. |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 199 | void WarnUnsupported(const Stmt *S, const char *Type); |
| 200 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 201 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 202 | /// specified decl yet. |
| 203 | void WarnUnsupported(const Decl *D, const char *Type); |
| 204 | |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 205 | /// setVisibility - Set the visibility for the given LLVM GlobalValue |
| 206 | /// according to the given clang AST visibility value. |
| 207 | static void setVisibility(llvm::GlobalValue *GV, |
| 208 | VisibilityAttr::VisibilityTypes); |
| 209 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 210 | private: |
Eli Friedman | ff4a2d9 | 2008-06-01 15:54:49 +0000 | [diff] [blame] | 211 | void SetFunctionAttributes(const FunctionDecl *FD, |
| 212 | llvm::Function *F, |
| 213 | const llvm::FunctionType *FTy); |
Nuno Lopes | d4cbda6 | 2008-06-08 15:45:52 +0000 | [diff] [blame] | 214 | |
| 215 | void SetGlobalValueAttributes(const FunctionDecl *FD, |
| 216 | llvm::GlobalValue *GV); |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 217 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 218 | void EmitGlobalDefinition(const ValueDecl *D); |
| 219 | llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D); |
| 220 | void EmitGlobalFunctionDefinition(const FunctionDecl *D); |
| 221 | void EmitGlobalVarDefinition(const VarDecl *D); |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 222 | |
| 223 | // FIXME: Hardcoding priority here is gross. |
| 224 | void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535); |
| 225 | void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535); |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 226 | |
Daniel Dunbar | 6bfed7e | 2008-08-01 00:01:51 +0000 | [diff] [blame] | 227 | /// EmitCtorList - Generates a global array of functions and |
| 228 | /// priorities using the given list and name. This array will have |
| 229 | /// appending linkage and is suitable for use as a LLVM constructor |
| 230 | /// or destructor array. |
| 231 | void EmitCtorList(const CtorList &Fns, const char *GlobalName); |
| 232 | |
Daniel Dunbar | bd012ff | 2008-07-29 23:18:29 +0000 | [diff] [blame] | 233 | void EmitAnnotations(void); |
| 234 | void EmitStatics(void); |
| 235 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | }; |
| 237 | } // end namespace CodeGen |
| 238 | } // end namespace clang |
| 239 | |
| 240 | #endif |