Provide InitializeAllTargets and InitializeNativeTarget functions in the
[oota-llvm.git] / include / llvm-c / Target.h
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 #ifndef LLVM_C_TARGET_H
20 #define LLVM_C_TARGET_H
21
22 #include "llvm-c/Core.h"
23 #include "llvm/Config/config.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 enum { LLVMBigEndian, LLVMLittleEndian };
30 typedef int LLVMByteOrdering;
31
32 typedef struct LLVMOpaqueTargetData *LLVMTargetDataRef;
33 typedef struct LLVMStructLayout *LLVMStructLayoutRef;
34
35 /* Declare all of the target-initialization functions that are available. */
36 #define LLVM_TARGET(TargetName) void LLVMInitialize##TargetName##Target();
37 #include "llvm/Config/Targets.def"
38
39 /** LLVMInitializeAllTargets - The main program should call this function if it
40     wants to link in all available targets that LLVM is configured to
41     support. */
42 static inline void LLVMInitializeAllTargets() {
43 #define LLVM_TARGET(TargetName) LLVMInitialize##TargetName##Target();
44 #include "llvm/Config/Targets.def"
45 }
46   
47 /** LLVMInitializeNativeTarget - The main program should call this function to
48     initialize the native target corresponding to the host.  This is useful 
49     for JIT applications to ensure that the target gets linked in correctly. */
50 static inline int LLVMInitializeNativeTarget() {
51   /* If we have a native target, initialize it to ensure it is linked in. */
52 #ifdef LLVM_NATIVE_ARCH
53 #define DoInit2(TARG)   LLVMInitialize ## TARG ()
54 #define DoInit(T) DoInit2(T)
55   DoInit(LLVM_NATIVE_ARCH);
56   return 0;
57 #undef DoInit
58 #undef DoInit2
59 #else
60   return 1;
61 #endif
62 }  
63
64 /*===-- Target Data -------------------------------------------------------===*/
65
66 /** Creates target data from a target layout string.
67     See the constructor llvm::TargetData::TargetData. */
68 LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep);
69
70 /** Adds target data information to a pass manager. This does not take ownership
71     of the target data.
72     See the method llvm::PassManagerBase::add. */
73 void LLVMAddTargetData(LLVMTargetDataRef, LLVMPassManagerRef);
74
75 /** Converts target data to a target layout string. The string must be disposed
76     with LLVMDisposeMessage.
77     See the constructor llvm::TargetData::TargetData. */
78 char *LLVMCopyStringRepOfTargetData(LLVMTargetDataRef);
79
80 /** Returns the byte order of a target, either LLVMBigEndian or
81     LLVMLittleEndian.
82     See the method llvm::TargetData::isLittleEndian. */
83 LLVMByteOrdering LLVMByteOrder(LLVMTargetDataRef);
84
85 /** Returns the pointer size in bytes for a target.
86     See the method llvm::TargetData::getPointerSize. */
87 unsigned LLVMPointerSize(LLVMTargetDataRef);
88
89 /** Returns the integer type that is the same size as a pointer on a target.
90     See the method llvm::TargetData::getIntPtrType. */
91 LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef);
92
93 /** Computes the size of a type in bytes for a target.
94     See the method llvm::TargetData::getTypeSizeInBits. */
95 unsigned long long LLVMSizeOfTypeInBits(LLVMTargetDataRef, LLVMTypeRef);
96
97 /** Computes the storage size of a type in bytes for a target.
98     See the method llvm::TargetData::getTypeStoreSize. */
99 unsigned long long LLVMStoreSizeOfType(LLVMTargetDataRef, LLVMTypeRef);
100
101 /** Computes the ABI size of a type in bytes for a target.
102     See the method llvm::TargetData::getTypeAllocSize. */
103 unsigned long long LLVMABISizeOfType(LLVMTargetDataRef, LLVMTypeRef);
104
105 /** Computes the ABI alignment of a type in bytes for a target.
106     See the method llvm::TargetData::getTypeABISize. */
107 unsigned LLVMABIAlignmentOfType(LLVMTargetDataRef, LLVMTypeRef);
108
109 /** Computes the call frame alignment of a type in bytes for a target.
110     See the method llvm::TargetData::getTypeABISize. */
111 unsigned LLVMCallFrameAlignmentOfType(LLVMTargetDataRef, LLVMTypeRef);
112
113 /** Computes the preferred alignment of a type in bytes for a target.
114     See the method llvm::TargetData::getTypeABISize. */
115 unsigned LLVMPreferredAlignmentOfType(LLVMTargetDataRef, LLVMTypeRef);
116
117 /** Computes the preferred alignment of a global variable in bytes for a target.
118     See the method llvm::TargetData::getPreferredAlignment. */
119 unsigned LLVMPreferredAlignmentOfGlobal(LLVMTargetDataRef,
120                                         LLVMValueRef GlobalVar);
121
122 /** Computes the structure element that contains the byte offset for a target.
123     See the method llvm::StructLayout::getElementContainingOffset. */
124 unsigned LLVMElementAtOffset(LLVMTargetDataRef, LLVMTypeRef StructTy,
125                              unsigned long long Offset);
126
127 /** Computes the byte offset of the indexed struct element for a target.
128     See the method llvm::StructLayout::getElementContainingOffset. */
129 unsigned long long LLVMOffsetOfElement(LLVMTargetDataRef, LLVMTypeRef StructTy,
130                                        unsigned Element);
131
132 /** Struct layouts are speculatively cached. If a TargetDataRef is alive when
133     types are being refined and removed, this method must be called whenever a
134     struct type is removed to avoid a dangling pointer in this cache.
135     See the method llvm::TargetData::InvalidateStructLayoutInfo. */
136 void LLVMInvalidateStructLayout(LLVMTargetDataRef, LLVMTypeRef StructTy);
137
138 /** Deallocates a TargetData.
139     See the destructor llvm::TargetData::~TargetData. */
140 void LLVMDisposeTargetData(LLVMTargetDataRef);
141
142
143 #ifdef __cplusplus
144 }
145
146 namespace llvm {
147   class TargetData;
148
149   inline TargetData *unwrap(LLVMTargetDataRef P) {
150     return reinterpret_cast<TargetData*>(P);
151   }
152   
153   inline LLVMTargetDataRef wrap(const TargetData *P) {
154     return reinterpret_cast<LLVMTargetDataRef>(const_cast<TargetData*>(P));
155   }
156 }
157
158 #endif /* defined(__cplusplus) */
159
160 #endif