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