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