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