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
26LLVM_C_EXTERN_C_BEGIN
27
28/**
29 * @addtogroup LLVMCTarget
30 *
31 * @{
32 */
33
34typedef struct LLVMOpaqueTargetMachineOptions *LLVMTargetMachineOptionsRef;
35typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
36typedef struct LLVMTarget *LLVMTargetRef;
37
38typedef enum {
39 LLVMCodeGenLevelNone,
40 LLVMCodeGenLevelLess,
41 LLVMCodeGenLevelDefault,
42 LLVMCodeGenLevelAggressive
43} LLVMCodeGenOptLevel;
44
45typedef enum {
46 LLVMRelocDefault,
47 LLVMRelocStatic,
48 LLVMRelocPIC,
49 LLVMRelocDynamicNoPic,
50 LLVMRelocROPI,
51 LLVMRelocRWPI,
52 LLVMRelocROPI_RWPI
53} LLVMRelocMode;
54
55typedef enum {
56 LLVMCodeModelDefault,
57 LLVMCodeModelJITDefault,
58 LLVMCodeModelTiny,
59 LLVMCodeModelSmall,
60 LLVMCodeModelKernel,
61 LLVMCodeModelMedium,
62 LLVMCodeModelLarge
63} LLVMCodeModel;
64
65typedef enum {
66 LLVMAssemblyFile,
67 LLVMObjectFile
68} LLVMCodeGenFileType;
69
70typedef enum {
71 LLVMGlobalISelAbortEnable,
72 LLVMGlobalISelAbortDisable,
73 LLVMGlobalISelAbortDisableWithDiag,
74} LLVMGlobalISelAbortMode;
75
76/** Returns the first llvm::Target in the registered targets list. */
77LLVMTargetRef LLVMGetFirstTarget(void);
78/** Returns the next llvm::Target given a previous one (or null if there's none) */
79LLVMTargetRef 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. */
84LLVMTargetRef 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. */
89LLVMBool LLVMGetTargetFromTriple(const char* Triple, LLVMTargetRef *T,
90 char **ErrorMessage);
91
92/** Returns the name of a target. See llvm::Target::getName */
93const char *LLVMGetTargetName(LLVMTargetRef T);
94
95/** Returns the description of a target. See llvm::Target::getDescription */
96const char *LLVMGetTargetDescription(LLVMTargetRef T);
97
98/** Returns if the target has a JIT */
99LLVMBool LLVMTargetHasJIT(LLVMTargetRef T);
100
101/** Returns if the target has a TargetMachine associated */
102LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T);
103
104/** Returns if the target as an ASM backend (required for emitting output) */
105LLVMBool 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 */
115LLVMTargetMachineOptionsRef LLVMCreateTargetMachineOptions(void);
116
117/**
118 * Dispose of an LLVMTargetMachineOptionsRef instance.
119 */
120void LLVMDisposeTargetMachineOptions(LLVMTargetMachineOptionsRef Options);
121
122void 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 */
130void LLVMTargetMachineOptionsSetFeatures(LLVMTargetMachineOptionsRef Options,
131 const char *Features);
132
133void LLVMTargetMachineOptionsSetABI(LLVMTargetMachineOptionsRef Options,
134 const char *ABI);
135
136void LLVMTargetMachineOptionsSetCodeGenOptLevel(
137 LLVMTargetMachineOptionsRef Options, LLVMCodeGenOptLevel Level);
138
139void LLVMTargetMachineOptionsSetRelocMode(LLVMTargetMachineOptionsRef Options,
140 LLVMRelocMode Reloc);
141
142void 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 */
153LLVMTargetMachineRef
154LLVMCreateTargetMachineWithOptions(LLVMTargetRef T, const char *Triple,
155 LLVMTargetMachineOptionsRef Options);
156
157/** Creates a new llvm::TargetMachine. See llvm::Target::createTargetMachine */
158LLVMTargetMachineRef 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. */
164void LLVMDisposeTargetMachine(LLVMTargetMachineRef T);
165
166/** Returns the Target used in a TargetMachine */
167LLVMTargetRef 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. */
172char *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. */
177char *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. */
182char *LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T);
183
184/** Create a DataLayout based on the targetMachine. */
185LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T);
186
187/** Set the target machine's ASM verbosity. */
188void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
189 LLVMBool VerboseAsm);
190
191/** Enable fast-path instruction selection. */
192void LLVMSetTargetMachineFastISel(LLVMTargetMachineRef T, LLVMBool Enable);
193
194/** Enable global instruction selection. */
195void LLVMSetTargetMachineGlobalISel(LLVMTargetMachineRef T, LLVMBool Enable);
196
197/** Set abort behaviour when global instruction selection fails to lower/select
198 * an instruction. */
199void LLVMSetTargetMachineGlobalISelAbort(LLVMTargetMachineRef T,
200 LLVMGlobalISelAbortMode Mode);
201
202/** Enable the MachineOutliner pass. */
203void 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. */
209LLVMBool 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. */
215LLVMBool 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. */
221char* LLVMGetDefaultTargetTriple(void);
222
223/** Normalize a target triple. The result needs to be disposed with
224 LLVMDisposeMessage. */
225char* LLVMNormalizeTargetTriple(const char* triple);
226
227/** Get the host CPU as a string. The result needs to be disposed with
228 LLVMDisposeMessage. */
229char* LLVMGetHostCPUName(void);
230
231/** Get the host CPU's features as a string. The result needs to be disposed
232 with LLVMDisposeMessage. */
233char* LLVMGetHostCPUFeatures(void);
234
235/** Adds the target-specific analysis passes to the pass manager. */
236void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM);
237
238/**
239 * @}
240 */
241
242LLVM_C_EXTERN_C_END
243
244#endif
245