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