1 //===---- TargetInfo.h - Encapsulate target details -------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // These classes wrap the information about a call or function 11 // definition used to handle ABI compliancy. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef CLANG_CODEGEN_TARGETINFO_H 16 #define CLANG_CODEGEN_TARGETINFO_H 17 18 #include "clang/Basic/LLVM.h" 19 #include "clang/AST/Type.h" 20 #include "llvm/ADT/StringRef.h" 21 22 namespace llvm { 23 class GlobalValue; 24 class Type; 25 class Value; 26 } 27 28 namespace clang { 29 class ABIInfo; 30 class Decl; 31 32 namespace CodeGen { 33 class CodeGenModule; 34 class CodeGenFunction; 35 class CGFunctionInfo; 36 } 37 38 /// TargetCodeGenInfo - This class organizes various target-specific 39 /// codegeneration issues, like target-specific attributes, builtins and so 40 /// on. 41 class TargetCodeGenInfo { 42 ABIInfo *Info; 43 public: 44 // WARNING: Acquires the ownership of ABIInfo. 45 TargetCodeGenInfo(ABIInfo *info = 0):Info(info) { } 46 virtual ~TargetCodeGenInfo(); 47 48 /// getABIInfo() - Returns ABI info helper for the target. 49 const ABIInfo& getABIInfo() const { return *Info; } 50 51 /// SetTargetAttributes - Provides a convenient hook to handle extra 52 /// target-specific attributes for the given global. 53 virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, 54 CodeGen::CodeGenModule &M) const { } 55 56 /// Determines the size of struct _Unwind_Exception on this platform, 57 /// in 8-bit units. The Itanium ABI defines this as: 58 /// struct _Unwind_Exception { 59 /// uint64 exception_class; 60 /// _Unwind_Exception_Cleanup_Fn exception_cleanup; 61 /// uint64 private_1; 62 /// uint64 private_2; 63 /// }; 64 virtual unsigned getSizeOfUnwindException() const; 65 66 /// Controls whether __builtin_extend_pointer should sign-extend 67 /// pointers to uint64_t or zero-extend them (the default). Has 68 /// no effect for targets: 69 /// - that have 64-bit pointers, or 70 /// - that cannot address through registers larger than pointers, or 71 /// - that implicitly ignore/truncate the top bits when addressing 72 /// through such registers. 73 virtual bool extendPointerWithSExt() const { return false; } 74 75 /// Determines the DWARF register number for the stack pointer, for 76 /// exception-handling purposes. Implements __builtin_dwarf_sp_column. 77 /// 78 /// Returns -1 if the operation is unsupported by this target. 79 virtual int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { 80 return -1; 81 } 82 83 /// Initializes the given DWARF EH register-size table, a char*. 84 /// Implements __builtin_init_dwarf_reg_size_table. 85 /// 86 /// Returns true if the operation is unsupported by this target. 87 virtual bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, 88 llvm::Value *Address) const { 89 return true; 90 } 91 92 /// Performs the code-generation required to convert a return 93 /// address as stored by the system into the actual address of the 94 /// next instruction that will be executed. 95 /// 96 /// Used by __builtin_extract_return_addr(). 97 virtual llvm::Value *decodeReturnAddress(CodeGen::CodeGenFunction &CGF, 98 llvm::Value *Address) const { 99 return Address; 100 } 101 102 /// Performs the code-generation required to convert the address 103 /// of an instruction into a return address suitable for storage 104 /// by the system in a return slot. 105 /// 106 /// Used by __builtin_frob_return_addr(). 107 virtual llvm::Value *encodeReturnAddress(CodeGen::CodeGenFunction &CGF, 108 llvm::Value *Address) const { 109 return Address; 110 } 111 112 virtual llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, 113 StringRef Constraint, 114 llvm::Type* Ty) const { 115 return Ty; 116 } 117 118 /// Retrieve the address of a function to call immediately before 119 /// calling objc_retainAutoreleasedReturnValue. The 120 /// implementation of objc_autoreleaseReturnValue sniffs the 121 /// instruction stream following its return address to decide 122 /// whether it's a call to objc_retainAutoreleasedReturnValue. 123 /// This can be prohibitively expensive, depending on the 124 /// relocation model, and so on some targets it instead sniffs for 125 /// a particular instruction sequence. This functions returns 126 /// that instruction sequence in inline assembly, which will be 127 /// empty if none is required. 128 virtual StringRef getARCRetainAutoreleasedReturnValueMarker() const { 129 return ""; 130 } 131 132 /// Determine whether a call to an unprototyped functions under 133 /// the given calling convention should use the variadic 134 /// convention or the non-variadic convention. 135 /// 136 /// There's a good reason to make a platform's variadic calling 137 /// convention be different from its non-variadic calling 138 /// convention: the non-variadic arguments can be passed in 139 /// registers (better for performance), and the variadic arguments 140 /// can be passed on the stack (also better for performance). If 141 /// this is done, however, unprototyped functions *must* use the 142 /// non-variadic convention, because C99 states that a call 143 /// through an unprototyped function type must succeed if the 144 /// function was defined with a non-variadic prototype with 145 /// compatible parameters. Therefore, splitting the conventions 146 /// makes it impossible to call a variadic function through an 147 /// unprototyped type. Since function prototypes came out in the 148 /// late 1970s, this is probably an acceptable trade-off. 149 /// Nonetheless, not all platforms are willing to make it, and in 150 /// particularly x86-64 bends over backwards to make the 151 /// conventions compatible. 152 /// 153 /// The default is false. This is correct whenever: 154 /// - the conventions are exactly the same, because it does not 155 /// matter and the resulting IR will be somewhat prettier in 156 /// certain cases; or 157 /// - the conventions are substantively different in how they pass 158 /// arguments, because in this case using the variadic convention 159 /// will lead to C99 violations. 160 /// It is not necessarily correct when arguments are passed in the 161 /// same way and some out-of-band information is passed for the 162 /// benefit of variadic callees, as is the case for x86-64. 163 /// In this case the ABI should be consulted. 164 virtual bool isNoProtoCallVariadic(const CodeGen::CGFunctionInfo &) const; 165 }; 166 } 167 168 #endif // CLANG_CODEGEN_TARGETINFO_H 169