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