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