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