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