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