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