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