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