Skip to content
CodeGenModule.h 13.8 KiB
Newer Older
//===--- CodeGenModule.h - Per-Module state for LLVM CodeGen ----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
Mike Stump's avatar
Mike Stump committed
// This is the internal per-translation-unit state used for llvm translation.
//
//===----------------------------------------------------------------------===//

#ifndef CLANG_CODEGEN_CODEGENMODULE_H
#define CLANG_CODEGEN_CODEGENMODULE_H
#include "clang/AST/Attr.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include "CGCall.h"

  class GlobalValue;
Devang Patel's avatar
Devang Patel committed
  class TargetData;
namespace clang {
  class ASTContext;
  class FunctionDecl;
  class IdentifierInfo;
  class ObjCImplementationDecl;
  class ObjCCategoryImplDecl;
  class ObjCProtocolDecl;
Nate Begeman's avatar
Nate Begeman committed
  class NamedDecl;
  class ValueDecl;
  class AnnotateAttr;
Mike Stump's avatar
Mike Stump committed

Mike Stump's avatar
Mike Stump committed

/// CodeGenModule - This class organizes the cross-function state that is used
/// while generating LLVM code.
Chris Lattner's avatar
Chris Lattner committed
class CodeGenModule {
  typedef std::vector< std::pair<llvm::Constant*, int> > CtorList;

Devang Patel's avatar
Devang Patel committed
  const llvm::TargetData &TheTargetData;
  CGObjCRuntime* Runtime;
  CGDebugInfo* DebugInfo;
  llvm::Function *MemSetFn;
Mike Stump's avatar
Mike Stump committed
  /// 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;

  /// GlobalDeclMap - Mapping of decl names (represented as unique
  /// character pointers from either the identifier table or the set
  /// of mangled names) to global variables we have already
  /// emitted. Note that the entries in this map are the 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).  This key
  /// to this map should be generated using getMangledName().
  llvm::DenseMap<const char*, llvm::GlobalValue*> GlobalDeclMap;

  /// \brief Contains the strings used for mangled names.
  ///
  /// FIXME: Eventually, this should map from the semantic/canonical
  /// declaration for each global entity to its mangled name (if it
  /// has one).
  llvm::StringSet<> MangledNames;
Mike Stump's avatar
Mike Stump committed
  /// 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;

  /// DeferredDecls - List of decls for which code generation has been
  /// deferred. 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.
  std::list<const ValueDecl*> DeferredDecls;

  /// LLVMUsed - List of global values which are required to be
  /// present in the object file; bitcast to i8*. This is used for
  /// forcing visibility of symbols which may otherwise be optimized
  /// out.
  std::vector<llvm::Constant*> LLVMUsed;
Mike Stump's avatar
Mike Stump committed

  /// GlobalCtors - Store the list of global constructors and their respective
  /// priorities to be emitted when the translation unit is complete.
Mike Stump's avatar
Mike Stump committed
  /// GlobalDtors - Store the list of global destructors and their respective
  /// priorities to be emitted when the translation unit is complete.
  std::vector<llvm::Constant*> Annotations;
Mike Stump's avatar
Mike Stump committed

  llvm::StringMap<llvm::Constant*> CFConstantStringMap;
  llvm::StringMap<llvm::Constant*> ConstantStringMap;
Mike Stump's avatar
Mike Stump committed
  /// CFConstantStringClassRef - Cached reference to the class for constant
  /// strings. This value has type int * but is actually an Obj-C class pointer.
  llvm::Constant *CFConstantStringClassRef;
Mike Stump's avatar
Mike Stump committed

  /// NSConcreteGlobalBlock - Cached reference to the class pointer for global
  /// blocks.
Mike Stump's avatar
Mike Stump committed

  /// NSConcreteStackBlock - Cached reference to the class poinnter for stack
  /// blocks.
  llvm::Constant *NSConcreteStackBlock;
  
  const llvm::Type *BlockDescriptorType;
  const llvm::Type *GenericBlockLiteralType;
  const llvm::Type *GenericExtendedBlockLiteralType;
  struct {
    int GlobalUniqueCount;
  } Block;
  std::vector<llvm::Function *> BuiltinFunctions;
Mike Stump's avatar
Mike Stump committed
  CodeGenModule(ASTContext &C, const LangOptions &Features, llvm::Module &M,
                const llvm::TargetData &TD, Diagnostic &Diags,
                bool GenerateDebugInfo);
Mike Stump's avatar
Mike Stump committed

