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