LLVM API Documentation

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