  /// Release - Finalize LLVM code generation.
  void Release();
  llvm::Constant *getNSConcreteGlobalBlock();
  llvm::Constant *getNSConcreteStackBlock();
  int getGlobalUniqueCount() { return ++Block.GlobalUniqueCount; }
  const llvm::Type *getBlockDescriptorType();

  const llvm::Type *getGenericBlockLiteralType();
  const llvm::Type *getGenericExtendedBlockLiteralType();
  /// getObjCRuntime() - Return a reference to the configured
  /// Objective-C runtime.
Mike Stump's avatar
Mike Stump committed
  CGObjCRuntime &getObjCRuntime() {
    assert(Runtime && "No Objective-C runtime has been configured.");
Mike Stump's avatar
Mike Stump committed
    return *Runtime;
Mike Stump's avatar
Mike Stump committed

  /// hasObjCRuntime() - Return true iff an Objective-C runtime has
  /// been configured.
  bool hasObjCRuntime() { return !!Runtime; }

  CGDebugInfo *getDebugInfo() { return DebugInfo; }
  ASTContext &getContext() const { return Context; }
  const LangOptions &getLangOptions() const { return Features; }
  llvm::Module &getModule() const { return TheModule; }
  CodeGenTypes &getTypes() { return Types; }
  Diagnostic &getDiags() const { return Diags; }
  const llvm::TargetData &getTargetData() const { return TheTargetData; }
Mike Stump's avatar
Mike Stump committed
  /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
  /// given global variable.
  llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D);

Mike Stump's avatar
Mike Stump committed
  /// GetAddrOfFunction - Return the llvm::Constant for the address of the given
  /// function.
  llvm::Constant *GetAddrOfFunction(const FunctionDecl *D);
Mike Stump's avatar
Mike Stump committed
  /// 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);

Mike Stump's avatar
Mike Stump committed
  /// GetAddrOfConstantCFString - Return a pointer to a constant CFString object
  /// for the given string.
  llvm::Constant *GetAddrOfConstantCFString(const std::string& str);
Mike Stump's avatar
Mike Stump committed
  /// GetAddrOfConstantStringFromLiteral - Return a pointer to a constant array
  /// for the given string literal.
  llvm::Constant *GetAddrOfConstantStringFromLiteral(const StringLiteral *S);
  /// GetAddrOfConstantString - Returns a pointer to a character array
Mike Stump's avatar
Mike Stump committed
  /// 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.
  ///
  /// \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);
Mike Stump's avatar
Mike Stump committed
  /// GetAddrOfConstantCString - Returns a pointer to a character array
  /// containing the literal and a terminating '\0' character. The result has
  /// pointer to array type.
Mike Stump's avatar
Mike Stump committed
  /// \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);
Mike Stump's avatar
Mike Stump committed

  llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *);
Mike Stump's avatar
Mike Stump committed

