C bindings for libLLVMCore.a and libLLVMBitWriter.a.
[oota-llvm.git] / lib / VMCore / Core.cpp
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Gordon Henriksen and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the C bindings for libLLVMCore.a, which implements
11 // the LLVM intermediate representation.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm-c/Core.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/CHelpers.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/GlobalVariable.h"
21 #include <ostream>
22 #include <fstream>
23 #include <cassert>
24
25 using namespace llvm;
26
27
28 /*===-- Operations on modules ---------------------------------------------===*/
29
30 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
31   return wrap(new Module(ModuleID));
32 }
33
34 void LLVMDisposeModule(LLVMModuleRef M) {
35   delete unwrap(M);
36 }
37
38 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
39   return unwrap(M)->addTypeName(Name, unwrap(Ty));
40 }
41
42
43 /*===-- Operations on types -----------------------------------------------===*/
44
45 /*--.. Operations on all types (mostly) ....................................--*/
46
47 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
48   return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
49 }
50
51 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
52   DerivedType *Ty = unwrap<DerivedType>(AbstractType);
53   Ty->refineAbstractTypeTo(unwrap(ConcreteType));
54 }
55
56 /*--.. Operations on integer types .........................................--*/
57
58 LLVMTypeRef LLVMInt1Type()  { return (LLVMTypeRef) Type::Int1Ty;  }
59 LLVMTypeRef LLVMInt8Type()  { return (LLVMTypeRef) Type::Int8Ty;  }
60 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
61 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
62 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
63
64 LLVMTypeRef LLVMCreateIntegerType(unsigned NumBits) {
65   return wrap(IntegerType::get(NumBits));
66 }
67
68 unsigned LLVMGetIntegerTypeWidth(LLVMTypeRef IntegerTy) {
69   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
70 }
71
72 /*--.. Operations on real types ............................................--*/
73
74 LLVMTypeRef LLVMFloatType()    { return (LLVMTypeRef) Type::FloatTy;     }
75 LLVMTypeRef LLVMDoubleType()   { return (LLVMTypeRef) Type::DoubleTy;    }
76 LLVMTypeRef LLVMX86FP80Type()  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
77 LLVMTypeRef LLVMFP128Type()    { return (LLVMTypeRef) Type::FP128Ty;     }
78 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
79
80 /*--.. Operations on function types ........................................--*/
81
82 LLVMTypeRef LLVMCreateFunctionType(LLVMTypeRef ReturnType,
83                            LLVMTypeRef *ParamTypes, unsigned ParamCount,
84                            int IsVarArg) {
85   std::vector<const Type*> Tys;
86   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
87     Tys.push_back(unwrap(*I));
88   
89   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
90 }
91
92 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
93   return unwrap<FunctionType>(FunctionTy)->isVarArg();
94 }
95
96 LLVMTypeRef LLVMGetFunctionReturnType(LLVMTypeRef FunctionTy) {
97   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
98 }
99
100 unsigned LLVMGetFunctionParamCount(LLVMTypeRef FunctionTy) {
101   return unwrap<FunctionType>(FunctionTy)->getNumParams();
102 }
103
104 void LLVMGetFunctionParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
105   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
106   for (FunctionType::param_iterator I = Ty->param_begin(),
107                                     E = Ty->param_end(); I != E; ++I)
108     *Dest++ = wrap(*I);
109 }
110
111 /*--.. Operations on struct types ..........................................--*/
112
113 LLVMTypeRef LLVMCreateStructType(LLVMTypeRef *ElementTypes,
114                                  unsigned ElementCount, int Packed) {
115   std::vector<const Type*> Tys;
116   for (LLVMTypeRef *I = ElementTypes,
117                    *E = ElementTypes + ElementCount; I != E; ++I)
118     Tys.push_back(unwrap(*I));
119   
120   return wrap(StructType::get(Tys, Packed != 0));
121 }
122
123 unsigned LLVMGetStructElementCount(LLVMTypeRef StructTy) {
124   return unwrap<StructType>(StructTy)->getNumElements();
125 }
126
127 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
128   StructType *Ty = unwrap<StructType>(StructTy);
129   for (FunctionType::param_iterator I = Ty->element_begin(),
130                                     E = Ty->element_end(); I != E; ++I)
131     *Dest++ = wrap(*I);
132 }
133
134 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
135   return unwrap<StructType>(StructTy)->isPacked();
136 }
137
138 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
139
140 LLVMTypeRef LLVMCreateArrayType(LLVMTypeRef ElementType, unsigned ElementCount){
141   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
142 }
143
144 LLVMTypeRef LLVMCreatePointerType(LLVMTypeRef ElementType) {
145   return wrap(PointerType::get(unwrap(ElementType)));
146 }
147
148 LLVMTypeRef LLVMCreateVectorType(LLVMTypeRef ElementType,unsigned ElementCount){
149   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
150 }
151
152 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
153   return wrap(unwrap<SequentialType>(Ty)->getElementType());
154 }
155
156 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
157   return unwrap<ArrayType>(ArrayTy)->getNumElements();
158 }
159
160 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
161   return unwrap<VectorType>(VectorTy)->getNumElements();
162 }
163
164 /*--.. Operations on other types ...........................................--*/
165
166 LLVMTypeRef LLVMVoidType()  { return (LLVMTypeRef) Type::VoidTy;  }
167 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
168
169 LLVMTypeRef LLVMCreateOpaqueType() {
170   return wrap(llvm::OpaqueType::get());
171 }
172
173
174 /*===-- Operations on values ----------------------------------------------===*/
175
176 /*--.. Operations on all values ............................................--*/
177
178 LLVMTypeRef LLVMGetTypeOfValue(LLVMValueRef Val) {
179   return wrap(unwrap(Val)->getType());
180 }
181
182 const char *LLVMGetValueName(LLVMValueRef Val) {
183   return unwrap(Val)->getNameStart();
184 }
185
186 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
187   unwrap(Val)->setName(Name);
188 }
189
190 /*--.. Operations on constants of any type .................................--*/
191
192 LLVMValueRef LLVMGetNull(LLVMTypeRef Ty) {
193   return wrap(Constant::getNullValue(unwrap(Ty)));
194 }
195
196 LLVMValueRef LLVMGetAllOnes(LLVMTypeRef Ty) {
197   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
198 }
199
200 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
201   return wrap(UndefValue::get(unwrap(Ty)));
202 }
203
204 int LLVMIsNull(LLVMValueRef Val) {
205   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
206     return C->isNullValue();
207   return false;
208 }
209
210 /*--.. Operations on scalar constants ......................................--*/
211
212 LLVMValueRef LLVMGetIntConstant(LLVMTypeRef IntTy, unsigned long long N,
213                                 int SignExtend) {
214   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
215 }
216
217 LLVMValueRef LLVMGetRealConstant(LLVMTypeRef RealTy, double N) {
218   return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
219 }
220
221 /*--.. Operations on composite constants ...................................--*/
222
223 LLVMValueRef LLVMGetStringConstant(const char *Str, unsigned Length,
224                                    int DontNullTerminate) {
225   /* Inverted the sense of AddNull because ', 0)' is a
226      better mnemonic for null termination than ', 1)'. */
227   return wrap(ConstantArray::get(std::string(Str, Length),
228                                  DontNullTerminate == 0));
229 }
230
231 LLVMValueRef LLVMGetArrayConstant(LLVMTypeRef ElementTy,
232                                   LLVMValueRef *ConstantVals, unsigned Length) {
233   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
234                                  unwrap<Constant>(ConstantVals, Length),
235                                  Length));
236 }
237
238 LLVMValueRef LLVMGetStructConstant(LLVMValueRef *ConstantVals, unsigned Count,
239                                    int Packed) {
240   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
241                                   Count, Packed != 0));
242 }
243
244 LLVMValueRef LLVMGetVectorConstant(LLVMValueRef *ScalarConstantVals,
245                                    unsigned Size) {
246   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
247                                   Size));
248 }
249
250 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
251
252 int LLVMIsDeclaration(LLVMValueRef Global) {
253   return unwrap<GlobalValue>(Global)->isDeclaration();
254 }
255
256 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
257   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
258 }
259
260 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
261   unwrap<GlobalValue>(Global)
262     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
263 }
264
265 const char *LLVMGetSection(LLVMValueRef Global) {
266   return unwrap<GlobalValue>(Global)->getSection().c_str();
267 }
268
269 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
270   unwrap<GlobalValue>(Global)->setSection(Section);
271 }
272
273 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
274   return static_cast<LLVMVisibility>(
275     unwrap<GlobalValue>(Global)->getVisibility());
276 }
277
278 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
279   unwrap<GlobalValue>(Global)
280     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
281 }
282
283 unsigned LLVMGetAlignment(LLVMValueRef Global) {
284   return unwrap<GlobalValue>(Global)->getAlignment();
285 }
286
287 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
288   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
289 }
290
291 /*--.. Operations on global variables ......................................--*/
292
293 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
294   return wrap(new GlobalVariable(unwrap(Ty), false,
295               GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
296 }
297
298 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
299   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
300 }
301
302 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
303   return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
304 }
305
306 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
307   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
308 }
309
310 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
311   unwrap<GlobalVariable>(GlobalVar)
312     ->setInitializer(unwrap<Constant>(ConstantVal));
313 }
314
315 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
316   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
317 }
318
319 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
320   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
321 }
322