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