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