LLVM API Documentation

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