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