blob: bcb464b66aedb07aeafd0e37ae1d708b08cde14f [file] [log] [blame]
Daniel Dunbar9c426522008-07-29 23:18:29 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
Chris Lattnerf97fe382007-05-24 06:29:05 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerf97fe382007-05-24 06:29:05 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This is the internal per-translation-unit state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnerc18bfbb2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENMODULE_H
15#define CLANG_CODEGEN_CODEGENMODULE_H
Chris Lattnerf97fe382007-05-24 06:29:05 +000016
Chris Lattner2ccb73b2007-06-16 00:16:26 +000017#include "CodeGenTypes.h"
Dan Gohman75d69da2008-05-22 00:50:06 +000018#include "clang/AST/Attr.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000019#include "llvm/ADT/DenseMap.h"
Anders Carlssonb04ea612007-08-21 00:21:21 +000020#include "llvm/ADT/StringMap.h"
Chris Lattner2ccb73b2007-06-16 00:16:26 +000021
Chris Lattnerf97fe382007-05-24 06:29:05 +000022namespace llvm {
23 class Module;
Chris Lattner2052bc82007-06-16 00:12:05 +000024 class Constant;
Chris Lattner09153c02007-06-22 18:48:09 +000025 class Function;
Nate Begemanfaae0812008-04-19 04:17:09 +000026 class GlobalValue;
Devang Patel75ef2f02007-10-31 20:01:01 +000027 class TargetData;
Eli Friedmanf6b091f2008-06-01 15:54:49 +000028 class FunctionType;
Chris Lattner23b7eb62007-06-15 23:05:46 +000029}
30
Chris Lattnerf97fe382007-05-24 06:29:05 +000031namespace clang {
32 class ASTContext;
33 class FunctionDecl;
Daniel Dunbareb998102008-08-25 06:18:57 +000034 class IdentifierInfo;
Chris Lattner4bd55962008-03-30 23:03:07 +000035 class ObjCMethodDecl;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000036 class ObjCImplementationDecl;
37 class ObjCCategoryImplDecl;
38 class ObjCProtocolDecl;
Chris Lattner2052bc82007-06-16 00:12:05 +000039 class Decl;
Oliver Huntaefc8fd2007-12-02 00:11:25 +000040 class Expr;
Chris Lattnerd45aa2a2007-12-02 07:19:18 +000041 class Stmt;
Daniel Dunbar6dfdf8c2008-08-10 20:25:57 +000042 class StringLiteral;
Nate Begeman1c906202008-04-20 20:38:08 +000043 class NamedDecl;
Daniel Dunbar9c426522008-07-29 23:18:29 +000044 class ValueDecl;
Chris Lattner37bd2ec2007-12-18 08:16:44 +000045 class VarDecl;
Chris Lattnerfb300092007-11-28 05:34:05 +000046 struct LangOptions;
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +000047 class Diagnostic;
Nate Begemanfaae0812008-04-19 04:17:09 +000048 class AnnotateAttr;
Chris Lattnerf97fe382007-05-24 06:29:05 +000049
50namespace CodeGen {
51
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000052 class CodeGenFunction;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000053 class CGDebugInfo;
Daniel Dunbar89da6ad2008-08-13 00:59:25 +000054 class CGObjCRuntime;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000055
Daniel Dunbarfce4be82008-08-15 23:26:23 +000056/// CodeGenModule - This class organizes the cross-function state that
57/// is used while generating LLVM code.
Chris Lattnerbed31442007-05-28 01:07:47 +000058class CodeGenModule {
Daniel Dunbar74aa7e12008-08-01 00:01:51 +000059 typedef std::vector< std::pair<llvm::Constant*, int> > CtorList;
60
Chris Lattnerf97fe382007-05-24 06:29:05 +000061 ASTContext &Context;
Chris Lattnerfb300092007-11-28 05:34:05 +000062 const LangOptions &Features;
Chris Lattner23b7eb62007-06-15 23:05:46 +000063 llvm::Module &TheModule;
Devang Patel75ef2f02007-10-31 20:01:01 +000064 const llvm::TargetData &TheTargetData;
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +000065 Diagnostic &Diags;
Chris Lattner2ccb73b2007-06-16 00:16:26 +000066 CodeGenTypes Types;
Ted Kremenek2c674f62008-08-05 18:50:11 +000067 CGObjCRuntime* Runtime;
68 CGDebugInfo* DebugInfo;
Chris Lattner2ccb73b2007-06-16 00:16:26 +000069
Chris Lattner09153c02007-06-22 18:48:09 +000070 llvm::Function *MemCpyFn;
Eli Friedmandf649f32008-05-26 12:59:39 +000071 llvm::Function *MemMoveFn;
Lauro Ramos Venancio91fdb9e2008-02-19 22:01:01 +000072 llvm::Function *MemSetFn;
Daniel Dunbare49df9b52008-07-30 16:32:24 +000073
Daniel Dunbar829e9882008-08-05 23:31:02 +000074 /// GlobalDeclMap - Mapping of decl names global variables we have
Daniel Dunbare49df9b52008-07-30 16:32:24 +000075 /// already emitted. Note that the entries in this map are the
Daniel Dunbar829e9882008-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 Dunbareb998102008-08-25 06:18:57 +000080 llvm::DenseMap<IdentifierInfo*, llvm::GlobalValue*> GlobalDeclMap;
Daniel Dunbar9c426522008-07-29 23:18:29 +000081
Daniel Dunbar0beedc12008-09-08 23:44:31 +000082 /// Aliases - List of aliases in module. These cannot be emitted
83 /// until all the code has been seen, as they reference things by
84 /// name instead of directly and may reference forward.
85 std::vector<const FunctionDecl*> Aliases;
86
87 /// StaticDecls - List of static global for which code generation is
88 /// delayed. When the translation unit has been fully processed we
89 /// will lazily emit definitions for only the decls that were
90 /// actually used. This should contain only Function and Var decls,
91 /// and only those which actually define something.
Daniel Dunbar9c426522008-07-29 23:18:29 +000092 std::vector<const ValueDecl*> StaticDecls;
Nate Begeman8e8d4982008-04-20 06:29:50 +000093
Daniel Dunbar74aa7e12008-08-01 00:01:51 +000094 /// GlobalCtors - Store the list of global constructors and their
95 /// respective priorities to be emitted when the translation unit is
96 /// complete.
97 CtorList GlobalCtors;
98
99 /// GlobalDtors - Store the list of global destructors and their
100 /// respective priorities to be emitted when the translation unit is
101 /// complete.
102 CtorList GlobalDtors;
103
Nate Begeman7fab5782008-04-18 23:43:57 +0000104 std::vector<llvm::Constant*> Annotations;
Chris Lattnerfb300092007-11-28 05:34:05 +0000105
Anders Carlssonb04ea612007-08-21 00:21:21 +0000106 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattnerfb300092007-11-28 05:34:05 +0000107 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Daniel Dunbard644ad62008-08-23 18:37:06 +0000108
109 /// CFConstantStringClassRef - Cached reference to the class for
110 /// constant strings. This value has type int * but is actually an
111 /// Obj-C class pointer.
Anders Carlssonb04ea612007-08-21 00:21:21 +0000112 llvm::Constant *CFConstantStringClassRef;
Chris Lattner1eec6602007-08-31 04:31:45 +0000113
114 std::vector<llvm::Function *> BuiltinFunctions;
Chris Lattnerf97fe382007-05-24 06:29:05 +0000115public:
Chris Lattnerfb300092007-11-28 05:34:05 +0000116 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000117 const llvm::TargetData &TD, Diagnostic &Diags,
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000118 bool GenerateDebugInfo);
Ted Kremenek2c674f62008-08-05 18:50:11 +0000119
Chris Lattnera087ff92008-03-01 08:45:05 +0000120 ~CodeGenModule();
Chris Lattnerf97fe382007-05-24 06:29:05 +0000121
Ted Kremenek2c674f62008-08-05 18:50:11 +0000122 /// Release - Finalize LLVM code generation.
123 void Release();
Daniel Dunbar8d480592008-08-11 18:12:00 +0000124
125 /// getObjCRuntime() - Return a reference to the configured
126 /// Objective-C runtime.
127 CGObjCRuntime &getObjCRuntime() {
128 assert(Runtime && "No Objective-C runtime has been configured.");
129 return *Runtime;
130 }
Ted Kremenek2c674f62008-08-05 18:50:11 +0000131
Daniel Dunbar8d480592008-08-11 18:12:00 +0000132 /// hasObjCRuntime() - Return true iff an Objective-C runtime has
133 /// been configured.
134 bool hasObjCRuntime() { return !!Runtime; }
135
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000136 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Chris Lattnerd1af2d22007-05-29 23:17:50 +0000137 ASTContext &getContext() const { return Context; }
Chris Lattnerfb300092007-11-28 05:34:05 +0000138 const LangOptions &getLangOptions() const { return Features; }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000139 llvm::Module &getModule() const { return TheModule; }
Chris Lattner2ccb73b2007-06-16 00:16:26 +0000140 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +0000141 Diagnostic &getDiags() const { return Diags; }
Chris Lattner20455f22008-01-03 06:36:51 +0000142 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbar9c426522008-07-29 23:18:29 +0000143
144 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address
145 /// of the given global variable.
146 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D);
147
148 /// GetAddrOfFunction - Return the llvm::Constant for the address
149 /// of the given function.
150 llvm::Constant *GetAddrOfFunction(const FunctionDecl *D);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000151
152 /// GetStringForStringLiteral - Return the appropriate bytes for a
153 /// string literal, properly padded to match the literal type. If
154 /// only the address of a constant is needed consider using
155 /// GetAddrOfConstantStringLiteral.
156 std::string GetStringForStringLiteral(const StringLiteral *E);
157
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000158 /// GetAddrOfConstantCFString - Return a pointer to a
159 /// constant CFString object for the given string.
Anders Carlssonb04ea612007-08-21 00:21:21 +0000160 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattner36fc8792008-02-11 00:02:17 +0000161
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000162 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
163 /// constant array for the given string literal.
164 llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
Daniel Dunbar6dfdf8c2008-08-10 20:25:57 +0000165
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000166 /// GetAddrOfConstantString - Returns a pointer to a character array
167 /// containing the literal. This contents are exactly that of the
168 /// given string, i.e. it will not be null terminated automatically;
169 /// see GetAddrOfConstantCString. Note that whether the result is
170 /// actually a pointer to an LLVM constant depends on
171 /// Feature.WriteableStrings.
172 ///
173 /// The result has pointer to array type.
Chris Lattnerfb300092007-11-28 05:34:05 +0000174 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000175
176 /// GetAddrOfConstantCString - Returns a pointer to a character
177 /// array containing the literal and a terminating '\-'
178 /// character. The result has pointer to array type.
179 llvm::Constant *GetAddrOfConstantCString(const std::string &str);
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000180
181 /// getBuiltinLibFunction - Given a builtin id for a function like
182 /// "__builtin_fabsf", return a Function* for "fabsf".
183 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Daniel Dunbarc4baa062008-08-13 23:20:05 +0000184
Chris Lattner09153c02007-06-22 18:48:09 +0000185 llvm::Function *getMemCpyFn();
Eli Friedmandf649f32008-05-26 12:59:39 +0000186 llvm::Function *getMemMoveFn();
Lauro Ramos Venancio91fdb9e2008-02-19 22:01:01 +0000187 llvm::Function *getMemSetFn();
Chris Lattnerb8be97e2007-12-18 00:25:38 +0000188 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
189 unsigned NumTys = 0);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000190
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000191 /// EmitTopLevelDecl - Emit code for a single top level declaration.
192 void EmitTopLevelDecl(Decl *D);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000193
194 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
195
Chris Lattner68be6062008-02-06 05:08:19 +0000196 void UpdateCompletedType(const TagDecl *D);
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +0000197 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begemanfaae0812008-04-19 04:17:09 +0000198 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
199 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlsson610ee712008-01-26 01:36:00 +0000200
Daniel Dunbara7c8cf62008-08-16 00:56:44 +0000201 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Daniel Dunbarf2cf6d12008-09-04 03:43:08 +0000202 /// specified stmt yet.
203 /// \param OmitOnError - If true, then this error should only be
204 /// emitted if no other errors have been reported.
205 void ErrorUnsupported(const Stmt *S, const char *Type,
206 bool OmitOnError=false);
Chris Lattnerd45aa2a2007-12-02 07:19:18 +0000207
Daniel Dunbara7c8cf62008-08-16 00:56:44 +0000208 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattner38376f12008-01-12 07:05:38 +0000209 /// specified decl yet.
Daniel Dunbarf2cf6d12008-09-04 03:43:08 +0000210 /// \param OmitOnError - If true, then this error should only be
211 /// emitted if no other errors have been reported.
212 void ErrorUnsupported(const Decl *D, const char *Type,
213 bool OmitOnError=false);
Dan Gohman75d69da2008-05-22 00:50:06 +0000214
Daniel Dunbar449a3392008-09-04 23:41:35 +0000215 void SetMethodAttributes(const ObjCMethodDecl *MD,
216 llvm::Function *F);
217
Chris Lattner5bcdf242007-12-02 07:09:19 +0000218private:
Daniel Dunbar449a3392008-09-04 23:41:35 +0000219 /// SetFunctionAttributesForDefinition - Set function attributes
220 /// specific to a function definition.
Daniel Dunbar0beedc12008-09-08 23:44:31 +0000221 /// \param D - The ObjCMethodDecl or FunctionDecl defining \arg F.
222 void SetFunctionAttributesForDefinition(const Decl *D,
223 llvm::Function *F);
Daniel Dunbar449a3392008-09-04 23:41:35 +0000224
Eli Friedmanf6b091f2008-06-01 15:54:49 +0000225 void SetFunctionAttributes(const FunctionDecl *FD,
Daniel Dunbar449a3392008-09-04 23:41:35 +0000226 llvm::Function *F);
Nuno Lopesb6f79532008-06-08 15:45:52 +0000227
Daniel Dunbarfce4be82008-08-15 23:26:23 +0000228 /// EmitGlobal - Emit code for a singal global function or var
229 /// decl. Forward declarations are emitted lazily.
230 void EmitGlobal(const ValueDecl *D);
231
Daniel Dunbar9c426522008-07-29 23:18:29 +0000232 void EmitGlobalDefinition(const ValueDecl *D);
233 llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D);
234 void EmitGlobalFunctionDefinition(const FunctionDecl *D);
235 void EmitGlobalVarDefinition(const VarDecl *D);
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000236 void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000237
238 // FIXME: Hardcoding priority here is gross.
239 void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
240 void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000241
Daniel Dunbar74aa7e12008-08-01 00:01:51 +0000242 /// EmitCtorList - Generates a global array of functions and
243 /// priorities using the given list and name. This array will have
244 /// appending linkage and is suitable for use as a LLVM constructor
245 /// or destructor array.
246 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
247
Daniel Dunbar0beedc12008-09-08 23:44:31 +0000248 void EmitAliases(void);
Daniel Dunbar9c426522008-07-29 23:18:29 +0000249 void EmitAnnotations(void);
250 void EmitStatics(void);
Chris Lattnerf97fe382007-05-24 06:29:05 +0000251};
252} // end namespace CodeGen
253} // end namespace clang
Chris Lattnerf97fe382007-05-24 06:29:05 +0000254
255#endif