  /// getBuiltinLibFunction - Given a builtin id for a function like
  /// "__builtin_fabsf", return a Function* for "fabsf".
  llvm::Function *getBuiltinLibFunction(unsigned BuiltinID);
  llvm::Function *getMemCpyFn();
  llvm::Function *getMemSetFn();
Mike Stump's avatar
Mike Stump committed
  llvm::Function *getIntrinsic(unsigned IID, const llvm::Type **Tys = 0,
  /// EmitTopLevelDecl - Emit code for a single top level declaration.
  void EmitTopLevelDecl(Decl *D);
  /// AddUsedGlobal - Add a global which should be forced to be
  /// present in the object file; these are emitted to the llvm.used
  /// metadata global.
  void AddUsedGlobal(llvm::GlobalValue *GV);

  void AddAnnotation(llvm::Constant *C) { Annotations.push_back(C); }

Mike Stump's avatar
Mike Stump committed
  /// CreateRuntimeFunction - Create a new runtime function whose name must be
  /// protected from collisions.
  llvm::Function *CreateRuntimeFunction(const llvm::FunctionType *Ty,
  void UpdateCompletedType(const TagDecl *D);

  /// EmitConstantExpr - Try to emit the given expression as a
  /// constant; returns 0 if the expression cannot be emitted as a
  /// constant.
  llvm::Constant *EmitConstantExpr(const Expr *E, CodeGenFunction *CGF = 0);
  llvm::Constant *EmitAnnotateAttr(llvm::GlobalValue *GV,
                                   const AnnotateAttr *AA, unsigned LineNo);
Mike Stump's avatar
Mike Stump committed

  /// ErrorUnsupported - Print out an error that codegen doesn't support the
Mike Stump's avatar
Mike Stump committed
  /// \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,
Mike Stump's avatar
Mike Stump committed

  /// ErrorUnsupported - Print out an error that codegen doesn't support the
Mike Stump's avatar
Mike Stump committed
  /// \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);
  void SetMethodAttributes(const ObjCMethodDecl *MD,
                           llvm::Function *F);

Devang Patel's avatar
Devang Patel committed
  void SetFunctionAttributes(const Decl *D,
Mike Stump's avatar
Mike Stump committed
                             const CGFunctionInfo &Info,
                             llvm::Function *F);

Mike Stump's avatar
Mike Stump committed
  /// ReturnTypeUsesSret - Return true iff the given type uses 'sret' when used
  /// as a return type.
Daniel Dunbar's avatar
Daniel Dunbar committed
  bool ReturnTypeUsesSret(const CGFunctionInfo &FI);
Daniel Dunbar's avatar
Daniel Dunbar committed
  void ConstructAttributeList(const CGFunctionInfo &Info,
                              const Decl *TargetDecl,
Devang Patel's avatar
Devang Patel committed
                              AttributeListType &PAL);
  const char *getMangledName(const NamedDecl *ND);
  /// SetGlobalValueAttributes - Set attributes for a global decl.
  void SetGlobalValueAttributes(const Decl *D, 
                                bool IsInternal,
                                bool IsInline,
                                llvm::GlobalValue *GV,
                                bool ForDefinition);
    
Mike Stump's avatar
Mike Stump committed
  /// SetFunctionAttributesForDefinition - Set function attributes specific to a
  /// function definition.
  /// \param D - The ObjCMethodDecl or FunctionDecl defining \arg F.
  void SetFunctionAttributesForDefinition(const Decl *D,
                                          llvm::Function *F);
  void SetFunctionAttributes(const FunctionDecl *FD,
Mike Stump's avatar
Mike Stump committed
  /// EmitGlobal - Emit code for a singal global function or var decl. Forward
  /// declarations are emitted lazily.
  void EmitGlobal(const ValueDecl *D);

  void EmitGlobalDefinition(const ValueDecl *D);

  /// EmitForwardFunctionDefinition - Create a new function for the
  /// given decl and set attributes as appropriate.
  ///
  /// \arg Ty - If non-null the LLVM function type to use for the
  /// decl; it is the callers responsibility to make sure this is
  /// compatible with the correct type.
  llvm::GlobalValue *EmitForwardFunctionDefinition(const FunctionDecl *D,
                                                   const llvm::Type *Ty);

  void EmitGlobalFunctionDefinition(const FunctionDecl *D);
  void EmitGlobalVarDefinition(const VarDecl *D);
  void EmitObjCPropertyImplementations(const ObjCImplementationDecl *D);
Mike Stump's avatar
Mike Stump committed

  // FIXME: Hardcoding priority here is gross.
  void AddGlobalCtor(llvm::Function * Ctor, int Priority=65535);
  void AddGlobalDtor(llvm::Function * Dtor, int Priority=65535);

Mike Stump's avatar
Mike Stump committed
  /// 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);
  void EmitAnnotations(void);

  /// EmitDeferred - Emit any needed decls for which code generation
  /// was deferred.
  void EmitDeferred(void);

  /// EmitLLVMUsed - Emit the llvm.used metadata used to force
  /// references to global which may otherwise be optimized out.
  void EmitLLVMUsed(void);

  /// MayDeferGeneration - Determine if the given decl can be emitted
  /// lazily; this is only relevant for definitions. The given decl
  /// must be either a function or var decl.
  bool MayDeferGeneration(const ValueDecl *D);
};
}  // end namespace CodeGen
}  // end namespace clang

#endif