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