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