1 /*===-- llvm-c/TargetMachine.h - Target Machine Library C Interface - C++ -*-=*\
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
5 |* See https://llvm.org/LICENSE.txt for license information. *|
6 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
8 |*===----------------------------------------------------------------------===*|
10 |* This header declares the C interface to the Target and TargetMachine *|
11 |* classes, which can be used to generate assembly or object files. *|
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. *|
17 \*===----------------------------------------------------------------------===*/
19 #ifndef LLVM_C_TARGETMACHINE_H
20 #define LLVM_C_TARGETMACHINE_H
22 #include "llvm-c/ExternC.h"
23 #include "llvm-c/Target.h"
24 #include "llvm-c/Types.h"
29 * @addtogroup LLVMCTarget
34 typedef struct LLVMOpaqueTargetMachineOptions
*LLVMTargetMachineOptionsRef
;
35 typedef struct LLVMOpaqueTargetMachine
*LLVMTargetMachineRef
;
36 typedef struct LLVMTarget
*LLVMTargetRef
;
41 LLVMCodeGenLevelDefault
,
42 LLVMCodeGenLevelAggressive
43 } LLVMCodeGenOptLevel
;
49 LLVMRelocDynamicNoPic
,
57 LLVMCodeModelJITDefault
,
68 } LLVMCodeGenFileType
;
71 LLVMGlobalISelAbortEnable
,
72 LLVMGlobalISelAbortDisable
,
73 LLVMGlobalISelAbortDisableWithDiag
,
74 } LLVMGlobalISelAbortMode
;
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
);
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
);
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
,
92 /** Returns the name of a target. See llvm::Target::getName */
93 const char *LLVMGetTargetName(LLVMTargetRef T
);
95 /** Returns the description of a target. See llvm::Target::getDescription */
96 const char *LLVMGetTargetDescription(LLVMTargetRef T
);
98 /** Returns if the target has a JIT */
99 LLVMBool
LLVMTargetHasJIT(LLVMTargetRef T
);
101 /** Returns if the target has a TargetMachine associated */
102 LLVMBool
LLVMTargetHasTargetMachine(LLVMTargetRef T
);
104 /** Returns if the target as an ASM backend (required for emitting output) */
105 LLVMBool
LLVMTargetHasAsmBackend(LLVMTargetRef T
);
107 /*===-- Target Machine ----------------------------------------------------===*/
109 * Create a new set of options for an llvm::TargetMachine.
111 * The returned option structure must be released with
112 * LLVMDisposeTargetMachineOptions() after the call to
113 * LLVMCreateTargetMachineWithOptions().
115 LLVMTargetMachineOptionsRef
LLVMCreateTargetMachineOptions(void);
118 * Dispose of an LLVMTargetMachineOptionsRef instance.
120 void LLVMDisposeTargetMachineOptions(LLVMTargetMachineOptionsRef Options
);
122 void LLVMTargetMachineOptionsSetCPU(LLVMTargetMachineOptionsRef Options
,
126 * Set the list of features for the target machine.
128 * \param Features a comma-separated list of features.
130 void LLVMTargetMachineOptionsSetFeatures(LLVMTargetMachineOptionsRef Options
,
131 const char *Features
);
133 void LLVMTargetMachineOptionsSetABI(LLVMTargetMachineOptionsRef Options
,
136 void LLVMTargetMachineOptionsSetCodeGenOptLevel(
137 LLVMTargetMachineOptionsRef Options
, LLVMCodeGenOptLevel Level
);
139 void LLVMTargetMachineOptionsSetRelocMode(LLVMTargetMachineOptionsRef Options
,
140 LLVMRelocMode Reloc
);
142 void LLVMTargetMachineOptionsSetCodeModel(LLVMTargetMachineOptionsRef Options
,
143 LLVMCodeModel CodeModel
);
146 * Create a new llvm::TargetMachine.
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()).
154 LLVMCreateTargetMachineWithOptions(LLVMTargetRef T
, const char *Triple
,
155 LLVMTargetMachineOptionsRef Options
);
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
);
162 /** Dispose the LLVMTargetMachineRef instance generated by
163 LLVMCreateTargetMachine. */
164 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T
);
166 /** Returns the Target used in a TargetMachine */
167 LLVMTargetRef
LLVMGetTargetMachineTarget(LLVMTargetMachineRef T
);
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
);
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
);
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
);
184 /** Create a DataLayout based on the targetMachine. */
185 LLVMTargetDataRef
LLVMCreateTargetDataLayout(LLVMTargetMachineRef T
);
187 /** Set the target machine's ASM verbosity. */
188 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T
,
189 LLVMBool VerboseAsm
);
191 /** Enable fast-path instruction selection. */
192 void LLVMSetTargetMachineFastISel(LLVMTargetMachineRef T
, LLVMBool Enable
);
194 /** Enable global instruction selection. */
195 void LLVMSetTargetMachineGlobalISel(LLVMTargetMachineRef T
, LLVMBool Enable
);
197 /** Set abort behaviour when global instruction selection fails to lower/select
199 void LLVMSetTargetMachineGlobalISelAbort(LLVMTargetMachineRef T
,
200 LLVMGlobalISelAbortMode Mode
);
202 /** Enable the MachineOutliner pass. */
203 void LLVMSetTargetMachineMachineOutliner(LLVMTargetMachineRef T
,
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
);
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
);
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);
223 /** Normalize a target triple. The result needs to be disposed with
224 LLVMDisposeMessage. */
225 char* LLVMNormalizeTargetTriple(const char* triple
);
227 /** Get the host CPU as a string. The result needs to be disposed with
228 LLVMDisposeMessage. */
229 char* LLVMGetHostCPUName(void);
231 /** Get the host CPU's features as a string. The result needs to be disposed
232 with LLVMDisposeMessage. */
233 char* LLVMGetHostCPUFeatures(void);
235 /** Adds the target-specific analysis passes to the pass manager. */
236 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T
, LLVMPassManagerRef PM
);