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