1*67e74705SXin Li //===---- TargetInfo.h - Encapsulate target details -------------*- C++ -*-===// 2*67e74705SXin Li // 3*67e74705SXin Li // The LLVM Compiler Infrastructure 4*67e74705SXin Li // 5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source 6*67e74705SXin Li // License. See LICENSE.TXT for details. 7*67e74705SXin Li // 8*67e74705SXin Li //===----------------------------------------------------------------------===// 9*67e74705SXin Li // 10*67e74705SXin Li // These classes wrap the information about a call or function 11*67e74705SXin Li // definition used to handle ABI compliancy. 12*67e74705SXin Li // 13*67e74705SXin Li //===----------------------------------------------------------------------===// 14*67e74705SXin Li 15*67e74705SXin Li #ifndef LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H 16*67e74705SXin Li #define LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H 17*67e74705SXin Li 18*67e74705SXin Li #include "CGValue.h" 19*67e74705SXin Li #include "clang/AST/Type.h" 20*67e74705SXin Li #include "clang/Basic/LLVM.h" 21*67e74705SXin Li #include "llvm/ADT/SmallString.h" 22*67e74705SXin Li #include "llvm/ADT/StringRef.h" 23*67e74705SXin Li 24*67e74705SXin Li namespace llvm { 25*67e74705SXin Li class Constant; 26*67e74705SXin Li class GlobalValue; 27*67e74705SXin Li class Type; 28*67e74705SXin Li class Value; 29*67e74705SXin Li } 30*67e74705SXin Li 31*67e74705SXin Li namespace clang { 32*67e74705SXin Li class Decl; 33*67e74705SXin Li 34*67e74705SXin Li namespace CodeGen { 35*67e74705SXin Li class ABIInfo; 36*67e74705SXin Li class CallArgList; 37*67e74705SXin Li class CodeGenModule; 38*67e74705SXin Li class CodeGenFunction; 39*67e74705SXin Li class CGFunctionInfo; 40*67e74705SXin Li 41*67e74705SXin Li /// TargetCodeGenInfo - This class organizes various target-specific 42*67e74705SXin Li /// codegeneration issues, like target-specific attributes, builtins and so 43*67e74705SXin Li /// on. 44*67e74705SXin Li class TargetCodeGenInfo { 45*67e74705SXin Li ABIInfo *Info; 46*67e74705SXin Li 47*67e74705SXin Li public: 48*67e74705SXin Li // WARNING: Acquires the ownership of ABIInfo. Info(info)49*67e74705SXin Li TargetCodeGenInfo(ABIInfo *info = nullptr) : Info(info) {} 50*67e74705SXin Li virtual ~TargetCodeGenInfo(); 51*67e74705SXin Li 52*67e74705SXin Li /// getABIInfo() - Returns ABI info helper for the target. getABIInfo()53*67e74705SXin Li const ABIInfo &getABIInfo() const { return *Info; } 54*67e74705SXin Li 55*67e74705SXin Li /// setTargetAttributes - Provides a convenient hook to handle extra 56*67e74705SXin Li /// target-specific attributes for the given global. setTargetAttributes(const Decl * D,llvm::GlobalValue * GV,CodeGen::CodeGenModule & M)57*67e74705SXin Li virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 58*67e74705SXin Li CodeGen::CodeGenModule &M) const {} 59*67e74705SXin Li 60*67e74705SXin Li /// emitTargetMD - Provides a convenient hook to handle extra 61*67e74705SXin Li /// target-specific metadata for the given global. emitTargetMD(const Decl * D,llvm::GlobalValue * GV,CodeGen::CodeGenModule & M)62*67e74705SXin Li virtual void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, 63*67e74705SXin Li CodeGen::CodeGenModule &M) const {} 64*67e74705SXin Li 65*67e74705SXin Li /// Determines the size of struct _Unwind_Exception on this platform, 66*67e74705SXin Li /// in 8-bit units. The Itanium ABI defines this as: 67*67e74705SXin Li /// struct _Unwind_Exception { 68*67e74705SXin Li /// uint64 exception_class; 69*67e74705SXin Li /// _Unwind_Exception_Cleanup_Fn exception_cleanup; 70*67e74705SXin Li /// uint64 private_1; 71*67e74705SXin Li /// uint64 private_2; 72*67e74705SXin Li /// }; 73*67e74705SXin Li virtual unsigned getSizeOfUnwindException() const; 74*67e74705SXin Li 75*67e74705SXin Li /// Controls whether __builtin_extend_pointer should sign-extend 76*67e74705SXin Li /// pointers to uint64_t or zero-extend them (the default). Has 77*67e74705SXin Li /// no effect for targets: 78*67e74705SXin Li /// - that have 64-bit pointers, or 79*67e74705SXin Li /// - that cannot address through registers larger than pointers, or 80*67e74705SXin Li /// - that implicitly ignore/truncate the top bits when addressing 81*67e74705SXin Li /// through such registers. extendPointerWithSExt()82*67e74705SXin Li virtual bool extendPointerWithSExt() const { return false; } 83*67e74705SXin Li 84*67e74705SXin Li /// Determines the DWARF register number for the stack pointer, for 85*67e74705SXin Li /// exception-handling purposes. Implements __builtin_dwarf_sp_column. 86*67e74705SXin Li /// 87*67e74705SXin Li /// Returns -1 if the operation is unsupported by this target. getDwarfEHStackPointer(CodeGen::CodeGenModule & M)88*67e74705SXin Li virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 89*67e74705SXin Li return -1; 90*67e74705SXin Li } 91*67e74705SXin Li 92*67e74705SXin Li /// Initializes the given DWARF EH register-size table, a char*. 93*67e74705SXin Li /// Implements __builtin_init_dwarf_reg_size_table. 94*67e74705SXin Li /// 95*67e74705SXin Li /// Returns true if the operation is unsupported by this target. initDwarfEHRegSizeTable(CodeGen::CodeGenFunction & CGF,llvm::Value * Address)96*67e74705SXin Li virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 97*67e74705SXin Li llvm::Value *Address) const { 98*67e74705SXin Li return true; 99*67e74705SXin Li } 100*67e74705SXin Li 101*67e74705SXin Li /// Performs the code-generation required to convert a return 102*67e74705SXin Li /// address as stored by the system into the actual address of the 103*67e74705SXin Li /// next instruction that will be executed. 104*67e74705SXin Li /// 105*67e74705SXin Li /// Used by __builtin_extract_return_addr(). decodeReturnAddress(CodeGen::CodeGenFunction & CGF,llvm::Value * Address)106*67e74705SXin Li virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF, 107*67e74705SXin Li llvm::Value *Address) const { 108*67e74705SXin Li return Address; 109*67e74705SXin Li } 110*67e74705SXin Li 111*67e74705SXin Li /// Performs the code-generation required to convert the address 112*67e74705SXin Li /// of an instruction into a return address suitable for storage 113*67e74705SXin Li /// by the system in a return slot. 114*67e74705SXin Li /// 115*67e74705SXin Li /// Used by __builtin_frob_return_addr(). encodeReturnAddress(CodeGen::CodeGenFunction & CGF,llvm::Value * Address)116*67e74705SXin Li virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF, 117*67e74705SXin Li llvm::Value *Address) const { 118*67e74705SXin Li return Address; 119*67e74705SXin Li } 120*67e74705SXin Li 121*67e74705SXin Li /// Corrects the low-level LLVM type for a given constraint and "usual" 122*67e74705SXin Li /// type. 123*67e74705SXin Li /// 124*67e74705SXin Li /// \returns A pointer to a new LLVM type, possibly the same as the original 125*67e74705SXin Li /// on success; 0 on failure. adjustInlineAsmType(CodeGen::CodeGenFunction & CGF,StringRef Constraint,llvm::Type * Ty)126*67e74705SXin Li virtual llvm::Type *adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 127*67e74705SXin Li StringRef Constraint, 128*67e74705SXin Li llvm::Type *Ty) const { 129*67e74705SXin Li return Ty; 130*67e74705SXin Li } 131*67e74705SXin Li 132*67e74705SXin Li /// Adds constraints and types for result registers. addReturnRegisterOutputs(CodeGen::CodeGenFunction & CGF,CodeGen::LValue ReturnValue,std::string & Constraints,std::vector<llvm::Type * > & ResultRegTypes,std::vector<llvm::Type * > & ResultTruncRegTypes,std::vector<CodeGen::LValue> & ResultRegDests,std::string & AsmString,unsigned NumOutputs)133*67e74705SXin Li virtual void addReturnRegisterOutputs( 134*67e74705SXin Li CodeGen::CodeGenFunction &CGF, CodeGen::LValue ReturnValue, 135*67e74705SXin Li std::string &Constraints, std::vector<llvm::Type *> &ResultRegTypes, 136*67e74705SXin Li std::vector<llvm::Type *> &ResultTruncRegTypes, 137*67e74705SXin Li std::vector<CodeGen::LValue> &ResultRegDests, std::string &AsmString, 138*67e74705SXin Li unsigned NumOutputs) const {} 139*67e74705SXin Li 140*67e74705SXin Li /// doesReturnSlotInterfereWithArgs - Return true if the target uses an 141*67e74705SXin Li /// argument slot for an 'sret' type. doesReturnSlotInterfereWithArgs()142*67e74705SXin Li virtual bool doesReturnSlotInterfereWithArgs() const { return true; } 143*67e74705SXin Li 144*67e74705SXin Li /// Retrieve the address of a function to call immediately before 145*67e74705SXin Li /// calling objc_retainAutoreleasedReturnValue. The 146*67e74705SXin Li /// implementation of objc_autoreleaseReturnValue sniffs the 147*67e74705SXin Li /// instruction stream following its return address to decide 148*67e74705SXin Li /// whether it's a call to objc_retainAutoreleasedReturnValue. 149*67e74705SXin Li /// This can be prohibitively expensive, depending on the 150*67e74705SXin Li /// relocation model, and so on some targets it instead sniffs for 151*67e74705SXin Li /// a particular instruction sequence. This functions returns 152*67e74705SXin Li /// that instruction sequence in inline assembly, which will be 153*67e74705SXin Li /// empty if none is required. getARCRetainAutoreleasedReturnValueMarker()154*67e74705SXin Li virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const { 155*67e74705SXin Li return ""; 156*67e74705SXin Li } 157*67e74705SXin Li 158*67e74705SXin Li /// Return a constant used by UBSan as a signature to identify functions 159*67e74705SXin Li /// possessing type information, or 0 if the platform is unsupported. 160*67e74705SXin Li virtual llvm::Constant * getUBSanFunctionSignature(CodeGen::CodeGenModule & CGM)161*67e74705SXin Li getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const { 162*67e74705SXin Li return nullptr; 163*67e74705SXin Li } 164*67e74705SXin Li 165*67e74705SXin Li /// Determine whether a call to an unprototyped functions under 166*67e74705SXin Li /// the given calling convention should use the variadic 167*67e74705SXin Li /// convention or the non-variadic convention. 168*67e74705SXin Li /// 169*67e74705SXin Li /// There's a good reason to make a platform's variadic calling 170*67e74705SXin Li /// convention be different from its non-variadic calling 171*67e74705SXin Li /// convention: the non-variadic arguments can be passed in 172*67e74705SXin Li /// registers (better for performance), and the variadic arguments 173*67e74705SXin Li /// can be passed on the stack (also better for performance). If 174*67e74705SXin Li /// this is done, however, unprototyped functions *must* use the 175*67e74705SXin Li /// non-variadic convention, because C99 states that a call 176*67e74705SXin Li /// through an unprototyped function type must succeed if the 177*67e74705SXin Li /// function was defined with a non-variadic prototype with 178*67e74705SXin Li /// compatible parameters. Therefore, splitting the conventions 179*67e74705SXin Li /// makes it impossible to call a variadic function through an 180*67e74705SXin Li /// unprototyped type. Since function prototypes came out in the 181*67e74705SXin Li /// late 1970s, this is probably an acceptable trade-off. 182*67e74705SXin Li /// Nonetheless, not all platforms are willing to make it, and in 183*67e74705SXin Li /// particularly x86-64 bends over backwards to make the 184*67e74705SXin Li /// conventions compatible. 185*67e74705SXin Li /// 186*67e74705SXin Li /// The default is false. This is correct whenever: 187*67e74705SXin Li /// - the conventions are exactly the same, because it does not 188*67e74705SXin Li /// matter and the resulting IR will be somewhat prettier in 189*67e74705SXin Li /// certain cases; or 190*67e74705SXin Li /// - the conventions are substantively different in how they pass 191*67e74705SXin Li /// arguments, because in this case using the variadic convention 192*67e74705SXin Li /// will lead to C99 violations. 193*67e74705SXin Li /// 194*67e74705SXin Li /// However, some platforms make the conventions identical except 195*67e74705SXin Li /// for passing additional out-of-band information to a variadic 196*67e74705SXin Li /// function: for example, x86-64 passes the number of SSE 197*67e74705SXin Li /// arguments in %al. On these platforms, it is desirable to 198*67e74705SXin Li /// call unprototyped functions using the variadic convention so 199*67e74705SXin Li /// that unprototyped calls to varargs functions still succeed. 200*67e74705SXin Li /// 201*67e74705SXin Li /// Relatedly, platforms which pass the fixed arguments to this: 202*67e74705SXin Li /// A foo(B, C, D); 203*67e74705SXin Li /// differently than they would pass them to this: 204*67e74705SXin Li /// A foo(B, C, D, ...); 205*67e74705SXin Li /// may need to adjust the debugger-support code in Sema to do the 206*67e74705SXin Li /// right thing when calling a function with no know signature. 207*67e74705SXin Li virtual bool isNoProtoCallVariadic(const CodeGen::CallArgList &args, 208*67e74705SXin Li const FunctionNoProtoType *fnType) const; 209*67e74705SXin Li 210*67e74705SXin Li /// Gets the linker options necessary to link a dependent library on this 211*67e74705SXin Li /// platform. 212*67e74705SXin Li virtual void getDependentLibraryOption(llvm::StringRef Lib, 213*67e74705SXin Li llvm::SmallString<24> &Opt) const; 214*67e74705SXin Li 215*67e74705SXin Li /// Gets the linker options necessary to detect object file mismatches on 216*67e74705SXin Li /// this platform. getDetectMismatchOption(llvm::StringRef Name,llvm::StringRef Value,llvm::SmallString<32> & Opt)217*67e74705SXin Li virtual void getDetectMismatchOption(llvm::StringRef Name, 218*67e74705SXin Li llvm::StringRef Value, 219*67e74705SXin Li llvm::SmallString<32> &Opt) const {} 220*67e74705SXin Li 221*67e74705SXin Li /// Get LLVM calling convention for OpenCL kernel. 222*67e74705SXin Li virtual unsigned getOpenCLKernelCallingConv() const; 223*67e74705SXin Li }; 224*67e74705SXin Li 225*67e74705SXin Li } // namespace CodeGen 226*67e74705SXin Li } // namespace clang 227*67e74705SXin Li 228*67e74705SXin Li #endif // LLVM_CLANG_LIB_CODEGEN_TARGETINFO_H 229