ocaml/C bindings: getmdstring, add num_op, get_op should work on metadata too
[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   Value *V = unwrap(Val);
460   if (MDNode *MD = dyn_cast<MDNode>(V))
461       return wrap(MD->getOperand(Index));
462   return wrap(cast<User>(V)->getOperand(Index));
463 }
464
465 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
466   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
467 }
468
469 int LLVMGetNumOperands(LLVMValueRef Val) {
470   Value *V = unwrap(Val);
471   if (MDNode *MD = dyn_cast<MDNode>(V))
472       return MD->getNumOperands();
473   return cast<User>(V)->getNumOperands();
474 }
475
476 /*--.. Operations on constants of any type .................................--*/
477
478 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
479   return wrap(Constant::getNullValue(unwrap(Ty)));
480 }
481
482 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
483   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
484 }
485
486 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
487   return wrap(UndefValue::get(unwrap(Ty)));
488 }
489
490 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
491   return isa<Constant>(unwrap(Ty));
492 }
493
494 LLVMBool LLVMIsNull(LLVMValueRef Val) {
495   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
496     return C->isNullValue();
497   return false;
498 }
499
500 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
501   return isa<UndefValue>(unwrap(Val));
502 }
503
504 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
505   return
506       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
507 }
508
509 /*--.. Operations on metadata nodes ........................................--*/
510
511 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
512                                    unsigned SLen) {
513   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
514 }
515
516 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
517   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
518 }
519
520 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
521                                  unsigned Count) {
522   return wrap(MDNode::get(*unwrap(C),
523                           makeArrayRef(unwrap<Value>(Vals, Count), Count)));
524 }
525
526 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
527   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
528 }
529
530 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
531     if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
532         *Len = S->getString().size();
533         return S->getString().data();
534     }
535     *Len = 0;
536     return 0;
537 }
538
539 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
540 {
541     if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
542         return N->getNumOperands();
543     }
544     return 0;
545 }
546
547 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
548 {
549     NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
550     if (!N)
551         return;
552     for (unsigned i=0;i<N->getNumOperands();i++)
553         Dest[i] = wrap(N->getOperand(i));
554 }
555
556 /*--.. Operations on scalar constants ......................................--*/
557
558 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
559                           LLVMBool SignExtend) {
560   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
561 }
562
563 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
564                                               unsigned NumWords,
565                                               const uint64_t Words[]) {
566     IntegerType *Ty = unwrap<IntegerType>(IntTy);
567     return wrap(ConstantInt::get(Ty->getContext(),
568                                  APInt(Ty->getBitWidth(),
569                                        makeArrayRef(Words, NumWords))));
570 }
571
572 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
573                                   uint8_t Radix) {
574   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
575                                Radix));
576 }
577
578 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
579                                          unsigned SLen, uint8_t Radix) {
580   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
581                                Radix));
582 }
583
584 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
585   return wrap(ConstantFP::get(unwrap(RealTy), N));
586 }
587
588 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
589   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
590 }
591
592 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
593                                           unsigned SLen) {
594   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
595 }
596
597 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
598   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
599 }
600
601 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
602   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
603 }
604
605 /*--.. Operations on composite constants ...................................--*/
606
607 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
608                                       unsigned Length,
609                                       LLVMBool DontNullTerminate) {
610   /* Inverted the sense of AddNull because ', 0)' is a
611      better mnemonic for null termination than ', 1)'. */
612   return wrap(ConstantArray::get(*unwrap(C), StringRef(Str, Length),
613                                  DontNullTerminate == 0));
614 }
615 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 
616                                       LLVMValueRef *ConstantVals,
617                                       unsigned Count, LLVMBool Packed) {
618   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
619   return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
620                                       Packed != 0));
621 }
622
623 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
624                              LLVMBool DontNullTerminate) {
625   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
626                                   DontNullTerminate);
627 }
628 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
629                             LLVMValueRef *ConstantVals, unsigned Length) {
630   ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
631   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
632 }
633 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
634                              LLVMBool Packed) {
635   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
636                                   Packed);
637 }
638
639 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
640                                   LLVMValueRef *ConstantVals,
641                                   unsigned Count) {
642   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
643   StructType *Ty = cast<StructType>(unwrap(StructTy));
644
645   return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
646 }
647
648 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
649   return wrap(ConstantVector::get(makeArrayRef(
650                             unwrap<Constant>(ScalarConstantVals, Size), Size)));
651 }
652 /*--.. Constant expressions ................................................--*/
653
654 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
655   return (LLVMOpcode)unwrap<ConstantExpr>(ConstantVal)->getOpcode();
656 }
657
658 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
659   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
660 }
661
662 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
663   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
664 }
665
666 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
667   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
668 }
669
670 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
671   return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
672 }
673
674 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
675   return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
676 }
677
678
679 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
680   return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
681 }
682
683 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
684   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
685 }
686
687 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
688   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
689                                    unwrap<Constant>(RHSConstant)));
690 }
691
692 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
693                              LLVMValueRef RHSConstant) {
694   return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
695                                       unwrap<Constant>(RHSConstant)));
696 }
697
698 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
699                              LLVMValueRef RHSConstant) {
700   return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
701                                       unwrap<Constant>(RHSConstant)));
702 }
703
704 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
705   return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
706                                     unwrap<Constant>(RHSConstant)));
707 }
708
709 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
710   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
711                                    unwrap<Constant>(RHSConstant)));
712 }
713
714 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
715                              LLVMValueRef RHSConstant) {
716   return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
717                                       unwrap<Constant>(RHSConstant)));
718 }
719
720 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
721                              LLVMValueRef RHSConstant) {
722   return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
723                                       unwrap<Constant>(RHSConstant)));
724 }
725
726 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
727   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
728                                     unwrap<Constant>(RHSConstant)));
729 }
730
731 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
732   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
733                                    unwrap<Constant>(RHSConstant)));
734 }
735
736 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
737                              LLVMValueRef RHSConstant) {
738   return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
739                                       unwrap<Constant>(RHSConstant)));
740 }
741
742 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
743                              LLVMValueRef RHSConstant) {
744   return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
745                                       unwrap<Constant>(RHSConstant)));
746 }
747
748 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
749   return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
750                                     unwrap<Constant>(RHSConstant)));
751 }
752
753 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
754   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
755                                     unwrap<Constant>(RHSConstant)));
756 }
757
758 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
759   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
760                                     unwrap<Constant>(RHSConstant)));
761 }
762
763 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
764                                 LLVMValueRef RHSConstant) {
765   return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
766                                          unwrap<Constant>(RHSConstant)));
767 }
768
769 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
770   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
771                                     unwrap<Constant>(RHSConstant)));
772 }
773
774 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
775   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
776                                     unwrap<Constant>(RHSConstant)));
777 }
778
779 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
780   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
781                                     unwrap<Constant>(RHSConstant)));
782 }
783
784 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
785   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
786                                     unwrap<Constant>(RHSConstant)));
787 }
788
789 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
790   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
791                                    unwrap<Constant>(RHSConstant)));
792 }
793
794 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
795   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
796                                   unwrap<Constant>(RHSConstant)));
797 }
798
799 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
800   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
801                                    unwrap<Constant>(RHSConstant)));
802 }
803
804 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
805                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
806   return wrap(ConstantExpr::getICmp(Predicate,
807                                     unwrap<Constant>(LHSConstant),
808                                     unwrap<Constant>(RHSConstant)));
809 }
810
811 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
812                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
813   return wrap(ConstantExpr::getFCmp(Predicate,
814                                     unwrap<Constant>(LHSConstant),
815                                     unwrap<Constant>(RHSConstant)));
816 }
817
818 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
819   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
820                                    unwrap<Constant>(RHSConstant)));
821 }
822
823 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
824   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
825                                     unwrap<Constant>(RHSConstant)));
826 }
827
828 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
829   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
830                                     unwrap<Constant>(RHSConstant)));
831 }
832
833 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
834                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
835   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
836                                NumIndices);
837   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
838                                              IdxList));
839 }
840
841 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
842                                   LLVMValueRef *ConstantIndices,
843                                   unsigned NumIndices) {
844   Constant* Val = unwrap<Constant>(ConstantVal);
845   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
846                                NumIndices);
847   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
848 }
849
850 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
851   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
852                                      unwrap(ToType)));
853 }
854
855 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
856   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
857                                     unwrap(ToType)));
858 }
859
860 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
861   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
862                                     unwrap(ToType)));
863 }
864
865 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
866   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
867                                        unwrap(ToType)));
868 }
869
870 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
871   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
872                                         unwrap(ToType)));
873 }
874
875 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
876   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
877                                       unwrap(ToType)));
878 }
879
880 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
881   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
882                                       unwrap(ToType)));
883 }
884
885 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
886   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
887                                       unwrap(ToType)));
888 }
889
890 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
891   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
892                                       unwrap(ToType)));
893 }
894
895 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
896   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
897                                         unwrap(ToType)));
898 }
899
900 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
901   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
902                                         unwrap(ToType)));
903 }
904
905 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
906   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
907                                        unwrap(ToType)));
908 }
909
910 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
911                                     LLVMTypeRef ToType) {
912   return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
913                                              unwrap(ToType)));
914 }
915
916 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
917                                     LLVMTypeRef ToType) {
918   return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
919                                              unwrap(ToType)));
920 }
921
922 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
923                                      LLVMTypeRef ToType) {
924   return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
925                                               unwrap(ToType)));
926 }
927
928 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
929                                   LLVMTypeRef ToType) {
930   return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
931                                            unwrap(ToType)));
932 }
933
934 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
935                               LLVMBool isSigned) {
936   return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
937                                            unwrap(ToType), isSigned));
938 }
939
940 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
941   return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
942                                       unwrap(ToType)));
943 }
944
945 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
946                              LLVMValueRef ConstantIfTrue,
947                              LLVMValueRef ConstantIfFalse) {
948   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
949                                       unwrap<Constant>(ConstantIfTrue),
950                                       unwrap<Constant>(ConstantIfFalse)));
951 }
952
953 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
954                                      LLVMValueRef IndexConstant) {
955   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
956                                               unwrap<Constant>(IndexConstant)));
957 }
958
959 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
960                                     LLVMValueRef ElementValueConstant,
961                                     LLVMValueRef IndexConstant) {
962   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
963                                          unwrap<Constant>(ElementValueConstant),
964                                              unwrap<Constant>(IndexConstant)));
965 }
966
967 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
968                                     LLVMValueRef VectorBConstant,
969                                     LLVMValueRef MaskConstant) {
970   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
971                                              unwrap<Constant>(VectorBConstant),
972                                              unwrap<Constant>(MaskConstant)));
973 }
974
975 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
976                                    unsigned NumIdx) {
977   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
978                                             makeArrayRef(IdxList, NumIdx)));
979 }
980
981 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
982                                   LLVMValueRef ElementValueConstant,
983                                   unsigned *IdxList, unsigned NumIdx) {
984   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
985                                          unwrap<Constant>(ElementValueConstant),
986                                            makeArrayRef(IdxList, NumIdx)));
987 }
988
989 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
990                                 const char *Constraints,
991                                 LLVMBool HasSideEffects,
992                                 LLVMBool IsAlignStack) {
993   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
994                              Constraints, HasSideEffects, IsAlignStack));
995 }
996
997 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
998   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
999 }
1000
1001 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1002
1003 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1004   return wrap(unwrap<GlobalValue>(Global)->getParent());
1005 }
1006
1007 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1008   return unwrap<GlobalValue>(Global)->isDeclaration();
1009 }
1010
1011 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1012   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1013   default:
1014     assert(false && "Unhandled Linkage Type.");
1015   case GlobalValue::ExternalLinkage:
1016     return LLVMExternalLinkage;
1017   case GlobalValue::AvailableExternallyLinkage:
1018     return LLVMAvailableExternallyLinkage;
1019   case GlobalValue::LinkOnceAnyLinkage:
1020     return LLVMLinkOnceAnyLinkage;
1021   case GlobalValue::LinkOnceODRLinkage:
1022     return LLVMLinkOnceODRLinkage;
1023   case GlobalValue::WeakAnyLinkage:
1024     return LLVMWeakAnyLinkage;
1025   case GlobalValue::WeakODRLinkage:
1026     return LLVMWeakODRLinkage;
1027   case GlobalValue::AppendingLinkage:
1028     return LLVMAppendingLinkage;
1029   case GlobalValue::InternalLinkage:
1030     return LLVMInternalLinkage;
1031   case GlobalValue::PrivateLinkage:
1032     return LLVMPrivateLinkage;
1033   case GlobalValue::LinkerPrivateLinkage:
1034     return LLVMLinkerPrivateLinkage;
1035   case GlobalValue::LinkerPrivateWeakLinkage:
1036     return LLVMLinkerPrivateWeakLinkage;
1037   case GlobalValue::LinkerPrivateWeakDefAutoLinkage:
1038     return LLVMLinkerPrivateWeakDefAutoLinkage;
1039   case GlobalValue::DLLImportLinkage:
1040     return LLVMDLLImportLinkage;
1041   case GlobalValue::DLLExportLinkage:
1042     return LLVMDLLExportLinkage;
1043   case GlobalValue::ExternalWeakLinkage:
1044     return LLVMExternalWeakLinkage;
1045   case GlobalValue::CommonLinkage:
1046     return LLVMCommonLinkage;
1047   }
1048
1049   // Should never get here.
1050   return static_cast<LLVMLinkage>(0);
1051 }
1052
1053 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1054   GlobalValue *GV = unwrap<GlobalValue>(Global);
1055
1056   switch (Linkage) {
1057   default:
1058     assert(false && "Unhandled Linkage Type.");
1059   case LLVMExternalLinkage:
1060     GV->setLinkage(GlobalValue::ExternalLinkage);
1061     break;
1062   case LLVMAvailableExternallyLinkage:
1063     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1064     break;
1065   case LLVMLinkOnceAnyLinkage:
1066     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1067     break;
1068   case LLVMLinkOnceODRLinkage:
1069     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1070     break;
1071   case LLVMWeakAnyLinkage:
1072     GV->setLinkage(GlobalValue::WeakAnyLinkage);
1073     break;
1074   case LLVMWeakODRLinkage:
1075     GV->setLinkage(GlobalValue::WeakODRLinkage);
1076     break;
1077   case LLVMAppendingLinkage:
1078     GV->setLinkage(GlobalValue::AppendingLinkage);
1079     break;
1080   case LLVMInternalLinkage:
1081     GV->setLinkage(GlobalValue::InternalLinkage);
1082     break;
1083   case LLVMPrivateLinkage:
1084     GV->setLinkage(GlobalValue::PrivateLinkage);
1085     break;
1086   case LLVMLinkerPrivateLinkage:
1087     GV->setLinkage(GlobalValue::LinkerPrivateLinkage);
1088     break;
1089   case LLVMLinkerPrivateWeakLinkage:
1090     GV->setLinkage(GlobalValue::LinkerPrivateWeakLinkage);
1091     break;
1092   case LLVMLinkerPrivateWeakDefAutoLinkage:
1093     GV->setLinkage(GlobalValue::LinkerPrivateWeakDefAutoLinkage);
1094     break;
1095   case LLVMDLLImportLinkage:
1096     GV->setLinkage(GlobalValue::DLLImportLinkage);
1097     break;
1098   case LLVMDLLExportLinkage:
1099     GV->setLinkage(GlobalValue::DLLExportLinkage);
1100     break;
1101   case LLVMExternalWeakLinkage:
1102     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1103     break;
1104   case LLVMGhostLinkage:
1105     DEBUG(errs()
1106           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1107     break;
1108   case LLVMCommonLinkage:
1109     GV->setLinkage(GlobalValue::CommonLinkage);
1110     break;
1111   }
1112 }
1113
1114 const char *LLVMGetSection(LLVMValueRef Global) {
1115   return unwrap<GlobalValue>(Global)->getSection().c_str();
1116 }
1117
1118 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1119   unwrap<GlobalValue>(Global)->setSection(Section);
1120 }
1121
1122 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1123   return static_cast<LLVMVisibility>(
1124     unwrap<GlobalValue>(Global)->getVisibility());
1125 }
1126
1127 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1128   unwrap<GlobalValue>(Global)
1129     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1130 }
1131
1132 unsigned LLVMGetAlignment(LLVMValueRef Global) {
1133   return unwrap<GlobalValue>(Global)->getAlignment();
1134 }
1135
1136 void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) {
1137   unwrap<GlobalValue>(Global)->setAlignment(Bytes);
1138 }
1139
1140 /*--.. Operations on global variables ......................................--*/
1141
1142 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1143   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1144                                  GlobalValue::ExternalLinkage, 0, Name));
1145 }
1146
1147 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1148                                          const char *Name,
1149                                          unsigned AddressSpace) {
1150   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1151                                  GlobalValue::ExternalLinkage, 0, Name, 0,
1152                                  false, AddressSpace));
1153 }
1154
1155 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1156   return wrap(unwrap(M)->getNamedGlobal(Name));
1157 }
1158
1159 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1160   Module *Mod = unwrap(M);
1161   Module::global_iterator I = Mod->global_begin();
1162   if (I == Mod->global_end())
1163     return 0;
1164   return wrap(I);
1165 }
1166
1167 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1168   Module *Mod = unwrap(M);
1169   Module::global_iterator I = Mod->global_end();
1170   if (I == Mod->global_begin())
1171     return 0;
1172   return wrap(--I);
1173 }
1174
1175 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1176   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1177   Module::global_iterator I = GV;
1178   if (++I == GV->getParent()->global_end())
1179     return 0;
1180   return wrap(I);
1181 }
1182
1183 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1184   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1185   Module::global_iterator I = GV;
1186   if (I == GV->getParent()->global_begin())
1187     return 0;
1188   return wrap(--I);
1189 }
1190
1191 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1192   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1193 }
1194
1195 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1196   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1197   if ( !GV->hasInitializer() )
1198     return 0;
1199   return wrap(GV->getInitializer());
1200 }
1201
1202 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1203   unwrap<GlobalVariable>(GlobalVar)
1204     ->setInitializer(unwrap<Constant>(ConstantVal));
1205 }
1206
1207 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1208   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1209 }
1210
1211 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1212   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1213 }
1214
1215 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1216   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1217 }
1218
1219 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1220   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1221 }
1222
1223 /*--.. Operations on aliases ......................................--*/
1224
1225 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1226                           const char *Name) {
1227   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1228                               unwrap<Constant>(Aliasee), unwrap (M)));
1229 }
1230
1231 /*--.. Operations on functions .............................................--*/
1232
1233 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1234                              LLVMTypeRef FunctionTy) {
1235   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1236                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
1237 }
1238
1239 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1240   return wrap(unwrap(M)->getFunction(Name));
1241 }
1242
1243 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1244   Module *Mod = unwrap(M);
1245   Module::iterator I = Mod->begin();
1246   if (I == Mod->end())
1247     return 0;
1248   return wrap(I);
1249 }
1250
1251 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1252   Module *Mod = unwrap(M);
1253   Module::iterator I = Mod->end();
1254   if (I == Mod->begin())
1255     return 0;
1256   return wrap(--I);
1257 }
1258
1259 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1260   Function *Func = unwrap<Function>(Fn);
1261   Module::iterator I = Func;
1262   if (++I == Func->getParent()->end())
1263     return 0;
1264   return wrap(I);
1265 }
1266
1267 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1268   Function *Func = unwrap<Function>(Fn);
1269   Module::iterator I = Func;
1270   if (I == Func->getParent()->begin())
1271     return 0;
1272   return wrap(--I);
1273 }
1274
1275 void LLVMDeleteFunction(LLVMValueRef Fn) {
1276   unwrap<Function>(Fn)->eraseFromParent();
1277 }
1278
1279 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1280   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1281     return F->getIntrinsicID();
1282   return 0;
1283 }
1284
1285 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1286   return unwrap<Function>(Fn)->getCallingConv();
1287 }
1288
1289 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1290   return unwrap<Function>(Fn)->setCallingConv(
1291     static_cast<CallingConv::ID>(CC));
1292 }
1293
1294 const char *LLVMGetGC(LLVMValueRef Fn) {
1295   Function *F = unwrap<Function>(Fn);
1296   return F->hasGC()? F->getGC() : 0;
1297 }
1298
1299 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1300   Function *F = unwrap<Function>(Fn);
1301   if (GC)
1302     F->setGC(GC);
1303   else
1304     F->clearGC();
1305 }
1306
1307 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1308   Function *Func = unwrap<Function>(Fn);
1309   const AttrListPtr PAL = Func->getAttributes();
1310   const AttrListPtr PALnew = PAL.addAttr(~0U, PA);
1311   Func->setAttributes(PALnew);
1312 }
1313
1314 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1315   Function *Func = unwrap<Function>(Fn);
1316   const AttrListPtr PAL = Func->getAttributes();
1317   const AttrListPtr PALnew = PAL.removeAttr(~0U, PA);
1318   Func->setAttributes(PALnew);
1319 }
1320
1321 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1322   Function *Func = unwrap<Function>(Fn);
1323   const AttrListPtr PAL = Func->getAttributes();
1324   Attributes attr = PAL.getFnAttributes();
1325   return (LLVMAttribute)attr;
1326 }
1327
1328 /*--.. Operations on parameters ............................................--*/
1329
1330 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1331   // This function is strictly redundant to
1332   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1333   return unwrap<Function>(FnRef)->arg_size();
1334 }
1335
1336 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1337   Function *Fn = unwrap<Function>(FnRef);
1338   for (Function::arg_iterator I = Fn->arg_begin(),
1339                               E = Fn->arg_end(); I != E; I++)
1340     *ParamRefs++ = wrap(I);
1341 }
1342
1343 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1344   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1345   while (index --> 0)
1346     AI++;
1347   return wrap(AI);
1348 }
1349
1350 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1351   return wrap(unwrap<Argument>(V)->getParent());
1352 }
1353
1354 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1355   Function *Func = unwrap<Function>(Fn);
1356   Function::arg_iterator I = Func->arg_begin();
1357   if (I == Func->arg_end())
1358     return 0;
1359   return wrap(I);
1360 }
1361
1362 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1363   Function *Func = unwrap<Function>(Fn);
1364   Function::arg_iterator I = Func->arg_end();
1365   if (I == Func->arg_begin())
1366     return 0;
1367   return wrap(--I);
1368 }
1369
1370 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1371   Argument *A = unwrap<Argument>(Arg);
1372   Function::arg_iterator I = A;
1373   if (++I == A->getParent()->arg_end())
1374     return 0;
1375   return wrap(I);
1376 }
1377
1378 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1379   Argument *A = unwrap<Argument>(Arg);
1380   Function::arg_iterator I = A;
1381   if (I == A->getParent()->arg_begin())
1382     return 0;
1383   return wrap(--I);
1384 }
1385
1386 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1387   unwrap<Argument>(Arg)->addAttr(PA);
1388 }
1389
1390 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1391   unwrap<Argument>(Arg)->removeAttr(PA);
1392 }
1393
1394 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1395   Argument *A = unwrap<Argument>(Arg);
1396   Attributes attr = A->getParent()->getAttributes().getParamAttributes(
1397     A->getArgNo()+1);
1398   return (LLVMAttribute)attr;
1399 }
1400   
1401
1402 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1403   unwrap<Argument>(Arg)->addAttr(
1404           Attribute::constructAlignmentFromInt(align));
1405 }
1406
1407 /*--.. Operations on basic blocks ..........................................--*/
1408
1409 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1410   return wrap(static_cast<Value*>(unwrap(BB)));
1411 }
1412
1413 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1414   return isa<BasicBlock>(unwrap(Val));
1415 }
1416
1417 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1418   return wrap(unwrap<BasicBlock>(Val));
1419 }
1420
1421 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1422   return wrap(unwrap(BB)->getParent());
1423 }
1424
1425 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1426   return wrap(unwrap(BB)->getTerminator());
1427 }
1428
1429 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1430   return unwrap<Function>(FnRef)->size();
1431 }
1432
1433 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1434   Function *Fn = unwrap<Function>(FnRef);
1435   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1436     *BasicBlocksRefs++ = wrap(I);
1437 }
1438
1439 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1440   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1441 }
1442
1443 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1444   Function *Func = unwrap<Function>(Fn);
1445   Function::iterator I = Func->begin();
1446   if (I == Func->end())
1447     return 0;
1448   return wrap(I);
1449 }
1450
1451 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1452   Function *Func = unwrap<Function>(Fn);
1453   Function::iterator I = Func->end();
1454   if (I == Func->begin())
1455     return 0;
1456   return wrap(--I);
1457 }
1458
1459 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1460   BasicBlock *Block = unwrap(BB);
1461   Function::iterator I = Block;
1462   if (++I == Block->getParent()->end())
1463     return 0;
1464   return wrap(I);
1465 }
1466
1467 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1468   BasicBlock *Block = unwrap(BB);
1469   Function::iterator I = Block;
1470   if (I == Block->getParent()->begin())
1471     return 0;
1472   return wrap(--I);
1473 }
1474
1475 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1476                                                 LLVMValueRef FnRef,
1477                                                 const char *Name) {
1478   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1479 }
1480
1481 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1482   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1483 }
1484
1485 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1486                                                 LLVMBasicBlockRef BBRef,
1487                                                 const char *Name) {
1488   BasicBlock *BB = unwrap(BBRef);
1489   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1490 }
1491
1492 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1493                                        const char *Name) {
1494   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1495 }
1496
1497 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1498   unwrap(BBRef)->eraseFromParent();
1499 }
1500
1501 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1502   unwrap(BBRef)->removeFromParent();
1503 }
1504
1505 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1506   unwrap(BB)->moveBefore(unwrap(MovePos));
1507 }
1508
1509 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1510   unwrap(BB)->moveAfter(unwrap(MovePos));
1511 }
1512
1513 /*--.. Operations on instructions ..........................................--*/
1514
1515 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1516   return wrap(unwrap<Instruction>(Inst)->getParent());
1517 }
1518
1519 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1520   BasicBlock *Block = unwrap(BB);
1521   BasicBlock::iterator I = Block->begin();
1522   if (I == Block->end())
1523     return 0;
1524   return wrap(I);
1525 }
1526
1527 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1528   BasicBlock *Block = unwrap(BB);
1529   BasicBlock::iterator I = Block->end();
1530   if (I == Block->begin())
1531     return 0;
1532   return wrap(--I);
1533 }
1534
1535 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1536   Instruction *Instr = unwrap<Instruction>(Inst);
1537   BasicBlock::iterator I = Instr;
1538   if (++I == Instr->getParent()->end())
1539     return 0;
1540   return wrap(I);
1541 }
1542
1543 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1544   Instruction *Instr = unwrap<Instruction>(Inst);
1545   BasicBlock::iterator I = Instr;
1546   if (I == Instr->getParent()->begin())
1547     return 0;
1548   return wrap(--I);
1549 }
1550
1551 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1552   unwrap<Instruction>(Inst)->eraseFromParent();
1553 }
1554
1555 /*--.. Call and invoke instructions ........................................--*/
1556
1557 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1558   Value *V = unwrap(Instr);
1559   if (CallInst *CI = dyn_cast<CallInst>(V))
1560     return CI->getCallingConv();
1561   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1562     return II->getCallingConv();
1563   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1564   return 0;
1565 }
1566
1567 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1568   Value *V = unwrap(Instr);
1569   if (CallInst *CI = dyn_cast<CallInst>(V))
1570     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1571   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1572     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1573   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1574 }
1575
1576 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1577                            LLVMAttribute PA) {
1578   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1579   Call.setAttributes(
1580     Call.getAttributes().addAttr(index, PA));
1581 }
1582
1583 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1584                               LLVMAttribute PA) {
1585   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1586   Call.setAttributes(
1587     Call.getAttributes().removeAttr(index, PA));
1588 }
1589
1590 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1591                                 unsigned align) {
1592   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1593   Call.setAttributes(
1594     Call.getAttributes().addAttr(index, 
1595         Attribute::constructAlignmentFromInt(align)));
1596 }
1597
1598 /*--.. Operations on call instructions (only) ..............................--*/
1599
1600 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1601   return unwrap<CallInst>(Call)->isTailCall();
1602 }
1603
1604 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1605   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1606 }
1607
1608 /*--.. Operations on switch instructions (only) ............................--*/
1609
1610 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1611   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1612 }
1613
1614 /*--.. Operations on phi nodes .............................................--*/
1615
1616 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1617                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1618   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1619   for (unsigned I = 0; I != Count; ++I)
1620     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1621 }
1622
1623 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1624   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1625 }
1626
1627 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1628   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1629 }
1630
1631 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1632   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1633 }
1634
1635
1636 /*===-- Instruction builders ----------------------------------------------===*/
1637
1638 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1639   return wrap(new IRBuilder<>(*unwrap(C)));
1640 }
1641
1642 LLVMBuilderRef LLVMCreateBuilder(void) {
1643   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1644 }
1645
1646 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1647                          LLVMValueRef Instr) {
1648   BasicBlock *BB = unwrap(Block);
1649   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1650   unwrap(Builder)->SetInsertPoint(BB, I);
1651 }
1652
1653 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1654   Instruction *I = unwrap<Instruction>(Instr);
1655   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1656 }
1657
1658 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1659   BasicBlock *BB = unwrap(Block);
1660   unwrap(Builder)->SetInsertPoint(BB);
1661 }
1662
1663 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1664    return wrap(unwrap(Builder)->GetInsertBlock());
1665 }
1666
1667 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1668   unwrap(Builder)->ClearInsertionPoint();
1669 }
1670
1671 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1672   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1673 }
1674
1675 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1676                                    const char *Name) {
1677   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1678 }
1679
1680 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1681   delete unwrap(Builder);
1682 }
1683
1684 /*--.. Metadata builders ...................................................--*/
1685
1686 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1687   MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1688   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1689 }
1690
1691 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1692   return wrap(unwrap(Builder)->getCurrentDebugLocation()
1693               .getAsMDNode(unwrap(Builder)->getContext()));
1694 }
1695
1696 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1697   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1698 }
1699
1700
1701 /*--.. Instruction builders ................................................--*/
1702
1703 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1704   return wrap(unwrap(B)->CreateRetVoid());
1705 }
1706
1707 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1708   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1709 }
1710
1711 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1712                                    unsigned N) {
1713   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1714 }
1715
1716 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1717   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1718 }
1719
1720 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1721                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1722   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1723 }
1724
1725 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1726                              LLVMBasicBlockRef Else, unsigned NumCases) {
1727   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1728 }
1729
1730 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1731                                  unsigned NumDests) {
1732   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1733 }
1734
1735 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1736                              LLVMValueRef *Args, unsigned NumArgs,
1737                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1738                              const char *Name) {
1739   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1740                                       makeArrayRef(unwrap(Args), NumArgs),
1741                                       Name));
1742 }
1743
1744 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1745                                  LLVMValueRef PersFn, unsigned NumClauses,
1746                                  const char *Name) {
1747   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1748                                           cast<Function>(unwrap(PersFn)),
1749                                           NumClauses, Name));
1750 }
1751
1752 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1753   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1754 }
1755
1756 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1757   return wrap(unwrap(B)->CreateUnreachable());
1758 }
1759
1760 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1761                  LLVMBasicBlockRef Dest) {
1762   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1763 }
1764
1765 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1766   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1767 }
1768
1769 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1770   unwrap<LandingPadInst>(LandingPad)->
1771     addClause(cast<Constant>(unwrap(ClauseVal)));
1772 }
1773
1774 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1775   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1776 }
1777
1778 /*--.. Arithmetic ..........................................................--*/
1779
1780 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1781                           const char *Name) {
1782   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1783 }
1784
1785 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1786                           const char *Name) {
1787   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1788 }
1789
1790 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1791                           const char *Name) {
1792   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1793 }
1794
1795 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1796                           const char *Name) {
1797   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1798 }
1799
1800 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1801                           const char *Name) {
1802   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1803 }
1804
1805 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1806                           const char *Name) {
1807   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1808 }
1809
1810 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1811                           const char *Name) {
1812   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1813 }
1814
1815 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1816                           const char *Name) {
1817   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1818 }
1819
1820 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1821                           const char *Name) {
1822   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1823 }
1824
1825 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1826                           const char *Name) {
1827   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1828 }
1829
1830 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1831                           const char *Name) {
1832   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1833 }
1834
1835 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1836                           const char *Name) {
1837   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1838 }
1839
1840 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1841                            const char *Name) {
1842   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1843 }
1844
1845 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1846                            const char *Name) {
1847   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1848 }
1849
1850 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1851                                 LLVMValueRef RHS, const char *Name) {
1852   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1853 }
1854
1855 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1856                            const char *Name) {
1857   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1858 }
1859
1860 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1861                            const char *Name) {
1862   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1863 }
1864
1865 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1866                            const char *Name) {
1867   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1868 }
1869
1870 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1871                            const char *Name) {
1872   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1873 }
1874
1875 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1876                           const char *Name) {
1877   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1878 }
1879
1880 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1881                            const char *Name) {
1882   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1883 }
1884
1885 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1886                            const char *Name) {
1887   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1888 }
1889
1890 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1891                           const char *Name) {
1892   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1893 }
1894
1895 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1896                          const char *Name) {
1897   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1898 }
1899
1900 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1901                           const char *Name) {
1902   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1903 }
1904
1905 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1906                             LLVMValueRef LHS, LLVMValueRef RHS,
1907                             const char *Name) {
1908   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1909                                      unwrap(RHS), Name));
1910 }
1911
1912 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1913   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1914 }
1915
1916 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1917                              const char *Name) {
1918   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1919 }
1920
1921 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1922                              const char *Name) {
1923   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1924 }
1925
1926 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1927   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1928 }
1929
1930 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1931   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1932 }
1933
1934 /*--.. Memory ..............................................................--*/
1935
1936 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1937                              const char *Name) {
1938   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1939   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1940   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1941   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1942                                                ITy, unwrap(Ty), AllocSize, 
1943                                                0, 0, "");
1944   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1945 }
1946
1947 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1948                                   LLVMValueRef Val, const char *Name) {
1949   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1950   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1951   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1952   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1953                                                ITy, unwrap(Ty), AllocSize, 
1954                                                unwrap(Val), 0, "");
1955   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1956 }
1957
1958 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1959                              const char *Name) {
1960   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1961 }
1962
1963 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1964                                   LLVMValueRef Val, const char *Name) {
1965   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1966 }
1967
1968 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1969   return wrap(unwrap(B)->Insert(
1970      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1971 }
1972
1973
1974 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1975                            const char *Name) {
1976   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1977 }
1978
1979 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1980                             LLVMValueRef PointerVal) {
1981   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1982 }
1983
1984 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1985                           LLVMValueRef *Indices, unsigned NumIndices,
1986                           const char *Name) {
1987   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
1988   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
1989 }
1990
1991 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1992                                   LLVMValueRef *Indices, unsigned NumIndices,
1993                                   const char *Name) {
1994   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
1995   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
1996 }
1997
1998 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1999                                 unsigned Idx, const char *Name) {
2000   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2001 }
2002
2003 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2004                                    const char *Name) {
2005   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2006 }
2007
2008 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2009                                       const char *Name) {
2010   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2011 }
2012
2013 /*--.. Casts ...............................................................--*/
2014
2015 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2016                             LLVMTypeRef DestTy, const char *Name) {
2017   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2018 }
2019
2020 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2021                            LLVMTypeRef DestTy, const char *Name) {
2022   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2023 }
2024
2025 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2026                            LLVMTypeRef DestTy, const char *Name) {
2027   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2028 }
2029
2030 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2031                              LLVMTypeRef DestTy, const char *Name) {
2032   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2033 }
2034
2035 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2036                              LLVMTypeRef DestTy, const char *Name) {
2037   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2038 }
2039
2040 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2041                              LLVMTypeRef DestTy, const char *Name) {
2042   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2043 }
2044
2045 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2046                              LLVMTypeRef DestTy, const char *Name) {
2047   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2048 }
2049
2050 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2051                               LLVMTypeRef DestTy, const char *Name) {
2052   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2053 }
2054
2055 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2056                             LLVMTypeRef DestTy, const char *Name) {
2057   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2058 }
2059
2060 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2061                                LLVMTypeRef DestTy, const char *Name) {
2062   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2063 }
2064
2065 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2066                                LLVMTypeRef DestTy, const char *Name) {
2067   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2068 }
2069
2070 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2071                               LLVMTypeRef DestTy, const char *Name) {
2072   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2073 }
2074
2075 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2076                                     LLVMTypeRef DestTy, const char *Name) {
2077   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2078                                              Name));
2079 }
2080
2081 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2082                                     LLVMTypeRef DestTy, const char *Name) {
2083   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2084                                              Name));
2085 }
2086
2087 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2088                                      LLVMTypeRef DestTy, const char *Name) {
2089   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2090                                               Name));
2091 }
2092
2093 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2094                            LLVMTypeRef DestTy, const char *Name) {
2095   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2096                                     unwrap(DestTy), Name));
2097 }
2098
2099 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2100                                   LLVMTypeRef DestTy, const char *Name) {
2101   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2102 }
2103
2104 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2105                               LLVMTypeRef DestTy, const char *Name) {
2106   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2107                                        /*isSigned*/true, Name));
2108 }
2109
2110 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2111                              LLVMTypeRef DestTy, const char *Name) {
2112   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2113 }
2114
2115 /*--.. Comparisons .........................................................--*/
2116
2117 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2118                            LLVMValueRef LHS, LLVMValueRef RHS,
2119                            const char *Name) {
2120   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2121                                     unwrap(LHS), unwrap(RHS), Name));
2122 }
2123
2124 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2125                            LLVMValueRef LHS, LLVMValueRef RHS,
2126                            const char *Name) {
2127   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2128                                     unwrap(LHS), unwrap(RHS), Name));
2129 }
2130
2131 /*--.. Miscellaneous instructions ..........................................--*/
2132
2133 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2134   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2135 }
2136
2137 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2138                            LLVMValueRef *Args, unsigned NumArgs,
2139                            const char *Name) {
2140   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2141                                     makeArrayRef(unwrap(Args), NumArgs),
2142                                     Name));
2143 }
2144
2145 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2146                              LLVMValueRef Then, LLVMValueRef Else,
2147                              const char *Name) {
2148   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2149                                       Name));
2150 }
2151
2152 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2153                             LLVMTypeRef Ty, const char *Name) {
2154   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2155 }
2156
2157 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2158                                       LLVMValueRef Index, const char *Name) {
2159   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2160                                               Name));
2161 }
2162
2163 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2164                                     LLVMValueRef EltVal, LLVMValueRef Index,
2165                                     const char *Name) {
2166   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2167                                              unwrap(Index), Name));
2168 }
2169
2170 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2171                                     LLVMValueRef V2, LLVMValueRef Mask,
2172                                     const char *Name) {
2173   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2174                                              unwrap(Mask), Name));
2175 }
2176
2177 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2178                                    unsigned Index, const char *Name) {
2179   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2180 }
2181
2182 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2183                                   LLVMValueRef EltVal, unsigned Index,
2184                                   const char *Name) {
2185   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2186                                            Index, Name));
2187 }
2188
2189 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2190                              const char *Name) {
2191   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2192 }
2193
2194 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2195                                 const char *Name) {
2196   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2197 }
2198
2199 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2200                               LLVMValueRef RHS, const char *Name) {
2201   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2202 }
2203
2204
2205 /*===-- Module providers --------------------------------------------------===*/
2206
2207 LLVMModuleProviderRef
2208 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2209   return reinterpret_cast<LLVMModuleProviderRef>(M);
2210 }
2211
2212 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2213   delete unwrap(MP);
2214 }
2215
2216
2217 /*===-- Memory buffers ----------------------------------------------------===*/
2218
2219 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2220     const char *Path,
2221     LLVMMemoryBufferRef *OutMemBuf,
2222     char **OutMessage) {
2223
2224   OwningPtr<MemoryBuffer> MB;
2225   error_code ec;
2226   if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2227     *OutMemBuf = wrap(MB.take());
2228     return 0;
2229   }
2230
2231   *OutMessage = strdup(ec.message().c_str());
2232   return 1;
2233 }
2234
2235 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2236                                          char **OutMessage) {
2237   OwningPtr<MemoryBuffer> MB;
2238   error_code ec;
2239   if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2240     *OutMemBuf = wrap(MB.take());
2241     return 0;
2242   }
2243
2244   *OutMessage = strdup(ec.message().c_str());
2245   return 1;
2246 }
2247
2248 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2249   delete unwrap(MemBuf);
2250 }
2251
2252 /*===-- Pass Registry -----------------------------------------------------===*/
2253
2254 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2255   return wrap(PassRegistry::getPassRegistry());
2256 }
2257
2258 /*===-- Pass Manager ------------------------------------------------------===*/
2259
2260 LLVMPassManagerRef LLVMCreatePassManager() {
2261   return wrap(new PassManager());
2262 }
2263
2264 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2265   return wrap(new FunctionPassManager(unwrap(M)));
2266 }
2267
2268 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2269   return LLVMCreateFunctionPassManagerForModule(
2270                                             reinterpret_cast<LLVMModuleRef>(P));
2271 }
2272
2273 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2274   return unwrap<PassManager>(PM)->run(*unwrap(M));
2275 }
2276
2277 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2278   return unwrap<FunctionPassManager>(FPM)->doInitialization();
2279 }
2280
2281 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2282   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2283 }
2284
2285 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2286   return unwrap<FunctionPassManager>(FPM)->doFinalization();
2287 }
2288
2289 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2290   delete unwrap(PM);
2291 }