Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- CodeGenModule.h - Per-Module state for LLVM CodeGen --------------===// |
| 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" |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 18 | #include "CGObjCRuntime.h" |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/Attr.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | class Module; |
| 25 | class Constant; |
| 26 | class Function; |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 27 | class GlobalValue; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 28 | class TargetData; |
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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | class Decl; |
Oliver Hunt | 2824723 | 2007-12-02 00:11:25 +0000 | [diff] [blame] | 36 | class Expr; |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 37 | class Stmt; |
Nate Begeman | 1a1d92a | 2008-04-20 20:38:08 +0000 | [diff] [blame] | 38 | class NamedDecl; |
Chris Lattner | 2b9d2ca | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 39 | class VarDecl; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 40 | struct LangOptions; |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 41 | class Diagnostic; |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 42 | class AnnotateAttr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | |
| 44 | namespace CodeGen { |
| 45 | |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 46 | class CodeGenFunction; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 47 | class CGDebugInfo; |
| 48 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | /// CodeGenModule - This class organizes the cross-module state that is used |
| 50 | /// while generating LLVM code. |
| 51 | class CodeGenModule { |
| 52 | ASTContext &Context; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 53 | const LangOptions &Features; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | llvm::Module &TheModule; |
Devang Patel | 7a4718e | 2007-10-31 20:01:01 +0000 | [diff] [blame] | 55 | const llvm::TargetData &TheTargetData; |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 56 | Diagnostic &Diags; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | CodeGenTypes Types; |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 58 | CGObjCRuntime *Runtime; |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 59 | CGDebugInfo *DebugInfo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | |
| 61 | llvm::Function *MemCpyFn; |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame^] | 62 | llvm::Function *MemMoveFn; |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 63 | llvm::Function *MemSetFn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap; |
Nate Begeman | 1a1d92a | 2008-04-20 20:38:08 +0000 | [diff] [blame] | 65 | std::vector<const NamedDecl*> StaticDecls; |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 67 | std::vector<llvm::Constant*> GlobalCtors; |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 68 | std::vector<llvm::Constant*> Annotations; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 69 | |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 70 | llvm::StringMap<llvm::Constant*> CFConstantStringMap; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 71 | llvm::StringMap<llvm::Constant*> ConstantStringMap; |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 72 | llvm::Constant *CFConstantStringClassRef; |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 73 | |
| 74 | std::vector<llvm::Function *> BuiltinFunctions; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | public: |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 76 | CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M, |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 77 | const llvm::TargetData &TD, Diagnostic &Diags, |
| 78 | bool GenerateDebugInfo); |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 79 | ~CodeGenModule(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 2b94fe3 | 2008-03-01 08:45:05 +0000 | [diff] [blame] | 81 | CGObjCRuntime *getObjCRuntime() { return Runtime; } |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 82 | CGDebugInfo *getDebugInfo() { return DebugInfo; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | ASTContext &getContext() const { return Context; } |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 84 | const LangOptions &getLangOptions() const { return Features; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | llvm::Module &getModule() const { return TheModule; } |
| 86 | CodeGenTypes &getTypes() { return Types; } |
Chris Lattner | fb97b03 | 2007-12-02 01:40:18 +0000 | [diff] [blame] | 87 | Diagnostic &getDiags() const { return Diags; } |
Chris Lattner | 8f92528 | 2008-01-03 06:36:51 +0000 | [diff] [blame] | 88 | const llvm::TargetData &getTargetData() const { return TheTargetData; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 90 | llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D, |
| 91 | bool isDefinition); |
Chris Lattner | 2b9d2ca | 2007-12-18 08:16:44 +0000 | [diff] [blame] | 92 | llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, bool isDefinition); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 58c3f9e | 2007-12-02 06:27:33 +0000 | [diff] [blame] | 94 | |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 95 | /// getBuiltinLibFunction - Given a builtin id for a function like |
| 96 | /// "__builtin_fabsf", return a Function* for "fabsf". |
| 97 | /// |
| 98 | llvm::Function *getBuiltinLibFunction(unsigned BuiltinID); |
Anders Carlsson | c9e2091 | 2007-08-21 00:21:21 +0000 | [diff] [blame] | 99 | llvm::Constant *GetAddrOfConstantCFString(const std::string& str); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 100 | |
| 101 | /// GetAddrOfConstantString -- returns a pointer to the character |
| 102 | /// array containing the literal. The result is pointer to array type. |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 103 | llvm::Constant *GetAddrOfConstantString(const std::string& str); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | llvm::Function *getMemCpyFn(); |
Eli Friedman | 0c99509 | 2008-05-26 12:59:39 +0000 | [diff] [blame^] | 105 | llvm::Function *getMemMoveFn(); |
Lauro Ramos Venancio | 41ef30e | 2008-02-19 22:01:01 +0000 | [diff] [blame] | 106 | llvm::Function *getMemSetFn(); |
Chris Lattner | 7acda7c | 2007-12-18 00:25:38 +0000 | [diff] [blame] | 107 | llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0, |
| 108 | unsigned NumTys = 0); |
Chris Lattner | bef20ac | 2007-08-31 04:31:45 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 110 | void AddGlobalCtor(llvm::Function * Ctor); |
| 111 | void EmitGlobalCtors(void); |
Nate Begeman | 532485c | 2008-04-18 23:43:57 +0000 | [diff] [blame] | 112 | void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); } |
| 113 | void EmitAnnotations(void); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 114 | void EmitStatics(void); |
Chris Lattner | 6d39760 | 2008-03-14 17:18:18 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 116 | void EmitObjCMethod(const ObjCMethodDecl *OMD); |
Chris Lattner | 88a69ad | 2007-07-13 05:13:43 +0000 | [diff] [blame] | 117 | void EmitFunction(const FunctionDecl *FD); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 118 | void EmitGlobalVar(const VarDecl *D); |
Nate Begeman | 4c13b7a | 2008-04-20 06:29:50 +0000 | [diff] [blame] | 119 | void EmitGlobalVarInit(const VarDecl *D); |
Steve Naroff | 248a753 | 2008-04-15 22:42:06 +0000 | [diff] [blame] | 120 | void EmitGlobalVarDeclarator(const VarDecl *D); |
Chris Lattner | c5b8806 | 2008-02-06 05:08:19 +0000 | [diff] [blame] | 121 | void UpdateCompletedType(const TagDecl *D); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 122 | llvm::Constant *EmitGlobalInit(const Expr *E); |
Lauro Ramos Venancio | 8137335 | 2008-02-26 21:41:45 +0000 | [diff] [blame] | 123 | llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0); |
Nate Begeman | 8bd4afe | 2008-04-19 04:17:09 +0000 | [diff] [blame] | 124 | llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV, |
| 125 | const AnnotateAttr *AA, unsigned LineNo); |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 127 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 128 | /// specified stmt yet. |
Anders Carlsson | 3b1d57b | 2008-01-26 01:36:00 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 2c8569d | 2007-12-02 07:19:18 +0000 | [diff] [blame] | 130 | void WarnUnsupported(const Stmt *S, const char *Type); |
| 131 | |
Chris Lattner | c6fdc34 | 2008-01-12 07:05:38 +0000 | [diff] [blame] | 132 | /// WarnUnsupported - Print out a warning that codegen doesn't support the |
| 133 | /// specified decl yet. |
| 134 | void WarnUnsupported(const Decl *D, const char *Type); |
| 135 | |
Dan Gohman | 4f8d123 | 2008-05-22 00:50:06 +0000 | [diff] [blame] | 136 | /// setVisibility - Set the visibility for the given LLVM GlobalValue |
| 137 | /// according to the given clang AST visibility value. |
| 138 | static void setVisibility(llvm::GlobalValue *GV, |
| 139 | VisibilityAttr::VisibilityTypes); |
| 140 | |
Chris Lattner | 9cd4fe4 | 2007-12-02 07:09:19 +0000 | [diff] [blame] | 141 | private: |
| 142 | /// ReplaceMapValuesWith - This is a really slow and bad function that |
| 143 | /// searches for any entries in GlobalDeclMap that point to OldVal, changing |
| 144 | /// them to point to NewVal. This is badbadbad, FIXME! |
| 145 | void ReplaceMapValuesWith(llvm::Constant *OldVal, llvm::Constant *NewVal); |
| 146 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | }; |
| 148 | } // end namespace CodeGen |
| 149 | } // end namespace clang |
| 150 | |
| 151 | #endif |