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