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 LLVMOpaqueTargetMachine
*LLVMTargetMachineRef
;
35 typedef struct LLVMTarget
*LLVMTargetRef
;
40 LLVMCodeGenLevelDefault
,
41 LLVMCodeGenLevelAggressive
42 } LLVMCodeGenOptLevel
;
48 LLVMRelocDynamicNoPic
,
56 LLVMCodeModelJITDefault
,
67 } LLVMCodeGenFileType
;
69 /** Returns the first llvm::Target in the registered targets list. */
70 LLVMTargetRef
LLVMGetFirstTarget(void);
71 /** Returns the next llvm::Target given a previous one (or null if there's none) */
72 LLVMTargetRef
LLVMGetNextTarget(LLVMTargetRef T
);
74 /*===-- Target ------------------------------------------------------------===*/
75 /** Finds the target corresponding to the given name and stores it in \p T.
76 Returns 0 on success. */
77 LLVMTargetRef
LLVMGetTargetFromName(const char *Name
);
79 /** Finds the target corresponding to the given triple and stores it in \p T.
80 Returns 0 on success. Optionally returns any error in ErrorMessage.
81 Use LLVMDisposeMessage to dispose the message. */
82 LLVMBool
LLVMGetTargetFromTriple(const char* Triple
, LLVMTargetRef
*T
,
85 /** Returns the name of a target. See llvm::Target::getName */
86 const char *LLVMGetTargetName(LLVMTargetRef T
);
88 /** Returns the description of a target. See llvm::Target::getDescription */
89 const char *LLVMGetTargetDescription(LLVMTargetRef T
);
91 /** Returns if the target has a JIT */
92 LLVMBool
LLVMTargetHasJIT(LLVMTargetRef T
);
94 /** Returns if the target has a TargetMachine associated */
95 LLVMBool
LLVMTargetHasTargetMachine(LLVMTargetRef T
);
97 /** Returns if the target as an ASM backend (required for emitting output) */
98 LLVMBool
LLVMTargetHasAsmBackend(LLVMTargetRef T
);
100 /*===-- Target Machine ----------------------------------------------------===*/
101 /** Creates a new llvm::TargetMachine. See llvm::Target::createTargetMachine */
102 LLVMTargetMachineRef
LLVMCreateTargetMachine(LLVMTargetRef T
,
103 const char *Triple
, const char *CPU
, const char *Features
,
104 LLVMCodeGenOptLevel Level
, LLVMRelocMode Reloc
, LLVMCodeModel CodeModel
);
106 /** Dispose the LLVMTargetMachineRef instance generated by
107 LLVMCreateTargetMachine. */
108 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T
);
110 /** Returns the Target used in a TargetMachine */
111 LLVMTargetRef
LLVMGetTargetMachineTarget(LLVMTargetMachineRef T
);
113 /** Returns the triple used creating this target machine. See
114 llvm::TargetMachine::getTriple. The result needs to be disposed with
115 LLVMDisposeMessage. */
116 char *LLVMGetTargetMachineTriple(LLVMTargetMachineRef T
);
118 /** Returns the cpu used creating this target machine. See
119 llvm::TargetMachine::getCPU. The result needs to be disposed with
120 LLVMDisposeMessage. */
121 char *LLVMGetTargetMachineCPU(LLVMTargetMachineRef T
);
123 /** Returns the feature string used creating this target machine. See
124 llvm::TargetMachine::getFeatureString. The result needs to be disposed with
125 LLVMDisposeMessage. */
126 char *LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T
);
128 /** Create a DataLayout based on the targetMachine. */
129 LLVMTargetDataRef
LLVMCreateTargetDataLayout(LLVMTargetMachineRef T
);
131 /** Set the target machine's ASM verbosity. */
132 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T
,
133 LLVMBool VerboseAsm
);
135 /** Emits an asm or object file for the given module to the filename. This
136 wraps several c++ only classes (among them a file stream). Returns any
137 error in ErrorMessage. Use LLVMDisposeMessage to dispose the message. */
138 LLVMBool
LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T
, LLVMModuleRef M
,
139 const char *Filename
,
140 LLVMCodeGenFileType codegen
,
141 char **ErrorMessage
);
143 /** Compile the LLVM IR stored in \p M and store the result in \p OutMemBuf. */
144 LLVMBool
LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T
, LLVMModuleRef M
,
145 LLVMCodeGenFileType codegen
, char** ErrorMessage
, LLVMMemoryBufferRef
*OutMemBuf
);
147 /*===-- Triple ------------------------------------------------------------===*/
148 /** Get a triple for the host machine as a string. The result needs to be
149 disposed with LLVMDisposeMessage. */
150 char* LLVMGetDefaultTargetTriple(void);
152 /** Normalize a target triple. The result needs to be disposed with
153 LLVMDisposeMessage. */
154 char* LLVMNormalizeTargetTriple(const char* triple
);
156 /** Get the host CPU as a string. The result needs to be disposed with
157 LLVMDisposeMessage. */
158 char* LLVMGetHostCPUName(void);
160 /** Get the host CPU's features as a string. The result needs to be disposed
161 with LLVMDisposeMessage. */
162 char* LLVMGetHostCPUFeatures(void);
164 /** Adds the target-specific analysis passes to the pass manager. */
165 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T
, LLVMPassManagerRef PM
);