blob: e192e9913ee741c950867adc531094900280857d [file] [log] [blame]
Chris Lattnerbed31442007-05-28 01:07:47 +00001//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen --------------===//
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"
Chris Lattnera087ff92008-03-01 08:45:05 +000018#include "CGObjCRuntime.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;
Devang Patel73721a12007-10-30 21:27:20 +000026 class GlobalVariable;
Devang Patel75ef2f02007-10-31 20:01:01 +000027 class TargetData;
Chris Lattner23b7eb62007-06-15 23:05:46 +000028}
29
Chris Lattnerf97fe382007-05-24 06:29:05 +000030namespace clang {
31 class ASTContext;
32 class FunctionDecl;
Chris Lattner4bd55962008-03-30 23:03:07 +000033 class ObjCMethodDecl;
Chris Lattner2052bc82007-06-16 00:12:05 +000034 class Decl;
Oliver Huntaefc8fd2007-12-02 00:11:25 +000035 class Expr;
Chris Lattnerd45aa2a2007-12-02 07:19:18 +000036 class Stmt;
Steve Naroff9def2b12007-09-13 21:41:19 +000037 class ValueDecl;
Chris Lattner37bd2ec2007-12-18 08:16:44 +000038 class VarDecl;
Chris Lattnerb1537eb2008-02-05 08:06:13 +000039 class TypeDecl;
Chris Lattnerfb300092007-11-28 05:34:05 +000040 struct LangOptions;
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +000041 class Diagnostic;
Chris Lattnerf97fe382007-05-24 06:29:05 +000042
43namespace CodeGen {
44
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +000045 class CodeGenFunction;
46
Chris Lattnerbed31442007-05-28 01:07:47 +000047/// CodeGenModule - This class organizes the cross-module state that is used
48/// while generating LLVM code.
49class CodeGenModule {
Chris Lattnerf97fe382007-05-24 06:29:05 +000050 ASTContext &Context;
Chris Lattnerfb300092007-11-28 05:34:05 +000051 const LangOptions &Features;
Chris Lattner23b7eb62007-06-15 23:05:46 +000052 llvm::Module &TheModule;
Devang Patel75ef2f02007-10-31 20:01:01 +000053 const llvm::TargetData &TheTargetData;
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +000054 Diagnostic &Diags;
Chris Lattner2ccb73b2007-06-16 00:16:26 +000055 CodeGenTypes Types;
Chris Lattnera087ff92008-03-01 08:45:05 +000056 CGObjCRuntime *Runtime;
Chris Lattner2ccb73b2007-06-16 00:16:26 +000057
Chris Lattner09153c02007-06-22 18:48:09 +000058 llvm::Function *MemCpyFn;
Lauro Ramos Venancio91fdb9e2008-02-19 22:01:01 +000059 llvm::Function *MemSetFn;
Chris Lattnerb6984c42007-06-20 04:44:43 +000060 llvm::DenseMap<const Decl*, llvm::Constant*> GlobalDeclMap;
Chris Lattner5ff2d5d2008-03-14 17:18:18 +000061 std::vector<llvm::Constant*> GlobalCtors;
Nate Begeman7fab5782008-04-18 23:43:57 +000062 std::vector<llvm::Constant*> Annotations;
Chris Lattnerfb300092007-11-28 05:34:05 +000063
Anders Carlssonb04ea612007-08-21 00:21:21 +000064 llvm::StringMap<llvm::Constant*> CFConstantStringMap;
Chris Lattnerfb300092007-11-28 05:34:05 +000065 llvm::StringMap<llvm::Constant*> ConstantStringMap;
Anders Carlssonb04ea612007-08-21 00:21:21 +000066 llvm::Constant *CFConstantStringClassRef;
Chris Lattner1eec6602007-08-31 04:31:45 +000067
68 std::vector<llvm::Function *> BuiltinFunctions;
Chris Lattnerf97fe382007-05-24 06:29:05 +000069public:
Chris Lattnerfb300092007-11-28 05:34:05 +000070 CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +000071 const llvm::TargetData &TD, Diagnostic &Diags);
Chris Lattnera087ff92008-03-01 08:45:05 +000072 ~CodeGenModule();
Chris Lattnerf97fe382007-05-24 06:29:05 +000073
Chris Lattnera087ff92008-03-01 08:45:05 +000074 CGObjCRuntime *getObjCRuntime() { return Runtime; }
Chris Lattnerd1af2d22007-05-29 23:17:50 +000075 ASTContext &getContext() const { return Context; }
Chris Lattnerfb300092007-11-28 05:34:05 +000076 const LangOptions &getLangOptions() const { return Features; }
Chris Lattner23b7eb62007-06-15 23:05:46 +000077 llvm::Module &getModule() const { return TheModule; }
Chris Lattner2ccb73b2007-06-16 00:16:26 +000078 CodeGenTypes &getTypes() { return Types; }
Chris Lattnerc8dbe1e2007-12-02 01:40:18 +000079 Diagnostic &getDiags() const { return Diags; }
Chris Lattner20455f22008-01-03 06:36:51 +000080 const llvm::TargetData &getTargetData() const { return TheTargetData; }
Chris Lattnerd1af2d22007-05-29 23:17:50 +000081
Chris Lattner5bcdf242007-12-02 07:09:19 +000082 llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D,
83 bool isDefinition);
Chris Lattner37bd2ec2007-12-18 08:16:44 +000084 llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, bool isDefinition);
Chris Lattner1eec6602007-08-31 04:31:45 +000085
Chris Lattner41af8182007-12-02 06:27:33 +000086
Chris Lattner1eec6602007-08-31 04:31:45 +000087 /// getBuiltinLibFunction - Given a builtin id for a function like
88 /// "__builtin_fabsf", return a Function* for "fabsf".
89 ///
90 llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
Anders Carlssonb04ea612007-08-21 00:21:21 +000091 llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Chris Lattner36fc8792008-02-11 00:02:17 +000092
93 /// GetAddrOfConstantString -- returns a pointer to the character
94 /// array containing the literal. The result is pointer to array type.
Chris Lattnerfb300092007-11-28 05:34:05 +000095 llvm::Constant *GetAddrOfConstantString(const std::string& str);
Chris Lattner09153c02007-06-22 18:48:09 +000096 llvm::Function *getMemCpyFn();
Lauro Ramos Venancio91fdb9e2008-02-19 22:01:01 +000097 llvm::Function *getMemSetFn();
Chris Lattnerb8be97e2007-12-18 00:25:38 +000098 llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
99 unsigned NumTys = 0);
Chris Lattner1eec6602007-08-31 04:31:45 +0000100
Chris Lattner5ff2d5d2008-03-14 17:18:18 +0000101 void AddGlobalCtor(llvm::Function * Ctor);
102 void EmitGlobalCtors(void);
Nate Begeman7fab5782008-04-18 23:43:57 +0000103 void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
104 void EmitAnnotations(void);
Chris Lattner5ff2d5d2008-03-14 17:18:18 +0000105
Chris Lattner4bd55962008-03-30 23:03:07 +0000106 void EmitObjCMethod(const ObjCMethodDecl *OMD);
Chris Lattnerd14bfa92007-07-13 05:13:43 +0000107 void EmitFunction(const FunctionDecl *FD);
Steve Naroff08899ff2008-04-15 22:42:06 +0000108 void EmitGlobalVar(const VarDecl *D);
109 void EmitGlobalVarDeclarator(const VarDecl *D);
Chris Lattner68be6062008-02-06 05:08:19 +0000110 void UpdateCompletedType(const TagDecl *D);
Anders Carlsson610ee712008-01-26 01:36:00 +0000111 llvm::Constant *EmitGlobalInit(const Expr *E);
Lauro Ramos Venancio01a72ff2008-02-26 21:41:45 +0000112 llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
Anders Carlsson610ee712008-01-26 01:36:00 +0000113
Chris Lattnerd45aa2a2007-12-02 07:19:18 +0000114 /// WarnUnsupported - Print out a warning that codegen doesn't support the
115 /// specified stmt yet.
Anders Carlsson610ee712008-01-26 01:36:00 +0000116
Chris Lattnerd45aa2a2007-12-02 07:19:18 +0000117 void WarnUnsupported(const Stmt *S, const char *Type);
118
Chris Lattner38376f12008-01-12 07:05:38 +0000119 /// WarnUnsupported - Print out a warning that codegen doesn't support the
120 /// specified decl yet.
121 void WarnUnsupported(const Decl *D, const char *Type);
122
Chris Lattner5bcdf242007-12-02 07:09:19 +0000123private:
124 /// ReplaceMapValuesWith - This is a really slow and bad function that
125 /// searches for any entries in GlobalDeclMap that point to OldVal, changing
126 /// them to point to NewVal. This is badbadbad, FIXME!
127 void ReplaceMapValuesWith(llvm::Constant *OldVal, llvm::Constant *NewVal);
128
Chris Lattnerf97fe382007-05-24 06:29:05 +0000129};
130} // end namespace CodeGen
131} // end namespace clang
Chris Lattnerf97fe382007-05-24 06:29:05 +0000132
133#endif