1 /*===-- llvm-c/Target.h - Target Lib C Iface --------------------*- C++ -*-===*/
2 /*                                                                            */
3 /*                     The LLVM Compiler Infrastructure                       */
4 /*                                                                            */
5 /* This file is distributed under the University of Illinois Open Source      */
6 /* License. See LICENSE.TXT for details.                                      */
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 module llvm.c.target;
20 
21 public import llvm.c.types;
22 
23 extern(C) nothrow:
24 
25 /**
26  * @defgroup LLVMCTarget Target information
27  * @ingroup LLVMC
28  *
29  * @{
30  */
31 
32 enum LLVMByteOrdering { BigEndian, LittleEndian };
33 
34 struct __LLVMOpaqueTargetData {};
35 alias LLVMTargetDataRef = __LLVMOpaqueTargetData*;
36 struct __LLVMOpaqueTargetLibraryInfotData {};
37 alias LLVMTargetLibraryInfoRef = __LLVMOpaqueTargetLibraryInfotData*;
38 struct __LLVMStructLayout {};
39 
40 extern(D) string LLVM_TARGET(string delegate(string) nothrow fun) {
41   string ret;
42   foreach (str; [
43   /*
44     "ARM",
45     "CellSPU",
46     "CppBackend",
47     "Hexagon",
48     "Mips",
49     "MBlaze",
50     "MSP430",
51     "PowerPC",
52     "PTX",
53     "Sparc",
54   */
55     "X86",
56   /*
57     "XCore",
58   */
59   ]) {
60     ret ~= fun(str) ~ "\n";
61   }
62 
63   return ret;
64 }
65 
66 /* Declare all of the target-initialization functions that are available. */
67 extern(D) mixin(LLVM_TARGET(delegate string(string name) {
68   return "extern(C) void LLVMInitialize" ~ name ~ "TargetInfo();";
69 }));
70 
71 extern(D) mixin(LLVM_TARGET(delegate string(string name) {
72   return "extern(C) void LLVMInitialize" ~ name ~ "Target();";
73 }));
74 
75 extern(D) mixin(LLVM_TARGET(delegate string(string name) {
76   return "extern(C) void LLVMInitialize" ~ name ~ "TargetMC();";
77 }));
78 
79 
80 extern(D) string LLVM_ASM_PRINTER(string delegate(string) nothrow fun) {
81   string ret;
82   foreach (str; [
83   /*
84     "ARM",
85     "CellSPU",
86     "Hexagon",
87     "Mips",
88     "MBlaze",
89     "MSP430",
90     "PowerPC",
91     "PTX",
92     "Sparc",
93   */
94     "X86",
95   /*
96     "XCore",
97   */
98   ]) {
99     ret ~= fun(str) ~ "\n";
100   }
101 
102   return ret;
103 }
104 
105 /* Declare all of the available assembly printer initialization functions. */
106 extern(D) mixin(LLVM_ASM_PRINTER(delegate string(string name) {
107   return "extern(C) void LLVMInitialize" ~ name ~ "AsmPrinter();";
108 }));
109 
110 extern(D) string LLVM_ASM_PARSER(string delegate(string) nothrow fun) {
111   string ret;
112   foreach (str; [
113   /*
114     "ARM",
115     "Mips",
116     "MBlaze",
117   */
118     "X86",
119   ]) {
120     ret ~= fun(str) ~ "\n";
121   }
122 
123   return ret;
124 }
125 
126 /* Declare all of the available assembly parser initialization functions. */
127 extern(D) mixin(LLVM_ASM_PARSER(delegate string(string name) {
128   return "extern(C) void LLVMInitialize" ~ name ~ "AsmParser();";
129 }));
130 
131 extern(D) string LLVM_ASM_DISASSEMBLER(string delegate(string) nothrow fun) {
132   string ret;
133   foreach (str; [
134   /*
135     "ARM",
136     "Mips",
137     "MBlaze",
138   */
139     "X86",
140   ]) {
141     ret ~= fun(str) ~ "\n";
142   }
143 
144   return ret;
145 }
146 
147 /* Declare all of the available disassembler initialization functions. */
148 extern(D) mixin(LLVM_ASM_PARSER(delegate string(string name) {
149   return "extern(C) void LLVMInitialize" ~ name ~ "Disassembler();";
150 }));
151 
152 /** LLVMInitializeAllTargetInfos - The main program should call this function if
153     it wants access to all available targets that LLVM is configured to
154     support. */
155 static void LLVMInitializeAllTargetInfos() {
156   mixin(LLVM_TARGET(delegate string(string name) {
157     return "LLVMInitialize" ~ name ~ "TargetInfo();";
158   }));
159 }
160 
161 /** LLVMInitializeAllTargets - The main program should call this function if it
162     wants to link in all available targets that LLVM is configured to
163     support. */
164 static void LLVMInitializeAllTargets() {
165   mixin(LLVM_TARGET(delegate string(string name) {
166     return "LLVMInitialize" ~ name ~ "Target();";
167   }));
168 }
169 
170 /** LLVMInitializeAllTargetMCs - The main program should call this function if
171     it wants access to all available target MC that LLVM is configured to
172     support. */
173 static void LLVMInitializeAllTargetMCs() {
174   mixin(LLVM_TARGET(delegate string(string name) {
175     return "LLVMInitialize" ~ name ~ "TargetMC();";
176   }));
177 }
178 
179 /** LLVMInitializeAllAsmPrinters - The main program should call this function if
180     it wants all asm printers that LLVM is configured to support, to make them
181     available via the TargetRegistry. */
182 static void LLVMInitializeAllAsmPrinters() {
183   mixin(LLVM_ASM_PRINTER(delegate string(string name) {
184     return "LLVMInitialize" ~ name ~ "AsmPrinter();";
185   }));
186 }
187 
188 /** LLVMInitializeAllAsmParsers - The main program should call this function if
189     it wants all asm parsers that LLVM is configured to support, to make them
190     available via the TargetRegistry. */
191 static void LLVMInitializeAllAsmParsers() {
192   mixin(LLVM_ASM_PARSER(delegate string(string name) {
193     return "LLVMInitialize" ~ name ~ "AsmParser();";
194   }));
195 }
196 
197 /** LLVMInitializeAllDisassemblers - The main program should call this function
198     if it wants all disassemblers that LLVM is configured to support, to make
199     them available via the TargetRegistry. */
200 static void LLVMInitializeAllDisassemblers() {
201   mixin(LLVM_ASM_DISASSEMBLER(delegate string(string name) {
202     return "LLVMInitialize" ~ name ~ "Disassembler();";
203   }));
204 }
205 
206 /** LLVMInitializeNativeTarget - The main program should call this function to
207     initialize the native target corresponding to the host.  This is useful
208     for JIT applications to ensure that the target gets linked in correctly. */
209 static LLVMBool LLVMInitializeNativeTarget() {
210   /* If we have a native target, initialize it to ensure it is linked in. */
211   return 1;
212 }
213 /+
214 /** LLVMInitializeNativeTargetAsmParser - The main program should call this
215     function to initialize the parser for the native target corresponding to the
216     host. */
217 static inline LLVMBool LLVMInitializeNativeAsmParser() {
218 #ifdef LLVM_NATIVE_ASMPARSER
219   LLVM_NATIVE_ASMPARSER();
220   return 0;
221 #else
222   return 1;
223 #endif
224 }
225 
226 /** LLVMInitializeNativeTargetAsmPrinter - The main program should call this
227     function to initialize the printer for the native target corresponding to
228     the host. */
229 static inline LLVMBool LLVMInitializeNativeAsmPrinter() {
230 #ifdef LLVM_NATIVE_ASMPRINTER
231   LLVM_NATIVE_ASMPRINTER();
232   return 0;
233 #else
234   return 1;
235 #endif
236 }
237 
238 /** LLVMInitializeNativeTargetDisassembler - The main program should call this
239     function to initialize the disassembler for the native target corresponding
240     to the host. */
241 static inline LLVMBool LLVMInitializeNativeDisassembler() {
242 #ifdef LLVM_NATIVE_DISASSEMBLER
243   LLVM_NATIVE_DISASSEMBLER();
244   return 0;
245 #else
246   return 1;
247 #endif
248 }
249 +/
250 /*===-- Target Data -------------------------------------------------------===*/
251 
252 /**
253  * Obtain the data layout for a module.
254  *
255  * @see Module::getDataLayout()
256  */
257 LLVMTargetDataRef LLVMGetModuleDataLayout(LLVMModuleRef M);
258 
259 /**
260  * Set the data layout for a module.
261  *
262  * @see Module::setDataLayout()
263  */
264 void LLVMSetModuleDataLayout(LLVMModuleRef M, LLVMTargetDataRef DL);
265 
266 /** Creates target data from a target layout string.
267     See the constructor llvm::DataLayout::DataLayout. */
268 LLVMTargetDataRef LLVMCreateTargetData(const(char)* StringRep);
269 
270 /** Deallocates a TargetData.
271     See the destructor llvm::DataLayout::~DataLayout. */
272 void LLVMDisposeTargetData(LLVMTargetDataRef TD);
273 
274 /** Adds target library information to a pass manager. This does not take
275     ownership of the target library info.
276     See the method llvm::PassManagerBase::add. */
277 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
278                               LLVMPassManagerRef PM);
279 
280 /** Converts target data to a target layout string. The string must be disposed
281     with LLVMDisposeMessage.
282     See the constructor llvm::DataLayout::DataLayout. */
283 char* LLVMCopyStringRepOfTargetData(LLVMTargetDataRef TD);
284 
285 /** Returns the byte order of a target, either LLVMBigEndian or
286     LLVMLittleEndian.
287     See the method llvm::DataLayout::isLittleEndian. */
288 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef TD);
289 
290 /** Returns the pointer size in bytes for a target.
291     See the method llvm::TargetData::getPointerSize. */
292 uint LLVMPointerSize(LLVMTargetDataRef TD);
293 
294 /** Returns the pointer size in bytes for a target for a specified
295     address space.
296     See the method llvm::DataLayout::getPointerSize. */
297 uint LLVMPointerSizeForAS(LLVMTargetDataRef TD, uint AS);
298 
299 /** Returns the integer type that is the same size as a pointer on a target.
300     See the method llvm::DataLayout::getIntPtrType. */
301 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD);
302 
303 /** Returns the integer type that is the same size as a pointer on a target.
304     This version allows the address space to be specified.
305     See the method llvm::DataLayout::getIntPtrType. */
306 LLVMTypeRef LLVMIntPtrTypeForAS(LLVMTargetDataRef TD, uint AS);
307 
308 /** Returns the integer type that is the same size as a pointer on a target.
309     See the method llvm::DataLayout::getIntPtrType. */
310 LLVMTypeRef LLVMIntPtrTypeInContext(LLVMContextRef C, LLVMTargetDataRef TD);
311 
312 /** Returns the integer type that is the same size as a pointer on a target.
313     This version allows the address space to be specified.
314     See the method llvm::DataLayout::getIntPtrType. */
315 LLVMTypeRef LLVMIntPtrTypeForASInContext(LLVMContextRef C, LLVMTargetDataRef TD,
316                                          uint AS);
317 
318 /** Computes the size of a type in bytes for a target.
319     See the method llvm::DataLayout::getTypeSizeInBits. */
320 ulong LLVMSizeOfTypeInBits(LLVMTargetDataRef TD, LLVMTypeRef Ty);
321 
322 /** Computes the storage size of a type in bytes for a target.
323     See the method llvm::DataLayout::getTypeStoreSize. */
324 ulong LLVMStoreSizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty);
325 
326 /** Computes the ABI size of a type in bytes for a target.
327     See the method llvm::DataLayout::getTypeAllocSize. */
328 ulong LLVMABISizeOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty);
329 
330 /** Computes the ABI alignment of a type in bytes for a target.
331     See the method llvm::DataLayout::getTypeABISize. */
332 uint LLVMABIAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty);
333 
334 /** Computes the call frame alignment of a type in bytes for a target.
335     See the method llvm::DataLayout::getTypeABISize. */
336 uint LLVMCallFrameAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty);
337 
338 /** Computes the preferred alignment of a type in bytes for a target.
339     See the method llvm::DataLayout::getTypeABISize. */
340 uint LLVMPreferredAlignmentOfType(LLVMTargetDataRef TD, LLVMTypeRef Ty);
341 
342 /** Computes the preferred alignment of a global variable in bytes for a target.
343     See the method llvm::DataLayout::getPreferredAlignment. */
344 uint LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef TD,
345                                     LLVMValueRef GlobalVar);
346 
347 /** Computes the structure element that contains the byte offset for a target.
348     See the method llvm::StructLayout::getElementContainingOffset. */
349 uint LLVMElementAtOffset(LLVMTargetDataRef TD, LLVMTypeRef StructTy,
350                          ulong Offset);
351 
352 /** Computes the byte offset of the indexed struct element for a target.
353     See the method llvm::StructLayout::getElementContainingOffset. */
354 ulong LLVMOffsetOfElement(LLVMTargetDataRef TD,
355                           LLVMTypeRef StructTy, uint Element);
356 
357 /**
358  * @}
359  */