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