10b226cc7c25c2cc3b702f0565a5f8a0eb770fd3
[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/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/GlobalVariable.h"
20 #include "llvm/TypeSymbolTable.h"
21 #include <cassert>
22
23 using namespace llvm;
24
25
26 /*===-- Operations on modules ---------------------------------------------===*/
27
28 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
29   return wrap(new Module(ModuleID));
30 }
31
32 void LLVMDisposeModule(LLVMModuleRef M) {
33   delete unwrap(M);
34 }
35
36 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
37   return unwrap(M)->addTypeName(Name, unwrap(Ty));
38 }
39
40 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
41   std::string N(Name);
42   
43   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
44   for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
45     if (I->first == N)
46       TST.remove(I);
47 }
48
49
50 /*===-- Operations on types -----------------------------------------------===*/
51
52 /*--.. Operations on all types (mostly) ....................................--*/
53
54 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
55   return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
56 }
57
58 void LLVMRefineAbstractType(LLVMTypeRef AbstractType, LLVMTypeRef ConcreteType){
59   DerivedType *Ty = unwrap<DerivedType>(AbstractType);
60   Ty->refineAbstractTypeTo(unwrap(ConcreteType));
61 }
62
63 /*--.. Operations on integer types .........................................--*/
64
65 LLVMTypeRef LLVMInt1Type()  { return (LLVMTypeRef) Type::Int1Ty;  }
66 LLVMTypeRef LLVMInt8Type()  { return (LLVMTypeRef) Type::Int8Ty;  }
67 LLVMTypeRef LLVMInt16Type() { return (LLVMTypeRef) Type::Int16Ty; }
68 LLVMTypeRef LLVMInt32Type() { return (LLVMTypeRef) Type::Int32Ty; }
69 LLVMTypeRef LLVMInt64Type() { return (LLVMTypeRef) Type::Int64Ty; }
70
71 LLVMTypeRef LLVMIntType(unsigned NumBits) {
72   return wrap(IntegerType::get(NumBits));
73 }
74
75 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
76   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
77 }
78
79 /*--.. Operations on real types ............................................--*/
80
81 LLVMTypeRef LLVMFloatType()    { return (LLVMTypeRef) Type::FloatTy;     }
82 LLVMTypeRef LLVMDoubleType()   { return (LLVMTypeRef) Type::DoubleTy;    }
83 LLVMTypeRef LLVMX86FP80Type()  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
84 LLVMTypeRef LLVMFP128Type()    { return (LLVMTypeRef) Type::FP128Ty;     }
85 LLVMTypeRef LLVMPPCFP128Type() { return (LLVMTypeRef) Type::PPC_FP128Ty; }
86
87 /*--.. Operations on function types ........................................--*/
88
89 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
90                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
91                              int IsVarArg) {
92   std::vector<const Type*> Tys;
93   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
94     Tys.push_back(unwrap(*I));
95   
96   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
97 }
98
99 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
100   return unwrap<FunctionType>(FunctionTy)->isVarArg();
101 }
102
103 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
104   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
105 }
106
107 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
108   return unwrap<FunctionType>(FunctionTy)->getNumParams();
109 }
110
111 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
112   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
113   for (FunctionType::param_iterator I = Ty->param_begin(),
114                                     E = Ty->param_end(); I != E; ++I)
115     *Dest++ = wrap(*I);
116 }
117
118 /*--.. Operations on struct types ..........................................--*/
119
120 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
121                            unsigned ElementCount, int Packed) {
122   std::vector<const Type*> Tys;
123   for (LLVMTypeRef *I = ElementTypes,
124                    *E = ElementTypes + ElementCount; I != E; ++I)
125     Tys.push_back(unwrap(*I));
126   
127   return wrap(StructType::get(Tys, Packed != 0));
128 }
129
130 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
131   return unwrap<StructType>(StructTy)->getNumElements();
132 }
133
134 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
135   StructType *Ty = unwrap<StructType>(StructTy);
136   for (FunctionType::param_iterator I = Ty->element_begin(),
137                                     E = Ty->element_end(); I != E; ++I)
138     *Dest++ = wrap(*I);
139 }
140
141 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
142   return unwrap<StructType>(StructTy)->isPacked();
143 }
144
145 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
146
147 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount){
148   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
149 }
150
151 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType) {
152   return wrap(PointerType::get(unwrap(ElementType)));
153 }
154
155 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType,unsigned ElementCount){
156   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
157 }
158
159 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
160   return wrap(unwrap<SequentialType>(Ty)->getElementType());
161 }
162
163 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
164   return unwrap<ArrayType>(ArrayTy)->getNumElements();
165 }
166
167 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
168   return unwrap<VectorType>(VectorTy)->getNumElements();
169 }
170
171 /*--.. Operations on other types ...........................................--*/
172
173 LLVMTypeRef LLVMVoidType()  { return (LLVMTypeRef) Type::VoidTy;  }
174 LLVMTypeRef LLVMLabelType() { return (LLVMTypeRef) Type::LabelTy; }
175
176 LLVMTypeRef LLVMOpaqueType() {
177   return wrap(llvm::OpaqueType::get());
178 }
179
180
181 /*===-- Operations on values ----------------------------------------------===*/
182
183 /*--.. Operations on all values ............................................--*/
184
185 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
186   return wrap(unwrap(Val)->getType());
187 }
188
189 const char *LLVMGetValueName(LLVMValueRef Val) {
190   return unwrap(Val)->getNameStart();
191 }
192
193 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
194   unwrap(Val)->setName(Name);
195 }
196
197 void LLVMDumpValue(LLVMValueRef Val) {
198   unwrap(Val)->dump();
199 }
200
201 /*--.. Operations on constants of any type .................................--*/
202
203 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
204   return wrap(Constant::getNullValue(unwrap(Ty)));
205 }
206
207 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
208   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
209 }
210
211 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
212   return wrap(UndefValue::get(unwrap(Ty)));
213 }
214
215 int LLVMIsConstant(LLVMValueRef Ty) {
216   return isa<Constant>(unwrap(Ty));
217 }
218
219 int LLVMIsNull(LLVMValueRef Val) {
220   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
221     return C->isNullValue();
222   return false;
223 }
224
225 int LLVMIsUndef(LLVMValueRef Val) {
226   return isa<UndefValue>(unwrap(Val));
227 }
228
229 /*--.. Operations on scalar constants ......................................--*/
230
231 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
232                           int SignExtend) {
233   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
234 }
235
236 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
237   return wrap(ConstantFP::get(unwrap(RealTy), APFloat(N)));
238 }
239
240 /*--.. Operations on composite constants ...................................--*/
241
242 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
243                              int DontNullTerminate) {
244   /* Inverted the sense of AddNull because ', 0)' is a
245      better mnemonic for null termination than ', 1)'. */
246   return wrap(ConstantArray::get(std::string(Str, Length),
247                                  DontNullTerminate == 0));
248 }
249
250 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
251                             LLVMValueRef *ConstantVals, unsigned Length) {
252   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
253                                  unwrap<Constant>(ConstantVals, Length),
254                                  Length));
255 }
256
257 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
258                              int Packed) {
259   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
260                                   Count, Packed != 0));
261 }
262
263 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
264   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
265                                   Size));
266 }
267
268 /*--.. Constant expressions ................................................--*/
269
270 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
271   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
272 }
273
274 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
275   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
276 }
277
278 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
279   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
280 }
281
282 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
283   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
284                                    unwrap<Constant>(RHSConstant)));
285 }
286
287 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
288   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
289                                    unwrap<Constant>(RHSConstant)));
290 }
291
292 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
293   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
294                                    unwrap<Constant>(RHSConstant)));
295 }
296
297 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
298   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
299                                     unwrap<Constant>(RHSConstant)));
300 }
301
302 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
303   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
304                                     unwrap<Constant>(RHSConstant)));
305 }
306
307 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
308   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
309                                     unwrap<Constant>(RHSConstant)));
310 }
311
312 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
313   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
314                                     unwrap<Constant>(RHSConstant)));
315 }
316
317 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
318   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
319                                     unwrap<Constant>(RHSConstant)));
320 }
321
322 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
323   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
324                                     unwrap<Constant>(RHSConstant)));
325 }
326
327 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
328   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
329                                    unwrap<Constant>(RHSConstant)));
330 }
331
332 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
333   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
334                                   unwrap<Constant>(RHSConstant)));
335 }
336
337 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
338   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
339                                    unwrap<Constant>(RHSConstant)));
340 }
341
342 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
343                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
344   return wrap(ConstantExpr::getICmp(Predicate,
345                                     unwrap<Constant>(LHSConstant),
346                                     unwrap<Constant>(RHSConstant)));
347 }
348
349 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
350                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
351   return wrap(ConstantExpr::getFCmp(Predicate,
352                                     unwrap<Constant>(LHSConstant),
353                                     unwrap<Constant>(RHSConstant)));
354 }
355
356 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
357   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
358                                   unwrap<Constant>(RHSConstant)));
359 }
360
361 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
362   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
363                                     unwrap<Constant>(RHSConstant)));
364 }
365
366 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
367   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
368                                     unwrap<Constant>(RHSConstant)));
369 }
370
371 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
372                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
373   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
374                                              unwrap<Constant>(ConstantIndices, 
375                                                               NumIndices),
376                                              NumIndices));
377 }
378
379 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
380   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
381                                      unwrap(ToType)));
382 }
383
384 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
385   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
386                                     unwrap(ToType)));
387 }
388
389 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
390   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
391                                     unwrap(ToType)));
392 }
393
394 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
395   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
396                                        unwrap(ToType)));
397 }
398
399 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
400   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
401                                         unwrap(ToType)));
402 }
403
404 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
405   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
406                                       unwrap(ToType)));
407 }
408
409 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
410   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
411                                       unwrap(ToType)));
412 }
413
414 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
415   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
416                                       unwrap(ToType)));
417 }
418
419 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
420   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
421                                       unwrap(ToType)));
422 }
423
424 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
425   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
426                                         unwrap(ToType)));
427 }
428
429 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
430   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
431                                         unwrap(ToType)));
432 }
433
434 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
435   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
436                                        unwrap(ToType)));
437 }
438
439 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
440                              LLVMValueRef ConstantIfTrue,
441                              LLVMValueRef ConstantIfFalse) {
442   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
443                                       unwrap<Constant>(ConstantIfTrue),
444                                       unwrap<Constant>(ConstantIfFalse)));
445 }
446
447 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
448                                      LLVMValueRef IndexConstant) {
449   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
450                                               unwrap<Constant>(IndexConstant)));
451 }
452
453 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
454                                     LLVMValueRef ElementValueConstant,
455                                     LLVMValueRef IndexConstant) {
456   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
457                                          unwrap<Constant>(ElementValueConstant),
458                                              unwrap<Constant>(IndexConstant)));
459 }
460
461 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
462                                     LLVMValueRef VectorBConstant,
463                                     LLVMValueRef MaskConstant) {
464   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
465                                              unwrap<Constant>(VectorBConstant),
466                                              unwrap<Constant>(MaskConstant)));
467 }
468
469 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
470
471 int LLVMIsDeclaration(LLVMValueRef Global) {
472   return unwrap<GlobalValue>(Global)->isDeclaration();
473 }
474
475 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
476   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
477 }
478
479 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
480   unwrap<GlobalValue>(Global)
481     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
482 }
483
484 const char *LLVMGetSection(LLVMValueRef Global) {
485   return unwrap<GlobalValue>(Global)->getSection().c_str();
486 }
487
488 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
489   unwrap<GlobalValue>(Global)->setSection(Section);
490 }
491
492 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
493   return static_cast<LLVMVisibility>(
494     unwrap<GlobalValue>(Global)->getVisibility());
495 }
496
497 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
498   unwrap<GlobalValue>(Global)
499     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
500 }
501
502 unsigned LLVMGetAlignment(LLVMValueRef Global) {
503   return unwrap<GlobalValue>(Global)->getAlignment();
504 }
505
506 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
507   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
508 }
509
510 /*--.. Operations on global variables ......................................--*/
511
512 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
513   return wrap(new GlobalVariable(unwrap(Ty), false,
514               GlobalValue::ExternalLinkage, 0, Name, unwrap(M)));
515 }
516
517 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
518   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
519 }
520
521 int LLVMHasInitializer(LLVMValueRef GlobalVar) {
522   return unwrap<GlobalVariable>(GlobalVar)->hasInitializer();
523 }
524
525 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
526   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
527 }
528
529 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
530   unwrap<GlobalVariable>(GlobalVar)
531     ->setInitializer(unwrap<Constant>(ConstantVal));
532 }
533
534 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
535   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
536 }
537
538 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
539   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
540 }
541
542 /*--.. Operations on functions .............................................--*/
543
544 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
545                              LLVMTypeRef FunctionTy) {
546   return wrap(new Function(unwrap<FunctionType>(FunctionTy),
547                            GlobalValue::ExternalLinkage, Name, unwrap(M)));
548 }
549
550 void LLVMDeleteFunction(LLVMValueRef Fn) {
551   unwrap<Function>(Fn)->eraseFromParent();
552 }
553
554 unsigned LLVMCountParams(LLVMValueRef FnRef) {
555   // This function is strictly redundant to
556   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
557   return unwrap<Function>(FnRef)->getArgumentList().size();
558 }
559
560 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
561   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
562   while (index --> 0)
563     AI++;
564   return wrap(AI);
565 }
566
567 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
568   Function *Fn = unwrap<Function>(FnRef);
569   for (Function::arg_iterator I = Fn->arg_begin(),
570                               E = Fn->arg_end(); I != E; I++)
571     *ParamRefs++ = wrap(I);
572 }
573
574 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
575   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
576     return F->getIntrinsicID();
577   return 0;
578 }
579
580 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
581   return unwrap<Function>(Fn)->getCallingConv();
582 }
583
584 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
585   return unwrap<Function>(Fn)->setCallingConv(CC);
586 }
587
588 /*--.. Operations on basic blocks ..........................................--*/
589
590 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef Bb) {
591   return wrap(static_cast<Value*>(unwrap(Bb)));
592 }
593
594 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
595   return isa<BasicBlock>(unwrap(Val));
596 }
597
598 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
599   return wrap(unwrap<BasicBlock>(Val));
600 }
601
602 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
603   return unwrap<Function>(FnRef)->getBasicBlockList().size();
604 }
605
606 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
607   Function *Fn = unwrap<Function>(FnRef);
608   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
609     *BasicBlocksRefs++ = wrap(I);
610 }
611
612 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
613   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
614 }
615
616 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
617   return wrap(new BasicBlock(Name, unwrap<Function>(FnRef)));
618 }
619
620 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
621                                        const char *Name) {
622   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
623   return wrap(new BasicBlock(Name, InsertBeforeBB->getParent(),
624                              InsertBeforeBB));
625 }
626
627 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
628   unwrap(BBRef)->eraseFromParent();
629 }
630
631 /*--.. Call and invoke instructions ........................................--*/
632
633 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
634   Value *V = unwrap(Instr);
635   if (CallInst *CI = dyn_cast<CallInst>(V))
636     return CI->getCallingConv();
637   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
638     return II->getCallingConv();
639   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
640   return 0;
641 }
642
643 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
644   Value *V = unwrap(Instr);
645   if (CallInst *CI = dyn_cast<CallInst>(V))
646     return CI->setCallingConv(CC);
647   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
648     return II->setCallingConv(CC);
649   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
650 }
651
652
653 /*===-- Instruction builders ----------------------------------------------===*/
654
655 LLVMBuilderRef LLVMCreateBuilder() {
656   return wrap(new LLVMBuilder());
657 }
658
659 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
660   Instruction *I = unwrap<Instruction>(Instr);
661   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
662 }
663
664 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
665   BasicBlock *BB = unwrap(Block);
666   unwrap(Builder)->SetInsertPoint(BB);
667 }
668
669 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
670   delete unwrap(Builder);
671 }
672
673 /*--.. Instruction builders ................................................--*/
674
675 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
676   return wrap(unwrap(B)->CreateRetVoid());
677 }
678
679 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
680   return wrap(unwrap(B)->CreateRet(unwrap(V)));
681 }
682
683 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
684   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
685 }
686
687 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
688                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
689   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
690 }
691
692 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
693                              LLVMBasicBlockRef Else, unsigned NumCases) {
694   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
695 }
696
697 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
698                              LLVMValueRef *Args, unsigned NumArgs,
699                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
700                              const char *Name) {
701   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
702                                       unwrap(Args), unwrap(Args) + NumArgs,
703                                       Name));
704 }
705
706 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
707   return wrap(unwrap(B)->CreateUnwind());
708 }
709
710 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
711   return wrap(unwrap(B)->CreateUnreachable());
712 }
713
714 /*--.. Arithmetic ..........................................................--*/
715
716 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
717                           const char *Name) {
718   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
719 }
720
721 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
722                           const char *Name) {
723   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
724 }
725
726 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
727                           const char *Name) {
728   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
729 }
730
731 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
732                            const char *Name) {
733   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
734 }
735
736 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
737                            const char *Name) {
738   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
739 }
740
741 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
742                            const char *Name) {
743   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
744 }
745
746 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
747                            const char *Name) {
748   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
749 }
750
751 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
752                            const char *Name) {
753   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
754 }
755
756 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
757                            const char *Name) {
758   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
759 }
760
761 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
762                           const char *Name) {
763   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
764 }
765
766 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
767                            const char *Name) {
768   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
769 }
770
771 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
772                            const char *Name) {
773   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
774 }
775
776 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
777                           const char *Name) {
778   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
779 }
780
781 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
782                          const char *Name) {
783   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
784 }
785
786 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
787                           const char *Name) {
788   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
789 }
790
791 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
792   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
793 }
794
795 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
796   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
797 }
798
799 /*--.. Memory ..............................................................--*/
800
801 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
802                              const char *Name) {
803   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
804 }
805
806 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
807                                   LLVMValueRef Val, const char *Name) {
808   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
809 }
810
811 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
812                              const char *Name) {
813   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
814 }
815
816 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
817                                   LLVMValueRef Val, const char *Name) {
818   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
819 }
820
821 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
822   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
823 }
824
825
826 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
827                            const char *Name) {
828   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
829 }
830
831 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
832                             LLVMValueRef PointerVal) {
833   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
834 }
835
836 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
837                           LLVMValueRef *Indices, unsigned NumIndices,
838                           const char *Name) {
839   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
840                                    unwrap(Indices) + NumIndices, Name));
841 }
842
843 /*--.. Casts ...............................................................--*/
844
845 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
846                             LLVMTypeRef DestTy, const char *Name) {
847   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
848 }
849
850 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
851                            LLVMTypeRef DestTy, const char *Name) {
852   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
853 }
854
855 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
856                            LLVMTypeRef DestTy, const char *Name) {
857   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
858 }
859
860 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
861                              LLVMTypeRef DestTy, const char *Name) {
862   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
863 }
864
865 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
866                              LLVMTypeRef DestTy, const char *Name) {
867   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
868 }
869
870 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
871                              LLVMTypeRef DestTy, const char *Name) {
872   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
873 }
874
875 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
876                              LLVMTypeRef DestTy, const char *Name) {
877   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
878 }
879
880 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
881                               LLVMTypeRef DestTy, const char *Name) {
882   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
883 }
884
885 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
886                             LLVMTypeRef DestTy, const char *Name) {
887   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
888 }
889
890 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
891                                LLVMTypeRef DestTy, const char *Name) {
892   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
893 }
894
895 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
896                                LLVMTypeRef DestTy, const char *Name) {
897   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
898 }
899
900 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
901                               LLVMTypeRef DestTy, const char *Name) {
902   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
903 }
904
905 /*--.. Comparisons .........................................................--*/
906
907 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
908                            LLVMValueRef LHS, LLVMValueRef RHS,
909                            const char *Name) {
910   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
911                                     unwrap(LHS), unwrap(RHS), Name));
912 }
913
914 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
915                            LLVMValueRef LHS, LLVMValueRef RHS,
916                            const char *Name) {
917   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
918                                     unwrap(LHS), unwrap(RHS), Name));
919 }
920
921 /*--.. Miscellaneous instructions ..........................................--*/
922
923 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
924   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
925 }
926
927 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
928                            LLVMValueRef *Args, unsigned NumArgs,
929                            const char *Name) {
930   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
931                                     unwrap(Args) + NumArgs, Name));
932 }
933
934 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
935                              LLVMValueRef Then, LLVMValueRef Else,
936                              const char *Name) {
937   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
938                                       Name));
939 }
940
941 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
942                             LLVMTypeRef Ty, const char *Name) {
943   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
944 }
945
946 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
947                                       LLVMValueRef Index, const char *Name) {
948   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
949                                               Name));
950 }
951
952 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
953                                     LLVMValueRef EltVal, LLVMValueRef Index,
954                                     const char *Name) {
955   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
956                                              unwrap(Index), Name));
957 }
958
959 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
960                                     LLVMValueRef V2, LLVMValueRef Mask,
961                                     const char *Name) {
962   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
963                                              unwrap(Mask), Name));
964 }