blob: 0bd0e2c724fd7406a1be3c5ecd0e401316d53cee [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
Anders Carlssonb723f752009-01-04 02:08:04 +000024#include <list>
25
Reid Spencer5f016e22007-07-11 17:01:13 +000026namespace llvm {
27 class Module;
28 class Constant;
29 class Function;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000030 class GlobalValue;
Devang Patel7a4718e2007-10-31 20:01:01 +000031 class TargetData;
Eli Friedmanff4a2d92008-06-01 15:54:49 +000032 class FunctionType;
Reid Spencer5f016e22007-07-11 17:01:13 +000033}
34
35namespace clang {
36 class ASTContext;
37 class FunctionDecl;
Daniel Dunbar90db8822008-08-25 06:18:57 +000038 class IdentifierInfo;
Chris Lattner391d77a2008-03-30 23:03:07 +000039 class ObjCMethodDecl;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000040 class ObjCImplementationDecl;
41 class ObjCCategoryImplDecl;
42 class ObjCProtocolDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000043 class Decl;
Oliver Hunt28247232007-12-02 00:11:25 +000044 class Expr;
Chris Lattner2c8569d2007-12-02 07:19:18 +000045 class Stmt;
Daniel Dunbar1e049762008-08-10 20:25:57 +000046 class StringLiteral;
Nate Begeman1a1d92a2008-04-20 20:38:08 +000047 class NamedDecl;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000048 class ValueDecl;
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000049 class VarDecl;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000050 struct LangOptions;
Chris Lattnerfb97b032007-12-02 01:40:18 +000051 class Diagnostic;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000052 class AnnotateAttr;
Reid Spencer5f016e22007-07-11 17:01:13 +000053
54namespace CodeGen {
55
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000056 class CodeGenFunction;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000057 class CGDebugInfo;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000058 class CGObjCRuntime;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000059
Daniel Dunbar41071de2008-08-15 23:26:23 +000060/// CodeGenModule - This class organizes the cross-function state that
61/// is used while generating LLVM code.
Reid Spencer5f016e22007-07-11 17:01:13 +000062class CodeGenModule {
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +000063 typedef std::vector< std::pair<llvm::Constant*, int> > CtorList;
64
Reid Spencer5f016e22007-07-11 17:01:13 +000065 ASTContext &Context;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000066 const LangOptions &Features;
Reid Spencer5f016e22007-07-11 17:01:13 +000067 llvm::Module &TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000068 const llvm::TargetData &TheTargetData;
Chris Lattnerfb97b032007-12-02 01:40:18 +000069 Diagnostic &Diags;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 CodeGenTypes Types;
Ted Kremenek815c78f2008-08-05 18:50:11 +000071 CGObjCRuntime* Runtime;
72 CGDebugInfo* DebugInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +000073
74 llvm::Function *MemCpyFn;
Eli Friedman0c995092008-05-26 12:59:39 +000075 llvm::Function *MemMoveFn;
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000076 llvm::Function *MemSetFn;
Daniel Dunbar9986eab2008-07-30 16:32:24 +000077
Daniel Dunbarf1968f22008-10-01 00:49:24 +000078 /// RuntimeFunctions - List of runtime functions whose names must be
79 /// protected from introducing conflicts. These functions should be
80 /// created unnamed, we will name them and patch up conflicts when
81 /// we release the module.
82 std::vector< std::pair<llvm::Function*, std::string> > RuntimeFunctions;
83
Daniel Dunbar3c827a72008-08-05 23:31:02 +000084 /// GlobalDeclMap - Mapping of decl names global variables we have
Daniel Dunbar9986eab2008-07-30 16:32:24 +000085 /// already emitted. Note that the entries in this map are the
Daniel Dunbar3c827a72008-08-05 23:31:02 +000086 /// actual globals and therefore may not be of the same type as the
87 /// decl, they should be bitcasted on retrieval. Also note that the
88 /// globals are keyed on their source name, not the global name
89 /// (which may change with attributes such as asm-labels).
Daniel Dunbar90db8822008-08-25 06:18:57 +000090 llvm::DenseMap<IdentifierInfo*, llvm::GlobalValue*> GlobalDeclMap;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000091
Daniel Dunbar219df662008-09-08 23:44:31 +000092 /// Aliases - List of aliases in module. These cannot be emitted
93 /// until all the code has been seen, as they reference things by
94 /// name instead of directly and may reference forward.
95 std::vector<const FunctionDecl*> Aliases;
96
97 /// StaticDecls - List of static global for which code generation is
98 /// delayed. When the translation unit has been fully processed we
99 /// will lazily emit definitions for only the decls that were
100 /// actually used. This should contain only Function and Var decls,
101 /// and only those which actually define something.
Anders Carlssonb723f752009-01-04 02:08:04 +0000102 std::list<const ValueDecl*> StaticDecls;
Nate Begeman4c13b7a2008-04-20 06:29:50 +0000103
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000104 /// GlobalCtors - Store the list of global constructors and their
105 /// respective priorities to be emitted when the translation unit is
106 /// complete.
107 CtorList GlobalCtors;
108
109 /// GlobalDtors - Store the list of global destructors and their
110 /// respective priorities to be emitted when the translation unit is
111 /// complete.
112 CtorList GlobalDtors;
113
Nate Begeman532485c2008-04-18 23:43:57 +0000114 std::vector<llvm::Constant*> Annotations;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000115
Anders Carlssonc9e20912007-08-21 00:21:21 +0000116 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000117 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000118
119 /// CFConstantStringClassRef - Cached reference to the class for
120 /// constant strings. This value has type int * but is actually an
121 /// Obj-C class pointer.
Anders Carlssonc9e20912007-08-21 00:21:21 +0000122 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000123
124 std::vector<llvm::Function *> BuiltinFunctions;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000126 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000127 const llvm::TargetData &TD, Diagnostic &Diags,
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000128 bool GenerateDebugInfo);
Ted Kremenek815c78f2008-08-05 18:50:11 +0000129
Chris Lattner2b94fe32008-03-01 08:45:05 +0000130 ~CodeGenModule();
Reid Spencer5f016e22007-07-11 17:01:13 +0000131
Ted Kremenek815c78f2008-08-05 18:50:11 +0000132 /// Release - Finalize LLVM code generation.
133 void Release();
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000134
135 /// getObjCRuntime() - Return a reference to the configured
136 /// Objective-C runtime.
137 CGObjCRuntime &getObjCRuntime() {
138 assert(Runtime && "No Objective-C runtime has been configured.");
139 return *Runtime;
140 }
Ted Kremenek815c78f2008-08-05 18:50:11 +0000141
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000142 /// hasObjCRuntime() - Return true iff an Objective-C runtime has
143 /// been configured.
144 bool hasObjCRuntime() { return !!Runtime; }
145
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000146 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ASTContext &getContext() const { return Context; }
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000148 const LangOptions &getLangOptions() const { return Features; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 llvm::Module &getModule() const { return TheModule; }
150 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerfb97b032007-12-02 01:40:18 +0000151 Diagnostic &getDiags() const { return Diags; }
Chris Lattner8f925282008-01-03 06:36:51 +0000152 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000153
154 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address
155 /// of the given global variable.
156 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D);
157
158 /// GetAddrOfFunction - Return the llvm::Constant for the address
159 /// of the given function.
160 llvm::Constant *GetAddrOfFunction(const FunctionDecl *D);
Daniel Dunbar61432932008-08-13 23:20:05 +0000161
162 /// GetStringForStringLiteral - Return the appropriate bytes for a
163 /// string literal, properly padded to match the literal type. If
164 /// only the address of a constant is needed consider using
165 /// GetAddrOfConstantStringLiteral.
166 std::string GetStringForStringLiteral(const StringLiteral *E);
167
Daniel Dunbar41071de2008-08-15 23:26:23 +0000168 /// GetAddrOfConstantCFString - Return a pointer to a
169 /// constant CFString object for the given string.
Anders Carlssonc9e20912007-08-21 00:21:21 +0000170 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000171
Daniel Dunbar61432932008-08-13 23:20:05 +0000172 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
173 /// constant array for the given string literal.
174 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
Daniel Dunbar1e049762008-08-10 20:25:57 +0000175
Daniel Dunbar61432932008-08-13 23:20:05 +0000176 /// GetAddrOfConstantString - Returns a pointer to a character array
177 /// containing the literal. This contents are exactly that of the
178 /// given string, i.e. it will not be null terminated automatically;
179 /// see GetAddrOfConstantCString. Note that whether the result is
180 /// actually a pointer to an LLVM constant depends on
181 /// Feature.WriteableStrings.
182 ///
183 /// The result has pointer to array type.
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000184 ///
185 /// \param GlobalName If provided, the name to use for the global
186 /// (if one is created).
187 llvm::Constant *GetAddrOfConstantString(const std::string& str,
188 const char *GlobalName=0);
Daniel Dunbar61432932008-08-13 23:20:05 +0000189
190 /// GetAddrOfConstantCString - Returns a pointer to a character
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000191 /// array containing the literal and a terminating '\0'
Daniel Dunbar61432932008-08-13 23:20:05 +0000192 /// character. The result has pointer to array type.
Daniel Dunbar5fabf9d2008-10-17 21:56:50 +0000193 ///
194 /// \param GlobalName If provided, the name to use for the global
195 /// (if one is created).
196 llvm::Constant *GetAddrOfConstantCString(const std::string &str,
197 const char *GlobalName=0);
Daniel Dunbar41071de2008-08-15 23:26:23 +0000198
199 /// getBuiltinLibFunction - Given a builtin id for a function like
200 /// "__builtin_fabsf", return a Function* for "fabsf".
201 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Daniel Dunbar61432932008-08-13 23:20:05 +0000202
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 llvm::Function *getMemCpyFn();
Eli Friedman0c995092008-05-26 12:59:39 +0000204 llvm::Function *getMemMoveFn();
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +0000205 llvm::Function *getMemSetFn();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000206 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
207 unsigned NumTys = 0);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000208
Daniel Dunbar41071de2008-08-15 23:26:23 +0000209 /// EmitTopLevelDecl - Emit code for a single top level declaration.
210 void EmitTopLevelDecl(Decl *D);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000211
212 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
213
Daniel Dunbarf1968f22008-10-01 00:49:24 +0000214 /// CreateRuntimeFunction - Create a new runtime function whose name
215 /// must be protected from collisions.
216 llvm::Function *CreateRuntimeFunction(const llvm::FunctionType *Ty,
217 const std::string &Name);
218
Chris Lattnerc5b88062008-02-06 05:08:19 +0000219 void UpdateCompletedType(const TagDecl *D);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000220 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000221 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
222 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000223
Daniel Dunbar488e9932008-08-16 00:56:44 +0000224 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000225 /// specified stmt yet.
226 /// \param OmitOnError - If true, then this error should only be
227 /// emitted if no other errors have been reported.
228 void ErrorUnsupported(const Stmt *S, const char *Type,
229 bool OmitOnError=false);
Chris Lattner2c8569d2007-12-02 07:19:18 +0000230
Daniel Dunbar488e9932008-08-16 00:56:44 +0000231 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000232 /// specified decl yet.
Daniel Dunbar90df4b62008-09-04 03:43:08 +0000233 /// \param OmitOnError - If true, then this error should only be
234 /// emitted if no other errors have been reported.
235 void ErrorUnsupported(const Decl *D, const char *Type,
236 bool OmitOnError=false);
Dan Gohman4f8d1232008-05-22 00:50:06 +0000237
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000238 void SetMethodAttributes(const ObjCMethodDecl *MD,
239 llvm::Function *F);
240
Devang Patel761d7f72008-09-25 21:02:23 +0000241 void SetFunctionAttributes(const Decl *D,
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000242 const CGFunctionInfo &Info,
Daniel Dunbarb7688072008-09-10 00:41:16 +0000243 llvm::Function *F);
244
245 /// ReturnTypeUsesSret - Return true iff the given type uses 'sret'
246 /// when used as a return type.
247 bool ReturnTypeUsesSret(QualType RetTy);
248
Devang Patel761d7f72008-09-25 21:02:23 +0000249 void ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbarb7688072008-09-10 00:41:16 +0000250 const ArgTypeIterator begin,
251 const ArgTypeIterator end,
Devang Patel761d7f72008-09-25 21:02:23 +0000252 AttributeListType &PAL);
Daniel Dunbarb7688072008-09-10 00:41:16 +0000253
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000254private:
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000255 /// SetFunctionAttributesForDefinition - Set function attributes
256 /// specific to a function definition.
Daniel Dunbar219df662008-09-08 23:44:31 +0000257 /// \param D - The ObjCMethodDecl or FunctionDecl defining \arg F.
258 void SetFunctionAttributesForDefinition(const Decl *D,
259 llvm::Function *F);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000260
Eli Friedmanff4a2d92008-06-01 15:54:49 +0000261 void SetFunctionAttributes(const FunctionDecl *FD,
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000262 llvm::Function *F);
Nuno Lopesd4cbda62008-06-08 15:45:52 +0000263
Daniel Dunbar41071de2008-08-15 23:26:23 +0000264 /// EmitGlobal - Emit code for a singal global function or var
265 /// decl. Forward declarations are emitted lazily.
266 void EmitGlobal(const ValueDecl *D);
267
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000268 void EmitGlobalDefinition(const ValueDecl *D);
269 llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D);
270 void EmitGlobalFunctionDefinition(const FunctionDecl *D);
271 void EmitGlobalVarDefinition(const VarDecl *D);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000272 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000273
274 // FIXME: Hardcoding priority here is gross.
275 void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
276 void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000277
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000278 /// EmitCtorList - Generates a global array of functions and
279 /// priorities using the given list and name. This array will have
280 /// appending linkage and is suitable for use as a LLVM constructor
281 /// or destructor array.
282 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
283
Daniel Dunbar219df662008-09-08 23:44:31 +0000284 void EmitAliases(void);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000285 void EmitAnnotations(void);
286 void EmitStatics(void);
Daniel Dunbarf1968f22008-10-01 00:49:24 +0000287
288 void BindRuntimeFunctions();
Reid Spencer5f016e22007-07-11 17:01:13 +0000289};
290} // end namespace CodeGen
291} // end namespace clang
292
293#endif