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/DiagnosticInfo.h"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/IR/DerivedTypes.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/ManagedStatic.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Threading.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/system_error.h"
44 void llvm::initializeCore(PassRegistry &Registry) {
45 initializeDominatorTreeWrapperPassPass(Registry);
46 initializePrintModulePassWrapperPass(Registry);
47 initializePrintFunctionPassWrapperPass(Registry);
48 initializePrintBasicBlockPassPass(Registry);
49 initializeVerifierLegacyPassPass(Registry);
52 void LLVMInitializeCore(LLVMPassRegistryRef R) {
53 initializeCore(*unwrap(R));
60 /*===-- Error handling ----------------------------------------------------===*/
62 char *LLVMCreateMessage(const char *Message) {
63 return strdup(Message);
66 void LLVMDisposeMessage(char *Message) {
71 /*===-- Operations on contexts --------------------------------------------===*/
73 LLVMContextRef LLVMContextCreate() {
74 return wrap(new LLVMContext());
77 LLVMContextRef LLVMGetGlobalContext() {
78 return wrap(&getGlobalContext());
81 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
82 LLVMDiagnosticHandler Handler,
83 void *DiagnosticContext) {
84 unwrap(C)->setDiagnosticHandler(
85 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(Handler),
89 void LLVMContextDispose(LLVMContextRef C) {
93 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
95 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
98 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
99 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
102 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
103 std::string MsgStorage;
104 raw_string_ostream Stream(MsgStorage);
105 DiagnosticPrinterRawOStream DP(Stream);
107 unwrap(DI)->print(DP);
110 return LLVMCreateMessage(MsgStorage.c_str());
113 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
114 LLVMDiagnosticSeverity severity;
116 switch(unwrap(DI)->getSeverity()) {
118 severity = LLVMDSError;
121 severity = LLVMDSWarning;
124 severity = LLVMDSRemark;
127 severity = LLVMDSNote;
137 /*===-- Operations on modules ---------------------------------------------===*/
139 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
140 return wrap(new Module(ModuleID, getGlobalContext()));
143 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
145 return wrap(new Module(ModuleID, *unwrap(C)));
148 void LLVMDisposeModule(LLVMModuleRef M) {
152 /*--.. Data layout .........................................................--*/
153 const char * LLVMGetDataLayout(LLVMModuleRef M) {
154 return unwrap(M)->getDataLayoutStr().c_str();
157 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
158 unwrap(M)->setDataLayout(Triple);
161 /*--.. Target triple .......................................................--*/
162 const char * LLVMGetTarget(LLVMModuleRef M) {
163 return unwrap(M)->getTargetTriple().c_str();
166 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
167 unwrap(M)->setTargetTriple(Triple);
170 void LLVMDumpModule(LLVMModuleRef M) {
174 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
175 char **ErrorMessage) {
177 raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
178 if (!error.empty()) {
179 *ErrorMessage = strdup(error.c_str());
183 unwrap(M)->print(dest, nullptr);
185 if (!error.empty()) {
186 *ErrorMessage = strdup(error.c_str());
193 char *LLVMPrintModuleToString(LLVMModuleRef M) {
195 raw_string_ostream os(buf);
197 unwrap(M)->print(os, nullptr);
200 return strdup(buf.c_str());
203 /*--.. Operations on inline assembler ......................................--*/
204 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
205 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
209 /*--.. Operations on module contexts ......................................--*/
210 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
211 return wrap(&unwrap(M)->getContext());
215 /*===-- Operations on types -----------------------------------------------===*/
217 /*--.. Operations on all types (mostly) ....................................--*/
219 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
220 switch (unwrap(Ty)->getTypeID()) {
222 return LLVMVoidTypeKind;
224 return LLVMHalfTypeKind;
225 case Type::FloatTyID:
226 return LLVMFloatTypeKind;
227 case Type::DoubleTyID:
228 return LLVMDoubleTypeKind;
229 case Type::X86_FP80TyID:
230 return LLVMX86_FP80TypeKind;
231 case Type::FP128TyID:
232 return LLVMFP128TypeKind;
233 case Type::PPC_FP128TyID:
234 return LLVMPPC_FP128TypeKind;
235 case Type::LabelTyID:
236 return LLVMLabelTypeKind;
237 case Type::MetadataTyID:
238 return LLVMMetadataTypeKind;
239 case Type::IntegerTyID:
240 return LLVMIntegerTypeKind;
241 case Type::FunctionTyID:
242 return LLVMFunctionTypeKind;
243 case Type::StructTyID:
244 return LLVMStructTypeKind;
245 case Type::ArrayTyID:
246 return LLVMArrayTypeKind;
247 case Type::PointerTyID:
248 return LLVMPointerTypeKind;
249 case Type::VectorTyID:
250 return LLVMVectorTypeKind;
251 case Type::X86_MMXTyID:
252 return LLVMX86_MMXTypeKind;
254 llvm_unreachable("Unhandled TypeID.");
257 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
259 return unwrap(Ty)->isSized();
262 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
263 return wrap(&unwrap(Ty)->getContext());
266 void LLVMDumpType(LLVMTypeRef Ty) {
267 return unwrap(Ty)->dump();
270 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
272 raw_string_ostream os(buf);
274 unwrap(Ty)->print(os);
277 return strdup(buf.c_str());
280 /*--.. Operations on integer types .........................................--*/
282 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
283 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
285 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
286 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
288 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
289 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
291 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
292 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
294 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
295 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
297 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
298 return wrap(IntegerType::get(*unwrap(C), NumBits));
301 LLVMTypeRef LLVMInt1Type(void) {
302 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
304 LLVMTypeRef LLVMInt8Type(void) {
305 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
307 LLVMTypeRef LLVMInt16Type(void) {
308 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
310 LLVMTypeRef LLVMInt32Type(void) {
311 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
313 LLVMTypeRef LLVMInt64Type(void) {
314 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
316 LLVMTypeRef LLVMIntType(unsigned NumBits) {
317 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
320 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
321 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
324 /*--.. Operations on real types ............................................--*/
326 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
327 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
329 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
330 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
332 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
333 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
335 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
336 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
338 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
339 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
341 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
342 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
344 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
345 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
348 LLVMTypeRef LLVMHalfType(void) {
349 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
351 LLVMTypeRef LLVMFloatType(void) {
352 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
354 LLVMTypeRef LLVMDoubleType(void) {
355 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
357 LLVMTypeRef LLVMX86FP80Type(void) {
358 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
360 LLVMTypeRef LLVMFP128Type(void) {
361 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
363 LLVMTypeRef LLVMPPCFP128Type(void) {
364 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
366 LLVMTypeRef LLVMX86MMXType(void) {
367 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
370 /*--.. Operations on function types ........................................--*/
372 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
373 LLVMTypeRef *ParamTypes, unsigned ParamCount,
375 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
376 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
379 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
380 return unwrap<FunctionType>(FunctionTy)->isVarArg();
383 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
384 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
387 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
388 return unwrap<FunctionType>(FunctionTy)->getNumParams();
391 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
392 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
393 for (FunctionType::param_iterator I = Ty->param_begin(),
394 E = Ty->param_end(); I != E; ++I)
398 /*--.. Operations on struct types ..........................................--*/
400 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
401 unsigned ElementCount, LLVMBool Packed) {
402 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
403 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
406 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
407 unsigned ElementCount, LLVMBool Packed) {
408 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
409 ElementCount, Packed);
412 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
414 return wrap(StructType::create(*unwrap(C), Name));
417 const char *LLVMGetStructName(LLVMTypeRef Ty)
419 StructType *Type = unwrap<StructType>(Ty);
420 if (!Type->hasName())
422 return Type->getName().data();
425 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
426 unsigned ElementCount, LLVMBool Packed) {
427 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
428 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
431 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
432 return unwrap<StructType>(StructTy)->getNumElements();
435 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
436 StructType *Ty = unwrap<StructType>(StructTy);
437 for (StructType::element_iterator I = Ty->element_begin(),
438 E = Ty->element_end(); I != E; ++I)
442 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
443 return unwrap<StructType>(StructTy)->isPacked();
446 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
447 return unwrap<StructType>(StructTy)->isOpaque();
450 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
451 return wrap(unwrap(M)->getTypeByName(Name));
454 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
456 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
457 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
460 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
461 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
464 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
465 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
468 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
469 return wrap(unwrap<SequentialType>(Ty)->getElementType());
472 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
473 return unwrap<ArrayType>(ArrayTy)->getNumElements();
476 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
477 return unwrap<PointerType>(PointerTy)->getAddressSpace();
480 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
481 return unwrap<VectorType>(VectorTy)->getNumElements();
484 /*--.. Operations on other types ...........................................--*/
486 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
487 return wrap(Type::getVoidTy(*unwrap(C)));
489 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
490 return wrap(Type::getLabelTy(*unwrap(C)));
493 LLVMTypeRef LLVMVoidType(void) {
494 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
496 LLVMTypeRef LLVMLabelType(void) {
497 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
500 /*===-- Operations on values ----------------------------------------------===*/
502 /*--.. Operations on all values ............................................--*/
504 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
505 return wrap(unwrap(Val)->getType());
508 const char *LLVMGetValueName(LLVMValueRef Val) {
509 return unwrap(Val)->getName().data();
512 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
513 unwrap(Val)->setName(Name);
516 void LLVMDumpValue(LLVMValueRef Val) {
520 char* LLVMPrintValueToString(LLVMValueRef Val) {
522 raw_string_ostream os(buf);
524 unwrap(Val)->print(os);
527 return strdup(buf.c_str());
530 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
531 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
534 int LLVMHasMetadata(LLVMValueRef Inst) {
535 return unwrap<Instruction>(Inst)->hasMetadata();
538 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
539 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
542 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
543 unwrap<Instruction>(Inst)->setMetadata(KindID,
544 MD ? unwrap<MDNode>(MD) : nullptr);
547 /*--.. Conversion functions ................................................--*/
549 #define LLVM_DEFINE_VALUE_CAST(name) \
550 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
551 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
554 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
556 /*--.. Operations on Uses ..................................................--*/
557 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
558 Value *V = unwrap(Val);
559 Value::use_iterator I = V->use_begin();
560 if (I == V->use_end())
565 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
566 Use *Next = unwrap(U)->getNext();
572 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
573 return wrap(unwrap(U)->getUser());
576 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
577 return wrap(unwrap(U)->get());
580 /*--.. Operations on Users .................................................--*/
581 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
582 Value *V = unwrap(Val);
583 if (MDNode *MD = dyn_cast<MDNode>(V))
584 return wrap(MD->getOperand(Index));
585 return wrap(cast<User>(V)->getOperand(Index));
588 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
589 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
592 int LLVMGetNumOperands(LLVMValueRef Val) {
593 Value *V = unwrap(Val);
594 if (MDNode *MD = dyn_cast<MDNode>(V))
595 return MD->getNumOperands();
596 return cast<User>(V)->getNumOperands();
599 /*--.. Operations on constants of any type .................................--*/
601 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
602 return wrap(Constant::getNullValue(unwrap(Ty)));
605 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
606 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
609 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
610 return wrap(UndefValue::get(unwrap(Ty)));
613 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
614 return isa<Constant>(unwrap(Ty));
617 LLVMBool LLVMIsNull(LLVMValueRef Val) {
618 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
619 return C->isNullValue();
623 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
624 return isa<UndefValue>(unwrap(Val));
627 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
629 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
632 /*--.. Operations on metadata nodes ........................................--*/
634 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
636 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
639 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
640 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
643 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
645 return wrap(MDNode::get(*unwrap(C),
646 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
649 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
650 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
653 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
654 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
655 *Len = S->getString().size();
656 return S->getString().data();
662 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
664 return cast<MDNode>(unwrap(V))->getNumOperands();
667 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
669 const MDNode *N = cast<MDNode>(unwrap(V));
670 const unsigned numOperands = N->getNumOperands();
671 for (unsigned i = 0; i < numOperands; i++)
672 Dest[i] = wrap(N->getOperand(i));
675 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
677 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
678 return N->getNumOperands();
683 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
685 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
688 for (unsigned i=0;i<N->getNumOperands();i++)
689 Dest[i] = wrap(N->getOperand(i));
692 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
695 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
698 MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
703 /*--.. Operations on scalar constants ......................................--*/
705 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
706 LLVMBool SignExtend) {
707 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
710 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
712 const uint64_t Words[]) {
713 IntegerType *Ty = unwrap<IntegerType>(IntTy);
714 return wrap(ConstantInt::get(Ty->getContext(),
715 APInt(Ty->getBitWidth(),
716 makeArrayRef(Words, NumWords))));
719 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
721 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
725 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
726 unsigned SLen, uint8_t Radix) {
727 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
731 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
732 return wrap(ConstantFP::get(unwrap(RealTy), N));
735 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
736 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
739 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
741 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
744 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
745 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
748 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
749 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
752 /*--.. Operations on composite constants ...................................--*/
754 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
756 LLVMBool DontNullTerminate) {
757 /* Inverted the sense of AddNull because ', 0)' is a
758 better mnemonic for null termination than ', 1)'. */
759 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
760 DontNullTerminate == 0));
762 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
763 LLVMValueRef *ConstantVals,
764 unsigned Count, LLVMBool Packed) {
765 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
766 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
770 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
771 LLVMBool DontNullTerminate) {
772 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
775 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
776 LLVMValueRef *ConstantVals, unsigned Length) {
777 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
778 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
780 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
782 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
786 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
787 LLVMValueRef *ConstantVals,
789 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
790 StructType *Ty = cast<StructType>(unwrap(StructTy));
792 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
795 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
796 return wrap(ConstantVector::get(makeArrayRef(
797 unwrap<Constant>(ScalarConstantVals, Size), Size)));
800 /*-- Opcode mapping */
802 static LLVMOpcode map_to_llvmopcode(int opcode)
805 default: llvm_unreachable("Unhandled Opcode.");
806 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
807 #include "llvm/IR/Instruction.def"
812 static int map_from_llvmopcode(LLVMOpcode code)
815 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
816 #include "llvm/IR/Instruction.def"
819 llvm_unreachable("Unhandled Opcode.");
822 /*--.. Constant expressions ................................................--*/
824 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
825 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
828 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
829 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
832 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
833 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
836 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
837 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
840 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
841 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
844 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
845 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
849 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
850 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
853 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
854 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
857 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
858 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
859 unwrap<Constant>(RHSConstant)));
862 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
863 LLVMValueRef RHSConstant) {
864 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
865 unwrap<Constant>(RHSConstant)));
868 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
869 LLVMValueRef RHSConstant) {
870 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
871 unwrap<Constant>(RHSConstant)));
874 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
875 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
876 unwrap<Constant>(RHSConstant)));
879 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
880 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
881 unwrap<Constant>(RHSConstant)));
884 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
885 LLVMValueRef RHSConstant) {
886 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
887 unwrap<Constant>(RHSConstant)));
890 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
891 LLVMValueRef RHSConstant) {
892 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
893 unwrap<Constant>(RHSConstant)));
896 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
897 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
898 unwrap<Constant>(RHSConstant)));
901 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
902 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
903 unwrap<Constant>(RHSConstant)));
906 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
907 LLVMValueRef RHSConstant) {
908 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
909 unwrap<Constant>(RHSConstant)));
912 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
913 LLVMValueRef RHSConstant) {
914 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
915 unwrap<Constant>(RHSConstant)));
918 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
919 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
920 unwrap<Constant>(RHSConstant)));
923 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
924 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
925 unwrap<Constant>(RHSConstant)));
928 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
929 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
930 unwrap<Constant>(RHSConstant)));
933 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
934 LLVMValueRef RHSConstant) {
935 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
936 unwrap<Constant>(RHSConstant)));
939 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
940 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
941 unwrap<Constant>(RHSConstant)));
944 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
945 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
946 unwrap<Constant>(RHSConstant)));
949 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
950 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
951 unwrap<Constant>(RHSConstant)));
954 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
955 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
956 unwrap<Constant>(RHSConstant)));
959 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
960 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
961 unwrap<Constant>(RHSConstant)));
964 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
965 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
966 unwrap<Constant>(RHSConstant)));
969 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
970 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
971 unwrap<Constant>(RHSConstant)));
974 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
975 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
976 return wrap(ConstantExpr::getICmp(Predicate,
977 unwrap<Constant>(LHSConstant),
978 unwrap<Constant>(RHSConstant)));
981 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
982 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
983 return wrap(ConstantExpr::getFCmp(Predicate,
984 unwrap<Constant>(LHSConstant),
985 unwrap<Constant>(RHSConstant)));
988 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
989 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
990 unwrap<Constant>(RHSConstant)));
993 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
994 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
995 unwrap<Constant>(RHSConstant)));
998 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
999 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1000 unwrap<Constant>(RHSConstant)));
1003 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1004 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1005 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1007 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
1011 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1012 LLVMValueRef *ConstantIndices,
1013 unsigned NumIndices) {
1014 Constant* Val = unwrap<Constant>(ConstantVal);
1015 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1017 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
1020 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1021 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1025 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1026 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1030 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1031 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1035 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1036 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1040 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1041 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1045 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1046 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1050 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1051 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1055 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1056 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1060 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1061 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1065 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1066 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1070 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1071 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1075 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1076 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1080 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1081 LLVMTypeRef ToType) {
1082 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1086 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1087 LLVMTypeRef ToType) {
1088 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1092 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1093 LLVMTypeRef ToType) {
1094 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1098 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1099 LLVMTypeRef ToType) {
1100 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1104 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1105 LLVMTypeRef ToType) {
1106 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1110 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1111 LLVMBool isSigned) {
1112 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1113 unwrap(ToType), isSigned));
1116 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1117 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1121 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1122 LLVMValueRef ConstantIfTrue,
1123 LLVMValueRef ConstantIfFalse) {
1124 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1125 unwrap<Constant>(ConstantIfTrue),
1126 unwrap<Constant>(ConstantIfFalse)));
1129 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1130 LLVMValueRef IndexConstant) {
1131 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1132 unwrap<Constant>(IndexConstant)));
1135 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1136 LLVMValueRef ElementValueConstant,
1137 LLVMValueRef IndexConstant) {
1138 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1139 unwrap<Constant>(ElementValueConstant),
1140 unwrap<Constant>(IndexConstant)));
1143 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1144 LLVMValueRef VectorBConstant,
1145 LLVMValueRef MaskConstant) {
1146 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1147 unwrap<Constant>(VectorBConstant),
1148 unwrap<Constant>(MaskConstant)));
1151 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1153 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1154 makeArrayRef(IdxList, NumIdx)));
1157 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1158 LLVMValueRef ElementValueConstant,
1159 unsigned *IdxList, unsigned NumIdx) {
1160 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1161 unwrap<Constant>(ElementValueConstant),
1162 makeArrayRef(IdxList, NumIdx)));
1165 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1166 const char *Constraints,
1167 LLVMBool HasSideEffects,
1168 LLVMBool IsAlignStack) {
1169 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1170 Constraints, HasSideEffects, IsAlignStack));
1173 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1174 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1177 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1179 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1180 return wrap(unwrap<GlobalValue>(Global)->getParent());
1183 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1184 return unwrap<GlobalValue>(Global)->isDeclaration();
1187 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1188 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1189 case GlobalValue::ExternalLinkage:
1190 return LLVMExternalLinkage;
1191 case GlobalValue::AvailableExternallyLinkage:
1192 return LLVMAvailableExternallyLinkage;
1193 case GlobalValue::LinkOnceAnyLinkage:
1194 return LLVMLinkOnceAnyLinkage;
1195 case GlobalValue::LinkOnceODRLinkage:
1196 return LLVMLinkOnceODRLinkage;
1197 case GlobalValue::WeakAnyLinkage:
1198 return LLVMWeakAnyLinkage;
1199 case GlobalValue::WeakODRLinkage:
1200 return LLVMWeakODRLinkage;
1201 case GlobalValue::AppendingLinkage:
1202 return LLVMAppendingLinkage;
1203 case GlobalValue::InternalLinkage:
1204 return LLVMInternalLinkage;
1205 case GlobalValue::PrivateLinkage:
1206 return LLVMPrivateLinkage;
1207 case GlobalValue::ExternalWeakLinkage:
1208 return LLVMExternalWeakLinkage;
1209 case GlobalValue::CommonLinkage:
1210 return LLVMCommonLinkage;
1213 llvm_unreachable("Invalid GlobalValue linkage!");
1216 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1217 GlobalValue *GV = unwrap<GlobalValue>(Global);
1220 case LLVMExternalLinkage:
1221 GV->setLinkage(GlobalValue::ExternalLinkage);
1223 case LLVMAvailableExternallyLinkage:
1224 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1226 case LLVMLinkOnceAnyLinkage:
1227 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1229 case LLVMLinkOnceODRLinkage:
1230 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1232 case LLVMLinkOnceODRAutoHideLinkage:
1233 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1234 "longer supported.");
1236 case LLVMWeakAnyLinkage:
1237 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1239 case LLVMWeakODRLinkage:
1240 GV->setLinkage(GlobalValue::WeakODRLinkage);
1242 case LLVMAppendingLinkage:
1243 GV->setLinkage(GlobalValue::AppendingLinkage);
1245 case LLVMInternalLinkage:
1246 GV->setLinkage(GlobalValue::InternalLinkage);
1248 case LLVMPrivateLinkage:
1249 GV->setLinkage(GlobalValue::PrivateLinkage);
1251 case LLVMLinkerPrivateLinkage:
1252 GV->setLinkage(GlobalValue::PrivateLinkage);
1254 case LLVMLinkerPrivateWeakLinkage:
1255 GV->setLinkage(GlobalValue::PrivateLinkage);
1257 case LLVMDLLImportLinkage:
1259 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1261 case LLVMDLLExportLinkage:
1263 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1265 case LLVMExternalWeakLinkage:
1266 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1268 case LLVMGhostLinkage:
1270 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1272 case LLVMCommonLinkage:
1273 GV->setLinkage(GlobalValue::CommonLinkage);
1278 const char *LLVMGetSection(LLVMValueRef Global) {
1279 return unwrap<GlobalValue>(Global)->getSection().c_str();
1282 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1283 unwrap<GlobalValue>(Global)->setSection(Section);
1286 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1287 return static_cast<LLVMVisibility>(
1288 unwrap<GlobalValue>(Global)->getVisibility());
1291 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1292 unwrap<GlobalValue>(Global)
1293 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1296 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1297 return static_cast<LLVMDLLStorageClass>(
1298 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1301 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1302 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1303 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1306 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1307 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1310 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1311 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1314 /*--.. Operations on global variables, load and store instructions .........--*/
1316 unsigned LLVMGetAlignment(LLVMValueRef V) {
1317 Value *P = unwrap<Value>(V);
1318 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1319 return GV->getAlignment();
1320 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1321 return AI->getAlignment();
1322 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1323 return LI->getAlignment();
1324 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1325 return SI->getAlignment();
1328 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1331 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1332 Value *P = unwrap<Value>(V);
1333 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1334 GV->setAlignment(Bytes);
1335 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1336 AI->setAlignment(Bytes);
1337 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1338 LI->setAlignment(Bytes);
1339 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1340 SI->setAlignment(Bytes);
1343 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1346 /*--.. Operations on global variables ......................................--*/
1348 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1349 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1350 GlobalValue::ExternalLinkage, nullptr, Name));
1353 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1355 unsigned AddressSpace) {
1356 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1357 GlobalValue::ExternalLinkage, nullptr, Name,
1358 nullptr, GlobalVariable::NotThreadLocal,
1362 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1363 return wrap(unwrap(M)->getNamedGlobal(Name));
1366 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1367 Module *Mod = unwrap(M);
1368 Module::global_iterator I = Mod->global_begin();
1369 if (I == Mod->global_end())
1374 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1375 Module *Mod = unwrap(M);
1376 Module::global_iterator I = Mod->global_end();
1377 if (I == Mod->global_begin())
1382 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1383 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1384 Module::global_iterator I = GV;
1385 if (++I == GV->getParent()->global_end())
1390 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1391 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1392 Module::global_iterator I = GV;
1393 if (I == GV->getParent()->global_begin())
1398 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1399 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1402 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1403 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1404 if ( !GV->hasInitializer() )
1406 return wrap(GV->getInitializer());
1409 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1410 unwrap<GlobalVariable>(GlobalVar)
1411 ->setInitializer(unwrap<Constant>(ConstantVal));
1414 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1415 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1418 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1419 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1422 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1423 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1426 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1427 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1430 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1431 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1432 case GlobalVariable::NotThreadLocal:
1433 return LLVMNotThreadLocal;
1434 case GlobalVariable::GeneralDynamicTLSModel:
1435 return LLVMGeneralDynamicTLSModel;
1436 case GlobalVariable::LocalDynamicTLSModel:
1437 return LLVMLocalDynamicTLSModel;
1438 case GlobalVariable::InitialExecTLSModel:
1439 return LLVMInitialExecTLSModel;
1440 case GlobalVariable::LocalExecTLSModel:
1441 return LLVMLocalExecTLSModel;
1444 llvm_unreachable("Invalid GlobalVariable thread local mode");
1447 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1448 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1451 case LLVMNotThreadLocal:
1452 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1454 case LLVMGeneralDynamicTLSModel:
1455 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1457 case LLVMLocalDynamicTLSModel:
1458 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1460 case LLVMInitialExecTLSModel:
1461 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1463 case LLVMLocalExecTLSModel:
1464 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1469 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1470 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1473 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1474 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1477 /*--.. Operations on aliases ......................................--*/
1479 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1481 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1482 unwrap<Constant>(Aliasee), unwrap (M)));
1485 /*--.. Operations on functions .............................................--*/
1487 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1488 LLVMTypeRef FunctionTy) {
1489 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1490 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1493 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1494 return wrap(unwrap(M)->getFunction(Name));
1497 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1498 Module *Mod = unwrap(M);
1499 Module::iterator I = Mod->begin();
1500 if (I == Mod->end())
1505 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1506 Module *Mod = unwrap(M);
1507 Module::iterator I = Mod->end();
1508 if (I == Mod->begin())
1513 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1514 Function *Func = unwrap<Function>(Fn);
1515 Module::iterator I = Func;
1516 if (++I == Func->getParent()->end())
1521 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1522 Function *Func = unwrap<Function>(Fn);
1523 Module::iterator I = Func;
1524 if (I == Func->getParent()->begin())
1529 void LLVMDeleteFunction(LLVMValueRef Fn) {
1530 unwrap<Function>(Fn)->eraseFromParent();
1533 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1534 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1535 return F->getIntrinsicID();
1539 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1540 return unwrap<Function>(Fn)->getCallingConv();
1543 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1544 return unwrap<Function>(Fn)->setCallingConv(
1545 static_cast<CallingConv::ID>(CC));
1548 const char *LLVMGetGC(LLVMValueRef Fn) {
1549 Function *F = unwrap<Function>(Fn);
1550 return F->hasGC()? F->getGC() : nullptr;
1553 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1554 Function *F = unwrap<Function>(Fn);
1561 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1562 Function *Func = unwrap<Function>(Fn);
1563 const AttributeSet PAL = Func->getAttributes();
1565 const AttributeSet PALnew =
1566 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1567 AttributeSet::get(Func->getContext(),
1568 AttributeSet::FunctionIndex, B));
1569 Func->setAttributes(PALnew);
1572 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1574 Function *Func = unwrap<Function>(Fn);
1575 AttributeSet::AttrIndex Idx =
1576 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1579 B.addAttribute(A, V);
1580 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1581 Func->addAttributes(Idx, Set);
1584 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1585 Function *Func = unwrap<Function>(Fn);
1586 const AttributeSet PAL = Func->getAttributes();
1588 const AttributeSet PALnew =
1589 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1590 AttributeSet::get(Func->getContext(),
1591 AttributeSet::FunctionIndex, B));
1592 Func->setAttributes(PALnew);
1595 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1596 Function *Func = unwrap<Function>(Fn);
1597 const AttributeSet PAL = Func->getAttributes();
1598 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1601 /*--.. Operations on parameters ............................................--*/
1603 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1604 // This function is strictly redundant to
1605 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1606 return unwrap<Function>(FnRef)->arg_size();
1609 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1610 Function *Fn = unwrap<Function>(FnRef);
1611 for (Function::arg_iterator I = Fn->arg_begin(),
1612 E = Fn->arg_end(); I != E; I++)
1613 *ParamRefs++ = wrap(I);
1616 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1617 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1623 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1624 return wrap(unwrap<Argument>(V)->getParent());
1627 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1628 Function *Func = unwrap<Function>(Fn);
1629 Function::arg_iterator I = Func->arg_begin();
1630 if (I == Func->arg_end())
1635 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1636 Function *Func = unwrap<Function>(Fn);
1637 Function::arg_iterator I = Func->arg_end();
1638 if (I == Func->arg_begin())
1643 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1644 Argument *A = unwrap<Argument>(Arg);
1645 Function::arg_iterator I = A;
1646 if (++I == A->getParent()->arg_end())
1651 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1652 Argument *A = unwrap<Argument>(Arg);
1653 Function::arg_iterator I = A;
1654 if (I == A->getParent()->arg_begin())
1659 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1660 Argument *A = unwrap<Argument>(Arg);
1662 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1665 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1666 Argument *A = unwrap<Argument>(Arg);
1668 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1671 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1672 Argument *A = unwrap<Argument>(Arg);
1673 return (LLVMAttribute)A->getParent()->getAttributes().
1674 Raw(A->getArgNo()+1);
1678 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1679 Argument *A = unwrap<Argument>(Arg);
1681 B.addAlignmentAttr(align);
1682 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1685 /*--.. Operations on basic blocks ..........................................--*/
1687 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1688 return wrap(static_cast<Value*>(unwrap(BB)));
1691 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1692 return isa<BasicBlock>(unwrap(Val));
1695 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1696 return wrap(unwrap<BasicBlock>(Val));
1699 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1700 return wrap(unwrap(BB)->getParent());
1703 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1704 return wrap(unwrap(BB)->getTerminator());
1707 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1708 return unwrap<Function>(FnRef)->size();
1711 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1712 Function *Fn = unwrap<Function>(FnRef);
1713 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1714 *BasicBlocksRefs++ = wrap(I);
1717 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1718 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1721 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1722 Function *Func = unwrap<Function>(Fn);
1723 Function::iterator I = Func->begin();
1724 if (I == Func->end())
1729 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1730 Function *Func = unwrap<Function>(Fn);
1731 Function::iterator I = Func->end();
1732 if (I == Func->begin())
1737 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1738 BasicBlock *Block = unwrap(BB);
1739 Function::iterator I = Block;
1740 if (++I == Block->getParent()->end())
1745 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1746 BasicBlock *Block = unwrap(BB);
1747 Function::iterator I = Block;
1748 if (I == Block->getParent()->begin())
1753 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1756 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1759 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1760 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1763 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1764 LLVMBasicBlockRef BBRef,
1766 BasicBlock *BB = unwrap(BBRef);
1767 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1770 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1772 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1775 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1776 unwrap(BBRef)->eraseFromParent();
1779 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1780 unwrap(BBRef)->removeFromParent();
1783 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1784 unwrap(BB)->moveBefore(unwrap(MovePos));
1787 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1788 unwrap(BB)->moveAfter(unwrap(MovePos));
1791 /*--.. Operations on instructions ..........................................--*/
1793 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1794 return wrap(unwrap<Instruction>(Inst)->getParent());
1797 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1798 BasicBlock *Block = unwrap(BB);
1799 BasicBlock::iterator I = Block->begin();
1800 if (I == Block->end())
1805 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1806 BasicBlock *Block = unwrap(BB);
1807 BasicBlock::iterator I = Block->end();
1808 if (I == Block->begin())
1813 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1814 Instruction *Instr = unwrap<Instruction>(Inst);
1815 BasicBlock::iterator I = Instr;
1816 if (++I == Instr->getParent()->end())
1821 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1822 Instruction *Instr = unwrap<Instruction>(Inst);
1823 BasicBlock::iterator I = Instr;
1824 if (I == Instr->getParent()->begin())
1829 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1830 unwrap<Instruction>(Inst)->eraseFromParent();
1833 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1834 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1835 return (LLVMIntPredicate)I->getPredicate();
1836 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1837 if (CE->getOpcode() == Instruction::ICmp)
1838 return (LLVMIntPredicate)CE->getPredicate();
1839 return (LLVMIntPredicate)0;
1842 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1843 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1844 return map_to_llvmopcode(C->getOpcode());
1845 return (LLVMOpcode)0;
1848 /*--.. Call and invoke instructions ........................................--*/
1850 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1851 Value *V = unwrap(Instr);
1852 if (CallInst *CI = dyn_cast<CallInst>(V))
1853 return CI->getCallingConv();
1854 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1855 return II->getCallingConv();
1856 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1859 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1860 Value *V = unwrap(Instr);
1861 if (CallInst *CI = dyn_cast<CallInst>(V))
1862 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1863 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1864 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1865 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1868 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1870 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1873 Call.getAttributes().addAttributes(Call->getContext(), index,
1874 AttributeSet::get(Call->getContext(),
1878 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1880 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1882 Call.setAttributes(Call.getAttributes()
1883 .removeAttributes(Call->getContext(), index,
1884 AttributeSet::get(Call->getContext(),
1888 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1890 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1892 B.addAlignmentAttr(align);
1893 Call.setAttributes(Call.getAttributes()
1894 .addAttributes(Call->getContext(), index,
1895 AttributeSet::get(Call->getContext(),
1899 /*--.. Operations on call instructions (only) ..............................--*/
1901 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1902 return unwrap<CallInst>(Call)->isTailCall();
1905 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1906 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1909 /*--.. Operations on switch instructions (only) ............................--*/
1911 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1912 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1915 /*--.. Operations on phi nodes .............................................--*/
1917 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1918 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1919 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1920 for (unsigned I = 0; I != Count; ++I)
1921 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1924 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1925 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1928 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1929 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1932 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1933 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1937 /*===-- Instruction builders ----------------------------------------------===*/
1939 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1940 return wrap(new IRBuilder<>(*unwrap(C)));
1943 LLVMBuilderRef LLVMCreateBuilder(void) {
1944 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1947 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1948 LLVMValueRef Instr) {
1949 BasicBlock *BB = unwrap(Block);
1950 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1951 unwrap(Builder)->SetInsertPoint(BB, I);
1954 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1955 Instruction *I = unwrap<Instruction>(Instr);
1956 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1959 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1960 BasicBlock *BB = unwrap(Block);
1961 unwrap(Builder)->SetInsertPoint(BB);
1964 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1965 return wrap(unwrap(Builder)->GetInsertBlock());
1968 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1969 unwrap(Builder)->ClearInsertionPoint();
1972 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1973 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
1976 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
1978 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
1981 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
1982 delete unwrap(Builder);
1985 /*--.. Metadata builders ...................................................--*/
1987 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
1988 MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
1989 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
1992 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
1993 return wrap(unwrap(Builder)->getCurrentDebugLocation()
1994 .getAsMDNode(unwrap(Builder)->getContext()));
1997 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
1998 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2002 /*--.. Instruction builders ................................................--*/
2004 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2005 return wrap(unwrap(B)->CreateRetVoid());
2008 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2009 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2012 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2014 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2017 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2018 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2021 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2022 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2023 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2026 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2027 LLVMBasicBlockRef Else, unsigned NumCases) {
2028 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2031 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2032 unsigned NumDests) {
2033 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2036 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2037 LLVMValueRef *Args, unsigned NumArgs,
2038 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2040 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2041 makeArrayRef(unwrap(Args), NumArgs),
2045 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2046 LLVMValueRef PersFn, unsigned NumClauses,
2048 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2049 cast<Function>(unwrap(PersFn)),
2053 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2054 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2057 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2058 return wrap(unwrap(B)->CreateUnreachable());
2061 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2062 LLVMBasicBlockRef Dest) {
2063 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2066 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2067 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2070 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2071 unwrap<LandingPadInst>(LandingPad)->
2072 addClause(cast<Constant>(unwrap(ClauseVal)));
2075 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2076 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2079 /*--.. Arithmetic ..........................................................--*/
2081 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2083 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2086 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2088 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2091 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2093 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2096 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2098 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2101 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2103 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2106 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2108 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2111 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2113 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2116 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2118 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2121 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2123 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2126 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2128 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2131 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2133 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2136 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2138 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2141 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2143 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2146 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2148 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2151 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2152 LLVMValueRef RHS, const char *Name) {
2153 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2156 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2158 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2161 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2163 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2166 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2168 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2171 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2173 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2176 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2178 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2181 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2183 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2186 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2188 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2191 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2193 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2196 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2198 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2201 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2203 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2206 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2207 LLVMValueRef LHS, LLVMValueRef RHS,
2209 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2210 unwrap(RHS), Name));
2213 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2214 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2217 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2219 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2222 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2224 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2227 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2228 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2231 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2232 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2235 /*--.. Memory ..............................................................--*/
2237 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2239 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2240 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2241 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2242 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2243 ITy, unwrap(Ty), AllocSize,
2244 nullptr, nullptr, "");
2245 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2248 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2249 LLVMValueRef Val, const char *Name) {
2250 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2251 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2252 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2253 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2254 ITy, unwrap(Ty), AllocSize,
2255 unwrap(Val), nullptr, "");
2256 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2259 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2261 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2264 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2265 LLVMValueRef Val, const char *Name) {
2266 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2269 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2270 return wrap(unwrap(B)->Insert(
2271 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2275 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2277 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2280 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2281 LLVMValueRef PointerVal) {
2282 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2285 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2287 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2288 case LLVMAtomicOrderingUnordered: return Unordered;
2289 case LLVMAtomicOrderingMonotonic: return Monotonic;
2290 case LLVMAtomicOrderingAcquire: return Acquire;
2291 case LLVMAtomicOrderingRelease: return Release;
2292 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2293 case LLVMAtomicOrderingSequentiallyConsistent:
2294 return SequentiallyConsistent;
2297 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2300 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2301 LLVMBool isSingleThread, const char *Name) {
2303 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2304 isSingleThread ? SingleThread : CrossThread,
2308 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2309 LLVMValueRef *Indices, unsigned NumIndices,
2311 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2312 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2315 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2316 LLVMValueRef *Indices, unsigned NumIndices,
2318 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2319 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2322 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2323 unsigned Idx, const char *Name) {
2324 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2327 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2329 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2332 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2334 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2337 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2338 Value *P = unwrap<Value>(MemAccessInst);
2339 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2340 return LI->isVolatile();
2341 return cast<StoreInst>(P)->isVolatile();
2344 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2345 Value *P = unwrap<Value>(MemAccessInst);
2346 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2347 return LI->setVolatile(isVolatile);
2348 return cast<StoreInst>(P)->setVolatile(isVolatile);
2351 /*--.. Casts ...............................................................--*/
2353 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2354 LLVMTypeRef DestTy, const char *Name) {
2355 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2358 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2359 LLVMTypeRef DestTy, const char *Name) {
2360 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2363 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2364 LLVMTypeRef DestTy, const char *Name) {
2365 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2368 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2369 LLVMTypeRef DestTy, const char *Name) {
2370 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2373 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2374 LLVMTypeRef DestTy, const char *Name) {
2375 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2378 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2379 LLVMTypeRef DestTy, const char *Name) {
2380 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2383 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2384 LLVMTypeRef DestTy, const char *Name) {
2385 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2388 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2389 LLVMTypeRef DestTy, const char *Name) {
2390 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2393 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2394 LLVMTypeRef DestTy, const char *Name) {
2395 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2398 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2399 LLVMTypeRef DestTy, const char *Name) {
2400 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2403 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2404 LLVMTypeRef DestTy, const char *Name) {
2405 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2408 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2409 LLVMTypeRef DestTy, const char *Name) {
2410 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2413 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2414 LLVMTypeRef DestTy, const char *Name) {
2415 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2418 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2419 LLVMTypeRef DestTy, const char *Name) {
2420 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2424 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2425 LLVMTypeRef DestTy, const char *Name) {
2426 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2430 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2431 LLVMTypeRef DestTy, const char *Name) {
2432 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2436 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2437 LLVMTypeRef DestTy, const char *Name) {
2438 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2439 unwrap(DestTy), Name));
2442 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2443 LLVMTypeRef DestTy, const char *Name) {
2444 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2447 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2448 LLVMTypeRef DestTy, const char *Name) {
2449 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2450 /*isSigned*/true, Name));
2453 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2454 LLVMTypeRef DestTy, const char *Name) {
2455 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2458 /*--.. Comparisons .........................................................--*/
2460 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2461 LLVMValueRef LHS, LLVMValueRef RHS,
2463 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2464 unwrap(LHS), unwrap(RHS), Name));
2467 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2468 LLVMValueRef LHS, LLVMValueRef RHS,
2470 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2471 unwrap(LHS), unwrap(RHS), Name));
2474 /*--.. Miscellaneous instructions ..........................................--*/
2476 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2477 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2480 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2481 LLVMValueRef *Args, unsigned NumArgs,
2483 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2484 makeArrayRef(unwrap(Args), NumArgs),
2488 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2489 LLVMValueRef Then, LLVMValueRef Else,
2491 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2495 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2496 LLVMTypeRef Ty, const char *Name) {
2497 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2500 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2501 LLVMValueRef Index, const char *Name) {
2502 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2506 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2507 LLVMValueRef EltVal, LLVMValueRef Index,
2509 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2510 unwrap(Index), Name));
2513 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2514 LLVMValueRef V2, LLVMValueRef Mask,
2516 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2517 unwrap(Mask), Name));
2520 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2521 unsigned Index, const char *Name) {
2522 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2525 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2526 LLVMValueRef EltVal, unsigned Index,
2528 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2532 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2534 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2537 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2539 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2542 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2543 LLVMValueRef RHS, const char *Name) {
2544 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2547 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2548 LLVMValueRef PTR, LLVMValueRef Val,
2549 LLVMAtomicOrdering ordering,
2550 LLVMBool singleThread) {
2551 AtomicRMWInst::BinOp intop;
2553 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2554 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2555 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2556 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2557 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2558 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2559 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2560 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2561 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2562 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2563 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2565 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2566 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2570 /*===-- Module providers --------------------------------------------------===*/
2572 LLVMModuleProviderRef
2573 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2574 return reinterpret_cast<LLVMModuleProviderRef>(M);
2577 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2582 /*===-- Memory buffers ----------------------------------------------------===*/
2584 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2586 LLVMMemoryBufferRef *OutMemBuf,
2587 char **OutMessage) {
2589 std::unique_ptr<MemoryBuffer> MB;
2591 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2592 *OutMemBuf = wrap(MB.release());
2596 *OutMessage = strdup(ec.message().c_str());
2600 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2601 char **OutMessage) {
2602 std::unique_ptr<MemoryBuffer> MB;
2604 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2605 *OutMemBuf = wrap(MB.release());
2609 *OutMessage = strdup(ec.message().c_str());
2613 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2614 const char *InputData,
2615 size_t InputDataLength,
2616 const char *BufferName,
2617 LLVMBool RequiresNullTerminator) {
2619 return wrap(MemoryBuffer::getMemBuffer(
2620 StringRef(InputData, InputDataLength),
2621 StringRef(BufferName),
2622 RequiresNullTerminator));
2625 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2626 const char *InputData,
2627 size_t InputDataLength,
2628 const char *BufferName) {
2630 return wrap(MemoryBuffer::getMemBufferCopy(
2631 StringRef(InputData, InputDataLength),
2632 StringRef(BufferName)));
2635 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2636 return unwrap(MemBuf)->getBufferStart();
2639 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2640 return unwrap(MemBuf)->getBufferSize();
2643 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2644 delete unwrap(MemBuf);
2647 /*===-- Pass Registry -----------------------------------------------------===*/
2649 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2650 return wrap(PassRegistry::getPassRegistry());
2653 /*===-- Pass Manager ------------------------------------------------------===*/
2655 LLVMPassManagerRef LLVMCreatePassManager() {
2656 return wrap(new PassManager());
2659 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2660 return wrap(new FunctionPassManager(unwrap(M)));
2663 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2664 return LLVMCreateFunctionPassManagerForModule(
2665 reinterpret_cast<LLVMModuleRef>(P));
2668 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2669 return unwrap<PassManager>(PM)->run(*unwrap(M));
2672 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2673 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2676 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2677 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2680 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2681 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2684 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2688 /*===-- Threading ------------------------------------------------------===*/
2690 LLVMBool LLVMStartMultithreaded() {
2691 return llvm_start_multithreaded();
2694 void LLVMStopMultithreaded() {
2695 llvm_stop_multithreaded();
2698 LLVMBool LLVMIsMultithreaded() {
2699 return llvm_is_multithreaded();