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