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