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