TargetMachine.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*===-- llvm-c/TargetMachine.h - Target Machine Library C Interface - 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 the Target and TargetMachine *|
  11. |* classes, which can be used to generate assembly or object files. *|
  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. #ifndef LLVM_C_TARGETMACHINE_H
  19. #define LLVM_C_TARGETMACHINE_H
  20. #include "ExternC.h"
  21. #include "Target.h"
  22. #include "Types.h"
  23. LLVM_C_EXTERN_C_BEGIN
  24. /**
  25. * @addtogroup LLVMCTarget
  26. *
  27. * @{
  28. */
  29. typedef struct LLVMOpaqueTargetMachineOptions *LLVMTargetMachineOptionsRef;
  30. typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
  31. typedef struct LLVMTarget *LLVMTargetRef;
  32. typedef enum {
  33. LLVMCodeGenLevelNone,
  34. LLVMCodeGenLevelLess,
  35. LLVMCodeGenLevelDefault,
  36. LLVMCodeGenLevelAggressive
  37. } LLVMCodeGenOptLevel;
  38. typedef enum {
  39. LLVMRelocDefault,
  40. LLVMRelocStatic,
  41. LLVMRelocPIC,
  42. LLVMRelocDynamicNoPic,
  43. LLVMRelocROPI,
  44. LLVMRelocRWPI,
  45. LLVMRelocROPI_RWPI
  46. } LLVMRelocMode;
  47. typedef enum {
  48. LLVMCodeModelDefault,
  49. LLVMCodeModelJITDefault,
  50. LLVMCodeModelTiny,
  51. LLVMCodeModelSmall,
  52. LLVMCodeModelKernel,
  53. LLVMCodeModelMedium,
  54. LLVMCodeModelLarge
  55. } LLVMCodeModel;
  56. typedef enum {
  57. LLVMAssemblyFile,
  58. LLVMObjectFile
  59. } LLVMCodeGenFileType;
  60. typedef enum {
  61. LLVMGlobalISelAbortEnable,
  62. LLVMGlobalISelAbortDisable,
  63. LLVMGlobalISelAbortDisableWithDiag,
  64. } LLVMGlobalISelAbortMode;
  65. /** Returns the first llvm::Target in the registered targets list. */
  66. LLVMTargetRef LLVMGetFirstTarget(void);
  67. /** Returns the next llvm::Target given a previous one (or null if there's none) */
  68. LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T);
  69. /*===-- Target ------------------------------------------------------------===*/
  70. /** Finds the target corresponding to the given name and stores it in \p T.
  71. Returns 0 on success. */
  72. LLVMTargetRef LLVMGetTargetFromName(const char *Name);
  73. /** Finds the target corresponding to the given triple and stores it in \p T.
  74. Returns 0 on success. Optionally returns any error in ErrorMessage.
  75. Use LLVMDisposeMessage to dispose the message. */
  76. LLVMBool LLVMGetTargetFromTriple(const char* Triple, LLVMTargetRef *T,
  77. char **ErrorMessage);
  78. /** Returns the name of a target. See llvm::Target::getName */
  79. const char *LLVMGetTargetName(LLVMTargetRef T);
  80. /** Returns the description of a target. See llvm::Target::getDescription */
  81. const char *LLVMGetTargetDescription(LLVMTargetRef T);
  82. /** Returns if the target has a JIT */
  83. LLVMBool LLVMTargetHasJIT(LLVMTargetRef T);
  84. /** Returns if the target has a TargetMachine associated */
  85. LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T);
  86. /** Returns if the target as an ASM backend (required for emitting output) */
  87. LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T);
  88. /*===-- Target Machine ----------------------------------------------------===*/
  89. /**
  90. * Create a new set of options for an llvm::TargetMachine.
  91. *
  92. * The returned option structure must be released with
  93. * LLVMDisposeTargetMachineOptions() after the call to
  94. * LLVMCreateTargetMachineWithOptions().
  95. */
  96. LLVMTargetMachineOptionsRef LLVMCreateTargetMachineOptions(void);
  97. /**
  98. * Dispose of an LLVMTargetMachineOptionsRef instance.
  99. */
  100. void LLVMDisposeTargetMachineOptions(LLVMTargetMachineOptionsRef Options);
  101. void LLVMTargetMachineOptionsSetCPU(LLVMTargetMachineOptionsRef Options,
  102. const char *CPU);
  103. /**
  104. * Set the list of features for the target machine.
  105. *
  106. * \param Features a comma-separated list of features.
  107. */
  108. void LLVMTargetMachineOptionsSetFeatures(LLVMTargetMachineOptionsRef Options,
  109. const char *Features);
  110. void LLVMTargetMachineOptionsSetABI(LLVMTargetMachineOptionsRef Options,
  111. const char *ABI);
  112. void LLVMTargetMachineOptionsSetCodeGenOptLevel(
  113. LLVMTargetMachineOptionsRef Options, LLVMCodeGenOptLevel Level);
  114. void LLVMTargetMachineOptionsSetRelocMode(LLVMTargetMachineOptionsRef Options,
  115. LLVMRelocMode Reloc);
  116. void LLVMTargetMachineOptionsSetCodeModel(LLVMTargetMachineOptionsRef Options,
  117. LLVMCodeModel CodeModel);
  118. /**
  119. * Create a new llvm::TargetMachine.
  120. *
  121. * \param T the target to create a machine for.
  122. * \param Triple a triple describing the target machine.
  123. * \param Options additional configuration (see
  124. * LLVMCreateTargetMachineOptions()).
  125. */
  126. LLVMTargetMachineRef
  127. LLVMCreateTargetMachineWithOptions(LLVMTargetRef T, const char *Triple,
  128. LLVMTargetMachineOptionsRef Options);
  129. /** Creates a new llvm::TargetMachine. See llvm::Target::createTargetMachine */
  130. LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
  131. const char *Triple, const char *CPU, const char *Features,
  132. LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, LLVMCodeModel CodeModel);
  133. /** Dispose the LLVMTargetMachineRef instance generated by
  134. LLVMCreateTargetMachine. */
  135. void LLVMDisposeTargetMachine(LLVMTargetMachineRef T);
  136. /** Returns the Target used in a TargetMachine */
  137. LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T);
  138. /** Returns the triple used creating this target machine. See
  139. llvm::TargetMachine::getTriple. The result needs to be disposed with
  140. LLVMDisposeMessage. */
  141. char *LLVMGetTargetMachineTriple(LLVMTargetMachineRef T);
  142. /** Returns the cpu used creating this target machine. See
  143. llvm::TargetMachine::getCPU. The result needs to be disposed with
  144. LLVMDisposeMessage. */
  145. char *LLVMGetTargetMachineCPU(LLVMTargetMachineRef T);
  146. /** Returns the feature string used creating this target machine. See
  147. llvm::TargetMachine::getFeatureString. The result needs to be disposed with
  148. LLVMDisposeMessage. */
  149. char *LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T);
  150. /** Create a DataLayout based on the targetMachine. */
  151. LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T);
  152. /** Set the target machine's ASM verbosity. */
  153. void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
  154. LLVMBool VerboseAsm);
  155. /** Enable fast-path instruction selection. */
  156. void LLVMSetTargetMachineFastISel(LLVMTargetMachineRef T, LLVMBool Enable);
  157. /** Enable global instruction selection. */
  158. void LLVMSetTargetMachineGlobalISel(LLVMTargetMachineRef T, LLVMBool Enable);
  159. /** Set abort behaviour when global instruction selection fails to lower/select
  160. * an instruction. */
  161. void LLVMSetTargetMachineGlobalISelAbort(LLVMTargetMachineRef T,
  162. LLVMGlobalISelAbortMode Mode);
  163. /** Enable the MachineOutliner pass. */
  164. void LLVMSetTargetMachineMachineOutliner(LLVMTargetMachineRef T,
  165. LLVMBool Enable);
  166. /** Emits an asm or object file for the given module to the filename. This
  167. wraps several c++ only classes (among them a file stream). Returns any
  168. error in ErrorMessage. Use LLVMDisposeMessage to dispose the message. */
  169. LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
  170. const char *Filename,
  171. LLVMCodeGenFileType codegen,
  172. char **ErrorMessage);
  173. /** Compile the LLVM IR stored in \p M and store the result in \p OutMemBuf. */
  174. LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, LLVMModuleRef M,
  175. LLVMCodeGenFileType codegen, char** ErrorMessage, LLVMMemoryBufferRef *OutMemBuf);
  176. /*===-- Triple ------------------------------------------------------------===*/
  177. /** Get a triple for the host machine as a string. The result needs to be
  178. disposed with LLVMDisposeMessage. */
  179. char* LLVMGetDefaultTargetTriple(void);
  180. /** Normalize a target triple. The result needs to be disposed with
  181. LLVMDisposeMessage. */
  182. char* LLVMNormalizeTargetTriple(const char* triple);
  183. /** Get the host CPU as a string. The result needs to be disposed with
  184. LLVMDisposeMessage. */
  185. char* LLVMGetHostCPUName(void);
  186. /** Get the host CPU's features as a string. The result needs to be disposed
  187. with LLVMDisposeMessage. */
  188. char* LLVMGetHostCPUFeatures(void);
  189. /** Adds the target-specific analysis passes to the pass manager. */
  190. void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM);
  191. /**
  192. * @}
  193. */
  194. LLVM_C_EXTERN_C_END
  195. #endif