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 LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
35 typedef struct LLVMTarget *LLVMTargetRef;
36 
37 typedef enum {
38     LLVMCodeGenLevelNone,
39     LLVMCodeGenLevelLess,
40     LLVMCodeGenLevelDefault,
41     LLVMCodeGenLevelAggressive
42 } LLVMCodeGenOptLevel;
43 
44 typedef enum {
45     LLVMRelocDefault,
46     LLVMRelocStatic,
47     LLVMRelocPIC,
48     LLVMRelocDynamicNoPic,
49     LLVMRelocROPI,
50     LLVMRelocRWPI,
51     LLVMRelocROPI_RWPI
52 } LLVMRelocMode;
53 
54 typedef enum {
55     LLVMCodeModelDefault,
56     LLVMCodeModelJITDefault,
57     LLVMCodeModelTiny,
58     LLVMCodeModelSmall,
59     LLVMCodeModelKernel,
60     LLVMCodeModelMedium,
61     LLVMCodeModelLarge
62 } LLVMCodeModel;
63 
64 typedef enum {
65     LLVMAssemblyFile,
66     LLVMObjectFile
67 } LLVMCodeGenFileType;
68 
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);
73 
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);
78 
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,
83                                  char **ErrorMessage);
84 
85 /** Returns the name of a target. See llvm::Target::getName */
86 const char *LLVMGetTargetName(LLVMTargetRef T);
87 
88 /** Returns the description  of a target. See llvm::Target::getDescription */
89 const char *LLVMGetTargetDescription(LLVMTargetRef T);
90 
91 /** Returns if the target has a JIT */
92 LLVMBool LLVMTargetHasJIT(LLVMTargetRef T);
93 
94 /** Returns if the target has a TargetMachine associated */
95 LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T);
96 
97 /** Returns if the target as an ASM backend (required for emitting output) */
98 LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T);
99 
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);
105 
106 /** Dispose the LLVMTargetMachineRef instance generated by
107   LLVMCreateTargetMachine. */
108 void LLVMDisposeTargetMachine(LLVMTargetMachineRef T);
109 
110 /** Returns the Target used in a TargetMachine */
111 LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T);
112 
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);
117 
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);
122 
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);
127 
128 /** Create a DataLayout based on the targetMachine. */
129 LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T);
130 
131 /** Set the target machine's ASM verbosity. */
132 void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
133                                       LLVMBool VerboseAsm);
134 
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);
142 
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);
146 
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);
151 
152 /** Normalize a target triple. The result needs to be disposed with
153   LLVMDisposeMessage. */
154 char* LLVMNormalizeTargetTriple(const char* triple);
155 
156 /** Get the host CPU as a string. The result needs to be disposed with
157   LLVMDisposeMessage. */
158 char* LLVMGetHostCPUName(void);
159 
160 /** Get the host CPU's features as a string. The result needs to be disposed
161   with LLVMDisposeMessage. */
162 char* LLVMGetHostCPUFeatures(void);
163 
164 /** Adds the target-specific analysis passes to the pass manager. */
165 void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM);
166 
167 /**
168  * @}
169  */
170 
171 LLVM_C_EXTERN_C_END
172 
173 #endif
174