blob: 70a91b6a9975d19d0184d866d99fcd3c346938ed [file] [log] [blame]
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is the internal per-translation-unit state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattneref52a2f2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
17#include "CodeGenTypes.h"
Dan Gohman4f8d1232008-05-22 00:50:06 +000018#include "clang/AST/Attr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/DenseMap.h"
Anders Carlssonc9e20912007-08-21 00:21:21 +000020#include "llvm/ADT/StringMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021
22namespace llvm {
23 class Module;
24 class Constant;
25 class Function;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000026 class GlobalValue;
Devang Patel7a4718e2007-10-31 20:01:01 +000027 class TargetData;
Eli Friedmanff4a2d92008-06-01 15:54:49 +000028 class FunctionType;
Reid Spencer5f016e22007-07-11 17:01:13 +000029}
30
31namespace clang {
32 class ASTContext;
33 class FunctionDecl;
Daniel Dunbar90db8822008-08-25 06:18:57 +000034 class IdentifierInfo;
Chris Lattner391d77a2008-03-30 23:03:07 +000035 class ObjCMethodDecl;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000036 class ObjCImplementationDecl;
37 class ObjCCategoryImplDecl;
38 class ObjCProtocolDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 class Decl;
Oliver Hunt28247232007-12-02 00:11:25 +000040 class Expr;
Chris Lattner2c8569d2007-12-02 07:19:18 +000041 class Stmt;
Daniel Dunbar1e049762008-08-10 20:25:57 +000042 class StringLiteral;
Nate Begeman1a1d92a2008-04-20 20:38:08 +000043 class NamedDecl;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000044 class ValueDecl;
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000045 class VarDecl;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000046 struct LangOptions;
Chris Lattnerfb97b032007-12-02 01:40:18 +000047 class Diagnostic;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000048 class AnnotateAttr;
Reid Spencer5f016e22007-07-11 17:01:13 +000049
50namespace CodeGen {
51
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000052 class CodeGenFunction;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000053 class CGDebugInfo;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000054 class CGObjCRuntime;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000055
Daniel Dunbar41071de2008-08-15 23:26:23 +000056/// CodeGenModule - This class organizes the cross-function state that
57/// is used while generating LLVM code.
Reid Spencer5f016e22007-07-11 17:01:13 +000058class CodeGenModule {
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +000059 typedef std::vector< std::pair<llvm::Constant*, int> > CtorList;
60
Reid Spencer5f016e22007-07-11 17:01:13 +000061 ASTContext &Context;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000062 const LangOptions &Features;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 llvm::Module &TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000064 const llvm::TargetData &TheTargetData;
Chris Lattnerfb97b032007-12-02 01:40:18 +000065 Diagnostic &Diags;
Reid Spencer5f016e22007-07-11 17:01:13 +000066 CodeGenTypes Types;
Ted Kremenek815c78f2008-08-05 18:50:11 +000067 CGObjCRuntime* Runtime;
68 CGDebugInfo* DebugInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +000069
70 llvm::Function *MemCpyFn;
Eli Friedman0c995092008-05-26 12:59:39 +000071 llvm::Function *MemMoveFn;
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000072 llvm::Function *MemSetFn;
Daniel Dunbar9986eab2008-07-30 16:32:24 +000073
Daniel Dunbar3c827a72008-08-05 23:31:02 +000074 /// GlobalDeclMap - Mapping of decl names global variables we have
Daniel Dunbar9986eab2008-07-30 16:32:24 +000075 /// already emitted. Note that the entries in this map are the
Daniel Dunbar3c827a72008-08-05 23:31:02 +000076 /// actual globals and therefore may not be of the same type as the
77 /// decl, they should be bitcasted on retrieval. Also note that the
78 /// globals are keyed on their source name, not the global name
79 /// (which may change with attributes such as asm-labels).
Daniel Dunbar90db8822008-08-25 06:18:57 +000080 llvm::DenseMap<IdentifierInfo*, llvm::GlobalValue*> GlobalDeclMap;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000081
82 /// List of static global for which code generation is delayed. When
83 /// the translation unit has been fully processed we will lazily
84 /// emit definitions for only the decls that were actually used.
85 /// This should contain only Function and Var decls, and only those
86 /// which actually define something.
87 std::vector<const ValueDecl*> StaticDecls;
Nate Begeman4c13b7a2008-04-20 06:29:50 +000088
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +000089 /// GlobalCtors - Store the list of global constructors and their
90 /// respective priorities to be emitted when the translation unit is
91 /// complete.
92 CtorList GlobalCtors;
93
94 /// GlobalDtors - Store the list of global destructors and their
95 /// respective priorities to be emitted when the translation unit is
96 /// complete.
97 CtorList GlobalDtors;
98
Nate Begeman532485c2008-04-18 23:43:57 +000099 std::vector<llvm::Constant*> Annotations;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000100
Anders Carlssonc9e20912007-08-21 00:21:21 +0000101 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000102 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000103
104 /// CFConstantStringClassRef - Cached reference to the class for
105 /// constant strings. This value has type int * but is actually an
106 /// Obj-C class pointer.
Anders Carlssonc9e20912007-08-21 00:21:21 +0000107 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000108
109 std::vector<llvm::Function *> BuiltinFunctions;
Reid Spencer5f016e22007-07-11 17:01:13 +0000110public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000111 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000112 const llvm::TargetData &TD, Diagnostic &Diags,
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000113 bool GenerateDebugInfo);
Ted Kremenek815c78f2008-08-05 18:50:11 +0000114
Chris Lattner2b94fe32008-03-01 08:45:05 +0000115 ~CodeGenModule();
Reid Spencer5f016e22007-07-11 17:01:13 +0000116
Ted Kremenek815c78f2008-08-05 18:50:11 +0000117 /// Release - Finalize LLVM code generation.
118 void Release();
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000119
120 /// getObjCRuntime() - Return a reference to the configured
121 /// Objective-C runtime.
122 CGObjCRuntime &getObjCRuntime() {
123 assert(Runtime && "No Objective-C runtime has been configured.");
124 return *Runtime;
125 }
Ted Kremenek815c78f2008-08-05 18:50:11 +0000126
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000127 /// hasObjCRuntime() - Return true iff an Objective-C runtime has
128 /// been configured.
129 bool hasObjCRuntime() { return !!Runtime; }
130
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000131 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 ASTContext &getContext() const { return Context; }
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000133 const LangOptions &getLangOptions() const { return Features; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 llvm::Module &getModule() const { return TheModule; }
135 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerfb97b032007-12-02 01:40:18 +0000136 Diagnostic &getDiags() const { return Diags; }
Chris Lattner8f925282008-01-03 06:36:51 +0000137 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000138
139 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address
140 /// of the given global variable.
141 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D);
142
143 /// GetAddrOfFunction - Return the llvm::Constant for the address
144 /// of the given function.
145 llvm::Constant *GetAddrOfFunction(const FunctionDecl *D);
Daniel Dunbar61432932008-08-13 23:20:05 +0000146
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
Daniel Dunbar41071de2008-08-15 23:26:23 +0000153 /// GetAddrOfConstantCFString - Return a pointer to a
154 /// constant CFString object for the given string.
Anders Carlssonc9e20912007-08-21 00:21:21 +0000155 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000156
Daniel Dunbar61432932008-08-13 23:20:05 +0000157 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
158 /// constant array for the given string literal.
159 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
Daniel Dunbar1e049762008-08-10 20:25:57 +0000160
Daniel Dunbar61432932008-08-13 23:20:05 +0000161 /// GetAddrOfConstantString - Returns a pointer to a character array
162 /// containing the literal. This contents are exactly that of the
163 /// given string, i.e. it will not be null terminated automatically;
164 /// see GetAddrOfConstantCString. Note that whether the result is
165 /// actually a pointer to an LLVM constant depends on
166 /// Feature.WriteableStrings.
167 ///
168 /// The result has pointer to array type.
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000169 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Daniel Dunbar61432932008-08-13 23:20:05 +0000170
171 /// GetAddrOfConstantCString - Returns a pointer to a character
172 /// array containing the literal and a terminating '\-'
173 /// character. The result has pointer to array type.
174 llvm::Constant *GetAddrOfConstantCString(const std::string &str);
Daniel Dunbar41071de2008-08-15 23:26:23 +0000175
176 /// getBuiltinLibFunction - Given a builtin id for a function like
177 /// "__builtin_fabsf", return a Function* for "fabsf".
178 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Daniel Dunbar61432932008-08-13 23:20:05 +0000179
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 llvm::Function *getMemCpyFn();
Eli Friedman0c995092008-05-26 12:59:39 +0000181 llvm::Function *getMemMoveFn();
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +0000182 llvm::Function *getMemSetFn();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000183 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
184 unsigned NumTys = 0);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000185
Daniel Dunbar41071de2008-08-15 23:26:23 +0000186 /// EmitTopLevelDecl - Emit code for a single top level declaration.
187 void EmitTopLevelDecl(Decl *D);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000188
189 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
190
Chris Lattnerc5b88062008-02-06 05:08:19 +0000191 void UpdateCompletedType(const TagDecl *D);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000192 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000193 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
194 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000195
Daniel Dunbar488e9932008-08-16 00:56:44 +0000196 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000197 /// specified stmt yet.
198 /// \param OmitOnError - If true, then this error should only be
199 /// emitted if no other errors have been reported.
200 void ErrorUnsupported(const Stmt *S, const char *Type,
201 bool OmitOnError=false);
Chris Lattner2c8569d2007-12-02 07:19:18 +0000202
Daniel Dunbar488e9932008-08-16 00:56:44 +0000203 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000204 /// specified decl yet.
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000205 /// \param OmitOnError - If true, then this error should only be
206 /// emitted if no other errors have been reported.
207 void ErrorUnsupported(const Decl *D, const char *Type,
208 bool OmitOnError=false);
Dan Gohman4f8d1232008-05-22 00:50:06 +0000209
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000210private:
Eli Friedmanff4a2d92008-06-01 15:54:49 +0000211 void SetFunctionAttributes(const FunctionDecl *FD,
212 llvm::Function *F,
213 const llvm::FunctionType *FTy);
Nuno Lopesd4cbda62008-06-08 15:45:52 +0000214
215 void SetGlobalValueAttributes(const FunctionDecl *FD,
216 llvm::GlobalValue *GV);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000217
Daniel Dunbar41071de2008-08-15 23:26:23 +0000218 /// EmitGlobal - Emit code for a singal global function or var
219 /// decl. Forward declarations are emitted lazily.
220 void EmitGlobal(const ValueDecl *D);
221
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000222 void EmitGlobalDefinition(const ValueDecl *D);
223 llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D);
224 void EmitGlobalFunctionDefinition(const FunctionDecl *D);
225 void EmitGlobalVarDefinition(const VarDecl *D);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000226 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000227
228 // FIXME: Hardcoding priority here is gross.
229 void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
230 void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000231
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000232 /// EmitCtorList - Generates a global array of functions and
233 /// priorities using the given list and name. This array will have
234 /// appending linkage and is suitable for use as a LLVM constructor
235 /// or destructor array.
236 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
237
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000238 void EmitAnnotations(void);
239 void EmitStatics(void);
240
Reid Spencer5f016e22007-07-11 17:01:13 +0000241};
242} // end namespace CodeGen
243} // end namespace clang
244
245#endif