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/Pass.h"
31 #include "llvm/PassManager.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/ManagedStatic.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Threading.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Support/system_error.h"
46 #define DEBUG_TYPE "ir"
49 struct LLVMPassRunListener : PassRunListener {
50 LLVMPassRunListenerHandlerTy Callback;
52 LLVMPassRunListener(LLVMContext *Context, LLVMPassRunListenerHandlerTy Fn)
53 : PassRunListener(Context), Callback(Fn) {}
54 void passRun(LLVMContext *C, Pass *P, Module *M, Function *F,
55 BasicBlock *BB) override {
56 Callback(wrap(C), wrap(P), wrap(M), wrap(F), wrap(BB));
59 // Create wrappers for C Binding types (see CBindingWrapping.h).
60 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMPassRunListener, LLVMPassRunListenerRef)
61 } // end anonymous namespace
63 void llvm::initializeCore(PassRegistry &Registry) {
64 initializeDominatorTreeWrapperPassPass(Registry);
65 initializePrintModulePassWrapperPass(Registry);
66 initializePrintFunctionPassWrapperPass(Registry);
67 initializePrintBasicBlockPassPass(Registry);
68 initializeVerifierLegacyPassPass(Registry);
71 void LLVMInitializeCore(LLVMPassRegistryRef R) {
72 initializeCore(*unwrap(R));
79 /*===-- Error handling ----------------------------------------------------===*/
81 char *LLVMCreateMessage(const char *Message) {
82 return strdup(Message);
85 void LLVMDisposeMessage(char *Message) {
90 /*===-- Operations on contexts --------------------------------------------===*/
92 LLVMContextRef LLVMContextCreate() {
93 return wrap(new LLVMContext());
96 LLVMContextRef LLVMGetGlobalContext() {
97 return wrap(&getGlobalContext());
100 void LLVMContextSetDiagnosticHandler(LLVMContextRef C,
101 LLVMDiagnosticHandler Handler,
102 void *DiagnosticContext) {
103 unwrap(C)->setDiagnosticHandler(
104 LLVM_EXTENSION reinterpret_cast<LLVMContext::DiagnosticHandlerTy>(Handler),
108 void LLVMContextDispose(LLVMContextRef C) {
112 unsigned LLVMGetMDKindIDInContext(LLVMContextRef C, const char* Name,
114 return unwrap(C)->getMDKindID(StringRef(Name, SLen));
117 unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) {
118 return LLVMGetMDKindIDInContext(LLVMGetGlobalContext(), Name, SLen);
121 char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) {
122 std::string MsgStorage;
123 raw_string_ostream Stream(MsgStorage);
124 DiagnosticPrinterRawOStream DP(Stream);
126 unwrap(DI)->print(DP);
129 return LLVMCreateMessage(MsgStorage.c_str());
132 LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){
133 LLVMDiagnosticSeverity severity;
135 switch(unwrap(DI)->getSeverity()) {
137 severity = LLVMDSError;
140 severity = LLVMDSWarning;
143 severity = LLVMDSRemark;
146 severity = LLVMDSNote;
153 LLVMPassRunListenerRef LLVMAddPassRunListener(LLVMContextRef Context,
154 LLVMPassRunListenerHandlerTy Fn) {
155 return wrap(new LLVMPassRunListener(unwrap(Context), Fn));
158 void LLVMRemovePassRunListener(LLVMContextRef Context,
159 LLVMPassRunListenerRef Listener) {
160 unwrap(Context)->removeRunListener(unwrap(Listener));
164 /*===-- Operations on modules ---------------------------------------------===*/
166 LLVMModuleRef LLVMModuleCreateWithName(const char *ModuleID) {
167 return wrap(new Module(ModuleID, getGlobalContext()));
170 LLVMModuleRef LLVMModuleCreateWithNameInContext(const char *ModuleID,
172 return wrap(new Module(ModuleID, *unwrap(C)));
175 void LLVMDisposeModule(LLVMModuleRef M) {
179 /*--.. Data layout .........................................................--*/
180 const char * LLVMGetDataLayout(LLVMModuleRef M) {
181 return unwrap(M)->getDataLayoutStr().c_str();
184 void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) {
185 unwrap(M)->setDataLayout(Triple);
188 /*--.. Target triple .......................................................--*/
189 const char * LLVMGetTarget(LLVMModuleRef M) {
190 return unwrap(M)->getTargetTriple().c_str();
193 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
194 unwrap(M)->setTargetTriple(Triple);
197 void LLVMDumpModule(LLVMModuleRef M) {
201 LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
202 char **ErrorMessage) {
204 raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
205 if (!error.empty()) {
206 *ErrorMessage = strdup(error.c_str());
210 unwrap(M)->print(dest, nullptr);
212 if (!error.empty()) {
213 *ErrorMessage = strdup(error.c_str());
220 char *LLVMPrintModuleToString(LLVMModuleRef M) {
222 raw_string_ostream os(buf);
224 unwrap(M)->print(os, nullptr);
227 return strdup(buf.c_str());
230 /*--.. Operations on inline assembler ......................................--*/
231 void LLVMSetModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
232 unwrap(M)->setModuleInlineAsm(StringRef(Asm));
236 /*--.. Operations on module contexts ......................................--*/
237 LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M) {
238 return wrap(&unwrap(M)->getContext());
242 /*===-- Operations on types -----------------------------------------------===*/
244 /*--.. Operations on all types (mostly) ....................................--*/
246 LLVMTypeKind LLVMGetTypeKind(LLVMTypeRef Ty) {
247 switch (unwrap(Ty)->getTypeID()) {
249 return LLVMVoidTypeKind;
251 return LLVMHalfTypeKind;
252 case Type::FloatTyID:
253 return LLVMFloatTypeKind;
254 case Type::DoubleTyID:
255 return LLVMDoubleTypeKind;
256 case Type::X86_FP80TyID:
257 return LLVMX86_FP80TypeKind;
258 case Type::FP128TyID:
259 return LLVMFP128TypeKind;
260 case Type::PPC_FP128TyID:
261 return LLVMPPC_FP128TypeKind;
262 case Type::LabelTyID:
263 return LLVMLabelTypeKind;
264 case Type::MetadataTyID:
265 return LLVMMetadataTypeKind;
266 case Type::IntegerTyID:
267 return LLVMIntegerTypeKind;
268 case Type::FunctionTyID:
269 return LLVMFunctionTypeKind;
270 case Type::StructTyID:
271 return LLVMStructTypeKind;
272 case Type::ArrayTyID:
273 return LLVMArrayTypeKind;
274 case Type::PointerTyID:
275 return LLVMPointerTypeKind;
276 case Type::VectorTyID:
277 return LLVMVectorTypeKind;
278 case Type::X86_MMXTyID:
279 return LLVMX86_MMXTypeKind;
281 llvm_unreachable("Unhandled TypeID.");
284 LLVMBool LLVMTypeIsSized(LLVMTypeRef Ty)
286 return unwrap(Ty)->isSized();
289 LLVMContextRef LLVMGetTypeContext(LLVMTypeRef Ty) {
290 return wrap(&unwrap(Ty)->getContext());
293 void LLVMDumpType(LLVMTypeRef Ty) {
294 return unwrap(Ty)->dump();
297 char *LLVMPrintTypeToString(LLVMTypeRef Ty) {
299 raw_string_ostream os(buf);
301 unwrap(Ty)->print(os);
304 return strdup(buf.c_str());
307 /*--.. Operations on integer types .........................................--*/
309 LLVMTypeRef LLVMInt1TypeInContext(LLVMContextRef C) {
310 return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
312 LLVMTypeRef LLVMInt8TypeInContext(LLVMContextRef C) {
313 return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
315 LLVMTypeRef LLVMInt16TypeInContext(LLVMContextRef C) {
316 return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
318 LLVMTypeRef LLVMInt32TypeInContext(LLVMContextRef C) {
319 return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
321 LLVMTypeRef LLVMInt64TypeInContext(LLVMContextRef C) {
322 return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
324 LLVMTypeRef LLVMIntTypeInContext(LLVMContextRef C, unsigned NumBits) {
325 return wrap(IntegerType::get(*unwrap(C), NumBits));
328 LLVMTypeRef LLVMInt1Type(void) {
329 return LLVMInt1TypeInContext(LLVMGetGlobalContext());
331 LLVMTypeRef LLVMInt8Type(void) {
332 return LLVMInt8TypeInContext(LLVMGetGlobalContext());
334 LLVMTypeRef LLVMInt16Type(void) {
335 return LLVMInt16TypeInContext(LLVMGetGlobalContext());
337 LLVMTypeRef LLVMInt32Type(void) {
338 return LLVMInt32TypeInContext(LLVMGetGlobalContext());
340 LLVMTypeRef LLVMInt64Type(void) {
341 return LLVMInt64TypeInContext(LLVMGetGlobalContext());
343 LLVMTypeRef LLVMIntType(unsigned NumBits) {
344 return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
347 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
348 return unwrap<IntegerType>(IntegerTy)->getBitWidth();
351 /*--.. Operations on real types ............................................--*/
353 LLVMTypeRef LLVMHalfTypeInContext(LLVMContextRef C) {
354 return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
356 LLVMTypeRef LLVMFloatTypeInContext(LLVMContextRef C) {
357 return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
359 LLVMTypeRef LLVMDoubleTypeInContext(LLVMContextRef C) {
360 return (LLVMTypeRef) Type::getDoubleTy(*unwrap(C));
362 LLVMTypeRef LLVMX86FP80TypeInContext(LLVMContextRef C) {
363 return (LLVMTypeRef) Type::getX86_FP80Ty(*unwrap(C));
365 LLVMTypeRef LLVMFP128TypeInContext(LLVMContextRef C) {
366 return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
368 LLVMTypeRef LLVMPPCFP128TypeInContext(LLVMContextRef C) {
369 return (LLVMTypeRef) Type::getPPC_FP128Ty(*unwrap(C));
371 LLVMTypeRef LLVMX86MMXTypeInContext(LLVMContextRef C) {
372 return (LLVMTypeRef) Type::getX86_MMXTy(*unwrap(C));
375 LLVMTypeRef LLVMHalfType(void) {
376 return LLVMHalfTypeInContext(LLVMGetGlobalContext());
378 LLVMTypeRef LLVMFloatType(void) {
379 return LLVMFloatTypeInContext(LLVMGetGlobalContext());
381 LLVMTypeRef LLVMDoubleType(void) {
382 return LLVMDoubleTypeInContext(LLVMGetGlobalContext());
384 LLVMTypeRef LLVMX86FP80Type(void) {
385 return LLVMX86FP80TypeInContext(LLVMGetGlobalContext());
387 LLVMTypeRef LLVMFP128Type(void) {
388 return LLVMFP128TypeInContext(LLVMGetGlobalContext());
390 LLVMTypeRef LLVMPPCFP128Type(void) {
391 return LLVMPPCFP128TypeInContext(LLVMGetGlobalContext());
393 LLVMTypeRef LLVMX86MMXType(void) {
394 return LLVMX86MMXTypeInContext(LLVMGetGlobalContext());
397 /*--.. Operations on function types ........................................--*/
399 LLVMTypeRef LLVMFunctionType(LLVMTypeRef ReturnType,
400 LLVMTypeRef *ParamTypes, unsigned ParamCount,
402 ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
403 return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
406 LLVMBool LLVMIsFunctionVarArg(LLVMTypeRef FunctionTy) {
407 return unwrap<FunctionType>(FunctionTy)->isVarArg();
410 LLVMTypeRef LLVMGetReturnType(LLVMTypeRef FunctionTy) {
411 return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
414 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
415 return unwrap<FunctionType>(FunctionTy)->getNumParams();
418 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
419 FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
420 for (FunctionType::param_iterator I = Ty->param_begin(),
421 E = Ty->param_end(); I != E; ++I)
425 /*--.. Operations on struct types ..........................................--*/
427 LLVMTypeRef LLVMStructTypeInContext(LLVMContextRef C, LLVMTypeRef *ElementTypes,
428 unsigned ElementCount, LLVMBool Packed) {
429 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
430 return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
433 LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
434 unsigned ElementCount, LLVMBool Packed) {
435 return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
436 ElementCount, Packed);
439 LLVMTypeRef LLVMStructCreateNamed(LLVMContextRef C, const char *Name)
441 return wrap(StructType::create(*unwrap(C), Name));
444 const char *LLVMGetStructName(LLVMTypeRef Ty)
446 StructType *Type = unwrap<StructType>(Ty);
447 if (!Type->hasName())
449 return Type->getName().data();
452 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
453 unsigned ElementCount, LLVMBool Packed) {
454 ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
455 unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
458 unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
459 return unwrap<StructType>(StructTy)->getNumElements();
462 void LLVMGetStructElementTypes(LLVMTypeRef StructTy, LLVMTypeRef *Dest) {
463 StructType *Ty = unwrap<StructType>(StructTy);
464 for (StructType::element_iterator I = Ty->element_begin(),
465 E = Ty->element_end(); I != E; ++I)
469 LLVMBool LLVMIsPackedStruct(LLVMTypeRef StructTy) {
470 return unwrap<StructType>(StructTy)->isPacked();
473 LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy) {
474 return unwrap<StructType>(StructTy)->isOpaque();
477 LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
478 return wrap(unwrap(M)->getTypeByName(Name));
481 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
483 LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
484 return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
487 LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
488 return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
491 LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
492 return wrap(VectorType::get(unwrap(ElementType), ElementCount));
495 LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty) {
496 return wrap(unwrap<SequentialType>(Ty)->getElementType());
499 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
500 return unwrap<ArrayType>(ArrayTy)->getNumElements();
503 unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
504 return unwrap<PointerType>(PointerTy)->getAddressSpace();
507 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
508 return unwrap<VectorType>(VectorTy)->getNumElements();
511 /*--.. Operations on other types ...........................................--*/
513 LLVMTypeRef LLVMVoidTypeInContext(LLVMContextRef C) {
514 return wrap(Type::getVoidTy(*unwrap(C)));
516 LLVMTypeRef LLVMLabelTypeInContext(LLVMContextRef C) {
517 return wrap(Type::getLabelTy(*unwrap(C)));
520 LLVMTypeRef LLVMVoidType(void) {
521 return LLVMVoidTypeInContext(LLVMGetGlobalContext());
523 LLVMTypeRef LLVMLabelType(void) {
524 return LLVMLabelTypeInContext(LLVMGetGlobalContext());
527 /*===-- Operations on values ----------------------------------------------===*/
529 /*--.. Operations on all values ............................................--*/
531 LLVMTypeRef LLVMTypeOf(LLVMValueRef Val) {
532 return wrap(unwrap(Val)->getType());
535 const char *LLVMGetValueName(LLVMValueRef Val) {
536 return unwrap(Val)->getName().data();
539 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
540 unwrap(Val)->setName(Name);
543 void LLVMDumpValue(LLVMValueRef Val) {
547 char* LLVMPrintValueToString(LLVMValueRef Val) {
549 raw_string_ostream os(buf);
551 unwrap(Val)->print(os);
554 return strdup(buf.c_str());
557 void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
558 unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
561 int LLVMHasMetadata(LLVMValueRef Inst) {
562 return unwrap<Instruction>(Inst)->hasMetadata();
565 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
566 return wrap(unwrap<Instruction>(Inst)->getMetadata(KindID));
569 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
570 unwrap<Instruction>(Inst)->setMetadata(KindID,
571 MD ? unwrap<MDNode>(MD) : nullptr);
574 /*--.. Conversion functions ................................................--*/
576 #define LLVM_DEFINE_VALUE_CAST(name) \
577 LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
578 return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
581 LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DEFINE_VALUE_CAST)
583 /*--.. Operations on Uses ..................................................--*/
584 LLVMUseRef LLVMGetFirstUse(LLVMValueRef Val) {
585 Value *V = unwrap(Val);
586 Value::use_iterator I = V->use_begin();
587 if (I == V->use_end())
592 LLVMUseRef LLVMGetNextUse(LLVMUseRef U) {
593 Use *Next = unwrap(U)->getNext();
599 LLVMValueRef LLVMGetUser(LLVMUseRef U) {
600 return wrap(unwrap(U)->getUser());
603 LLVMValueRef LLVMGetUsedValue(LLVMUseRef U) {
604 return wrap(unwrap(U)->get());
607 /*--.. Operations on Users .................................................--*/
608 LLVMValueRef LLVMGetOperand(LLVMValueRef Val, unsigned Index) {
609 Value *V = unwrap(Val);
610 if (MDNode *MD = dyn_cast<MDNode>(V))
611 return wrap(MD->getOperand(Index));
612 return wrap(cast<User>(V)->getOperand(Index));
615 void LLVMSetOperand(LLVMValueRef Val, unsigned Index, LLVMValueRef Op) {
616 unwrap<User>(Val)->setOperand(Index, unwrap(Op));
619 int LLVMGetNumOperands(LLVMValueRef Val) {
620 Value *V = unwrap(Val);
621 if (MDNode *MD = dyn_cast<MDNode>(V))
622 return MD->getNumOperands();
623 return cast<User>(V)->getNumOperands();
626 /*--.. Operations on constants of any type .................................--*/
628 LLVMValueRef LLVMConstNull(LLVMTypeRef Ty) {
629 return wrap(Constant::getNullValue(unwrap(Ty)));
632 LLVMValueRef LLVMConstAllOnes(LLVMTypeRef Ty) {
633 return wrap(Constant::getAllOnesValue(unwrap(Ty)));
636 LLVMValueRef LLVMGetUndef(LLVMTypeRef Ty) {
637 return wrap(UndefValue::get(unwrap(Ty)));
640 LLVMBool LLVMIsConstant(LLVMValueRef Ty) {
641 return isa<Constant>(unwrap(Ty));
644 LLVMBool LLVMIsNull(LLVMValueRef Val) {
645 if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
646 return C->isNullValue();
650 LLVMBool LLVMIsUndef(LLVMValueRef Val) {
651 return isa<UndefValue>(unwrap(Val));
654 LLVMValueRef LLVMConstPointerNull(LLVMTypeRef Ty) {
656 wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
659 /*--.. Operations on metadata nodes ........................................--*/
661 LLVMValueRef LLVMMDStringInContext(LLVMContextRef C, const char *Str,
663 return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
666 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
667 return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
670 LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals,
672 return wrap(MDNode::get(*unwrap(C),
673 makeArrayRef(unwrap<Value>(Vals, Count), Count)));
676 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
677 return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
680 const char *LLVMGetMDString(LLVMValueRef V, unsigned* Len) {
681 if (const MDString *S = dyn_cast<MDString>(unwrap(V))) {
682 *Len = S->getString().size();
683 return S->getString().data();
689 unsigned LLVMGetMDNodeNumOperands(LLVMValueRef V)
691 return cast<MDNode>(unwrap(V))->getNumOperands();
694 void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest)
696 const MDNode *N = cast<MDNode>(unwrap(V));
697 const unsigned numOperands = N->getNumOperands();
698 for (unsigned i = 0; i < numOperands; i++)
699 Dest[i] = wrap(N->getOperand(i));
702 unsigned LLVMGetNamedMetadataNumOperands(LLVMModuleRef M, const char* name)
704 if (NamedMDNode *N = unwrap(M)->getNamedMetadata(name)) {
705 return N->getNumOperands();
710 void LLVMGetNamedMetadataOperands(LLVMModuleRef M, const char* name, LLVMValueRef *Dest)
712 NamedMDNode *N = unwrap(M)->getNamedMetadata(name);
715 for (unsigned i=0;i<N->getNumOperands();i++)
716 Dest[i] = wrap(N->getOperand(i));
719 void LLVMAddNamedMetadataOperand(LLVMModuleRef M, const char* name,
722 NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
725 MDNode *Op = Val ? unwrap<MDNode>(Val) : nullptr;
730 /*--.. Operations on scalar constants ......................................--*/
732 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
733 LLVMBool SignExtend) {
734 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
737 LLVMValueRef LLVMConstIntOfArbitraryPrecision(LLVMTypeRef IntTy,
739 const uint64_t Words[]) {
740 IntegerType *Ty = unwrap<IntegerType>(IntTy);
741 return wrap(ConstantInt::get(Ty->getContext(),
742 APInt(Ty->getBitWidth(),
743 makeArrayRef(Words, NumWords))));
746 LLVMValueRef LLVMConstIntOfString(LLVMTypeRef IntTy, const char Str[],
748 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
752 LLVMValueRef LLVMConstIntOfStringAndSize(LLVMTypeRef IntTy, const char Str[],
753 unsigned SLen, uint8_t Radix) {
754 return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
758 LLVMValueRef LLVMConstReal(LLVMTypeRef RealTy, double N) {
759 return wrap(ConstantFP::get(unwrap(RealTy), N));
762 LLVMValueRef LLVMConstRealOfString(LLVMTypeRef RealTy, const char *Text) {
763 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
766 LLVMValueRef LLVMConstRealOfStringAndSize(LLVMTypeRef RealTy, const char Str[],
768 return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
771 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
772 return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
775 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
776 return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
779 /*--.. Operations on composite constants ...................................--*/
781 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
783 LLVMBool DontNullTerminate) {
784 /* Inverted the sense of AddNull because ', 0)' is a
785 better mnemonic for null termination than ', 1)'. */
786 return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
787 DontNullTerminate == 0));
789 LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
790 LLVMValueRef *ConstantVals,
791 unsigned Count, LLVMBool Packed) {
792 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
793 return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
797 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
798 LLVMBool DontNullTerminate) {
799 return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
802 LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
803 LLVMValueRef *ConstantVals, unsigned Length) {
804 ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
805 return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
807 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
809 return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
813 LLVMValueRef LLVMConstNamedStruct(LLVMTypeRef StructTy,
814 LLVMValueRef *ConstantVals,
816 Constant **Elements = unwrap<Constant>(ConstantVals, Count);
817 StructType *Ty = cast<StructType>(unwrap(StructTy));
819 return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
822 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
823 return wrap(ConstantVector::get(makeArrayRef(
824 unwrap<Constant>(ScalarConstantVals, Size), Size)));
827 /*-- Opcode mapping */
829 static LLVMOpcode map_to_llvmopcode(int opcode)
832 default: llvm_unreachable("Unhandled Opcode.");
833 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
834 #include "llvm/IR/Instruction.def"
839 static int map_from_llvmopcode(LLVMOpcode code)
842 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
843 #include "llvm/IR/Instruction.def"
846 llvm_unreachable("Unhandled Opcode.");
849 /*--.. Constant expressions ................................................--*/
851 LLVMOpcode LLVMGetConstOpcode(LLVMValueRef ConstantVal) {
852 return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
855 LLVMValueRef LLVMAlignOf(LLVMTypeRef Ty) {
856 return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
859 LLVMValueRef LLVMSizeOf(LLVMTypeRef Ty) {
860 return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
863 LLVMValueRef LLVMConstNeg(LLVMValueRef ConstantVal) {
864 return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
867 LLVMValueRef LLVMConstNSWNeg(LLVMValueRef ConstantVal) {
868 return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
871 LLVMValueRef LLVMConstNUWNeg(LLVMValueRef ConstantVal) {
872 return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
876 LLVMValueRef LLVMConstFNeg(LLVMValueRef ConstantVal) {
877 return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
880 LLVMValueRef LLVMConstNot(LLVMValueRef ConstantVal) {
881 return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
884 LLVMValueRef LLVMConstAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
885 return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
886 unwrap<Constant>(RHSConstant)));
889 LLVMValueRef LLVMConstNSWAdd(LLVMValueRef LHSConstant,
890 LLVMValueRef RHSConstant) {
891 return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
892 unwrap<Constant>(RHSConstant)));
895 LLVMValueRef LLVMConstNUWAdd(LLVMValueRef LHSConstant,
896 LLVMValueRef RHSConstant) {
897 return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
898 unwrap<Constant>(RHSConstant)));
901 LLVMValueRef LLVMConstFAdd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
902 return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
903 unwrap<Constant>(RHSConstant)));
906 LLVMValueRef LLVMConstSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
907 return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
908 unwrap<Constant>(RHSConstant)));
911 LLVMValueRef LLVMConstNSWSub(LLVMValueRef LHSConstant,
912 LLVMValueRef RHSConstant) {
913 return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
914 unwrap<Constant>(RHSConstant)));
917 LLVMValueRef LLVMConstNUWSub(LLVMValueRef LHSConstant,
918 LLVMValueRef RHSConstant) {
919 return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
920 unwrap<Constant>(RHSConstant)));
923 LLVMValueRef LLVMConstFSub(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
924 return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
925 unwrap<Constant>(RHSConstant)));
928 LLVMValueRef LLVMConstMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
929 return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
930 unwrap<Constant>(RHSConstant)));
933 LLVMValueRef LLVMConstNSWMul(LLVMValueRef LHSConstant,
934 LLVMValueRef RHSConstant) {
935 return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
936 unwrap<Constant>(RHSConstant)));
939 LLVMValueRef LLVMConstNUWMul(LLVMValueRef LHSConstant,
940 LLVMValueRef RHSConstant) {
941 return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
942 unwrap<Constant>(RHSConstant)));
945 LLVMValueRef LLVMConstFMul(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
946 return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
947 unwrap<Constant>(RHSConstant)));
950 LLVMValueRef LLVMConstUDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
951 return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
952 unwrap<Constant>(RHSConstant)));
955 LLVMValueRef LLVMConstSDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
956 return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
957 unwrap<Constant>(RHSConstant)));
960 LLVMValueRef LLVMConstExactSDiv(LLVMValueRef LHSConstant,
961 LLVMValueRef RHSConstant) {
962 return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
963 unwrap<Constant>(RHSConstant)));
966 LLVMValueRef LLVMConstFDiv(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
967 return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
968 unwrap<Constant>(RHSConstant)));
971 LLVMValueRef LLVMConstURem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
972 return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
973 unwrap<Constant>(RHSConstant)));
976 LLVMValueRef LLVMConstSRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
977 return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
978 unwrap<Constant>(RHSConstant)));
981 LLVMValueRef LLVMConstFRem(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
982 return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
983 unwrap<Constant>(RHSConstant)));
986 LLVMValueRef LLVMConstAnd(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
987 return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
988 unwrap<Constant>(RHSConstant)));
991 LLVMValueRef LLVMConstOr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
992 return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
993 unwrap<Constant>(RHSConstant)));
996 LLVMValueRef LLVMConstXor(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
997 return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
998 unwrap<Constant>(RHSConstant)));
1001 LLVMValueRef LLVMConstICmp(LLVMIntPredicate Predicate,
1002 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1003 return wrap(ConstantExpr::getICmp(Predicate,
1004 unwrap<Constant>(LHSConstant),
1005 unwrap<Constant>(RHSConstant)));
1008 LLVMValueRef LLVMConstFCmp(LLVMRealPredicate Predicate,
1009 LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1010 return wrap(ConstantExpr::getFCmp(Predicate,
1011 unwrap<Constant>(LHSConstant),
1012 unwrap<Constant>(RHSConstant)));
1015 LLVMValueRef LLVMConstShl(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1016 return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1017 unwrap<Constant>(RHSConstant)));
1020 LLVMValueRef LLVMConstLShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1021 return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1022 unwrap<Constant>(RHSConstant)));
1025 LLVMValueRef LLVMConstAShr(LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1026 return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1027 unwrap<Constant>(RHSConstant)));
1030 LLVMValueRef LLVMConstGEP(LLVMValueRef ConstantVal,
1031 LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1032 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1034 return wrap(ConstantExpr::getGetElementPtr(unwrap<Constant>(ConstantVal),
1038 LLVMValueRef LLVMConstInBoundsGEP(LLVMValueRef ConstantVal,
1039 LLVMValueRef *ConstantIndices,
1040 unsigned NumIndices) {
1041 Constant* Val = unwrap<Constant>(ConstantVal);
1042 ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1044 return wrap(ConstantExpr::getInBoundsGetElementPtr(Val, IdxList));
1047 LLVMValueRef LLVMConstTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1048 return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1052 LLVMValueRef LLVMConstSExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1053 return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1057 LLVMValueRef LLVMConstZExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1058 return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1062 LLVMValueRef LLVMConstFPTrunc(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1063 return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1067 LLVMValueRef LLVMConstFPExt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1068 return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1072 LLVMValueRef LLVMConstUIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1073 return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1077 LLVMValueRef LLVMConstSIToFP(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1078 return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1082 LLVMValueRef LLVMConstFPToUI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1083 return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1087 LLVMValueRef LLVMConstFPToSI(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1088 return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1092 LLVMValueRef LLVMConstPtrToInt(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1093 return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1097 LLVMValueRef LLVMConstIntToPtr(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1098 return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1102 LLVMValueRef LLVMConstBitCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1103 return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1107 LLVMValueRef LLVMConstAddrSpaceCast(LLVMValueRef ConstantVal,
1108 LLVMTypeRef ToType) {
1109 return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1113 LLVMValueRef LLVMConstZExtOrBitCast(LLVMValueRef ConstantVal,
1114 LLVMTypeRef ToType) {
1115 return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1119 LLVMValueRef LLVMConstSExtOrBitCast(LLVMValueRef ConstantVal,
1120 LLVMTypeRef ToType) {
1121 return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1125 LLVMValueRef LLVMConstTruncOrBitCast(LLVMValueRef ConstantVal,
1126 LLVMTypeRef ToType) {
1127 return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1131 LLVMValueRef LLVMConstPointerCast(LLVMValueRef ConstantVal,
1132 LLVMTypeRef ToType) {
1133 return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1137 LLVMValueRef LLVMConstIntCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType,
1138 LLVMBool isSigned) {
1139 return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1140 unwrap(ToType), isSigned));
1143 LLVMValueRef LLVMConstFPCast(LLVMValueRef ConstantVal, LLVMTypeRef ToType) {
1144 return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1148 LLVMValueRef LLVMConstSelect(LLVMValueRef ConstantCondition,
1149 LLVMValueRef ConstantIfTrue,
1150 LLVMValueRef ConstantIfFalse) {
1151 return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1152 unwrap<Constant>(ConstantIfTrue),
1153 unwrap<Constant>(ConstantIfFalse)));
1156 LLVMValueRef LLVMConstExtractElement(LLVMValueRef VectorConstant,
1157 LLVMValueRef IndexConstant) {
1158 return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1159 unwrap<Constant>(IndexConstant)));
1162 LLVMValueRef LLVMConstInsertElement(LLVMValueRef VectorConstant,
1163 LLVMValueRef ElementValueConstant,
1164 LLVMValueRef IndexConstant) {
1165 return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1166 unwrap<Constant>(ElementValueConstant),
1167 unwrap<Constant>(IndexConstant)));
1170 LLVMValueRef LLVMConstShuffleVector(LLVMValueRef VectorAConstant,
1171 LLVMValueRef VectorBConstant,
1172 LLVMValueRef MaskConstant) {
1173 return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1174 unwrap<Constant>(VectorBConstant),
1175 unwrap<Constant>(MaskConstant)));
1178 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1180 return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1181 makeArrayRef(IdxList, NumIdx)));
1184 LLVMValueRef LLVMConstInsertValue(LLVMValueRef AggConstant,
1185 LLVMValueRef ElementValueConstant,
1186 unsigned *IdxList, unsigned NumIdx) {
1187 return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1188 unwrap<Constant>(ElementValueConstant),
1189 makeArrayRef(IdxList, NumIdx)));
1192 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1193 const char *Constraints,
1194 LLVMBool HasSideEffects,
1195 LLVMBool IsAlignStack) {
1196 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1197 Constraints, HasSideEffects, IsAlignStack));
1200 LLVMValueRef LLVMBlockAddress(LLVMValueRef F, LLVMBasicBlockRef BB) {
1201 return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1204 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1206 LLVMModuleRef LLVMGetGlobalParent(LLVMValueRef Global) {
1207 return wrap(unwrap<GlobalValue>(Global)->getParent());
1210 LLVMBool LLVMIsDeclaration(LLVMValueRef Global) {
1211 return unwrap<GlobalValue>(Global)->isDeclaration();
1214 LLVMLinkage LLVMGetLinkage(LLVMValueRef Global) {
1215 switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1216 case GlobalValue::ExternalLinkage:
1217 return LLVMExternalLinkage;
1218 case GlobalValue::AvailableExternallyLinkage:
1219 return LLVMAvailableExternallyLinkage;
1220 case GlobalValue::LinkOnceAnyLinkage:
1221 return LLVMLinkOnceAnyLinkage;
1222 case GlobalValue::LinkOnceODRLinkage:
1223 return LLVMLinkOnceODRLinkage;
1224 case GlobalValue::WeakAnyLinkage:
1225 return LLVMWeakAnyLinkage;
1226 case GlobalValue::WeakODRLinkage:
1227 return LLVMWeakODRLinkage;
1228 case GlobalValue::AppendingLinkage:
1229 return LLVMAppendingLinkage;
1230 case GlobalValue::InternalLinkage:
1231 return LLVMInternalLinkage;
1232 case GlobalValue::PrivateLinkage:
1233 return LLVMPrivateLinkage;
1234 case GlobalValue::ExternalWeakLinkage:
1235 return LLVMExternalWeakLinkage;
1236 case GlobalValue::CommonLinkage:
1237 return LLVMCommonLinkage;
1240 llvm_unreachable("Invalid GlobalValue linkage!");
1243 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1244 GlobalValue *GV = unwrap<GlobalValue>(Global);
1247 case LLVMExternalLinkage:
1248 GV->setLinkage(GlobalValue::ExternalLinkage);
1250 case LLVMAvailableExternallyLinkage:
1251 GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
1253 case LLVMLinkOnceAnyLinkage:
1254 GV->setLinkage(GlobalValue::LinkOnceAnyLinkage);
1256 case LLVMLinkOnceODRLinkage:
1257 GV->setLinkage(GlobalValue::LinkOnceODRLinkage);
1259 case LLVMLinkOnceODRAutoHideLinkage:
1260 DEBUG(errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1261 "longer supported.");
1263 case LLVMWeakAnyLinkage:
1264 GV->setLinkage(GlobalValue::WeakAnyLinkage);
1266 case LLVMWeakODRLinkage:
1267 GV->setLinkage(GlobalValue::WeakODRLinkage);
1269 case LLVMAppendingLinkage:
1270 GV->setLinkage(GlobalValue::AppendingLinkage);
1272 case LLVMInternalLinkage:
1273 GV->setLinkage(GlobalValue::InternalLinkage);
1275 case LLVMPrivateLinkage:
1276 GV->setLinkage(GlobalValue::PrivateLinkage);
1278 case LLVMLinkerPrivateLinkage:
1279 GV->setLinkage(GlobalValue::PrivateLinkage);
1281 case LLVMLinkerPrivateWeakLinkage:
1282 GV->setLinkage(GlobalValue::PrivateLinkage);
1284 case LLVMDLLImportLinkage:
1286 << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1288 case LLVMDLLExportLinkage:
1290 << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1292 case LLVMExternalWeakLinkage:
1293 GV->setLinkage(GlobalValue::ExternalWeakLinkage);
1295 case LLVMGhostLinkage:
1297 << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
1299 case LLVMCommonLinkage:
1300 GV->setLinkage(GlobalValue::CommonLinkage);
1305 const char *LLVMGetSection(LLVMValueRef Global) {
1306 return unwrap<GlobalValue>(Global)->getSection().c_str();
1309 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
1310 unwrap<GlobalValue>(Global)->setSection(Section);
1313 LLVMVisibility LLVMGetVisibility(LLVMValueRef Global) {
1314 return static_cast<LLVMVisibility>(
1315 unwrap<GlobalValue>(Global)->getVisibility());
1318 void LLVMSetVisibility(LLVMValueRef Global, LLVMVisibility Viz) {
1319 unwrap<GlobalValue>(Global)
1320 ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
1323 LLVMDLLStorageClass LLVMGetDLLStorageClass(LLVMValueRef Global) {
1324 return static_cast<LLVMDLLStorageClass>(
1325 unwrap<GlobalValue>(Global)->getDLLStorageClass());
1328 void LLVMSetDLLStorageClass(LLVMValueRef Global, LLVMDLLStorageClass Class) {
1329 unwrap<GlobalValue>(Global)->setDLLStorageClass(
1330 static_cast<GlobalValue::DLLStorageClassTypes>(Class));
1333 LLVMBool LLVMHasUnnamedAddr(LLVMValueRef Global) {
1334 return unwrap<GlobalValue>(Global)->hasUnnamedAddr();
1337 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
1338 unwrap<GlobalValue>(Global)->setUnnamedAddr(HasUnnamedAddr);
1341 /*--.. Operations on global variables, load and store instructions .........--*/
1343 unsigned LLVMGetAlignment(LLVMValueRef V) {
1344 Value *P = unwrap<Value>(V);
1345 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1346 return GV->getAlignment();
1347 if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1348 return AI->getAlignment();
1349 if (LoadInst *LI = dyn_cast<LoadInst>(P))
1350 return LI->getAlignment();
1351 if (StoreInst *SI = dyn_cast<StoreInst>(P))
1352 return SI->getAlignment();
1355 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1358 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
1359 Value *P = unwrap<Value>(V);
1360 if (GlobalValue *GV = dyn_cast<GlobalValue>(P))
1361 GV->setAlignment(Bytes);
1362 else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
1363 AI->setAlignment(Bytes);
1364 else if (LoadInst *LI = dyn_cast<LoadInst>(P))
1365 LI->setAlignment(Bytes);
1366 else if (StoreInst *SI = dyn_cast<StoreInst>(P))
1367 SI->setAlignment(Bytes);
1370 "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment");
1373 /*--.. Operations on global variables ......................................--*/
1375 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
1376 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1377 GlobalValue::ExternalLinkage, nullptr, Name));
1380 LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
1382 unsigned AddressSpace) {
1383 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
1384 GlobalValue::ExternalLinkage, nullptr, Name,
1385 nullptr, GlobalVariable::NotThreadLocal,
1389 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
1390 return wrap(unwrap(M)->getNamedGlobal(Name));
1393 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
1394 Module *Mod = unwrap(M);
1395 Module::global_iterator I = Mod->global_begin();
1396 if (I == Mod->global_end())
1401 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M) {
1402 Module *Mod = unwrap(M);
1403 Module::global_iterator I = Mod->global_end();
1404 if (I == Mod->global_begin())
1409 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar) {
1410 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1411 Module::global_iterator I = GV;
1412 if (++I == GV->getParent()->global_end())
1417 LLVMValueRef LLVMGetPreviousGlobal(LLVMValueRef GlobalVar) {
1418 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1419 Module::global_iterator I = GV;
1420 if (I == GV->getParent()->global_begin())
1425 void LLVMDeleteGlobal(LLVMValueRef GlobalVar) {
1426 unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
1429 LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
1430 GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
1431 if ( !GV->hasInitializer() )
1433 return wrap(GV->getInitializer());
1436 void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
1437 unwrap<GlobalVariable>(GlobalVar)
1438 ->setInitializer(unwrap<Constant>(ConstantVal));
1441 LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
1442 return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
1445 void LLVMSetThreadLocal(LLVMValueRef GlobalVar, LLVMBool IsThreadLocal) {
1446 unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
1449 LLVMBool LLVMIsGlobalConstant(LLVMValueRef GlobalVar) {
1450 return unwrap<GlobalVariable>(GlobalVar)->isConstant();
1453 void LLVMSetGlobalConstant(LLVMValueRef GlobalVar, LLVMBool IsConstant) {
1454 unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
1457 LLVMThreadLocalMode LLVMGetThreadLocalMode(LLVMValueRef GlobalVar) {
1458 switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
1459 case GlobalVariable::NotThreadLocal:
1460 return LLVMNotThreadLocal;
1461 case GlobalVariable::GeneralDynamicTLSModel:
1462 return LLVMGeneralDynamicTLSModel;
1463 case GlobalVariable::LocalDynamicTLSModel:
1464 return LLVMLocalDynamicTLSModel;
1465 case GlobalVariable::InitialExecTLSModel:
1466 return LLVMInitialExecTLSModel;
1467 case GlobalVariable::LocalExecTLSModel:
1468 return LLVMLocalExecTLSModel;
1471 llvm_unreachable("Invalid GlobalVariable thread local mode");
1474 void LLVMSetThreadLocalMode(LLVMValueRef GlobalVar, LLVMThreadLocalMode Mode) {
1475 GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
1478 case LLVMNotThreadLocal:
1479 GV->setThreadLocalMode(GlobalVariable::NotThreadLocal);
1481 case LLVMGeneralDynamicTLSModel:
1482 GV->setThreadLocalMode(GlobalVariable::GeneralDynamicTLSModel);
1484 case LLVMLocalDynamicTLSModel:
1485 GV->setThreadLocalMode(GlobalVariable::LocalDynamicTLSModel);
1487 case LLVMInitialExecTLSModel:
1488 GV->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
1490 case LLVMLocalExecTLSModel:
1491 GV->setThreadLocalMode(GlobalVariable::LocalExecTLSModel);
1496 LLVMBool LLVMIsExternallyInitialized(LLVMValueRef GlobalVar) {
1497 return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
1500 void LLVMSetExternallyInitialized(LLVMValueRef GlobalVar, LLVMBool IsExtInit) {
1501 unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
1504 /*--.. Operations on aliases ......................................--*/
1506 LLVMValueRef LLVMAddAlias(LLVMModuleRef M, LLVMTypeRef Ty, LLVMValueRef Aliasee,
1508 return wrap(new GlobalAlias(unwrap(Ty), GlobalValue::ExternalLinkage, Name,
1509 unwrap<Constant>(Aliasee), unwrap (M)));
1512 /*--.. Operations on functions .............................................--*/
1514 LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
1515 LLVMTypeRef FunctionTy) {
1516 return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
1517 GlobalValue::ExternalLinkage, Name, unwrap(M)));
1520 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
1521 return wrap(unwrap(M)->getFunction(Name));
1524 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
1525 Module *Mod = unwrap(M);
1526 Module::iterator I = Mod->begin();
1527 if (I == Mod->end())
1532 LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
1533 Module *Mod = unwrap(M);
1534 Module::iterator I = Mod->end();
1535 if (I == Mod->begin())
1540 LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
1541 Function *Func = unwrap<Function>(Fn);
1542 Module::iterator I = Func;
1543 if (++I == Func->getParent()->end())
1548 LLVMValueRef LLVMGetPreviousFunction(LLVMValueRef Fn) {
1549 Function *Func = unwrap<Function>(Fn);
1550 Module::iterator I = Func;
1551 if (I == Func->getParent()->begin())
1556 void LLVMDeleteFunction(LLVMValueRef Fn) {
1557 unwrap<Function>(Fn)->eraseFromParent();
1560 unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
1561 if (Function *F = dyn_cast<Function>(unwrap(Fn)))
1562 return F->getIntrinsicID();
1566 unsigned LLVMGetFunctionCallConv(LLVMValueRef Fn) {
1567 return unwrap<Function>(Fn)->getCallingConv();
1570 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
1571 return unwrap<Function>(Fn)->setCallingConv(
1572 static_cast<CallingConv::ID>(CC));
1575 const char *LLVMGetGC(LLVMValueRef Fn) {
1576 Function *F = unwrap<Function>(Fn);
1577 return F->hasGC()? F->getGC() : nullptr;
1580 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
1581 Function *F = unwrap<Function>(Fn);
1588 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1589 Function *Func = unwrap<Function>(Fn);
1590 const AttributeSet PAL = Func->getAttributes();
1592 const AttributeSet PALnew =
1593 PAL.addAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1594 AttributeSet::get(Func->getContext(),
1595 AttributeSet::FunctionIndex, B));
1596 Func->setAttributes(PALnew);
1599 void LLVMAddTargetDependentFunctionAttr(LLVMValueRef Fn, const char *A,
1601 Function *Func = unwrap<Function>(Fn);
1602 AttributeSet::AttrIndex Idx =
1603 AttributeSet::AttrIndex(AttributeSet::FunctionIndex);
1606 B.addAttribute(A, V);
1607 AttributeSet Set = AttributeSet::get(Func->getContext(), Idx, B);
1608 Func->addAttributes(Idx, Set);
1611 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
1612 Function *Func = unwrap<Function>(Fn);
1613 const AttributeSet PAL = Func->getAttributes();
1615 const AttributeSet PALnew =
1616 PAL.removeAttributes(Func->getContext(), AttributeSet::FunctionIndex,
1617 AttributeSet::get(Func->getContext(),
1618 AttributeSet::FunctionIndex, B));
1619 Func->setAttributes(PALnew);
1622 LLVMAttribute LLVMGetFunctionAttr(LLVMValueRef Fn) {
1623 Function *Func = unwrap<Function>(Fn);
1624 const AttributeSet PAL = Func->getAttributes();
1625 return (LLVMAttribute)PAL.Raw(AttributeSet::FunctionIndex);
1628 /*--.. Operations on parameters ............................................--*/
1630 unsigned LLVMCountParams(LLVMValueRef FnRef) {
1631 // This function is strictly redundant to
1632 // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
1633 return unwrap<Function>(FnRef)->arg_size();
1636 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
1637 Function *Fn = unwrap<Function>(FnRef);
1638 for (Function::arg_iterator I = Fn->arg_begin(),
1639 E = Fn->arg_end(); I != E; I++)
1640 *ParamRefs++ = wrap(I);
1643 LLVMValueRef LLVMGetParam(LLVMValueRef FnRef, unsigned index) {
1644 Function::arg_iterator AI = unwrap<Function>(FnRef)->arg_begin();
1650 LLVMValueRef LLVMGetParamParent(LLVMValueRef V) {
1651 return wrap(unwrap<Argument>(V)->getParent());
1654 LLVMValueRef LLVMGetFirstParam(LLVMValueRef Fn) {
1655 Function *Func = unwrap<Function>(Fn);
1656 Function::arg_iterator I = Func->arg_begin();
1657 if (I == Func->arg_end())
1662 LLVMValueRef LLVMGetLastParam(LLVMValueRef Fn) {
1663 Function *Func = unwrap<Function>(Fn);
1664 Function::arg_iterator I = Func->arg_end();
1665 if (I == Func->arg_begin())
1670 LLVMValueRef LLVMGetNextParam(LLVMValueRef Arg) {
1671 Argument *A = unwrap<Argument>(Arg);
1672 Function::arg_iterator I = A;
1673 if (++I == A->getParent()->arg_end())
1678 LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) {
1679 Argument *A = unwrap<Argument>(Arg);
1680 Function::arg_iterator I = A;
1681 if (I == A->getParent()->arg_begin())
1686 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1687 Argument *A = unwrap<Argument>(Arg);
1689 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1692 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
1693 Argument *A = unwrap<Argument>(Arg);
1695 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
1698 LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) {
1699 Argument *A = unwrap<Argument>(Arg);
1700 return (LLVMAttribute)A->getParent()->getAttributes().
1701 Raw(A->getArgNo()+1);
1705 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1706 Argument *A = unwrap<Argument>(Arg);
1708 B.addAlignmentAttr(align);
1709 A->addAttr(AttributeSet::get(A->getContext(),A->getArgNo() + 1, B));
1712 /*--.. Operations on basic blocks ..........................................--*/
1714 LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) {
1715 return wrap(static_cast<Value*>(unwrap(BB)));
1718 LLVMBool LLVMValueIsBasicBlock(LLVMValueRef Val) {
1719 return isa<BasicBlock>(unwrap(Val));
1722 LLVMBasicBlockRef LLVMValueAsBasicBlock(LLVMValueRef Val) {
1723 return wrap(unwrap<BasicBlock>(Val));
1726 LLVMValueRef LLVMGetBasicBlockParent(LLVMBasicBlockRef BB) {
1727 return wrap(unwrap(BB)->getParent());
1730 LLVMValueRef LLVMGetBasicBlockTerminator(LLVMBasicBlockRef BB) {
1731 return wrap(unwrap(BB)->getTerminator());
1734 unsigned LLVMCountBasicBlocks(LLVMValueRef FnRef) {
1735 return unwrap<Function>(FnRef)->size();
1738 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
1739 Function *Fn = unwrap<Function>(FnRef);
1740 for (Function::iterator I = Fn->begin(), E = Fn->end(); I != E; I++)
1741 *BasicBlocksRefs++ = wrap(I);
1744 LLVMBasicBlockRef LLVMGetEntryBasicBlock(LLVMValueRef Fn) {
1745 return wrap(&unwrap<Function>(Fn)->getEntryBlock());
1748 LLVMBasicBlockRef LLVMGetFirstBasicBlock(LLVMValueRef Fn) {
1749 Function *Func = unwrap<Function>(Fn);
1750 Function::iterator I = Func->begin();
1751 if (I == Func->end())
1756 LLVMBasicBlockRef LLVMGetLastBasicBlock(LLVMValueRef Fn) {
1757 Function *Func = unwrap<Function>(Fn);
1758 Function::iterator I = Func->end();
1759 if (I == Func->begin())
1764 LLVMBasicBlockRef LLVMGetNextBasicBlock(LLVMBasicBlockRef BB) {
1765 BasicBlock *Block = unwrap(BB);
1766 Function::iterator I = Block;
1767 if (++I == Block->getParent()->end())
1772 LLVMBasicBlockRef LLVMGetPreviousBasicBlock(LLVMBasicBlockRef BB) {
1773 BasicBlock *Block = unwrap(BB);
1774 Function::iterator I = Block;
1775 if (I == Block->getParent()->begin())
1780 LLVMBasicBlockRef LLVMAppendBasicBlockInContext(LLVMContextRef C,
1783 return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
1786 LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef FnRef, const char *Name) {
1787 return LLVMAppendBasicBlockInContext(LLVMGetGlobalContext(), FnRef, Name);
1790 LLVMBasicBlockRef LLVMInsertBasicBlockInContext(LLVMContextRef C,
1791 LLVMBasicBlockRef BBRef,
1793 BasicBlock *BB = unwrap(BBRef);
1794 return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
1797 LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef BBRef,
1799 return LLVMInsertBasicBlockInContext(LLVMGetGlobalContext(), BBRef, Name);
1802 void LLVMDeleteBasicBlock(LLVMBasicBlockRef BBRef) {
1803 unwrap(BBRef)->eraseFromParent();
1806 void LLVMRemoveBasicBlockFromParent(LLVMBasicBlockRef BBRef) {
1807 unwrap(BBRef)->removeFromParent();
1810 void LLVMMoveBasicBlockBefore(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1811 unwrap(BB)->moveBefore(unwrap(MovePos));
1814 void LLVMMoveBasicBlockAfter(LLVMBasicBlockRef BB, LLVMBasicBlockRef MovePos) {
1815 unwrap(BB)->moveAfter(unwrap(MovePos));
1818 /*--.. Operations on instructions ..........................................--*/
1820 LLVMBasicBlockRef LLVMGetInstructionParent(LLVMValueRef Inst) {
1821 return wrap(unwrap<Instruction>(Inst)->getParent());
1824 LLVMValueRef LLVMGetFirstInstruction(LLVMBasicBlockRef BB) {
1825 BasicBlock *Block = unwrap(BB);
1826 BasicBlock::iterator I = Block->begin();
1827 if (I == Block->end())
1832 LLVMValueRef LLVMGetLastInstruction(LLVMBasicBlockRef BB) {
1833 BasicBlock *Block = unwrap(BB);
1834 BasicBlock::iterator I = Block->end();
1835 if (I == Block->begin())
1840 LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst) {
1841 Instruction *Instr = unwrap<Instruction>(Inst);
1842 BasicBlock::iterator I = Instr;
1843 if (++I == Instr->getParent()->end())
1848 LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst) {
1849 Instruction *Instr = unwrap<Instruction>(Inst);
1850 BasicBlock::iterator I = Instr;
1851 if (I == Instr->getParent()->begin())
1856 void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
1857 unwrap<Instruction>(Inst)->eraseFromParent();
1860 LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
1861 if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
1862 return (LLVMIntPredicate)I->getPredicate();
1863 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
1864 if (CE->getOpcode() == Instruction::ICmp)
1865 return (LLVMIntPredicate)CE->getPredicate();
1866 return (LLVMIntPredicate)0;
1869 LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst) {
1870 if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
1871 return map_to_llvmopcode(C->getOpcode());
1872 return (LLVMOpcode)0;
1875 /*--.. Call and invoke instructions ........................................--*/
1877 unsigned LLVMGetInstructionCallConv(LLVMValueRef Instr) {
1878 Value *V = unwrap(Instr);
1879 if (CallInst *CI = dyn_cast<CallInst>(V))
1880 return CI->getCallingConv();
1881 if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1882 return II->getCallingConv();
1883 llvm_unreachable("LLVMGetInstructionCallConv applies only to call and invoke!");
1886 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
1887 Value *V = unwrap(Instr);
1888 if (CallInst *CI = dyn_cast<CallInst>(V))
1889 return CI->setCallingConv(static_cast<CallingConv::ID>(CC));
1890 else if (InvokeInst *II = dyn_cast<InvokeInst>(V))
1891 return II->setCallingConv(static_cast<CallingConv::ID>(CC));
1892 llvm_unreachable("LLVMSetInstructionCallConv applies only to call and invoke!");
1895 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
1897 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1900 Call.getAttributes().addAttributes(Call->getContext(), index,
1901 AttributeSet::get(Call->getContext(),
1905 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
1907 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1909 Call.setAttributes(Call.getAttributes()
1910 .removeAttributes(Call->getContext(), index,
1911 AttributeSet::get(Call->getContext(),
1915 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
1917 CallSite Call = CallSite(unwrap<Instruction>(Instr));
1919 B.addAlignmentAttr(align);
1920 Call.setAttributes(Call.getAttributes()
1921 .addAttributes(Call->getContext(), index,
1922 AttributeSet::get(Call->getContext(),
1926 /*--.. Operations on call instructions (only) ..............................--*/
1928 LLVMBool LLVMIsTailCall(LLVMValueRef Call) {
1929 return unwrap<CallInst>(Call)->isTailCall();
1932 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
1933 unwrap<CallInst>(Call)->setTailCall(isTailCall);
1936 /*--.. Operations on switch instructions (only) ............................--*/
1938 LLVMBasicBlockRef LLVMGetSwitchDefaultDest(LLVMValueRef Switch) {
1939 return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
1942 /*--.. Operations on phi nodes .............................................--*/
1944 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
1945 LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
1946 PHINode *PhiVal = unwrap<PHINode>(PhiNode);
1947 for (unsigned I = 0; I != Count; ++I)
1948 PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
1951 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
1952 return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
1955 LLVMValueRef LLVMGetIncomingValue(LLVMValueRef PhiNode, unsigned Index) {
1956 return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
1959 LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
1960 return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
1964 /*===-- Instruction builders ----------------------------------------------===*/
1966 LLVMBuilderRef LLVMCreateBuilderInContext(LLVMContextRef C) {
1967 return wrap(new IRBuilder<>(*unwrap(C)));
1970 LLVMBuilderRef LLVMCreateBuilder(void) {
1971 return LLVMCreateBuilderInContext(LLVMGetGlobalContext());
1974 void LLVMPositionBuilder(LLVMBuilderRef Builder, LLVMBasicBlockRef Block,
1975 LLVMValueRef Instr) {
1976 BasicBlock *BB = unwrap(Block);
1977 Instruction *I = Instr? unwrap<Instruction>(Instr) : (Instruction*) BB->end();
1978 unwrap(Builder)->SetInsertPoint(BB, I);
1981 void LLVMPositionBuilderBefore(LLVMBuilderRef Builder, LLVMValueRef Instr) {
1982 Instruction *I = unwrap<Instruction>(Instr);
1983 unwrap(Builder)->SetInsertPoint(I->getParent(), I);
1986 void LLVMPositionBuilderAtEnd(LLVMBuilderRef Builder, LLVMBasicBlockRef Block) {
1987 BasicBlock *BB = unwrap(Block);
1988 unwrap(Builder)->SetInsertPoint(BB);
1991 LLVMBasicBlockRef LLVMGetInsertBlock(LLVMBuilderRef Builder) {
1992 return wrap(unwrap(Builder)->GetInsertBlock());
1995 void LLVMClearInsertionPosition(LLVMBuilderRef Builder) {
1996 unwrap(Builder)->ClearInsertionPoint();
1999 void LLVMInsertIntoBuilder(LLVMBuilderRef Builder, LLVMValueRef Instr) {
2000 unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
2003 void LLVMInsertIntoBuilderWithName(LLVMBuilderRef Builder, LLVMValueRef Instr,
2005 unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
2008 void LLVMDisposeBuilder(LLVMBuilderRef Builder) {
2009 delete unwrap(Builder);
2012 /*--.. Metadata builders ...................................................--*/
2014 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
2015 MDNode *Loc = L ? unwrap<MDNode>(L) : nullptr;
2016 unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
2019 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
2020 return wrap(unwrap(Builder)->getCurrentDebugLocation()
2021 .getAsMDNode(unwrap(Builder)->getContext()));
2024 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {
2025 unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
2029 /*--.. Instruction builders ................................................--*/
2031 LLVMValueRef LLVMBuildRetVoid(LLVMBuilderRef B) {
2032 return wrap(unwrap(B)->CreateRetVoid());
2035 LLVMValueRef LLVMBuildRet(LLVMBuilderRef B, LLVMValueRef V) {
2036 return wrap(unwrap(B)->CreateRet(unwrap(V)));
2039 LLVMValueRef LLVMBuildAggregateRet(LLVMBuilderRef B, LLVMValueRef *RetVals,
2041 return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
2044 LLVMValueRef LLVMBuildBr(LLVMBuilderRef B, LLVMBasicBlockRef Dest) {
2045 return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
2048 LLVMValueRef LLVMBuildCondBr(LLVMBuilderRef B, LLVMValueRef If,
2049 LLVMBasicBlockRef Then, LLVMBasicBlockRef Else) {
2050 return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
2053 LLVMValueRef LLVMBuildSwitch(LLVMBuilderRef B, LLVMValueRef V,
2054 LLVMBasicBlockRef Else, unsigned NumCases) {
2055 return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
2058 LLVMValueRef LLVMBuildIndirectBr(LLVMBuilderRef B, LLVMValueRef Addr,
2059 unsigned NumDests) {
2060 return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
2063 LLVMValueRef LLVMBuildInvoke(LLVMBuilderRef B, LLVMValueRef Fn,
2064 LLVMValueRef *Args, unsigned NumArgs,
2065 LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch,
2067 return wrap(unwrap(B)->CreateInvoke(unwrap(Fn), unwrap(Then), unwrap(Catch),
2068 makeArrayRef(unwrap(Args), NumArgs),
2072 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
2073 LLVMValueRef PersFn, unsigned NumClauses,
2075 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
2076 cast<Function>(unwrap(PersFn)),
2080 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
2081 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
2084 LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef B) {
2085 return wrap(unwrap(B)->CreateUnreachable());
2088 void LLVMAddCase(LLVMValueRef Switch, LLVMValueRef OnVal,
2089 LLVMBasicBlockRef Dest) {
2090 unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
2093 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
2094 unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
2097 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
2098 unwrap<LandingPadInst>(LandingPad)->
2099 addClause(cast<Constant>(unwrap(ClauseVal)));
2102 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
2103 unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
2106 /*--.. Arithmetic ..........................................................--*/
2108 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2110 return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
2113 LLVMValueRef LLVMBuildNSWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2115 return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
2118 LLVMValueRef LLVMBuildNUWAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2120 return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
2123 LLVMValueRef LLVMBuildFAdd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2125 return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
2128 LLVMValueRef LLVMBuildSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2130 return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
2133 LLVMValueRef LLVMBuildNSWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2135 return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
2138 LLVMValueRef LLVMBuildNUWSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2140 return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
2143 LLVMValueRef LLVMBuildFSub(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2145 return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
2148 LLVMValueRef LLVMBuildMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2150 return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
2153 LLVMValueRef LLVMBuildNSWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2155 return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
2158 LLVMValueRef LLVMBuildNUWMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2160 return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
2163 LLVMValueRef LLVMBuildFMul(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2165 return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
2168 LLVMValueRef LLVMBuildUDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2170 return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
2173 LLVMValueRef LLVMBuildSDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2175 return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
2178 LLVMValueRef LLVMBuildExactSDiv(LLVMBuilderRef B, LLVMValueRef LHS,
2179 LLVMValueRef RHS, const char *Name) {
2180 return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
2183 LLVMValueRef LLVMBuildFDiv(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2185 return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
2188 LLVMValueRef LLVMBuildURem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2190 return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
2193 LLVMValueRef LLVMBuildSRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2195 return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
2198 LLVMValueRef LLVMBuildFRem(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2200 return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
2203 LLVMValueRef LLVMBuildShl(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2205 return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
2208 LLVMValueRef LLVMBuildLShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2210 return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
2213 LLVMValueRef LLVMBuildAShr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2215 return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
2218 LLVMValueRef LLVMBuildAnd(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2220 return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
2223 LLVMValueRef LLVMBuildOr(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2225 return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
2228 LLVMValueRef LLVMBuildXor(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS,
2230 return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
2233 LLVMValueRef LLVMBuildBinOp(LLVMBuilderRef B, LLVMOpcode Op,
2234 LLVMValueRef LHS, LLVMValueRef RHS,
2236 return wrap(unwrap(B)->CreateBinOp(Instruction::BinaryOps(map_from_llvmopcode(Op)), unwrap(LHS),
2237 unwrap(RHS), Name));
2240 LLVMValueRef LLVMBuildNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2241 return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
2244 LLVMValueRef LLVMBuildNSWNeg(LLVMBuilderRef B, LLVMValueRef V,
2246 return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
2249 LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
2251 return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
2254 LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2255 return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
2258 LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
2259 return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
2262 /*--.. Memory ..............................................................--*/
2264 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2266 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2267 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2268 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2269 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2270 ITy, unwrap(Ty), AllocSize,
2271 nullptr, nullptr, "");
2272 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2275 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
2276 LLVMValueRef Val, const char *Name) {
2277 Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
2278 Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
2279 AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
2280 Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
2281 ITy, unwrap(Ty), AllocSize,
2282 unwrap(Val), nullptr, "");
2283 return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
2286 LLVMValueRef LLVMBuildAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2288 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
2291 LLVMValueRef LLVMBuildArrayAlloca(LLVMBuilderRef B, LLVMTypeRef Ty,
2292 LLVMValueRef Val, const char *Name) {
2293 return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
2296 LLVMValueRef LLVMBuildFree(LLVMBuilderRef B, LLVMValueRef PointerVal) {
2297 return wrap(unwrap(B)->Insert(
2298 CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
2302 LLVMValueRef LLVMBuildLoad(LLVMBuilderRef B, LLVMValueRef PointerVal,
2304 return wrap(unwrap(B)->CreateLoad(unwrap(PointerVal), Name));
2307 LLVMValueRef LLVMBuildStore(LLVMBuilderRef B, LLVMValueRef Val,
2308 LLVMValueRef PointerVal) {
2309 return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
2312 static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) {
2314 case LLVMAtomicOrderingNotAtomic: return NotAtomic;
2315 case LLVMAtomicOrderingUnordered: return Unordered;
2316 case LLVMAtomicOrderingMonotonic: return Monotonic;
2317 case LLVMAtomicOrderingAcquire: return Acquire;
2318 case LLVMAtomicOrderingRelease: return Release;
2319 case LLVMAtomicOrderingAcquireRelease: return AcquireRelease;
2320 case LLVMAtomicOrderingSequentiallyConsistent:
2321 return SequentiallyConsistent;
2324 llvm_unreachable("Invalid LLVMAtomicOrdering value!");
2327 LLVMValueRef LLVMBuildFence(LLVMBuilderRef B, LLVMAtomicOrdering Ordering,
2328 LLVMBool isSingleThread, const char *Name) {
2330 unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
2331 isSingleThread ? SingleThread : CrossThread,
2335 LLVMValueRef LLVMBuildGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2336 LLVMValueRef *Indices, unsigned NumIndices,
2338 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2339 return wrap(unwrap(B)->CreateGEP(unwrap(Pointer), IdxList, Name));
2342 LLVMValueRef LLVMBuildInBoundsGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2343 LLVMValueRef *Indices, unsigned NumIndices,
2345 ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
2346 return wrap(unwrap(B)->CreateInBoundsGEP(unwrap(Pointer), IdxList, Name));
2349 LLVMValueRef LLVMBuildStructGEP(LLVMBuilderRef B, LLVMValueRef Pointer,
2350 unsigned Idx, const char *Name) {
2351 return wrap(unwrap(B)->CreateStructGEP(unwrap(Pointer), Idx, Name));
2354 LLVMValueRef LLVMBuildGlobalString(LLVMBuilderRef B, const char *Str,
2356 return wrap(unwrap(B)->CreateGlobalString(Str, Name));
2359 LLVMValueRef LLVMBuildGlobalStringPtr(LLVMBuilderRef B, const char *Str,
2361 return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
2364 LLVMBool LLVMGetVolatile(LLVMValueRef MemAccessInst) {
2365 Value *P = unwrap<Value>(MemAccessInst);
2366 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2367 return LI->isVolatile();
2368 return cast<StoreInst>(P)->isVolatile();
2371 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
2372 Value *P = unwrap<Value>(MemAccessInst);
2373 if (LoadInst *LI = dyn_cast<LoadInst>(P))
2374 return LI->setVolatile(isVolatile);
2375 return cast<StoreInst>(P)->setVolatile(isVolatile);
2378 /*--.. Casts ...............................................................--*/
2380 LLVMValueRef LLVMBuildTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2381 LLVMTypeRef DestTy, const char *Name) {
2382 return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
2385 LLVMValueRef LLVMBuildZExt(LLVMBuilderRef B, LLVMValueRef Val,
2386 LLVMTypeRef DestTy, const char *Name) {
2387 return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
2390 LLVMValueRef LLVMBuildSExt(LLVMBuilderRef B, LLVMValueRef Val,
2391 LLVMTypeRef DestTy, const char *Name) {
2392 return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
2395 LLVMValueRef LLVMBuildFPToUI(LLVMBuilderRef B, LLVMValueRef Val,
2396 LLVMTypeRef DestTy, const char *Name) {
2397 return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
2400 LLVMValueRef LLVMBuildFPToSI(LLVMBuilderRef B, LLVMValueRef Val,
2401 LLVMTypeRef DestTy, const char *Name) {
2402 return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
2405 LLVMValueRef LLVMBuildUIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2406 LLVMTypeRef DestTy, const char *Name) {
2407 return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
2410 LLVMValueRef LLVMBuildSIToFP(LLVMBuilderRef B, LLVMValueRef Val,
2411 LLVMTypeRef DestTy, const char *Name) {
2412 return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
2415 LLVMValueRef LLVMBuildFPTrunc(LLVMBuilderRef B, LLVMValueRef Val,
2416 LLVMTypeRef DestTy, const char *Name) {
2417 return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
2420 LLVMValueRef LLVMBuildFPExt(LLVMBuilderRef B, LLVMValueRef Val,
2421 LLVMTypeRef DestTy, const char *Name) {
2422 return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
2425 LLVMValueRef LLVMBuildPtrToInt(LLVMBuilderRef B, LLVMValueRef Val,
2426 LLVMTypeRef DestTy, const char *Name) {
2427 return wrap(unwrap(B)->CreatePtrToInt(unwrap(Val), unwrap(DestTy), Name));
2430 LLVMValueRef LLVMBuildIntToPtr(LLVMBuilderRef B, LLVMValueRef Val,
2431 LLVMTypeRef DestTy, const char *Name) {
2432 return wrap(unwrap(B)->CreateIntToPtr(unwrap(Val), unwrap(DestTy), Name));
2435 LLVMValueRef LLVMBuildBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2436 LLVMTypeRef DestTy, const char *Name) {
2437 return wrap(unwrap(B)->CreateBitCast(unwrap(Val), unwrap(DestTy), Name));
2440 LLVMValueRef LLVMBuildAddrSpaceCast(LLVMBuilderRef B, LLVMValueRef Val,
2441 LLVMTypeRef DestTy, const char *Name) {
2442 return wrap(unwrap(B)->CreateAddrSpaceCast(unwrap(Val), unwrap(DestTy), Name));
2445 LLVMValueRef LLVMBuildZExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2446 LLVMTypeRef DestTy, const char *Name) {
2447 return wrap(unwrap(B)->CreateZExtOrBitCast(unwrap(Val), unwrap(DestTy),
2451 LLVMValueRef LLVMBuildSExtOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2452 LLVMTypeRef DestTy, const char *Name) {
2453 return wrap(unwrap(B)->CreateSExtOrBitCast(unwrap(Val), unwrap(DestTy),
2457 LLVMValueRef LLVMBuildTruncOrBitCast(LLVMBuilderRef B, LLVMValueRef Val,
2458 LLVMTypeRef DestTy, const char *Name) {
2459 return wrap(unwrap(B)->CreateTruncOrBitCast(unwrap(Val), unwrap(DestTy),
2463 LLVMValueRef LLVMBuildCast(LLVMBuilderRef B, LLVMOpcode Op, LLVMValueRef Val,
2464 LLVMTypeRef DestTy, const char *Name) {
2465 return wrap(unwrap(B)->CreateCast(Instruction::CastOps(map_from_llvmopcode(Op)), unwrap(Val),
2466 unwrap(DestTy), Name));
2469 LLVMValueRef LLVMBuildPointerCast(LLVMBuilderRef B, LLVMValueRef Val,
2470 LLVMTypeRef DestTy, const char *Name) {
2471 return wrap(unwrap(B)->CreatePointerCast(unwrap(Val), unwrap(DestTy), Name));
2474 LLVMValueRef LLVMBuildIntCast(LLVMBuilderRef B, LLVMValueRef Val,
2475 LLVMTypeRef DestTy, const char *Name) {
2476 return wrap(unwrap(B)->CreateIntCast(unwrap(Val), unwrap(DestTy),
2477 /*isSigned*/true, Name));
2480 LLVMValueRef LLVMBuildFPCast(LLVMBuilderRef B, LLVMValueRef Val,
2481 LLVMTypeRef DestTy, const char *Name) {
2482 return wrap(unwrap(B)->CreateFPCast(unwrap(Val), unwrap(DestTy), Name));
2485 /*--.. Comparisons .........................................................--*/
2487 LLVMValueRef LLVMBuildICmp(LLVMBuilderRef B, LLVMIntPredicate Op,
2488 LLVMValueRef LHS, LLVMValueRef RHS,
2490 return wrap(unwrap(B)->CreateICmp(static_cast<ICmpInst::Predicate>(Op),
2491 unwrap(LHS), unwrap(RHS), Name));
2494 LLVMValueRef LLVMBuildFCmp(LLVMBuilderRef B, LLVMRealPredicate Op,
2495 LLVMValueRef LHS, LLVMValueRef RHS,
2497 return wrap(unwrap(B)->CreateFCmp(static_cast<FCmpInst::Predicate>(Op),
2498 unwrap(LHS), unwrap(RHS), Name));
2501 /*--.. Miscellaneous instructions ..........................................--*/
2503 LLVMValueRef LLVMBuildPhi(LLVMBuilderRef B, LLVMTypeRef Ty, const char *Name) {
2504 return wrap(unwrap(B)->CreatePHI(unwrap(Ty), 0, Name));
2507 LLVMValueRef LLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn,
2508 LLVMValueRef *Args, unsigned NumArgs,
2510 return wrap(unwrap(B)->CreateCall(unwrap(Fn),
2511 makeArrayRef(unwrap(Args), NumArgs),
2515 LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If,
2516 LLVMValueRef Then, LLVMValueRef Else,
2518 return wrap(unwrap(B)->CreateSelect(unwrap(If), unwrap(Then), unwrap(Else),
2522 LLVMValueRef LLVMBuildVAArg(LLVMBuilderRef B, LLVMValueRef List,
2523 LLVMTypeRef Ty, const char *Name) {
2524 return wrap(unwrap(B)->CreateVAArg(unwrap(List), unwrap(Ty), Name));
2527 LLVMValueRef LLVMBuildExtractElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2528 LLVMValueRef Index, const char *Name) {
2529 return wrap(unwrap(B)->CreateExtractElement(unwrap(VecVal), unwrap(Index),
2533 LLVMValueRef LLVMBuildInsertElement(LLVMBuilderRef B, LLVMValueRef VecVal,
2534 LLVMValueRef EltVal, LLVMValueRef Index,
2536 return wrap(unwrap(B)->CreateInsertElement(unwrap(VecVal), unwrap(EltVal),
2537 unwrap(Index), Name));
2540 LLVMValueRef LLVMBuildShuffleVector(LLVMBuilderRef B, LLVMValueRef V1,
2541 LLVMValueRef V2, LLVMValueRef Mask,
2543 return wrap(unwrap(B)->CreateShuffleVector(unwrap(V1), unwrap(V2),
2544 unwrap(Mask), Name));
2547 LLVMValueRef LLVMBuildExtractValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2548 unsigned Index, const char *Name) {
2549 return wrap(unwrap(B)->CreateExtractValue(unwrap(AggVal), Index, Name));
2552 LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef B, LLVMValueRef AggVal,
2553 LLVMValueRef EltVal, unsigned Index,
2555 return wrap(unwrap(B)->CreateInsertValue(unwrap(AggVal), unwrap(EltVal),
2559 LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val,
2561 return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name));
2564 LLVMValueRef LLVMBuildIsNotNull(LLVMBuilderRef B, LLVMValueRef Val,
2566 return wrap(unwrap(B)->CreateIsNotNull(unwrap(Val), Name));
2569 LLVMValueRef LLVMBuildPtrDiff(LLVMBuilderRef B, LLVMValueRef LHS,
2570 LLVMValueRef RHS, const char *Name) {
2571 return wrap(unwrap(B)->CreatePtrDiff(unwrap(LHS), unwrap(RHS), Name));
2574 LLVMValueRef LLVMBuildAtomicRMW(LLVMBuilderRef B,LLVMAtomicRMWBinOp op,
2575 LLVMValueRef PTR, LLVMValueRef Val,
2576 LLVMAtomicOrdering ordering,
2577 LLVMBool singleThread) {
2578 AtomicRMWInst::BinOp intop;
2580 case LLVMAtomicRMWBinOpXchg: intop = AtomicRMWInst::Xchg; break;
2581 case LLVMAtomicRMWBinOpAdd: intop = AtomicRMWInst::Add; break;
2582 case LLVMAtomicRMWBinOpSub: intop = AtomicRMWInst::Sub; break;
2583 case LLVMAtomicRMWBinOpAnd: intop = AtomicRMWInst::And; break;
2584 case LLVMAtomicRMWBinOpNand: intop = AtomicRMWInst::Nand; break;
2585 case LLVMAtomicRMWBinOpOr: intop = AtomicRMWInst::Or; break;
2586 case LLVMAtomicRMWBinOpXor: intop = AtomicRMWInst::Xor; break;
2587 case LLVMAtomicRMWBinOpMax: intop = AtomicRMWInst::Max; break;
2588 case LLVMAtomicRMWBinOpMin: intop = AtomicRMWInst::Min; break;
2589 case LLVMAtomicRMWBinOpUMax: intop = AtomicRMWInst::UMax; break;
2590 case LLVMAtomicRMWBinOpUMin: intop = AtomicRMWInst::UMin; break;
2592 return wrap(unwrap(B)->CreateAtomicRMW(intop, unwrap(PTR), unwrap(Val),
2593 mapFromLLVMOrdering(ordering), singleThread ? SingleThread : CrossThread));
2597 /*===-- Module providers --------------------------------------------------===*/
2599 LLVMModuleProviderRef
2600 LLVMCreateModuleProviderForExistingModule(LLVMModuleRef M) {
2601 return reinterpret_cast<LLVMModuleProviderRef>(M);
2604 void LLVMDisposeModuleProvider(LLVMModuleProviderRef MP) {
2609 /*===-- Memory buffers ----------------------------------------------------===*/
2611 LLVMBool LLVMCreateMemoryBufferWithContentsOfFile(
2613 LLVMMemoryBufferRef *OutMemBuf,
2614 char **OutMessage) {
2616 std::unique_ptr<MemoryBuffer> MB;
2618 if (!(ec = MemoryBuffer::getFile(Path, MB))) {
2619 *OutMemBuf = wrap(MB.release());
2623 *OutMessage = strdup(ec.message().c_str());
2627 LLVMBool LLVMCreateMemoryBufferWithSTDIN(LLVMMemoryBufferRef *OutMemBuf,
2628 char **OutMessage) {
2629 std::unique_ptr<MemoryBuffer> MB;
2631 if (!(ec = MemoryBuffer::getSTDIN(MB))) {
2632 *OutMemBuf = wrap(MB.release());
2636 *OutMessage = strdup(ec.message().c_str());
2640 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRange(
2641 const char *InputData,
2642 size_t InputDataLength,
2643 const char *BufferName,
2644 LLVMBool RequiresNullTerminator) {
2646 return wrap(MemoryBuffer::getMemBuffer(
2647 StringRef(InputData, InputDataLength),
2648 StringRef(BufferName),
2649 RequiresNullTerminator));
2652 LLVMMemoryBufferRef LLVMCreateMemoryBufferWithMemoryRangeCopy(
2653 const char *InputData,
2654 size_t InputDataLength,
2655 const char *BufferName) {
2657 return wrap(MemoryBuffer::getMemBufferCopy(
2658 StringRef(InputData, InputDataLength),
2659 StringRef(BufferName)));
2662 const char *LLVMGetBufferStart(LLVMMemoryBufferRef MemBuf) {
2663 return unwrap(MemBuf)->getBufferStart();
2666 size_t LLVMGetBufferSize(LLVMMemoryBufferRef MemBuf) {
2667 return unwrap(MemBuf)->getBufferSize();
2670 void LLVMDisposeMemoryBuffer(LLVMMemoryBufferRef MemBuf) {
2671 delete unwrap(MemBuf);
2674 /*===-- Pass -------------------------------------------------------------===*/
2676 const char *LLVMGetPassName(LLVMPassRef P) {
2677 return unwrap(P)->getPassName();
2680 /*===-- Pass Registry -----------------------------------------------------===*/
2682 LLVMPassRegistryRef LLVMGetGlobalPassRegistry(void) {
2683 return wrap(PassRegistry::getPassRegistry());
2686 /*===-- Pass Manager ------------------------------------------------------===*/
2688 LLVMPassManagerRef LLVMCreatePassManager() {
2689 return wrap(new PassManager());
2692 LLVMPassManagerRef LLVMCreateFunctionPassManagerForModule(LLVMModuleRef M) {
2693 return wrap(new FunctionPassManager(unwrap(M)));
2696 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
2697 return LLVMCreateFunctionPassManagerForModule(
2698 reinterpret_cast<LLVMModuleRef>(P));
2701 LLVMBool LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
2702 return unwrap<PassManager>(PM)->run(*unwrap(M));
2705 LLVMBool LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
2706 return unwrap<FunctionPassManager>(FPM)->doInitialization();
2709 LLVMBool LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
2710 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
2713 LLVMBool LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
2714 return unwrap<FunctionPassManager>(FPM)->doFinalization();
2717 void LLVMDisposePassManager(LLVMPassManagerRef PM) {
2721 /*===-- Threading ------------------------------------------------------===*/
2723 LLVMBool LLVMStartMultithreaded() {
2724 return llvm_start_multithreaded();
2727 void LLVMStopMultithreaded() {
2728 llvm_stop_multithreaded();
2731 LLVMBool LLVMIsMultithreaded() {
2732 return llvm_is_multithreaded();