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