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