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, GlobalValue::ExternalLinkage, Name,
01638                                   unwrap<Constant>(Aliasee), unwrap(M)));
01639 }
01640 
01641 /*--.. Operations on functions .............................................--*/
01642 
01643 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
01644                              LLVMTypeRef FunctionTy) {
01645   return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
01646                                GlobalValue::ExternalLinkage, Name, unwrap(M)));
01647 }
01648 
01649 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
01650   return wrap(unwrap(M)->getFunction(Name));
01651 }
01652 
01653 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
01654   Module *Mod = unwrap(M);
01655   Module::iterator I = Mod->begin();
01656   if (I == Mod->end())
01657     return nullptr;
01658   return wrap(I);
01659 }
01660 
01661 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
01662   Module *Mod = unwrap(M);
01663   Module::iterator I = Mod->end();
01664   if (I == Mod->begin())
01665     return nullptr;
01666   return wrap(--I);
01667 }
01668 
01669 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
01670   Function *Func = unwrap<Function>(Fn);
01671   Module::iterator I = Func;
01672   if (++I == Func->getParent()->end())
01673     return nullptr;
01674   return wrap(I);
01675 }
01676 
01677 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
01678   Function *Func = unwrap<Function>(Fn);
01679   Module::iterator I = Func;
01680   if (I == Func->getParent()->begin())
01681     return nullptr;
01682   return wrap(--I);
01683 }
01684 
01685 void LLVMDeleteFunction(LLVMValueRef Fn) {
01686   unwrap<Function>(Fn)->eraseFromParent();
01687 }
01688 
01689 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
01690   if (Function *F = dyn_cast<Function>(unwrap(Fn)))
01691     return F->getIntrinsicID();
01692   return 0;
01693 }
01694 
01695 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
01696   return unwrap<Function>(Fn)->getCallingConv();
01697 }
01698 
01699 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
01700   return unwrap<Function>(Fn)->setCallingConv(
01701     static_cast<CallingConv::ID>(CC));
01702 }
01703 
01704 const char *LLVMGetGC(LLVMValueRef Fn) {
01705   Function *F = unwrap<Function>(Fn);
01706   return F->hasGC()? F->getGC() : nullptr;
01707 }
01708 
01709 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
01710   Function *F = unwrap<Function>(Fn);
01711   if (GC)
01712     F->setGC(GC);
01713   else
01714     F->clearGC();
01715 }
01716 
01717 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
01718   Function *Func = unwrap<Function>(Fn);
01719   const AttributeSet PAL = Func->getAttributes();
01720   AttrBuilder B(PA);
01721   const AttributeSet PALnew =
01722     PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
01723                       AttributeSet::get(Func->getContext(),
01724                                         AttributeSet::FunctionIndex, B));
01725   Func->setAttributes(PALnew);
01726 }
01727 
01728 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
01729                                         const char *V) {
01730   Function *Func = unwrap<Function>(Fn);
01731   AttributeSet::AttrIndex Idx =
01732     AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
01733   AttrBuilder B;
01734 
01735   B.addAttribute(A, V);
01736   AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
01737   Func->addAttributes(Idx, Set);
01738 }
01739 
01740 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
01741   Function *Func = unwrap<Function>(Fn);
01742   const AttributeSet PAL = Func->getAttributes();
01743   AttrBuilder B(PA);
01744   const AttributeSet PALnew =
01745     PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
01746                          AttributeSet::get(Func->getContext(),
01747                                            AttributeSet::FunctionIndex, B));
01748   Func->setAttributes(PALnew);
01749 }
01750 
01751 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
01752   Function *Func = unwrap<Function>(Fn);
01753   const AttributeSet PAL = Func->getAttributes();
01754   return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
01755 }
01756 
01757 /*--.. Operations on parameters ............................................--*/
01758 
01759 unsigned LLVMCountParams(LLVMValueRef FnRef) {
01760   // This function is strictly redundant to
01761   //   LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
01762   return unwrap<Function>(FnRef)->arg_size();
01763 }
01764 
01765 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
01766   Function *Fn = unwrap<Function>(FnRef);
01767   for (Function::arg_iterator I = Fn->arg_begin(),
01768                               E = Fn->arg_end(); I != E; I++)
01769     *ParamRefs++ = wrap(I);
01770 }
01771 
01772 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
01773   Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
01774   while (index --> 0)
01775     AI++;
01776   return wrap(AI);
01777 }
01778 
01779 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
01780   return wrap(unwrap<Argument>(V)->getParent());
01781 }
01782 
01783 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
01784   Function *Func = unwrap<Function>(Fn);
01785   Function::arg_iterator I = Func->arg_begin();
01786   if (I == Func->arg_end())
01787     return nullptr;
01788   return wrap(I);
01789 }
01790 
01791 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
01792   Function *Func = unwrap<Function>(Fn);
01793   Function::arg_iterator I = Func->arg_end();
01794   if (I == Func->arg_begin())
01795     return nullptr;
01796   return wrap(--I);
01797 }
01798 
01799 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
01800   Argument *A = unwrap<Argument>(Arg);
01801   Function::arg_iterator I = A;
01802   if (++I == A->getParent()->arg_end())
01803     return nullptr;
01804   return wrap(I);
01805 }
01806 
01807 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
01808   Argument *A = unwrap<Argument>(Arg);
01809   Function::arg_iterator I = A;
01810   if (I == A->getParent()->arg_begin())
01811     return nullptr;
01812   return wrap(--I);
01813 }
01814 
01815 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
01816   Argument *A = unwrap<Argument>(Arg);
01817   AttrBuilder B(PA);
01818   A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
01819 }
01820 
01821 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
01822   Argument *A = unwrap<Argument>(Arg);
01823   AttrBuilder B(PA);
01824   A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,  B));
01825 }
01826 
01827 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
01828   Argument *A = unwrap<Argument>(Arg);
01829   return (LLVMAttribute)A->getParent()->getAttributes().
01830     Raw(A->getArgNo()+1);
01831 }
01832 
01833 
01834 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
01835   Argument *A = unwrap<Argument>(Arg);
01836   AttrBuilder B;
01837   B.addAlignmentAttr(align);
01838   A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
01839 }
01840 
01841 /*--.. Operations on basic blocks ..........................................--*/
01842 
01843 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
01844   return wrap(static_cast<Value*>(unwrap(BB)));
01845 }
01846 
01847 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
01848   return isa<BasicBlock>(unwrap(Val));
01849 }
01850 
01851 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
01852   return wrap(unwrap<BasicBlock>(Val));
01853 }
01854 
01855 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
01856   return wrap(unwrap(BB)->getParent());
01857 }
01858 
01859 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
01860   return wrap(unwrap(BB)->getTerminator());
01861 }
01862 
01863 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
01864   return unwrap<Function>(FnRef)->size();
01865 }
01866 
01867 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
01868   Function *Fn = unwrap<Function>(FnRef);
01869   for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
01870     *BasicBlocksRefs++ = wrap(I);
01871 }
01872 
01873 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
01874   return wrap(&unwrap<Function>(Fn)->getEntryBlock());
01875 }
01876 
01877 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
01878   Function *Func = unwrap<Function>(Fn);
01879   Function::iterator I = Func->begin();
01880   if (I == Func->end())
01881     return nullptr;
01882   return wrap(I);
01883 }
01884 
01885 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
01886   Function *Func = unwrap<Function>(Fn);
01887   Function::iterator I = Func->end();
01888   if (I == Func->begin())
01889     return nullptr;
01890   return wrap(--I);
01891 }
01892 
01893 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
01894   BasicBlock *Block = unwrap(BB);
01895   Function::iterator I = Block;
01896   if (++I == Block->getParent()->end())
01897     return nullptr;
01898   return wrap(I);
01899 }
01900 
01901 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
01902   BasicBlock *Block = unwrap(BB);
01903   Function::iterator I = Block;
01904   if (I == Block->getParent()->begin())
01905     return nullptr;
01906   return wrap(--I);
01907 }
01908 
01909 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
01910                                                 LLVMValueRef FnRef,
01911                                                 const char *Name) {
01912   return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
01913 }
01914 
01915 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
01916   return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
01917 }
01918 
01919 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
01920                                                 LLVMBasicBlockRef BBRef,
01921                                                 const char *Name) {
01922   BasicBlock *BB = unwrap(BBRef);
01923   return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
01924 }
01925 
01926 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
01927                                        const char *Name) {
01928   return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
01929 }
01930 
01931 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
01932   unwrap(BBRef)->eraseFromParent();
01933 }
01934 
01935 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
01936   unwrap(BBRef)->removeFromParent();
01937 }
01938 
01939 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
01940   unwrap(BB)->moveBefore(unwrap(MovePos));
01941 }
01942 
01943 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
01944   unwrap(BB)->moveAfter(unwrap(MovePos));
01945 }
01946 
01947 /*--.. Operations on instructions ..........................................--*/
01948 
01949 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
01950   return wrap(unwrap<Instruction>(Inst)->getParent());
01951 }
01952 
01953 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
01954   BasicBlock *Block = unwrap(BB);
01955   BasicBlock::iterator I = Block->begin();
01956   if (I == Block->end())
01957     return nullptr;
01958   return wrap(I);
01959 }
01960 
01961 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
01962   BasicBlock *Block = unwrap(BB);
01963   BasicBlock::iterator I = Block->end();
01964   if (I == Block->begin())
01965     return nullptr;
01966   return wrap(--I);
01967 }
01968 
01969 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
01970   Instruction *Instr = unwrap<Instruction>(Inst);
01971   BasicBlock::iterator I = Instr;
01972   if (++I == Instr->getParent()->end())
01973     return nullptr;
01974   return wrap(I);
01975 }
01976 
01977 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
01978   Instruction *Instr = unwrap<Instruction>(Inst);
01979   BasicBlock::iterator I = Instr;
01980   if (I == Instr->getParent()->begin())
01981     return nullptr;
01982   return wrap(--I);
01983 }
01984 
01985 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
01986   unwrap<Instruction>(Inst)->eraseFromParent();
01987 }
01988 
01989 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
01990   if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
01991     return (LLVMIntPredicate)I->getPredicate();
01992   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
01993     if (CE->getOpcode() == Instruction::ICmp)
01994       return (LLVMIntPredicate)CE->getPredicate();
01995   return (LLVMIntPredicate)0;
01996 }
01997 
01998 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
01999   if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
02000     return (LLVMRealPredicate)I->getPredicate();
02001   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
02002     if (CE->getOpcode() == Instruction::FCmp)
02003       return (LLVMRealPredicate)CE->getPredicate();
02004   return (LLVMRealPredicate)0;
02005 }
02006 
02007 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
02008   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
02009     return map_to_llvmopcode(C->getOpcode());
02010   return (LLVMOpcode)0;
02011 }
02012 
02013 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) {
02014   if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
02015     return wrap(C->clone());
02016   return nullptr;
02017 }
02018 
02019 /*--.. Call and invoke instructions ........................................--*/
02020 
02021 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
02022   Value *V = unwrap(Instr);
02023   if (CallInst *CI = dyn_cast<CallInst>(V))
02024     return CI->getCallingConv();
02025   if (InvokeInst *II = dyn_cast<InvokeInst>(V))
02026     return II->getCallingConv();
02027   llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
02028 }
02029 
02030 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
02031   Value *V = unwrap(Instr);
02032   if (CallInst *CI = dyn_cast<CallInst>(V))
02033     return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
02034   else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
02035     return II->setCallingConv(static_cast<CallingConv::ID>(CC));
02036   llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
02037 }
02038 
02039 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
02040                            LLVMAttribute PA) {
02041   CallSite Call = CallSite(unwrap<Instruction>(Instr));
02042   AttrBuilder B(PA);
02043   Call.setAttributes(
02044     Call.getAttributes().addAttributes(Call->getContext(), index,
02045                                        AttributeSet::get(Call->getContext(),
02046                                                          index, B)));
02047 }
02048 
02049 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
02050                               LLVMAttribute PA) {
02051   CallSite Call = CallSite(unwrap<Instruction>(Instr));
02052   AttrBuilder B(PA);
02053   Call.setAttributes(Call.getAttributes()
02054                        .removeAttributes(Call->getContext(), index,
02055                                          AttributeSet::get(Call->getContext(),
02056                                                            index, B)));
02057 }
02058 
02059 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
02060                                 unsigned align) {
02061   CallSite Call = CallSite(unwrap<Instruction>(Instr));
02062   AttrBuilder B;
02063   B.addAlignmentAttr(align);
02064   Call.setAttributes(Call.getAttributes()
02065                        .addAttributes(Call->getContext(), index,
02066                                       AttributeSet::get(Call->getContext(),
02067                                                         index, B)));
02068 }
02069 
02070 /*--.. Operations on call instructions (only) ..............................--*/
02071 
02072 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
02073   return unwrap<CallInst>(Call)->isTailCall();
02074 }
02075 
02076 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
02077   unwrap<CallInst>(Call)->setTailCall(isTailCall);
02078 }
02079 
02080 /*--.. Operations on terminators ...........................................--*/
02081 
02082 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) {
02083   return unwrap<TerminatorInst>(Term)->getNumSuccessors();
02084 }
02085 
02086 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) {
02087   return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i));
02088 }
02089 
02090 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) {
02091   return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block));
02092 }
02093 
02094 /*--.. Operations on branch instructions (only) ............................--*/
02095 
02096 LLVMBool LLVMIsConditional(LLVMValueRef Branch) {
02097   return unwrap<BranchInst>(Branch)->isConditional();
02098 }
02099 
02100 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) {
02101   return wrap(unwrap<BranchInst>(Branch)->getCondition());
02102 }
02103 
02104 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) {
02105   return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
02106 }
02107 
02108 /*--.. Operations on switch instructions (only) ............................--*/
02109 
02110 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
02111   return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
02112 }
02113 
02114 /*--.. Operations on phi nodes .............................................--*/
02115 
02116 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
02117                      LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
02118   PHINode *PhiVal = unwrap<PHINode>(PhiNode);
02119   for (unsigned I = 0; I != Count; ++I)
02120     PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
02121 }
02122 
02123 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
02124   return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
02125 }
02126 
02127 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
02128   return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
02129 }
02130 
02131 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
02132   return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
02133 }
02134 
02135 
02136 /*===-- Instruction builders ----------------------------------------------===*/
02137 
02138 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
02139   return wrap(new IRBuilder<>(*unwrap(C)));
02140 }
02141 
02142 LLVMBuilderRef LLVMCreateBuilder(void) {
02143   return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
02144 }
02145 
02146 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
02147                          LLVMValueRef Instr) {
02148   BasicBlock *BB = unwrap(Block);
02149   Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
02150   unwrap(Builder)->SetInsertPoint(BB, I);
02151 }
02152 
02153 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
02154   Instruction *I = unwrap<Instruction>(Instr);
02155   unwrap(Builder)->SetInsertPoint(I->getParent(), I);
02156 }
02157 
02158 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
02159   BasicBlock *BB = unwrap(Block);
02160   unwrap(Builder)->SetInsertPoint(BB);
02161 }
02162 
02163 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
02164    return wrap(unwrap(Builder)->GetInsertBlock());
02165 }
02166 
02167 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
02168   unwrap(Builder)->ClearInsertionPoint();
02169 }
02170 
02171 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
02172   unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
02173 }
02174 
02175 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
02176                                    const char *Name) {
02177   unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
02178 }
02179 
02180 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
02181   delete unwrap(Builder);
02182 }
02183 
02184 /*--.. Metadata builders ...................................................--*/
02185 
02186 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
02187   MDNode *Loc =
02188       L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
02189   unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
02190 }
02191 
02192 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
02193   LLVMContext &Context = unwrap(Builder)->getContext();
02194   return wrap(MetadataAsValue::get(
02195       Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
02196 }
02197 
02198 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
02199   unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
02200 }
02201 
02202 
02203 /*--.. Instruction builders ................................................--*/
02204 
02205 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
02206   return wrap(unwrap(B)->CreateRetVoid());
02207 }
02208 
02209 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
02210   return wrap(unwrap(B)->CreateRet(unwrap(V)));
02211 }
02212 
02213 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
02214                                    unsigned N) {
02215   return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
02216 }
02217 
02218 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
02219   return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
02220 }
02221 
02222 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
02223                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
02224   return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
02225 }
02226 
02227 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
02228                              LLVMBasicBlockRef Else, unsigned NumCases) {
02229   return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
02230 }
02231 
02232 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
02233                                  unsigned NumDests) {
02234   return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
02235 }
02236 
02237 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
02238                              LLVMValueRef *Args, unsigned NumArgs,
02239                              LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
02240                              const char *Name) {
02241   return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
02242                                       makeArrayRef(unwrap(Args), NumArgs),
02243                                       Name));
02244 }
02245 
02246 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
02247                                  LLVMValueRef PersFn, unsigned NumClauses,
02248                                  const char *Name) {
02249   return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
02250                                           cast<Function>(unwrap(PersFn)),
02251                                           NumClauses, Name));
02252 }
02253 
02254 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
02255   return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
02256 }
02257 
02258 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
02259   return wrap(unwrap(B)->CreateUnreachable());
02260 }
02261 
02262 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
02263                  LLVMBasicBlockRef Dest) {
02264   unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
02265 }
02266 
02267 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
02268   unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
02269 }
02270 
02271 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
02272   unwrap<LandingPadInst>(LandingPad)->
02273     addClause(cast<Constant>(unwrap(ClauseVal)));
02274 }
02275 
02276 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
02277   unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
02278 }
02279 
02280 /*--.. Arithmetic ..........................................................--*/
02281 
02282 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02283                           const char *Name) {
02284   return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
02285 }
02286 
02287 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02288                           const char *Name) {
02289   return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
02290 }
02291 
02292 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02293                           const char *Name) {
02294   return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
02295 }
02296 
02297 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02298                           const char *Name) {
02299   return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
02300 }
02301 
02302 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02303                           const char *Name) {
02304   return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
02305 }
02306 
02307 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02308                           const char *Name) {
02309   return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
02310 }
02311 
02312 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02313                           const char *Name) {
02314   return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
02315 }
02316 
02317 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02318                           const char *Name) {
02319   return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
02320 }
02321 
02322 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02323                           const char *Name) {
02324   return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
02325 }
02326 
02327 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02328                           const char *Name) {
02329   return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
02330 }
02331 
02332 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02333                           const char *Name) {
02334   return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
02335 }
02336 
02337 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02338                           const char *Name) {
02339   return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
02340 }
02341 
02342 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02343                            const char *Name) {
02344   return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
02345 }
02346 
02347 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02348                            const char *Name) {
02349   return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
02350 }
02351 
02352 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
02353                                 LLVMValueRef RHS, const char *Name) {
02354   return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
02355 }
02356 
02357 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02358                            const char *Name) {
02359   return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
02360 }
02361 
02362 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02363                            const char *Name) {
02364   return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
02365 }
02366 
02367 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02368                            const char *Name) {
02369   return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
02370 }
02371 
02372 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02373                            const char *Name) {
02374   return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
02375 }
02376 
02377 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02378                           const char *Name) {
02379   return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
02380 }
02381 
02382 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02383                            const char *Name) {
02384   return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
02385 }
02386 
02387 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02388                            const char *Name) {
02389   return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
02390 }
02391 
02392 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02393                           const char *Name) {
02394   return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
02395 }
02396 
02397 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02398                          const char *Name) {
02399   return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
02400 }
02401 
02402 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
02403                           const char *Name) {
02404   return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
02405 }
02406 
02407 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
02408                             LLVMValueRef LHS, LLVMValueRef RHS,
02409                             const char *Name) {
02410   return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
02411                                      unwrap(RHS), Name));
02412 }
02413 
02414 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02415   return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
02416 }
02417 
02418 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
02419                              const char *Name) {
02420   return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
02421 }
02422 
02423 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
02424                              const char *Name) {
02425   return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
02426 }
02427 
02428 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02429   return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
02430 }
02431 
02432 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
02433   return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
02434 }
02435 
02436 /*--.. Memory ..............................................................--*/
02437 
02438 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
02439                              const char *Name) {
02440   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
02441   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
02442   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
02443   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
02444                                                ITy, unwrap(Ty), AllocSize,
02445                                                nullptr, nullptr, "");
02446   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
02447 }
02448 
02449 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
02450                                   LLVMValueRef Val, const char *Name) {
02451   Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
02452   Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
02453   AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
02454   Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
02455                                                ITy, unwrap(Ty), AllocSize,
02456                                                unwrap(Val), nullptr, "");
02457   return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
02458 }
02459 
02460 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
02461                              const char *Name) {
02462   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
02463 }
02464 
02465 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
02466                                   LLVMValueRef Val, const char *Name) {
02467   return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
02468 }
02469 
02470 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
02471   return wrap(unwrap(B)->Insert(
02472      CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
02473 }
02474 
02475 
02476 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
02477                            const char *Name) {
02478   return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
02479 }
02480 
02481 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
02482                             LLVMValueRef PointerVal) {
02483   return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
02484 }
02485 
02486 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
02487   switch (Ordering) {
02488     case LLVMAtomicOrderingNotAtomic: return NotAtomic;
02489     case LLVMAtomicOrderingUnordered: return Unordered;
02490     case LLVMAtomicOrderingMonotonic: return Monotonic;
02491     case LLVMAtomicOrderingAcquire: return Acquire;
02492     case LLVMAtomicOrderingRelease: return Release;
02493     case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
02494     case LLVMAtomicOrderingSequentiallyConsistent:
02495       return SequentiallyConsistent;
02496   }
02497 
02498   llvm_unreachable("Invalid LLVMAtomicOrdering value!");
02499 }
02500 
02501 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
02502                             LLVMBool isSingleThread, const char *Name) {
02503   return wrap(
02504     unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
02505                            isSingleThread ? SingleThread : CrossThread,
02506                            Name));
02507 }
02508 
02509 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02510                           LLVMValueRef *Indices, unsigned NumIndices,
02511                           const char *Name) {
02512   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
02513   return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name));
02514 }
02515 
02516 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02517                                   LLVMValueRef *Indices, unsigned NumIndices,
02518                                   const char *Name) {
02519   ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
02520   return wrap(
02521       unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name));
02522 }
02523 
02524 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
02525                                 unsigned Idx, const char *Name) {
02526   return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name));
02527 }
02528 
02529 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
02530                                    const char *Name) {
02531   return wrap(unwrap(B)->CreateGlobalString(Str, Name));
02532 }
02533 
02534 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
02535                                       const char *Name) {
02536   return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
02537 }
02538 
02539 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
02540   Value *P = unwrap<Value>(MemAccessInst);
02541   if (LoadInst *LI = dyn_cast<LoadInst>(P))
02542     return LI->isVolatile();
02543   return cast<StoreInst>(P)->isVolatile();
02544 }
02545 
02546 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
02547   Value *P = unwrap<Value>(MemAccessInst);
02548   if (LoadInst *LI = dyn_cast<LoadInst>(P))
02549     return LI->setVolatile(isVolatile);
02550   return cast<StoreInst>(P)->setVolatile(isVolatile);
02551 }
02552 
02553 /*--.. Casts ...............................................................--*/
02554 
02555 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
02556                             LLVMTypeRef DestTy, const char *Name) {
02557   return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
02558 }
02559 
02560 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
02561                            LLVMTypeRef DestTy, const char *Name) {
02562   return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
02563 }
02564 
02565 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
02566                            LLVMTypeRef DestTy, const char *Name) {
02567   return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
02568 }
02569 
02570 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
02571                              LLVMTypeRef DestTy, const char *Name) {
02572   return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
02573 }
02574 
02575 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
02576                              LLVMTypeRef DestTy, const char *Name) {
02577   return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
02578 }
02579 
02580 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
02581                              LLVMTypeRef DestTy, const char *Name) {
02582   return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
02583 }
02584 
02585 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
02586                              LLVMTypeRef DestTy, const char *Name) {
02587   return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
02588 }
02589 
02590 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
02591                               LLVMTypeRef DestTy, const char *Name) {
02592   return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
02593 }
02594 
02595 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
02596                             LLVMTypeRef DestTy, const char *Name) {
02597   return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
02598 }
02599 
02600 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
02601                                LLVMTypeRef DestTy, const char *Name) {
02602   return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
02603 }
02604 
02605 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
02606                                LLVMTypeRef DestTy, const char *Name) {
02607   return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
02608 }
02609 
02610 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02611                               LLVMTypeRef DestTy, const char *Name) {
02612   return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
02613 }
02614 
02615 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
02616                                     LLVMTypeRef DestTy, const char *Name) {
02617   return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
02618 }
02619 
02620 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02621                                     LLVMTypeRef DestTy, const char *Name) {
02622   return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
02623                                              Name));
02624 }
02625 
02626 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02627                                     LLVMTypeRef DestTy, const char *Name) {
02628   return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
02629                                              Name));
02630 }
02631 
02632 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
02633                                      LLVMTypeRef DestTy, const char *Name) {
02634   return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
02635                                               Name));
02636 }
02637 
02638 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
02639                            LLVMTypeRef DestTy, const char *Name) {
02640   return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
02641                                     unwrap(DestTy), Name));
02642 }
02643 
02644 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
02645                                   LLVMTypeRef DestTy, const char *Name) {
02646   return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
02647 }
02648 
02649 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
02650                               LLVMTypeRef DestTy, const char *Name) {
02651   return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
02652                                        /*isSigned*/true, Name));
02653 }
02654 
02655 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
02656                              LLVMTypeRef DestTy, const char *Name) {
02657   return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
02658 }
02659 
02660 /*--.. Comparisons .........................................................--*/
02661 
02662 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
02663                            LLVMValueRef LHS, LLVMValueRef RHS,
02664                            const char *Name) {
02665   return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
02666                                     unwrap(LHS), unwrap(RHS), Name));
02667 }
02668 
02669 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
02670                            LLVMValueRef LHS, LLVMValueRef RHS,
02671                            const char *Name) {
02672   return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
02673                                     unwrap(LHS), unwrap(RHS), Name));
02674 }
02675 
02676 /*--.. Miscellaneous instructions ..........................................--*/
02677 
02678 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
02679   return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
02680 }
02681 
02682 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
02683                            LLVMValueRef *Args, unsigned NumArgs,
02684                            const char *Name) {
02685   return wrap(unwrap(B)->CreateCall(unwrap(Fn),
02686                                     makeArrayRef(unwrap(Args), NumArgs),
02687                                     Name));
02688 }
02689 
02690 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
02691                              LLVMValueRef Then, LLVMValueRef Else,
02692                              const char *Name) {
02693   return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
02694                                       Name));
02695 }
02696 
02697 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
02698                             LLVMTypeRef Ty, const char *Name) {
02699   return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
02700 }
02701 
02702 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
02703                                       LLVMValueRef Index, const char *Name) {
02704   return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
02705                                               Name));
02706 }
02707 
02708 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
02709                                     LLVMValueRef EltVal, LLVMValueRef Index,
02710                                     const char *Name) {
02711   return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
02712                                              unwrap(Index), Name));
02713 }
02714 
02715 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
02716                                     LLVMValueRef V2, LLVMValueRef Mask,
02717                                     const char *Name) {
02718   return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
02719                                              unwrap(Mask), Name));
02720 }
02721 
02722 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
02723                                    unsigned Index, const char *Name) {
02724   return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
02725 }
02726 
02727 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
02728                                   LLVMValueRef EltVal, unsigned Index,
02729                                   const char *Name) {
02730   return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
02731                                            Index, Name));
02732 }
02733 
02734 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
02735                              const char *Name) {
02736   return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
02737 }
02738 
02739 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
02740                                 const char *Name) {
02741   return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
02742 }
02743 
02744 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
02745                               LLVMValueRef RHS, const char *Name) {
02746   return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
02747 }
02748 
02749 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
02750                                LLVMValueRef PTR, LLVMValueRef Val,
02751                                LLVMAtomicOrdering ordering,
02752                                LLVMBool singleThread) {
02753   AtomicRMWInst::BinOp intop;
02754   switch (op) {
02755     case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
02756     case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
02757     case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
02758     case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
02759     case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
02760     case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
02761     case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
02762     case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
02763     case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
02764     case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
02765     case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
02766   }
02767   return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
02768     mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
02769 }
02770 
02771 
02772 /*===-- Module providers --------------------------------------------------===*/
02773 
02774 LLVMModuleProviderRef
02775 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
02776   return reinterpret_cast<LLVMModuleProviderRef>(M);
02777 }
02778 
02779 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
02780   delete unwrap(MP);
02781 }
02782 
02783 
02784 /*===-- Memory buffers ----------------------------------------------------===*/
02785 
02786 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
02787     const char *Path,
02788     LLVMMemoryBufferRef *OutMemBuf,
02789     char **OutMessage) {
02790 
02791   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path);
02792   if (std::error_code EC = MBOrErr.getError()) {
02793     *OutMessage = strdup(EC.message().c_str());
02794     return 1;
02795   }
02796   *OutMemBuf = wrap(MBOrErr.get().release());
02797   return 0;
02798 }
02799 
02800 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
02801                                          char **OutMessage) {
02802   ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN();
02803   if (std::error_code EC = MBOrErr.getError()) {
02804     *OutMessage = strdup(EC.message().c_str());
02805     return 1;
02806   }
02807   *OutMemBuf = wrap(MBOrErr.get().release());
02808   return 0;
02809 }
02810 
02811 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
02812     const char *InputData,
02813     size_t InputDataLength,
02814     const char *BufferName,
02815     LLVMBool RequiresNullTerminator) {
02816 
02817   return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength),
02818                                          StringRef(BufferName),
02819                                          RequiresNullTerminator).release());
02820 }
02821 
02822 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
02823     const char *InputData,
02824     size_t InputDataLength,
02825     const char *BufferName) {
02826 
02827   return wrap(
02828       MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength),
02829                                      StringRef(BufferName)).release());
02830 }
02831 
02832 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
02833   return unwrap(MemBuf)->getBufferStart();
02834 }
02835 
02836 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
02837   return unwrap(MemBuf)->getBufferSize();
02838 }
02839 
02840 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
02841   delete unwrap(MemBuf);
02842 }
02843 
02844 /*===-- Pass Registry -----------------------------------------------------===*/
02845 
02846 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
02847   return wrap(PassRegistry::getPassRegistry());
02848 }
02849 
02850 /*===-- Pass Manager ------------------------------------------------------===*/
02851 
02852 LLVMPassManagerRef LLVMCreatePassManager() {
02853   return wrap(new legacy::PassManager());
02854 }
02855 
02856 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
02857   return wrap(new legacy::FunctionPassManager(unwrap(M)));
02858 }
02859 
02860 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
02861   return LLVMCreateFunctionPassManagerForModule(
02862                                             reinterpret_cast<LLVMModuleRef>(P));
02863 }
02864 
02865 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
02866   return unwrap<legacy::PassManager>(PM)->run(*unwrap(M));
02867 }
02868 
02869 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
02870   return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization();
02871 }
02872 
02873 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
02874   return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
02875 }
02876 
02877 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
02878   return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization();
02879 }
02880 
02881 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
02882   delete unwrap(PM);
02883 }
02884 
02885 /*===-- Threading ------------------------------------------------------===*/
02886 
02887 LLVMBool LLVMStartMultithreaded() {
02888   return LLVMIsMultithreaded();
02889 }
02890 
02891 void LLVMStopMultithreaded() {
02892 }
02893 
02894 LLVMBool LLVMIsMultithreaded() {
02895   return llvm_is_multithreaded();
02896 }