2008-07-30 07:18:29 +08:00
|
|
|
//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
|
2007-05-24 14:29:05 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-05-24 14:29:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is the internal per-translation-unit state used for llvm translation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-01 01:10:38 +08:00
|
|
|
#ifndef CLANG_CODEGEN_CODEGENMODULE_H
|
|
|
|
#define CLANG_CODEGEN_CODEGENMODULE_H
|
2007-05-24 14:29:05 +08:00
|
|
|
|
2007-06-16 08:16:26 +08:00
|
|
|
#include "CodeGenTypes.h"
|
2008-05-22 08:50:06 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2007-06-20 12:44:43 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2007-08-21 08:21:21 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2007-06-16 08:16:26 +08:00
|
|
|
|
2008-09-10 08:41:16 +08:00
|
|
|
#include "CGCall.h"
|
|
|
|
|
2007-05-24 14:29:05 +08:00
|
|
|
namespace llvm {
|
|
|
|
class Module;
|
2007-06-16 08:12:05 +08:00
|
|
|
class Constant;
|
2007-06-23 02:48:09 +08:00
|
|
|
class Function;
|
2008-04-19 12:17:09 +08:00
|
|
|
class GlobalValue;
|
2007-11-01 04:01:01 +08:00
|
|
|
class TargetData;
|
2008-06-01 23:54:49 +08:00
|
|
|
class FunctionType;
|
2007-06-16 07:05:46 +08:00
|
|
|
}
|
|
|
|
|
2007-05-24 14:29:05 +08:00
|
|
|
namespace clang {
|
|
|
|
class ASTContext;
|
|
|
|
class FunctionDecl;
|
2008-08-25 14:18:57 +08:00
|
|
|
class IdentifierInfo;
|
2008-03-31 07:03:07 +08:00
|
|
|
class ObjCMethodDecl;
|
2008-06-01 22:13:53 +08:00
|
|
|
class ObjCImplementationDecl;
|
|
|
|
class ObjCCategoryImplDecl;
|
|
|
|
class ObjCProtocolDecl;
|
2007-06-16 08:12:05 +08:00
|
|
|
class Decl;
|
2007-12-02 08:11:25 +08:00
|
|
|
class Expr;
|
2007-12-02 15:19:18 +08:00
|
|
|
class Stmt;
|
2008-08-11 04:25:57 +08:00
|
|
|
class StringLiteral;
|
2008-04-21 04:38:08 +08:00
|
|
|
class NamedDecl;
|
2008-07-30 07:18:29 +08:00
|
|
|
class ValueDecl;
|
2007-12-18 16:16:44 +08:00
|
|
|
class VarDecl;
|
2007-11-28 13:34:05 +08:00
|
|
|
struct LangOptions;
|
2007-12-02 09:40:18 +08:00
|
|
|
class Diagnostic;
|
2008-04-19 12:17:09 +08:00
|
|
|
class AnnotateAttr;
|
2007-05-24 14:29:05 +08:00
|
|
|
|
|
|
|
namespace CodeGen {
|
|
|
|
|
2008-02-27 05:41:45 +08:00
|
|
|
class CodeGenFunction;
|
2008-05-08 16:54:20 +08:00
|
|
|
class CGDebugInfo;
|
2008-08-13 08:59:25 +08:00
|
|
|
class CGObjCRuntime;
|
2008-05-08 16:54:20 +08:00
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
/// CodeGenModule - This class organizes the cross-function state that
|
|
|
|
/// is used while generating LLVM code.
|
2007-05-28 09:07:47 +08:00
|
|
|
class CodeGenModule {
|
2008-08-01 08:01:51 +08:00
|
|
|
typedef std::vector< std::pair<llvm::Constant*, int> > CtorList;
|
|
|
|
|
2007-05-24 14:29:05 +08:00
|
|
|
ASTContext &Context;
|
2007-11-28 13:34:05 +08:00
|
|
|
const LangOptions &Features;
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Module &TheModule;
|
2007-11-01 04:01:01 +08:00
|
|
|
const llvm::TargetData &TheTargetData;
|
2007-12-02 09:40:18 +08:00
|
|
|
Diagnostic &Diags;
|
2007-06-16 08:16:26 +08:00
|
|
|
CodeGenTypes Types;
|
2008-08-06 02:50:11 +08:00
|
|
|
CGObjCRuntime* Runtime;
|
|
|
|
CGDebugInfo* DebugInfo;
|
2007-06-16 08:16:26 +08:00
|
|
|
|
2007-06-23 02:48:09 +08:00
|
|
|
llvm::Function *MemCpyFn;
|
2008-05-26 20:59:39 +08:00
|
|
|
llvm::Function *MemMoveFn;
|
2008-02-20 06:01:01 +08:00
|
|
|
llvm::Function *MemSetFn;
|
2008-07-31 00:32:24 +08:00
|
|
|
|
2008-10-01 08:49:24 +08:00
|
|
|
/// RuntimeFunctions - List of runtime functions whose names must be
|
|
|
|
/// protected from introducing conflicts. These functions should be
|
|
|
|
/// created unnamed, we will name them and patch up conflicts when
|
|
|
|
/// we release the module.
|
|
|
|
std::vector< std::pair<llvm::Function*, std::string> > RuntimeFunctions;
|
|
|
|
|
2008-08-06 07:31:02 +08:00
|
|
|
/// GlobalDeclMap - Mapping of decl names global variables we have
|
2008-07-31 00:32:24 +08:00
|
|
|
/// already emitted. Note that the entries in this map are the
|
2008-08-06 07:31:02 +08:00
|
|
|
/// actual globals and therefore may not be of the same type as the
|
|
|
|
/// decl, they should be bitcasted on retrieval. Also note that the
|
|
|
|
/// globals are keyed on their source name, not the global name
|
|
|
|
/// (which may change with attributes such as asm-labels).
|
2008-08-25 14:18:57 +08:00
|
|
|
llvm::DenseMap<IdentifierInfo*, llvm::GlobalValue*> GlobalDeclMap;
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2008-09-09 07:44:31 +08:00
|
|
|
/// Aliases - List of aliases in module. These cannot be emitted
|
|
|
|
/// until all the code has been seen, as they reference things by
|
|
|
|
/// name instead of directly and may reference forward.
|
|
|
|
std::vector<const FunctionDecl*> Aliases;
|
|
|
|
|
|
|
|
/// StaticDecls - List of static global for which code generation is
|
|
|
|
/// delayed. When the translation unit has been fully processed we
|
|
|
|
/// will lazily emit definitions for only the decls that were
|
|
|
|
/// actually used. This should contain only Function and Var decls,
|
|
|
|
/// and only those which actually define something.
|
2008-07-30 07:18:29 +08:00
|
|
|
std::vector<const ValueDecl*> StaticDecls;
|
2008-04-20 14:29:50 +08:00
|
|
|
|
2008-08-01 08:01:51 +08:00
|
|
|
/// GlobalCtors - Store the list of global constructors and their
|
|
|
|
/// respective priorities to be emitted when the translation unit is
|
|
|
|
/// complete.
|
|
|
|
CtorList GlobalCtors;
|
|
|
|
|
|
|
|
/// GlobalDtors - Store the list of global destructors and their
|
|
|
|
/// respective priorities to be emitted when the translation unit is
|
|
|
|
/// complete.
|
|
|
|
CtorList GlobalDtors;
|
|
|
|
|
2008-04-19 07:43:57 +08:00
|
|
|
std::vector<llvm::Constant*> Annotations;
|
2007-11-28 13:34:05 +08:00
|
|
|
|
2007-08-21 08:21:21 +08:00
|
|
|
llvm::StringMap<llvm::Constant*> CFConstantStringMap;
|
2007-11-28 13:34:05 +08:00
|
|
|
llvm::StringMap<llvm::Constant*> ConstantStringMap;
|
2008-08-24 02:37:06 +08:00
|
|
|
|
|
|
|
/// CFConstantStringClassRef - Cached reference to the class for
|
|
|
|
/// constant strings. This value has type int * but is actually an
|
|
|
|
/// Obj-C class pointer.
|
2007-08-21 08:21:21 +08:00
|
|
|
llvm::Constant *CFConstantStringClassRef;
|
2007-08-31 12:31:45 +08:00
|
|
|
|
|
|
|
std::vector<llvm::Function *> BuiltinFunctions;
|
2007-05-24 14:29:05 +08:00
|
|
|
public:
|
2007-11-28 13:34:05 +08:00
|
|
|
CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
|
2008-05-08 16:54:20 +08:00
|
|
|
const llvm::TargetData &TD, Diagnostic &Diags,
|
2008-08-12 05:35:06 +08:00
|
|
|
bool GenerateDebugInfo);
|
2008-08-06 02:50:11 +08:00
|
|
|
|
2008-03-01 16:45:05 +08:00
|
|
|
~CodeGenModule();
|
2007-05-24 14:29:05 +08:00
|
|
|
|
2008-08-06 02:50:11 +08:00
|
|
|
/// Release - Finalize LLVM code generation.
|
|
|
|
void Release();
|
2008-08-12 02:12:00 +08:00
|
|
|
|
|
|
|
/// getObjCRuntime() - Return a reference to the configured
|
|
|
|
/// Objective-C runtime.
|
|
|
|
CGObjCRuntime &getObjCRuntime() {
|
|
|
|
assert(Runtime && "No Objective-C runtime has been configured.");
|
|
|
|
return *Runtime;
|
|
|
|
}
|
2008-08-06 02:50:11 +08:00
|
|
|
|
2008-08-12 02:12:00 +08:00
|
|
|
/// hasObjCRuntime() - Return true iff an Objective-C runtime has
|
|
|
|
/// been configured.
|
|
|
|
bool hasObjCRuntime() { return !!Runtime; }
|
|
|
|
|
2008-05-08 16:54:20 +08:00
|
|
|
CGDebugInfo *getDebugInfo() { return DebugInfo; }
|
2007-05-30 07:17:50 +08:00
|
|
|
ASTContext &getContext() const { return Context; }
|
2007-11-28 13:34:05 +08:00
|
|
|
const LangOptions &getLangOptions() const { return Features; }
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::Module &getModule() const { return TheModule; }
|
2007-06-16 08:16:26 +08:00
|
|
|
CodeGenTypes &getTypes() { return Types; }
|
2007-12-02 09:40:18 +08:00
|
|
|
Diagnostic &getDiags() const { return Diags; }
|
2008-01-03 14:36:51 +08:00
|
|
|
const llvm::TargetData &getTargetData() const { return TheTargetData; }
|
2008-07-30 07:18:29 +08:00
|
|
|
|
|
|
|
/// GetAddrOfGlobalVar - Return the llvm::Constant for the address
|
|
|
|
/// of the given global variable.
|
|
|
|
llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D);
|
|
|
|
|
|
|
|
/// GetAddrOfFunction - Return the llvm::Constant for the address
|
|
|
|
/// of the given function.
|
|
|
|
llvm::Constant *GetAddrOfFunction(const FunctionDecl *D);
|
2008-08-14 07:20:05 +08:00
|
|
|
|
|
|
|
/// GetStringForStringLiteral - Return the appropriate bytes for a
|
|
|
|
/// string literal, properly padded to match the literal type. If
|
|
|
|
/// only the address of a constant is needed consider using
|
|
|
|
/// GetAddrOfConstantStringLiteral.
|
|
|
|
std::string GetStringForStringLiteral(const StringLiteral *E);
|
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
/// GetAddrOfConstantCFString - Return a pointer to a
|
|
|
|
/// constant CFString object for the given string.
|
2007-08-21 08:21:21 +08:00
|
|
|
llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
|
2008-02-11 08:02:17 +08:00
|
|
|
|
2008-08-14 07:20:05 +08:00
|
|
|
/// GetAddrOfConstantStringFromLiteral - Return a pointer to a
|
|
|
|
/// constant array for the given string literal.
|
|
|
|
llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
|
2008-08-11 04:25:57 +08:00
|
|
|
|
2008-08-14 07:20:05 +08:00
|
|
|
/// GetAddrOfConstantString - Returns a pointer to a character array
|
|
|
|
/// containing the literal. This contents are exactly that of the
|
|
|
|
/// given string, i.e. it will not be null terminated automatically;
|
|
|
|
/// see GetAddrOfConstantCString. Note that whether the result is
|
|
|
|
/// actually a pointer to an LLVM constant depends on
|
|
|
|
/// Feature.WriteableStrings.
|
|
|
|
///
|
|
|
|
/// The result has pointer to array type.
|
2008-10-18 05:56:50 +08:00
|
|
|
///
|
|
|
|
/// \param GlobalName If provided, the name to use for the global
|
|
|
|
/// (if one is created).
|
|
|
|
llvm::Constant *GetAddrOfConstantString(const std::string& str,
|
|
|
|
const char *GlobalName=0);
|
2008-08-14 07:20:05 +08:00
|
|
|
|
|
|
|
/// GetAddrOfConstantCString - Returns a pointer to a character
|
2008-10-18 05:56:50 +08:00
|
|
|
/// array containing the literal and a terminating '\0'
|
2008-08-14 07:20:05 +08:00
|
|
|
/// character. The result has pointer to array type.
|
2008-10-18 05:56:50 +08:00
|
|
|
///
|
|
|
|
/// \param GlobalName If provided, the name to use for the global
|
|
|
|
/// (if one is created).
|
|
|
|
llvm::Constant *GetAddrOfConstantCString(const std::string &str,
|
|
|
|
const char *GlobalName=0);
|
2008-08-16 07:26:23 +08:00
|
|
|
|
|
|
|
/// getBuiltinLibFunction - Given a builtin id for a function like
|
|
|
|
/// "__builtin_fabsf", return a Function* for "fabsf".
|
|
|
|
llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
|
2008-08-14 07:20:05 +08:00
|
|
|
|
2007-06-23 02:48:09 +08:00
|
|
|
llvm::Function *getMemCpyFn();
|
2008-05-26 20:59:39 +08:00
|
|
|
llvm::Function *getMemMoveFn();
|
2008-02-20 06:01:01 +08:00
|
|
|
llvm::Function *getMemSetFn();
|
2007-12-18 08:25:38 +08:00
|
|
|
llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
|
|
|
|
unsigned NumTys = 0);
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
/// EmitTopLevelDecl - Emit code for a single top level declaration.
|
|
|
|
void EmitTopLevelDecl(Decl *D);
|
2008-07-30 07:18:29 +08:00
|
|
|
|
|
|
|
void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }
|
|
|
|
|
2008-10-01 08:49:24 +08:00
|
|
|
/// CreateRuntimeFunction - Create a new runtime function whose name
|
|
|
|
/// must be protected from collisions.
|
|
|
|
llvm::Function *CreateRuntimeFunction(const llvm::FunctionType *Ty,
|
|
|
|
const std::string &Name);
|
|
|
|
|
2008-02-06 13:08:19 +08:00
|
|
|
void UpdateCompletedType(const TagDecl *D);
|
2008-02-27 05:41:45 +08:00
|
|
|
llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
|
2008-04-19 12:17:09 +08:00
|
|
|
llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
|
|
|
|
const AnnotateAttr *AA, unsigned LineNo);
|
2008-01-26 09:36:00 +08:00
|
|
|
|
2008-08-16 08:56:44 +08:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2008-09-04 11:43:08 +08:00
|
|
|
/// specified stmt yet.
|
|
|
|
/// \param OmitOnError - If true, then this error should only be
|
|
|
|
/// emitted if no other errors have been reported.
|
|
|
|
void ErrorUnsupported(const Stmt *S, const char *Type,
|
|
|
|
bool OmitOnError=false);
|
2007-12-02 15:19:18 +08:00
|
|
|
|
2008-08-16 08:56:44 +08:00
|
|
|
/// ErrorUnsupported - Print out an error that codegen doesn't support the
|
2008-01-12 15:05:38 +08:00
|
|
|
/// specified decl yet.
|
2008-09-04 11:43:08 +08:00
|
|
|
/// \param OmitOnError - If true, then this error should only be
|
|
|
|
/// emitted if no other errors have been reported.
|
|
|
|
void ErrorUnsupported(const Decl *D, const char *Type,
|
|
|
|
bool OmitOnError=false);
|
2008-05-22 08:50:06 +08:00
|
|
|
|
2008-09-05 07:41:35 +08:00
|
|
|
void SetMethodAttributes(const ObjCMethodDecl *MD,
|
|
|
|
llvm::Function *F);
|
|
|
|
|
2008-09-26 05:02:23 +08:00
|
|
|
void SetFunctionAttributes(const Decl *D,
|
2008-09-10 12:01:49 +08:00
|
|
|
const CGFunctionInfo &Info,
|
2008-09-10 08:41:16 +08:00
|
|
|
llvm::Function *F);
|
|
|
|
|
|
|
|
/// ReturnTypeUsesSret - Return true iff the given type uses 'sret'
|
|
|
|
/// when used as a return type.
|
|
|
|
bool ReturnTypeUsesSret(QualType RetTy);
|
|
|
|
|
2008-09-26 05:02:23 +08:00
|
|
|
void ConstructAttributeList(const Decl *TargetDecl,
|
2008-09-10 08:41:16 +08:00
|
|
|
const ArgTypeIterator begin,
|
|
|
|
const ArgTypeIterator end,
|
2008-09-26 05:02:23 +08:00
|
|
|
AttributeListType &PAL);
|
2008-09-10 08:41:16 +08:00
|
|
|
|
2007-12-02 15:09:19 +08:00
|
|
|
private:
|
2008-09-05 07:41:35 +08:00
|
|
|
/// SetFunctionAttributesForDefinition - Set function attributes
|
|
|
|
/// specific to a function definition.
|
2008-09-09 07:44:31 +08:00
|
|
|
/// \param D - The ObjCMethodDecl or FunctionDecl defining \arg F.
|
|
|
|
void SetFunctionAttributesForDefinition(const Decl *D,
|
|
|
|
llvm::Function *F);
|
2008-09-05 07:41:35 +08:00
|
|
|
|
2008-06-01 23:54:49 +08:00
|
|
|
void SetFunctionAttributes(const FunctionDecl *FD,
|
2008-09-05 07:41:35 +08:00
|
|
|
llvm::Function *F);
|
2008-06-08 23:45:52 +08:00
|
|
|
|
2008-08-16 07:26:23 +08:00
|
|
|
/// EmitGlobal - Emit code for a singal global function or var
|
|
|
|
/// decl. Forward declarations are emitted lazily.
|
|
|
|
void EmitGlobal(const ValueDecl *D);
|
|
|
|
|
2008-07-30 07:18:29 +08:00
|
|
|
void EmitGlobalDefinition(const ValueDecl *D);
|
|
|
|
llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D);
|
|
|
|
void EmitGlobalFunctionDefinition(const FunctionDecl *D);
|
|
|
|
void EmitGlobalVarDefinition(const VarDecl *D);
|
2008-08-26 16:29:31 +08:00
|
|
|
void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
|
2008-08-01 08:01:51 +08:00
|
|
|
|
|
|
|
// FIXME: Hardcoding priority here is gross.
|
|
|
|
void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
|
|
|
|
void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);
|
|
|
|
|
|
|
|
/// EmitCtorList - Generates a global array of functions and
|
|
|
|
/// priorities using the given list and name. This array will have
|
|
|
|
/// appending linkage and is suitable for use as a LLVM constructor
|
|
|
|
/// or destructor array.
|
|
|
|
void EmitCtorList(const CtorList &Fns, const char *GlobalName);
|
2008-07-30 07:18:29 +08:00
|
|
|
|
2008-09-09 07:44:31 +08:00
|
|
|
void EmitAliases(void);
|
2008-07-30 07:18:29 +08:00
|
|
|
void EmitAnnotations(void);
|
|
|
|
void EmitStatics(void);
|
2008-10-01 08:49:24 +08:00
|
|
|
|
|
|
|
void BindRuntimeFunctions();
|
2007-05-24 14:29:05 +08:00
|
|
|
};
|
|
|
|
} // end namespace CodeGen
|
|
|
|
} // end namespace clang
|
|
|
|
|
|
|
|
#endif
|