blob: d40e35e00ce71e47bef8eb2df1b589daeb625c4a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen --------------===//
2//
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"
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
22namespace llvm {
23 class Module;
24 class Constant;
25 class Function;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000026 class GlobalValue;
Devang Patel7a4718e2007-10-31 20:01:01 +000027 class TargetData;
Reid Spencer5f016e22007-07-11 17:01:13 +000028}
29
30namespace clang {
31 class ASTContext;
32 class FunctionDecl;
Chris Lattner391d77a2008-03-30 23:03:07 +000033 class ObjCMethodDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000034 class Decl;
Oliver Hunt28247232007-12-02 00:11:25 +000035 class Expr;
Chris Lattner2c8569d2007-12-02 07:19:18 +000036 class Stmt;
Steve Naroff8e74c932007-09-13 21:41:19 +000037 class ValueDecl;
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000038 class VarDecl;
Chris Lattnerd86e6bc2008-02-05 08:06:13 +000039 class TypeDecl;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000040 struct LangOptions;
Chris Lattnerfb97b032007-12-02 01:40:18 +000041 class Diagnostic;
Nate Begeman8bd4afe2008-04-19 04:17:09 +000042 class AnnotateAttr;
Reid Spencer5f016e22007-07-11 17:01:13 +000043
44namespace CodeGen {
45
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000046 class CodeGenFunction;
47
Reid Spencer5f016e22007-07-11 17:01:13 +000048/// CodeGenModule - This class organizes the cross-module state that is used
49/// while generating LLVM code.
50class CodeGenModule {
51 ASTContext &Context;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000052 const LangOptions &Features;
Reid Spencer5f016e22007-07-11 17:01:13 +000053 llvm::Module &TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000054 const llvm::TargetData &TheTargetData;
Chris Lattnerfb97b032007-12-02 01:40:18 +000055 Diagnostic &Diags;
Reid Spencer5f016e22007-07-11 17:01:13 +000056 CodeGenTypes Types;
Chris Lattner2b94fe32008-03-01 08:45:05 +000057 CGObjCRuntime *Runtime;
Reid Spencer5f016e22007-07-11 17:01:13 +000058
59 llvm::Function *MemCpyFn;
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000060 llvm::Function *MemSetFn;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
Chris Lattner6d397602008-03-14 17:18:18 +000062 std::vector<llvm::Constant*> GlobalCtors;
Nate Begeman532485c2008-04-18 23:43:57 +000063 std::vector<llvm::Constant*> Annotations;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000064
Anders Carlssonc9e20912007-08-21 00:21:21 +000065 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000066 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlssonc9e20912007-08-21 00:21:21 +000067 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerbef20ac2007-08-31 04:31:45 +000068
69 std::vector<llvm::Function *> BuiltinFunctions;
Reid Spencer5f016e22007-07-11 17:01:13 +000070public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +000071 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Chris Lattnerfb97b032007-12-02 01:40:18 +000072 const llvm::TargetData &TD, Diagnostic &Diags);
Chris Lattner2b94fe32008-03-01 08:45:05 +000073 ~CodeGenModule();
Reid Spencer5f016e22007-07-11 17:01:13 +000074
Chris Lattner2b94fe32008-03-01 08:45:05 +000075 CGObjCRuntime *getObjCRuntime() { return Runtime; }
Reid Spencer5f016e22007-07-11 17:01:13 +000076 ASTContext &getContext() const { return Context; }
Chris Lattner45e8cbd2007-11-28 05:34:05 +000077 const LangOptions &getLangOptions() const { return Features; }
Reid Spencer5f016e22007-07-11 17:01:13 +000078 llvm::Module &getModule() const { return TheModule; }
79 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerfb97b032007-12-02 01:40:18 +000080 Diagnostic &getDiags() const { return Diags; }
Chris Lattner8f925282008-01-03 06:36:51 +000081 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +000082
Chris Lattner9cd4fe42007-12-02 07:09:19 +000083 llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
84 bool isDefinition);
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000085 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, bool isDefinition);
Chris Lattnerbef20ac2007-08-31 04:31:45 +000086
Chris Lattner58c3f9e2007-12-02 06:27:33 +000087
Chris Lattnerbef20ac2007-08-31 04:31:45 +000088 /// getBuiltinLibFunction - Given a builtin id for a function like
89 /// "__builtin_fabsf", return a Function* for "fabsf".
90 ///
91 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlssonc9e20912007-08-21 00:21:21 +000092 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera7ad98f2008-02-11 00:02:17 +000093
94 /// GetAddrOfConstantString -- returns a pointer to the character
95 /// array containing the literal. The result is pointer to array type.
Chris Lattner45e8cbd2007-11-28 05:34:05 +000096 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Reid Spencer5f016e22007-07-11 17:01:13 +000097 llvm::Function *getMemCpyFn();
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000098 llvm::Function *getMemSetFn();
Chris Lattner7acda7c2007-12-18 00:25:38 +000099 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
100 unsigned NumTys = 0);
Chris Lattnerbef20ac2007-08-31 04:31:45 +0000101
Chris Lattner6d397602008-03-14 17:18:18 +0000102 void AddGlobalCtor(llvm::Function * Ctor);
103 void EmitGlobalCtors(void);
Nate Begeman532485c2008-04-18 23:43:57 +0000104 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
105 void EmitAnnotations(void);
Chris Lattner6d397602008-03-14 17:18:18 +0000106
Chris Lattner391d77a2008-03-30 23:03:07 +0000107 void EmitObjCMethod(const ObjCMethodDecl *OMD);
Chris Lattner88a69ad2007-07-13 05:13:43 +0000108 void EmitFunction(const FunctionDecl *FD);
Steve Naroff248a7532008-04-15 22:42:06 +0000109 void EmitGlobalVar(const VarDecl *D);
110 void EmitGlobalVarDeclarator(const VarDecl *D);
Chris Lattnerc5b88062008-02-06 05:08:19 +0000111 void UpdateCompletedType(const TagDecl *D);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000112 llvm::Constant *EmitGlobalInit(const Expr *E);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000113 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Nate Begeman8bd4afe2008-04-19 04:17:09 +0000114 llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
115 const AnnotateAttr *AA, unsigned LineNo);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000116
Chris Lattner2c8569d2007-12-02 07:19:18 +0000117 /// WarnUnsupported - Print out a warning that codegen doesn't support the
118 /// specified stmt yet.
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000119
Chris Lattner2c8569d2007-12-02 07:19:18 +0000120 void WarnUnsupported(const Stmt *S, const char *Type);
121
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000122 /// WarnUnsupported - Print out a warning that codegen doesn't support the
123 /// specified decl yet.
124 void WarnUnsupported(const Decl *D, const char *Type);
125
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000126private:
127 /// ReplaceMapValuesWith - This is a really slow and bad function that
128 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
129 /// them to point to NewVal. This is badbadbad, FIXME!
130 void ReplaceMapValuesWith(llvm::Constant *OldVal, llvm::Constant *NewVal);
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132};
133} // end namespace CodeGen
134} // end namespace clang
135
136#endif