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