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