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