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