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