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