blob: 3f604dd048b25ea7bb25cb6aa39f027e80e9942a [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
Daniel Dunbarb7688072008-09-10 00:41:16 +000022#include "CGCall.h"
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024namespace llvm {
25 class Module;
26 class Constant;
27 class Function;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000028 class GlobalValue;
Devang Patel7a4718e2007-10-31 20:01:01 +000029 class TargetData;
Eli Friedmanff4a2d92008-06-01 15:54:49 +000030 class FunctionType;
Reid Spencer5f016e22007-07-11 17:01:13 +000031}
32
33namespace clang {
34 class ASTContext;
35 class FunctionDecl;
Daniel Dunbar90db8822008-08-25 06:18:57 +000036 class IdentifierInfo;
Chris Lattner391d77a2008-03-30 23:03:07 +000037 class ObjCMethodDecl;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000038 class ObjCImplementationDecl;
39 class ObjCCategoryImplDecl;
40 class ObjCProtocolDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000041 class Decl;
Oliver Hunt28247232007-12-02 00:11:25 +000042 class Expr;
Chris Lattner2c8569d2007-12-02 07:19:18 +000043 class Stmt;
Daniel Dunbar1e049762008-08-10 20:25:57 +000044 class StringLiteral;
Nate Begeman1a1d92a2008-04-20 20:38:08 +000045 class NamedDecl;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000046 class ValueDecl;
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000047 class VarDecl;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000048 struct LangOptions;
Chris Lattnerfb97b032007-12-02 01:40:18 +000049 class Diagnostic;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000050 class AnnotateAttr;
Reid Spencer5f016e22007-07-11 17:01:13 +000051
52namespace CodeGen {
53
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000054 class CodeGenFunction;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000055 class CGDebugInfo;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000056 class CGObjCRuntime;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000057
Daniel Dunbar41071de2008-08-15 23:26:23 +000058/// CodeGenModule - This class organizes the cross-function state that
59/// is used while generating LLVM code.
Reid Spencer5f016e22007-07-11 17:01:13 +000060class CodeGenModule {
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +000061 typedef std::vector< std::pair<llvm::Constant*, int> > CtorList;
62
Reid Spencer5f016e22007-07-11 17:01:13 +000063 ASTContext &Context;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000064 const LangOptions &Features;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 llvm::Module &TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000066 const llvm::TargetData &TheTargetData;
Chris Lattnerfb97b032007-12-02 01:40:18 +000067 Diagnostic &Diags;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 CodeGenTypes Types;
Ted Kremenek815c78f2008-08-05 18:50:11 +000069 CGObjCRuntime* Runtime;
70 CGDebugInfo* DebugInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +000071
72 llvm::Function *MemCpyFn;
Eli Friedman0c995092008-05-26 12:59:39 +000073 llvm::Function *MemMoveFn;
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000074 llvm::Function *MemSetFn;
Daniel Dunbar9986eab2008-07-30 16:32:24 +000075
Daniel Dunbarf1968f22008-10-01 00:49:24 +000076 /// RuntimeFunctions - List of runtime functions whose names must be
77 /// protected from introducing conflicts. These functions should be
78 /// created unnamed, we will name them and patch up conflicts when
79 /// we release the module.
80 std::vector< std::pair<llvm::Function*, std::string> > RuntimeFunctions;
81
Daniel Dunbar3c827a72008-08-05 23:31:02 +000082 /// GlobalDeclMap - Mapping of decl names global variables we have
Daniel Dunbar9986eab2008-07-30 16:32:24 +000083 /// already emitted. Note that the entries in this map are the
Daniel Dunbar3c827a72008-08-05 23:31:02 +000084 /// actual globals and therefore may not be of the same type as the
85 /// decl, they should be bitcasted on retrieval. Also note that the
86 /// globals are keyed on their source name, not the global name
87 /// (which may change with attributes such as asm-labels).
Daniel Dunbar90db8822008-08-25 06:18:57 +000088 llvm::DenseMap<IdentifierInfo*, llvm::GlobalValue*> GlobalDeclMap;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000089
Daniel Dunbar219df662008-09-08 23:44:31 +000090 /// Aliases - List of aliases in module. These cannot be emitted
91 /// until all the code has been seen, as they reference things by
92 /// name instead of directly and may reference forward.
93 std::vector<const FunctionDecl*> Aliases;
94
95 /// StaticDecls - List of static global for which code generation is
96 /// delayed. When the translation unit has been fully processed we
97 /// will lazily emit definitions for only the decls that were
98 /// actually used. This should contain only Function and Var decls,
99 /// and only those which actually define something.
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000100 std::vector<const ValueDecl*> StaticDecls;
Nate Begeman4c13b7a2008-04-20 06:29:50 +0000101
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000102 /// GlobalCtors - Store the list of global constructors and their
103 /// respective priorities to be emitted when the translation unit is
104 /// complete.
105 CtorList GlobalCtors;
106
107 /// GlobalDtors - Store the list of global destructors and their
108 /// respective priorities to be emitted when the translation unit is
109 /// complete.
110 CtorList GlobalDtors;
111
Nate Begeman532485c2008-04-18 23:43:57 +0000112 std::vector<llvm::Constant*> Annotations;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000113
Anders Carlssonc9e20912007-08-21 00:21:21 +0000114 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000115 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000116
117 /// CFConstantStringClassRef - Cached reference to the class for
118 /// constant strings. This value has type int * but is actually an
119 /// Obj-C class pointer.
Anders Carlssonc9e20912007-08-21 00:21:21 +0000120 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000121
122 std::vector<llvm::Function *> BuiltinFunctions;
Reid Spencer5f016e22007-07-11 17:01:13 +0000123public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000124 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000125 const llvm::TargetData &TD, Diagnostic &Diags,
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000126 bool GenerateDebugInfo);
Ted Kremenek815c78f2008-08-05 18:50:11 +0000127
Chris Lattner2b94fe32008-03-01 08:45:05 +0000128 ~CodeGenModule();
Reid Spencer5f016e22007-07-11 17:01:13 +0000129
Ted Kremenek815c78f2008-08-05 18:50:11 +0000130 /// Release - Finalize LLVM code generation.
131 void Release();
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000132
133 /// getObjCRuntime() - Return a reference to the configured
134 /// Objective-C runtime.
135 CGObjCRuntime &getObjCRuntime() {
136 assert(Runtime && "No Objective-C runtime has been configured.");
137 return *Runtime;
138 }
Ted Kremenek815c78f2008-08-05 18:50:11 +0000139
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000140 /// hasObjCRuntime() - Return true iff an Objective-C runtime has
141 /// been configured.
142 bool hasObjCRuntime() { return !!Runtime; }
143
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000144 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 ASTContext &getContext() const { return Context; }
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000146 const LangOptions &getLangOptions() const { return Features; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 llvm::Module &getModule() const { return TheModule; }
148 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerfb97b032007-12-02 01:40:18 +0000149 Diagnostic &getDiags() const { return Diags; }
Chris Lattner8f925282008-01-03 06:36:51 +0000150 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000151
152 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address
153 /// of the given global variable.
154 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D);
155
156 /// GetAddrOfFunction - Return the llvm::Constant for the address
157 /// of the given function.
158 llvm::Constant *GetAddrOfFunction(const FunctionDecl *D);
Daniel Dunbar61432932008-08-13 23:20:05 +0000159
160 /// GetStringForStringLiteral - Return the appropriate bytes for a
161 /// string literal, properly padded to match the literal type. If
162 /// only the address of a constant is needed consider using
163 /// GetAddrOfConstantStringLiteral.
164 std::string GetStringForStringLiteral(const StringLiteral *E);
165
Daniel Dunbar41071de2008-08-15 23:26:23 +0000166 /// GetAddrOfConstantCFString - Return a pointer to a
167 /// constant CFString object for the given string.
Anders Carlssonc9e20912007-08-21 00:21:21 +0000168 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000169
Daniel Dunbar61432932008-08-13 23:20:05 +0000170 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
171 /// constant array for the given string literal.
172 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
Daniel Dunbar1e049762008-08-10 20:25:57 +0000173
Daniel Dunbar61432932008-08-13 23:20:05 +0000174 /// GetAddrOfConstantString - Returns a pointer to a character array
175 /// containing the literal. This contents are exactly that of the
176 /// given string, i.e. it will not be null terminated automatically;
177 /// see GetAddrOfConstantCString. Note that whether the result is
178 /// actually a pointer to an LLVM constant depends on
179 /// Feature.WriteableStrings.
180 ///
181 /// The result has pointer to array type.
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000182 ///
183 /// \param GlobalName If provided, the name to use for the global
184 /// (if one is created).
185 llvm::Constant *GetAddrOfConstantString(const std::string& str,
186 const char *GlobalName=0);
Daniel Dunbar61432932008-08-13 23:20:05 +0000187
188 /// GetAddrOfConstantCString - Returns a pointer to a character
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000189 /// array containing the literal and a terminating '\0'
Daniel Dunbar61432932008-08-13 23:20:05 +0000190 /// character. The result has pointer to array type.
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000191 ///
192 /// \param GlobalName If provided, the name to use for the global
193 /// (if one is created).
194 llvm::Constant *GetAddrOfConstantCString(const std::string &str,
195 const char *GlobalName=0);
Daniel Dunbar41071de2008-08-15 23:26:23 +0000196
197 /// getBuiltinLibFunction - Given a builtin id for a function like
198 /// "__builtin_fabsf", return a Function* for "fabsf".
199 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Daniel Dunbar61432932008-08-13 23:20:05 +0000200
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 llvm::Function *getMemCpyFn();
Eli Friedman0c995092008-05-26 12:59:39 +0000202 llvm::Function *getMemMoveFn();
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +0000203 llvm::Function *getMemSetFn();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000204 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
205 unsigned NumTys = 0);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000206
Daniel Dunbar41071de2008-08-15 23:26:23 +0000207 /// EmitTopLevelDecl - Emit code for a single top level declaration.
208 void EmitTopLevelDecl(Decl *D);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000209
210 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
211
Daniel Dunbarf1968f22008-10-01 00:49:24 +0000212 /// CreateRuntimeFunction - Create a new runtime function whose name
213 /// must be protected from collisions.
214 llvm::Function *CreateRuntimeFunction(const llvm::FunctionType *Ty,
215 const std::string &Name);
216
Chris Lattnerc5b88062008-02-06 05:08:19 +0000217 void UpdateCompletedType(const TagDecl *D);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000218 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000219 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
220 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000221
Daniel Dunbar488e9932008-08-16 00:56:44 +0000222 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000223 /// specified stmt yet.
224 /// \param OmitOnError - If true, then this error should only be
225 /// emitted if no other errors have been reported.
226 void ErrorUnsupported(const Stmt *S, const char *Type,
227 bool OmitOnError=false);
Chris Lattner2c8569d2007-12-02 07:19:18 +0000228
Daniel Dunbar488e9932008-08-16 00:56:44 +0000229 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000230 /// specified decl yet.
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000231 /// \param OmitOnError - If true, then this error should only be
232 /// emitted if no other errors have been reported.
233 void ErrorUnsupported(const Decl *D, const char *Type,
234 bool OmitOnError=false);
Dan Gohman4f8d1232008-05-22 00:50:06 +0000235
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000236 void SetMethodAttributes(const ObjCMethodDecl *MD,
237 llvm::Function *F);
238
Devang Patel761d7f72008-09-25 21:02:23 +0000239 void SetFunctionAttributes(const Decl *D,
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000240 const CGFunctionInfo &Info,
Daniel Dunbarb7688072008-09-10 00:41:16 +0000241 llvm::Function *F);
242
243 /// ReturnTypeUsesSret - Return true iff the given type uses 'sret'
244 /// when used as a return type.
245 bool ReturnTypeUsesSret(QualType RetTy);
246
Devang Patel761d7f72008-09-25 21:02:23 +0000247 void ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbarb7688072008-09-10 00:41:16 +0000248 const ArgTypeIterator begin,
249 const ArgTypeIterator end,
Devang Patel761d7f72008-09-25 21:02:23 +0000250 AttributeListType &PAL);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000251
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000252private:
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000253 /// SetFunctionAttributesForDefinition - Set function attributes
254 /// specific to a function definition.
Daniel Dunbar219df662008-09-08 23:44:31 +0000255 /// \param D - The ObjCMethodDecl or FunctionDecl defining \arg F.
256 void SetFunctionAttributesForDefinition(const Decl *D,
257 llvm::Function *F);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000258
Eli Friedmanff4a2d92008-06-01 15:54:49 +0000259 void SetFunctionAttributes(const FunctionDecl *FD,
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000260 llvm::Function *F);
Nuno Lopesd4cbda62008-06-08 15:45:52 +0000261
Daniel Dunbar41071de2008-08-15 23:26:23 +0000262 /// EmitGlobal - Emit code for a singal global function or var
263 /// decl. Forward declarations are emitted lazily.
264 void EmitGlobal(const ValueDecl *D);
265
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000266 void EmitGlobalDefinition(const ValueDecl *D);
267 llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D);
268 void EmitGlobalFunctionDefinition(const FunctionDecl *D);
269 void EmitGlobalVarDefinition(const VarDecl *D);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000270 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000271
272 // FIXME: Hardcoding priority here is gross.
273 void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
274 void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000275
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000276 /// EmitCtorList - Generates a global array of functions and
277 /// priorities using the given list and name. This array will have
278 /// appending linkage and is suitable for use as a LLVM constructor
279 /// or destructor array.
280 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
281
Daniel Dunbar219df662008-09-08 23:44:31 +0000282 void EmitAliases(void);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000283 void EmitAnnotations(void);
284 void EmitStatics(void);
Daniel Dunbarf1968f22008-10-01 00:49:24 +0000285
286 void BindRuntimeFunctions();
Reid Spencer5f016e22007-07-11 17:01:13 +0000287};
288} // end namespace CodeGen
289} // end namespace clang
290
291#endif