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