Add the new builder arthmetic instructions to llvm-c and ocaml.
[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/LLVMContext.h"
22 #include "llvm/TypeSymbolTable.h"
23 #include "llvm/InlineAsm.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/Support/CallSite.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <cassert>
31 #include <cstdlib>
32 #include <cstring>
33
34 using namespace llvm;
35
36
37 /*===-- Error handling ----------------------------------------------------===*/
38
39 void LLVMDisposeMessage(char *Message) {
40   free(Message);
41 }
42
43
44 /*===-- Operations on contexts --------------------------------------------===*/
45
46 LLVMContextRef LLVMContextCreate() {
47   return wrap(new LLVMContext());
48 }
49
50 LLVMContextRef LLVMGetGlobalContext() {
51   return wrap(&getGlobalContext());
52 }
53
54 void LLVMContextDispose(LLVMContextRef C) {
55   delete unwrap(C);
56 }
57
58
59 /*===-- Operations on modules ---------------------------------------------===*/
60
61 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
62   return wrap(new Module(ModuleID, getGlobalContext()));
63 }
64
65 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 
66                                                 LLVMContextRef C) {
67   return wrap(new Module(ModuleID, *unwrap(C)));
68 }
69
70 void LLVMDisposeModule(LLVMModuleRef M) {
71   delete unwrap(M);
72 }
73
74 /*--.. Data layout .........................................................--*/
75 const char * LLVMGetDataLayout(LLVMModuleRef M) {
76   return unwrap(M)->getDataLayout().c_str();
77 }
78
79 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
80   unwrap(M)->setDataLayout(Triple);
81 }
82
83 /*--.. Target triple .......................................................--*/
84 const char * LLVMGetTarget(LLVMModuleRef M) {
85   return unwrap(M)->getTargetTriple().c_str();
86 }
87
88 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
89   unwrap(M)->setTargetTriple(Triple);
90 }
91
92 /*--.. Type names ..........................................................--*/
93 LLVMBool LLVMAddTypeName(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
94   return unwrap(M)->addTypeName(Name, unwrap(Ty));
95 }
96
97 void LLVMDeleteTypeName(LLVMModuleRef M, const char *Name) {
98   TypeSymbolTable &TST = unwrap(M)->getTypeSymbolTable();
99
100   TypeSymbolTable::iterator I = TST.find(Name);
101   if (I != TST.end())
102     TST.remove(I);
103 }
104
105 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
106   return wrap(unwrap(M)->getTypeByName(Name));
107 }
108
109 void LLVMDumpModule(LLVMModuleRef M) {
110   unwrap(M)->dump();
111 }
112
113
114 /*===-- Operations on types -----------------------------------------------===*/
115
116 /*--.. Operations on all types (mostly) ....................................--*/
117
118 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
119   switch (unwrap(Ty)->getTypeID()) {
120   default:
121     assert(false && "Unhandled TypeID.");
122   case Type::VoidTyID:
123     return LLVMVoidTypeKind;
124   case Type::FloatTyID:
125     return LLVMFloatTypeKind;
126   case Type::DoubleTyID:
127     return LLVMDoubleTypeKind;
128   case Type::X86_FP80TyID:
129     return LLVMX86_FP80TypeKind;
130   case Type::FP128TyID:
131     return LLVMFP128TypeKind;
132   case Type::PPC_FP128TyID:
133     return LLVMPPC_FP128TypeKind;
134   case Type::LabelTyID:
135     return LLVMLabelTypeKind;
136   case Type::MetadataTyID:
137     return LLVMMetadataTypeKind;
138   case Type::IntegerTyID:
139     return LLVMIntegerTypeKind;
140   case Type::FunctionTyID:
141     return LLVMFunctionTypeKind;
142   case Type::StructTyID:
143     return LLVMStructTypeKind;
144   case Type::UnionTyID:
145     return LLVMUnionTypeKind;
146   case Type::ArrayTyID:
147     return LLVMArrayTypeKind;
148   case Type::PointerTyID:
149     return LLVMPointerTypeKind;
150   case Type::OpaqueTyID:
151     return LLVMOpaqueTypeKind;
152   case Type::VectorTyID:
153     return LLVMVectorTypeKind;
154   }
155 }
156
157 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
158   return wrap(&unwrap(Ty)->getContext());
159 }
160
161 /*--.. Operations on integer types .........................................--*/
162
163 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
164   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
165 }
166 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
167   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
168 }
169 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
170   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
171 }
172 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
173   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
174 }
175 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
176   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
177 }
178 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
179   return wrap(IntegerType::get(*unwrap(C), NumBits));
180 }
181
182 LLVMTypeRef LLVMInt1Type(void)  {
183   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
184 }
185 LLVMTypeRef LLVMInt8Type(void)  {
186   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
187 }
188 LLVMTypeRef LLVMInt16Type(void) {
189   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
190 }
191 LLVMTypeRef LLVMInt32Type(void) {
192   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
193 }
194 LLVMTypeRef LLVMInt64Type(void) {
195   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
196 }
197 LLVMTypeRef LLVMIntType(unsigned NumBits) {
198   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
199 }
200
201 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
202   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
203 }
204
205 /*--.. Operations on real types ............................................--*/
206
207 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
208   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
209 }
210 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
211   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
212 }
213 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
214   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
215 }
216 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
217   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
218 }
219 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
220   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
221 }
222
223 LLVMTypeRef LLVMFloatType(void) {
224   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
225 }
226 LLVMTypeRef LLVMDoubleType(void) {
227   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
228 }
229 LLVMTypeRef LLVMX86FP80Type(void) {
230   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
231 }
232 LLVMTypeRef LLVMFP128Type(void) {
233   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
234 }
235 LLVMTypeRef LLVMPPCFP128Type(void) {
236   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
237 }
238
239 /*--.. Operations on function types ........................................--*/
240
241 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
242                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
243                              LLVMBool IsVarArg) {
244   std::vector<const Type*> Tys;
245   for (LLVMTypeRef *I = ParamTypes, *E = ParamTypes + ParamCount; I != E; ++I)
246     Tys.push_back(unwrap(*I));
247   
248   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
249 }
250
251 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
252   return unwrap<FunctionType>(FunctionTy)->isVarArg();
253 }
254
255 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
256   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
257 }
258
259 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
260   return unwrap<FunctionType>(FunctionTy)->getNumParams();
261 }
262
263 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
264   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
265   for (FunctionType::param_iterator I = Ty->param_begin(),
266                                     E = Ty->param_end(); I != E; ++I)
267     *Dest++ = wrap(*I);
268 }
269
270 /*--.. Operations on struct types ..........................................--*/
271
272 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
273                            unsigned ElementCount, LLVMBool Packed) {
274   std::vector<const Type*> Tys;
275   for (LLVMTypeRef *I = ElementTypes,
276                    *E = ElementTypes + ElementCount; I != E; ++I)
277     Tys.push_back(unwrap(*I));
278   
279   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
280 }
281
282 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
283                            unsigned ElementCount, LLVMBool Packed) {
284   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
285                                  ElementCount, Packed);
286 }
287
288
289 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
290   return unwrap<StructType>(StructTy)->getNumElements();
291 }
292
293 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
294   StructType *Ty = unwrap<StructType>(StructTy);
295   for (FunctionType::param_iterator I = Ty->element_begin(),
296                                     E = Ty->element_end(); I != E; ++I)
297     *Dest++ = wrap(*I);
298 }
299
300 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
301   return unwrap<StructType>(StructTy)->isPacked();
302 }
303
304 /*--.. Operations on union types ..........................................--*/
305
306 LLVMTypeRef LLVMUnionTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
307                                    unsigned ElementCount) {
308   SmallVector<const Type*, 8> Tys;
309   for (LLVMTypeRef *I = ElementTypes,
310                    *E = ElementTypes + ElementCount; I != E; ++I)
311     Tys.push_back(unwrap(*I));
312   
313   return wrap(UnionType::get(&Tys[0], Tys.size()));
314 }
315
316 LLVMTypeRef LLVMUnionType(LLVMTypeRef *ElementTypes,
317                            unsigned ElementCount, int Packed) {
318   return LLVMUnionTypeInContext(LLVMGetGlobalContext(), ElementTypes,
319                                 ElementCount);
320 }
321
322 unsigned LLVMCountUnionElementTypes(LLVMTypeRef UnionTy) {
323   return unwrap<UnionType>(UnionTy)->getNumElements();
324 }
325
326 void LLVMGetUnionElementTypes(LLVMTypeRef UnionTy, LLVMTypeRef *Dest) {
327   UnionType *Ty = unwrap<UnionType>(UnionTy);
328   for (FunctionType::param_iterator I = Ty->element_begin(),
329                                     E = Ty->element_end(); I != E; ++I)
330     *Dest++ = wrap(*I);
331 }
332
333 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
334
335 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
336   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
337 }
338
339 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
340   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
341 }
342
343 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
344   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
345 }
346
347 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
348   return wrap(unwrap<SequentialType>(Ty)->getElementType());
349 }
350
351 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
352   return unwrap<ArrayType>(ArrayTy)->getNumElements();
353 }
354
355 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
356   return unwrap<PointerType>(PointerTy)->getAddressSpace();
357 }
358
359 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
360   return unwrap<VectorType>(VectorTy)->getNumElements();
361 }
362
363 /*--.. Operations on other types ...........................................--*/
364
365 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
366   return wrap(Type::getVoidTy(*unwrap(C)));
367 }
368 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
369   return wrap(Type::getLabelTy(*unwrap(C)));
370 }
371 LLVMTypeRef LLVMOpaqueTypeInContext(LLVMContextRef C) {
372   return wrap(OpaqueType::get(*unwrap(C)));
373 }
374
375 LLVMTypeRef LLVMVoidType(void)  {
376   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
377 }
378 LLVMTypeRef LLVMLabelType(void) {
379   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
380 }
381 LLVMTypeRef LLVMOpaqueType(void) {
382   return LLVMOpaqueTypeInContext(LLVMGetGlobalContext());
383 }
384
385 /*--.. Operations on type handles ..........................................--*/
386
387 LLVMTypeHandleRef LLVMCreateTypeHandle(LLVMTypeRef PotentiallyAbstractTy) {
388   return wrap(new PATypeHolder(unwrap(PotentiallyAbstractTy)));
389 }
390
391 void LLVMDisposeTypeHandle(LLVMTypeHandleRef TypeHandle) {
392   delete unwrap(TypeHandle);
393 }
394
395 LLVMTypeRef LLVMResolveTypeHandle(LLVMTypeHandleRef TypeHandle) {
396   return wrap(unwrap(TypeHandle)->get());
397 }
398
399 void LLVMRefineType(LLVMTypeRef AbstractTy, LLVMTypeRef ConcreteTy) {
400   unwrap<DerivedType>(AbstractTy)->refineAbstractTypeTo(unwrap(ConcreteTy));
401 }
402
403
404 /*===-- Operations on values ----------------------------------------------===*/
405
406 /*--.. Operations on all values ............................................--*/
407
408 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
409   return wrap(unwrap(Val)->getType());
410 }
411
412 const char *LLVMGetValueName(LLVMValueRef Val) {
413   return unwrap(Val)->getName().data();
414 }
415
416 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
417   unwrap(Val)->setName(Name);
418 }
419
420 void LLVMDumpValue(LLVMValueRef Val) {
421   unwrap(Val)->dump();
422 }
423
424 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
425   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
426 }
427
428 /*--.. Conversion functions ................................................--*/
429
430 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
431   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
432     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
433   }
434
435 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
436
437 /*--.. Operations on Uses ..................................................--*/
438 LLVMUseIteratorRef LLVMGetFirstUse(LLVMValueRef Val) {
439   Value *V = unwrap(Val);
440   Value::use_iterator I = V->use_begin();
441   if (I == V->use_end())
442     return 0;
443   return wrap(&(I.getUse()));
444 }
445
446 LLVMUseIteratorRef LLVMGetNextUse(LLVMUseIteratorRef UR) {
447   return wrap(unwrap(UR)->getNext());
448 }
449
450 LLVMValueRef LLVMGetUser(LLVMUseIteratorRef UR) {
451   return wrap(unwrap(UR)->getUser());
452 }
453
454 LLVMValueRef LLVMGetUsedValue(LLVMUseIteratorRef UR) {
455   return wrap(unwrap(UR)->get());
456 }
457
458 /*--.. Operations on Users .................................................--*/
459 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
460   return wrap(unwrap<User>(Val)->getOperand(Index));
461 }
462
463 /*--.. Operations on constants of any type .................................--*/
464
465 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
466   return wrap(Constant::getNullValue(unwrap(Ty)));
467 }
468
469 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
470   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
471 }
472
473 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
474   return wrap(UndefValue::get(unwrap(Ty)));
475 }
476
477 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
478   return isa<Constant>(unwrap(Ty));
479 }
480
481 LLVMBool LLVMIsNull(LLVMValueRef Val) {
482   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
483     return C->isNullValue();
484   return false;
485 }
486
487 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
488   return isa<UndefValue>(unwrap(Val));
489 }
490
491 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
492   return
493       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
494 }
495
496 /*--.. Operations on scalar constants ......................................--*/
497
498 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
499                           LLVMBool SignExtend) {
500   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
501 }
502
503 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
504                                   uint8_t Radix) {
505   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
506                                Radix));
507 }
508
509 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
510                                          unsigned SLen, uint8_t Radix) {
511   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
512                                Radix));
513 }
514
515 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
516   return wrap(ConstantFP::get(unwrap(RealTy), N));
517 }
518
519 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
520   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
521 }
522
523 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
524                                           unsigned SLen) {
525   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
526 }
527
528 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
529   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
530 }
531
532 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
533   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
534 }
535
536 /*--.. Operations on composite constants ...................................--*/
537
538 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
539                                       unsigned Length,
540                                       LLVMBool DontNullTerminate) {
541   /* Inverted the sense of AddNull because ', 0)' is a
542      better mnemonic for null termination than ', 1)'. */
543   return wrap(ConstantArray::get(*unwrap(C), std::string(Str, Length),
544                                  DontNullTerminate == 0));
545 }
546 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 
547                                       LLVMValueRef *ConstantVals,
548                                       unsigned Count, LLVMBool Packed) {
549   return wrap(ConstantStruct::get(*unwrap(C),
550                                   unwrap<Constant>(ConstantVals, Count),
551                                   Count, Packed != 0));
552 }
553
554 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
555                              LLVMBool DontNullTerminate) {
556   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
557                                   DontNullTerminate);
558 }
559 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
560                             LLVMValueRef *ConstantVals, unsigned Length) {
561   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length),
562                                  unwrap<Constant>(ConstantVals, Length),
563                                  Length));
564 }
565 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
566                              LLVMBool Packed) {
567   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
568                                   Packed);
569 }
570 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
571   return wrap(ConstantVector::get(
572                             unwrap<Constant>(ScalarConstantVals, Size), Size));
573 }
574 LLVMValueRef LLVMConstUnion(LLVMTypeRef Ty, LLVMValueRef Val) {
575   return wrap(ConstantUnion::get(unwrap<UnionType>(Ty), unwrap<Constant>(Val)));
576 }
577
578 /*--.. Constant expressions ................................................--*/
579
580 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
581   return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
582 }
583
584 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
585   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
586 }
587
588 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
589   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
590 }
591
592 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
593   return wrap(ConstantExpr::getNeg(
594                                    unwrap<Constant>(ConstantVal)));
595 }
596
597 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
598   return wrap(ConstantExpr::getNSWNeg(
599                                       unwrap<Constant>(ConstantVal)));
600 }
601
602 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
603   return wrap(ConstantExpr::getNUWNeg(
604                                       unwrap<Constant>(ConstantVal)));
605 }
606
607
608 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
609   return wrap(ConstantExpr::getFNeg(
610                                     unwrap<Constant>(ConstantVal)));
611 }
612
613 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
614   return wrap(ConstantExpr::getNot(
615                                    unwrap<Constant>(ConstantVal)));
616 }
617
618 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
619   return wrap(ConstantExpr::getAdd(
620                                    unwrap<Constant>(LHSConstant),
621                                    unwrap<Constant>(RHSConstant)));
622 }
623
624 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
625                              LLVMValueRef RHSConstant) {
626   return wrap(ConstantExpr::getNSWAdd(
627                                       unwrap<Constant>(LHSConstant),
628                                       unwrap<Constant>(RHSConstant)));
629 }
630
631 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
632                              LLVMValueRef RHSConstant) {
633   return wrap(ConstantExpr::getNUWAdd(
634                                       unwrap<Constant>(LHSConstant),
635                                       unwrap<Constant>(RHSConstant)));
636 }
637
638 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
639   return wrap(ConstantExpr::getFAdd(
640                                     unwrap<Constant>(LHSConstant),
641                                     unwrap<Constant>(RHSConstant)));
642 }
643
644 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
645   return wrap(ConstantExpr::getSub(
646                                    unwrap<Constant>(LHSConstant),
647                                    unwrap<Constant>(RHSConstant)));
648 }
649
650 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
651                              LLVMValueRef RHSConstant) {
652   return wrap(ConstantExpr::getNSWSub(
653                                       unwrap<Constant>(LHSConstant),
654                                       unwrap<Constant>(RHSConstant)));
655 }
656
657 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
658                              LLVMValueRef RHSConstant) {
659   return wrap(ConstantExpr::getNUWSub(
660                                       unwrap<Constant>(LHSConstant),
661                                       unwrap<Constant>(RHSConstant)));
662 }
663
664 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
665   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
666                                     unwrap<Constant>(RHSConstant)));
667 }
668
669 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
670   return wrap(ConstantExpr::getMul(
671                                    unwrap<Constant>(LHSConstant),
672                                    unwrap<Constant>(RHSConstant)));
673 }
674
675 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
676                              LLVMValueRef RHSConstant) {
677   return wrap(ConstantExpr::getNSWMul(
678                                       unwrap<Constant>(LHSConstant),
679                                       unwrap<Constant>(RHSConstant)));
680 }
681
682 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
683                              LLVMValueRef RHSConstant) {
684   return wrap(ConstantExpr::getNUWMul(
685                                       unwrap<Constant>(LHSConstant),
686                                       unwrap<Constant>(RHSConstant)));
687 }
688
689 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
690   return wrap(ConstantExpr::getFMul(
691                                     unwrap<Constant>(LHSConstant),
692                                     unwrap<Constant>(RHSConstant)));
693 }
694
695 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
696   return wrap(ConstantExpr::getUDiv(
697                                     unwrap<Constant>(LHSConstant),
698                                     unwrap<Constant>(RHSConstant)));
699 }
700
701 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
702   return wrap(ConstantExpr::getSDiv(
703                                     unwrap<Constant>(LHSConstant),
704                                     unwrap<Constant>(RHSConstant)));
705 }
706
707 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
708                                 LLVMValueRef RHSConstant) {
709   return wrap(ConstantExpr::getExactSDiv(
710                                          unwrap<Constant>(LHSConstant),
711                                          unwrap<Constant>(RHSConstant)));
712 }
713
714 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
715   return wrap(ConstantExpr::getFDiv(
716                                     unwrap<Constant>(LHSConstant),
717                                     unwrap<Constant>(RHSConstant)));
718 }
719
720 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
721   return wrap(ConstantExpr::getURem(
722                                     unwrap<Constant>(LHSConstant),
723                                     unwrap<Constant>(RHSConstant)));
724 }
725
726 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
727   return wrap(ConstantExpr::getSRem(
728                                     unwrap<Constant>(LHSConstant),
729                                     unwrap<Constant>(RHSConstant)));
730 }
731
732 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
733   return wrap(ConstantExpr::getFRem(
734                                     unwrap<Constant>(LHSConstant),
735                                     unwrap<Constant>(RHSConstant)));
736 }
737
738 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
739   return wrap(ConstantExpr::getAnd(
740                                    unwrap<Constant>(LHSConstant),
741                                    unwrap<Constant>(RHSConstant)));
742 }
743
744 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
745   return wrap(ConstantExpr::getOr(
746                                   unwrap<Constant>(LHSConstant),
747                                   unwrap<Constant>(RHSConstant)));
748 }
749
750 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
751   return wrap(ConstantExpr::getXor(
752                                    unwrap<Constant>(LHSConstant),
753                                    unwrap<Constant>(RHSConstant)));
754 }
755
756 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
757                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
758   return wrap(ConstantExpr::getICmp(Predicate,
759                                     unwrap<Constant>(LHSConstant),
760                                     unwrap<Constant>(RHSConstant)));
761 }
762
763 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
764                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
765   return wrap(ConstantExpr::getFCmp(Predicate,
766                                     unwrap<Constant>(LHSConstant),
767                                     unwrap<Constant>(RHSConstant)));
768 }
769
770 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
771   return wrap(ConstantExpr::getShl(
772                                   unwrap<Constant>(LHSConstant),
773                                   unwrap<Constant>(RHSConstant)));
774 }
775
776 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
777   return wrap(ConstantExpr::getLShr(
778                                     unwrap<Constant>(LHSConstant),
779                                     unwrap<Constant>(RHSConstant)));
780 }
781
782 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
783   return wrap(ConstantExpr::getAShr(
784                                     unwrap<Constant>(LHSConstant),
785                                     unwrap<Constant>(RHSConstant)));
786 }
787
788 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
789                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
790   return wrap(ConstantExpr::getGetElementPtr(
791                                              unwrap<Constant>(ConstantVal),
792                                              unwrap<Constant>(ConstantIndices, 
793                                                               NumIndices),
794                                              NumIndices));
795 }
796
797 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
798                                   LLVMValueRef *ConstantIndices,
799                                   unsigned NumIndices) {
800   Constant* Val = unwrap<Constant>(ConstantVal);
801   Constant** Idxs = unwrap<Constant>(ConstantIndices, NumIndices);
802   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, Idxs, NumIndices));
803 }
804
805 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
806   return wrap(ConstantExpr::getTrunc(
807                                      unwrap<Constant>(ConstantVal),
808                                      unwrap(ToType)));
809 }
810
811 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
812   return wrap(ConstantExpr::getSExt(
813                                     unwrap<Constant>(ConstantVal),
814                                     unwrap(ToType)));
815 }
816
817 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
818   return wrap(ConstantExpr::getZExt(
819                                     unwrap<Constant>(ConstantVal),
820                                     unwrap(ToType)));
821 }
822
823 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
824   return wrap(ConstantExpr::getFPTrunc(
825                                        unwrap<Constant>(ConstantVal),
826                                        unwrap(ToType)));
827 }
828
829 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
830   return wrap(ConstantExpr::getFPExtend(
831                                         unwrap<Constant>(ConstantVal),
832                                         unwrap(ToType)));
833 }
834
835 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
836   return wrap(ConstantExpr::getUIToFP(
837                                       unwrap<Constant>(ConstantVal),
838                                       unwrap(ToType)));
839 }
840
841 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
842   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
843                                       unwrap(ToType)));
844 }
845
846 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
847   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
848                                       unwrap(ToType)));
849 }
850
851 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
852   return wrap(ConstantExpr::getFPToSI(
853                                       unwrap<Constant>(ConstantVal),
854                                       unwrap(ToType)));
855 }
856
857 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
858   return wrap(ConstantExpr::getPtrToInt(
859                                         unwrap<Constant>(ConstantVal),
860                                         unwrap(ToType)));
861 }
862
863 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
864   return wrap(ConstantExpr::getIntToPtr(
865                                         unwrap<Constant>(ConstantVal),
866                                         unwrap(ToType)));
867 }
868
869 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
870   return wrap(ConstantExpr::getBitCast(
871                                        unwrap<Constant>(ConstantVal),
872                                        unwrap(ToType)));
873 }
874
875 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
876                                     LLVMTypeRef ToType) {
877   return wrap(ConstantExpr::getZExtOrBitCast(
878                                              unwrap<Constant>(ConstantVal),
879                                              unwrap(ToType)));
880 }
881
882 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
883                                     LLVMTypeRef ToType) {
884   return wrap(ConstantExpr::getSExtOrBitCast(
885                                              unwrap<Constant>(ConstantVal),
886                                              unwrap(ToType)));
887 }
888
889 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
890                                      LLVMTypeRef ToType) {
891   return wrap(ConstantExpr::getTruncOrBitCast(
892                                               unwrap<Constant>(ConstantVal),
893                                               unwrap(ToType)));
894 }
895
896 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
897                                   LLVMTypeRef ToType) {
898   return wrap(ConstantExpr::getPointerCast(
899                                            unwrap<Constant>(ConstantVal),
900                                            unwrap(ToType)));
901 }
902
903 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
904                               LLVMBool isSigned) {
905   return wrap(ConstantExpr::getIntegerCast(
906                                            unwrap<Constant>(ConstantVal),
907                                            unwrap(ToType),
908                                            isSigned));
909 }
910
911 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
912   return wrap(ConstantExpr::getFPCast(
913                                       unwrap<Constant>(ConstantVal),
914                                       unwrap(ToType)));
915 }
916
917 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
918                              LLVMValueRef ConstantIfTrue,
919                              LLVMValueRef ConstantIfFalse) {
920   return wrap(ConstantExpr::getSelect(
921                                       unwrap<Constant>(ConstantCondition),
922                                       unwrap<Constant>(ConstantIfTrue),
923                                       unwrap<Constant>(ConstantIfFalse)));
924 }
925
926 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
927                                      LLVMValueRef IndexConstant) {
928   return wrap(ConstantExpr::getExtractElement(
929                                               unwrap<Constant>(VectorConstant),
930                                               unwrap<Constant>(IndexConstant)));
931 }
932
933 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
934                                     LLVMValueRef ElementValueConstant,
935                                     LLVMValueRef IndexConstant) {
936   return wrap(ConstantExpr::getInsertElement(
937                                          unwrap<Constant>(VectorConstant),
938                                          unwrap<Constant>(ElementValueConstant),
939                                              unwrap<Constant>(IndexConstant)));
940 }
941
942 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
943                                     LLVMValueRef VectorBConstant,
944                                     LLVMValueRef MaskConstant) {
945   return wrap(ConstantExpr::getShuffleVector(
946                                              unwrap<Constant>(VectorAConstant),
947                                              unwrap<Constant>(VectorBConstant),
948                                              unwrap<Constant>(MaskConstant)));
949 }
950
951 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
952                                    unsigned NumIdx) {
953   return wrap(ConstantExpr::getExtractValue(
954                                             unwrap<Constant>(AggConstant),
955                                             IdxList, NumIdx));
956 }
957
958 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
959                                   LLVMValueRef ElementValueConstant,
960                                   unsigned *IdxList, unsigned NumIdx) {
961   return wrap(ConstantExpr::getInsertValue(
962                                          unwrap<Constant>(AggConstant),
963                                          unwrap<Constant>(ElementValueConstant),
964                                            IdxList, NumIdx));
965 }
966
967 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
968                                 const char *Constraints,
969                                 LLVMBool HasSideEffects,
970                                 LLVMBool IsAlignStack) {
971   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
972                              Constraints, HasSideEffects, IsAlignStack));
973 }
974
975 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
976
977 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
978   return wrap(unwrap<GlobalValue>(Global)->getParent());
979 }
980
981 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
982   return unwrap<GlobalValue>(Global)->isDeclaration();
983 }
984
985 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
986   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
987   default:
988     assert(false && "Unhandled Linkage Type.");
989   case GlobalValue::ExternalLinkage:
990     return LLVMExternalLinkage;
991   case GlobalValue::AvailableExternallyLinkage:
992     return LLVMAvailableExternallyLinkage;
993   case GlobalValue::LinkOnceAnyLinkage:
994     return LLVMLinkOnceAnyLinkage;
995   case GlobalValue::LinkOnceODRLinkage:
996     return LLVMLinkOnceODRLinkage;
997   case GlobalValue::WeakAnyLinkage:
998     return LLVMWeakAnyLinkage;
999   case GlobalValue::WeakODRLinkage:
1000     return LLVMWeakODRLinkage;
1001   case GlobalValue::AppendingLinkage:
1002     return LLVMAppendingLinkage;
1003   case GlobalValue::InternalLinkage:
1004     return LLVMInternalLinkage;
1005   case GlobalValue::PrivateLinkage:
1006     return LLVMPrivateLinkage;
1007   case GlobalValue::LinkerPrivateLinkage:
1008     return LLVMLinkerPrivateLinkage;
1009   case GlobalValue::DLLImportLinkage:
1010     return LLVMDLLImportLinkage;
1011   case GlobalValue::DLLExportLinkage:
1012     return LLVMDLLExportLinkage;
1013   case GlobalValue::ExternalWeakLinkage:
1014     return LLVMExternalWeakLinkage;
1015   case GlobalValue::CommonLinkage:
1016     return LLVMCommonLinkage;
1017   }
1018
1019   // Should never get here.
1020   return static_cast<LLVMLinkage>(0);
1021 }
1022
1023 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1024   GlobalValue *GV = unwrap<GlobalValue>(Global);
1025
1026   switch (Linkage) {
1027   default:
1028     assert(false && "Unhandled Linkage Type.");
1029   case LLVMExternalLinkage:
1030     GV->setLinkage(GlobalValue::ExternalLinkage);
1031     break;
1032   case LLVMAvailableExternallyLinkage:
1033     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1034     break;
1035   case LLVMLinkOnceAnyLinkage:
1036     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1037     break;
1038   case LLVMLinkOnceODRLinkage:
1039     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1040     break;
1041   case LLVMWeakAnyLinkage:
1042     GV->setLinkage(GlobalValue::WeakAnyLinkage);
1043     break;
1044   case LLVMWeakODRLinkage:
1045     GV->setLinkage(GlobalValue::WeakODRLinkage);
1046     break;
1047   case LLVMAppendingLinkage:
1048     GV->setLinkage(GlobalValue::AppendingLinkage);
1049     break;
1050   case LLVMInternalLinkage:
1051     GV->setLinkage(GlobalValue::InternalLinkage);
1052     break;
1053   case LLVMPrivateLinkage:
1054     GV->setLinkage(GlobalValue::PrivateLinkage);
1055     break;
1056   case LLVMLinkerPrivateLinkage:
1057     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1058     break;
1059   case LLVMDLLImportLinkage:
1060     GV->setLinkage(GlobalValue::DLLImportLinkage);
1061     break;
1062   case LLVMDLLExportLinkage:
1063     GV->setLinkage(GlobalValue::DLLExportLinkage);
1064     break;
1065   case LLVMExternalWeakLinkage:
1066     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1067     break;
1068   case LLVMGhostLinkage:
1069     DEBUG(errs()
1070           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1071     break;
1072   case LLVMCommonLinkage:
1073     GV->setLinkage(GlobalValue::CommonLinkage);
1074     break;
1075   }
1076 }
1077
1078 const char *LLVMGetSection(LLVMValueRef Global) {
1079   return unwrap<GlobalValue>(Global)->getSection().c_str();
1080 }
1081
1082 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1083   unwrap<GlobalValue>(Global)->setSection(Section);
1084 }
1085
1086 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1087   return static_cast<LLVMVisibility>(
1088     unwrap<GlobalValue>(Global)->getVisibility());
1089 }
1090
1091 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1092   unwrap<GlobalValue>(Global)
1093     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1094 }
1095
1096 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1097   return unwrap<GlobalValue>(Global)->getAlignment();
1098 }
1099
1100 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1101   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1102 }
1103
1104 /*--.. Operations on global variables ......................................--*/
1105
1106 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1107   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1108                                  GlobalValue::ExternalLinkage, 0, Name));
1109 }
1110
1111 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1112   return wrap(unwrap(M)->getNamedGlobal(Name));
1113 }
1114
1115 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1116   Module *Mod = unwrap(M);
1117   Module::global_iterator I = Mod->global_begin();
1118   if (I == Mod->global_end())
1119     return 0;
1120   return wrap(I);
1121 }
1122
1123 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1124   Module *Mod = unwrap(M);
1125   Module::global_iterator I = Mod->global_end();
1126   if (I == Mod->global_begin())
1127     return 0;
1128   return wrap(--I);
1129 }
1130
1131 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1132   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1133   Module::global_iterator I = GV;
1134   if (++I == GV->getParent()->global_end())
1135     return 0;
1136   return wrap(I);
1137 }
1138
1139 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1140   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1141   Module::global_iterator I = GV;
1142   if (I == GV->getParent()->global_begin())
1143     return 0;
1144   return wrap(--I);
1145 }
1146
1147 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1148   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1149 }
1150
1151 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1152   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1153   if ( !GV->hasInitializer() )
1154     return 0;
1155   return wrap(GV->getInitializer());
1156 }
1157
1158 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1159   unwrap<GlobalVariable>(GlobalVar)
1160     ->setInitializer(unwrap<Constant>(ConstantVal));
1161 }
1162
1163 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1164   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1165 }
1166
1167 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1168   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1169 }
1170
1171 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1172   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1173 }
1174
1175 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1176   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1177 }
1178
1179 /*--.. Operations on aliases ......................................--*/
1180
1181 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1182                           const char *Name) {
1183   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1184                               unwrap<Constant>(Aliasee), unwrap (M)));
1185 }
1186
1187 /*--.. Operations on functions .............................................--*/
1188
1189 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1190                              LLVMTypeRef FunctionTy) {
1191   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1192                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1193 }
1194
1195 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1196   return wrap(unwrap(M)->getFunction(Name));
1197 }
1198
1199 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1200   Module *Mod = unwrap(M);
1201   Module::iterator I = Mod->begin();
1202   if (I == Mod->end())
1203     return 0;
1204   return wrap(I);
1205 }
1206
1207 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1208   Module *Mod = unwrap(M);
1209   Module::iterator I = Mod->end();
1210   if (I == Mod->begin())
1211     return 0;
1212   return wrap(--I);
1213 }
1214
1215 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1216   Function *Func = unwrap<Function>(Fn);
1217   Module::iterator I = Func;
1218   if (++I == Func->getParent()->end())
1219     return 0;
1220   return wrap(I);
1221 }
1222
1223 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1224   Function *Func = unwrap<Function>(Fn);
1225   Module::iterator I = Func;
1226   if (I == Func->getParent()->begin())
1227     return 0;
1228   return wrap(--I);
1229 }
1230
1231 void LLVMDeleteFunction(LLVMValueRef Fn) {
1232   unwrap<Function>(Fn)->eraseFromParent();
1233 }
1234
1235 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1236   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1237     return F->getIntrinsicID();
1238   return 0;
1239 }
1240
1241 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1242   return unwrap<Function>(Fn)->getCallingConv();
1243 }
1244
1245 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1246   return unwrap<Function>(Fn)->setCallingConv(
1247     static_cast<CallingConv::ID>(CC));
1248 }
1249
1250 const char *LLVMGetGC(LLVMValueRef Fn) {
1251   Function *F = unwrap<Function>(Fn);
1252   return F->hasGC()? F->getGC() : 0;
1253 }
1254
1255 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1256   Function *F = unwrap<Function>(Fn);
1257   if (GC)
1258     F->setGC(GC);
1259   else
1260     F->clearGC();
1261 }
1262
1263 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1264   Function *Func = unwrap<Function>(Fn);
1265   const AttrListPtr PAL = Func->getAttributes();
1266   const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1267   Func->setAttributes(PALnew);
1268 }
1269
1270 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1271   Function *Func = unwrap<Function>(Fn);
1272   const AttrListPtr PAL = Func->getAttributes();
1273   const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1274   Func->setAttributes(PALnew);
1275 }
1276
1277 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1278   Function *Func = unwrap<Function>(Fn);
1279   const AttrListPtr PAL = Func->getAttributes();
1280   Attributes attr = PAL.getFnAttributes();
1281   return (LLVMAttribute)attr;
1282 }
1283
1284 /*--.. Operations on parameters ............................................--*/
1285
1286 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1287   // This function is strictly redundant to
1288   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1289   return unwrap<Function>(FnRef)->arg_size();
1290 }
1291
1292 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1293   Function *Fn = unwrap<Function>(FnRef);
1294   for (Function::arg_iterator I = Fn->arg_begin(),
1295                               E = Fn->arg_end(); I != E; I++)
1296     *ParamRefs++ = wrap(I);
1297 }
1298
1299 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1300   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1301   while (index --> 0)
1302     AI++;
1303   return wrap(AI);
1304 }
1305
1306 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1307   return wrap(unwrap<Argument>(V)->getParent());
1308 }
1309
1310 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1311   Function *Func = unwrap<Function>(Fn);
1312   Function::arg_iterator I = Func->arg_begin();
1313   if (I == Func->arg_end())
1314     return 0;
1315   return wrap(I);
1316 }
1317
1318 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1319   Function *Func = unwrap<Function>(Fn);
1320   Function::arg_iterator I = Func->arg_end();
1321   if (I == Func->arg_begin())
1322     return 0;
1323   return wrap(--I);
1324 }
1325
1326 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1327   Argument *A = unwrap<Argument>(Arg);
1328   Function::arg_iterator I = A;
1329   if (++I == A->getParent()->arg_end())
1330     return 0;
1331   return wrap(I);
1332 }
1333
1334 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1335   Argument *A = unwrap<Argument>(Arg);
1336   Function::arg_iterator I = A;
1337   if (I == A->getParent()->arg_begin())
1338     return 0;
1339   return wrap(--I);
1340 }
1341
1342 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1343   unwrap<Argument>(Arg)->addAttr(PA);
1344 }
1345
1346 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1347   unwrap<Argument>(Arg)->removeAttr(PA);
1348 }
1349
1350 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1351   Argument *A = unwrap<Argument>(Arg);
1352   Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1353     A->getArgNo()+1);
1354   return (LLVMAttribute)attr;
1355 }
1356   
1357
1358 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1359   unwrap<Argument>(Arg)->addAttr(
1360           Attribute::constructAlignmentFromInt(align));
1361 }
1362
1363 /*--.. Operations on basic blocks ..........................................--*/
1364
1365 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1366   return wrap(static_cast<Value*>(unwrap(BB)));
1367 }
1368
1369 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1370   return isa<BasicBlock>(unwrap(Val));
1371 }
1372
1373 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1374   return wrap(unwrap<BasicBlock>(Val));
1375 }
1376
1377 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1378   return wrap(unwrap(BB)->getParent());
1379 }
1380
1381 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1382   return unwrap<Function>(FnRef)->size();
1383 }
1384
1385 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1386   Function *Fn = unwrap<Function>(FnRef);
1387   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1388     *BasicBlocksRefs++ = wrap(I);
1389 }
1390
1391 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1392   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1393 }
1394
1395 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1396   Function *Func = unwrap<Function>(Fn);
1397   Function::iterator I = Func->begin();
1398   if (I == Func->end())
1399     return 0;
1400   return wrap(I);
1401 }
1402
1403 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1404   Function *Func = unwrap<Function>(Fn);
1405   Function::iterator I = Func->end();
1406   if (I == Func->begin())
1407     return 0;
1408   return wrap(--I);
1409 }
1410
1411 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1412   BasicBlock *Block = unwrap(BB);
1413   Function::iterator I = Block;
1414   if (++I == Block->getParent()->end())
1415     return 0;
1416   return wrap(I);
1417 }
1418
1419 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1420   BasicBlock *Block = unwrap(BB);
1421   Function::iterator I = Block;
1422   if (I == Block->getParent()->begin())
1423     return 0;
1424   return wrap(--I);
1425 }
1426
1427 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1428                                                 LLVMValueRef FnRef,
1429                                                 const char *Name) {
1430   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1431 }
1432
1433 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1434   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1435 }
1436
1437 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1438                                                 LLVMBasicBlockRef BBRef,
1439                                                 const char *Name) {
1440   BasicBlock *BB = unwrap(BBRef);
1441   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1442 }
1443
1444 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1445                                        const char *Name) {
1446   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1447 }
1448
1449 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1450   unwrap(BBRef)->eraseFromParent();
1451 }
1452
1453 /*--.. Operations on instructions ..........................................--*/
1454
1455 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1456   return wrap(unwrap<Instruction>(Inst)->getParent());
1457 }
1458
1459 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1460   BasicBlock *Block = unwrap(BB);
1461   BasicBlock::iterator I = Block->begin();
1462   if (I == Block->end())
1463     return 0;
1464   return wrap(I);
1465 }
1466
1467 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1468   BasicBlock *Block = unwrap(BB);
1469   BasicBlock::iterator I = Block->end();
1470   if (I == Block->begin())
1471     return 0;
1472   return wrap(--I);
1473 }
1474
1475 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1476   Instruction *Instr = unwrap<Instruction>(Inst);
1477   BasicBlock::iterator I = Instr;
1478   if (++I == Instr->getParent()->end())
1479     return 0;
1480   return wrap(I);
1481 }
1482
1483 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1484   Instruction *Instr = unwrap<Instruction>(Inst);
1485   BasicBlock::iterator I = Instr;
1486   if (I == Instr->getParent()->begin())
1487     return 0;
1488   return wrap(--I);
1489 }
1490
1491 /*--.. Call and invoke instructions ........................................--*/
1492
1493 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1494   Value *V = unwrap(Instr);
1495   if (CallInst *CI = dyn_cast<CallInst>(V))
1496     return CI->getCallingConv();
1497   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1498     return II->getCallingConv();
1499   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1500   return 0;
1501 }
1502
1503 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1504   Value *V = unwrap(Instr);
1505   if (CallInst *CI = dyn_cast<CallInst>(V))
1506     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1507   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1508     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1509   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1510 }
1511
1512 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1513                            LLVMAttribute PA) {
1514   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1515   Call.setAttributes(
1516     Call.getAttributes().addAttr(index, PA));
1517 }
1518
1519 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1520                               LLVMAttribute PA) {
1521   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1522   Call.setAttributes(
1523     Call.getAttributes().removeAttr(index, PA));
1524 }
1525
1526 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1527                                 unsigned align) {
1528   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1529   Call.setAttributes(
1530     Call.getAttributes().addAttr(index, 
1531         Attribute::constructAlignmentFromInt(align)));
1532 }
1533
1534 /*--.. Operations on call instructions (only) ..............................--*/
1535
1536 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1537   return unwrap<CallInst>(Call)->isTailCall();
1538 }
1539
1540 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1541   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1542 }
1543
1544 /*--.. Operations on phi nodes .............................................--*/
1545
1546 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1547                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1548   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1549   for (unsigned I = 0; I != Count; ++I)
1550     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1551 }
1552
1553 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1554   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1555 }
1556
1557 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1558   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1559 }
1560
1561 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1562   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1563 }
1564
1565
1566 /*===-- Instruction builders ----------------------------------------------===*/
1567
1568 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1569   return wrap(new IRBuilder<>(*unwrap(C)));
1570 }
1571
1572 LLVMBuilderRef LLVMCreateBuilder(void) {
1573   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1574 }
1575
1576 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1577                          LLVMValueRef Instr) {
1578   BasicBlock *BB = unwrap(Block);
1579   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1580   unwrap(Builder)->SetInsertPoint(BB, I);
1581 }
1582
1583 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1584   Instruction *I = unwrap<Instruction>(Instr);
1585   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1586 }
1587
1588 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1589   BasicBlock *BB = unwrap(Block);
1590   unwrap(Builder)->SetInsertPoint(BB);
1591 }
1592
1593 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1594    return wrap(unwrap(Builder)->GetInsertBlock());
1595 }
1596
1597 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1598   unwrap(Builder)->ClearInsertionPoint ();
1599 }
1600
1601 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1602   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1603 }
1604
1605 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1606                                    const char *Name) {
1607   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1608 }
1609
1610 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1611   delete unwrap(Builder);
1612 }
1613
1614 /*--.. Instruction builders ................................................--*/
1615
1616 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1617   return wrap(unwrap(B)->CreateRetVoid());
1618 }
1619
1620 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1621   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1622 }
1623
1624 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1625                                    unsigned N) {
1626   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1627 }
1628
1629 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1630   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1631 }
1632
1633 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1634                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1635   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1636 }
1637
1638 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1639                              LLVMBasicBlockRef Else, unsigned NumCases) {
1640   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1641 }
1642
1643 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1644                              LLVMValueRef *Args, unsigned NumArgs,
1645                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1646                              const char *Name) {
1647   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1648                                       unwrap(Args), unwrap(Args) + NumArgs,
1649                                       Name));
1650 }
1651
1652 LLVMValueRef LLVMBuildUnwind(LLVMBuilderRef B) {
1653   return wrap(unwrap(B)->CreateUnwind());
1654 }
1655
1656 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1657   return wrap(unwrap(B)->CreateUnreachable());
1658 }
1659
1660 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1661                  LLVMBasicBlockRef Dest) {
1662   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1663 }
1664
1665 /*--.. Arithmetic ..........................................................--*/
1666
1667 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1668                           const char *Name) {
1669   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1670 }
1671
1672 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1673                           const char *Name) {
1674   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1675 }
1676
1677 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1678                           const char *Name) {
1679   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1680 }
1681
1682 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1683                           const char *Name) {
1684   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1685 }
1686
1687 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1688                           const char *Name) {
1689   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1690 }
1691
1692 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1693                           const char *Name) {
1694   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1695 }
1696
1697 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1698                           const char *Name) {
1699   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1700 }
1701
1702 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1703                           const char *Name) {
1704   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1705 }
1706
1707 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1708                           const char *Name) {
1709   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1710 }
1711
1712 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1713                           const char *Name) {
1714   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1715 }
1716
1717 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1718                           const char *Name) {
1719   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1720 }
1721
1722 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1723                           const char *Name) {
1724   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1725 }
1726
1727 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1728                            const char *Name) {
1729   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1730 }
1731
1732 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1733                            const char *Name) {
1734   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1735 }
1736
1737 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1738                                 LLVMValueRef RHS, const char *Name) {
1739   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1740 }
1741
1742 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1743                            const char *Name) {
1744   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1745 }
1746
1747 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1748                            const char *Name) {
1749   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1750 }
1751
1752 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1753                            const char *Name) {
1754   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1755 }
1756
1757 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1758                            const char *Name) {
1759   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1760 }
1761
1762 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1763                           const char *Name) {
1764   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1765 }
1766
1767 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1768                            const char *Name) {
1769   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1770 }
1771
1772 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1773                            const char *Name) {
1774   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1775 }
1776
1777 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1778                           const char *Name) {
1779   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1780 }
1781
1782 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1783                          const char *Name) {
1784   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1785 }
1786
1787 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1788                           const char *Name) {
1789   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1790 }
1791
1792 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1793                             LLVMValueRef LHS, LLVMValueRef RHS,
1794                             const char *Name) {
1795   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1796                                      unwrap(RHS), Name));
1797 }
1798
1799 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1800   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1801 }
1802
1803 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1804                              const char *Name) {
1805   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1806 }
1807
1808 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1809                              const char *Name) {
1810   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1811 }
1812
1813 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1814   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1815 }
1816
1817 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1818   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1819 }
1820
1821 /*--.. Memory ..............................................................--*/
1822
1823 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1824                              const char *Name) {
1825   const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1826   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1827   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1828   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1829                                                ITy, unwrap(Ty), AllocSize, 
1830                                                0, 0, "");
1831   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1832 }
1833
1834 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1835                                   LLVMValueRef Val, const char *Name) {
1836   const Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1837   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1838   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1839   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1840                                                ITy, unwrap(Ty), AllocSize, 
1841                                                unwrap(Val), 0, "");
1842   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1843 }
1844
1845 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1846                              const char *Name) {
1847   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1848 }
1849
1850 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1851                                   LLVMValueRef Val, const char *Name) {
1852   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1853 }
1854
1855 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1856   return wrap(unwrap(B)->Insert(
1857      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1858 }
1859
1860
1861 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1862                            const char *Name) {
1863   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1864 }
1865
1866 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1867                             LLVMValueRef PointerVal) {
1868   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1869 }
1870
1871 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1872                           LLVMValueRef *Indices, unsigned NumIndices,
1873                           const char *Name) {
1874   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), unwrap(Indices),
1875                                    unwrap(Indices) + NumIndices, Name));
1876 }
1877
1878 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1879                                   LLVMValueRef *Indices, unsigned NumIndices,
1880                                   const char *Name) {
1881   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), unwrap(Indices),
1882                                            unwrap(Indices) + NumIndices, Name));
1883 }
1884
1885 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1886                                 unsigned Idx, const char *Name) {
1887   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
1888 }
1889
1890 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
1891                                    const char *Name) {
1892   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
1893 }
1894
1895 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
1896                                       const char *Name) {
1897   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
1898 }
1899
1900 /*--.. Casts ...............................................................--*/
1901
1902 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1903                             LLVMTypeRef DestTy, const char *Name) {
1904   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
1905 }
1906
1907 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
1908                            LLVMTypeRef DestTy, const char *Name) {
1909   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
1910 }
1911
1912 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
1913                            LLVMTypeRef DestTy, const char *Name) {
1914   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
1915 }
1916
1917 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
1918                              LLVMTypeRef DestTy, const char *Name) {
1919   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
1920 }
1921
1922 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
1923                              LLVMTypeRef DestTy, const char *Name) {
1924   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
1925 }
1926
1927 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1928                              LLVMTypeRef DestTy, const char *Name) {
1929   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
1930 }
1931
1932 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
1933                              LLVMTypeRef DestTy, const char *Name) {
1934   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
1935 }
1936
1937 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
1938                               LLVMTypeRef DestTy, const char *Name) {
1939   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
1940 }
1941
1942 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
1943                             LLVMTypeRef DestTy, const char *Name) {
1944   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
1945 }
1946
1947 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
1948                                LLVMTypeRef DestTy, const char *Name) {
1949   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
1950 }
1951
1952 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
1953                                LLVMTypeRef DestTy, const char *Name) {
1954   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
1955 }
1956
1957 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1958                               LLVMTypeRef DestTy, const char *Name) {
1959   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
1960 }
1961
1962 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1963                                     LLVMTypeRef DestTy, const char *Name) {
1964   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
1965                                              Name));
1966 }
1967
1968 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1969                                     LLVMTypeRef DestTy, const char *Name) {
1970   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
1971                                              Name));
1972 }
1973
1974 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
1975                                      LLVMTypeRef DestTy, const char *Name) {
1976   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
1977                                               Name));
1978 }
1979
1980 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
1981                            LLVMTypeRef DestTy, const char *Name) {
1982   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
1983                                     unwrap(DestTy), Name));
1984 }
1985
1986 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
1987                                   LLVMTypeRef DestTy, const char *Name) {
1988   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
1989 }
1990
1991 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
1992                               LLVMTypeRef DestTy, const char *Name) {
1993   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
1994                                        /*isSigned*/true, Name));
1995 }
1996
1997 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
1998                              LLVMTypeRef DestTy, const char *Name) {
1999   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2000 }
2001
2002 /*--.. Comparisons .........................................................--*/
2003
2004 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2005                            LLVMValueRef LHS, LLVMValueRef RHS,
2006                            const char *Name) {
2007   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2008                                     unwrap(LHS), unwrap(RHS), Name));
2009 }
2010
2011 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2012                            LLVMValueRef LHS, LLVMValueRef RHS,
2013                            const char *Name) {
2014   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2015                                     unwrap(LHS), unwrap(RHS), Name));
2016 }
2017
2018 /*--.. Miscellaneous instructions ..........................................--*/
2019
2020 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2021   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), Name));
2022 }
2023
2024 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2025                            LLVMValueRef *Args, unsigned NumArgs,
2026                            const char *Name) {
2027   return wrap(unwrap(B)->CreateCall(unwrap(Fn), unwrap(Args),
2028                                     unwrap(Args) + NumArgs, Name));
2029 }
2030
2031 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2032                              LLVMValueRef Then, LLVMValueRef Else,
2033                              const char *Name) {
2034   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2035                                       Name));
2036 }
2037
2038 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2039                             LLVMTypeRef Ty, const char *Name) {
2040   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2041 }
2042
2043 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2044                                       LLVMValueRef Index, const char *Name) {
2045   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2046                                               Name));
2047 }
2048
2049 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2050                                     LLVMValueRef EltVal, LLVMValueRef Index,
2051                                     const char *Name) {
2052   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2053                                              unwrap(Index), Name));
2054 }
2055
2056 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2057                                     LLVMValueRef V2, LLVMValueRef Mask,
2058                                     const char *Name) {
2059   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2060                                              unwrap(Mask), Name));
2061 }
2062
2063 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2064                                    unsigned Index, const char *Name) {
2065   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2066 }
2067
2068 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2069                                   LLVMValueRef EltVal, unsigned Index,
2070                                   const char *Name) {
2071   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2072                                            Index, Name));
2073 }
2074
2075 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2076                              const char *Name) {
2077   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2078 }
2079
2080 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2081                                 const char *Name) {
2082   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2083 }
2084
2085 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2086                               LLVMValueRef RHS, const char *Name) {
2087   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2088 }
2089
2090
2091 /*===-- Module providers --------------------------------------------------===*/
2092
2093 LLVMModuleProviderRef
2094 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2095   return reinterpret_cast<LLVMModuleProviderRef>(M);
2096 }
2097
2098 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2099   delete unwrap(MP);
2100 }
2101
2102
2103 /*===-- Memory buffers ----------------------------------------------------===*/
2104
2105 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2106     const char *Path,
2107     LLVMMemoryBufferRef *OutMemBuf,
2108     char **OutMessage) {
2109
2110   std::string Error;
2111   if (MemoryBuffer *MB = MemoryBuffer::getFile(Path, &Error)) {
2112     *OutMemBuf = wrap(MB);
2113     return 0;
2114   }
2115   
2116   *OutMessage = strdup(Error.c_str());
2117   return 1;
2118 }
2119
2120 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2121                                          char **OutMessage) {
2122   MemoryBuffer *MB = MemoryBuffer::getSTDIN();
2123   if (!MB->getBufferSize()) {
2124     delete MB;
2125     *OutMessage = strdup("stdin is empty.");
2126     return 1;
2127   }
2128
2129   *OutMemBuf = wrap(MB);
2130   return 0;
2131 }
2132
2133 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2134   delete unwrap(MemBuf);
2135 }