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