blob: 6a201be3760865a2412cffce0d6344ccf51fbd68 [file] [log] [blame]
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is the internal per-translation-unit state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2629b882008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Chris Lattner4b009652007-07-25 00:24:17 +000016
17#include "CodeGenTypes.h"
Dan Gohman4751a3a2008-05-22 00:50:06 +000018#include "clang/AST/Attr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/DenseMap.h"
Anders Carlsson36a04872007-08-21 00:21:21 +000020#include "llvm/ADT/StringMap.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021
Daniel Dunbar3ef2e852008-09-10 00:41:16 +000022#include "CGCall.h"
23
Chris Lattner4b009652007-07-25 00:24:17 +000024namespace llvm {
25 class Module;
26 class Constant;
27 class Function;
Nate Begeman8a704172008-04-19 04:17:09 +000028 class GlobalValue;
Devang Patela8fccb82007-10-31 20:01:01 +000029 class TargetData;
Eli Friedman9be42212008-06-01 15:54:49 +000030 class FunctionType;
Chris Lattner4b009652007-07-25 00:24:17 +000031}
32
33namespace clang {
34 class ASTContext;
35 class FunctionDecl;
Daniel Dunbarad30be42008-08-25 06:18:57 +000036 class IdentifierInfo;
Chris Lattnerb326b172008-03-30 23:03:07 +000037 class ObjCMethodDecl;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000038 class ObjCImplementationDecl;
39 class ObjCCategoryImplDecl;
40 class ObjCProtocolDecl;
Chris Lattner4b009652007-07-25 00:24:17 +000041 class Decl;
Oliver Hunt253e0a72007-12-02 00:11:25 +000042 class Expr;
Chris Lattnercf9c9d02007-12-02 07:19:18 +000043 class Stmt;
Daniel Dunbar3c670e12008-08-10 20:25:57 +000044 class StringLiteral;
Nate Begeman91f52012008-04-20 20:38:08 +000045 class NamedDecl;
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +000046 class ValueDecl;
Chris Lattnerd2df2b52007-12-18 08:16:44 +000047 class VarDecl;
Chris Lattnerdb6be562007-11-28 05:34:05 +000048 struct LangOptions;
Chris Lattner22595b82007-12-02 01:40:18 +000049 class Diagnostic;
Nate Begeman8a704172008-04-19 04:17:09 +000050 class AnnotateAttr;
Chris Lattner4b009652007-07-25 00:24:17 +000051
52namespace CodeGen {
53
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +000054 class CodeGenFunction;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000055 class CGDebugInfo;
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000056 class CGObjCRuntime;
Sanjiv Gupta40e56a12008-05-08 08:54:20 +000057
Daniel Dunbar27250d32008-08-15 23:26:23 +000058/// CodeGenModule - This class organizes the cross-function state that
59/// is used while generating LLVM code.
Chris Lattner4b009652007-07-25 00:24:17 +000060class CodeGenModule {
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +000061 typedef std::vector< std::pair<llvm::Constant*, int> > CtorList;
62
Chris Lattner4b009652007-07-25 00:24:17 +000063 ASTContext &Context;
Chris Lattnerdb6be562007-11-28 05:34:05 +000064 const LangOptions &Features;
Chris Lattner4b009652007-07-25 00:24:17 +000065 llvm::Module &TheModule;
Devang Patela8fccb82007-10-31 20:01:01 +000066 const llvm::TargetData &TheTargetData;
Chris Lattner22595b82007-12-02 01:40:18 +000067 Diagnostic &Diags;
Chris Lattner4b009652007-07-25 00:24:17 +000068 CodeGenTypes Types;
Ted Kremenek7c65b6c2008-08-05 18:50:11 +000069 CGObjCRuntime* Runtime;
70 CGDebugInfo* DebugInfo;
Chris Lattner4b009652007-07-25 00:24:17 +000071
72 llvm::Function *MemCpyFn;
Eli Friedman8f08a252008-05-26 12:59:39 +000073 llvm::Function *MemMoveFn;
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +000074 llvm::Function *MemSetFn;
Daniel Dunbar2188c532008-07-30 16:32:24 +000075
Daniel Dunbar18c2ec62008-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 Dunbara31eaf72008-08-05 23:31:02 +000082 /// GlobalDeclMap - Mapping of decl names global variables we have
Daniel Dunbar2188c532008-07-30 16:32:24 +000083 /// already emitted. Note that the entries in this map are the
Daniel Dunbara31eaf72008-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 Dunbarad30be42008-08-25 06:18:57 +000088 llvm::DenseMap<IdentifierInfo*, llvm::GlobalValue*> GlobalDeclMap;
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +000089
Daniel Dunbar566a6502008-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 Dunbar7bf5b3d2008-07-29 23:18:29 +0000100 std::vector<const ValueDecl*> StaticDecls;
Nate Begemanad320b62008-04-20 06:29:50 +0000101
Daniel Dunbardd2e9ca2008-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 Begeman52da5c72008-04-18 23:43:57 +0000112 std::vector<llvm::Constant*> Annotations;
Chris Lattnerdb6be562007-11-28 05:34:05 +0000113
Anders Carlsson36a04872007-08-21 00:21:21 +0000114 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattnerdb6be562007-11-28 05:34:05 +0000115 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Daniel Dunbardbdb9512008-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 Carlsson36a04872007-08-21 00:21:21 +0000120 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerab862cc2007-08-31 04:31:45 +0000121
122 std::vector<llvm::Function *> BuiltinFunctions;
Chris Lattner4b009652007-07-25 00:24:17 +0000123public:
Chris Lattnerdb6be562007-11-28 05:34:05 +0000124 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Gupta40e56a12008-05-08 08:54:20 +0000125 const llvm::TargetData &TD, Diagnostic &Diags,
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000126 bool GenerateDebugInfo);
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000127
Chris Lattnercbfb5512008-03-01 08:45:05 +0000128 ~CodeGenModule();
Chris Lattner4b009652007-07-25 00:24:17 +0000129
Ted Kremenek7c65b6c2008-08-05 18:50:11 +0000130 /// Release - Finalize LLVM code generation.
131 void Release();
Daniel Dunbarfc69bde2008-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 Kremenek7c65b6c2008-08-05 18:50:11 +0000139
Daniel Dunbarfc69bde2008-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 Gupta40e56a12008-05-08 08:54:20 +0000144 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Chris Lattner4b009652007-07-25 00:24:17 +0000145 ASTContext &getContext() const { return Context; }
Chris Lattnerdb6be562007-11-28 05:34:05 +0000146 const LangOptions &getLangOptions() const { return Features; }
Chris Lattner4b009652007-07-25 00:24:17 +0000147 llvm::Module &getModule() const { return TheModule; }
148 CodeGenTypes &getTypes() { return Types; }
Chris Lattner22595b82007-12-02 01:40:18 +0000149 Diagnostic &getDiags() const { return Diags; }
Chris Lattner17c0cb02008-01-03 06:36:51 +0000150 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar7bf5b3d2008-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 Dunbar31fe9c32008-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 Dunbar27250d32008-08-15 23:26:23 +0000166 /// GetAddrOfConstantCFString - Return a pointer to a
167 /// constant CFString object for the given string.
Anders Carlsson36a04872007-08-21 00:21:21 +0000168 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera6dcce32008-02-11 00:02:17 +0000169
Daniel Dunbar31fe9c32008-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 Dunbar3c670e12008-08-10 20:25:57 +0000173
Daniel Dunbar31fe9c32008-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.
Chris Lattnerdb6be562007-11-28 05:34:05 +0000182 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000183
184 /// GetAddrOfConstantCString - Returns a pointer to a character
185 /// array containing the literal and a terminating '\-'
186 /// character. The result has pointer to array type.
187 llvm::Constant *GetAddrOfConstantCString(const std::string &str);
Daniel Dunbar27250d32008-08-15 23:26:23 +0000188
189 /// getBuiltinLibFunction - Given a builtin id for a function like
190 /// "__builtin_fabsf", return a Function* for "fabsf".
191 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000192
Chris Lattner4b009652007-07-25 00:24:17 +0000193 llvm::Function *getMemCpyFn();
Eli Friedman8f08a252008-05-26 12:59:39 +0000194 llvm::Function *getMemMoveFn();
Lauro Ramos Venancioe5bef732008-02-19 22:01:01 +0000195 llvm::Function *getMemSetFn();
Chris Lattner4b23f942007-12-18 00:25:38 +0000196 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
197 unsigned NumTys = 0);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000198
Daniel Dunbar27250d32008-08-15 23:26:23 +0000199 /// EmitTopLevelDecl - Emit code for a single top level declaration.
200 void EmitTopLevelDecl(Decl *D);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000201
202 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
203
Daniel Dunbar18c2ec62008-10-01 00:49:24 +0000204 /// CreateRuntimeFunction - Create a new runtime function whose name
205 /// must be protected from collisions.
206 llvm::Function *CreateRuntimeFunction(const llvm::FunctionType *Ty,
207 const std::string &Name);
208
Chris Lattner9ec3ca22008-02-06 05:08:19 +0000209 void UpdateCompletedType(const TagDecl *D);
Lauro Ramos Venancio934fb022008-02-26 21:41:45 +0000210 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8a704172008-04-19 04:17:09 +0000211 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
212 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlssond76cead2008-01-26 01:36:00 +0000213
Daniel Dunbar9503b782008-08-16 00:56:44 +0000214 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Daniel Dunbar49bddf72008-09-04 03:43:08 +0000215 /// specified stmt yet.
216 /// \param OmitOnError - If true, then this error should only be
217 /// emitted if no other errors have been reported.
218 void ErrorUnsupported(const Stmt *S, const char *Type,
219 bool OmitOnError=false);
Chris Lattnercf9c9d02007-12-02 07:19:18 +0000220
Daniel Dunbar9503b782008-08-16 00:56:44 +0000221 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattner806a5f52008-01-12 07:05:38 +0000222 /// specified decl yet.
Daniel Dunbar49bddf72008-09-04 03:43:08 +0000223 /// \param OmitOnError - If true, then this error should only be
224 /// emitted if no other errors have been reported.
225 void ErrorUnsupported(const Decl *D, const char *Type,
226 bool OmitOnError=false);
Dan Gohman4751a3a2008-05-22 00:50:06 +0000227
Daniel Dunbarf2787002008-09-04 23:41:35 +0000228 void SetMethodAttributes(const ObjCMethodDecl *MD,
229 llvm::Function *F);
230
Devang Patela85a9ef2008-09-25 21:02:23 +0000231 void SetFunctionAttributes(const Decl *D,
Daniel Dunbar3ad1f072008-09-10 04:01:49 +0000232 const CGFunctionInfo &Info,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000233 llvm::Function *F);
234
235 /// ReturnTypeUsesSret - Return true iff the given type uses 'sret'
236 /// when used as a return type.
237 bool ReturnTypeUsesSret(QualType RetTy);
238
Devang Patela85a9ef2008-09-25 21:02:23 +0000239 void ConstructAttributeList(const Decl *TargetDecl,
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000240 const ArgTypeIterator begin,
241 const ArgTypeIterator end,
Devang Patela85a9ef2008-09-25 21:02:23 +0000242 AttributeListType &PAL);
Daniel Dunbar3ef2e852008-09-10 00:41:16 +0000243
Chris Lattner1a3c1e22007-12-02 07:09:19 +0000244private:
Daniel Dunbarf2787002008-09-04 23:41:35 +0000245 /// SetFunctionAttributesForDefinition - Set function attributes
246 /// specific to a function definition.
Daniel Dunbar566a6502008-09-08 23:44:31 +0000247 /// \param D - The ObjCMethodDecl or FunctionDecl defining \arg F.
248 void SetFunctionAttributesForDefinition(const Decl *D,
249 llvm::Function *F);
Daniel Dunbarf2787002008-09-04 23:41:35 +0000250
Eli Friedman9be42212008-06-01 15:54:49 +0000251 void SetFunctionAttributes(const FunctionDecl *FD,
Daniel Dunbarf2787002008-09-04 23:41:35 +0000252 llvm::Function *F);
Nuno Lopes78534382008-06-08 15:45:52 +0000253
Daniel Dunbar27250d32008-08-15 23:26:23 +0000254 /// EmitGlobal - Emit code for a singal global function or var
255 /// decl. Forward declarations are emitted lazily.
256 void EmitGlobal(const ValueDecl *D);
257
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000258 void EmitGlobalDefinition(const ValueDecl *D);
259 llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D);
260 void EmitGlobalFunctionDefinition(const FunctionDecl *D);
261 void EmitGlobalVarDefinition(const VarDecl *D);
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000262 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000263
264 // FIXME: Hardcoding priority here is gross.
265 void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
266 void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000267
Daniel Dunbardd2e9ca2008-08-01 00:01:51 +0000268 /// EmitCtorList - Generates a global array of functions and
269 /// priorities using the given list and name. This array will have
270 /// appending linkage and is suitable for use as a LLVM constructor
271 /// or destructor array.
272 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
273
Daniel Dunbar566a6502008-09-08 23:44:31 +0000274 void EmitAliases(void);
Daniel Dunbar7bf5b3d2008-07-29 23:18:29 +0000275 void EmitAnnotations(void);
276 void EmitStatics(void);
Daniel Dunbar18c2ec62008-10-01 00:49:24 +0000277
278 void BindRuntimeFunctions();
Chris Lattner4b009652007-07-25 00:24:17 +0000279};
280} // end namespace CodeGen
281} // end namespace clang
282
283#endif