1 //===-- Core.cpp ----------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the common infrastructure (including the C bindings) 11 // for libLLVMCore.a, which implements the LLVM intermediate representation. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm-c/Core.h" 16 #include "llvm/ADT/StringSwitch.h" 17 #include "llvm/Bitcode/BitcodeReader.h" 18 #include "llvm/IR/Attributes.h" 19 #include "AttributeSetNode.h" 20 #include "llvm/IR/CallSite.h" 21 #include "llvm/IR/Constants.h" 22 #include "llvm/IR/DerivedTypes.h" 23 #include "llvm/IR/DiagnosticInfo.h" 24 #include "llvm/IR/DiagnosticPrinter.h" 25 #include "llvm/IR/GlobalAlias.h" 26 #include "llvm/IR/GlobalVariable.h" 27 #include "llvm/IR/IRBuilder.h" 28 #include "llvm/IR/InlineAsm.h" 29 #include "llvm/IR/IntrinsicInst.h" 30 #include "llvm/IR/LLVMContext.h" 31 #include "llvm/IR/LegacyPassManager.h" 32 #include "llvm/IR/Module.h" 33 #include "llvm/Support/Debug.h" 34 #include "llvm/Support/ErrorHandling.h" 35 #include "llvm/Support/FileSystem.h" 36 #include "llvm/Support/ManagedStatic.h" 37 #include "llvm/Support/MemoryBuffer.h" 38 #include "llvm/Support/Threading.h" 39 #include "llvm/Support/raw_ostream.h" 40 #include <cassert> 41 #include <cstdlib> 42 #include <cstring> 43 #include <system_error> 44 45 using namespace llvm; 46 47 #define DEBUG_TYPE "ir" 48 49 void llvm::initializeCore(PassRegistry &Registry) { 50 initializeDominatorTreeWrapperPassPass(Registry); 51 initializePrintModulePassWrapperPass(Registry); 52 initializePrintFunctionPassWrapperPass(Registry); 53 initializePrintBasicBlockPassPass(Registry); 54 initializeVerifierLegacyPassPass(Registry); 55 } 56 57 void LLVMInitializeCore(LLVMPassRegistryRef R) { 58 initializeCore(*unwrap(R)); 59 } 60 61 void LLVMShutdown() { 62 llvm_shutdown(); 63 } 64 65 /*===-- Error handling ----------------------------------------------------===*/ 66 67 char *LLVMCreateMessage(const char *Message) { 68 return strdup(Message); 69 } 70 71 void LLVMDisposeMessage(char *Message) { 72 free(Message); 73 } 74 75 76 /*===-- Operations on contexts --------------------------------------------===*/ 77 78 static ManagedStatic<LLVMContext> GlobalContext; 79 80 LLVMContextRef LLVMContextCreate() { 81 return wrap(new LLVMContext()); 82 } 83 84 LLVMContextRef LLVMGetGlobalContext() { return wrap(&*GlobalContext); } 85 86 void LLVMContextSetDiagnosticHandler(LLVMContextRef C, 87 LLVMDiagnosticHandler Handler, 88 void *DiagnosticContext) { 89 unwrap(C)->setDiagnosticHandler( 90 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>( 91 Handler), 92 DiagnosticContext); 93 } 94 95 LLVMDiagnosticHandler LLVMContextGetDiagnosticHandler(LLVMContextRef C) { 96 return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>( 97 unwrap(C)->getDiagnosticHandler()); 98 } 99 100 void *LLVMContextGetDiagnosticContext(LLVMContextRef C) { 101 return unwrap(C)->getDiagnosticContext(); 102 } 103 104 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback, 105 void *OpaqueHandle) { 106 auto YieldCallback = 107 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback); 108 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle); 109 } 110 111 void LLVMContextDispose(LLVMContextRef C) { 112 delete unwrap(C); 113 } 114 115 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char *Name, 116 unsigned SLen) { 117 return unwrap(C)->getMDKindID(StringRef(Name, SLen)); 118 } 119 120 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) { 121 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen); 122 } 123 124 #define GET_ATTR_KIND_FROM_NAME 125 #include "AttributesCompatFunc.inc" 126 127 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) { 128 return getAttrKindFromName(StringRef(Name, SLen)); 129 } 130 131 unsigned LLVMGetLastEnumAttributeKind(void) { 132 return Attribute::AttrKind::EndAttrKinds; 133 } 134 135 LLVMAttributeRef LLVMCreateEnumAttribute(LLVMContextRef C, unsigned KindID, 136 uint64_t Val) { 137 return wrap(Attribute::get(*unwrap(C), (Attribute::AttrKind)KindID, Val)); 138 } 139 140 unsigned LLVMGetEnumAttributeKind(LLVMAttributeRef A) { 141 return unwrap(A).getKindAsEnum(); 142 } 143 144 uint64_t LLVMGetEnumAttributeValue(LLVMAttributeRef A) { 145 auto Attr = unwrap(A); 146 if (Attr.isEnumAttribute()) 147 return 0; 148 return Attr.getValueAsInt(); 149 } 150 151 LLVMAttributeRef LLVMCreateStringAttribute(LLVMContextRef C, 152 const char *K, unsigned KLength, 153 const char *V, unsigned VLength) { 154 return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength), 155 StringRef(V, VLength))); 156 } 157 158 const char *LLVMGetStringAttributeKind(LLVMAttributeRef A, 159 unsigned *Length) { 160 auto S = unwrap(A).getKindAsString(); 161 *Length = S.size(); 162 return S.data(); 163 } 164 165 const char *LLVMGetStringAttributeValue(LLVMAttributeRef A, 166 unsigned *Length) { 167 auto S = unwrap(A).getValueAsString(); 168 *Length = S.size(); 169 return S.data(); 170 } 171 172 LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A) { 173 auto Attr = unwrap(A); 174 return Attr.isEnumAttribute() || Attr.isIntAttribute(); 175 } 176 177 LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A) { 178 return unwrap(A).isStringAttribute(); 179 } 180 181 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) { 182 std::string MsgStorage; 183 raw_string_ostream Stream(MsgStorage); 184 DiagnosticPrinterRawOStream DP(Stream); 185 186 unwrap(DI)->print(DP); 187 Stream.flush(); 188 189 return LLVMCreateMessage(MsgStorage.c_str()); 190 } 191 192 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI) { 193 LLVMDiagnosticSeverity severity; 194 195 switch(unwrap(DI)->getSeverity()) { 196 default: 197 severity = LLVMDSError; 198 break; 199 case DS_Warning: 200 severity = LLVMDSWarning; 201 break; 202 case DS_Remark: 203 severity = LLVMDSRemark; 204 break; 205 case DS_Note: 206 severity = LLVMDSNote; 207 break; 208 } 209 210 return severity; 211 } 212 213 /*===-- Operations on modules ---------------------------------------------===*/ 214 215 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) { 216 return wrap(new Module(ModuleID, *GlobalContext)); 217 } 218 219 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID, 220 LLVMContextRef C) { 221 return wrap(new Module(ModuleID, *unwrap(C))); 222 } 223 224 void LLVMDisposeModule(LLVMModuleRef M) { 225 delete unwrap(M); 226 } 227 228 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) { 229 auto &Str = unwrap(M)->getModuleIdentifier(); 230 *Len = Str.length(); 231 return Str.c_str(); 232 } 233 234 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) { 235 unwrap(M)->setModuleIdentifier(StringRef(Ident, Len)); 236 } 237 238 239 /*--.. Data layout .........................................................--*/ 240 const char *LLVMGetDataLayoutStr(LLVMModuleRef M) { 241 return unwrap(M)->getDataLayoutStr().c_str(); 242 } 243 244 const char *LLVMGetDataLayout(LLVMModuleRef M) { 245 return LLVMGetDataLayoutStr(M); 246 } 247 248 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) { 249 unwrap(M)->setDataLayout(DataLayoutStr); 250 } 251 252 /*--.. Target triple .......................................................--*/ 253 const char * LLVMGetTarget(LLVMModuleRef M) { 254 return unwrap(M)->getTargetTriple().c_str(); 255 } 256 257 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) { 258 unwrap(M)->setTargetTriple(Triple); 259 } 260 261 void LLVMDumpModule(LLVMModuleRef M) { 262 unwrap(M)->print(errs(), nullptr, 263 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true); 264 } 265 266 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, 267 char **ErrorMessage) { 268 std::error_code EC; 269 raw_fd_ostream dest(Filename, EC, sys::fs::F_Text); 270 if (EC) { 271 *ErrorMessage = strdup(EC.message().c_str()); 272 return true; 273 } 274 275 unwrap(M)->print(dest, nullptr); 276 277 dest.close(); 278 279 if (dest.has_error()) { 280 *ErrorMessage = strdup("Error printing to file"); 281 return true; 282 } 283 284 return false; 285 } 286 287 char *LLVMPrintModuleToString(LLVMModuleRef M) { 288 std::string buf; 289 raw_string_ostream os(buf); 290 291 unwrap(M)->print(os, nullptr); 292 os.flush(); 293 294 return strdup(buf.c_str()); 295 } 296 297 /*--.. Operations on inline assembler ......................................--*/ 298 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) { 299 unwrap(M)->setModuleInlineAsm(StringRef(Asm)); 300 } 301 302 303 /*--.. Operations on module contexts ......................................--*/ 304 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) { 305 return wrap(&unwrap(M)->getContext()); 306 } 307 308 309 /*===-- Operations on types -----------------------------------------------===*/ 310 311 /*--.. Operations on all types (mostly) ....................................--*/ 312 313 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) { 314 switch (unwrap(Ty)->getTypeID()) { 315 case Type::VoidTyID: 316 return LLVMVoidTypeKind; 317 case Type::HalfTyID: 318 return LLVMHalfTypeKind; 319 case Type::FloatTyID: 320 return LLVMFloatTypeKind; 321 case Type::DoubleTyID: 322 return LLVMDoubleTypeKind; 323 case Type::X86_FP80TyID: 324 return LLVMX86_FP80TypeKind; 325 case Type::FP128TyID: 326 return LLVMFP128TypeKind; 327 case Type::PPC_FP128TyID: 328 return LLVMPPC_FP128TypeKind; 329 case Type::LabelTyID: 330 return LLVMLabelTypeKind; 331 case Type::MetadataTyID: 332 return LLVMMetadataTypeKind; 333 case Type::IntegerTyID: 334 return LLVMIntegerTypeKind; 335 case Type::FunctionTyID: 336 return LLVMFunctionTypeKind; 337 case Type::StructTyID: 338 return LLVMStructTypeKind; 339 case Type::ArrayTyID: 340 return LLVMArrayTypeKind; 341 case Type::PointerTyID: 342 return LLVMPointerTypeKind; 343 case Type::VectorTyID: 344 return LLVMVectorTypeKind; 345 case Type::X86_MMXTyID: 346 return LLVMX86_MMXTypeKind; 347 case Type::TokenTyID: 348 return LLVMTokenTypeKind; 349 } 350 llvm_unreachable("Unhandled TypeID."); 351 } 352 353 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty) 354 { 355 return unwrap(Ty)->isSized(); 356 } 357 358 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) { 359 return wrap(&unwrap(Ty)->getContext()); 360 } 361 362 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 363 LLVM_DUMP_METHOD void LLVMDumpType(LLVMTypeRef Ty) { 364 return unwrap(Ty)->dump(); 365 } 366 #endif 367 368 char *LLVMPrintTypeToString(LLVMTypeRef Ty) { 369 std::string buf; 370 raw_string_ostream os(buf); 371 372 if (unwrap(Ty)) 373 unwrap(Ty)->print(os); 374 else 375 os << "Printing <null> Type"; 376 377 os.flush(); 378 379 return strdup(buf.c_str()); 380 } 381 382 /*--.. Operations on integer types .........................................--*/ 383 384 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) { 385 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C)); 386 } 387 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) { 388 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C)); 389 } 390 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) { 391 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C)); 392 } 393 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) { 394 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C)); 395 } 396 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) { 397 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C)); 398 } 399 LLVMTypeRef LLVMInt128TypeInContext(LLVMContextRef C) { 400 return (LLVMTypeRef) Type::getInt128Ty(*unwrap(C)); 401 } 402 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) { 403 return wrap(IntegerType::get(*unwrap(C), NumBits)); 404 } 405 406 LLVMTypeRef LLVMInt1Type(void) { 407 return LLVMInt1TypeInContext(LLVMGetGlobalContext()); 408 } 409 LLVMTypeRef LLVMInt8Type(void) { 410 return LLVMInt8TypeInContext(LLVMGetGlobalContext()); 411 } 412 LLVMTypeRef LLVMInt16Type(void) { 413 return LLVMInt16TypeInContext(LLVMGetGlobalContext()); 414 } 415 LLVMTypeRef LLVMInt32Type(void) { 416 return LLVMInt32TypeInContext(LLVMGetGlobalContext()); 417 } 418 LLVMTypeRef LLVMInt64Type(void) { 419 return LLVMInt64TypeInContext(LLVMGetGlobalContext()); 420 } 421 LLVMTypeRef LLVMInt128Type(void) { 422 return LLVMInt128TypeInContext(LLVMGetGlobalContext()); 423 } 424 LLVMTypeRef LLVMIntType(unsigned NumBits) { 425 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits); 426 } 427 428 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) { 429 return unwrap<IntegerType>(IntegerTy)->getBitWidth(); 430 } 431 432 /*--.. Operations on real types ............................................--*/ 433 434 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) { 435 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C)); 436 } 437 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) { 438 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C)); 439 } 440 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) { 441 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C)); 442 } 443 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) { 444 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C)); 445 } 446 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) { 447 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C)); 448 } 449 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) { 450 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C)); 451 } 452 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) { 453 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C)); 454 } 455 LLVMTypeRef LLVMTokenTypeInContext(LLVMContextRef C) { 456 return (LLVMTypeRef) Type::getTokenTy(*unwrap(C)); 457 } 458 459 LLVMTypeRef LLVMHalfType(void) { 460 return LLVMHalfTypeInContext(LLVMGetGlobalContext()); 461 } 462 LLVMTypeRef LLVMFloatType(void) { 463 return LLVMFloatTypeInContext(LLVMGetGlobalContext()); 464 } 465 LLVMTypeRef LLVMDoubleType(void) { 466 return LLVMDoubleTypeInContext(LLVMGetGlobalContext()); 467 } 468 LLVMTypeRef LLVMX86FP80Type(void) { 469 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext()); 470 } 471 LLVMTypeRef LLVMFP128Type(void) { 472 return LLVMFP128TypeInContext(LLVMGetGlobalContext()); 473 } 474 LLVMTypeRef LLVMPPCFP128Type(void) { 475 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext()); 476 } 477 LLVMTypeRef LLVMX86MMXType(void) { 478 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext()); 479 } 480 481 /*--.. Operations on function types ........................................--*/ 482 483 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType, 484 LLVMTypeRef *ParamTypes, unsigned ParamCount, 485 LLVMBool IsVarArg) { 486 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount); 487 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0)); 488 } 489 490 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) { 491 return unwrap<FunctionType>(FunctionTy)->isVarArg(); 492 } 493 494 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) { 495 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType()); 496 } 497 498 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) { 499 return unwrap<FunctionType>(FunctionTy)->getNumParams(); 500 } 501 502 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) { 503 FunctionType *Ty = unwrap<FunctionType>(FunctionTy); 504 for (FunctionType::param_iterator I = Ty->param_begin(), 505 E = Ty->param_end(); I != E; ++I) 506 *Dest++ = wrap(*I); 507 } 508 509 /*--.. Operations on struct types ..........................................--*/ 510 511 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes, 512 unsigned ElementCount, LLVMBool Packed) { 513 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount); 514 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0)); 515 } 516 517 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes, 518 unsigned ElementCount, LLVMBool Packed) { 519 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes, 520 ElementCount, Packed); 521 } 522 523 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name) 524 { 525 return wrap(StructType::create(*unwrap(C), Name)); 526 } 527 528 const char *LLVMGetStructName(LLVMTypeRef Ty) 529 { 530 StructType *Type = unwrap<StructType>(Ty); 531 if (!Type->hasName()) 532 return nullptr; 533 return Type->getName().data(); 534 } 535 536 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes, 537 unsigned ElementCount, LLVMBool Packed) { 538 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount); 539 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0); 540 } 541 542 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) { 543 return unwrap<StructType>(StructTy)->getNumElements(); 544 } 545 546 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) { 547 StructType *Ty = unwrap<StructType>(StructTy); 548 for (StructType::element_iterator I = Ty->element_begin(), 549 E = Ty->element_end(); I != E; ++I) 550 *Dest++ = wrap(*I); 551 } 552 553 LLVMTypeRef LLVMStructGetTypeAtIndex(LLVMTypeRef StructTy, unsigned i) { 554 StructType *Ty = unwrap<StructType>(StructTy); 555 return wrap(Ty->getTypeAtIndex(i)); 556 } 557 558 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) { 559 return unwrap<StructType>(StructTy)->isPacked(); 560 } 561 562 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) { 563 return unwrap<StructType>(StructTy)->isOpaque(); 564 } 565 566 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) { 567 return wrap(unwrap(M)->getTypeByName(Name)); 568 } 569 570 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/ 571 572 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) { 573 return wrap(ArrayType::get(unwrap(ElementType), ElementCount)); 574 } 575 576 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) { 577 return wrap(PointerType::get(unwrap(ElementType), AddressSpace)); 578 } 579 580 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) { 581 return wrap(VectorType::get(unwrap(ElementType), ElementCount)); 582 } 583 584 LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) { 585 auto *Ty = unwrap<Type>(WrappedTy); 586 if (auto *PTy = dyn_cast<PointerType>(Ty)) 587 return wrap(PTy->getElementType()); 588 return wrap(cast<SequentialType>(Ty)->getElementType()); 589 } 590 591 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) { 592 return unwrap<ArrayType>(ArrayTy)->getNumElements(); 593 } 594 595 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) { 596 return unwrap<PointerType>(PointerTy)->getAddressSpace(); 597 } 598 599 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) { 600 return unwrap<VectorType>(VectorTy)->getNumElements(); 601 } 602 603 /*--.. Operations on other types ...........................................--*/ 604 605 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) { 606 return wrap(Type::getVoidTy(*unwrap(C))); 607 } 608 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) { 609 return wrap(Type::getLabelTy(*unwrap(C))); 610 } 611 612 LLVMTypeRef LLVMVoidType(void) { 613 return LLVMVoidTypeInContext(LLVMGetGlobalContext()); 614 } 615 LLVMTypeRef LLVMLabelType(void) { 616 return LLVMLabelTypeInContext(LLVMGetGlobalContext()); 617 } 618 619 /*===-- Operations on values ----------------------------------------------===*/ 620 621 /*--.. Operations on all values ............................................--*/ 622 623 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) { 624 return wrap(unwrap(Val)->getType()); 625 } 626 627 LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) { 628 switch(unwrap(Val)->getValueID()) { 629 #define HANDLE_VALUE(Name) \ 630 case Value::Name##Val: \ 631 return LLVM##Name##ValueKind; 632 #include "llvm/IR/Value.def" 633 default: 634 return LLVMInstructionValueKind; 635 } 636 } 637 638 const char *LLVMGetValueName(LLVMValueRef Val) { 639 return unwrap(Val)->getName().data(); 640 } 641 642 void LLVMSetValueName(LLVMValueRef Val, const char *Name) { 643 unwrap(Val)->setName(Name); 644 } 645 646 LLVM_DUMP_METHOD void LLVMDumpValue(LLVMValueRef Val) { 647 unwrap(Val)->print(errs(), /*IsForDebug=*/true); 648 } 649 650 char* LLVMPrintValueToString(LLVMValueRef Val) { 651 std::string buf; 652 raw_string_ostream os(buf); 653 654 if (unwrap(Val)) 655 unwrap(Val)->print(os); 656 else 657 os << "Printing <null> Value"; 658 659 os.flush(); 660 661 return strdup(buf.c_str()); 662 } 663 664 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) { 665 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal)); 666 } 667 668 int LLVMHasMetadata(LLVMValueRef Inst) { 669 return unwrap<Instruction>(Inst)->hasMetadata(); 670 } 671 672 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) { 673 auto *I = unwrap<Instruction>(Inst); 674 assert(I && "Expected instruction"); 675 if (auto *MD = I->getMetadata(KindID)) 676 return wrap(MetadataAsValue::get(I->getContext(), MD)); 677 return nullptr; 678 } 679 680 // MetadataAsValue uses a canonical format which strips the actual MDNode for 681 // MDNode with just a single constant value, storing just a ConstantAsMetadata 682 // This undoes this canonicalization, reconstructing the MDNode. 683 static MDNode *extractMDNode(MetadataAsValue *MAV) { 684 Metadata *MD = MAV->getMetadata(); 685 assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) && 686 "Expected a metadata node or a canonicalized constant"); 687 688 if (MDNode *N = dyn_cast<MDNode>(MD)) 689 return N; 690 691 return MDNode::get(MAV->getContext(), MD); 692 } 693 694 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) { 695 MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr; 696 697 unwrap<Instruction>(Inst)->setMetadata(KindID, N); 698 } 699 700 /*--.. Conversion functions ................................................--*/ 701 702 #define LLVM_DEFINE_VALUE_CAST(name) \ 703 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \ 704 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \ 705 } 706 707 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST) 708 709 LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val) { 710 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val))) 711 if (isa<MDNode>(MD->getMetadata()) || 712 isa<ValueAsMetadata>(MD->getMetadata())) 713 return Val; 714 return nullptr; 715 } 716 717 LLVMValueRef LLVMIsAMDString(LLVMValueRef Val) { 718 if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val))) 719 if (isa<MDString>(MD->getMetadata())) 720 return Val; 721 return nullptr; 722 } 723 724 /*--.. Operations on Uses ..................................................--*/ 725 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) { 726 Value *V = unwrap(Val); 727 Value::use_iterator I = V->use_begin(); 728 if (I == V->use_end()) 729 return nullptr; 730 return wrap(&*I); 731 } 732 733 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) { 734 Use *Next = unwrap(U)->getNext(); 735 if (Next) 736 return wrap(Next); 737 return nullptr; 738 } 739 740 LLVMValueRef LLVMGetUser(LLVMUseRef U) { 741 return wrap(unwrap(U)->getUser()); 742 } 743 744 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) { 745 return wrap(unwrap(U)->get()); 746 } 747 748 /*--.. Operations on Users .................................................--*/ 749 750 static LLVMValueRef getMDNodeOperandImpl(LLVMContext &Context, const MDNode *N, 751 unsigned Index) { 752 Metadata *Op = N->getOperand(Index); 753 if (!Op) 754 return nullptr; 755 if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) 756 return wrap(C->getValue()); 757 return wrap(MetadataAsValue::get(Context, Op)); 758 } 759 760 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) { 761 Value *V = unwrap(Val); 762 if (auto *MD = dyn_cast<MetadataAsValue>(V)) { 763 if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) { 764 assert(Index == 0 && "Function-local metadata can only have one operand"); 765 return wrap(L->getValue()); 766 } 767 return getMDNodeOperandImpl(V->getContext(), 768 cast<MDNode>(MD->getMetadata()), Index); 769 } 770 771 return wrap(cast<User>(V)->getOperand(Index)); 772 } 773 774 LLVMUseRef LLVMGetOperandUse(LLVMValueRef Val, unsigned Index) { 775 Value *V = unwrap(Val); 776 return wrap(&cast<User>(V)->getOperandUse(Index)); 777 } 778 779 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) { 780 unwrap<User>(Val)->setOperand(Index, unwrap(Op)); 781 } 782 783 int LLVMGetNumOperands(LLVMValueRef Val) { 784 Value *V = unwrap(Val); 785 if (isa<MetadataAsValue>(V)) 786 return LLVMGetMDNodeNumOperands(Val); 787 788 return cast<User>(V)->getNumOperands(); 789 } 790 791 /*--.. Operations on constants of any type .................................--*/ 792 793 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) { 794 return wrap(Constant::getNullValue(unwrap(Ty))); 795 } 796 797 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) { 798 return wrap(Constant::getAllOnesValue(unwrap(Ty))); 799 } 800 801 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) { 802 return wrap(UndefValue::get(unwrap(Ty))); 803 } 804 805 LLVMBool LLVMIsConstant(LLVMValueRef Ty) { 806 return isa<Constant>(unwrap(Ty)); 807 } 808 809 LLVMBool LLVMIsNull(LLVMValueRef Val) { 810 if (Constant *C = dyn_cast<Constant>(unwrap(Val))) 811 return C->isNullValue(); 812 return false; 813 } 814 815 LLVMBool LLVMIsUndef(LLVMValueRef Val) { 816 return isa<UndefValue>(unwrap(Val)); 817 } 818 819 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) { 820 return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty))); 821 } 822 823 /*--.. Operations on metadata nodes ........................................--*/ 824 825 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str, 826 unsigned SLen) { 827 LLVMContext &Context = *unwrap(C); 828 return wrap(MetadataAsValue::get( 829 Context, MDString::get(Context, StringRef(Str, SLen)))); 830 } 831 832 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) { 833 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen); 834 } 835 836 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, 837 unsigned Count) { 838 LLVMContext &Context = *unwrap(C); 839 SmallVector<Metadata *, 8> MDs; 840 for (auto *OV : makeArrayRef(Vals, Count)) { 841 Value *V = unwrap(OV); 842 Metadata *MD; 843 if (!V) 844 MD = nullptr; 845 else if (auto *C = dyn_cast<Constant>(V)) 846 MD = ConstantAsMetadata::get(C); 847 else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) { 848 MD = MDV->getMetadata(); 849 assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata " 850 "outside of direct argument to call"); 851 } else { 852 // This is function-local metadata. Pretend to make an MDNode. 853 assert(Count == 1 && 854 "Expected only one operand to function-local metadata"); 855 return wrap(MetadataAsValue::get(Context, LocalAsMetadata::get(V))); 856 } 857 858 MDs.push_back(MD); 859 } 860 return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs))); 861 } 862 863 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) { 864 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count); 865 } 866 867 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) { 868 if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V))) 869 if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) { 870 *Length = S->getString().size(); 871 return S->getString().data(); 872 } 873 *Length = 0; 874 return nullptr; 875 } 876 877 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V) { 878 auto *MD = cast<MetadataAsValue>(unwrap(V)); 879 if (isa<ValueAsMetadata>(MD->getMetadata())) 880 return 1; 881 return cast<MDNode>(MD->getMetadata())->getNumOperands(); 882 } 883 884 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) { 885 auto *MD = cast<MetadataAsValue>(unwrap(V)); 886 if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) { 887 *Dest = wrap(MDV->getValue()); 888 return; 889 } 890 const auto *N = cast<MDNode>(MD->getMetadata()); 891 const unsigned numOperands = N->getNumOperands(); 892 LLVMContext &Context = unwrap(V)->getContext(); 893 for (unsigned i = 0; i < numOperands; i++) 894 Dest[i] = getMDNodeOperandImpl(Context, N, i); 895 } 896 897 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char *Name) { 898 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) { 899 return N->getNumOperands(); 900 } 901 return 0; 902 } 903 904 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char *Name, 905 LLVMValueRef *Dest) { 906 NamedMDNode *N = unwrap(M)->getNamedMetadata(Name); 907 if (!N) 908 return; 909 LLVMContext &Context = unwrap(M)->getContext(); 910 for (unsigned i=0;i<N->getNumOperands();i++) 911 Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i))); 912 } 913 914 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char *Name, 915 LLVMValueRef Val) { 916 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name); 917 if (!N) 918 return; 919 if (!Val) 920 return; 921 N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val))); 922 } 923 924 /*--.. Operations on scalar constants ......................................--*/ 925 926 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N, 927 LLVMBool SignExtend) { 928 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0)); 929 } 930 931 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy, 932 unsigned NumWords, 933 const uint64_t Words[]) { 934 IntegerType *Ty = unwrap<IntegerType>(IntTy); 935 return wrap(ConstantInt::get(Ty->getContext(), 936 APInt(Ty->getBitWidth(), 937 makeArrayRef(Words, NumWords)))); 938 } 939 940 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[], 941 uint8_t Radix) { 942 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str), 943 Radix)); 944 } 945 946 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[], 947 unsigned SLen, uint8_t Radix) { 948 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen), 949 Radix)); 950 } 951 952 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) { 953 return wrap(ConstantFP::get(unwrap(RealTy), N)); 954 } 955 956 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) { 957 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text))); 958 } 959 960 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[], 961 unsigned SLen) { 962 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen))); 963 } 964 965 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) { 966 return unwrap<ConstantInt>(ConstantVal)->getZExtValue(); 967 } 968 969 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) { 970 return unwrap<ConstantInt>(ConstantVal)->getSExtValue(); 971 } 972 973 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) { 974 ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ; 975 Type *Ty = cFP->getType(); 976 977 if (Ty->isFloatTy()) { 978 *LosesInfo = false; 979 return cFP->getValueAPF().convertToFloat(); 980 } 981 982 if (Ty->isDoubleTy()) { 983 *LosesInfo = false; 984 return cFP->getValueAPF().convertToDouble(); 985 } 986 987 bool APFLosesInfo; 988 APFloat APF = cFP->getValueAPF(); 989 APF.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &APFLosesInfo); 990 *LosesInfo = APFLosesInfo; 991 return APF.convertToDouble(); 992 } 993 994 /*--.. Operations on composite constants ...................................--*/ 995 996 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str, 997 unsigned Length, 998 LLVMBool DontNullTerminate) { 999 /* Inverted the sense of AddNull because ', 0)' is a 1000 better mnemonic for null termination than ', 1)'. */ 1001 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length), 1002 DontNullTerminate == 0)); 1003 } 1004 1005 LLVMValueRef LLVMConstString(const char *Str, unsigned Length, 1006 LLVMBool DontNullTerminate) { 1007 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length, 1008 DontNullTerminate); 1009 } 1010 1011 LLVMValueRef LLVMGetElementAsConstant(LLVMValueRef C, unsigned idx) { 1012 return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx)); 1013 } 1014 1015 LLVMBool LLVMIsConstantString(LLVMValueRef C) { 1016 return unwrap<ConstantDataSequential>(C)->isString(); 1017 } 1018 1019 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) { 1020 StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString(); 1021 *Length = Str.size(); 1022 return Str.data(); 1023 } 1024 1025 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy, 1026 LLVMValueRef *ConstantVals, unsigned Length) { 1027 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length); 1028 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V)); 1029 } 1030 1031 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C, 1032 LLVMValueRef *ConstantVals, 1033 unsigned Count, LLVMBool Packed) { 1034 Constant **Elements = unwrap<Constant>(ConstantVals, Count); 1035 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count), 1036 Packed != 0)); 1037 } 1038 1039 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count, 1040 LLVMBool Packed) { 1041 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count, 1042 Packed); 1043 } 1044 1045 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy, 1046 LLVMValueRef *ConstantVals, 1047 unsigned Count) { 1048 Constant **Elements = unwrap<Constant>(ConstantVals, Count); 1049 StructType *Ty = cast<StructType>(unwrap(StructTy)); 1050 1051 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count))); 1052 } 1053 1054 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) { 1055 return wrap(ConstantVector::get(makeArrayRef( 1056 unwrap<Constant>(ScalarConstantVals, Size), Size))); 1057 } 1058 1059 /*-- Opcode mapping */ 1060 1061 static LLVMOpcode map_to_llvmopcode(int opcode) 1062 { 1063 switch (opcode) { 1064 default: llvm_unreachable("Unhandled Opcode."); 1065 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc; 1066 #include "llvm/IR/Instruction.def" 1067 #undef HANDLE_INST 1068 } 1069 } 1070 1071 static int map_from_llvmopcode(LLVMOpcode code) 1072 { 1073 switch (code) { 1074 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num; 1075 #include "llvm/IR/Instruction.def" 1076 #undef HANDLE_INST 1077 } 1078 llvm_unreachable("Unhandled Opcode."); 1079 } 1080 1081 /*--.. Constant expressions ................................................--*/ 1082 1083 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) { 1084 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode()); 1085 } 1086 1087 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) { 1088 return wrap(ConstantExpr::getAlignOf(unwrap(Ty))); 1089 } 1090 1091 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) { 1092 return wrap(ConstantExpr::getSizeOf(unwrap(Ty))); 1093 } 1094 1095 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) { 1096 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal))); 1097 } 1098 1099 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) { 1100 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal))); 1101 } 1102 1103 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) { 1104 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal))); 1105 } 1106 1107 1108 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) { 1109 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal))); 1110 } 1111 1112 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) { 1113 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal))); 1114 } 1115 1116 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1117 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant), 1118 unwrap<Constant>(RHSConstant))); 1119 } 1120 1121 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant, 1122 LLVMValueRef RHSConstant) { 1123 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant), 1124 unwrap<Constant>(RHSConstant))); 1125 } 1126 1127 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant, 1128 LLVMValueRef RHSConstant) { 1129 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant), 1130 unwrap<Constant>(RHSConstant))); 1131 } 1132 1133 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1134 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant), 1135 unwrap<Constant>(RHSConstant))); 1136 } 1137 1138 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1139 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant), 1140 unwrap<Constant>(RHSConstant))); 1141 } 1142 1143 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant, 1144 LLVMValueRef RHSConstant) { 1145 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant), 1146 unwrap<Constant>(RHSConstant))); 1147 } 1148 1149 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant, 1150 LLVMValueRef RHSConstant) { 1151 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant), 1152 unwrap<Constant>(RHSConstant))); 1153 } 1154 1155 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1156 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant), 1157 unwrap<Constant>(RHSConstant))); 1158 } 1159 1160 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1161 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant), 1162 unwrap<Constant>(RHSConstant))); 1163 } 1164 1165 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant, 1166 LLVMValueRef RHSConstant) { 1167 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant), 1168 unwrap<Constant>(RHSConstant))); 1169 } 1170 1171 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant, 1172 LLVMValueRef RHSConstant) { 1173 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant), 1174 unwrap<Constant>(RHSConstant))); 1175 } 1176 1177 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1178 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant), 1179 unwrap<Constant>(RHSConstant))); 1180 } 1181 1182 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1183 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant), 1184 unwrap<Constant>(RHSConstant))); 1185 } 1186 1187 LLVMValueRef LLVMConstExactUDiv(LLVMValueRef LHSConstant, 1188 LLVMValueRef RHSConstant) { 1189 return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant), 1190 unwrap<Constant>(RHSConstant))); 1191 } 1192 1193 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1194 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant), 1195 unwrap<Constant>(RHSConstant))); 1196 } 1197 1198 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, 1199 LLVMValueRef RHSConstant) { 1200 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant), 1201 unwrap<Constant>(RHSConstant))); 1202 } 1203 1204 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1205 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant), 1206 unwrap<Constant>(RHSConstant))); 1207 } 1208 1209 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1210 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant), 1211 unwrap<Constant>(RHSConstant))); 1212 } 1213 1214 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1215 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant), 1216 unwrap<Constant>(RHSConstant))); 1217 } 1218 1219 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1220 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant), 1221 unwrap<Constant>(RHSConstant))); 1222 } 1223 1224 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1225 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant), 1226 unwrap<Constant>(RHSConstant))); 1227 } 1228 1229 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1230 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant), 1231 unwrap<Constant>(RHSConstant))); 1232 } 1233 1234 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1235 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant), 1236 unwrap<Constant>(RHSConstant))); 1237 } 1238 1239 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate, 1240 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1241 return wrap(ConstantExpr::getICmp(Predicate, 1242 unwrap<Constant>(LHSConstant), 1243 unwrap<Constant>(RHSConstant))); 1244 } 1245 1246 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate, 1247 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1248 return wrap(ConstantExpr::getFCmp(Predicate, 1249 unwrap<Constant>(LHSConstant), 1250 unwrap<Constant>(RHSConstant))); 1251 } 1252 1253 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1254 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant), 1255 unwrap<Constant>(RHSConstant))); 1256 } 1257 1258 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1259 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant), 1260 unwrap<Constant>(RHSConstant))); 1261 } 1262 1263 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1264 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant), 1265 unwrap<Constant>(RHSConstant))); 1266 } 1267 1268 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, 1269 LLVMValueRef *ConstantIndices, unsigned NumIndices) { 1270 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1271 NumIndices); 1272 return wrap(ConstantExpr::getGetElementPtr( 1273 nullptr, unwrap<Constant>(ConstantVal), IdxList)); 1274 } 1275 1276 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, 1277 LLVMValueRef *ConstantIndices, 1278 unsigned NumIndices) { 1279 Constant* Val = unwrap<Constant>(ConstantVal); 1280 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1281 NumIndices); 1282 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList)); 1283 } 1284 1285 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1286 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal), 1287 unwrap(ToType))); 1288 } 1289 1290 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1291 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal), 1292 unwrap(ToType))); 1293 } 1294 1295 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1296 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal), 1297 unwrap(ToType))); 1298 } 1299 1300 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1301 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal), 1302 unwrap(ToType))); 1303 } 1304 1305 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1306 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal), 1307 unwrap(ToType))); 1308 } 1309 1310 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1311 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal), 1312 unwrap(ToType))); 1313 } 1314 1315 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1316 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal), 1317 unwrap(ToType))); 1318 } 1319 1320 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1321 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal), 1322 unwrap(ToType))); 1323 } 1324 1325 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1326 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal), 1327 unwrap(ToType))); 1328 } 1329 1330 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1331 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal), 1332 unwrap(ToType))); 1333 } 1334 1335 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1336 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal), 1337 unwrap(ToType))); 1338 } 1339 1340 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1341 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal), 1342 unwrap(ToType))); 1343 } 1344 1345 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal, 1346 LLVMTypeRef ToType) { 1347 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal), 1348 unwrap(ToType))); 1349 } 1350 1351 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal, 1352 LLVMTypeRef ToType) { 1353 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal), 1354 unwrap(ToType))); 1355 } 1356 1357 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal, 1358 LLVMTypeRef ToType) { 1359 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal), 1360 unwrap(ToType))); 1361 } 1362 1363 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal, 1364 LLVMTypeRef ToType) { 1365 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal), 1366 unwrap(ToType))); 1367 } 1368 1369 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal, 1370 LLVMTypeRef ToType) { 1371 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal), 1372 unwrap(ToType))); 1373 } 1374 1375 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType, 1376 LLVMBool isSigned) { 1377 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal), 1378 unwrap(ToType), isSigned)); 1379 } 1380 1381 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1382 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal), 1383 unwrap(ToType))); 1384 } 1385 1386 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition, 1387 LLVMValueRef ConstantIfTrue, 1388 LLVMValueRef ConstantIfFalse) { 1389 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition), 1390 unwrap<Constant>(ConstantIfTrue), 1391 unwrap<Constant>(ConstantIfFalse))); 1392 } 1393 1394 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant, 1395 LLVMValueRef IndexConstant) { 1396 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant), 1397 unwrap<Constant>(IndexConstant))); 1398 } 1399 1400 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, 1401 LLVMValueRef ElementValueConstant, 1402 LLVMValueRef IndexConstant) { 1403 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant), 1404 unwrap<Constant>(ElementValueConstant), 1405 unwrap<Constant>(IndexConstant))); 1406 } 1407 1408 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, 1409 LLVMValueRef VectorBConstant, 1410 LLVMValueRef MaskConstant) { 1411 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant), 1412 unwrap<Constant>(VectorBConstant), 1413 unwrap<Constant>(MaskConstant))); 1414 } 1415 1416 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, 1417 unsigned NumIdx) { 1418 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant), 1419 makeArrayRef(IdxList, NumIdx))); 1420 } 1421 1422 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant, 1423 LLVMValueRef ElementValueConstant, 1424 unsigned *IdxList, unsigned NumIdx) { 1425 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant), 1426 unwrap<Constant>(ElementValueConstant), 1427 makeArrayRef(IdxList, NumIdx))); 1428 } 1429 1430 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 1431 const char *Constraints, 1432 LLVMBool HasSideEffects, 1433 LLVMBool IsAlignStack) { 1434 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 1435 Constraints, HasSideEffects, IsAlignStack)); 1436 } 1437 1438 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) { 1439 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB))); 1440 } 1441 1442 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/ 1443 1444 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) { 1445 return wrap(unwrap<GlobalValue>(Global)->getParent()); 1446 } 1447 1448 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) { 1449 return unwrap<GlobalValue>(Global)->isDeclaration(); 1450 } 1451 1452 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) { 1453 switch (unwrap<GlobalValue>(Global)->getLinkage()) { 1454 case GlobalValue::ExternalLinkage: 1455 return LLVMExternalLinkage; 1456 case GlobalValue::AvailableExternallyLinkage: 1457 return LLVMAvailableExternallyLinkage; 1458 case GlobalValue::LinkOnceAnyLinkage: 1459 return LLVMLinkOnceAnyLinkage; 1460 case GlobalValue::LinkOnceODRLinkage: 1461 return LLVMLinkOnceODRLinkage; 1462 case GlobalValue::WeakAnyLinkage: 1463 return LLVMWeakAnyLinkage; 1464 case GlobalValue::WeakODRLinkage: 1465 return LLVMWeakODRLinkage; 1466 case GlobalValue::AppendingLinkage: 1467 return LLVMAppendingLinkage; 1468 case GlobalValue::InternalLinkage: 1469 return LLVMInternalLinkage; 1470 case GlobalValue::PrivateLinkage: 1471 return LLVMPrivateLinkage; 1472 case GlobalValue::ExternalWeakLinkage: 1473 return LLVMExternalWeakLinkage; 1474 case GlobalValue::CommonLinkage: 1475 return LLVMCommonLinkage; 1476 } 1477 1478 llvm_unreachable("Invalid GlobalValue linkage!"); 1479 } 1480 1481 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) { 1482 GlobalValue *GV = unwrap<GlobalValue>(Global); 1483 1484 switch (Linkage) { 1485 case LLVMExternalLinkage: 1486 GV->setLinkage(GlobalValue::ExternalLinkage); 1487 break; 1488 case LLVMAvailableExternallyLinkage: 1489 GV->setLinkage(GlobalValue::AvailableExternallyLinkage); 1490 break; 1491 case LLVMLinkOnceAnyLinkage: 1492 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage); 1493 break; 1494 case LLVMLinkOnceODRLinkage: 1495 GV->setLinkage(GlobalValue::LinkOnceODRLinkage); 1496 break; 1497 case LLVMLinkOnceODRAutoHideLinkage: 1498 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no " 1499 "longer supported."); 1500 break; 1501 case LLVMWeakAnyLinkage: 1502 GV->setLinkage(GlobalValue::WeakAnyLinkage); 1503 break; 1504 case LLVMWeakODRLinkage: 1505 GV->setLinkage(GlobalValue::WeakODRLinkage); 1506 break; 1507 case LLVMAppendingLinkage: 1508 GV->setLinkage(GlobalValue::AppendingLinkage); 1509 break; 1510 case LLVMInternalLinkage: 1511 GV->setLinkage(GlobalValue::InternalLinkage); 1512 break; 1513 case LLVMPrivateLinkage: 1514 GV->setLinkage(GlobalValue::PrivateLinkage); 1515 break; 1516 case LLVMLinkerPrivateLinkage: 1517 GV->setLinkage(GlobalValue::PrivateLinkage); 1518 break; 1519 case LLVMLinkerPrivateWeakLinkage: 1520 GV->setLinkage(GlobalValue::PrivateLinkage); 1521 break; 1522 case LLVMDLLImportLinkage: 1523 DEBUG(errs() 1524 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported."); 1525 break; 1526 case LLVMDLLExportLinkage: 1527 DEBUG(errs() 1528 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported."); 1529 break; 1530 case LLVMExternalWeakLinkage: 1531 GV->setLinkage(GlobalValue::ExternalWeakLinkage); 1532 break; 1533 case LLVMGhostLinkage: 1534 DEBUG(errs() 1535 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported."); 1536 break; 1537 case LLVMCommonLinkage: 1538 GV->setLinkage(GlobalValue::CommonLinkage); 1539 break; 1540 } 1541 } 1542 1543 const char *LLVMGetSection(LLVMValueRef Global) { 1544 // Using .data() is safe because of how GlobalObject::setSection is 1545 // implemented. 1546 return unwrap<GlobalValue>(Global)->getSection().data(); 1547 } 1548 1549 void LLVMSetSection(LLVMValueRef Global, const char *Section) { 1550 unwrap<GlobalObject>(Global)->setSection(Section); 1551 } 1552 1553 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) { 1554 return static_cast<LLVMVisibility>( 1555 unwrap<GlobalValue>(Global)->getVisibility()); 1556 } 1557 1558 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) { 1559 unwrap<GlobalValue>(Global) 1560 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz)); 1561 } 1562 1563 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) { 1564 return static_cast<LLVMDLLStorageClass>( 1565 unwrap<GlobalValue>(Global)->getDLLStorageClass()); 1566 } 1567 1568 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) { 1569 unwrap<GlobalValue>(Global)->setDLLStorageClass( 1570 static_cast<GlobalValue::DLLStorageClassTypes>(Class)); 1571 } 1572 1573 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) { 1574 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr(); 1575 } 1576 1577 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) { 1578 unwrap<GlobalValue>(Global)->setUnnamedAddr( 1579 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global 1580 : GlobalValue::UnnamedAddr::None); 1581 } 1582 1583 /*--.. Operations on global variables, load and store instructions .........--*/ 1584 1585 unsigned LLVMGetAlignment(LLVMValueRef V) { 1586 Value *P = unwrap<Value>(V); 1587 if (GlobalValue *GV = dyn_cast<GlobalValue>(P)) 1588 return GV->getAlignment(); 1589 if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 1590 return AI->getAlignment(); 1591 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 1592 return LI->getAlignment(); 1593 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 1594 return SI->getAlignment(); 1595 1596 llvm_unreachable( 1597 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment"); 1598 } 1599 1600 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) { 1601 Value *P = unwrap<Value>(V); 1602 if (GlobalObject *GV = dyn_cast<GlobalObject>(P)) 1603 GV->setAlignment(Bytes); 1604 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 1605 AI->setAlignment(Bytes); 1606 else if (LoadInst *LI = dyn_cast<LoadInst>(P)) 1607 LI->setAlignment(Bytes); 1608 else if (StoreInst *SI = dyn_cast<StoreInst>(P)) 1609 SI->setAlignment(Bytes); 1610 else 1611 llvm_unreachable( 1612 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment"); 1613 } 1614 1615 /*--.. Operations on global variables ......................................--*/ 1616 1617 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) { 1618 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 1619 GlobalValue::ExternalLinkage, nullptr, Name)); 1620 } 1621 1622 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty, 1623 const char *Name, 1624 unsigned AddressSpace) { 1625 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 1626 GlobalValue::ExternalLinkage, nullptr, Name, 1627 nullptr, GlobalVariable::NotThreadLocal, 1628 AddressSpace)); 1629 } 1630 1631 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) { 1632 return wrap(unwrap(M)->getNamedGlobal(Name)); 1633 } 1634 1635 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) { 1636 Module *Mod = unwrap(M); 1637 Module::global_iterator I = Mod->global_begin(); 1638 if (I == Mod->global_end()) 1639 return nullptr; 1640 return wrap(&*I); 1641 } 1642 1643 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) { 1644 Module *Mod = unwrap(M); 1645 Module::global_iterator I = Mod->global_end(); 1646 if (I == Mod->global_begin()) 1647 return nullptr; 1648 return wrap(&*--I); 1649 } 1650 1651 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) { 1652 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1653 Module::global_iterator I(GV); 1654 if (++I == GV->getParent()->global_end()) 1655 return nullptr; 1656 return wrap(&*I); 1657 } 1658 1659 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) { 1660 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1661 Module::global_iterator I(GV); 1662 if (I == GV->getParent()->global_begin()) 1663 return nullptr; 1664 return wrap(&*--I); 1665 } 1666 1667 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) { 1668 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent(); 1669 } 1670 1671 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) { 1672 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar); 1673 if ( !GV->hasInitializer() ) 1674 return nullptr; 1675 return wrap(GV->getInitializer()); 1676 } 1677 1678 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) { 1679 unwrap<GlobalVariable>(GlobalVar) 1680 ->setInitializer(unwrap<Constant>(ConstantVal)); 1681 } 1682 1683 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) { 1684 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal(); 1685 } 1686 1687 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) { 1688 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0); 1689 } 1690 1691 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) { 1692 return unwrap<GlobalVariable>(GlobalVar)->isConstant(); 1693 } 1694 1695 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) { 1696 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0); 1697 } 1698 1699 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) { 1700 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) { 1701 case GlobalVariable::NotThreadLocal: 1702 return LLVMNotThreadLocal; 1703 case GlobalVariable::GeneralDynamicTLSModel: 1704 return LLVMGeneralDynamicTLSModel; 1705 case GlobalVariable::LocalDynamicTLSModel: 1706 return LLVMLocalDynamicTLSModel; 1707 case GlobalVariable::InitialExecTLSModel: 1708 return LLVMInitialExecTLSModel; 1709 case GlobalVariable::LocalExecTLSModel: 1710 return LLVMLocalExecTLSModel; 1711 } 1712 1713 llvm_unreachable("Invalid GlobalVariable thread local mode"); 1714 } 1715 1716 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) { 1717 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1718 1719 switch (Mode) { 1720 case LLVMNotThreadLocal: 1721 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal); 1722 break; 1723 case LLVMGeneralDynamicTLSModel: 1724 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel); 1725 break; 1726 case LLVMLocalDynamicTLSModel: 1727 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel); 1728 break; 1729 case LLVMInitialExecTLSModel: 1730 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); 1731 break; 1732 case LLVMLocalExecTLSModel: 1733 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel); 1734 break; 1735 } 1736 } 1737 1738 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) { 1739 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized(); 1740 } 1741 1742 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { 1743 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit); 1744 } 1745 1746 /*--.. Operations on aliases ......................................--*/ 1747 1748 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, 1749 const char *Name) { 1750 auto *PTy = cast<PointerType>(unwrap(Ty)); 1751 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), 1752 GlobalValue::ExternalLinkage, Name, 1753 unwrap<Constant>(Aliasee), unwrap(M))); 1754 } 1755 1756 /*--.. Operations on functions .............................................--*/ 1757 1758 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, 1759 LLVMTypeRef FunctionTy) { 1760 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy), 1761 GlobalValue::ExternalLinkage, Name, unwrap(M))); 1762 } 1763 1764 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) { 1765 return wrap(unwrap(M)->getFunction(Name)); 1766 } 1767 1768 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) { 1769 Module *Mod = unwrap(M); 1770 Module::iterator I = Mod->begin(); 1771 if (I == Mod->end()) 1772 return nullptr; 1773 return wrap(&*I); 1774 } 1775 1776 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) { 1777 Module *Mod = unwrap(M); 1778 Module::iterator I = Mod->end(); 1779 if (I == Mod->begin()) 1780 return nullptr; 1781 return wrap(&*--I); 1782 } 1783 1784 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) { 1785 Function *Func = unwrap<Function>(Fn); 1786 Module::iterator I(Func); 1787 if (++I == Func->getParent()->end()) 1788 return nullptr; 1789 return wrap(&*I); 1790 } 1791 1792 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) { 1793 Function *Func = unwrap<Function>(Fn); 1794 Module::iterator I(Func); 1795 if (I == Func->getParent()->begin()) 1796 return nullptr; 1797 return wrap(&*--I); 1798 } 1799 1800 void LLVMDeleteFunction(LLVMValueRef Fn) { 1801 unwrap<Function>(Fn)->eraseFromParent(); 1802 } 1803 1804 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) { 1805 return unwrap<Function>(Fn)->hasPersonalityFn(); 1806 } 1807 1808 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) { 1809 return wrap(unwrap<Function>(Fn)->getPersonalityFn()); 1810 } 1811 1812 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) { 1813 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn)); 1814 } 1815 1816 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { 1817 if (Function *F = dyn_cast<Function>(unwrap(Fn))) 1818 return F->getIntrinsicID(); 1819 return 0; 1820 } 1821 1822 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) { 1823 return unwrap<Function>(Fn)->getCallingConv(); 1824 } 1825 1826 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { 1827 return unwrap<Function>(Fn)->setCallingConv( 1828 static_cast<CallingConv::ID>(CC)); 1829 } 1830 1831 const char *LLVMGetGC(LLVMValueRef Fn) { 1832 Function *F = unwrap<Function>(Fn); 1833 return F->hasGC()? F->getGC().c_str() : nullptr; 1834 } 1835 1836 void LLVMSetGC(LLVMValueRef Fn, const char *GC) { 1837 Function *F = unwrap<Function>(Fn); 1838 if (GC) 1839 F->setGC(GC); 1840 else 1841 F->clearGC(); 1842 } 1843 1844 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1845 LLVMAttributeRef A) { 1846 unwrap<Function>(F)->addAttribute(Idx, unwrap(A)); 1847 } 1848 1849 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) { 1850 auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx); 1851 if (!ASN) 1852 return 0; 1853 return ASN->getNumAttributes(); 1854 } 1855 1856 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1857 LLVMAttributeRef *Attrs) { 1858 auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx); 1859 if (!ASN) 1860 return; 1861 for (auto A: make_range(ASN->begin(), ASN->end())) 1862 *Attrs++ = wrap(A); 1863 } 1864 1865 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F, 1866 LLVMAttributeIndex Idx, 1867 unsigned KindID) { 1868 return wrap(unwrap<Function>(F)->getAttribute(Idx, 1869 (Attribute::AttrKind)KindID)); 1870 } 1871 1872 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F, 1873 LLVMAttributeIndex Idx, 1874 const char *K, unsigned KLen) { 1875 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen))); 1876 } 1877 1878 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1879 unsigned KindID) { 1880 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID); 1881 } 1882 1883 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1884 const char *K, unsigned KLen) { 1885 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen)); 1886 } 1887 1888 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, 1889 const char *V) { 1890 Function *Func = unwrap<Function>(Fn); 1891 AttributeSet::AttrIndex Idx = 1892 AttributeSet::AttrIndex(AttributeSet::FunctionIndex); 1893 AttrBuilder B; 1894 1895 B.addAttribute(A, V); 1896 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B); 1897 Func->addAttributes(Idx, Set); 1898 } 1899 1900 /*--.. Operations on parameters ............................................--*/ 1901 1902 unsigned LLVMCountParams(LLVMValueRef FnRef) { 1903 // This function is strictly redundant to 1904 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef))) 1905 return unwrap<Function>(FnRef)->arg_size(); 1906 } 1907 1908 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { 1909 Function *Fn = unwrap<Function>(FnRef); 1910 for (Function::arg_iterator I = Fn->arg_begin(), 1911 E = Fn->arg_end(); I != E; I++) 1912 *ParamRefs++ = wrap(&*I); 1913 } 1914 1915 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { 1916 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin(); 1917 while (index --> 0) 1918 AI++; 1919 return wrap(&*AI); 1920 } 1921 1922 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) { 1923 return wrap(unwrap<Argument>(V)->getParent()); 1924 } 1925 1926 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) { 1927 Function *Func = unwrap<Function>(Fn); 1928 Function::arg_iterator I = Func->arg_begin(); 1929 if (I == Func->arg_end()) 1930 return nullptr; 1931 return wrap(&*I); 1932 } 1933 1934 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) { 1935 Function *Func = unwrap<Function>(Fn); 1936 Function::arg_iterator I = Func->arg_end(); 1937 if (I == Func->arg_begin()) 1938 return nullptr; 1939 return wrap(&*--I); 1940 } 1941 1942 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) { 1943 Argument *A = unwrap<Argument>(Arg); 1944 Function::arg_iterator I(A); 1945 if (++I == A->getParent()->arg_end()) 1946 return nullptr; 1947 return wrap(&*I); 1948 } 1949 1950 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { 1951 Argument *A = unwrap<Argument>(Arg); 1952 Function::arg_iterator I(A); 1953 if (I == A->getParent()->arg_begin()) 1954 return nullptr; 1955 return wrap(&*--I); 1956 } 1957 1958 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { 1959 Argument *A = unwrap<Argument>(Arg); 1960 AttrBuilder B; 1961 B.addAlignmentAttr(align); 1962 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B)); 1963 } 1964 1965 /*--.. Operations on basic blocks ..........................................--*/ 1966 1967 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { 1968 return wrap(static_cast<Value*>(unwrap(BB))); 1969 } 1970 1971 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) { 1972 return isa<BasicBlock>(unwrap(Val)); 1973 } 1974 1975 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) { 1976 return wrap(unwrap<BasicBlock>(Val)); 1977 } 1978 1979 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) { 1980 return unwrap(BB)->getName().data(); 1981 } 1982 1983 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) { 1984 return wrap(unwrap(BB)->getParent()); 1985 } 1986 1987 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) { 1988 return wrap(unwrap(BB)->getTerminator()); 1989 } 1990 1991 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) { 1992 return unwrap<Function>(FnRef)->size(); 1993 } 1994 1995 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){ 1996 Function *Fn = unwrap<Function>(FnRef); 1997 for (BasicBlock &BB : *Fn) 1998 *BasicBlocksRefs++ = wrap(&BB); 1999 } 2000 2001 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) { 2002 return wrap(&unwrap<Function>(Fn)->getEntryBlock()); 2003 } 2004 2005 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) { 2006 Function *Func = unwrap<Function>(Fn); 2007 Function::iterator I = Func->begin(); 2008 if (I == Func->end()) 2009 return nullptr; 2010 return wrap(&*I); 2011 } 2012 2013 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) { 2014 Function *Func = unwrap<Function>(Fn); 2015 Function::iterator I = Func->end(); 2016 if (I == Func->begin()) 2017 return nullptr; 2018 return wrap(&*--I); 2019 } 2020 2021 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) { 2022 BasicBlock *Block = unwrap(BB); 2023 Function::iterator I(Block); 2024 if (++I == Block->getParent()->end()) 2025 return nullptr; 2026 return wrap(&*I); 2027 } 2028 2029 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) { 2030 BasicBlock *Block = unwrap(BB); 2031 Function::iterator I(Block); 2032 if (I == Block->getParent()->begin()) 2033 return nullptr; 2034 return wrap(&*--I); 2035 } 2036 2037 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, 2038 LLVMValueRef FnRef, 2039 const char *Name) { 2040 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef))); 2041 } 2042 2043 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) { 2044 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name); 2045 } 2046 2047 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, 2048 LLVMBasicBlockRef BBRef, 2049 const char *Name) { 2050 BasicBlock *BB = unwrap(BBRef); 2051 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB)); 2052 } 2053 2054 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef, 2055 const char *Name) { 2056 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name); 2057 } 2058 2059 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) { 2060 unwrap(BBRef)->eraseFromParent(); 2061 } 2062 2063 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) { 2064 unwrap(BBRef)->removeFromParent(); 2065 } 2066 2067 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2068 unwrap(BB)->moveBefore(unwrap(MovePos)); 2069 } 2070 2071 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2072 unwrap(BB)->moveAfter(unwrap(MovePos)); 2073 } 2074 2075 /*--.. Operations on instructions ..........................................--*/ 2076 2077 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) { 2078 return wrap(unwrap<Instruction>(Inst)->getParent()); 2079 } 2080 2081 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) { 2082 BasicBlock *Block = unwrap(BB); 2083 BasicBlock::iterator I = Block->begin(); 2084 if (I == Block->end()) 2085 return nullptr; 2086 return wrap(&*I); 2087 } 2088 2089 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) { 2090 BasicBlock *Block = unwrap(BB); 2091 BasicBlock::iterator I = Block->end(); 2092 if (I == Block->begin()) 2093 return nullptr; 2094 return wrap(&*--I); 2095 } 2096 2097 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) { 2098 Instruction *Instr = unwrap<Instruction>(Inst); 2099 BasicBlock::iterator I(Instr); 2100 if (++I == Instr->getParent()->end()) 2101 return nullptr; 2102 return wrap(&*I); 2103 } 2104 2105 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) { 2106 Instruction *Instr = unwrap<Instruction>(Inst); 2107 BasicBlock::iterator I(Instr); 2108 if (I == Instr->getParent()->begin()) 2109 return nullptr; 2110 return wrap(&*--I); 2111 } 2112 2113 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) { 2114 unwrap<Instruction>(Inst)->removeFromParent(); 2115 } 2116 2117 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) { 2118 unwrap<Instruction>(Inst)->eraseFromParent(); 2119 } 2120 2121 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { 2122 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst))) 2123 return (LLVMIntPredicate)I->getPredicate(); 2124 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2125 if (CE->getOpcode() == Instruction::ICmp) 2126 return (LLVMIntPredicate)CE->getPredicate(); 2127 return (LLVMIntPredicate)0; 2128 } 2129 2130 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) { 2131 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst))) 2132 return (LLVMRealPredicate)I->getPredicate(); 2133 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2134 if (CE->getOpcode() == Instruction::FCmp) 2135 return (LLVMRealPredicate)CE->getPredicate(); 2136 return (LLVMRealPredicate)0; 2137 } 2138 2139 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { 2140 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2141 return map_to_llvmopcode(C->getOpcode()); 2142 return (LLVMOpcode)0; 2143 } 2144 2145 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) { 2146 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2147 return wrap(C->clone()); 2148 return nullptr; 2149 } 2150 2151 /*--.. Call and invoke instructions ........................................--*/ 2152 2153 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) { 2154 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands(); 2155 } 2156 2157 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { 2158 return CallSite(unwrap<Instruction>(Instr)).getCallingConv(); 2159 } 2160 2161 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { 2162 return CallSite(unwrap<Instruction>(Instr)) 2163 .setCallingConv(static_cast<CallingConv::ID>(CC)); 2164 } 2165 2166 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 2167 unsigned align) { 2168 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2169 AttrBuilder B; 2170 B.addAlignmentAttr(align); 2171 Call.setAttributes(Call.getAttributes() 2172 .addAttributes(Call->getContext(), index, 2173 AttributeSet::get(Call->getContext(), 2174 index, B))); 2175 } 2176 2177 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2178 LLVMAttributeRef A) { 2179 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A)); 2180 } 2181 2182 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C, 2183 LLVMAttributeIndex Idx) { 2184 auto CS = CallSite(unwrap<Instruction>(C)); 2185 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx); 2186 if (!ASN) 2187 return 0; 2188 return ASN->getNumAttributes(); 2189 } 2190 2191 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx, 2192 LLVMAttributeRef *Attrs) { 2193 auto CS = CallSite(unwrap<Instruction>(C)); 2194 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx); 2195 if (!ASN) 2196 return; 2197 for (auto A: make_range(ASN->begin(), ASN->end())) 2198 *Attrs++ = wrap(A); 2199 } 2200 2201 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C, 2202 LLVMAttributeIndex Idx, 2203 unsigned KindID) { 2204 return wrap(CallSite(unwrap<Instruction>(C)) 2205 .getAttribute(Idx, (Attribute::AttrKind)KindID)); 2206 } 2207 2208 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C, 2209 LLVMAttributeIndex Idx, 2210 const char *K, unsigned KLen) { 2211 return wrap(CallSite(unwrap<Instruction>(C)) 2212 .getAttribute(Idx, StringRef(K, KLen))); 2213 } 2214 2215 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2216 unsigned KindID) { 2217 CallSite(unwrap<Instruction>(C)) 2218 .removeAttribute(Idx, (Attribute::AttrKind)KindID); 2219 } 2220 2221 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2222 const char *K, unsigned KLen) { 2223 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen)); 2224 } 2225 2226 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) { 2227 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue()); 2228 } 2229 2230 /*--.. Operations on call instructions (only) ..............................--*/ 2231 2232 LLVMBool LLVMIsTailCall(LLVMValueRef Call) { 2233 return unwrap<CallInst>(Call)->isTailCall(); 2234 } 2235 2236 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) { 2237 unwrap<CallInst>(Call)->setTailCall(isTailCall); 2238 } 2239 2240 /*--.. Operations on invoke instructions (only) ............................--*/ 2241 2242 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) { 2243 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest()); 2244 } 2245 2246 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) { 2247 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest()); 2248 } 2249 2250 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2251 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B)); 2252 } 2253 2254 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2255 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B)); 2256 } 2257 2258 /*--.. Operations on terminators ...........................................--*/ 2259 2260 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) { 2261 return unwrap<TerminatorInst>(Term)->getNumSuccessors(); 2262 } 2263 2264 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) { 2265 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i)); 2266 } 2267 2268 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) { 2269 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block)); 2270 } 2271 2272 /*--.. Operations on branch instructions (only) ............................--*/ 2273 2274 LLVMBool LLVMIsConditional(LLVMValueRef Branch) { 2275 return unwrap<BranchInst>(Branch)->isConditional(); 2276 } 2277 2278 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) { 2279 return wrap(unwrap<BranchInst>(Branch)->getCondition()); 2280 } 2281 2282 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) { 2283 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond)); 2284 } 2285 2286 /*--.. Operations on switch instructions (only) ............................--*/ 2287 2288 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { 2289 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest()); 2290 } 2291 2292 /*--.. Operations on alloca instructions (only) ............................--*/ 2293 2294 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) { 2295 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType()); 2296 } 2297 2298 /*--.. Operations on gep instructions (only) ...............................--*/ 2299 2300 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) { 2301 return unwrap<GetElementPtrInst>(GEP)->isInBounds(); 2302 } 2303 2304 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) { 2305 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds); 2306 } 2307 2308 /*--.. Operations on phi nodes .............................................--*/ 2309 2310 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, 2311 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) { 2312 PHINode *PhiVal = unwrap<PHINode>(PhiNode); 2313 for (unsigned I = 0; I != Count; ++I) 2314 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I])); 2315 } 2316 2317 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) { 2318 return unwrap<PHINode>(PhiNode)->getNumIncomingValues(); 2319 } 2320 2321 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) { 2322 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index)); 2323 } 2324 2325 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) { 2326 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index)); 2327 } 2328 2329 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/ 2330 2331 unsigned LLVMGetNumIndices(LLVMValueRef Inst) { 2332 auto *I = unwrap(Inst); 2333 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) 2334 return GEP->getNumIndices(); 2335 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 2336 return EV->getNumIndices(); 2337 if (auto *IV = dyn_cast<InsertValueInst>(I)) 2338 return IV->getNumIndices(); 2339 llvm_unreachable( 2340 "LLVMGetNumIndices applies only to extractvalue and insertvalue!"); 2341 } 2342 2343 const unsigned *LLVMGetIndices(LLVMValueRef Inst) { 2344 auto *I = unwrap(Inst); 2345 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 2346 return EV->getIndices().data(); 2347 if (auto *IV = dyn_cast<InsertValueInst>(I)) 2348 return IV->getIndices().data(); 2349 llvm_unreachable( 2350 "LLVMGetIndices applies only to extractvalue and insertvalue!"); 2351 } 2352 2353 2354 /*===-- Instruction builders ----------------------------------------------===*/ 2355 2356 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) { 2357 return wrap(new IRBuilder<>(*unwrap(C))); 2358 } 2359 2360 LLVMBuilderRef LLVMCreateBuilder(void) { 2361 return LLVMCreateBuilderInContext(LLVMGetGlobalContext()); 2362 } 2363 2364 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, 2365 LLVMValueRef Instr) { 2366 BasicBlock *BB = unwrap(Block); 2367 auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end(); 2368 unwrap(Builder)->SetInsertPoint(BB, I); 2369 } 2370 2371 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2372 Instruction *I = unwrap<Instruction>(Instr); 2373 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator()); 2374 } 2375 2376 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) { 2377 BasicBlock *BB = unwrap(Block); 2378 unwrap(Builder)->SetInsertPoint(BB); 2379 } 2380 2381 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) { 2382 return wrap(unwrap(Builder)->GetInsertBlock()); 2383 } 2384 2385 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) { 2386 unwrap(Builder)->ClearInsertionPoint(); 2387 } 2388 2389 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2390 unwrap(Builder)->Insert(unwrap<Instruction>(Instr)); 2391 } 2392 2393 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, 2394 const char *Name) { 2395 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name); 2396 } 2397 2398 void LLVMDisposeBuilder(LLVMBuilderRef Builder) { 2399 delete unwrap(Builder); 2400 } 2401 2402 /*--.. Metadata builders ...................................................--*/ 2403 2404 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { 2405 MDNode *Loc = 2406 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr; 2407 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc)); 2408 } 2409 2410 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { 2411 LLVMContext &Context = unwrap(Builder)->getContext(); 2412 return wrap(MetadataAsValue::get( 2413 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode())); 2414 } 2415 2416 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { 2417 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst)); 2418 } 2419 2420 2421 /*--.. Instruction builders ................................................--*/ 2422 2423 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { 2424 return wrap(unwrap(B)->CreateRetVoid()); 2425 } 2426 2427 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) { 2428 return wrap(unwrap(B)->CreateRet(unwrap(V))); 2429 } 2430 2431 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals, 2432 unsigned N) { 2433 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N)); 2434 } 2435 2436 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) { 2437 return wrap(unwrap(B)->CreateBr(unwrap(Dest))); 2438 } 2439 2440 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, 2441 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) { 2442 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else))); 2443 } 2444 2445 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V, 2446 LLVMBasicBlockRef Else, unsigned NumCases) { 2447 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases)); 2448 } 2449 2450 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr, 2451 unsigned NumDests) { 2452 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests)); 2453 } 2454 2455 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, 2456 LLVMValueRef *Args, unsigned NumArgs, 2457 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 2458 const char *Name) { 2459 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch), 2460 makeArrayRef(unwrap(Args), NumArgs), 2461 Name)); 2462 } 2463 2464 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, 2465 LLVMValueRef PersFn, unsigned NumClauses, 2466 const char *Name) { 2467 // The personality used to live on the landingpad instruction, but now it 2468 // lives on the parent function. For compatibility, take the provided 2469 // personality and put it on the parent function. 2470 if (PersFn) 2471 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn( 2472 cast<Function>(unwrap(PersFn))); 2473 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name)); 2474 } 2475 2476 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) { 2477 return wrap(unwrap(B)->CreateResume(unwrap(Exn))); 2478 } 2479 2480 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) { 2481 return wrap(unwrap(B)->CreateUnreachable()); 2482 } 2483 2484 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, 2485 LLVMBasicBlockRef Dest) { 2486 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest)); 2487 } 2488 2489 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { 2490 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest)); 2491 } 2492 2493 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) { 2494 return unwrap<LandingPadInst>(LandingPad)->getNumClauses(); 2495 } 2496 2497 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) { 2498 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx)); 2499 } 2500 2501 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) { 2502 unwrap<LandingPadInst>(LandingPad)-> 2503 addClause(cast<Constant>(unwrap(ClauseVal))); 2504 } 2505 2506 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) { 2507 return unwrap<LandingPadInst>(LandingPad)->isCleanup(); 2508 } 2509 2510 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) { 2511 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val); 2512 } 2513 2514 /*--.. Arithmetic ..........................................................--*/ 2515 2516 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2517 const char *Name) { 2518 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name)); 2519 } 2520 2521 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2522 const char *Name) { 2523 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name)); 2524 } 2525 2526 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2527 const char *Name) { 2528 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name)); 2529 } 2530 2531 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2532 const char *Name) { 2533 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name)); 2534 } 2535 2536 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2537 const char *Name) { 2538 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name)); 2539 } 2540 2541 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2542 const char *Name) { 2543 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name)); 2544 } 2545 2546 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2547 const char *Name) { 2548 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name)); 2549 } 2550 2551 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2552 const char *Name) { 2553 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name)); 2554 } 2555 2556 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2557 const char *Name) { 2558 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name)); 2559 } 2560 2561 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2562 const char *Name) { 2563 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name)); 2564 } 2565 2566 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2567 const char *Name) { 2568 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name)); 2569 } 2570 2571 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2572 const char *Name) { 2573 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name)); 2574 } 2575 2576 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2577 const char *Name) { 2578 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name)); 2579 } 2580 2581 LLVMValueRef LLVMBuildExactUDiv(LLVMBuilderRef B, LLVMValueRef LHS, 2582 LLVMValueRef RHS, const char *Name) { 2583 return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name)); 2584 } 2585 2586 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2587 const char *Name) { 2588 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name)); 2589 } 2590 2591 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS, 2592 LLVMValueRef RHS, const char *Name) { 2593 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name)); 2594 } 2595 2596 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2597 const char *Name) { 2598 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name)); 2599 } 2600 2601 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2602 const char *Name) { 2603 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name)); 2604 } 2605 2606 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2607 const char *Name) { 2608 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name)); 2609 } 2610 2611 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2612 const char *Name) { 2613 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name)); 2614 } 2615 2616 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2617 const char *Name) { 2618 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name)); 2619 } 2620 2621 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2622 const char *Name) { 2623 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name)); 2624 } 2625 2626 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2627 const char *Name) { 2628 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name)); 2629 } 2630 2631 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2632 const char *Name) { 2633 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name)); 2634 } 2635 2636 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2637 const char *Name) { 2638 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name)); 2639 } 2640 2641 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2642 const char *Name) { 2643 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name)); 2644 } 2645 2646 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op, 2647 LLVMValueRef LHS, LLVMValueRef RHS, 2648 const char *Name) { 2649 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS), 2650 unwrap(RHS), Name)); 2651 } 2652 2653 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2654 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name)); 2655 } 2656 2657 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V, 2658 const char *Name) { 2659 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name)); 2660 } 2661 2662 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V, 2663 const char *Name) { 2664 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name)); 2665 } 2666 2667 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2668 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name)); 2669 } 2670 2671 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2672 return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); 2673 } 2674 2675 /*--.. Memory ..............................................................--*/ 2676 2677 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2678 const char *Name) { 2679 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2680 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2681 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2682 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2683 ITy, unwrap(Ty), AllocSize, 2684 nullptr, nullptr, ""); 2685 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2686 } 2687 2688 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2689 LLVMValueRef Val, const char *Name) { 2690 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2691 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2692 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2693 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2694 ITy, unwrap(Ty), AllocSize, 2695 unwrap(Val), nullptr, ""); 2696 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2697 } 2698 2699 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2700 const char *Name) { 2701 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name)); 2702 } 2703 2704 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2705 LLVMValueRef Val, const char *Name) { 2706 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name)); 2707 } 2708 2709 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { 2710 return wrap(unwrap(B)->Insert( 2711 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock()))); 2712 } 2713 2714 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, 2715 const char *Name) { 2716 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name)); 2717 } 2718 2719 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 2720 LLVMValueRef PointerVal) { 2721 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal))); 2722 } 2723 2724 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) { 2725 switch (Ordering) { 2726 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic; 2727 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered; 2728 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic; 2729 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire; 2730 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release; 2731 case LLVMAtomicOrderingAcquireRelease: 2732 return AtomicOrdering::AcquireRelease; 2733 case LLVMAtomicOrderingSequentiallyConsistent: 2734 return AtomicOrdering::SequentiallyConsistent; 2735 } 2736 2737 llvm_unreachable("Invalid LLVMAtomicOrdering value!"); 2738 } 2739 2740 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) { 2741 switch (Ordering) { 2742 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic; 2743 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered; 2744 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic; 2745 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire; 2746 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease; 2747 case AtomicOrdering::AcquireRelease: 2748 return LLVMAtomicOrderingAcquireRelease; 2749 case AtomicOrdering::SequentiallyConsistent: 2750 return LLVMAtomicOrderingSequentiallyConsistent; 2751 } 2752 2753 llvm_unreachable("Invalid AtomicOrdering value!"); 2754 } 2755 2756 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering, 2757 LLVMBool isSingleThread, const char *Name) { 2758 return wrap( 2759 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering), 2760 isSingleThread ? SingleThread : CrossThread, 2761 Name)); 2762 } 2763 2764 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2765 LLVMValueRef *Indices, unsigned NumIndices, 2766 const char *Name) { 2767 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2768 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2769 } 2770 2771 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2772 LLVMValueRef *Indices, unsigned NumIndices, 2773 const char *Name) { 2774 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2775 return wrap( 2776 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2777 } 2778 2779 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2780 unsigned Idx, const char *Name) { 2781 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name)); 2782 } 2783 2784 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, 2785 const char *Name) { 2786 return wrap(unwrap(B)->CreateGlobalString(Str, Name)); 2787 } 2788 2789 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, 2790 const char *Name) { 2791 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); 2792 } 2793 2794 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { 2795 Value *P = unwrap<Value>(MemAccessInst); 2796 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2797 return LI->isVolatile(); 2798 return cast<StoreInst>(P)->isVolatile(); 2799 } 2800 2801 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) { 2802 Value *P = unwrap<Value>(MemAccessInst); 2803 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2804 return LI->setVolatile(isVolatile); 2805 return cast<StoreInst>(P)->setVolatile(isVolatile); 2806 } 2807 2808 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) { 2809 Value *P = unwrap<Value>(MemAccessInst); 2810 AtomicOrdering O; 2811 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2812 O = LI->getOrdering(); 2813 else 2814 O = cast<StoreInst>(P)->getOrdering(); 2815 return mapToLLVMOrdering(O); 2816 } 2817 2818 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) { 2819 Value *P = unwrap<Value>(MemAccessInst); 2820 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 2821 2822 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2823 return LI->setOrdering(O); 2824 return cast<StoreInst>(P)->setOrdering(O); 2825 } 2826 2827 /*--.. Casts ...............................................................--*/ 2828 2829 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2830 LLVMTypeRef DestTy, const char *Name) { 2831 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name)); 2832 } 2833 2834 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, 2835 LLVMTypeRef DestTy, const char *Name) { 2836 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name)); 2837 } 2838 2839 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, 2840 LLVMTypeRef DestTy, const char *Name) { 2841 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name)); 2842 } 2843 2844 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, 2845 LLVMTypeRef DestTy, const char *Name) { 2846 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name)); 2847 } 2848 2849 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val, 2850 LLVMTypeRef DestTy, const char *Name) { 2851 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name)); 2852 } 2853 2854 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2855 LLVMTypeRef DestTy, const char *Name) { 2856 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name)); 2857 } 2858 2859 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2860 LLVMTypeRef DestTy, const char *Name) { 2861 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name)); 2862 } 2863 2864 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2865 LLVMTypeRef DestTy, const char *Name) { 2866 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name)); 2867 } 2868 2869 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val, 2870 LLVMTypeRef DestTy, const char *Name) { 2871 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name)); 2872 } 2873 2874 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val, 2875 LLVMTypeRef DestTy, const char *Name) { 2876 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name)); 2877 } 2878 2879 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val, 2880 LLVMTypeRef DestTy, const char *Name) { 2881 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name)); 2882 } 2883 2884 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2885 LLVMTypeRef DestTy, const char *Name) { 2886 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); 2887 } 2888 2889 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val, 2890 LLVMTypeRef DestTy, const char *Name) { 2891 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name)); 2892 } 2893 2894 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2895 LLVMTypeRef DestTy, const char *Name) { 2896 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy), 2897 Name)); 2898 } 2899 2900 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2901 LLVMTypeRef DestTy, const char *Name) { 2902 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy), 2903 Name)); 2904 } 2905 2906 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2907 LLVMTypeRef DestTy, const char *Name) { 2908 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy), 2909 Name)); 2910 } 2911 2912 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val, 2913 LLVMTypeRef DestTy, const char *Name) { 2914 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val), 2915 unwrap(DestTy), Name)); 2916 } 2917 2918 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val, 2919 LLVMTypeRef DestTy, const char *Name) { 2920 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name)); 2921 } 2922 2923 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val, 2924 LLVMTypeRef DestTy, const char *Name) { 2925 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), 2926 /*isSigned*/true, Name)); 2927 } 2928 2929 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val, 2930 LLVMTypeRef DestTy, const char *Name) { 2931 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name)); 2932 } 2933 2934 /*--.. Comparisons .........................................................--*/ 2935 2936 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, 2937 LLVMValueRef LHS, LLVMValueRef RHS, 2938 const char *Name) { 2939 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op), 2940 unwrap(LHS), unwrap(RHS), Name)); 2941 } 2942 2943 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, 2944 LLVMValueRef LHS, LLVMValueRef RHS, 2945 const char *Name) { 2946 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op), 2947 unwrap(LHS), unwrap(RHS), Name)); 2948 } 2949 2950 /*--.. Miscellaneous instructions ..........................................--*/ 2951 2952 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) { 2953 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name)); 2954 } 2955 2956 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, 2957 LLVMValueRef *Args, unsigned NumArgs, 2958 const char *Name) { 2959 return wrap(unwrap(B)->CreateCall(unwrap(Fn), 2960 makeArrayRef(unwrap(Args), NumArgs), 2961 Name)); 2962 } 2963 2964 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, 2965 LLVMValueRef Then, LLVMValueRef Else, 2966 const char *Name) { 2967 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else), 2968 Name)); 2969 } 2970 2971 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List, 2972 LLVMTypeRef Ty, const char *Name) { 2973 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name)); 2974 } 2975 2976 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal, 2977 LLVMValueRef Index, const char *Name) { 2978 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index), 2979 Name)); 2980 } 2981 2982 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal, 2983 LLVMValueRef EltVal, LLVMValueRef Index, 2984 const char *Name) { 2985 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal), 2986 unwrap(Index), Name)); 2987 } 2988 2989 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1, 2990 LLVMValueRef V2, LLVMValueRef Mask, 2991 const char *Name) { 2992 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2), 2993 unwrap(Mask), Name)); 2994 } 2995 2996 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal, 2997 unsigned Index, const char *Name) { 2998 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name)); 2999 } 3000 3001 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal, 3002 LLVMValueRef EltVal, unsigned Index, 3003 const char *Name) { 3004 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal), 3005 Index, Name)); 3006 } 3007 3008 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, 3009 const char *Name) { 3010 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); 3011 } 3012 3013 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val, 3014 const char *Name) { 3015 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name)); 3016 } 3017 3018 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS, 3019 LLVMValueRef RHS, const char *Name) { 3020 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); 3021 } 3022 3023 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op, 3024 LLVMValueRef PTR, LLVMValueRef Val, 3025 LLVMAtomicOrdering ordering, 3026 LLVMBool singleThread) { 3027 AtomicRMWInst::BinOp intop; 3028 switch (op) { 3029 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break; 3030 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break; 3031 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break; 3032 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break; 3033 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break; 3034 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break; 3035 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break; 3036 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break; 3037 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break; 3038 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break; 3039 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break; 3040 } 3041 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val), 3042 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread)); 3043 } 3044 3045 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, 3046 LLVMValueRef Cmp, LLVMValueRef New, 3047 LLVMAtomicOrdering SuccessOrdering, 3048 LLVMAtomicOrdering FailureOrdering, 3049 LLVMBool singleThread) { 3050 3051 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp), 3052 unwrap(New), mapFromLLVMOrdering(SuccessOrdering), 3053 mapFromLLVMOrdering(FailureOrdering), 3054 singleThread ? SingleThread : CrossThread)); 3055 } 3056 3057 3058 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) { 3059 Value *P = unwrap<Value>(AtomicInst); 3060 3061 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 3062 return I->getSynchScope() == SingleThread; 3063 return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread; 3064 } 3065 3066 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) { 3067 Value *P = unwrap<Value>(AtomicInst); 3068 SynchronizationScope Sync = NewValue ? SingleThread : CrossThread; 3069 3070 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 3071 return I->setSynchScope(Sync); 3072 return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync); 3073 } 3074 3075 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) { 3076 Value *P = unwrap<Value>(CmpXchgInst); 3077 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering()); 3078 } 3079 3080 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst, 3081 LLVMAtomicOrdering Ordering) { 3082 Value *P = unwrap<Value>(CmpXchgInst); 3083 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 3084 3085 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O); 3086 } 3087 3088 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) { 3089 Value *P = unwrap<Value>(CmpXchgInst); 3090 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering()); 3091 } 3092 3093 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst, 3094 LLVMAtomicOrdering Ordering) { 3095 Value *P = unwrap<Value>(CmpXchgInst); 3096 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 3097 3098 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O); 3099 } 3100 3101 /*===-- Module providers --------------------------------------------------===*/ 3102 3103 LLVMModuleProviderRef 3104 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) { 3105 return reinterpret_cast<LLVMModuleProviderRef>(M); 3106 } 3107 3108 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) { 3109 delete unwrap(MP); 3110 } 3111 3112 3113 /*===-- Memory buffers ----------------------------------------------------===*/ 3114 3115 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( 3116 const char *Path, 3117 LLVMMemoryBufferRef *OutMemBuf, 3118 char **OutMessage) { 3119 3120 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); 3121 if (std::error_code EC = MBOrErr.getError()) { 3122 *OutMessage = strdup(EC.message().c_str()); 3123 return 1; 3124 } 3125 *OutMemBuf = wrap(MBOrErr.get().release()); 3126 return 0; 3127 } 3128 3129 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, 3130 char **OutMessage) { 3131 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN(); 3132 if (std::error_code EC = MBOrErr.getError()) { 3133 *OutMessage = strdup(EC.message().c_str()); 3134 return 1; 3135 } 3136 *OutMemBuf = wrap(MBOrErr.get().release()); 3137 return 0; 3138 } 3139 3140 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( 3141 const char *InputData, 3142 size_t InputDataLength, 3143 const char *BufferName, 3144 LLVMBool RequiresNullTerminator) { 3145 3146 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength), 3147 StringRef(BufferName), 3148 RequiresNullTerminator).release()); 3149 } 3150 3151 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( 3152 const char *InputData, 3153 size_t InputDataLength, 3154 const char *BufferName) { 3155 3156 return wrap( 3157 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength), 3158 StringRef(BufferName)).release()); 3159 } 3160 3161 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) { 3162 return unwrap(MemBuf)->getBufferStart(); 3163 } 3164 3165 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) { 3166 return unwrap(MemBuf)->getBufferSize(); 3167 } 3168 3169 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { 3170 delete unwrap(MemBuf); 3171 } 3172 3173 /*===-- Pass Registry -----------------------------------------------------===*/ 3174 3175 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) { 3176 return wrap(PassRegistry::getPassRegistry()); 3177 } 3178 3179 /*===-- Pass Manager ------------------------------------------------------===*/ 3180 3181 LLVMPassManagerRef LLVMCreatePassManager() { 3182 return wrap(new legacy::PassManager()); 3183 } 3184 3185 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { 3186 return wrap(new legacy::FunctionPassManager(unwrap(M))); 3187 } 3188 3189 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { 3190 return LLVMCreateFunctionPassManagerForModule( 3191 reinterpret_cast<LLVMModuleRef>(P)); 3192 } 3193 3194 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { 3195 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M)); 3196 } 3197 3198 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { 3199 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization(); 3200 } 3201 3202 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { 3203 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); 3204 } 3205 3206 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { 3207 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization(); 3208 } 3209 3210 void LLVMDisposePassManager(LLVMPassManagerRef PM) { 3211 delete unwrap(PM); 3212 } 3213 3214 /*===-- Threading ------------------------------------------------------===*/ 3215 3216 LLVMBool LLVMStartMultithreaded() { 3217 return LLVMIsMultithreaded(); 3218 } 3219 3220 void LLVMStopMultithreaded() { 3221 } 3222 3223 LLVMBool LLVMIsMultithreaded() { 3224 return llvm_is_multithreaded(); 3225 } 3226