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