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