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