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