blob: 4d4b18ab627b99095838307a8e2599108930edb1 [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 Lattner88a69ad2007-07-13 05:13:43 +000036 class FileVarDecl;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000037 struct LangOptions;
Chris Lattnerfb97b032007-12-02 01:40:18 +000038 class Diagnostic;
Reid Spencer5f016e22007-07-11 17:01:13 +000039
40namespace CodeGen {
41
42/// CodeGenModule - This class organizes the cross-module state that is used
43/// while generating LLVM code.
44class CodeGenModule {
45 ASTContext &Context;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000046 const LangOptions &Features;
Reid Spencer5f016e22007-07-11 17:01:13 +000047 llvm::Module &TheModule;
Devang Patel7a4718e2007-10-31 20:01:01 +000048 const llvm::TargetData &TheTargetData;
Chris Lattnerfb97b032007-12-02 01:40:18 +000049 Diagnostic &Diags;
Reid Spencer5f016e22007-07-11 17:01:13 +000050 CodeGenTypes Types;
51
52 llvm::Function *MemCpyFn;
53 llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000054
Anders Carlssonc9e20912007-08-21 00:21:21 +000055 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattner45e8cbd2007-11-28 05:34:05 +000056 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlssonc9e20912007-08-21 00:21:21 +000057 llvm::Constant *CFConstantStringClassRef;
Chris Lattnerbef20ac2007-08-31 04:31:45 +000058
59 std::vector<llvm::Function *> BuiltinFunctions;
Reid Spencer5f016e22007-07-11 17:01:13 +000060public:
Chris Lattner45e8cbd2007-11-28 05:34:05 +000061 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Chris Lattnerfb97b032007-12-02 01:40:18 +000062 const llvm::TargetData &TD, Diagnostic &Diags);
Reid Spencer5f016e22007-07-11 17:01:13 +000063
64 ASTContext &getContext() const { return Context; }
Chris Lattner45e8cbd2007-11-28 05:34:05 +000065 const LangOptions &getLangOptions() const { return Features; }
Reid Spencer5f016e22007-07-11 17:01:13 +000066 llvm::Module &getModule() const { return TheModule; }
67 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerfb97b032007-12-02 01:40:18 +000068 Diagnostic &getDiags() const { return Diags; }
Reid Spencer5f016e22007-07-11 17:01:13 +000069
Chris Lattner9cd4fe42007-12-02 07:09:19 +000070 llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
71 bool isDefinition);
72 llvm::Constant *GetAddrOfFileVarDecl(const FileVarDecl *D,
73 bool isDefinition);
Chris Lattnerbef20ac2007-08-31 04:31:45 +000074
Chris Lattner58c3f9e2007-12-02 06:27:33 +000075
Chris Lattnerbef20ac2007-08-31 04:31:45 +000076 /// getBuiltinLibFunction - Given a builtin id for a function like
77 /// "__builtin_fabsf", return a Function* for "fabsf".
78 ///
79 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlssonc9e20912007-08-21 00:21:21 +000080 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattner45e8cbd2007-11-28 05:34:05 +000081 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Reid Spencer5f016e22007-07-11 17:01:13 +000082 llvm::Function *getMemCpyFn();
83
Chris Lattnerbef20ac2007-08-31 04:31:45 +000084
Chris Lattner88a69ad2007-07-13 05:13:43 +000085 void EmitFunction(const FunctionDecl *FD);
86 void EmitGlobalVar(const FileVarDecl *D);
Chris Lattner32b266c2007-07-14 00:16:50 +000087 void EmitGlobalVarDeclarator(const FileVarDecl *D);
Oliver Hunt28247232007-12-02 00:11:25 +000088 llvm::Constant *EmitGlobalInit(const Expr *Expression);
Reid Spencer5f016e22007-07-11 17:01:13 +000089
90 void PrintStats() {}
Chris Lattner9cd4fe42007-12-02 07:09:19 +000091
Chris Lattner2c8569d2007-12-02 07:19:18 +000092 /// WarnUnsupported - Print out a warning that codegen doesn't support the
93 /// specified stmt yet.
94 void WarnUnsupported(const Stmt *S, const char *Type);
95
Chris Lattner9cd4fe42007-12-02 07:09:19 +000096private:
97 /// ReplaceMapValuesWith - This is a really slow and bad function that
98 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
99 /// them to point to NewVal. This is badbadbad, FIXME!
100 void ReplaceMapValuesWith(llvm::Constant *OldVal, llvm::Constant *NewVal);
101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102};
103} // end namespace CodeGen
104} // end namespace clang
105
106#endif