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