blob: 7ea6ef9941e770f4da53525bc1d4c958505d55a5 [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
14#ifndef CODEGEN_CODEGENMODULE_H
15#define CODEGEN_CODEGENMODULE_H
16
17#include "CodeGenTypes.h"
18#include "llvm/ADT/DenseMap.h"
Anders Carlssonc9e20912007-08-21 00:21:21 +000019#include "llvm/ADT/StringMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020
21namespace llvm {
22 class Module;
23 class Constant;
24 class Function;
Devang Patel9e32d4b2007-10-30 21:27:20 +000025 class GlobalVariable;
Devang Patel7a4718e2007-10-31 20:01:01 +000026 class TargetData;
Reid Spencer5f016e22007-07-11 17:01:13 +000027}
28
29namespace clang {
30 class ASTContext;
31 class FunctionDecl;
32 class Decl;
Oliver Hunt28247232007-12-02 00:11:25 +000033 class Expr;
Chris Lattner2c8569d2007-12-02 07:19:18 +000034 class Stmt;
Steve Naroff8e74c932007-09-13 21:41:19 +000035 class ValueDecl;
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000036 class VarDecl;
Chris Lattnerd86e6bc2008-02-05 08:06:13 +000037 class TypeDecl;
Chris Lattner88a69ad2007-07-13 05:13:43 +000038 class FileVarDecl;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000039 struct LangOptions;
Chris Lattnerfb97b032007-12-02 01:40:18 +000040 class Diagnostic;
Reid Spencer5f016e22007-07-11 17:01:13 +000041
42namespace CodeGen {
43
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000044 class CodeGenFunction;
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046/// CodeGenModule - This class organizes the cross-module state that is used
47/// while generating LLVM code.
48class CodeGenModule {
49 ASTContext &Context;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000050 const LangOptions &Features;
Reid Spencer5f016e22007-07-11 17:01:13 +000051 llvm::Module &TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000052 const llvm::TargetData &TheTargetData;
Chris Lattnerfb97b032007-12-02 01:40:18 +000053 Diagnostic &Diags;
Reid Spencer5f016e22007-07-11 17:01:13 +000054 CodeGenTypes Types;
55
56 llvm::Function *MemCpyFn;
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000057 llvm::Function *MemSetFn;
Reid Spencer5f016e22007-07-11 17:01:13 +000058 llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000059
Anders Carlssonc9e20912007-08-21 00:21:21 +000060 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000061 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlssonc9e20912007-08-21 00:21:21 +000062 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerbef20ac2007-08-31 04:31:45 +000063
64 std::vector<llvm::Function *> BuiltinFunctions;
Reid Spencer5f016e22007-07-11 17:01:13 +000065public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +000066 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Chris Lattnerfb97b032007-12-02 01:40:18 +000067 const llvm::TargetData &TD, Diagnostic &Diags);
Reid Spencer5f016e22007-07-11 17:01:13 +000068
69 ASTContext &getContext() const { return Context; }
Chris Lattner45e8cbd2007-11-28 05:34:05 +000070 const LangOptions &getLangOptions() const { return Features; }
Reid Spencer5f016e22007-07-11 17:01:13 +000071 llvm::Module &getModule() const { return TheModule; }
72 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerfb97b032007-12-02 01:40:18 +000073 Diagnostic &getDiags() const { return Diags; }
Chris Lattner8f925282008-01-03 06:36:51 +000074 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Reid Spencer5f016e22007-07-11 17:01:13 +000075
Chris Lattner9cd4fe42007-12-02 07:09:19 +000076 llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
77 bool isDefinition);
Chris Lattner2b9d2ca2007-12-18 08:16:44 +000078 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, bool isDefinition);
Chris Lattnerbef20ac2007-08-31 04:31:45 +000079
Chris Lattner58c3f9e2007-12-02 06:27:33 +000080
Chris Lattnerbef20ac2007-08-31 04:31:45 +000081 /// getBuiltinLibFunction - Given a builtin id for a function like
82 /// "__builtin_fabsf", return a Function* for "fabsf".
83 ///
84 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlssonc9e20912007-08-21 00:21:21 +000085 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattnera7ad98f2008-02-11 00:02:17 +000086
87 /// GetAddrOfConstantString -- returns a pointer to the character
88 /// array containing the literal. The result is pointer to array type.
Chris Lattner45e8cbd2007-11-28 05:34:05 +000089 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Reid Spencer5f016e22007-07-11 17:01:13 +000090 llvm::Function *getMemCpyFn();
Lauro Ramos Venancio41ef30e2008-02-19 22:01:01 +000091 llvm::Function *getMemSetFn();
Chris Lattner7acda7c2007-12-18 00:25:38 +000092 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
93 unsigned NumTys = 0);
Chris Lattnerbef20ac2007-08-31 04:31:45 +000094
Chris Lattner88a69ad2007-07-13 05:13:43 +000095 void EmitFunction(const FunctionDecl *FD);
96 void EmitGlobalVar(const FileVarDecl *D);
Chris Lattner32b266c2007-07-14 00:16:50 +000097 void EmitGlobalVarDeclarator(const FileVarDecl *D);
Chris Lattnerc5b88062008-02-06 05:08:19 +000098 void UpdateCompletedType(const TagDecl *D);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000099 llvm::Constant *EmitGlobalInit(const Expr *E);
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000100 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000101
Chris Lattner2c8569d2007-12-02 07:19:18 +0000102 /// WarnUnsupported - Print out a warning that codegen doesn't support the
103 /// specified stmt yet.
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000104
Chris Lattner2c8569d2007-12-02 07:19:18 +0000105 void WarnUnsupported(const Stmt *S, const char *Type);
106
Chris Lattnerc6fdc342008-01-12 07:05:38 +0000107 /// WarnUnsupported - Print out a warning that codegen doesn't support the
108 /// specified decl yet.
109 void WarnUnsupported(const Decl *D, const char *Type);
110
Chris Lattner9cd4fe42007-12-02 07:09:19 +0000111private:
112 /// ReplaceMapValuesWith - This is a really slow and bad function that
113 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
114 /// them to point to NewVal. This is badbadbad, FIXME!
115 void ReplaceMapValuesWith(llvm::Constant *OldVal, llvm::Constant *NewVal);
116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117};
118} // end namespace CodeGen
119} // end namespace clang
120
121#endif