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 return unwrap<GlobalValue>(Global)->getSection(); 1489 } 1490 1491 void LLVMSetSection(LLVMValueRef Global, const char *Section) { 1492 unwrap<GlobalObject>(Global)->setSection(Section); 1493 } 1494 1495 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) { 1496 return static_cast<LLVMVisibility>( 1497 unwrap<GlobalValue>(Global)->getVisibility()); 1498 } 1499 1500 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) { 1501 unwrap<GlobalValue>(Global) 1502 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz)); 1503 } 1504 1505 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) { 1506 return static_cast<LLVMDLLStorageClass>( 1507 unwrap<GlobalValue>(Global)->getDLLStorageClass()); 1508 } 1509 1510 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) { 1511 unwrap<GlobalValue>(Global)->setDLLStorageClass( 1512 static_cast<GlobalValue::DLLStorageClassTypes>(Class)); 1513 } 1514 1515 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) { 1516 return unwrap<GlobalValue>(Global)->hasUnnamedAddr(); 1517 } 1518 1519 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) { 1520 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr); 1521 } 1522 1523 /*--.. Operations on global variables, load and store instructions .........--*/ 1524 1525 unsigned LLVMGetAlignment(LLVMValueRef V) { 1526 Value *P = unwrap<Value>(V); 1527 if (GlobalValue *GV = dyn_cast<GlobalValue>(P)) 1528 return GV->getAlignment(); 1529 if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 1530 return AI->getAlignment(); 1531 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 1532 return LI->getAlignment(); 1533 if (StoreInst *SI = dyn_cast<StoreInst>(P)) 1534 return SI->getAlignment(); 1535 1536 llvm_unreachable( 1537 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment"); 1538 } 1539 1540 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) { 1541 Value *P = unwrap<Value>(V); 1542 if (GlobalObject *GV = dyn_cast<GlobalObject>(P)) 1543 GV->setAlignment(Bytes); 1544 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P)) 1545 AI->setAlignment(Bytes); 1546 else if (LoadInst *LI = dyn_cast<LoadInst>(P)) 1547 LI->setAlignment(Bytes); 1548 else if (StoreInst *SI = dyn_cast<StoreInst>(P)) 1549 SI->setAlignment(Bytes); 1550 else 1551 llvm_unreachable( 1552 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment"); 1553 } 1554 1555 /*--.. Operations on global variables ......................................--*/ 1556 1557 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) { 1558 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 1559 GlobalValue::ExternalLinkage, nullptr, Name)); 1560 } 1561 1562 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty, 1563 const char *Name, 1564 unsigned AddressSpace) { 1565 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false, 1566 GlobalValue::ExternalLinkage, nullptr, Name, 1567 nullptr, GlobalVariable::NotThreadLocal, 1568 AddressSpace)); 1569 } 1570 1571 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) { 1572 return wrap(unwrap(M)->getNamedGlobal(Name)); 1573 } 1574 1575 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) { 1576 Module *Mod = unwrap(M); 1577 Module::global_iterator I = Mod->global_begin(); 1578 if (I == Mod->global_end()) 1579 return nullptr; 1580 return wrap(&*I); 1581 } 1582 1583 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) { 1584 Module *Mod = unwrap(M); 1585 Module::global_iterator I = Mod->global_end(); 1586 if (I == Mod->global_begin()) 1587 return nullptr; 1588 return wrap(&*--I); 1589 } 1590 1591 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) { 1592 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1593 Module::global_iterator I(GV); 1594 if (++I == GV->getParent()->global_end()) 1595 return nullptr; 1596 return wrap(&*I); 1597 } 1598 1599 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) { 1600 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1601 Module::global_iterator I(GV); 1602 if (I == GV->getParent()->global_begin()) 1603 return nullptr; 1604 return wrap(&*--I); 1605 } 1606 1607 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) { 1608 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent(); 1609 } 1610 1611 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) { 1612 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar); 1613 if ( !GV->hasInitializer() ) 1614 return nullptr; 1615 return wrap(GV->getInitializer()); 1616 } 1617 1618 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) { 1619 unwrap<GlobalVariable>(GlobalVar) 1620 ->setInitializer(unwrap<Constant>(ConstantVal)); 1621 } 1622 1623 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) { 1624 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal(); 1625 } 1626 1627 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) { 1628 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0); 1629 } 1630 1631 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) { 1632 return unwrap<GlobalVariable>(GlobalVar)->isConstant(); 1633 } 1634 1635 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) { 1636 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0); 1637 } 1638 1639 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) { 1640 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) { 1641 case GlobalVariable::NotThreadLocal: 1642 return LLVMNotThreadLocal; 1643 case GlobalVariable::GeneralDynamicTLSModel: 1644 return LLVMGeneralDynamicTLSModel; 1645 case GlobalVariable::LocalDynamicTLSModel: 1646 return LLVMLocalDynamicTLSModel; 1647 case GlobalVariable::InitialExecTLSModel: 1648 return LLVMInitialExecTLSModel; 1649 case GlobalVariable::LocalExecTLSModel: 1650 return LLVMLocalExecTLSModel; 1651 } 1652 1653 llvm_unreachable("Invalid GlobalVariable thread local mode"); 1654 } 1655 1656 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) { 1657 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar); 1658 1659 switch (Mode) { 1660 case LLVMNotThreadLocal: 1661 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal); 1662 break; 1663 case LLVMGeneralDynamicTLSModel: 1664 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel); 1665 break; 1666 case LLVMLocalDynamicTLSModel: 1667 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel); 1668 break; 1669 case LLVMInitialExecTLSModel: 1670 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); 1671 break; 1672 case LLVMLocalExecTLSModel: 1673 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel); 1674 break; 1675 } 1676 } 1677 1678 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) { 1679 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized(); 1680 } 1681 1682 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) { 1683 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit); 1684 } 1685 1686 /*--.. Operations on aliases ......................................--*/ 1687 1688 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee, 1689 const char *Name) { 1690 auto *PTy = cast<PointerType>(unwrap(Ty)); 1691 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), 1692 GlobalValue::ExternalLinkage, Name, 1693 unwrap<Constant>(Aliasee), unwrap(M))); 1694 } 1695 1696 /*--.. Operations on functions .............................................--*/ 1697 1698 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name, 1699 LLVMTypeRef FunctionTy) { 1700 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy), 1701 GlobalValue::ExternalLinkage, Name, unwrap(M))); 1702 } 1703 1704 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) { 1705 return wrap(unwrap(M)->getFunction(Name)); 1706 } 1707 1708 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) { 1709 Module *Mod = unwrap(M); 1710 Module::iterator I = Mod->begin(); 1711 if (I == Mod->end()) 1712 return nullptr; 1713 return wrap(&*I); 1714 } 1715 1716 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) { 1717 Module *Mod = unwrap(M); 1718 Module::iterator I = Mod->end(); 1719 if (I == Mod->begin()) 1720 return nullptr; 1721 return wrap(&*--I); 1722 } 1723 1724 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) { 1725 Function *Func = unwrap<Function>(Fn); 1726 Module::iterator I(Func); 1727 if (++I == Func->getParent()->end()) 1728 return nullptr; 1729 return wrap(&*I); 1730 } 1731 1732 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) { 1733 Function *Func = unwrap<Function>(Fn); 1734 Module::iterator I(Func); 1735 if (I == Func->getParent()->begin()) 1736 return nullptr; 1737 return wrap(&*--I); 1738 } 1739 1740 void LLVMDeleteFunction(LLVMValueRef Fn) { 1741 unwrap<Function>(Fn)->eraseFromParent(); 1742 } 1743 1744 LLVMBool LLVMHasPersonalityFn(LLVMValueRef Fn) { 1745 return unwrap<Function>(Fn)->hasPersonalityFn(); 1746 } 1747 1748 LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) { 1749 return wrap(unwrap<Function>(Fn)->getPersonalityFn()); 1750 } 1751 1752 void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) { 1753 unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn)); 1754 } 1755 1756 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { 1757 if (Function *F = dyn_cast<Function>(unwrap(Fn))) 1758 return F->getIntrinsicID(); 1759 return 0; 1760 } 1761 1762 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) { 1763 return unwrap<Function>(Fn)->getCallingConv(); 1764 } 1765 1766 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { 1767 return unwrap<Function>(Fn)->setCallingConv( 1768 static_cast<CallingConv::ID>(CC)); 1769 } 1770 1771 const char *LLVMGetGC(LLVMValueRef Fn) { 1772 Function *F = unwrap<Function>(Fn); 1773 return F->hasGC()? F->getGC().c_str() : nullptr; 1774 } 1775 1776 void LLVMSetGC(LLVMValueRef Fn, const char *GC) { 1777 Function *F = unwrap<Function>(Fn); 1778 if (GC) 1779 F->setGC(GC); 1780 else 1781 F->clearGC(); 1782 } 1783 1784 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 1785 Function *Func = unwrap<Function>(Fn); 1786 const AttributeSet PAL = Func->getAttributes(); 1787 AttrBuilder B(PA); 1788 const AttributeSet PALnew = 1789 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex, 1790 AttributeSet::get(Func->getContext(), 1791 AttributeSet::FunctionIndex, B)); 1792 Func->setAttributes(PALnew); 1793 } 1794 1795 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, 1796 const char *V) { 1797 Function *Func = unwrap<Function>(Fn); 1798 AttributeSet::AttrIndex Idx = 1799 AttributeSet::AttrIndex(AttributeSet::FunctionIndex); 1800 AttrBuilder B; 1801 1802 B.addAttribute(A, V); 1803 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B); 1804 Func->addAttributes(Idx, Set); 1805 } 1806 1807 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 1808 Function *Func = unwrap<Function>(Fn); 1809 const AttributeSet PAL = Func->getAttributes(); 1810 AttrBuilder B(PA); 1811 const AttributeSet PALnew = 1812 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex, 1813 AttributeSet::get(Func->getContext(), 1814 AttributeSet::FunctionIndex, B)); 1815 Func->setAttributes(PALnew); 1816 } 1817 1818 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) { 1819 Function *Func = unwrap<Function>(Fn); 1820 const AttributeSet PAL = Func->getAttributes(); 1821 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex); 1822 } 1823 1824 /*--.. Operations on parameters ............................................--*/ 1825 1826 unsigned LLVMCountParams(LLVMValueRef FnRef) { 1827 // This function is strictly redundant to 1828 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef))) 1829 return unwrap<Function>(FnRef)->arg_size(); 1830 } 1831 1832 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { 1833 Function *Fn = unwrap<Function>(FnRef); 1834 for (Function::arg_iterator I = Fn->arg_begin(), 1835 E = Fn->arg_end(); I != E; I++) 1836 *ParamRefs++ = wrap(&*I); 1837 } 1838 1839 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { 1840 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin(); 1841 while (index --> 0) 1842 AI++; 1843 return wrap(&*AI); 1844 } 1845 1846 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) { 1847 return wrap(unwrap<Argument>(V)->getParent()); 1848 } 1849 1850 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) { 1851 Function *Func = unwrap<Function>(Fn); 1852 Function::arg_iterator I = Func->arg_begin(); 1853 if (I == Func->arg_end()) 1854 return nullptr; 1855 return wrap(&*I); 1856 } 1857 1858 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) { 1859 Function *Func = unwrap<Function>(Fn); 1860 Function::arg_iterator I = Func->arg_end(); 1861 if (I == Func->arg_begin()) 1862 return nullptr; 1863 return wrap(&*--I); 1864 } 1865 1866 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) { 1867 Argument *A = unwrap<Argument>(Arg); 1868 Function::arg_iterator I(A); 1869 if (++I == A->getParent()->arg_end()) 1870 return nullptr; 1871 return wrap(&*I); 1872 } 1873 1874 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { 1875 Argument *A = unwrap<Argument>(Arg); 1876 Function::arg_iterator I(A); 1877 if (I == A->getParent()->arg_begin()) 1878 return nullptr; 1879 return wrap(&*--I); 1880 } 1881 1882 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 1883 Argument *A = unwrap<Argument>(Arg); 1884 AttrBuilder B(PA); 1885 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 1886 } 1887 1888 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 1889 Argument *A = unwrap<Argument>(Arg); 1890 AttrBuilder B(PA); 1891 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 1892 } 1893 1894 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) { 1895 Argument *A = unwrap<Argument>(Arg); 1896 return (LLVMAttribute)A->getParent()->getAttributes(). 1897 Raw(A->getArgNo()+1); 1898 } 1899 1900 1901 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { 1902 Argument *A = unwrap<Argument>(Arg); 1903 AttrBuilder B; 1904 B.addAlignmentAttr(align); 1905 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B)); 1906 } 1907 1908 /*--.. Operations on basic blocks ..........................................--*/ 1909 1910 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { 1911 return wrap(static_cast<Value*>(unwrap(BB))); 1912 } 1913 1914 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) { 1915 return isa<BasicBlock>(unwrap(Val)); 1916 } 1917 1918 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) { 1919 return wrap(unwrap<BasicBlock>(Val)); 1920 } 1921 1922 const char *LLVMGetBasicBlockName(LLVMBasicBlockRef BB) { 1923 return unwrap(BB)->getName().data(); 1924 } 1925 1926 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) { 1927 return wrap(unwrap(BB)->getParent()); 1928 } 1929 1930 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) { 1931 return wrap(unwrap(BB)->getTerminator()); 1932 } 1933 1934 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) { 1935 return unwrap<Function>(FnRef)->size(); 1936 } 1937 1938 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){ 1939 Function *Fn = unwrap<Function>(FnRef); 1940 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) 1941 *BasicBlocksRefs++ = wrap(&*I); 1942 } 1943 1944 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) { 1945 return wrap(&unwrap<Function>(Fn)->getEntryBlock()); 1946 } 1947 1948 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) { 1949 Function *Func = unwrap<Function>(Fn); 1950 Function::iterator I = Func->begin(); 1951 if (I == Func->end()) 1952 return nullptr; 1953 return wrap(&*I); 1954 } 1955 1956 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) { 1957 Function *Func = unwrap<Function>(Fn); 1958 Function::iterator I = Func->end(); 1959 if (I == Func->begin()) 1960 return nullptr; 1961 return wrap(&*--I); 1962 } 1963 1964 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) { 1965 BasicBlock *Block = unwrap(BB); 1966 Function::iterator I(Block); 1967 if (++I == Block->getParent()->end()) 1968 return nullptr; 1969 return wrap(&*I); 1970 } 1971 1972 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) { 1973 BasicBlock *Block = unwrap(BB); 1974 Function::iterator I(Block); 1975 if (I == Block->getParent()->begin()) 1976 return nullptr; 1977 return wrap(&*--I); 1978 } 1979 1980 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, 1981 LLVMValueRef FnRef, 1982 const char *Name) { 1983 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef))); 1984 } 1985 1986 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) { 1987 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name); 1988 } 1989 1990 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, 1991 LLVMBasicBlockRef BBRef, 1992 const char *Name) { 1993 BasicBlock *BB = unwrap(BBRef); 1994 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB)); 1995 } 1996 1997 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef, 1998 const char *Name) { 1999 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name); 2000 } 2001 2002 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) { 2003 unwrap(BBRef)->eraseFromParent(); 2004 } 2005 2006 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) { 2007 unwrap(BBRef)->removeFromParent(); 2008 } 2009 2010 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2011 unwrap(BB)->moveBefore(unwrap(MovePos)); 2012 } 2013 2014 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 2015 unwrap(BB)->moveAfter(unwrap(MovePos)); 2016 } 2017 2018 /*--.. Operations on instructions ..........................................--*/ 2019 2020 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) { 2021 return wrap(unwrap<Instruction>(Inst)->getParent()); 2022 } 2023 2024 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) { 2025 BasicBlock *Block = unwrap(BB); 2026 BasicBlock::iterator I = Block->begin(); 2027 if (I == Block->end()) 2028 return nullptr; 2029 return wrap(&*I); 2030 } 2031 2032 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) { 2033 BasicBlock *Block = unwrap(BB); 2034 BasicBlock::iterator I = Block->end(); 2035 if (I == Block->begin()) 2036 return nullptr; 2037 return wrap(&*--I); 2038 } 2039 2040 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) { 2041 Instruction *Instr = unwrap<Instruction>(Inst); 2042 BasicBlock::iterator I(Instr); 2043 if (++I == Instr->getParent()->end()) 2044 return nullptr; 2045 return wrap(&*I); 2046 } 2047 2048 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) { 2049 Instruction *Instr = unwrap<Instruction>(Inst); 2050 BasicBlock::iterator I(Instr); 2051 if (I == Instr->getParent()->begin()) 2052 return nullptr; 2053 return wrap(&*--I); 2054 } 2055 2056 void LLVMInstructionRemoveFromParent(LLVMValueRef Inst) { 2057 unwrap<Instruction>(Inst)->removeFromParent(); 2058 } 2059 2060 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) { 2061 unwrap<Instruction>(Inst)->eraseFromParent(); 2062 } 2063 2064 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { 2065 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst))) 2066 return (LLVMIntPredicate)I->getPredicate(); 2067 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2068 if (CE->getOpcode() == Instruction::ICmp) 2069 return (LLVMIntPredicate)CE->getPredicate(); 2070 return (LLVMIntPredicate)0; 2071 } 2072 2073 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) { 2074 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst))) 2075 return (LLVMRealPredicate)I->getPredicate(); 2076 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2077 if (CE->getOpcode() == Instruction::FCmp) 2078 return (LLVMRealPredicate)CE->getPredicate(); 2079 return (LLVMRealPredicate)0; 2080 } 2081 2082 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { 2083 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2084 return map_to_llvmopcode(C->getOpcode()); 2085 return (LLVMOpcode)0; 2086 } 2087 2088 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) { 2089 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2090 return wrap(C->clone()); 2091 return nullptr; 2092 } 2093 2094 /*--.. Call and invoke instructions ........................................--*/ 2095 2096 unsigned LLVMGetNumArgOperands(LLVMValueRef Instr) { 2097 return CallSite(unwrap<Instruction>(Instr)).getNumArgOperands(); 2098 } 2099 2100 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { 2101 return CallSite(unwrap<Instruction>(Instr)).getCallingConv(); 2102 } 2103 2104 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { 2105 return CallSite(unwrap<Instruction>(Instr)) 2106 .setCallingConv(static_cast<CallingConv::ID>(CC)); 2107 } 2108 2109 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 2110 LLVMAttribute PA) { 2111 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2112 AttrBuilder B(PA); 2113 Call.setAttributes( 2114 Call.getAttributes().addAttributes(Call->getContext(), index, 2115 AttributeSet::get(Call->getContext(), 2116 index, B))); 2117 } 2118 2119 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 2120 LLVMAttribute PA) { 2121 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2122 AttrBuilder B(PA); 2123 Call.setAttributes(Call.getAttributes() 2124 .removeAttributes(Call->getContext(), index, 2125 AttributeSet::get(Call->getContext(), 2126 index, B))); 2127 } 2128 2129 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 2130 unsigned align) { 2131 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2132 AttrBuilder B; 2133 B.addAlignmentAttr(align); 2134 Call.setAttributes(Call.getAttributes() 2135 .addAttributes(Call->getContext(), index, 2136 AttributeSet::get(Call->getContext(), 2137 index, B))); 2138 } 2139 2140 LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr) { 2141 return wrap(CallSite(unwrap<Instruction>(Instr)).getCalledValue()); 2142 } 2143 2144 /*--.. Operations on call instructions (only) ..............................--*/ 2145 2146 LLVMBool LLVMIsTailCall(LLVMValueRef Call) { 2147 return unwrap<CallInst>(Call)->isTailCall(); 2148 } 2149 2150 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) { 2151 unwrap<CallInst>(Call)->setTailCall(isTailCall); 2152 } 2153 2154 /*--.. Operations on invoke instructions (only) ............................--*/ 2155 2156 LLVMBasicBlockRef LLVMGetNormalDest(LLVMValueRef Invoke) { 2157 return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest()); 2158 } 2159 2160 LLVMBasicBlockRef LLVMGetUnwindDest(LLVMValueRef Invoke) { 2161 return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest()); 2162 } 2163 2164 void LLVMSetNormalDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2165 unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B)); 2166 } 2167 2168 void LLVMSetUnwindDest(LLVMValueRef Invoke, LLVMBasicBlockRef B) { 2169 unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B)); 2170 } 2171 2172 /*--.. Operations on terminators ...........................................--*/ 2173 2174 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) { 2175 return unwrap<TerminatorInst>(Term)->getNumSuccessors(); 2176 } 2177 2178 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) { 2179 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i)); 2180 } 2181 2182 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) { 2183 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block)); 2184 } 2185 2186 /*--.. Operations on branch instructions (only) ............................--*/ 2187 2188 LLVMBool LLVMIsConditional(LLVMValueRef Branch) { 2189 return unwrap<BranchInst>(Branch)->isConditional(); 2190 } 2191 2192 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) { 2193 return wrap(unwrap<BranchInst>(Branch)->getCondition()); 2194 } 2195 2196 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) { 2197 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond)); 2198 } 2199 2200 /*--.. Operations on switch instructions (only) ............................--*/ 2201 2202 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { 2203 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest()); 2204 } 2205 2206 /*--.. Operations on alloca instructions (only) ............................--*/ 2207 2208 LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) { 2209 return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType()); 2210 } 2211 2212 /*--.. Operations on gep instructions (only) ...............................--*/ 2213 2214 LLVMBool LLVMIsInBounds(LLVMValueRef GEP) { 2215 return unwrap<GetElementPtrInst>(GEP)->isInBounds(); 2216 } 2217 2218 void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool b) { 2219 return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(b); 2220 } 2221 2222 /*--.. Operations on phi nodes .............................................--*/ 2223 2224 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, 2225 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) { 2226 PHINode *PhiVal = unwrap<PHINode>(PhiNode); 2227 for (unsigned I = 0; I != Count; ++I) 2228 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I])); 2229 } 2230 2231 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) { 2232 return unwrap<PHINode>(PhiNode)->getNumIncomingValues(); 2233 } 2234 2235 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) { 2236 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index)); 2237 } 2238 2239 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) { 2240 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index)); 2241 } 2242 2243 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/ 2244 2245 unsigned LLVMGetNumIndices(LLVMValueRef Inst) { 2246 auto *I = unwrap(Inst); 2247 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) 2248 return GEP->getNumIndices(); 2249 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 2250 return EV->getNumIndices(); 2251 if (auto *IV = dyn_cast<InsertValueInst>(I)) 2252 return IV->getNumIndices(); 2253 llvm_unreachable( 2254 "LLVMGetNumIndices applies only to extractvalue and insertvalue!"); 2255 } 2256 2257 const unsigned *LLVMGetIndices(LLVMValueRef Inst) { 2258 auto *I = unwrap(Inst); 2259 if (auto *EV = dyn_cast<ExtractValueInst>(I)) 2260 return EV->getIndices().data(); 2261 if (auto *IV = dyn_cast<InsertValueInst>(I)) 2262 return IV->getIndices().data(); 2263 llvm_unreachable( 2264 "LLVMGetIndices applies only to extractvalue and insertvalue!"); 2265 } 2266 2267 2268 /*===-- Instruction builders ----------------------------------------------===*/ 2269 2270 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) { 2271 return wrap(new IRBuilder<>(*unwrap(C))); 2272 } 2273 2274 LLVMBuilderRef LLVMCreateBuilder(void) { 2275 return LLVMCreateBuilderInContext(LLVMGetGlobalContext()); 2276 } 2277 2278 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, 2279 LLVMValueRef Instr) { 2280 BasicBlock *BB = unwrap(Block); 2281 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end(); 2282 unwrap(Builder)->SetInsertPoint(BB, I->getIterator()); 2283 } 2284 2285 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2286 Instruction *I = unwrap<Instruction>(Instr); 2287 unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator()); 2288 } 2289 2290 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) { 2291 BasicBlock *BB = unwrap(Block); 2292 unwrap(Builder)->SetInsertPoint(BB); 2293 } 2294 2295 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) { 2296 return wrap(unwrap(Builder)->GetInsertBlock()); 2297 } 2298 2299 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) { 2300 unwrap(Builder)->ClearInsertionPoint(); 2301 } 2302 2303 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2304 unwrap(Builder)->Insert(unwrap<Instruction>(Instr)); 2305 } 2306 2307 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, 2308 const char *Name) { 2309 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name); 2310 } 2311 2312 void LLVMDisposeBuilder(LLVMBuilderRef Builder) { 2313 delete unwrap(Builder); 2314 } 2315 2316 /*--.. Metadata builders ...................................................--*/ 2317 2318 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { 2319 MDNode *Loc = 2320 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr; 2321 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc)); 2322 } 2323 2324 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { 2325 LLVMContext &Context = unwrap(Builder)->getContext(); 2326 return wrap(MetadataAsValue::get( 2327 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode())); 2328 } 2329 2330 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { 2331 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst)); 2332 } 2333 2334 2335 /*--.. Instruction builders ................................................--*/ 2336 2337 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { 2338 return wrap(unwrap(B)->CreateRetVoid()); 2339 } 2340 2341 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) { 2342 return wrap(unwrap(B)->CreateRet(unwrap(V))); 2343 } 2344 2345 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals, 2346 unsigned N) { 2347 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N)); 2348 } 2349 2350 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) { 2351 return wrap(unwrap(B)->CreateBr(unwrap(Dest))); 2352 } 2353 2354 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, 2355 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) { 2356 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else))); 2357 } 2358 2359 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V, 2360 LLVMBasicBlockRef Else, unsigned NumCases) { 2361 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases)); 2362 } 2363 2364 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr, 2365 unsigned NumDests) { 2366 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests)); 2367 } 2368 2369 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, 2370 LLVMValueRef *Args, unsigned NumArgs, 2371 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 2372 const char *Name) { 2373 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch), 2374 makeArrayRef(unwrap(Args), NumArgs), 2375 Name)); 2376 } 2377 2378 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, 2379 LLVMValueRef PersFn, unsigned NumClauses, 2380 const char *Name) { 2381 // The personality used to live on the landingpad instruction, but now it 2382 // lives on the parent function. For compatibility, take the provided 2383 // personality and put it on the parent function. 2384 if (PersFn) 2385 unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn( 2386 cast<Function>(unwrap(PersFn))); 2387 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name)); 2388 } 2389 2390 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) { 2391 return wrap(unwrap(B)->CreateResume(unwrap(Exn))); 2392 } 2393 2394 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) { 2395 return wrap(unwrap(B)->CreateUnreachable()); 2396 } 2397 2398 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, 2399 LLVMBasicBlockRef Dest) { 2400 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest)); 2401 } 2402 2403 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { 2404 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest)); 2405 } 2406 2407 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) { 2408 return unwrap<LandingPadInst>(LandingPad)->getNumClauses(); 2409 } 2410 2411 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) { 2412 return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx)); 2413 } 2414 2415 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) { 2416 unwrap<LandingPadInst>(LandingPad)-> 2417 addClause(cast<Constant>(unwrap(ClauseVal))); 2418 } 2419 2420 LLVMBool LLVMIsCleanup(LLVMValueRef LandingPad) { 2421 return unwrap<LandingPadInst>(LandingPad)->isCleanup(); 2422 } 2423 2424 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) { 2425 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val); 2426 } 2427 2428 /*--.. Arithmetic ..........................................................--*/ 2429 2430 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2431 const char *Name) { 2432 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name)); 2433 } 2434 2435 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2436 const char *Name) { 2437 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name)); 2438 } 2439 2440 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2441 const char *Name) { 2442 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name)); 2443 } 2444 2445 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2446 const char *Name) { 2447 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name)); 2448 } 2449 2450 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2451 const char *Name) { 2452 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name)); 2453 } 2454 2455 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2456 const char *Name) { 2457 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name)); 2458 } 2459 2460 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2461 const char *Name) { 2462 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name)); 2463 } 2464 2465 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2466 const char *Name) { 2467 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name)); 2468 } 2469 2470 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2471 const char *Name) { 2472 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name)); 2473 } 2474 2475 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2476 const char *Name) { 2477 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name)); 2478 } 2479 2480 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2481 const char *Name) { 2482 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name)); 2483 } 2484 2485 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2486 const char *Name) { 2487 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name)); 2488 } 2489 2490 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2491 const char *Name) { 2492 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name)); 2493 } 2494 2495 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2496 const char *Name) { 2497 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name)); 2498 } 2499 2500 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS, 2501 LLVMValueRef RHS, const char *Name) { 2502 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name)); 2503 } 2504 2505 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2506 const char *Name) { 2507 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name)); 2508 } 2509 2510 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2511 const char *Name) { 2512 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name)); 2513 } 2514 2515 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2516 const char *Name) { 2517 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name)); 2518 } 2519 2520 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2521 const char *Name) { 2522 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name)); 2523 } 2524 2525 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2526 const char *Name) { 2527 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name)); 2528 } 2529 2530 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2531 const char *Name) { 2532 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name)); 2533 } 2534 2535 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2536 const char *Name) { 2537 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name)); 2538 } 2539 2540 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2541 const char *Name) { 2542 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name)); 2543 } 2544 2545 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2546 const char *Name) { 2547 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name)); 2548 } 2549 2550 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2551 const char *Name) { 2552 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name)); 2553 } 2554 2555 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op, 2556 LLVMValueRef LHS, LLVMValueRef RHS, 2557 const char *Name) { 2558 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS), 2559 unwrap(RHS), Name)); 2560 } 2561 2562 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2563 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name)); 2564 } 2565 2566 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V, 2567 const char *Name) { 2568 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name)); 2569 } 2570 2571 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V, 2572 const char *Name) { 2573 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name)); 2574 } 2575 2576 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2577 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name)); 2578 } 2579 2580 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2581 return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); 2582 } 2583 2584 /*--.. Memory ..............................................................--*/ 2585 2586 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2587 const char *Name) { 2588 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2589 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2590 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2591 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2592 ITy, unwrap(Ty), AllocSize, 2593 nullptr, nullptr, ""); 2594 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2595 } 2596 2597 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2598 LLVMValueRef Val, const char *Name) { 2599 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2600 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2601 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2602 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2603 ITy, unwrap(Ty), AllocSize, 2604 unwrap(Val), nullptr, ""); 2605 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2606 } 2607 2608 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2609 const char *Name) { 2610 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name)); 2611 } 2612 2613 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2614 LLVMValueRef Val, const char *Name) { 2615 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name)); 2616 } 2617 2618 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { 2619 return wrap(unwrap(B)->Insert( 2620 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock()))); 2621 } 2622 2623 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, 2624 const char *Name) { 2625 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name)); 2626 } 2627 2628 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 2629 LLVMValueRef PointerVal) { 2630 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal))); 2631 } 2632 2633 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) { 2634 switch (Ordering) { 2635 case LLVMAtomicOrderingNotAtomic: return AtomicOrdering::NotAtomic; 2636 case LLVMAtomicOrderingUnordered: return AtomicOrdering::Unordered; 2637 case LLVMAtomicOrderingMonotonic: return AtomicOrdering::Monotonic; 2638 case LLVMAtomicOrderingAcquire: return AtomicOrdering::Acquire; 2639 case LLVMAtomicOrderingRelease: return AtomicOrdering::Release; 2640 case LLVMAtomicOrderingAcquireRelease: 2641 return AtomicOrdering::AcquireRelease; 2642 case LLVMAtomicOrderingSequentiallyConsistent: 2643 return AtomicOrdering::SequentiallyConsistent; 2644 } 2645 2646 llvm_unreachable("Invalid LLVMAtomicOrdering value!"); 2647 } 2648 2649 static LLVMAtomicOrdering mapToLLVMOrdering(AtomicOrdering Ordering) { 2650 switch (Ordering) { 2651 case AtomicOrdering::NotAtomic: return LLVMAtomicOrderingNotAtomic; 2652 case AtomicOrdering::Unordered: return LLVMAtomicOrderingUnordered; 2653 case AtomicOrdering::Monotonic: return LLVMAtomicOrderingMonotonic; 2654 case AtomicOrdering::Acquire: return LLVMAtomicOrderingAcquire; 2655 case AtomicOrdering::Release: return LLVMAtomicOrderingRelease; 2656 case AtomicOrdering::AcquireRelease: 2657 return LLVMAtomicOrderingAcquireRelease; 2658 case AtomicOrdering::SequentiallyConsistent: 2659 return LLVMAtomicOrderingSequentiallyConsistent; 2660 } 2661 2662 llvm_unreachable("Invalid AtomicOrdering value!"); 2663 } 2664 2665 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering, 2666 LLVMBool isSingleThread, const char *Name) { 2667 return wrap( 2668 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering), 2669 isSingleThread ? SingleThread : CrossThread, 2670 Name)); 2671 } 2672 2673 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2674 LLVMValueRef *Indices, unsigned NumIndices, 2675 const char *Name) { 2676 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2677 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2678 } 2679 2680 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2681 LLVMValueRef *Indices, unsigned NumIndices, 2682 const char *Name) { 2683 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2684 return wrap( 2685 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2686 } 2687 2688 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2689 unsigned Idx, const char *Name) { 2690 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name)); 2691 } 2692 2693 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, 2694 const char *Name) { 2695 return wrap(unwrap(B)->CreateGlobalString(Str, Name)); 2696 } 2697 2698 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, 2699 const char *Name) { 2700 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); 2701 } 2702 2703 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { 2704 Value *P = unwrap<Value>(MemAccessInst); 2705 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2706 return LI->isVolatile(); 2707 return cast<StoreInst>(P)->isVolatile(); 2708 } 2709 2710 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) { 2711 Value *P = unwrap<Value>(MemAccessInst); 2712 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2713 return LI->setVolatile(isVolatile); 2714 return cast<StoreInst>(P)->setVolatile(isVolatile); 2715 } 2716 2717 LLVMAtomicOrdering LLVMGetOrdering(LLVMValueRef MemAccessInst) { 2718 Value *P = unwrap<Value>(MemAccessInst); 2719 AtomicOrdering O; 2720 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2721 O = LI->getOrdering(); 2722 else 2723 O = cast<StoreInst>(P)->getOrdering(); 2724 return mapToLLVMOrdering(O); 2725 } 2726 2727 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) { 2728 Value *P = unwrap<Value>(MemAccessInst); 2729 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 2730 2731 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2732 return LI->setOrdering(O); 2733 return cast<StoreInst>(P)->setOrdering(O); 2734 } 2735 2736 /*--.. Casts ...............................................................--*/ 2737 2738 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2739 LLVMTypeRef DestTy, const char *Name) { 2740 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name)); 2741 } 2742 2743 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, 2744 LLVMTypeRef DestTy, const char *Name) { 2745 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name)); 2746 } 2747 2748 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, 2749 LLVMTypeRef DestTy, const char *Name) { 2750 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name)); 2751 } 2752 2753 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, 2754 LLVMTypeRef DestTy, const char *Name) { 2755 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name)); 2756 } 2757 2758 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val, 2759 LLVMTypeRef DestTy, const char *Name) { 2760 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name)); 2761 } 2762 2763 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2764 LLVMTypeRef DestTy, const char *Name) { 2765 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name)); 2766 } 2767 2768 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2769 LLVMTypeRef DestTy, const char *Name) { 2770 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name)); 2771 } 2772 2773 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2774 LLVMTypeRef DestTy, const char *Name) { 2775 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name)); 2776 } 2777 2778 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val, 2779 LLVMTypeRef DestTy, const char *Name) { 2780 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name)); 2781 } 2782 2783 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val, 2784 LLVMTypeRef DestTy, const char *Name) { 2785 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name)); 2786 } 2787 2788 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val, 2789 LLVMTypeRef DestTy, const char *Name) { 2790 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name)); 2791 } 2792 2793 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2794 LLVMTypeRef DestTy, const char *Name) { 2795 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); 2796 } 2797 2798 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val, 2799 LLVMTypeRef DestTy, const char *Name) { 2800 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name)); 2801 } 2802 2803 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2804 LLVMTypeRef DestTy, const char *Name) { 2805 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy), 2806 Name)); 2807 } 2808 2809 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2810 LLVMTypeRef DestTy, const char *Name) { 2811 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy), 2812 Name)); 2813 } 2814 2815 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2816 LLVMTypeRef DestTy, const char *Name) { 2817 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy), 2818 Name)); 2819 } 2820 2821 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val, 2822 LLVMTypeRef DestTy, const char *Name) { 2823 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val), 2824 unwrap(DestTy), Name)); 2825 } 2826 2827 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val, 2828 LLVMTypeRef DestTy, const char *Name) { 2829 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name)); 2830 } 2831 2832 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val, 2833 LLVMTypeRef DestTy, const char *Name) { 2834 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), 2835 /*isSigned*/true, Name)); 2836 } 2837 2838 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val, 2839 LLVMTypeRef DestTy, const char *Name) { 2840 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name)); 2841 } 2842 2843 /*--.. Comparisons .........................................................--*/ 2844 2845 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, 2846 LLVMValueRef LHS, LLVMValueRef RHS, 2847 const char *Name) { 2848 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op), 2849 unwrap(LHS), unwrap(RHS), Name)); 2850 } 2851 2852 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, 2853 LLVMValueRef LHS, LLVMValueRef RHS, 2854 const char *Name) { 2855 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op), 2856 unwrap(LHS), unwrap(RHS), Name)); 2857 } 2858 2859 /*--.. Miscellaneous instructions ..........................................--*/ 2860 2861 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) { 2862 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name)); 2863 } 2864 2865 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, 2866 LLVMValueRef *Args, unsigned NumArgs, 2867 const char *Name) { 2868 return wrap(unwrap(B)->CreateCall(unwrap(Fn), 2869 makeArrayRef(unwrap(Args), NumArgs), 2870 Name)); 2871 } 2872 2873 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, 2874 LLVMValueRef Then, LLVMValueRef Else, 2875 const char *Name) { 2876 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else), 2877 Name)); 2878 } 2879 2880 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List, 2881 LLVMTypeRef Ty, const char *Name) { 2882 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name)); 2883 } 2884 2885 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal, 2886 LLVMValueRef Index, const char *Name) { 2887 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index), 2888 Name)); 2889 } 2890 2891 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal, 2892 LLVMValueRef EltVal, LLVMValueRef Index, 2893 const char *Name) { 2894 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal), 2895 unwrap(Index), Name)); 2896 } 2897 2898 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1, 2899 LLVMValueRef V2, LLVMValueRef Mask, 2900 const char *Name) { 2901 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2), 2902 unwrap(Mask), Name)); 2903 } 2904 2905 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal, 2906 unsigned Index, const char *Name) { 2907 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name)); 2908 } 2909 2910 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal, 2911 LLVMValueRef EltVal, unsigned Index, 2912 const char *Name) { 2913 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal), 2914 Index, Name)); 2915 } 2916 2917 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, 2918 const char *Name) { 2919 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); 2920 } 2921 2922 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val, 2923 const char *Name) { 2924 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name)); 2925 } 2926 2927 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS, 2928 LLVMValueRef RHS, const char *Name) { 2929 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); 2930 } 2931 2932 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op, 2933 LLVMValueRef PTR, LLVMValueRef Val, 2934 LLVMAtomicOrdering ordering, 2935 LLVMBool singleThread) { 2936 AtomicRMWInst::BinOp intop; 2937 switch (op) { 2938 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break; 2939 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break; 2940 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break; 2941 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break; 2942 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break; 2943 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break; 2944 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break; 2945 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break; 2946 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break; 2947 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break; 2948 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break; 2949 } 2950 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val), 2951 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread)); 2952 } 2953 2954 LLVMValueRef LLVMBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Ptr, 2955 LLVMValueRef Cmp, LLVMValueRef New, 2956 LLVMAtomicOrdering SuccessOrdering, 2957 LLVMAtomicOrdering FailureOrdering, 2958 LLVMBool singleThread) { 2959 2960 return wrap(unwrap(B)->CreateAtomicCmpXchg(unwrap(Ptr), unwrap(Cmp), 2961 unwrap(New), mapFromLLVMOrdering(SuccessOrdering), 2962 mapFromLLVMOrdering(FailureOrdering), 2963 singleThread ? SingleThread : CrossThread)); 2964 } 2965 2966 2967 LLVMBool LLVMIsAtomicSingleThread(LLVMValueRef AtomicInst) { 2968 Value *P = unwrap<Value>(AtomicInst); 2969 2970 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 2971 return I->getSynchScope() == SingleThread; 2972 return cast<AtomicCmpXchgInst>(P)->getSynchScope() == SingleThread; 2973 } 2974 2975 void LLVMSetAtomicSingleThread(LLVMValueRef AtomicInst, LLVMBool NewValue) { 2976 Value *P = unwrap<Value>(AtomicInst); 2977 SynchronizationScope Sync = NewValue ? SingleThread : CrossThread; 2978 2979 if (AtomicRMWInst *I = dyn_cast<AtomicRMWInst>(P)) 2980 return I->setSynchScope(Sync); 2981 return cast<AtomicCmpXchgInst>(P)->setSynchScope(Sync); 2982 } 2983 2984 LLVMAtomicOrdering LLVMGetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst) { 2985 Value *P = unwrap<Value>(CmpXchgInst); 2986 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getSuccessOrdering()); 2987 } 2988 2989 void LLVMSetCmpXchgSuccessOrdering(LLVMValueRef CmpXchgInst, 2990 LLVMAtomicOrdering Ordering) { 2991 Value *P = unwrap<Value>(CmpXchgInst); 2992 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 2993 2994 return cast<AtomicCmpXchgInst>(P)->setSuccessOrdering(O); 2995 } 2996 2997 LLVMAtomicOrdering LLVMGetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst) { 2998 Value *P = unwrap<Value>(CmpXchgInst); 2999 return mapToLLVMOrdering(cast<AtomicCmpXchgInst>(P)->getFailureOrdering()); 3000 } 3001 3002 void LLVMSetCmpXchgFailureOrdering(LLVMValueRef CmpXchgInst, 3003 LLVMAtomicOrdering Ordering) { 3004 Value *P = unwrap<Value>(CmpXchgInst); 3005 AtomicOrdering O = mapFromLLVMOrdering(Ordering); 3006 3007 return cast<AtomicCmpXchgInst>(P)->setFailureOrdering(O); 3008 } 3009 3010 /*===-- Module providers --------------------------------------------------===*/ 3011 3012 LLVMModuleProviderRef 3013 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) { 3014 return reinterpret_cast<LLVMModuleProviderRef>(M); 3015 } 3016 3017 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) { 3018 delete unwrap(MP); 3019 } 3020 3021 3022 /*===-- Memory buffers ----------------------------------------------------===*/ 3023 3024 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( 3025 const char *Path, 3026 LLVMMemoryBufferRef *OutMemBuf, 3027 char **OutMessage) { 3028 3029 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); 3030 if (std::error_code EC = MBOrErr.getError()) { 3031 *OutMessage = strdup(EC.message().c_str()); 3032 return 1; 3033 } 3034 *OutMemBuf = wrap(MBOrErr.get().release()); 3035 return 0; 3036 } 3037 3038 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, 3039 char **OutMessage) { 3040 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN(); 3041 if (std::error_code EC = MBOrErr.getError()) { 3042 *OutMessage = strdup(EC.message().c_str()); 3043 return 1; 3044 } 3045 *OutMemBuf = wrap(MBOrErr.get().release()); 3046 return 0; 3047 } 3048 3049 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( 3050 const char *InputData, 3051 size_t InputDataLength, 3052 const char *BufferName, 3053 LLVMBool RequiresNullTerminator) { 3054 3055 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength), 3056 StringRef(BufferName), 3057 RequiresNullTerminator).release()); 3058 } 3059 3060 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( 3061 const char *InputData, 3062 size_t InputDataLength, 3063 const char *BufferName) { 3064 3065 return wrap( 3066 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength), 3067 StringRef(BufferName)).release()); 3068 } 3069 3070 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) { 3071 return unwrap(MemBuf)->getBufferStart(); 3072 } 3073 3074 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) { 3075 return unwrap(MemBuf)->getBufferSize(); 3076 } 3077 3078 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { 3079 delete unwrap(MemBuf); 3080 } 3081 3082 /*===-- Pass Registry -----------------------------------------------------===*/ 3083 3084 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) { 3085 return wrap(PassRegistry::getPassRegistry()); 3086 } 3087 3088 /*===-- Pass Manager ------------------------------------------------------===*/ 3089 3090 LLVMPassManagerRef LLVMCreatePassManager() { 3091 return wrap(new legacy::PassManager()); 3092 } 3093 3094 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { 3095 return wrap(new legacy::FunctionPassManager(unwrap(M))); 3096 } 3097 3098 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { 3099 return LLVMCreateFunctionPassManagerForModule( 3100 reinterpret_cast<LLVMModuleRef>(P)); 3101 } 3102 3103 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { 3104 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M)); 3105 } 3106 3107 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { 3108 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization(); 3109 } 3110 3111 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { 3112 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); 3113 } 3114 3115 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { 3116 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization(); 3117 } 3118 3119 void LLVMDisposePassManager(LLVMPassManagerRef PM) { 3120 delete unwrap(PM); 3121 } 3122 3123 /*===-- Threading ------------------------------------------------------===*/ 3124 3125 LLVMBool LLVMStartMultithreaded() { 3126 return LLVMIsMultithreaded(); 3127 } 3128 3129 void LLVMStopMultithreaded() { 3130 } 3131 3132 LLVMBool LLVMIsMultithreaded() { 3133 return llvm_is_multithreaded(); 3134 } 3135