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