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