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