xref: /freebsd/contrib/llvm-project/llvm/include/llvm-c/Target.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 /*===-- llvm-c/Target.h - Target Lib C Iface --------------------*- 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 libLLVMTarget.a, which             */
11 /* implements target information.                                             */
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_TARGET_H
20 #define LLVM_C_TARGET_H
21 
22 #include "llvm-c/ExternC.h"
23 #include "llvm-c/Types.h"
24 #include "llvm-c/Visibility.h"
25 #include "llvm/Config/llvm-config.h"
26 
27 LLVM_C_EXTERN_C_BEGIN
28 
29 /**
30  * @defgroup LLVMCTarget Target information
31  * @ingroup LLVMC
32  *
33  * @{
34  */
35 
36 enum LLVMByteOrdering { LLVMBigEndian, LLVMLittleEndian };
37 
38 typedef struct LLVMOpaqueTargetData *LLVMTargetDataRef;
39 typedef struct LLVMOpaqueTargetLibraryInfotData *LLVMTargetLibraryInfoRef;
40 
41 /* Declare all of the target-initialization functions that are available. */
42 #define LLVM_TARGET(TargetName)                                                \
43   LLVM_C_ABI void LLVMInitialize##TargetName##TargetInfo(void);
44 #include "llvm/Config/Targets.def"
45 #undef LLVM_TARGET  /* Explicit undef to make SWIG happier */
46 
47 #define LLVM_TARGET(TargetName)                                                \
48   LLVM_C_ABI void LLVMInitialize##TargetName##Target(void);
49 #include "llvm/Config/Targets.def"
50 #undef LLVM_TARGET  /* Explicit undef to make SWIG happier */
51 
52 #define LLVM_TARGET(TargetName)                                                \
53   LLVM_C_ABI void LLVMInitialize##TargetName##TargetMC(void);
54 #include "llvm/Config/Targets.def"
55 #undef LLVM_TARGET  /* Explicit undef to make SWIG happier */
56 
57 /* Declare all of the available assembly printer initialization functions. */
58 #define LLVM_ASM_PRINTER(TargetName)                                           \
59   LLVM_C_ABI void LLVMInitialize##TargetName##AsmPrinter(void);
60 #include "llvm/Config/AsmPrinters.def"
61 #undef LLVM_ASM_PRINTER  /* Explicit undef to make SWIG happier */
62 
63 /* Declare all of the available assembly parser initialization functions. */
64 #define LLVM_ASM_PARSER(TargetName)                                            \
65   LLVM_C_ABI void LLVMInitialize##TargetName##AsmParser(void);
66 #include "llvm/Config/AsmParsers.def"
67 #undef LLVM_ASM_PARSER  /* Explicit undef to make SWIG happier */
68 
69 /* Declare all of the available disassembler initialization functions. */
70 #define LLVM_DISASSEMBLER(TargetName)                                          \
71   LLVM_C_ABI void LLVMInitialize##TargetName##Disassembler(void);
72 #include "llvm/Config/Disassemblers.def"
73 #undef LLVM_DISASSEMBLER  /* Explicit undef to make SWIG happier */
74 
75 /** LLVMInitializeAllTargetInfos - The main program should call this function if
76     it wants access to all available targets that LLVM is configured to
77     support. */
LLVMInitializeAllTargetInfos(void)78 static inline void LLVMInitializeAllTargetInfos(void) {
79 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##TargetInfo();
80 #include "llvm/Config/Targets.def"
81 #undef LLVM_TARGET  /* Explicit undef to make SWIG happier */
82 }
83 
84 /** LLVMInitializeAllTargets - The main program should call this function if it
85     wants to link in all available targets that LLVM is configured to
86     support. */
LLVMInitializeAllTargets(void)87 static inline void LLVMInitializeAllTargets(void) {
88 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##Target();
89 #include "llvm/Config/Targets.def"
90 #undef LLVM_TARGET  /* Explicit undef to make SWIG happier */
91 }
92 
93 /** LLVMInitializeAllTargetMCs - The main program should call this function if
94     it wants access to all available target MC that LLVM is configured to
95     support. */
LLVMInitializeAllTargetMCs(void)96 static inline void LLVMInitializeAllTargetMCs(void) {
97 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##TargetMC();
98 #include "llvm/Config/Targets.def"
99 #undef LLVM_TARGET  /* Explicit undef to make SWIG happier */
100 }
101 
102 /** LLVMInitializeAllAsmPrinters - The main program should call this function if
103     it wants all asm printers that LLVM is configured to support, to make them
104     available via the TargetRegistry. */
LLVMInitializeAllAsmPrinters(void)105 static inline void LLVMInitializeAllAsmPrinters(void) {
106 #define LLVM_ASM_PRINTER(TargetName) LLVMInitialize##TargetName##AsmPrinter();
107 #include "llvm/Config/AsmPrinters.def"
108 #undef LLVM_ASM_PRINTER  /* Explicit undef to make SWIG happier */
109 }
110 
111 /** LLVMInitializeAllAsmParsers - The main program should call this function if
112     it wants all asm parsers that LLVM is configured to support, to make them
113     available via the TargetRegistry. */
LLVMInitializeAllAsmParsers(void)114 static inline void LLVMInitializeAllAsmParsers(void) {
115 #define LLVM_ASM_PARSER(TargetName) LLVMInitialize##TargetName##AsmParser();
116 #include "llvm/Config/AsmParsers.def"
117 #undef LLVM_ASM_PARSER  /* Explicit undef to make SWIG happier */
118 }
119 
120 /** LLVMInitializeAllDisassemblers - The main program should call this function
121     if it wants all disassemblers that LLVM is configured to support, to make
122     them available via the TargetRegistry. */
LLVMInitializeAllDisassemblers(void)123 static inline void LLVMInitializeAllDisassemblers(void) {
124 #define LLVM_DISASSEMBLER(TargetName) \
125   LLVMInitialize##TargetName##Disassembler();
126 #include "llvm/Config/Disassemblers.def"
127 #undef LLVM_DISASSEMBLER  /* Explicit undef to make SWIG happier */
128 }
129 
130 /** LLVMInitializeNativeTarget - The main program should call this function to
131     initialize the native target corresponding to the host.  This is useful
132     for JIT applications to ensure that the target gets linked in correctly. */
LLVMInitializeNativeTarget(void)133 static inline LLVMBool LLVMInitializeNativeTarget(void) {
134   /* If we have a native target, initialize it to ensure it is linked in. */
135 #ifdef LLVM_NATIVE_TARGET
136   LLVM_NATIVE_TARGETINFO();
137   LLVM_NATIVE_TARGET();
138   LLVM_NATIVE_TARGETMC();
139   return 0;
140 #else
141   return 1;
142 #endif
143 }
144 
145 /** LLVMInitializeNativeTargetAsmParser - The main program should call this
146     function to initialize the parser for the native target corresponding to the
147     host. */
LLVMInitializeNativeAsmParser(void)148 static inline LLVMBool LLVMInitializeNativeAsmParser(void) {
149 #ifdef LLVM_NATIVE_ASMPARSER
150   LLVM_NATIVE_ASMPARSER();
151   return 0;
152 #else
153   return 1;
154 #endif
155 }
156 
157 /** LLVMInitializeNativeTargetAsmPrinter - The main program should call this
158     function to initialize the printer for the native target corresponding to
159     the host. */
LLVMInitializeNativeAsmPrinter(void)160 static inline LLVMBool LLVMInitializeNativeAsmPrinter(void) {
161 #ifdef LLVM_NATIVE_ASMPRINTER
162   LLVM_NATIVE_ASMPRINTER();
163   return 0;
164 #else
165   return 1;
166 #endif
167 }
168 
169 /** LLVMInitializeNativeTargetDisassembler - The main program should call this
170     function to initialize the disassembler for the native target corresponding
171     to the host. */
LLVMInitializeNativeDisassembler(void)172 static inline LLVMBool LLVMInitializeNativeDisassembler(void) {
173 #ifdef LLVM_NATIVE_DISASSEMBLER
174   LLVM_NATIVE_DISASSEMBLER();
175   return 0;
176 #else
177   return 1;
178 #endif
179 }
180 
181 /*===-- Target Data -------------------------------------------------------===*/
182 
183 /**
184  * Obtain the data layout for a module.
185  *
186  * @see Module::getDataLayout()
187  */
188 LLVM_C_ABI LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M);
189 
190 /**
191  * Set the data layout for a module.
192  *
193  * @see Module::setDataLayout()
194  */
195 LLVM_C_ABI void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL);
196 
197 /** Creates target data from a target layout string.
198     See the constructor llvm::DataLayout::DataLayout. */
199 LLVM_C_ABI LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep);
200 
201 /** Deallocates a TargetData.
202     See the destructor llvm::DataLayout::~DataLayout. */
203 LLVM_C_ABI void LLVMDisposeTargetData(LLVMTargetDataRef TD);
204 
205 /** Adds target library information to a pass manager. This does not take
206     ownership of the target library info.
207     See the method llvm::PassManagerBase::add. */
208 LLVM_C_ABI void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
209                                          LLVMPassManagerRef PM);
210 
211 /** Converts target data to a target layout string. The string must be disposed
212     with LLVMDisposeMessage.
213     See the constructor llvm::DataLayout::DataLayout. */
214 LLVM_C_ABI char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD);
215 
216 /** Returns the byte order of a target, either LLVMBigEndian or
217     LLVMLittleEndian.
218     See the method llvm::DataLayout::isLittleEndian. */
219 LLVM_C_ABI enum LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD);
220 
221 /** Returns the pointer size in bytes for a target.
222     See the method llvm::DataLayout::getPointerSize. */
223 LLVM_C_ABI unsigned LLVMPointerSize(LLVMTargetDataRef TD);
224 
225 /** Returns the pointer size in bytes for a target for a specified
226     address space.
227     See the method llvm::DataLayout::getPointerSize. */
228 LLVM_C_ABI unsigned LLVMPointerSizeForAS(LLVMTargetDataRef TD, unsigned AS);
229 
230 /** Returns the integer type that is the same size as a pointer on a target.
231     See the method llvm::DataLayout::getIntPtrType. */
232 LLVM_C_ABI LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD);
233 
234 /** Returns the integer type that is the same size as a pointer on a target.
235     This version allows the address space to be specified.
236     See the method llvm::DataLayout::getIntPtrType. */
237 LLVM_C_ABI LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, unsigned AS);
238 
239 /** Returns the integer type that is the same size as a pointer on a target.
240     See the method llvm::DataLayout::getIntPtrType. */
241 LLVM_C_ABI LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C,
242                                                LLVMTargetDataRef TD);
243 
244 /** Returns the integer type that is the same size as a pointer on a target.
245     This version allows the address space to be specified.
246     See the method llvm::DataLayout::getIntPtrType. */
247 LLVM_C_ABI LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C,
248                                                     LLVMTargetDataRef TD,
249                                                     unsigned AS);
250 
251 /** Computes the size of a type in bits for a target.
252     See the method llvm::DataLayout::getTypeSizeInBits. */
253 LLVM_C_ABI unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef TD,
254                                                    LLVMTypeRef Ty);
255 
256 /** Computes the storage size of a type in bytes for a target.
257     See the method llvm::DataLayout::getTypeStoreSize. */
258 LLVM_C_ABI unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef TD,
259                                                   LLVMTypeRef Ty);
260 
261 /** Computes the ABI size of a type in bytes for a target.
262     See the method llvm::DataLayout::getTypeAllocSize. */
263 LLVM_C_ABI unsigned long long LLVMABISizeOfType(LLVMTargetDataRef TD,
264                                                 LLVMTypeRef Ty);
265 
266 /** Computes the ABI alignment of a type in bytes for a target.
267     See the method llvm::DataLayout::getTypeABISize. */
268 LLVM_C_ABI unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef TD,
269                                            LLVMTypeRef Ty);
270 
271 /** Computes the call frame alignment of a type in bytes for a target.
272     See the method llvm::DataLayout::getTypeABISize. */
273 LLVM_C_ABI unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD,
274                                                  LLVMTypeRef Ty);
275 
276 /** Computes the preferred alignment of a type in bytes for a target.
277     See the method llvm::DataLayout::getTypeABISize. */
278 LLVM_C_ABI unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD,
279                                                  LLVMTypeRef Ty);
280 
281 /** Computes the preferred alignment of a global variable in bytes for a target.
282     See the method llvm::DataLayout::getPreferredAlignment. */
283 LLVM_C_ABI unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
284                                                    LLVMValueRef GlobalVar);
285 
286 /** Computes the structure element that contains the byte offset for a target.
287     See the method llvm::StructLayout::getElementContainingOffset. */
288 LLVM_C_ABI unsigned LLVMElementAtOffset(LLVMTargetDataRef TD,
289                                         LLVMTypeRef StructTy,
290                                         unsigned long long Offset);
291 
292 /** Computes the byte offset of the indexed struct element for a target.
293     See the method llvm::StructLayout::getElementContainingOffset. */
294 LLVM_C_ABI unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef TD,
295                                                   LLVMTypeRef StructTy,
296                                                   unsigned Element);
297 
298 /**
299  * @}
300  */
301 
302 LLVM_C_EXTERN_C_END
303 
304 #endif
305