LLVM API Documentation

Core.cpp
Go to the documentation of this file.
00001 //===-- Core.cpp ----------------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the common infrastructure (including the C bindings)
00011 // for libLLVMCore.a, which implements the LLVM intermediate representation.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm-c/Core.h"
00016 #include "llvm/Bitcode/ReaderWriter.h"
00017 #include "llvm/IR/Attributes.h"
00018 #include "llvm/IR/CallSite.h"
00019 #include "llvm/IR/Constants.h"
00020 #include "llvm/IR/DiagnosticInfo.h"
00021 #include "llvm/IR/DiagnosticPrinter.h"
00022 #include "llvm/IR/DerivedTypes.h"
00023 #include "llvm/IR/GlobalAlias.h"
00024 #include "llvm/IR/GlobalVariable.h"
00025 #include "llvm/IR/IRBuilder.h"
00026 #include "llvm/IR/InlineAsm.h"
00027 #include "llvm/IR/IntrinsicInst.h"
00028 #include "llvm/IR/LLVMContext.h"
00029 #include "llvm/IR/Module.h"
00030 #include "llvm/PassManager.h"
00031 #include "llvm/Support/Debug.h"
00032 #include "llvm/Support/ErrorHandling.h"
00033 #include "llvm/Support/ManagedStatic.h"
00034 #include "llvm/Support/MemoryBuffer.h"
00035 #include "llvm/Support/Threading.h"
00036 #include "llvm/Support/raw_ostream.h"
00037 #include "llvm/Support/system_error.h"
00038 #include <cassert>
00039 #include <cstdlib>
00040 #include <cstring>
00041 
00042 using namespace llvm;
00043 
00044 #define DEBUG_TYPE "ir"
00045 
00046 void llvm::initializeCore(PassRegistry &Registry) {
00047   initializeDominatorTreeWrapperPassPass(Registry);
00048   initializePrintModulePassWrapperPass(Registry);
00049   initializePrintFunctionPassWrapperPass(Registry);
00050   initializePrintBasicBlockPassPass(Registry);
00051   initializeVerifierLegacyPassPass(Registry);
00052 }
00053 
00054 void LLVMInitializeCore(LLVMPassRegistryRef R) {
00055   initializeCore(*unwrap(R));
00056 }
00057 
00058 void LLVMShutdown() {
00059   llvm_shutdown();
00060 }
00061 
00062 /*===-- Error handling ----------------------------------------------------===*/
00063 
00064 char *LLVMCreateMessage(const char *Message) {
00065   return strdup(Message);
00066 }
00067 
00068 void LLVMDisposeMessage(char *Message) {
00069   free(Message);
00070 }
00071 
00072 
00073 /*===-- Operations on contexts --------------------------------------------===*/
00074 
00075 LLVMContextRef LLVMContextCreate() {
00076   return wrap(new LLVMContext());
00077 }
00078 
00079 LLVMContextRef LLVMGetGlobalContext() {
00080   return wrap(&getGlobalContext());
00081 }
00082 
00083 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
00084                                      LLVMDiagnosticHandler Handler,
00085                                      void *DiagnosticContext) {
00086   unwrap(C)->setDiagnosticHandler(
00087       LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(Handler),
00088       DiagnosticContext);
00089 }
00090 
00091 void LLVMContextDispose(LLVMContextRef C) {
00092   delete unwrap(C);
00093 }
00094 
00095 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
00096                                   unsigned SLen) {
00097   return unwrap(C)->getMDKindID(StringRef(Name, SLen));
00098 }
00099 
00100 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
00101   return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
00102 }
00103 
00104 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
00105   std::string MsgStorage;
00106   raw_string_ostream Stream(MsgStorage);
00107   DiagnosticPrinterRawOStream DP(Stream);
00108 
00109   unwrap(DI)->print(DP);
00110   Stream.flush();
00111 
00112   return LLVMCreateMessage(MsgStorage.c_str());
00113 }
00114 
00115 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
00116     LLVMDiagnosticSeverity severity;
00117 
00118     switch(unwrap(DI)->getSeverity()) {
00119     default:
00120       severity = LLVMDSError;
00121       break;
00122     case DS_Warning:
00123       severity = LLVMDSWarning;
00124       break;
00125     case DS_Remark:
00126       severity = LLVMDSRemark;
00127       break;
00128     case DS_Note:
00129       severity = LLVMDSNote;
00130       break;
00131     }
00132 
00133     return severity;
00134 }
00135 
00136 
00137 
00138 
00139 /*===-- Operations on modules ---------------------------------------------===*/
00140 
00141 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
00142   return wrap(new Module(ModuleID, getGlobalContext()));
00143 }
00144 
00145 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
00146                                                 LLVMContextRef C) {
00147   return wrap(new Module(ModuleID, *unwrap(C)));
00148 }
00149 
00150 void LLVMDisposeModule(LLVMModuleRef M) {
00151   delete unwrap(M);
00152 }
00153 
00154 /*--.. Data layout .........................................................--*/
00155 const char * LLVMGetDataLayout(LLVMModuleRef M) {
00156   return unwrap(M)->getDataLayoutStr().c_str();
00157 }
00158 
00159 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
00160   unwrap(M)->setDataLayout(Triple);
00161 }
00162 
00163 /*--.. Target triple .......................................................--*/
00164 const char * LLVMGetTarget(LLVMModuleRef M) {
00165   return unwrap(M)->getTargetTriple().c_str();
00166 }
00167 
00168 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
00169   unwrap(M)->setTargetTriple(Triple);
00170 }
00171 
00172 void LLVMDumpModule(LLVMModuleRef M) {
00173   unwrap(M)->dump();
00174 }
00175 
00176 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
00177                                char **ErrorMessage) {
00178   std::string error;
00179   raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
00180   if (!error.empty()) {
00181     *ErrorMessage = strdup(error.c_str());
00182     return true;
00183   }
00184 
00185   unwrap(M)->print(dest, nullptr);
00186 
00187   if (!error.empty()) {
00188     *ErrorMessage = strdup(error.c_str());
00189     return true;
00190   }
00191   dest.flush();
00192   return false;
00193 }
00194 
00195 char *LLVMPrintModuleToString(LLVMModuleRef M) {
00196   std::string buf;
00197   raw_string_ostream os(buf);
00198 
00199   unwrap(M)->print(os, nullptr);
00200   os.flush();
00201 
00202   return strdup(buf.c_str());
00203 }
00204 
00205 /*--.. Operations on inline assembler ......................................--*/
00206 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
00207   unwrap(M)->setModuleInlineAsm(StringRef(Asm));
00208 }
00209 
00210 
00211 /*--.. Operations on module contexts ......................................--*/
00212 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
00213   return wrap(&unwrap(M)->getContext());
00214 }
00215 
00216 
00217 /*===-- Operations on types -----------------------------------------------===*/
00218 
00219 /*--.. Operations on all types (mostly) ....................................--*/
00220 
00221 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
00222   switch (unwrap(Ty)->getTypeID()) {
00223   case Type::VoidTyID:
00224     return LLVMVoidTypeKind;
00225   case Type::HalfTyID:
00226     return LLVMHalfTypeKind;
00227   case Type::FloatTyID:
00228     return LLVMFloatTypeKind;
00229   case Type::DoubleTyID:
00230     return LLVMDoubleTypeKind;
00231   case Type::X86_FP80TyID:
00232     return LLVMX86_FP80TypeKind;
00233   case Type::FP128TyID:
00234     return LLVMFP128TypeKind;
00235   case Type::PPC_FP128TyID:
00236     return LLVMPPC_FP128TypeKind;
00237   case Type::LabelTyID:
00238     return LLVMLabelTypeKind;
00239   case Type::MetadataTyID:
00240     return LLVMMetadataTypeKind;
00241   case Type::IntegerTyID:
00242     return LLVMIntegerTypeKind;
00243   case Type::FunctionTyID:
00244     return LLVMFunctionTypeKind;
00245   case Type::StructTyID:
00246     return LLVMStructTypeKind;
00247   case Type::ArrayTyID:
00248     return LLVMArrayTypeKind;
00249   case Type::PointerTyID:
00250     return LLVMPointerTypeKind;
00251   case Type::VectorTyID:
00252     return LLVMVectorTypeKind;
00253   case Type::X86_MMXTyID:
00254     return LLVMX86_MMXTypeKind;
00255   }
00256   llvm_unreachable("Unhandled TypeID.");
00257 }
00258 
00259 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
00260 {
00261     return unwrap(Ty)->isSized();
00262 }
00263 
00264 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
00265   return wrap(&unwrap(Ty)->getContext());
00266 }
00267 
00268 void LLVMDumpType(LLVMTypeRef Ty) {
00269   return unwrap(Ty)->dump();
00270 }
00271 
00272 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
00273   std::string buf;
00274   raw_string_ostream os(buf);
00275 
00276   unwrap(Ty)->print(os);
00277   os.flush();
00278 
00279   return strdup(buf.c_str());
00280 }
00281 
00282 /*--.. Operations on integer types .........................................--*/
00283 
00284 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C)  {
00285   return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
00286 }
00287 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C)  {
00288   return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
00289 }
00290 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
00291   return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
00292 }
00293 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
00294   return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
00295 }
00296 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
00297   return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
00298 }
00299 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
00300   return wrap(IntegerType::get(*unwrap(C), NumBits));
00301 }
00302 
00303 LLVMTypeRef LLVMInt1Type(void)  {
00304   return LLVMInt1TypeInContext(LLVMGetGlobalContext());
00305 }
00306 LLVMTypeRef LLVMInt8Type(void)  {
00307   return LLVMInt8TypeInContext(LLVMGetGlobalContext());
00308 }
00309 LLVMTypeRef LLVMInt16Type(void) {
00310   return LLVMInt16TypeInContext(LLVMGetGlobalContext());
00311 }
00312 LLVMTypeRef LLVMInt32Type(void) {
00313   return LLVMInt32TypeInContext(LLVMGetGlobalContext());
00314 }
00315 LLVMTypeRef LLVMInt64Type(void) {
00316   return LLVMInt64TypeInContext(LLVMGetGlobalContext());
00317 }
00318 LLVMTypeRef LLVMIntType(unsigned NumBits) {
00319   return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
00320 }
00321 
00322 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
00323   return unwrap<IntegerType>(IntegerTy)->getBitWidth();
00324 }
00325 
00326 /*--.. Operations on real types ............................................--*/
00327 
00328 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
00329   return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
00330 }
00331 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
00332   return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
00333 }
00334 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
00335   return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
00336 }
00337 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
00338   return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
00339 }
00340 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
00341   return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
00342 }
00343 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
00344   return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
00345 }
00346 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
00347   return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
00348 }
00349 
00350 LLVMTypeRef LLVMHalfType(void) {
00351   return LLVMHalfTypeInContext(LLVMGetGlobalContext());
00352 }
00353 LLVMTypeRef LLVMFloatType(void) {
00354   return LLVMFloatTypeInContext(LLVMGetGlobalContext());
00355 }
00356 LLVMTypeRef LLVMDoubleType(void) {
00357   return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
00358 }
00359 LLVMTypeRef LLVMX86FP80Type(void) {
00360   return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
00361 }
00362 LLVMTypeRef LLVMFP128Type(void) {
00363   return LLVMFP128TypeInContext(LLVMGetGlobalContext());
00364 }
00365 LLVMTypeRef LLVMPPCFP128Type(void) {
00366   return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
00367 }
00368 LLVMTypeRef LLVMX86MMXType(void) {
00369   return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
00370 }
00371 
00372 /*--.. Operations on function types ........................................--*/
00373 
00374 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
00375                              LLVMTypeRef *ParamTypes, unsigned ParamCount,
00376                              LLVMBool IsVarArg) {
00377   ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
00378   return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
00379 }
00380 
00381 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
00382   return unwrap<FunctionType>(FunctionTy)->isVarArg();
00383 }
00384 
00385 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
00386   return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
00387 }
00388 
00389 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
00390   return unwrap<FunctionType>(FunctionTy)->getNumParams();
00391 }
00392 
00393 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
00394   FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
00395   for (FunctionType::param_iterator I = Ty->param_begin(),
00396                                     E = Ty->param_end(); I != E; ++I)
00397     *Dest++ = wrap(*I);
00398 }
00399 
00400 /*--.. Operations on struct types ..........................................--*/
00401 
00402 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
00403                            unsigned ElementCount, LLVMBool Packed) {
00404   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
00405   return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
00406 }
00407 
00408 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
00409                            unsigned ElementCount, LLVMBool Packed) {
00410   return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
00411                                  ElementCount, Packed);
00412 }
00413 
00414 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
00415 {
00416   return wrap(StructType::create(*unwrap(C), Name));
00417 }
00418 
00419 const char *LLVMGetStructName(LLVMTypeRef Ty)
00420 {
00421   StructType *Type = unwrap<StructType>(Ty);
00422   if (!Type->hasName())
00423     return nullptr;
00424   return Type->getName().data();
00425 }
00426 
00427 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
00428                        unsigned ElementCount, LLVMBool Packed) {
00429   ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
00430   unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
00431 }
00432 
00433 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
00434   return unwrap<StructType>(StructTy)->getNumElements();
00435 }
00436 
00437 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
00438   StructType *Ty = unwrap<StructType>(StructTy);
00439   for (StructType::element_iterator I = Ty->element_begin(),
00440                                     E = Ty->element_end(); I != E; ++I)
00441     *Dest++ = wrap(*I);
00442 }
00443 
00444 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
00445   return unwrap<StructType>(StructTy)->isPacked();
00446 }
00447 
00448 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
00449   return unwrap<StructType>(StructTy)->isOpaque();
00450 }
00451 
00452 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
00453   return wrap(unwrap(M)->getTypeByName(Name));
00454 }
00455 
00456 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
00457 
00458 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
00459   return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
00460 }
00461 
00462 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
00463   return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
00464 }
00465 
00466 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
00467   return wrap(VectorType::get(unwrap(ElementType), ElementCount));
00468 }
00469 
00470 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
00471   return wrap(unwrap<SequentialType>(Ty)->getElementType());
00472 }
00473 
00474 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
00475   return unwrap<ArrayType>(ArrayTy)->getNumElements();
00476 }
00477 
00478 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
00479   return unwrap<PointerType>(PointerTy)->getAddressSpace();
00480 }
00481 
00482 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
00483   return unwrap<VectorType>(VectorTy)->getNumElements();
00484 }
00485 
00486 /*--.. Operations on other types ...........................................--*/
00487 
00488 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C)  {
00489   return wrap(Type::getVoidTy(*unwrap(C)));
00490 }
00491 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
00492   return wrap(Type::getLabelTy(*unwrap(C)));
00493 }
00494 
00495 LLVMTypeRef LLVMVoidType(void)  {
00496   return LLVMVoidTypeInContext(LLVMGetGlobalContext());
00497 }
00498 LLVMTypeRef LLVMLabelType(void) {
00499   return LLVMLabelTypeInContext(LLVMGetGlobalContext());
00500 }
00501 
00502 /*===-- Operations on values ----------------------------------------------===*/
00503 
00504 /*--.. Operations on all values ............................................--*/
00505 
00506 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
00507   return wrap(unwrap(Val)->getType());
00508 }
00509 
00510 const char *LLVMGetValueName(LLVMValueRef Val) {
00511   return unwrap(Val)->getName().data();
00512 }
00513 
00514 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
00515   unwrap(Val)->setName(Name);
00516 }
00517 
00518 void LLVMDumpValue(LLVMValueRef Val) {
00519   unwrap(Val)->dump();
00520 }
00521 
00522 char* LLVMPrintValueToString(LLVMValueRef Val) {
00523   std::string buf;
00524   raw_string_ostream os(buf);
00525 
00526   unwrap(Val)->print(os);
00527   os.flush();
00528 
00529   return strdup(buf.c_str());
00530 }
00531 
00532 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
00533   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
00534 }
00535 
00536 int LLVMHasMetadata(LLVMValueRef Inst) {
00537   return unwrap<Instruction>(Inst)->hasMetadata();
00538 }
00539 
00540 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
00541   return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
00542 }
00543 
00544 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
00545   unwrap<Instruction>(Inst)->setMetadata(KindID,
00546                                          MD ? unwrap<MDNode>(MD) : nullptr);
00547 }
00548 
00549 /*--.. Conversion functions ................................................--*/
00550 
00551 #define LLVM_DEFINE_VALUE_CAST(name)                                       \
00552   LLVMValueRef LLVMIsA##name(LLVMValueRef Val) {                           \
00553     return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
00554   }
00555 
00556 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
00557 
00558 /*--.. Operations on Uses ..................................................--*/
00559 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
00560   Value *V = unwrap(Val);
00561   Value::use_iterator I = V->use_begin();
00562   if (I == V->use_end())
00563     return nullptr;
00564   return wrap(&*I);
00565 }
00566 
00567 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
00568   Use *Next = unwrap(U)->getNext();
00569   if (Next)
00570     return wrap(Next);
00571   return nullptr;
00572 }
00573 
00574 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
00575   return wrap(unwrap(U)->getUser());
00576 }
00577 
00578 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
00579   return wrap(unwrap(U)->get());
00580 }
00581 
00582 /*--.. Operations on Users .................................................--*/
00583 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
00584   Value *V = unwrap(Val);
00585   if (MDNode *MD = dyn_cast<MDNode>(V))
00586       return wrap(MD->getOperand(Index));
00587   return wrap(cast<User>(V)->getOperand(Index));
00588 }
00589 
00590 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
00591   unwrap<User>(Val)->setOperand(Index, unwrap(Op));
00592 }
00593 
00594 int LLVMGetNumOperands(LLVMValueRef Val) {
00595   Value *V = unwrap(Val);
00596   if (MDNode *MD = dyn_cast<MDNode>(V))
00597       return MD->getNumOperands();
00598   return cast<User>(V)->getNumOperands();
00599 }
00600 
00601 /*--.. Operations on constants of any type .................................--*/
00602 
00603 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
00604   return wrap(Constant::getNullValue(unwrap(Ty)));
00605 }
00606 
00607 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
00608   return wrap(Constant::getAllOnesValue(unwrap(Ty)));
00609 }
00610 
00611 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
00612   return wrap(UndefValue::get(unwrap(Ty)));
00613 }
00614 
00615 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
00616   return isa<Constant>(unwrap(Ty));
00617 }
00618 
00619 LLVMBool LLVMIsNull(LLVMValueRef Val) {
00620   if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
00621     return C->isNullValue();
00622   return false;
00623 }
00624 
00625 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
00626   return isa<UndefValue>(unwrap(Val));
00627 }
00628 
00629 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
00630   return
00631       wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
00632 }
00633 
00634 /*--.. Operations on metadata nodes ........................................--*/
00635 
00636 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
00637                                    unsigned SLen) {
00638   return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
00639 }
00640 
00641 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
00642   return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
00643 }
00644 
00645 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
00646                                  unsigned Count) {
00647   return wrap(MDNode::get(*unwrap(C),
00648                           makeArrayRef(unwrap<Value>(Vals, Count), Count)));
00649 }
00650 
00651 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
00652   return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
00653 }
00654 
00655 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
00656   if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
00657     *Len = S->getString().size();
00658     return S->getString().data();
00659   }
00660   *Len = 0;
00661   return nullptr;
00662 }
00663 
00664 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
00665 {
00666   return cast<MDNode>(unwrap(V))->getNumOperands();
00667 }
00668 
00669 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
00670 {
00671   const MDNode *N = cast<MDNode>(unwrap(V));
00672   const unsigned numOperands = N->getNumOperands();
00673   for (unsigned i = 0; i < numOperands; i++)
00674     Dest[i] = wrap(N->getOperand(i));
00675 }
00676 
00677 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
00678 {
00679   if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
00680     return N->getNumOperands();
00681   }
00682   return 0;
00683 }
00684 
00685 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
00686 {
00687   NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
00688   if (!N)
00689     return;
00690   for (unsigned i=0;i<N->getNumOperands();i++)
00691     Dest[i] = wrap(N->getOperand(i));
00692 }
00693 
00694 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
00695                                  LLVMValueRef Val)
00696 {
00697   NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
00698   if (!N)
00699     return;
00700   MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
00701   if (Op)
00702     N->addOperand(Op);
00703 }
00704 
00705 /*--.. Operations on scalar constants ......................................--*/
00706 
00707 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
00708                           LLVMBool SignExtend) {
00709   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
00710 }
00711 
00712 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
00713                                               unsigned NumWords,
00714                                               const uint64_t Words[]) {
00715     IntegerType *Ty = unwrap<IntegerType>(IntTy);
00716     return wrap(ConstantInt::get(Ty->getContext(),
00717                                  APInt(Ty->getBitWidth(),
00718                                        makeArrayRef(Words, NumWords))));
00719 }
00720 
00721 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
00722                                   uint8_t Radix) {
00723   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
00724                                Radix));
00725 }
00726 
00727 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
00728                                          unsigned SLen, uint8_t Radix) {
00729   return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
00730                                Radix));
00731 }
00732 
00733 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
00734   return wrap(ConstantFP::get(unwrap(RealTy), N));
00735 }
00736 
00737 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
00738   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
00739 }
00740 
00741 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
00742                                           unsigned SLen) {
00743   return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
00744 }
00745 
00746 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
00747   return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
00748 }
00749 
00750 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
00751   return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
00752 }
00753 
00754 /*--.. Operations on composite constants ...................................--*/
00755 
00756 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
00757                                       unsigned Length,
00758                                       LLVMBool DontNullTerminate) {
00759   /* Inverted the sense of AddNull because ', 0)' is a
00760      better mnemonic for null termination than ', 1)'. */
00761   return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
00762                                            DontNullTerminate == 0));
00763 }
00764 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
00765                                       LLVMValueRef *ConstantVals,
00766                                       unsigned Count, LLVMBool Packed) {
00767   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
00768   return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
00769                                       Packed != 0));
00770 }
00771 
00772 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
00773                              LLVMBool DontNullTerminate) {
00774   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
00775                                   DontNullTerminate);
00776 }
00777 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
00778                             LLVMValueRef *ConstantVals, unsigned Length) {
00779   ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
00780   return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
00781 }
00782 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
00783                              LLVMBool Packed) {
00784   return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
00785                                   Packed);
00786 }
00787 
00788 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
00789                                   LLVMValueRef *ConstantVals,
00790                                   unsigned Count) {
00791   Constant **Elements = unwrap<Constant>(ConstantVals, Count);
00792   StructType *Ty = cast<StructType>(unwrap(StructTy));
00793 
00794   return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
00795 }
00796 
00797 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
00798   return wrap(ConstantVector::get(makeArrayRef(
00799                             unwrap<Constant>(ScalarConstantVals, Size), Size)));
00800 }
00801 
00802 /*-- Opcode mapping */
00803 
00804 static LLVMOpcode map_to_llvmopcode(int opcode)
00805 {
00806     switch (opcode) {
00807       default: llvm_unreachable("Unhandled Opcode.");
00808 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
00809 #include "llvm/IR/Instruction.def"
00810 #undef HANDLE_INST
00811     }
00812 }
00813 
00814 static int map_from_llvmopcode(LLVMOpcode code)
00815 {
00816     switch (code) {
00817 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
00818 #include "llvm/IR/Instruction.def"
00819 #undef HANDLE_INST
00820     }
00821     llvm_unreachable("Unhandled Opcode.");
00822 }
00823 
00824 /*--.. Constant expressions ................................................--*/
00825 
00826 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
00827   return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
00828 }
00829 
00830 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
00831   return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
00832 }
00833 
00834 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
00835   return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
00836 }
00837 
00838 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
00839   return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
00840 }
00841 
00842 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
00843   return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
00844 }
00845 
00846 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
00847   return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
00848 }
00849 
00850 
00851 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
00852   return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
00853 }
00854 
00855 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
00856   return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
00857 }
00858 
00859 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00860   return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
00861                                    unwrap<Constant>(RHSConstant)));
00862 }
00863 
00864 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
00865                              LLVMValueRef RHSConstant) {
00866   return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
00867                                       unwrap<Constant>(RHSConstant)));
00868 }
00869 
00870 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
00871                              LLVMValueRef RHSConstant) {
00872   return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
00873                                       unwrap<Constant>(RHSConstant)));
00874 }
00875 
00876 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00877   return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
00878                                     unwrap<Constant>(RHSConstant)));
00879 }
00880 
00881 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00882   return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
00883                                    unwrap<Constant>(RHSConstant)));
00884 }
00885 
00886 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
00887                              LLVMValueRef RHSConstant) {
00888   return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
00889                                       unwrap<Constant>(RHSConstant)));
00890 }
00891 
00892 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
00893                              LLVMValueRef RHSConstant) {
00894   return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
00895                                       unwrap<Constant>(RHSConstant)));
00896 }
00897 
00898 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00899   return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
00900                                     unwrap<Constant>(RHSConstant)));
00901 }
00902 
00903 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00904   return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
00905                                    unwrap<Constant>(RHSConstant)));
00906 }
00907 
00908 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
00909                              LLVMValueRef RHSConstant) {
00910   return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
00911                                       unwrap<Constant>(RHSConstant)));
00912 }
00913 
00914 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
00915                              LLVMValueRef RHSConstant) {
00916   return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
00917                                       unwrap<Constant>(RHSConstant)));
00918 }
00919 
00920 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00921   return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
00922                                     unwrap<Constant>(RHSConstant)));
00923 }
00924 
00925 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00926   return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
00927                                     unwrap<Constant>(RHSConstant)));
00928 }
00929 
00930 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00931   return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
00932                                     unwrap<Constant>(RHSConstant)));
00933 }
00934 
00935 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
00936                                 LLVMValueRef RHSConstant) {
00937   return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
00938                                          unwrap<Constant>(RHSConstant)));
00939 }
00940 
00941 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00942   return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
00943                                     unwrap<Constant>(RHSConstant)));
00944 }
00945 
00946 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00947   return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
00948                                     unwrap<Constant>(RHSConstant)));
00949 }
00950 
00951 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00952   return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
00953                                     unwrap<Constant>(RHSConstant)));
00954 }
00955 
00956 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00957   return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
00958                                     unwrap<Constant>(RHSConstant)));
00959 }
00960 
00961 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00962   return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
00963                                    unwrap<Constant>(RHSConstant)));
00964 }
00965 
00966 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00967   return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
00968                                   unwrap<Constant>(RHSConstant)));
00969 }
00970 
00971 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00972   return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
00973                                    unwrap<Constant>(RHSConstant)));
00974 }
00975 
00976 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
00977                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00978   return wrap(ConstantExpr::getICmp(Predicate,
00979                                     unwrap<Constant>(LHSConstant),
00980                                     unwrap<Constant>(RHSConstant)));
00981 }
00982 
00983 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
00984                            LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00985   return wrap(ConstantExpr::getFCmp(Predicate,
00986                                     unwrap<Constant>(LHSConstant),
00987                                     unwrap<Constant>(RHSConstant)));
00988 }
00989 
00990 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00991   return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
00992                                    unwrap<Constant>(RHSConstant)));
00993 }
00994 
00995 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
00996   return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
00997                                     unwrap<Constant>(RHSConstant)));
00998 }
00999 
01000 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
01001   return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
01002                                     unwrap<Constant>(RHSConstant)));
01003 }
01004 
01005 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
01006                           LLVMValueRef *ConstantIndices, unsigned NumIndices) {
01007   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
01008                                NumIndices);
01009   return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
01010                                              IdxList));
01011 }
01012 
01013 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
01014                                   LLVMValueRef *ConstantIndices,
01015                                   unsigned NumIndices) {
01016   Constant* Val = unwrap<Constant>(ConstantVal);
01017   ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
01018                                NumIndices);
01019   return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
01020 }
01021 
01022 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01023   return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
01024                                      unwrap(ToType)));
01025 }
01026 
01027 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01028   return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
01029                                     unwrap(ToType)));
01030 }
01031 
01032 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01033   return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
01034                                     unwrap(ToType)));
01035 }
01036 
01037 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01038   return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
01039                                        unwrap(ToType)));
01040 }
01041 
01042 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01043   return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
01044                                         unwrap(ToType)));
01045 }
01046 
01047 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01048   return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
01049                                       unwrap(ToType)));
01050 }
01051 
01052 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01053   return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
01054                                       unwrap(ToType)));
01055 }
01056 
01057 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01058   return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
01059                                       unwrap(ToType)));
01060 }
01061 
01062 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01063   return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
01064                                       unwrap(ToType)));
01065 }
01066 
01067 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01068   return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
01069                                         unwrap(ToType)));
01070 }
01071 
01072 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01073   return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
01074                                         unwrap(ToType)));
01075 }
01076 
01077 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01078   return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
01079                                        unwrap(ToType)));
01080 }
01081 
01082 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
01083                                     LLVMTypeRef ToType) {
01084   return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
01085                                              unwrap(ToType)));
01086 }
01087 
01088 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
01089                                     LLVMTypeRef ToType) {
01090   return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
01091                                              unwrap(ToType)));
01092 }
01093 
01094 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
01095                                     LLVMTypeRef ToType) {
01096   return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
01097                                              unwrap(ToType)));
01098 }
01099 
01100 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
01101                                      LLVMTypeRef ToType) {
01102   return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
01103                                               unwrap(ToType)));
01104 }
01105 
01106 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
01107                                   LLVMTypeRef ToType) {
01108   return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
01109                                            unwrap(ToType)));
01110 }
01111 
01112 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
01113                               LLVMBool isSigned) {
01114   return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
01115                                            unwrap(ToType), isSigned));
01116 }
01117 
01118 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
01119   return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
01120                                       unwrap(ToType)));
01121 }
01122 
01123 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
01124                              LLVMValueRef ConstantIfTrue,
01125                              LLVMValueRef ConstantIfFalse) {
01126   return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
01127                                       unwrap<Constant>(ConstantIfTrue),
01128                                       unwrap<Constant>(ConstantIfFalse)));
01129 }
01130 
01131 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
01132                                      LLVMValueRef IndexConstant) {
01133   return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
01134                                               unwrap<Constant>(IndexConstant)));
01135 }
01136 
01137 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
01138                                     LLVMValueRef ElementValueConstant,
01139                                     LLVMValueRef IndexConstant) {
01140   return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
01141                                          unwrap<Constant>(ElementValueConstant),
01142                                              unwrap<Constant>(IndexConstant)));
01143 }
01144 
01145 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
01146                                     LLVMValueRef VectorBConstant,
01147                                     LLVMValueRef MaskConstant) {
01148   return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
01149                                              unwrap<Constant>(VectorBConstant),
01150                                              unwrap<Constant>(MaskConstant)));
01151 }
01152 
01153 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
01154                                    unsigned NumIdx) {
01155   return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
01156                                             makeArrayRef(IdxList, NumIdx)));
01157 }
01158 
01159 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
01160                                   LLVMValueRef ElementValueConstant,
01161                                   unsigned *IdxList, unsigned NumIdx) {
01162   return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
01163                                          unwrap<Constant>(ElementValueConstant),
01164                                            makeArrayRef(IdxList, NumIdx)));
01165 }
01166 
01167 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
01168                                 const char *Constraints,
01169                                 LLVMBool HasSideEffects,
01170                                 LLVMBool IsAlignStack) {
01171   return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
01172                              Constraints, HasSideEffects, IsAlignStack));
01173 }
01174 
01175 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
01176   return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
01177 }
01178 
01179 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
01180 
01181 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
01182   return wrap(unwrap<GlobalValue>(Global)->getParent());
01183 }
01184 
01185 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
01186   return unwrap<GlobalValue>(Global)->isDeclaration();
01187 }
01188 
01189 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
01190   switch (unwrap<GlobalValue>(Global)->getLinkage()) {
01191   case GlobalValue::ExternalLinkage:
01192     return LLVMExternalLinkage;
01193   case GlobalValue::AvailableExternallyLinkage:
01194     return LLVMAvailableExternallyLinkage;
01195   case GlobalValue::LinkOnceAnyLinkage:
01196     return LLVMLinkOnceAnyLinkage;
01197   case GlobalValue::LinkOnceODRLinkage:
01198     return LLVMLinkOnceODRLinkage;
01199   case GlobalValue::WeakAnyLinkage:
01200     return LLVMWeakAnyLinkage;
01201   case GlobalValue::WeakODRLinkage:
01202     return LLVMWeakODRLinkage;
01203   case GlobalValue::AppendingLinkage:
01204     return LLVMAppendingLinkage;
01205   case GlobalValue::InternalLinkage:
01206     return LLVMInternalLinkage;
01207   case GlobalValue::PrivateLinkage:
01208     return LLVMPrivateLinkage;
01209   case GlobalValue::ExternalWeakLinkage:
01210     return LLVMExternalWeakLinkage;
01211   case GlobalValue::CommonLinkage:
01212     return LLVMCommonLinkage;
01213   }
01214 
01215   llvm_unreachable("Invalid GlobalValue linkage!");
01216 }
01217 
01218 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
01219   GlobalValue *GV = unwrap<GlobalValue>(Global);
01220 
01221   switch (Linkage) {
01222   case LLVMExternalLinkage:
01223     GV->setLinkage(GlobalValue::ExternalLinkage);
01224     break;
01225   case LLVMAvailableExternallyLinkage:
01226     GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
01227     break;
01228   case LLVMLinkOnceAnyLinkage:
01229     GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
01230     break;
01231   case LLVMLinkOnceODRLinkage:
01232     GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
01233     break;
01234   case LLVMLinkOnceODRAutoHideLinkage:
01235     DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
01236                     "longer supported.");
01237     break;
01238   case LLVMWeakAnyLinkage:
01239     GV->setLinkage(GlobalValue::WeakAnyLinkage);
01240     break;
01241   case LLVMWeakODRLinkage:
01242     GV->setLinkage(GlobalValue::WeakODRLinkage);
01243     break;
01244   case LLVMAppendingLinkage:
01245     GV->setLinkage(GlobalValue::AppendingLinkage);
01246     break;
01247   case LLVMInternalLinkage:
01248     GV->setLinkage(GlobalValue::InternalLinkage);
01249     break;
01250   case LLVMPrivateLinkage:
01251     GV->setLinkage(GlobalValue::PrivateLinkage);
01252     break;
01253   case LLVMLinkerPrivateLinkage:
01254     GV->setLinkage(GlobalValue::PrivateLinkage);
01255     break;
01256   case LLVMLinkerPrivateWeakLinkage:
01257     GV->setLinkage(GlobalValue::PrivateLinkage);
01258     break;
01259   case LLVMDLLImportLinkage:
01260     DEBUG(errs()
01261           << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
01262     break;
01263   case LLVMDLLExportLinkage:
01264     DEBUG(errs()
01265           << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
01266     break;
01267   case LLVMExternalWeakLinkage:
01268     GV->setLinkage(GlobalValue::ExternalWeakLinkage);
01269     break;
01270   case LLVMGhostLinkage:
01271     DEBUG(errs()
01272           << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
01273     break;
01274   case LLVMCommonLinkage:
01275     GV->setLinkage(GlobalValue::CommonLinkage);
01276     break;
01277   }
01278 }
01279 
01280 const char *LLVMGetSection(LLVMValueRef Global) {
01281   return unwrap<GlobalValue>(Global)->getSection().c_str();
01282 }
01283 
01284 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
01285   unwrap<GlobalValue>(Global)->setSection(Section);
01286 }
01287 
01288 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
01289   return static_cast<LLVMVisibility>(
01290     unwrap<GlobalValue>(Global)->getVisibility());
01291 }
01292 
01293 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
01294   unwrap<GlobalValue>(Global)
01295     ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
01296 }
01297 
01298 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
01299   return static_cast<LLVMDLLStorageClass>(
01300       unwrap<GlobalValue>(Global)->getDLLStorageClass());
01301 }
01302 
01303 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
01304   unwrap<GlobalValue>(Global)->setDLLStorageClass(
01305       static_cast<GlobalValue::DLLStorageClassTypes>(Class));
01306 }
01307 
01308 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
01309   return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
01310 }
01311 
01312 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
01313   unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
01314 }
01315 
01316 /*--.. Operations on global variables, load and store instructions .........--*/
01317 
01318 unsigned LLVMGetAlignment(LLVMValueRef V) {
01319   Value *P = unwrap<Value>(V);
01320   if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
01321     return GV->getAlignment();
01322   if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
01323     return AI->getAlignment();
01324   if (LoadInst *LI = dyn_cast<LoadInst>(P))
01325     return LI->getAlignment();
01326   if (StoreInst *SI = dyn_cast<StoreInst>(P))
01327     return SI->getAlignment();
01328 
01329   llvm_unreachable(
01330       "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
01331 }
01332 
01333 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
01334   Value *P = unwrap<Value>(V);
01335   if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
01336     GV->setAlignment(Bytes);
01337   else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
01338     AI->setAlignment(Bytes);
01339   else if (LoadInst *LI = dyn_cast<LoadInst>(P))
01340     LI->setAlignment(Bytes);
01341   else if (StoreInst *SI = dyn_cast<StoreInst>(P))
01342     SI->setAlignment(Bytes);
01343   else
01344     llvm_unreachable(
01345         "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
01346 }
01347 
01348 /*--.. Operations on global variables ......................................--*/
01349 
01350 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
01351   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
01352                                  GlobalValue::ExternalLinkage, nullptr, Name));
01353 }
01354 
01355 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
01356                                          const char *Name,
01357                                          unsigned AddressSpace) {
01358   return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
01359                                  GlobalValue::ExternalLinkage, nullptr, Name,
01360                                  nullptr, GlobalVariable::NotThreadLocal,
01361                                  AddressSpace));
01362 }
01363 
01364 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
01365   return wrap(unwrap(M)->getNamedGlobal(Name));
01366 }
01367 
01368 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
01369   Module *Mod = unwrap(M);
01370   Module::global_iterator I = Mod->global_begin();
01371   if (I == Mod->global_end())
01372     return nullptr;
01373   return wrap(I);
01374 }
01375 
01376 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
01377   Module *Mod = unwrap(M);
01378   Module::global_iterator I = Mod->global_end();
01379   if (I == Mod->global_begin())
01380     return nullptr;
01381   return wrap(--I);
01382 }
01383 
01384 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
01385   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
01386   Module::global_iterator I = GV;
01387   if (++I == GV->getParent()->global_end())
01388     return nullptr;
01389   return wrap(I);
01390 }
01391 
01392 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
01393   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
01394   Module::global_iterator I = GV;
01395   if (I == GV->getParent()->global_begin())
01396     return nullptr;
01397   return wrap(--I);
01398 }
01399 
01400 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
01401   unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
01402 }
01403 
01404 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
01405   GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
01406   if ( !GV->hasInitializer() )
01407     return nullptr;
01408   return wrap(GV->getInitializer());
01409 }
01410 
01411 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
01412   unwrap<GlobalVariable>(GlobalVar)
01413     ->setInitializer(unwrap<Constant>(ConstantVal));
01414 }
01415 
01416 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
01417   return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
01418 }
01419 
01420 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
01421   unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
01422 }
01423 
01424 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
01425   return unwrap<GlobalVariable>(GlobalVar)->isConstant();
01426 }
01427 
01428 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
01429   unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
01430 }
01431 
01432 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
01433   switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
01434   case GlobalVariable::NotThreadLocal:
01435     return LLVMNotThreadLocal;
01436   case GlobalVariable::GeneralDynamicTLSModel:
01437     return LLVMGeneralDynamicTLSModel;
01438   case GlobalVariable::LocalDynamicTLSModel:
01439     return LLVMLocalDynamicTLSModel;
01440   case GlobalVariable::InitialExecTLSModel:
01441     return LLVMInitialExecTLSModel;
01442   case GlobalVariable::LocalExecTLSModel:
01443     return LLVMLocalExecTLSModel;
01444   }
01445 
01446   llvm_unreachable("Invalid GlobalVariable thread local mode");
01447 }
01448 
01449 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
01450   GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
01451 
01452   switch (Mode) {
01453   case LLVMNotThreadLocal:
01454     GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
01455     break;
01456   case LLVMGeneralDynamicTLSModel:
01457     GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
01458     break;
01459   case LLVMLocalDynamicTLSModel:
01460     GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
01461     break;
01462   case LLVMInitialExecTLSModel:
01463     GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
01464     break;
01465   case LLVMLocalExecTLSModel:
01466     GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
01467     break;
01468   }
01469 }
01470 
01471 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
01472   return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
01473 }
01474 
01475 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
01476   unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
01477 }
01478 
01479 /*--.. Operations on aliases ......................................--*/
01480 
01481 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
01482                           const char *Name) {
01483   return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
01484                               unwrap<Constant>(Aliasee), unwrap (M)));
01485 }
01486 
01487 /*--.. Operations on functions .............................................--*/
01488 
01489 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
01490                              LLVMTypeRef FunctionTy) {
01491   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
01492                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
01493 }
01494 
01495 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
01496   return wrap(unwrap(M)->getFunction(Name));
01497 }
01498 
01499 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
01500   Module *Mod = unwrap(M);
01501   Module::iterator I = Mod->begin();
01502   if (I == Mod->end())
01503     return nullptr;
01504   return wrap(I);
01505 }
01506 
01507 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
01508   Module *Mod = unwrap(M);
01509   Module::iterator I = Mod->end();
01510   if (I == Mod->begin())
01511     return nullptr;
01512   return wrap(--I);
01513 }
01514 
01515 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
01516   Function *Func = unwrap<Function>(Fn);
01517   Module::iterator I = Func;
01518   if (++I == Func->getParent()->end())
01519     return nullptr;
01520   return wrap(I);
01521 }
01522 
01523 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
01524   Function *Func = unwrap<Function>(Fn);
01525   Module::iterator I = Func;
01526   if (I == Func->getParent()->begin())
01527     return nullptr;
01528   return wrap(--I);
01529 }
01530 
01531 void LLVMDeleteFunction(LLVMValueRef Fn) {
01532   unwrap<Function>(Fn)->eraseFromParent();
01533 }
01534 
01535 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
01536   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
01537     return F->getIntrinsicID();
01538   return 0;
01539 }
01540 
01541 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
01542   return unwrap<Function>(Fn)->getCallingConv();
01543 }
01544 
01545 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
01546   return unwrap<Function>(Fn)->setCallingConv(
01547     static_cast<CallingConv::ID>(CC));
01548 }
01549 
01550 const char *LLVMGetGC(LLVMValueRef Fn) {
01551   Function *F = unwrap<Function>(Fn);
01552   return F->hasGC()? F->getGC() : nullptr;
01553 }
01554 
01555 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
01556   Function *F = unwrap<Function>(Fn);
01557   if (GC)
01558     F->setGC(GC);
01559   else
01560     F->clearGC();
01561 }
01562 
01563 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
01564   Function *Func = unwrap<Function>(Fn);
01565   const AttributeSet PAL = Func->getAttributes();
01566   AttrBuilder B(PA);
01567   const AttributeSet PALnew =
01568     PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
01569                       AttributeSet::get(Func->getContext(),
01570                                         AttributeSet::FunctionIndex, B));
01571   Func->setAttributes(PALnew);
01572 }
01573 
01574 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
01575                                         const char *V) {
01576   Function *Func = unwrap<Function>(Fn);
01577   AttributeSet::AttrIndex Idx =
01578     AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
01579   AttrBuilder B;
01580 
01581   B.addAttribute(A, V);
01582   AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
01583   Func->addAttributes(Idx, Set);
01584 }
01585 
01586 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
01587   Function *Func = unwrap<Function>(Fn);
01588   const AttributeSet PAL = Func->getAttributes();
01589   AttrBuilder B(PA);
01590   const AttributeSet PALnew =
01591     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
01592                          AttributeSet::get(Func->getContext(),
01593                                            AttributeSet::FunctionIndex, B));
01594   Func->setAttributes(PALnew);
01595 }
01596 
01597 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
01598   Function *Func = unwrap<Function>(Fn);
01599   const AttributeSet PAL = Func->getAttributes();
01600   return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
01601 }
01602 
01603 /*--.. Operations on parameters ............................................--*/
01604 
01605 unsigned LLVMCountParams(LLVMValueRef FnRef) {
01606   // This function is strictly redundant to
01607   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
01608   return unwrap<Function>(FnRef)->arg_size();
01609 }
01610 
01611 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
01612   Function *Fn = unwrap<Function>(FnRef);
01613   for (Function::arg_iterator I = Fn->arg_begin(),
01614                               E = Fn->arg_end(); I != E; I++)
01615     *ParamRefs++ = wrap(I);
01616 }
01617 
01618 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
01619   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
01620   while (index --> 0)
01621     AI++;
01622   return wrap(AI);
01623 }
01624 
01625 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
01626   return wrap(unwrap<Argument>(V)->getParent());
01627 }
01628 
01629 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
01630   Function *Func = unwrap<Function>(Fn);
01631   Function::arg_iterator I = Func->arg_begin();
01632   if (I == Func->arg_end())
01633     return nullptr;
01634   return wrap(I);
01635 }
01636 
01637 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
01638   Function *Func = unwrap<Function>(Fn);
01639   Function::arg_iterator I = Func->arg_end();
01640   if (I == Func->arg_begin())
01641     return nullptr;
01642   return wrap(--I);
01643 }
01644 
01645 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
01646   Argument *A = unwrap<Argument>(Arg);
01647   Function::arg_iterator I = A;
01648   if (++I == A->getParent()->arg_end())
01649     return nullptr;
01650   return wrap(I);
01651 }
01652 
01653 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
01654   Argument *A = unwrap<Argument>(Arg);
01655   Function::arg_iterator I = A;
01656   if (I == A->getParent()->arg_begin())
01657     return nullptr;
01658   return wrap(--I);
01659 }
01660 
01661 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
01662   Argument *A = unwrap<Argument>(Arg);
01663   AttrBuilder B(PA);
01664   A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
01665 }
01666 
01667 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
01668   Argument *A = unwrap<Argument>(Arg);
01669   AttrBuilder B(PA);
01670   A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
01671 }
01672 
01673 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
01674   Argument *A = unwrap<Argument>(Arg);
01675   return (LLVMAttribute)A->getParent()->getAttributes().
01676     Raw(A->getArgNo()+1);
01677 }
01678 
01679 
01680 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
01681   Argument *A = unwrap<Argument>(Arg);
01682   AttrBuilder B;
01683   B.addAlignmentAttr(align);
01684   A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
01685 }
01686 
01687 /*--.. Operations on basic blocks ..........................................--*/
01688 
01689 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
01690   return wrap(static_cast<Value*>(unwrap(BB)));
01691 }
01692 
01693 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
01694   return isa<BasicBlock>(unwrap(Val));
01695 }
01696 
01697 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
01698   return wrap(unwrap<BasicBlock>(Val));
01699 }
01700 
01701 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
01702   return wrap(unwrap(BB)->getParent());
01703 }
01704 
01705 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
01706   return wrap(unwrap(BB)->getTerminator());
01707 }
01708 
01709 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
01710   return unwrap<Function>(FnRef)->size();
01711 }
01712 
01713 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
01714   Function *Fn = unwrap<Function>(FnRef);
01715   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
01716     *BasicBlocksRefs++ = wrap(I);
01717 }
01718 
01719 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
01720   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
01721 }
01722 
01723 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
01724   Function *Func = unwrap<Function>(Fn);
01725   Function::iterator I = Func->begin();
01726   if (I == Func->end())
01727     return nullptr;
01728   return wrap(I);
01729 }
01730 
01731 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
01732   Function *Func = unwrap<Function>(Fn);
01733   Function::iterator I = Func->end();
01734   if (I == Func->begin())
01735     return nullptr;
01736   return wrap(--I);
01737 }
01738 
01739 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
01740   BasicBlock *Block = unwrap(BB);
01741   Function::iterator I = Block;
01742   if (++I == Block->getParent()->end())
01743     return nullptr;
01744   return wrap(I);
01745 }
01746 
01747 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
01748   BasicBlock *Block = unwrap(BB);
01749   Function::iterator I = Block;
01750   if (I == Block->getParent()->begin())
01751     return nullptr;
01752   return wrap(--I);
01753 }
01754 
01755 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
01756                                                 LLVMValueRef FnRef,
01757                                                 const char *Name) {
01758   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
01759 }
01760 
01761 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
01762   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
01763 }
01764 
01765 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
01766                                                 LLVMBasicBlockRef BBRef,
01767                                                 const char *Name) {
01768   BasicBlock *BB = unwrap(BBRef);
01769   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
01770 }
01771 
01772 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
01773                                        const char *Name) {
01774   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
01775 }
01776 
01777 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
01778   unwrap(BBRef)->eraseFromParent();
01779 }
01780 
01781 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
01782   unwrap(BBRef)->removeFromParent();
01783 }
01784 
01785 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
01786   unwrap(BB)->moveBefore(unwrap(MovePos));
01787 }
01788 
01789 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
01790   unwrap(BB)->moveAfter(unwrap(MovePos));
01791 }
01792 
01793 /*--.. Operations on instructions ..........................................--*/
01794 
01795 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
01796   return wrap(unwrap<Instruction>(Inst)->getParent());
01797 }
01798 
01799 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
01800   BasicBlock *Block = unwrap(BB);
01801   BasicBlock::iterator I = Block->begin();
01802   if (I == Block->end())
01803     return nullptr;
01804   return wrap(I);
01805 }
01806 
01807 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
01808   BasicBlock *Block = unwrap(BB);
01809   BasicBlock::iterator I = Block->end();
01810   if (I == Block->begin())
01811     return nullptr;
01812   return wrap(--I);
01813 }
01814 
01815 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
01816   Instruction *Instr = unwrap<Instruction>(Inst);
01817   BasicBlock::iterator I = Instr;
01818   if (++I == Instr->getParent()->end())
01819     return nullptr;
01820   return wrap(I);
01821 }
01822 
01823 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
01824   Instruction *Instr = unwrap<Instruction>(Inst);
01825   BasicBlock::iterator I = Instr;
01826   if (I == Instr->getParent()->begin())
01827     return nullptr;
01828   return wrap(--I);
01829 }
01830 
01831 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
01832   unwrap<Instruction>(Inst)->eraseFromParent();
01833 }
01834 
01835 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
01836   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
01837     return (LLVMIntPredicate)I->getPredicate();
01838   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
01839     if (CE->getOpcode() == Instruction::ICmp)
01840       return (LLVMIntPredicate)CE->getPredicate();
01841   return (LLVMIntPredicate)0;
01842 }
01843 
01844 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
01845   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
01846     return map_to_llvmopcode(C->getOpcode());
01847   return (LLVMOpcode)0;
01848 }
01849 
01850 /*--.. Call and invoke instructions ........................................--*/
01851 
01852 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
01853   Value *V = unwrap(Instr);
01854   if (CallInst *CI = dyn_cast<CallInst>(V))
01855     return CI->getCallingConv();
01856   if (InvokeInst *II = dyn_cast<InvokeInst>(V))
01857     return II->getCallingConv();
01858   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
01859 }
01860 
01861 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
01862   Value *V = unwrap(Instr);
01863   if (CallInst *CI = dyn_cast<CallInst>(V))
01864     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
01865   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
01866     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
01867   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
01868 }
01869 
01870 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
01871                            LLVMAttribute PA) {
01872   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01873   AttrBuilder B(PA);
01874   Call.setAttributes(
01875     Call.getAttributes().addAttributes(Call->getContext(), index,
01876                                        AttributeSet::get(Call->getContext(),
01877                                                          index, B)));
01878 }
01879 
01880 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
01881                               LLVMAttribute PA) {
01882   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01883   AttrBuilder B(PA);
01884   Call.setAttributes(Call.getAttributes()
01885                        .removeAttributes(Call->getContext(), index,
01886                                          AttributeSet::get(Call->getContext(),
01887                                                            index, B)));
01888 }
01889 
01890 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
01891                                 unsigned align) {
01892   CallSite Call = CallSite(unwrap<Instruction>(Instr));
01893   AttrBuilder B;
01894   B.addAlignmentAttr(align);
01895   Call.setAttributes(Call.getAttributes()
01896                        .addAttributes(Call->getContext(), index,
01897                                       AttributeSet::get(Call->getContext(),
01898                                                         index, B)));
01899 }
01900 
01901 /*--.. Operations on call instructions (only) ..............................--*/
01902 
01903 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
01904   return unwrap<CallInst>(Call)->isTailCall();
01905 }
01906 
01907 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
01908   unwrap<CallInst>(Call)->setTailCall(isTailCall);
01909 }
01910 
01911 /*--.. Operations on switch instructions (only) ............................--*/
01912 
01913 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
01914   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
01915 }
01916 
01917 /*--.. Operations on phi nodes .............................................--*/
01918 
01919 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
01920                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
01921   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
01922   for (unsigned I = 0; I != Count; ++I)
01923     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
01924 }
01925 
01926 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
01927   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
01928 }
01929 
01930 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
01931   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
01932 }
01933 
01934 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
01935   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
01936 }
01937 
01938 
01939 /*===-- Instruction builders ----------------------------------------------===*/
01940 
01941 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
01942   return wrap(new IRBuilder<>(*unwrap(C)));
01943 }
01944 
01945 LLVMBuilderRef LLVMCreateBuilder(void) {
01946   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
01947 }
01948 
01949 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
01950                          LLVMValueRef Instr) {
01951   BasicBlock *BB = unwrap(Block);
01952   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
01953   unwrap(Builder)->SetInsertPoint(BB, I);
01954 }
01955 
01956 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
01957   Instruction *I = unwrap<Instruction>(Instr);
01958   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
01959 }
01960 
01961 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
01962   BasicBlock *BB = unwrap(Block);
01963   unwrap(Builder)->SetInsertPoint(BB);
01964 }
01965 
01966 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
01967    return wrap(unwrap(Builder)->GetInsertBlock());
01968 }
01969 
01970 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
01971   unwrap(Builder)->ClearInsertionPoint();
01972 }
01973 
01974 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
01975   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
01976 }
01977 
01978 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
01979                                    const char *Name) {
01980   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
01981 }
01982 
01983 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
01984   delete unwrap(Builder);
01985 }
01986 
01987 /*--.. Metadata builders ...................................................--*/
01988 
01989 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
01990   MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
01991   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
01992 }
01993 
01994 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
01995   return wrap(unwrap(Builder)->getCurrentDebugLocation()
01996               .getAsMDNode(unwrap(Builder)->getContext()));
01997 }
01998 
01999 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
02000   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
02001 }
02002 
02003 
02004 /*--.. Instruction builders ................................................--*/
02005 
02006 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
02007   return wrap(unwrap(B)->CreateRetVoid());
02008 }
02009 
02010 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
02011   return wrap(unwrap(B)->CreateRet(unwrap(V)));
02012 }
02013 
02014 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
02015                                    unsigned N) {
02016   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
02017 }
02018 
02019 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
02020   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
02021 }
02022 
02023 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
02024                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
02025   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
02026 }
02027 
02028 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
02029                              LLVMBasicBlockRef Else, unsigned NumCases) {
02030   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
02031 }
02032 
02033 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
02034                                  unsigned NumDests) {
02035   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
02036 }
02037 
02038 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
02039                              LLVMValueRef *Args, unsigned NumArgs,
02040                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
02041                              const char *Name) {
02042   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
02043                                       makeArrayRef(unwrap(Args), NumArgs),
02044                                       Name));
02045 }
02046 
02047 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
02048                                  LLVMValueRef PersFn, unsigned NumClauses,
02049                                  const char *Name) {
02050   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
02051                                           cast<Function>(unwrap(PersFn)),
02052                                           NumClauses, Name));
02053 }
02054 
02055 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
02056   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
02057 }
02058 
02059 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
02060   return wrap(unwrap(B)->CreateUnreachable());
02061 }
02062 
02063 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
02064                  LLVMBasicBlockRef Dest) {
02065   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
02066 }
02067 
02068 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
02069   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
02070 }
02071 
02072 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
02073   unwrap<LandingPadInst>(LandingPad)->
02074     addClause(cast<Constant>(unwrap(ClauseVal)));
02075 }
02076 
02077 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
02078   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
02079 }
02080 
02081 /*--.. Arithmetic ..........................................................--*/
02082 
02083 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02084                           const char *Name) {
02085   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
02086 }
02087 
02088 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02089                           const char *Name) {
02090   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
02091 }
02092 
02093 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02094                           const char *Name) {
02095   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
02096 }
02097 
02098 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02099                           const char *Name) {
02100   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
02101 }
02102 
02103 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02104                           const char *Name) {
02105   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
02106 }
02107 
02108 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02109                           const char *Name) {
02110   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
02111 }
02112 
02113 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02114                           const char *Name) {
02115   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
02116 }
02117 
02118 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02119                           const char *Name) {
02120   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
02121 }
02122 
02123 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02124                           const char *Name) {
02125   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
02126 }
02127 
02128 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02129                           const char *Name) {
02130   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
02131 }
02132 
02133 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02134                           const char *Name) {
02135   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
02136 }
02137 
02138 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02139                           const char *Name) {
02140   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
02141 }
02142 
02143 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02144                            const char *Name) {
02145   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
02146 }
02147 
02148 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02149                            const char *Name) {
02150   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
02151 }
02152 
02153 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
02154                                 LLVMValueRef RHS, const char *Name) {
02155   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
02156 }
02157 
02158 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02159                            const char *Name) {
02160   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
02161 }
02162 
02163 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02164                            const char *Name) {
02165   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
02166 }
02167 
02168 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02169                            const char *Name) {
02170   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
02171 }
02172 
02173 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02174                            const char *Name) {
02175   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
02176 }
02177 
02178 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02179                           const char *Name) {
02180   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
02181 }
02182 
02183 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02184                            const char *Name) {
02185   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
02186 }
02187 
02188 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02189                            const char *Name) {
02190   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
02191 }
02192 
02193 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02194                           const char *Name) {
02195   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
02196 }
02197 
02198 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02199                          const char *Name) {
02200   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
02201 }
02202 
02203 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02204                           const char *Name) {
02205   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
02206 }
02207 
02208 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
02209                             LLVMValueRef LHS, LLVMValueRef RHS,
02210                             const char *Name) {
02211   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
02212                                      unwrap(RHS), Name));
02213 }
02214 
02215 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02216   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
02217 }
02218 
02219 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
02220                              const char *Name) {
02221   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
02222 }
02223 
02224 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
02225                              const char *Name) {
02226   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
02227 }
02228 
02229 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02230   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
02231 }
02232 
02233 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02234   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
02235 }
02236 
02237 /*--.. Memory ..............................................................--*/
02238 
02239 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
02240                              const char *Name) {
02241   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
02242   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
02243   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
02244   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
02245                                                ITy, unwrap(Ty), AllocSize,
02246                                                nullptr, nullptr, "");
02247   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
02248 }
02249 
02250 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
02251                                   LLVMValueRef Val, const char *Name) {
02252   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
02253   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
02254   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
02255   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
02256                                                ITy, unwrap(Ty), AllocSize,
02257                                                unwrap(Val), nullptr, "");
02258   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
02259 }
02260 
02261 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
02262                              const char *Name) {
02263   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
02264 }
02265 
02266 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
02267                                   LLVMValueRef Val, const char *Name) {
02268   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
02269 }
02270 
02271 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
02272   return wrap(unwrap(B)->Insert(
02273      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
02274 }
02275 
02276 
02277 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
02278                            const char *Name) {
02279   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
02280 }
02281 
02282 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
02283                             LLVMValueRef PointerVal) {
02284   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
02285 }
02286 
02287 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
02288   switch (Ordering) {
02289     case LLVMAtomicOrderingNotAtomic: return NotAtomic;
02290     case LLVMAtomicOrderingUnordered: return Unordered;
02291     case LLVMAtomicOrderingMonotonic: return Monotonic;
02292     case LLVMAtomicOrderingAcquire: return Acquire;
02293     case LLVMAtomicOrderingRelease: return Release;
02294     case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
02295     case LLVMAtomicOrderingSequentiallyConsistent:
02296       return SequentiallyConsistent;
02297   }
02298   
02299   llvm_unreachable("Invalid LLVMAtomicOrdering value!");
02300 }
02301 
02302 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
02303                             LLVMBool isSingleThread, const char *Name) {
02304   return wrap(
02305     unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
02306                            isSingleThread ? SingleThread : CrossThread,
02307                            Name));
02308 }
02309 
02310 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02311                           LLVMValueRef *Indices, unsigned NumIndices,
02312                           const char *Name) {
02313   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
02314   return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
02315 }
02316 
02317 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02318                                   LLVMValueRef *Indices, unsigned NumIndices,
02319                                   const char *Name) {
02320   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
02321   return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
02322 }
02323 
02324 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02325                                 unsigned Idx, const char *Name) {
02326   return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
02327 }
02328 
02329 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
02330                                    const char *Name) {
02331   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
02332 }
02333 
02334 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
02335                                       const char *Name) {
02336   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
02337 }
02338 
02339 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
02340   Value *P = unwrap<Value>(MemAccessInst);
02341   if (LoadInst *LI = dyn_cast<LoadInst>(P))
02342     return LI->isVolatile();
02343   return cast<StoreInst>(P)->isVolatile();
02344 }
02345 
02346 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
02347   Value *P = unwrap<Value>(MemAccessInst);
02348   if (LoadInst *LI = dyn_cast<LoadInst>(P))
02349     return LI->setVolatile(isVolatile);
02350   return cast<StoreInst>(P)->setVolatile(isVolatile);
02351 }
02352 
02353 /*--.. Casts ...............................................................--*/
02354 
02355 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
02356                             LLVMTypeRef DestTy, const char *Name) {
02357   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
02358 }
02359 
02360 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
02361                            LLVMTypeRef DestTy, const char *Name) {
02362   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
02363 }
02364 
02365 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
02366                            LLVMTypeRef DestTy, const char *Name) {
02367   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
02368 }
02369 
02370 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
02371                              LLVMTypeRef DestTy, const char *Name) {
02372   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
02373 }
02374 
02375 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
02376                              LLVMTypeRef DestTy, const char *Name) {
02377   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
02378 }
02379 
02380 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
02381                              LLVMTypeRef DestTy, const char *Name) {
02382   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
02383 }
02384 
02385 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
02386                              LLVMTypeRef DestTy, const char *Name) {
02387   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
02388 }
02389 
02390 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
02391                               LLVMTypeRef DestTy, const char *Name) {
02392   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
02393 }
02394 
02395 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
02396                             LLVMTypeRef DestTy, const char *Name) {
02397   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
02398 }
02399 
02400 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
02401                                LLVMTypeRef DestTy, const char *Name) {
02402   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
02403 }
02404 
02405 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
02406                                LLVMTypeRef DestTy, const char *Name) {
02407   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
02408 }
02409 
02410 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02411                               LLVMTypeRef DestTy, const char *Name) {
02412   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
02413 }
02414 
02415 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
02416                                     LLVMTypeRef DestTy, const char *Name) {
02417   return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
02418 }
02419 
02420 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02421                                     LLVMTypeRef DestTy, const char *Name) {
02422   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
02423                                              Name));
02424 }
02425 
02426 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02427                                     LLVMTypeRef DestTy, const char *Name) {
02428   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
02429                                              Name));
02430 }
02431 
02432 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02433                                      LLVMTypeRef DestTy, const char *Name) {
02434   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
02435                                               Name));
02436 }
02437 
02438 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
02439                            LLVMTypeRef DestTy, const char *Name) {
02440   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
02441                                     unwrap(DestTy), Name));
02442 }
02443 
02444 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
02445                                   LLVMTypeRef DestTy, const char *Name) {
02446   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
02447 }
02448 
02449 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
02450                               LLVMTypeRef DestTy, const char *Name) {
02451   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
02452                                        /*isSigned*/true, Name));
02453 }
02454 
02455 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
02456                              LLVMTypeRef DestTy, const char *Name) {
02457   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
02458 }
02459 
02460 /*--.. Comparisons .........................................................--*/
02461 
02462 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
02463                            LLVMValueRef LHS, LLVMValueRef RHS,
02464                            const char *Name) {
02465   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
02466                                     unwrap(LHS), unwrap(RHS), Name));
02467 }
02468 
02469 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
02470                            LLVMValueRef LHS, LLVMValueRef RHS,
02471                            const char *Name) {
02472   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
02473                                     unwrap(LHS), unwrap(RHS), Name));
02474 }
02475 
02476 /*--.. Miscellaneous instructions ..........................................--*/
02477 
02478 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
02479   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
02480 }
02481 
02482 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
02483                            LLVMValueRef *Args, unsigned NumArgs,
02484                            const char *Name) {
02485   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
02486                                     makeArrayRef(unwrap(Args), NumArgs),
02487                                     Name));
02488 }
02489 
02490 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
02491                              LLVMValueRef Then, LLVMValueRef Else,
02492                              const char *Name) {
02493   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
02494                                       Name));
02495 }
02496 
02497 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
02498                             LLVMTypeRef Ty, const char *Name) {
02499   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
02500 }
02501 
02502 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
02503                                       LLVMValueRef Index, const char *Name) {
02504   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
02505                                               Name));
02506 }
02507 
02508 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
02509                                     LLVMValueRef EltVal, LLVMValueRef Index,
02510                                     const char *Name) {
02511   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
02512                                              unwrap(Index), Name));
02513 }
02514 
02515 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
02516                                     LLVMValueRef V2, LLVMValueRef Mask,
02517                                     const char *Name) {
02518   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
02519                                              unwrap(Mask), Name));
02520 }
02521 
02522 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
02523                                    unsigned Index, const char *Name) {
02524   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
02525 }
02526 
02527 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
02528                                   LLVMValueRef EltVal, unsigned Index,
02529                                   const char *Name) {
02530   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
02531                                            Index, Name));
02532 }
02533 
02534 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
02535                              const char *Name) {
02536   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
02537 }
02538 
02539 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
02540                                 const char *Name) {
02541   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
02542 }
02543 
02544 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
02545                               LLVMValueRef RHS, const char *Name) {
02546   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
02547 }
02548 
02549 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
02550                                LLVMValueRef PTR, LLVMValueRef Val,
02551                                LLVMAtomicOrdering ordering,
02552                                LLVMBool singleThread) {
02553   AtomicRMWInst::BinOp intop;
02554   switch (op) {
02555     case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
02556     case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
02557     case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
02558     case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
02559     case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
02560     case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
02561     case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
02562     case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
02563     case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
02564     case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
02565     case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
02566   }
02567   return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
02568     mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
02569 }
02570 
02571 
02572 /*===-- Module providers --------------------------------------------------===*/
02573 
02574 LLVMModuleProviderRef
02575 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
02576   return reinterpret_cast<LLVMModuleProviderRef>(M);
02577 }
02578 
02579 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
02580   delete unwrap(MP);
02581 }
02582 
02583 
02584 /*===-- Memory buffers ----------------------------------------------------===*/
02585 
02586 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
02587     const char *Path,
02588     LLVMMemoryBufferRef *OutMemBuf,
02589     char **OutMessage) {
02590 
02591   std::unique_ptr<MemoryBuffer> MB;
02592   error_code ec;
02593   if (!(ec = MemoryBuffer::getFile(Path, MB))) {
02594     *OutMemBuf = wrap(MB.release());
02595     return 0;
02596   }
02597 
02598   *OutMessage = strdup(ec.message().c_str());
02599   return 1;
02600 }
02601 
02602 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
02603                                          char **OutMessage) {
02604   std::unique_ptr<MemoryBuffer> MB;
02605   error_code ec;
02606   if (!(ec = MemoryBuffer::getSTDIN(MB))) {
02607     *OutMemBuf = wrap(MB.release());
02608     return 0;
02609   }
02610 
02611   *OutMessage = strdup(ec.message().c_str());
02612   return 1;
02613 }
02614 
02615 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
02616     const char *InputData,
02617     size_t InputDataLength,
02618     const char *BufferName,
02619     LLVMBool RequiresNullTerminator) {
02620 
02621   return wrap(MemoryBuffer::getMemBuffer(
02622       StringRef(InputData, InputDataLength),
02623       StringRef(BufferName),
02624       RequiresNullTerminator));
02625 }
02626 
02627 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
02628     const char *InputData,
02629     size_t InputDataLength,
02630     const char *BufferName) {
02631 
02632   return wrap(MemoryBuffer::getMemBufferCopy(
02633       StringRef(InputData, InputDataLength),
02634       StringRef(BufferName)));
02635 }
02636 
02637 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
02638   return unwrap(MemBuf)->getBufferStart();
02639 }
02640 
02641 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
02642   return unwrap(MemBuf)->getBufferSize();
02643 }
02644 
02645 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
02646   delete unwrap(MemBuf);
02647 }
02648 
02649 /*===-- Pass Registry -----------------------------------------------------===*/
02650 
02651 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
02652   return wrap(PassRegistry::getPassRegistry());
02653 }
02654 
02655 /*===-- Pass Manager ------------------------------------------------------===*/
02656 
02657 LLVMPassManagerRef LLVMCreatePassManager() {
02658   return wrap(new PassManager());
02659 }
02660 
02661 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
02662   return wrap(new FunctionPassManager(unwrap(M)));
02663 }
02664 
02665 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
02666   return LLVMCreateFunctionPassManagerForModule(
02667                                             reinterpret_cast<LLVMModuleRef>(P));
02668 }
02669 
02670 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
02671   return unwrap<PassManager>(PM)->run(*unwrap(M));
02672 }
02673 
02674 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
02675   return unwrap<FunctionPassManager>(FPM)->doInitialization();
02676 }
02677 
02678 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
02679   return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
02680 }
02681 
02682 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
02683   return unwrap<FunctionPassManager>(FPM)->doFinalization();
02684 }
02685 
02686 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
02687   delete unwrap(PM);
02688 }
02689 
02690 /*===-- Threading ------------------------------------------------------===*/
02691 
02692 LLVMBool LLVMStartMultithreaded() {
02693   return llvm_start_multithreaded();
02694 }
02695 
02696 void LLVMStopMultithreaded() {
02697   llvm_stop_multithreaded();
02698 }
02699 
02700 LLVMBool LLVMIsMultithreaded() {
02701   return llvm_is_multithreaded();
02702 }