blob: bb5de154fb18ed2063f5dd321be2902a8f654cbe [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"
Chris Lattner2b94fe32008-03-01 08:45:05 +000018#include "CGObjCRuntime.h"
Dan Gohman4f8d1232008-05-22 00:50:06 +000019#include "clang/AST/Attr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/DenseMap.h"
Anders Carlssonc9e20912007-08-21 00:21:21 +000021#include "llvm/ADT/StringMap.h"
Ted Kremenek815c78f2008-08-05 18:50:11 +000022#include "llvm/ADT/OwningPtr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023
24namespace 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;
Chris Lattner391d77a2008-03-30 23:03:07 +000036 class ObjCMethodDecl;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000037 class ObjCImplementationDecl;
38 class ObjCCategoryImplDecl;
39 class ObjCProtocolDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000040 class Decl;
Oliver Hunt28247232007-12-02 00:11:25 +000041 class Expr;
Chris Lattner2c8569d2007-12-02 07:19:18 +000042 class Stmt;
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;
54
Reid Spencer5f016e22007-07-11 17:01:13 +000055/// CodeGenModule - This class organizes the cross-module state that is used
56/// while generating LLVM code.
57class CodeGenModule {
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +000058 typedef std::vector< std::pair<llvm::Constant*, int> > CtorList;
59
Reid Spencer5f016e22007-07-11 17:01:13 +000060 ASTContext &Context;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000061 const LangOptions &Features;
Reid Spencer5f016e22007-07-11 17:01:13 +000062 llvm::Module &TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000063 const llvm::TargetData &TheTargetData;
Chris Lattnerfb97b032007-12-02 01:40:18 +000064 Diagnostic &Diags;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 CodeGenTypes Types;
Ted Kremenek815c78f2008-08-05 18:50:11 +000066 CGObjCRuntime* Runtime;
67 CGDebugInfo* DebugInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +000068
69 llvm::Function *MemCpyFn;
Eli Friedman0c995092008-05-26 12:59:39 +000070 llvm::Function *MemMoveFn;
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000071 llvm::Function *MemSetFn;
Daniel Dunbar9986eab2008-07-30 16:32:24 +000072
73 /// GlobalDeclMap - Mapping of decls to global variables we have
74 /// already emitted. Note that the entries in this map are the
75 /// actual global and therefore may not be of the same type as the
76 /// decl, they should be bitcasted on retrieval.
77 llvm::DenseMap<const Decl*, llvm::GlobalValue*> GlobalDeclMap;
Daniel Dunbarbd012ff2008-07-29 23:18:29 +000078
79 /// List of static global for which code generation is delayed. When
80 /// the translation unit has been fully processed we will lazily
81 /// emit definitions for only the decls that were actually used.
82 /// This should contain only Function and Var decls, and only those
83 /// which actually define something.
84 std::vector<const ValueDecl*> StaticDecls;
Nate Begeman4c13b7a2008-04-20 06:29:50 +000085
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +000086 /// GlobalCtors - Store the list of global constructors and their
87 /// respective priorities to be emitted when the translation unit is
88 /// complete.
89 CtorList GlobalCtors;
90
91 /// GlobalDtors - Store the list of global destructors and their
92 /// respective priorities to be emitted when the translation unit is
93 /// complete.
94 CtorList GlobalDtors;
95
Nate Begeman532485c2008-04-18 23:43:57 +000096 std::vector<llvm::Constant*> Annotations;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000097
Anders Carlssonc9e20912007-08-21 00:21:21 +000098 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000099 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlssonc9e20912007-08-21 00:21:21 +0000100 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000101
102 std::vector<llvm::Function *> BuiltinFunctions;
Reid Spencer5f016e22007-07-11 17:01:13 +0000103public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000104 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000105 const llvm::TargetData &TD, Diagnostic &Diags,
106 bool GenerateDebugInfo);
Ted Kremenek815c78f2008-08-05 18:50:11 +0000107
Chris Lattner2b94fe32008-03-01 08:45:05 +0000108 ~CodeGenModule();
Reid Spencer5f016e22007-07-11 17:01:13 +0000109
Ted Kremenek815c78f2008-08-05 18:50:11 +0000110 /// Release - Finalize LLVM code generation.
111 void Release();
112
Chris Lattner2b94fe32008-03-01 08:45:05 +0000113 CGObjCRuntime *getObjCRuntime() { return Runtime; }
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000114 CGDebugInfo *getDebugInfo() { return DebugInfo; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ASTContext &getContext() const { return Context; }
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000116 const LangOptions &getLangOptions() const { return Features; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 llvm::Module &getModule() const { return TheModule; }
118 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerfb97b032007-12-02 01:40:18 +0000119 Diagnostic &getDiags() const { return Diags; }
Chris Lattner8f925282008-01-03 06:36:51 +0000120 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000121
122 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address
123 /// of the given global variable.
124 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D);
125
126 /// GetAddrOfFunction - Return the llvm::Constant for the address
127 /// of the given function.
128 llvm::Constant *GetAddrOfFunction(const FunctionDecl *D);
Chris Lattner58c3f9e2007-12-02 06:27:33 +0000129
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000130 /// getBuiltinLibFunction - Given a builtin id for a function like
131 /// "__builtin_fabsf", return a Function* for "fabsf".
132 ///
133 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlssonc9e20912007-08-21 00:21:21 +0000134 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000135
136 /// GetAddrOfConstantString -- returns a pointer to the character
137 /// array containing the literal. The result is pointer to array type.
Chris Lattner45e8cbd2007-11-28 05:34:05 +0000138 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 llvm::Function *getMemCpyFn();
Eli Friedman0c995092008-05-26 12:59:39 +0000140 llvm::Function *getMemMoveFn();
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +0000141 llvm::Function *getMemSetFn();
Chris Lattner7acda7c2007-12-18 00:25:38 +0000142 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
143 unsigned NumTys = 0);
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000144
Chris Lattner391d77a2008-03-30 23:03:07 +0000145 void EmitObjCMethod(const ObjCMethodDecl *OMD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000146 void EmitObjCCategoryImpl(const ObjCCategoryImplDecl *OCD);
147 void EmitObjCClassImplementation(const ObjCImplementationDecl *OID);
148 void EmitObjCProtocolImplementation(const ObjCProtocolDecl *PD);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000149
150 /// EmitGlobal - Emit code for a singal global function or var
151 /// decl. Forward declarations are emitted lazily.
152 void EmitGlobal(const ValueDecl *D);
153
154 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
155
Chris Lattnerc5b88062008-02-06 05:08:19 +0000156 void UpdateCompletedType(const TagDecl *D);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000157 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000158 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
159 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000160
Chris Lattner2c8569d2007-12-02 07:19:18 +0000161 /// WarnUnsupported - Print out a warning that codegen doesn't support the
162 /// specified stmt yet.
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000163
Chris Lattner2c8569d2007-12-02 07:19:18 +0000164 void WarnUnsupported(const Stmt *S, const char *Type);
165
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000166 /// WarnUnsupported - Print out a warning that codegen doesn't support the
167 /// specified decl yet.
168 void WarnUnsupported(const Decl *D, const char *Type);
169
Dan Gohman4f8d1232008-05-22 00:50:06 +0000170 /// setVisibility - Set the visibility for the given LLVM GlobalValue
171 /// according to the given clang AST visibility value.
172 static void setVisibility(llvm::GlobalValue *GV,
173 VisibilityAttr::VisibilityTypes);
174
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000175private:
176 /// ReplaceMapValuesWith - This is a really slow and bad function that
177 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
178 /// them to point to NewVal. This is badbadbad, FIXME!
Daniel Dunbar9986eab2008-07-30 16:32:24 +0000179 void ReplaceMapValuesWith(llvm::GlobalValue *OldVal, llvm::GlobalValue *NewVal);
Eli Friedmanff4a2d92008-06-01 15:54:49 +0000180
181 void SetFunctionAttributes(const FunctionDecl *FD,
182 llvm::Function *F,
183 const llvm::FunctionType *FTy);
Nuno Lopesd4cbda62008-06-08 15:45:52 +0000184
185 void SetGlobalValueAttributes(const FunctionDecl *FD,
186 llvm::GlobalValue *GV);
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000187
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000188 void EmitGlobalDefinition(const ValueDecl *D);
189 llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D);
190 void EmitGlobalFunctionDefinition(const FunctionDecl *D);
191 void EmitGlobalVarDefinition(const VarDecl *D);
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000192
193 // FIXME: Hardcoding priority here is gross.
194 void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
195 void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000196
Daniel Dunbar6bfed7e2008-08-01 00:01:51 +0000197 /// EmitCtorList - Generates a global array of functions and
198 /// priorities using the given list and name. This array will have
199 /// appending linkage and is suitable for use as a LLVM constructor
200 /// or destructor array.
201 void EmitCtorList(const CtorList &Fns, const char *GlobalName);
202
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000203 void EmitAnnotations(void);
204 void EmitStatics(void);
205
Reid Spencer5f016e22007-07-11 17:01:13 +0000206};
207} // end namespace CodeGen
208} // end namespace clang
209
210#endif