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