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