C bindings for dyn_cast_or_null.
[oota-llvm.git] / lib / VMCore / Core.cpp
1 //===-- Core.cpp ----------------------------------------------------------===//
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 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/GlobalAlias.h"
21 #include "llvm/TypeSymbolTable.h"
22 #include "llvm/ModuleProvider.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/CallSite.h"
27 #include <cassert>
28 #include <cstdlib>
29 #include <cstring>
30
31 using namespace llvm;
32
33
34 /*===-- Error handling ----------------------------------------------------===*/
35
36 void LLVMDisposeMessage(char *Message) {
37   free(Message);
38 }
39
40
41 /*===-- Operations on modules ---------------------------------------------===*/
42
43 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
44   return wrap(new Module(ModuleID));
45 }
46
47 void LLVMDisposeModule(LLVMModuleRef M) {
48   delete unwrap(M);
49 }
50
51 /*--.. Data layout .........................................................--*/
52 const char * LLVMGetDataLayout(LLVMModuleRef M) {
53   return unwrap(M)->getDataLayout().c_str();
54 }
55
56 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
57   unwrap(M)->setDataLayout(Triple);
58 }
59
60 /*--.. Target triple .......................................................--*/
61 const char * LLVMGetTarget(LLVMModuleRef M) {
62   return unwrap(M)->getTargetTriple().c_str();
63 }
64
65 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
66   unwrap(M)->setTargetTriple(Triple);
67 }
68
69 /*--.. Type names ..........................................................--*/
70 int LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
71   return unwrap(M)->addTypeName(Name, unwrap(Ty));
72 }
73
74 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
75   std::string N(Name);
76   
77   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
78   for (TypeSymbolTable::iterator I = TST.begin(), E = TST.end(); I != E; ++I)
79     if (I->first == N)
80       TST.remove(I);
81 }
82
83 void LLVMDumpModule(LLVMModuleRef M) {
84   unwrap(M)->dump();
85 }
86
87
88 /*===-- Operations on types -----------------------------------------------===*/
89
90 /*--.. Operations on all types (mostly) ....................................--*/
91
92 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
93   return static_cast<LLVMTypeKind>(unwrap(Ty)->getTypeID());
94 }
95
96 /*--.. Operations on integer types .........................................--*/
97
98 LLVMTypeRef LLVMInt1Type(void)  { return (LLVMTypeRef) Type::Int1Ty;  }
99 LLVMTypeRef LLVMInt8Type(void)  { return (LLVMTypeRef) Type::Int8Ty;  }
100 LLVMTypeRef LLVMInt16Type(void) { return (LLVMTypeRef) Type::Int16Ty; }
101 LLVMTypeRef LLVMInt32Type(void) { return (LLVMTypeRef) Type::Int32Ty; }
102 LLVMTypeRef LLVMInt64Type(void) { return (LLVMTypeRef) Type::Int64Ty; }
103
104 LLVMTypeRef LLVMIntType(unsigned NumBits) {
105   return wrap(IntegerType::get(NumBits));
106 }
107
108 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
109   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
110 }
111
112 /*--.. Operations on real types ............................................--*/
113
114 LLVMTypeRef LLVMFloatType(void)    { return (LLVMTypeRef) Type::FloatTy;     }
115 LLVMTypeRef LLVMDoubleType(void)   { return (LLVMTypeRef) Type::DoubleTy;    }
116 LLVMTypeRef LLVMX86FP80Type(void)  { return (LLVMTypeRef) Type::X86_FP80Ty;  }
117 LLVMTypeRef LLVMFP128Type(void)    { return (LLVMTypeRef) Type::FP128Ty;     }
118 LLVMTypeRef LLVMPPCFP128Type(void) { return (LLVMTypeRef) Type::PPC_FP128Ty; }
119
120 /*--.. Operations on function types ........................................--*/
121
122 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
123                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
124                              int IsVarArg) {
125   std::vector<const Type*> Tys;
126   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
127     Tys.push_back(unwrap(*I));
128   
129   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
130 }
131
132 int LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
133   return unwrap<FunctionType>(FunctionTy)->isVarArg();
134 }
135
136 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
137   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
138 }
139
140 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
141   return unwrap<FunctionType>(FunctionTy)->getNumParams();
142 }
143
144 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
145   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
146   for (FunctionType::param_iterator I = Ty->param_begin(),
147                                     E = Ty->param_end(); I != E; ++I)
148     *Dest++ = wrap(*I);
149 }
150
151 /*--.. Operations on struct types ..........................................--*/
152
153 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
154                            unsigned ElementCount, int Packed) {
155   std::vector<const Type*> Tys;
156   for (LLVMTypeRef *I = ElementTypes,
157                    *E = ElementTypes + ElementCount; I != E; ++I)
158     Tys.push_back(unwrap(*I));
159   
160   return wrap(StructType::get(Tys, Packed != 0));
161 }
162
163 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
164   return unwrap<StructType>(StructTy)->getNumElements();
165 }
166
167 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
168   StructType *Ty = unwrap<StructType>(StructTy);
169   for (FunctionType::param_iterator I = Ty->element_begin(),
170                                     E = Ty->element_end(); I != E; ++I)
171     *Dest++ = wrap(*I);
172 }
173
174 int LLVMIsPackedStruct(LLVMTypeRef StructTy) {
175   return unwrap<StructType>(StructTy)->isPacked();
176 }
177
178 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
179
180 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
181   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
182 }
183
184 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
185   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
186 }
187
188 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
189   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
190 }
191
192 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
193   return wrap(unwrap<SequentialType>(Ty)->getElementType());
194 }
195
196 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
197   return unwrap<ArrayType>(ArrayTy)->getNumElements();
198 }
199
200 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
201   return unwrap<PointerType>(PointerTy)->getAddressSpace();
202 }
203
204 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
205   return unwrap<VectorType>(VectorTy)->getNumElements();
206 }
207
208 /*--.. Operations on other types ...........................................--*/
209
210 LLVMTypeRef LLVMVoidType(void)  { return (LLVMTypeRef) Type::VoidTy;  }
211 LLVMTypeRef LLVMLabelType(void) { return (LLVMTypeRef) Type::LabelTy; }
212
213 LLVMTypeRef LLVMOpaqueType(void) {
214   return wrap(llvm::OpaqueType::get());
215 }
216
217 /*--.. Operations on type handles ..........................................--*/
218
219 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
220   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
221 }
222
223 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
224   delete unwrap(TypeHandle);
225 }
226
227 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
228   return wrap(unwrap(TypeHandle)->get());
229 }
230
231 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
232   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
233 }
234
235
236 /*===-- Operations on values ----------------------------------------------===*/
237
238 /*--.. Operations on all values ............................................--*/
239
240 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
241   return wrap(unwrap(Val)->getType());
242 }
243
244 const char *LLVMGetValueName(LLVMValueRef Val) {
245   return unwrap(Val)->getNameStart();
246 }
247
248 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
249   unwrap(Val)->setName(Name);
250 }
251
252 void LLVMDumpValue(LLVMValueRef Val) {
253   unwrap(Val)->dump();
254 }
255
256
257 /*--.. Conversion functions ................................................--*/
258
259 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
260   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
261     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
262   }
263
264 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
265
266
267 /*--.. Operations on constants of any type .................................--*/
268
269 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
270   return wrap(Constant::getNullValue(unwrap(Ty)));
271 }
272
273 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
274   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
275 }
276
277 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
278   return wrap(UndefValue::get(unwrap(Ty)));
279 }
280
281 int LLVMIsConstant(LLVMValueRef Ty) {
282   return isa<Constant>(unwrap(Ty));
283 }
284
285 int LLVMIsNull(LLVMValueRef Val) {
286   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
287     return C->isNullValue();
288   return false;
289 }
290
291 int LLVMIsUndef(LLVMValueRef Val) {
292   return isa<UndefValue>(unwrap(Val));
293 }
294
295 /*--.. Operations on scalar constants ......................................--*/
296
297 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
298                           int SignExtend) {
299   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
300 }
301
302 static const fltSemantics &SemanticsForType(Type *Ty) {
303   assert(Ty->isFloatingPoint() && "Type is not floating point!");
304   if (Ty == Type::FloatTy)
305     return APFloat::IEEEsingle;
306   if (Ty == Type::DoubleTy)
307     return APFloat::IEEEdouble;
308   if (Ty == Type::X86_FP80Ty)
309     return APFloat::x87DoubleExtended;
310   if (Ty == Type::FP128Ty)
311     return APFloat::IEEEquad;
312   if (Ty == Type::PPC_FP128Ty)
313     return APFloat::PPCDoubleDouble;
314   return APFloat::Bogus;
315 }
316
317 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
318   APFloat APN(N);
319   bool ignored;
320   APN.convert(SemanticsForType(unwrap(RealTy)), APFloat::rmNearestTiesToEven,
321               &ignored);
322   return wrap(ConstantFP::get(APN));
323 }
324
325 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
326   return wrap(ConstantFP::get(APFloat(SemanticsForType(unwrap(RealTy)), Text)));
327 }
328
329 /*--.. Operations on composite constants ...................................--*/
330
331 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
332                              int DontNullTerminate) {
333   /* Inverted the sense of AddNull because ', 0)' is a
334      better mnemonic for null termination than ', 1)'. */
335   return wrap(ConstantArray::get(std::string(Str, Length),
336                                  DontNullTerminate == 0));
337 }
338
339 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
340                             LLVMValueRef *ConstantVals, unsigned Length) {
341   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
342                                  unwrap<Constant>(ConstantVals, Length),
343                                  Length));
344 }
345
346 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
347                              int Packed) {
348   return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
349                                   Count, Packed != 0));
350 }
351
352 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
353   return wrap(ConstantVector::get(unwrap<Constant>(ScalarConstantVals, Size),
354                                   Size));
355 }
356
357 /*--.. Constant expressions ................................................--*/
358
359 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
360   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
361 }
362
363 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
364   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
365 }
366
367 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
368   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
369 }
370
371 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
372   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
373                                    unwrap<Constant>(RHSConstant)));
374 }
375
376 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
377   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
378                                    unwrap<Constant>(RHSConstant)));
379 }
380
381 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
382   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
383                                    unwrap<Constant>(RHSConstant)));
384 }
385
386 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
387   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
388                                     unwrap<Constant>(RHSConstant)));
389 }
390
391 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
392   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
393                                     unwrap<Constant>(RHSConstant)));
394 }
395
396 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
397   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
398                                     unwrap<Constant>(RHSConstant)));
399 }
400
401 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
402   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
403                                     unwrap<Constant>(RHSConstant)));
404 }
405
406 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
407   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
408                                     unwrap<Constant>(RHSConstant)));
409 }
410
411 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
412   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
413                                     unwrap<Constant>(RHSConstant)));
414 }
415
416 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
417   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
418                                    unwrap<Constant>(RHSConstant)));
419 }
420
421 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
422   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
423                                   unwrap<Constant>(RHSConstant)));
424 }
425
426 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
427   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
428                                    unwrap<Constant>(RHSConstant)));
429 }
430
431 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
432                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
433   return wrap(ConstantExpr::getICmp(Predicate,
434                                     unwrap<Constant>(LHSConstant),
435                                     unwrap<Constant>(RHSConstant)));
436 }
437
438 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
439                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
440   return wrap(ConstantExpr::getFCmp(Predicate,
441                                     unwrap<Constant>(LHSConstant),
442                                     unwrap<Constant>(RHSConstant)));
443 }
444
445 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
446   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
447                                   unwrap<Constant>(RHSConstant)));
448 }
449
450 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
451   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
452                                     unwrap<Constant>(RHSConstant)));
453 }
454
455 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
456   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
457                                     unwrap<Constant>(RHSConstant)));
458 }
459
460 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
461                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
462   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
463                                              unwrap<Constant>(ConstantIndices, 
464                                                               NumIndices),
465                                              NumIndices));
466 }
467
468 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
469   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
470                                      unwrap(ToType)));
471 }
472
473 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
474   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
475                                     unwrap(ToType)));
476 }
477
478 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
479   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
480                                     unwrap(ToType)));
481 }
482
483 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
484   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
485                                        unwrap(ToType)));
486 }
487
488 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
489   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
490                                         unwrap(ToType)));
491 }
492
493 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
494   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
495                                       unwrap(ToType)));
496 }
497
498 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
499   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
500                                       unwrap(ToType)));
501 }
502
503 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
504   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
505                                       unwrap(ToType)));
506 }
507
508 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
509   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
510                                       unwrap(ToType)));
511 }
512
513 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
514   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
515                                         unwrap(ToType)));
516 }
517
518 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
519   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
520                                         unwrap(ToType)));
521 }
522
523 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
524   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
525                                        unwrap(ToType)));
526 }
527
528 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
529                              LLVMValueRef ConstantIfTrue,
530                              LLVMValueRef ConstantIfFalse) {
531   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
532                                       unwrap<Constant>(ConstantIfTrue),
533                                       unwrap<Constant>(ConstantIfFalse)));
534 }
535
536 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
537                                      LLVMValueRef IndexConstant) {
538   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
539                                               unwrap<Constant>(IndexConstant)));
540 }
541
542 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
543                                     LLVMValueRef ElementValueConstant,
544                                     LLVMValueRef IndexConstant) {
545   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
546                                          unwrap<Constant>(ElementValueConstant),
547                                              unwrap<Constant>(IndexConstant)));
548 }
549
550 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
551                                     LLVMValueRef VectorBConstant,
552                                     LLVMValueRef MaskConstant) {
553   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
554                                              unwrap<Constant>(VectorBConstant),
555                                              unwrap<Constant>(MaskConstant)));
556 }
557
558 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
559                                    unsigned NumIdx) {
560   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
561                                             IdxList, NumIdx));
562 }
563
564 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
565                                   LLVMValueRef ElementValueConstant,
566                                   unsigned *IdxList, unsigned NumIdx) {
567   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
568                                          unwrap<Constant>(ElementValueConstant),
569                                            IdxList, NumIdx));
570 }
571
572 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 
573                                 const char *Constraints, int HasSideEffects) {
574   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 
575                              Constraints, HasSideEffects));
576 }
577
578 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
579
580 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
581   return wrap(unwrap<GlobalValue>(Global)->getParent());
582 }
583
584 int LLVMIsDeclaration(LLVMValueRef Global) {
585   return unwrap<GlobalValue>(Global)->isDeclaration();
586 }
587
588 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
589   return static_cast<LLVMLinkage>(unwrap<GlobalValue>(Global)->getLinkage());
590 }
591
592 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
593   unwrap<GlobalValue>(Global)
594     ->setLinkage(static_cast<GlobalValue::LinkageTypes>(Linkage));
595 }
596
597 const char *LLVMGetSection(LLVMValueRef Global) {
598   return unwrap<GlobalValue>(Global)->getSection().c_str();
599 }
600
601 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
602   unwrap<GlobalValue>(Global)->setSection(Section);
603 }
604
605 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
606   return static_cast<LLVMVisibility>(
607     unwrap<GlobalValue>(Global)->getVisibility());
608 }
609
610 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
611   unwrap<GlobalValue>(Global)
612     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
613 }
614
615 unsigned LLVMGetAlignment(LLVMValueRef Global) {
616   return unwrap<GlobalValue>(Global)->getAlignment();
617 }
618
619 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
620   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
621 }
622
623 /*--.. Operations on global variables ......................................--*/
624
625 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
626   return wrap(new GlobalVariable(unwrap(Ty), false,
627                                  GlobalValue::ExternalLinkage, 0, Name,
628                                  unwrap(M)));
629 }
630
631 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
632   return wrap(unwrap(M)->getNamedGlobal(Name));
633 }
634
635 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
636   Module *Mod = unwrap(M);
637   Module::global_iterator I = Mod->global_begin();
638   if (I == Mod->global_end())
639     return 0;
640   return wrap(I);
641 }
642
643 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
644   Module *Mod = unwrap(M);
645   Module::global_iterator I = Mod->global_end();
646   if (I == Mod->global_begin())
647     return 0;
648   return wrap(--I);
649 }
650
651 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
652   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
653   Module::global_iterator I = GV;
654   if (++I == GV->getParent()->global_end())
655     return 0;
656   return wrap(I);
657 }
658
659 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
660   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
661   Module::global_iterator I = GV;
662   if (I == GV->getParent()->global_begin())
663     return 0;
664   return wrap(--I);
665 }
666
667 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
668   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
669 }
670
671 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
672   return wrap(unwrap<GlobalVariable>(GlobalVar)->getInitializer());
673 }
674
675 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
676   unwrap<GlobalVariable>(GlobalVar)
677     ->setInitializer(unwrap<Constant>(ConstantVal));
678 }
679
680 int LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
681   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
682 }
683
684 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, int IsThreadLocal) {
685   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
686 }
687
688 int LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
689   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
690 }
691
692 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, int IsConstant) {
693   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
694 }
695
696 /*--.. Operations on aliases ......................................--*/
697
698 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
699                           const char *Name) {
700   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
701                               unwrap<Constant>(Aliasee), unwrap (M)));
702 }
703
704 /*--.. Operations on functions .............................................--*/
705
706 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
707                              LLVMTypeRef FunctionTy) {
708   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
709                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
710 }
711
712 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
713   return wrap(unwrap(M)->getFunction(Name));
714 }
715
716 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
717   Module *Mod = unwrap(M);
718   Module::iterator I = Mod->begin();
719   if (I == Mod->end())
720     return 0;
721   return wrap(I);
722 }
723
724 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
725   Module *Mod = unwrap(M);
726   Module::iterator I = Mod->end();
727   if (I == Mod->begin())
728     return 0;
729   return wrap(--I);
730 }
731
732 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
733   Function *Func = unwrap<Function>(Fn);
734   Module::iterator I = Func;
735   if (++I == Func->getParent()->end())
736     return 0;
737   return wrap(I);
738 }
739
740 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
741   Function *Func = unwrap<Function>(Fn);
742   Module::iterator I = Func;
743   if (I == Func->getParent()->begin())
744     return 0;
745   return wrap(--I);
746 }
747
748 void LLVMDeleteFunction(LLVMValueRef Fn) {
749   unwrap<Function>(Fn)->eraseFromParent();
750 }
751
752 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
753   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
754     return F->getIntrinsicID();
755   return 0;
756 }
757
758 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
759   return unwrap<Function>(Fn)->getCallingConv();
760 }
761
762 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
763   return unwrap<Function>(Fn)->setCallingConv(CC);
764 }
765
766 const char *LLVMGetGC(LLVMValueRef Fn) {
767   Function *F = unwrap<Function>(Fn);
768   return F->hasGC()? F->getGC() : 0;
769 }
770
771 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
772   Function *F = unwrap<Function>(Fn);
773   if (GC)
774     F->setGC(GC);
775   else
776     F->clearGC();
777 }
778
779 /*--.. Operations on parameters ............................................--*/
780
781 unsigned LLVMCountParams(LLVMValueRef FnRef) {
782   // This function is strictly redundant to
783   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
784   return unwrap<Function>(FnRef)->arg_size();
785 }
786
787 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
788   Function *Fn = unwrap<Function>(FnRef);
789   for (Function::arg_iterator I = Fn->arg_begin(),
790                               E = Fn->arg_end(); I != E; I++)
791     *ParamRefs++ = wrap(I);
792 }
793
794 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
795   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
796   while (index --> 0)
797     AI++;
798   return wrap(AI);
799 }
800
801 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
802   return wrap(unwrap<Argument>(V)->getParent());
803 }
804
805 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
806   Function *Func = unwrap<Function>(Fn);
807   Function::arg_iterator I = Func->arg_begin();
808   if (I == Func->arg_end())
809     return 0;
810   return wrap(I);
811 }
812
813 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
814   Function *Func = unwrap<Function>(Fn);
815   Function::arg_iterator I = Func->arg_end();
816   if (I == Func->arg_begin())
817     return 0;
818   return wrap(--I);
819 }
820
821 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
822   Argument *A = unwrap<Argument>(Arg);
823   Function::arg_iterator I = A;
824   if (++I == A->getParent()->arg_end())
825     return 0;
826   return wrap(I);
827 }
828
829 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
830   Argument *A = unwrap<Argument>(Arg);
831   Function::arg_iterator I = A;
832   if (I == A->getParent()->arg_begin())
833     return 0;
834   return wrap(--I);
835 }
836
837 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
838   unwrap<Argument>(Arg)->addAttr(PA);
839 }
840
841 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
842   unwrap<Argument>(Arg)->removeAttr(PA);
843 }
844
845 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
846   unwrap<Argument>(Arg)->addAttr(
847           Attribute::constructAlignmentFromInt(align));
848 }
849
850 /*--.. Operations on basic blocks ..........................................--*/
851
852 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
853   return wrap(static_cast<Value*>(unwrap(BB)));
854 }
855
856 int LLVMValueIsBasicBlock(LLVMValueRef Val) {
857   return isa<BasicBlock>(unwrap(Val));
858 }
859
860 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
861   return wrap(unwrap<BasicBlock>(Val));
862 }
863
864 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
865   return wrap(unwrap(BB)->getParent());
866 }
867
868 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
869   return unwrap<Function>(FnRef)->size();
870 }
871
872 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
873   Function *Fn = unwrap<Function>(FnRef);
874   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
875     *BasicBlocksRefs++ = wrap(I);
876 }
877
878 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
879   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
880 }
881
882 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
883   Function *Func = unwrap<Function>(Fn);
884   Function::iterator I = Func->begin();
885   if (I == Func->end())
886     return 0;
887   return wrap(I);
888 }
889
890 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
891   Function *Func = unwrap<Function>(Fn);
892   Function::iterator I = Func->end();
893   if (I == Func->begin())
894     return 0;
895   return wrap(--I);
896 }
897
898 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
899   BasicBlock *Block = unwrap(BB);
900   Function::iterator I = Block;
901   if (++I == Block->getParent()->end())
902     return 0;
903   return wrap(I);
904 }
905
906 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
907   BasicBlock *Block = unwrap(BB);
908   Function::iterator I = Block;
909   if (I == Block->getParent()->begin())
910     return 0;
911   return wrap(--I);
912 }
913
914 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
915   return wrap(BasicBlock::Create(Name, unwrap<Function>(FnRef)));
916 }
917
918 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBBRef,
919                                        const char *Name) {
920   BasicBlock *InsertBeforeBB = unwrap(InsertBeforeBBRef);
921   return wrap(BasicBlock::Create(Name, InsertBeforeBB->getParent(),
922                                  InsertBeforeBB));
923 }
924
925 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
926   unwrap(BBRef)->eraseFromParent();
927 }
928
929 /*--.. Operations on instructions ..........................................--*/
930
931 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
932   return wrap(unwrap<Instruction>(Inst)->getParent());
933 }
934
935 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
936   BasicBlock *Block = unwrap(BB);
937   BasicBlock::iterator I = Block->begin();
938   if (I == Block->end())
939     return 0;
940   return wrap(I);
941 }
942
943 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
944   BasicBlock *Block = unwrap(BB);
945   BasicBlock::iterator I = Block->end();
946   if (I == Block->begin())
947     return 0;
948   return wrap(--I);
949 }
950
951 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
952   Instruction *Instr = unwrap<Instruction>(Inst);
953   BasicBlock::iterator I = Instr;
954   if (++I == Instr->getParent()->end())
955     return 0;
956   return wrap(I);
957 }
958
959 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
960   Instruction *Instr = unwrap<Instruction>(Inst);
961   BasicBlock::iterator I = Instr;
962   if (I == Instr->getParent()->begin())
963     return 0;
964   return wrap(--I);
965 }
966
967 /*--.. Call and invoke instructions ........................................--*/
968
969 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
970   Value *V = unwrap(Instr);
971   if (CallInst *CI = dyn_cast<CallInst>(V))
972     return CI->getCallingConv();
973   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
974     return II->getCallingConv();
975   assert(0 && "LLVMGetInstructionCallConv applies only to call and invoke!");
976   return 0;
977 }
978
979 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
980   Value *V = unwrap(Instr);
981   if (CallInst *CI = dyn_cast<CallInst>(V))
982     return CI->setCallingConv(CC);
983   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
984     return II->setCallingConv(CC);
985   assert(0 && "LLVMSetInstructionCallConv applies only to call and invoke!");
986 }
987
988 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
989                            LLVMAttribute PA) {
990   CallSite Call = CallSite(unwrap<Instruction>(Instr));
991   Call.setAttributes(
992     Call.getAttributes().addAttr(index, PA));
993 }
994
995 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
996                               LLVMAttribute PA) {
997   CallSite Call = CallSite(unwrap<Instruction>(Instr));
998   Call.setAttributes(
999     Call.getAttributes().removeAttr(index, PA));
1000 }
1001
1002 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1003                                 unsigned align) {
1004   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1005   Call.setAttributes(
1006     Call.getAttributes().addAttr(index, 
1007         Attribute::constructAlignmentFromInt(align)));
1008 }
1009
1010 /*--.. Operations on call instructions (only) ..............................--*/
1011
1012 int LLVMIsTailCall(LLVMValueRef Call) {
1013   return unwrap<CallInst>(Call)->isTailCall();
1014 }
1015
1016 void LLVMSetTailCall(LLVMValueRef Call, int isTailCall) {
1017   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1018 }
1019
1020 /*--.. Operations on phi nodes .............................................--*/
1021
1022 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1023                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1024   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1025   for (unsigned I = 0; I != Count; ++I)
1026     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1027 }
1028
1029 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1030   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1031 }
1032
1033 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1034   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1035 }
1036
1037 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1038   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1039 }
1040
1041
1042 /*===-- Instruction builders ----------------------------------------------===*/
1043
1044 LLVMBuilderRef LLVMCreateBuilder(void) {
1045   return wrap(new IRBuilder<>());
1046 }
1047
1048 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1049                          LLVMValueRef Instr) {
1050   BasicBlock *BB = unwrap(Block);
1051   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1052   unwrap(Builder)->SetInsertPoint(BB, I);
1053 }
1054
1055 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1056   Instruction *I = unwrap<Instruction>(Instr);
1057   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1058 }
1059
1060 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1061   BasicBlock *BB = unwrap(Block);
1062   unwrap(Builder)->SetInsertPoint(BB);
1063 }
1064
1065 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1066    return wrap(unwrap(Builder)->GetInsertBlock());
1067 }
1068
1069 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1070   unwrap(Builder)->ClearInsertionPoint ();
1071 }
1072
1073 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1074   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1075 }
1076
1077 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1078   delete unwrap(Builder);
1079 }
1080
1081 /*--.. Instruction builders ................................................--*/
1082
1083 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1084   return wrap(unwrap(B)->CreateRetVoid());
1085 }
1086
1087 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1088   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1089 }
1090
1091 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1092   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1093 }
1094
1095 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1096                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1097   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1098 }
1099
1100 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1101                              LLVMBasicBlockRef Else, unsigned NumCases) {
1102   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1103 }
1104
1105 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1106                              LLVMValueRef *Args, unsigned NumArgs,
1107                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1108                              const char *Name) {
1109   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1110                                       unwrap(Args), unwrap(Args) + NumArgs,
1111                                       Name));
1112 }
1113
1114 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1115   return wrap(unwrap(B)->CreateUnwind());
1116 }
1117
1118 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1119   return wrap(unwrap(B)->CreateUnreachable());
1120 }
1121
1122 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1123                  LLVMBasicBlockRef Dest) {
1124   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1125 }
1126
1127 /*--.. Arithmetic ..........................................................--*/
1128
1129 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1130                           const char *Name) {
1131   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1132 }
1133
1134 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1135                           const char *Name) {
1136   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1137 }
1138
1139 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1140                           const char *Name) {
1141   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1142 }
1143
1144 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1145                            const char *Name) {
1146   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1147 }
1148
1149 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1150                            const char *Name) {
1151   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1152 }
1153
1154 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1155                            const char *Name) {
1156   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1157 }
1158
1159 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1160                            const char *Name) {
1161   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1162 }
1163
1164 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1165                            const char *Name) {
1166   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1167 }
1168
1169 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1170                            const char *Name) {
1171   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1172 }
1173
1174 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1175                           const char *Name) {
1176   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1177 }
1178
1179 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1180                            const char *Name) {
1181   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1182 }
1183
1184 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1185                            const char *Name) {
1186   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1187 }
1188
1189 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1190                           const char *Name) {
1191   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1192 }
1193
1194 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1195                          const char *Name) {
1196   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1197 }
1198
1199 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1200                           const char *Name) {
1201   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1202 }
1203
1204 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1205   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1206 }
1207
1208 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1209   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1210 }
1211
1212 /*--.. Memory ..............................................................--*/
1213
1214 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1215                              const char *Name) {
1216   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), 0, Name));
1217 }
1218
1219 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1220                                   LLVMValueRef Val, const char *Name) {
1221   return wrap(unwrap(B)->CreateMalloc(unwrap(Ty), unwrap(Val), Name));
1222 }
1223
1224 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1225                              const char *Name) {
1226   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1227 }
1228
1229 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1230                                   LLVMValueRef Val, const char *Name) {
1231   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1232 }
1233
1234 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1235   return wrap(unwrap(B)->CreateFree(unwrap(PointerVal)));
1236 }
1237
1238
1239 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1240                            const char *Name) {
1241   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1242 }
1243
1244 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1245                             LLVMValueRef PointerVal) {
1246   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1247 }
1248
1249 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1250                           LLVMValueRef *Indices, unsigned NumIndices,
1251                           const char *Name) {
1252   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1253                                    unwrap(Indices) + NumIndices, Name));
1254 }
1255
1256 /*--.. Casts ...............................................................--*/
1257
1258 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1259                             LLVMTypeRef DestTy, const char *Name) {
1260   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1261 }
1262
1263 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1264                            LLVMTypeRef DestTy, const char *Name) {
1265   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1266 }
1267
1268 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1269                            LLVMTypeRef DestTy, const char *Name) {
1270   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1271 }
1272
1273 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1274                              LLVMTypeRef DestTy, const char *Name) {
1275   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1276 }
1277
1278 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1279                              LLVMTypeRef DestTy, const char *Name) {
1280   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1281 }
1282
1283 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1284                              LLVMTypeRef DestTy, const char *Name) {
1285   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1286 }
1287
1288 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1289                              LLVMTypeRef DestTy, const char *Name) {
1290   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1291 }
1292
1293 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1294                               LLVMTypeRef DestTy, const char *Name) {
1295   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1296 }
1297
1298 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1299                             LLVMTypeRef DestTy, const char *Name) {
1300   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1301 }
1302
1303 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1304                                LLVMTypeRef DestTy, const char *Name) {
1305   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1306 }
1307
1308 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1309                                LLVMTypeRef DestTy, const char *Name) {
1310   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1311 }
1312
1313 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1314                               LLVMTypeRef DestTy, const char *Name) {
1315   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1316 }
1317
1318 /*--.. Comparisons .........................................................--*/
1319
1320 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
1321                            LLVMValueRef LHS, LLVMValueRef RHS,
1322                            const char *Name) {
1323   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
1324                                     unwrap(LHS), unwrap(RHS), Name));
1325 }
1326
1327 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
1328                            LLVMValueRef LHS, LLVMValueRef RHS,
1329                            const char *Name) {
1330   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
1331                                     unwrap(LHS), unwrap(RHS), Name));
1332 }
1333
1334 /*--.. Miscellaneous instructions ..........................................--*/
1335
1336 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
1337   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
1338 }
1339
1340 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
1341                            LLVMValueRef *Args, unsigned NumArgs,
1342                            const char *Name) {
1343   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
1344                                     unwrap(Args) + NumArgs, Name));
1345 }
1346
1347 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
1348                              LLVMValueRef Then, LLVMValueRef Else,
1349                              const char *Name) {
1350   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
1351                                       Name));
1352 }
1353
1354 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
1355                             LLVMTypeRef Ty, const char *Name) {
1356   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
1357 }
1358
1359 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1360                                       LLVMValueRef Index, const char *Name) {
1361   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
1362                                               Name));
1363 }
1364
1365 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
1366                                     LLVMValueRef EltVal, LLVMValueRef Index,
1367                                     const char *Name) {
1368   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
1369                                              unwrap(Index), Name));
1370 }
1371
1372 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
1373                                     LLVMValueRef V2, LLVMValueRef Mask,
1374                                     const char *Name) {
1375   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
1376                                              unwrap(Mask), Name));
1377 }
1378
1379 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1380                                    unsigned Index, const char *Name) {
1381   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
1382 }
1383
1384 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
1385                                   LLVMValueRef EltVal, unsigned Index,
1386                                   const char *Name) {
1387   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
1388                                            Index, Name));
1389 }
1390
1391
1392 /*===-- Module providers --------------------------------------------------===*/
1393
1394 LLVMModuleProviderRef
1395 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
1396   return wrap(new ExistingModuleProvider(unwrap(M)));
1397 }
1398
1399 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
1400   delete unwrap(MP);
1401 }
1402
1403
1404 /*===-- Memory buffers ----------------------------------------------------===*/
1405
1406 int LLVMCreateMemoryBufferWithContentsOfFile(const char *Path,
1407                                              LLVMMemoryBufferRef *OutMemBuf,
1408                                              char **OutMessage) {
1409   std::string Error;
1410   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
1411     *OutMemBuf = wrap(MB);
1412     return 0;
1413   }
1414   
1415   *OutMessage = strdup(Error.c_str());
1416   return 1;
1417 }
1418
1419 int LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
1420                                     char **OutMessage) {
1421   if (MemoryBuffer *MB = MemoryBuffer::getSTDIN()) {
1422     *OutMemBuf = wrap(MB);
1423     return 0;
1424   }
1425   
1426   *OutMessage = strdup("stdin is empty.");
1427   return 1;
1428 }
1429
1430 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
1431   delete unwrap(MemBuf);
1432 }