#ifndef CLANG_CODEGEN_CODEGENMODULE_H
#define CLANG_CODEGEN_CODEGENMODULE_H
#include "clang/Basic/ABI.h"
#include "clang/Basic/LangOptions.h"
#include "clang/AST/Attr.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/GlobalDecl.h"
#include "clang/AST/Mangle.h"
#include "CGVTables.h"
#include "CodeGenTypes.h"
#include "llvm/Module.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/ValueHandle.h"
namespace llvm {
class Module;
class Constant;
class ConstantInt;
class Function;
class GlobalValue;
class TargetData;
class FunctionType;
class LLVMContext;
}
namespace clang {
class TargetCodeGenInfo;
class ASTContext;
class FunctionDecl;
class IdentifierInfo;
class ObjCMethodDecl;
class ObjCImplementationDecl;
class ObjCCategoryImplDecl;
class ObjCProtocolDecl;
class ObjCEncodeExpr;
class BlockExpr;
class CharUnits;
class Decl;
class Expr;
class Stmt;
class InitListExpr;
class StringLiteral;
class NamedDecl;
class ValueDecl;
class VarDecl;
class LangOptions;
class CodeGenOptions;
class DiagnosticsEngine;
class AnnotateAttr;
class CXXDestructorDecl;
class MangleBuffer;
namespace CodeGen {
class CallArgList;
class CodeGenFunction;
class CodeGenTBAA;
class CGCXXABI;
class CGDebugInfo;
class CGObjCRuntime;
class CGOpenCLRuntime;
class CGCUDARuntime;
class BlockFieldFlags;
class FunctionArgList;
struct OrderGlobalInits {
unsigned int priority;
unsigned int lex_order;
OrderGlobalInits(unsigned int p, unsigned int l)
: priority(p), lex_order(l) {}
bool operator==(const OrderGlobalInits &RHS) const {
return priority == RHS.priority &&
lex_order == RHS.lex_order;
}
bool operator<(const OrderGlobalInits &RHS) const {
if (priority < RHS.priority)
return true;
return priority == RHS.priority && lex_order < RHS.lex_order;
}
};
struct CodeGenTypeCache {
llvm::Type *VoidTy;
llvm::IntegerType *Int8Ty, *Int16Ty, *Int32Ty, *Int64Ty;
llvm::Type *FloatTy, *DoubleTy;
llvm::IntegerType *IntTy;
union {
llvm::IntegerType *IntPtrTy;
llvm::IntegerType *SizeTy;
llvm::IntegerType *PtrDiffTy;
};
union {
llvm::PointerType *VoidPtrTy;
llvm::PointerType *Int8PtrTy;
};
union {
llvm::PointerType *VoidPtrPtrTy;
llvm::PointerType *Int8PtrPtrTy;
};
unsigned char PointerWidthInBits;
union {
unsigned char PointerAlignInBytes;
unsigned char PointerSizeInBytes;
};
};
struct RREntrypoints {
RREntrypoints() { memset(this, 0, sizeof(*this)); }
llvm::Constant *objc_autoreleasePoolPop;
llvm::Constant *objc_autoreleasePoolPush;
};
struct ARCEntrypoints {
ARCEntrypoints() { memset(this, 0, sizeof(*this)); }
llvm::Constant *objc_autorelease;
llvm::Constant *objc_autoreleaseReturnValue;
llvm::Constant *objc_copyWeak;
llvm::Constant *objc_destroyWeak;
llvm::Constant *objc_initWeak;
llvm::Constant *objc_loadWeak;
llvm::Constant *objc_loadWeakRetained;
llvm::Constant *objc_moveWeak;
llvm::Constant *objc_retain;
llvm::Constant *objc_retainAutorelease;
llvm::Constant *objc_retainAutoreleaseReturnValue;
llvm::Constant *objc_retainAutoreleasedReturnValue;
llvm::Constant *objc_retainBlock;
llvm::Constant *objc_release;
llvm::Constant *objc_storeStrong;
llvm::Constant *objc_storeWeak;
llvm::InlineAsm *retainAutoreleasedReturnValueMarker;
};
class CodeGenModule : public CodeGenTypeCache {
CodeGenModule(const CodeGenModule&); void operator=(const CodeGenModule&);
typedef std::vector<std::pair<llvm::Constant*, int> > CtorList;
ASTContext &Context;
const LangOptions &Features;
const CodeGenOptions &CodeGenOpts;
llvm::Module &TheModule;
const llvm::TargetData &TheTargetData;
mutable const TargetCodeGenInfo *TheTargetCodeGenInfo;
DiagnosticsEngine &Diags;
CGCXXABI &ABI;
CodeGenTypes Types;
CodeGenTBAA *TBAA;
CodeGenVTables VTables;
friend class CodeGenVTables;
CGObjCRuntime* ObjCRuntime;
CGOpenCLRuntime* OpenCLRuntime;
CGCUDARuntime* CUDARuntime;
CGDebugInfo* DebugInfo;
ARCEntrypoints *ARCData;
llvm::MDNode *NoObjCARCExceptionsMetadata;
RREntrypoints *RRData;
llvm::SmallPtrSet<llvm::GlobalValue*, 10> WeakRefReferences;
llvm::StringMap<GlobalDecl> DeferredDecls;
std::vector<GlobalDecl> DeferredDeclsToEmit;
std::vector<llvm::WeakVH> LLVMUsed;
CtorList GlobalCtors;
CtorList GlobalDtors;
llvm::DenseMap<GlobalDecl, StringRef> MangledDeclNames;
llvm::BumpPtrAllocator MangledNamesAllocator;
std::vector<llvm::Constant*> Annotations;
llvm::StringMap<llvm::Constant*> AnnotationStrings;
llvm::StringMap<llvm::Constant*> CFConstantStringMap;
llvm::StringMap<llvm::GlobalVariable*> ConstantStringMap;
llvm::DenseMap<const Decl*, llvm::Value*> StaticLocalDeclMap;
llvm::DenseMap<QualType, llvm::Constant *> AtomicSetterHelperFnMap;
llvm::DenseMap<QualType, llvm::Constant *> AtomicGetterHelperFnMap;
std::vector<llvm::Constant*> CXXGlobalInits;
llvm::DenseMap<const Decl*, unsigned> DelayedCXXInitPosition;
SmallVector<std::pair<OrderGlobalInits, llvm::Function*>, 8>
PrioritizedCXXGlobalInits;
std::vector<std::pair<llvm::WeakVH,llvm::Constant*> > CXXGlobalDtors;
llvm::Constant *CFConstantStringClassRef;
llvm::Constant *ConstantStringClassRef;
llvm::StructType *NSConstantStringType;
QualType ObjCFastEnumerationStateType;
void createObjCRuntime();
void createOpenCLRuntime();
void createCUDARuntime();
bool isTriviallyRecursive(const FunctionDecl *F);
bool shouldEmitFunction(const FunctionDecl *F);
llvm::LLVMContext &VMContext;
llvm::Constant *NSConcreteGlobalBlock;
llvm::Constant *NSConcreteStackBlock;
llvm::Constant *BlockObjectAssign;
llvm::Constant *BlockObjectDispose;
llvm::Type *BlockDescriptorType;
llvm::Type *GenericBlockLiteralType;
struct {
int GlobalUniqueCount;
} Block;
public:
CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
llvm::Module &M, const llvm::TargetData &TD,
DiagnosticsEngine &Diags);
~CodeGenModule();
void Release();
CGObjCRuntime &getObjCRuntime() {
if (!ObjCRuntime) createObjCRuntime();
return *ObjCRuntime;
}
bool hasObjCRuntime() { return !!ObjCRuntime; }
CGOpenCLRuntime &getOpenCLRuntime() {
assert(OpenCLRuntime != 0);
return *OpenCLRuntime;
}
CGCUDARuntime &getCUDARuntime() {
assert(CUDARuntime != 0);
return *CUDARuntime;
}
CGCXXABI &getCXXABI() { return ABI; }
ARCEntrypoints &getARCEntrypoints() const {
assert(getLangOptions().ObjCAutoRefCount && ARCData != 0);
return *ARCData;
}
RREntrypoints &getRREntrypoints() const {
assert(RRData != 0);
return *RRData;
}
llvm::Value *getStaticLocalDeclAddress(const VarDecl *VD) {
return StaticLocalDeclMap[VD];
}
void setStaticLocalDeclAddress(const VarDecl *D,
llvm::GlobalVariable *GV) {
StaticLocalDeclMap[D] = GV;
}
llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
return AtomicSetterHelperFnMap[Ty];
}
void setAtomicSetterHelperFnMap(QualType Ty,
llvm::Constant *Fn) {
AtomicSetterHelperFnMap[Ty] = Fn;
}
llvm::Constant *getAtomicGetterHelperFnMap(QualType Ty) {
return AtomicGetterHelperFnMap[Ty];
}
void setAtomicGetterHelperFnMap(QualType Ty,
llvm::Constant *Fn) {
AtomicGetterHelperFnMap[Ty] = Fn;
}
CGDebugInfo *getModuleDebugInfo() { return DebugInfo; }
llvm::MDNode *getNoObjCARCExceptionsMetadata() {
if (!NoObjCARCExceptionsMetadata)
NoObjCARCExceptionsMetadata =
llvm::MDNode::get(getLLVMContext(),
SmallVector<llvm::Value*,1>());
return NoObjCARCExceptionsMetadata;
}
ASTContext &getContext() const { return Context; }
const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; }
const LangOptions &getLangOptions() const { return Features; }
llvm::Module &getModule() const { return TheModule; }
CodeGenTypes &getTypes() { return Types; }
CodeGenVTables &getVTables() { return VTables; }
VTableContext &getVTableContext() { return VTables.getVTableContext(); }
DiagnosticsEngine &getDiags() const { return Diags; }
const llvm::TargetData &getTargetData() const { return TheTargetData; }
const TargetInfo &getTarget() const { return Context.getTargetInfo(); }
llvm::LLVMContext &getLLVMContext() { return VMContext; }
const TargetCodeGenInfo &getTargetCodeGenInfo();
bool isTargetDarwin() const;
bool shouldUseTBAA() const { return TBAA != 0; }
llvm::MDNode *getTBAAInfo(QualType QTy);
bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
static void DecorateInstruction(llvm::Instruction *Inst,
llvm::MDNode *TBAAInfo);
llvm::ConstantInt *getSize(CharUnits numChars);
void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const;
enum TypeVisibilityKind {
TVK_ForVTT,
TVK_ForVTable,
TVK_ForConstructionVTable,
TVK_ForRTTI,
TVK_ForRTTIName
};
void setTypeVisibility(llvm::GlobalValue *GV, const CXXRecordDecl *D,
TypeVisibilityKind TVK) const;
static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V) {
switch (V) {
case DefaultVisibility: return llvm::GlobalValue::DefaultVisibility;
case HiddenVisibility: return llvm::GlobalValue::HiddenVisibility;
case ProtectedVisibility: return llvm::GlobalValue::ProtectedVisibility;
}
llvm_unreachable("unknown visibility!");
}
llvm::Constant *GetAddrOfGlobal(GlobalDecl GD) {
if (isa<CXXConstructorDecl>(GD.getDecl()))
return GetAddrOfCXXConstructor(cast<CXXConstructorDecl>(GD.getDecl()),
GD.getCtorType());
else if (isa<CXXDestructorDecl>(GD.getDecl()))
return GetAddrOfCXXDestructor(cast<CXXDestructorDecl>(GD.getDecl()),
GD.getDtorType());
else if (isa<FunctionDecl>(GD.getDecl()))
return GetAddrOfFunction(GD);
else
return GetAddrOfGlobalVar(cast<VarDecl>(GD.getDecl()));
}
llvm::GlobalVariable *
CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty,
llvm::GlobalValue::LinkageTypes Linkage);
llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D,
llvm::Type *Ty = 0);
llvm::Constant *GetAddrOfFunction(GlobalDecl GD,
llvm::Type *Ty = 0,
bool ForVTable = false);
llvm::Constant *GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH = false);
llvm::Constant *GetAddrOfThunk(GlobalDecl GD, const ThunkInfo &Thunk);
llvm::Constant *GetWeakRefReference(const ValueDecl *VD);
llvm::Constant *
GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
CastExpr::path_const_iterator PathBegin,
CastExpr::path_const_iterator PathEnd);
class ByrefHelpers : public llvm::FoldingSetNode {
public:
llvm::Constant *CopyHelper;
llvm::Constant *DisposeHelper;
CharUnits Alignment;
ByrefHelpers(CharUnits alignment) : Alignment(alignment) {}
virtual ~ByrefHelpers();
void Profile(llvm::FoldingSetNodeID &id) const {
id.AddInteger(Alignment.getQuantity());
profileImpl(id);
}
virtual void profileImpl(llvm::FoldingSetNodeID &id) const = 0;
virtual bool needsCopy() const { return true; }
virtual void emitCopy(CodeGenFunction &CGF,
llvm::Value *dest, llvm::Value *src) = 0;
virtual bool needsDispose() const { return true; }
virtual void emitDispose(CodeGenFunction &CGF, llvm::Value *field) = 0;
};
llvm::FoldingSet<ByrefHelpers> ByrefHelpersCache;
int getUniqueBlockCount() { return ++Block.GlobalUniqueCount; }
llvm::Type *getBlockDescriptorType();
llvm::Type *getGenericBlockLiteralType();
llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
llvm::Constant *GetAddrOfConstantCFString(const StringLiteral *Literal);
llvm::Constant *GetAddrOfConstantString(const StringLiteral *Literal);
llvm::Constant *GetConstantArrayFromStringLiteral(const StringLiteral *E);
llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
llvm::Constant *GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *);
llvm::Constant *GetAddrOfConstantString(StringRef Str,
const char *GlobalName=0,
unsigned Alignment=1);
llvm::Constant *GetAddrOfConstantCString(const std::string &str,
const char *GlobalName=0,
unsigned Alignment=1);
llvm::Constant *GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr*E);
QualType getObjCFastEnumerationStateType();
llvm::GlobalValue *GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor,
CXXCtorType ctorType,
const CGFunctionInfo *fnInfo = 0);
llvm::GlobalValue *GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor,
CXXDtorType dtorType,
const CGFunctionInfo *fnInfo = 0);
llvm::Value *getBuiltinLibFunction(const FunctionDecl *FD,
unsigned BuiltinID);
llvm::Function *getIntrinsic(unsigned IID, ArrayRef<llvm::Type*> Tys =
ArrayRef<llvm::Type*>());
void EmitTopLevelDecl(Decl *D);
void MarkVarRequired(VarDecl *VD);
void AddUsedGlobal(llvm::GlobalValue *GV);
void AddCXXDtorEntry(llvm::Constant *DtorFn, llvm::Constant *Object) {
CXXGlobalDtors.push_back(std::make_pair(DtorFn, Object));
}
llvm::Constant *CreateRuntimeFunction(llvm::FunctionType *Ty,
StringRef Name,
llvm::Attributes ExtraAttrs =
llvm::Attribute::None);
llvm::Constant *CreateRuntimeVariable(llvm::Type *Ty,
StringRef Name);
llvm::Constant *getNSConcreteGlobalBlock();
llvm::Constant *getNSConcreteStackBlock();
llvm::Constant *getBlockObjectAssign();
llvm::Constant *getBlockObjectDispose();
void UpdateCompletedType(const TagDecl *TD);
llvm::Constant *getMemberPointerConstant(const UnaryOperator *e);
llvm::Constant *EmitConstantInit(const VarDecl &D, CodeGenFunction *CGF = 0);
llvm::Constant *EmitConstantExpr(const Expr *E, QualType DestType,
CodeGenFunction *CGF = 0);
llvm::Constant *EmitConstantValue(const APValue &Value, QualType DestType,
CodeGenFunction *CGF = 0);
llvm::Constant *EmitConstantValueForMemory(const APValue &Value,
QualType DestType,
CodeGenFunction *CGF = 0);
llvm::Constant *EmitNullConstant(QualType T);
llvm::Constant *EmitNullConstantForBase(const CXXRecordDecl *Record);
void Error(SourceLocation loc, StringRef error);
void ErrorUnsupported(const Stmt *S, const char *Type,
bool OmitOnError=false);
void ErrorUnsupported(const Decl *D, const char *Type,
bool OmitOnError=false);
void SetInternalFunctionAttributes(const Decl *D, llvm::Function *F,
const CGFunctionInfo &FI);
void SetLLVMFunctionAttributes(const Decl *D,
const CGFunctionInfo &Info,
llvm::Function *F);
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F);
bool ReturnTypeUsesSRet(const CGFunctionInfo &FI);
bool ReturnTypeUsesFPRet(QualType ResultType);
bool ReturnTypeUsesFP2Ret(QualType ResultType);
void ConstructAttributeList(const CGFunctionInfo &Info,
const Decl *TargetDecl,
AttributeListType &PAL,
unsigned &CallingConv);
StringRef getMangledName(GlobalDecl GD);
void getBlockMangledName(GlobalDecl GD, MangleBuffer &Buffer,
const BlockDecl *BD);
void EmitTentativeDefinition(const VarDecl *D);
void EmitVTable(CXXRecordDecl *Class, bool DefinitionRequired);
llvm::GlobalVariable::LinkageTypes
getFunctionLinkage(const FunctionDecl *FD);
void setFunctionLinkage(const FunctionDecl *FD, llvm::GlobalValue *V) {
V->setLinkage(getFunctionLinkage(FD));
}
llvm::GlobalVariable::LinkageTypes getVTableLinkage(const CXXRecordDecl *RD);
CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const;
llvm::GlobalValue::LinkageTypes
GetLLVMLinkageVarDefinition(const VarDecl *D,
llvm::GlobalVariable *GV);
std::vector<const CXXRecordDecl*> DeferredVTables;
void EmitGlobalAnnotations();
llvm::Constant *EmitAnnotationString(llvm::StringRef Str);
llvm::Constant *EmitAnnotationUnit(SourceLocation Loc);
llvm::Constant *EmitAnnotationLineNo(SourceLocation L);
llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
const AnnotateAttr *AA,
SourceLocation L);
void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV);
private:
llvm::GlobalValue *GetGlobalValue(StringRef Ref);
llvm::Constant *GetOrCreateLLVMFunction(StringRef MangledName,
llvm::Type *Ty,
GlobalDecl D,
bool ForVTable,
llvm::Attributes ExtraAttrs =
llvm::Attribute::None);
llvm::Constant *GetOrCreateLLVMGlobal(StringRef MangledName,
llvm::PointerType *PTy,
const VarDecl *D,
bool UnnamedAddr = false);
void SetCommonAttributes(const Decl *D, llvm::GlobalValue *GV);
void SetFunctionDefinitionAttributes(const FunctionDecl *D,
llvm::GlobalValue *GV);
void SetFunctionAttributes(GlobalDecl GD,
llvm::Function *F,
bool IsIncompleteFunction);
void EmitGlobal(GlobalDecl D);
void EmitGlobalDefinition(GlobalDecl D);
void EmitGlobalFunctionDefinition(GlobalDecl GD);
void EmitGlobalVarDefinition(const VarDecl *D);
llvm::Constant *MaybeEmitGlobalStdInitializerListInitializer(const VarDecl *D,
const Expr *init);
void EmitAliasDefinition(GlobalDecl GD);
void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
void EmitObjCIvarInitializations(ObjCImplementationDecl *D);
bool TryEmitDefinitionAsAlias(GlobalDecl Alias, GlobalDecl Target);
bool TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D);
void EmitNamespace(const NamespaceDecl *D);
void EmitLinkageSpec(const LinkageSpecDecl *D);
void EmitCXXConstructors(const CXXConstructorDecl *D);
void EmitCXXConstructor(const CXXConstructorDecl *D, CXXCtorType Type);
void EmitCXXDestructors(const CXXDestructorDecl *D);
void EmitCXXDestructor(const CXXDestructorDecl *D, CXXDtorType Type);
void EmitCXXGlobalInitFunc();
void EmitCXXGlobalDtorFunc();
void EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
llvm::GlobalVariable *Addr,
bool PerformInit);
void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535);
void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535);
void EmitCtorList(const CtorList &Fns, const char *GlobalName);
void EmitFundamentalRTTIDescriptor(QualType Type);
void EmitFundamentalRTTIDescriptors();
void EmitDeferred(void);
void EmitLLVMUsed(void);
void EmitDeclMetadata();
void EmitCoverageFile();
bool MayDeferGeneration(const ValueDecl *D);
void SimplifyPersonality();
};
} }
#endif