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