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