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