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