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