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/ReaderWriter.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 LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1182 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant), 1183 unwrap<Constant>(RHSConstant))); 1184 } 1185 1186 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant, 1187 LLVMValueRef RHSConstant) { 1188 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant), 1189 unwrap<Constant>(RHSConstant))); 1190 } 1191 1192 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1193 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant), 1194 unwrap<Constant>(RHSConstant))); 1195 } 1196 1197 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1198 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant), 1199 unwrap<Constant>(RHSConstant))); 1200 } 1201 1202 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1203 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant), 1204 unwrap<Constant>(RHSConstant))); 1205 } 1206 1207 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1208 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant), 1209 unwrap<Constant>(RHSConstant))); 1210 } 1211 1212 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1213 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant), 1214 unwrap<Constant>(RHSConstant))); 1215 } 1216 1217 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1218 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant), 1219 unwrap<Constant>(RHSConstant))); 1220 } 1221 1222 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1223 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant), 1224 unwrap<Constant>(RHSConstant))); 1225 } 1226 1227 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate, 1228 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1229 return wrap(ConstantExpr::getICmp(Predicate, 1230 unwrap<Constant>(LHSConstant), 1231 unwrap<Constant>(RHSConstant))); 1232 } 1233 1234 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate, 1235 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1236 return wrap(ConstantExpr::getFCmp(Predicate, 1237 unwrap<Constant>(LHSConstant), 1238 unwrap<Constant>(RHSConstant))); 1239 } 1240 1241 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1242 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant), 1243 unwrap<Constant>(RHSConstant))); 1244 } 1245 1246 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1247 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant), 1248 unwrap<Constant>(RHSConstant))); 1249 } 1250 1251 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) { 1252 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant), 1253 unwrap<Constant>(RHSConstant))); 1254 } 1255 1256 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal, 1257 LLVMValueRef *ConstantIndices, unsigned NumIndices) { 1258 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1259 NumIndices); 1260 return wrap(ConstantExpr::getGetElementPtr( 1261 nullptr, unwrap<Constant>(ConstantVal), IdxList)); 1262 } 1263 1264 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal, 1265 LLVMValueRef *ConstantIndices, 1266 unsigned NumIndices) { 1267 Constant* Val = unwrap<Constant>(ConstantVal); 1268 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices), 1269 NumIndices); 1270 return wrap(ConstantExpr::getInBoundsGetElementPtr(nullptr, Val, IdxList)); 1271 } 1272 1273 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1274 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal), 1275 unwrap(ToType))); 1276 } 1277 1278 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1279 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal), 1280 unwrap(ToType))); 1281 } 1282 1283 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1284 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal), 1285 unwrap(ToType))); 1286 } 1287 1288 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1289 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal), 1290 unwrap(ToType))); 1291 } 1292 1293 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1294 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal), 1295 unwrap(ToType))); 1296 } 1297 1298 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1299 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal), 1300 unwrap(ToType))); 1301 } 1302 1303 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1304 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal), 1305 unwrap(ToType))); 1306 } 1307 1308 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1309 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal), 1310 unwrap(ToType))); 1311 } 1312 1313 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1314 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal), 1315 unwrap(ToType))); 1316 } 1317 1318 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1319 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal), 1320 unwrap(ToType))); 1321 } 1322 1323 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1324 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal), 1325 unwrap(ToType))); 1326 } 1327 1328 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1329 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal), 1330 unwrap(ToType))); 1331 } 1332 1333 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal, 1334 LLVMTypeRef ToType) { 1335 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal), 1336 unwrap(ToType))); 1337 } 1338 1339 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal, 1340 LLVMTypeRef ToType) { 1341 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal), 1342 unwrap(ToType))); 1343 } 1344 1345 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal, 1346 LLVMTypeRef ToType) { 1347 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal), 1348 unwrap(ToType))); 1349 } 1350 1351 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal, 1352 LLVMTypeRef ToType) { 1353 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal), 1354 unwrap(ToType))); 1355 } 1356 1357 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal, 1358 LLVMTypeRef ToType) { 1359 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal), 1360 unwrap(ToType))); 1361 } 1362 1363 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType, 1364 LLVMBool isSigned) { 1365 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal), 1366 unwrap(ToType), isSigned)); 1367 } 1368 1369 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) { 1370 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal), 1371 unwrap(ToType))); 1372 } 1373 1374 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition, 1375 LLVMValueRef ConstantIfTrue, 1376 LLVMValueRef ConstantIfFalse) { 1377 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition), 1378 unwrap<Constant>(ConstantIfTrue), 1379 unwrap<Constant>(ConstantIfFalse))); 1380 } 1381 1382 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant, 1383 LLVMValueRef IndexConstant) { 1384 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant), 1385 unwrap<Constant>(IndexConstant))); 1386 } 1387 1388 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant, 1389 LLVMValueRef ElementValueConstant, 1390 LLVMValueRef IndexConstant) { 1391 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant), 1392 unwrap<Constant>(ElementValueConstant), 1393 unwrap<Constant>(IndexConstant))); 1394 } 1395 1396 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant, 1397 LLVMValueRef VectorBConstant, 1398 LLVMValueRef MaskConstant) { 1399 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant), 1400 unwrap<Constant>(VectorBConstant), 1401 unwrap<Constant>(MaskConstant))); 1402 } 1403 1404 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList, 1405 unsigned NumIdx) { 1406 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant), 1407 makeArrayRef(IdxList, NumIdx))); 1408 } 1409 1410 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant, 1411 LLVMValueRef ElementValueConstant, 1412 unsigned *IdxList, unsigned NumIdx) { 1413 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant), 1414 unwrap<Constant>(ElementValueConstant), 1415 makeArrayRef(IdxList, NumIdx))); 1416 } 1417 1418 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString, 1419 const char *Constraints, 1420 LLVMBool HasSideEffects, 1421 LLVMBool IsAlignStack) { 1422 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString, 1423 Constraints, HasSideEffects, IsAlignStack)); 1424 } 1425 1426 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) { 1427 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB))); 1428 } 1429 1430 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/ 1431 1432 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) { 1433 return wrap(unwrap<GlobalValue>(Global)->getParent()); 1434 } 1435 1436 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) { 1437 return unwrap<GlobalValue>(Global)->isDeclaration(); 1438 } 1439 1440 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) { 1441 switch (unwrap<GlobalValue>(Global)->getLinkage()) { 1442 case GlobalValue::ExternalLinkage: 1443 return LLVMExternalLinkage; 1444 case GlobalValue::AvailableExternallyLinkage: 1445 return LLVMAvailableExternallyLinkage; 1446 case GlobalValue::LinkOnceAnyLinkage: 1447 return LLVMLinkOnceAnyLinkage; 1448 case GlobalValue::LinkOnceODRLinkage: 1449 return LLVMLinkOnceODRLinkage; 1450 case GlobalValue::WeakAnyLinkage: 1451 return LLVMWeakAnyLinkage; 1452 case GlobalValue::WeakODRLinkage: 1453 return LLVMWeakODRLinkage; 1454 case GlobalValue::AppendingLinkage: 1455 return LLVMAppendingLinkage; 1456 case GlobalValue::InternalLinkage: 1457 return LLVMInternalLinkage; 1458 case GlobalValue::PrivateLinkage: 1459 return LLVMPrivateLinkage; 1460 case GlobalValue::ExternalWeakLinkage: 1461 return LLVMExternalWeakLinkage; 1462 case GlobalValue::CommonLinkage: 1463 return LLVMCommonLinkage; 1464 } 1465 1466 llvm_unreachable("Invalid GlobalValue linkage!"); 1467 } 1468 1469 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) { 1470 GlobalValue *GV = unwrap<GlobalValue>(Global); 1471 1472 switch (Linkage) { 1473 case LLVMExternalLinkage: 1474 GV->setLinkage(GlobalValue::ExternalLinkage); 1475 break; 1476 case LLVMAvailableExternallyLinkage: 1477 GV->setLinkage(GlobalValue::AvailableExternallyLinkage); 1478 break; 1479 case LLVMLinkOnceAnyLinkage: 1480 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage); 1481 break; 1482 case LLVMLinkOnceODRLinkage: 1483 GV->setLinkage(GlobalValue::LinkOnceODRLinkage); 1484 break; 1485 case LLVMLinkOnceODRAutoHideLinkage: 1486 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no " 1487 "longer supported."); 1488 break; 1489 case LLVMWeakAnyLinkage: 1490 GV->setLinkage(GlobalValue::WeakAnyLinkage); 1491 break; 1492 case LLVMWeakODRLinkage: 1493 GV->setLinkage(GlobalValue::WeakODRLinkage); 1494 break; 1495 case LLVMAppendingLinkage: 1496 GV->setLinkage(GlobalValue::AppendingLinkage); 1497 break; 1498 case LLVMInternalLinkage: 1499 GV->setLinkage(GlobalValue::InternalLinkage); 1500 break; 1501 case LLVMPrivateLinkage: 1502 GV->setLinkage(GlobalValue::PrivateLinkage); 1503 break; 1504 case LLVMLinkerPrivateLinkage: 1505 GV->setLinkage(GlobalValue::PrivateLinkage); 1506 break; 1507 case LLVMLinkerPrivateWeakLinkage: 1508 GV->setLinkage(GlobalValue::PrivateLinkage); 1509 break; 1510 case LLVMDLLImportLinkage: 1511 DEBUG(errs() 1512 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported."); 1513 break; 1514 case LLVMDLLExportLinkage: 1515 DEBUG(errs() 1516 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported."); 1517 break; 1518 case LLVMExternalWeakLinkage: 1519 GV->setLinkage(GlobalValue::ExternalWeakLinkage); 1520 break; 1521 case LLVMGhostLinkage: 1522 DEBUG(errs() 1523 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported."); 1524 break; 1525 case LLVMCommonLinkage: 1526 GV->setLinkage(GlobalValue::CommonLinkage); 1527 break; 1528 } 1529 } 1530 1531 const char *LLVMGetSection(LLVMValueRef Global) { 1532 // Using .data() is safe because of how GlobalObject::setSection is 1533 // implemented. 1534 return unwrap<GlobalValue>(Global)->getSection().data(); 1535 } 1536 1537 void LLVMSetSection(LLVMValueRef Global, const char *Section) { 1538 unwrap<GlobalObject>(Global)->setSection(Section); 1539 } 1540 1541 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) { 1542 return static_cast<LLVMVisibility>( 1543 unwrap<GlobalValue>(Global)->getVisibility()); 1544 } 1545 1546 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) { 1547 unwrap<GlobalValue>(Global) 1548 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz)); 1549 } 1550 1551 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) { 1552 return static_cast<LLVMDLLStorageClass>( 1553 unwrap<GlobalValue>(Global)->getDLLStorageClass()); 1554 } 1555 1556 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) { 1557 unwrap<GlobalValue>(Global)->setDLLStorageClass( 1558 static_cast<GlobalValue::DLLStorageClassTypes>(Class)); 1559 } 1560 1561 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) { 1562 return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr(); 1563 } 1564 1565 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) { 1566 unwrap<GlobalValue>(Global)->setUnnamedAddr( 1567 HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global 1568 : GlobalValue::UnnamedAddr::None); 1569 } 1570 1571 /*--.. Operations on global variables, load and store instructions .........--*/ 1572 1573 unsigned LLVMGetAlignment(LLVMValueRef V) { 1574 Value *P = unwrap<Value>(V); 1575 if (GlobalValue *GV = dyn_cast<GlobalValue>(P)) 1576 return GV->getAlignment(); 1577 if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 1578 return AI->getAlignment(); 1579 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 1580 return LI->getAlignment(); 1581 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 1582 return SI->getAlignment(); 1583 1584 llvm_unreachable( 1585 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment"); 1586 } 1587 1588 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) { 1589 Value *P = unwrap<Value>(V); 1590 if (GlobalObject *GV = dyn_cast<GlobalObject>(P)) 1591 GV->setAlignment(Bytes); 1592 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 1593 AI->setAlignment(Bytes); 1594 else if (LoadInst *LI = dyn_cast<LoadInst>(P)) 1595 LI->setAlignment(Bytes); 1596 else if (StoreInst *SI = dyn_cast<StoreInst>(P)) 1597 SI->setAlignment(Bytes); 1598 else 1599 llvm_unreachable( 1600 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment"); 1601 } 1602 1603 /*--.. Operations on global variables ......................................--*/ 1604 1605 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) { 1606 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 1607 GlobalValue::ExternalLinkage, nullptr, Name)); 1608 } 1609 1610 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty, 1611 const char *Name, 1612 unsigned AddressSpace) { 1613 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 1614 GlobalValue::ExternalLinkage, nullptr, Name, 1615 nullptr, GlobalVariable::NotThreadLocal, 1616 AddressSpace)); 1617 } 1618 1619 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) { 1620 return wrap(unwrap(M)->getNamedGlobal(Name)); 1621 } 1622 1623 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) { 1624 Module *Mod = unwrap(M); 1625 Module::global_iterator I = Mod->global_begin(); 1626 if (I == Mod->global_end()) 1627 return nullptr; 1628 return wrap(&*I); 1629 } 1630 1631 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) { 1632 Module *Mod = unwrap(M); 1633 Module::global_iterator I = Mod->global_end(); 1634 if (I == Mod->global_begin()) 1635 return nullptr; 1636 return wrap(&*--I); 1637 } 1638 1639 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) { 1640 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1641 Module::global_iterator I(GV); 1642 if (++I == GV->getParent()->global_end()) 1643 return nullptr; 1644 return wrap(&*I); 1645 } 1646 1647 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) { 1648 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1649 Module::global_iterator I(GV); 1650 if (I == GV->getParent()->global_begin()) 1651 return nullptr; 1652 return wrap(&*--I); 1653 } 1654 1655 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) { 1656 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent(); 1657 } 1658 1659 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) { 1660 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar); 1661 if ( !GV->hasInitializer() ) 1662 return nullptr; 1663 return wrap(GV->getInitializer()); 1664 } 1665 1666 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) { 1667 unwrap<GlobalVariable>(GlobalVar) 1668 ->setInitializer(unwrap<Constant>(ConstantVal)); 1669 } 1670 1671 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) { 1672 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal(); 1673 } 1674 1675 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) { 1676 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0); 1677 } 1678 1679 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) { 1680 return unwrap<GlobalVariable>(GlobalVar)->isConstant(); 1681 } 1682 1683 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) { 1684 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0); 1685 } 1686 1687 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) { 1688 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) { 1689 case GlobalVariable::NotThreadLocal: 1690 return LLVMNotThreadLocal; 1691 case GlobalVariable::GeneralDynamicTLSModel: 1692 return LLVMGeneralDynamicTLSModel; 1693 case GlobalVariable::LocalDynamicTLSModel: 1694 return LLVMLocalDynamicTLSModel; 1695 case GlobalVariable::InitialExecTLSModel: 1696 return LLVMInitialExecTLSModel; 1697 case GlobalVariable::LocalExecTLSModel: 1698 return LLVMLocalExecTLSModel; 1699 } 1700 1701 llvm_unreachable("Invalid GlobalVariable thread local mode"); 1702 } 1703 1704 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) { 1705 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1706 1707 switch (Mode) { 1708 case LLVMNotThreadLocal: 1709 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal); 1710 break; 1711 case LLVMGeneralDynamicTLSModel: 1712 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel); 1713 break; 1714 case LLVMLocalDynamicTLSModel: 1715 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel); 1716 break; 1717 case LLVMInitialExecTLSModel: 1718 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); 1719 break; 1720 case LLVMLocalExecTLSModel: 1721 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel); 1722 break; 1723 } 1724 } 1725 1726 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) { 1727 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized(); 1728 } 1729 1730 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { 1731 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit); 1732 } 1733 1734 /*--.. Operations on aliases ......................................--*/ 1735 1736 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, 1737 const char *Name) { 1738 auto *PTy = cast<PointerType>(unwrap(Ty)); 1739 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), 1740 GlobalValue::ExternalLinkage, Name, 1741 unwrap<Constant>(Aliasee), unwrap(M))); 1742 } 1743 1744 /*--.. Operations on functions .............................................--*/ 1745 1746 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, 1747 LLVMTypeRef FunctionTy) { 1748 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy), 1749 GlobalValue::ExternalLinkage, Name, unwrap(M))); 1750 } 1751 1752 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) { 1753 return wrap(unwrap(M)->getFunction(Name)); 1754 } 1755 1756 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) { 1757 Module *Mod = unwrap(M); 1758 Module::iterator I = Mod->begin(); 1759 if (I == Mod->end()) 1760 return nullptr; 1761 return wrap(&*I); 1762 } 1763 1764 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) { 1765 Module *Mod = unwrap(M); 1766 Module::iterator I = Mod->end(); 1767 if (I == Mod->begin()) 1768 return nullptr; 1769 return wrap(&*--I); 1770 } 1771 1772 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) { 1773 Function *Func = unwrap<Function>(Fn); 1774 Module::iterator I(Func); 1775 if (++I == Func->getParent()->end()) 1776 return nullptr; 1777 return wrap(&*I); 1778 } 1779 1780 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) { 1781 Function *Func = unwrap<Function>(Fn); 1782 Module::iterator I(Func); 1783 if (I == Func->getParent()->begin()) 1784 return nullptr; 1785 return wrap(&*--I); 1786 } 1787 1788 void LLVMDeleteFunction(LLVMValueRef Fn) { 1789 unwrap<Function>(Fn)->eraseFromParent(); 1790 } 1791 1792 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) { 1793 return unwrap<Function>(Fn)->hasPersonalityFn(); 1794 } 1795 1796 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) { 1797 return wrap(unwrap<Function>(Fn)->getPersonalityFn()); 1798 } 1799 1800 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) { 1801 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn)); 1802 } 1803 1804 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { 1805 if (Function *F = dyn_cast<Function>(unwrap(Fn))) 1806 return F->getIntrinsicID(); 1807 return 0; 1808 } 1809 1810 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) { 1811 return unwrap<Function>(Fn)->getCallingConv(); 1812 } 1813 1814 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { 1815 return unwrap<Function>(Fn)->setCallingConv( 1816 static_cast<CallingConv::ID>(CC)); 1817 } 1818 1819 const char *LLVMGetGC(LLVMValueRef Fn) { 1820 Function *F = unwrap<Function>(Fn); 1821 return F->hasGC()? F->getGC().c_str() : nullptr; 1822 } 1823 1824 void LLVMSetGC(LLVMValueRef Fn, const char *GC) { 1825 Function *F = unwrap<Function>(Fn); 1826 if (GC) 1827 F->setGC(GC); 1828 else 1829 F->clearGC(); 1830 } 1831 1832 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 1833 Function *Func = unwrap<Function>(Fn); 1834 const AttributeSet PAL = Func->getAttributes(); 1835 AttrBuilder B(PA); 1836 const AttributeSet PALnew = 1837 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex, 1838 AttributeSet::get(Func->getContext(), 1839 AttributeSet::FunctionIndex, B)); 1840 Func->setAttributes(PALnew); 1841 } 1842 1843 void LLVMAddAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1844 LLVMAttributeRef A) { 1845 unwrap<Function>(F)->addAttribute(Idx, unwrap(A)); 1846 } 1847 1848 unsigned LLVMGetAttributeCountAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx) { 1849 auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx); 1850 return ASN->getNumAttributes(); 1851 } 1852 1853 void LLVMGetAttributesAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1854 LLVMAttributeRef *Attrs) { 1855 auto *ASN = AttributeSetNode::get(unwrap<Function>(F)->getAttributes(), Idx); 1856 for (auto A: make_range(ASN->begin(), ASN->end())) 1857 *Attrs++ = wrap(A); 1858 } 1859 1860 LLVMAttributeRef LLVMGetEnumAttributeAtIndex(LLVMValueRef F, 1861 LLVMAttributeIndex Idx, 1862 unsigned KindID) { 1863 return wrap(unwrap<Function>(F)->getAttribute(Idx, 1864 (Attribute::AttrKind)KindID)); 1865 } 1866 1867 LLVMAttributeRef LLVMGetStringAttributeAtIndex(LLVMValueRef F, 1868 LLVMAttributeIndex Idx, 1869 const char *K, unsigned KLen) { 1870 return wrap(unwrap<Function>(F)->getAttribute(Idx, StringRef(K, KLen))); 1871 } 1872 1873 void LLVMRemoveEnumAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1874 unsigned KindID) { 1875 unwrap<Function>(F)->removeAttribute(Idx, (Attribute::AttrKind)KindID); 1876 } 1877 1878 void LLVMRemoveStringAttributeAtIndex(LLVMValueRef F, LLVMAttributeIndex Idx, 1879 const char *K, unsigned KLen) { 1880 unwrap<Function>(F)->removeAttribute(Idx, StringRef(K, KLen)); 1881 } 1882 1883 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, 1884 const char *V) { 1885 Function *Func = unwrap<Function>(Fn); 1886 AttributeSet::AttrIndex Idx = 1887 AttributeSet::AttrIndex(AttributeSet::FunctionIndex); 1888 AttrBuilder B; 1889 1890 B.addAttribute(A, V); 1891 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B); 1892 Func->addAttributes(Idx, Set); 1893 } 1894 1895 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 1896 Function *Func = unwrap<Function>(Fn); 1897 const AttributeSet PAL = Func->getAttributes(); 1898 AttrBuilder B(PA); 1899 const AttributeSet PALnew = 1900 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex, 1901 AttributeSet::get(Func->getContext(), 1902 AttributeSet::FunctionIndex, B)); 1903 Func->setAttributes(PALnew); 1904 } 1905 1906 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) { 1907 Function *Func = unwrap<Function>(Fn); 1908 const AttributeSet PAL = Func->getAttributes(); 1909 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex); 1910 } 1911 1912 /*--.. Operations on parameters ............................................--*/ 1913 1914 unsigned LLVMCountParams(LLVMValueRef FnRef) { 1915 // This function is strictly redundant to 1916 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef))) 1917 return unwrap<Function>(FnRef)->arg_size(); 1918 } 1919 1920 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { 1921 Function *Fn = unwrap<Function>(FnRef); 1922 for (Function::arg_iterator I = Fn->arg_begin(), 1923 E = Fn->arg_end(); I != E; I++) 1924 *ParamRefs++ = wrap(&*I); 1925 } 1926 1927 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { 1928 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin(); 1929 while (index --> 0) 1930 AI++; 1931 return wrap(&*AI); 1932 } 1933 1934 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) { 1935 return wrap(unwrap<Argument>(V)->getParent()); 1936 } 1937 1938 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) { 1939 Function *Func = unwrap<Function>(Fn); 1940 Function::arg_iterator I = Func->arg_begin(); 1941 if (I == Func->arg_end()) 1942 return nullptr; 1943 return wrap(&*I); 1944 } 1945 1946 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) { 1947 Function *Func = unwrap<Function>(Fn); 1948 Function::arg_iterator I = Func->arg_end(); 1949 if (I == Func->arg_begin()) 1950 return nullptr; 1951 return wrap(&*--I); 1952 } 1953 1954 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) { 1955 Argument *A = unwrap<Argument>(Arg); 1956 Function::arg_iterator I(A); 1957 if (++I == A->getParent()->arg_end()) 1958 return nullptr; 1959 return wrap(&*I); 1960 } 1961 1962 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { 1963 Argument *A = unwrap<Argument>(Arg); 1964 Function::arg_iterator I(A); 1965 if (I == A->getParent()->arg_begin()) 1966 return nullptr; 1967 return wrap(&*--I); 1968 } 1969 1970 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 1971 Argument *A = unwrap<Argument>(Arg); 1972 AttrBuilder B(PA); 1973 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 1974 } 1975 1976 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 1977 Argument *A = unwrap<Argument>(Arg); 1978 AttrBuilder B(PA); 1979 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 1980 } 1981 1982 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) { 1983 Argument *A = unwrap<Argument>(Arg); 1984 return (LLVMAttribute)A->getParent()->getAttributes(). 1985 Raw(A->getArgNo()+1); 1986 } 1987 1988 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { 1989 Argument *A = unwrap<Argument>(Arg); 1990 AttrBuilder B; 1991 B.addAlignmentAttr(align); 1992 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B)); 1993 } 1994 1995 /*--.. Operations on basic blocks ..........................................--*/ 1996 1997 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { 1998 return wrap(static_cast<Value*>(unwrap(BB))); 1999 } 2000 2001 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) { 2002 return isa<BasicBlock>(unwrap(Val)); 2003 } 2004 2005 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) { 2006 return wrap(unwrap<BasicBlock>(Val)); 2007 } 2008 2009 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) { 2010 return unwrap(BB)->getName().data(); 2011 } 2012 2013 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) { 2014 return wrap(unwrap(BB)->getParent()); 2015 } 2016 2017 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) { 2018 return wrap(unwrap(BB)->getTerminator()); 2019 } 2020 2021 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) { 2022 return unwrap<Function>(FnRef)->size(); 2023 } 2024 2025 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){ 2026 Function *Fn = unwrap<Function>(FnRef); 2027 for (BasicBlock &BB : *Fn) 2028 *BasicBlocksRefs++ = wrap(&BB); 2029 } 2030 2031 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) { 2032 return wrap(&unwrap<Function>(Fn)->getEntryBlock()); 2033 } 2034 2035 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) { 2036 Function *Func = unwrap<Function>(Fn); 2037 Function::iterator I = Func->begin(); 2038 if (I == Func->end()) 2039 return nullptr; 2040 return wrap(&*I); 2041 } 2042 2043 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) { 2044 Function *Func = unwrap<Function>(Fn); 2045 Function::iterator I = Func->end(); 2046 if (I == Func->begin()) 2047 return nullptr; 2048 return wrap(&*--I); 2049 } 2050 2051 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) { 2052 BasicBlock *Block = unwrap(BB); 2053 Function::iterator I(Block); 2054 if (++I == Block->getParent()->end()) 2055 return nullptr; 2056 return wrap(&*I); 2057 } 2058 2059 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) { 2060 BasicBlock *Block = unwrap(BB); 2061 Function::iterator I(Block); 2062 if (I == Block->getParent()->begin()) 2063 return nullptr; 2064 return wrap(&*--I); 2065 } 2066 2067 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, 2068 LLVMValueRef FnRef, 2069 const char *Name) { 2070 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef))); 2071 } 2072 2073 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) { 2074 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name); 2075 } 2076 2077 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, 2078 LLVMBasicBlockRef BBRef, 2079 const char *Name) { 2080 BasicBlock *BB = unwrap(BBRef); 2081 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB)); 2082 } 2083 2084 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef, 2085 const char *Name) { 2086 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name); 2087 } 2088 2089 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) { 2090 unwrap(BBRef)->eraseFromParent(); 2091 } 2092 2093 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) { 2094 unwrap(BBRef)->removeFromParent(); 2095 } 2096 2097 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2098 unwrap(BB)->moveBefore(unwrap(MovePos)); 2099 } 2100 2101 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2102 unwrap(BB)->moveAfter(unwrap(MovePos)); 2103 } 2104 2105 /*--.. Operations on instructions ..........................................--*/ 2106 2107 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) { 2108 return wrap(unwrap<Instruction>(Inst)->getParent()); 2109 } 2110 2111 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) { 2112 BasicBlock *Block = unwrap(BB); 2113 BasicBlock::iterator I = Block->begin(); 2114 if (I == Block->end()) 2115 return nullptr; 2116 return wrap(&*I); 2117 } 2118 2119 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) { 2120 BasicBlock *Block = unwrap(BB); 2121 BasicBlock::iterator I = Block->end(); 2122 if (I == Block->begin()) 2123 return nullptr; 2124 return wrap(&*--I); 2125 } 2126 2127 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) { 2128 Instruction *Instr = unwrap<Instruction>(Inst); 2129 BasicBlock::iterator I(Instr); 2130 if (++I == Instr->getParent()->end()) 2131 return nullptr; 2132 return wrap(&*I); 2133 } 2134 2135 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) { 2136 Instruction *Instr = unwrap<Instruction>(Inst); 2137 BasicBlock::iterator I(Instr); 2138 if (I == Instr->getParent()->begin()) 2139 return nullptr; 2140 return wrap(&*--I); 2141 } 2142 2143 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) { 2144 unwrap<Instruction>(Inst)->removeFromParent(); 2145 } 2146 2147 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) { 2148 unwrap<Instruction>(Inst)->eraseFromParent(); 2149 } 2150 2151 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { 2152 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst))) 2153 return (LLVMIntPredicate)I->getPredicate(); 2154 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2155 if (CE->getOpcode() == Instruction::ICmp) 2156 return (LLVMIntPredicate)CE->getPredicate(); 2157 return (LLVMIntPredicate)0; 2158 } 2159 2160 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) { 2161 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst))) 2162 return (LLVMRealPredicate)I->getPredicate(); 2163 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2164 if (CE->getOpcode() == Instruction::FCmp) 2165 return (LLVMRealPredicate)CE->getPredicate(); 2166 return (LLVMRealPredicate)0; 2167 } 2168 2169 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { 2170 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2171 return map_to_llvmopcode(C->getOpcode()); 2172 return (LLVMOpcode)0; 2173 } 2174 2175 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) { 2176 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2177 return wrap(C->clone()); 2178 return nullptr; 2179 } 2180 2181 /*--.. Call and invoke instructions ........................................--*/ 2182 2183 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) { 2184 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands(); 2185 } 2186 2187 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { 2188 return CallSite(unwrap<Instruction>(Instr)).getCallingConv(); 2189 } 2190 2191 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { 2192 return CallSite(unwrap<Instruction>(Instr)) 2193 .setCallingConv(static_cast<CallingConv::ID>(CC)); 2194 } 2195 2196 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 2197 LLVMAttribute PA) { 2198 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2199 AttrBuilder B(PA); 2200 Call.setAttributes( 2201 Call.getAttributes().addAttributes(Call->getContext(), index, 2202 AttributeSet::get(Call->getContext(), 2203 index, B))); 2204 } 2205 2206 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 2207 LLVMAttribute PA) { 2208 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2209 AttrBuilder B(PA); 2210 Call.setAttributes(Call.getAttributes() 2211 .removeAttributes(Call->getContext(), index, 2212 AttributeSet::get(Call->getContext(), 2213 index, B))); 2214 } 2215 2216 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 2217 unsigned align) { 2218 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2219 AttrBuilder B; 2220 B.addAlignmentAttr(align); 2221 Call.setAttributes(Call.getAttributes() 2222 .addAttributes(Call->getContext(), index, 2223 AttributeSet::get(Call->getContext(), 2224 index, B))); 2225 } 2226 2227 void LLVMAddCallSiteAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2228 LLVMAttributeRef A) { 2229 CallSite(unwrap<Instruction>(C)).addAttribute(Idx, unwrap(A)); 2230 } 2231 2232 unsigned LLVMGetCallSiteAttributeCount(LLVMValueRef C, 2233 LLVMAttributeIndex Idx) { 2234 auto CS = CallSite(unwrap<Instruction>(C)); 2235 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx); 2236 return ASN->getNumAttributes(); 2237 } 2238 2239 void LLVMGetCallSiteAttributes(LLVMValueRef C, LLVMAttributeIndex Idx, 2240 LLVMAttributeRef *Attrs) { 2241 auto CS = CallSite(unwrap<Instruction>(C)); 2242 auto *ASN = AttributeSetNode::get(CS.getAttributes(), Idx); 2243 for (auto A: make_range(ASN->begin(), ASN->end())) 2244 *Attrs++ = wrap(A); 2245 } 2246 2247 LLVMAttributeRef LLVMGetCallSiteEnumAttribute(LLVMValueRef C, 2248 LLVMAttributeIndex Idx, 2249 unsigned KindID) { 2250 return wrap(CallSite(unwrap<Instruction>(C)) 2251 .getAttribute(Idx, (Attribute::AttrKind)KindID)); 2252 } 2253 2254 LLVMAttributeRef LLVMGetCallSiteStringAttribute(LLVMValueRef C, 2255 LLVMAttributeIndex Idx, 2256 const char *K, unsigned KLen) { 2257 return wrap(CallSite(unwrap<Instruction>(C)) 2258 .getAttribute(Idx, StringRef(K, KLen))); 2259 } 2260 2261 void LLVMRemoveCallSiteEnumAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2262 unsigned KindID) { 2263 CallSite(unwrap<Instruction>(C)) 2264 .removeAttribute(Idx, (Attribute::AttrKind)KindID); 2265 } 2266 2267 void LLVMRemoveCallSiteStringAttribute(LLVMValueRef C, LLVMAttributeIndex Idx, 2268 const char *K, unsigned KLen) { 2269 CallSite(unwrap<Instruction>(C)).removeAttribute(Idx, StringRef(K, KLen)); 2270 } 2271 2272 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) { 2273 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue()); 2274 } 2275 2276 /*--.. Operations on call instructions (only) ..............................--*/ 2277 2278 LLVMBool LLVMIsTailCall(LLVMValueRef Call) { 2279 return unwrap<CallInst>(Call)->isTailCall(); 2280 } 2281 2282 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) { 2283 unwrap<CallInst>(Call)->setTailCall(isTailCall); 2284 } 2285 2286 /*--.. Operations on invoke instructions (only) ............................--*/ 2287 2288 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) { 2289 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest()); 2290 } 2291 2292 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) { 2293 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest()); 2294 } 2295 2296 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2297 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B)); 2298 } 2299 2300 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2301 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B)); 2302 } 2303 2304 /*--.. Operations on terminators ...........................................--*/ 2305 2306 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) { 2307 return unwrap<TerminatorInst>(Term)->getNumSuccessors(); 2308 } 2309 2310 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) { 2311 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i)); 2312 } 2313 2314 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) { 2315 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block)); 2316 } 2317 2318 /*--.. Operations on branch instructions (only) ............................--*/ 2319 2320 LLVMBool LLVMIsConditional(LLVMValueRef Branch) { 2321 return unwrap<BranchInst>(Branch)->isConditional(); 2322 } 2323 2324 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) { 2325 return wrap(unwrap<BranchInst>(Branch)->getCondition()); 2326 } 2327 2328 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) { 2329 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond)); 2330 } 2331 2332 /*--.. Operations on switch instructions (only) ............................--*/ 2333 2334 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { 2335 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest()); 2336 } 2337 2338 /*--.. Operations on alloca instructions (only) ............................--*/ 2339 2340 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) { 2341 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType()); 2342 } 2343 2344 /*--.. Operations on gep instructions (only) ...............................--*/ 2345 2346 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) { 2347 return unwrap<GetElementPtrInst>(GEP)->isInBounds(); 2348 } 2349 2350 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) { 2351 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds); 2352 } 2353 2354 /*--.. Operations on phi nodes .............................................--*/ 2355 2356 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, 2357 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) { 2358 PHINode *PhiVal = unwrap<PHINode>(PhiNode); 2359 for (unsigned I = 0; I != Count; ++I) 2360 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I])); 2361 } 2362 2363 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) { 2364 return unwrap<PHINode>(PhiNode)->getNumIncomingValues(); 2365 } 2366 2367 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) { 2368 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index)); 2369 } 2370 2371 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) { 2372 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index)); 2373 } 2374 2375 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/ 2376 2377 unsigned LLVMGetNumIndices(LLVMValueRef Inst) { 2378 auto *I = unwrap(Inst); 2379 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) 2380 return GEP->getNumIndices(); 2381 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 2382 return EV->getNumIndices(); 2383 if (auto *IV = dyn_cast<InsertValueInst>(I)) 2384 return IV->getNumIndices(); 2385 llvm_unreachable( 2386 "LLVMGetNumIndices applies only to extractvalue and insertvalue!"); 2387 } 2388 2389 const unsigned *LLVMGetIndices(LLVMValueRef Inst) { 2390 auto *I = unwrap(Inst); 2391 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 2392 return EV->getIndices().data(); 2393 if (auto *IV = dyn_cast<InsertValueInst>(I)) 2394 return IV->getIndices().data(); 2395 llvm_unreachable( 2396 "LLVMGetIndices applies only to extractvalue and insertvalue!"); 2397 } 2398 2399 2400 /*===-- Instruction builders ----------------------------------------------===*/ 2401 2402 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) { 2403 return wrap(new IRBuilder<>(*unwrap(C))); 2404 } 2405 2406 LLVMBuilderRef LLVMCreateBuilder(void) { 2407 return LLVMCreateBuilderInContext(LLVMGetGlobalContext()); 2408 } 2409 2410 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, 2411 LLVMValueRef Instr) { 2412 BasicBlock *BB = unwrap(Block); 2413 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end(); 2414 unwrap(Builder)->SetInsertPoint(BB, I->getIterator()); 2415 } 2416 2417 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2418 Instruction *I = unwrap<Instruction>(Instr); 2419 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator()); 2420 } 2421 2422 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) { 2423 BasicBlock *BB = unwrap(Block); 2424 unwrap(Builder)->SetInsertPoint(BB); 2425 } 2426 2427 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) { 2428 return wrap(unwrap(Builder)->GetInsertBlock()); 2429 } 2430 2431 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) { 2432 unwrap(Builder)->ClearInsertionPoint(); 2433 } 2434 2435 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2436 unwrap(Builder)->Insert(unwrap<Instruction>(Instr)); 2437 } 2438 2439 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, 2440 const char *Name) { 2441 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name); 2442 } 2443 2444 void LLVMDisposeBuilder(LLVMBuilderRef Builder) { 2445 delete unwrap(Builder); 2446 } 2447 2448 /*--.. Metadata builders ...................................................--*/ 2449 2450 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { 2451 MDNode *Loc = 2452 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr; 2453 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc)); 2454 } 2455 2456 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { 2457 LLVMContext &Context = unwrap(Builder)->getContext(); 2458 return wrap(MetadataAsValue::get( 2459 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode())); 2460 } 2461 2462 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { 2463 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst)); 2464 } 2465 2466 2467 /*--.. Instruction builders ................................................--*/ 2468 2469 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { 2470 return wrap(unwrap(B)->CreateRetVoid()); 2471 } 2472 2473 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) { 2474 return wrap(unwrap(B)->CreateRet(unwrap(V))); 2475 } 2476 2477 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals, 2478 unsigned N) { 2479 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N)); 2480 } 2481 2482 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) { 2483 return wrap(unwrap(B)->CreateBr(unwrap(Dest))); 2484 } 2485 2486 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, 2487 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) { 2488 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else))); 2489 } 2490 2491 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V, 2492 LLVMBasicBlockRef Else, unsigned NumCases) { 2493 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases)); 2494 } 2495 2496 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr, 2497 unsigned NumDests) { 2498 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests)); 2499 } 2500 2501 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, 2502 LLVMValueRef *Args, unsigned NumArgs, 2503 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 2504 const char *Name) { 2505 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch), 2506 makeArrayRef(unwrap(Args), NumArgs), 2507 Name)); 2508 } 2509 2510 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, 2511 LLVMValueRef PersFn, unsigned NumClauses, 2512 const char *Name) { 2513 // The personality used to live on the landingpad instruction, but now it 2514 // lives on the parent function. For compatibility, take the provided 2515 // personality and put it on the parent function. 2516 if (PersFn) 2517 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn( 2518 cast<Function>(unwrap(PersFn))); 2519 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name)); 2520 } 2521 2522 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) { 2523 return wrap(unwrap(B)->CreateResume(unwrap(Exn))); 2524 } 2525 2526 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) { 2527 return wrap(unwrap(B)->CreateUnreachable()); 2528 } 2529 2530 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, 2531 LLVMBasicBlockRef Dest) { 2532 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest)); 2533 } 2534 2535 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { 2536 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest)); 2537 } 2538 2539 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) { 2540 return unwrap<LandingPadInst>(LandingPad)->getNumClauses(); 2541 } 2542 2543 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) { 2544 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx)); 2545 } 2546 2547 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) { 2548 unwrap<LandingPadInst>(LandingPad)-> 2549 addClause(cast<Constant>(unwrap(ClauseVal))); 2550 } 2551 2552 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) { 2553 return unwrap<LandingPadInst>(LandingPad)->isCleanup(); 2554 } 2555 2556 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) { 2557 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val); 2558 } 2559 2560 /*--.. Arithmetic ..........................................................--*/ 2561 2562 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2563 const char *Name) { 2564 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name)); 2565 } 2566 2567 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2568 const char *Name) { 2569 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name)); 2570 } 2571 2572 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2573 const char *Name) { 2574 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name)); 2575 } 2576 2577 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2578 const char *Name) { 2579 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name)); 2580 } 2581 2582 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2583 const char *Name) { 2584 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name)); 2585 } 2586 2587 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2588 const char *Name) { 2589 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name)); 2590 } 2591 2592 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2593 const char *Name) { 2594 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name)); 2595 } 2596 2597 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2598 const char *Name) { 2599 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name)); 2600 } 2601 2602 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2603 const char *Name) { 2604 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name)); 2605 } 2606 2607 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2608 const char *Name) { 2609 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name)); 2610 } 2611 2612 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2613 const char *Name) { 2614 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name)); 2615 } 2616 2617 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2618 const char *Name) { 2619 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name)); 2620 } 2621 2622 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2623 const char *Name) { 2624 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name)); 2625 } 2626 2627 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2628 const char *Name) { 2629 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name)); 2630 } 2631 2632 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS, 2633 LLVMValueRef RHS, const char *Name) { 2634 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name)); 2635 } 2636 2637 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2638 const char *Name) { 2639 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name)); 2640 } 2641 2642 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2643 const char *Name) { 2644 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name)); 2645 } 2646 2647 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2648 const char *Name) { 2649 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name)); 2650 } 2651 2652 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2653 const char *Name) { 2654 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name)); 2655 } 2656 2657 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2658 const char *Name) { 2659 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name)); 2660 } 2661 2662 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2663 const char *Name) { 2664 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name)); 2665 } 2666 2667 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2668 const char *Name) { 2669 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name)); 2670 } 2671 2672 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2673 const char *Name) { 2674 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name)); 2675 } 2676 2677 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2678 const char *Name) { 2679 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name)); 2680 } 2681 2682 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2683 const char *Name) { 2684 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name)); 2685 } 2686 2687 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op, 2688 LLVMValueRef LHS, LLVMValueRef RHS, 2689 const char *Name) { 2690 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS), 2691 unwrap(RHS), Name)); 2692 } 2693 2694 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2695 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name)); 2696 } 2697 2698 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V, 2699 const char *Name) { 2700 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name)); 2701 } 2702 2703 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V, 2704 const char *Name) { 2705 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name)); 2706 } 2707 2708 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2709 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name)); 2710 } 2711 2712 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2713 return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); 2714 } 2715 2716 /*--.. Memory ..............................................................--*/ 2717 2718 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2719 const char *Name) { 2720 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2721 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2722 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2723 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2724 ITy, unwrap(Ty), AllocSize, 2725 nullptr, nullptr, ""); 2726 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2727 } 2728 2729 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2730 LLVMValueRef Val, const char *Name) { 2731 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2732 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2733 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2734 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2735 ITy, unwrap(Ty), AllocSize, 2736 unwrap(Val), nullptr, ""); 2737 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2738 } 2739 2740 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2741 const char *Name) { 2742 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name)); 2743 } 2744 2745 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2746 LLVMValueRef Val, const char *Name) { 2747 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name)); 2748 } 2749 2750 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { 2751 return wrap(unwrap(B)->Insert( 2752 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock()))); 2753 } 2754 2755 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, 2756 const char *Name) { 2757 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name)); 2758 } 2759 2760 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 2761 LLVMValueRef PointerVal) { 2762 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal))); 2763 } 2764 2765 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) { 2766 switch (Ordering) { 2767 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic; 2768 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered; 2769 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic; 2770 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire; 2771 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release; 2772 case LLVMAtomicOrderingAcquireRelease: 2773 return AtomicOrdering::AcquireRelease; 2774 case LLVMAtomicOrderingSequentiallyConsistent: 2775 return AtomicOrdering::SequentiallyConsistent; 2776 } 2777 2778 llvm_unreachable("Invalid LLVMAtomicOrdering value!"); 2779 } 2780 2781 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) { 2782 switch (Ordering) { 2783 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic; 2784 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered; 2785 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic; 2786 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire; 2787 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease; 2788 case AtomicOrdering::AcquireRelease: 2789 return LLVMAtomicOrderingAcquireRelease; 2790 case AtomicOrdering::SequentiallyConsistent: 2791 return LLVMAtomicOrderingSequentiallyConsistent; 2792 } 2793 2794 llvm_unreachable("Invalid AtomicOrdering value!"); 2795 } 2796 2797 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering, 2798 LLVMBool isSingleThread, const char *Name) { 2799 return wrap( 2800 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering), 2801 isSingleThread ? SingleThread : CrossThread, 2802 Name)); 2803 } 2804 2805 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2806 LLVMValueRef *Indices, unsigned NumIndices, 2807 const char *Name) { 2808 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2809 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2810 } 2811 2812 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2813 LLVMValueRef *Indices, unsigned NumIndices, 2814 const char *Name) { 2815 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2816 return wrap( 2817 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2818 } 2819 2820 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2821 unsigned Idx, const char *Name) { 2822 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name)); 2823 } 2824 2825 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, 2826 const char *Name) { 2827 return wrap(unwrap(B)->CreateGlobalString(Str, Name)); 2828 } 2829 2830 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, 2831 const char *Name) { 2832 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); 2833 } 2834 2835 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { 2836 Value *P = unwrap<Value>(MemAccessInst); 2837 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2838 return LI->isVolatile(); 2839 return cast<StoreInst>(P)->isVolatile(); 2840 } 2841 2842 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) { 2843 Value *P = unwrap<Value>(MemAccessInst); 2844 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2845 return LI->setVolatile(isVolatile); 2846 return cast<StoreInst>(P)->setVolatile(isVolatile); 2847 } 2848 2849 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) { 2850 Value *P = unwrap<Value>(MemAccessInst); 2851 AtomicOrdering O; 2852 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2853 O = LI->getOrdering(); 2854 else 2855 O = cast<StoreInst>(P)->getOrdering(); 2856 return mapToLLVMOrdering(O); 2857 } 2858 2859 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) { 2860 Value *P = unwrap<Value>(MemAccessInst); 2861 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 2862 2863 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2864 return LI->setOrdering(O); 2865 return cast<StoreInst>(P)->setOrdering(O); 2866 } 2867 2868 /*--.. Casts ...............................................................--*/ 2869 2870 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2871 LLVMTypeRef DestTy, const char *Name) { 2872 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name)); 2873 } 2874 2875 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, 2876 LLVMTypeRef DestTy, const char *Name) { 2877 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name)); 2878 } 2879 2880 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, 2881 LLVMTypeRef DestTy, const char *Name) { 2882 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name)); 2883 } 2884 2885 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, 2886 LLVMTypeRef DestTy, const char *Name) { 2887 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name)); 2888 } 2889 2890 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val, 2891 LLVMTypeRef DestTy, const char *Name) { 2892 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name)); 2893 } 2894 2895 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2896 LLVMTypeRef DestTy, const char *Name) { 2897 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name)); 2898 } 2899 2900 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2901 LLVMTypeRef DestTy, const char *Name) { 2902 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name)); 2903 } 2904 2905 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2906 LLVMTypeRef DestTy, const char *Name) { 2907 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name)); 2908 } 2909 2910 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val, 2911 LLVMTypeRef DestTy, const char *Name) { 2912 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name)); 2913 } 2914 2915 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val, 2916 LLVMTypeRef DestTy, const char *Name) { 2917 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name)); 2918 } 2919 2920 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val, 2921 LLVMTypeRef DestTy, const char *Name) { 2922 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name)); 2923 } 2924 2925 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2926 LLVMTypeRef DestTy, const char *Name) { 2927 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); 2928 } 2929 2930 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val, 2931 LLVMTypeRef DestTy, const char *Name) { 2932 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name)); 2933 } 2934 2935 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2936 LLVMTypeRef DestTy, const char *Name) { 2937 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy), 2938 Name)); 2939 } 2940 2941 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2942 LLVMTypeRef DestTy, const char *Name) { 2943 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy), 2944 Name)); 2945 } 2946 2947 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2948 LLVMTypeRef DestTy, const char *Name) { 2949 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy), 2950 Name)); 2951 } 2952 2953 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val, 2954 LLVMTypeRef DestTy, const char *Name) { 2955 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val), 2956 unwrap(DestTy), Name)); 2957 } 2958 2959 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val, 2960 LLVMTypeRef DestTy, const char *Name) { 2961 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name)); 2962 } 2963 2964 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val, 2965 LLVMTypeRef DestTy, const char *Name) { 2966 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), 2967 /*isSigned*/true, Name)); 2968 } 2969 2970 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val, 2971 LLVMTypeRef DestTy, const char *Name) { 2972 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name)); 2973 } 2974 2975 /*--.. Comparisons .........................................................--*/ 2976 2977 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, 2978 LLVMValueRef LHS, LLVMValueRef RHS, 2979 const char *Name) { 2980 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op), 2981 unwrap(LHS), unwrap(RHS), Name)); 2982 } 2983 2984 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, 2985 LLVMValueRef LHS, LLVMValueRef RHS, 2986 const char *Name) { 2987 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op), 2988 unwrap(LHS), unwrap(RHS), Name)); 2989 } 2990 2991 /*--.. Miscellaneous instructions ..........................................--*/ 2992 2993 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) { 2994 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name)); 2995 } 2996 2997 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, 2998 LLVMValueRef *Args, unsigned NumArgs, 2999 const char *Name) { 3000 return wrap(unwrap(B)->CreateCall(unwrap(Fn), 3001 makeArrayRef(unwrap(Args), NumArgs), 3002 Name)); 3003 } 3004 3005 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, 3006 LLVMValueRef Then, LLVMValueRef Else, 3007 const char *Name) { 3008 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else), 3009 Name)); 3010 } 3011 3012 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List, 3013 LLVMTypeRef Ty, const char *Name) { 3014 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name)); 3015 } 3016 3017 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal, 3018 LLVMValueRef Index, const char *Name) { 3019 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index), 3020 Name)); 3021 } 3022 3023 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal, 3024 LLVMValueRef EltVal, LLVMValueRef Index, 3025 const char *Name) { 3026 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal), 3027 unwrap(Index), Name)); 3028 } 3029 3030 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1, 3031 LLVMValueRef V2, LLVMValueRef Mask, 3032 const char *Name) { 3033 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2), 3034 unwrap(Mask), Name)); 3035 } 3036 3037 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal, 3038 unsigned Index, const char *Name) { 3039 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name)); 3040 } 3041 3042 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal, 3043 LLVMValueRef EltVal, unsigned Index, 3044 const char *Name) { 3045 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal), 3046 Index, Name)); 3047 } 3048 3049 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, 3050 const char *Name) { 3051 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); 3052 } 3053 3054 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val, 3055 const char *Name) { 3056 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name)); 3057 } 3058 3059 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS, 3060 LLVMValueRef RHS, const char *Name) { 3061 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); 3062 } 3063 3064 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op, 3065 LLVMValueRef PTR, LLVMValueRef Val, 3066 LLVMAtomicOrdering ordering, 3067 LLVMBool singleThread) { 3068 AtomicRMWInst::BinOp intop; 3069 switch (op) { 3070 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break; 3071 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break; 3072 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break; 3073 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break; 3074 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break; 3075 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break; 3076 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break; 3077 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break; 3078 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break; 3079 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break; 3080 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break; 3081 } 3082 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val), 3083 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread)); 3084 } 3085 3086 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, 3087 LLVMValueRef Cmp, LLVMValueRef New, 3088 LLVMAtomicOrdering SuccessOrdering, 3089 LLVMAtomicOrdering FailureOrdering, 3090 LLVMBool singleThread) { 3091 3092 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp), 3093 unwrap(New), mapFromLLVMOrdering(SuccessOrdering), 3094 mapFromLLVMOrdering(FailureOrdering), 3095 singleThread ? SingleThread : CrossThread)); 3096 } 3097 3098 3099 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) { 3100 Value *P = unwrap<Value>(AtomicInst); 3101 3102 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 3103 return I->getSynchScope() == SingleThread; 3104 return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread; 3105 } 3106 3107 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) { 3108 Value *P = unwrap<Value>(AtomicInst); 3109 SynchronizationScope Sync = NewValue ? SingleThread : CrossThread; 3110 3111 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 3112 return I->setSynchScope(Sync); 3113 return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync); 3114 } 3115 3116 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) { 3117 Value *P = unwrap<Value>(CmpXchgInst); 3118 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering()); 3119 } 3120 3121 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst, 3122 LLVMAtomicOrdering Ordering) { 3123 Value *P = unwrap<Value>(CmpXchgInst); 3124 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 3125 3126 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O); 3127 } 3128 3129 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) { 3130 Value *P = unwrap<Value>(CmpXchgInst); 3131 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering()); 3132 } 3133 3134 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst, 3135 LLVMAtomicOrdering Ordering) { 3136 Value *P = unwrap<Value>(CmpXchgInst); 3137 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 3138 3139 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O); 3140 } 3141 3142 /*===-- Module providers --------------------------------------------------===*/ 3143 3144 LLVMModuleProviderRef 3145 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) { 3146 return reinterpret_cast<LLVMModuleProviderRef>(M); 3147 } 3148 3149 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) { 3150 delete unwrap(MP); 3151 } 3152 3153 3154 /*===-- Memory buffers ----------------------------------------------------===*/ 3155 3156 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( 3157 const char *Path, 3158 LLVMMemoryBufferRef *OutMemBuf, 3159 char **OutMessage) { 3160 3161 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); 3162 if (std::error_code EC = MBOrErr.getError()) { 3163 *OutMessage = strdup(EC.message().c_str()); 3164 return 1; 3165 } 3166 *OutMemBuf = wrap(MBOrErr.get().release()); 3167 return 0; 3168 } 3169 3170 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, 3171 char **OutMessage) { 3172 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN(); 3173 if (std::error_code EC = MBOrErr.getError()) { 3174 *OutMessage = strdup(EC.message().c_str()); 3175 return 1; 3176 } 3177 *OutMemBuf = wrap(MBOrErr.get().release()); 3178 return 0; 3179 } 3180 3181 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( 3182 const char *InputData, 3183 size_t InputDataLength, 3184 const char *BufferName, 3185 LLVMBool RequiresNullTerminator) { 3186 3187 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength), 3188 StringRef(BufferName), 3189 RequiresNullTerminator).release()); 3190 } 3191 3192 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( 3193 const char *InputData, 3194 size_t InputDataLength, 3195 const char *BufferName) { 3196 3197 return wrap( 3198 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength), 3199 StringRef(BufferName)).release()); 3200 } 3201 3202 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) { 3203 return unwrap(MemBuf)->getBufferStart(); 3204 } 3205 3206 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) { 3207 return unwrap(MemBuf)->getBufferSize(); 3208 } 3209 3210 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { 3211 delete unwrap(MemBuf); 3212 } 3213 3214 /*===-- Pass Registry -----------------------------------------------------===*/ 3215 3216 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) { 3217 return wrap(PassRegistry::getPassRegistry()); 3218 } 3219 3220 /*===-- Pass Manager ------------------------------------------------------===*/ 3221 3222 LLVMPassManagerRef LLVMCreatePassManager() { 3223 return wrap(new legacy::PassManager()); 3224 } 3225 3226 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { 3227 return wrap(new legacy::FunctionPassManager(unwrap(M))); 3228 } 3229 3230 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { 3231 return LLVMCreateFunctionPassManagerForModule( 3232 reinterpret_cast<LLVMModuleRef>(P)); 3233 } 3234 3235 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { 3236 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M)); 3237 } 3238 3239 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { 3240 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization(); 3241 } 3242 3243 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { 3244 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); 3245 } 3246 3247 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { 3248 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization(); 3249 } 3250 3251 void LLVMDisposePassManager(LLVMPassManagerRef PM) { 3252 delete unwrap(PM); 3253 } 3254 3255 /*===-- Threading ------------------------------------------------------===*/ 3256 3257 LLVMBool LLVMStartMultithreaded() { 3258 return LLVMIsMultithreaded(); 3259 } 3260 3261 void LLVMStopMultithreaded() { 3262 } 3263 3264 LLVMBool LLVMIsMultithreaded() { 3265 return llvm_is_multithreaded(); 3266 } 3267