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