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