1 //===-- Core.cpp ----------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the common infrastructure (including the C bindings)
11 // for libLLVMCore.a, which implements the LLVM intermediate representation.
13 //===----------------------------------------------------------------------===//
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/Module.h"
30 #include "llvm/PassManager.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"
41 #include <system_error>
45 #define DEBUG_TYPE "ir"
47 void llvm::initializeCore(PassRegistry &Registry) {
48 initializeDominatorTreeWrapperPassPass(Registry);
49 initializePrintModulePassWrapperPass(Registry);
50 initializePrintFunctionPassWrapperPass(Registry);
51 initializePrintBasicBlockPassPass(Registry);
52 initializeVerifierLegacyPassPass(Registry);
55 void LLVMInitializeCore(LLVMPassRegistryRef R) {
56 initializeCore(*unwrap(R));
63 /*===-- Error handling ----------------------------------------------------===*/
65 char *LLVMCreateMessage(const char *Message) {
66 return strdup(Message);
69 void LLVMDisposeMessage(char *Message) {
74 /*===-- Operations on contexts --------------------------------------------===*/
76 LLVMContextRef LLVMContextCreate() {
77 return wrap(new LLVMContext());
80 LLVMContextRef LLVMGetGlobalContext() {
81 return wrap(&getGlobalContext());
84 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
85 LLVMDiagnosticHandler Handler,
86 void *DiagnosticContext) {
87 unwrap(C)->setDiagnosticHandler(
88 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(Handler),
92 void LLVMContextSetYieldCallback(LLVMContextRef C, LLVMYieldCallback Callback,
95 LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
96 unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
99 void LLVMContextDispose(LLVMContextRef C) {
103 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
105 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
108 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
109 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
112 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
113 std::string MsgStorage;
114 raw_string_ostream Stream(MsgStorage);
115 DiagnosticPrinterRawOStream DP(Stream);
117 unwrap(DI)->print(DP);
120 return LLVMCreateMessage(MsgStorage.c_str());
123 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
124 LLVMDiagnosticSeverity severity;
126 switch(unwrap(DI)->getSeverity()) {
128 severity = LLVMDSError;
131 severity = LLVMDSWarning;
134 severity = LLVMDSRemark;
137 severity = LLVMDSNote;
147 /*===-- Operations on modules ---------------------------------------------===*/
149 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
150 return wrap(new Module(ModuleID, getGlobalContext()));
153 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
155 return wrap(new Module(ModuleID, *unwrap(C)));
158 void LLVMDisposeModule(LLVMModuleRef M) {
162 /*--.. Data layout .........................................................--*/
163 const char * LLVMGetDataLayout(LLVMModuleRef M) {
164 return unwrap(M)->getDataLayoutStr().c_str();
167 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
168 unwrap(M)->setDataLayout(Triple);
171 /*--.. Target triple .......................................................--*/
172 const char * LLVMGetTarget(LLVMModuleRef M) {
173 return unwrap(M)->getTargetTriple().c_str();
176 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
177 unwrap(M)->setTargetTriple(Triple);
180 void LLVMDumpModule(LLVMModuleRef M) {
184 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
185 char **ErrorMessage) {
187 raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
188 if (!error.empty()) {
189 *ErrorMessage = strdup(error.c_str());
193 unwrap(M)->print(dest, nullptr);
195 if (!error.empty()) {
196 *ErrorMessage = strdup(error.c_str());
203 char *LLVMPrintModuleToString(LLVMModuleRef M) {
205 raw_string_ostream os(buf);
207 unwrap(M)->print(os, nullptr);
210 return strdup(buf.c_str());
213 /*--.. Operations on inline assembler ......................................--*/
214 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
215 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
219 /*--.. Operations on module contexts ......................................--*/
220 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
221 return wrap(&unwrap(M)->getContext());
225 /*===-- Operations on types -----------------------------------------------===*/
227 /*--.. Operations on all types (mostly) ....................................--*/
229 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
230 switch (unwrap(Ty)->getTypeID()) {
232 return LLVMVoidTypeKind;
234 return LLVMHalfTypeKind;
235 case Type::FloatTyID:
236 return LLVMFloatTypeKind;
237 case Type::DoubleTyID:
238 return LLVMDoubleTypeKind;
239 case Type::X86_FP80TyID:
240 return LLVMX86_FP80TypeKind;
241 case Type::FP128TyID:
242 return LLVMFP128TypeKind;
243 case Type::PPC_FP128TyID:
244 return LLVMPPC_FP128TypeKind;
245 case Type::LabelTyID:
246 return LLVMLabelTypeKind;
247 case Type::MetadataTyID:
248 return LLVMMetadataTypeKind;
249 case Type::IntegerTyID:
250 return LLVMIntegerTypeKind;
251 case Type::FunctionTyID:
252 return LLVMFunctionTypeKind;
253 case Type::StructTyID:
254 return LLVMStructTypeKind;
255 case Type::ArrayTyID:
256 return LLVMArrayTypeKind;
257 case Type::PointerTyID:
258 return LLVMPointerTypeKind;
259 case Type::VectorTyID:
260 return LLVMVectorTypeKind;
261 case Type::X86_MMXTyID:
262 return LLVMX86_MMXTypeKind;
264 llvm_unreachable("Unhandled TypeID.");
267 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
269 return unwrap(Ty)->isSized();
272 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
273 return wrap(&unwrap(Ty)->getContext());
276 void LLVMDumpType(LLVMTypeRef Ty) {
277 return unwrap(Ty)->dump();
280 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
282 raw_string_ostream os(buf);
284 assert(unwrap(Ty) != nullptr && "Expecting non-null Type");
285 unwrap(Ty)->print(os);
288 return strdup(buf.c_str());
291 /*--.. Operations on integer types .........................................--*/
293 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
294 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
296 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
297 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
299 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
300 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
302 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
303 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
305 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
306 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
308 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
309 return wrap(IntegerType::get(*unwrap(C), NumBits));
312 LLVMTypeRef LLVMInt1Type(void) {
313 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
315 LLVMTypeRef LLVMInt8Type(void) {
316 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
318 LLVMTypeRef LLVMInt16Type(void) {
319 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
321 LLVMTypeRef LLVMInt32Type(void) {
322 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
324 LLVMTypeRef LLVMInt64Type(void) {
325 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
327 LLVMTypeRef LLVMIntType(unsigned NumBits) {
328 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
331 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
332 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
335 /*--.. Operations on real types ............................................--*/
337 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
338 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
340 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
341 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
343 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
344 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
346 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
347 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
349 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
350 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
352 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
353 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
355 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
356 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
359 LLVMTypeRef LLVMHalfType(void) {
360 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
362 LLVMTypeRef LLVMFloatType(void) {
363 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
365 LLVMTypeRef LLVMDoubleType(void) {
366 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
368 LLVMTypeRef LLVMX86FP80Type(void) {
369 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
371 LLVMTypeRef LLVMFP128Type(void) {
372 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
374 LLVMTypeRef LLVMPPCFP128Type(void) {
375 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
377 LLVMTypeRef LLVMX86MMXType(void) {
378 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
381 /*--.. Operations on function types ........................................--*/
383 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
384 LLVMTypeRef *ParamTypes, unsigned ParamCount,
386 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
387 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
390 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
391 return unwrap<FunctionType>(FunctionTy)->isVarArg();
394 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
395 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
398 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
399 return unwrap<FunctionType>(FunctionTy)->getNumParams();
402 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
403 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
404 for (FunctionType::param_iterator I = Ty->param_begin(),
405 E = Ty->param_end(); I != E; ++I)
409 /*--.. Operations on struct types ..........................................--*/
411 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
412 unsigned ElementCount, LLVMBool Packed) {
413 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
414 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
417 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
418 unsigned ElementCount, LLVMBool Packed) {
419 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
420 ElementCount, Packed);
423 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
425 return wrap(StructType::create(*unwrap(C), Name));
428 const char *LLVMGetStructName(LLVMTypeRef Ty)
430 StructType *Type = unwrap<StructType>(Ty);
431 if (!Type->hasName())
433 return Type->getName().data();
436 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
437 unsigned ElementCount, LLVMBool Packed) {
438 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
439 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
442 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
443 return unwrap<StructType>(StructTy)->getNumElements();
446 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
447 StructType *Ty = unwrap<StructType>(StructTy);
448 for (StructType::element_iterator I = Ty->element_begin(),
449 E = Ty->element_end(); I != E; ++I)
453 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
454 return unwrap<StructType>(StructTy)->isPacked();
457 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
458 return unwrap<StructType>(StructTy)->isOpaque();
461 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
462 return wrap(unwrap(M)->getTypeByName(Name));
465 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
467 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
468 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
471 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
472 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
475 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
476 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
479 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
480 return wrap(unwrap<SequentialType>(Ty)->getElementType());
483 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
484 return unwrap<ArrayType>(ArrayTy)->getNumElements();
487 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
488 return unwrap<PointerType>(PointerTy)->getAddressSpace();
491 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
492 return unwrap<VectorType>(VectorTy)->getNumElements();
495 /*--.. Operations on other types ...........................................--*/
497 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
498 return wrap(Type::getVoidTy(*unwrap(C)));
500 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
501 return wrap(Type::getLabelTy(*unwrap(C)));
504 LLVMTypeRef LLVMVoidType(void) {
505 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
507 LLVMTypeRef LLVMLabelType(void) {
508 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
511 /*===-- Operations on values ----------------------------------------------===*/
513 /*--.. Operations on all values ............................................--*/
515 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
516 return wrap(unwrap(Val)->getType());
519 const char *LLVMGetValueName(LLVMValueRef Val) {
520 return unwrap(Val)->getName().data();
523 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
524 unwrap(Val)->setName(Name);
527 void LLVMDumpValue(LLVMValueRef Val) {
531 char* LLVMPrintValueToString(LLVMValueRef Val) {
533 raw_string_ostream os(buf);
535 assert(unwrap(Val) != nullptr && "Expecting non-null Value");
536 unwrap(Val)->print(os);
539 return strdup(buf.c_str());
542 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
543 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
546 int LLVMHasMetadata(LLVMValueRef Inst) {
547 return unwrap<Instruction>(Inst)->hasMetadata();
550 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
551 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
554 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
555 unwrap<Instruction>(Inst)->setMetadata(KindID,
556 MD ? unwrap<MDNode>(MD) : nullptr);
559 /*--.. Conversion functions ................................................--*/
561 #define LLVM_DEFINE_VALUE_CAST(name) \
562 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
563 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
566 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
568 /*--.. Operations on Uses ..................................................--*/
569 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
570 Value *V = unwrap(Val);
571 Value::use_iterator I = V->use_begin();
572 if (I == V->use_end())
577 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
578 Use *Next = unwrap(U)->getNext();
584 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
585 return wrap(unwrap(U)->getUser());
588 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
589 return wrap(unwrap(U)->get());
592 /*--.. Operations on Users .................................................--*/
593 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
594 Value *V = unwrap(Val);
595 if (MDNode *MD = dyn_cast<MDNode>(V))
596 return wrap(MD->getOperand(Index));
597 return wrap(cast<User>(V)->getOperand(Index));
600 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
601 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
604 int LLVMGetNumOperands(LLVMValueRef Val) {
605 Value *V = unwrap(Val);
606 if (MDNode *MD = dyn_cast<MDNode>(V))
607 return MD->getNumOperands();
608 return cast<User>(V)->getNumOperands();
611 /*--.. Operations on constants of any type .................................--*/
613 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
614 return wrap(Constant::getNullValue(unwrap(Ty)));
617 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
618 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
621 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
622 return wrap(UndefValue::get(unwrap(Ty)));
625 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
626 return isa<Constant>(unwrap(Ty));
629 LLVMBool LLVMIsNull(LLVMValueRef Val) {
630 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
631 return C->isNullValue();
635 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
636 return isa<UndefValue>(unwrap(Val));
639 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
641 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
644 /*--.. Operations on metadata nodes ........................................--*/
646 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
648 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
651 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
652 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
655 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
657 return wrap(MDNode::get(*unwrap(C),
658 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
661 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
662 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
665 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
666 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
667 *Len = S->getString().size();
668 return S->getString().data();
674 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
676 return cast<MDNode>(unwrap(V))->getNumOperands();
679 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
681 const MDNode *N = cast<MDNode>(unwrap(V));
682 const unsigned numOperands = N->getNumOperands();
683 for (unsigned i = 0; i < numOperands; i++)
684 Dest[i] = wrap(N->getOperand(i));
687 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
689 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
690 return N->getNumOperands();
695 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
697 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
700 for (unsigned i=0;i<N->getNumOperands();i++)
701 Dest[i] = wrap(N->getOperand(i));
704 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
707 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
710 MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
715 /*--.. Operations on scalar constants ......................................--*/
717 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
718 LLVMBool SignExtend) {
719 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
722 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
724 const uint64_t Words[]) {
725 IntegerType *Ty = unwrap<IntegerType>(IntTy);
726 return wrap(ConstantInt::get(Ty->getContext(),
727 APInt(Ty->getBitWidth(),
728 makeArrayRef(Words, NumWords))));
731 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
733 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
737 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
738 unsigned SLen, uint8_t Radix) {
739 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
743 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
744 return wrap(ConstantFP::get(unwrap(RealTy), N));
747 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
748 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
751 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
753 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
756 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
757 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
760 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
761 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
764 /*--.. Operations on composite constants ...................................--*/
766 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
768 LLVMBool DontNullTerminate) {
769 /* Inverted the sense of AddNull because ', 0)' is a
770 better mnemonic for null termination than ', 1)'. */
771 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
772 DontNullTerminate == 0));
774 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
775 LLVMValueRef *ConstantVals,
776 unsigned Count, LLVMBool Packed) {
777 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
778 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
782 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
783 LLVMBool DontNullTerminate) {
784 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
787 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
788 LLVMValueRef *ConstantVals, unsigned Length) {
789 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
790 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
792 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
794 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
798 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
799 LLVMValueRef *ConstantVals,
801 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
802 StructType *Ty = cast<StructType>(unwrap(StructTy));
804 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
807 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
808 return wrap(ConstantVector::get(makeArrayRef(
809 unwrap<Constant>(ScalarConstantVals, Size), Size)));
812 /*-- Opcode mapping */
814 static LLVMOpcode map_to_llvmopcode(int opcode)
817 default: llvm_unreachable("Unhandled Opcode.");
818 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
819 #include "llvm/IR/Instruction.def"
824 static int map_from_llvmopcode(LLVMOpcode code)
827 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
828 #include "llvm/IR/Instruction.def"
831 llvm_unreachable("Unhandled Opcode.");
834 /*--.. Constant expressions ................................................--*/
836 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
837 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
840 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
841 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
844 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
845 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
848 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
849 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
852 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
853 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
856 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
857 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
861 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
862 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
865 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
866 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
869 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
870 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
871 unwrap<Constant>(RHSConstant)));
874 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
875 LLVMValueRef RHSConstant) {
876 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
877 unwrap<Constant>(RHSConstant)));
880 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
881 LLVMValueRef RHSConstant) {
882 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
883 unwrap<Constant>(RHSConstant)));
886 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
887 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
888 unwrap<Constant>(RHSConstant)));
891 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
892 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
893 unwrap<Constant>(RHSConstant)));
896 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
897 LLVMValueRef RHSConstant) {
898 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
899 unwrap<Constant>(RHSConstant)));
902 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
903 LLVMValueRef RHSConstant) {
904 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
905 unwrap<Constant>(RHSConstant)));
908 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
909 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
910 unwrap<Constant>(RHSConstant)));
913 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
914 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
915 unwrap<Constant>(RHSConstant)));
918 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
919 LLVMValueRef RHSConstant) {
920 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
921 unwrap<Constant>(RHSConstant)));
924 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
925 LLVMValueRef RHSConstant) {
926 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
927 unwrap<Constant>(RHSConstant)));
930 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
931 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
932 unwrap<Constant>(RHSConstant)));
935 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
936 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
937 unwrap<Constant>(RHSConstant)));
940 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
941 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
942 unwrap<Constant>(RHSConstant)));
945 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
946 LLVMValueRef RHSConstant) {
947 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
948 unwrap<Constant>(RHSConstant)));
951 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
952 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
953 unwrap<Constant>(RHSConstant)));
956 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
957 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
958 unwrap<Constant>(RHSConstant)));
961 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
962 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
963 unwrap<Constant>(RHSConstant)));
966 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
967 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
968 unwrap<Constant>(RHSConstant)));
971 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
972 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
973 unwrap<Constant>(RHSConstant)));
976 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
977 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
978 unwrap<Constant>(RHSConstant)));
981 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
982 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
983 unwrap<Constant>(RHSConstant)));
986 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
987 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
988 return wrap(ConstantExpr::getICmp(Predicate,
989 unwrap<Constant>(LHSConstant),
990 unwrap<Constant>(RHSConstant)));
993 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
994 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
995 return wrap(ConstantExpr::getFCmp(Predicate,
996 unwrap<Constant>(LHSConstant),
997 unwrap<Constant>(RHSConstant)));
1000 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1001 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1002 unwrap<Constant>(RHSConstant)));
1005 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1006 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1007 unwrap<Constant>(RHSConstant)));
1010 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1011 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1012 unwrap<Constant>(RHSConstant)));
1015 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1016 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1017 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1019 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
1023 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1024 LLVMValueRef *ConstantIndices,
1025 unsigned NumIndices) {
1026 Constant* Val = unwrap<Constant>(ConstantVal);
1027 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1029 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
1032 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1033 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1037 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1038 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1042 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1043 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1047 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1048 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1052 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1053 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1057 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1058 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1062 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1063 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1067 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1068 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1072 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1073 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1077 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1078 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1082 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1083 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1087 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1088 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1092 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1093 LLVMTypeRef ToType) {
1094 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1098 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1099 LLVMTypeRef ToType) {
1100 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1104 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1105 LLVMTypeRef ToType) {
1106 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1110 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1111 LLVMTypeRef ToType) {
1112 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1116 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1117 LLVMTypeRef ToType) {
1118 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1122 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1123 LLVMBool isSigned) {
1124 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1125 unwrap(ToType), isSigned));
1128 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1129 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1133 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1134 LLVMValueRef ConstantIfTrue,
1135 LLVMValueRef ConstantIfFalse) {
1136 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1137 unwrap<Constant>(ConstantIfTrue),
1138 unwrap<Constant>(ConstantIfFalse)));
1141 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1142 LLVMValueRef IndexConstant) {
1143 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1144 unwrap<Constant>(IndexConstant)));
1147 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1148 LLVMValueRef ElementValueConstant,
1149 LLVMValueRef IndexConstant) {
1150 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1151 unwrap<Constant>(ElementValueConstant),
1152 unwrap<Constant>(IndexConstant)));
1155 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1156 LLVMValueRef VectorBConstant,
1157 LLVMValueRef MaskConstant) {
1158 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1159 unwrap<Constant>(VectorBConstant),
1160 unwrap<Constant>(MaskConstant)));
1163 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1165 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1166 makeArrayRef(IdxList, NumIdx)));
1169 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1170 LLVMValueRef ElementValueConstant,
1171 unsigned *IdxList, unsigned NumIdx) {
1172 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1173 unwrap<Constant>(ElementValueConstant),
1174 makeArrayRef(IdxList, NumIdx)));
1177 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1178 const char *Constraints,
1179 LLVMBool HasSideEffects,
1180 LLVMBool IsAlignStack) {
1181 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1182 Constraints, HasSideEffects, IsAlignStack));
1185 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1186 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1189 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1191 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1192 return wrap(unwrap<GlobalValue>(Global)->getParent());
1195 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1196 return unwrap<GlobalValue>(Global)->isDeclaration();
1199 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1200 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1201 case GlobalValue::ExternalLinkage:
1202 return LLVMExternalLinkage;
1203 case GlobalValue::AvailableExternallyLinkage:
1204 return LLVMAvailableExternallyLinkage;
1205 case GlobalValue::LinkOnceAnyLinkage:
1206 return LLVMLinkOnceAnyLinkage;
1207 case GlobalValue::LinkOnceODRLinkage:
1208 return LLVMLinkOnceODRLinkage;
1209 case GlobalValue::WeakAnyLinkage:
1210 return LLVMWeakAnyLinkage;
1211 case GlobalValue::WeakODRLinkage:
1212 return LLVMWeakODRLinkage;
1213 case GlobalValue::AppendingLinkage:
1214 return LLVMAppendingLinkage;
1215 case GlobalValue::InternalLinkage:
1216 return LLVMInternalLinkage;
1217 case GlobalValue::PrivateLinkage:
1218 return LLVMPrivateLinkage;
1219 case GlobalValue::ExternalWeakLinkage:
1220 return LLVMExternalWeakLinkage;
1221 case GlobalValue::CommonLinkage:
1222 return LLVMCommonLinkage;
1225 llvm_unreachable("Invalid GlobalValue linkage!");
1228 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1229 GlobalValue *GV = unwrap<GlobalValue>(Global);
1232 case LLVMExternalLinkage:
1233 GV->setLinkage(GlobalValue::ExternalLinkage);
1235 case LLVMAvailableExternallyLinkage:
1236 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1238 case LLVMLinkOnceAnyLinkage:
1239 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1241 case LLVMLinkOnceODRLinkage:
1242 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1244 case LLVMLinkOnceODRAutoHideLinkage:
1245 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1246 "longer supported.");
1248 case LLVMWeakAnyLinkage:
1249 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1251 case LLVMWeakODRLinkage:
1252 GV->setLinkage(GlobalValue::WeakODRLinkage);
1254 case LLVMAppendingLinkage:
1255 GV->setLinkage(GlobalValue::AppendingLinkage);
1257 case LLVMInternalLinkage:
1258 GV->setLinkage(GlobalValue::InternalLinkage);
1260 case LLVMPrivateLinkage:
1261 GV->setLinkage(GlobalValue::PrivateLinkage);
1263 case LLVMLinkerPrivateLinkage:
1264 GV->setLinkage(GlobalValue::PrivateLinkage);
1266 case LLVMLinkerPrivateWeakLinkage:
1267 GV->setLinkage(GlobalValue::PrivateLinkage);
1269 case LLVMDLLImportLinkage:
1271 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1273 case LLVMDLLExportLinkage:
1275 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1277 case LLVMExternalWeakLinkage:
1278 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1280 case LLVMGhostLinkage:
1282 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1284 case LLVMCommonLinkage:
1285 GV->setLinkage(GlobalValue::CommonLinkage);
1290 const char *LLVMGetSection(LLVMValueRef Global) {
1291 return unwrap<GlobalValue>(Global)->getSection();
1294 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1295 unwrap<GlobalObject>(Global)->setSection(Section);
1298 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1299 return static_cast<LLVMVisibility>(
1300 unwrap<GlobalValue>(Global)->getVisibility());
1303 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1304 unwrap<GlobalValue>(Global)
1305 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1308 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1309 return static_cast<LLVMDLLStorageClass>(
1310 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1313 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1314 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1315 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1318 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1319 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1322 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1323 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1326 /*--.. Operations on global variables, load and store instructions .........--*/
1328 unsigned LLVMGetAlignment(LLVMValueRef V) {
1329 Value *P = unwrap<Value>(V);
1330 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1331 return GV->getAlignment();
1332 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1333 return AI->getAlignment();
1334 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1335 return LI->getAlignment();
1336 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1337 return SI->getAlignment();
1340 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1343 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1344 Value *P = unwrap<Value>(V);
1345 if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
1346 GV->setAlignment(Bytes);
1347 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1348 AI->setAlignment(Bytes);
1349 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1350 LI->setAlignment(Bytes);
1351 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1352 SI->setAlignment(Bytes);
1355 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1358 /*--.. Operations on global variables ......................................--*/
1360 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1361 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1362 GlobalValue::ExternalLinkage, nullptr, Name));
1365 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1367 unsigned AddressSpace) {
1368 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1369 GlobalValue::ExternalLinkage, nullptr, Name,
1370 nullptr, GlobalVariable::NotThreadLocal,
1374 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1375 return wrap(unwrap(M)->getNamedGlobal(Name));
1378 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1379 Module *Mod = unwrap(M);
1380 Module::global_iterator I = Mod->global_begin();
1381 if (I == Mod->global_end())
1386 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1387 Module *Mod = unwrap(M);
1388 Module::global_iterator I = Mod->global_end();
1389 if (I == Mod->global_begin())
1394 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1395 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1396 Module::global_iterator I = GV;
1397 if (++I == GV->getParent()->global_end())
1402 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1403 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1404 Module::global_iterator I = GV;
1405 if (I == GV->getParent()->global_begin())
1410 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1411 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1414 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1415 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1416 if ( !GV->hasInitializer() )
1418 return wrap(GV->getInitializer());
1421 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1422 unwrap<GlobalVariable>(GlobalVar)
1423 ->setInitializer(unwrap<Constant>(ConstantVal));
1426 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1427 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1430 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1431 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1434 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1435 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1438 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1439 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1442 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1443 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1444 case GlobalVariable::NotThreadLocal:
1445 return LLVMNotThreadLocal;
1446 case GlobalVariable::GeneralDynamicTLSModel:
1447 return LLVMGeneralDynamicTLSModel;
1448 case GlobalVariable::LocalDynamicTLSModel:
1449 return LLVMLocalDynamicTLSModel;
1450 case GlobalVariable::InitialExecTLSModel:
1451 return LLVMInitialExecTLSModel;
1452 case GlobalVariable::LocalExecTLSModel:
1453 return LLVMLocalExecTLSModel;
1456 llvm_unreachable("Invalid GlobalVariable thread local mode");
1459 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1460 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1463 case LLVMNotThreadLocal:
1464 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1466 case LLVMGeneralDynamicTLSModel:
1467 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1469 case LLVMLocalDynamicTLSModel:
1470 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1472 case LLVMInitialExecTLSModel:
1473 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1475 case LLVMLocalExecTLSModel:
1476 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1481 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1482 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1485 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1486 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1489 /*--.. Operations on aliases ......................................--*/
1491 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1493 auto *PTy = cast<PointerType>(unwrap(Ty));
1494 return wrap(GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1495 GlobalValue::ExternalLinkage, Name,
1496 unwrap<GlobalObject>(Aliasee), unwrap(M)));
1499 /*--.. Operations on functions .............................................--*/
1501 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1502 LLVMTypeRef FunctionTy) {
1503 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1504 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1507 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1508 return wrap(unwrap(M)->getFunction(Name));
1511 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1512 Module *Mod = unwrap(M);
1513 Module::iterator I = Mod->begin();
1514 if (I == Mod->end())
1519 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1520 Module *Mod = unwrap(M);
1521 Module::iterator I = Mod->end();
1522 if (I == Mod->begin())
1527 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1528 Function *Func = unwrap<Function>(Fn);
1529 Module::iterator I = Func;
1530 if (++I == Func->getParent()->end())
1535 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1536 Function *Func = unwrap<Function>(Fn);
1537 Module::iterator I = Func;
1538 if (I == Func->getParent()->begin())
1543 void LLVMDeleteFunction(LLVMValueRef Fn) {
1544 unwrap<Function>(Fn)->eraseFromParent();
1547 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1548 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1549 return F->getIntrinsicID();
1553 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1554 return unwrap<Function>(Fn)->getCallingConv();
1557 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1558 return unwrap<Function>(Fn)->setCallingConv(
1559 static_cast<CallingConv::ID>(CC));
1562 const char *LLVMGetGC(LLVMValueRef Fn) {
1563 Function *F = unwrap<Function>(Fn);
1564 return F->hasGC()? F->getGC() : nullptr;
1567 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1568 Function *F = unwrap<Function>(Fn);
1575 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1576 Function *Func = unwrap<Function>(Fn);
1577 const AttributeSet PAL = Func->getAttributes();
1579 const AttributeSet PALnew =
1580 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1581 AttributeSet::get(Func->getContext(),
1582 AttributeSet::FunctionIndex, B));
1583 Func->setAttributes(PALnew);
1586 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1588 Function *Func = unwrap<Function>(Fn);
1589 AttributeSet::AttrIndex Idx =
1590 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1593 B.addAttribute(A, V);
1594 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1595 Func->addAttributes(Idx, Set);
1598 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1599 Function *Func = unwrap<Function>(Fn);
1600 const AttributeSet PAL = Func->getAttributes();
1602 const AttributeSet PALnew =
1603 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1604 AttributeSet::get(Func->getContext(),
1605 AttributeSet::FunctionIndex, B));
1606 Func->setAttributes(PALnew);
1609 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1610 Function *Func = unwrap<Function>(Fn);
1611 const AttributeSet PAL = Func->getAttributes();
1612 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1615 /*--.. Operations on parameters ............................................--*/
1617 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1618 // This function is strictly redundant to
1619 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1620 return unwrap<Function>(FnRef)->arg_size();
1623 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1624 Function *Fn = unwrap<Function>(FnRef);
1625 for (Function::arg_iterator I = Fn->arg_begin(),
1626 E = Fn->arg_end(); I != E; I++)
1627 *ParamRefs++ = wrap(I);
1630 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1631 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1637 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1638 return wrap(unwrap<Argument>(V)->getParent());
1641 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1642 Function *Func = unwrap<Function>(Fn);
1643 Function::arg_iterator I = Func->arg_begin();
1644 if (I == Func->arg_end())
1649 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1650 Function *Func = unwrap<Function>(Fn);
1651 Function::arg_iterator I = Func->arg_end();
1652 if (I == Func->arg_begin())
1657 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1658 Argument *A = unwrap<Argument>(Arg);
1659 Function::arg_iterator I = A;
1660 if (++I == A->getParent()->arg_end())
1665 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1666 Argument *A = unwrap<Argument>(Arg);
1667 Function::arg_iterator I = A;
1668 if (I == A->getParent()->arg_begin())
1673 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1674 Argument *A = unwrap<Argument>(Arg);
1676 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1679 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1680 Argument *A = unwrap<Argument>(Arg);
1682 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1685 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1686 Argument *A = unwrap<Argument>(Arg);
1687 return (LLVMAttribute)A->getParent()->getAttributes().
1688 Raw(A->getArgNo()+1);
1692 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1693 Argument *A = unwrap<Argument>(Arg);
1695 B.addAlignmentAttr(align);
1696 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1699 /*--.. Operations on basic blocks ..........................................--*/
1701 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1702 return wrap(static_cast<Value*>(unwrap(BB)));
1705 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1706 return isa<BasicBlock>(unwrap(Val));
1709 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1710 return wrap(unwrap<BasicBlock>(Val));
1713 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1714 return wrap(unwrap(BB)->getParent());
1717 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1718 return wrap(unwrap(BB)->getTerminator());
1721 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1722 return unwrap<Function>(FnRef)->size();
1725 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1726 Function *Fn = unwrap<Function>(FnRef);
1727 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1728 *BasicBlocksRefs++ = wrap(I);
1731 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1732 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1735 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1736 Function *Func = unwrap<Function>(Fn);
1737 Function::iterator I = Func->begin();
1738 if (I == Func->end())
1743 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1744 Function *Func = unwrap<Function>(Fn);
1745 Function::iterator I = Func->end();
1746 if (I == Func->begin())
1751 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1752 BasicBlock *Block = unwrap(BB);
1753 Function::iterator I = Block;
1754 if (++I == Block->getParent()->end())
1759 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1760 BasicBlock *Block = unwrap(BB);
1761 Function::iterator I = Block;
1762 if (I == Block->getParent()->begin())
1767 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1770 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1773 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1774 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1777 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1778 LLVMBasicBlockRef BBRef,
1780 BasicBlock *BB = unwrap(BBRef);
1781 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1784 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1786 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1789 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1790 unwrap(BBRef)->eraseFromParent();
1793 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1794 unwrap(BBRef)->removeFromParent();
1797 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1798 unwrap(BB)->moveBefore(unwrap(MovePos));
1801 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1802 unwrap(BB)->moveAfter(unwrap(MovePos));
1805 /*--.. Operations on instructions ..........................................--*/
1807 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1808 return wrap(unwrap<Instruction>(Inst)->getParent());
1811 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1812 BasicBlock *Block = unwrap(BB);
1813 BasicBlock::iterator I = Block->begin();
1814 if (I == Block->end())
1819 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1820 BasicBlock *Block = unwrap(BB);
1821 BasicBlock::iterator I = Block->end();
1822 if (I == Block->begin())
1827 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1828 Instruction *Instr = unwrap<Instruction>(Inst);
1829 BasicBlock::iterator I = Instr;
1830 if (++I == Instr->getParent()->end())
1835 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1836 Instruction *Instr = unwrap<Instruction>(Inst);
1837 BasicBlock::iterator I = Instr;
1838 if (I == Instr->getParent()->begin())
1843 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1844 unwrap<Instruction>(Inst)->eraseFromParent();
1847 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1848 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1849 return (LLVMIntPredicate)I->getPredicate();
1850 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1851 if (CE->getOpcode() == Instruction::ICmp)
1852 return (LLVMIntPredicate)CE->getPredicate();
1853 return (LLVMIntPredicate)0;
1856 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1857 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1858 return map_to_llvmopcode(C->getOpcode());
1859 return (LLVMOpcode)0;
1862 /*--.. Call and invoke instructions ........................................--*/
1864 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1865 Value *V = unwrap(Instr);
1866 if (CallInst *CI = dyn_cast<CallInst>(V))
1867 return CI->getCallingConv();
1868 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1869 return II->getCallingConv();
1870 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1873 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1874 Value *V = unwrap(Instr);
1875 if (CallInst *CI = dyn_cast<CallInst>(V))
1876 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1877 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1878 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1879 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1882 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1884 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1887 Call.getAttributes().addAttributes(Call->getContext(), index,
1888 AttributeSet::get(Call->getContext(),
1892 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1894 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1896 Call.setAttributes(Call.getAttributes()
1897 .removeAttributes(Call->getContext(), index,
1898 AttributeSet::get(Call->getContext(),
1902 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1904 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1906 B.addAlignmentAttr(align);
1907 Call.setAttributes(Call.getAttributes()
1908 .addAttributes(Call->getContext(), index,
1909 AttributeSet::get(Call->getContext(),
1913 /*--.. Operations on call instructions (only) ..............................--*/
1915 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1916 return unwrap<CallInst>(Call)->isTailCall();
1919 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1920 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1923 /*--.. Operations on switch instructions (only) ............................--*/
1925 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1926 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1929 /*--.. Operations on phi nodes .............................................--*/
1931 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1932 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1933 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1934 for (unsigned I = 0; I != Count; ++I)
1935 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1938 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1939 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1942 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1943 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1946 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1947 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1951 /*===-- Instruction builders ----------------------------------------------===*/
1953 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1954 return wrap(new IRBuilder<>(*unwrap(C)));
1957 LLVMBuilderRef LLVMCreateBuilder(void) {
1958 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1961 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1962 LLVMValueRef Instr) {
1963 BasicBlock *BB = unwrap(Block);
1964 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1965 unwrap(Builder)->SetInsertPoint(BB, I);
1968 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1969 Instruction *I = unwrap<Instruction>(Instr);
1970 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1973 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1974 BasicBlock *BB = unwrap(Block);
1975 unwrap(Builder)->SetInsertPoint(BB);
1978 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1979 return wrap(unwrap(Builder)->GetInsertBlock());
1982 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1983 unwrap(Builder)->ClearInsertionPoint();
1986 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1987 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1990 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1992 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1995 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1996 delete unwrap(Builder);
1999 /*--.. Metadata builders ...................................................--*/
2001 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2002 MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
2003 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
2006 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2007 return wrap(unwrap(Builder)->getCurrentDebugLocation()
2008 .getAsMDNode(unwrap(Builder)->getContext()));
2011 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2012 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2016 /*--.. Instruction builders ................................................--*/
2018 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2019 return wrap(unwrap(B)->CreateRetVoid());
2022 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2023 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2026 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2028 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2031 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2032 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2035 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2036 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2037 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2040 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2041 LLVMBasicBlockRef Else, unsigned NumCases) {
2042 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2045 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2046 unsigned NumDests) {
2047 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2050 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2051 LLVMValueRef *Args, unsigned NumArgs,
2052 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2054 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2055 makeArrayRef(unwrap(Args), NumArgs),
2059 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2060 LLVMValueRef PersFn, unsigned NumClauses,
2062 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2063 cast<Function>(unwrap(PersFn)),
2067 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2068 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2071 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2072 return wrap(unwrap(B)->CreateUnreachable());
2075 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2076 LLVMBasicBlockRef Dest) {
2077 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2080 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2081 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2084 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2085 unwrap<LandingPadInst>(LandingPad)->
2086 addClause(cast<Constant>(unwrap(ClauseVal)));
2089 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2090 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2093 /*--.. Arithmetic ..........................................................--*/
2095 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2097 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2100 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2102 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2105 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2107 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2110 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2112 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2115 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2117 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2120 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2122 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2125 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2127 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2130 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2132 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2135 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2137 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2140 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2142 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2145 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2147 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2150 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2152 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2155 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2157 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2160 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2162 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2165 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2166 LLVMValueRef RHS, const char *Name) {
2167 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2170 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2172 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2175 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2177 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2180 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2182 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2185 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2187 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2190 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2192 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2195 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2197 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2200 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2202 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2205 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2207 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2210 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2212 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2215 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2217 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2220 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2221 LLVMValueRef LHS, LLVMValueRef RHS,
2223 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2224 unwrap(RHS), Name));
2227 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2228 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2231 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2233 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2236 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2238 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2241 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2242 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2245 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2246 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2249 /*--.. Memory ..............................................................--*/
2251 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2253 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2254 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2255 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2256 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2257 ITy, unwrap(Ty), AllocSize,
2258 nullptr, nullptr, "");
2259 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2262 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2263 LLVMValueRef Val, const char *Name) {
2264 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2265 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2266 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2267 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2268 ITy, unwrap(Ty), AllocSize,
2269 unwrap(Val), nullptr, "");
2270 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2273 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2275 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2278 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2279 LLVMValueRef Val, const char *Name) {
2280 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2283 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2284 return wrap(unwrap(B)->Insert(
2285 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2289 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2291 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2294 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2295 LLVMValueRef PointerVal) {
2296 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2299 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2301 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2302 case LLVMAtomicOrderingUnordered: return Unordered;
2303 case LLVMAtomicOrderingMonotonic: return Monotonic;
2304 case LLVMAtomicOrderingAcquire: return Acquire;
2305 case LLVMAtomicOrderingRelease: return Release;
2306 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2307 case LLVMAtomicOrderingSequentiallyConsistent:
2308 return SequentiallyConsistent;
2311 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2314 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2315 LLVMBool isSingleThread, const char *Name) {
2317 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2318 isSingleThread ? SingleThread : CrossThread,
2322 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2323 LLVMValueRef *Indices, unsigned NumIndices,
2325 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2326 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2329 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2330 LLVMValueRef *Indices, unsigned NumIndices,
2332 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2333 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2336 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2337 unsigned Idx, const char *Name) {
2338 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2341 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2343 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2346 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2348 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2351 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2352 Value *P = unwrap<Value>(MemAccessInst);
2353 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2354 return LI->isVolatile();
2355 return cast<StoreInst>(P)->isVolatile();
2358 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2359 Value *P = unwrap<Value>(MemAccessInst);
2360 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2361 return LI->setVolatile(isVolatile);
2362 return cast<StoreInst>(P)->setVolatile(isVolatile);
2365 /*--.. Casts ...............................................................--*/
2367 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2368 LLVMTypeRef DestTy, const char *Name) {
2369 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2372 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2373 LLVMTypeRef DestTy, const char *Name) {
2374 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2377 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2378 LLVMTypeRef DestTy, const char *Name) {
2379 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2382 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2383 LLVMTypeRef DestTy, const char *Name) {
2384 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2387 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2388 LLVMTypeRef DestTy, const char *Name) {
2389 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2392 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2393 LLVMTypeRef DestTy, const char *Name) {
2394 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2397 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2398 LLVMTypeRef DestTy, const char *Name) {
2399 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2402 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2403 LLVMTypeRef DestTy, const char *Name) {
2404 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2407 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2408 LLVMTypeRef DestTy, const char *Name) {
2409 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2412 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2413 LLVMTypeRef DestTy, const char *Name) {
2414 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2417 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2418 LLVMTypeRef DestTy, const char *Name) {
2419 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2422 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2423 LLVMTypeRef DestTy, const char *Name) {
2424 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2427 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2428 LLVMTypeRef DestTy, const char *Name) {
2429 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2432 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2433 LLVMTypeRef DestTy, const char *Name) {
2434 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2438 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2439 LLVMTypeRef DestTy, const char *Name) {
2440 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2444 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2445 LLVMTypeRef DestTy, const char *Name) {
2446 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2450 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2451 LLVMTypeRef DestTy, const char *Name) {
2452 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2453 unwrap(DestTy), Name));
2456 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2457 LLVMTypeRef DestTy, const char *Name) {
2458 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2461 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2462 LLVMTypeRef DestTy, const char *Name) {
2463 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2464 /*isSigned*/true, Name));
2467 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2468 LLVMTypeRef DestTy, const char *Name) {
2469 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2472 /*--.. Comparisons .........................................................--*/
2474 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2475 LLVMValueRef LHS, LLVMValueRef RHS,
2477 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2478 unwrap(LHS), unwrap(RHS), Name));
2481 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2482 LLVMValueRef LHS, LLVMValueRef RHS,
2484 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2485 unwrap(LHS), unwrap(RHS), Name));
2488 /*--.. Miscellaneous instructions ..........................................--*/
2490 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2491 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2494 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2495 LLVMValueRef *Args, unsigned NumArgs,
2497 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2498 makeArrayRef(unwrap(Args), NumArgs),
2502 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2503 LLVMValueRef Then, LLVMValueRef Else,
2505 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2509 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2510 LLVMTypeRef Ty, const char *Name) {
2511 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2514 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2515 LLVMValueRef Index, const char *Name) {
2516 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2520 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2521 LLVMValueRef EltVal, LLVMValueRef Index,
2523 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2524 unwrap(Index), Name));
2527 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2528 LLVMValueRef V2, LLVMValueRef Mask,
2530 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2531 unwrap(Mask), Name));
2534 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2535 unsigned Index, const char *Name) {
2536 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2539 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2540 LLVMValueRef EltVal, unsigned Index,
2542 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2546 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2548 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2551 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2553 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2556 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2557 LLVMValueRef RHS, const char *Name) {
2558 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2561 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2562 LLVMValueRef PTR, LLVMValueRef Val,
2563 LLVMAtomicOrdering ordering,
2564 LLVMBool singleThread) {
2565 AtomicRMWInst::BinOp intop;
2567 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2568 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2569 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2570 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2571 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2572 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2573 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2574 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2575 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2576 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2577 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2579 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2580 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2584 /*===-- Module providers --------------------------------------------------===*/
2586 LLVMModuleProviderRef
2587 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2588 return reinterpret_cast<LLVMModuleProviderRef>(M);
2591 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2596 /*===-- Memory buffers ----------------------------------------------------===*/
2598 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2600 LLVMMemoryBufferRef *OutMemBuf,
2601 char **OutMessage) {
2603 std::unique_ptr<MemoryBuffer> MB;
2605 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2606 *OutMemBuf = wrap(MB.release());
2610 *OutMessage = strdup(ec.message().c_str());
2614 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2615 char **OutMessage) {
2616 std::unique_ptr<MemoryBuffer> MB;
2618 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2619 *OutMemBuf = wrap(MB.release());
2623 *OutMessage = strdup(ec.message().c_str());
2627 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2628 const char *InputData,
2629 size_t InputDataLength,
2630 const char *BufferName,
2631 LLVMBool RequiresNullTerminator) {
2633 return wrap(MemoryBuffer::getMemBuffer(
2634 StringRef(InputData, InputDataLength),
2635 StringRef(BufferName),
2636 RequiresNullTerminator));
2639 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2640 const char *InputData,
2641 size_t InputDataLength,
2642 const char *BufferName) {
2644 return wrap(MemoryBuffer::getMemBufferCopy(
2645 StringRef(InputData, InputDataLength),
2646 StringRef(BufferName)));
2649 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2650 return unwrap(MemBuf)->getBufferStart();
2653 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2654 return unwrap(MemBuf)->getBufferSize();
2657 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2658 delete unwrap(MemBuf);
2661 /*===-- Pass Registry -----------------------------------------------------===*/
2663 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2664 return wrap(PassRegistry::getPassRegistry());
2667 /*===-- Pass Manager ------------------------------------------------------===*/
2669 LLVMPassManagerRef LLVMCreatePassManager() {
2670 return wrap(new PassManager());
2673 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2674 return wrap(new FunctionPassManager(unwrap(M)));
2677 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2678 return LLVMCreateFunctionPassManagerForModule(
2679 reinterpret_cast<LLVMModuleRef>(P));
2682 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2683 return unwrap<PassManager>(PM)->run(*unwrap(M));
2686 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2687 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2690 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2691 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2694 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2695 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2698 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2702 /*===-- Threading ------------------------------------------------------===*/
2704 LLVMBool LLVMStartMultithreaded() {
2705 return llvm_start_multithreaded();
2708 void LLVMStopMultithreaded() {
2709 llvm_stop_multithreaded();
2712 LLVMBool LLVMIsMultithreaded() {
2713 return llvm_is_multithreaded();