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