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