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 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) { 1695 if (Function *F = dyn_cast<Function>(unwrap(Fn))) 1696 return F->getIntrinsicID(); 1697 return 0; 1698 } 1699 1700 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) { 1701 return unwrap<Function>(Fn)->getCallingConv(); 1702 } 1703 1704 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) { 1705 return unwrap<Function>(Fn)->setCallingConv( 1706 static_cast<CallingConv::ID>(CC)); 1707 } 1708 1709 const char *LLVMGetGC(LLVMValueRef Fn) { 1710 Function *F = unwrap<Function>(Fn); 1711 return F->hasGC()? F->getGC() : nullptr; 1712 } 1713 1714 void LLVMSetGC(LLVMValueRef Fn, const char *GC) { 1715 Function *F = unwrap<Function>(Fn); 1716 if (GC) 1717 F->setGC(GC); 1718 else 1719 F->clearGC(); 1720 } 1721 1722 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 1723 Function *Func = unwrap<Function>(Fn); 1724 const AttributeSet PAL = Func->getAttributes(); 1725 AttrBuilder B(PA); 1726 const AttributeSet PALnew = 1727 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex, 1728 AttributeSet::get(Func->getContext(), 1729 AttributeSet::FunctionIndex, B)); 1730 Func->setAttributes(PALnew); 1731 } 1732 1733 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A, 1734 const char *V) { 1735 Function *Func = unwrap<Function>(Fn); 1736 AttributeSet::AttrIndex Idx = 1737 AttributeSet::AttrIndex(AttributeSet::FunctionIndex); 1738 AttrBuilder B; 1739 1740 B.addAttribute(A, V); 1741 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B); 1742 Func->addAttributes(Idx, Set); 1743 } 1744 1745 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { 1746 Function *Func = unwrap<Function>(Fn); 1747 const AttributeSet PAL = Func->getAttributes(); 1748 AttrBuilder B(PA); 1749 const AttributeSet PALnew = 1750 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex, 1751 AttributeSet::get(Func->getContext(), 1752 AttributeSet::FunctionIndex, B)); 1753 Func->setAttributes(PALnew); 1754 } 1755 1756 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) { 1757 Function *Func = unwrap<Function>(Fn); 1758 const AttributeSet PAL = Func->getAttributes(); 1759 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex); 1760 } 1761 1762 /*--.. Operations on parameters ............................................--*/ 1763 1764 unsigned LLVMCountParams(LLVMValueRef FnRef) { 1765 // This function is strictly redundant to 1766 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef))) 1767 return unwrap<Function>(FnRef)->arg_size(); 1768 } 1769 1770 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) { 1771 Function *Fn = unwrap<Function>(FnRef); 1772 for (Function::arg_iterator I = Fn->arg_begin(), 1773 E = Fn->arg_end(); I != E; I++) 1774 *ParamRefs++ = wrap(I); 1775 } 1776 1777 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) { 1778 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin(); 1779 while (index --> 0) 1780 AI++; 1781 return wrap(AI); 1782 } 1783 1784 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) { 1785 return wrap(unwrap<Argument>(V)->getParent()); 1786 } 1787 1788 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) { 1789 Function *Func = unwrap<Function>(Fn); 1790 Function::arg_iterator I = Func->arg_begin(); 1791 if (I == Func->arg_end()) 1792 return nullptr; 1793 return wrap(I); 1794 } 1795 1796 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) { 1797 Function *Func = unwrap<Function>(Fn); 1798 Function::arg_iterator I = Func->arg_end(); 1799 if (I == Func->arg_begin()) 1800 return nullptr; 1801 return wrap(--I); 1802 } 1803 1804 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) { 1805 Argument *A = unwrap<Argument>(Arg); 1806 Function::arg_iterator I = A; 1807 if (++I == A->getParent()->arg_end()) 1808 return nullptr; 1809 return wrap(I); 1810 } 1811 1812 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { 1813 Argument *A = unwrap<Argument>(Arg); 1814 Function::arg_iterator I = A; 1815 if (I == A->getParent()->arg_begin()) 1816 return nullptr; 1817 return wrap(--I); 1818 } 1819 1820 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 1821 Argument *A = unwrap<Argument>(Arg); 1822 AttrBuilder B(PA); 1823 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 1824 } 1825 1826 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) { 1827 Argument *A = unwrap<Argument>(Arg); 1828 AttrBuilder B(PA); 1829 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B)); 1830 } 1831 1832 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) { 1833 Argument *A = unwrap<Argument>(Arg); 1834 return (LLVMAttribute)A->getParent()->getAttributes(). 1835 Raw(A->getArgNo()+1); 1836 } 1837 1838 1839 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { 1840 Argument *A = unwrap<Argument>(Arg); 1841 AttrBuilder B; 1842 B.addAlignmentAttr(align); 1843 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B)); 1844 } 1845 1846 /*--.. Operations on basic blocks ..........................................--*/ 1847 1848 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { 1849 return wrap(static_cast<Value*>(unwrap(BB))); 1850 } 1851 1852 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) { 1853 return isa<BasicBlock>(unwrap(Val)); 1854 } 1855 1856 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) { 1857 return wrap(unwrap<BasicBlock>(Val)); 1858 } 1859 1860 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) { 1861 return wrap(unwrap(BB)->getParent()); 1862 } 1863 1864 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) { 1865 return wrap(unwrap(BB)->getTerminator()); 1866 } 1867 1868 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) { 1869 return unwrap<Function>(FnRef)->size(); 1870 } 1871 1872 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){ 1873 Function *Fn = unwrap<Function>(FnRef); 1874 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++) 1875 *BasicBlocksRefs++ = wrap(I); 1876 } 1877 1878 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) { 1879 return wrap(&unwrap<Function>(Fn)->getEntryBlock()); 1880 } 1881 1882 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) { 1883 Function *Func = unwrap<Function>(Fn); 1884 Function::iterator I = Func->begin(); 1885 if (I == Func->end()) 1886 return nullptr; 1887 return wrap(I); 1888 } 1889 1890 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) { 1891 Function *Func = unwrap<Function>(Fn); 1892 Function::iterator I = Func->end(); 1893 if (I == Func->begin()) 1894 return nullptr; 1895 return wrap(--I); 1896 } 1897 1898 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) { 1899 BasicBlock *Block = unwrap(BB); 1900 Function::iterator I = Block; 1901 if (++I == Block->getParent()->end()) 1902 return nullptr; 1903 return wrap(I); 1904 } 1905 1906 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) { 1907 BasicBlock *Block = unwrap(BB); 1908 Function::iterator I = Block; 1909 if (I == Block->getParent()->begin()) 1910 return nullptr; 1911 return wrap(--I); 1912 } 1913 1914 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C, 1915 LLVMValueRef FnRef, 1916 const char *Name) { 1917 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef))); 1918 } 1919 1920 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) { 1921 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name); 1922 } 1923 1924 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C, 1925 LLVMBasicBlockRef BBRef, 1926 const char *Name) { 1927 BasicBlock *BB = unwrap(BBRef); 1928 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB)); 1929 } 1930 1931 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef, 1932 const char *Name) { 1933 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name); 1934 } 1935 1936 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) { 1937 unwrap(BBRef)->eraseFromParent(); 1938 } 1939 1940 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) { 1941 unwrap(BBRef)->removeFromParent(); 1942 } 1943 1944 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 1945 unwrap(BB)->moveBefore(unwrap(MovePos)); 1946 } 1947 1948 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) { 1949 unwrap(BB)->moveAfter(unwrap(MovePos)); 1950 } 1951 1952 /*--.. Operations on instructions ..........................................--*/ 1953 1954 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) { 1955 return wrap(unwrap<Instruction>(Inst)->getParent()); 1956 } 1957 1958 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) { 1959 BasicBlock *Block = unwrap(BB); 1960 BasicBlock::iterator I = Block->begin(); 1961 if (I == Block->end()) 1962 return nullptr; 1963 return wrap(I); 1964 } 1965 1966 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) { 1967 BasicBlock *Block = unwrap(BB); 1968 BasicBlock::iterator I = Block->end(); 1969 if (I == Block->begin()) 1970 return nullptr; 1971 return wrap(--I); 1972 } 1973 1974 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) { 1975 Instruction *Instr = unwrap<Instruction>(Inst); 1976 BasicBlock::iterator I = Instr; 1977 if (++I == Instr->getParent()->end()) 1978 return nullptr; 1979 return wrap(I); 1980 } 1981 1982 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) { 1983 Instruction *Instr = unwrap<Instruction>(Inst); 1984 BasicBlock::iterator I = Instr; 1985 if (I == Instr->getParent()->begin()) 1986 return nullptr; 1987 return wrap(--I); 1988 } 1989 1990 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) { 1991 unwrap<Instruction>(Inst)->eraseFromParent(); 1992 } 1993 1994 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { 1995 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst))) 1996 return (LLVMIntPredicate)I->getPredicate(); 1997 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 1998 if (CE->getOpcode() == Instruction::ICmp) 1999 return (LLVMIntPredicate)CE->getPredicate(); 2000 return (LLVMIntPredicate)0; 2001 } 2002 2003 LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) { 2004 if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst))) 2005 return (LLVMRealPredicate)I->getPredicate(); 2006 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst))) 2007 if (CE->getOpcode() == Instruction::FCmp) 2008 return (LLVMRealPredicate)CE->getPredicate(); 2009 return (LLVMRealPredicate)0; 2010 } 2011 2012 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) { 2013 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2014 return map_to_llvmopcode(C->getOpcode()); 2015 return (LLVMOpcode)0; 2016 } 2017 2018 LLVMValueRef LLVMInstructionClone(LLVMValueRef Inst) { 2019 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst))) 2020 return wrap(C->clone()); 2021 return nullptr; 2022 } 2023 2024 /*--.. Call and invoke instructions ........................................--*/ 2025 2026 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) { 2027 Value *V = unwrap(Instr); 2028 if (CallInst *CI = dyn_cast<CallInst>(V)) 2029 return CI->getCallingConv(); 2030 if (InvokeInst *II = dyn_cast<InvokeInst>(V)) 2031 return II->getCallingConv(); 2032 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!"); 2033 } 2034 2035 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { 2036 Value *V = unwrap(Instr); 2037 if (CallInst *CI = dyn_cast<CallInst>(V)) 2038 return CI->setCallingConv(static_cast<CallingConv::ID>(CC)); 2039 else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) 2040 return II->setCallingConv(static_cast<CallingConv::ID>(CC)); 2041 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!"); 2042 } 2043 2044 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, 2045 LLVMAttribute PA) { 2046 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2047 AttrBuilder B(PA); 2048 Call.setAttributes( 2049 Call.getAttributes().addAttributes(Call->getContext(), index, 2050 AttributeSet::get(Call->getContext(), 2051 index, B))); 2052 } 2053 2054 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, 2055 LLVMAttribute PA) { 2056 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2057 AttrBuilder B(PA); 2058 Call.setAttributes(Call.getAttributes() 2059 .removeAttributes(Call->getContext(), index, 2060 AttributeSet::get(Call->getContext(), 2061 index, B))); 2062 } 2063 2064 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, 2065 unsigned align) { 2066 CallSite Call = CallSite(unwrap<Instruction>(Instr)); 2067 AttrBuilder B; 2068 B.addAlignmentAttr(align); 2069 Call.setAttributes(Call.getAttributes() 2070 .addAttributes(Call->getContext(), index, 2071 AttributeSet::get(Call->getContext(), 2072 index, B))); 2073 } 2074 2075 /*--.. Operations on call instructions (only) ..............................--*/ 2076 2077 LLVMBool LLVMIsTailCall(LLVMValueRef Call) { 2078 return unwrap<CallInst>(Call)->isTailCall(); 2079 } 2080 2081 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) { 2082 unwrap<CallInst>(Call)->setTailCall(isTailCall); 2083 } 2084 2085 /*--.. Operations on terminators ...........................................--*/ 2086 2087 unsigned LLVMGetNumSuccessors(LLVMValueRef Term) { 2088 return unwrap<TerminatorInst>(Term)->getNumSuccessors(); 2089 } 2090 2091 LLVMBasicBlockRef LLVMGetSuccessor(LLVMValueRef Term, unsigned i) { 2092 return wrap(unwrap<TerminatorInst>(Term)->getSuccessor(i)); 2093 } 2094 2095 void LLVMSetSuccessor(LLVMValueRef Term, unsigned i, LLVMBasicBlockRef block) { 2096 return unwrap<TerminatorInst>(Term)->setSuccessor(i,unwrap(block)); 2097 } 2098 2099 /*--.. Operations on branch instructions (only) ............................--*/ 2100 2101 LLVMBool LLVMIsConditional(LLVMValueRef Branch) { 2102 return unwrap<BranchInst>(Branch)->isConditional(); 2103 } 2104 2105 LLVMValueRef LLVMGetCondition(LLVMValueRef Branch) { 2106 return wrap(unwrap<BranchInst>(Branch)->getCondition()); 2107 } 2108 2109 void LLVMSetCondition(LLVMValueRef Branch, LLVMValueRef Cond) { 2110 return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond)); 2111 } 2112 2113 /*--.. Operations on switch instructions (only) ............................--*/ 2114 2115 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) { 2116 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest()); 2117 } 2118 2119 /*--.. Operations on phi nodes .............................................--*/ 2120 2121 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, 2122 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) { 2123 PHINode *PhiVal = unwrap<PHINode>(PhiNode); 2124 for (unsigned I = 0; I != Count; ++I) 2125 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I])); 2126 } 2127 2128 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) { 2129 return unwrap<PHINode>(PhiNode)->getNumIncomingValues(); 2130 } 2131 2132 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) { 2133 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index)); 2134 } 2135 2136 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) { 2137 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index)); 2138 } 2139 2140 2141 /*===-- Instruction builders ----------------------------------------------===*/ 2142 2143 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) { 2144 return wrap(new IRBuilder<>(*unwrap(C))); 2145 } 2146 2147 LLVMBuilderRef LLVMCreateBuilder(void) { 2148 return LLVMCreateBuilderInContext(LLVMGetGlobalContext()); 2149 } 2150 2151 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block, 2152 LLVMValueRef Instr) { 2153 BasicBlock *BB = unwrap(Block); 2154 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end(); 2155 unwrap(Builder)->SetInsertPoint(BB, I); 2156 } 2157 2158 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2159 Instruction *I = unwrap<Instruction>(Instr); 2160 unwrap(Builder)->SetInsertPoint(I->getParent(), I); 2161 } 2162 2163 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) { 2164 BasicBlock *BB = unwrap(Block); 2165 unwrap(Builder)->SetInsertPoint(BB); 2166 } 2167 2168 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) { 2169 return wrap(unwrap(Builder)->GetInsertBlock()); 2170 } 2171 2172 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) { 2173 unwrap(Builder)->ClearInsertionPoint(); 2174 } 2175 2176 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) { 2177 unwrap(Builder)->Insert(unwrap<Instruction>(Instr)); 2178 } 2179 2180 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr, 2181 const char *Name) { 2182 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name); 2183 } 2184 2185 void LLVMDisposeBuilder(LLVMBuilderRef Builder) { 2186 delete unwrap(Builder); 2187 } 2188 2189 /*--.. Metadata builders ...................................................--*/ 2190 2191 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) { 2192 MDNode *Loc = 2193 L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr; 2194 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc)); 2195 } 2196 2197 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) { 2198 LLVMContext &Context = unwrap(Builder)->getContext(); 2199 return wrap(MetadataAsValue::get( 2200 Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode())); 2201 } 2202 2203 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) { 2204 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst)); 2205 } 2206 2207 2208 /*--.. Instruction builders ................................................--*/ 2209 2210 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) { 2211 return wrap(unwrap(B)->CreateRetVoid()); 2212 } 2213 2214 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) { 2215 return wrap(unwrap(B)->CreateRet(unwrap(V))); 2216 } 2217 2218 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals, 2219 unsigned N) { 2220 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N)); 2221 } 2222 2223 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) { 2224 return wrap(unwrap(B)->CreateBr(unwrap(Dest))); 2225 } 2226 2227 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If, 2228 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) { 2229 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else))); 2230 } 2231 2232 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V, 2233 LLVMBasicBlockRef Else, unsigned NumCases) { 2234 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases)); 2235 } 2236 2237 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr, 2238 unsigned NumDests) { 2239 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests)); 2240 } 2241 2242 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn, 2243 LLVMValueRef *Args, unsigned NumArgs, 2244 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, 2245 const char *Name) { 2246 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch), 2247 makeArrayRef(unwrap(Args), NumArgs), 2248 Name)); 2249 } 2250 2251 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, 2252 unsigned NumClauses, const char *Name) { 2253 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name)); 2254 } 2255 2256 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) { 2257 return wrap(unwrap(B)->CreateResume(unwrap(Exn))); 2258 } 2259 2260 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) { 2261 return wrap(unwrap(B)->CreateUnreachable()); 2262 } 2263 2264 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal, 2265 LLVMBasicBlockRef Dest) { 2266 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest)); 2267 } 2268 2269 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) { 2270 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest)); 2271 } 2272 2273 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) { 2274 unwrap<LandingPadInst>(LandingPad)-> 2275 addClause(cast<Constant>(unwrap(ClauseVal))); 2276 } 2277 2278 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) { 2279 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val); 2280 } 2281 2282 /*--.. Arithmetic ..........................................................--*/ 2283 2284 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2285 const char *Name) { 2286 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name)); 2287 } 2288 2289 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2290 const char *Name) { 2291 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name)); 2292 } 2293 2294 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2295 const char *Name) { 2296 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name)); 2297 } 2298 2299 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2300 const char *Name) { 2301 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name)); 2302 } 2303 2304 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2305 const char *Name) { 2306 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name)); 2307 } 2308 2309 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2310 const char *Name) { 2311 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name)); 2312 } 2313 2314 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2315 const char *Name) { 2316 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name)); 2317 } 2318 2319 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2320 const char *Name) { 2321 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name)); 2322 } 2323 2324 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2325 const char *Name) { 2326 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name)); 2327 } 2328 2329 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2330 const char *Name) { 2331 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name)); 2332 } 2333 2334 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2335 const char *Name) { 2336 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name)); 2337 } 2338 2339 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2340 const char *Name) { 2341 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name)); 2342 } 2343 2344 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2345 const char *Name) { 2346 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name)); 2347 } 2348 2349 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2350 const char *Name) { 2351 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name)); 2352 } 2353 2354 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS, 2355 LLVMValueRef RHS, const char *Name) { 2356 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name)); 2357 } 2358 2359 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2360 const char *Name) { 2361 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name)); 2362 } 2363 2364 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2365 const char *Name) { 2366 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name)); 2367 } 2368 2369 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2370 const char *Name) { 2371 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name)); 2372 } 2373 2374 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2375 const char *Name) { 2376 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name)); 2377 } 2378 2379 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2380 const char *Name) { 2381 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name)); 2382 } 2383 2384 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2385 const char *Name) { 2386 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name)); 2387 } 2388 2389 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2390 const char *Name) { 2391 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name)); 2392 } 2393 2394 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2395 const char *Name) { 2396 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name)); 2397 } 2398 2399 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2400 const char *Name) { 2401 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name)); 2402 } 2403 2404 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS, 2405 const char *Name) { 2406 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name)); 2407 } 2408 2409 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op, 2410 LLVMValueRef LHS, LLVMValueRef RHS, 2411 const char *Name) { 2412 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS), 2413 unwrap(RHS), Name)); 2414 } 2415 2416 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2417 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name)); 2418 } 2419 2420 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V, 2421 const char *Name) { 2422 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name)); 2423 } 2424 2425 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V, 2426 const char *Name) { 2427 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name)); 2428 } 2429 2430 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2431 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name)); 2432 } 2433 2434 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { 2435 return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); 2436 } 2437 2438 /*--.. Memory ..............................................................--*/ 2439 2440 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2441 const char *Name) { 2442 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2443 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2444 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2445 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2446 ITy, unwrap(Ty), AllocSize, 2447 nullptr, nullptr, ""); 2448 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2449 } 2450 2451 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty, 2452 LLVMValueRef Val, const char *Name) { 2453 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext()); 2454 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty)); 2455 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy); 2456 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(), 2457 ITy, unwrap(Ty), AllocSize, 2458 unwrap(Val), nullptr, ""); 2459 return wrap(unwrap(B)->Insert(Malloc, Twine(Name))); 2460 } 2461 2462 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2463 const char *Name) { 2464 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name)); 2465 } 2466 2467 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty, 2468 LLVMValueRef Val, const char *Name) { 2469 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name)); 2470 } 2471 2472 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) { 2473 return wrap(unwrap(B)->Insert( 2474 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock()))); 2475 } 2476 2477 2478 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal, 2479 const char *Name) { 2480 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name)); 2481 } 2482 2483 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val, 2484 LLVMValueRef PointerVal) { 2485 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal))); 2486 } 2487 2488 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) { 2489 switch (Ordering) { 2490 case LLVMAtomicOrderingNotAtomic: return NotAtomic; 2491 case LLVMAtomicOrderingUnordered: return Unordered; 2492 case LLVMAtomicOrderingMonotonic: return Monotonic; 2493 case LLVMAtomicOrderingAcquire: return Acquire; 2494 case LLVMAtomicOrderingRelease: return Release; 2495 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease; 2496 case LLVMAtomicOrderingSequentiallyConsistent: 2497 return SequentiallyConsistent; 2498 } 2499 2500 llvm_unreachable("Invalid LLVMAtomicOrdering value!"); 2501 } 2502 2503 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering, 2504 LLVMBool isSingleThread, const char *Name) { 2505 return wrap( 2506 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering), 2507 isSingleThread ? SingleThread : CrossThread, 2508 Name)); 2509 } 2510 2511 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2512 LLVMValueRef *Indices, unsigned NumIndices, 2513 const char *Name) { 2514 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2515 return wrap(unwrap(B)->CreateGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2516 } 2517 2518 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2519 LLVMValueRef *Indices, unsigned NumIndices, 2520 const char *Name) { 2521 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices); 2522 return wrap( 2523 unwrap(B)->CreateInBoundsGEP(nullptr, unwrap(Pointer), IdxList, Name)); 2524 } 2525 2526 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer, 2527 unsigned Idx, const char *Name) { 2528 return wrap(unwrap(B)->CreateStructGEP(nullptr, unwrap(Pointer), Idx, Name)); 2529 } 2530 2531 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str, 2532 const char *Name) { 2533 return wrap(unwrap(B)->CreateGlobalString(Str, Name)); 2534 } 2535 2536 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str, 2537 const char *Name) { 2538 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name)); 2539 } 2540 2541 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) { 2542 Value *P = unwrap<Value>(MemAccessInst); 2543 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2544 return LI->isVolatile(); 2545 return cast<StoreInst>(P)->isVolatile(); 2546 } 2547 2548 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) { 2549 Value *P = unwrap<Value>(MemAccessInst); 2550 if (LoadInst *LI = dyn_cast<LoadInst>(P)) 2551 return LI->setVolatile(isVolatile); 2552 return cast<StoreInst>(P)->setVolatile(isVolatile); 2553 } 2554 2555 /*--.. Casts ...............................................................--*/ 2556 2557 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2558 LLVMTypeRef DestTy, const char *Name) { 2559 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name)); 2560 } 2561 2562 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val, 2563 LLVMTypeRef DestTy, const char *Name) { 2564 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name)); 2565 } 2566 2567 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val, 2568 LLVMTypeRef DestTy, const char *Name) { 2569 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name)); 2570 } 2571 2572 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val, 2573 LLVMTypeRef DestTy, const char *Name) { 2574 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name)); 2575 } 2576 2577 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val, 2578 LLVMTypeRef DestTy, const char *Name) { 2579 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name)); 2580 } 2581 2582 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2583 LLVMTypeRef DestTy, const char *Name) { 2584 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name)); 2585 } 2586 2587 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val, 2588 LLVMTypeRef DestTy, const char *Name) { 2589 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name)); 2590 } 2591 2592 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val, 2593 LLVMTypeRef DestTy, const char *Name) { 2594 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name)); 2595 } 2596 2597 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val, 2598 LLVMTypeRef DestTy, const char *Name) { 2599 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name)); 2600 } 2601 2602 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val, 2603 LLVMTypeRef DestTy, const char *Name) { 2604 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name)); 2605 } 2606 2607 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val, 2608 LLVMTypeRef DestTy, const char *Name) { 2609 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name)); 2610 } 2611 2612 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2613 LLVMTypeRef DestTy, const char *Name) { 2614 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name)); 2615 } 2616 2617 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val, 2618 LLVMTypeRef DestTy, const char *Name) { 2619 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name)); 2620 } 2621 2622 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2623 LLVMTypeRef DestTy, const char *Name) { 2624 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy), 2625 Name)); 2626 } 2627 2628 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2629 LLVMTypeRef DestTy, const char *Name) { 2630 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy), 2631 Name)); 2632 } 2633 2634 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val, 2635 LLVMTypeRef DestTy, const char *Name) { 2636 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy), 2637 Name)); 2638 } 2639 2640 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val, 2641 LLVMTypeRef DestTy, const char *Name) { 2642 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val), 2643 unwrap(DestTy), Name)); 2644 } 2645 2646 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val, 2647 LLVMTypeRef DestTy, const char *Name) { 2648 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name)); 2649 } 2650 2651 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val, 2652 LLVMTypeRef DestTy, const char *Name) { 2653 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy), 2654 /*isSigned*/true, Name)); 2655 } 2656 2657 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val, 2658 LLVMTypeRef DestTy, const char *Name) { 2659 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name)); 2660 } 2661 2662 /*--.. Comparisons .........................................................--*/ 2663 2664 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op, 2665 LLVMValueRef LHS, LLVMValueRef RHS, 2666 const char *Name) { 2667 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op), 2668 unwrap(LHS), unwrap(RHS), Name)); 2669 } 2670 2671 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, 2672 LLVMValueRef LHS, LLVMValueRef RHS, 2673 const char *Name) { 2674 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op), 2675 unwrap(LHS), unwrap(RHS), Name)); 2676 } 2677 2678 /*--.. Miscellaneous instructions ..........................................--*/ 2679 2680 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) { 2681 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name)); 2682 } 2683 2684 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, 2685 LLVMValueRef *Args, unsigned NumArgs, 2686 const char *Name) { 2687 return wrap(unwrap(B)->CreateCall(unwrap(Fn), 2688 makeArrayRef(unwrap(Args), NumArgs), 2689 Name)); 2690 } 2691 2692 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, 2693 LLVMValueRef Then, LLVMValueRef Else, 2694 const char *Name) { 2695 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else), 2696 Name)); 2697 } 2698 2699 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List, 2700 LLVMTypeRef Ty, const char *Name) { 2701 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name)); 2702 } 2703 2704 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal, 2705 LLVMValueRef Index, const char *Name) { 2706 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index), 2707 Name)); 2708 } 2709 2710 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal, 2711 LLVMValueRef EltVal, LLVMValueRef Index, 2712 const char *Name) { 2713 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal), 2714 unwrap(Index), Name)); 2715 } 2716 2717 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1, 2718 LLVMValueRef V2, LLVMValueRef Mask, 2719 const char *Name) { 2720 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2), 2721 unwrap(Mask), Name)); 2722 } 2723 2724 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal, 2725 unsigned Index, const char *Name) { 2726 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name)); 2727 } 2728 2729 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal, 2730 LLVMValueRef EltVal, unsigned Index, 2731 const char *Name) { 2732 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal), 2733 Index, Name)); 2734 } 2735 2736 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, 2737 const char *Name) { 2738 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); 2739 } 2740 2741 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val, 2742 const char *Name) { 2743 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name)); 2744 } 2745 2746 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS, 2747 LLVMValueRef RHS, const char *Name) { 2748 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name)); 2749 } 2750 2751 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op, 2752 LLVMValueRef PTR, LLVMValueRef Val, 2753 LLVMAtomicOrdering ordering, 2754 LLVMBool singleThread) { 2755 AtomicRMWInst::BinOp intop; 2756 switch (op) { 2757 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break; 2758 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break; 2759 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break; 2760 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break; 2761 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break; 2762 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break; 2763 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break; 2764 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break; 2765 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break; 2766 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break; 2767 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break; 2768 } 2769 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val), 2770 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread)); 2771 } 2772 2773 2774 /*===-- Module providers --------------------------------------------------===*/ 2775 2776 LLVMModuleProviderRef 2777 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) { 2778 return reinterpret_cast<LLVMModuleProviderRef>(M); 2779 } 2780 2781 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) { 2782 delete unwrap(MP); 2783 } 2784 2785 2786 /*===-- Memory buffers ----------------------------------------------------===*/ 2787 2788 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile( 2789 const char *Path, 2790 LLVMMemoryBufferRef *OutMemBuf, 2791 char **OutMessage) { 2792 2793 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(Path); 2794 if (std::error_code EC = MBOrErr.getError()) { 2795 *OutMessage = strdup(EC.message().c_str()); 2796 return 1; 2797 } 2798 *OutMemBuf = wrap(MBOrErr.get().release()); 2799 return 0; 2800 } 2801 2802 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf, 2803 char **OutMessage) { 2804 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getSTDIN(); 2805 if (std::error_code EC = MBOrErr.getError()) { 2806 *OutMessage = strdup(EC.message().c_str()); 2807 return 1; 2808 } 2809 *OutMemBuf = wrap(MBOrErr.get().release()); 2810 return 0; 2811 } 2812 2813 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange( 2814 const char *InputData, 2815 size_t InputDataLength, 2816 const char *BufferName, 2817 LLVMBool RequiresNullTerminator) { 2818 2819 return wrap(MemoryBuffer::getMemBuffer(StringRef(InputData, InputDataLength), 2820 StringRef(BufferName), 2821 RequiresNullTerminator).release()); 2822 } 2823 2824 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy( 2825 const char *InputData, 2826 size_t InputDataLength, 2827 const char *BufferName) { 2828 2829 return wrap( 2830 MemoryBuffer::getMemBufferCopy(StringRef(InputData, InputDataLength), 2831 StringRef(BufferName)).release()); 2832 } 2833 2834 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) { 2835 return unwrap(MemBuf)->getBufferStart(); 2836 } 2837 2838 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) { 2839 return unwrap(MemBuf)->getBufferSize(); 2840 } 2841 2842 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) { 2843 delete unwrap(MemBuf); 2844 } 2845 2846 /*===-- Pass Registry -----------------------------------------------------===*/ 2847 2848 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) { 2849 return wrap(PassRegistry::getPassRegistry()); 2850 } 2851 2852 /*===-- Pass Manager ------------------------------------------------------===*/ 2853 2854 LLVMPassManagerRef LLVMCreatePassManager() { 2855 return wrap(new legacy::PassManager()); 2856 } 2857 2858 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) { 2859 return wrap(new legacy::FunctionPassManager(unwrap(M))); 2860 } 2861 2862 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) { 2863 return LLVMCreateFunctionPassManagerForModule( 2864 reinterpret_cast<LLVMModuleRef>(P)); 2865 } 2866 2867 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) { 2868 return unwrap<legacy::PassManager>(PM)->run(*unwrap(M)); 2869 } 2870 2871 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) { 2872 return unwrap<legacy::FunctionPassManager>(FPM)->doInitialization(); 2873 } 2874 2875 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) { 2876 return unwrap<legacy::FunctionPassManager>(FPM)->run(*unwrap<Function>(F)); 2877 } 2878 2879 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) { 2880 return unwrap<legacy::FunctionPassManager>(FPM)->doFinalization(); 2881 } 2882 2883 void LLVMDisposePassManager(LLVMPassManagerRef PM) { 2884 delete unwrap(PM); 2885 } 2886 2887 /*===-- Threading ------------------------------------------------------===*/ 2888 2889 LLVMBool LLVMStartMultithreaded() { 2890 return LLVMIsMultithreaded(); 2891 } 2892 2893 void LLVMStopMultithreaded() { 2894 } 2895 2896 LLVMBool LLVMIsMultithreaded() { 2897 return llvm_is_multithreaded(); 2898 } 2899