add binding to read icmp predicate
[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 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1556     if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1557         return (LLVMIntPredicate)I->getPredicate();
1558     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1559         if (CE->getOpcode() == Instruction::ICmp)
1560             return (LLVMIntPredicate)CE->getPredicate();
1561     return (LLVMIntPredicate)0;
1562 }
1563
1564 /*--.. Call and invoke instructions ........................................--*/
1565
1566 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1567   Value *V = unwrap(Instr);
1568   if (CallInst *CI = dyn_cast<CallInst>(V))
1569     return CI->getCallingConv();
1570   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1571     return II->getCallingConv();
1572   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1573   return 0;
1574 }
1575
1576 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1577   Value *V = unwrap(Instr);
1578   if (CallInst *CI = dyn_cast<CallInst>(V))
1579     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1580   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1581     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1582   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1583 }
1584
1585 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 
1586                            LLVMAttribute PA) {
1587   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1588   Call.setAttributes(
1589     Call.getAttributes().addAttr(index, PA));
1590 }
1591
1592 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 
1593                               LLVMAttribute PA) {
1594   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1595   Call.setAttributes(
1596     Call.getAttributes().removeAttr(index, PA));
1597 }
1598
1599 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 
1600                                 unsigned align) {
1601   CallSite Call = CallSite(unwrap<Instruction>(Instr));
1602   Call.setAttributes(
1603     Call.getAttributes().addAttr(index, 
1604         Attribute::constructAlignmentFromInt(align)));
1605 }
1606
1607 /*--.. Operations on call instructions (only) ..............................--*/
1608
1609 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1610   return unwrap<CallInst>(Call)->isTailCall();
1611 }
1612
1613 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1614   unwrap<CallInst>(Call)->setTailCall(isTailCall);
1615 }
1616
1617 /*--.. Operations on switch instructions (only) ............................--*/
1618
1619 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1620   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1621 }
1622
1623 /*--.. Operations on phi nodes .............................................--*/
1624
1625 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1626                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1627   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1628   for (unsigned I = 0; I != Count; ++I)
1629     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1630 }
1631
1632 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1633   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1634 }
1635
1636 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1637   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1638 }
1639
1640 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1641   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1642 }
1643
1644
1645 /*===-- Instruction builders ----------------------------------------------===*/
1646
1647 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1648   return wrap(new IRBuilder<>(*unwrap(C)));
1649 }
1650
1651 LLVMBuilderRef LLVMCreateBuilder(void) {
1652   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1653 }
1654
1655 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1656                          LLVMValueRef Instr) {
1657   BasicBlock *BB = unwrap(Block);
1658   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1659   unwrap(Builder)->SetInsertPoint(BB, I);
1660 }
1661
1662 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1663   Instruction *I = unwrap<Instruction>(Instr);
1664   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1665 }
1666
1667 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1668   BasicBlock *BB = unwrap(Block);
1669   unwrap(Builder)->SetInsertPoint(BB);
1670 }
1671
1672 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1673    return wrap(unwrap(Builder)->GetInsertBlock());
1674 }
1675
1676 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1677   unwrap(Builder)->ClearInsertionPoint();
1678 }
1679
1680 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1681   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1682 }
1683
1684 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1685                                    const char *Name) {
1686   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1687 }
1688
1689 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1690   delete unwrap(Builder);
1691 }
1692
1693 /*--.. Metadata builders ...................................................--*/
1694
1695 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1696   MDNode *Loc = L ? unwrap<MDNode>(L) : NULL;
1697   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1698 }
1699
1700 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1701   return wrap(unwrap(Builder)->getCurrentDebugLocation()
1702               .getAsMDNode(unwrap(Builder)->getContext()));
1703 }
1704
1705 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1706   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
1707 }
1708
1709
1710 /*--.. Instruction builders ................................................--*/
1711
1712 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
1713   return wrap(unwrap(B)->CreateRetVoid());
1714 }
1715
1716 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
1717   return wrap(unwrap(B)->CreateRet(unwrap(V)));
1718 }
1719
1720 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
1721                                    unsigned N) {
1722   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
1723 }
1724
1725 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
1726   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
1727 }
1728
1729 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
1730                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
1731   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
1732 }
1733
1734 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
1735                              LLVMBasicBlockRef Else, unsigned NumCases) {
1736   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
1737 }
1738
1739 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
1740                                  unsigned NumDests) {
1741   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
1742 }
1743
1744 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
1745                              LLVMValueRef *Args, unsigned NumArgs,
1746                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
1747                              const char *Name) {
1748   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
1749                                       makeArrayRef(unwrap(Args), NumArgs),
1750                                       Name));
1751 }
1752
1753 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1754                                  LLVMValueRef PersFn, unsigned NumClauses,
1755                                  const char *Name) {
1756   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1757                                           cast<Function>(unwrap(PersFn)),
1758                                           NumClauses, Name));
1759 }
1760
1761 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
1762   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
1763 }
1764
1765 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
1766   return wrap(unwrap(B)->CreateUnreachable());
1767 }
1768
1769 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
1770                  LLVMBasicBlockRef Dest) {
1771   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
1772 }
1773
1774 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
1775   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
1776 }
1777
1778 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1779   unwrap<LandingPadInst>(LandingPad)->
1780     addClause(cast<Constant>(unwrap(ClauseVal)));
1781 }
1782
1783 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1784   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
1785 }
1786
1787 /*--.. Arithmetic ..........................................................--*/
1788
1789 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1790                           const char *Name) {
1791   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
1792 }
1793
1794 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1795                           const char *Name) {
1796   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
1797 }
1798
1799 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1800                           const char *Name) {
1801   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
1802 }
1803
1804 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1805                           const char *Name) {
1806   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
1807 }
1808
1809 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1810                           const char *Name) {
1811   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
1812 }
1813
1814 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1815                           const char *Name) {
1816   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
1817 }
1818
1819 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1820                           const char *Name) {
1821   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
1822 }
1823
1824 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1825                           const char *Name) {
1826   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
1827 }
1828
1829 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1830                           const char *Name) {
1831   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
1832 }
1833
1834 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1835                           const char *Name) {
1836   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
1837 }
1838
1839 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1840                           const char *Name) {
1841   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
1842 }
1843
1844 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1845                           const char *Name) {
1846   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
1847 }
1848
1849 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1850                            const char *Name) {
1851   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
1852 }
1853
1854 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1855                            const char *Name) {
1856   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
1857 }
1858
1859 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
1860                                 LLVMValueRef RHS, const char *Name) {
1861   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
1862 }
1863
1864 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1865                            const char *Name) {
1866   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
1867 }
1868
1869 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1870                            const char *Name) {
1871   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
1872 }
1873
1874 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1875                            const char *Name) {
1876   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
1877 }
1878
1879 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1880                            const char *Name) {
1881   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
1882 }
1883
1884 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1885                           const char *Name) {
1886   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
1887 }
1888
1889 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1890                            const char *Name) {
1891   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
1892 }
1893
1894 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1895                            const char *Name) {
1896   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
1897 }
1898
1899 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1900                           const char *Name) {
1901   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
1902 }
1903
1904 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1905                          const char *Name) {
1906   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
1907 }
1908
1909 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
1910                           const char *Name) {
1911   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
1912 }
1913
1914 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
1915                             LLVMValueRef LHS, LLVMValueRef RHS,
1916                             const char *Name) {
1917   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(Op), unwrap(LHS),
1918                                      unwrap(RHS), Name));
1919 }
1920
1921 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1922   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
1923 }
1924
1925 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
1926                              const char *Name) {
1927   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
1928 }
1929
1930 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
1931                              const char *Name) {
1932   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
1933 }
1934
1935 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1936   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
1937 }
1938
1939 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
1940   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
1941 }
1942
1943 /*--.. Memory ..............................................................--*/
1944
1945 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1946                              const char *Name) {
1947   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1948   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1949   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1950   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1951                                                ITy, unwrap(Ty), AllocSize, 
1952                                                0, 0, "");
1953   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1954 }
1955
1956 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
1957                                   LLVMValueRef Val, const char *Name) {
1958   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
1959   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
1960   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
1961   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 
1962                                                ITy, unwrap(Ty), AllocSize, 
1963                                                unwrap(Val), 0, "");
1964   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
1965 }
1966
1967 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1968                              const char *Name) {
1969   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), 0, Name));
1970 }
1971
1972 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
1973                                   LLVMValueRef Val, const char *Name) {
1974   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
1975 }
1976
1977 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
1978   return wrap(unwrap(B)->Insert(
1979      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
1980 }
1981
1982
1983 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
1984                            const char *Name) {
1985   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
1986 }
1987
1988 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 
1989                             LLVMValueRef PointerVal) {
1990   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
1991 }
1992
1993 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
1994                           LLVMValueRef *Indices, unsigned NumIndices,
1995                           const char *Name) {
1996   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
1997   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
1998 }
1999
2000 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2001                                   LLVMValueRef *Indices, unsigned NumIndices,
2002                                   const char *Name) {
2003   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2004   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2005 }
2006
2007 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2008                                 unsigned Idx, const char *Name) {
2009   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2010 }
2011
2012 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2013                                    const char *Name) {
2014   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2015 }
2016
2017 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2018                                       const char *Name) {
2019   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2020 }
2021
2022 /*--.. Casts ...............................................................--*/
2023
2024 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2025                             LLVMTypeRef DestTy, const char *Name) {
2026   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2027 }
2028
2029 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2030                            LLVMTypeRef DestTy, const char *Name) {
2031   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2032 }
2033
2034 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2035                            LLVMTypeRef DestTy, const char *Name) {
2036   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2037 }
2038
2039 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2040                              LLVMTypeRef DestTy, const char *Name) {
2041   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2042 }
2043
2044 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2045                              LLVMTypeRef DestTy, const char *Name) {
2046   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2047 }
2048
2049 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2050                              LLVMTypeRef DestTy, const char *Name) {
2051   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2052 }
2053
2054 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2055                              LLVMTypeRef DestTy, const char *Name) {
2056   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2057 }
2058
2059 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2060                               LLVMTypeRef DestTy, const char *Name) {
2061   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2062 }
2063
2064 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2065                             LLVMTypeRef DestTy, const char *Name) {
2066   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2067 }
2068
2069 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2070                                LLVMTypeRef DestTy, const char *Name) {
2071   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2072 }
2073
2074 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2075                                LLVMTypeRef DestTy, const char *Name) {
2076   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2077 }
2078
2079 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2080                               LLVMTypeRef DestTy, const char *Name) {
2081   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2082 }
2083
2084 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2085                                     LLVMTypeRef DestTy, const char *Name) {
2086   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2087                                              Name));
2088 }
2089
2090 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2091                                     LLVMTypeRef DestTy, const char *Name) {
2092   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2093                                              Name));
2094 }
2095
2096 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2097                                      LLVMTypeRef DestTy, const char *Name) {
2098   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2099                                               Name));
2100 }
2101
2102 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2103                            LLVMTypeRef DestTy, const char *Name) {
2104   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(Op), unwrap(Val),
2105                                     unwrap(DestTy), Name));
2106 }
2107
2108 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2109                                   LLVMTypeRef DestTy, const char *Name) {
2110   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2111 }
2112
2113 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2114                               LLVMTypeRef DestTy, const char *Name) {
2115   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2116                                        /*isSigned*/true, Name));
2117 }
2118
2119 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2120                              LLVMTypeRef DestTy, const char *Name) {
2121   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2122 }
2123
2124 /*--.. Comparisons .........................................................--*/
2125
2126 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2127                            LLVMValueRef LHS, LLVMValueRef RHS,
2128                            const char *Name) {
2129   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2130                                     unwrap(LHS), unwrap(RHS), Name));
2131 }
2132
2133 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2134                            LLVMValueRef LHS, LLVMValueRef RHS,
2135                            const char *Name) {
2136   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2137                                     unwrap(LHS), unwrap(RHS), Name));
2138 }
2139
2140 /*--.. Miscellaneous instructions ..........................................--*/
2141
2142 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2143   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2144 }
2145
2146 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2147                            LLVMValueRef *Args, unsigned NumArgs,
2148                            const char *Name) {
2149   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2150                                     makeArrayRef(unwrap(Args), NumArgs),
2151                                     Name));
2152 }
2153
2154 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2155                              LLVMValueRef Then, LLVMValueRef Else,
2156                              const char *Name) {
2157   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2158                                       Name));
2159 }
2160
2161 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2162                             LLVMTypeRef Ty, const char *Name) {
2163   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2164 }
2165
2166 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2167                                       LLVMValueRef Index, const char *Name) {
2168   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2169                                               Name));
2170 }
2171
2172 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2173                                     LLVMValueRef EltVal, LLVMValueRef Index,
2174                                     const char *Name) {
2175   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2176                                              unwrap(Index), Name));
2177 }
2178
2179 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2180                                     LLVMValueRef V2, LLVMValueRef Mask,
2181                                     const char *Name) {
2182   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2183                                              unwrap(Mask), Name));
2184 }
2185
2186 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2187                                    unsigned Index, const char *Name) {
2188   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2189 }
2190
2191 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2192                                   LLVMValueRef EltVal, unsigned Index,
2193                                   const char *Name) {
2194   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2195                                            Index, Name));
2196 }
2197
2198 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2199                              const char *Name) {
2200   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2201 }
2202
2203 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2204                                 const char *Name) {
2205   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2206 }
2207
2208 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2209                               LLVMValueRef RHS, const char *Name) {
2210   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2211 }
2212
2213
2214 /*===-- Module providers --------------------------------------------------===*/
2215
2216 LLVMModuleProviderRef
2217 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2218   return reinterpret_cast<LLVMModuleProviderRef>(M);
2219 }
2220
2221 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2222   delete unwrap(MP);
2223 }
2224
2225
2226 /*===-- Memory buffers ----------------------------------------------------===*/
2227
2228 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2229     const char *Path,
2230     LLVMMemoryBufferRef *OutMemBuf,
2231     char **OutMessage) {
2232
2233   OwningPtr<MemoryBuffer> MB;
2234   error_code ec;
2235   if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2236     *OutMemBuf = wrap(MB.take());
2237     return 0;
2238   }
2239
2240   *OutMessage = strdup(ec.message().c_str());
2241   return 1;
2242 }
2243
2244 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2245                                          char **OutMessage) {
2246   OwningPtr<MemoryBuffer> MB;
2247   error_code ec;
2248   if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2249     *OutMemBuf = wrap(MB.take());
2250     return 0;
2251   }
2252
2253   *OutMessage = strdup(ec.message().c_str());
2254   return 1;
2255 }
2256
2257 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2258   delete unwrap(MemBuf);
2259 }
2260
2261 /*===-- Pass Registry -----------------------------------------------------===*/
2262
2263 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2264   return wrap(PassRegistry::getPassRegistry());
2265 }
2266
2267 /*===-- Pass Manager ------------------------------------------------------===*/
2268
2269 LLVMPassManagerRef LLVMCreatePassManager() {
2270   return wrap(new PassManager());
2271 }
2272
2273 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2274   return wrap(new FunctionPassManager(unwrap(M)));
2275 }
2276
2277 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2278   return LLVMCreateFunctionPassManagerForModule(
2279                                             reinterpret_cast<LLVMModuleRef>(P));
2280 }
2281
2282 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2283   return unwrap<PassManager>(PM)->run(*unwrap(M));
2284 }
2285
2286 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2287   return unwrap<FunctionPassManager>(FPM)->doInitialization();
2288 }
2289
2290 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2291   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2292 }
2293
2294 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2295   return unwrap<FunctionPassManager>(FPM)->doFinalization();
2296 }
2297
2298 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2299   delete unwrap(PM);
2300 }