1 /*===-- llvm-c/Target.h - Target Lib C Iface --------------------*- C++ -*-===*/ 2 /* */ 3 /* Part of the LLVM Project, under the Apache License v2.0 with LLVM */ 4 /* Exceptions. */ 5 /* See https://llvm.org/LICENSE.txt for license information. */ 6 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 7 /* */ 8 /*===----------------------------------------------------------------------===*/ 9 /* */ 10 /* This header declares the C interface to libLLVMTarget.a, which */ 11 /* implements target information. */ 12 /* */ 13 /* Many exotic languages can interoperate with C code but have a harder time */ 14 /* with C++ due to name mangling. So in addition to C, this interface enables */ 15 /* tools written in such languages. */ 16 /* */ 17 /*===----------------------------------------------------------------------===*/ 18 19 #ifndef LLVM_C_TARGET_H 20 #define LLVM_C_TARGET_H 21 22 #include "llvm-c/ExternC.h" 23 #include "llvm-c/Types.h" 24 #include "llvm/Config/llvm-config.h" 25 26 LLVM_C_EXTERN_C_BEGIN 27 28 /** 29 * @defgroup LLVMCTarget Target information 30 * @ingroup LLVMC 31 * 32 * @{ 33 */ 34 35 enum LLVMByteOrdering { LLVMBigEndian, LLVMLittleEndian }; 36 37 typedef struct LLVMOpaqueTargetData *LLVMTargetDataRef; 38 typedef struct LLVMOpaqueTargetLibraryInfotData *LLVMTargetLibraryInfoRef; 39 40 /* Declare all of the target-initialization functions that are available. */ 41 #define LLVM_TARGET(TargetName) \ 42 void LLVMInitialize##TargetName##TargetInfo(void); 43 #include "llvm/Config/Targets.def" 44 #undef LLVM_TARGET /* Explicit undef to make SWIG happier */ 45 46 #define LLVM_TARGET(TargetName) void LLVMInitialize##TargetName##Target(void); 47 #include "llvm/Config/Targets.def" 48 #undef LLVM_TARGET /* Explicit undef to make SWIG happier */ 49 50 #define LLVM_TARGET(TargetName) \ 51 void LLVMInitialize##TargetName##TargetMC(void); 52 #include "llvm/Config/Targets.def" 53 #undef LLVM_TARGET /* Explicit undef to make SWIG happier */ 54 55 /* Declare all of the available assembly printer initialization functions. */ 56 #define LLVM_ASM_PRINTER(TargetName) \ 57 void LLVMInitialize##TargetName##AsmPrinter(void); 58 #include "llvm/Config/AsmPrinters.def" 59 #undef LLVM_ASM_PRINTER /* Explicit undef to make SWIG happier */ 60 61 /* Declare all of the available assembly parser initialization functions. */ 62 #define LLVM_ASM_PARSER(TargetName) \ 63 void LLVMInitialize##TargetName##AsmParser(void); 64 #include "llvm/Config/AsmParsers.def" 65 #undef LLVM_ASM_PARSER /* Explicit undef to make SWIG happier */ 66 67 /* Declare all of the available disassembler initialization functions. */ 68 #define LLVM_DISASSEMBLER(TargetName) \ 69 void LLVMInitialize##TargetName##Disassembler(void); 70 #include "llvm/Config/Disassemblers.def" 71 #undef LLVM_DISASSEMBLER /* Explicit undef to make SWIG happier */ 72 73 /** LLVMInitializeAllTargetInfos - The main program should call this function if 74 it wants access to all available targets that LLVM is configured to 75 support. */ 76 static inline void LLVMInitializeAllTargetInfos(void) { 77 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##TargetInfo(); 78 #include "llvm/Config/Targets.def" 79 #undef LLVM_TARGET /* Explicit undef to make SWIG happier */ 80 } 81 82 /** LLVMInitializeAllTargets - The main program should call this function if it 83 wants to link in all available targets that LLVM is configured to 84 support. */ 85 static inline void LLVMInitializeAllTargets(void) { 86 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##Target(); 87 #include "llvm/Config/Targets.def" 88 #undef LLVM_TARGET /* Explicit undef to make SWIG happier */ 89 } 90 91 /** LLVMInitializeAllTargetMCs - The main program should call this function if 92 it wants access to all available target MC that LLVM is configured to 93 support. */ 94 static inline void LLVMInitializeAllTargetMCs(void) { 95 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##TargetMC(); 96 #include "llvm/Config/Targets.def" 97 #undef LLVM_TARGET /* Explicit undef to make SWIG happier */ 98 } 99 100 /** LLVMInitializeAllAsmPrinters - The main program should call this function if 101 it wants all asm printers that LLVM is configured to support, to make them 102 available via the TargetRegistry. */ 103 static inline void LLVMInitializeAllAsmPrinters(void) { 104 #define LLVM_ASM_PRINTER(TargetName) LLVMInitialize##TargetName##AsmPrinter(); 105 #include "llvm/Config/AsmPrinters.def" 106 #undef LLVM_ASM_PRINTER /* Explicit undef to make SWIG happier */ 107 } 108 109 /** LLVMInitializeAllAsmParsers - The main program should call this function if 110 it wants all asm parsers that LLVM is configured to support, to make them 111 available via the TargetRegistry. */ 112 static inline void LLVMInitializeAllAsmParsers(void) { 113 #define LLVM_ASM_PARSER(TargetName) LLVMInitialize##TargetName##AsmParser(); 114 #include "llvm/Config/AsmParsers.def" 115 #undef LLVM_ASM_PARSER /* Explicit undef to make SWIG happier */ 116 } 117 118 /** LLVMInitializeAllDisassemblers - The main program should call this function 119 if it wants all disassemblers that LLVM is configured to support, to make 120 them available via the TargetRegistry. */ 121 static inline void LLVMInitializeAllDisassemblers(void) { 122 #define LLVM_DISASSEMBLER(TargetName) \ 123 LLVMInitialize##TargetName##Disassembler(); 124 #include "llvm/Config/Disassemblers.def" 125 #undef LLVM_DISASSEMBLER /* Explicit undef to make SWIG happier */ 126 } 127 128 /** LLVMInitializeNativeTarget - The main program should call this function to 129 initialize the native target corresponding to the host. This is useful 130 for JIT applications to ensure that the target gets linked in correctly. */ 131 static inline LLVMBool LLVMInitializeNativeTarget(void) { 132 /* If we have a native target, initialize it to ensure it is linked in. */ 133 #ifdef LLVM_NATIVE_TARGET 134 LLVM_NATIVE_TARGETINFO(); 135 LLVM_NATIVE_TARGET(); 136 LLVM_NATIVE_TARGETMC(); 137 return 0; 138 #else 139 return 1; 140 #endif 141 } 142 143 /** LLVMInitializeNativeTargetAsmParser - The main program should call this 144 function to initialize the parser for the native target corresponding to the 145 host. */ 146 static inline LLVMBool LLVMInitializeNativeAsmParser(void) { 147 #ifdef LLVM_NATIVE_ASMPARSER 148 LLVM_NATIVE_ASMPARSER(); 149 return 0; 150 #else 151 return 1; 152 #endif 153 } 154 155 /** LLVMInitializeNativeTargetAsmPrinter - The main program should call this 156 function to initialize the printer for the native target corresponding to 157 the host. */ 158 static inline LLVMBool LLVMInitializeNativeAsmPrinter(void) { 159 #ifdef LLVM_NATIVE_ASMPRINTER 160 LLVM_NATIVE_ASMPRINTER(); 161 return 0; 162 #else 163 return 1; 164 #endif 165 } 166 167 /** LLVMInitializeNativeTargetDisassembler - The main program should call this 168 function to initialize the disassembler for the native target corresponding 169 to the host. */ 170 static inline LLVMBool LLVMInitializeNativeDisassembler(void) { 171 #ifdef LLVM_NATIVE_DISASSEMBLER 172 LLVM_NATIVE_DISASSEMBLER(); 173 return 0; 174 #else 175 return 1; 176 #endif 177 } 178 179 /*===-- Target Data -------------------------------------------------------===*/ 180 181 /** 182 * Obtain the data layout for a module. 183 * 184 * @see Module::getDataLayout() 185 */ 186 LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M); 187 188 /** 189 * Set the data layout for a module. 190 * 191 * @see Module::setDataLayout() 192 */ 193 void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL); 194 195 /** Creates target data from a target layout string. 196 See the constructor llvm::DataLayout::DataLayout. */ 197 LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep); 198 199 /** Deallocates a TargetData. 200 See the destructor llvm::DataLayout::~DataLayout. */ 201 void LLVMDisposeTargetData(LLVMTargetDataRef TD); 202 203 /** Adds target library information to a pass manager. This does not take 204 ownership of the target library info. 205 See the method llvm::PassManagerBase::add. */ 206 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI, 207 LLVMPassManagerRef PM); 208 209 /** Converts target data to a target layout string. The string must be disposed 210 with LLVMDisposeMessage. 211 See the constructor llvm::DataLayout::DataLayout. */ 212 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD); 213 214 /** Returns the byte order of a target, either LLVMBigEndian or 215 LLVMLittleEndian. 216 See the method llvm::DataLayout::isLittleEndian. */ 217 enum LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD); 218 219 /** Returns the pointer size in bytes for a target. 220 See the method llvm::DataLayout::getPointerSize. */ 221 unsigned LLVMPointerSize(LLVMTargetDataRef TD); 222 223 /** Returns the pointer size in bytes for a target for a specified 224 address space. 225 See the method llvm::DataLayout::getPointerSize. */ 226 unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS); 227 228 /** Returns the integer type that is the same size as a pointer on a target. 229 See the method llvm::DataLayout::getIntPtrType. */ 230 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD); 231 232 /** Returns the integer type that is the same size as a pointer on a target. 233 This version allows the address space to be specified. 234 See the method llvm::DataLayout::getIntPtrType. */ 235 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS); 236 237 /** Returns the integer type that is the same size as a pointer on a target. 238 See the method llvm::DataLayout::getIntPtrType. */ 239 LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD); 240 241 /** Returns the integer type that is the same size as a pointer on a target. 242 This version allows the address space to be specified. 243 See the method llvm::DataLayout::getIntPtrType. */ 244 LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD, 245 unsigned AS); 246 247 /** Computes the size of a type in bytes for a target. 248 See the method llvm::DataLayout::getTypeSizeInBits. */ 249 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty); 250 251 /** Computes the storage size of a type in bytes for a target. 252 See the method llvm::DataLayout::getTypeStoreSize. */ 253 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty); 254 255 /** Computes the ABI size of a type in bytes for a target. 256 See the method llvm::DataLayout::getTypeAllocSize. */ 257 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty); 258 259 /** Computes the ABI alignment of a type in bytes for a target. 260 See the method llvm::DataLayout::getTypeABISize. */ 261 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty); 262 263 /** Computes the call frame alignment of a type in bytes for a target. 264 See the method llvm::DataLayout::getTypeABISize. */ 265 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty); 266 267 /** Computes the preferred alignment of a type in bytes for a target. 268 See the method llvm::DataLayout::getTypeABISize. */ 269 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty); 270 271 /** Computes the preferred alignment of a global variable in bytes for a target. 272 See the method llvm::DataLayout::getPreferredAlignment. */ 273 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD, 274 LLVMValueRef GlobalVar); 275 276 /** Computes the structure element that contains the byte offset for a target. 277 See the method llvm::StructLayout::getElementContainingOffset. */ 278 unsigned LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy, 279 unsigned long long Offset); 280 281 /** Computes the byte offset of the indexed struct element for a target. 282 See the method llvm::StructLayout::getElementContainingOffset. */ 283 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD, 284 LLVMTypeRef StructTy, unsigned Element); 285 286 /** 287 * @} 288 */ 289 290 LLVM_C_EXTERN_C_END 291 292 #endif 293